synclink.c 233 KB

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