synclink.c 231 KB

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