synclink.c 232 KB

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