synclink.c 229 KB

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