synclink.c 230 KB

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