synclink.c 230 KB

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