synclink.c 231 KB

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