synclink.c 231 KB

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