synclink.c 231 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166
  1. /*
  2. * linux/drivers/char/synclink.c
  3. *
  4. * $Id: synclink.c,v 4.38 2005/11/07 16:30:34 paulkf Exp $
  5. *
  6. * Device driver for Microgate SyncLink ISA and PCI
  7. * high speed multiprotocol serial adapters.
  8. *
  9. * written by Paul Fulghum for Microgate Corporation
  10. * paulkf@microgate.com
  11. *
  12. * Microgate and SyncLink are trademarks of Microgate Corporation
  13. *
  14. * Derived from serial.c written by Theodore Ts'o and Linus Torvalds
  15. *
  16. * Original release 01/11/99
  17. *
  18. * This code is released under the GNU General Public License (GPL)
  19. *
  20. * This driver is primarily intended for use in synchronous
  21. * HDLC mode. Asynchronous mode is also provided.
  22. *
  23. * When operating in synchronous mode, each call to mgsl_write()
  24. * contains exactly one complete HDLC frame. Calling mgsl_put_char
  25. * will start assembling an HDLC frame that will not be sent until
  26. * mgsl_flush_chars or mgsl_write is called.
  27. *
  28. * Synchronous receive data is reported as complete frames. To accomplish
  29. * this, the TTY flip buffer is bypassed (too small to hold largest
  30. * frame and may fragment frames) and the line discipline
  31. * receive entry point is called directly.
  32. *
  33. * This driver has been tested with a slightly modified ppp.c driver
  34. * for synchronous PPP.
  35. *
  36. * 2000/02/16
  37. * Added interface for syncppp.c driver (an alternate synchronous PPP
  38. * implementation that also supports Cisco HDLC). Each device instance
  39. * registers as a tty device AND a network device (if dosyncppp option
  40. * is set for the device). The functionality is determined by which
  41. * device interface is opened.
  42. *
  43. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  44. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  45. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  46. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  47. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  48. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  49. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  50. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  51. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  52. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  53. * OF THE POSSIBILITY OF SUCH DAMAGE.
  54. */
  55. #if defined(__i386__)
  56. # define BREAKPOINT() asm(" int $3");
  57. #else
  58. # define BREAKPOINT() { }
  59. #endif
  60. #define MAX_ISA_DEVICES 10
  61. #define MAX_PCI_DEVICES 10
  62. #define MAX_TOTAL_DEVICES 20
  63. #include <linux/module.h>
  64. #include <linux/errno.h>
  65. #include <linux/signal.h>
  66. #include <linux/sched.h>
  67. #include <linux/timer.h>
  68. #include <linux/interrupt.h>
  69. #include <linux/pci.h>
  70. #include <linux/tty.h>
  71. #include <linux/tty_flip.h>
  72. #include <linux/serial.h>
  73. #include <linux/major.h>
  74. #include <linux/string.h>
  75. #include <linux/fcntl.h>
  76. #include <linux/ptrace.h>
  77. #include <linux/ioport.h>
  78. #include <linux/mm.h>
  79. #include <linux/slab.h>
  80. #include <linux/delay.h>
  81. #include <linux/netdevice.h>
  82. #include <linux/vmalloc.h>
  83. #include <linux/init.h>
  84. #include <linux/ioctl.h>
  85. #include <linux/synclink.h>
  86. #include <asm/system.h>
  87. #include <asm/io.h>
  88. #include <asm/irq.h>
  89. #include <asm/dma.h>
  90. #include <linux/bitops.h>
  91. #include <asm/types.h>
  92. #include <linux/termios.h>
  93. #include <linux/workqueue.h>
  94. #include <linux/hdlc.h>
  95. #include <linux/dma-mapping.h>
  96. #if defined(CONFIG_HDLC) || (defined(CONFIG_HDLC_MODULE) && defined(CONFIG_SYNCLINK_MODULE))
  97. #define SYNCLINK_GENERIC_HDLC 1
  98. #else
  99. #define SYNCLINK_GENERIC_HDLC 0
  100. #endif
  101. #define GET_USER(error,value,addr) error = get_user(value,addr)
  102. #define COPY_FROM_USER(error,dest,src,size) error = copy_from_user(dest,src,size) ? -EFAULT : 0
  103. #define PUT_USER(error,value,addr) error = put_user(value,addr)
  104. #define COPY_TO_USER(error,dest,src,size) error = copy_to_user(dest,src,size) ? -EFAULT : 0
  105. #include <asm/uaccess.h>
  106. #define RCLRVALUE 0xffff
  107. static MGSL_PARAMS default_params = {
  108. MGSL_MODE_HDLC, /* unsigned long mode */
  109. 0, /* unsigned char loopback; */
  110. HDLC_FLAG_UNDERRUN_ABORT15, /* unsigned short flags; */
  111. HDLC_ENCODING_NRZI_SPACE, /* unsigned char encoding; */
  112. 0, /* unsigned long clock_speed; */
  113. 0xff, /* unsigned char addr_filter; */
  114. HDLC_CRC_16_CCITT, /* unsigned short crc_type; */
  115. HDLC_PREAMBLE_LENGTH_8BITS, /* unsigned char preamble_length; */
  116. HDLC_PREAMBLE_PATTERN_NONE, /* unsigned char preamble; */
  117. 9600, /* unsigned long data_rate; */
  118. 8, /* unsigned char data_bits; */
  119. 1, /* unsigned char stop_bits; */
  120. ASYNC_PARITY_NONE /* unsigned char parity; */
  121. };
  122. #define SHARED_MEM_ADDRESS_SIZE 0x40000
  123. #define BUFFERLISTSIZE 4096
  124. #define DMABUFFERSIZE 4096
  125. #define MAXRXFRAMES 7
  126. typedef struct _DMABUFFERENTRY
  127. {
  128. u32 phys_addr; /* 32-bit flat physical address of data buffer */
  129. volatile u16 count; /* buffer size/data count */
  130. volatile u16 status; /* Control/status field */
  131. volatile u16 rcc; /* character count field */
  132. u16 reserved; /* padding required by 16C32 */
  133. u32 link; /* 32-bit flat link to next buffer entry */
  134. char *virt_addr; /* virtual address of data buffer */
  135. u32 phys_entry; /* physical address of this buffer entry */
  136. dma_addr_t dma_addr;
  137. } DMABUFFERENTRY, *DMAPBUFFERENTRY;
  138. /* The queue of BH actions to be performed */
  139. #define BH_RECEIVE 1
  140. #define BH_TRANSMIT 2
  141. #define BH_STATUS 4
  142. #define IO_PIN_SHUTDOWN_LIMIT 100
  143. struct _input_signal_events {
  144. int ri_up;
  145. int ri_down;
  146. int dsr_up;
  147. int dsr_down;
  148. int dcd_up;
  149. int dcd_down;
  150. int cts_up;
  151. int cts_down;
  152. };
  153. /* transmit holding buffer definitions*/
  154. #define MAX_TX_HOLDING_BUFFERS 5
  155. struct tx_holding_buffer {
  156. int buffer_size;
  157. unsigned char * buffer;
  158. };
  159. /*
  160. * Device instance data structure
  161. */
  162. struct mgsl_struct {
  163. int magic;
  164. int flags;
  165. int count; /* count of opens */
  166. int line;
  167. int hw_version;
  168. unsigned short close_delay;
  169. unsigned short closing_wait; /* time to wait before closing */
  170. struct mgsl_icount icount;
  171. struct tty_struct *tty;
  172. int timeout;
  173. int x_char; /* xon/xoff character */
  174. int blocked_open; /* # of blocked opens */
  175. u16 read_status_mask;
  176. u16 ignore_status_mask;
  177. unsigned char *xmit_buf;
  178. int xmit_head;
  179. int xmit_tail;
  180. int xmit_cnt;
  181. wait_queue_head_t open_wait;
  182. wait_queue_head_t close_wait;
  183. wait_queue_head_t status_event_wait_q;
  184. wait_queue_head_t event_wait_q;
  185. struct timer_list tx_timer; /* HDLC transmit timeout timer */
  186. struct mgsl_struct *next_device; /* device list link */
  187. spinlock_t irq_spinlock; /* spinlock for synchronizing with ISR */
  188. struct work_struct task; /* task structure for scheduling bh */
  189. u32 EventMask; /* event trigger mask */
  190. u32 RecordedEvents; /* pending events */
  191. u32 max_frame_size; /* as set by device config */
  192. u32 pending_bh;
  193. bool bh_running; /* Protection from multiple */
  194. int isr_overflow;
  195. bool bh_requested;
  196. int dcd_chkcount; /* check counts to prevent */
  197. int cts_chkcount; /* too many IRQs if a signal */
  198. int dsr_chkcount; /* is floating */
  199. int ri_chkcount;
  200. char *buffer_list; /* virtual address of Rx & Tx buffer lists */
  201. u32 buffer_list_phys;
  202. dma_addr_t buffer_list_dma_addr;
  203. unsigned int rx_buffer_count; /* count of total allocated Rx buffers */
  204. DMABUFFERENTRY *rx_buffer_list; /* list of receive buffer entries */
  205. unsigned int current_rx_buffer;
  206. int num_tx_dma_buffers; /* number of tx dma frames required */
  207. int tx_dma_buffers_used;
  208. unsigned int tx_buffer_count; /* count of total allocated Tx buffers */
  209. DMABUFFERENTRY *tx_buffer_list; /* list of transmit buffer entries */
  210. int start_tx_dma_buffer; /* tx dma buffer to start tx dma operation */
  211. int current_tx_buffer; /* next tx dma buffer to be loaded */
  212. unsigned char *intermediate_rxbuffer;
  213. int num_tx_holding_buffers; /* number of tx holding buffer allocated */
  214. int get_tx_holding_index; /* next tx holding buffer for adapter to load */
  215. int put_tx_holding_index; /* next tx holding buffer to store user request */
  216. int tx_holding_count; /* number of tx holding buffers waiting */
  217. struct tx_holding_buffer tx_holding_buffers[MAX_TX_HOLDING_BUFFERS];
  218. bool rx_enabled;
  219. bool rx_overflow;
  220. bool rx_rcc_underrun;
  221. bool tx_enabled;
  222. bool tx_active;
  223. u32 idle_mode;
  224. u16 cmr_value;
  225. u16 tcsr_value;
  226. char device_name[25]; /* device instance name */
  227. unsigned int bus_type; /* expansion bus type (ISA,EISA,PCI) */
  228. unsigned char bus; /* expansion bus number (zero based) */
  229. unsigned char function; /* PCI device number */
  230. unsigned int io_base; /* base I/O address of adapter */
  231. unsigned int io_addr_size; /* size of the I/O address range */
  232. bool io_addr_requested; /* true if I/O address requested */
  233. unsigned int irq_level; /* interrupt level */
  234. unsigned long irq_flags;
  235. bool irq_requested; /* true if IRQ requested */
  236. unsigned int dma_level; /* DMA channel */
  237. bool dma_requested; /* true if dma channel requested */
  238. u16 mbre_bit;
  239. u16 loopback_bits;
  240. u16 usc_idle_mode;
  241. MGSL_PARAMS params; /* communications parameters */
  242. unsigned char serial_signals; /* current serial signal states */
  243. bool irq_occurred; /* for diagnostics use */
  244. unsigned int init_error; /* Initialization startup error (DIAGS) */
  245. int fDiagnosticsmode; /* Driver in Diagnostic mode? (DIAGS) */
  246. u32 last_mem_alloc;
  247. unsigned char* memory_base; /* shared memory address (PCI only) */
  248. u32 phys_memory_base;
  249. bool shared_mem_requested;
  250. unsigned char* lcr_base; /* local config registers (PCI only) */
  251. u32 phys_lcr_base;
  252. u32 lcr_offset;
  253. bool lcr_mem_requested;
  254. u32 misc_ctrl_value;
  255. char flag_buf[MAX_ASYNC_BUFFER_SIZE];
  256. char char_buf[MAX_ASYNC_BUFFER_SIZE];
  257. bool drop_rts_on_tx_done;
  258. bool loopmode_insert_requested;
  259. bool loopmode_send_done_requested;
  260. struct _input_signal_events input_signal_events;
  261. /* generic HDLC device parts */
  262. int netcount;
  263. int dosyncppp;
  264. spinlock_t netlock;
  265. #if SYNCLINK_GENERIC_HDLC
  266. struct net_device *netdev;
  267. #endif
  268. };
  269. #define MGSL_MAGIC 0x5401
  270. /*
  271. * The size of the serial xmit buffer is 1 page, or 4096 bytes
  272. */
  273. #ifndef SERIAL_XMIT_SIZE
  274. #define SERIAL_XMIT_SIZE 4096
  275. #endif
  276. /*
  277. * These macros define the offsets used in calculating the
  278. * I/O address of the specified USC registers.
  279. */
  280. #define DCPIN 2 /* Bit 1 of I/O address */
  281. #define SDPIN 4 /* Bit 2 of I/O address */
  282. #define DCAR 0 /* DMA command/address register */
  283. #define CCAR SDPIN /* channel command/address register */
  284. #define DATAREG DCPIN + SDPIN /* serial data register */
  285. #define MSBONLY 0x41
  286. #define LSBONLY 0x40
  287. /*
  288. * These macros define the register address (ordinal number)
  289. * used for writing address/value pairs to the USC.
  290. */
  291. #define CMR 0x02 /* Channel mode Register */
  292. #define CCSR 0x04 /* Channel Command/status Register */
  293. #define CCR 0x06 /* Channel Control Register */
  294. #define PSR 0x08 /* Port status Register */
  295. #define PCR 0x0a /* Port Control Register */
  296. #define TMDR 0x0c /* Test mode Data Register */
  297. #define TMCR 0x0e /* Test mode Control Register */
  298. #define CMCR 0x10 /* Clock mode Control Register */
  299. #define HCR 0x12 /* Hardware Configuration Register */
  300. #define IVR 0x14 /* Interrupt Vector Register */
  301. #define IOCR 0x16 /* Input/Output Control Register */
  302. #define ICR 0x18 /* Interrupt Control Register */
  303. #define DCCR 0x1a /* Daisy Chain Control Register */
  304. #define MISR 0x1c /* Misc Interrupt status Register */
  305. #define SICR 0x1e /* status Interrupt Control Register */
  306. #define RDR 0x20 /* Receive Data Register */
  307. #define RMR 0x22 /* Receive mode Register */
  308. #define RCSR 0x24 /* Receive Command/status Register */
  309. #define RICR 0x26 /* Receive Interrupt Control Register */
  310. #define RSR 0x28 /* Receive Sync Register */
  311. #define RCLR 0x2a /* Receive count Limit Register */
  312. #define RCCR 0x2c /* Receive Character count Register */
  313. #define TC0R 0x2e /* Time Constant 0 Register */
  314. #define TDR 0x30 /* Transmit Data Register */
  315. #define TMR 0x32 /* Transmit mode Register */
  316. #define TCSR 0x34 /* Transmit Command/status Register */
  317. #define TICR 0x36 /* Transmit Interrupt Control Register */
  318. #define TSR 0x38 /* Transmit Sync Register */
  319. #define TCLR 0x3a /* Transmit count Limit Register */
  320. #define TCCR 0x3c /* Transmit Character count Register */
  321. #define TC1R 0x3e /* Time Constant 1 Register */
  322. /*
  323. * MACRO DEFINITIONS FOR DMA REGISTERS
  324. */
  325. #define DCR 0x06 /* DMA Control Register (shared) */
  326. #define DACR 0x08 /* DMA Array count Register (shared) */
  327. #define BDCR 0x12 /* Burst/Dwell Control Register (shared) */
  328. #define DIVR 0x14 /* DMA Interrupt Vector Register (shared) */
  329. #define DICR 0x18 /* DMA Interrupt Control Register (shared) */
  330. #define CDIR 0x1a /* Clear DMA Interrupt Register (shared) */
  331. #define SDIR 0x1c /* Set DMA Interrupt Register (shared) */
  332. #define TDMR 0x02 /* Transmit DMA mode Register */
  333. #define TDIAR 0x1e /* Transmit DMA Interrupt Arm Register */
  334. #define TBCR 0x2a /* Transmit Byte count Register */
  335. #define TARL 0x2c /* Transmit Address Register (low) */
  336. #define TARU 0x2e /* Transmit Address Register (high) */
  337. #define NTBCR 0x3a /* Next Transmit Byte count Register */
  338. #define NTARL 0x3c /* Next Transmit Address Register (low) */
  339. #define NTARU 0x3e /* Next Transmit Address Register (high) */
  340. #define RDMR 0x82 /* Receive DMA mode Register (non-shared) */
  341. #define RDIAR 0x9e /* Receive DMA Interrupt Arm Register */
  342. #define RBCR 0xaa /* Receive Byte count Register */
  343. #define RARL 0xac /* Receive Address Register (low) */
  344. #define RARU 0xae /* Receive Address Register (high) */
  345. #define NRBCR 0xba /* Next Receive Byte count Register */
  346. #define NRARL 0xbc /* Next Receive Address Register (low) */
  347. #define NRARU 0xbe /* Next Receive Address Register (high) */
  348. /*
  349. * MACRO DEFINITIONS FOR MODEM STATUS BITS
  350. */
  351. #define MODEMSTATUS_DTR 0x80
  352. #define MODEMSTATUS_DSR 0x40
  353. #define MODEMSTATUS_RTS 0x20
  354. #define MODEMSTATUS_CTS 0x10
  355. #define MODEMSTATUS_RI 0x04
  356. #define MODEMSTATUS_DCD 0x01
  357. /*
  358. * Channel Command/Address Register (CCAR) Command Codes
  359. */
  360. #define RTCmd_Null 0x0000
  361. #define RTCmd_ResetHighestIus 0x1000
  362. #define RTCmd_TriggerChannelLoadDma 0x2000
  363. #define RTCmd_TriggerRxDma 0x2800
  364. #define RTCmd_TriggerTxDma 0x3000
  365. #define RTCmd_TriggerRxAndTxDma 0x3800
  366. #define RTCmd_PurgeRxFifo 0x4800
  367. #define RTCmd_PurgeTxFifo 0x5000
  368. #define RTCmd_PurgeRxAndTxFifo 0x5800
  369. #define RTCmd_LoadRcc 0x6800
  370. #define RTCmd_LoadTcc 0x7000
  371. #define RTCmd_LoadRccAndTcc 0x7800
  372. #define RTCmd_LoadTC0 0x8800
  373. #define RTCmd_LoadTC1 0x9000
  374. #define RTCmd_LoadTC0AndTC1 0x9800
  375. #define RTCmd_SerialDataLSBFirst 0xa000
  376. #define RTCmd_SerialDataMSBFirst 0xa800
  377. #define RTCmd_SelectBigEndian 0xb000
  378. #define RTCmd_SelectLittleEndian 0xb800
  379. /*
  380. * DMA Command/Address Register (DCAR) Command Codes
  381. */
  382. #define DmaCmd_Null 0x0000
  383. #define DmaCmd_ResetTxChannel 0x1000
  384. #define DmaCmd_ResetRxChannel 0x1200
  385. #define DmaCmd_StartTxChannel 0x2000
  386. #define DmaCmd_StartRxChannel 0x2200
  387. #define DmaCmd_ContinueTxChannel 0x3000
  388. #define DmaCmd_ContinueRxChannel 0x3200
  389. #define DmaCmd_PauseTxChannel 0x4000
  390. #define DmaCmd_PauseRxChannel 0x4200
  391. #define DmaCmd_AbortTxChannel 0x5000
  392. #define DmaCmd_AbortRxChannel 0x5200
  393. #define DmaCmd_InitTxChannel 0x7000
  394. #define DmaCmd_InitRxChannel 0x7200
  395. #define DmaCmd_ResetHighestDmaIus 0x8000
  396. #define DmaCmd_ResetAllChannels 0x9000
  397. #define DmaCmd_StartAllChannels 0xa000
  398. #define DmaCmd_ContinueAllChannels 0xb000
  399. #define DmaCmd_PauseAllChannels 0xc000
  400. #define DmaCmd_AbortAllChannels 0xd000
  401. #define DmaCmd_InitAllChannels 0xf000
  402. #define TCmd_Null 0x0000
  403. #define TCmd_ClearTxCRC 0x2000
  404. #define TCmd_SelectTicrTtsaData 0x4000
  405. #define TCmd_SelectTicrTxFifostatus 0x5000
  406. #define TCmd_SelectTicrIntLevel 0x6000
  407. #define TCmd_SelectTicrdma_level 0x7000
  408. #define TCmd_SendFrame 0x8000
  409. #define TCmd_SendAbort 0x9000
  410. #define TCmd_EnableDleInsertion 0xc000
  411. #define TCmd_DisableDleInsertion 0xd000
  412. #define TCmd_ClearEofEom 0xe000
  413. #define TCmd_SetEofEom 0xf000
  414. #define RCmd_Null 0x0000
  415. #define RCmd_ClearRxCRC 0x2000
  416. #define RCmd_EnterHuntmode 0x3000
  417. #define RCmd_SelectRicrRtsaData 0x4000
  418. #define RCmd_SelectRicrRxFifostatus 0x5000
  419. #define RCmd_SelectRicrIntLevel 0x6000
  420. #define RCmd_SelectRicrdma_level 0x7000
  421. /*
  422. * Bits for enabling and disabling IRQs in Interrupt Control Register (ICR)
  423. */
  424. #define RECEIVE_STATUS BIT5
  425. #define RECEIVE_DATA BIT4
  426. #define TRANSMIT_STATUS BIT3
  427. #define TRANSMIT_DATA BIT2
  428. #define IO_PIN BIT1
  429. #define MISC BIT0
  430. /*
  431. * Receive status Bits in Receive Command/status Register RCSR
  432. */
  433. #define RXSTATUS_SHORT_FRAME BIT8
  434. #define RXSTATUS_CODE_VIOLATION BIT8
  435. #define RXSTATUS_EXITED_HUNT BIT7
  436. #define RXSTATUS_IDLE_RECEIVED BIT6
  437. #define RXSTATUS_BREAK_RECEIVED BIT5
  438. #define RXSTATUS_ABORT_RECEIVED BIT5
  439. #define RXSTATUS_RXBOUND BIT4
  440. #define RXSTATUS_CRC_ERROR BIT3
  441. #define RXSTATUS_FRAMING_ERROR BIT3
  442. #define RXSTATUS_ABORT BIT2
  443. #define RXSTATUS_PARITY_ERROR BIT2
  444. #define RXSTATUS_OVERRUN BIT1
  445. #define RXSTATUS_DATA_AVAILABLE BIT0
  446. #define RXSTATUS_ALL 0x01f6
  447. #define usc_UnlatchRxstatusBits(a,b) usc_OutReg( (a), RCSR, (u16)((b) & RXSTATUS_ALL) )
  448. /*
  449. * Values for setting transmit idle mode in
  450. * Transmit Control/status Register (TCSR)
  451. */
  452. #define IDLEMODE_FLAGS 0x0000
  453. #define IDLEMODE_ALT_ONE_ZERO 0x0100
  454. #define IDLEMODE_ZERO 0x0200
  455. #define IDLEMODE_ONE 0x0300
  456. #define IDLEMODE_ALT_MARK_SPACE 0x0500
  457. #define IDLEMODE_SPACE 0x0600
  458. #define IDLEMODE_MARK 0x0700
  459. #define IDLEMODE_MASK 0x0700
  460. /*
  461. * IUSC revision identifiers
  462. */
  463. #define IUSC_SL1660 0x4d44
  464. #define IUSC_PRE_SL1660 0x4553
  465. /*
  466. * Transmit status Bits in Transmit Command/status Register (TCSR)
  467. */
  468. #define TCSR_PRESERVE 0x0F00
  469. #define TCSR_UNDERWAIT BIT11
  470. #define TXSTATUS_PREAMBLE_SENT BIT7
  471. #define TXSTATUS_IDLE_SENT BIT6
  472. #define TXSTATUS_ABORT_SENT BIT5
  473. #define TXSTATUS_EOF_SENT BIT4
  474. #define TXSTATUS_EOM_SENT BIT4
  475. #define TXSTATUS_CRC_SENT BIT3
  476. #define TXSTATUS_ALL_SENT BIT2
  477. #define TXSTATUS_UNDERRUN BIT1
  478. #define TXSTATUS_FIFO_EMPTY BIT0
  479. #define TXSTATUS_ALL 0x00fa
  480. #define usc_UnlatchTxstatusBits(a,b) usc_OutReg( (a), TCSR, (u16)((a)->tcsr_value + ((b) & 0x00FF)) )
  481. #define MISCSTATUS_RXC_LATCHED BIT15
  482. #define MISCSTATUS_RXC BIT14
  483. #define MISCSTATUS_TXC_LATCHED BIT13
  484. #define MISCSTATUS_TXC BIT12
  485. #define MISCSTATUS_RI_LATCHED BIT11
  486. #define MISCSTATUS_RI BIT10
  487. #define MISCSTATUS_DSR_LATCHED BIT9
  488. #define MISCSTATUS_DSR BIT8
  489. #define MISCSTATUS_DCD_LATCHED BIT7
  490. #define MISCSTATUS_DCD BIT6
  491. #define MISCSTATUS_CTS_LATCHED BIT5
  492. #define MISCSTATUS_CTS BIT4
  493. #define MISCSTATUS_RCC_UNDERRUN BIT3
  494. #define MISCSTATUS_DPLL_NO_SYNC BIT2
  495. #define MISCSTATUS_BRG1_ZERO BIT1
  496. #define MISCSTATUS_BRG0_ZERO BIT0
  497. #define usc_UnlatchIostatusBits(a,b) usc_OutReg((a),MISR,(u16)((b) & 0xaaa0))
  498. #define usc_UnlatchMiscstatusBits(a,b) usc_OutReg((a),MISR,(u16)((b) & 0x000f))
  499. #define SICR_RXC_ACTIVE BIT15
  500. #define SICR_RXC_INACTIVE BIT14
  501. #define SICR_RXC (BIT15+BIT14)
  502. #define SICR_TXC_ACTIVE BIT13
  503. #define SICR_TXC_INACTIVE BIT12
  504. #define SICR_TXC (BIT13+BIT12)
  505. #define SICR_RI_ACTIVE BIT11
  506. #define SICR_RI_INACTIVE BIT10
  507. #define SICR_RI (BIT11+BIT10)
  508. #define SICR_DSR_ACTIVE BIT9
  509. #define SICR_DSR_INACTIVE BIT8
  510. #define SICR_DSR (BIT9+BIT8)
  511. #define SICR_DCD_ACTIVE BIT7
  512. #define SICR_DCD_INACTIVE BIT6
  513. #define SICR_DCD (BIT7+BIT6)
  514. #define SICR_CTS_ACTIVE BIT5
  515. #define SICR_CTS_INACTIVE BIT4
  516. #define SICR_CTS (BIT5+BIT4)
  517. #define SICR_RCC_UNDERFLOW BIT3
  518. #define SICR_DPLL_NO_SYNC BIT2
  519. #define SICR_BRG1_ZERO BIT1
  520. #define SICR_BRG0_ZERO BIT0
  521. void usc_DisableMasterIrqBit( struct mgsl_struct *info );
  522. void usc_EnableMasterIrqBit( struct mgsl_struct *info );
  523. void usc_EnableInterrupts( struct mgsl_struct *info, u16 IrqMask );
  524. void usc_DisableInterrupts( struct mgsl_struct *info, u16 IrqMask );
  525. void usc_ClearIrqPendingBits( struct mgsl_struct *info, u16 IrqMask );
  526. #define usc_EnableInterrupts( a, b ) \
  527. usc_OutReg( (a), ICR, (u16)((usc_InReg((a),ICR) & 0xff00) + 0xc0 + (b)) )
  528. #define usc_DisableInterrupts( a, b ) \
  529. usc_OutReg( (a), ICR, (u16)((usc_InReg((a),ICR) & 0xff00) + 0x80 + (b)) )
  530. #define usc_EnableMasterIrqBit(a) \
  531. usc_OutReg( (a), ICR, (u16)((usc_InReg((a),ICR) & 0x0f00) + 0xb000) )
  532. #define usc_DisableMasterIrqBit(a) \
  533. usc_OutReg( (a), ICR, (u16)(usc_InReg((a),ICR) & 0x7f00) )
  534. #define usc_ClearIrqPendingBits( a, b ) usc_OutReg( (a), DCCR, 0x40 + (b) )
  535. /*
  536. * Transmit status Bits in Transmit Control status Register (TCSR)
  537. * and Transmit Interrupt Control Register (TICR) (except BIT2, BIT0)
  538. */
  539. #define TXSTATUS_PREAMBLE_SENT BIT7
  540. #define TXSTATUS_IDLE_SENT BIT6
  541. #define TXSTATUS_ABORT_SENT BIT5
  542. #define TXSTATUS_EOF BIT4
  543. #define TXSTATUS_CRC_SENT BIT3
  544. #define TXSTATUS_ALL_SENT BIT2
  545. #define TXSTATUS_UNDERRUN BIT1
  546. #define TXSTATUS_FIFO_EMPTY BIT0
  547. #define DICR_MASTER BIT15
  548. #define DICR_TRANSMIT BIT0
  549. #define DICR_RECEIVE BIT1
  550. #define usc_EnableDmaInterrupts(a,b) \
  551. usc_OutDmaReg( (a), DICR, (u16)(usc_InDmaReg((a),DICR) | (b)) )
  552. #define usc_DisableDmaInterrupts(a,b) \
  553. usc_OutDmaReg( (a), DICR, (u16)(usc_InDmaReg((a),DICR) & ~(b)) )
  554. #define usc_EnableStatusIrqs(a,b) \
  555. usc_OutReg( (a), SICR, (u16)(usc_InReg((a),SICR) | (b)) )
  556. #define usc_DisablestatusIrqs(a,b) \
  557. usc_OutReg( (a), SICR, (u16)(usc_InReg((a),SICR) & ~(b)) )
  558. /* Transmit status Bits in Transmit Control status Register (TCSR) */
  559. /* and Transmit Interrupt Control Register (TICR) (except BIT2, BIT0) */
  560. #define DISABLE_UNCONDITIONAL 0
  561. #define DISABLE_END_OF_FRAME 1
  562. #define ENABLE_UNCONDITIONAL 2
  563. #define ENABLE_AUTO_CTS 3
  564. #define ENABLE_AUTO_DCD 3
  565. #define usc_EnableTransmitter(a,b) \
  566. usc_OutReg( (a), TMR, (u16)((usc_InReg((a),TMR) & 0xfffc) | (b)) )
  567. #define usc_EnableReceiver(a,b) \
  568. usc_OutReg( (a), RMR, (u16)((usc_InReg((a),RMR) & 0xfffc) | (b)) )
  569. static u16 usc_InDmaReg( struct mgsl_struct *info, u16 Port );
  570. static void usc_OutDmaReg( struct mgsl_struct *info, u16 Port, u16 Value );
  571. static void usc_DmaCmd( struct mgsl_struct *info, u16 Cmd );
  572. static u16 usc_InReg( struct mgsl_struct *info, u16 Port );
  573. static void usc_OutReg( struct mgsl_struct *info, u16 Port, u16 Value );
  574. static void usc_RTCmd( struct mgsl_struct *info, u16 Cmd );
  575. void usc_RCmd( struct mgsl_struct *info, u16 Cmd );
  576. void usc_TCmd( struct mgsl_struct *info, u16 Cmd );
  577. #define usc_TCmd(a,b) usc_OutReg((a), TCSR, (u16)((a)->tcsr_value + (b)))
  578. #define usc_RCmd(a,b) usc_OutReg((a), RCSR, (b))
  579. #define usc_SetTransmitSyncChars(a,s0,s1) usc_OutReg((a), TSR, (u16)(((u16)s0<<8)|(u16)s1))
  580. static void usc_process_rxoverrun_sync( struct mgsl_struct *info );
  581. static void usc_start_receiver( struct mgsl_struct *info );
  582. static void usc_stop_receiver( struct mgsl_struct *info );
  583. static void usc_start_transmitter( struct mgsl_struct *info );
  584. static void usc_stop_transmitter( struct mgsl_struct *info );
  585. static void usc_set_txidle( struct mgsl_struct *info );
  586. static void usc_load_txfifo( struct mgsl_struct *info );
  587. static void usc_enable_aux_clock( struct mgsl_struct *info, u32 DataRate );
  588. static void usc_enable_loopback( struct mgsl_struct *info, int enable );
  589. static void usc_get_serial_signals( struct mgsl_struct *info );
  590. static void usc_set_serial_signals( struct mgsl_struct *info );
  591. static void usc_reset( struct mgsl_struct *info );
  592. static void usc_set_sync_mode( struct mgsl_struct *info );
  593. static void usc_set_sdlc_mode( struct mgsl_struct *info );
  594. static void usc_set_async_mode( struct mgsl_struct *info );
  595. static void usc_enable_async_clock( struct mgsl_struct *info, u32 DataRate );
  596. static void usc_loopback_frame( struct mgsl_struct *info );
  597. static void mgsl_tx_timeout(unsigned long context);
  598. static void usc_loopmode_cancel_transmit( struct mgsl_struct * info );
  599. static void usc_loopmode_insert_request( struct mgsl_struct * info );
  600. static int usc_loopmode_active( struct mgsl_struct * info);
  601. static void usc_loopmode_send_done( struct mgsl_struct * info );
  602. static int mgsl_ioctl_common(struct mgsl_struct *info, unsigned int cmd, unsigned long arg);
  603. #if SYNCLINK_GENERIC_HDLC
  604. #define dev_to_port(D) (dev_to_hdlc(D)->priv)
  605. static void hdlcdev_tx_done(struct mgsl_struct *info);
  606. static void hdlcdev_rx(struct mgsl_struct *info, char *buf, int size);
  607. static int hdlcdev_init(struct mgsl_struct *info);
  608. static void hdlcdev_exit(struct mgsl_struct *info);
  609. #endif
  610. /*
  611. * Defines a BUS descriptor value for the PCI adapter
  612. * local bus address ranges.
  613. */
  614. #define BUS_DESCRIPTOR( WrHold, WrDly, RdDly, Nwdd, Nwad, Nxda, Nrdd, Nrad ) \
  615. (0x00400020 + \
  616. ((WrHold) << 30) + \
  617. ((WrDly) << 28) + \
  618. ((RdDly) << 26) + \
  619. ((Nwdd) << 20) + \
  620. ((Nwad) << 15) + \
  621. ((Nxda) << 13) + \
  622. ((Nrdd) << 11) + \
  623. ((Nrad) << 6) )
  624. static void mgsl_trace_block(struct mgsl_struct *info,const char* data, int count, int xmit);
  625. /*
  626. * Adapter diagnostic routines
  627. */
  628. static bool mgsl_register_test( struct mgsl_struct *info );
  629. static bool mgsl_irq_test( struct mgsl_struct *info );
  630. static bool mgsl_dma_test( struct mgsl_struct *info );
  631. static bool mgsl_memory_test( struct mgsl_struct *info );
  632. static int mgsl_adapter_test( struct mgsl_struct *info );
  633. /*
  634. * device and resource management routines
  635. */
  636. static int mgsl_claim_resources(struct mgsl_struct *info);
  637. static void mgsl_release_resources(struct mgsl_struct *info);
  638. static void mgsl_add_device(struct mgsl_struct *info);
  639. static struct mgsl_struct* mgsl_allocate_device(void);
  640. /*
  641. * DMA buffer manupulation functions.
  642. */
  643. static void mgsl_free_rx_frame_buffers( struct mgsl_struct *info, unsigned int StartIndex, unsigned int EndIndex );
  644. static bool mgsl_get_rx_frame( struct mgsl_struct *info );
  645. static bool mgsl_get_raw_rx_frame( struct mgsl_struct *info );
  646. static void mgsl_reset_rx_dma_buffers( struct mgsl_struct *info );
  647. static void mgsl_reset_tx_dma_buffers( struct mgsl_struct *info );
  648. static int num_free_tx_dma_buffers(struct mgsl_struct *info);
  649. static void mgsl_load_tx_dma_buffer( struct mgsl_struct *info, const char *Buffer, unsigned int BufferSize);
  650. static void mgsl_load_pci_memory(char* TargetPtr, const char* SourcePtr, unsigned short count);
  651. /*
  652. * DMA and Shared Memory buffer allocation and formatting
  653. */
  654. static int mgsl_allocate_dma_buffers(struct mgsl_struct *info);
  655. static void mgsl_free_dma_buffers(struct mgsl_struct *info);
  656. static int mgsl_alloc_frame_memory(struct mgsl_struct *info, DMABUFFERENTRY *BufferList,int Buffercount);
  657. static void mgsl_free_frame_memory(struct mgsl_struct *info, DMABUFFERENTRY *BufferList,int Buffercount);
  658. static int mgsl_alloc_buffer_list_memory(struct mgsl_struct *info);
  659. static void mgsl_free_buffer_list_memory(struct mgsl_struct *info);
  660. static int mgsl_alloc_intermediate_rxbuffer_memory(struct mgsl_struct *info);
  661. static void mgsl_free_intermediate_rxbuffer_memory(struct mgsl_struct *info);
  662. static int mgsl_alloc_intermediate_txbuffer_memory(struct mgsl_struct *info);
  663. static void mgsl_free_intermediate_txbuffer_memory(struct mgsl_struct *info);
  664. static bool load_next_tx_holding_buffer(struct mgsl_struct *info);
  665. static int save_tx_buffer_request(struct mgsl_struct *info,const char *Buffer, unsigned int BufferSize);
  666. /*
  667. * Bottom half interrupt handlers
  668. */
  669. static void mgsl_bh_handler(struct work_struct *work);
  670. static void mgsl_bh_receive(struct mgsl_struct *info);
  671. static void mgsl_bh_transmit(struct mgsl_struct *info);
  672. static void mgsl_bh_status(struct mgsl_struct *info);
  673. /*
  674. * Interrupt handler routines and dispatch table.
  675. */
  676. static void mgsl_isr_null( struct mgsl_struct *info );
  677. static void mgsl_isr_transmit_data( struct mgsl_struct *info );
  678. static void mgsl_isr_receive_data( struct mgsl_struct *info );
  679. static void mgsl_isr_receive_status( struct mgsl_struct *info );
  680. static void mgsl_isr_transmit_status( struct mgsl_struct *info );
  681. static void mgsl_isr_io_pin( struct mgsl_struct *info );
  682. static void mgsl_isr_misc( struct mgsl_struct *info );
  683. static void mgsl_isr_receive_dma( struct mgsl_struct *info );
  684. static void mgsl_isr_transmit_dma( struct mgsl_struct *info );
  685. typedef void (*isr_dispatch_func)(struct mgsl_struct *);
  686. static isr_dispatch_func UscIsrTable[7] =
  687. {
  688. mgsl_isr_null,
  689. mgsl_isr_misc,
  690. mgsl_isr_io_pin,
  691. mgsl_isr_transmit_data,
  692. mgsl_isr_transmit_status,
  693. mgsl_isr_receive_data,
  694. mgsl_isr_receive_status
  695. };
  696. /*
  697. * ioctl call handlers
  698. */
  699. static int tiocmget(struct tty_struct *tty, struct file *file);
  700. static int tiocmset(struct tty_struct *tty, struct file *file,
  701. unsigned int set, unsigned int clear);
  702. static int mgsl_get_stats(struct mgsl_struct * info, struct mgsl_icount
  703. __user *user_icount);
  704. static int mgsl_get_params(struct mgsl_struct * info, MGSL_PARAMS __user *user_params);
  705. static int mgsl_set_params(struct mgsl_struct * info, MGSL_PARAMS __user *new_params);
  706. static int mgsl_get_txidle(struct mgsl_struct * info, int __user *idle_mode);
  707. static int mgsl_set_txidle(struct mgsl_struct * info, int idle_mode);
  708. static int mgsl_txenable(struct mgsl_struct * info, int enable);
  709. static int mgsl_txabort(struct mgsl_struct * info);
  710. static int mgsl_rxenable(struct mgsl_struct * info, int enable);
  711. static int mgsl_wait_event(struct mgsl_struct * info, int __user *mask);
  712. static int mgsl_loopmode_send_done( struct mgsl_struct * info );
  713. /* set non-zero on successful registration with PCI subsystem */
  714. static bool pci_registered;
  715. /*
  716. * Global linked list of SyncLink devices
  717. */
  718. static struct mgsl_struct *mgsl_device_list;
  719. static int mgsl_device_count;
  720. /*
  721. * Set this param to non-zero to load eax with the
  722. * .text section address and breakpoint on module load.
  723. * This is useful for use with gdb and add-symbol-file command.
  724. */
  725. static int break_on_load;
  726. /*
  727. * Driver major number, defaults to zero to get auto
  728. * assigned major number. May be forced as module parameter.
  729. */
  730. static int ttymajor;
  731. /*
  732. * Array of user specified options for ISA adapters.
  733. */
  734. static int io[MAX_ISA_DEVICES];
  735. static int irq[MAX_ISA_DEVICES];
  736. static int dma[MAX_ISA_DEVICES];
  737. static int debug_level;
  738. static int maxframe[MAX_TOTAL_DEVICES];
  739. static int dosyncppp[MAX_TOTAL_DEVICES];
  740. static int txdmabufs[MAX_TOTAL_DEVICES];
  741. static int txholdbufs[MAX_TOTAL_DEVICES];
  742. module_param(break_on_load, bool, 0);
  743. module_param(ttymajor, int, 0);
  744. module_param_array(io, int, NULL, 0);
  745. module_param_array(irq, int, NULL, 0);
  746. module_param_array(dma, int, NULL, 0);
  747. module_param(debug_level, int, 0);
  748. module_param_array(maxframe, int, NULL, 0);
  749. module_param_array(dosyncppp, int, NULL, 0);
  750. module_param_array(txdmabufs, int, NULL, 0);
  751. module_param_array(txholdbufs, int, NULL, 0);
  752. static char *driver_name = "SyncLink serial driver";
  753. static char *driver_version = "$Revision: 4.38 $";
  754. static int synclink_init_one (struct pci_dev *dev,
  755. const struct pci_device_id *ent);
  756. static void synclink_remove_one (struct pci_dev *dev);
  757. static struct pci_device_id synclink_pci_tbl[] = {
  758. { PCI_VENDOR_ID_MICROGATE, PCI_DEVICE_ID_MICROGATE_USC, PCI_ANY_ID, PCI_ANY_ID, },
  759. { PCI_VENDOR_ID_MICROGATE, 0x0210, PCI_ANY_ID, PCI_ANY_ID, },
  760. { 0, }, /* terminate list */
  761. };
  762. MODULE_DEVICE_TABLE(pci, synclink_pci_tbl);
  763. MODULE_LICENSE("GPL");
  764. static struct pci_driver synclink_pci_driver = {
  765. .name = "synclink",
  766. .id_table = synclink_pci_tbl,
  767. .probe = synclink_init_one,
  768. .remove = __devexit_p(synclink_remove_one),
  769. };
  770. static struct tty_driver *serial_driver;
  771. /* number of characters left in xmit buffer before we ask for more */
  772. #define WAKEUP_CHARS 256
  773. static void mgsl_change_params(struct mgsl_struct *info);
  774. static void mgsl_wait_until_sent(struct tty_struct *tty, int timeout);
  775. /*
  776. * 1st function defined in .text section. Calling this function in
  777. * init_module() followed by a breakpoint allows a remote debugger
  778. * (gdb) to get the .text address for the add-symbol-file command.
  779. * This allows remote debugging of dynamically loadable modules.
  780. */
  781. static void* mgsl_get_text_ptr(void)
  782. {
  783. return mgsl_get_text_ptr;
  784. }
  785. static inline int mgsl_paranoia_check(struct mgsl_struct *info,
  786. char *name, const char *routine)
  787. {
  788. #ifdef MGSL_PARANOIA_CHECK
  789. static const char *badmagic =
  790. "Warning: bad magic number for mgsl struct (%s) in %s\n";
  791. static const char *badinfo =
  792. "Warning: null mgsl_struct for (%s) in %s\n";
  793. if (!info) {
  794. printk(badinfo, name, routine);
  795. return 1;
  796. }
  797. if (info->magic != MGSL_MAGIC) {
  798. printk(badmagic, name, routine);
  799. return 1;
  800. }
  801. #else
  802. if (!info)
  803. return 1;
  804. #endif
  805. return 0;
  806. }
  807. /**
  808. * line discipline callback wrappers
  809. *
  810. * The wrappers maintain line discipline references
  811. * while calling into the line discipline.
  812. *
  813. * ldisc_receive_buf - pass receive data to line discipline
  814. */
  815. static void ldisc_receive_buf(struct tty_struct *tty,
  816. const __u8 *data, char *flags, int count)
  817. {
  818. struct tty_ldisc *ld;
  819. if (!tty)
  820. return;
  821. ld = tty_ldisc_ref(tty);
  822. if (ld) {
  823. if (ld->receive_buf)
  824. ld->receive_buf(tty, data, flags, count);
  825. tty_ldisc_deref(ld);
  826. }
  827. }
  828. /* mgsl_stop() throttle (stop) transmitter
  829. *
  830. * Arguments: tty pointer to tty info structure
  831. * Return Value: None
  832. */
  833. static void mgsl_stop(struct tty_struct *tty)
  834. {
  835. struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
  836. unsigned long flags;
  837. if (mgsl_paranoia_check(info, tty->name, "mgsl_stop"))
  838. return;
  839. if ( debug_level >= DEBUG_LEVEL_INFO )
  840. printk("mgsl_stop(%s)\n",info->device_name);
  841. spin_lock_irqsave(&info->irq_spinlock,flags);
  842. if (info->tx_enabled)
  843. usc_stop_transmitter(info);
  844. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  845. } /* end of mgsl_stop() */
  846. /* mgsl_start() release (start) transmitter
  847. *
  848. * Arguments: tty pointer to tty info structure
  849. * Return Value: None
  850. */
  851. static void mgsl_start(struct tty_struct *tty)
  852. {
  853. struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
  854. unsigned long flags;
  855. if (mgsl_paranoia_check(info, tty->name, "mgsl_start"))
  856. return;
  857. if ( debug_level >= DEBUG_LEVEL_INFO )
  858. printk("mgsl_start(%s)\n",info->device_name);
  859. spin_lock_irqsave(&info->irq_spinlock,flags);
  860. if (!info->tx_enabled)
  861. usc_start_transmitter(info);
  862. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  863. } /* end of mgsl_start() */
  864. /*
  865. * Bottom half work queue access functions
  866. */
  867. /* mgsl_bh_action() Return next bottom half action to perform.
  868. * Return Value: BH action code or 0 if nothing to do.
  869. */
  870. static int mgsl_bh_action(struct mgsl_struct *info)
  871. {
  872. unsigned long flags;
  873. int rc = 0;
  874. spin_lock_irqsave(&info->irq_spinlock,flags);
  875. if (info->pending_bh & BH_RECEIVE) {
  876. info->pending_bh &= ~BH_RECEIVE;
  877. rc = BH_RECEIVE;
  878. } else if (info->pending_bh & BH_TRANSMIT) {
  879. info->pending_bh &= ~BH_TRANSMIT;
  880. rc = BH_TRANSMIT;
  881. } else if (info->pending_bh & BH_STATUS) {
  882. info->pending_bh &= ~BH_STATUS;
  883. rc = BH_STATUS;
  884. }
  885. if (!rc) {
  886. /* Mark BH routine as complete */
  887. info->bh_running = false;
  888. info->bh_requested = false;
  889. }
  890. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  891. return rc;
  892. }
  893. /*
  894. * Perform bottom half processing of work items queued by ISR.
  895. */
  896. static void mgsl_bh_handler(struct work_struct *work)
  897. {
  898. struct mgsl_struct *info =
  899. container_of(work, struct mgsl_struct, task);
  900. int action;
  901. if (!info)
  902. return;
  903. if ( debug_level >= DEBUG_LEVEL_BH )
  904. printk( "%s(%d):mgsl_bh_handler(%s) entry\n",
  905. __FILE__,__LINE__,info->device_name);
  906. info->bh_running = true;
  907. while((action = mgsl_bh_action(info)) != 0) {
  908. /* Process work item */
  909. if ( debug_level >= DEBUG_LEVEL_BH )
  910. printk( "%s(%d):mgsl_bh_handler() work item action=%d\n",
  911. __FILE__,__LINE__,action);
  912. switch (action) {
  913. case BH_RECEIVE:
  914. mgsl_bh_receive(info);
  915. break;
  916. case BH_TRANSMIT:
  917. mgsl_bh_transmit(info);
  918. break;
  919. case BH_STATUS:
  920. mgsl_bh_status(info);
  921. break;
  922. default:
  923. /* unknown work item ID */
  924. printk("Unknown work item ID=%08X!\n", action);
  925. break;
  926. }
  927. }
  928. if ( debug_level >= DEBUG_LEVEL_BH )
  929. printk( "%s(%d):mgsl_bh_handler(%s) exit\n",
  930. __FILE__,__LINE__,info->device_name);
  931. }
  932. static void mgsl_bh_receive(struct mgsl_struct *info)
  933. {
  934. bool (*get_rx_frame)(struct mgsl_struct *info) =
  935. (info->params.mode == MGSL_MODE_HDLC ? mgsl_get_rx_frame : mgsl_get_raw_rx_frame);
  936. if ( debug_level >= DEBUG_LEVEL_BH )
  937. printk( "%s(%d):mgsl_bh_receive(%s)\n",
  938. __FILE__,__LINE__,info->device_name);
  939. do
  940. {
  941. if (info->rx_rcc_underrun) {
  942. unsigned long flags;
  943. spin_lock_irqsave(&info->irq_spinlock,flags);
  944. usc_start_receiver(info);
  945. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  946. return;
  947. }
  948. } while(get_rx_frame(info));
  949. }
  950. static void mgsl_bh_transmit(struct mgsl_struct *info)
  951. {
  952. struct tty_struct *tty = info->tty;
  953. unsigned long flags;
  954. if ( debug_level >= DEBUG_LEVEL_BH )
  955. printk( "%s(%d):mgsl_bh_transmit() entry on %s\n",
  956. __FILE__,__LINE__,info->device_name);
  957. if (tty)
  958. tty_wakeup(tty);
  959. /* if transmitter idle and loopmode_send_done_requested
  960. * then start echoing RxD to TxD
  961. */
  962. spin_lock_irqsave(&info->irq_spinlock,flags);
  963. if ( !info->tx_active && info->loopmode_send_done_requested )
  964. usc_loopmode_send_done( info );
  965. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  966. }
  967. static void mgsl_bh_status(struct mgsl_struct *info)
  968. {
  969. if ( debug_level >= DEBUG_LEVEL_BH )
  970. printk( "%s(%d):mgsl_bh_status() entry on %s\n",
  971. __FILE__,__LINE__,info->device_name);
  972. info->ri_chkcount = 0;
  973. info->dsr_chkcount = 0;
  974. info->dcd_chkcount = 0;
  975. info->cts_chkcount = 0;
  976. }
  977. /* mgsl_isr_receive_status()
  978. *
  979. * Service a receive status interrupt. The type of status
  980. * interrupt is indicated by the state of the RCSR.
  981. * This is only used for HDLC mode.
  982. *
  983. * Arguments: info pointer to device instance data
  984. * Return Value: None
  985. */
  986. static void mgsl_isr_receive_status( struct mgsl_struct *info )
  987. {
  988. u16 status = usc_InReg( info, RCSR );
  989. if ( debug_level >= DEBUG_LEVEL_ISR )
  990. printk("%s(%d):mgsl_isr_receive_status status=%04X\n",
  991. __FILE__,__LINE__,status);
  992. if ( (status & RXSTATUS_ABORT_RECEIVED) &&
  993. info->loopmode_insert_requested &&
  994. usc_loopmode_active(info) )
  995. {
  996. ++info->icount.rxabort;
  997. info->loopmode_insert_requested = false;
  998. /* clear CMR:13 to start echoing RxD to TxD */
  999. info->cmr_value &= ~BIT13;
  1000. usc_OutReg(info, CMR, info->cmr_value);
  1001. /* disable received abort irq (no longer required) */
  1002. usc_OutReg(info, RICR,
  1003. (usc_InReg(info, RICR) & ~RXSTATUS_ABORT_RECEIVED));
  1004. }
  1005. if (status & (RXSTATUS_EXITED_HUNT + RXSTATUS_IDLE_RECEIVED)) {
  1006. if (status & RXSTATUS_EXITED_HUNT)
  1007. info->icount.exithunt++;
  1008. if (status & RXSTATUS_IDLE_RECEIVED)
  1009. info->icount.rxidle++;
  1010. wake_up_interruptible(&info->event_wait_q);
  1011. }
  1012. if (status & RXSTATUS_OVERRUN){
  1013. info->icount.rxover++;
  1014. usc_process_rxoverrun_sync( info );
  1015. }
  1016. usc_ClearIrqPendingBits( info, RECEIVE_STATUS );
  1017. usc_UnlatchRxstatusBits( info, status );
  1018. } /* end of mgsl_isr_receive_status() */
  1019. /* mgsl_isr_transmit_status()
  1020. *
  1021. * Service a transmit status interrupt
  1022. * HDLC mode :end of transmit frame
  1023. * Async mode:all data is sent
  1024. * transmit status is indicated by bits in the TCSR.
  1025. *
  1026. * Arguments: info pointer to device instance data
  1027. * Return Value: None
  1028. */
  1029. static void mgsl_isr_transmit_status( struct mgsl_struct *info )
  1030. {
  1031. u16 status = usc_InReg( info, TCSR );
  1032. if ( debug_level >= DEBUG_LEVEL_ISR )
  1033. printk("%s(%d):mgsl_isr_transmit_status status=%04X\n",
  1034. __FILE__,__LINE__,status);
  1035. usc_ClearIrqPendingBits( info, TRANSMIT_STATUS );
  1036. usc_UnlatchTxstatusBits( info, status );
  1037. if ( status & (TXSTATUS_UNDERRUN | TXSTATUS_ABORT_SENT) )
  1038. {
  1039. /* finished sending HDLC abort. This may leave */
  1040. /* the TxFifo with data from the aborted frame */
  1041. /* so purge the TxFifo. Also shutdown the DMA */
  1042. /* channel in case there is data remaining in */
  1043. /* the DMA buffer */
  1044. usc_DmaCmd( info, DmaCmd_ResetTxChannel );
  1045. usc_RTCmd( info, RTCmd_PurgeTxFifo );
  1046. }
  1047. if ( status & TXSTATUS_EOF_SENT )
  1048. info->icount.txok++;
  1049. else if ( status & TXSTATUS_UNDERRUN )
  1050. info->icount.txunder++;
  1051. else if ( status & TXSTATUS_ABORT_SENT )
  1052. info->icount.txabort++;
  1053. else
  1054. info->icount.txunder++;
  1055. info->tx_active = false;
  1056. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  1057. del_timer(&info->tx_timer);
  1058. if ( info->drop_rts_on_tx_done ) {
  1059. usc_get_serial_signals( info );
  1060. if ( info->serial_signals & SerialSignal_RTS ) {
  1061. info->serial_signals &= ~SerialSignal_RTS;
  1062. usc_set_serial_signals( info );
  1063. }
  1064. info->drop_rts_on_tx_done = false;
  1065. }
  1066. #if SYNCLINK_GENERIC_HDLC
  1067. if (info->netcount)
  1068. hdlcdev_tx_done(info);
  1069. else
  1070. #endif
  1071. {
  1072. if (info->tty->stopped || info->tty->hw_stopped) {
  1073. usc_stop_transmitter(info);
  1074. return;
  1075. }
  1076. info->pending_bh |= BH_TRANSMIT;
  1077. }
  1078. } /* end of mgsl_isr_transmit_status() */
  1079. /* mgsl_isr_io_pin()
  1080. *
  1081. * Service an Input/Output pin interrupt. The type of
  1082. * interrupt is indicated by bits in the MISR
  1083. *
  1084. * Arguments: info pointer to device instance data
  1085. * Return Value: None
  1086. */
  1087. static void mgsl_isr_io_pin( struct mgsl_struct *info )
  1088. {
  1089. struct mgsl_icount *icount;
  1090. u16 status = usc_InReg( info, MISR );
  1091. if ( debug_level >= DEBUG_LEVEL_ISR )
  1092. printk("%s(%d):mgsl_isr_io_pin status=%04X\n",
  1093. __FILE__,__LINE__,status);
  1094. usc_ClearIrqPendingBits( info, IO_PIN );
  1095. usc_UnlatchIostatusBits( info, status );
  1096. if (status & (MISCSTATUS_CTS_LATCHED | MISCSTATUS_DCD_LATCHED |
  1097. MISCSTATUS_DSR_LATCHED | MISCSTATUS_RI_LATCHED) ) {
  1098. icount = &info->icount;
  1099. /* update input line counters */
  1100. if (status & MISCSTATUS_RI_LATCHED) {
  1101. if ((info->ri_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
  1102. usc_DisablestatusIrqs(info,SICR_RI);
  1103. icount->rng++;
  1104. if ( status & MISCSTATUS_RI )
  1105. info->input_signal_events.ri_up++;
  1106. else
  1107. info->input_signal_events.ri_down++;
  1108. }
  1109. if (status & MISCSTATUS_DSR_LATCHED) {
  1110. if ((info->dsr_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
  1111. usc_DisablestatusIrqs(info,SICR_DSR);
  1112. icount->dsr++;
  1113. if ( status & MISCSTATUS_DSR )
  1114. info->input_signal_events.dsr_up++;
  1115. else
  1116. info->input_signal_events.dsr_down++;
  1117. }
  1118. if (status & MISCSTATUS_DCD_LATCHED) {
  1119. if ((info->dcd_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
  1120. usc_DisablestatusIrqs(info,SICR_DCD);
  1121. icount->dcd++;
  1122. if (status & MISCSTATUS_DCD) {
  1123. info->input_signal_events.dcd_up++;
  1124. } else
  1125. info->input_signal_events.dcd_down++;
  1126. #if SYNCLINK_GENERIC_HDLC
  1127. if (info->netcount) {
  1128. if (status & MISCSTATUS_DCD)
  1129. netif_carrier_on(info->netdev);
  1130. else
  1131. netif_carrier_off(info->netdev);
  1132. }
  1133. #endif
  1134. }
  1135. if (status & MISCSTATUS_CTS_LATCHED)
  1136. {
  1137. if ((info->cts_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
  1138. usc_DisablestatusIrqs(info,SICR_CTS);
  1139. icount->cts++;
  1140. if ( status & MISCSTATUS_CTS )
  1141. info->input_signal_events.cts_up++;
  1142. else
  1143. info->input_signal_events.cts_down++;
  1144. }
  1145. wake_up_interruptible(&info->status_event_wait_q);
  1146. wake_up_interruptible(&info->event_wait_q);
  1147. if ( (info->flags & ASYNC_CHECK_CD) &&
  1148. (status & MISCSTATUS_DCD_LATCHED) ) {
  1149. if ( debug_level >= DEBUG_LEVEL_ISR )
  1150. printk("%s CD now %s...", info->device_name,
  1151. (status & MISCSTATUS_DCD) ? "on" : "off");
  1152. if (status & MISCSTATUS_DCD)
  1153. wake_up_interruptible(&info->open_wait);
  1154. else {
  1155. if ( debug_level >= DEBUG_LEVEL_ISR )
  1156. printk("doing serial hangup...");
  1157. if (info->tty)
  1158. tty_hangup(info->tty);
  1159. }
  1160. }
  1161. if ( (info->flags & ASYNC_CTS_FLOW) &&
  1162. (status & MISCSTATUS_CTS_LATCHED) ) {
  1163. if (info->tty->hw_stopped) {
  1164. if (status & MISCSTATUS_CTS) {
  1165. if ( debug_level >= DEBUG_LEVEL_ISR )
  1166. printk("CTS tx start...");
  1167. if (info->tty)
  1168. info->tty->hw_stopped = 0;
  1169. usc_start_transmitter(info);
  1170. info->pending_bh |= BH_TRANSMIT;
  1171. return;
  1172. }
  1173. } else {
  1174. if (!(status & MISCSTATUS_CTS)) {
  1175. if ( debug_level >= DEBUG_LEVEL_ISR )
  1176. printk("CTS tx stop...");
  1177. if (info->tty)
  1178. info->tty->hw_stopped = 1;
  1179. usc_stop_transmitter(info);
  1180. }
  1181. }
  1182. }
  1183. }
  1184. info->pending_bh |= BH_STATUS;
  1185. /* for diagnostics set IRQ flag */
  1186. if ( status & MISCSTATUS_TXC_LATCHED ){
  1187. usc_OutReg( info, SICR,
  1188. (unsigned short)(usc_InReg(info,SICR) & ~(SICR_TXC_ACTIVE+SICR_TXC_INACTIVE)) );
  1189. usc_UnlatchIostatusBits( info, MISCSTATUS_TXC_LATCHED );
  1190. info->irq_occurred = true;
  1191. }
  1192. } /* end of mgsl_isr_io_pin() */
  1193. /* mgsl_isr_transmit_data()
  1194. *
  1195. * Service a transmit data interrupt (async mode only).
  1196. *
  1197. * Arguments: info pointer to device instance data
  1198. * Return Value: None
  1199. */
  1200. static void mgsl_isr_transmit_data( struct mgsl_struct *info )
  1201. {
  1202. if ( debug_level >= DEBUG_LEVEL_ISR )
  1203. printk("%s(%d):mgsl_isr_transmit_data xmit_cnt=%d\n",
  1204. __FILE__,__LINE__,info->xmit_cnt);
  1205. usc_ClearIrqPendingBits( info, TRANSMIT_DATA );
  1206. if (info->tty->stopped || info->tty->hw_stopped) {
  1207. usc_stop_transmitter(info);
  1208. return;
  1209. }
  1210. if ( info->xmit_cnt )
  1211. usc_load_txfifo( info );
  1212. else
  1213. info->tx_active = false;
  1214. if (info->xmit_cnt < WAKEUP_CHARS)
  1215. info->pending_bh |= BH_TRANSMIT;
  1216. } /* end of mgsl_isr_transmit_data() */
  1217. /* mgsl_isr_receive_data()
  1218. *
  1219. * Service a receive data interrupt. This occurs
  1220. * when operating in asynchronous interrupt transfer mode.
  1221. * The receive data FIFO is flushed to the receive data buffers.
  1222. *
  1223. * Arguments: info pointer to device instance data
  1224. * Return Value: None
  1225. */
  1226. static void mgsl_isr_receive_data( struct mgsl_struct *info )
  1227. {
  1228. int Fifocount;
  1229. u16 status;
  1230. int work = 0;
  1231. unsigned char DataByte;
  1232. struct tty_struct *tty = info->tty;
  1233. struct mgsl_icount *icount = &info->icount;
  1234. if ( debug_level >= DEBUG_LEVEL_ISR )
  1235. printk("%s(%d):mgsl_isr_receive_data\n",
  1236. __FILE__,__LINE__);
  1237. usc_ClearIrqPendingBits( info, RECEIVE_DATA );
  1238. /* select FIFO status for RICR readback */
  1239. usc_RCmd( info, RCmd_SelectRicrRxFifostatus );
  1240. /* clear the Wordstatus bit so that status readback */
  1241. /* only reflects the status of this byte */
  1242. usc_OutReg( info, RICR+LSBONLY, (u16)(usc_InReg(info, RICR+LSBONLY) & ~BIT3 ));
  1243. /* flush the receive FIFO */
  1244. while( (Fifocount = (usc_InReg(info,RICR) >> 8)) ) {
  1245. int flag;
  1246. /* read one byte from RxFIFO */
  1247. outw( (inw(info->io_base + CCAR) & 0x0780) | (RDR+LSBONLY),
  1248. info->io_base + CCAR );
  1249. DataByte = inb( info->io_base + CCAR );
  1250. /* get the status of the received byte */
  1251. status = usc_InReg(info, RCSR);
  1252. if ( status & (RXSTATUS_FRAMING_ERROR + RXSTATUS_PARITY_ERROR +
  1253. RXSTATUS_OVERRUN + RXSTATUS_BREAK_RECEIVED) )
  1254. usc_UnlatchRxstatusBits(info,RXSTATUS_ALL);
  1255. icount->rx++;
  1256. flag = 0;
  1257. if ( status & (RXSTATUS_FRAMING_ERROR + RXSTATUS_PARITY_ERROR +
  1258. RXSTATUS_OVERRUN + RXSTATUS_BREAK_RECEIVED) ) {
  1259. printk("rxerr=%04X\n",status);
  1260. /* update error statistics */
  1261. if ( status & RXSTATUS_BREAK_RECEIVED ) {
  1262. status &= ~(RXSTATUS_FRAMING_ERROR + RXSTATUS_PARITY_ERROR);
  1263. icount->brk++;
  1264. } else if (status & RXSTATUS_PARITY_ERROR)
  1265. icount->parity++;
  1266. else if (status & RXSTATUS_FRAMING_ERROR)
  1267. icount->frame++;
  1268. else if (status & RXSTATUS_OVERRUN) {
  1269. /* must issue purge fifo cmd before */
  1270. /* 16C32 accepts more receive chars */
  1271. usc_RTCmd(info,RTCmd_PurgeRxFifo);
  1272. icount->overrun++;
  1273. }
  1274. /* discard char if tty control flags say so */
  1275. if (status & info->ignore_status_mask)
  1276. continue;
  1277. status &= info->read_status_mask;
  1278. if (status & RXSTATUS_BREAK_RECEIVED) {
  1279. flag = TTY_BREAK;
  1280. if (info->flags & ASYNC_SAK)
  1281. do_SAK(tty);
  1282. } else if (status & RXSTATUS_PARITY_ERROR)
  1283. flag = TTY_PARITY;
  1284. else if (status & RXSTATUS_FRAMING_ERROR)
  1285. flag = TTY_FRAME;
  1286. } /* end of if (error) */
  1287. tty_insert_flip_char(tty, DataByte, flag);
  1288. if (status & RXSTATUS_OVERRUN) {
  1289. /* Overrun is special, since it's
  1290. * reported immediately, and doesn't
  1291. * affect the current character
  1292. */
  1293. work += tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  1294. }
  1295. }
  1296. if ( debug_level >= DEBUG_LEVEL_ISR ) {
  1297. printk("%s(%d):rx=%d brk=%d parity=%d frame=%d overrun=%d\n",
  1298. __FILE__,__LINE__,icount->rx,icount->brk,
  1299. icount->parity,icount->frame,icount->overrun);
  1300. }
  1301. if(work)
  1302. tty_flip_buffer_push(tty);
  1303. }
  1304. /* mgsl_isr_misc()
  1305. *
  1306. * Service a miscellaneous interrupt source.
  1307. *
  1308. * Arguments: info pointer to device extension (instance data)
  1309. * Return Value: None
  1310. */
  1311. static void mgsl_isr_misc( struct mgsl_struct *info )
  1312. {
  1313. u16 status = usc_InReg( info, MISR );
  1314. if ( debug_level >= DEBUG_LEVEL_ISR )
  1315. printk("%s(%d):mgsl_isr_misc status=%04X\n",
  1316. __FILE__,__LINE__,status);
  1317. if ((status & MISCSTATUS_RCC_UNDERRUN) &&
  1318. (info->params.mode == MGSL_MODE_HDLC)) {
  1319. /* turn off receiver and rx DMA */
  1320. usc_EnableReceiver(info,DISABLE_UNCONDITIONAL);
  1321. usc_DmaCmd(info, DmaCmd_ResetRxChannel);
  1322. usc_UnlatchRxstatusBits(info, RXSTATUS_ALL);
  1323. usc_ClearIrqPendingBits(info, RECEIVE_DATA + RECEIVE_STATUS);
  1324. usc_DisableInterrupts(info, RECEIVE_DATA + RECEIVE_STATUS);
  1325. /* schedule BH handler to restart receiver */
  1326. info->pending_bh |= BH_RECEIVE;
  1327. info->rx_rcc_underrun = true;
  1328. }
  1329. usc_ClearIrqPendingBits( info, MISC );
  1330. usc_UnlatchMiscstatusBits( info, status );
  1331. } /* end of mgsl_isr_misc() */
  1332. /* mgsl_isr_null()
  1333. *
  1334. * Services undefined interrupt vectors from the
  1335. * USC. (hence this function SHOULD never be called)
  1336. *
  1337. * Arguments: info pointer to device extension (instance data)
  1338. * Return Value: None
  1339. */
  1340. static void mgsl_isr_null( struct mgsl_struct *info )
  1341. {
  1342. } /* end of mgsl_isr_null() */
  1343. /* mgsl_isr_receive_dma()
  1344. *
  1345. * Service a receive DMA channel interrupt.
  1346. * For this driver there are two sources of receive DMA interrupts
  1347. * as identified in the Receive DMA mode Register (RDMR):
  1348. *
  1349. * BIT3 EOA/EOL End of List, all receive buffers in receive
  1350. * buffer list have been filled (no more free buffers
  1351. * available). The DMA controller has shut down.
  1352. *
  1353. * BIT2 EOB End of Buffer. This interrupt occurs when a receive
  1354. * DMA buffer is terminated in response to completion
  1355. * of a good frame or a frame with errors. The status
  1356. * of the frame is stored in the buffer entry in the
  1357. * list of receive buffer entries.
  1358. *
  1359. * Arguments: info pointer to device instance data
  1360. * Return Value: None
  1361. */
  1362. static void mgsl_isr_receive_dma( struct mgsl_struct *info )
  1363. {
  1364. u16 status;
  1365. /* clear interrupt pending and IUS bit for Rx DMA IRQ */
  1366. usc_OutDmaReg( info, CDIR, BIT9+BIT1 );
  1367. /* Read the receive DMA status to identify interrupt type. */
  1368. /* This also clears the status bits. */
  1369. status = usc_InDmaReg( info, RDMR );
  1370. if ( debug_level >= DEBUG_LEVEL_ISR )
  1371. printk("%s(%d):mgsl_isr_receive_dma(%s) status=%04X\n",
  1372. __FILE__,__LINE__,info->device_name,status);
  1373. info->pending_bh |= BH_RECEIVE;
  1374. if ( status & BIT3 ) {
  1375. info->rx_overflow = true;
  1376. info->icount.buf_overrun++;
  1377. }
  1378. } /* end of mgsl_isr_receive_dma() */
  1379. /* mgsl_isr_transmit_dma()
  1380. *
  1381. * This function services a transmit DMA channel interrupt.
  1382. *
  1383. * For this driver there is one source of transmit DMA interrupts
  1384. * as identified in the Transmit DMA Mode Register (TDMR):
  1385. *
  1386. * BIT2 EOB End of Buffer. This interrupt occurs when a
  1387. * transmit DMA buffer has been emptied.
  1388. *
  1389. * The driver maintains enough transmit DMA buffers to hold at least
  1390. * one max frame size transmit frame. When operating in a buffered
  1391. * transmit mode, there may be enough transmit DMA buffers to hold at
  1392. * least two or more max frame size frames. On an EOB condition,
  1393. * determine if there are any queued transmit buffers and copy into
  1394. * transmit DMA buffers if we have room.
  1395. *
  1396. * Arguments: info pointer to device instance data
  1397. * Return Value: None
  1398. */
  1399. static void mgsl_isr_transmit_dma( struct mgsl_struct *info )
  1400. {
  1401. u16 status;
  1402. /* clear interrupt pending and IUS bit for Tx DMA IRQ */
  1403. usc_OutDmaReg(info, CDIR, BIT8+BIT0 );
  1404. /* Read the transmit DMA status to identify interrupt type. */
  1405. /* This also clears the status bits. */
  1406. status = usc_InDmaReg( info, TDMR );
  1407. if ( debug_level >= DEBUG_LEVEL_ISR )
  1408. printk("%s(%d):mgsl_isr_transmit_dma(%s) status=%04X\n",
  1409. __FILE__,__LINE__,info->device_name,status);
  1410. if ( status & BIT2 ) {
  1411. --info->tx_dma_buffers_used;
  1412. /* if there are transmit frames queued,
  1413. * try to load the next one
  1414. */
  1415. if ( load_next_tx_holding_buffer(info) ) {
  1416. /* if call returns non-zero value, we have
  1417. * at least one free tx holding buffer
  1418. */
  1419. info->pending_bh |= BH_TRANSMIT;
  1420. }
  1421. }
  1422. } /* end of mgsl_isr_transmit_dma() */
  1423. /* mgsl_interrupt()
  1424. *
  1425. * Interrupt service routine entry point.
  1426. *
  1427. * Arguments:
  1428. *
  1429. * irq interrupt number that caused interrupt
  1430. * dev_id device ID supplied during interrupt registration
  1431. *
  1432. * Return Value: None
  1433. */
  1434. static irqreturn_t mgsl_interrupt(int dummy, void *dev_id)
  1435. {
  1436. struct mgsl_struct *info = dev_id;
  1437. u16 UscVector;
  1438. u16 DmaVector;
  1439. if ( debug_level >= DEBUG_LEVEL_ISR )
  1440. printk(KERN_DEBUG "%s(%d):mgsl_interrupt(%d)entry.\n",
  1441. __FILE__, __LINE__, info->irq_level);
  1442. spin_lock(&info->irq_spinlock);
  1443. for(;;) {
  1444. /* Read the interrupt vectors from hardware. */
  1445. UscVector = usc_InReg(info, IVR) >> 9;
  1446. DmaVector = usc_InDmaReg(info, DIVR);
  1447. if ( debug_level >= DEBUG_LEVEL_ISR )
  1448. printk("%s(%d):%s UscVector=%08X DmaVector=%08X\n",
  1449. __FILE__,__LINE__,info->device_name,UscVector,DmaVector);
  1450. if ( !UscVector && !DmaVector )
  1451. break;
  1452. /* Dispatch interrupt vector */
  1453. if ( UscVector )
  1454. (*UscIsrTable[UscVector])(info);
  1455. else if ( (DmaVector&(BIT10|BIT9)) == BIT10)
  1456. mgsl_isr_transmit_dma(info);
  1457. else
  1458. mgsl_isr_receive_dma(info);
  1459. if ( info->isr_overflow ) {
  1460. printk(KERN_ERR "%s(%d):%s isr overflow irq=%d\n",
  1461. __FILE__, __LINE__, info->device_name, info->irq_level);
  1462. usc_DisableMasterIrqBit(info);
  1463. usc_DisableDmaInterrupts(info,DICR_MASTER);
  1464. break;
  1465. }
  1466. }
  1467. /* Request bottom half processing if there's something
  1468. * for it to do and the bh is not already running
  1469. */
  1470. if ( info->pending_bh && !info->bh_running && !info->bh_requested ) {
  1471. if ( debug_level >= DEBUG_LEVEL_ISR )
  1472. printk("%s(%d):%s queueing bh task.\n",
  1473. __FILE__,__LINE__,info->device_name);
  1474. schedule_work(&info->task);
  1475. info->bh_requested = true;
  1476. }
  1477. spin_unlock(&info->irq_spinlock);
  1478. if ( debug_level >= DEBUG_LEVEL_ISR )
  1479. printk(KERN_DEBUG "%s(%d):mgsl_interrupt(%d)exit.\n",
  1480. __FILE__, __LINE__, info->irq_level);
  1481. return IRQ_HANDLED;
  1482. } /* end of mgsl_interrupt() */
  1483. /* startup()
  1484. *
  1485. * Initialize and start device.
  1486. *
  1487. * Arguments: info pointer to device instance data
  1488. * Return Value: 0 if success, otherwise error code
  1489. */
  1490. static int startup(struct mgsl_struct * info)
  1491. {
  1492. int retval = 0;
  1493. if ( debug_level >= DEBUG_LEVEL_INFO )
  1494. printk("%s(%d):mgsl_startup(%s)\n",__FILE__,__LINE__,info->device_name);
  1495. if (info->flags & ASYNC_INITIALIZED)
  1496. return 0;
  1497. if (!info->xmit_buf) {
  1498. /* allocate a page of memory for a transmit buffer */
  1499. info->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
  1500. if (!info->xmit_buf) {
  1501. printk(KERN_ERR"%s(%d):%s can't allocate transmit buffer\n",
  1502. __FILE__,__LINE__,info->device_name);
  1503. return -ENOMEM;
  1504. }
  1505. }
  1506. info->pending_bh = 0;
  1507. memset(&info->icount, 0, sizeof(info->icount));
  1508. setup_timer(&info->tx_timer, mgsl_tx_timeout, (unsigned long)info);
  1509. /* Allocate and claim adapter resources */
  1510. retval = mgsl_claim_resources(info);
  1511. /* perform existence check and diagnostics */
  1512. if ( !retval )
  1513. retval = mgsl_adapter_test(info);
  1514. if ( retval ) {
  1515. if (capable(CAP_SYS_ADMIN) && info->tty)
  1516. set_bit(TTY_IO_ERROR, &info->tty->flags);
  1517. mgsl_release_resources(info);
  1518. return retval;
  1519. }
  1520. /* program hardware for current parameters */
  1521. mgsl_change_params(info);
  1522. if (info->tty)
  1523. clear_bit(TTY_IO_ERROR, &info->tty->flags);
  1524. info->flags |= ASYNC_INITIALIZED;
  1525. return 0;
  1526. } /* end of startup() */
  1527. /* shutdown()
  1528. *
  1529. * Called by mgsl_close() and mgsl_hangup() to shutdown hardware
  1530. *
  1531. * Arguments: info pointer to device instance data
  1532. * Return Value: None
  1533. */
  1534. static void shutdown(struct mgsl_struct * info)
  1535. {
  1536. unsigned long flags;
  1537. if (!(info->flags & ASYNC_INITIALIZED))
  1538. return;
  1539. if (debug_level >= DEBUG_LEVEL_INFO)
  1540. printk("%s(%d):mgsl_shutdown(%s)\n",
  1541. __FILE__,__LINE__, info->device_name );
  1542. /* clear status wait queue because status changes */
  1543. /* can't happen after shutting down the hardware */
  1544. wake_up_interruptible(&info->status_event_wait_q);
  1545. wake_up_interruptible(&info->event_wait_q);
  1546. del_timer_sync(&info->tx_timer);
  1547. if (info->xmit_buf) {
  1548. free_page((unsigned long) info->xmit_buf);
  1549. info->xmit_buf = NULL;
  1550. }
  1551. spin_lock_irqsave(&info->irq_spinlock,flags);
  1552. usc_DisableMasterIrqBit(info);
  1553. usc_stop_receiver(info);
  1554. usc_stop_transmitter(info);
  1555. usc_DisableInterrupts(info,RECEIVE_DATA + RECEIVE_STATUS +
  1556. TRANSMIT_DATA + TRANSMIT_STATUS + IO_PIN + MISC );
  1557. usc_DisableDmaInterrupts(info,DICR_MASTER + DICR_TRANSMIT + DICR_RECEIVE);
  1558. /* Disable DMAEN (Port 7, Bit 14) */
  1559. /* This disconnects the DMA request signal from the ISA bus */
  1560. /* on the ISA adapter. This has no effect for the PCI adapter */
  1561. usc_OutReg(info, PCR, (u16)((usc_InReg(info, PCR) | BIT15) | BIT14));
  1562. /* Disable INTEN (Port 6, Bit12) */
  1563. /* This disconnects the IRQ request signal to the ISA bus */
  1564. /* on the ISA adapter. This has no effect for the PCI adapter */
  1565. usc_OutReg(info, PCR, (u16)((usc_InReg(info, PCR) | BIT13) | BIT12));
  1566. if (!info->tty || info->tty->termios->c_cflag & HUPCL) {
  1567. info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
  1568. usc_set_serial_signals(info);
  1569. }
  1570. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  1571. mgsl_release_resources(info);
  1572. if (info->tty)
  1573. set_bit(TTY_IO_ERROR, &info->tty->flags);
  1574. info->flags &= ~ASYNC_INITIALIZED;
  1575. } /* end of shutdown() */
  1576. static void mgsl_program_hw(struct mgsl_struct *info)
  1577. {
  1578. unsigned long flags;
  1579. spin_lock_irqsave(&info->irq_spinlock,flags);
  1580. usc_stop_receiver(info);
  1581. usc_stop_transmitter(info);
  1582. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  1583. if (info->params.mode == MGSL_MODE_HDLC ||
  1584. info->params.mode == MGSL_MODE_RAW ||
  1585. info->netcount)
  1586. usc_set_sync_mode(info);
  1587. else
  1588. usc_set_async_mode(info);
  1589. usc_set_serial_signals(info);
  1590. info->dcd_chkcount = 0;
  1591. info->cts_chkcount = 0;
  1592. info->ri_chkcount = 0;
  1593. info->dsr_chkcount = 0;
  1594. usc_EnableStatusIrqs(info,SICR_CTS+SICR_DSR+SICR_DCD+SICR_RI);
  1595. usc_EnableInterrupts(info, IO_PIN);
  1596. usc_get_serial_signals(info);
  1597. if (info->netcount || info->tty->termios->c_cflag & CREAD)
  1598. usc_start_receiver(info);
  1599. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  1600. }
  1601. /* Reconfigure adapter based on new parameters
  1602. */
  1603. static void mgsl_change_params(struct mgsl_struct *info)
  1604. {
  1605. unsigned cflag;
  1606. int bits_per_char;
  1607. if (!info->tty || !info->tty->termios)
  1608. return;
  1609. if (debug_level >= DEBUG_LEVEL_INFO)
  1610. printk("%s(%d):mgsl_change_params(%s)\n",
  1611. __FILE__,__LINE__, info->device_name );
  1612. cflag = info->tty->termios->c_cflag;
  1613. /* if B0 rate (hangup) specified then negate DTR and RTS */
  1614. /* otherwise assert DTR and RTS */
  1615. if (cflag & CBAUD)
  1616. info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
  1617. else
  1618. info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
  1619. /* byte size and parity */
  1620. switch (cflag & CSIZE) {
  1621. case CS5: info->params.data_bits = 5; break;
  1622. case CS6: info->params.data_bits = 6; break;
  1623. case CS7: info->params.data_bits = 7; break;
  1624. case CS8: info->params.data_bits = 8; break;
  1625. /* Never happens, but GCC is too dumb to figure it out */
  1626. default: info->params.data_bits = 7; break;
  1627. }
  1628. if (cflag & CSTOPB)
  1629. info->params.stop_bits = 2;
  1630. else
  1631. info->params.stop_bits = 1;
  1632. info->params.parity = ASYNC_PARITY_NONE;
  1633. if (cflag & PARENB) {
  1634. if (cflag & PARODD)
  1635. info->params.parity = ASYNC_PARITY_ODD;
  1636. else
  1637. info->params.parity = ASYNC_PARITY_EVEN;
  1638. #ifdef CMSPAR
  1639. if (cflag & CMSPAR)
  1640. info->params.parity = ASYNC_PARITY_SPACE;
  1641. #endif
  1642. }
  1643. /* calculate number of jiffies to transmit a full
  1644. * FIFO (32 bytes) at specified data rate
  1645. */
  1646. bits_per_char = info->params.data_bits +
  1647. info->params.stop_bits + 1;
  1648. /* if port data rate is set to 460800 or less then
  1649. * allow tty settings to override, otherwise keep the
  1650. * current data rate.
  1651. */
  1652. if (info->params.data_rate <= 460800)
  1653. info->params.data_rate = tty_get_baud_rate(info->tty);
  1654. if ( info->params.data_rate ) {
  1655. info->timeout = (32*HZ*bits_per_char) /
  1656. info->params.data_rate;
  1657. }
  1658. info->timeout += HZ/50; /* Add .02 seconds of slop */
  1659. if (cflag & CRTSCTS)
  1660. info->flags |= ASYNC_CTS_FLOW;
  1661. else
  1662. info->flags &= ~ASYNC_CTS_FLOW;
  1663. if (cflag & CLOCAL)
  1664. info->flags &= ~ASYNC_CHECK_CD;
  1665. else
  1666. info->flags |= ASYNC_CHECK_CD;
  1667. /* process tty input control flags */
  1668. info->read_status_mask = RXSTATUS_OVERRUN;
  1669. if (I_INPCK(info->tty))
  1670. info->read_status_mask |= RXSTATUS_PARITY_ERROR | RXSTATUS_FRAMING_ERROR;
  1671. if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
  1672. info->read_status_mask |= RXSTATUS_BREAK_RECEIVED;
  1673. if (I_IGNPAR(info->tty))
  1674. info->ignore_status_mask |= RXSTATUS_PARITY_ERROR | RXSTATUS_FRAMING_ERROR;
  1675. if (I_IGNBRK(info->tty)) {
  1676. info->ignore_status_mask |= RXSTATUS_BREAK_RECEIVED;
  1677. /* If ignoring parity and break indicators, ignore
  1678. * overruns too. (For real raw support).
  1679. */
  1680. if (I_IGNPAR(info->tty))
  1681. info->ignore_status_mask |= RXSTATUS_OVERRUN;
  1682. }
  1683. mgsl_program_hw(info);
  1684. } /* end of mgsl_change_params() */
  1685. /* mgsl_put_char()
  1686. *
  1687. * Add a character to the transmit buffer.
  1688. *
  1689. * Arguments: tty pointer to tty information structure
  1690. * ch character to add to transmit buffer
  1691. *
  1692. * Return Value: None
  1693. */
  1694. static void mgsl_put_char(struct tty_struct *tty, unsigned char ch)
  1695. {
  1696. struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
  1697. unsigned long flags;
  1698. if ( debug_level >= DEBUG_LEVEL_INFO ) {
  1699. printk( "%s(%d):mgsl_put_char(%d) on %s\n",
  1700. __FILE__,__LINE__,ch,info->device_name);
  1701. }
  1702. if (mgsl_paranoia_check(info, tty->name, "mgsl_put_char"))
  1703. return;
  1704. if (!tty || !info->xmit_buf)
  1705. return;
  1706. spin_lock_irqsave(&info->irq_spinlock,flags);
  1707. if ( (info->params.mode == MGSL_MODE_ASYNC ) || !info->tx_active ) {
  1708. if (info->xmit_cnt < SERIAL_XMIT_SIZE - 1) {
  1709. info->xmit_buf[info->xmit_head++] = ch;
  1710. info->xmit_head &= SERIAL_XMIT_SIZE-1;
  1711. info->xmit_cnt++;
  1712. }
  1713. }
  1714. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  1715. } /* end of mgsl_put_char() */
  1716. /* mgsl_flush_chars()
  1717. *
  1718. * Enable transmitter so remaining characters in the
  1719. * transmit buffer are sent.
  1720. *
  1721. * Arguments: tty pointer to tty information structure
  1722. * Return Value: None
  1723. */
  1724. static void mgsl_flush_chars(struct tty_struct *tty)
  1725. {
  1726. struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
  1727. unsigned long flags;
  1728. if ( debug_level >= DEBUG_LEVEL_INFO )
  1729. printk( "%s(%d):mgsl_flush_chars() entry on %s xmit_cnt=%d\n",
  1730. __FILE__,__LINE__,info->device_name,info->xmit_cnt);
  1731. if (mgsl_paranoia_check(info, tty->name, "mgsl_flush_chars"))
  1732. return;
  1733. if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
  1734. !info->xmit_buf)
  1735. return;
  1736. if ( debug_level >= DEBUG_LEVEL_INFO )
  1737. printk( "%s(%d):mgsl_flush_chars() entry on %s starting transmitter\n",
  1738. __FILE__,__LINE__,info->device_name );
  1739. spin_lock_irqsave(&info->irq_spinlock,flags);
  1740. if (!info->tx_active) {
  1741. if ( (info->params.mode == MGSL_MODE_HDLC ||
  1742. info->params.mode == MGSL_MODE_RAW) && info->xmit_cnt ) {
  1743. /* operating in synchronous (frame oriented) mode */
  1744. /* copy data from circular xmit_buf to */
  1745. /* transmit DMA buffer. */
  1746. mgsl_load_tx_dma_buffer(info,
  1747. info->xmit_buf,info->xmit_cnt);
  1748. }
  1749. usc_start_transmitter(info);
  1750. }
  1751. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  1752. } /* end of mgsl_flush_chars() */
  1753. /* mgsl_write()
  1754. *
  1755. * Send a block of data
  1756. *
  1757. * Arguments:
  1758. *
  1759. * tty pointer to tty information structure
  1760. * buf pointer to buffer containing send data
  1761. * count size of send data in bytes
  1762. *
  1763. * Return Value: number of characters written
  1764. */
  1765. static int mgsl_write(struct tty_struct * tty,
  1766. const unsigned char *buf, int count)
  1767. {
  1768. int c, ret = 0;
  1769. struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
  1770. unsigned long flags;
  1771. if ( debug_level >= DEBUG_LEVEL_INFO )
  1772. printk( "%s(%d):mgsl_write(%s) count=%d\n",
  1773. __FILE__,__LINE__,info->device_name,count);
  1774. if (mgsl_paranoia_check(info, tty->name, "mgsl_write"))
  1775. goto cleanup;
  1776. if (!tty || !info->xmit_buf)
  1777. goto cleanup;
  1778. if ( info->params.mode == MGSL_MODE_HDLC ||
  1779. info->params.mode == MGSL_MODE_RAW ) {
  1780. /* operating in synchronous (frame oriented) mode */
  1781. /* operating in synchronous (frame oriented) mode */
  1782. if (info->tx_active) {
  1783. if ( info->params.mode == MGSL_MODE_HDLC ) {
  1784. ret = 0;
  1785. goto cleanup;
  1786. }
  1787. /* transmitter is actively sending data -
  1788. * if we have multiple transmit dma and
  1789. * holding buffers, attempt to queue this
  1790. * frame for transmission at a later time.
  1791. */
  1792. if (info->tx_holding_count >= info->num_tx_holding_buffers ) {
  1793. /* no tx holding buffers available */
  1794. ret = 0;
  1795. goto cleanup;
  1796. }
  1797. /* queue transmit frame request */
  1798. ret = count;
  1799. save_tx_buffer_request(info,buf,count);
  1800. /* if we have sufficient tx dma buffers,
  1801. * load the next buffered tx request
  1802. */
  1803. spin_lock_irqsave(&info->irq_spinlock,flags);
  1804. load_next_tx_holding_buffer(info);
  1805. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  1806. goto cleanup;
  1807. }
  1808. /* if operating in HDLC LoopMode and the adapter */
  1809. /* has yet to be inserted into the loop, we can't */
  1810. /* transmit */
  1811. if ( (info->params.flags & HDLC_FLAG_HDLC_LOOPMODE) &&
  1812. !usc_loopmode_active(info) )
  1813. {
  1814. ret = 0;
  1815. goto cleanup;
  1816. }
  1817. if ( info->xmit_cnt ) {
  1818. /* Send accumulated from send_char() calls */
  1819. /* as frame and wait before accepting more data. */
  1820. ret = 0;
  1821. /* copy data from circular xmit_buf to */
  1822. /* transmit DMA buffer. */
  1823. mgsl_load_tx_dma_buffer(info,
  1824. info->xmit_buf,info->xmit_cnt);
  1825. if ( debug_level >= DEBUG_LEVEL_INFO )
  1826. printk( "%s(%d):mgsl_write(%s) sync xmit_cnt flushing\n",
  1827. __FILE__,__LINE__,info->device_name);
  1828. } else {
  1829. if ( debug_level >= DEBUG_LEVEL_INFO )
  1830. printk( "%s(%d):mgsl_write(%s) sync transmit accepted\n",
  1831. __FILE__,__LINE__,info->device_name);
  1832. ret = count;
  1833. info->xmit_cnt = count;
  1834. mgsl_load_tx_dma_buffer(info,buf,count);
  1835. }
  1836. } else {
  1837. while (1) {
  1838. spin_lock_irqsave(&info->irq_spinlock,flags);
  1839. c = min_t(int, count,
  1840. min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
  1841. SERIAL_XMIT_SIZE - info->xmit_head));
  1842. if (c <= 0) {
  1843. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  1844. break;
  1845. }
  1846. memcpy(info->xmit_buf + info->xmit_head, buf, c);
  1847. info->xmit_head = ((info->xmit_head + c) &
  1848. (SERIAL_XMIT_SIZE-1));
  1849. info->xmit_cnt += c;
  1850. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  1851. buf += c;
  1852. count -= c;
  1853. ret += c;
  1854. }
  1855. }
  1856. if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
  1857. spin_lock_irqsave(&info->irq_spinlock,flags);
  1858. if (!info->tx_active)
  1859. usc_start_transmitter(info);
  1860. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  1861. }
  1862. cleanup:
  1863. if ( debug_level >= DEBUG_LEVEL_INFO )
  1864. printk( "%s(%d):mgsl_write(%s) returning=%d\n",
  1865. __FILE__,__LINE__,info->device_name,ret);
  1866. return ret;
  1867. } /* end of mgsl_write() */
  1868. /* mgsl_write_room()
  1869. *
  1870. * Return the count of free bytes in transmit buffer
  1871. *
  1872. * Arguments: tty pointer to tty info structure
  1873. * Return Value: None
  1874. */
  1875. static int mgsl_write_room(struct tty_struct *tty)
  1876. {
  1877. struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
  1878. int ret;
  1879. if (mgsl_paranoia_check(info, tty->name, "mgsl_write_room"))
  1880. return 0;
  1881. ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
  1882. if (ret < 0)
  1883. ret = 0;
  1884. if (debug_level >= DEBUG_LEVEL_INFO)
  1885. printk("%s(%d):mgsl_write_room(%s)=%d\n",
  1886. __FILE__,__LINE__, info->device_name,ret );
  1887. if ( info->params.mode == MGSL_MODE_HDLC ||
  1888. info->params.mode == MGSL_MODE_RAW ) {
  1889. /* operating in synchronous (frame oriented) mode */
  1890. if ( info->tx_active )
  1891. return 0;
  1892. else
  1893. return HDLC_MAX_FRAME_SIZE;
  1894. }
  1895. return ret;
  1896. } /* end of mgsl_write_room() */
  1897. /* mgsl_chars_in_buffer()
  1898. *
  1899. * Return the count of bytes in transmit buffer
  1900. *
  1901. * Arguments: tty pointer to tty info structure
  1902. * Return Value: None
  1903. */
  1904. static int mgsl_chars_in_buffer(struct tty_struct *tty)
  1905. {
  1906. struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
  1907. if (debug_level >= DEBUG_LEVEL_INFO)
  1908. printk("%s(%d):mgsl_chars_in_buffer(%s)\n",
  1909. __FILE__,__LINE__, info->device_name );
  1910. if (mgsl_paranoia_check(info, tty->name, "mgsl_chars_in_buffer"))
  1911. return 0;
  1912. if (debug_level >= DEBUG_LEVEL_INFO)
  1913. printk("%s(%d):mgsl_chars_in_buffer(%s)=%d\n",
  1914. __FILE__,__LINE__, info->device_name,info->xmit_cnt );
  1915. if ( info->params.mode == MGSL_MODE_HDLC ||
  1916. info->params.mode == MGSL_MODE_RAW ) {
  1917. /* operating in synchronous (frame oriented) mode */
  1918. if ( info->tx_active )
  1919. return info->max_frame_size;
  1920. else
  1921. return 0;
  1922. }
  1923. return info->xmit_cnt;
  1924. } /* end of mgsl_chars_in_buffer() */
  1925. /* mgsl_flush_buffer()
  1926. *
  1927. * Discard all data in the send buffer
  1928. *
  1929. * Arguments: tty pointer to tty info structure
  1930. * Return Value: None
  1931. */
  1932. static void mgsl_flush_buffer(struct tty_struct *tty)
  1933. {
  1934. struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
  1935. unsigned long flags;
  1936. if (debug_level >= DEBUG_LEVEL_INFO)
  1937. printk("%s(%d):mgsl_flush_buffer(%s) entry\n",
  1938. __FILE__,__LINE__, info->device_name );
  1939. if (mgsl_paranoia_check(info, tty->name, "mgsl_flush_buffer"))
  1940. return;
  1941. spin_lock_irqsave(&info->irq_spinlock,flags);
  1942. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  1943. del_timer(&info->tx_timer);
  1944. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  1945. tty_wakeup(tty);
  1946. }
  1947. /* mgsl_send_xchar()
  1948. *
  1949. * Send a high-priority XON/XOFF character
  1950. *
  1951. * Arguments: tty pointer to tty info structure
  1952. * ch character to send
  1953. * Return Value: None
  1954. */
  1955. static void mgsl_send_xchar(struct tty_struct *tty, char ch)
  1956. {
  1957. struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
  1958. unsigned long flags;
  1959. if (debug_level >= DEBUG_LEVEL_INFO)
  1960. printk("%s(%d):mgsl_send_xchar(%s,%d)\n",
  1961. __FILE__,__LINE__, info->device_name, ch );
  1962. if (mgsl_paranoia_check(info, tty->name, "mgsl_send_xchar"))
  1963. return;
  1964. info->x_char = ch;
  1965. if (ch) {
  1966. /* Make sure transmit interrupts are on */
  1967. spin_lock_irqsave(&info->irq_spinlock,flags);
  1968. if (!info->tx_enabled)
  1969. usc_start_transmitter(info);
  1970. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  1971. }
  1972. } /* end of mgsl_send_xchar() */
  1973. /* mgsl_throttle()
  1974. *
  1975. * Signal remote device to throttle send data (our receive data)
  1976. *
  1977. * Arguments: tty pointer to tty info structure
  1978. * Return Value: None
  1979. */
  1980. static void mgsl_throttle(struct tty_struct * tty)
  1981. {
  1982. struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
  1983. unsigned long flags;
  1984. if (debug_level >= DEBUG_LEVEL_INFO)
  1985. printk("%s(%d):mgsl_throttle(%s) entry\n",
  1986. __FILE__,__LINE__, info->device_name );
  1987. if (mgsl_paranoia_check(info, tty->name, "mgsl_throttle"))
  1988. return;
  1989. if (I_IXOFF(tty))
  1990. mgsl_send_xchar(tty, STOP_CHAR(tty));
  1991. if (tty->termios->c_cflag & CRTSCTS) {
  1992. spin_lock_irqsave(&info->irq_spinlock,flags);
  1993. info->serial_signals &= ~SerialSignal_RTS;
  1994. usc_set_serial_signals(info);
  1995. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  1996. }
  1997. } /* end of mgsl_throttle() */
  1998. /* mgsl_unthrottle()
  1999. *
  2000. * Signal remote device to stop throttling send data (our receive data)
  2001. *
  2002. * Arguments: tty pointer to tty info structure
  2003. * Return Value: None
  2004. */
  2005. static void mgsl_unthrottle(struct tty_struct * tty)
  2006. {
  2007. struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
  2008. unsigned long flags;
  2009. if (debug_level >= DEBUG_LEVEL_INFO)
  2010. printk("%s(%d):mgsl_unthrottle(%s) entry\n",
  2011. __FILE__,__LINE__, info->device_name );
  2012. if (mgsl_paranoia_check(info, tty->name, "mgsl_unthrottle"))
  2013. return;
  2014. if (I_IXOFF(tty)) {
  2015. if (info->x_char)
  2016. info->x_char = 0;
  2017. else
  2018. mgsl_send_xchar(tty, START_CHAR(tty));
  2019. }
  2020. if (tty->termios->c_cflag & CRTSCTS) {
  2021. spin_lock_irqsave(&info->irq_spinlock,flags);
  2022. info->serial_signals |= SerialSignal_RTS;
  2023. usc_set_serial_signals(info);
  2024. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2025. }
  2026. } /* end of mgsl_unthrottle() */
  2027. /* mgsl_get_stats()
  2028. *
  2029. * get the current serial parameters information
  2030. *
  2031. * Arguments: info pointer to device instance data
  2032. * user_icount pointer to buffer to hold returned stats
  2033. *
  2034. * Return Value: 0 if success, otherwise error code
  2035. */
  2036. static int mgsl_get_stats(struct mgsl_struct * info, struct mgsl_icount __user *user_icount)
  2037. {
  2038. int err;
  2039. if (debug_level >= DEBUG_LEVEL_INFO)
  2040. printk("%s(%d):mgsl_get_params(%s)\n",
  2041. __FILE__,__LINE__, info->device_name);
  2042. if (!user_icount) {
  2043. memset(&info->icount, 0, sizeof(info->icount));
  2044. } else {
  2045. COPY_TO_USER(err, user_icount, &info->icount, sizeof(struct mgsl_icount));
  2046. if (err)
  2047. return -EFAULT;
  2048. }
  2049. return 0;
  2050. } /* end of mgsl_get_stats() */
  2051. /* mgsl_get_params()
  2052. *
  2053. * get the current serial parameters information
  2054. *
  2055. * Arguments: info pointer to device instance data
  2056. * user_params pointer to buffer to hold returned params
  2057. *
  2058. * Return Value: 0 if success, otherwise error code
  2059. */
  2060. static int mgsl_get_params(struct mgsl_struct * info, MGSL_PARAMS __user *user_params)
  2061. {
  2062. int err;
  2063. if (debug_level >= DEBUG_LEVEL_INFO)
  2064. printk("%s(%d):mgsl_get_params(%s)\n",
  2065. __FILE__,__LINE__, info->device_name);
  2066. COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS));
  2067. if (err) {
  2068. if ( debug_level >= DEBUG_LEVEL_INFO )
  2069. printk( "%s(%d):mgsl_get_params(%s) user buffer copy failed\n",
  2070. __FILE__,__LINE__,info->device_name);
  2071. return -EFAULT;
  2072. }
  2073. return 0;
  2074. } /* end of mgsl_get_params() */
  2075. /* mgsl_set_params()
  2076. *
  2077. * set the serial parameters
  2078. *
  2079. * Arguments:
  2080. *
  2081. * info pointer to device instance data
  2082. * new_params user buffer containing new serial params
  2083. *
  2084. * Return Value: 0 if success, otherwise error code
  2085. */
  2086. static int mgsl_set_params(struct mgsl_struct * info, MGSL_PARAMS __user *new_params)
  2087. {
  2088. unsigned long flags;
  2089. MGSL_PARAMS tmp_params;
  2090. int err;
  2091. if (debug_level >= DEBUG_LEVEL_INFO)
  2092. printk("%s(%d):mgsl_set_params %s\n", __FILE__,__LINE__,
  2093. info->device_name );
  2094. COPY_FROM_USER(err,&tmp_params, new_params, sizeof(MGSL_PARAMS));
  2095. if (err) {
  2096. if ( debug_level >= DEBUG_LEVEL_INFO )
  2097. printk( "%s(%d):mgsl_set_params(%s) user buffer copy failed\n",
  2098. __FILE__,__LINE__,info->device_name);
  2099. return -EFAULT;
  2100. }
  2101. spin_lock_irqsave(&info->irq_spinlock,flags);
  2102. memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
  2103. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2104. mgsl_change_params(info);
  2105. return 0;
  2106. } /* end of mgsl_set_params() */
  2107. /* mgsl_get_txidle()
  2108. *
  2109. * get the current transmit idle mode
  2110. *
  2111. * Arguments: info pointer to device instance data
  2112. * idle_mode pointer to buffer to hold returned idle mode
  2113. *
  2114. * Return Value: 0 if success, otherwise error code
  2115. */
  2116. static int mgsl_get_txidle(struct mgsl_struct * info, int __user *idle_mode)
  2117. {
  2118. int err;
  2119. if (debug_level >= DEBUG_LEVEL_INFO)
  2120. printk("%s(%d):mgsl_get_txidle(%s)=%d\n",
  2121. __FILE__,__LINE__, info->device_name, info->idle_mode);
  2122. COPY_TO_USER(err,idle_mode, &info->idle_mode, sizeof(int));
  2123. if (err) {
  2124. if ( debug_level >= DEBUG_LEVEL_INFO )
  2125. printk( "%s(%d):mgsl_get_txidle(%s) user buffer copy failed\n",
  2126. __FILE__,__LINE__,info->device_name);
  2127. return -EFAULT;
  2128. }
  2129. return 0;
  2130. } /* end of mgsl_get_txidle() */
  2131. /* mgsl_set_txidle() service ioctl to set transmit idle mode
  2132. *
  2133. * Arguments: info pointer to device instance data
  2134. * idle_mode new idle mode
  2135. *
  2136. * Return Value: 0 if success, otherwise error code
  2137. */
  2138. static int mgsl_set_txidle(struct mgsl_struct * info, int idle_mode)
  2139. {
  2140. unsigned long flags;
  2141. if (debug_level >= DEBUG_LEVEL_INFO)
  2142. printk("%s(%d):mgsl_set_txidle(%s,%d)\n", __FILE__,__LINE__,
  2143. info->device_name, idle_mode );
  2144. spin_lock_irqsave(&info->irq_spinlock,flags);
  2145. info->idle_mode = idle_mode;
  2146. usc_set_txidle( info );
  2147. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2148. return 0;
  2149. } /* end of mgsl_set_txidle() */
  2150. /* mgsl_txenable()
  2151. *
  2152. * enable or disable the transmitter
  2153. *
  2154. * Arguments:
  2155. *
  2156. * info pointer to device instance data
  2157. * enable 1 = enable, 0 = disable
  2158. *
  2159. * Return Value: 0 if success, otherwise error code
  2160. */
  2161. static int mgsl_txenable(struct mgsl_struct * info, int enable)
  2162. {
  2163. unsigned long flags;
  2164. if (debug_level >= DEBUG_LEVEL_INFO)
  2165. printk("%s(%d):mgsl_txenable(%s,%d)\n", __FILE__,__LINE__,
  2166. info->device_name, enable);
  2167. spin_lock_irqsave(&info->irq_spinlock,flags);
  2168. if ( enable ) {
  2169. if ( !info->tx_enabled ) {
  2170. usc_start_transmitter(info);
  2171. /*--------------------------------------------------
  2172. * if HDLC/SDLC Loop mode, attempt to insert the
  2173. * station in the 'loop' by setting CMR:13. Upon
  2174. * receipt of the next GoAhead (RxAbort) sequence,
  2175. * the OnLoop indicator (CCSR:7) should go active
  2176. * to indicate that we are on the loop
  2177. *--------------------------------------------------*/
  2178. if ( info->params.flags & HDLC_FLAG_HDLC_LOOPMODE )
  2179. usc_loopmode_insert_request( info );
  2180. }
  2181. } else {
  2182. if ( info->tx_enabled )
  2183. usc_stop_transmitter(info);
  2184. }
  2185. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2186. return 0;
  2187. } /* end of mgsl_txenable() */
  2188. /* mgsl_txabort() abort send HDLC frame
  2189. *
  2190. * Arguments: info pointer to device instance data
  2191. * Return Value: 0 if success, otherwise error code
  2192. */
  2193. static int mgsl_txabort(struct mgsl_struct * info)
  2194. {
  2195. unsigned long flags;
  2196. if (debug_level >= DEBUG_LEVEL_INFO)
  2197. printk("%s(%d):mgsl_txabort(%s)\n", __FILE__,__LINE__,
  2198. info->device_name);
  2199. spin_lock_irqsave(&info->irq_spinlock,flags);
  2200. if ( info->tx_active && info->params.mode == MGSL_MODE_HDLC )
  2201. {
  2202. if ( info->params.flags & HDLC_FLAG_HDLC_LOOPMODE )
  2203. usc_loopmode_cancel_transmit( info );
  2204. else
  2205. usc_TCmd(info,TCmd_SendAbort);
  2206. }
  2207. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2208. return 0;
  2209. } /* end of mgsl_txabort() */
  2210. /* mgsl_rxenable() enable or disable the receiver
  2211. *
  2212. * Arguments: info pointer to device instance data
  2213. * enable 1 = enable, 0 = disable
  2214. * Return Value: 0 if success, otherwise error code
  2215. */
  2216. static int mgsl_rxenable(struct mgsl_struct * info, int enable)
  2217. {
  2218. unsigned long flags;
  2219. if (debug_level >= DEBUG_LEVEL_INFO)
  2220. printk("%s(%d):mgsl_rxenable(%s,%d)\n", __FILE__,__LINE__,
  2221. info->device_name, enable);
  2222. spin_lock_irqsave(&info->irq_spinlock,flags);
  2223. if ( enable ) {
  2224. if ( !info->rx_enabled )
  2225. usc_start_receiver(info);
  2226. } else {
  2227. if ( info->rx_enabled )
  2228. usc_stop_receiver(info);
  2229. }
  2230. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2231. return 0;
  2232. } /* end of mgsl_rxenable() */
  2233. /* mgsl_wait_event() wait for specified event to occur
  2234. *
  2235. * Arguments: info pointer to device instance data
  2236. * mask pointer to bitmask of events to wait for
  2237. * Return Value: 0 if successful and bit mask updated with
  2238. * of events triggerred,
  2239. * otherwise error code
  2240. */
  2241. static int mgsl_wait_event(struct mgsl_struct * info, int __user * mask_ptr)
  2242. {
  2243. unsigned long flags;
  2244. int s;
  2245. int rc=0;
  2246. struct mgsl_icount cprev, cnow;
  2247. int events;
  2248. int mask;
  2249. struct _input_signal_events oldsigs, newsigs;
  2250. DECLARE_WAITQUEUE(wait, current);
  2251. COPY_FROM_USER(rc,&mask, mask_ptr, sizeof(int));
  2252. if (rc) {
  2253. return -EFAULT;
  2254. }
  2255. if (debug_level >= DEBUG_LEVEL_INFO)
  2256. printk("%s(%d):mgsl_wait_event(%s,%d)\n", __FILE__,__LINE__,
  2257. info->device_name, mask);
  2258. spin_lock_irqsave(&info->irq_spinlock,flags);
  2259. /* return immediately if state matches requested events */
  2260. usc_get_serial_signals(info);
  2261. s = info->serial_signals;
  2262. events = mask &
  2263. ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
  2264. ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
  2265. ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
  2266. ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
  2267. if (events) {
  2268. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2269. goto exit;
  2270. }
  2271. /* save current irq counts */
  2272. cprev = info->icount;
  2273. oldsigs = info->input_signal_events;
  2274. /* enable hunt and idle irqs if needed */
  2275. if (mask & (MgslEvent_ExitHuntMode + MgslEvent_IdleReceived)) {
  2276. u16 oldreg = usc_InReg(info,RICR);
  2277. u16 newreg = oldreg +
  2278. (mask & MgslEvent_ExitHuntMode ? RXSTATUS_EXITED_HUNT:0) +
  2279. (mask & MgslEvent_IdleReceived ? RXSTATUS_IDLE_RECEIVED:0);
  2280. if (oldreg != newreg)
  2281. usc_OutReg(info, RICR, newreg);
  2282. }
  2283. set_current_state(TASK_INTERRUPTIBLE);
  2284. add_wait_queue(&info->event_wait_q, &wait);
  2285. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2286. for(;;) {
  2287. schedule();
  2288. if (signal_pending(current)) {
  2289. rc = -ERESTARTSYS;
  2290. break;
  2291. }
  2292. /* get current irq counts */
  2293. spin_lock_irqsave(&info->irq_spinlock,flags);
  2294. cnow = info->icount;
  2295. newsigs = info->input_signal_events;
  2296. set_current_state(TASK_INTERRUPTIBLE);
  2297. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2298. /* if no change, wait aborted for some reason */
  2299. if (newsigs.dsr_up == oldsigs.dsr_up &&
  2300. newsigs.dsr_down == oldsigs.dsr_down &&
  2301. newsigs.dcd_up == oldsigs.dcd_up &&
  2302. newsigs.dcd_down == oldsigs.dcd_down &&
  2303. newsigs.cts_up == oldsigs.cts_up &&
  2304. newsigs.cts_down == oldsigs.cts_down &&
  2305. newsigs.ri_up == oldsigs.ri_up &&
  2306. newsigs.ri_down == oldsigs.ri_down &&
  2307. cnow.exithunt == cprev.exithunt &&
  2308. cnow.rxidle == cprev.rxidle) {
  2309. rc = -EIO;
  2310. break;
  2311. }
  2312. events = mask &
  2313. ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
  2314. (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
  2315. (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
  2316. (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
  2317. (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
  2318. (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
  2319. (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
  2320. (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
  2321. (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
  2322. (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
  2323. if (events)
  2324. break;
  2325. cprev = cnow;
  2326. oldsigs = newsigs;
  2327. }
  2328. remove_wait_queue(&info->event_wait_q, &wait);
  2329. set_current_state(TASK_RUNNING);
  2330. if (mask & (MgslEvent_ExitHuntMode + MgslEvent_IdleReceived)) {
  2331. spin_lock_irqsave(&info->irq_spinlock,flags);
  2332. if (!waitqueue_active(&info->event_wait_q)) {
  2333. /* disable enable exit hunt mode/idle rcvd IRQs */
  2334. usc_OutReg(info, RICR, usc_InReg(info,RICR) &
  2335. ~(RXSTATUS_EXITED_HUNT + RXSTATUS_IDLE_RECEIVED));
  2336. }
  2337. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2338. }
  2339. exit:
  2340. if ( rc == 0 )
  2341. PUT_USER(rc, events, mask_ptr);
  2342. return rc;
  2343. } /* end of mgsl_wait_event() */
  2344. static int modem_input_wait(struct mgsl_struct *info,int arg)
  2345. {
  2346. unsigned long flags;
  2347. int rc;
  2348. struct mgsl_icount cprev, cnow;
  2349. DECLARE_WAITQUEUE(wait, current);
  2350. /* save current irq counts */
  2351. spin_lock_irqsave(&info->irq_spinlock,flags);
  2352. cprev = info->icount;
  2353. add_wait_queue(&info->status_event_wait_q, &wait);
  2354. set_current_state(TASK_INTERRUPTIBLE);
  2355. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2356. for(;;) {
  2357. schedule();
  2358. if (signal_pending(current)) {
  2359. rc = -ERESTARTSYS;
  2360. break;
  2361. }
  2362. /* get new irq counts */
  2363. spin_lock_irqsave(&info->irq_spinlock,flags);
  2364. cnow = info->icount;
  2365. set_current_state(TASK_INTERRUPTIBLE);
  2366. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2367. /* if no change, wait aborted for some reason */
  2368. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  2369. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
  2370. rc = -EIO;
  2371. break;
  2372. }
  2373. /* check for change in caller specified modem input */
  2374. if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
  2375. (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
  2376. (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
  2377. (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
  2378. rc = 0;
  2379. break;
  2380. }
  2381. cprev = cnow;
  2382. }
  2383. remove_wait_queue(&info->status_event_wait_q, &wait);
  2384. set_current_state(TASK_RUNNING);
  2385. return rc;
  2386. }
  2387. /* return the state of the serial control and status signals
  2388. */
  2389. static int tiocmget(struct tty_struct *tty, struct file *file)
  2390. {
  2391. struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
  2392. unsigned int result;
  2393. unsigned long flags;
  2394. spin_lock_irqsave(&info->irq_spinlock,flags);
  2395. usc_get_serial_signals(info);
  2396. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2397. result = ((info->serial_signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
  2398. ((info->serial_signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
  2399. ((info->serial_signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
  2400. ((info->serial_signals & SerialSignal_RI) ? TIOCM_RNG:0) +
  2401. ((info->serial_signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
  2402. ((info->serial_signals & SerialSignal_CTS) ? TIOCM_CTS:0);
  2403. if (debug_level >= DEBUG_LEVEL_INFO)
  2404. printk("%s(%d):%s tiocmget() value=%08X\n",
  2405. __FILE__,__LINE__, info->device_name, result );
  2406. return result;
  2407. }
  2408. /* set modem control signals (DTR/RTS)
  2409. */
  2410. static int tiocmset(struct tty_struct *tty, struct file *file,
  2411. unsigned int set, unsigned int clear)
  2412. {
  2413. struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
  2414. unsigned long flags;
  2415. if (debug_level >= DEBUG_LEVEL_INFO)
  2416. printk("%s(%d):%s tiocmset(%x,%x)\n",
  2417. __FILE__,__LINE__,info->device_name, set, clear);
  2418. if (set & TIOCM_RTS)
  2419. info->serial_signals |= SerialSignal_RTS;
  2420. if (set & TIOCM_DTR)
  2421. info->serial_signals |= SerialSignal_DTR;
  2422. if (clear & TIOCM_RTS)
  2423. info->serial_signals &= ~SerialSignal_RTS;
  2424. if (clear & TIOCM_DTR)
  2425. info->serial_signals &= ~SerialSignal_DTR;
  2426. spin_lock_irqsave(&info->irq_spinlock,flags);
  2427. usc_set_serial_signals(info);
  2428. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2429. return 0;
  2430. }
  2431. /* mgsl_break() Set or clear transmit break condition
  2432. *
  2433. * Arguments: tty pointer to tty instance data
  2434. * break_state -1=set break condition, 0=clear
  2435. * Return Value: None
  2436. */
  2437. static void mgsl_break(struct tty_struct *tty, int break_state)
  2438. {
  2439. struct mgsl_struct * info = (struct mgsl_struct *)tty->driver_data;
  2440. unsigned long flags;
  2441. if (debug_level >= DEBUG_LEVEL_INFO)
  2442. printk("%s(%d):mgsl_break(%s,%d)\n",
  2443. __FILE__,__LINE__, info->device_name, break_state);
  2444. if (mgsl_paranoia_check(info, tty->name, "mgsl_break"))
  2445. return;
  2446. spin_lock_irqsave(&info->irq_spinlock,flags);
  2447. if (break_state == -1)
  2448. usc_OutReg(info,IOCR,(u16)(usc_InReg(info,IOCR) | BIT7));
  2449. else
  2450. usc_OutReg(info,IOCR,(u16)(usc_InReg(info,IOCR) & ~BIT7));
  2451. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2452. } /* end of mgsl_break() */
  2453. /* mgsl_ioctl() Service an IOCTL request
  2454. *
  2455. * Arguments:
  2456. *
  2457. * tty pointer to tty instance data
  2458. * file pointer to associated file object for device
  2459. * cmd IOCTL command code
  2460. * arg command argument/context
  2461. *
  2462. * Return Value: 0 if success, otherwise error code
  2463. */
  2464. static int mgsl_ioctl(struct tty_struct *tty, struct file * file,
  2465. unsigned int cmd, unsigned long arg)
  2466. {
  2467. struct mgsl_struct * info = (struct mgsl_struct *)tty->driver_data;
  2468. int ret;
  2469. if (debug_level >= DEBUG_LEVEL_INFO)
  2470. printk("%s(%d):mgsl_ioctl %s cmd=%08X\n", __FILE__,__LINE__,
  2471. info->device_name, cmd );
  2472. if (mgsl_paranoia_check(info, tty->name, "mgsl_ioctl"))
  2473. return -ENODEV;
  2474. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  2475. (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
  2476. if (tty->flags & (1 << TTY_IO_ERROR))
  2477. return -EIO;
  2478. }
  2479. lock_kernel();
  2480. ret = mgsl_ioctl_common(info, cmd, arg);
  2481. unlock_kernel();
  2482. return ret;
  2483. }
  2484. static int mgsl_ioctl_common(struct mgsl_struct *info, unsigned int cmd, unsigned long arg)
  2485. {
  2486. int error;
  2487. struct mgsl_icount cnow; /* kernel counter temps */
  2488. void __user *argp = (void __user *)arg;
  2489. struct serial_icounter_struct __user *p_cuser; /* user space */
  2490. unsigned long flags;
  2491. switch (cmd) {
  2492. case MGSL_IOCGPARAMS:
  2493. return mgsl_get_params(info, argp);
  2494. case MGSL_IOCSPARAMS:
  2495. return mgsl_set_params(info, argp);
  2496. case MGSL_IOCGTXIDLE:
  2497. return mgsl_get_txidle(info, argp);
  2498. case MGSL_IOCSTXIDLE:
  2499. return mgsl_set_txidle(info,(int)arg);
  2500. case MGSL_IOCTXENABLE:
  2501. return mgsl_txenable(info,(int)arg);
  2502. case MGSL_IOCRXENABLE:
  2503. return mgsl_rxenable(info,(int)arg);
  2504. case MGSL_IOCTXABORT:
  2505. return mgsl_txabort(info);
  2506. case MGSL_IOCGSTATS:
  2507. return mgsl_get_stats(info, argp);
  2508. case MGSL_IOCWAITEVENT:
  2509. return mgsl_wait_event(info, argp);
  2510. case MGSL_IOCLOOPTXDONE:
  2511. return mgsl_loopmode_send_done(info);
  2512. /* Wait for modem input (DCD,RI,DSR,CTS) change
  2513. * as specified by mask in arg (TIOCM_RNG/DSR/CD/CTS)
  2514. */
  2515. case TIOCMIWAIT:
  2516. return modem_input_wait(info,(int)arg);
  2517. /*
  2518. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  2519. * Return: write counters to the user passed counter struct
  2520. * NB: both 1->0 and 0->1 transitions are counted except for
  2521. * RI where only 0->1 is counted.
  2522. */
  2523. case TIOCGICOUNT:
  2524. spin_lock_irqsave(&info->irq_spinlock,flags);
  2525. cnow = info->icount;
  2526. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2527. p_cuser = argp;
  2528. PUT_USER(error,cnow.cts, &p_cuser->cts);
  2529. if (error) return error;
  2530. PUT_USER(error,cnow.dsr, &p_cuser->dsr);
  2531. if (error) return error;
  2532. PUT_USER(error,cnow.rng, &p_cuser->rng);
  2533. if (error) return error;
  2534. PUT_USER(error,cnow.dcd, &p_cuser->dcd);
  2535. if (error) return error;
  2536. PUT_USER(error,cnow.rx, &p_cuser->rx);
  2537. if (error) return error;
  2538. PUT_USER(error,cnow.tx, &p_cuser->tx);
  2539. if (error) return error;
  2540. PUT_USER(error,cnow.frame, &p_cuser->frame);
  2541. if (error) return error;
  2542. PUT_USER(error,cnow.overrun, &p_cuser->overrun);
  2543. if (error) return error;
  2544. PUT_USER(error,cnow.parity, &p_cuser->parity);
  2545. if (error) return error;
  2546. PUT_USER(error,cnow.brk, &p_cuser->brk);
  2547. if (error) return error;
  2548. PUT_USER(error,cnow.buf_overrun, &p_cuser->buf_overrun);
  2549. if (error) return error;
  2550. return 0;
  2551. default:
  2552. return -ENOIOCTLCMD;
  2553. }
  2554. return 0;
  2555. }
  2556. /* mgsl_set_termios()
  2557. *
  2558. * Set new termios settings
  2559. *
  2560. * Arguments:
  2561. *
  2562. * tty pointer to tty structure
  2563. * termios pointer to buffer to hold returned old termios
  2564. *
  2565. * Return Value: None
  2566. */
  2567. static void mgsl_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
  2568. {
  2569. struct mgsl_struct *info = (struct mgsl_struct *)tty->driver_data;
  2570. unsigned long flags;
  2571. if (debug_level >= DEBUG_LEVEL_INFO)
  2572. printk("%s(%d):mgsl_set_termios %s\n", __FILE__,__LINE__,
  2573. tty->driver->name );
  2574. mgsl_change_params(info);
  2575. /* Handle transition to B0 status */
  2576. if (old_termios->c_cflag & CBAUD &&
  2577. !(tty->termios->c_cflag & CBAUD)) {
  2578. info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
  2579. spin_lock_irqsave(&info->irq_spinlock,flags);
  2580. usc_set_serial_signals(info);
  2581. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2582. }
  2583. /* Handle transition away from B0 status */
  2584. if (!(old_termios->c_cflag & CBAUD) &&
  2585. tty->termios->c_cflag & CBAUD) {
  2586. info->serial_signals |= SerialSignal_DTR;
  2587. if (!(tty->termios->c_cflag & CRTSCTS) ||
  2588. !test_bit(TTY_THROTTLED, &tty->flags)) {
  2589. info->serial_signals |= SerialSignal_RTS;
  2590. }
  2591. spin_lock_irqsave(&info->irq_spinlock,flags);
  2592. usc_set_serial_signals(info);
  2593. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2594. }
  2595. /* Handle turning off CRTSCTS */
  2596. if (old_termios->c_cflag & CRTSCTS &&
  2597. !(tty->termios->c_cflag & CRTSCTS)) {
  2598. tty->hw_stopped = 0;
  2599. mgsl_start(tty);
  2600. }
  2601. } /* end of mgsl_set_termios() */
  2602. /* mgsl_close()
  2603. *
  2604. * Called when port is closed. Wait for remaining data to be
  2605. * sent. Disable port and free resources.
  2606. *
  2607. * Arguments:
  2608. *
  2609. * tty pointer to open tty structure
  2610. * filp pointer to open file object
  2611. *
  2612. * Return Value: None
  2613. */
  2614. static void mgsl_close(struct tty_struct *tty, struct file * filp)
  2615. {
  2616. struct mgsl_struct * info = (struct mgsl_struct *)tty->driver_data;
  2617. if (mgsl_paranoia_check(info, tty->name, "mgsl_close"))
  2618. return;
  2619. if (debug_level >= DEBUG_LEVEL_INFO)
  2620. printk("%s(%d):mgsl_close(%s) entry, count=%d\n",
  2621. __FILE__,__LINE__, info->device_name, info->count);
  2622. if (!info->count)
  2623. return;
  2624. if (tty_hung_up_p(filp))
  2625. goto cleanup;
  2626. if ((tty->count == 1) && (info->count != 1)) {
  2627. /*
  2628. * tty->count is 1 and the tty structure will be freed.
  2629. * info->count should be one in this case.
  2630. * if it's not, correct it so that the port is shutdown.
  2631. */
  2632. printk("mgsl_close: bad refcount; tty->count is 1, "
  2633. "info->count is %d\n", info->count);
  2634. info->count = 1;
  2635. }
  2636. info->count--;
  2637. /* if at least one open remaining, leave hardware active */
  2638. if (info->count)
  2639. goto cleanup;
  2640. info->flags |= ASYNC_CLOSING;
  2641. /* set tty->closing to notify line discipline to
  2642. * only process XON/XOFF characters. Only the N_TTY
  2643. * discipline appears to use this (ppp does not).
  2644. */
  2645. tty->closing = 1;
  2646. /* wait for transmit data to clear all layers */
  2647. if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE) {
  2648. if (debug_level >= DEBUG_LEVEL_INFO)
  2649. printk("%s(%d):mgsl_close(%s) calling tty_wait_until_sent\n",
  2650. __FILE__,__LINE__, info->device_name );
  2651. tty_wait_until_sent(tty, info->closing_wait);
  2652. }
  2653. if (info->flags & ASYNC_INITIALIZED)
  2654. mgsl_wait_until_sent(tty, info->timeout);
  2655. if (tty->driver->flush_buffer)
  2656. tty->driver->flush_buffer(tty);
  2657. tty_ldisc_flush(tty);
  2658. shutdown(info);
  2659. tty->closing = 0;
  2660. info->tty = NULL;
  2661. if (info->blocked_open) {
  2662. if (info->close_delay) {
  2663. msleep_interruptible(jiffies_to_msecs(info->close_delay));
  2664. }
  2665. wake_up_interruptible(&info->open_wait);
  2666. }
  2667. info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
  2668. wake_up_interruptible(&info->close_wait);
  2669. cleanup:
  2670. if (debug_level >= DEBUG_LEVEL_INFO)
  2671. printk("%s(%d):mgsl_close(%s) exit, count=%d\n", __FILE__,__LINE__,
  2672. tty->driver->name, info->count);
  2673. } /* end of mgsl_close() */
  2674. /* mgsl_wait_until_sent()
  2675. *
  2676. * Wait until the transmitter is empty.
  2677. *
  2678. * Arguments:
  2679. *
  2680. * tty pointer to tty info structure
  2681. * timeout time to wait for send completion
  2682. *
  2683. * Return Value: None
  2684. */
  2685. static void mgsl_wait_until_sent(struct tty_struct *tty, int timeout)
  2686. {
  2687. struct mgsl_struct * info = (struct mgsl_struct *)tty->driver_data;
  2688. unsigned long orig_jiffies, char_time;
  2689. if (!info )
  2690. return;
  2691. if (debug_level >= DEBUG_LEVEL_INFO)
  2692. printk("%s(%d):mgsl_wait_until_sent(%s) entry\n",
  2693. __FILE__,__LINE__, info->device_name );
  2694. if (mgsl_paranoia_check(info, tty->name, "mgsl_wait_until_sent"))
  2695. return;
  2696. if (!(info->flags & ASYNC_INITIALIZED))
  2697. goto exit;
  2698. orig_jiffies = jiffies;
  2699. /* Set check interval to 1/5 of estimated time to
  2700. * send a character, and make it at least 1. The check
  2701. * interval should also be less than the timeout.
  2702. * Note: use tight timings here to satisfy the NIST-PCTS.
  2703. */
  2704. if ( info->params.data_rate ) {
  2705. char_time = info->timeout/(32 * 5);
  2706. if (!char_time)
  2707. char_time++;
  2708. } else
  2709. char_time = 1;
  2710. if (timeout)
  2711. char_time = min_t(unsigned long, char_time, timeout);
  2712. if ( info->params.mode == MGSL_MODE_HDLC ||
  2713. info->params.mode == MGSL_MODE_RAW ) {
  2714. while (info->tx_active) {
  2715. msleep_interruptible(jiffies_to_msecs(char_time));
  2716. if (signal_pending(current))
  2717. break;
  2718. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  2719. break;
  2720. }
  2721. } else {
  2722. while (!(usc_InReg(info,TCSR) & TXSTATUS_ALL_SENT) &&
  2723. info->tx_enabled) {
  2724. msleep_interruptible(jiffies_to_msecs(char_time));
  2725. if (signal_pending(current))
  2726. break;
  2727. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  2728. break;
  2729. }
  2730. }
  2731. exit:
  2732. if (debug_level >= DEBUG_LEVEL_INFO)
  2733. printk("%s(%d):mgsl_wait_until_sent(%s) exit\n",
  2734. __FILE__,__LINE__, info->device_name );
  2735. } /* end of mgsl_wait_until_sent() */
  2736. /* mgsl_hangup()
  2737. *
  2738. * Called by tty_hangup() when a hangup is signaled.
  2739. * This is the same as to closing all open files for the port.
  2740. *
  2741. * Arguments: tty pointer to associated tty object
  2742. * Return Value: None
  2743. */
  2744. static void mgsl_hangup(struct tty_struct *tty)
  2745. {
  2746. struct mgsl_struct * info = (struct mgsl_struct *)tty->driver_data;
  2747. if (debug_level >= DEBUG_LEVEL_INFO)
  2748. printk("%s(%d):mgsl_hangup(%s)\n",
  2749. __FILE__,__LINE__, info->device_name );
  2750. if (mgsl_paranoia_check(info, tty->name, "mgsl_hangup"))
  2751. return;
  2752. mgsl_flush_buffer(tty);
  2753. shutdown(info);
  2754. info->count = 0;
  2755. info->flags &= ~ASYNC_NORMAL_ACTIVE;
  2756. info->tty = NULL;
  2757. wake_up_interruptible(&info->open_wait);
  2758. } /* end of mgsl_hangup() */
  2759. /* block_til_ready()
  2760. *
  2761. * Block the current process until the specified port
  2762. * is ready to be opened.
  2763. *
  2764. * Arguments:
  2765. *
  2766. * tty pointer to tty info structure
  2767. * filp pointer to open file object
  2768. * info pointer to device instance data
  2769. *
  2770. * Return Value: 0 if success, otherwise error code
  2771. */
  2772. static int block_til_ready(struct tty_struct *tty, struct file * filp,
  2773. struct mgsl_struct *info)
  2774. {
  2775. DECLARE_WAITQUEUE(wait, current);
  2776. int retval;
  2777. bool do_clocal = false;
  2778. bool extra_count = false;
  2779. unsigned long flags;
  2780. if (debug_level >= DEBUG_LEVEL_INFO)
  2781. printk("%s(%d):block_til_ready on %s\n",
  2782. __FILE__,__LINE__, tty->driver->name );
  2783. if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
  2784. /* nonblock mode is set or port is not enabled */
  2785. info->flags |= ASYNC_NORMAL_ACTIVE;
  2786. return 0;
  2787. }
  2788. if (tty->termios->c_cflag & CLOCAL)
  2789. do_clocal = true;
  2790. /* Wait for carrier detect and the line to become
  2791. * free (i.e., not in use by the callout). While we are in
  2792. * this loop, info->count is dropped by one, so that
  2793. * mgsl_close() knows when to free things. We restore it upon
  2794. * exit, either normal or abnormal.
  2795. */
  2796. retval = 0;
  2797. add_wait_queue(&info->open_wait, &wait);
  2798. if (debug_level >= DEBUG_LEVEL_INFO)
  2799. printk("%s(%d):block_til_ready before block on %s count=%d\n",
  2800. __FILE__,__LINE__, tty->driver->name, info->count );
  2801. spin_lock_irqsave(&info->irq_spinlock, flags);
  2802. if (!tty_hung_up_p(filp)) {
  2803. extra_count = true;
  2804. info->count--;
  2805. }
  2806. spin_unlock_irqrestore(&info->irq_spinlock, flags);
  2807. info->blocked_open++;
  2808. while (1) {
  2809. if (tty->termios->c_cflag & CBAUD) {
  2810. spin_lock_irqsave(&info->irq_spinlock,flags);
  2811. info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
  2812. usc_set_serial_signals(info);
  2813. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2814. }
  2815. set_current_state(TASK_INTERRUPTIBLE);
  2816. if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)){
  2817. retval = (info->flags & ASYNC_HUP_NOTIFY) ?
  2818. -EAGAIN : -ERESTARTSYS;
  2819. break;
  2820. }
  2821. spin_lock_irqsave(&info->irq_spinlock,flags);
  2822. usc_get_serial_signals(info);
  2823. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2824. if (!(info->flags & ASYNC_CLOSING) &&
  2825. (do_clocal || (info->serial_signals & SerialSignal_DCD)) ) {
  2826. break;
  2827. }
  2828. if (signal_pending(current)) {
  2829. retval = -ERESTARTSYS;
  2830. break;
  2831. }
  2832. if (debug_level >= DEBUG_LEVEL_INFO)
  2833. printk("%s(%d):block_til_ready blocking on %s count=%d\n",
  2834. __FILE__,__LINE__, tty->driver->name, info->count );
  2835. schedule();
  2836. }
  2837. set_current_state(TASK_RUNNING);
  2838. remove_wait_queue(&info->open_wait, &wait);
  2839. if (extra_count)
  2840. info->count++;
  2841. info->blocked_open--;
  2842. if (debug_level >= DEBUG_LEVEL_INFO)
  2843. printk("%s(%d):block_til_ready after blocking on %s count=%d\n",
  2844. __FILE__,__LINE__, tty->driver->name, info->count );
  2845. if (!retval)
  2846. info->flags |= ASYNC_NORMAL_ACTIVE;
  2847. return retval;
  2848. } /* end of block_til_ready() */
  2849. /* mgsl_open()
  2850. *
  2851. * Called when a port is opened. Init and enable port.
  2852. * Perform serial-specific initialization for the tty structure.
  2853. *
  2854. * Arguments: tty pointer to tty info structure
  2855. * filp associated file pointer
  2856. *
  2857. * Return Value: 0 if success, otherwise error code
  2858. */
  2859. static int mgsl_open(struct tty_struct *tty, struct file * filp)
  2860. {
  2861. struct mgsl_struct *info;
  2862. int retval, line;
  2863. unsigned long flags;
  2864. /* verify range of specified line number */
  2865. line = tty->index;
  2866. if ((line < 0) || (line >= mgsl_device_count)) {
  2867. printk("%s(%d):mgsl_open with invalid line #%d.\n",
  2868. __FILE__,__LINE__,line);
  2869. return -ENODEV;
  2870. }
  2871. /* find the info structure for the specified line */
  2872. info = mgsl_device_list;
  2873. while(info && info->line != line)
  2874. info = info->next_device;
  2875. if (mgsl_paranoia_check(info, tty->name, "mgsl_open"))
  2876. return -ENODEV;
  2877. tty->driver_data = info;
  2878. info->tty = tty;
  2879. if (debug_level >= DEBUG_LEVEL_INFO)
  2880. printk("%s(%d):mgsl_open(%s), old ref count = %d\n",
  2881. __FILE__,__LINE__,tty->driver->name, info->count);
  2882. /* If port is closing, signal caller to try again */
  2883. if (tty_hung_up_p(filp) || info->flags & ASYNC_CLOSING){
  2884. if (info->flags & ASYNC_CLOSING)
  2885. interruptible_sleep_on(&info->close_wait);
  2886. retval = ((info->flags & ASYNC_HUP_NOTIFY) ?
  2887. -EAGAIN : -ERESTARTSYS);
  2888. goto cleanup;
  2889. }
  2890. info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  2891. spin_lock_irqsave(&info->netlock, flags);
  2892. if (info->netcount) {
  2893. retval = -EBUSY;
  2894. spin_unlock_irqrestore(&info->netlock, flags);
  2895. goto cleanup;
  2896. }
  2897. info->count++;
  2898. spin_unlock_irqrestore(&info->netlock, flags);
  2899. if (info->count == 1) {
  2900. /* 1st open on this device, init hardware */
  2901. retval = startup(info);
  2902. if (retval < 0)
  2903. goto cleanup;
  2904. }
  2905. retval = block_til_ready(tty, filp, info);
  2906. if (retval) {
  2907. if (debug_level >= DEBUG_LEVEL_INFO)
  2908. printk("%s(%d):block_til_ready(%s) returned %d\n",
  2909. __FILE__,__LINE__, info->device_name, retval);
  2910. goto cleanup;
  2911. }
  2912. if (debug_level >= DEBUG_LEVEL_INFO)
  2913. printk("%s(%d):mgsl_open(%s) success\n",
  2914. __FILE__,__LINE__, info->device_name);
  2915. retval = 0;
  2916. cleanup:
  2917. if (retval) {
  2918. if (tty->count == 1)
  2919. info->tty = NULL; /* tty layer will release tty struct */
  2920. if(info->count)
  2921. info->count--;
  2922. }
  2923. return retval;
  2924. } /* end of mgsl_open() */
  2925. /*
  2926. * /proc fs routines....
  2927. */
  2928. static inline int line_info(char *buf, struct mgsl_struct *info)
  2929. {
  2930. char stat_buf[30];
  2931. int ret;
  2932. unsigned long flags;
  2933. if (info->bus_type == MGSL_BUS_TYPE_PCI) {
  2934. ret = sprintf(buf, "%s:PCI io:%04X irq:%d mem:%08X lcr:%08X",
  2935. info->device_name, info->io_base, info->irq_level,
  2936. info->phys_memory_base, info->phys_lcr_base);
  2937. } else {
  2938. ret = sprintf(buf, "%s:(E)ISA io:%04X irq:%d dma:%d",
  2939. info->device_name, info->io_base,
  2940. info->irq_level, info->dma_level);
  2941. }
  2942. /* output current serial signal states */
  2943. spin_lock_irqsave(&info->irq_spinlock,flags);
  2944. usc_get_serial_signals(info);
  2945. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  2946. stat_buf[0] = 0;
  2947. stat_buf[1] = 0;
  2948. if (info->serial_signals & SerialSignal_RTS)
  2949. strcat(stat_buf, "|RTS");
  2950. if (info->serial_signals & SerialSignal_CTS)
  2951. strcat(stat_buf, "|CTS");
  2952. if (info->serial_signals & SerialSignal_DTR)
  2953. strcat(stat_buf, "|DTR");
  2954. if (info->serial_signals & SerialSignal_DSR)
  2955. strcat(stat_buf, "|DSR");
  2956. if (info->serial_signals & SerialSignal_DCD)
  2957. strcat(stat_buf, "|CD");
  2958. if (info->serial_signals & SerialSignal_RI)
  2959. strcat(stat_buf, "|RI");
  2960. if (info->params.mode == MGSL_MODE_HDLC ||
  2961. info->params.mode == MGSL_MODE_RAW ) {
  2962. ret += sprintf(buf+ret, " HDLC txok:%d rxok:%d",
  2963. info->icount.txok, info->icount.rxok);
  2964. if (info->icount.txunder)
  2965. ret += sprintf(buf+ret, " txunder:%d", info->icount.txunder);
  2966. if (info->icount.txabort)
  2967. ret += sprintf(buf+ret, " txabort:%d", info->icount.txabort);
  2968. if (info->icount.rxshort)
  2969. ret += sprintf(buf+ret, " rxshort:%d", info->icount.rxshort);
  2970. if (info->icount.rxlong)
  2971. ret += sprintf(buf+ret, " rxlong:%d", info->icount.rxlong);
  2972. if (info->icount.rxover)
  2973. ret += sprintf(buf+ret, " rxover:%d", info->icount.rxover);
  2974. if (info->icount.rxcrc)
  2975. ret += sprintf(buf+ret, " rxcrc:%d", info->icount.rxcrc);
  2976. } else {
  2977. ret += sprintf(buf+ret, " ASYNC tx:%d rx:%d",
  2978. info->icount.tx, info->icount.rx);
  2979. if (info->icount.frame)
  2980. ret += sprintf(buf+ret, " fe:%d", info->icount.frame);
  2981. if (info->icount.parity)
  2982. ret += sprintf(buf+ret, " pe:%d", info->icount.parity);
  2983. if (info->icount.brk)
  2984. ret += sprintf(buf+ret, " brk:%d", info->icount.brk);
  2985. if (info->icount.overrun)
  2986. ret += sprintf(buf+ret, " oe:%d", info->icount.overrun);
  2987. }
  2988. /* Append serial signal status to end */
  2989. ret += sprintf(buf+ret, " %s\n", stat_buf+1);
  2990. ret += sprintf(buf+ret, "txactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
  2991. info->tx_active,info->bh_requested,info->bh_running,
  2992. info->pending_bh);
  2993. spin_lock_irqsave(&info->irq_spinlock,flags);
  2994. {
  2995. u16 Tcsr = usc_InReg( info, TCSR );
  2996. u16 Tdmr = usc_InDmaReg( info, TDMR );
  2997. u16 Ticr = usc_InReg( info, TICR );
  2998. u16 Rscr = usc_InReg( info, RCSR );
  2999. u16 Rdmr = usc_InDmaReg( info, RDMR );
  3000. u16 Ricr = usc_InReg( info, RICR );
  3001. u16 Icr = usc_InReg( info, ICR );
  3002. u16 Dccr = usc_InReg( info, DCCR );
  3003. u16 Tmr = usc_InReg( info, TMR );
  3004. u16 Tccr = usc_InReg( info, TCCR );
  3005. u16 Ccar = inw( info->io_base + CCAR );
  3006. ret += sprintf(buf+ret, "tcsr=%04X tdmr=%04X ticr=%04X rcsr=%04X rdmr=%04X\n"
  3007. "ricr=%04X icr =%04X dccr=%04X tmr=%04X tccr=%04X ccar=%04X\n",
  3008. Tcsr,Tdmr,Ticr,Rscr,Rdmr,Ricr,Icr,Dccr,Tmr,Tccr,Ccar );
  3009. }
  3010. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  3011. return ret;
  3012. } /* end of line_info() */
  3013. /* mgsl_read_proc()
  3014. *
  3015. * Called to print information about devices
  3016. *
  3017. * Arguments:
  3018. * page page of memory to hold returned info
  3019. * start
  3020. * off
  3021. * count
  3022. * eof
  3023. * data
  3024. *
  3025. * Return Value:
  3026. */
  3027. static int mgsl_read_proc(char *page, char **start, off_t off, int count,
  3028. int *eof, void *data)
  3029. {
  3030. int len = 0, l;
  3031. off_t begin = 0;
  3032. struct mgsl_struct *info;
  3033. len += sprintf(page, "synclink driver:%s\n", driver_version);
  3034. info = mgsl_device_list;
  3035. while( info ) {
  3036. l = line_info(page + len, info);
  3037. len += l;
  3038. if (len+begin > off+count)
  3039. goto done;
  3040. if (len+begin < off) {
  3041. begin += len;
  3042. len = 0;
  3043. }
  3044. info = info->next_device;
  3045. }
  3046. *eof = 1;
  3047. done:
  3048. if (off >= len+begin)
  3049. return 0;
  3050. *start = page + (off-begin);
  3051. return ((count < begin+len-off) ? count : begin+len-off);
  3052. } /* end of mgsl_read_proc() */
  3053. /* mgsl_allocate_dma_buffers()
  3054. *
  3055. * Allocate and format DMA buffers (ISA adapter)
  3056. * or format shared memory buffers (PCI adapter).
  3057. *
  3058. * Arguments: info pointer to device instance data
  3059. * Return Value: 0 if success, otherwise error
  3060. */
  3061. static int mgsl_allocate_dma_buffers(struct mgsl_struct *info)
  3062. {
  3063. unsigned short BuffersPerFrame;
  3064. info->last_mem_alloc = 0;
  3065. /* Calculate the number of DMA buffers necessary to hold the */
  3066. /* largest allowable frame size. Note: If the max frame size is */
  3067. /* not an even multiple of the DMA buffer size then we need to */
  3068. /* round the buffer count per frame up one. */
  3069. BuffersPerFrame = (unsigned short)(info->max_frame_size/DMABUFFERSIZE);
  3070. if ( info->max_frame_size % DMABUFFERSIZE )
  3071. BuffersPerFrame++;
  3072. if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
  3073. /*
  3074. * The PCI adapter has 256KBytes of shared memory to use.
  3075. * This is 64 PAGE_SIZE buffers.
  3076. *
  3077. * The first page is used for padding at this time so the
  3078. * buffer list does not begin at offset 0 of the PCI
  3079. * adapter's shared memory.
  3080. *
  3081. * The 2nd page is used for the buffer list. A 4K buffer
  3082. * list can hold 128 DMA_BUFFER structures at 32 bytes
  3083. * each.
  3084. *
  3085. * This leaves 62 4K pages.
  3086. *
  3087. * The next N pages are used for transmit frame(s). We
  3088. * reserve enough 4K page blocks to hold the required
  3089. * number of transmit dma buffers (num_tx_dma_buffers),
  3090. * each of MaxFrameSize size.
  3091. *
  3092. * Of the remaining pages (62-N), determine how many can
  3093. * be used to receive full MaxFrameSize inbound frames
  3094. */
  3095. info->tx_buffer_count = info->num_tx_dma_buffers * BuffersPerFrame;
  3096. info->rx_buffer_count = 62 - info->tx_buffer_count;
  3097. } else {
  3098. /* Calculate the number of PAGE_SIZE buffers needed for */
  3099. /* receive and transmit DMA buffers. */
  3100. /* Calculate the number of DMA buffers necessary to */
  3101. /* hold 7 max size receive frames and one max size transmit frame. */
  3102. /* The receive buffer count is bumped by one so we avoid an */
  3103. /* End of List condition if all receive buffers are used when */
  3104. /* using linked list DMA buffers. */
  3105. info->tx_buffer_count = info->num_tx_dma_buffers * BuffersPerFrame;
  3106. info->rx_buffer_count = (BuffersPerFrame * MAXRXFRAMES) + 6;
  3107. /*
  3108. * limit total TxBuffers & RxBuffers to 62 4K total
  3109. * (ala PCI Allocation)
  3110. */
  3111. if ( (info->tx_buffer_count + info->rx_buffer_count) > 62 )
  3112. info->rx_buffer_count = 62 - info->tx_buffer_count;
  3113. }
  3114. if ( debug_level >= DEBUG_LEVEL_INFO )
  3115. printk("%s(%d):Allocating %d TX and %d RX DMA buffers.\n",
  3116. __FILE__,__LINE__, info->tx_buffer_count,info->rx_buffer_count);
  3117. if ( mgsl_alloc_buffer_list_memory( info ) < 0 ||
  3118. mgsl_alloc_frame_memory(info, info->rx_buffer_list, info->rx_buffer_count) < 0 ||
  3119. mgsl_alloc_frame_memory(info, info->tx_buffer_list, info->tx_buffer_count) < 0 ||
  3120. mgsl_alloc_intermediate_rxbuffer_memory(info) < 0 ||
  3121. mgsl_alloc_intermediate_txbuffer_memory(info) < 0 ) {
  3122. printk("%s(%d):Can't allocate DMA buffer memory\n",__FILE__,__LINE__);
  3123. return -ENOMEM;
  3124. }
  3125. mgsl_reset_rx_dma_buffers( info );
  3126. mgsl_reset_tx_dma_buffers( info );
  3127. return 0;
  3128. } /* end of mgsl_allocate_dma_buffers() */
  3129. /*
  3130. * mgsl_alloc_buffer_list_memory()
  3131. *
  3132. * Allocate a common DMA buffer for use as the
  3133. * receive and transmit buffer lists.
  3134. *
  3135. * A buffer list is a set of buffer entries where each entry contains
  3136. * a pointer to an actual buffer and a pointer to the next buffer entry
  3137. * (plus some other info about the buffer).
  3138. *
  3139. * The buffer entries for a list are built to form a circular list so
  3140. * that when the entire list has been traversed you start back at the
  3141. * beginning.
  3142. *
  3143. * This function allocates memory for just the buffer entries.
  3144. * The links (pointer to next entry) are filled in with the physical
  3145. * address of the next entry so the adapter can navigate the list
  3146. * using bus master DMA. The pointers to the actual buffers are filled
  3147. * out later when the actual buffers are allocated.
  3148. *
  3149. * Arguments: info pointer to device instance data
  3150. * Return Value: 0 if success, otherwise error
  3151. */
  3152. static int mgsl_alloc_buffer_list_memory( struct mgsl_struct *info )
  3153. {
  3154. unsigned int i;
  3155. if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
  3156. /* PCI adapter uses shared memory. */
  3157. info->buffer_list = info->memory_base + info->last_mem_alloc;
  3158. info->buffer_list_phys = info->last_mem_alloc;
  3159. info->last_mem_alloc += BUFFERLISTSIZE;
  3160. } else {
  3161. /* ISA adapter uses system memory. */
  3162. /* The buffer lists are allocated as a common buffer that both */
  3163. /* the processor and adapter can access. This allows the driver to */
  3164. /* inspect portions of the buffer while other portions are being */
  3165. /* updated by the adapter using Bus Master DMA. */
  3166. info->buffer_list = dma_alloc_coherent(NULL, BUFFERLISTSIZE, &info->buffer_list_dma_addr, GFP_KERNEL);
  3167. if (info->buffer_list == NULL)
  3168. return -ENOMEM;
  3169. info->buffer_list_phys = (u32)(info->buffer_list_dma_addr);
  3170. }
  3171. /* We got the memory for the buffer entry lists. */
  3172. /* Initialize the memory block to all zeros. */
  3173. memset( info->buffer_list, 0, BUFFERLISTSIZE );
  3174. /* Save virtual address pointers to the receive and */
  3175. /* transmit buffer lists. (Receive 1st). These pointers will */
  3176. /* be used by the processor to access the lists. */
  3177. info->rx_buffer_list = (DMABUFFERENTRY *)info->buffer_list;
  3178. info->tx_buffer_list = (DMABUFFERENTRY *)info->buffer_list;
  3179. info->tx_buffer_list += info->rx_buffer_count;
  3180. /*
  3181. * Build the links for the buffer entry lists such that
  3182. * two circular lists are built. (Transmit and Receive).
  3183. *
  3184. * Note: the links are physical addresses
  3185. * which are read by the adapter to determine the next
  3186. * buffer entry to use.
  3187. */
  3188. for ( i = 0; i < info->rx_buffer_count; i++ ) {
  3189. /* calculate and store physical address of this buffer entry */
  3190. info->rx_buffer_list[i].phys_entry =
  3191. info->buffer_list_phys + (i * sizeof(DMABUFFERENTRY));
  3192. /* calculate and store physical address of */
  3193. /* next entry in cirular list of entries */
  3194. info->rx_buffer_list[i].link = info->buffer_list_phys;
  3195. if ( i < info->rx_buffer_count - 1 )
  3196. info->rx_buffer_list[i].link += (i + 1) * sizeof(DMABUFFERENTRY);
  3197. }
  3198. for ( i = 0; i < info->tx_buffer_count; i++ ) {
  3199. /* calculate and store physical address of this buffer entry */
  3200. info->tx_buffer_list[i].phys_entry = info->buffer_list_phys +
  3201. ((info->rx_buffer_count + i) * sizeof(DMABUFFERENTRY));
  3202. /* calculate and store physical address of */
  3203. /* next entry in cirular list of entries */
  3204. info->tx_buffer_list[i].link = info->buffer_list_phys +
  3205. info->rx_buffer_count * sizeof(DMABUFFERENTRY);
  3206. if ( i < info->tx_buffer_count - 1 )
  3207. info->tx_buffer_list[i].link += (i + 1) * sizeof(DMABUFFERENTRY);
  3208. }
  3209. return 0;
  3210. } /* end of mgsl_alloc_buffer_list_memory() */
  3211. /* Free DMA buffers allocated for use as the
  3212. * receive and transmit buffer lists.
  3213. * Warning:
  3214. *
  3215. * The data transfer buffers associated with the buffer list
  3216. * MUST be freed before freeing the buffer list itself because
  3217. * the buffer list contains the information necessary to free
  3218. * the individual buffers!
  3219. */
  3220. static void mgsl_free_buffer_list_memory( struct mgsl_struct *info )
  3221. {
  3222. if (info->buffer_list && info->bus_type != MGSL_BUS_TYPE_PCI)
  3223. dma_free_coherent(NULL, BUFFERLISTSIZE, info->buffer_list, info->buffer_list_dma_addr);
  3224. info->buffer_list = NULL;
  3225. info->rx_buffer_list = NULL;
  3226. info->tx_buffer_list = NULL;
  3227. } /* end of mgsl_free_buffer_list_memory() */
  3228. /*
  3229. * mgsl_alloc_frame_memory()
  3230. *
  3231. * Allocate the frame DMA buffers used by the specified buffer list.
  3232. * Each DMA buffer will be one memory page in size. This is necessary
  3233. * because memory can fragment enough that it may be impossible
  3234. * contiguous pages.
  3235. *
  3236. * Arguments:
  3237. *
  3238. * info pointer to device instance data
  3239. * BufferList pointer to list of buffer entries
  3240. * Buffercount count of buffer entries in buffer list
  3241. *
  3242. * Return Value: 0 if success, otherwise -ENOMEM
  3243. */
  3244. static int mgsl_alloc_frame_memory(struct mgsl_struct *info,DMABUFFERENTRY *BufferList,int Buffercount)
  3245. {
  3246. int i;
  3247. u32 phys_addr;
  3248. /* Allocate page sized buffers for the receive buffer list */
  3249. for ( i = 0; i < Buffercount; i++ ) {
  3250. if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
  3251. /* PCI adapter uses shared memory buffers. */
  3252. BufferList[i].virt_addr = info->memory_base + info->last_mem_alloc;
  3253. phys_addr = info->last_mem_alloc;
  3254. info->last_mem_alloc += DMABUFFERSIZE;
  3255. } else {
  3256. /* ISA adapter uses system memory. */
  3257. BufferList[i].virt_addr = dma_alloc_coherent(NULL, DMABUFFERSIZE, &BufferList[i].dma_addr, GFP_KERNEL);
  3258. if (BufferList[i].virt_addr == NULL)
  3259. return -ENOMEM;
  3260. phys_addr = (u32)(BufferList[i].dma_addr);
  3261. }
  3262. BufferList[i].phys_addr = phys_addr;
  3263. }
  3264. return 0;
  3265. } /* end of mgsl_alloc_frame_memory() */
  3266. /*
  3267. * mgsl_free_frame_memory()
  3268. *
  3269. * Free the buffers associated with
  3270. * each buffer entry of a buffer list.
  3271. *
  3272. * Arguments:
  3273. *
  3274. * info pointer to device instance data
  3275. * BufferList pointer to list of buffer entries
  3276. * Buffercount count of buffer entries in buffer list
  3277. *
  3278. * Return Value: None
  3279. */
  3280. static void mgsl_free_frame_memory(struct mgsl_struct *info, DMABUFFERENTRY *BufferList, int Buffercount)
  3281. {
  3282. int i;
  3283. if ( BufferList ) {
  3284. for ( i = 0 ; i < Buffercount ; i++ ) {
  3285. if ( BufferList[i].virt_addr ) {
  3286. if ( info->bus_type != MGSL_BUS_TYPE_PCI )
  3287. dma_free_coherent(NULL, DMABUFFERSIZE, BufferList[i].virt_addr, BufferList[i].dma_addr);
  3288. BufferList[i].virt_addr = NULL;
  3289. }
  3290. }
  3291. }
  3292. } /* end of mgsl_free_frame_memory() */
  3293. /* mgsl_free_dma_buffers()
  3294. *
  3295. * Free DMA buffers
  3296. *
  3297. * Arguments: info pointer to device instance data
  3298. * Return Value: None
  3299. */
  3300. static void mgsl_free_dma_buffers( struct mgsl_struct *info )
  3301. {
  3302. mgsl_free_frame_memory( info, info->rx_buffer_list, info->rx_buffer_count );
  3303. mgsl_free_frame_memory( info, info->tx_buffer_list, info->tx_buffer_count );
  3304. mgsl_free_buffer_list_memory( info );
  3305. } /* end of mgsl_free_dma_buffers() */
  3306. /*
  3307. * mgsl_alloc_intermediate_rxbuffer_memory()
  3308. *
  3309. * Allocate a buffer large enough to hold max_frame_size. This buffer
  3310. * is used to pass an assembled frame to the line discipline.
  3311. *
  3312. * Arguments:
  3313. *
  3314. * info pointer to device instance data
  3315. *
  3316. * Return Value: 0 if success, otherwise -ENOMEM
  3317. */
  3318. static int mgsl_alloc_intermediate_rxbuffer_memory(struct mgsl_struct *info)
  3319. {
  3320. info->intermediate_rxbuffer = kmalloc(info->max_frame_size, GFP_KERNEL | GFP_DMA);
  3321. if ( info->intermediate_rxbuffer == NULL )
  3322. return -ENOMEM;
  3323. return 0;
  3324. } /* end of mgsl_alloc_intermediate_rxbuffer_memory() */
  3325. /*
  3326. * mgsl_free_intermediate_rxbuffer_memory()
  3327. *
  3328. *
  3329. * Arguments:
  3330. *
  3331. * info pointer to device instance data
  3332. *
  3333. * Return Value: None
  3334. */
  3335. static void mgsl_free_intermediate_rxbuffer_memory(struct mgsl_struct *info)
  3336. {
  3337. kfree(info->intermediate_rxbuffer);
  3338. info->intermediate_rxbuffer = NULL;
  3339. } /* end of mgsl_free_intermediate_rxbuffer_memory() */
  3340. /*
  3341. * mgsl_alloc_intermediate_txbuffer_memory()
  3342. *
  3343. * Allocate intermdiate transmit buffer(s) large enough to hold max_frame_size.
  3344. * This buffer is used to load transmit frames into the adapter's dma transfer
  3345. * buffers when there is sufficient space.
  3346. *
  3347. * Arguments:
  3348. *
  3349. * info pointer to device instance data
  3350. *
  3351. * Return Value: 0 if success, otherwise -ENOMEM
  3352. */
  3353. static int mgsl_alloc_intermediate_txbuffer_memory(struct mgsl_struct *info)
  3354. {
  3355. int i;
  3356. if ( debug_level >= DEBUG_LEVEL_INFO )
  3357. printk("%s %s(%d) allocating %d tx holding buffers\n",
  3358. info->device_name, __FILE__,__LINE__,info->num_tx_holding_buffers);
  3359. memset(info->tx_holding_buffers,0,sizeof(info->tx_holding_buffers));
  3360. for ( i=0; i<info->num_tx_holding_buffers; ++i) {
  3361. info->tx_holding_buffers[i].buffer =
  3362. kmalloc(info->max_frame_size, GFP_KERNEL);
  3363. if (info->tx_holding_buffers[i].buffer == NULL) {
  3364. for (--i; i >= 0; i--) {
  3365. kfree(info->tx_holding_buffers[i].buffer);
  3366. info->tx_holding_buffers[i].buffer = NULL;
  3367. }
  3368. return -ENOMEM;
  3369. }
  3370. }
  3371. return 0;
  3372. } /* end of mgsl_alloc_intermediate_txbuffer_memory() */
  3373. /*
  3374. * mgsl_free_intermediate_txbuffer_memory()
  3375. *
  3376. *
  3377. * Arguments:
  3378. *
  3379. * info pointer to device instance data
  3380. *
  3381. * Return Value: None
  3382. */
  3383. static void mgsl_free_intermediate_txbuffer_memory(struct mgsl_struct *info)
  3384. {
  3385. int i;
  3386. for ( i=0; i<info->num_tx_holding_buffers; ++i ) {
  3387. kfree(info->tx_holding_buffers[i].buffer);
  3388. info->tx_holding_buffers[i].buffer = NULL;
  3389. }
  3390. info->get_tx_holding_index = 0;
  3391. info->put_tx_holding_index = 0;
  3392. info->tx_holding_count = 0;
  3393. } /* end of mgsl_free_intermediate_txbuffer_memory() */
  3394. /*
  3395. * load_next_tx_holding_buffer()
  3396. *
  3397. * attempts to load the next buffered tx request into the
  3398. * tx dma buffers
  3399. *
  3400. * Arguments:
  3401. *
  3402. * info pointer to device instance data
  3403. *
  3404. * Return Value: true if next buffered tx request loaded
  3405. * into adapter's tx dma buffer,
  3406. * false otherwise
  3407. */
  3408. static bool load_next_tx_holding_buffer(struct mgsl_struct *info)
  3409. {
  3410. bool ret = false;
  3411. if ( info->tx_holding_count ) {
  3412. /* determine if we have enough tx dma buffers
  3413. * to accommodate the next tx frame
  3414. */
  3415. struct tx_holding_buffer *ptx =
  3416. &info->tx_holding_buffers[info->get_tx_holding_index];
  3417. int num_free = num_free_tx_dma_buffers(info);
  3418. int num_needed = ptx->buffer_size / DMABUFFERSIZE;
  3419. if ( ptx->buffer_size % DMABUFFERSIZE )
  3420. ++num_needed;
  3421. if (num_needed <= num_free) {
  3422. info->xmit_cnt = ptx->buffer_size;
  3423. mgsl_load_tx_dma_buffer(info,ptx->buffer,ptx->buffer_size);
  3424. --info->tx_holding_count;
  3425. if ( ++info->get_tx_holding_index >= info->num_tx_holding_buffers)
  3426. info->get_tx_holding_index=0;
  3427. /* restart transmit timer */
  3428. mod_timer(&info->tx_timer, jiffies + msecs_to_jiffies(5000));
  3429. ret = true;
  3430. }
  3431. }
  3432. return ret;
  3433. }
  3434. /*
  3435. * save_tx_buffer_request()
  3436. *
  3437. * attempt to store transmit frame request for later transmission
  3438. *
  3439. * Arguments:
  3440. *
  3441. * info pointer to device instance data
  3442. * Buffer pointer to buffer containing frame to load
  3443. * BufferSize size in bytes of frame in Buffer
  3444. *
  3445. * Return Value: 1 if able to store, 0 otherwise
  3446. */
  3447. static int save_tx_buffer_request(struct mgsl_struct *info,const char *Buffer, unsigned int BufferSize)
  3448. {
  3449. struct tx_holding_buffer *ptx;
  3450. if ( info->tx_holding_count >= info->num_tx_holding_buffers ) {
  3451. return 0; /* all buffers in use */
  3452. }
  3453. ptx = &info->tx_holding_buffers[info->put_tx_holding_index];
  3454. ptx->buffer_size = BufferSize;
  3455. memcpy( ptx->buffer, Buffer, BufferSize);
  3456. ++info->tx_holding_count;
  3457. if ( ++info->put_tx_holding_index >= info->num_tx_holding_buffers)
  3458. info->put_tx_holding_index=0;
  3459. return 1;
  3460. }
  3461. static int mgsl_claim_resources(struct mgsl_struct *info)
  3462. {
  3463. if (request_region(info->io_base,info->io_addr_size,"synclink") == NULL) {
  3464. printk( "%s(%d):I/O address conflict on device %s Addr=%08X\n",
  3465. __FILE__,__LINE__,info->device_name, info->io_base);
  3466. return -ENODEV;
  3467. }
  3468. info->io_addr_requested = true;
  3469. if ( request_irq(info->irq_level,mgsl_interrupt,info->irq_flags,
  3470. info->device_name, info ) < 0 ) {
  3471. printk( "%s(%d):Cant request interrupt on device %s IRQ=%d\n",
  3472. __FILE__,__LINE__,info->device_name, info->irq_level );
  3473. goto errout;
  3474. }
  3475. info->irq_requested = true;
  3476. if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
  3477. if (request_mem_region(info->phys_memory_base,0x40000,"synclink") == NULL) {
  3478. printk( "%s(%d):mem addr conflict device %s Addr=%08X\n",
  3479. __FILE__,__LINE__,info->device_name, info->phys_memory_base);
  3480. goto errout;
  3481. }
  3482. info->shared_mem_requested = true;
  3483. if (request_mem_region(info->phys_lcr_base + info->lcr_offset,128,"synclink") == NULL) {
  3484. printk( "%s(%d):lcr mem addr conflict device %s Addr=%08X\n",
  3485. __FILE__,__LINE__,info->device_name, info->phys_lcr_base + info->lcr_offset);
  3486. goto errout;
  3487. }
  3488. info->lcr_mem_requested = true;
  3489. info->memory_base = ioremap(info->phys_memory_base,0x40000);
  3490. if (!info->memory_base) {
  3491. printk( "%s(%d):Cant map shared memory on device %s MemAddr=%08X\n",
  3492. __FILE__,__LINE__,info->device_name, info->phys_memory_base );
  3493. goto errout;
  3494. }
  3495. if ( !mgsl_memory_test(info) ) {
  3496. printk( "%s(%d):Failed shared memory test %s MemAddr=%08X\n",
  3497. __FILE__,__LINE__,info->device_name, info->phys_memory_base );
  3498. goto errout;
  3499. }
  3500. info->lcr_base = ioremap(info->phys_lcr_base,PAGE_SIZE) + info->lcr_offset;
  3501. if (!info->lcr_base) {
  3502. printk( "%s(%d):Cant map LCR memory on device %s MemAddr=%08X\n",
  3503. __FILE__,__LINE__,info->device_name, info->phys_lcr_base );
  3504. goto errout;
  3505. }
  3506. } else {
  3507. /* claim DMA channel */
  3508. if (request_dma(info->dma_level,info->device_name) < 0){
  3509. printk( "%s(%d):Cant request DMA channel on device %s DMA=%d\n",
  3510. __FILE__,__LINE__,info->device_name, info->dma_level );
  3511. mgsl_release_resources( info );
  3512. return -ENODEV;
  3513. }
  3514. info->dma_requested = true;
  3515. /* ISA adapter uses bus master DMA */
  3516. set_dma_mode(info->dma_level,DMA_MODE_CASCADE);
  3517. enable_dma(info->dma_level);
  3518. }
  3519. if ( mgsl_allocate_dma_buffers(info) < 0 ) {
  3520. printk( "%s(%d):Cant allocate DMA buffers on device %s DMA=%d\n",
  3521. __FILE__,__LINE__,info->device_name, info->dma_level );
  3522. goto errout;
  3523. }
  3524. return 0;
  3525. errout:
  3526. mgsl_release_resources(info);
  3527. return -ENODEV;
  3528. } /* end of mgsl_claim_resources() */
  3529. static void mgsl_release_resources(struct mgsl_struct *info)
  3530. {
  3531. if ( debug_level >= DEBUG_LEVEL_INFO )
  3532. printk( "%s(%d):mgsl_release_resources(%s) entry\n",
  3533. __FILE__,__LINE__,info->device_name );
  3534. if ( info->irq_requested ) {
  3535. free_irq(info->irq_level, info);
  3536. info->irq_requested = false;
  3537. }
  3538. if ( info->dma_requested ) {
  3539. disable_dma(info->dma_level);
  3540. free_dma(info->dma_level);
  3541. info->dma_requested = false;
  3542. }
  3543. mgsl_free_dma_buffers(info);
  3544. mgsl_free_intermediate_rxbuffer_memory(info);
  3545. mgsl_free_intermediate_txbuffer_memory(info);
  3546. if ( info->io_addr_requested ) {
  3547. release_region(info->io_base,info->io_addr_size);
  3548. info->io_addr_requested = false;
  3549. }
  3550. if ( info->shared_mem_requested ) {
  3551. release_mem_region(info->phys_memory_base,0x40000);
  3552. info->shared_mem_requested = false;
  3553. }
  3554. if ( info->lcr_mem_requested ) {
  3555. release_mem_region(info->phys_lcr_base + info->lcr_offset,128);
  3556. info->lcr_mem_requested = false;
  3557. }
  3558. if (info->memory_base){
  3559. iounmap(info->memory_base);
  3560. info->memory_base = NULL;
  3561. }
  3562. if (info->lcr_base){
  3563. iounmap(info->lcr_base - info->lcr_offset);
  3564. info->lcr_base = NULL;
  3565. }
  3566. if ( debug_level >= DEBUG_LEVEL_INFO )
  3567. printk( "%s(%d):mgsl_release_resources(%s) exit\n",
  3568. __FILE__,__LINE__,info->device_name );
  3569. } /* end of mgsl_release_resources() */
  3570. /* mgsl_add_device()
  3571. *
  3572. * Add the specified device instance data structure to the
  3573. * global linked list of devices and increment the device count.
  3574. *
  3575. * Arguments: info pointer to device instance data
  3576. * Return Value: None
  3577. */
  3578. static void mgsl_add_device( struct mgsl_struct *info )
  3579. {
  3580. info->next_device = NULL;
  3581. info->line = mgsl_device_count;
  3582. sprintf(info->device_name,"ttySL%d",info->line);
  3583. if (info->line < MAX_TOTAL_DEVICES) {
  3584. if (maxframe[info->line])
  3585. info->max_frame_size = maxframe[info->line];
  3586. info->dosyncppp = dosyncppp[info->line];
  3587. if (txdmabufs[info->line]) {
  3588. info->num_tx_dma_buffers = txdmabufs[info->line];
  3589. if (info->num_tx_dma_buffers < 1)
  3590. info->num_tx_dma_buffers = 1;
  3591. }
  3592. if (txholdbufs[info->line]) {
  3593. info->num_tx_holding_buffers = txholdbufs[info->line];
  3594. if (info->num_tx_holding_buffers < 1)
  3595. info->num_tx_holding_buffers = 1;
  3596. else if (info->num_tx_holding_buffers > MAX_TX_HOLDING_BUFFERS)
  3597. info->num_tx_holding_buffers = MAX_TX_HOLDING_BUFFERS;
  3598. }
  3599. }
  3600. mgsl_device_count++;
  3601. if ( !mgsl_device_list )
  3602. mgsl_device_list = info;
  3603. else {
  3604. struct mgsl_struct *current_dev = mgsl_device_list;
  3605. while( current_dev->next_device )
  3606. current_dev = current_dev->next_device;
  3607. current_dev->next_device = info;
  3608. }
  3609. if ( info->max_frame_size < 4096 )
  3610. info->max_frame_size = 4096;
  3611. else if ( info->max_frame_size > 65535 )
  3612. info->max_frame_size = 65535;
  3613. if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
  3614. printk( "SyncLink PCI v%d %s: IO=%04X IRQ=%d Mem=%08X,%08X MaxFrameSize=%u\n",
  3615. info->hw_version + 1, info->device_name, info->io_base, info->irq_level,
  3616. info->phys_memory_base, info->phys_lcr_base,
  3617. info->max_frame_size );
  3618. } else {
  3619. printk( "SyncLink ISA %s: IO=%04X IRQ=%d DMA=%d MaxFrameSize=%u\n",
  3620. info->device_name, info->io_base, info->irq_level, info->dma_level,
  3621. info->max_frame_size );
  3622. }
  3623. #if SYNCLINK_GENERIC_HDLC
  3624. hdlcdev_init(info);
  3625. #endif
  3626. } /* end of mgsl_add_device() */
  3627. /* mgsl_allocate_device()
  3628. *
  3629. * Allocate and initialize a device instance structure
  3630. *
  3631. * Arguments: none
  3632. * Return Value: pointer to mgsl_struct if success, otherwise NULL
  3633. */
  3634. static struct mgsl_struct* mgsl_allocate_device(void)
  3635. {
  3636. struct mgsl_struct *info;
  3637. info = kzalloc(sizeof(struct mgsl_struct),
  3638. GFP_KERNEL);
  3639. if (!info) {
  3640. printk("Error can't allocate device instance data\n");
  3641. } else {
  3642. info->magic = MGSL_MAGIC;
  3643. INIT_WORK(&info->task, mgsl_bh_handler);
  3644. info->max_frame_size = 4096;
  3645. info->close_delay = 5*HZ/10;
  3646. info->closing_wait = 30*HZ;
  3647. init_waitqueue_head(&info->open_wait);
  3648. init_waitqueue_head(&info->close_wait);
  3649. init_waitqueue_head(&info->status_event_wait_q);
  3650. init_waitqueue_head(&info->event_wait_q);
  3651. spin_lock_init(&info->irq_spinlock);
  3652. spin_lock_init(&info->netlock);
  3653. memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
  3654. info->idle_mode = HDLC_TXIDLE_FLAGS;
  3655. info->num_tx_dma_buffers = 1;
  3656. info->num_tx_holding_buffers = 0;
  3657. }
  3658. return info;
  3659. } /* end of mgsl_allocate_device()*/
  3660. static const struct tty_operations mgsl_ops = {
  3661. .open = mgsl_open,
  3662. .close = mgsl_close,
  3663. .write = mgsl_write,
  3664. .put_char = mgsl_put_char,
  3665. .flush_chars = mgsl_flush_chars,
  3666. .write_room = mgsl_write_room,
  3667. .chars_in_buffer = mgsl_chars_in_buffer,
  3668. .flush_buffer = mgsl_flush_buffer,
  3669. .ioctl = mgsl_ioctl,
  3670. .throttle = mgsl_throttle,
  3671. .unthrottle = mgsl_unthrottle,
  3672. .send_xchar = mgsl_send_xchar,
  3673. .break_ctl = mgsl_break,
  3674. .wait_until_sent = mgsl_wait_until_sent,
  3675. .read_proc = mgsl_read_proc,
  3676. .set_termios = mgsl_set_termios,
  3677. .stop = mgsl_stop,
  3678. .start = mgsl_start,
  3679. .hangup = mgsl_hangup,
  3680. .tiocmget = tiocmget,
  3681. .tiocmset = tiocmset,
  3682. };
  3683. /*
  3684. * perform tty device initialization
  3685. */
  3686. static int mgsl_init_tty(void)
  3687. {
  3688. int rc;
  3689. serial_driver = alloc_tty_driver(128);
  3690. if (!serial_driver)
  3691. return -ENOMEM;
  3692. serial_driver->owner = THIS_MODULE;
  3693. serial_driver->driver_name = "synclink";
  3694. serial_driver->name = "ttySL";
  3695. serial_driver->major = ttymajor;
  3696. serial_driver->minor_start = 64;
  3697. serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  3698. serial_driver->subtype = SERIAL_TYPE_NORMAL;
  3699. serial_driver->init_termios = tty_std_termios;
  3700. serial_driver->init_termios.c_cflag =
  3701. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  3702. serial_driver->init_termios.c_ispeed = 9600;
  3703. serial_driver->init_termios.c_ospeed = 9600;
  3704. serial_driver->flags = TTY_DRIVER_REAL_RAW;
  3705. tty_set_operations(serial_driver, &mgsl_ops);
  3706. if ((rc = tty_register_driver(serial_driver)) < 0) {
  3707. printk("%s(%d):Couldn't register serial driver\n",
  3708. __FILE__,__LINE__);
  3709. put_tty_driver(serial_driver);
  3710. serial_driver = NULL;
  3711. return rc;
  3712. }
  3713. printk("%s %s, tty major#%d\n",
  3714. driver_name, driver_version,
  3715. serial_driver->major);
  3716. return 0;
  3717. }
  3718. /* enumerate user specified ISA adapters
  3719. */
  3720. static void mgsl_enum_isa_devices(void)
  3721. {
  3722. struct mgsl_struct *info;
  3723. int i;
  3724. /* Check for user specified ISA devices */
  3725. for (i=0 ;(i < MAX_ISA_DEVICES) && io[i] && irq[i]; i++){
  3726. if ( debug_level >= DEBUG_LEVEL_INFO )
  3727. printk("ISA device specified io=%04X,irq=%d,dma=%d\n",
  3728. io[i], irq[i], dma[i] );
  3729. info = mgsl_allocate_device();
  3730. if ( !info ) {
  3731. /* error allocating device instance data */
  3732. if ( debug_level >= DEBUG_LEVEL_ERROR )
  3733. printk( "can't allocate device instance data.\n");
  3734. continue;
  3735. }
  3736. /* Copy user configuration info to device instance data */
  3737. info->io_base = (unsigned int)io[i];
  3738. info->irq_level = (unsigned int)irq[i];
  3739. info->irq_level = irq_canonicalize(info->irq_level);
  3740. info->dma_level = (unsigned int)dma[i];
  3741. info->bus_type = MGSL_BUS_TYPE_ISA;
  3742. info->io_addr_size = 16;
  3743. info->irq_flags = 0;
  3744. mgsl_add_device( info );
  3745. }
  3746. }
  3747. static void synclink_cleanup(void)
  3748. {
  3749. int rc;
  3750. struct mgsl_struct *info;
  3751. struct mgsl_struct *tmp;
  3752. printk("Unloading %s: %s\n", driver_name, driver_version);
  3753. if (serial_driver) {
  3754. if ((rc = tty_unregister_driver(serial_driver)))
  3755. printk("%s(%d) failed to unregister tty driver err=%d\n",
  3756. __FILE__,__LINE__,rc);
  3757. put_tty_driver(serial_driver);
  3758. }
  3759. info = mgsl_device_list;
  3760. while(info) {
  3761. #if SYNCLINK_GENERIC_HDLC
  3762. hdlcdev_exit(info);
  3763. #endif
  3764. mgsl_release_resources(info);
  3765. tmp = info;
  3766. info = info->next_device;
  3767. kfree(tmp);
  3768. }
  3769. if (pci_registered)
  3770. pci_unregister_driver(&synclink_pci_driver);
  3771. }
  3772. static int __init synclink_init(void)
  3773. {
  3774. int rc;
  3775. if (break_on_load) {
  3776. mgsl_get_text_ptr();
  3777. BREAKPOINT();
  3778. }
  3779. printk("%s %s\n", driver_name, driver_version);
  3780. mgsl_enum_isa_devices();
  3781. if ((rc = pci_register_driver(&synclink_pci_driver)) < 0)
  3782. printk("%s:failed to register PCI driver, error=%d\n",__FILE__,rc);
  3783. else
  3784. pci_registered = true;
  3785. if ((rc = mgsl_init_tty()) < 0)
  3786. goto error;
  3787. return 0;
  3788. error:
  3789. synclink_cleanup();
  3790. return rc;
  3791. }
  3792. static void __exit synclink_exit(void)
  3793. {
  3794. synclink_cleanup();
  3795. }
  3796. module_init(synclink_init);
  3797. module_exit(synclink_exit);
  3798. /*
  3799. * usc_RTCmd()
  3800. *
  3801. * Issue a USC Receive/Transmit command to the
  3802. * Channel Command/Address Register (CCAR).
  3803. *
  3804. * Notes:
  3805. *
  3806. * The command is encoded in the most significant 5 bits <15..11>
  3807. * of the CCAR value. Bits <10..7> of the CCAR must be preserved
  3808. * and Bits <6..0> must be written as zeros.
  3809. *
  3810. * Arguments:
  3811. *
  3812. * info pointer to device information structure
  3813. * Cmd command mask (use symbolic macros)
  3814. *
  3815. * Return Value:
  3816. *
  3817. * None
  3818. */
  3819. static void usc_RTCmd( struct mgsl_struct *info, u16 Cmd )
  3820. {
  3821. /* output command to CCAR in bits <15..11> */
  3822. /* preserve bits <10..7>, bits <6..0> must be zero */
  3823. outw( Cmd + info->loopback_bits, info->io_base + CCAR );
  3824. /* Read to flush write to CCAR */
  3825. if ( info->bus_type == MGSL_BUS_TYPE_PCI )
  3826. inw( info->io_base + CCAR );
  3827. } /* end of usc_RTCmd() */
  3828. /*
  3829. * usc_DmaCmd()
  3830. *
  3831. * Issue a DMA command to the DMA Command/Address Register (DCAR).
  3832. *
  3833. * Arguments:
  3834. *
  3835. * info pointer to device information structure
  3836. * Cmd DMA command mask (usc_DmaCmd_XX Macros)
  3837. *
  3838. * Return Value:
  3839. *
  3840. * None
  3841. */
  3842. static void usc_DmaCmd( struct mgsl_struct *info, u16 Cmd )
  3843. {
  3844. /* write command mask to DCAR */
  3845. outw( Cmd + info->mbre_bit, info->io_base );
  3846. /* Read to flush write to DCAR */
  3847. if ( info->bus_type == MGSL_BUS_TYPE_PCI )
  3848. inw( info->io_base );
  3849. } /* end of usc_DmaCmd() */
  3850. /*
  3851. * usc_OutDmaReg()
  3852. *
  3853. * Write a 16-bit value to a USC DMA register
  3854. *
  3855. * Arguments:
  3856. *
  3857. * info pointer to device info structure
  3858. * RegAddr register address (number) for write
  3859. * RegValue 16-bit value to write to register
  3860. *
  3861. * Return Value:
  3862. *
  3863. * None
  3864. *
  3865. */
  3866. static void usc_OutDmaReg( struct mgsl_struct *info, u16 RegAddr, u16 RegValue )
  3867. {
  3868. /* Note: The DCAR is located at the adapter base address */
  3869. /* Note: must preserve state of BIT8 in DCAR */
  3870. outw( RegAddr + info->mbre_bit, info->io_base );
  3871. outw( RegValue, info->io_base );
  3872. /* Read to flush write to DCAR */
  3873. if ( info->bus_type == MGSL_BUS_TYPE_PCI )
  3874. inw( info->io_base );
  3875. } /* end of usc_OutDmaReg() */
  3876. /*
  3877. * usc_InDmaReg()
  3878. *
  3879. * Read a 16-bit value from a DMA register
  3880. *
  3881. * Arguments:
  3882. *
  3883. * info pointer to device info structure
  3884. * RegAddr register address (number) to read from
  3885. *
  3886. * Return Value:
  3887. *
  3888. * The 16-bit value read from register
  3889. *
  3890. */
  3891. static u16 usc_InDmaReg( struct mgsl_struct *info, u16 RegAddr )
  3892. {
  3893. /* Note: The DCAR is located at the adapter base address */
  3894. /* Note: must preserve state of BIT8 in DCAR */
  3895. outw( RegAddr + info->mbre_bit, info->io_base );
  3896. return inw( info->io_base );
  3897. } /* end of usc_InDmaReg() */
  3898. /*
  3899. *
  3900. * usc_OutReg()
  3901. *
  3902. * Write a 16-bit value to a USC serial channel register
  3903. *
  3904. * Arguments:
  3905. *
  3906. * info pointer to device info structure
  3907. * RegAddr register address (number) to write to
  3908. * RegValue 16-bit value to write to register
  3909. *
  3910. * Return Value:
  3911. *
  3912. * None
  3913. *
  3914. */
  3915. static void usc_OutReg( struct mgsl_struct *info, u16 RegAddr, u16 RegValue )
  3916. {
  3917. outw( RegAddr + info->loopback_bits, info->io_base + CCAR );
  3918. outw( RegValue, info->io_base + CCAR );
  3919. /* Read to flush write to CCAR */
  3920. if ( info->bus_type == MGSL_BUS_TYPE_PCI )
  3921. inw( info->io_base + CCAR );
  3922. } /* end of usc_OutReg() */
  3923. /*
  3924. * usc_InReg()
  3925. *
  3926. * Reads a 16-bit value from a USC serial channel register
  3927. *
  3928. * Arguments:
  3929. *
  3930. * info pointer to device extension
  3931. * RegAddr register address (number) to read from
  3932. *
  3933. * Return Value:
  3934. *
  3935. * 16-bit value read from register
  3936. */
  3937. static u16 usc_InReg( struct mgsl_struct *info, u16 RegAddr )
  3938. {
  3939. outw( RegAddr + info->loopback_bits, info->io_base + CCAR );
  3940. return inw( info->io_base + CCAR );
  3941. } /* end of usc_InReg() */
  3942. /* usc_set_sdlc_mode()
  3943. *
  3944. * Set up the adapter for SDLC DMA communications.
  3945. *
  3946. * Arguments: info pointer to device instance data
  3947. * Return Value: NONE
  3948. */
  3949. static void usc_set_sdlc_mode( struct mgsl_struct *info )
  3950. {
  3951. u16 RegValue;
  3952. bool PreSL1660;
  3953. /*
  3954. * determine if the IUSC on the adapter is pre-SL1660. If
  3955. * not, take advantage of the UnderWait feature of more
  3956. * modern chips. If an underrun occurs and this bit is set,
  3957. * the transmitter will idle the programmed idle pattern
  3958. * until the driver has time to service the underrun. Otherwise,
  3959. * the dma controller may get the cycles previously requested
  3960. * and begin transmitting queued tx data.
  3961. */
  3962. usc_OutReg(info,TMCR,0x1f);
  3963. RegValue=usc_InReg(info,TMDR);
  3964. PreSL1660 = (RegValue == IUSC_PRE_SL1660);
  3965. if ( info->params.flags & HDLC_FLAG_HDLC_LOOPMODE )
  3966. {
  3967. /*
  3968. ** Channel Mode Register (CMR)
  3969. **
  3970. ** <15..14> 10 Tx Sub Modes, Send Flag on Underrun
  3971. ** <13> 0 0 = Transmit Disabled (initially)
  3972. ** <12> 0 1 = Consecutive Idles share common 0
  3973. ** <11..8> 1110 Transmitter Mode = HDLC/SDLC Loop
  3974. ** <7..4> 0000 Rx Sub Modes, addr/ctrl field handling
  3975. ** <3..0> 0110 Receiver Mode = HDLC/SDLC
  3976. **
  3977. ** 1000 1110 0000 0110 = 0x8e06
  3978. */
  3979. RegValue = 0x8e06;
  3980. /*--------------------------------------------------
  3981. * ignore user options for UnderRun Actions and
  3982. * preambles
  3983. *--------------------------------------------------*/
  3984. }
  3985. else
  3986. {
  3987. /* Channel mode Register (CMR)
  3988. *
  3989. * <15..14> 00 Tx Sub modes, Underrun Action
  3990. * <13> 0 1 = Send Preamble before opening flag
  3991. * <12> 0 1 = Consecutive Idles share common 0
  3992. * <11..8> 0110 Transmitter mode = HDLC/SDLC
  3993. * <7..4> 0000 Rx Sub modes, addr/ctrl field handling
  3994. * <3..0> 0110 Receiver mode = HDLC/SDLC
  3995. *
  3996. * 0000 0110 0000 0110 = 0x0606
  3997. */
  3998. if (info->params.mode == MGSL_MODE_RAW) {
  3999. RegValue = 0x0001; /* Set Receive mode = external sync */
  4000. usc_OutReg( info, IOCR, /* Set IOCR DCD is RxSync Detect Input */
  4001. (unsigned short)((usc_InReg(info, IOCR) & ~(BIT13|BIT12)) | BIT12));
  4002. /*
  4003. * TxSubMode:
  4004. * CMR <15> 0 Don't send CRC on Tx Underrun
  4005. * CMR <14> x undefined
  4006. * CMR <13> 0 Send preamble before openning sync
  4007. * CMR <12> 0 Send 8-bit syncs, 1=send Syncs per TxLength
  4008. *
  4009. * TxMode:
  4010. * CMR <11-8) 0100 MonoSync
  4011. *
  4012. * 0x00 0100 xxxx xxxx 04xx
  4013. */
  4014. RegValue |= 0x0400;
  4015. }
  4016. else {
  4017. RegValue = 0x0606;
  4018. if ( info->params.flags & HDLC_FLAG_UNDERRUN_ABORT15 )
  4019. RegValue |= BIT14;
  4020. else if ( info->params.flags & HDLC_FLAG_UNDERRUN_FLAG )
  4021. RegValue |= BIT15;
  4022. else if ( info->params.flags & HDLC_FLAG_UNDERRUN_CRC )
  4023. RegValue |= BIT15 + BIT14;
  4024. }
  4025. if ( info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE )
  4026. RegValue |= BIT13;
  4027. }
  4028. if ( info->params.mode == MGSL_MODE_HDLC &&
  4029. (info->params.flags & HDLC_FLAG_SHARE_ZERO) )
  4030. RegValue |= BIT12;
  4031. if ( info->params.addr_filter != 0xff )
  4032. {
  4033. /* set up receive address filtering */
  4034. usc_OutReg( info, RSR, info->params.addr_filter );
  4035. RegValue |= BIT4;
  4036. }
  4037. usc_OutReg( info, CMR, RegValue );
  4038. info->cmr_value = RegValue;
  4039. /* Receiver mode Register (RMR)
  4040. *
  4041. * <15..13> 000 encoding
  4042. * <12..11> 00 FCS = 16bit CRC CCITT (x15 + x12 + x5 + 1)
  4043. * <10> 1 1 = Set CRC to all 1s (use for SDLC/HDLC)
  4044. * <9> 0 1 = Include Receive chars in CRC
  4045. * <8> 1 1 = Use Abort/PE bit as abort indicator
  4046. * <7..6> 00 Even parity
  4047. * <5> 0 parity disabled
  4048. * <4..2> 000 Receive Char Length = 8 bits
  4049. * <1..0> 00 Disable Receiver
  4050. *
  4051. * 0000 0101 0000 0000 = 0x0500
  4052. */
  4053. RegValue = 0x0500;
  4054. switch ( info->params.encoding ) {
  4055. case HDLC_ENCODING_NRZB: RegValue |= BIT13; break;
  4056. case HDLC_ENCODING_NRZI_MARK: RegValue |= BIT14; break;
  4057. case HDLC_ENCODING_NRZI_SPACE: RegValue |= BIT14 + BIT13; break;
  4058. case HDLC_ENCODING_BIPHASE_MARK: RegValue |= BIT15; break;
  4059. case HDLC_ENCODING_BIPHASE_SPACE: RegValue |= BIT15 + BIT13; break;
  4060. case HDLC_ENCODING_BIPHASE_LEVEL: RegValue |= BIT15 + BIT14; break;
  4061. case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: RegValue |= BIT15 + BIT14 + BIT13; break;
  4062. }
  4063. if ( (info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_16_CCITT )
  4064. RegValue |= BIT9;
  4065. else if ( (info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_32_CCITT )
  4066. RegValue |= ( BIT12 | BIT10 | BIT9 );
  4067. usc_OutReg( info, RMR, RegValue );
  4068. /* Set the Receive count Limit Register (RCLR) to 0xffff. */
  4069. /* When an opening flag of an SDLC frame is recognized the */
  4070. /* Receive Character count (RCC) is loaded with the value in */
  4071. /* RCLR. The RCC is decremented for each received byte. The */
  4072. /* value of RCC is stored after the closing flag of the frame */
  4073. /* allowing the frame size to be computed. */
  4074. usc_OutReg( info, RCLR, RCLRVALUE );
  4075. usc_RCmd( info, RCmd_SelectRicrdma_level );
  4076. /* Receive Interrupt Control Register (RICR)
  4077. *
  4078. * <15..8> ? RxFIFO DMA Request Level
  4079. * <7> 0 Exited Hunt IA (Interrupt Arm)
  4080. * <6> 0 Idle Received IA
  4081. * <5> 0 Break/Abort IA
  4082. * <4> 0 Rx Bound IA
  4083. * <3> 1 Queued status reflects oldest 2 bytes in FIFO
  4084. * <2> 0 Abort/PE IA
  4085. * <1> 1 Rx Overrun IA
  4086. * <0> 0 Select TC0 value for readback
  4087. *
  4088. * 0000 0000 0000 1000 = 0x000a
  4089. */
  4090. /* Carry over the Exit Hunt and Idle Received bits */
  4091. /* in case they have been armed by usc_ArmEvents. */
  4092. RegValue = usc_InReg( info, RICR ) & 0xc0;
  4093. if ( info->bus_type == MGSL_BUS_TYPE_PCI )
  4094. usc_OutReg( info, RICR, (u16)(0x030a | RegValue) );
  4095. else
  4096. usc_OutReg( info, RICR, (u16)(0x140a | RegValue) );
  4097. /* Unlatch all Rx status bits and clear Rx status IRQ Pending */
  4098. usc_UnlatchRxstatusBits( info, RXSTATUS_ALL );
  4099. usc_ClearIrqPendingBits( info, RECEIVE_STATUS );
  4100. /* Transmit mode Register (TMR)
  4101. *
  4102. * <15..13> 000 encoding
  4103. * <12..11> 00 FCS = 16bit CRC CCITT (x15 + x12 + x5 + 1)
  4104. * <10> 1 1 = Start CRC as all 1s (use for SDLC/HDLC)
  4105. * <9> 0 1 = Tx CRC Enabled
  4106. * <8> 0 1 = Append CRC to end of transmit frame
  4107. * <7..6> 00 Transmit parity Even
  4108. * <5> 0 Transmit parity Disabled
  4109. * <4..2> 000 Tx Char Length = 8 bits
  4110. * <1..0> 00 Disable Transmitter
  4111. *
  4112. * 0000 0100 0000 0000 = 0x0400
  4113. */
  4114. RegValue = 0x0400;
  4115. switch ( info->params.encoding ) {
  4116. case HDLC_ENCODING_NRZB: RegValue |= BIT13; break;
  4117. case HDLC_ENCODING_NRZI_MARK: RegValue |= BIT14; break;
  4118. case HDLC_ENCODING_NRZI_SPACE: RegValue |= BIT14 + BIT13; break;
  4119. case HDLC_ENCODING_BIPHASE_MARK: RegValue |= BIT15; break;
  4120. case HDLC_ENCODING_BIPHASE_SPACE: RegValue |= BIT15 + BIT13; break;
  4121. case HDLC_ENCODING_BIPHASE_LEVEL: RegValue |= BIT15 + BIT14; break;
  4122. case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: RegValue |= BIT15 + BIT14 + BIT13; break;
  4123. }
  4124. if ( (info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_16_CCITT )
  4125. RegValue |= BIT9 + BIT8;
  4126. else if ( (info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_32_CCITT )
  4127. RegValue |= ( BIT12 | BIT10 | BIT9 | BIT8);
  4128. usc_OutReg( info, TMR, RegValue );
  4129. usc_set_txidle( info );
  4130. usc_TCmd( info, TCmd_SelectTicrdma_level );
  4131. /* Transmit Interrupt Control Register (TICR)
  4132. *
  4133. * <15..8> ? Transmit FIFO DMA Level
  4134. * <7> 0 Present IA (Interrupt Arm)
  4135. * <6> 0 Idle Sent IA
  4136. * <5> 1 Abort Sent IA
  4137. * <4> 1 EOF/EOM Sent IA
  4138. * <3> 0 CRC Sent IA
  4139. * <2> 1 1 = Wait for SW Trigger to Start Frame
  4140. * <1> 1 Tx Underrun IA
  4141. * <0> 0 TC0 constant on read back
  4142. *
  4143. * 0000 0000 0011 0110 = 0x0036
  4144. */
  4145. if ( info->bus_type == MGSL_BUS_TYPE_PCI )
  4146. usc_OutReg( info, TICR, 0x0736 );
  4147. else
  4148. usc_OutReg( info, TICR, 0x1436 );
  4149. usc_UnlatchTxstatusBits( info, TXSTATUS_ALL );
  4150. usc_ClearIrqPendingBits( info, TRANSMIT_STATUS );
  4151. /*
  4152. ** Transmit Command/Status Register (TCSR)
  4153. **
  4154. ** <15..12> 0000 TCmd
  4155. ** <11> 0/1 UnderWait
  4156. ** <10..08> 000 TxIdle
  4157. ** <7> x PreSent
  4158. ** <6> x IdleSent
  4159. ** <5> x AbortSent
  4160. ** <4> x EOF/EOM Sent
  4161. ** <3> x CRC Sent
  4162. ** <2> x All Sent
  4163. ** <1> x TxUnder
  4164. ** <0> x TxEmpty
  4165. **
  4166. ** 0000 0000 0000 0000 = 0x0000
  4167. */
  4168. info->tcsr_value = 0;
  4169. if ( !PreSL1660 )
  4170. info->tcsr_value |= TCSR_UNDERWAIT;
  4171. usc_OutReg( info, TCSR, info->tcsr_value );
  4172. /* Clock mode Control Register (CMCR)
  4173. *
  4174. * <15..14> 00 counter 1 Source = Disabled
  4175. * <13..12> 00 counter 0 Source = Disabled
  4176. * <11..10> 11 BRG1 Input is TxC Pin
  4177. * <9..8> 11 BRG0 Input is TxC Pin
  4178. * <7..6> 01 DPLL Input is BRG1 Output
  4179. * <5..3> XXX TxCLK comes from Port 0
  4180. * <2..0> XXX RxCLK comes from Port 1
  4181. *
  4182. * 0000 1111 0111 0111 = 0x0f77
  4183. */
  4184. RegValue = 0x0f40;
  4185. if ( info->params.flags & HDLC_FLAG_RXC_DPLL )
  4186. RegValue |= 0x0003; /* RxCLK from DPLL */
  4187. else if ( info->params.flags & HDLC_FLAG_RXC_BRG )
  4188. RegValue |= 0x0004; /* RxCLK from BRG0 */
  4189. else if ( info->params.flags & HDLC_FLAG_RXC_TXCPIN)
  4190. RegValue |= 0x0006; /* RxCLK from TXC Input */
  4191. else
  4192. RegValue |= 0x0007; /* RxCLK from Port1 */
  4193. if ( info->params.flags & HDLC_FLAG_TXC_DPLL )
  4194. RegValue |= 0x0018; /* TxCLK from DPLL */
  4195. else if ( info->params.flags & HDLC_FLAG_TXC_BRG )
  4196. RegValue |= 0x0020; /* TxCLK from BRG0 */
  4197. else if ( info->params.flags & HDLC_FLAG_TXC_RXCPIN)
  4198. RegValue |= 0x0038; /* RxCLK from TXC Input */
  4199. else
  4200. RegValue |= 0x0030; /* TxCLK from Port0 */
  4201. usc_OutReg( info, CMCR, RegValue );
  4202. /* Hardware Configuration Register (HCR)
  4203. *
  4204. * <15..14> 00 CTR0 Divisor:00=32,01=16,10=8,11=4
  4205. * <13> 0 CTR1DSel:0=CTR0Div determines CTR0Div
  4206. * <12> 0 CVOK:0=report code violation in biphase
  4207. * <11..10> 00 DPLL Divisor:00=32,01=16,10=8,11=4
  4208. * <9..8> XX DPLL mode:00=disable,01=NRZ,10=Biphase,11=Biphase Level
  4209. * <7..6> 00 reserved
  4210. * <5> 0 BRG1 mode:0=continuous,1=single cycle
  4211. * <4> X BRG1 Enable
  4212. * <3..2> 00 reserved
  4213. * <1> 0 BRG0 mode:0=continuous,1=single cycle
  4214. * <0> 0 BRG0 Enable
  4215. */
  4216. RegValue = 0x0000;
  4217. if ( info->params.flags & (HDLC_FLAG_RXC_DPLL + HDLC_FLAG_TXC_DPLL) ) {
  4218. u32 XtalSpeed;
  4219. u32 DpllDivisor;
  4220. u16 Tc;
  4221. /* DPLL is enabled. Use BRG1 to provide continuous reference clock */
  4222. /* for DPLL. DPLL mode in HCR is dependent on the encoding used. */
  4223. if ( info->bus_type == MGSL_BUS_TYPE_PCI )
  4224. XtalSpeed = 11059200;
  4225. else
  4226. XtalSpeed = 14745600;
  4227. if ( info->params.flags & HDLC_FLAG_DPLL_DIV16 ) {
  4228. DpllDivisor = 16;
  4229. RegValue |= BIT10;
  4230. }
  4231. else if ( info->params.flags & HDLC_FLAG_DPLL_DIV8 ) {
  4232. DpllDivisor = 8;
  4233. RegValue |= BIT11;
  4234. }
  4235. else
  4236. DpllDivisor = 32;
  4237. /* Tc = (Xtal/Speed) - 1 */
  4238. /* If twice the remainder of (Xtal/Speed) is greater than Speed */
  4239. /* then rounding up gives a more precise time constant. Instead */
  4240. /* of rounding up and then subtracting 1 we just don't subtract */
  4241. /* the one in this case. */
  4242. /*--------------------------------------------------
  4243. * ejz: for DPLL mode, application should use the
  4244. * same clock speed as the partner system, even
  4245. * though clocking is derived from the input RxData.
  4246. * In case the user uses a 0 for the clock speed,
  4247. * default to 0xffffffff and don't try to divide by
  4248. * zero
  4249. *--------------------------------------------------*/
  4250. if ( info->params.clock_speed )
  4251. {
  4252. Tc = (u16)((XtalSpeed/DpllDivisor)/info->params.clock_speed);
  4253. if ( !((((XtalSpeed/DpllDivisor) % info->params.clock_speed) * 2)
  4254. / info->params.clock_speed) )
  4255. Tc--;
  4256. }
  4257. else
  4258. Tc = -1;
  4259. /* Write 16-bit Time Constant for BRG1 */
  4260. usc_OutReg( info, TC1R, Tc );
  4261. RegValue |= BIT4; /* enable BRG1 */
  4262. switch ( info->params.encoding ) {
  4263. case HDLC_ENCODING_NRZ:
  4264. case HDLC_ENCODING_NRZB:
  4265. case HDLC_ENCODING_NRZI_MARK:
  4266. case HDLC_ENCODING_NRZI_SPACE: RegValue |= BIT8; break;
  4267. case HDLC_ENCODING_BIPHASE_MARK:
  4268. case HDLC_ENCODING_BIPHASE_SPACE: RegValue |= BIT9; break;
  4269. case HDLC_ENCODING_BIPHASE_LEVEL:
  4270. case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: RegValue |= BIT9 + BIT8; break;
  4271. }
  4272. }
  4273. usc_OutReg( info, HCR, RegValue );
  4274. /* Channel Control/status Register (CCSR)
  4275. *
  4276. * <15> X RCC FIFO Overflow status (RO)
  4277. * <14> X RCC FIFO Not Empty status (RO)
  4278. * <13> 0 1 = Clear RCC FIFO (WO)
  4279. * <12> X DPLL Sync (RW)
  4280. * <11> X DPLL 2 Missed Clocks status (RO)
  4281. * <10> X DPLL 1 Missed Clock status (RO)
  4282. * <9..8> 00 DPLL Resync on rising and falling edges (RW)
  4283. * <7> X SDLC Loop On status (RO)
  4284. * <6> X SDLC Loop Send status (RO)
  4285. * <5> 1 Bypass counters for TxClk and RxClk (RW)
  4286. * <4..2> 000 Last Char of SDLC frame has 8 bits (RW)
  4287. * <1..0> 00 reserved
  4288. *
  4289. * 0000 0000 0010 0000 = 0x0020
  4290. */
  4291. usc_OutReg( info, CCSR, 0x1020 );
  4292. if ( info->params.flags & HDLC_FLAG_AUTO_CTS ) {
  4293. usc_OutReg( info, SICR,
  4294. (u16)(usc_InReg(info,SICR) | SICR_CTS_INACTIVE) );
  4295. }
  4296. /* enable Master Interrupt Enable bit (MIE) */
  4297. usc_EnableMasterIrqBit( info );
  4298. usc_ClearIrqPendingBits( info, RECEIVE_STATUS + RECEIVE_DATA +
  4299. TRANSMIT_STATUS + TRANSMIT_DATA + MISC);
  4300. /* arm RCC underflow interrupt */
  4301. usc_OutReg(info, SICR, (u16)(usc_InReg(info,SICR) | BIT3));
  4302. usc_EnableInterrupts(info, MISC);
  4303. info->mbre_bit = 0;
  4304. outw( 0, info->io_base ); /* clear Master Bus Enable (DCAR) */
  4305. usc_DmaCmd( info, DmaCmd_ResetAllChannels ); /* disable both DMA channels */
  4306. info->mbre_bit = BIT8;
  4307. outw( BIT8, info->io_base ); /* set Master Bus Enable (DCAR) */
  4308. if (info->bus_type == MGSL_BUS_TYPE_ISA) {
  4309. /* Enable DMAEN (Port 7, Bit 14) */
  4310. /* This connects the DMA request signal to the ISA bus */
  4311. usc_OutReg(info, PCR, (u16)((usc_InReg(info, PCR) | BIT15) & ~BIT14));
  4312. }
  4313. /* DMA Control Register (DCR)
  4314. *
  4315. * <15..14> 10 Priority mode = Alternating Tx/Rx
  4316. * 01 Rx has priority
  4317. * 00 Tx has priority
  4318. *
  4319. * <13> 1 Enable Priority Preempt per DCR<15..14>
  4320. * (WARNING DCR<11..10> must be 00 when this is 1)
  4321. * 0 Choose activate channel per DCR<11..10>
  4322. *
  4323. * <12> 0 Little Endian for Array/List
  4324. * <11..10> 00 Both Channels can use each bus grant
  4325. * <9..6> 0000 reserved
  4326. * <5> 0 7 CLK - Minimum Bus Re-request Interval
  4327. * <4> 0 1 = drive D/C and S/D pins
  4328. * <3> 1 1 = Add one wait state to all DMA cycles.
  4329. * <2> 0 1 = Strobe /UAS on every transfer.
  4330. * <1..0> 11 Addr incrementing only affects LS24 bits
  4331. *
  4332. * 0110 0000 0000 1011 = 0x600b
  4333. */
  4334. if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
  4335. /* PCI adapter does not need DMA wait state */
  4336. usc_OutDmaReg( info, DCR, 0xa00b );
  4337. }
  4338. else
  4339. usc_OutDmaReg( info, DCR, 0x800b );
  4340. /* Receive DMA mode Register (RDMR)
  4341. *
  4342. * <15..14> 11 DMA mode = Linked List Buffer mode
  4343. * <13> 1 RSBinA/L = store Rx status Block in Arrary/List entry
  4344. * <12> 1 Clear count of List Entry after fetching
  4345. * <11..10> 00 Address mode = Increment
  4346. * <9> 1 Terminate Buffer on RxBound
  4347. * <8> 0 Bus Width = 16bits
  4348. * <7..0> ? status Bits (write as 0s)
  4349. *
  4350. * 1111 0010 0000 0000 = 0xf200
  4351. */
  4352. usc_OutDmaReg( info, RDMR, 0xf200 );
  4353. /* Transmit DMA mode Register (TDMR)
  4354. *
  4355. * <15..14> 11 DMA mode = Linked List Buffer mode
  4356. * <13> 1 TCBinA/L = fetch Tx Control Block from List entry
  4357. * <12> 1 Clear count of List Entry after fetching
  4358. * <11..10> 00 Address mode = Increment
  4359. * <9> 1 Terminate Buffer on end of frame
  4360. * <8> 0 Bus Width = 16bits
  4361. * <7..0> ? status Bits (Read Only so write as 0)
  4362. *
  4363. * 1111 0010 0000 0000 = 0xf200
  4364. */
  4365. usc_OutDmaReg( info, TDMR, 0xf200 );
  4366. /* DMA Interrupt Control Register (DICR)
  4367. *
  4368. * <15> 1 DMA Interrupt Enable
  4369. * <14> 0 1 = Disable IEO from USC
  4370. * <13> 0 1 = Don't provide vector during IntAck
  4371. * <12> 1 1 = Include status in Vector
  4372. * <10..2> 0 reserved, Must be 0s
  4373. * <1> 0 1 = Rx DMA Interrupt Enabled
  4374. * <0> 0 1 = Tx DMA Interrupt Enabled
  4375. *
  4376. * 1001 0000 0000 0000 = 0x9000
  4377. */
  4378. usc_OutDmaReg( info, DICR, 0x9000 );
  4379. usc_InDmaReg( info, RDMR ); /* clear pending receive DMA IRQ bits */
  4380. usc_InDmaReg( info, TDMR ); /* clear pending transmit DMA IRQ bits */
  4381. usc_OutDmaReg( info, CDIR, 0x0303 ); /* clear IUS and Pending for Tx and Rx */
  4382. /* Channel Control Register (CCR)
  4383. *
  4384. * <15..14> 10 Use 32-bit Tx Control Blocks (TCBs)
  4385. * <13> 0 Trigger Tx on SW Command Disabled
  4386. * <12> 0 Flag Preamble Disabled
  4387. * <11..10> 00 Preamble Length
  4388. * <9..8> 00 Preamble Pattern
  4389. * <7..6> 10 Use 32-bit Rx status Blocks (RSBs)
  4390. * <5> 0 Trigger Rx on SW Command Disabled
  4391. * <4..0> 0 reserved
  4392. *
  4393. * 1000 0000 1000 0000 = 0x8080
  4394. */
  4395. RegValue = 0x8080;
  4396. switch ( info->params.preamble_length ) {
  4397. case HDLC_PREAMBLE_LENGTH_16BITS: RegValue |= BIT10; break;
  4398. case HDLC_PREAMBLE_LENGTH_32BITS: RegValue |= BIT11; break;
  4399. case HDLC_PREAMBLE_LENGTH_64BITS: RegValue |= BIT11 + BIT10; break;
  4400. }
  4401. switch ( info->params.preamble ) {
  4402. case HDLC_PREAMBLE_PATTERN_FLAGS: RegValue |= BIT8 + BIT12; break;
  4403. case HDLC_PREAMBLE_PATTERN_ONES: RegValue |= BIT8; break;
  4404. case HDLC_PREAMBLE_PATTERN_10: RegValue |= BIT9; break;
  4405. case HDLC_PREAMBLE_PATTERN_01: RegValue |= BIT9 + BIT8; break;
  4406. }
  4407. usc_OutReg( info, CCR, RegValue );
  4408. /*
  4409. * Burst/Dwell Control Register
  4410. *
  4411. * <15..8> 0x20 Maximum number of transfers per bus grant
  4412. * <7..0> 0x00 Maximum number of clock cycles per bus grant
  4413. */
  4414. if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
  4415. /* don't limit bus occupancy on PCI adapter */
  4416. usc_OutDmaReg( info, BDCR, 0x0000 );
  4417. }
  4418. else
  4419. usc_OutDmaReg( info, BDCR, 0x2000 );
  4420. usc_stop_transmitter(info);
  4421. usc_stop_receiver(info);
  4422. } /* end of usc_set_sdlc_mode() */
  4423. /* usc_enable_loopback()
  4424. *
  4425. * Set the 16C32 for internal loopback mode.
  4426. * The TxCLK and RxCLK signals are generated from the BRG0 and
  4427. * the TxD is looped back to the RxD internally.
  4428. *
  4429. * Arguments: info pointer to device instance data
  4430. * enable 1 = enable loopback, 0 = disable
  4431. * Return Value: None
  4432. */
  4433. static void usc_enable_loopback(struct mgsl_struct *info, int enable)
  4434. {
  4435. if (enable) {
  4436. /* blank external TXD output */
  4437. usc_OutReg(info,IOCR,usc_InReg(info,IOCR) | (BIT7+BIT6));
  4438. /* Clock mode Control Register (CMCR)
  4439. *
  4440. * <15..14> 00 counter 1 Disabled
  4441. * <13..12> 00 counter 0 Disabled
  4442. * <11..10> 11 BRG1 Input is TxC Pin
  4443. * <9..8> 11 BRG0 Input is TxC Pin
  4444. * <7..6> 01 DPLL Input is BRG1 Output
  4445. * <5..3> 100 TxCLK comes from BRG0
  4446. * <2..0> 100 RxCLK comes from BRG0
  4447. *
  4448. * 0000 1111 0110 0100 = 0x0f64
  4449. */
  4450. usc_OutReg( info, CMCR, 0x0f64 );
  4451. /* Write 16-bit Time Constant for BRG0 */
  4452. /* use clock speed if available, otherwise use 8 for diagnostics */
  4453. if (info->params.clock_speed) {
  4454. if (info->bus_type == MGSL_BUS_TYPE_PCI)
  4455. usc_OutReg(info, TC0R, (u16)((11059200/info->params.clock_speed)-1));
  4456. else
  4457. usc_OutReg(info, TC0R, (u16)((14745600/info->params.clock_speed)-1));
  4458. } else
  4459. usc_OutReg(info, TC0R, (u16)8);
  4460. /* Hardware Configuration Register (HCR) Clear Bit 1, BRG0
  4461. mode = Continuous Set Bit 0 to enable BRG0. */
  4462. usc_OutReg( info, HCR, (u16)((usc_InReg( info, HCR ) & ~BIT1) | BIT0) );
  4463. /* Input/Output Control Reg, <2..0> = 100, Drive RxC pin with BRG0 */
  4464. usc_OutReg(info, IOCR, (u16)((usc_InReg(info, IOCR) & 0xfff8) | 0x0004));
  4465. /* set Internal Data loopback mode */
  4466. info->loopback_bits = 0x300;
  4467. outw( 0x0300, info->io_base + CCAR );
  4468. } else {
  4469. /* enable external TXD output */
  4470. usc_OutReg(info,IOCR,usc_InReg(info,IOCR) & ~(BIT7+BIT6));
  4471. /* clear Internal Data loopback mode */
  4472. info->loopback_bits = 0;
  4473. outw( 0,info->io_base + CCAR );
  4474. }
  4475. } /* end of usc_enable_loopback() */
  4476. /* usc_enable_aux_clock()
  4477. *
  4478. * Enabled the AUX clock output at the specified frequency.
  4479. *
  4480. * Arguments:
  4481. *
  4482. * info pointer to device extension
  4483. * data_rate data rate of clock in bits per second
  4484. * A data rate of 0 disables the AUX clock.
  4485. *
  4486. * Return Value: None
  4487. */
  4488. static void usc_enable_aux_clock( struct mgsl_struct *info, u32 data_rate )
  4489. {
  4490. u32 XtalSpeed;
  4491. u16 Tc;
  4492. if ( data_rate ) {
  4493. if ( info->bus_type == MGSL_BUS_TYPE_PCI )
  4494. XtalSpeed = 11059200;
  4495. else
  4496. XtalSpeed = 14745600;
  4497. /* Tc = (Xtal/Speed) - 1 */
  4498. /* If twice the remainder of (Xtal/Speed) is greater than Speed */
  4499. /* then rounding up gives a more precise time constant. Instead */
  4500. /* of rounding up and then subtracting 1 we just don't subtract */
  4501. /* the one in this case. */
  4502. Tc = (u16)(XtalSpeed/data_rate);
  4503. if ( !(((XtalSpeed % data_rate) * 2) / data_rate) )
  4504. Tc--;
  4505. /* Write 16-bit Time Constant for BRG0 */
  4506. usc_OutReg( info, TC0R, Tc );
  4507. /*
  4508. * Hardware Configuration Register (HCR)
  4509. * Clear Bit 1, BRG0 mode = Continuous
  4510. * Set Bit 0 to enable BRG0.
  4511. */
  4512. usc_OutReg( info, HCR, (u16)((usc_InReg( info, HCR ) & ~BIT1) | BIT0) );
  4513. /* Input/Output Control Reg, <2..0> = 100, Drive RxC pin with BRG0 */
  4514. usc_OutReg( info, IOCR, (u16)((usc_InReg(info, IOCR) & 0xfff8) | 0x0004) );
  4515. } else {
  4516. /* data rate == 0 so turn off BRG0 */
  4517. usc_OutReg( info, HCR, (u16)(usc_InReg( info, HCR ) & ~BIT0) );
  4518. }
  4519. } /* end of usc_enable_aux_clock() */
  4520. /*
  4521. *
  4522. * usc_process_rxoverrun_sync()
  4523. *
  4524. * This function processes a receive overrun by resetting the
  4525. * receive DMA buffers and issuing a Purge Rx FIFO command
  4526. * to allow the receiver to continue receiving.
  4527. *
  4528. * Arguments:
  4529. *
  4530. * info pointer to device extension
  4531. *
  4532. * Return Value: None
  4533. */
  4534. static void usc_process_rxoverrun_sync( struct mgsl_struct *info )
  4535. {
  4536. int start_index;
  4537. int end_index;
  4538. int frame_start_index;
  4539. bool start_of_frame_found = false;
  4540. bool end_of_frame_found = false;
  4541. bool reprogram_dma = false;
  4542. DMABUFFERENTRY *buffer_list = info->rx_buffer_list;
  4543. u32 phys_addr;
  4544. usc_DmaCmd( info, DmaCmd_PauseRxChannel );
  4545. usc_RCmd( info, RCmd_EnterHuntmode );
  4546. usc_RTCmd( info, RTCmd_PurgeRxFifo );
  4547. /* CurrentRxBuffer points to the 1st buffer of the next */
  4548. /* possibly available receive frame. */
  4549. frame_start_index = start_index = end_index = info->current_rx_buffer;
  4550. /* Search for an unfinished string of buffers. This means */
  4551. /* that a receive frame started (at least one buffer with */
  4552. /* count set to zero) but there is no terminiting buffer */
  4553. /* (status set to non-zero). */
  4554. while( !buffer_list[end_index].count )
  4555. {
  4556. /* Count field has been reset to zero by 16C32. */
  4557. /* This buffer is currently in use. */
  4558. if ( !start_of_frame_found )
  4559. {
  4560. start_of_frame_found = true;
  4561. frame_start_index = end_index;
  4562. end_of_frame_found = false;
  4563. }
  4564. if ( buffer_list[end_index].status )
  4565. {
  4566. /* Status field has been set by 16C32. */
  4567. /* This is the last buffer of a received frame. */
  4568. /* We want to leave the buffers for this frame intact. */
  4569. /* Move on to next possible frame. */
  4570. start_of_frame_found = false;
  4571. end_of_frame_found = true;
  4572. }
  4573. /* advance to next buffer entry in linked list */
  4574. end_index++;
  4575. if ( end_index == info->rx_buffer_count )
  4576. end_index = 0;
  4577. if ( start_index == end_index )
  4578. {
  4579. /* The entire list has been searched with all Counts == 0 and */
  4580. /* all Status == 0. The receive buffers are */
  4581. /* completely screwed, reset all receive buffers! */
  4582. mgsl_reset_rx_dma_buffers( info );
  4583. frame_start_index = 0;
  4584. start_of_frame_found = false;
  4585. reprogram_dma = true;
  4586. break;
  4587. }
  4588. }
  4589. if ( start_of_frame_found && !end_of_frame_found )
  4590. {
  4591. /* There is an unfinished string of receive DMA buffers */
  4592. /* as a result of the receiver overrun. */
  4593. /* Reset the buffers for the unfinished frame */
  4594. /* and reprogram the receive DMA controller to start */
  4595. /* at the 1st buffer of unfinished frame. */
  4596. start_index = frame_start_index;
  4597. do
  4598. {
  4599. *((unsigned long *)&(info->rx_buffer_list[start_index++].count)) = DMABUFFERSIZE;
  4600. /* Adjust index for wrap around. */
  4601. if ( start_index == info->rx_buffer_count )
  4602. start_index = 0;
  4603. } while( start_index != end_index );
  4604. reprogram_dma = true;
  4605. }
  4606. if ( reprogram_dma )
  4607. {
  4608. usc_UnlatchRxstatusBits(info,RXSTATUS_ALL);
  4609. usc_ClearIrqPendingBits(info, RECEIVE_DATA|RECEIVE_STATUS);
  4610. usc_UnlatchRxstatusBits(info, RECEIVE_DATA|RECEIVE_STATUS);
  4611. usc_EnableReceiver(info,DISABLE_UNCONDITIONAL);
  4612. /* This empties the receive FIFO and loads the RCC with RCLR */
  4613. usc_OutReg( info, CCSR, (u16)(usc_InReg(info,CCSR) | BIT13) );
  4614. /* program 16C32 with physical address of 1st DMA buffer entry */
  4615. phys_addr = info->rx_buffer_list[frame_start_index].phys_entry;
  4616. usc_OutDmaReg( info, NRARL, (u16)phys_addr );
  4617. usc_OutDmaReg( info, NRARU, (u16)(phys_addr >> 16) );
  4618. usc_UnlatchRxstatusBits( info, RXSTATUS_ALL );
  4619. usc_ClearIrqPendingBits( info, RECEIVE_DATA + RECEIVE_STATUS );
  4620. usc_EnableInterrupts( info, RECEIVE_STATUS );
  4621. /* 1. Arm End of Buffer (EOB) Receive DMA Interrupt (BIT2 of RDIAR) */
  4622. /* 2. Enable Receive DMA Interrupts (BIT1 of DICR) */
  4623. usc_OutDmaReg( info, RDIAR, BIT3 + BIT2 );
  4624. usc_OutDmaReg( info, DICR, (u16)(usc_InDmaReg(info,DICR) | BIT1) );
  4625. usc_DmaCmd( info, DmaCmd_InitRxChannel );
  4626. if ( info->params.flags & HDLC_FLAG_AUTO_DCD )
  4627. usc_EnableReceiver(info,ENABLE_AUTO_DCD);
  4628. else
  4629. usc_EnableReceiver(info,ENABLE_UNCONDITIONAL);
  4630. }
  4631. else
  4632. {
  4633. /* This empties the receive FIFO and loads the RCC with RCLR */
  4634. usc_OutReg( info, CCSR, (u16)(usc_InReg(info,CCSR) | BIT13) );
  4635. usc_RTCmd( info, RTCmd_PurgeRxFifo );
  4636. }
  4637. } /* end of usc_process_rxoverrun_sync() */
  4638. /* usc_stop_receiver()
  4639. *
  4640. * Disable USC receiver
  4641. *
  4642. * Arguments: info pointer to device instance data
  4643. * Return Value: None
  4644. */
  4645. static void usc_stop_receiver( struct mgsl_struct *info )
  4646. {
  4647. if (debug_level >= DEBUG_LEVEL_ISR)
  4648. printk("%s(%d):usc_stop_receiver(%s)\n",
  4649. __FILE__,__LINE__, info->device_name );
  4650. /* Disable receive DMA channel. */
  4651. /* This also disables receive DMA channel interrupts */
  4652. usc_DmaCmd( info, DmaCmd_ResetRxChannel );
  4653. usc_UnlatchRxstatusBits( info, RXSTATUS_ALL );
  4654. usc_ClearIrqPendingBits( info, RECEIVE_DATA + RECEIVE_STATUS );
  4655. usc_DisableInterrupts( info, RECEIVE_DATA + RECEIVE_STATUS );
  4656. usc_EnableReceiver(info,DISABLE_UNCONDITIONAL);
  4657. /* This empties the receive FIFO and loads the RCC with RCLR */
  4658. usc_OutReg( info, CCSR, (u16)(usc_InReg(info,CCSR) | BIT13) );
  4659. usc_RTCmd( info, RTCmd_PurgeRxFifo );
  4660. info->rx_enabled = false;
  4661. info->rx_overflow = false;
  4662. info->rx_rcc_underrun = false;
  4663. } /* end of stop_receiver() */
  4664. /* usc_start_receiver()
  4665. *
  4666. * Enable the USC receiver
  4667. *
  4668. * Arguments: info pointer to device instance data
  4669. * Return Value: None
  4670. */
  4671. static void usc_start_receiver( struct mgsl_struct *info )
  4672. {
  4673. u32 phys_addr;
  4674. if (debug_level >= DEBUG_LEVEL_ISR)
  4675. printk("%s(%d):usc_start_receiver(%s)\n",
  4676. __FILE__,__LINE__, info->device_name );
  4677. mgsl_reset_rx_dma_buffers( info );
  4678. usc_stop_receiver( info );
  4679. usc_OutReg( info, CCSR, (u16)(usc_InReg(info,CCSR) | BIT13) );
  4680. usc_RTCmd( info, RTCmd_PurgeRxFifo );
  4681. if ( info->params.mode == MGSL_MODE_HDLC ||
  4682. info->params.mode == MGSL_MODE_RAW ) {
  4683. /* DMA mode Transfers */
  4684. /* Program the DMA controller. */
  4685. /* Enable the DMA controller end of buffer interrupt. */
  4686. /* program 16C32 with physical address of 1st DMA buffer entry */
  4687. phys_addr = info->rx_buffer_list[0].phys_entry;
  4688. usc_OutDmaReg( info, NRARL, (u16)phys_addr );
  4689. usc_OutDmaReg( info, NRARU, (u16)(phys_addr >> 16) );
  4690. usc_UnlatchRxstatusBits( info, RXSTATUS_ALL );
  4691. usc_ClearIrqPendingBits( info, RECEIVE_DATA + RECEIVE_STATUS );
  4692. usc_EnableInterrupts( info, RECEIVE_STATUS );
  4693. /* 1. Arm End of Buffer (EOB) Receive DMA Interrupt (BIT2 of RDIAR) */
  4694. /* 2. Enable Receive DMA Interrupts (BIT1 of DICR) */
  4695. usc_OutDmaReg( info, RDIAR, BIT3 + BIT2 );
  4696. usc_OutDmaReg( info, DICR, (u16)(usc_InDmaReg(info,DICR) | BIT1) );
  4697. usc_DmaCmd( info, DmaCmd_InitRxChannel );
  4698. if ( info->params.flags & HDLC_FLAG_AUTO_DCD )
  4699. usc_EnableReceiver(info,ENABLE_AUTO_DCD);
  4700. else
  4701. usc_EnableReceiver(info,ENABLE_UNCONDITIONAL);
  4702. } else {
  4703. usc_UnlatchRxstatusBits(info, RXSTATUS_ALL);
  4704. usc_ClearIrqPendingBits(info, RECEIVE_DATA + RECEIVE_STATUS);
  4705. usc_EnableInterrupts(info, RECEIVE_DATA);
  4706. usc_RTCmd( info, RTCmd_PurgeRxFifo );
  4707. usc_RCmd( info, RCmd_EnterHuntmode );
  4708. usc_EnableReceiver(info,ENABLE_UNCONDITIONAL);
  4709. }
  4710. usc_OutReg( info, CCSR, 0x1020 );
  4711. info->rx_enabled = true;
  4712. } /* end of usc_start_receiver() */
  4713. /* usc_start_transmitter()
  4714. *
  4715. * Enable the USC transmitter and send a transmit frame if
  4716. * one is loaded in the DMA buffers.
  4717. *
  4718. * Arguments: info pointer to device instance data
  4719. * Return Value: None
  4720. */
  4721. static void usc_start_transmitter( struct mgsl_struct *info )
  4722. {
  4723. u32 phys_addr;
  4724. unsigned int FrameSize;
  4725. if (debug_level >= DEBUG_LEVEL_ISR)
  4726. printk("%s(%d):usc_start_transmitter(%s)\n",
  4727. __FILE__,__LINE__, info->device_name );
  4728. if ( info->xmit_cnt ) {
  4729. /* If auto RTS enabled and RTS is inactive, then assert */
  4730. /* RTS and set a flag indicating that the driver should */
  4731. /* negate RTS when the transmission completes. */
  4732. info->drop_rts_on_tx_done = false;
  4733. if ( info->params.flags & HDLC_FLAG_AUTO_RTS ) {
  4734. usc_get_serial_signals( info );
  4735. if ( !(info->serial_signals & SerialSignal_RTS) ) {
  4736. info->serial_signals |= SerialSignal_RTS;
  4737. usc_set_serial_signals( info );
  4738. info->drop_rts_on_tx_done = true;
  4739. }
  4740. }
  4741. if ( info->params.mode == MGSL_MODE_ASYNC ) {
  4742. if ( !info->tx_active ) {
  4743. usc_UnlatchTxstatusBits(info, TXSTATUS_ALL);
  4744. usc_ClearIrqPendingBits(info, TRANSMIT_STATUS + TRANSMIT_DATA);
  4745. usc_EnableInterrupts(info, TRANSMIT_DATA);
  4746. usc_load_txfifo(info);
  4747. }
  4748. } else {
  4749. /* Disable transmit DMA controller while programming. */
  4750. usc_DmaCmd( info, DmaCmd_ResetTxChannel );
  4751. /* Transmit DMA buffer is loaded, so program USC */
  4752. /* to send the frame contained in the buffers. */
  4753. FrameSize = info->tx_buffer_list[info->start_tx_dma_buffer].rcc;
  4754. /* if operating in Raw sync mode, reset the rcc component
  4755. * of the tx dma buffer entry, otherwise, the serial controller
  4756. * will send a closing sync char after this count.
  4757. */
  4758. if ( info->params.mode == MGSL_MODE_RAW )
  4759. info->tx_buffer_list[info->start_tx_dma_buffer].rcc = 0;
  4760. /* Program the Transmit Character Length Register (TCLR) */
  4761. /* and clear FIFO (TCC is loaded with TCLR on FIFO clear) */
  4762. usc_OutReg( info, TCLR, (u16)FrameSize );
  4763. usc_RTCmd( info, RTCmd_PurgeTxFifo );
  4764. /* Program the address of the 1st DMA Buffer Entry in linked list */
  4765. phys_addr = info->tx_buffer_list[info->start_tx_dma_buffer].phys_entry;
  4766. usc_OutDmaReg( info, NTARL, (u16)phys_addr );
  4767. usc_OutDmaReg( info, NTARU, (u16)(phys_addr >> 16) );
  4768. usc_UnlatchTxstatusBits( info, TXSTATUS_ALL );
  4769. usc_ClearIrqPendingBits( info, TRANSMIT_STATUS );
  4770. usc_EnableInterrupts( info, TRANSMIT_STATUS );
  4771. if ( info->params.mode == MGSL_MODE_RAW &&
  4772. info->num_tx_dma_buffers > 1 ) {
  4773. /* When running external sync mode, attempt to 'stream' transmit */
  4774. /* by filling tx dma buffers as they become available. To do this */
  4775. /* we need to enable Tx DMA EOB Status interrupts : */
  4776. /* */
  4777. /* 1. Arm End of Buffer (EOB) Transmit DMA Interrupt (BIT2 of TDIAR) */
  4778. /* 2. Enable Transmit DMA Interrupts (BIT0 of DICR) */
  4779. usc_OutDmaReg( info, TDIAR, BIT2|BIT3 );
  4780. usc_OutDmaReg( info, DICR, (u16)(usc_InDmaReg(info,DICR) | BIT0) );
  4781. }
  4782. /* Initialize Transmit DMA Channel */
  4783. usc_DmaCmd( info, DmaCmd_InitTxChannel );
  4784. usc_TCmd( info, TCmd_SendFrame );
  4785. mod_timer(&info->tx_timer, jiffies +
  4786. msecs_to_jiffies(5000));
  4787. }
  4788. info->tx_active = true;
  4789. }
  4790. if ( !info->tx_enabled ) {
  4791. info->tx_enabled = true;
  4792. if ( info->params.flags & HDLC_FLAG_AUTO_CTS )
  4793. usc_EnableTransmitter(info,ENABLE_AUTO_CTS);
  4794. else
  4795. usc_EnableTransmitter(info,ENABLE_UNCONDITIONAL);
  4796. }
  4797. } /* end of usc_start_transmitter() */
  4798. /* usc_stop_transmitter()
  4799. *
  4800. * Stops the transmitter and DMA
  4801. *
  4802. * Arguments: info pointer to device isntance data
  4803. * Return Value: None
  4804. */
  4805. static void usc_stop_transmitter( struct mgsl_struct *info )
  4806. {
  4807. if (debug_level >= DEBUG_LEVEL_ISR)
  4808. printk("%s(%d):usc_stop_transmitter(%s)\n",
  4809. __FILE__,__LINE__, info->device_name );
  4810. del_timer(&info->tx_timer);
  4811. usc_UnlatchTxstatusBits( info, TXSTATUS_ALL );
  4812. usc_ClearIrqPendingBits( info, TRANSMIT_STATUS + TRANSMIT_DATA );
  4813. usc_DisableInterrupts( info, TRANSMIT_STATUS + TRANSMIT_DATA );
  4814. usc_EnableTransmitter(info,DISABLE_UNCONDITIONAL);
  4815. usc_DmaCmd( info, DmaCmd_ResetTxChannel );
  4816. usc_RTCmd( info, RTCmd_PurgeTxFifo );
  4817. info->tx_enabled = false;
  4818. info->tx_active = false;
  4819. } /* end of usc_stop_transmitter() */
  4820. /* usc_load_txfifo()
  4821. *
  4822. * Fill the transmit FIFO until the FIFO is full or
  4823. * there is no more data to load.
  4824. *
  4825. * Arguments: info pointer to device extension (instance data)
  4826. * Return Value: None
  4827. */
  4828. static void usc_load_txfifo( struct mgsl_struct *info )
  4829. {
  4830. int Fifocount;
  4831. u8 TwoBytes[2];
  4832. if ( !info->xmit_cnt && !info->x_char )
  4833. return;
  4834. /* Select transmit FIFO status readback in TICR */
  4835. usc_TCmd( info, TCmd_SelectTicrTxFifostatus );
  4836. /* load the Transmit FIFO until FIFOs full or all data sent */
  4837. while( (Fifocount = usc_InReg(info, TICR) >> 8) && info->xmit_cnt ) {
  4838. /* there is more space in the transmit FIFO and */
  4839. /* there is more data in transmit buffer */
  4840. if ( (info->xmit_cnt > 1) && (Fifocount > 1) && !info->x_char ) {
  4841. /* write a 16-bit word from transmit buffer to 16C32 */
  4842. TwoBytes[0] = info->xmit_buf[info->xmit_tail++];
  4843. info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
  4844. TwoBytes[1] = info->xmit_buf[info->xmit_tail++];
  4845. info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
  4846. outw( *((u16 *)TwoBytes), info->io_base + DATAREG);
  4847. info->xmit_cnt -= 2;
  4848. info->icount.tx += 2;
  4849. } else {
  4850. /* only 1 byte left to transmit or 1 FIFO slot left */
  4851. outw( (inw( info->io_base + CCAR) & 0x0780) | (TDR+LSBONLY),
  4852. info->io_base + CCAR );
  4853. if (info->x_char) {
  4854. /* transmit pending high priority char */
  4855. outw( info->x_char,info->io_base + CCAR );
  4856. info->x_char = 0;
  4857. } else {
  4858. outw( info->xmit_buf[info->xmit_tail++],info->io_base + CCAR );
  4859. info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
  4860. info->xmit_cnt--;
  4861. }
  4862. info->icount.tx++;
  4863. }
  4864. }
  4865. } /* end of usc_load_txfifo() */
  4866. /* usc_reset()
  4867. *
  4868. * Reset the adapter to a known state and prepare it for further use.
  4869. *
  4870. * Arguments: info pointer to device instance data
  4871. * Return Value: None
  4872. */
  4873. static void usc_reset( struct mgsl_struct *info )
  4874. {
  4875. if ( info->bus_type == MGSL_BUS_TYPE_PCI ) {
  4876. int i;
  4877. u32 readval;
  4878. /* Set BIT30 of Misc Control Register */
  4879. /* (Local Control Register 0x50) to force reset of USC. */
  4880. volatile u32 *MiscCtrl = (u32 *)(info->lcr_base + 0x50);
  4881. u32 *LCR0BRDR = (u32 *)(info->lcr_base + 0x28);
  4882. info->misc_ctrl_value |= BIT30;
  4883. *MiscCtrl = info->misc_ctrl_value;
  4884. /*
  4885. * Force at least 170ns delay before clearing
  4886. * reset bit. Each read from LCR takes at least
  4887. * 30ns so 10 times for 300ns to be safe.
  4888. */
  4889. for(i=0;i<10;i++)
  4890. readval = *MiscCtrl;
  4891. info->misc_ctrl_value &= ~BIT30;
  4892. *MiscCtrl = info->misc_ctrl_value;
  4893. *LCR0BRDR = BUS_DESCRIPTOR(
  4894. 1, // Write Strobe Hold (0-3)
  4895. 2, // Write Strobe Delay (0-3)
  4896. 2, // Read Strobe Delay (0-3)
  4897. 0, // NWDD (Write data-data) (0-3)
  4898. 4, // NWAD (Write Addr-data) (0-31)
  4899. 0, // NXDA (Read/Write Data-Addr) (0-3)
  4900. 0, // NRDD (Read Data-Data) (0-3)
  4901. 5 // NRAD (Read Addr-Data) (0-31)
  4902. );
  4903. } else {
  4904. /* do HW reset */
  4905. outb( 0,info->io_base + 8 );
  4906. }
  4907. info->mbre_bit = 0;
  4908. info->loopback_bits = 0;
  4909. info->usc_idle_mode = 0;
  4910. /*
  4911. * Program the Bus Configuration Register (BCR)
  4912. *
  4913. * <15> 0 Don't use separate address
  4914. * <14..6> 0 reserved
  4915. * <5..4> 00 IAckmode = Default, don't care
  4916. * <3> 1 Bus Request Totem Pole output
  4917. * <2> 1 Use 16 Bit data bus
  4918. * <1> 0 IRQ Totem Pole output
  4919. * <0> 0 Don't Shift Right Addr
  4920. *
  4921. * 0000 0000 0000 1100 = 0x000c
  4922. *
  4923. * By writing to io_base + SDPIN the Wait/Ack pin is
  4924. * programmed to work as a Wait pin.
  4925. */
  4926. outw( 0x000c,info->io_base + SDPIN );
  4927. outw( 0,info->io_base );
  4928. outw( 0,info->io_base + CCAR );
  4929. /* select little endian byte ordering */
  4930. usc_RTCmd( info, RTCmd_SelectLittleEndian );
  4931. /* Port Control Register (PCR)
  4932. *
  4933. * <15..14> 11 Port 7 is Output (~DMAEN, Bit 14 : 0 = Enabled)
  4934. * <13..12> 11 Port 6 is Output (~INTEN, Bit 12 : 0 = Enabled)
  4935. * <11..10> 00 Port 5 is Input (No Connect, Don't Care)
  4936. * <9..8> 00 Port 4 is Input (No Connect, Don't Care)
  4937. * <7..6> 11 Port 3 is Output (~RTS, Bit 6 : 0 = Enabled )
  4938. * <5..4> 11 Port 2 is Output (~DTR, Bit 4 : 0 = Enabled )
  4939. * <3..2> 01 Port 1 is Input (Dedicated RxC)
  4940. * <1..0> 01 Port 0 is Input (Dedicated TxC)
  4941. *
  4942. * 1111 0000 1111 0101 = 0xf0f5
  4943. */
  4944. usc_OutReg( info, PCR, 0xf0f5 );
  4945. /*
  4946. * Input/Output Control Register
  4947. *
  4948. * <15..14> 00 CTS is active low input
  4949. * <13..12> 00 DCD is active low input
  4950. * <11..10> 00 TxREQ pin is input (DSR)
  4951. * <9..8> 00 RxREQ pin is input (RI)
  4952. * <7..6> 00 TxD is output (Transmit Data)
  4953. * <5..3> 000 TxC Pin in Input (14.7456MHz Clock)
  4954. * <2..0> 100 RxC is Output (drive with BRG0)
  4955. *
  4956. * 0000 0000 0000 0100 = 0x0004
  4957. */
  4958. usc_OutReg( info, IOCR, 0x0004 );
  4959. } /* end of usc_reset() */
  4960. /* usc_set_async_mode()
  4961. *
  4962. * Program adapter for asynchronous communications.
  4963. *
  4964. * Arguments: info pointer to device instance data
  4965. * Return Value: None
  4966. */
  4967. static void usc_set_async_mode( struct mgsl_struct *info )
  4968. {
  4969. u16 RegValue;
  4970. /* disable interrupts while programming USC */
  4971. usc_DisableMasterIrqBit( info );
  4972. outw( 0, info->io_base ); /* clear Master Bus Enable (DCAR) */
  4973. usc_DmaCmd( info, DmaCmd_ResetAllChannels ); /* disable both DMA channels */
  4974. usc_loopback_frame( info );
  4975. /* Channel mode Register (CMR)
  4976. *
  4977. * <15..14> 00 Tx Sub modes, 00 = 1 Stop Bit
  4978. * <13..12> 00 00 = 16X Clock
  4979. * <11..8> 0000 Transmitter mode = Asynchronous
  4980. * <7..6> 00 reserved?
  4981. * <5..4> 00 Rx Sub modes, 00 = 16X Clock
  4982. * <3..0> 0000 Receiver mode = Asynchronous
  4983. *
  4984. * 0000 0000 0000 0000 = 0x0
  4985. */
  4986. RegValue = 0;
  4987. if ( info->params.stop_bits != 1 )
  4988. RegValue |= BIT14;
  4989. usc_OutReg( info, CMR, RegValue );
  4990. /* Receiver mode Register (RMR)
  4991. *
  4992. * <15..13> 000 encoding = None
  4993. * <12..08> 00000 reserved (Sync Only)
  4994. * <7..6> 00 Even parity
  4995. * <5> 0 parity disabled
  4996. * <4..2> 000 Receive Char Length = 8 bits
  4997. * <1..0> 00 Disable Receiver
  4998. *
  4999. * 0000 0000 0000 0000 = 0x0
  5000. */
  5001. RegValue = 0;
  5002. if ( info->params.data_bits != 8 )
  5003. RegValue |= BIT4+BIT3+BIT2;
  5004. if ( info->params.parity != ASYNC_PARITY_NONE ) {
  5005. RegValue |= BIT5;
  5006. if ( info->params.parity != ASYNC_PARITY_ODD )
  5007. RegValue |= BIT6;
  5008. }
  5009. usc_OutReg( info, RMR, RegValue );
  5010. /* Set IRQ trigger level */
  5011. usc_RCmd( info, RCmd_SelectRicrIntLevel );
  5012. /* Receive Interrupt Control Register (RICR)
  5013. *
  5014. * <15..8> ? RxFIFO IRQ Request Level
  5015. *
  5016. * Note: For async mode the receive FIFO level must be set
  5017. * to 0 to avoid the situation where the FIFO contains fewer bytes
  5018. * than the trigger level and no more data is expected.
  5019. *
  5020. * <7> 0 Exited Hunt IA (Interrupt Arm)
  5021. * <6> 0 Idle Received IA
  5022. * <5> 0 Break/Abort IA
  5023. * <4> 0 Rx Bound IA
  5024. * <3> 0 Queued status reflects oldest byte in FIFO
  5025. * <2> 0 Abort/PE IA
  5026. * <1> 0 Rx Overrun IA
  5027. * <0> 0 Select TC0 value for readback
  5028. *
  5029. * 0000 0000 0100 0000 = 0x0000 + (FIFOLEVEL in MSB)
  5030. */
  5031. usc_OutReg( info, RICR, 0x0000 );
  5032. usc_UnlatchRxstatusBits( info, RXSTATUS_ALL );
  5033. usc_ClearIrqPendingBits( info, RECEIVE_STATUS );
  5034. /* Transmit mode Register (TMR)
  5035. *
  5036. * <15..13> 000 encoding = None
  5037. * <12..08> 00000 reserved (Sync Only)
  5038. * <7..6> 00 Transmit parity Even
  5039. * <5> 0 Transmit parity Disabled
  5040. * <4..2> 000 Tx Char Length = 8 bits
  5041. * <1..0> 00 Disable Transmitter
  5042. *
  5043. * 0000 0000 0000 0000 = 0x0
  5044. */
  5045. RegValue = 0;
  5046. if ( info->params.data_bits != 8 )
  5047. RegValue |= BIT4+BIT3+BIT2;
  5048. if ( info->params.parity != ASYNC_PARITY_NONE ) {
  5049. RegValue |= BIT5;
  5050. if ( info->params.parity != ASYNC_PARITY_ODD )
  5051. RegValue |= BIT6;
  5052. }
  5053. usc_OutReg( info, TMR, RegValue );
  5054. usc_set_txidle( info );
  5055. /* Set IRQ trigger level */
  5056. usc_TCmd( info, TCmd_SelectTicrIntLevel );
  5057. /* Transmit Interrupt Control Register (TICR)
  5058. *
  5059. * <15..8> ? Transmit FIFO IRQ Level
  5060. * <7> 0 Present IA (Interrupt Arm)
  5061. * <6> 1 Idle Sent IA
  5062. * <5> 0 Abort Sent IA
  5063. * <4> 0 EOF/EOM Sent IA
  5064. * <3> 0 CRC Sent IA
  5065. * <2> 0 1 = Wait for SW Trigger to Start Frame
  5066. * <1> 0 Tx Underrun IA
  5067. * <0> 0 TC0 constant on read back
  5068. *
  5069. * 0000 0000 0100 0000 = 0x0040
  5070. */
  5071. usc_OutReg( info, TICR, 0x1f40 );
  5072. usc_UnlatchTxstatusBits( info, TXSTATUS_ALL );
  5073. usc_ClearIrqPendingBits( info, TRANSMIT_STATUS );
  5074. usc_enable_async_clock( info, info->params.data_rate );
  5075. /* Channel Control/status Register (CCSR)
  5076. *
  5077. * <15> X RCC FIFO Overflow status (RO)
  5078. * <14> X RCC FIFO Not Empty status (RO)
  5079. * <13> 0 1 = Clear RCC FIFO (WO)
  5080. * <12> X DPLL in Sync status (RO)
  5081. * <11> X DPLL 2 Missed Clocks status (RO)
  5082. * <10> X DPLL 1 Missed Clock status (RO)
  5083. * <9..8> 00 DPLL Resync on rising and falling edges (RW)
  5084. * <7> X SDLC Loop On status (RO)
  5085. * <6> X SDLC Loop Send status (RO)
  5086. * <5> 1 Bypass counters for TxClk and RxClk (RW)
  5087. * <4..2> 000 Last Char of SDLC frame has 8 bits (RW)
  5088. * <1..0> 00 reserved
  5089. *
  5090. * 0000 0000 0010 0000 = 0x0020
  5091. */
  5092. usc_OutReg( info, CCSR, 0x0020 );
  5093. usc_DisableInterrupts( info, TRANSMIT_STATUS + TRANSMIT_DATA +
  5094. RECEIVE_DATA + RECEIVE_STATUS );
  5095. usc_ClearIrqPendingBits( info, TRANSMIT_STATUS + TRANSMIT_DATA +
  5096. RECEIVE_DATA + RECEIVE_STATUS );
  5097. usc_EnableMasterIrqBit( info );
  5098. if (info->bus_type == MGSL_BUS_TYPE_ISA) {
  5099. /* Enable INTEN (Port 6, Bit12) */
  5100. /* This connects the IRQ request signal to the ISA bus */
  5101. usc_OutReg(info, PCR, (u16)((usc_InReg(info, PCR) | BIT13) & ~BIT12));
  5102. }
  5103. if (info->params.loopback) {
  5104. info->loopback_bits = 0x300;
  5105. outw(0x0300, info->io_base + CCAR);
  5106. }
  5107. } /* end of usc_set_async_mode() */
  5108. /* usc_loopback_frame()
  5109. *
  5110. * Loop back a small (2 byte) dummy SDLC frame.
  5111. * Interrupts and DMA are NOT used. The purpose of this is to
  5112. * clear any 'stale' status info left over from running in async mode.
  5113. *
  5114. * The 16C32 shows the strange behaviour of marking the 1st
  5115. * received SDLC frame with a CRC error even when there is no
  5116. * CRC error. To get around this a small dummy from of 2 bytes
  5117. * is looped back when switching from async to sync mode.
  5118. *
  5119. * Arguments: info pointer to device instance data
  5120. * Return Value: None
  5121. */
  5122. static void usc_loopback_frame( struct mgsl_struct *info )
  5123. {
  5124. int i;
  5125. unsigned long oldmode = info->params.mode;
  5126. info->params.mode = MGSL_MODE_HDLC;
  5127. usc_DisableMasterIrqBit( info );
  5128. usc_set_sdlc_mode( info );
  5129. usc_enable_loopback( info, 1 );
  5130. /* Write 16-bit Time Constant for BRG0 */
  5131. usc_OutReg( info, TC0R, 0 );
  5132. /* Channel Control Register (CCR)
  5133. *
  5134. * <15..14> 00 Don't use 32-bit Tx Control Blocks (TCBs)
  5135. * <13> 0 Trigger Tx on SW Command Disabled
  5136. * <12> 0 Flag Preamble Disabled
  5137. * <11..10> 00 Preamble Length = 8-Bits
  5138. * <9..8> 01 Preamble Pattern = flags
  5139. * <7..6> 10 Don't use 32-bit Rx status Blocks (RSBs)
  5140. * <5> 0 Trigger Rx on SW Command Disabled
  5141. * <4..0> 0 reserved
  5142. *
  5143. * 0000 0001 0000 0000 = 0x0100
  5144. */
  5145. usc_OutReg( info, CCR, 0x0100 );
  5146. /* SETUP RECEIVER */
  5147. usc_RTCmd( info, RTCmd_PurgeRxFifo );
  5148. usc_EnableReceiver(info,ENABLE_UNCONDITIONAL);
  5149. /* SETUP TRANSMITTER */
  5150. /* Program the Transmit Character Length Register (TCLR) */
  5151. /* and clear FIFO (TCC is loaded with TCLR on FIFO clear) */
  5152. usc_OutReg( info, TCLR, 2 );
  5153. usc_RTCmd( info, RTCmd_PurgeTxFifo );
  5154. /* unlatch Tx status bits, and start transmit channel. */
  5155. usc_UnlatchTxstatusBits(info,TXSTATUS_ALL);
  5156. outw(0,info->io_base + DATAREG);
  5157. /* ENABLE TRANSMITTER */
  5158. usc_TCmd( info, TCmd_SendFrame );
  5159. usc_EnableTransmitter(info,ENABLE_UNCONDITIONAL);
  5160. /* WAIT FOR RECEIVE COMPLETE */
  5161. for (i=0 ; i<1000 ; i++)
  5162. if (usc_InReg( info, RCSR ) & (BIT8 + BIT4 + BIT3 + BIT1))
  5163. break;
  5164. /* clear Internal Data loopback mode */
  5165. usc_enable_loopback(info, 0);
  5166. usc_EnableMasterIrqBit(info);
  5167. info->params.mode = oldmode;
  5168. } /* end of usc_loopback_frame() */
  5169. /* usc_set_sync_mode() Programs the USC for SDLC communications.
  5170. *
  5171. * Arguments: info pointer to adapter info structure
  5172. * Return Value: None
  5173. */
  5174. static void usc_set_sync_mode( struct mgsl_struct *info )
  5175. {
  5176. usc_loopback_frame( info );
  5177. usc_set_sdlc_mode( info );
  5178. if (info->bus_type == MGSL_BUS_TYPE_ISA) {
  5179. /* Enable INTEN (Port 6, Bit12) */
  5180. /* This connects the IRQ request signal to the ISA bus */
  5181. usc_OutReg(info, PCR, (u16)((usc_InReg(info, PCR) | BIT13) & ~BIT12));
  5182. }
  5183. usc_enable_aux_clock(info, info->params.clock_speed);
  5184. if (info->params.loopback)
  5185. usc_enable_loopback(info,1);
  5186. } /* end of mgsl_set_sync_mode() */
  5187. /* usc_set_txidle() Set the HDLC idle mode for the transmitter.
  5188. *
  5189. * Arguments: info pointer to device instance data
  5190. * Return Value: None
  5191. */
  5192. static void usc_set_txidle( struct mgsl_struct *info )
  5193. {
  5194. u16 usc_idle_mode = IDLEMODE_FLAGS;
  5195. /* Map API idle mode to USC register bits */
  5196. switch( info->idle_mode ){
  5197. case HDLC_TXIDLE_FLAGS: usc_idle_mode = IDLEMODE_FLAGS; break;
  5198. case HDLC_TXIDLE_ALT_ZEROS_ONES: usc_idle_mode = IDLEMODE_ALT_ONE_ZERO; break;
  5199. case HDLC_TXIDLE_ZEROS: usc_idle_mode = IDLEMODE_ZERO; break;
  5200. case HDLC_TXIDLE_ONES: usc_idle_mode = IDLEMODE_ONE; break;
  5201. case HDLC_TXIDLE_ALT_MARK_SPACE: usc_idle_mode = IDLEMODE_ALT_MARK_SPACE; break;
  5202. case HDLC_TXIDLE_SPACE: usc_idle_mode = IDLEMODE_SPACE; break;
  5203. case HDLC_TXIDLE_MARK: usc_idle_mode = IDLEMODE_MARK; break;
  5204. }
  5205. info->usc_idle_mode = usc_idle_mode;
  5206. //usc_OutReg(info, TCSR, usc_idle_mode);
  5207. info->tcsr_value &= ~IDLEMODE_MASK; /* clear idle mode bits */
  5208. info->tcsr_value += usc_idle_mode;
  5209. usc_OutReg(info, TCSR, info->tcsr_value);
  5210. /*
  5211. * if SyncLink WAN adapter is running in external sync mode, the
  5212. * transmitter has been set to Monosync in order to try to mimic
  5213. * a true raw outbound bit stream. Monosync still sends an open/close
  5214. * sync char at the start/end of a frame. Try to match those sync
  5215. * patterns to the idle mode set here
  5216. */
  5217. if ( info->params.mode == MGSL_MODE_RAW ) {
  5218. unsigned char syncpat = 0;
  5219. switch( info->idle_mode ) {
  5220. case HDLC_TXIDLE_FLAGS:
  5221. syncpat = 0x7e;
  5222. break;
  5223. case HDLC_TXIDLE_ALT_ZEROS_ONES:
  5224. syncpat = 0x55;
  5225. break;
  5226. case HDLC_TXIDLE_ZEROS:
  5227. case HDLC_TXIDLE_SPACE:
  5228. syncpat = 0x00;
  5229. break;
  5230. case HDLC_TXIDLE_ONES:
  5231. case HDLC_TXIDLE_MARK:
  5232. syncpat = 0xff;
  5233. break;
  5234. case HDLC_TXIDLE_ALT_MARK_SPACE:
  5235. syncpat = 0xaa;
  5236. break;
  5237. }
  5238. usc_SetTransmitSyncChars(info,syncpat,syncpat);
  5239. }
  5240. } /* end of usc_set_txidle() */
  5241. /* usc_get_serial_signals()
  5242. *
  5243. * Query the adapter for the state of the V24 status (input) signals.
  5244. *
  5245. * Arguments: info pointer to device instance data
  5246. * Return Value: None
  5247. */
  5248. static void usc_get_serial_signals( struct mgsl_struct *info )
  5249. {
  5250. u16 status;
  5251. /* clear all serial signals except DTR and RTS */
  5252. info->serial_signals &= SerialSignal_DTR + SerialSignal_RTS;
  5253. /* Read the Misc Interrupt status Register (MISR) to get */
  5254. /* the V24 status signals. */
  5255. status = usc_InReg( info, MISR );
  5256. /* set serial signal bits to reflect MISR */
  5257. if ( status & MISCSTATUS_CTS )
  5258. info->serial_signals |= SerialSignal_CTS;
  5259. if ( status & MISCSTATUS_DCD )
  5260. info->serial_signals |= SerialSignal_DCD;
  5261. if ( status & MISCSTATUS_RI )
  5262. info->serial_signals |= SerialSignal_RI;
  5263. if ( status & MISCSTATUS_DSR )
  5264. info->serial_signals |= SerialSignal_DSR;
  5265. } /* end of usc_get_serial_signals() */
  5266. /* usc_set_serial_signals()
  5267. *
  5268. * Set the state of DTR and RTS based on contents of
  5269. * serial_signals member of device extension.
  5270. *
  5271. * Arguments: info pointer to device instance data
  5272. * Return Value: None
  5273. */
  5274. static void usc_set_serial_signals( struct mgsl_struct *info )
  5275. {
  5276. u16 Control;
  5277. unsigned char V24Out = info->serial_signals;
  5278. /* get the current value of the Port Control Register (PCR) */
  5279. Control = usc_InReg( info, PCR );
  5280. if ( V24Out & SerialSignal_RTS )
  5281. Control &= ~(BIT6);
  5282. else
  5283. Control |= BIT6;
  5284. if ( V24Out & SerialSignal_DTR )
  5285. Control &= ~(BIT4);
  5286. else
  5287. Control |= BIT4;
  5288. usc_OutReg( info, PCR, Control );
  5289. } /* end of usc_set_serial_signals() */
  5290. /* usc_enable_async_clock()
  5291. *
  5292. * Enable the async clock at the specified frequency.
  5293. *
  5294. * Arguments: info pointer to device instance data
  5295. * data_rate data rate of clock in bps
  5296. * 0 disables the AUX clock.
  5297. * Return Value: None
  5298. */
  5299. static void usc_enable_async_clock( struct mgsl_struct *info, u32 data_rate )
  5300. {
  5301. if ( data_rate ) {
  5302. /*
  5303. * Clock mode Control Register (CMCR)
  5304. *
  5305. * <15..14> 00 counter 1 Disabled
  5306. * <13..12> 00 counter 0 Disabled
  5307. * <11..10> 11 BRG1 Input is TxC Pin
  5308. * <9..8> 11 BRG0 Input is TxC Pin
  5309. * <7..6> 01 DPLL Input is BRG1 Output
  5310. * <5..3> 100 TxCLK comes from BRG0
  5311. * <2..0> 100 RxCLK comes from BRG0
  5312. *
  5313. * 0000 1111 0110 0100 = 0x0f64
  5314. */
  5315. usc_OutReg( info, CMCR, 0x0f64 );
  5316. /*
  5317. * Write 16-bit Time Constant for BRG0
  5318. * Time Constant = (ClkSpeed / data_rate) - 1
  5319. * ClkSpeed = 921600 (ISA), 691200 (PCI)
  5320. */
  5321. if ( info->bus_type == MGSL_BUS_TYPE_PCI )
  5322. usc_OutReg( info, TC0R, (u16)((691200/data_rate) - 1) );
  5323. else
  5324. usc_OutReg( info, TC0R, (u16)((921600/data_rate) - 1) );
  5325. /*
  5326. * Hardware Configuration Register (HCR)
  5327. * Clear Bit 1, BRG0 mode = Continuous
  5328. * Set Bit 0 to enable BRG0.
  5329. */
  5330. usc_OutReg( info, HCR,
  5331. (u16)((usc_InReg( info, HCR ) & ~BIT1) | BIT0) );
  5332. /* Input/Output Control Reg, <2..0> = 100, Drive RxC pin with BRG0 */
  5333. usc_OutReg( info, IOCR,
  5334. (u16)((usc_InReg(info, IOCR) & 0xfff8) | 0x0004) );
  5335. } else {
  5336. /* data rate == 0 so turn off BRG0 */
  5337. usc_OutReg( info, HCR, (u16)(usc_InReg( info, HCR ) & ~BIT0) );
  5338. }
  5339. } /* end of usc_enable_async_clock() */
  5340. /*
  5341. * Buffer Structures:
  5342. *
  5343. * Normal memory access uses virtual addresses that can make discontiguous
  5344. * physical memory pages appear to be contiguous in the virtual address
  5345. * space (the processors memory mapping handles the conversions).
  5346. *
  5347. * DMA transfers require physically contiguous memory. This is because
  5348. * the DMA system controller and DMA bus masters deal with memory using
  5349. * only physical addresses.
  5350. *
  5351. * This causes a problem under Windows NT when large DMA buffers are
  5352. * needed. Fragmentation of the nonpaged pool prevents allocations of
  5353. * physically contiguous buffers larger than the PAGE_SIZE.
  5354. *
  5355. * However the 16C32 supports Bus Master Scatter/Gather DMA which
  5356. * allows DMA transfers to physically discontiguous buffers. Information
  5357. * about each data transfer buffer is contained in a memory structure
  5358. * called a 'buffer entry'. A list of buffer entries is maintained
  5359. * to track and control the use of the data transfer buffers.
  5360. *
  5361. * To support this strategy we will allocate sufficient PAGE_SIZE
  5362. * contiguous memory buffers to allow for the total required buffer
  5363. * space.
  5364. *
  5365. * The 16C32 accesses the list of buffer entries using Bus Master
  5366. * DMA. Control information is read from the buffer entries by the
  5367. * 16C32 to control data transfers. status information is written to
  5368. * the buffer entries by the 16C32 to indicate the status of completed
  5369. * transfers.
  5370. *
  5371. * The CPU writes control information to the buffer entries to control
  5372. * the 16C32 and reads status information from the buffer entries to
  5373. * determine information about received and transmitted frames.
  5374. *
  5375. * Because the CPU and 16C32 (adapter) both need simultaneous access
  5376. * to the buffer entries, the buffer entry memory is allocated with
  5377. * HalAllocateCommonBuffer(). This restricts the size of the buffer
  5378. * entry list to PAGE_SIZE.
  5379. *
  5380. * The actual data buffers on the other hand will only be accessed
  5381. * by the CPU or the adapter but not by both simultaneously. This allows
  5382. * Scatter/Gather packet based DMA procedures for using physically
  5383. * discontiguous pages.
  5384. */
  5385. /*
  5386. * mgsl_reset_tx_dma_buffers()
  5387. *
  5388. * Set the count for all transmit buffers to 0 to indicate the
  5389. * buffer is available for use and set the current buffer to the
  5390. * first buffer. This effectively makes all buffers free and
  5391. * discards any data in buffers.
  5392. *
  5393. * Arguments: info pointer to device instance data
  5394. * Return Value: None
  5395. */
  5396. static void mgsl_reset_tx_dma_buffers( struct mgsl_struct *info )
  5397. {
  5398. unsigned int i;
  5399. for ( i = 0; i < info->tx_buffer_count; i++ ) {
  5400. *((unsigned long *)&(info->tx_buffer_list[i].count)) = 0;
  5401. }
  5402. info->current_tx_buffer = 0;
  5403. info->start_tx_dma_buffer = 0;
  5404. info->tx_dma_buffers_used = 0;
  5405. info->get_tx_holding_index = 0;
  5406. info->put_tx_holding_index = 0;
  5407. info->tx_holding_count = 0;
  5408. } /* end of mgsl_reset_tx_dma_buffers() */
  5409. /*
  5410. * num_free_tx_dma_buffers()
  5411. *
  5412. * returns the number of free tx dma buffers available
  5413. *
  5414. * Arguments: info pointer to device instance data
  5415. * Return Value: number of free tx dma buffers
  5416. */
  5417. static int num_free_tx_dma_buffers(struct mgsl_struct *info)
  5418. {
  5419. return info->tx_buffer_count - info->tx_dma_buffers_used;
  5420. }
  5421. /*
  5422. * mgsl_reset_rx_dma_buffers()
  5423. *
  5424. * Set the count for all receive buffers to DMABUFFERSIZE
  5425. * and set the current buffer to the first buffer. This effectively
  5426. * makes all buffers free and discards any data in buffers.
  5427. *
  5428. * Arguments: info pointer to device instance data
  5429. * Return Value: None
  5430. */
  5431. static void mgsl_reset_rx_dma_buffers( struct mgsl_struct *info )
  5432. {
  5433. unsigned int i;
  5434. for ( i = 0; i < info->rx_buffer_count; i++ ) {
  5435. *((unsigned long *)&(info->rx_buffer_list[i].count)) = DMABUFFERSIZE;
  5436. // info->rx_buffer_list[i].count = DMABUFFERSIZE;
  5437. // info->rx_buffer_list[i].status = 0;
  5438. }
  5439. info->current_rx_buffer = 0;
  5440. } /* end of mgsl_reset_rx_dma_buffers() */
  5441. /*
  5442. * mgsl_free_rx_frame_buffers()
  5443. *
  5444. * Free the receive buffers used by a received SDLC
  5445. * frame such that the buffers can be reused.
  5446. *
  5447. * Arguments:
  5448. *
  5449. * info pointer to device instance data
  5450. * StartIndex index of 1st receive buffer of frame
  5451. * EndIndex index of last receive buffer of frame
  5452. *
  5453. * Return Value: None
  5454. */
  5455. static void mgsl_free_rx_frame_buffers( struct mgsl_struct *info, unsigned int StartIndex, unsigned int EndIndex )
  5456. {
  5457. bool Done = false;
  5458. DMABUFFERENTRY *pBufEntry;
  5459. unsigned int Index;
  5460. /* Starting with 1st buffer entry of the frame clear the status */
  5461. /* field and set the count field to DMA Buffer Size. */
  5462. Index = StartIndex;
  5463. while( !Done ) {
  5464. pBufEntry = &(info->rx_buffer_list[Index]);
  5465. if ( Index == EndIndex ) {
  5466. /* This is the last buffer of the frame! */
  5467. Done = true;
  5468. }
  5469. /* reset current buffer for reuse */
  5470. // pBufEntry->status = 0;
  5471. // pBufEntry->count = DMABUFFERSIZE;
  5472. *((unsigned long *)&(pBufEntry->count)) = DMABUFFERSIZE;
  5473. /* advance to next buffer entry in linked list */
  5474. Index++;
  5475. if ( Index == info->rx_buffer_count )
  5476. Index = 0;
  5477. }
  5478. /* set current buffer to next buffer after last buffer of frame */
  5479. info->current_rx_buffer = Index;
  5480. } /* end of free_rx_frame_buffers() */
  5481. /* mgsl_get_rx_frame()
  5482. *
  5483. * This function attempts to return a received SDLC frame from the
  5484. * receive DMA buffers. Only frames received without errors are returned.
  5485. *
  5486. * Arguments: info pointer to device extension
  5487. * Return Value: true if frame returned, otherwise false
  5488. */
  5489. static bool mgsl_get_rx_frame(struct mgsl_struct *info)
  5490. {
  5491. unsigned int StartIndex, EndIndex; /* index of 1st and last buffers of Rx frame */
  5492. unsigned short status;
  5493. DMABUFFERENTRY *pBufEntry;
  5494. unsigned int framesize = 0;
  5495. bool ReturnCode = false;
  5496. unsigned long flags;
  5497. struct tty_struct *tty = info->tty;
  5498. bool return_frame = false;
  5499. /*
  5500. * current_rx_buffer points to the 1st buffer of the next available
  5501. * receive frame. To find the last buffer of the frame look for
  5502. * a non-zero status field in the buffer entries. (The status
  5503. * field is set by the 16C32 after completing a receive frame.
  5504. */
  5505. StartIndex = EndIndex = info->current_rx_buffer;
  5506. while( !info->rx_buffer_list[EndIndex].status ) {
  5507. /*
  5508. * If the count field of the buffer entry is non-zero then
  5509. * this buffer has not been used. (The 16C32 clears the count
  5510. * field when it starts using the buffer.) If an unused buffer
  5511. * is encountered then there are no frames available.
  5512. */
  5513. if ( info->rx_buffer_list[EndIndex].count )
  5514. goto Cleanup;
  5515. /* advance to next buffer entry in linked list */
  5516. EndIndex++;
  5517. if ( EndIndex == info->rx_buffer_count )
  5518. EndIndex = 0;
  5519. /* if entire list searched then no frame available */
  5520. if ( EndIndex == StartIndex ) {
  5521. /* If this occurs then something bad happened,
  5522. * all buffers have been 'used' but none mark
  5523. * the end of a frame. Reset buffers and receiver.
  5524. */
  5525. if ( info->rx_enabled ){
  5526. spin_lock_irqsave(&info->irq_spinlock,flags);
  5527. usc_start_receiver(info);
  5528. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  5529. }
  5530. goto Cleanup;
  5531. }
  5532. }
  5533. /* check status of receive frame */
  5534. status = info->rx_buffer_list[EndIndex].status;
  5535. if ( status & (RXSTATUS_SHORT_FRAME + RXSTATUS_OVERRUN +
  5536. RXSTATUS_CRC_ERROR + RXSTATUS_ABORT) ) {
  5537. if ( status & RXSTATUS_SHORT_FRAME )
  5538. info->icount.rxshort++;
  5539. else if ( status & RXSTATUS_ABORT )
  5540. info->icount.rxabort++;
  5541. else if ( status & RXSTATUS_OVERRUN )
  5542. info->icount.rxover++;
  5543. else {
  5544. info->icount.rxcrc++;
  5545. if ( info->params.crc_type & HDLC_CRC_RETURN_EX )
  5546. return_frame = true;
  5547. }
  5548. framesize = 0;
  5549. #if SYNCLINK_GENERIC_HDLC
  5550. {
  5551. struct net_device_stats *stats = hdlc_stats(info->netdev);
  5552. stats->rx_errors++;
  5553. stats->rx_frame_errors++;
  5554. }
  5555. #endif
  5556. } else
  5557. return_frame = true;
  5558. if ( return_frame ) {
  5559. /* receive frame has no errors, get frame size.
  5560. * The frame size is the starting value of the RCC (which was
  5561. * set to 0xffff) minus the ending value of the RCC (decremented
  5562. * once for each receive character) minus 2 for the 16-bit CRC.
  5563. */
  5564. framesize = RCLRVALUE - info->rx_buffer_list[EndIndex].rcc;
  5565. /* adjust frame size for CRC if any */
  5566. if ( info->params.crc_type == HDLC_CRC_16_CCITT )
  5567. framesize -= 2;
  5568. else if ( info->params.crc_type == HDLC_CRC_32_CCITT )
  5569. framesize -= 4;
  5570. }
  5571. if ( debug_level >= DEBUG_LEVEL_BH )
  5572. printk("%s(%d):mgsl_get_rx_frame(%s) status=%04X size=%d\n",
  5573. __FILE__,__LINE__,info->device_name,status,framesize);
  5574. if ( debug_level >= DEBUG_LEVEL_DATA )
  5575. mgsl_trace_block(info,info->rx_buffer_list[StartIndex].virt_addr,
  5576. min_t(int, framesize, DMABUFFERSIZE),0);
  5577. if (framesize) {
  5578. if ( ( (info->params.crc_type & HDLC_CRC_RETURN_EX) &&
  5579. ((framesize+1) > info->max_frame_size) ) ||
  5580. (framesize > info->max_frame_size) )
  5581. info->icount.rxlong++;
  5582. else {
  5583. /* copy dma buffer(s) to contiguous intermediate buffer */
  5584. int copy_count = framesize;
  5585. int index = StartIndex;
  5586. unsigned char *ptmp = info->intermediate_rxbuffer;
  5587. if ( !(status & RXSTATUS_CRC_ERROR))
  5588. info->icount.rxok++;
  5589. while(copy_count) {
  5590. int partial_count;
  5591. if ( copy_count > DMABUFFERSIZE )
  5592. partial_count = DMABUFFERSIZE;
  5593. else
  5594. partial_count = copy_count;
  5595. pBufEntry = &(info->rx_buffer_list[index]);
  5596. memcpy( ptmp, pBufEntry->virt_addr, partial_count );
  5597. ptmp += partial_count;
  5598. copy_count -= partial_count;
  5599. if ( ++index == info->rx_buffer_count )
  5600. index = 0;
  5601. }
  5602. if ( info->params.crc_type & HDLC_CRC_RETURN_EX ) {
  5603. ++framesize;
  5604. *ptmp = (status & RXSTATUS_CRC_ERROR ?
  5605. RX_CRC_ERROR :
  5606. RX_OK);
  5607. if ( debug_level >= DEBUG_LEVEL_DATA )
  5608. printk("%s(%d):mgsl_get_rx_frame(%s) rx frame status=%d\n",
  5609. __FILE__,__LINE__,info->device_name,
  5610. *ptmp);
  5611. }
  5612. #if SYNCLINK_GENERIC_HDLC
  5613. if (info->netcount)
  5614. hdlcdev_rx(info,info->intermediate_rxbuffer,framesize);
  5615. else
  5616. #endif
  5617. ldisc_receive_buf(tty, info->intermediate_rxbuffer, info->flag_buf, framesize);
  5618. }
  5619. }
  5620. /* Free the buffers used by this frame. */
  5621. mgsl_free_rx_frame_buffers( info, StartIndex, EndIndex );
  5622. ReturnCode = true;
  5623. Cleanup:
  5624. if ( info->rx_enabled && info->rx_overflow ) {
  5625. /* The receiver needs to restarted because of
  5626. * a receive overflow (buffer or FIFO). If the
  5627. * receive buffers are now empty, then restart receiver.
  5628. */
  5629. if ( !info->rx_buffer_list[EndIndex].status &&
  5630. info->rx_buffer_list[EndIndex].count ) {
  5631. spin_lock_irqsave(&info->irq_spinlock,flags);
  5632. usc_start_receiver(info);
  5633. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  5634. }
  5635. }
  5636. return ReturnCode;
  5637. } /* end of mgsl_get_rx_frame() */
  5638. /* mgsl_get_raw_rx_frame()
  5639. *
  5640. * This function attempts to return a received frame from the
  5641. * receive DMA buffers when running in external loop mode. In this mode,
  5642. * we will return at most one DMABUFFERSIZE frame to the application.
  5643. * The USC receiver is triggering off of DCD going active to start a new
  5644. * frame, and DCD going inactive to terminate the frame (similar to
  5645. * processing a closing flag character).
  5646. *
  5647. * In this routine, we will return DMABUFFERSIZE "chunks" at a time.
  5648. * If DCD goes inactive, the last Rx DMA Buffer will have a non-zero
  5649. * status field and the RCC field will indicate the length of the
  5650. * entire received frame. We take this RCC field and get the modulus
  5651. * of RCC and DMABUFFERSIZE to determine if number of bytes in the
  5652. * last Rx DMA buffer and return that last portion of the frame.
  5653. *
  5654. * Arguments: info pointer to device extension
  5655. * Return Value: true if frame returned, otherwise false
  5656. */
  5657. static bool mgsl_get_raw_rx_frame(struct mgsl_struct *info)
  5658. {
  5659. unsigned int CurrentIndex, NextIndex;
  5660. unsigned short status;
  5661. DMABUFFERENTRY *pBufEntry;
  5662. unsigned int framesize = 0;
  5663. bool ReturnCode = false;
  5664. unsigned long flags;
  5665. struct tty_struct *tty = info->tty;
  5666. /*
  5667. * current_rx_buffer points to the 1st buffer of the next available
  5668. * receive frame. The status field is set by the 16C32 after
  5669. * completing a receive frame. If the status field of this buffer
  5670. * is zero, either the USC is still filling this buffer or this
  5671. * is one of a series of buffers making up a received frame.
  5672. *
  5673. * If the count field of this buffer is zero, the USC is either
  5674. * using this buffer or has used this buffer. Look at the count
  5675. * field of the next buffer. If that next buffer's count is
  5676. * non-zero, the USC is still actively using the current buffer.
  5677. * Otherwise, if the next buffer's count field is zero, the
  5678. * current buffer is complete and the USC is using the next
  5679. * buffer.
  5680. */
  5681. CurrentIndex = NextIndex = info->current_rx_buffer;
  5682. ++NextIndex;
  5683. if ( NextIndex == info->rx_buffer_count )
  5684. NextIndex = 0;
  5685. if ( info->rx_buffer_list[CurrentIndex].status != 0 ||
  5686. (info->rx_buffer_list[CurrentIndex].count == 0 &&
  5687. info->rx_buffer_list[NextIndex].count == 0)) {
  5688. /*
  5689. * Either the status field of this dma buffer is non-zero
  5690. * (indicating the last buffer of a receive frame) or the next
  5691. * buffer is marked as in use -- implying this buffer is complete
  5692. * and an intermediate buffer for this received frame.
  5693. */
  5694. status = info->rx_buffer_list[CurrentIndex].status;
  5695. if ( status & (RXSTATUS_SHORT_FRAME + RXSTATUS_OVERRUN +
  5696. RXSTATUS_CRC_ERROR + RXSTATUS_ABORT) ) {
  5697. if ( status & RXSTATUS_SHORT_FRAME )
  5698. info->icount.rxshort++;
  5699. else if ( status & RXSTATUS_ABORT )
  5700. info->icount.rxabort++;
  5701. else if ( status & RXSTATUS_OVERRUN )
  5702. info->icount.rxover++;
  5703. else
  5704. info->icount.rxcrc++;
  5705. framesize = 0;
  5706. } else {
  5707. /*
  5708. * A receive frame is available, get frame size and status.
  5709. *
  5710. * The frame size is the starting value of the RCC (which was
  5711. * set to 0xffff) minus the ending value of the RCC (decremented
  5712. * once for each receive character) minus 2 or 4 for the 16-bit
  5713. * or 32-bit CRC.
  5714. *
  5715. * If the status field is zero, this is an intermediate buffer.
  5716. * It's size is 4K.
  5717. *
  5718. * If the DMA Buffer Entry's Status field is non-zero, the
  5719. * receive operation completed normally (ie: DCD dropped). The
  5720. * RCC field is valid and holds the received frame size.
  5721. * It is possible that the RCC field will be zero on a DMA buffer
  5722. * entry with a non-zero status. This can occur if the total
  5723. * frame size (number of bytes between the time DCD goes active
  5724. * to the time DCD goes inactive) exceeds 65535 bytes. In this
  5725. * case the 16C32 has underrun on the RCC count and appears to
  5726. * stop updating this counter to let us know the actual received
  5727. * frame size. If this happens (non-zero status and zero RCC),
  5728. * simply return the entire RxDMA Buffer
  5729. */
  5730. if ( status ) {
  5731. /*
  5732. * In the event that the final RxDMA Buffer is
  5733. * terminated with a non-zero status and the RCC
  5734. * field is zero, we interpret this as the RCC
  5735. * having underflowed (received frame > 65535 bytes).
  5736. *
  5737. * Signal the event to the user by passing back
  5738. * a status of RxStatus_CrcError returning the full
  5739. * buffer and let the app figure out what data is
  5740. * actually valid
  5741. */
  5742. if ( info->rx_buffer_list[CurrentIndex].rcc )
  5743. framesize = RCLRVALUE - info->rx_buffer_list[CurrentIndex].rcc;
  5744. else
  5745. framesize = DMABUFFERSIZE;
  5746. }
  5747. else
  5748. framesize = DMABUFFERSIZE;
  5749. }
  5750. if ( framesize > DMABUFFERSIZE ) {
  5751. /*
  5752. * if running in raw sync mode, ISR handler for
  5753. * End Of Buffer events terminates all buffers at 4K.
  5754. * If this frame size is said to be >4K, get the
  5755. * actual number of bytes of the frame in this buffer.
  5756. */
  5757. framesize = framesize % DMABUFFERSIZE;
  5758. }
  5759. if ( debug_level >= DEBUG_LEVEL_BH )
  5760. printk("%s(%d):mgsl_get_raw_rx_frame(%s) status=%04X size=%d\n",
  5761. __FILE__,__LINE__,info->device_name,status,framesize);
  5762. if ( debug_level >= DEBUG_LEVEL_DATA )
  5763. mgsl_trace_block(info,info->rx_buffer_list[CurrentIndex].virt_addr,
  5764. min_t(int, framesize, DMABUFFERSIZE),0);
  5765. if (framesize) {
  5766. /* copy dma buffer(s) to contiguous intermediate buffer */
  5767. /* NOTE: we never copy more than DMABUFFERSIZE bytes */
  5768. pBufEntry = &(info->rx_buffer_list[CurrentIndex]);
  5769. memcpy( info->intermediate_rxbuffer, pBufEntry->virt_addr, framesize);
  5770. info->icount.rxok++;
  5771. ldisc_receive_buf(tty, info->intermediate_rxbuffer, info->flag_buf, framesize);
  5772. }
  5773. /* Free the buffers used by this frame. */
  5774. mgsl_free_rx_frame_buffers( info, CurrentIndex, CurrentIndex );
  5775. ReturnCode = true;
  5776. }
  5777. if ( info->rx_enabled && info->rx_overflow ) {
  5778. /* The receiver needs to restarted because of
  5779. * a receive overflow (buffer or FIFO). If the
  5780. * receive buffers are now empty, then restart receiver.
  5781. */
  5782. if ( !info->rx_buffer_list[CurrentIndex].status &&
  5783. info->rx_buffer_list[CurrentIndex].count ) {
  5784. spin_lock_irqsave(&info->irq_spinlock,flags);
  5785. usc_start_receiver(info);
  5786. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  5787. }
  5788. }
  5789. return ReturnCode;
  5790. } /* end of mgsl_get_raw_rx_frame() */
  5791. /* mgsl_load_tx_dma_buffer()
  5792. *
  5793. * Load the transmit DMA buffer with the specified data.
  5794. *
  5795. * Arguments:
  5796. *
  5797. * info pointer to device extension
  5798. * Buffer pointer to buffer containing frame to load
  5799. * BufferSize size in bytes of frame in Buffer
  5800. *
  5801. * Return Value: None
  5802. */
  5803. static void mgsl_load_tx_dma_buffer(struct mgsl_struct *info,
  5804. const char *Buffer, unsigned int BufferSize)
  5805. {
  5806. unsigned short Copycount;
  5807. unsigned int i = 0;
  5808. DMABUFFERENTRY *pBufEntry;
  5809. if ( debug_level >= DEBUG_LEVEL_DATA )
  5810. mgsl_trace_block(info,Buffer, min_t(int, BufferSize, DMABUFFERSIZE), 1);
  5811. if (info->params.flags & HDLC_FLAG_HDLC_LOOPMODE) {
  5812. /* set CMR:13 to start transmit when
  5813. * next GoAhead (abort) is received
  5814. */
  5815. info->cmr_value |= BIT13;
  5816. }
  5817. /* begin loading the frame in the next available tx dma
  5818. * buffer, remember it's starting location for setting
  5819. * up tx dma operation
  5820. */
  5821. i = info->current_tx_buffer;
  5822. info->start_tx_dma_buffer = i;
  5823. /* Setup the status and RCC (Frame Size) fields of the 1st */
  5824. /* buffer entry in the transmit DMA buffer list. */
  5825. info->tx_buffer_list[i].status = info->cmr_value & 0xf000;
  5826. info->tx_buffer_list[i].rcc = BufferSize;
  5827. info->tx_buffer_list[i].count = BufferSize;
  5828. /* Copy frame data from 1st source buffer to the DMA buffers. */
  5829. /* The frame data may span multiple DMA buffers. */
  5830. while( BufferSize ){
  5831. /* Get a pointer to next DMA buffer entry. */
  5832. pBufEntry = &info->tx_buffer_list[i++];
  5833. if ( i == info->tx_buffer_count )
  5834. i=0;
  5835. /* Calculate the number of bytes that can be copied from */
  5836. /* the source buffer to this DMA buffer. */
  5837. if ( BufferSize > DMABUFFERSIZE )
  5838. Copycount = DMABUFFERSIZE;
  5839. else
  5840. Copycount = BufferSize;
  5841. /* Actually copy data from source buffer to DMA buffer. */
  5842. /* Also set the data count for this individual DMA buffer. */
  5843. if ( info->bus_type == MGSL_BUS_TYPE_PCI )
  5844. mgsl_load_pci_memory(pBufEntry->virt_addr, Buffer,Copycount);
  5845. else
  5846. memcpy(pBufEntry->virt_addr, Buffer, Copycount);
  5847. pBufEntry->count = Copycount;
  5848. /* Advance source pointer and reduce remaining data count. */
  5849. Buffer += Copycount;
  5850. BufferSize -= Copycount;
  5851. ++info->tx_dma_buffers_used;
  5852. }
  5853. /* remember next available tx dma buffer */
  5854. info->current_tx_buffer = i;
  5855. } /* end of mgsl_load_tx_dma_buffer() */
  5856. /*
  5857. * mgsl_register_test()
  5858. *
  5859. * Performs a register test of the 16C32.
  5860. *
  5861. * Arguments: info pointer to device instance data
  5862. * Return Value: true if test passed, otherwise false
  5863. */
  5864. static bool mgsl_register_test( struct mgsl_struct *info )
  5865. {
  5866. static unsigned short BitPatterns[] =
  5867. { 0x0000, 0xffff, 0xaaaa, 0x5555, 0x1234, 0x6969, 0x9696, 0x0f0f };
  5868. static unsigned int Patterncount = ARRAY_SIZE(BitPatterns);
  5869. unsigned int i;
  5870. bool rc = true;
  5871. unsigned long flags;
  5872. spin_lock_irqsave(&info->irq_spinlock,flags);
  5873. usc_reset(info);
  5874. /* Verify the reset state of some registers. */
  5875. if ( (usc_InReg( info, SICR ) != 0) ||
  5876. (usc_InReg( info, IVR ) != 0) ||
  5877. (usc_InDmaReg( info, DIVR ) != 0) ){
  5878. rc = false;
  5879. }
  5880. if ( rc ){
  5881. /* Write bit patterns to various registers but do it out of */
  5882. /* sync, then read back and verify values. */
  5883. for ( i = 0 ; i < Patterncount ; i++ ) {
  5884. usc_OutReg( info, TC0R, BitPatterns[i] );
  5885. usc_OutReg( info, TC1R, BitPatterns[(i+1)%Patterncount] );
  5886. usc_OutReg( info, TCLR, BitPatterns[(i+2)%Patterncount] );
  5887. usc_OutReg( info, RCLR, BitPatterns[(i+3)%Patterncount] );
  5888. usc_OutReg( info, RSR, BitPatterns[(i+4)%Patterncount] );
  5889. usc_OutDmaReg( info, TBCR, BitPatterns[(i+5)%Patterncount] );
  5890. if ( (usc_InReg( info, TC0R ) != BitPatterns[i]) ||
  5891. (usc_InReg( info, TC1R ) != BitPatterns[(i+1)%Patterncount]) ||
  5892. (usc_InReg( info, TCLR ) != BitPatterns[(i+2)%Patterncount]) ||
  5893. (usc_InReg( info, RCLR ) != BitPatterns[(i+3)%Patterncount]) ||
  5894. (usc_InReg( info, RSR ) != BitPatterns[(i+4)%Patterncount]) ||
  5895. (usc_InDmaReg( info, TBCR ) != BitPatterns[(i+5)%Patterncount]) ){
  5896. rc = false;
  5897. break;
  5898. }
  5899. }
  5900. }
  5901. usc_reset(info);
  5902. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  5903. return rc;
  5904. } /* end of mgsl_register_test() */
  5905. /* mgsl_irq_test() Perform interrupt test of the 16C32.
  5906. *
  5907. * Arguments: info pointer to device instance data
  5908. * Return Value: true if test passed, otherwise false
  5909. */
  5910. static bool mgsl_irq_test( struct mgsl_struct *info )
  5911. {
  5912. unsigned long EndTime;
  5913. unsigned long flags;
  5914. spin_lock_irqsave(&info->irq_spinlock,flags);
  5915. usc_reset(info);
  5916. /*
  5917. * Setup 16C32 to interrupt on TxC pin (14MHz clock) transition.
  5918. * The ISR sets irq_occurred to true.
  5919. */
  5920. info->irq_occurred = false;
  5921. /* Enable INTEN gate for ISA adapter (Port 6, Bit12) */
  5922. /* Enable INTEN (Port 6, Bit12) */
  5923. /* This connects the IRQ request signal to the ISA bus */
  5924. /* on the ISA adapter. This has no effect for the PCI adapter */
  5925. usc_OutReg( info, PCR, (unsigned short)((usc_InReg(info, PCR) | BIT13) & ~BIT12) );
  5926. usc_EnableMasterIrqBit(info);
  5927. usc_EnableInterrupts(info, IO_PIN);
  5928. usc_ClearIrqPendingBits(info, IO_PIN);
  5929. usc_UnlatchIostatusBits(info, MISCSTATUS_TXC_LATCHED);
  5930. usc_EnableStatusIrqs(info, SICR_TXC_ACTIVE + SICR_TXC_INACTIVE);
  5931. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  5932. EndTime=100;
  5933. while( EndTime-- && !info->irq_occurred ) {
  5934. msleep_interruptible(10);
  5935. }
  5936. spin_lock_irqsave(&info->irq_spinlock,flags);
  5937. usc_reset(info);
  5938. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  5939. return info->irq_occurred;
  5940. } /* end of mgsl_irq_test() */
  5941. /* mgsl_dma_test()
  5942. *
  5943. * Perform a DMA test of the 16C32. A small frame is
  5944. * transmitted via DMA from a transmit buffer to a receive buffer
  5945. * using single buffer DMA mode.
  5946. *
  5947. * Arguments: info pointer to device instance data
  5948. * Return Value: true if test passed, otherwise false
  5949. */
  5950. static bool mgsl_dma_test( struct mgsl_struct *info )
  5951. {
  5952. unsigned short FifoLevel;
  5953. unsigned long phys_addr;
  5954. unsigned int FrameSize;
  5955. unsigned int i;
  5956. char *TmpPtr;
  5957. bool rc = true;
  5958. unsigned short status=0;
  5959. unsigned long EndTime;
  5960. unsigned long flags;
  5961. MGSL_PARAMS tmp_params;
  5962. /* save current port options */
  5963. memcpy(&tmp_params,&info->params,sizeof(MGSL_PARAMS));
  5964. /* load default port options */
  5965. memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
  5966. #define TESTFRAMESIZE 40
  5967. spin_lock_irqsave(&info->irq_spinlock,flags);
  5968. /* setup 16C32 for SDLC DMA transfer mode */
  5969. usc_reset(info);
  5970. usc_set_sdlc_mode(info);
  5971. usc_enable_loopback(info,1);
  5972. /* Reprogram the RDMR so that the 16C32 does NOT clear the count
  5973. * field of the buffer entry after fetching buffer address. This
  5974. * way we can detect a DMA failure for a DMA read (which should be
  5975. * non-destructive to system memory) before we try and write to
  5976. * memory (where a failure could corrupt system memory).
  5977. */
  5978. /* Receive DMA mode Register (RDMR)
  5979. *
  5980. * <15..14> 11 DMA mode = Linked List Buffer mode
  5981. * <13> 1 RSBinA/L = store Rx status Block in List entry
  5982. * <12> 0 1 = Clear count of List Entry after fetching
  5983. * <11..10> 00 Address mode = Increment
  5984. * <9> 1 Terminate Buffer on RxBound
  5985. * <8> 0 Bus Width = 16bits
  5986. * <7..0> ? status Bits (write as 0s)
  5987. *
  5988. * 1110 0010 0000 0000 = 0xe200
  5989. */
  5990. usc_OutDmaReg( info, RDMR, 0xe200 );
  5991. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  5992. /* SETUP TRANSMIT AND RECEIVE DMA BUFFERS */
  5993. FrameSize = TESTFRAMESIZE;
  5994. /* setup 1st transmit buffer entry: */
  5995. /* with frame size and transmit control word */
  5996. info->tx_buffer_list[0].count = FrameSize;
  5997. info->tx_buffer_list[0].rcc = FrameSize;
  5998. info->tx_buffer_list[0].status = 0x4000;
  5999. /* build a transmit frame in 1st transmit DMA buffer */
  6000. TmpPtr = info->tx_buffer_list[0].virt_addr;
  6001. for (i = 0; i < FrameSize; i++ )
  6002. *TmpPtr++ = i;
  6003. /* setup 1st receive buffer entry: */
  6004. /* clear status, set max receive buffer size */
  6005. info->rx_buffer_list[0].status = 0;
  6006. info->rx_buffer_list[0].count = FrameSize + 4;
  6007. /* zero out the 1st receive buffer */
  6008. memset( info->rx_buffer_list[0].virt_addr, 0, FrameSize + 4 );
  6009. /* Set count field of next buffer entries to prevent */
  6010. /* 16C32 from using buffers after the 1st one. */
  6011. info->tx_buffer_list[1].count = 0;
  6012. info->rx_buffer_list[1].count = 0;
  6013. /***************************/
  6014. /* Program 16C32 receiver. */
  6015. /***************************/
  6016. spin_lock_irqsave(&info->irq_spinlock,flags);
  6017. /* setup DMA transfers */
  6018. usc_RTCmd( info, RTCmd_PurgeRxFifo );
  6019. /* program 16C32 receiver with physical address of 1st DMA buffer entry */
  6020. phys_addr = info->rx_buffer_list[0].phys_entry;
  6021. usc_OutDmaReg( info, NRARL, (unsigned short)phys_addr );
  6022. usc_OutDmaReg( info, NRARU, (unsigned short)(phys_addr >> 16) );
  6023. /* Clear the Rx DMA status bits (read RDMR) and start channel */
  6024. usc_InDmaReg( info, RDMR );
  6025. usc_DmaCmd( info, DmaCmd_InitRxChannel );
  6026. /* Enable Receiver (RMR <1..0> = 10) */
  6027. usc_OutReg( info, RMR, (unsigned short)((usc_InReg(info, RMR) & 0xfffc) | 0x0002) );
  6028. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  6029. /*************************************************************/
  6030. /* WAIT FOR RECEIVER TO DMA ALL PARAMETERS FROM BUFFER ENTRY */
  6031. /*************************************************************/
  6032. /* Wait 100ms for interrupt. */
  6033. EndTime = jiffies + msecs_to_jiffies(100);
  6034. for(;;) {
  6035. if (time_after(jiffies, EndTime)) {
  6036. rc = false;
  6037. break;
  6038. }
  6039. spin_lock_irqsave(&info->irq_spinlock,flags);
  6040. status = usc_InDmaReg( info, RDMR );
  6041. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  6042. if ( !(status & BIT4) && (status & BIT5) ) {
  6043. /* INITG (BIT 4) is inactive (no entry read in progress) AND */
  6044. /* BUSY (BIT 5) is active (channel still active). */
  6045. /* This means the buffer entry read has completed. */
  6046. break;
  6047. }
  6048. }
  6049. /******************************/
  6050. /* Program 16C32 transmitter. */
  6051. /******************************/
  6052. spin_lock_irqsave(&info->irq_spinlock,flags);
  6053. /* Program the Transmit Character Length Register (TCLR) */
  6054. /* and clear FIFO (TCC is loaded with TCLR on FIFO clear) */
  6055. usc_OutReg( info, TCLR, (unsigned short)info->tx_buffer_list[0].count );
  6056. usc_RTCmd( info, RTCmd_PurgeTxFifo );
  6057. /* Program the address of the 1st DMA Buffer Entry in linked list */
  6058. phys_addr = info->tx_buffer_list[0].phys_entry;
  6059. usc_OutDmaReg( info, NTARL, (unsigned short)phys_addr );
  6060. usc_OutDmaReg( info, NTARU, (unsigned short)(phys_addr >> 16) );
  6061. /* unlatch Tx status bits, and start transmit channel. */
  6062. usc_OutReg( info, TCSR, (unsigned short)(( usc_InReg(info, TCSR) & 0x0f00) | 0xfa) );
  6063. usc_DmaCmd( info, DmaCmd_InitTxChannel );
  6064. /* wait for DMA controller to fill transmit FIFO */
  6065. usc_TCmd( info, TCmd_SelectTicrTxFifostatus );
  6066. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  6067. /**********************************/
  6068. /* WAIT FOR TRANSMIT FIFO TO FILL */
  6069. /**********************************/
  6070. /* Wait 100ms */
  6071. EndTime = jiffies + msecs_to_jiffies(100);
  6072. for(;;) {
  6073. if (time_after(jiffies, EndTime)) {
  6074. rc = false;
  6075. break;
  6076. }
  6077. spin_lock_irqsave(&info->irq_spinlock,flags);
  6078. FifoLevel = usc_InReg(info, TICR) >> 8;
  6079. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  6080. if ( FifoLevel < 16 )
  6081. break;
  6082. else
  6083. if ( FrameSize < 32 ) {
  6084. /* This frame is smaller than the entire transmit FIFO */
  6085. /* so wait for the entire frame to be loaded. */
  6086. if ( FifoLevel <= (32 - FrameSize) )
  6087. break;
  6088. }
  6089. }
  6090. if ( rc )
  6091. {
  6092. /* Enable 16C32 transmitter. */
  6093. spin_lock_irqsave(&info->irq_spinlock,flags);
  6094. /* Transmit mode Register (TMR), <1..0> = 10, Enable Transmitter */
  6095. usc_TCmd( info, TCmd_SendFrame );
  6096. usc_OutReg( info, TMR, (unsigned short)((usc_InReg(info, TMR) & 0xfffc) | 0x0002) );
  6097. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  6098. /******************************/
  6099. /* WAIT FOR TRANSMIT COMPLETE */
  6100. /******************************/
  6101. /* Wait 100ms */
  6102. EndTime = jiffies + msecs_to_jiffies(100);
  6103. /* While timer not expired wait for transmit complete */
  6104. spin_lock_irqsave(&info->irq_spinlock,flags);
  6105. status = usc_InReg( info, TCSR );
  6106. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  6107. while ( !(status & (BIT6+BIT5+BIT4+BIT2+BIT1)) ) {
  6108. if (time_after(jiffies, EndTime)) {
  6109. rc = false;
  6110. break;
  6111. }
  6112. spin_lock_irqsave(&info->irq_spinlock,flags);
  6113. status = usc_InReg( info, TCSR );
  6114. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  6115. }
  6116. }
  6117. if ( rc ){
  6118. /* CHECK FOR TRANSMIT ERRORS */
  6119. if ( status & (BIT5 + BIT1) )
  6120. rc = false;
  6121. }
  6122. if ( rc ) {
  6123. /* WAIT FOR RECEIVE COMPLETE */
  6124. /* Wait 100ms */
  6125. EndTime = jiffies + msecs_to_jiffies(100);
  6126. /* Wait for 16C32 to write receive status to buffer entry. */
  6127. status=info->rx_buffer_list[0].status;
  6128. while ( status == 0 ) {
  6129. if (time_after(jiffies, EndTime)) {
  6130. rc = false;
  6131. break;
  6132. }
  6133. status=info->rx_buffer_list[0].status;
  6134. }
  6135. }
  6136. if ( rc ) {
  6137. /* CHECK FOR RECEIVE ERRORS */
  6138. status = info->rx_buffer_list[0].status;
  6139. if ( status & (BIT8 + BIT3 + BIT1) ) {
  6140. /* receive error has occurred */
  6141. rc = false;
  6142. } else {
  6143. if ( memcmp( info->tx_buffer_list[0].virt_addr ,
  6144. info->rx_buffer_list[0].virt_addr, FrameSize ) ){
  6145. rc = false;
  6146. }
  6147. }
  6148. }
  6149. spin_lock_irqsave(&info->irq_spinlock,flags);
  6150. usc_reset( info );
  6151. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  6152. /* restore current port options */
  6153. memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
  6154. return rc;
  6155. } /* end of mgsl_dma_test() */
  6156. /* mgsl_adapter_test()
  6157. *
  6158. * Perform the register, IRQ, and DMA tests for the 16C32.
  6159. *
  6160. * Arguments: info pointer to device instance data
  6161. * Return Value: 0 if success, otherwise -ENODEV
  6162. */
  6163. static int mgsl_adapter_test( struct mgsl_struct *info )
  6164. {
  6165. if ( debug_level >= DEBUG_LEVEL_INFO )
  6166. printk( "%s(%d):Testing device %s\n",
  6167. __FILE__,__LINE__,info->device_name );
  6168. if ( !mgsl_register_test( info ) ) {
  6169. info->init_error = DiagStatus_AddressFailure;
  6170. printk( "%s(%d):Register test failure for device %s Addr=%04X\n",
  6171. __FILE__,__LINE__,info->device_name, (unsigned short)(info->io_base) );
  6172. return -ENODEV;
  6173. }
  6174. if ( !mgsl_irq_test( info ) ) {
  6175. info->init_error = DiagStatus_IrqFailure;
  6176. printk( "%s(%d):Interrupt test failure for device %s IRQ=%d\n",
  6177. __FILE__,__LINE__,info->device_name, (unsigned short)(info->irq_level) );
  6178. return -ENODEV;
  6179. }
  6180. if ( !mgsl_dma_test( info ) ) {
  6181. info->init_error = DiagStatus_DmaFailure;
  6182. printk( "%s(%d):DMA test failure for device %s DMA=%d\n",
  6183. __FILE__,__LINE__,info->device_name, (unsigned short)(info->dma_level) );
  6184. return -ENODEV;
  6185. }
  6186. if ( debug_level >= DEBUG_LEVEL_INFO )
  6187. printk( "%s(%d):device %s passed diagnostics\n",
  6188. __FILE__,__LINE__,info->device_name );
  6189. return 0;
  6190. } /* end of mgsl_adapter_test() */
  6191. /* mgsl_memory_test()
  6192. *
  6193. * Test the shared memory on a PCI adapter.
  6194. *
  6195. * Arguments: info pointer to device instance data
  6196. * Return Value: true if test passed, otherwise false
  6197. */
  6198. static bool mgsl_memory_test( struct mgsl_struct *info )
  6199. {
  6200. static unsigned long BitPatterns[] =
  6201. { 0x0, 0x55555555, 0xaaaaaaaa, 0x66666666, 0x99999999, 0xffffffff, 0x12345678 };
  6202. unsigned long Patterncount = ARRAY_SIZE(BitPatterns);
  6203. unsigned long i;
  6204. unsigned long TestLimit = SHARED_MEM_ADDRESS_SIZE/sizeof(unsigned long);
  6205. unsigned long * TestAddr;
  6206. if ( info->bus_type != MGSL_BUS_TYPE_PCI )
  6207. return true;
  6208. TestAddr = (unsigned long *)info->memory_base;
  6209. /* Test data lines with test pattern at one location. */
  6210. for ( i = 0 ; i < Patterncount ; i++ ) {
  6211. *TestAddr = BitPatterns[i];
  6212. if ( *TestAddr != BitPatterns[i] )
  6213. return false;
  6214. }
  6215. /* Test address lines with incrementing pattern over */
  6216. /* entire address range. */
  6217. for ( i = 0 ; i < TestLimit ; i++ ) {
  6218. *TestAddr = i * 4;
  6219. TestAddr++;
  6220. }
  6221. TestAddr = (unsigned long *)info->memory_base;
  6222. for ( i = 0 ; i < TestLimit ; i++ ) {
  6223. if ( *TestAddr != i * 4 )
  6224. return false;
  6225. TestAddr++;
  6226. }
  6227. memset( info->memory_base, 0, SHARED_MEM_ADDRESS_SIZE );
  6228. return true;
  6229. } /* End Of mgsl_memory_test() */
  6230. /* mgsl_load_pci_memory()
  6231. *
  6232. * Load a large block of data into the PCI shared memory.
  6233. * Use this instead of memcpy() or memmove() to move data
  6234. * into the PCI shared memory.
  6235. *
  6236. * Notes:
  6237. *
  6238. * This function prevents the PCI9050 interface chip from hogging
  6239. * the adapter local bus, which can starve the 16C32 by preventing
  6240. * 16C32 bus master cycles.
  6241. *
  6242. * The PCI9050 documentation says that the 9050 will always release
  6243. * control of the local bus after completing the current read
  6244. * or write operation.
  6245. *
  6246. * It appears that as long as the PCI9050 write FIFO is full, the
  6247. * PCI9050 treats all of the writes as a single burst transaction
  6248. * and will not release the bus. This causes DMA latency problems
  6249. * at high speeds when copying large data blocks to the shared
  6250. * memory.
  6251. *
  6252. * This function in effect, breaks the a large shared memory write
  6253. * into multiple transations by interleaving a shared memory read
  6254. * which will flush the write FIFO and 'complete' the write
  6255. * transation. This allows any pending DMA request to gain control
  6256. * of the local bus in a timely fasion.
  6257. *
  6258. * Arguments:
  6259. *
  6260. * TargetPtr pointer to target address in PCI shared memory
  6261. * SourcePtr pointer to source buffer for data
  6262. * count count in bytes of data to copy
  6263. *
  6264. * Return Value: None
  6265. */
  6266. static void mgsl_load_pci_memory( char* TargetPtr, const char* SourcePtr,
  6267. unsigned short count )
  6268. {
  6269. /* 16 32-bit writes @ 60ns each = 960ns max latency on local bus */
  6270. #define PCI_LOAD_INTERVAL 64
  6271. unsigned short Intervalcount = count / PCI_LOAD_INTERVAL;
  6272. unsigned short Index;
  6273. unsigned long Dummy;
  6274. for ( Index = 0 ; Index < Intervalcount ; Index++ )
  6275. {
  6276. memcpy(TargetPtr, SourcePtr, PCI_LOAD_INTERVAL);
  6277. Dummy = *((volatile unsigned long *)TargetPtr);
  6278. TargetPtr += PCI_LOAD_INTERVAL;
  6279. SourcePtr += PCI_LOAD_INTERVAL;
  6280. }
  6281. memcpy( TargetPtr, SourcePtr, count % PCI_LOAD_INTERVAL );
  6282. } /* End Of mgsl_load_pci_memory() */
  6283. static void mgsl_trace_block(struct mgsl_struct *info,const char* data, int count, int xmit)
  6284. {
  6285. int i;
  6286. int linecount;
  6287. if (xmit)
  6288. printk("%s tx data:\n",info->device_name);
  6289. else
  6290. printk("%s rx data:\n",info->device_name);
  6291. while(count) {
  6292. if (count > 16)
  6293. linecount = 16;
  6294. else
  6295. linecount = count;
  6296. for(i=0;i<linecount;i++)
  6297. printk("%02X ",(unsigned char)data[i]);
  6298. for(;i<17;i++)
  6299. printk(" ");
  6300. for(i=0;i<linecount;i++) {
  6301. if (data[i]>=040 && data[i]<=0176)
  6302. printk("%c",data[i]);
  6303. else
  6304. printk(".");
  6305. }
  6306. printk("\n");
  6307. data += linecount;
  6308. count -= linecount;
  6309. }
  6310. } /* end of mgsl_trace_block() */
  6311. /* mgsl_tx_timeout()
  6312. *
  6313. * called when HDLC frame times out
  6314. * update stats and do tx completion processing
  6315. *
  6316. * Arguments: context pointer to device instance data
  6317. * Return Value: None
  6318. */
  6319. static void mgsl_tx_timeout(unsigned long context)
  6320. {
  6321. struct mgsl_struct *info = (struct mgsl_struct*)context;
  6322. unsigned long flags;
  6323. if ( debug_level >= DEBUG_LEVEL_INFO )
  6324. printk( "%s(%d):mgsl_tx_timeout(%s)\n",
  6325. __FILE__,__LINE__,info->device_name);
  6326. if(info->tx_active &&
  6327. (info->params.mode == MGSL_MODE_HDLC ||
  6328. info->params.mode == MGSL_MODE_RAW) ) {
  6329. info->icount.txtimeout++;
  6330. }
  6331. spin_lock_irqsave(&info->irq_spinlock,flags);
  6332. info->tx_active = false;
  6333. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  6334. if ( info->params.flags & HDLC_FLAG_HDLC_LOOPMODE )
  6335. usc_loopmode_cancel_transmit( info );
  6336. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  6337. #if SYNCLINK_GENERIC_HDLC
  6338. if (info->netcount)
  6339. hdlcdev_tx_done(info);
  6340. else
  6341. #endif
  6342. mgsl_bh_transmit(info);
  6343. } /* end of mgsl_tx_timeout() */
  6344. /* signal that there are no more frames to send, so that
  6345. * line is 'released' by echoing RxD to TxD when current
  6346. * transmission is complete (or immediately if no tx in progress).
  6347. */
  6348. static int mgsl_loopmode_send_done( struct mgsl_struct * info )
  6349. {
  6350. unsigned long flags;
  6351. spin_lock_irqsave(&info->irq_spinlock,flags);
  6352. if (info->params.flags & HDLC_FLAG_HDLC_LOOPMODE) {
  6353. if (info->tx_active)
  6354. info->loopmode_send_done_requested = true;
  6355. else
  6356. usc_loopmode_send_done(info);
  6357. }
  6358. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  6359. return 0;
  6360. }
  6361. /* release the line by echoing RxD to TxD
  6362. * upon completion of a transmit frame
  6363. */
  6364. static void usc_loopmode_send_done( struct mgsl_struct * info )
  6365. {
  6366. info->loopmode_send_done_requested = false;
  6367. /* clear CMR:13 to 0 to start echoing RxData to TxData */
  6368. info->cmr_value &= ~BIT13;
  6369. usc_OutReg(info, CMR, info->cmr_value);
  6370. }
  6371. /* abort a transmit in progress while in HDLC LoopMode
  6372. */
  6373. static void usc_loopmode_cancel_transmit( struct mgsl_struct * info )
  6374. {
  6375. /* reset tx dma channel and purge TxFifo */
  6376. usc_RTCmd( info, RTCmd_PurgeTxFifo );
  6377. usc_DmaCmd( info, DmaCmd_ResetTxChannel );
  6378. usc_loopmode_send_done( info );
  6379. }
  6380. /* for HDLC/SDLC LoopMode, setting CMR:13 after the transmitter is enabled
  6381. * is an Insert Into Loop action. Upon receipt of a GoAhead sequence (RxAbort)
  6382. * we must clear CMR:13 to begin repeating TxData to RxData
  6383. */
  6384. static void usc_loopmode_insert_request( struct mgsl_struct * info )
  6385. {
  6386. info->loopmode_insert_requested = true;
  6387. /* enable RxAbort irq. On next RxAbort, clear CMR:13 to
  6388. * begin repeating TxData on RxData (complete insertion)
  6389. */
  6390. usc_OutReg( info, RICR,
  6391. (usc_InReg( info, RICR ) | RXSTATUS_ABORT_RECEIVED ) );
  6392. /* set CMR:13 to insert into loop on next GoAhead (RxAbort) */
  6393. info->cmr_value |= BIT13;
  6394. usc_OutReg(info, CMR, info->cmr_value);
  6395. }
  6396. /* return 1 if station is inserted into the loop, otherwise 0
  6397. */
  6398. static int usc_loopmode_active( struct mgsl_struct * info)
  6399. {
  6400. return usc_InReg( info, CCSR ) & BIT7 ? 1 : 0 ;
  6401. }
  6402. #if SYNCLINK_GENERIC_HDLC
  6403. /**
  6404. * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
  6405. * set encoding and frame check sequence (FCS) options
  6406. *
  6407. * dev pointer to network device structure
  6408. * encoding serial encoding setting
  6409. * parity FCS setting
  6410. *
  6411. * returns 0 if success, otherwise error code
  6412. */
  6413. static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
  6414. unsigned short parity)
  6415. {
  6416. struct mgsl_struct *info = dev_to_port(dev);
  6417. unsigned char new_encoding;
  6418. unsigned short new_crctype;
  6419. /* return error if TTY interface open */
  6420. if (info->count)
  6421. return -EBUSY;
  6422. switch (encoding)
  6423. {
  6424. case ENCODING_NRZ: new_encoding = HDLC_ENCODING_NRZ; break;
  6425. case ENCODING_NRZI: new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
  6426. case ENCODING_FM_MARK: new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
  6427. case ENCODING_FM_SPACE: new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
  6428. case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
  6429. default: return -EINVAL;
  6430. }
  6431. switch (parity)
  6432. {
  6433. case PARITY_NONE: new_crctype = HDLC_CRC_NONE; break;
  6434. case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
  6435. case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
  6436. default: return -EINVAL;
  6437. }
  6438. info->params.encoding = new_encoding;
  6439. info->params.crc_type = new_crctype;
  6440. /* if network interface up, reprogram hardware */
  6441. if (info->netcount)
  6442. mgsl_program_hw(info);
  6443. return 0;
  6444. }
  6445. /**
  6446. * called by generic HDLC layer to send frame
  6447. *
  6448. * skb socket buffer containing HDLC frame
  6449. * dev pointer to network device structure
  6450. *
  6451. * returns 0 if success, otherwise error code
  6452. */
  6453. static int hdlcdev_xmit(struct sk_buff *skb, struct net_device *dev)
  6454. {
  6455. struct mgsl_struct *info = dev_to_port(dev);
  6456. struct net_device_stats *stats = hdlc_stats(dev);
  6457. unsigned long flags;
  6458. if (debug_level >= DEBUG_LEVEL_INFO)
  6459. printk(KERN_INFO "%s:hdlc_xmit(%s)\n",__FILE__,dev->name);
  6460. /* stop sending until this frame completes */
  6461. netif_stop_queue(dev);
  6462. /* copy data to device buffers */
  6463. info->xmit_cnt = skb->len;
  6464. mgsl_load_tx_dma_buffer(info, skb->data, skb->len);
  6465. /* update network statistics */
  6466. stats->tx_packets++;
  6467. stats->tx_bytes += skb->len;
  6468. /* done with socket buffer, so free it */
  6469. dev_kfree_skb(skb);
  6470. /* save start time for transmit timeout detection */
  6471. dev->trans_start = jiffies;
  6472. /* start hardware transmitter if necessary */
  6473. spin_lock_irqsave(&info->irq_spinlock,flags);
  6474. if (!info->tx_active)
  6475. usc_start_transmitter(info);
  6476. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  6477. return 0;
  6478. }
  6479. /**
  6480. * called by network layer when interface enabled
  6481. * claim resources and initialize hardware
  6482. *
  6483. * dev pointer to network device structure
  6484. *
  6485. * returns 0 if success, otherwise error code
  6486. */
  6487. static int hdlcdev_open(struct net_device *dev)
  6488. {
  6489. struct mgsl_struct *info = dev_to_port(dev);
  6490. int rc;
  6491. unsigned long flags;
  6492. if (debug_level >= DEBUG_LEVEL_INFO)
  6493. printk("%s:hdlcdev_open(%s)\n",__FILE__,dev->name);
  6494. /* generic HDLC layer open processing */
  6495. if ((rc = hdlc_open(dev)))
  6496. return rc;
  6497. /* arbitrate between network and tty opens */
  6498. spin_lock_irqsave(&info->netlock, flags);
  6499. if (info->count != 0 || info->netcount != 0) {
  6500. printk(KERN_WARNING "%s: hdlc_open returning busy\n", dev->name);
  6501. spin_unlock_irqrestore(&info->netlock, flags);
  6502. return -EBUSY;
  6503. }
  6504. info->netcount=1;
  6505. spin_unlock_irqrestore(&info->netlock, flags);
  6506. /* claim resources and init adapter */
  6507. if ((rc = startup(info)) != 0) {
  6508. spin_lock_irqsave(&info->netlock, flags);
  6509. info->netcount=0;
  6510. spin_unlock_irqrestore(&info->netlock, flags);
  6511. return rc;
  6512. }
  6513. /* assert DTR and RTS, apply hardware settings */
  6514. info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
  6515. mgsl_program_hw(info);
  6516. /* enable network layer transmit */
  6517. dev->trans_start = jiffies;
  6518. netif_start_queue(dev);
  6519. /* inform generic HDLC layer of current DCD status */
  6520. spin_lock_irqsave(&info->irq_spinlock, flags);
  6521. usc_get_serial_signals(info);
  6522. spin_unlock_irqrestore(&info->irq_spinlock, flags);
  6523. if (info->serial_signals & SerialSignal_DCD)
  6524. netif_carrier_on(dev);
  6525. else
  6526. netif_carrier_off(dev);
  6527. return 0;
  6528. }
  6529. /**
  6530. * called by network layer when interface is disabled
  6531. * shutdown hardware and release resources
  6532. *
  6533. * dev pointer to network device structure
  6534. *
  6535. * returns 0 if success, otherwise error code
  6536. */
  6537. static int hdlcdev_close(struct net_device *dev)
  6538. {
  6539. struct mgsl_struct *info = dev_to_port(dev);
  6540. unsigned long flags;
  6541. if (debug_level >= DEBUG_LEVEL_INFO)
  6542. printk("%s:hdlcdev_close(%s)\n",__FILE__,dev->name);
  6543. netif_stop_queue(dev);
  6544. /* shutdown adapter and release resources */
  6545. shutdown(info);
  6546. hdlc_close(dev);
  6547. spin_lock_irqsave(&info->netlock, flags);
  6548. info->netcount=0;
  6549. spin_unlock_irqrestore(&info->netlock, flags);
  6550. return 0;
  6551. }
  6552. /**
  6553. * called by network layer to process IOCTL call to network device
  6554. *
  6555. * dev pointer to network device structure
  6556. * ifr pointer to network interface request structure
  6557. * cmd IOCTL command code
  6558. *
  6559. * returns 0 if success, otherwise error code
  6560. */
  6561. static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  6562. {
  6563. const size_t size = sizeof(sync_serial_settings);
  6564. sync_serial_settings new_line;
  6565. sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
  6566. struct mgsl_struct *info = dev_to_port(dev);
  6567. unsigned int flags;
  6568. if (debug_level >= DEBUG_LEVEL_INFO)
  6569. printk("%s:hdlcdev_ioctl(%s)\n",__FILE__,dev->name);
  6570. /* return error if TTY interface open */
  6571. if (info->count)
  6572. return -EBUSY;
  6573. if (cmd != SIOCWANDEV)
  6574. return hdlc_ioctl(dev, ifr, cmd);
  6575. switch(ifr->ifr_settings.type) {
  6576. case IF_GET_IFACE: /* return current sync_serial_settings */
  6577. ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
  6578. if (ifr->ifr_settings.size < size) {
  6579. ifr->ifr_settings.size = size; /* data size wanted */
  6580. return -ENOBUFS;
  6581. }
  6582. flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
  6583. HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
  6584. HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
  6585. HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
  6586. switch (flags){
  6587. case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
  6588. case (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_INT; break;
  6589. case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_TXINT; break;
  6590. case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
  6591. default: new_line.clock_type = CLOCK_DEFAULT;
  6592. }
  6593. new_line.clock_rate = info->params.clock_speed;
  6594. new_line.loopback = info->params.loopback ? 1:0;
  6595. if (copy_to_user(line, &new_line, size))
  6596. return -EFAULT;
  6597. return 0;
  6598. case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
  6599. if(!capable(CAP_NET_ADMIN))
  6600. return -EPERM;
  6601. if (copy_from_user(&new_line, line, size))
  6602. return -EFAULT;
  6603. switch (new_line.clock_type)
  6604. {
  6605. case CLOCK_EXT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
  6606. case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
  6607. case CLOCK_INT: flags = HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG; break;
  6608. case CLOCK_TXINT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG; break;
  6609. case CLOCK_DEFAULT: flags = info->params.flags &
  6610. (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
  6611. HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
  6612. HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
  6613. HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN); break;
  6614. default: return -EINVAL;
  6615. }
  6616. if (new_line.loopback != 0 && new_line.loopback != 1)
  6617. return -EINVAL;
  6618. info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
  6619. HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
  6620. HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
  6621. HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
  6622. info->params.flags |= flags;
  6623. info->params.loopback = new_line.loopback;
  6624. if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
  6625. info->params.clock_speed = new_line.clock_rate;
  6626. else
  6627. info->params.clock_speed = 0;
  6628. /* if network interface up, reprogram hardware */
  6629. if (info->netcount)
  6630. mgsl_program_hw(info);
  6631. return 0;
  6632. default:
  6633. return hdlc_ioctl(dev, ifr, cmd);
  6634. }
  6635. }
  6636. /**
  6637. * called by network layer when transmit timeout is detected
  6638. *
  6639. * dev pointer to network device structure
  6640. */
  6641. static void hdlcdev_tx_timeout(struct net_device *dev)
  6642. {
  6643. struct mgsl_struct *info = dev_to_port(dev);
  6644. struct net_device_stats *stats = hdlc_stats(dev);
  6645. unsigned long flags;
  6646. if (debug_level >= DEBUG_LEVEL_INFO)
  6647. printk("hdlcdev_tx_timeout(%s)\n",dev->name);
  6648. stats->tx_errors++;
  6649. stats->tx_aborted_errors++;
  6650. spin_lock_irqsave(&info->irq_spinlock,flags);
  6651. usc_stop_transmitter(info);
  6652. spin_unlock_irqrestore(&info->irq_spinlock,flags);
  6653. netif_wake_queue(dev);
  6654. }
  6655. /**
  6656. * called by device driver when transmit completes
  6657. * reenable network layer transmit if stopped
  6658. *
  6659. * info pointer to device instance information
  6660. */
  6661. static void hdlcdev_tx_done(struct mgsl_struct *info)
  6662. {
  6663. if (netif_queue_stopped(info->netdev))
  6664. netif_wake_queue(info->netdev);
  6665. }
  6666. /**
  6667. * called by device driver when frame received
  6668. * pass frame to network layer
  6669. *
  6670. * info pointer to device instance information
  6671. * buf pointer to buffer contianing frame data
  6672. * size count of data bytes in buf
  6673. */
  6674. static void hdlcdev_rx(struct mgsl_struct *info, char *buf, int size)
  6675. {
  6676. struct sk_buff *skb = dev_alloc_skb(size);
  6677. struct net_device *dev = info->netdev;
  6678. struct net_device_stats *stats = hdlc_stats(dev);
  6679. if (debug_level >= DEBUG_LEVEL_INFO)
  6680. printk("hdlcdev_rx(%s)\n",dev->name);
  6681. if (skb == NULL) {
  6682. printk(KERN_NOTICE "%s: can't alloc skb, dropping packet\n", dev->name);
  6683. stats->rx_dropped++;
  6684. return;
  6685. }
  6686. memcpy(skb_put(skb, size),buf,size);
  6687. skb->protocol = hdlc_type_trans(skb, info->netdev);
  6688. stats->rx_packets++;
  6689. stats->rx_bytes += size;
  6690. netif_rx(skb);
  6691. info->netdev->last_rx = jiffies;
  6692. }
  6693. /**
  6694. * called by device driver when adding device instance
  6695. * do generic HDLC initialization
  6696. *
  6697. * info pointer to device instance information
  6698. *
  6699. * returns 0 if success, otherwise error code
  6700. */
  6701. static int hdlcdev_init(struct mgsl_struct *info)
  6702. {
  6703. int rc;
  6704. struct net_device *dev;
  6705. hdlc_device *hdlc;
  6706. /* allocate and initialize network and HDLC layer objects */
  6707. if (!(dev = alloc_hdlcdev(info))) {
  6708. printk(KERN_ERR "%s:hdlc device allocation failure\n",__FILE__);
  6709. return -ENOMEM;
  6710. }
  6711. /* for network layer reporting purposes only */
  6712. dev->base_addr = info->io_base;
  6713. dev->irq = info->irq_level;
  6714. dev->dma = info->dma_level;
  6715. /* network layer callbacks and settings */
  6716. dev->do_ioctl = hdlcdev_ioctl;
  6717. dev->open = hdlcdev_open;
  6718. dev->stop = hdlcdev_close;
  6719. dev->tx_timeout = hdlcdev_tx_timeout;
  6720. dev->watchdog_timeo = 10*HZ;
  6721. dev->tx_queue_len = 50;
  6722. /* generic HDLC layer callbacks and settings */
  6723. hdlc = dev_to_hdlc(dev);
  6724. hdlc->attach = hdlcdev_attach;
  6725. hdlc->xmit = hdlcdev_xmit;
  6726. /* register objects with HDLC layer */
  6727. if ((rc = register_hdlc_device(dev))) {
  6728. printk(KERN_WARNING "%s:unable to register hdlc device\n",__FILE__);
  6729. free_netdev(dev);
  6730. return rc;
  6731. }
  6732. info->netdev = dev;
  6733. return 0;
  6734. }
  6735. /**
  6736. * called by device driver when removing device instance
  6737. * do generic HDLC cleanup
  6738. *
  6739. * info pointer to device instance information
  6740. */
  6741. static void hdlcdev_exit(struct mgsl_struct *info)
  6742. {
  6743. unregister_hdlc_device(info->netdev);
  6744. free_netdev(info->netdev);
  6745. info->netdev = NULL;
  6746. }
  6747. #endif /* CONFIG_HDLC */
  6748. static int __devinit synclink_init_one (struct pci_dev *dev,
  6749. const struct pci_device_id *ent)
  6750. {
  6751. struct mgsl_struct *info;
  6752. if (pci_enable_device(dev)) {
  6753. printk("error enabling pci device %p\n", dev);
  6754. return -EIO;
  6755. }
  6756. if (!(info = mgsl_allocate_device())) {
  6757. printk("can't allocate device instance data.\n");
  6758. return -EIO;
  6759. }
  6760. /* Copy user configuration info to device instance data */
  6761. info->io_base = pci_resource_start(dev, 2);
  6762. info->irq_level = dev->irq;
  6763. info->phys_memory_base = pci_resource_start(dev, 3);
  6764. /* Because veremap only works on page boundaries we must map
  6765. * a larger area than is actually implemented for the LCR
  6766. * memory range. We map a full page starting at the page boundary.
  6767. */
  6768. info->phys_lcr_base = pci_resource_start(dev, 0);
  6769. info->lcr_offset = info->phys_lcr_base & (PAGE_SIZE-1);
  6770. info->phys_lcr_base &= ~(PAGE_SIZE-1);
  6771. info->bus_type = MGSL_BUS_TYPE_PCI;
  6772. info->io_addr_size = 8;
  6773. info->irq_flags = IRQF_SHARED;
  6774. if (dev->device == 0x0210) {
  6775. /* Version 1 PCI9030 based universal PCI adapter */
  6776. info->misc_ctrl_value = 0x007c4080;
  6777. info->hw_version = 1;
  6778. } else {
  6779. /* Version 0 PCI9050 based 5V PCI adapter
  6780. * A PCI9050 bug prevents reading LCR registers if
  6781. * LCR base address bit 7 is set. Maintain shadow
  6782. * value so we can write to LCR misc control reg.
  6783. */
  6784. info->misc_ctrl_value = 0x087e4546;
  6785. info->hw_version = 0;
  6786. }
  6787. mgsl_add_device(info);
  6788. return 0;
  6789. }
  6790. static void __devexit synclink_remove_one (struct pci_dev *dev)
  6791. {
  6792. }