synclink.c 231 KB

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