synclink.c 230 KB

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