synclink.c 229 KB

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