synclink.c 232 KB

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