synclink.c 232 KB

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