synclink.c 231 KB

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