synclink.c 229 KB

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