synclink.c 230 KB

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