synclink.c 230 KB

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