synclink.c 230 KB

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