synclink.c 230 KB

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