synclink.c 230 KB

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