synclink.c 231 KB

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