synclink.c 229 KB

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