synclinkmp.c 148 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661
  1. /*
  2. * $Id: synclinkmp.c,v 4.38 2005/07/15 13:29:44 paulkf Exp $
  3. *
  4. * Device driver for Microgate SyncLink Multiport
  5. * high speed multiprotocol serial adapter.
  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. * This code is released under the GNU General Public License (GPL)
  14. *
  15. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  16. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  19. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  25. * OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #define VERSION(ver,rel,seq) (((ver)<<16) | ((rel)<<8) | (seq))
  28. #if defined(__i386__)
  29. # define BREAKPOINT() asm(" int $3");
  30. #else
  31. # define BREAKPOINT() { }
  32. #endif
  33. #define MAX_DEVICES 12
  34. #include <linux/config.h>
  35. #include <linux/module.h>
  36. #include <linux/errno.h>
  37. #include <linux/signal.h>
  38. #include <linux/sched.h>
  39. #include <linux/timer.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/pci.h>
  42. #include <linux/tty.h>
  43. #include <linux/tty_flip.h>
  44. #include <linux/serial.h>
  45. #include <linux/major.h>
  46. #include <linux/string.h>
  47. #include <linux/fcntl.h>
  48. #include <linux/ptrace.h>
  49. #include <linux/ioport.h>
  50. #include <linux/mm.h>
  51. #include <linux/slab.h>
  52. #include <linux/netdevice.h>
  53. #include <linux/vmalloc.h>
  54. #include <linux/init.h>
  55. #include <linux/delay.h>
  56. #include <linux/ioctl.h>
  57. #include <asm/system.h>
  58. #include <asm/io.h>
  59. #include <asm/irq.h>
  60. #include <asm/dma.h>
  61. #include <linux/bitops.h>
  62. #include <asm/types.h>
  63. #include <linux/termios.h>
  64. #include <linux/workqueue.h>
  65. #include <linux/hdlc.h>
  66. #ifdef CONFIG_HDLC_MODULE
  67. #define CONFIG_HDLC 1
  68. #endif
  69. #define GET_USER(error,value,addr) error = get_user(value,addr)
  70. #define COPY_FROM_USER(error,dest,src,size) error = copy_from_user(dest,src,size) ? -EFAULT : 0
  71. #define PUT_USER(error,value,addr) error = put_user(value,addr)
  72. #define COPY_TO_USER(error,dest,src,size) error = copy_to_user(dest,src,size) ? -EFAULT : 0
  73. #include <asm/uaccess.h>
  74. #include "linux/synclink.h"
  75. static MGSL_PARAMS default_params = {
  76. MGSL_MODE_HDLC, /* unsigned long mode */
  77. 0, /* unsigned char loopback; */
  78. HDLC_FLAG_UNDERRUN_ABORT15, /* unsigned short flags; */
  79. HDLC_ENCODING_NRZI_SPACE, /* unsigned char encoding; */
  80. 0, /* unsigned long clock_speed; */
  81. 0xff, /* unsigned char addr_filter; */
  82. HDLC_CRC_16_CCITT, /* unsigned short crc_type; */
  83. HDLC_PREAMBLE_LENGTH_8BITS, /* unsigned char preamble_length; */
  84. HDLC_PREAMBLE_PATTERN_NONE, /* unsigned char preamble; */
  85. 9600, /* unsigned long data_rate; */
  86. 8, /* unsigned char data_bits; */
  87. 1, /* unsigned char stop_bits; */
  88. ASYNC_PARITY_NONE /* unsigned char parity; */
  89. };
  90. /* size in bytes of DMA data buffers */
  91. #define SCABUFSIZE 1024
  92. #define SCA_MEM_SIZE 0x40000
  93. #define SCA_BASE_SIZE 512
  94. #define SCA_REG_SIZE 16
  95. #define SCA_MAX_PORTS 4
  96. #define SCAMAXDESC 128
  97. #define BUFFERLISTSIZE 4096
  98. /* SCA-I style DMA buffer descriptor */
  99. typedef struct _SCADESC
  100. {
  101. u16 next; /* lower l6 bits of next descriptor addr */
  102. u16 buf_ptr; /* lower 16 bits of buffer addr */
  103. u8 buf_base; /* upper 8 bits of buffer addr */
  104. u8 pad1;
  105. u16 length; /* length of buffer */
  106. u8 status; /* status of buffer */
  107. u8 pad2;
  108. } SCADESC, *PSCADESC;
  109. typedef struct _SCADESC_EX
  110. {
  111. /* device driver bookkeeping section */
  112. char *virt_addr; /* virtual address of data buffer */
  113. u16 phys_entry; /* lower 16-bits of physical address of this descriptor */
  114. } SCADESC_EX, *PSCADESC_EX;
  115. /* The queue of BH actions to be performed */
  116. #define BH_RECEIVE 1
  117. #define BH_TRANSMIT 2
  118. #define BH_STATUS 4
  119. #define IO_PIN_SHUTDOWN_LIMIT 100
  120. #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
  121. struct _input_signal_events {
  122. int ri_up;
  123. int ri_down;
  124. int dsr_up;
  125. int dsr_down;
  126. int dcd_up;
  127. int dcd_down;
  128. int cts_up;
  129. int cts_down;
  130. };
  131. /*
  132. * Device instance data structure
  133. */
  134. typedef struct _synclinkmp_info {
  135. void *if_ptr; /* General purpose pointer (used by SPPP) */
  136. int magic;
  137. int flags;
  138. int count; /* count of opens */
  139. int line;
  140. unsigned short close_delay;
  141. unsigned short closing_wait; /* time to wait before closing */
  142. struct mgsl_icount icount;
  143. struct tty_struct *tty;
  144. int timeout;
  145. int x_char; /* xon/xoff character */
  146. int blocked_open; /* # of blocked opens */
  147. u16 read_status_mask1; /* break detection (SR1 indications) */
  148. u16 read_status_mask2; /* parity/framing/overun (SR2 indications) */
  149. unsigned char ignore_status_mask1; /* break detection (SR1 indications) */
  150. unsigned char ignore_status_mask2; /* parity/framing/overun (SR2 indications) */
  151. unsigned char *tx_buf;
  152. int tx_put;
  153. int tx_get;
  154. int tx_count;
  155. wait_queue_head_t open_wait;
  156. wait_queue_head_t close_wait;
  157. wait_queue_head_t status_event_wait_q;
  158. wait_queue_head_t event_wait_q;
  159. struct timer_list tx_timer; /* HDLC transmit timeout timer */
  160. struct _synclinkmp_info *next_device; /* device list link */
  161. struct timer_list status_timer; /* input signal status check timer */
  162. spinlock_t lock; /* spinlock for synchronizing with ISR */
  163. struct work_struct task; /* task structure for scheduling bh */
  164. u32 max_frame_size; /* as set by device config */
  165. u32 pending_bh;
  166. int bh_running; /* Protection from multiple */
  167. int isr_overflow;
  168. int bh_requested;
  169. int dcd_chkcount; /* check counts to prevent */
  170. int cts_chkcount; /* too many IRQs if a signal */
  171. int dsr_chkcount; /* is floating */
  172. int ri_chkcount;
  173. char *buffer_list; /* virtual address of Rx & Tx buffer lists */
  174. unsigned long buffer_list_phys;
  175. unsigned int rx_buf_count; /* count of total allocated Rx buffers */
  176. SCADESC *rx_buf_list; /* list of receive buffer entries */
  177. SCADESC_EX rx_buf_list_ex[SCAMAXDESC]; /* list of receive buffer entries */
  178. unsigned int current_rx_buf;
  179. unsigned int tx_buf_count; /* count of total allocated Tx buffers */
  180. SCADESC *tx_buf_list; /* list of transmit buffer entries */
  181. SCADESC_EX tx_buf_list_ex[SCAMAXDESC]; /* list of transmit buffer entries */
  182. unsigned int last_tx_buf;
  183. unsigned char *tmp_rx_buf;
  184. unsigned int tmp_rx_buf_count;
  185. int rx_enabled;
  186. int rx_overflow;
  187. int tx_enabled;
  188. int tx_active;
  189. u32 idle_mode;
  190. unsigned char ie0_value;
  191. unsigned char ie1_value;
  192. unsigned char ie2_value;
  193. unsigned char ctrlreg_value;
  194. unsigned char old_signals;
  195. char device_name[25]; /* device instance name */
  196. int port_count;
  197. int adapter_num;
  198. int port_num;
  199. struct _synclinkmp_info *port_array[SCA_MAX_PORTS];
  200. unsigned int bus_type; /* expansion bus type (ISA,EISA,PCI) */
  201. unsigned int irq_level; /* interrupt level */
  202. unsigned long irq_flags;
  203. int irq_requested; /* nonzero if IRQ requested */
  204. MGSL_PARAMS params; /* communications parameters */
  205. unsigned char serial_signals; /* current serial signal states */
  206. int irq_occurred; /* for diagnostics use */
  207. unsigned int init_error; /* Initialization startup error */
  208. u32 last_mem_alloc;
  209. unsigned char* memory_base; /* shared memory address (PCI only) */
  210. u32 phys_memory_base;
  211. int shared_mem_requested;
  212. unsigned char* sca_base; /* HD64570 SCA Memory address */
  213. u32 phys_sca_base;
  214. u32 sca_offset;
  215. int sca_base_requested;
  216. unsigned char* lcr_base; /* local config registers (PCI only) */
  217. u32 phys_lcr_base;
  218. u32 lcr_offset;
  219. int lcr_mem_requested;
  220. unsigned char* statctrl_base; /* status/control register memory */
  221. u32 phys_statctrl_base;
  222. u32 statctrl_offset;
  223. int sca_statctrl_requested;
  224. u32 misc_ctrl_value;
  225. char flag_buf[MAX_ASYNC_BUFFER_SIZE];
  226. char char_buf[MAX_ASYNC_BUFFER_SIZE];
  227. BOOLEAN drop_rts_on_tx_done;
  228. struct _input_signal_events input_signal_events;
  229. /* SPPP/Cisco HDLC device parts */
  230. int netcount;
  231. int dosyncppp;
  232. spinlock_t netlock;
  233. #ifdef CONFIG_HDLC
  234. struct net_device *netdev;
  235. #endif
  236. } SLMP_INFO;
  237. #define MGSL_MAGIC 0x5401
  238. /*
  239. * define serial signal status change macros
  240. */
  241. #define MISCSTATUS_DCD_LATCHED (SerialSignal_DCD<<8) /* indicates change in DCD */
  242. #define MISCSTATUS_RI_LATCHED (SerialSignal_RI<<8) /* indicates change in RI */
  243. #define MISCSTATUS_CTS_LATCHED (SerialSignal_CTS<<8) /* indicates change in CTS */
  244. #define MISCSTATUS_DSR_LATCHED (SerialSignal_DSR<<8) /* change in DSR */
  245. /* Common Register macros */
  246. #define LPR 0x00
  247. #define PABR0 0x02
  248. #define PABR1 0x03
  249. #define WCRL 0x04
  250. #define WCRM 0x05
  251. #define WCRH 0x06
  252. #define DPCR 0x08
  253. #define DMER 0x09
  254. #define ISR0 0x10
  255. #define ISR1 0x11
  256. #define ISR2 0x12
  257. #define IER0 0x14
  258. #define IER1 0x15
  259. #define IER2 0x16
  260. #define ITCR 0x18
  261. #define INTVR 0x1a
  262. #define IMVR 0x1c
  263. /* MSCI Register macros */
  264. #define TRB 0x20
  265. #define TRBL 0x20
  266. #define TRBH 0x21
  267. #define SR0 0x22
  268. #define SR1 0x23
  269. #define SR2 0x24
  270. #define SR3 0x25
  271. #define FST 0x26
  272. #define IE0 0x28
  273. #define IE1 0x29
  274. #define IE2 0x2a
  275. #define FIE 0x2b
  276. #define CMD 0x2c
  277. #define MD0 0x2e
  278. #define MD1 0x2f
  279. #define MD2 0x30
  280. #define CTL 0x31
  281. #define SA0 0x32
  282. #define SA1 0x33
  283. #define IDL 0x34
  284. #define TMC 0x35
  285. #define RXS 0x36
  286. #define TXS 0x37
  287. #define TRC0 0x38
  288. #define TRC1 0x39
  289. #define RRC 0x3a
  290. #define CST0 0x3c
  291. #define CST1 0x3d
  292. /* Timer Register Macros */
  293. #define TCNT 0x60
  294. #define TCNTL 0x60
  295. #define TCNTH 0x61
  296. #define TCONR 0x62
  297. #define TCONRL 0x62
  298. #define TCONRH 0x63
  299. #define TMCS 0x64
  300. #define TEPR 0x65
  301. /* DMA Controller Register macros */
  302. #define DARL 0x80
  303. #define DARH 0x81
  304. #define DARB 0x82
  305. #define BAR 0x80
  306. #define BARL 0x80
  307. #define BARH 0x81
  308. #define BARB 0x82
  309. #define SAR 0x84
  310. #define SARL 0x84
  311. #define SARH 0x85
  312. #define SARB 0x86
  313. #define CPB 0x86
  314. #define CDA 0x88
  315. #define CDAL 0x88
  316. #define CDAH 0x89
  317. #define EDA 0x8a
  318. #define EDAL 0x8a
  319. #define EDAH 0x8b
  320. #define BFL 0x8c
  321. #define BFLL 0x8c
  322. #define BFLH 0x8d
  323. #define BCR 0x8e
  324. #define BCRL 0x8e
  325. #define BCRH 0x8f
  326. #define DSR 0x90
  327. #define DMR 0x91
  328. #define FCT 0x93
  329. #define DIR 0x94
  330. #define DCMD 0x95
  331. /* combine with timer or DMA register address */
  332. #define TIMER0 0x00
  333. #define TIMER1 0x08
  334. #define TIMER2 0x10
  335. #define TIMER3 0x18
  336. #define RXDMA 0x00
  337. #define TXDMA 0x20
  338. /* SCA Command Codes */
  339. #define NOOP 0x00
  340. #define TXRESET 0x01
  341. #define TXENABLE 0x02
  342. #define TXDISABLE 0x03
  343. #define TXCRCINIT 0x04
  344. #define TXCRCEXCL 0x05
  345. #define TXEOM 0x06
  346. #define TXABORT 0x07
  347. #define MPON 0x08
  348. #define TXBUFCLR 0x09
  349. #define RXRESET 0x11
  350. #define RXENABLE 0x12
  351. #define RXDISABLE 0x13
  352. #define RXCRCINIT 0x14
  353. #define RXREJECT 0x15
  354. #define SEARCHMP 0x16
  355. #define RXCRCEXCL 0x17
  356. #define RXCRCCALC 0x18
  357. #define CHRESET 0x21
  358. #define HUNT 0x31
  359. /* DMA command codes */
  360. #define SWABORT 0x01
  361. #define FEICLEAR 0x02
  362. /* IE0 */
  363. #define TXINTE BIT7
  364. #define RXINTE BIT6
  365. #define TXRDYE BIT1
  366. #define RXRDYE BIT0
  367. /* IE1 & SR1 */
  368. #define UDRN BIT7
  369. #define IDLE BIT6
  370. #define SYNCD BIT4
  371. #define FLGD BIT4
  372. #define CCTS BIT3
  373. #define CDCD BIT2
  374. #define BRKD BIT1
  375. #define ABTD BIT1
  376. #define GAPD BIT1
  377. #define BRKE BIT0
  378. #define IDLD BIT0
  379. /* IE2 & SR2 */
  380. #define EOM BIT7
  381. #define PMP BIT6
  382. #define SHRT BIT6
  383. #define PE BIT5
  384. #define ABT BIT5
  385. #define FRME BIT4
  386. #define RBIT BIT4
  387. #define OVRN BIT3
  388. #define CRCE BIT2
  389. /*
  390. * Global linked list of SyncLink devices
  391. */
  392. static SLMP_INFO *synclinkmp_device_list = NULL;
  393. static int synclinkmp_adapter_count = -1;
  394. static int synclinkmp_device_count = 0;
  395. /*
  396. * Set this param to non-zero to load eax with the
  397. * .text section address and breakpoint on module load.
  398. * This is useful for use with gdb and add-symbol-file command.
  399. */
  400. static int break_on_load=0;
  401. /*
  402. * Driver major number, defaults to zero to get auto
  403. * assigned major number. May be forced as module parameter.
  404. */
  405. static int ttymajor=0;
  406. /*
  407. * Array of user specified options for ISA adapters.
  408. */
  409. static int debug_level = 0;
  410. static int maxframe[MAX_DEVICES] = {0,};
  411. static int dosyncppp[MAX_DEVICES] = {0,};
  412. module_param(break_on_load, bool, 0);
  413. module_param(ttymajor, int, 0);
  414. module_param(debug_level, int, 0);
  415. module_param_array(maxframe, int, NULL, 0);
  416. module_param_array(dosyncppp, int, NULL, 0);
  417. static char *driver_name = "SyncLink MultiPort driver";
  418. static char *driver_version = "$Revision: 4.38 $";
  419. static int synclinkmp_init_one(struct pci_dev *dev,const struct pci_device_id *ent);
  420. static void synclinkmp_remove_one(struct pci_dev *dev);
  421. static struct pci_device_id synclinkmp_pci_tbl[] = {
  422. { PCI_VENDOR_ID_MICROGATE, PCI_DEVICE_ID_MICROGATE_SCA, PCI_ANY_ID, PCI_ANY_ID, },
  423. { 0, }, /* terminate list */
  424. };
  425. MODULE_DEVICE_TABLE(pci, synclinkmp_pci_tbl);
  426. MODULE_LICENSE("GPL");
  427. static struct pci_driver synclinkmp_pci_driver = {
  428. .owner = THIS_MODULE,
  429. .name = "synclinkmp",
  430. .id_table = synclinkmp_pci_tbl,
  431. .probe = synclinkmp_init_one,
  432. .remove = __devexit_p(synclinkmp_remove_one),
  433. };
  434. static struct tty_driver *serial_driver;
  435. /* number of characters left in xmit buffer before we ask for more */
  436. #define WAKEUP_CHARS 256
  437. /* tty callbacks */
  438. static int open(struct tty_struct *tty, struct file * filp);
  439. static void close(struct tty_struct *tty, struct file * filp);
  440. static void hangup(struct tty_struct *tty);
  441. static void set_termios(struct tty_struct *tty, struct termios *old_termios);
  442. static int write(struct tty_struct *tty, const unsigned char *buf, int count);
  443. static void put_char(struct tty_struct *tty, unsigned char ch);
  444. static void send_xchar(struct tty_struct *tty, char ch);
  445. static void wait_until_sent(struct tty_struct *tty, int timeout);
  446. static int write_room(struct tty_struct *tty);
  447. static void flush_chars(struct tty_struct *tty);
  448. static void flush_buffer(struct tty_struct *tty);
  449. static void tx_hold(struct tty_struct *tty);
  450. static void tx_release(struct tty_struct *tty);
  451. static int ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
  452. static int read_proc(char *page, char **start, off_t off, int count,int *eof, void *data);
  453. static int chars_in_buffer(struct tty_struct *tty);
  454. static void throttle(struct tty_struct * tty);
  455. static void unthrottle(struct tty_struct * tty);
  456. static void set_break(struct tty_struct *tty, int break_state);
  457. #ifdef CONFIG_HDLC
  458. #define dev_to_port(D) (dev_to_hdlc(D)->priv)
  459. static void hdlcdev_tx_done(SLMP_INFO *info);
  460. static void hdlcdev_rx(SLMP_INFO *info, char *buf, int size);
  461. static int hdlcdev_init(SLMP_INFO *info);
  462. static void hdlcdev_exit(SLMP_INFO *info);
  463. #endif
  464. /* ioctl handlers */
  465. static int get_stats(SLMP_INFO *info, struct mgsl_icount __user *user_icount);
  466. static int get_params(SLMP_INFO *info, MGSL_PARAMS __user *params);
  467. static int set_params(SLMP_INFO *info, MGSL_PARAMS __user *params);
  468. static int get_txidle(SLMP_INFO *info, int __user *idle_mode);
  469. static int set_txidle(SLMP_INFO *info, int idle_mode);
  470. static int tx_enable(SLMP_INFO *info, int enable);
  471. static int tx_abort(SLMP_INFO *info);
  472. static int rx_enable(SLMP_INFO *info, int enable);
  473. static int modem_input_wait(SLMP_INFO *info,int arg);
  474. static int wait_mgsl_event(SLMP_INFO *info, int __user *mask_ptr);
  475. static int tiocmget(struct tty_struct *tty, struct file *file);
  476. static int tiocmset(struct tty_struct *tty, struct file *file,
  477. unsigned int set, unsigned int clear);
  478. static void set_break(struct tty_struct *tty, int break_state);
  479. static void add_device(SLMP_INFO *info);
  480. static void device_init(int adapter_num, struct pci_dev *pdev);
  481. static int claim_resources(SLMP_INFO *info);
  482. static void release_resources(SLMP_INFO *info);
  483. static int startup(SLMP_INFO *info);
  484. static int block_til_ready(struct tty_struct *tty, struct file * filp,SLMP_INFO *info);
  485. static void shutdown(SLMP_INFO *info);
  486. static void program_hw(SLMP_INFO *info);
  487. static void change_params(SLMP_INFO *info);
  488. static int init_adapter(SLMP_INFO *info);
  489. static int register_test(SLMP_INFO *info);
  490. static int irq_test(SLMP_INFO *info);
  491. static int loopback_test(SLMP_INFO *info);
  492. static int adapter_test(SLMP_INFO *info);
  493. static int memory_test(SLMP_INFO *info);
  494. static void reset_adapter(SLMP_INFO *info);
  495. static void reset_port(SLMP_INFO *info);
  496. static void async_mode(SLMP_INFO *info);
  497. static void hdlc_mode(SLMP_INFO *info);
  498. static void rx_stop(SLMP_INFO *info);
  499. static void rx_start(SLMP_INFO *info);
  500. static void rx_reset_buffers(SLMP_INFO *info);
  501. static void rx_free_frame_buffers(SLMP_INFO *info, unsigned int first, unsigned int last);
  502. static int rx_get_frame(SLMP_INFO *info);
  503. static void tx_start(SLMP_INFO *info);
  504. static void tx_stop(SLMP_INFO *info);
  505. static void tx_load_fifo(SLMP_INFO *info);
  506. static void tx_set_idle(SLMP_INFO *info);
  507. static void tx_load_dma_buffer(SLMP_INFO *info, const char *buf, unsigned int count);
  508. static void get_signals(SLMP_INFO *info);
  509. static void set_signals(SLMP_INFO *info);
  510. static void enable_loopback(SLMP_INFO *info, int enable);
  511. static void set_rate(SLMP_INFO *info, u32 data_rate);
  512. static int bh_action(SLMP_INFO *info);
  513. static void bh_handler(void* Context);
  514. static void bh_receive(SLMP_INFO *info);
  515. static void bh_transmit(SLMP_INFO *info);
  516. static void bh_status(SLMP_INFO *info);
  517. static void isr_timer(SLMP_INFO *info);
  518. static void isr_rxint(SLMP_INFO *info);
  519. static void isr_rxrdy(SLMP_INFO *info);
  520. static void isr_txint(SLMP_INFO *info);
  521. static void isr_txrdy(SLMP_INFO *info);
  522. static void isr_rxdmaok(SLMP_INFO *info);
  523. static void isr_rxdmaerror(SLMP_INFO *info);
  524. static void isr_txdmaok(SLMP_INFO *info);
  525. static void isr_txdmaerror(SLMP_INFO *info);
  526. static void isr_io_pin(SLMP_INFO *info, u16 status);
  527. static int alloc_dma_bufs(SLMP_INFO *info);
  528. static void free_dma_bufs(SLMP_INFO *info);
  529. static int alloc_buf_list(SLMP_INFO *info);
  530. static int alloc_frame_bufs(SLMP_INFO *info, SCADESC *list, SCADESC_EX *list_ex,int count);
  531. static int alloc_tmp_rx_buf(SLMP_INFO *info);
  532. static void free_tmp_rx_buf(SLMP_INFO *info);
  533. static void load_pci_memory(SLMP_INFO *info, char* dest, const char* src, unsigned short count);
  534. static void trace_block(SLMP_INFO *info, const char* data, int count, int xmit);
  535. static void tx_timeout(unsigned long context);
  536. static void status_timeout(unsigned long context);
  537. static unsigned char read_reg(SLMP_INFO *info, unsigned char addr);
  538. static void write_reg(SLMP_INFO *info, unsigned char addr, unsigned char val);
  539. static u16 read_reg16(SLMP_INFO *info, unsigned char addr);
  540. static void write_reg16(SLMP_INFO *info, unsigned char addr, u16 val);
  541. static unsigned char read_status_reg(SLMP_INFO * info);
  542. static void write_control_reg(SLMP_INFO * info);
  543. static unsigned char rx_active_fifo_level = 16; // rx request FIFO activation level in bytes
  544. static unsigned char tx_active_fifo_level = 16; // tx request FIFO activation level in bytes
  545. static unsigned char tx_negate_fifo_level = 32; // tx request FIFO negation level in bytes
  546. static u32 misc_ctrl_value = 0x007e4040;
  547. static u32 lcr1_brdr_value = 0x00800028;
  548. static u32 read_ahead_count = 8;
  549. /* DPCR, DMA Priority Control
  550. *
  551. * 07..05 Not used, must be 0
  552. * 04 BRC, bus release condition: 0=all transfers complete
  553. * 1=release after 1 xfer on all channels
  554. * 03 CCC, channel change condition: 0=every cycle
  555. * 1=after each channel completes all xfers
  556. * 02..00 PR<2..0>, priority 100=round robin
  557. *
  558. * 00000100 = 0x00
  559. */
  560. static unsigned char dma_priority = 0x04;
  561. // Number of bytes that can be written to shared RAM
  562. // in a single write operation
  563. static u32 sca_pci_load_interval = 64;
  564. /*
  565. * 1st function defined in .text section. Calling this function in
  566. * init_module() followed by a breakpoint allows a remote debugger
  567. * (gdb) to get the .text address for the add-symbol-file command.
  568. * This allows remote debugging of dynamically loadable modules.
  569. */
  570. static void* synclinkmp_get_text_ptr(void);
  571. static void* synclinkmp_get_text_ptr(void) {return synclinkmp_get_text_ptr;}
  572. static inline int sanity_check(SLMP_INFO *info,
  573. char *name, const char *routine)
  574. {
  575. #ifdef SANITY_CHECK
  576. static const char *badmagic =
  577. "Warning: bad magic number for synclinkmp_struct (%s) in %s\n";
  578. static const char *badinfo =
  579. "Warning: null synclinkmp_struct for (%s) in %s\n";
  580. if (!info) {
  581. printk(badinfo, name, routine);
  582. return 1;
  583. }
  584. if (info->magic != MGSL_MAGIC) {
  585. printk(badmagic, name, routine);
  586. return 1;
  587. }
  588. #else
  589. if (!info)
  590. return 1;
  591. #endif
  592. return 0;
  593. }
  594. /**
  595. * line discipline callback wrappers
  596. *
  597. * The wrappers maintain line discipline references
  598. * while calling into the line discipline.
  599. *
  600. * ldisc_receive_buf - pass receive data to line discipline
  601. */
  602. static void ldisc_receive_buf(struct tty_struct *tty,
  603. const __u8 *data, char *flags, int count)
  604. {
  605. struct tty_ldisc *ld;
  606. if (!tty)
  607. return;
  608. ld = tty_ldisc_ref(tty);
  609. if (ld) {
  610. if (ld->receive_buf)
  611. ld->receive_buf(tty, data, flags, count);
  612. tty_ldisc_deref(ld);
  613. }
  614. }
  615. /* tty callbacks */
  616. /* Called when a port is opened. Init and enable port.
  617. */
  618. static int open(struct tty_struct *tty, struct file *filp)
  619. {
  620. SLMP_INFO *info;
  621. int retval, line;
  622. unsigned long flags;
  623. line = tty->index;
  624. if ((line < 0) || (line >= synclinkmp_device_count)) {
  625. printk("%s(%d): open with invalid line #%d.\n",
  626. __FILE__,__LINE__,line);
  627. return -ENODEV;
  628. }
  629. info = synclinkmp_device_list;
  630. while(info && info->line != line)
  631. info = info->next_device;
  632. if (sanity_check(info, tty->name, "open"))
  633. return -ENODEV;
  634. if ( info->init_error ) {
  635. printk("%s(%d):%s device is not allocated, init error=%d\n",
  636. __FILE__,__LINE__,info->device_name,info->init_error);
  637. return -ENODEV;
  638. }
  639. tty->driver_data = info;
  640. info->tty = tty;
  641. if (debug_level >= DEBUG_LEVEL_INFO)
  642. printk("%s(%d):%s open(), old ref count = %d\n",
  643. __FILE__,__LINE__,tty->driver->name, info->count);
  644. /* If port is closing, signal caller to try again */
  645. if (tty_hung_up_p(filp) || info->flags & ASYNC_CLOSING){
  646. if (info->flags & ASYNC_CLOSING)
  647. interruptible_sleep_on(&info->close_wait);
  648. retval = ((info->flags & ASYNC_HUP_NOTIFY) ?
  649. -EAGAIN : -ERESTARTSYS);
  650. goto cleanup;
  651. }
  652. info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  653. spin_lock_irqsave(&info->netlock, flags);
  654. if (info->netcount) {
  655. retval = -EBUSY;
  656. spin_unlock_irqrestore(&info->netlock, flags);
  657. goto cleanup;
  658. }
  659. info->count++;
  660. spin_unlock_irqrestore(&info->netlock, flags);
  661. if (info->count == 1) {
  662. /* 1st open on this device, init hardware */
  663. retval = startup(info);
  664. if (retval < 0)
  665. goto cleanup;
  666. }
  667. retval = block_til_ready(tty, filp, info);
  668. if (retval) {
  669. if (debug_level >= DEBUG_LEVEL_INFO)
  670. printk("%s(%d):%s block_til_ready() returned %d\n",
  671. __FILE__,__LINE__, info->device_name, retval);
  672. goto cleanup;
  673. }
  674. if (debug_level >= DEBUG_LEVEL_INFO)
  675. printk("%s(%d):%s open() success\n",
  676. __FILE__,__LINE__, info->device_name);
  677. retval = 0;
  678. cleanup:
  679. if (retval) {
  680. if (tty->count == 1)
  681. info->tty = NULL; /* tty layer will release tty struct */
  682. if(info->count)
  683. info->count--;
  684. }
  685. return retval;
  686. }
  687. /* Called when port is closed. Wait for remaining data to be
  688. * sent. Disable port and free resources.
  689. */
  690. static void close(struct tty_struct *tty, struct file *filp)
  691. {
  692. SLMP_INFO * info = (SLMP_INFO *)tty->driver_data;
  693. if (sanity_check(info, tty->name, "close"))
  694. return;
  695. if (debug_level >= DEBUG_LEVEL_INFO)
  696. printk("%s(%d):%s close() entry, count=%d\n",
  697. __FILE__,__LINE__, info->device_name, info->count);
  698. if (!info->count)
  699. return;
  700. if (tty_hung_up_p(filp))
  701. goto cleanup;
  702. if ((tty->count == 1) && (info->count != 1)) {
  703. /*
  704. * tty->count is 1 and the tty structure will be freed.
  705. * info->count should be one in this case.
  706. * if it's not, correct it so that the port is shutdown.
  707. */
  708. printk("%s(%d):%s close: bad refcount; tty->count is 1, "
  709. "info->count is %d\n",
  710. __FILE__,__LINE__, info->device_name, info->count);
  711. info->count = 1;
  712. }
  713. info->count--;
  714. /* if at least one open remaining, leave hardware active */
  715. if (info->count)
  716. goto cleanup;
  717. info->flags |= ASYNC_CLOSING;
  718. /* set tty->closing to notify line discipline to
  719. * only process XON/XOFF characters. Only the N_TTY
  720. * discipline appears to use this (ppp does not).
  721. */
  722. tty->closing = 1;
  723. /* wait for transmit data to clear all layers */
  724. if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE) {
  725. if (debug_level >= DEBUG_LEVEL_INFO)
  726. printk("%s(%d):%s close() calling tty_wait_until_sent\n",
  727. __FILE__,__LINE__, info->device_name );
  728. tty_wait_until_sent(tty, info->closing_wait);
  729. }
  730. if (info->flags & ASYNC_INITIALIZED)
  731. wait_until_sent(tty, info->timeout);
  732. if (tty->driver->flush_buffer)
  733. tty->driver->flush_buffer(tty);
  734. tty_ldisc_flush(tty);
  735. shutdown(info);
  736. tty->closing = 0;
  737. info->tty = NULL;
  738. if (info->blocked_open) {
  739. if (info->close_delay) {
  740. msleep_interruptible(jiffies_to_msecs(info->close_delay));
  741. }
  742. wake_up_interruptible(&info->open_wait);
  743. }
  744. info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
  745. wake_up_interruptible(&info->close_wait);
  746. cleanup:
  747. if (debug_level >= DEBUG_LEVEL_INFO)
  748. printk("%s(%d):%s close() exit, count=%d\n", __FILE__,__LINE__,
  749. tty->driver->name, info->count);
  750. }
  751. /* Called by tty_hangup() when a hangup is signaled.
  752. * This is the same as closing all open descriptors for the port.
  753. */
  754. static void hangup(struct tty_struct *tty)
  755. {
  756. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  757. if (debug_level >= DEBUG_LEVEL_INFO)
  758. printk("%s(%d):%s hangup()\n",
  759. __FILE__,__LINE__, info->device_name );
  760. if (sanity_check(info, tty->name, "hangup"))
  761. return;
  762. flush_buffer(tty);
  763. shutdown(info);
  764. info->count = 0;
  765. info->flags &= ~ASYNC_NORMAL_ACTIVE;
  766. info->tty = NULL;
  767. wake_up_interruptible(&info->open_wait);
  768. }
  769. /* Set new termios settings
  770. */
  771. static void set_termios(struct tty_struct *tty, struct termios *old_termios)
  772. {
  773. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  774. unsigned long flags;
  775. if (debug_level >= DEBUG_LEVEL_INFO)
  776. printk("%s(%d):%s set_termios()\n", __FILE__,__LINE__,
  777. tty->driver->name );
  778. /* just return if nothing has changed */
  779. if ((tty->termios->c_cflag == old_termios->c_cflag)
  780. && (RELEVANT_IFLAG(tty->termios->c_iflag)
  781. == RELEVANT_IFLAG(old_termios->c_iflag)))
  782. return;
  783. change_params(info);
  784. /* Handle transition to B0 status */
  785. if (old_termios->c_cflag & CBAUD &&
  786. !(tty->termios->c_cflag & CBAUD)) {
  787. info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
  788. spin_lock_irqsave(&info->lock,flags);
  789. set_signals(info);
  790. spin_unlock_irqrestore(&info->lock,flags);
  791. }
  792. /* Handle transition away from B0 status */
  793. if (!(old_termios->c_cflag & CBAUD) &&
  794. tty->termios->c_cflag & CBAUD) {
  795. info->serial_signals |= SerialSignal_DTR;
  796. if (!(tty->termios->c_cflag & CRTSCTS) ||
  797. !test_bit(TTY_THROTTLED, &tty->flags)) {
  798. info->serial_signals |= SerialSignal_RTS;
  799. }
  800. spin_lock_irqsave(&info->lock,flags);
  801. set_signals(info);
  802. spin_unlock_irqrestore(&info->lock,flags);
  803. }
  804. /* Handle turning off CRTSCTS */
  805. if (old_termios->c_cflag & CRTSCTS &&
  806. !(tty->termios->c_cflag & CRTSCTS)) {
  807. tty->hw_stopped = 0;
  808. tx_release(tty);
  809. }
  810. }
  811. /* Send a block of data
  812. *
  813. * Arguments:
  814. *
  815. * tty pointer to tty information structure
  816. * buf pointer to buffer containing send data
  817. * count size of send data in bytes
  818. *
  819. * Return Value: number of characters written
  820. */
  821. static int write(struct tty_struct *tty,
  822. const unsigned char *buf, int count)
  823. {
  824. int c, ret = 0;
  825. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  826. unsigned long flags;
  827. if (debug_level >= DEBUG_LEVEL_INFO)
  828. printk("%s(%d):%s write() count=%d\n",
  829. __FILE__,__LINE__,info->device_name,count);
  830. if (sanity_check(info, tty->name, "write"))
  831. goto cleanup;
  832. if (!tty || !info->tx_buf)
  833. goto cleanup;
  834. if (info->params.mode == MGSL_MODE_HDLC) {
  835. if (count > info->max_frame_size) {
  836. ret = -EIO;
  837. goto cleanup;
  838. }
  839. if (info->tx_active)
  840. goto cleanup;
  841. if (info->tx_count) {
  842. /* send accumulated data from send_char() calls */
  843. /* as frame and wait before accepting more data. */
  844. tx_load_dma_buffer(info, info->tx_buf, info->tx_count);
  845. goto start;
  846. }
  847. ret = info->tx_count = count;
  848. tx_load_dma_buffer(info, buf, count);
  849. goto start;
  850. }
  851. for (;;) {
  852. c = min_t(int, count,
  853. min(info->max_frame_size - info->tx_count - 1,
  854. info->max_frame_size - info->tx_put));
  855. if (c <= 0)
  856. break;
  857. memcpy(info->tx_buf + info->tx_put, buf, c);
  858. spin_lock_irqsave(&info->lock,flags);
  859. info->tx_put += c;
  860. if (info->tx_put >= info->max_frame_size)
  861. info->tx_put -= info->max_frame_size;
  862. info->tx_count += c;
  863. spin_unlock_irqrestore(&info->lock,flags);
  864. buf += c;
  865. count -= c;
  866. ret += c;
  867. }
  868. if (info->params.mode == MGSL_MODE_HDLC) {
  869. if (count) {
  870. ret = info->tx_count = 0;
  871. goto cleanup;
  872. }
  873. tx_load_dma_buffer(info, info->tx_buf, info->tx_count);
  874. }
  875. start:
  876. if (info->tx_count && !tty->stopped && !tty->hw_stopped) {
  877. spin_lock_irqsave(&info->lock,flags);
  878. if (!info->tx_active)
  879. tx_start(info);
  880. spin_unlock_irqrestore(&info->lock,flags);
  881. }
  882. cleanup:
  883. if (debug_level >= DEBUG_LEVEL_INFO)
  884. printk( "%s(%d):%s write() returning=%d\n",
  885. __FILE__,__LINE__,info->device_name,ret);
  886. return ret;
  887. }
  888. /* Add a character to the transmit buffer.
  889. */
  890. static void put_char(struct tty_struct *tty, unsigned char ch)
  891. {
  892. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  893. unsigned long flags;
  894. if ( debug_level >= DEBUG_LEVEL_INFO ) {
  895. printk( "%s(%d):%s put_char(%d)\n",
  896. __FILE__,__LINE__,info->device_name,ch);
  897. }
  898. if (sanity_check(info, tty->name, "put_char"))
  899. return;
  900. if (!tty || !info->tx_buf)
  901. return;
  902. spin_lock_irqsave(&info->lock,flags);
  903. if ( (info->params.mode != MGSL_MODE_HDLC) ||
  904. !info->tx_active ) {
  905. if (info->tx_count < info->max_frame_size - 1) {
  906. info->tx_buf[info->tx_put++] = ch;
  907. if (info->tx_put >= info->max_frame_size)
  908. info->tx_put -= info->max_frame_size;
  909. info->tx_count++;
  910. }
  911. }
  912. spin_unlock_irqrestore(&info->lock,flags);
  913. }
  914. /* Send a high-priority XON/XOFF character
  915. */
  916. static void send_xchar(struct tty_struct *tty, char ch)
  917. {
  918. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  919. unsigned long flags;
  920. if (debug_level >= DEBUG_LEVEL_INFO)
  921. printk("%s(%d):%s send_xchar(%d)\n",
  922. __FILE__,__LINE__, info->device_name, ch );
  923. if (sanity_check(info, tty->name, "send_xchar"))
  924. return;
  925. info->x_char = ch;
  926. if (ch) {
  927. /* Make sure transmit interrupts are on */
  928. spin_lock_irqsave(&info->lock,flags);
  929. if (!info->tx_enabled)
  930. tx_start(info);
  931. spin_unlock_irqrestore(&info->lock,flags);
  932. }
  933. }
  934. /* Wait until the transmitter is empty.
  935. */
  936. static void wait_until_sent(struct tty_struct *tty, int timeout)
  937. {
  938. SLMP_INFO * info = (SLMP_INFO *)tty->driver_data;
  939. unsigned long orig_jiffies, char_time;
  940. if (!info )
  941. return;
  942. if (debug_level >= DEBUG_LEVEL_INFO)
  943. printk("%s(%d):%s wait_until_sent() entry\n",
  944. __FILE__,__LINE__, info->device_name );
  945. if (sanity_check(info, tty->name, "wait_until_sent"))
  946. return;
  947. if (!(info->flags & ASYNC_INITIALIZED))
  948. goto exit;
  949. orig_jiffies = jiffies;
  950. /* Set check interval to 1/5 of estimated time to
  951. * send a character, and make it at least 1. The check
  952. * interval should also be less than the timeout.
  953. * Note: use tight timings here to satisfy the NIST-PCTS.
  954. */
  955. if ( info->params.data_rate ) {
  956. char_time = info->timeout/(32 * 5);
  957. if (!char_time)
  958. char_time++;
  959. } else
  960. char_time = 1;
  961. if (timeout)
  962. char_time = min_t(unsigned long, char_time, timeout);
  963. if ( info->params.mode == MGSL_MODE_HDLC ) {
  964. while (info->tx_active) {
  965. msleep_interruptible(jiffies_to_msecs(char_time));
  966. if (signal_pending(current))
  967. break;
  968. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  969. break;
  970. }
  971. } else {
  972. //TODO: determine if there is something similar to USC16C32
  973. // TXSTATUS_ALL_SENT status
  974. while ( info->tx_active && info->tx_enabled) {
  975. msleep_interruptible(jiffies_to_msecs(char_time));
  976. if (signal_pending(current))
  977. break;
  978. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  979. break;
  980. }
  981. }
  982. exit:
  983. if (debug_level >= DEBUG_LEVEL_INFO)
  984. printk("%s(%d):%s wait_until_sent() exit\n",
  985. __FILE__,__LINE__, info->device_name );
  986. }
  987. /* Return the count of free bytes in transmit buffer
  988. */
  989. static int write_room(struct tty_struct *tty)
  990. {
  991. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  992. int ret;
  993. if (sanity_check(info, tty->name, "write_room"))
  994. return 0;
  995. if (info->params.mode == MGSL_MODE_HDLC) {
  996. ret = (info->tx_active) ? 0 : HDLC_MAX_FRAME_SIZE;
  997. } else {
  998. ret = info->max_frame_size - info->tx_count - 1;
  999. if (ret < 0)
  1000. ret = 0;
  1001. }
  1002. if (debug_level >= DEBUG_LEVEL_INFO)
  1003. printk("%s(%d):%s write_room()=%d\n",
  1004. __FILE__, __LINE__, info->device_name, ret);
  1005. return ret;
  1006. }
  1007. /* enable transmitter and send remaining buffered characters
  1008. */
  1009. static void flush_chars(struct tty_struct *tty)
  1010. {
  1011. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  1012. unsigned long flags;
  1013. if ( debug_level >= DEBUG_LEVEL_INFO )
  1014. printk( "%s(%d):%s flush_chars() entry tx_count=%d\n",
  1015. __FILE__,__LINE__,info->device_name,info->tx_count);
  1016. if (sanity_check(info, tty->name, "flush_chars"))
  1017. return;
  1018. if (info->tx_count <= 0 || tty->stopped || tty->hw_stopped ||
  1019. !info->tx_buf)
  1020. return;
  1021. if ( debug_level >= DEBUG_LEVEL_INFO )
  1022. printk( "%s(%d):%s flush_chars() entry, starting transmitter\n",
  1023. __FILE__,__LINE__,info->device_name );
  1024. spin_lock_irqsave(&info->lock,flags);
  1025. if (!info->tx_active) {
  1026. if ( (info->params.mode == MGSL_MODE_HDLC) &&
  1027. info->tx_count ) {
  1028. /* operating in synchronous (frame oriented) mode */
  1029. /* copy data from circular tx_buf to */
  1030. /* transmit DMA buffer. */
  1031. tx_load_dma_buffer(info,
  1032. info->tx_buf,info->tx_count);
  1033. }
  1034. tx_start(info);
  1035. }
  1036. spin_unlock_irqrestore(&info->lock,flags);
  1037. }
  1038. /* Discard all data in the send buffer
  1039. */
  1040. static void flush_buffer(struct tty_struct *tty)
  1041. {
  1042. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  1043. unsigned long flags;
  1044. if (debug_level >= DEBUG_LEVEL_INFO)
  1045. printk("%s(%d):%s flush_buffer() entry\n",
  1046. __FILE__,__LINE__, info->device_name );
  1047. if (sanity_check(info, tty->name, "flush_buffer"))
  1048. return;
  1049. spin_lock_irqsave(&info->lock,flags);
  1050. info->tx_count = info->tx_put = info->tx_get = 0;
  1051. del_timer(&info->tx_timer);
  1052. spin_unlock_irqrestore(&info->lock,flags);
  1053. wake_up_interruptible(&tty->write_wait);
  1054. tty_wakeup(tty);
  1055. }
  1056. /* throttle (stop) transmitter
  1057. */
  1058. static void tx_hold(struct tty_struct *tty)
  1059. {
  1060. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  1061. unsigned long flags;
  1062. if (sanity_check(info, tty->name, "tx_hold"))
  1063. return;
  1064. if ( debug_level >= DEBUG_LEVEL_INFO )
  1065. printk("%s(%d):%s tx_hold()\n",
  1066. __FILE__,__LINE__,info->device_name);
  1067. spin_lock_irqsave(&info->lock,flags);
  1068. if (info->tx_enabled)
  1069. tx_stop(info);
  1070. spin_unlock_irqrestore(&info->lock,flags);
  1071. }
  1072. /* release (start) transmitter
  1073. */
  1074. static void tx_release(struct tty_struct *tty)
  1075. {
  1076. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  1077. unsigned long flags;
  1078. if (sanity_check(info, tty->name, "tx_release"))
  1079. return;
  1080. if ( debug_level >= DEBUG_LEVEL_INFO )
  1081. printk("%s(%d):%s tx_release()\n",
  1082. __FILE__,__LINE__,info->device_name);
  1083. spin_lock_irqsave(&info->lock,flags);
  1084. if (!info->tx_enabled)
  1085. tx_start(info);
  1086. spin_unlock_irqrestore(&info->lock,flags);
  1087. }
  1088. /* Service an IOCTL request
  1089. *
  1090. * Arguments:
  1091. *
  1092. * tty pointer to tty instance data
  1093. * file pointer to associated file object for device
  1094. * cmd IOCTL command code
  1095. * arg command argument/context
  1096. *
  1097. * Return Value: 0 if success, otherwise error code
  1098. */
  1099. static int ioctl(struct tty_struct *tty, struct file *file,
  1100. unsigned int cmd, unsigned long arg)
  1101. {
  1102. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  1103. int error;
  1104. struct mgsl_icount cnow; /* kernel counter temps */
  1105. struct serial_icounter_struct __user *p_cuser; /* user space */
  1106. unsigned long flags;
  1107. void __user *argp = (void __user *)arg;
  1108. if (debug_level >= DEBUG_LEVEL_INFO)
  1109. printk("%s(%d):%s ioctl() cmd=%08X\n", __FILE__,__LINE__,
  1110. info->device_name, cmd );
  1111. if (sanity_check(info, tty->name, "ioctl"))
  1112. return -ENODEV;
  1113. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  1114. (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
  1115. if (tty->flags & (1 << TTY_IO_ERROR))
  1116. return -EIO;
  1117. }
  1118. switch (cmd) {
  1119. case MGSL_IOCGPARAMS:
  1120. return get_params(info, argp);
  1121. case MGSL_IOCSPARAMS:
  1122. return set_params(info, argp);
  1123. case MGSL_IOCGTXIDLE:
  1124. return get_txidle(info, argp);
  1125. case MGSL_IOCSTXIDLE:
  1126. return set_txidle(info, (int)arg);
  1127. case MGSL_IOCTXENABLE:
  1128. return tx_enable(info, (int)arg);
  1129. case MGSL_IOCRXENABLE:
  1130. return rx_enable(info, (int)arg);
  1131. case MGSL_IOCTXABORT:
  1132. return tx_abort(info);
  1133. case MGSL_IOCGSTATS:
  1134. return get_stats(info, argp);
  1135. case MGSL_IOCWAITEVENT:
  1136. return wait_mgsl_event(info, argp);
  1137. case MGSL_IOCLOOPTXDONE:
  1138. return 0; // TODO: Not supported, need to document
  1139. /* Wait for modem input (DCD,RI,DSR,CTS) change
  1140. * as specified by mask in arg (TIOCM_RNG/DSR/CD/CTS)
  1141. */
  1142. case TIOCMIWAIT:
  1143. return modem_input_wait(info,(int)arg);
  1144. /*
  1145. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1146. * Return: write counters to the user passed counter struct
  1147. * NB: both 1->0 and 0->1 transitions are counted except for
  1148. * RI where only 0->1 is counted.
  1149. */
  1150. case TIOCGICOUNT:
  1151. spin_lock_irqsave(&info->lock,flags);
  1152. cnow = info->icount;
  1153. spin_unlock_irqrestore(&info->lock,flags);
  1154. p_cuser = argp;
  1155. PUT_USER(error,cnow.cts, &p_cuser->cts);
  1156. if (error) return error;
  1157. PUT_USER(error,cnow.dsr, &p_cuser->dsr);
  1158. if (error) return error;
  1159. PUT_USER(error,cnow.rng, &p_cuser->rng);
  1160. if (error) return error;
  1161. PUT_USER(error,cnow.dcd, &p_cuser->dcd);
  1162. if (error) return error;
  1163. PUT_USER(error,cnow.rx, &p_cuser->rx);
  1164. if (error) return error;
  1165. PUT_USER(error,cnow.tx, &p_cuser->tx);
  1166. if (error) return error;
  1167. PUT_USER(error,cnow.frame, &p_cuser->frame);
  1168. if (error) return error;
  1169. PUT_USER(error,cnow.overrun, &p_cuser->overrun);
  1170. if (error) return error;
  1171. PUT_USER(error,cnow.parity, &p_cuser->parity);
  1172. if (error) return error;
  1173. PUT_USER(error,cnow.brk, &p_cuser->brk);
  1174. if (error) return error;
  1175. PUT_USER(error,cnow.buf_overrun, &p_cuser->buf_overrun);
  1176. if (error) return error;
  1177. return 0;
  1178. default:
  1179. return -ENOIOCTLCMD;
  1180. }
  1181. return 0;
  1182. }
  1183. /*
  1184. * /proc fs routines....
  1185. */
  1186. static inline int line_info(char *buf, SLMP_INFO *info)
  1187. {
  1188. char stat_buf[30];
  1189. int ret;
  1190. unsigned long flags;
  1191. ret = sprintf(buf, "%s: SCABase=%08x Mem=%08X StatusControl=%08x LCR=%08X\n"
  1192. "\tIRQ=%d MaxFrameSize=%u\n",
  1193. info->device_name,
  1194. info->phys_sca_base,
  1195. info->phys_memory_base,
  1196. info->phys_statctrl_base,
  1197. info->phys_lcr_base,
  1198. info->irq_level,
  1199. info->max_frame_size );
  1200. /* output current serial signal states */
  1201. spin_lock_irqsave(&info->lock,flags);
  1202. get_signals(info);
  1203. spin_unlock_irqrestore(&info->lock,flags);
  1204. stat_buf[0] = 0;
  1205. stat_buf[1] = 0;
  1206. if (info->serial_signals & SerialSignal_RTS)
  1207. strcat(stat_buf, "|RTS");
  1208. if (info->serial_signals & SerialSignal_CTS)
  1209. strcat(stat_buf, "|CTS");
  1210. if (info->serial_signals & SerialSignal_DTR)
  1211. strcat(stat_buf, "|DTR");
  1212. if (info->serial_signals & SerialSignal_DSR)
  1213. strcat(stat_buf, "|DSR");
  1214. if (info->serial_signals & SerialSignal_DCD)
  1215. strcat(stat_buf, "|CD");
  1216. if (info->serial_signals & SerialSignal_RI)
  1217. strcat(stat_buf, "|RI");
  1218. if (info->params.mode == MGSL_MODE_HDLC) {
  1219. ret += sprintf(buf+ret, "\tHDLC txok:%d rxok:%d",
  1220. info->icount.txok, info->icount.rxok);
  1221. if (info->icount.txunder)
  1222. ret += sprintf(buf+ret, " txunder:%d", info->icount.txunder);
  1223. if (info->icount.txabort)
  1224. ret += sprintf(buf+ret, " txabort:%d", info->icount.txabort);
  1225. if (info->icount.rxshort)
  1226. ret += sprintf(buf+ret, " rxshort:%d", info->icount.rxshort);
  1227. if (info->icount.rxlong)
  1228. ret += sprintf(buf+ret, " rxlong:%d", info->icount.rxlong);
  1229. if (info->icount.rxover)
  1230. ret += sprintf(buf+ret, " rxover:%d", info->icount.rxover);
  1231. if (info->icount.rxcrc)
  1232. ret += sprintf(buf+ret, " rxlong:%d", info->icount.rxcrc);
  1233. } else {
  1234. ret += sprintf(buf+ret, "\tASYNC tx:%d rx:%d",
  1235. info->icount.tx, info->icount.rx);
  1236. if (info->icount.frame)
  1237. ret += sprintf(buf+ret, " fe:%d", info->icount.frame);
  1238. if (info->icount.parity)
  1239. ret += sprintf(buf+ret, " pe:%d", info->icount.parity);
  1240. if (info->icount.brk)
  1241. ret += sprintf(buf+ret, " brk:%d", info->icount.brk);
  1242. if (info->icount.overrun)
  1243. ret += sprintf(buf+ret, " oe:%d", info->icount.overrun);
  1244. }
  1245. /* Append serial signal status to end */
  1246. ret += sprintf(buf+ret, " %s\n", stat_buf+1);
  1247. ret += sprintf(buf+ret, "\ttxactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
  1248. info->tx_active,info->bh_requested,info->bh_running,
  1249. info->pending_bh);
  1250. return ret;
  1251. }
  1252. /* Called to print information about devices
  1253. */
  1254. int read_proc(char *page, char **start, off_t off, int count,
  1255. int *eof, void *data)
  1256. {
  1257. int len = 0, l;
  1258. off_t begin = 0;
  1259. SLMP_INFO *info;
  1260. len += sprintf(page, "synclinkmp driver:%s\n", driver_version);
  1261. info = synclinkmp_device_list;
  1262. while( info ) {
  1263. l = line_info(page + len, info);
  1264. len += l;
  1265. if (len+begin > off+count)
  1266. goto done;
  1267. if (len+begin < off) {
  1268. begin += len;
  1269. len = 0;
  1270. }
  1271. info = info->next_device;
  1272. }
  1273. *eof = 1;
  1274. done:
  1275. if (off >= len+begin)
  1276. return 0;
  1277. *start = page + (off-begin);
  1278. return ((count < begin+len-off) ? count : begin+len-off);
  1279. }
  1280. /* Return the count of bytes in transmit buffer
  1281. */
  1282. static int chars_in_buffer(struct tty_struct *tty)
  1283. {
  1284. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  1285. if (sanity_check(info, tty->name, "chars_in_buffer"))
  1286. return 0;
  1287. if (debug_level >= DEBUG_LEVEL_INFO)
  1288. printk("%s(%d):%s chars_in_buffer()=%d\n",
  1289. __FILE__, __LINE__, info->device_name, info->tx_count);
  1290. return info->tx_count;
  1291. }
  1292. /* Signal remote device to throttle send data (our receive data)
  1293. */
  1294. static void throttle(struct tty_struct * tty)
  1295. {
  1296. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  1297. unsigned long flags;
  1298. if (debug_level >= DEBUG_LEVEL_INFO)
  1299. printk("%s(%d):%s throttle() entry\n",
  1300. __FILE__,__LINE__, info->device_name );
  1301. if (sanity_check(info, tty->name, "throttle"))
  1302. return;
  1303. if (I_IXOFF(tty))
  1304. send_xchar(tty, STOP_CHAR(tty));
  1305. if (tty->termios->c_cflag & CRTSCTS) {
  1306. spin_lock_irqsave(&info->lock,flags);
  1307. info->serial_signals &= ~SerialSignal_RTS;
  1308. set_signals(info);
  1309. spin_unlock_irqrestore(&info->lock,flags);
  1310. }
  1311. }
  1312. /* Signal remote device to stop throttling send data (our receive data)
  1313. */
  1314. static void unthrottle(struct tty_struct * tty)
  1315. {
  1316. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  1317. unsigned long flags;
  1318. if (debug_level >= DEBUG_LEVEL_INFO)
  1319. printk("%s(%d):%s unthrottle() entry\n",
  1320. __FILE__,__LINE__, info->device_name );
  1321. if (sanity_check(info, tty->name, "unthrottle"))
  1322. return;
  1323. if (I_IXOFF(tty)) {
  1324. if (info->x_char)
  1325. info->x_char = 0;
  1326. else
  1327. send_xchar(tty, START_CHAR(tty));
  1328. }
  1329. if (tty->termios->c_cflag & CRTSCTS) {
  1330. spin_lock_irqsave(&info->lock,flags);
  1331. info->serial_signals |= SerialSignal_RTS;
  1332. set_signals(info);
  1333. spin_unlock_irqrestore(&info->lock,flags);
  1334. }
  1335. }
  1336. /* set or clear transmit break condition
  1337. * break_state -1=set break condition, 0=clear
  1338. */
  1339. static void set_break(struct tty_struct *tty, int break_state)
  1340. {
  1341. unsigned char RegValue;
  1342. SLMP_INFO * info = (SLMP_INFO *)tty->driver_data;
  1343. unsigned long flags;
  1344. if (debug_level >= DEBUG_LEVEL_INFO)
  1345. printk("%s(%d):%s set_break(%d)\n",
  1346. __FILE__,__LINE__, info->device_name, break_state);
  1347. if (sanity_check(info, tty->name, "set_break"))
  1348. return;
  1349. spin_lock_irqsave(&info->lock,flags);
  1350. RegValue = read_reg(info, CTL);
  1351. if (break_state == -1)
  1352. RegValue |= BIT3;
  1353. else
  1354. RegValue &= ~BIT3;
  1355. write_reg(info, CTL, RegValue);
  1356. spin_unlock_irqrestore(&info->lock,flags);
  1357. }
  1358. #ifdef CONFIG_HDLC
  1359. /**
  1360. * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
  1361. * set encoding and frame check sequence (FCS) options
  1362. *
  1363. * dev pointer to network device structure
  1364. * encoding serial encoding setting
  1365. * parity FCS setting
  1366. *
  1367. * returns 0 if success, otherwise error code
  1368. */
  1369. static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
  1370. unsigned short parity)
  1371. {
  1372. SLMP_INFO *info = dev_to_port(dev);
  1373. unsigned char new_encoding;
  1374. unsigned short new_crctype;
  1375. /* return error if TTY interface open */
  1376. if (info->count)
  1377. return -EBUSY;
  1378. switch (encoding)
  1379. {
  1380. case ENCODING_NRZ: new_encoding = HDLC_ENCODING_NRZ; break;
  1381. case ENCODING_NRZI: new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
  1382. case ENCODING_FM_MARK: new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
  1383. case ENCODING_FM_SPACE: new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
  1384. case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
  1385. default: return -EINVAL;
  1386. }
  1387. switch (parity)
  1388. {
  1389. case PARITY_NONE: new_crctype = HDLC_CRC_NONE; break;
  1390. case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
  1391. case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
  1392. default: return -EINVAL;
  1393. }
  1394. info->params.encoding = new_encoding;
  1395. info->params.crc_type = new_crctype;;
  1396. /* if network interface up, reprogram hardware */
  1397. if (info->netcount)
  1398. program_hw(info);
  1399. return 0;
  1400. }
  1401. /**
  1402. * called by generic HDLC layer to send frame
  1403. *
  1404. * skb socket buffer containing HDLC frame
  1405. * dev pointer to network device structure
  1406. *
  1407. * returns 0 if success, otherwise error code
  1408. */
  1409. static int hdlcdev_xmit(struct sk_buff *skb, struct net_device *dev)
  1410. {
  1411. SLMP_INFO *info = dev_to_port(dev);
  1412. struct net_device_stats *stats = hdlc_stats(dev);
  1413. unsigned long flags;
  1414. if (debug_level >= DEBUG_LEVEL_INFO)
  1415. printk(KERN_INFO "%s:hdlc_xmit(%s)\n",__FILE__,dev->name);
  1416. /* stop sending until this frame completes */
  1417. netif_stop_queue(dev);
  1418. /* copy data to device buffers */
  1419. info->tx_count = skb->len;
  1420. tx_load_dma_buffer(info, skb->data, skb->len);
  1421. /* update network statistics */
  1422. stats->tx_packets++;
  1423. stats->tx_bytes += skb->len;
  1424. /* done with socket buffer, so free it */
  1425. dev_kfree_skb(skb);
  1426. /* save start time for transmit timeout detection */
  1427. dev->trans_start = jiffies;
  1428. /* start hardware transmitter if necessary */
  1429. spin_lock_irqsave(&info->lock,flags);
  1430. if (!info->tx_active)
  1431. tx_start(info);
  1432. spin_unlock_irqrestore(&info->lock,flags);
  1433. return 0;
  1434. }
  1435. /**
  1436. * called by network layer when interface enabled
  1437. * claim resources and initialize hardware
  1438. *
  1439. * dev pointer to network device structure
  1440. *
  1441. * returns 0 if success, otherwise error code
  1442. */
  1443. static int hdlcdev_open(struct net_device *dev)
  1444. {
  1445. SLMP_INFO *info = dev_to_port(dev);
  1446. int rc;
  1447. unsigned long flags;
  1448. if (debug_level >= DEBUG_LEVEL_INFO)
  1449. printk("%s:hdlcdev_open(%s)\n",__FILE__,dev->name);
  1450. /* generic HDLC layer open processing */
  1451. if ((rc = hdlc_open(dev)))
  1452. return rc;
  1453. /* arbitrate between network and tty opens */
  1454. spin_lock_irqsave(&info->netlock, flags);
  1455. if (info->count != 0 || info->netcount != 0) {
  1456. printk(KERN_WARNING "%s: hdlc_open returning busy\n", dev->name);
  1457. spin_unlock_irqrestore(&info->netlock, flags);
  1458. return -EBUSY;
  1459. }
  1460. info->netcount=1;
  1461. spin_unlock_irqrestore(&info->netlock, flags);
  1462. /* claim resources and init adapter */
  1463. if ((rc = startup(info)) != 0) {
  1464. spin_lock_irqsave(&info->netlock, flags);
  1465. info->netcount=0;
  1466. spin_unlock_irqrestore(&info->netlock, flags);
  1467. return rc;
  1468. }
  1469. /* assert DTR and RTS, apply hardware settings */
  1470. info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
  1471. program_hw(info);
  1472. /* enable network layer transmit */
  1473. dev->trans_start = jiffies;
  1474. netif_start_queue(dev);
  1475. /* inform generic HDLC layer of current DCD status */
  1476. spin_lock_irqsave(&info->lock, flags);
  1477. get_signals(info);
  1478. spin_unlock_irqrestore(&info->lock, flags);
  1479. hdlc_set_carrier(info->serial_signals & SerialSignal_DCD, dev);
  1480. return 0;
  1481. }
  1482. /**
  1483. * called by network layer when interface is disabled
  1484. * shutdown hardware and release resources
  1485. *
  1486. * dev pointer to network device structure
  1487. *
  1488. * returns 0 if success, otherwise error code
  1489. */
  1490. static int hdlcdev_close(struct net_device *dev)
  1491. {
  1492. SLMP_INFO *info = dev_to_port(dev);
  1493. unsigned long flags;
  1494. if (debug_level >= DEBUG_LEVEL_INFO)
  1495. printk("%s:hdlcdev_close(%s)\n",__FILE__,dev->name);
  1496. netif_stop_queue(dev);
  1497. /* shutdown adapter and release resources */
  1498. shutdown(info);
  1499. hdlc_close(dev);
  1500. spin_lock_irqsave(&info->netlock, flags);
  1501. info->netcount=0;
  1502. spin_unlock_irqrestore(&info->netlock, flags);
  1503. return 0;
  1504. }
  1505. /**
  1506. * called by network layer to process IOCTL call to network device
  1507. *
  1508. * dev pointer to network device structure
  1509. * ifr pointer to network interface request structure
  1510. * cmd IOCTL command code
  1511. *
  1512. * returns 0 if success, otherwise error code
  1513. */
  1514. static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1515. {
  1516. const size_t size = sizeof(sync_serial_settings);
  1517. sync_serial_settings new_line;
  1518. sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
  1519. SLMP_INFO *info = dev_to_port(dev);
  1520. unsigned int flags;
  1521. if (debug_level >= DEBUG_LEVEL_INFO)
  1522. printk("%s:hdlcdev_ioctl(%s)\n",__FILE__,dev->name);
  1523. /* return error if TTY interface open */
  1524. if (info->count)
  1525. return -EBUSY;
  1526. if (cmd != SIOCWANDEV)
  1527. return hdlc_ioctl(dev, ifr, cmd);
  1528. switch(ifr->ifr_settings.type) {
  1529. case IF_GET_IFACE: /* return current sync_serial_settings */
  1530. ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
  1531. if (ifr->ifr_settings.size < size) {
  1532. ifr->ifr_settings.size = size; /* data size wanted */
  1533. return -ENOBUFS;
  1534. }
  1535. flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
  1536. HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
  1537. HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
  1538. HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
  1539. switch (flags){
  1540. case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
  1541. case (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_INT; break;
  1542. case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_TXINT; break;
  1543. case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
  1544. default: new_line.clock_type = CLOCK_DEFAULT;
  1545. }
  1546. new_line.clock_rate = info->params.clock_speed;
  1547. new_line.loopback = info->params.loopback ? 1:0;
  1548. if (copy_to_user(line, &new_line, size))
  1549. return -EFAULT;
  1550. return 0;
  1551. case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
  1552. if(!capable(CAP_NET_ADMIN))
  1553. return -EPERM;
  1554. if (copy_from_user(&new_line, line, size))
  1555. return -EFAULT;
  1556. switch (new_line.clock_type)
  1557. {
  1558. case CLOCK_EXT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
  1559. case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
  1560. case CLOCK_INT: flags = HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG; break;
  1561. case CLOCK_TXINT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG; break;
  1562. case CLOCK_DEFAULT: flags = info->params.flags &
  1563. (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
  1564. HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
  1565. HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
  1566. HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN); break;
  1567. default: return -EINVAL;
  1568. }
  1569. if (new_line.loopback != 0 && new_line.loopback != 1)
  1570. return -EINVAL;
  1571. info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
  1572. HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
  1573. HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
  1574. HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
  1575. info->params.flags |= flags;
  1576. info->params.loopback = new_line.loopback;
  1577. if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
  1578. info->params.clock_speed = new_line.clock_rate;
  1579. else
  1580. info->params.clock_speed = 0;
  1581. /* if network interface up, reprogram hardware */
  1582. if (info->netcount)
  1583. program_hw(info);
  1584. return 0;
  1585. default:
  1586. return hdlc_ioctl(dev, ifr, cmd);
  1587. }
  1588. }
  1589. /**
  1590. * called by network layer when transmit timeout is detected
  1591. *
  1592. * dev pointer to network device structure
  1593. */
  1594. static void hdlcdev_tx_timeout(struct net_device *dev)
  1595. {
  1596. SLMP_INFO *info = dev_to_port(dev);
  1597. struct net_device_stats *stats = hdlc_stats(dev);
  1598. unsigned long flags;
  1599. if (debug_level >= DEBUG_LEVEL_INFO)
  1600. printk("hdlcdev_tx_timeout(%s)\n",dev->name);
  1601. stats->tx_errors++;
  1602. stats->tx_aborted_errors++;
  1603. spin_lock_irqsave(&info->lock,flags);
  1604. tx_stop(info);
  1605. spin_unlock_irqrestore(&info->lock,flags);
  1606. netif_wake_queue(dev);
  1607. }
  1608. /**
  1609. * called by device driver when transmit completes
  1610. * reenable network layer transmit if stopped
  1611. *
  1612. * info pointer to device instance information
  1613. */
  1614. static void hdlcdev_tx_done(SLMP_INFO *info)
  1615. {
  1616. if (netif_queue_stopped(info->netdev))
  1617. netif_wake_queue(info->netdev);
  1618. }
  1619. /**
  1620. * called by device driver when frame received
  1621. * pass frame to network layer
  1622. *
  1623. * info pointer to device instance information
  1624. * buf pointer to buffer contianing frame data
  1625. * size count of data bytes in buf
  1626. */
  1627. static void hdlcdev_rx(SLMP_INFO *info, char *buf, int size)
  1628. {
  1629. struct sk_buff *skb = dev_alloc_skb(size);
  1630. struct net_device *dev = info->netdev;
  1631. struct net_device_stats *stats = hdlc_stats(dev);
  1632. if (debug_level >= DEBUG_LEVEL_INFO)
  1633. printk("hdlcdev_rx(%s)\n",dev->name);
  1634. if (skb == NULL) {
  1635. printk(KERN_NOTICE "%s: can't alloc skb, dropping packet\n", dev->name);
  1636. stats->rx_dropped++;
  1637. return;
  1638. }
  1639. memcpy(skb_put(skb, size),buf,size);
  1640. skb->protocol = hdlc_type_trans(skb, info->netdev);
  1641. stats->rx_packets++;
  1642. stats->rx_bytes += size;
  1643. netif_rx(skb);
  1644. info->netdev->last_rx = jiffies;
  1645. }
  1646. /**
  1647. * called by device driver when adding device instance
  1648. * do generic HDLC initialization
  1649. *
  1650. * info pointer to device instance information
  1651. *
  1652. * returns 0 if success, otherwise error code
  1653. */
  1654. static int hdlcdev_init(SLMP_INFO *info)
  1655. {
  1656. int rc;
  1657. struct net_device *dev;
  1658. hdlc_device *hdlc;
  1659. /* allocate and initialize network and HDLC layer objects */
  1660. if (!(dev = alloc_hdlcdev(info))) {
  1661. printk(KERN_ERR "%s:hdlc device allocation failure\n",__FILE__);
  1662. return -ENOMEM;
  1663. }
  1664. /* for network layer reporting purposes only */
  1665. dev->mem_start = info->phys_sca_base;
  1666. dev->mem_end = info->phys_sca_base + SCA_BASE_SIZE - 1;
  1667. dev->irq = info->irq_level;
  1668. /* network layer callbacks and settings */
  1669. dev->do_ioctl = hdlcdev_ioctl;
  1670. dev->open = hdlcdev_open;
  1671. dev->stop = hdlcdev_close;
  1672. dev->tx_timeout = hdlcdev_tx_timeout;
  1673. dev->watchdog_timeo = 10*HZ;
  1674. dev->tx_queue_len = 50;
  1675. /* generic HDLC layer callbacks and settings */
  1676. hdlc = dev_to_hdlc(dev);
  1677. hdlc->attach = hdlcdev_attach;
  1678. hdlc->xmit = hdlcdev_xmit;
  1679. /* register objects with HDLC layer */
  1680. if ((rc = register_hdlc_device(dev))) {
  1681. printk(KERN_WARNING "%s:unable to register hdlc device\n",__FILE__);
  1682. free_netdev(dev);
  1683. return rc;
  1684. }
  1685. info->netdev = dev;
  1686. return 0;
  1687. }
  1688. /**
  1689. * called by device driver when removing device instance
  1690. * do generic HDLC cleanup
  1691. *
  1692. * info pointer to device instance information
  1693. */
  1694. static void hdlcdev_exit(SLMP_INFO *info)
  1695. {
  1696. unregister_hdlc_device(info->netdev);
  1697. free_netdev(info->netdev);
  1698. info->netdev = NULL;
  1699. }
  1700. #endif /* CONFIG_HDLC */
  1701. /* Return next bottom half action to perform.
  1702. * Return Value: BH action code or 0 if nothing to do.
  1703. */
  1704. int bh_action(SLMP_INFO *info)
  1705. {
  1706. unsigned long flags;
  1707. int rc = 0;
  1708. spin_lock_irqsave(&info->lock,flags);
  1709. if (info->pending_bh & BH_RECEIVE) {
  1710. info->pending_bh &= ~BH_RECEIVE;
  1711. rc = BH_RECEIVE;
  1712. } else if (info->pending_bh & BH_TRANSMIT) {
  1713. info->pending_bh &= ~BH_TRANSMIT;
  1714. rc = BH_TRANSMIT;
  1715. } else if (info->pending_bh & BH_STATUS) {
  1716. info->pending_bh &= ~BH_STATUS;
  1717. rc = BH_STATUS;
  1718. }
  1719. if (!rc) {
  1720. /* Mark BH routine as complete */
  1721. info->bh_running = 0;
  1722. info->bh_requested = 0;
  1723. }
  1724. spin_unlock_irqrestore(&info->lock,flags);
  1725. return rc;
  1726. }
  1727. /* Perform bottom half processing of work items queued by ISR.
  1728. */
  1729. void bh_handler(void* Context)
  1730. {
  1731. SLMP_INFO *info = (SLMP_INFO*)Context;
  1732. int action;
  1733. if (!info)
  1734. return;
  1735. if ( debug_level >= DEBUG_LEVEL_BH )
  1736. printk( "%s(%d):%s bh_handler() entry\n",
  1737. __FILE__,__LINE__,info->device_name);
  1738. info->bh_running = 1;
  1739. while((action = bh_action(info)) != 0) {
  1740. /* Process work item */
  1741. if ( debug_level >= DEBUG_LEVEL_BH )
  1742. printk( "%s(%d):%s bh_handler() work item action=%d\n",
  1743. __FILE__,__LINE__,info->device_name, action);
  1744. switch (action) {
  1745. case BH_RECEIVE:
  1746. bh_receive(info);
  1747. break;
  1748. case BH_TRANSMIT:
  1749. bh_transmit(info);
  1750. break;
  1751. case BH_STATUS:
  1752. bh_status(info);
  1753. break;
  1754. default:
  1755. /* unknown work item ID */
  1756. printk("%s(%d):%s Unknown work item ID=%08X!\n",
  1757. __FILE__,__LINE__,info->device_name,action);
  1758. break;
  1759. }
  1760. }
  1761. if ( debug_level >= DEBUG_LEVEL_BH )
  1762. printk( "%s(%d):%s bh_handler() exit\n",
  1763. __FILE__,__LINE__,info->device_name);
  1764. }
  1765. void bh_receive(SLMP_INFO *info)
  1766. {
  1767. if ( debug_level >= DEBUG_LEVEL_BH )
  1768. printk( "%s(%d):%s bh_receive()\n",
  1769. __FILE__,__LINE__,info->device_name);
  1770. while( rx_get_frame(info) );
  1771. }
  1772. void bh_transmit(SLMP_INFO *info)
  1773. {
  1774. struct tty_struct *tty = info->tty;
  1775. if ( debug_level >= DEBUG_LEVEL_BH )
  1776. printk( "%s(%d):%s bh_transmit() entry\n",
  1777. __FILE__,__LINE__,info->device_name);
  1778. if (tty) {
  1779. tty_wakeup(tty);
  1780. wake_up_interruptible(&tty->write_wait);
  1781. }
  1782. }
  1783. void bh_status(SLMP_INFO *info)
  1784. {
  1785. if ( debug_level >= DEBUG_LEVEL_BH )
  1786. printk( "%s(%d):%s bh_status() entry\n",
  1787. __FILE__,__LINE__,info->device_name);
  1788. info->ri_chkcount = 0;
  1789. info->dsr_chkcount = 0;
  1790. info->dcd_chkcount = 0;
  1791. info->cts_chkcount = 0;
  1792. }
  1793. void isr_timer(SLMP_INFO * info)
  1794. {
  1795. unsigned char timer = (info->port_num & 1) ? TIMER2 : TIMER0;
  1796. /* IER2<7..4> = timer<3..0> interrupt enables (0=disabled) */
  1797. write_reg(info, IER2, 0);
  1798. /* TMCS, Timer Control/Status Register
  1799. *
  1800. * 07 CMF, Compare match flag (read only) 1=match
  1801. * 06 ECMI, CMF Interrupt Enable: 0=disabled
  1802. * 05 Reserved, must be 0
  1803. * 04 TME, Timer Enable
  1804. * 03..00 Reserved, must be 0
  1805. *
  1806. * 0000 0000
  1807. */
  1808. write_reg(info, (unsigned char)(timer + TMCS), 0);
  1809. info->irq_occurred = TRUE;
  1810. if ( debug_level >= DEBUG_LEVEL_ISR )
  1811. printk("%s(%d):%s isr_timer()\n",
  1812. __FILE__,__LINE__,info->device_name);
  1813. }
  1814. void isr_rxint(SLMP_INFO * info)
  1815. {
  1816. struct tty_struct *tty = info->tty;
  1817. struct mgsl_icount *icount = &info->icount;
  1818. unsigned char status = read_reg(info, SR1) & info->ie1_value & (FLGD + IDLD + CDCD + BRKD);
  1819. unsigned char status2 = read_reg(info, SR2) & info->ie2_value & OVRN;
  1820. /* clear status bits */
  1821. if (status)
  1822. write_reg(info, SR1, status);
  1823. if (status2)
  1824. write_reg(info, SR2, status2);
  1825. if ( debug_level >= DEBUG_LEVEL_ISR )
  1826. printk("%s(%d):%s isr_rxint status=%02X %02x\n",
  1827. __FILE__,__LINE__,info->device_name,status,status2);
  1828. if (info->params.mode == MGSL_MODE_ASYNC) {
  1829. if (status & BRKD) {
  1830. icount->brk++;
  1831. /* process break detection if tty control
  1832. * is not set to ignore it
  1833. */
  1834. if ( tty ) {
  1835. if (!(status & info->ignore_status_mask1)) {
  1836. if (info->read_status_mask1 & BRKD) {
  1837. *tty->flip.flag_buf_ptr = TTY_BREAK;
  1838. if (info->flags & ASYNC_SAK)
  1839. do_SAK(tty);
  1840. }
  1841. }
  1842. }
  1843. }
  1844. }
  1845. else {
  1846. if (status & (FLGD|IDLD)) {
  1847. if (status & FLGD)
  1848. info->icount.exithunt++;
  1849. else if (status & IDLD)
  1850. info->icount.rxidle++;
  1851. wake_up_interruptible(&info->event_wait_q);
  1852. }
  1853. }
  1854. if (status & CDCD) {
  1855. /* simulate a common modem status change interrupt
  1856. * for our handler
  1857. */
  1858. get_signals( info );
  1859. isr_io_pin(info,
  1860. MISCSTATUS_DCD_LATCHED|(info->serial_signals&SerialSignal_DCD));
  1861. }
  1862. }
  1863. /*
  1864. * handle async rx data interrupts
  1865. */
  1866. void isr_rxrdy(SLMP_INFO * info)
  1867. {
  1868. u16 status;
  1869. unsigned char DataByte;
  1870. struct tty_struct *tty = info->tty;
  1871. struct mgsl_icount *icount = &info->icount;
  1872. if ( debug_level >= DEBUG_LEVEL_ISR )
  1873. printk("%s(%d):%s isr_rxrdy\n",
  1874. __FILE__,__LINE__,info->device_name);
  1875. while((status = read_reg(info,CST0)) & BIT0)
  1876. {
  1877. DataByte = read_reg(info,TRB);
  1878. if ( tty ) {
  1879. if (tty->flip.count >= TTY_FLIPBUF_SIZE)
  1880. continue;
  1881. *tty->flip.char_buf_ptr = DataByte;
  1882. *tty->flip.flag_buf_ptr = 0;
  1883. }
  1884. icount->rx++;
  1885. if ( status & (PE + FRME + OVRN) ) {
  1886. printk("%s(%d):%s rxerr=%04X\n",
  1887. __FILE__,__LINE__,info->device_name,status);
  1888. /* update error statistics */
  1889. if (status & PE)
  1890. icount->parity++;
  1891. else if (status & FRME)
  1892. icount->frame++;
  1893. else if (status & OVRN)
  1894. icount->overrun++;
  1895. /* discard char if tty control flags say so */
  1896. if (status & info->ignore_status_mask2)
  1897. continue;
  1898. status &= info->read_status_mask2;
  1899. if ( tty ) {
  1900. if (status & PE)
  1901. *tty->flip.flag_buf_ptr = TTY_PARITY;
  1902. else if (status & FRME)
  1903. *tty->flip.flag_buf_ptr = TTY_FRAME;
  1904. if (status & OVRN) {
  1905. /* Overrun is special, since it's
  1906. * reported immediately, and doesn't
  1907. * affect the current character
  1908. */
  1909. if (tty->flip.count < TTY_FLIPBUF_SIZE) {
  1910. tty->flip.count++;
  1911. tty->flip.flag_buf_ptr++;
  1912. tty->flip.char_buf_ptr++;
  1913. *tty->flip.flag_buf_ptr = TTY_OVERRUN;
  1914. }
  1915. }
  1916. }
  1917. } /* end of if (error) */
  1918. if ( tty ) {
  1919. tty->flip.flag_buf_ptr++;
  1920. tty->flip.char_buf_ptr++;
  1921. tty->flip.count++;
  1922. }
  1923. }
  1924. if ( debug_level >= DEBUG_LEVEL_ISR ) {
  1925. printk("%s(%d):%s isr_rxrdy() flip count=%d\n",
  1926. __FILE__,__LINE__,info->device_name,
  1927. tty ? tty->flip.count : 0);
  1928. printk("%s(%d):%s rx=%d brk=%d parity=%d frame=%d overrun=%d\n",
  1929. __FILE__,__LINE__,info->device_name,
  1930. icount->rx,icount->brk,icount->parity,
  1931. icount->frame,icount->overrun);
  1932. }
  1933. if ( tty && tty->flip.count )
  1934. tty_flip_buffer_push(tty);
  1935. }
  1936. static void isr_txeom(SLMP_INFO * info, unsigned char status)
  1937. {
  1938. if ( debug_level >= DEBUG_LEVEL_ISR )
  1939. printk("%s(%d):%s isr_txeom status=%02x\n",
  1940. __FILE__,__LINE__,info->device_name,status);
  1941. write_reg(info, TXDMA + DIR, 0x00); /* disable Tx DMA IRQs */
  1942. write_reg(info, TXDMA + DSR, 0xc0); /* clear IRQs and disable DMA */
  1943. write_reg(info, TXDMA + DCMD, SWABORT); /* reset/init DMA channel */
  1944. if (status & UDRN) {
  1945. write_reg(info, CMD, TXRESET);
  1946. write_reg(info, CMD, TXENABLE);
  1947. } else
  1948. write_reg(info, CMD, TXBUFCLR);
  1949. /* disable and clear tx interrupts */
  1950. info->ie0_value &= ~TXRDYE;
  1951. info->ie1_value &= ~(IDLE + UDRN);
  1952. write_reg16(info, IE0, (unsigned short)((info->ie1_value << 8) + info->ie0_value));
  1953. write_reg(info, SR1, (unsigned char)(UDRN + IDLE));
  1954. if ( info->tx_active ) {
  1955. if (info->params.mode != MGSL_MODE_ASYNC) {
  1956. if (status & UDRN)
  1957. info->icount.txunder++;
  1958. else if (status & IDLE)
  1959. info->icount.txok++;
  1960. }
  1961. info->tx_active = 0;
  1962. info->tx_count = info->tx_put = info->tx_get = 0;
  1963. del_timer(&info->tx_timer);
  1964. if (info->params.mode != MGSL_MODE_ASYNC && info->drop_rts_on_tx_done ) {
  1965. info->serial_signals &= ~SerialSignal_RTS;
  1966. info->drop_rts_on_tx_done = 0;
  1967. set_signals(info);
  1968. }
  1969. #ifdef CONFIG_HDLC
  1970. if (info->netcount)
  1971. hdlcdev_tx_done(info);
  1972. else
  1973. #endif
  1974. {
  1975. if (info->tty && (info->tty->stopped || info->tty->hw_stopped)) {
  1976. tx_stop(info);
  1977. return;
  1978. }
  1979. info->pending_bh |= BH_TRANSMIT;
  1980. }
  1981. }
  1982. }
  1983. /*
  1984. * handle tx status interrupts
  1985. */
  1986. void isr_txint(SLMP_INFO * info)
  1987. {
  1988. unsigned char status = read_reg(info, SR1) & info->ie1_value & (UDRN + IDLE + CCTS);
  1989. /* clear status bits */
  1990. write_reg(info, SR1, status);
  1991. if ( debug_level >= DEBUG_LEVEL_ISR )
  1992. printk("%s(%d):%s isr_txint status=%02x\n",
  1993. __FILE__,__LINE__,info->device_name,status);
  1994. if (status & (UDRN + IDLE))
  1995. isr_txeom(info, status);
  1996. if (status & CCTS) {
  1997. /* simulate a common modem status change interrupt
  1998. * for our handler
  1999. */
  2000. get_signals( info );
  2001. isr_io_pin(info,
  2002. MISCSTATUS_CTS_LATCHED|(info->serial_signals&SerialSignal_CTS));
  2003. }
  2004. }
  2005. /*
  2006. * handle async tx data interrupts
  2007. */
  2008. void isr_txrdy(SLMP_INFO * info)
  2009. {
  2010. if ( debug_level >= DEBUG_LEVEL_ISR )
  2011. printk("%s(%d):%s isr_txrdy() tx_count=%d\n",
  2012. __FILE__,__LINE__,info->device_name,info->tx_count);
  2013. if (info->params.mode != MGSL_MODE_ASYNC) {
  2014. /* disable TXRDY IRQ, enable IDLE IRQ */
  2015. info->ie0_value &= ~TXRDYE;
  2016. info->ie1_value |= IDLE;
  2017. write_reg16(info, IE0, (unsigned short)((info->ie1_value << 8) + info->ie0_value));
  2018. return;
  2019. }
  2020. if (info->tty && (info->tty->stopped || info->tty->hw_stopped)) {
  2021. tx_stop(info);
  2022. return;
  2023. }
  2024. if ( info->tx_count )
  2025. tx_load_fifo( info );
  2026. else {
  2027. info->tx_active = 0;
  2028. info->ie0_value &= ~TXRDYE;
  2029. write_reg(info, IE0, info->ie0_value);
  2030. }
  2031. if (info->tx_count < WAKEUP_CHARS)
  2032. info->pending_bh |= BH_TRANSMIT;
  2033. }
  2034. void isr_rxdmaok(SLMP_INFO * info)
  2035. {
  2036. /* BIT7 = EOT (end of transfer)
  2037. * BIT6 = EOM (end of message/frame)
  2038. */
  2039. unsigned char status = read_reg(info,RXDMA + DSR) & 0xc0;
  2040. /* clear IRQ (BIT0 must be 1 to prevent clearing DE bit) */
  2041. write_reg(info, RXDMA + DSR, (unsigned char)(status | 1));
  2042. if ( debug_level >= DEBUG_LEVEL_ISR )
  2043. printk("%s(%d):%s isr_rxdmaok(), status=%02x\n",
  2044. __FILE__,__LINE__,info->device_name,status);
  2045. info->pending_bh |= BH_RECEIVE;
  2046. }
  2047. void isr_rxdmaerror(SLMP_INFO * info)
  2048. {
  2049. /* BIT5 = BOF (buffer overflow)
  2050. * BIT4 = COF (counter overflow)
  2051. */
  2052. unsigned char status = read_reg(info,RXDMA + DSR) & 0x30;
  2053. /* clear IRQ (BIT0 must be 1 to prevent clearing DE bit) */
  2054. write_reg(info, RXDMA + DSR, (unsigned char)(status | 1));
  2055. if ( debug_level >= DEBUG_LEVEL_ISR )
  2056. printk("%s(%d):%s isr_rxdmaerror(), status=%02x\n",
  2057. __FILE__,__LINE__,info->device_name,status);
  2058. info->rx_overflow = TRUE;
  2059. info->pending_bh |= BH_RECEIVE;
  2060. }
  2061. void isr_txdmaok(SLMP_INFO * info)
  2062. {
  2063. unsigned char status_reg1 = read_reg(info, SR1);
  2064. write_reg(info, TXDMA + DIR, 0x00); /* disable Tx DMA IRQs */
  2065. write_reg(info, TXDMA + DSR, 0xc0); /* clear IRQs and disable DMA */
  2066. write_reg(info, TXDMA + DCMD, SWABORT); /* reset/init DMA channel */
  2067. if ( debug_level >= DEBUG_LEVEL_ISR )
  2068. printk("%s(%d):%s isr_txdmaok(), status=%02x\n",
  2069. __FILE__,__LINE__,info->device_name,status_reg1);
  2070. /* program TXRDY as FIFO empty flag, enable TXRDY IRQ */
  2071. write_reg16(info, TRC0, 0);
  2072. info->ie0_value |= TXRDYE;
  2073. write_reg(info, IE0, info->ie0_value);
  2074. }
  2075. void isr_txdmaerror(SLMP_INFO * info)
  2076. {
  2077. /* BIT5 = BOF (buffer overflow)
  2078. * BIT4 = COF (counter overflow)
  2079. */
  2080. unsigned char status = read_reg(info,TXDMA + DSR) & 0x30;
  2081. /* clear IRQ (BIT0 must be 1 to prevent clearing DE bit) */
  2082. write_reg(info, TXDMA + DSR, (unsigned char)(status | 1));
  2083. if ( debug_level >= DEBUG_LEVEL_ISR )
  2084. printk("%s(%d):%s isr_txdmaerror(), status=%02x\n",
  2085. __FILE__,__LINE__,info->device_name,status);
  2086. }
  2087. /* handle input serial signal changes
  2088. */
  2089. void isr_io_pin( SLMP_INFO *info, u16 status )
  2090. {
  2091. struct mgsl_icount *icount;
  2092. if ( debug_level >= DEBUG_LEVEL_ISR )
  2093. printk("%s(%d):isr_io_pin status=%04X\n",
  2094. __FILE__,__LINE__,status);
  2095. if (status & (MISCSTATUS_CTS_LATCHED | MISCSTATUS_DCD_LATCHED |
  2096. MISCSTATUS_DSR_LATCHED | MISCSTATUS_RI_LATCHED) ) {
  2097. icount = &info->icount;
  2098. /* update input line counters */
  2099. if (status & MISCSTATUS_RI_LATCHED) {
  2100. icount->rng++;
  2101. if ( status & SerialSignal_RI )
  2102. info->input_signal_events.ri_up++;
  2103. else
  2104. info->input_signal_events.ri_down++;
  2105. }
  2106. if (status & MISCSTATUS_DSR_LATCHED) {
  2107. icount->dsr++;
  2108. if ( status & SerialSignal_DSR )
  2109. info->input_signal_events.dsr_up++;
  2110. else
  2111. info->input_signal_events.dsr_down++;
  2112. }
  2113. if (status & MISCSTATUS_DCD_LATCHED) {
  2114. if ((info->dcd_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT) {
  2115. info->ie1_value &= ~CDCD;
  2116. write_reg(info, IE1, info->ie1_value);
  2117. }
  2118. icount->dcd++;
  2119. if (status & SerialSignal_DCD) {
  2120. info->input_signal_events.dcd_up++;
  2121. } else
  2122. info->input_signal_events.dcd_down++;
  2123. #ifdef CONFIG_HDLC
  2124. if (info->netcount)
  2125. hdlc_set_carrier(status & SerialSignal_DCD, info->netdev);
  2126. #endif
  2127. }
  2128. if (status & MISCSTATUS_CTS_LATCHED)
  2129. {
  2130. if ((info->cts_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT) {
  2131. info->ie1_value &= ~CCTS;
  2132. write_reg(info, IE1, info->ie1_value);
  2133. }
  2134. icount->cts++;
  2135. if ( status & SerialSignal_CTS )
  2136. info->input_signal_events.cts_up++;
  2137. else
  2138. info->input_signal_events.cts_down++;
  2139. }
  2140. wake_up_interruptible(&info->status_event_wait_q);
  2141. wake_up_interruptible(&info->event_wait_q);
  2142. if ( (info->flags & ASYNC_CHECK_CD) &&
  2143. (status & MISCSTATUS_DCD_LATCHED) ) {
  2144. if ( debug_level >= DEBUG_LEVEL_ISR )
  2145. printk("%s CD now %s...", info->device_name,
  2146. (status & SerialSignal_DCD) ? "on" : "off");
  2147. if (status & SerialSignal_DCD)
  2148. wake_up_interruptible(&info->open_wait);
  2149. else {
  2150. if ( debug_level >= DEBUG_LEVEL_ISR )
  2151. printk("doing serial hangup...");
  2152. if (info->tty)
  2153. tty_hangup(info->tty);
  2154. }
  2155. }
  2156. if ( (info->flags & ASYNC_CTS_FLOW) &&
  2157. (status & MISCSTATUS_CTS_LATCHED) ) {
  2158. if ( info->tty ) {
  2159. if (info->tty->hw_stopped) {
  2160. if (status & SerialSignal_CTS) {
  2161. if ( debug_level >= DEBUG_LEVEL_ISR )
  2162. printk("CTS tx start...");
  2163. info->tty->hw_stopped = 0;
  2164. tx_start(info);
  2165. info->pending_bh |= BH_TRANSMIT;
  2166. return;
  2167. }
  2168. } else {
  2169. if (!(status & SerialSignal_CTS)) {
  2170. if ( debug_level >= DEBUG_LEVEL_ISR )
  2171. printk("CTS tx stop...");
  2172. info->tty->hw_stopped = 1;
  2173. tx_stop(info);
  2174. }
  2175. }
  2176. }
  2177. }
  2178. }
  2179. info->pending_bh |= BH_STATUS;
  2180. }
  2181. /* Interrupt service routine entry point.
  2182. *
  2183. * Arguments:
  2184. * irq interrupt number that caused interrupt
  2185. * dev_id device ID supplied during interrupt registration
  2186. * regs interrupted processor context
  2187. */
  2188. static irqreturn_t synclinkmp_interrupt(int irq, void *dev_id,
  2189. struct pt_regs *regs)
  2190. {
  2191. SLMP_INFO * info;
  2192. unsigned char status, status0, status1=0;
  2193. unsigned char dmastatus, dmastatus0, dmastatus1=0;
  2194. unsigned char timerstatus0, timerstatus1=0;
  2195. unsigned char shift;
  2196. unsigned int i;
  2197. unsigned short tmp;
  2198. if ( debug_level >= DEBUG_LEVEL_ISR )
  2199. printk("%s(%d): synclinkmp_interrupt(%d)entry.\n",
  2200. __FILE__,__LINE__,irq);
  2201. info = (SLMP_INFO *)dev_id;
  2202. if (!info)
  2203. return IRQ_NONE;
  2204. spin_lock(&info->lock);
  2205. for(;;) {
  2206. /* get status for SCA0 (ports 0-1) */
  2207. tmp = read_reg16(info, ISR0); /* get ISR0 and ISR1 in one read */
  2208. status0 = (unsigned char)tmp;
  2209. dmastatus0 = (unsigned char)(tmp>>8);
  2210. timerstatus0 = read_reg(info, ISR2);
  2211. if ( debug_level >= DEBUG_LEVEL_ISR )
  2212. printk("%s(%d):%s status0=%02x, dmastatus0=%02x, timerstatus0=%02x\n",
  2213. __FILE__,__LINE__,info->device_name,
  2214. status0,dmastatus0,timerstatus0);
  2215. if (info->port_count == 4) {
  2216. /* get status for SCA1 (ports 2-3) */
  2217. tmp = read_reg16(info->port_array[2], ISR0);
  2218. status1 = (unsigned char)tmp;
  2219. dmastatus1 = (unsigned char)(tmp>>8);
  2220. timerstatus1 = read_reg(info->port_array[2], ISR2);
  2221. if ( debug_level >= DEBUG_LEVEL_ISR )
  2222. printk("%s(%d):%s status1=%02x, dmastatus1=%02x, timerstatus1=%02x\n",
  2223. __FILE__,__LINE__,info->device_name,
  2224. status1,dmastatus1,timerstatus1);
  2225. }
  2226. if (!status0 && !dmastatus0 && !timerstatus0 &&
  2227. !status1 && !dmastatus1 && !timerstatus1)
  2228. break;
  2229. for(i=0; i < info->port_count ; i++) {
  2230. if (info->port_array[i] == NULL)
  2231. continue;
  2232. if (i < 2) {
  2233. status = status0;
  2234. dmastatus = dmastatus0;
  2235. } else {
  2236. status = status1;
  2237. dmastatus = dmastatus1;
  2238. }
  2239. shift = i & 1 ? 4 :0;
  2240. if (status & BIT0 << shift)
  2241. isr_rxrdy(info->port_array[i]);
  2242. if (status & BIT1 << shift)
  2243. isr_txrdy(info->port_array[i]);
  2244. if (status & BIT2 << shift)
  2245. isr_rxint(info->port_array[i]);
  2246. if (status & BIT3 << shift)
  2247. isr_txint(info->port_array[i]);
  2248. if (dmastatus & BIT0 << shift)
  2249. isr_rxdmaerror(info->port_array[i]);
  2250. if (dmastatus & BIT1 << shift)
  2251. isr_rxdmaok(info->port_array[i]);
  2252. if (dmastatus & BIT2 << shift)
  2253. isr_txdmaerror(info->port_array[i]);
  2254. if (dmastatus & BIT3 << shift)
  2255. isr_txdmaok(info->port_array[i]);
  2256. }
  2257. if (timerstatus0 & (BIT5 | BIT4))
  2258. isr_timer(info->port_array[0]);
  2259. if (timerstatus0 & (BIT7 | BIT6))
  2260. isr_timer(info->port_array[1]);
  2261. if (timerstatus1 & (BIT5 | BIT4))
  2262. isr_timer(info->port_array[2]);
  2263. if (timerstatus1 & (BIT7 | BIT6))
  2264. isr_timer(info->port_array[3]);
  2265. }
  2266. for(i=0; i < info->port_count ; i++) {
  2267. SLMP_INFO * port = info->port_array[i];
  2268. /* Request bottom half processing if there's something
  2269. * for it to do and the bh is not already running.
  2270. *
  2271. * Note: startup adapter diags require interrupts.
  2272. * do not request bottom half processing if the
  2273. * device is not open in a normal mode.
  2274. */
  2275. if ( port && (port->count || port->netcount) &&
  2276. port->pending_bh && !port->bh_running &&
  2277. !port->bh_requested ) {
  2278. if ( debug_level >= DEBUG_LEVEL_ISR )
  2279. printk("%s(%d):%s queueing bh task.\n",
  2280. __FILE__,__LINE__,port->device_name);
  2281. schedule_work(&port->task);
  2282. port->bh_requested = 1;
  2283. }
  2284. }
  2285. spin_unlock(&info->lock);
  2286. if ( debug_level >= DEBUG_LEVEL_ISR )
  2287. printk("%s(%d):synclinkmp_interrupt(%d)exit.\n",
  2288. __FILE__,__LINE__,irq);
  2289. return IRQ_HANDLED;
  2290. }
  2291. /* Initialize and start device.
  2292. */
  2293. static int startup(SLMP_INFO * info)
  2294. {
  2295. if ( debug_level >= DEBUG_LEVEL_INFO )
  2296. printk("%s(%d):%s tx_releaseup()\n",__FILE__,__LINE__,info->device_name);
  2297. if (info->flags & ASYNC_INITIALIZED)
  2298. return 0;
  2299. if (!info->tx_buf) {
  2300. info->tx_buf = (unsigned char *)kmalloc(info->max_frame_size, GFP_KERNEL);
  2301. if (!info->tx_buf) {
  2302. printk(KERN_ERR"%s(%d):%s can't allocate transmit buffer\n",
  2303. __FILE__,__LINE__,info->device_name);
  2304. return -ENOMEM;
  2305. }
  2306. }
  2307. info->pending_bh = 0;
  2308. memset(&info->icount, 0, sizeof(info->icount));
  2309. /* program hardware for current parameters */
  2310. reset_port(info);
  2311. change_params(info);
  2312. info->status_timer.expires = jiffies + msecs_to_jiffies(10);
  2313. add_timer(&info->status_timer);
  2314. if (info->tty)
  2315. clear_bit(TTY_IO_ERROR, &info->tty->flags);
  2316. info->flags |= ASYNC_INITIALIZED;
  2317. return 0;
  2318. }
  2319. /* Called by close() and hangup() to shutdown hardware
  2320. */
  2321. static void shutdown(SLMP_INFO * info)
  2322. {
  2323. unsigned long flags;
  2324. if (!(info->flags & ASYNC_INITIALIZED))
  2325. return;
  2326. if (debug_level >= DEBUG_LEVEL_INFO)
  2327. printk("%s(%d):%s synclinkmp_shutdown()\n",
  2328. __FILE__,__LINE__, info->device_name );
  2329. /* clear status wait queue because status changes */
  2330. /* can't happen after shutting down the hardware */
  2331. wake_up_interruptible(&info->status_event_wait_q);
  2332. wake_up_interruptible(&info->event_wait_q);
  2333. del_timer(&info->tx_timer);
  2334. del_timer(&info->status_timer);
  2335. if (info->tx_buf) {
  2336. kfree(info->tx_buf);
  2337. info->tx_buf = NULL;
  2338. }
  2339. spin_lock_irqsave(&info->lock,flags);
  2340. reset_port(info);
  2341. if (!info->tty || info->tty->termios->c_cflag & HUPCL) {
  2342. info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
  2343. set_signals(info);
  2344. }
  2345. spin_unlock_irqrestore(&info->lock,flags);
  2346. if (info->tty)
  2347. set_bit(TTY_IO_ERROR, &info->tty->flags);
  2348. info->flags &= ~ASYNC_INITIALIZED;
  2349. }
  2350. static void program_hw(SLMP_INFO *info)
  2351. {
  2352. unsigned long flags;
  2353. spin_lock_irqsave(&info->lock,flags);
  2354. rx_stop(info);
  2355. tx_stop(info);
  2356. info->tx_count = info->tx_put = info->tx_get = 0;
  2357. if (info->params.mode == MGSL_MODE_HDLC || info->netcount)
  2358. hdlc_mode(info);
  2359. else
  2360. async_mode(info);
  2361. set_signals(info);
  2362. info->dcd_chkcount = 0;
  2363. info->cts_chkcount = 0;
  2364. info->ri_chkcount = 0;
  2365. info->dsr_chkcount = 0;
  2366. info->ie1_value |= (CDCD|CCTS);
  2367. write_reg(info, IE1, info->ie1_value);
  2368. get_signals(info);
  2369. if (info->netcount || (info->tty && info->tty->termios->c_cflag & CREAD) )
  2370. rx_start(info);
  2371. spin_unlock_irqrestore(&info->lock,flags);
  2372. }
  2373. /* Reconfigure adapter based on new parameters
  2374. */
  2375. static void change_params(SLMP_INFO *info)
  2376. {
  2377. unsigned cflag;
  2378. int bits_per_char;
  2379. if (!info->tty || !info->tty->termios)
  2380. return;
  2381. if (debug_level >= DEBUG_LEVEL_INFO)
  2382. printk("%s(%d):%s change_params()\n",
  2383. __FILE__,__LINE__, info->device_name );
  2384. cflag = info->tty->termios->c_cflag;
  2385. /* if B0 rate (hangup) specified then negate DTR and RTS */
  2386. /* otherwise assert DTR and RTS */
  2387. if (cflag & CBAUD)
  2388. info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
  2389. else
  2390. info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
  2391. /* byte size and parity */
  2392. switch (cflag & CSIZE) {
  2393. case CS5: info->params.data_bits = 5; break;
  2394. case CS6: info->params.data_bits = 6; break;
  2395. case CS7: info->params.data_bits = 7; break;
  2396. case CS8: info->params.data_bits = 8; break;
  2397. /* Never happens, but GCC is too dumb to figure it out */
  2398. default: info->params.data_bits = 7; break;
  2399. }
  2400. if (cflag & CSTOPB)
  2401. info->params.stop_bits = 2;
  2402. else
  2403. info->params.stop_bits = 1;
  2404. info->params.parity = ASYNC_PARITY_NONE;
  2405. if (cflag & PARENB) {
  2406. if (cflag & PARODD)
  2407. info->params.parity = ASYNC_PARITY_ODD;
  2408. else
  2409. info->params.parity = ASYNC_PARITY_EVEN;
  2410. #ifdef CMSPAR
  2411. if (cflag & CMSPAR)
  2412. info->params.parity = ASYNC_PARITY_SPACE;
  2413. #endif
  2414. }
  2415. /* calculate number of jiffies to transmit a full
  2416. * FIFO (32 bytes) at specified data rate
  2417. */
  2418. bits_per_char = info->params.data_bits +
  2419. info->params.stop_bits + 1;
  2420. /* if port data rate is set to 460800 or less then
  2421. * allow tty settings to override, otherwise keep the
  2422. * current data rate.
  2423. */
  2424. if (info->params.data_rate <= 460800) {
  2425. info->params.data_rate = tty_get_baud_rate(info->tty);
  2426. }
  2427. if ( info->params.data_rate ) {
  2428. info->timeout = (32*HZ*bits_per_char) /
  2429. info->params.data_rate;
  2430. }
  2431. info->timeout += HZ/50; /* Add .02 seconds of slop */
  2432. if (cflag & CRTSCTS)
  2433. info->flags |= ASYNC_CTS_FLOW;
  2434. else
  2435. info->flags &= ~ASYNC_CTS_FLOW;
  2436. if (cflag & CLOCAL)
  2437. info->flags &= ~ASYNC_CHECK_CD;
  2438. else
  2439. info->flags |= ASYNC_CHECK_CD;
  2440. /* process tty input control flags */
  2441. info->read_status_mask2 = OVRN;
  2442. if (I_INPCK(info->tty))
  2443. info->read_status_mask2 |= PE | FRME;
  2444. if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
  2445. info->read_status_mask1 |= BRKD;
  2446. if (I_IGNPAR(info->tty))
  2447. info->ignore_status_mask2 |= PE | FRME;
  2448. if (I_IGNBRK(info->tty)) {
  2449. info->ignore_status_mask1 |= BRKD;
  2450. /* If ignoring parity and break indicators, ignore
  2451. * overruns too. (For real raw support).
  2452. */
  2453. if (I_IGNPAR(info->tty))
  2454. info->ignore_status_mask2 |= OVRN;
  2455. }
  2456. program_hw(info);
  2457. }
  2458. static int get_stats(SLMP_INFO * info, struct mgsl_icount __user *user_icount)
  2459. {
  2460. int err;
  2461. if (debug_level >= DEBUG_LEVEL_INFO)
  2462. printk("%s(%d):%s get_params()\n",
  2463. __FILE__,__LINE__, info->device_name);
  2464. if (!user_icount) {
  2465. memset(&info->icount, 0, sizeof(info->icount));
  2466. } else {
  2467. COPY_TO_USER(err, user_icount, &info->icount, sizeof(struct mgsl_icount));
  2468. if (err)
  2469. return -EFAULT;
  2470. }
  2471. return 0;
  2472. }
  2473. static int get_params(SLMP_INFO * info, MGSL_PARAMS __user *user_params)
  2474. {
  2475. int err;
  2476. if (debug_level >= DEBUG_LEVEL_INFO)
  2477. printk("%s(%d):%s get_params()\n",
  2478. __FILE__,__LINE__, info->device_name);
  2479. COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS));
  2480. if (err) {
  2481. if ( debug_level >= DEBUG_LEVEL_INFO )
  2482. printk( "%s(%d):%s get_params() user buffer copy failed\n",
  2483. __FILE__,__LINE__,info->device_name);
  2484. return -EFAULT;
  2485. }
  2486. return 0;
  2487. }
  2488. static int set_params(SLMP_INFO * info, MGSL_PARAMS __user *new_params)
  2489. {
  2490. unsigned long flags;
  2491. MGSL_PARAMS tmp_params;
  2492. int err;
  2493. if (debug_level >= DEBUG_LEVEL_INFO)
  2494. printk("%s(%d):%s set_params\n",
  2495. __FILE__,__LINE__,info->device_name );
  2496. COPY_FROM_USER(err,&tmp_params, new_params, sizeof(MGSL_PARAMS));
  2497. if (err) {
  2498. if ( debug_level >= DEBUG_LEVEL_INFO )
  2499. printk( "%s(%d):%s set_params() user buffer copy failed\n",
  2500. __FILE__,__LINE__,info->device_name);
  2501. return -EFAULT;
  2502. }
  2503. spin_lock_irqsave(&info->lock,flags);
  2504. memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
  2505. spin_unlock_irqrestore(&info->lock,flags);
  2506. change_params(info);
  2507. return 0;
  2508. }
  2509. static int get_txidle(SLMP_INFO * info, int __user *idle_mode)
  2510. {
  2511. int err;
  2512. if (debug_level >= DEBUG_LEVEL_INFO)
  2513. printk("%s(%d):%s get_txidle()=%d\n",
  2514. __FILE__,__LINE__, info->device_name, info->idle_mode);
  2515. COPY_TO_USER(err,idle_mode, &info->idle_mode, sizeof(int));
  2516. if (err) {
  2517. if ( debug_level >= DEBUG_LEVEL_INFO )
  2518. printk( "%s(%d):%s get_txidle() user buffer copy failed\n",
  2519. __FILE__,__LINE__,info->device_name);
  2520. return -EFAULT;
  2521. }
  2522. return 0;
  2523. }
  2524. static int set_txidle(SLMP_INFO * info, int idle_mode)
  2525. {
  2526. unsigned long flags;
  2527. if (debug_level >= DEBUG_LEVEL_INFO)
  2528. printk("%s(%d):%s set_txidle(%d)\n",
  2529. __FILE__,__LINE__,info->device_name, idle_mode );
  2530. spin_lock_irqsave(&info->lock,flags);
  2531. info->idle_mode = idle_mode;
  2532. tx_set_idle( info );
  2533. spin_unlock_irqrestore(&info->lock,flags);
  2534. return 0;
  2535. }
  2536. static int tx_enable(SLMP_INFO * info, int enable)
  2537. {
  2538. unsigned long flags;
  2539. if (debug_level >= DEBUG_LEVEL_INFO)
  2540. printk("%s(%d):%s tx_enable(%d)\n",
  2541. __FILE__,__LINE__,info->device_name, enable);
  2542. spin_lock_irqsave(&info->lock,flags);
  2543. if ( enable ) {
  2544. if ( !info->tx_enabled ) {
  2545. tx_start(info);
  2546. }
  2547. } else {
  2548. if ( info->tx_enabled )
  2549. tx_stop(info);
  2550. }
  2551. spin_unlock_irqrestore(&info->lock,flags);
  2552. return 0;
  2553. }
  2554. /* abort send HDLC frame
  2555. */
  2556. static int tx_abort(SLMP_INFO * info)
  2557. {
  2558. unsigned long flags;
  2559. if (debug_level >= DEBUG_LEVEL_INFO)
  2560. printk("%s(%d):%s tx_abort()\n",
  2561. __FILE__,__LINE__,info->device_name);
  2562. spin_lock_irqsave(&info->lock,flags);
  2563. if ( info->tx_active && info->params.mode == MGSL_MODE_HDLC ) {
  2564. info->ie1_value &= ~UDRN;
  2565. info->ie1_value |= IDLE;
  2566. write_reg(info, IE1, info->ie1_value); /* disable tx status interrupts */
  2567. write_reg(info, SR1, (unsigned char)(IDLE + UDRN)); /* clear pending */
  2568. write_reg(info, TXDMA + DSR, 0); /* disable DMA channel */
  2569. write_reg(info, TXDMA + DCMD, SWABORT); /* reset/init DMA channel */
  2570. write_reg(info, CMD, TXABORT);
  2571. }
  2572. spin_unlock_irqrestore(&info->lock,flags);
  2573. return 0;
  2574. }
  2575. static int rx_enable(SLMP_INFO * info, int enable)
  2576. {
  2577. unsigned long flags;
  2578. if (debug_level >= DEBUG_LEVEL_INFO)
  2579. printk("%s(%d):%s rx_enable(%d)\n",
  2580. __FILE__,__LINE__,info->device_name,enable);
  2581. spin_lock_irqsave(&info->lock,flags);
  2582. if ( enable ) {
  2583. if ( !info->rx_enabled )
  2584. rx_start(info);
  2585. } else {
  2586. if ( info->rx_enabled )
  2587. rx_stop(info);
  2588. }
  2589. spin_unlock_irqrestore(&info->lock,flags);
  2590. return 0;
  2591. }
  2592. /* wait for specified event to occur
  2593. */
  2594. static int wait_mgsl_event(SLMP_INFO * info, int __user *mask_ptr)
  2595. {
  2596. unsigned long flags;
  2597. int s;
  2598. int rc=0;
  2599. struct mgsl_icount cprev, cnow;
  2600. int events;
  2601. int mask;
  2602. struct _input_signal_events oldsigs, newsigs;
  2603. DECLARE_WAITQUEUE(wait, current);
  2604. COPY_FROM_USER(rc,&mask, mask_ptr, sizeof(int));
  2605. if (rc) {
  2606. return -EFAULT;
  2607. }
  2608. if (debug_level >= DEBUG_LEVEL_INFO)
  2609. printk("%s(%d):%s wait_mgsl_event(%d)\n",
  2610. __FILE__,__LINE__,info->device_name,mask);
  2611. spin_lock_irqsave(&info->lock,flags);
  2612. /* return immediately if state matches requested events */
  2613. get_signals(info);
  2614. s = info->serial_signals;
  2615. events = mask &
  2616. ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
  2617. ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
  2618. ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
  2619. ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
  2620. if (events) {
  2621. spin_unlock_irqrestore(&info->lock,flags);
  2622. goto exit;
  2623. }
  2624. /* save current irq counts */
  2625. cprev = info->icount;
  2626. oldsigs = info->input_signal_events;
  2627. /* enable hunt and idle irqs if needed */
  2628. if (mask & (MgslEvent_ExitHuntMode+MgslEvent_IdleReceived)) {
  2629. unsigned char oldval = info->ie1_value;
  2630. unsigned char newval = oldval +
  2631. (mask & MgslEvent_ExitHuntMode ? FLGD:0) +
  2632. (mask & MgslEvent_IdleReceived ? IDLD:0);
  2633. if ( oldval != newval ) {
  2634. info->ie1_value = newval;
  2635. write_reg(info, IE1, info->ie1_value);
  2636. }
  2637. }
  2638. set_current_state(TASK_INTERRUPTIBLE);
  2639. add_wait_queue(&info->event_wait_q, &wait);
  2640. spin_unlock_irqrestore(&info->lock,flags);
  2641. for(;;) {
  2642. schedule();
  2643. if (signal_pending(current)) {
  2644. rc = -ERESTARTSYS;
  2645. break;
  2646. }
  2647. /* get current irq counts */
  2648. spin_lock_irqsave(&info->lock,flags);
  2649. cnow = info->icount;
  2650. newsigs = info->input_signal_events;
  2651. set_current_state(TASK_INTERRUPTIBLE);
  2652. spin_unlock_irqrestore(&info->lock,flags);
  2653. /* if no change, wait aborted for some reason */
  2654. if (newsigs.dsr_up == oldsigs.dsr_up &&
  2655. newsigs.dsr_down == oldsigs.dsr_down &&
  2656. newsigs.dcd_up == oldsigs.dcd_up &&
  2657. newsigs.dcd_down == oldsigs.dcd_down &&
  2658. newsigs.cts_up == oldsigs.cts_up &&
  2659. newsigs.cts_down == oldsigs.cts_down &&
  2660. newsigs.ri_up == oldsigs.ri_up &&
  2661. newsigs.ri_down == oldsigs.ri_down &&
  2662. cnow.exithunt == cprev.exithunt &&
  2663. cnow.rxidle == cprev.rxidle) {
  2664. rc = -EIO;
  2665. break;
  2666. }
  2667. events = mask &
  2668. ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
  2669. (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
  2670. (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
  2671. (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
  2672. (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
  2673. (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
  2674. (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
  2675. (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
  2676. (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
  2677. (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
  2678. if (events)
  2679. break;
  2680. cprev = cnow;
  2681. oldsigs = newsigs;
  2682. }
  2683. remove_wait_queue(&info->event_wait_q, &wait);
  2684. set_current_state(TASK_RUNNING);
  2685. if (mask & (MgslEvent_ExitHuntMode + MgslEvent_IdleReceived)) {
  2686. spin_lock_irqsave(&info->lock,flags);
  2687. if (!waitqueue_active(&info->event_wait_q)) {
  2688. /* disable enable exit hunt mode/idle rcvd IRQs */
  2689. info->ie1_value &= ~(FLGD|IDLD);
  2690. write_reg(info, IE1, info->ie1_value);
  2691. }
  2692. spin_unlock_irqrestore(&info->lock,flags);
  2693. }
  2694. exit:
  2695. if ( rc == 0 )
  2696. PUT_USER(rc, events, mask_ptr);
  2697. return rc;
  2698. }
  2699. static int modem_input_wait(SLMP_INFO *info,int arg)
  2700. {
  2701. unsigned long flags;
  2702. int rc;
  2703. struct mgsl_icount cprev, cnow;
  2704. DECLARE_WAITQUEUE(wait, current);
  2705. /* save current irq counts */
  2706. spin_lock_irqsave(&info->lock,flags);
  2707. cprev = info->icount;
  2708. add_wait_queue(&info->status_event_wait_q, &wait);
  2709. set_current_state(TASK_INTERRUPTIBLE);
  2710. spin_unlock_irqrestore(&info->lock,flags);
  2711. for(;;) {
  2712. schedule();
  2713. if (signal_pending(current)) {
  2714. rc = -ERESTARTSYS;
  2715. break;
  2716. }
  2717. /* get new irq counts */
  2718. spin_lock_irqsave(&info->lock,flags);
  2719. cnow = info->icount;
  2720. set_current_state(TASK_INTERRUPTIBLE);
  2721. spin_unlock_irqrestore(&info->lock,flags);
  2722. /* if no change, wait aborted for some reason */
  2723. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  2724. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
  2725. rc = -EIO;
  2726. break;
  2727. }
  2728. /* check for change in caller specified modem input */
  2729. if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
  2730. (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
  2731. (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
  2732. (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
  2733. rc = 0;
  2734. break;
  2735. }
  2736. cprev = cnow;
  2737. }
  2738. remove_wait_queue(&info->status_event_wait_q, &wait);
  2739. set_current_state(TASK_RUNNING);
  2740. return rc;
  2741. }
  2742. /* return the state of the serial control and status signals
  2743. */
  2744. static int tiocmget(struct tty_struct *tty, struct file *file)
  2745. {
  2746. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  2747. unsigned int result;
  2748. unsigned long flags;
  2749. spin_lock_irqsave(&info->lock,flags);
  2750. get_signals(info);
  2751. spin_unlock_irqrestore(&info->lock,flags);
  2752. result = ((info->serial_signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
  2753. ((info->serial_signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
  2754. ((info->serial_signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
  2755. ((info->serial_signals & SerialSignal_RI) ? TIOCM_RNG:0) +
  2756. ((info->serial_signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
  2757. ((info->serial_signals & SerialSignal_CTS) ? TIOCM_CTS:0);
  2758. if (debug_level >= DEBUG_LEVEL_INFO)
  2759. printk("%s(%d):%s tiocmget() value=%08X\n",
  2760. __FILE__,__LINE__, info->device_name, result );
  2761. return result;
  2762. }
  2763. /* set modem control signals (DTR/RTS)
  2764. */
  2765. static int tiocmset(struct tty_struct *tty, struct file *file,
  2766. unsigned int set, unsigned int clear)
  2767. {
  2768. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  2769. unsigned long flags;
  2770. if (debug_level >= DEBUG_LEVEL_INFO)
  2771. printk("%s(%d):%s tiocmset(%x,%x)\n",
  2772. __FILE__,__LINE__,info->device_name, set, clear);
  2773. if (set & TIOCM_RTS)
  2774. info->serial_signals |= SerialSignal_RTS;
  2775. if (set & TIOCM_DTR)
  2776. info->serial_signals |= SerialSignal_DTR;
  2777. if (clear & TIOCM_RTS)
  2778. info->serial_signals &= ~SerialSignal_RTS;
  2779. if (clear & TIOCM_DTR)
  2780. info->serial_signals &= ~SerialSignal_DTR;
  2781. spin_lock_irqsave(&info->lock,flags);
  2782. set_signals(info);
  2783. spin_unlock_irqrestore(&info->lock,flags);
  2784. return 0;
  2785. }
  2786. /* Block the current process until the specified port is ready to open.
  2787. */
  2788. static int block_til_ready(struct tty_struct *tty, struct file *filp,
  2789. SLMP_INFO *info)
  2790. {
  2791. DECLARE_WAITQUEUE(wait, current);
  2792. int retval;
  2793. int do_clocal = 0, extra_count = 0;
  2794. unsigned long flags;
  2795. if (debug_level >= DEBUG_LEVEL_INFO)
  2796. printk("%s(%d):%s block_til_ready()\n",
  2797. __FILE__,__LINE__, tty->driver->name );
  2798. if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
  2799. /* nonblock mode is set or port is not enabled */
  2800. /* just verify that callout device is not active */
  2801. info->flags |= ASYNC_NORMAL_ACTIVE;
  2802. return 0;
  2803. }
  2804. if (tty->termios->c_cflag & CLOCAL)
  2805. do_clocal = 1;
  2806. /* Wait for carrier detect and the line to become
  2807. * free (i.e., not in use by the callout). While we are in
  2808. * this loop, info->count is dropped by one, so that
  2809. * close() knows when to free things. We restore it upon
  2810. * exit, either normal or abnormal.
  2811. */
  2812. retval = 0;
  2813. add_wait_queue(&info->open_wait, &wait);
  2814. if (debug_level >= DEBUG_LEVEL_INFO)
  2815. printk("%s(%d):%s block_til_ready() before block, count=%d\n",
  2816. __FILE__,__LINE__, tty->driver->name, info->count );
  2817. spin_lock_irqsave(&info->lock, flags);
  2818. if (!tty_hung_up_p(filp)) {
  2819. extra_count = 1;
  2820. info->count--;
  2821. }
  2822. spin_unlock_irqrestore(&info->lock, flags);
  2823. info->blocked_open++;
  2824. while (1) {
  2825. if ((tty->termios->c_cflag & CBAUD)) {
  2826. spin_lock_irqsave(&info->lock,flags);
  2827. info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
  2828. set_signals(info);
  2829. spin_unlock_irqrestore(&info->lock,flags);
  2830. }
  2831. set_current_state(TASK_INTERRUPTIBLE);
  2832. if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)){
  2833. retval = (info->flags & ASYNC_HUP_NOTIFY) ?
  2834. -EAGAIN : -ERESTARTSYS;
  2835. break;
  2836. }
  2837. spin_lock_irqsave(&info->lock,flags);
  2838. get_signals(info);
  2839. spin_unlock_irqrestore(&info->lock,flags);
  2840. if (!(info->flags & ASYNC_CLOSING) &&
  2841. (do_clocal || (info->serial_signals & SerialSignal_DCD)) ) {
  2842. break;
  2843. }
  2844. if (signal_pending(current)) {
  2845. retval = -ERESTARTSYS;
  2846. break;
  2847. }
  2848. if (debug_level >= DEBUG_LEVEL_INFO)
  2849. printk("%s(%d):%s block_til_ready() count=%d\n",
  2850. __FILE__,__LINE__, tty->driver->name, info->count );
  2851. schedule();
  2852. }
  2853. set_current_state(TASK_RUNNING);
  2854. remove_wait_queue(&info->open_wait, &wait);
  2855. if (extra_count)
  2856. info->count++;
  2857. info->blocked_open--;
  2858. if (debug_level >= DEBUG_LEVEL_INFO)
  2859. printk("%s(%d):%s block_til_ready() after, count=%d\n",
  2860. __FILE__,__LINE__, tty->driver->name, info->count );
  2861. if (!retval)
  2862. info->flags |= ASYNC_NORMAL_ACTIVE;
  2863. return retval;
  2864. }
  2865. int alloc_dma_bufs(SLMP_INFO *info)
  2866. {
  2867. unsigned short BuffersPerFrame;
  2868. unsigned short BufferCount;
  2869. // Force allocation to start at 64K boundary for each port.
  2870. // This is necessary because *all* buffer descriptors for a port
  2871. // *must* be in the same 64K block. All descriptors on a port
  2872. // share a common 'base' address (upper 8 bits of 24 bits) programmed
  2873. // into the CBP register.
  2874. info->port_array[0]->last_mem_alloc = (SCA_MEM_SIZE/4) * info->port_num;
  2875. /* Calculate the number of DMA buffers necessary to hold the */
  2876. /* largest allowable frame size. Note: If the max frame size is */
  2877. /* not an even multiple of the DMA buffer size then we need to */
  2878. /* round the buffer count per frame up one. */
  2879. BuffersPerFrame = (unsigned short)(info->max_frame_size/SCABUFSIZE);
  2880. if ( info->max_frame_size % SCABUFSIZE )
  2881. BuffersPerFrame++;
  2882. /* calculate total number of data buffers (SCABUFSIZE) possible
  2883. * in one ports memory (SCA_MEM_SIZE/4) after allocating memory
  2884. * for the descriptor list (BUFFERLISTSIZE).
  2885. */
  2886. BufferCount = (SCA_MEM_SIZE/4 - BUFFERLISTSIZE)/SCABUFSIZE;
  2887. /* limit number of buffers to maximum amount of descriptors */
  2888. if (BufferCount > BUFFERLISTSIZE/sizeof(SCADESC))
  2889. BufferCount = BUFFERLISTSIZE/sizeof(SCADESC);
  2890. /* use enough buffers to transmit one max size frame */
  2891. info->tx_buf_count = BuffersPerFrame + 1;
  2892. /* never use more than half the available buffers for transmit */
  2893. if (info->tx_buf_count > (BufferCount/2))
  2894. info->tx_buf_count = BufferCount/2;
  2895. if (info->tx_buf_count > SCAMAXDESC)
  2896. info->tx_buf_count = SCAMAXDESC;
  2897. /* use remaining buffers for receive */
  2898. info->rx_buf_count = BufferCount - info->tx_buf_count;
  2899. if (info->rx_buf_count > SCAMAXDESC)
  2900. info->rx_buf_count = SCAMAXDESC;
  2901. if ( debug_level >= DEBUG_LEVEL_INFO )
  2902. printk("%s(%d):%s Allocating %d TX and %d RX DMA buffers.\n",
  2903. __FILE__,__LINE__, info->device_name,
  2904. info->tx_buf_count,info->rx_buf_count);
  2905. if ( alloc_buf_list( info ) < 0 ||
  2906. alloc_frame_bufs(info,
  2907. info->rx_buf_list,
  2908. info->rx_buf_list_ex,
  2909. info->rx_buf_count) < 0 ||
  2910. alloc_frame_bufs(info,
  2911. info->tx_buf_list,
  2912. info->tx_buf_list_ex,
  2913. info->tx_buf_count) < 0 ||
  2914. alloc_tmp_rx_buf(info) < 0 ) {
  2915. printk("%s(%d):%s Can't allocate DMA buffer memory\n",
  2916. __FILE__,__LINE__, info->device_name);
  2917. return -ENOMEM;
  2918. }
  2919. rx_reset_buffers( info );
  2920. return 0;
  2921. }
  2922. /* Allocate DMA buffers for the transmit and receive descriptor lists.
  2923. */
  2924. int alloc_buf_list(SLMP_INFO *info)
  2925. {
  2926. unsigned int i;
  2927. /* build list in adapter shared memory */
  2928. info->buffer_list = info->memory_base + info->port_array[0]->last_mem_alloc;
  2929. info->buffer_list_phys = info->port_array[0]->last_mem_alloc;
  2930. info->port_array[0]->last_mem_alloc += BUFFERLISTSIZE;
  2931. memset(info->buffer_list, 0, BUFFERLISTSIZE);
  2932. /* Save virtual address pointers to the receive and */
  2933. /* transmit buffer lists. (Receive 1st). These pointers will */
  2934. /* be used by the processor to access the lists. */
  2935. info->rx_buf_list = (SCADESC *)info->buffer_list;
  2936. info->tx_buf_list = (SCADESC *)info->buffer_list;
  2937. info->tx_buf_list += info->rx_buf_count;
  2938. /* Build links for circular buffer entry lists (tx and rx)
  2939. *
  2940. * Note: links are physical addresses read by the SCA device
  2941. * to determine the next buffer entry to use.
  2942. */
  2943. for ( i = 0; i < info->rx_buf_count; i++ ) {
  2944. /* calculate and store physical address of this buffer entry */
  2945. info->rx_buf_list_ex[i].phys_entry =
  2946. info->buffer_list_phys + (i * sizeof(SCABUFSIZE));
  2947. /* calculate and store physical address of */
  2948. /* next entry in cirular list of entries */
  2949. info->rx_buf_list[i].next = info->buffer_list_phys;
  2950. if ( i < info->rx_buf_count - 1 )
  2951. info->rx_buf_list[i].next += (i + 1) * sizeof(SCADESC);
  2952. info->rx_buf_list[i].length = SCABUFSIZE;
  2953. }
  2954. for ( i = 0; i < info->tx_buf_count; i++ ) {
  2955. /* calculate and store physical address of this buffer entry */
  2956. info->tx_buf_list_ex[i].phys_entry = info->buffer_list_phys +
  2957. ((info->rx_buf_count + i) * sizeof(SCADESC));
  2958. /* calculate and store physical address of */
  2959. /* next entry in cirular list of entries */
  2960. info->tx_buf_list[i].next = info->buffer_list_phys +
  2961. info->rx_buf_count * sizeof(SCADESC);
  2962. if ( i < info->tx_buf_count - 1 )
  2963. info->tx_buf_list[i].next += (i + 1) * sizeof(SCADESC);
  2964. }
  2965. return 0;
  2966. }
  2967. /* Allocate the frame DMA buffers used by the specified buffer list.
  2968. */
  2969. int alloc_frame_bufs(SLMP_INFO *info, SCADESC *buf_list,SCADESC_EX *buf_list_ex,int count)
  2970. {
  2971. int i;
  2972. unsigned long phys_addr;
  2973. for ( i = 0; i < count; i++ ) {
  2974. buf_list_ex[i].virt_addr = info->memory_base + info->port_array[0]->last_mem_alloc;
  2975. phys_addr = info->port_array[0]->last_mem_alloc;
  2976. info->port_array[0]->last_mem_alloc += SCABUFSIZE;
  2977. buf_list[i].buf_ptr = (unsigned short)phys_addr;
  2978. buf_list[i].buf_base = (unsigned char)(phys_addr >> 16);
  2979. }
  2980. return 0;
  2981. }
  2982. void free_dma_bufs(SLMP_INFO *info)
  2983. {
  2984. info->buffer_list = NULL;
  2985. info->rx_buf_list = NULL;
  2986. info->tx_buf_list = NULL;
  2987. }
  2988. /* allocate buffer large enough to hold max_frame_size.
  2989. * This buffer is used to pass an assembled frame to the line discipline.
  2990. */
  2991. int alloc_tmp_rx_buf(SLMP_INFO *info)
  2992. {
  2993. info->tmp_rx_buf = kmalloc(info->max_frame_size, GFP_KERNEL);
  2994. if (info->tmp_rx_buf == NULL)
  2995. return -ENOMEM;
  2996. return 0;
  2997. }
  2998. void free_tmp_rx_buf(SLMP_INFO *info)
  2999. {
  3000. if (info->tmp_rx_buf)
  3001. kfree(info->tmp_rx_buf);
  3002. info->tmp_rx_buf = NULL;
  3003. }
  3004. int claim_resources(SLMP_INFO *info)
  3005. {
  3006. if (request_mem_region(info->phys_memory_base,SCA_MEM_SIZE,"synclinkmp") == NULL) {
  3007. printk( "%s(%d):%s mem addr conflict, Addr=%08X\n",
  3008. __FILE__,__LINE__,info->device_name, info->phys_memory_base);
  3009. info->init_error = DiagStatus_AddressConflict;
  3010. goto errout;
  3011. }
  3012. else
  3013. info->shared_mem_requested = 1;
  3014. if (request_mem_region(info->phys_lcr_base + info->lcr_offset,128,"synclinkmp") == NULL) {
  3015. printk( "%s(%d):%s lcr mem addr conflict, Addr=%08X\n",
  3016. __FILE__,__LINE__,info->device_name, info->phys_lcr_base);
  3017. info->init_error = DiagStatus_AddressConflict;
  3018. goto errout;
  3019. }
  3020. else
  3021. info->lcr_mem_requested = 1;
  3022. if (request_mem_region(info->phys_sca_base + info->sca_offset,SCA_BASE_SIZE,"synclinkmp") == NULL) {
  3023. printk( "%s(%d):%s sca mem addr conflict, Addr=%08X\n",
  3024. __FILE__,__LINE__,info->device_name, info->phys_sca_base);
  3025. info->init_error = DiagStatus_AddressConflict;
  3026. goto errout;
  3027. }
  3028. else
  3029. info->sca_base_requested = 1;
  3030. if (request_mem_region(info->phys_statctrl_base + info->statctrl_offset,SCA_REG_SIZE,"synclinkmp") == NULL) {
  3031. printk( "%s(%d):%s stat/ctrl mem addr conflict, Addr=%08X\n",
  3032. __FILE__,__LINE__,info->device_name, info->phys_statctrl_base);
  3033. info->init_error = DiagStatus_AddressConflict;
  3034. goto errout;
  3035. }
  3036. else
  3037. info->sca_statctrl_requested = 1;
  3038. info->memory_base = ioremap(info->phys_memory_base,SCA_MEM_SIZE);
  3039. if (!info->memory_base) {
  3040. printk( "%s(%d):%s Cant map shared memory, MemAddr=%08X\n",
  3041. __FILE__,__LINE__,info->device_name, info->phys_memory_base );
  3042. info->init_error = DiagStatus_CantAssignPciResources;
  3043. goto errout;
  3044. }
  3045. info->lcr_base = ioremap(info->phys_lcr_base,PAGE_SIZE);
  3046. if (!info->lcr_base) {
  3047. printk( "%s(%d):%s Cant map LCR memory, MemAddr=%08X\n",
  3048. __FILE__,__LINE__,info->device_name, info->phys_lcr_base );
  3049. info->init_error = DiagStatus_CantAssignPciResources;
  3050. goto errout;
  3051. }
  3052. info->lcr_base += info->lcr_offset;
  3053. info->sca_base = ioremap(info->phys_sca_base,PAGE_SIZE);
  3054. if (!info->sca_base) {
  3055. printk( "%s(%d):%s Cant map SCA memory, MemAddr=%08X\n",
  3056. __FILE__,__LINE__,info->device_name, info->phys_sca_base );
  3057. info->init_error = DiagStatus_CantAssignPciResources;
  3058. goto errout;
  3059. }
  3060. info->sca_base += info->sca_offset;
  3061. info->statctrl_base = ioremap(info->phys_statctrl_base,PAGE_SIZE);
  3062. if (!info->statctrl_base) {
  3063. printk( "%s(%d):%s Cant map SCA Status/Control memory, MemAddr=%08X\n",
  3064. __FILE__,__LINE__,info->device_name, info->phys_statctrl_base );
  3065. info->init_error = DiagStatus_CantAssignPciResources;
  3066. goto errout;
  3067. }
  3068. info->statctrl_base += info->statctrl_offset;
  3069. if ( !memory_test(info) ) {
  3070. printk( "%s(%d):Shared Memory Test failed for device %s MemAddr=%08X\n",
  3071. __FILE__,__LINE__,info->device_name, info->phys_memory_base );
  3072. info->init_error = DiagStatus_MemoryError;
  3073. goto errout;
  3074. }
  3075. return 0;
  3076. errout:
  3077. release_resources( info );
  3078. return -ENODEV;
  3079. }
  3080. void release_resources(SLMP_INFO *info)
  3081. {
  3082. if ( debug_level >= DEBUG_LEVEL_INFO )
  3083. printk( "%s(%d):%s release_resources() entry\n",
  3084. __FILE__,__LINE__,info->device_name );
  3085. if ( info->irq_requested ) {
  3086. free_irq(info->irq_level, info);
  3087. info->irq_requested = 0;
  3088. }
  3089. if ( info->shared_mem_requested ) {
  3090. release_mem_region(info->phys_memory_base,SCA_MEM_SIZE);
  3091. info->shared_mem_requested = 0;
  3092. }
  3093. if ( info->lcr_mem_requested ) {
  3094. release_mem_region(info->phys_lcr_base + info->lcr_offset,128);
  3095. info->lcr_mem_requested = 0;
  3096. }
  3097. if ( info->sca_base_requested ) {
  3098. release_mem_region(info->phys_sca_base + info->sca_offset,SCA_BASE_SIZE);
  3099. info->sca_base_requested = 0;
  3100. }
  3101. if ( info->sca_statctrl_requested ) {
  3102. release_mem_region(info->phys_statctrl_base + info->statctrl_offset,SCA_REG_SIZE);
  3103. info->sca_statctrl_requested = 0;
  3104. }
  3105. if (info->memory_base){
  3106. iounmap(info->memory_base);
  3107. info->memory_base = NULL;
  3108. }
  3109. if (info->sca_base) {
  3110. iounmap(info->sca_base - info->sca_offset);
  3111. info->sca_base=NULL;
  3112. }
  3113. if (info->statctrl_base) {
  3114. iounmap(info->statctrl_base - info->statctrl_offset);
  3115. info->statctrl_base=NULL;
  3116. }
  3117. if (info->lcr_base){
  3118. iounmap(info->lcr_base - info->lcr_offset);
  3119. info->lcr_base = NULL;
  3120. }
  3121. if ( debug_level >= DEBUG_LEVEL_INFO )
  3122. printk( "%s(%d):%s release_resources() exit\n",
  3123. __FILE__,__LINE__,info->device_name );
  3124. }
  3125. /* Add the specified device instance data structure to the
  3126. * global linked list of devices and increment the device count.
  3127. */
  3128. void add_device(SLMP_INFO *info)
  3129. {
  3130. info->next_device = NULL;
  3131. info->line = synclinkmp_device_count;
  3132. sprintf(info->device_name,"ttySLM%dp%d",info->adapter_num,info->port_num);
  3133. if (info->line < MAX_DEVICES) {
  3134. if (maxframe[info->line])
  3135. info->max_frame_size = maxframe[info->line];
  3136. info->dosyncppp = dosyncppp[info->line];
  3137. }
  3138. synclinkmp_device_count++;
  3139. if ( !synclinkmp_device_list )
  3140. synclinkmp_device_list = info;
  3141. else {
  3142. SLMP_INFO *current_dev = synclinkmp_device_list;
  3143. while( current_dev->next_device )
  3144. current_dev = current_dev->next_device;
  3145. current_dev->next_device = info;
  3146. }
  3147. if ( info->max_frame_size < 4096 )
  3148. info->max_frame_size = 4096;
  3149. else if ( info->max_frame_size > 65535 )
  3150. info->max_frame_size = 65535;
  3151. printk( "SyncLink MultiPort %s: "
  3152. "Mem=(%08x %08X %08x %08X) IRQ=%d MaxFrameSize=%u\n",
  3153. info->device_name,
  3154. info->phys_sca_base,
  3155. info->phys_memory_base,
  3156. info->phys_statctrl_base,
  3157. info->phys_lcr_base,
  3158. info->irq_level,
  3159. info->max_frame_size );
  3160. #ifdef CONFIG_HDLC
  3161. hdlcdev_init(info);
  3162. #endif
  3163. }
  3164. /* Allocate and initialize a device instance structure
  3165. *
  3166. * Return Value: pointer to SLMP_INFO if success, otherwise NULL
  3167. */
  3168. static SLMP_INFO *alloc_dev(int adapter_num, int port_num, struct pci_dev *pdev)
  3169. {
  3170. SLMP_INFO *info;
  3171. info = (SLMP_INFO *)kmalloc(sizeof(SLMP_INFO),
  3172. GFP_KERNEL);
  3173. if (!info) {
  3174. printk("%s(%d) Error can't allocate device instance data for adapter %d, port %d\n",
  3175. __FILE__,__LINE__, adapter_num, port_num);
  3176. } else {
  3177. memset(info, 0, sizeof(SLMP_INFO));
  3178. info->magic = MGSL_MAGIC;
  3179. INIT_WORK(&info->task, bh_handler, info);
  3180. info->max_frame_size = 4096;
  3181. info->close_delay = 5*HZ/10;
  3182. info->closing_wait = 30*HZ;
  3183. init_waitqueue_head(&info->open_wait);
  3184. init_waitqueue_head(&info->close_wait);
  3185. init_waitqueue_head(&info->status_event_wait_q);
  3186. init_waitqueue_head(&info->event_wait_q);
  3187. spin_lock_init(&info->netlock);
  3188. memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
  3189. info->idle_mode = HDLC_TXIDLE_FLAGS;
  3190. info->adapter_num = adapter_num;
  3191. info->port_num = port_num;
  3192. /* Copy configuration info to device instance data */
  3193. info->irq_level = pdev->irq;
  3194. info->phys_lcr_base = pci_resource_start(pdev,0);
  3195. info->phys_sca_base = pci_resource_start(pdev,2);
  3196. info->phys_memory_base = pci_resource_start(pdev,3);
  3197. info->phys_statctrl_base = pci_resource_start(pdev,4);
  3198. /* Because veremap only works on page boundaries we must map
  3199. * a larger area than is actually implemented for the LCR
  3200. * memory range. We map a full page starting at the page boundary.
  3201. */
  3202. info->lcr_offset = info->phys_lcr_base & (PAGE_SIZE-1);
  3203. info->phys_lcr_base &= ~(PAGE_SIZE-1);
  3204. info->sca_offset = info->phys_sca_base & (PAGE_SIZE-1);
  3205. info->phys_sca_base &= ~(PAGE_SIZE-1);
  3206. info->statctrl_offset = info->phys_statctrl_base & (PAGE_SIZE-1);
  3207. info->phys_statctrl_base &= ~(PAGE_SIZE-1);
  3208. info->bus_type = MGSL_BUS_TYPE_PCI;
  3209. info->irq_flags = SA_SHIRQ;
  3210. init_timer(&info->tx_timer);
  3211. info->tx_timer.data = (unsigned long)info;
  3212. info->tx_timer.function = tx_timeout;
  3213. init_timer(&info->status_timer);
  3214. info->status_timer.data = (unsigned long)info;
  3215. info->status_timer.function = status_timeout;
  3216. /* Store the PCI9050 misc control register value because a flaw
  3217. * in the PCI9050 prevents LCR registers from being read if
  3218. * BIOS assigns an LCR base address with bit 7 set.
  3219. *
  3220. * Only the misc control register is accessed for which only
  3221. * write access is needed, so set an initial value and change
  3222. * bits to the device instance data as we write the value
  3223. * to the actual misc control register.
  3224. */
  3225. info->misc_ctrl_value = 0x087e4546;
  3226. /* initial port state is unknown - if startup errors
  3227. * occur, init_error will be set to indicate the
  3228. * problem. Once the port is fully initialized,
  3229. * this value will be set to 0 to indicate the
  3230. * port is available.
  3231. */
  3232. info->init_error = -1;
  3233. }
  3234. return info;
  3235. }
  3236. void device_init(int adapter_num, struct pci_dev *pdev)
  3237. {
  3238. SLMP_INFO *port_array[SCA_MAX_PORTS];
  3239. int port;
  3240. /* allocate device instances for up to SCA_MAX_PORTS devices */
  3241. for ( port = 0; port < SCA_MAX_PORTS; ++port ) {
  3242. port_array[port] = alloc_dev(adapter_num,port,pdev);
  3243. if( port_array[port] == NULL ) {
  3244. for ( --port; port >= 0; --port )
  3245. kfree(port_array[port]);
  3246. return;
  3247. }
  3248. }
  3249. /* give copy of port_array to all ports and add to device list */
  3250. for ( port = 0; port < SCA_MAX_PORTS; ++port ) {
  3251. memcpy(port_array[port]->port_array,port_array,sizeof(port_array));
  3252. add_device( port_array[port] );
  3253. spin_lock_init(&port_array[port]->lock);
  3254. }
  3255. /* Allocate and claim adapter resources */
  3256. if ( !claim_resources(port_array[0]) ) {
  3257. alloc_dma_bufs(port_array[0]);
  3258. /* copy resource information from first port to others */
  3259. for ( port = 1; port < SCA_MAX_PORTS; ++port ) {
  3260. port_array[port]->lock = port_array[0]->lock;
  3261. port_array[port]->irq_level = port_array[0]->irq_level;
  3262. port_array[port]->memory_base = port_array[0]->memory_base;
  3263. port_array[port]->sca_base = port_array[0]->sca_base;
  3264. port_array[port]->statctrl_base = port_array[0]->statctrl_base;
  3265. port_array[port]->lcr_base = port_array[0]->lcr_base;
  3266. alloc_dma_bufs(port_array[port]);
  3267. }
  3268. if ( request_irq(port_array[0]->irq_level,
  3269. synclinkmp_interrupt,
  3270. port_array[0]->irq_flags,
  3271. port_array[0]->device_name,
  3272. port_array[0]) < 0 ) {
  3273. printk( "%s(%d):%s Cant request interrupt, IRQ=%d\n",
  3274. __FILE__,__LINE__,
  3275. port_array[0]->device_name,
  3276. port_array[0]->irq_level );
  3277. }
  3278. else {
  3279. port_array[0]->irq_requested = 1;
  3280. adapter_test(port_array[0]);
  3281. }
  3282. }
  3283. }
  3284. static struct tty_operations ops = {
  3285. .open = open,
  3286. .close = close,
  3287. .write = write,
  3288. .put_char = put_char,
  3289. .flush_chars = flush_chars,
  3290. .write_room = write_room,
  3291. .chars_in_buffer = chars_in_buffer,
  3292. .flush_buffer = flush_buffer,
  3293. .ioctl = ioctl,
  3294. .throttle = throttle,
  3295. .unthrottle = unthrottle,
  3296. .send_xchar = send_xchar,
  3297. .break_ctl = set_break,
  3298. .wait_until_sent = wait_until_sent,
  3299. .read_proc = read_proc,
  3300. .set_termios = set_termios,
  3301. .stop = tx_hold,
  3302. .start = tx_release,
  3303. .hangup = hangup,
  3304. .tiocmget = tiocmget,
  3305. .tiocmset = tiocmset,
  3306. };
  3307. static void synclinkmp_cleanup(void)
  3308. {
  3309. int rc;
  3310. SLMP_INFO *info;
  3311. SLMP_INFO *tmp;
  3312. printk("Unloading %s %s\n", driver_name, driver_version);
  3313. if (serial_driver) {
  3314. if ((rc = tty_unregister_driver(serial_driver)))
  3315. printk("%s(%d) failed to unregister tty driver err=%d\n",
  3316. __FILE__,__LINE__,rc);
  3317. put_tty_driver(serial_driver);
  3318. }
  3319. /* reset devices */
  3320. info = synclinkmp_device_list;
  3321. while(info) {
  3322. reset_port(info);
  3323. info = info->next_device;
  3324. }
  3325. /* release devices */
  3326. info = synclinkmp_device_list;
  3327. while(info) {
  3328. #ifdef CONFIG_HDLC
  3329. hdlcdev_exit(info);
  3330. #endif
  3331. free_dma_bufs(info);
  3332. free_tmp_rx_buf(info);
  3333. if ( info->port_num == 0 ) {
  3334. if (info->sca_base)
  3335. write_reg(info, LPR, 1); /* set low power mode */
  3336. release_resources(info);
  3337. }
  3338. tmp = info;
  3339. info = info->next_device;
  3340. kfree(tmp);
  3341. }
  3342. pci_unregister_driver(&synclinkmp_pci_driver);
  3343. }
  3344. /* Driver initialization entry point.
  3345. */
  3346. static int __init synclinkmp_init(void)
  3347. {
  3348. int rc;
  3349. if (break_on_load) {
  3350. synclinkmp_get_text_ptr();
  3351. BREAKPOINT();
  3352. }
  3353. printk("%s %s\n", driver_name, driver_version);
  3354. if ((rc = pci_register_driver(&synclinkmp_pci_driver)) < 0) {
  3355. printk("%s:failed to register PCI driver, error=%d\n",__FILE__,rc);
  3356. return rc;
  3357. }
  3358. serial_driver = alloc_tty_driver(128);
  3359. if (!serial_driver) {
  3360. rc = -ENOMEM;
  3361. goto error;
  3362. }
  3363. /* Initialize the tty_driver structure */
  3364. serial_driver->owner = THIS_MODULE;
  3365. serial_driver->driver_name = "synclinkmp";
  3366. serial_driver->name = "ttySLM";
  3367. serial_driver->major = ttymajor;
  3368. serial_driver->minor_start = 64;
  3369. serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  3370. serial_driver->subtype = SERIAL_TYPE_NORMAL;
  3371. serial_driver->init_termios = tty_std_termios;
  3372. serial_driver->init_termios.c_cflag =
  3373. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  3374. serial_driver->flags = TTY_DRIVER_REAL_RAW;
  3375. tty_set_operations(serial_driver, &ops);
  3376. if ((rc = tty_register_driver(serial_driver)) < 0) {
  3377. printk("%s(%d):Couldn't register serial driver\n",
  3378. __FILE__,__LINE__);
  3379. put_tty_driver(serial_driver);
  3380. serial_driver = NULL;
  3381. goto error;
  3382. }
  3383. printk("%s %s, tty major#%d\n",
  3384. driver_name, driver_version,
  3385. serial_driver->major);
  3386. return 0;
  3387. error:
  3388. synclinkmp_cleanup();
  3389. return rc;
  3390. }
  3391. static void __exit synclinkmp_exit(void)
  3392. {
  3393. synclinkmp_cleanup();
  3394. }
  3395. module_init(synclinkmp_init);
  3396. module_exit(synclinkmp_exit);
  3397. /* Set the port for internal loopback mode.
  3398. * The TxCLK and RxCLK signals are generated from the BRG and
  3399. * the TxD is looped back to the RxD internally.
  3400. */
  3401. void enable_loopback(SLMP_INFO *info, int enable)
  3402. {
  3403. if (enable) {
  3404. /* MD2 (Mode Register 2)
  3405. * 01..00 CNCT<1..0> Channel Connection 11=Local Loopback
  3406. */
  3407. write_reg(info, MD2, (unsigned char)(read_reg(info, MD2) | (BIT1 + BIT0)));
  3408. /* degate external TxC clock source */
  3409. info->port_array[0]->ctrlreg_value |= (BIT0 << (info->port_num * 2));
  3410. write_control_reg(info);
  3411. /* RXS/TXS (Rx/Tx clock source)
  3412. * 07 Reserved, must be 0
  3413. * 06..04 Clock Source, 100=BRG
  3414. * 03..00 Clock Divisor, 0000=1
  3415. */
  3416. write_reg(info, RXS, 0x40);
  3417. write_reg(info, TXS, 0x40);
  3418. } else {
  3419. /* MD2 (Mode Register 2)
  3420. * 01..00 CNCT<1..0> Channel connection, 0=normal
  3421. */
  3422. write_reg(info, MD2, (unsigned char)(read_reg(info, MD2) & ~(BIT1 + BIT0)));
  3423. /* RXS/TXS (Rx/Tx clock source)
  3424. * 07 Reserved, must be 0
  3425. * 06..04 Clock Source, 000=RxC/TxC Pin
  3426. * 03..00 Clock Divisor, 0000=1
  3427. */
  3428. write_reg(info, RXS, 0x00);
  3429. write_reg(info, TXS, 0x00);
  3430. }
  3431. /* set LinkSpeed if available, otherwise default to 2Mbps */
  3432. if (info->params.clock_speed)
  3433. set_rate(info, info->params.clock_speed);
  3434. else
  3435. set_rate(info, 3686400);
  3436. }
  3437. /* Set the baud rate register to the desired speed
  3438. *
  3439. * data_rate data rate of clock in bits per second
  3440. * A data rate of 0 disables the AUX clock.
  3441. */
  3442. void set_rate( SLMP_INFO *info, u32 data_rate )
  3443. {
  3444. u32 TMCValue;
  3445. unsigned char BRValue;
  3446. u32 Divisor=0;
  3447. /* fBRG = fCLK/(TMC * 2^BR)
  3448. */
  3449. if (data_rate != 0) {
  3450. Divisor = 14745600/data_rate;
  3451. if (!Divisor)
  3452. Divisor = 1;
  3453. TMCValue = Divisor;
  3454. BRValue = 0;
  3455. if (TMCValue != 1 && TMCValue != 2) {
  3456. /* BRValue of 0 provides 50/50 duty cycle *only* when
  3457. * TMCValue is 1 or 2. BRValue of 1 to 9 always provides
  3458. * 50/50 duty cycle.
  3459. */
  3460. BRValue = 1;
  3461. TMCValue >>= 1;
  3462. }
  3463. /* while TMCValue is too big for TMC register, divide
  3464. * by 2 and increment BR exponent.
  3465. */
  3466. for(; TMCValue > 256 && BRValue < 10; BRValue++)
  3467. TMCValue >>= 1;
  3468. write_reg(info, TXS,
  3469. (unsigned char)((read_reg(info, TXS) & 0xf0) | BRValue));
  3470. write_reg(info, RXS,
  3471. (unsigned char)((read_reg(info, RXS) & 0xf0) | BRValue));
  3472. write_reg(info, TMC, (unsigned char)TMCValue);
  3473. }
  3474. else {
  3475. write_reg(info, TXS,0);
  3476. write_reg(info, RXS,0);
  3477. write_reg(info, TMC, 0);
  3478. }
  3479. }
  3480. /* Disable receiver
  3481. */
  3482. void rx_stop(SLMP_INFO *info)
  3483. {
  3484. if (debug_level >= DEBUG_LEVEL_ISR)
  3485. printk("%s(%d):%s rx_stop()\n",
  3486. __FILE__,__LINE__, info->device_name );
  3487. write_reg(info, CMD, RXRESET);
  3488. info->ie0_value &= ~RXRDYE;
  3489. write_reg(info, IE0, info->ie0_value); /* disable Rx data interrupts */
  3490. write_reg(info, RXDMA + DSR, 0); /* disable Rx DMA */
  3491. write_reg(info, RXDMA + DCMD, SWABORT); /* reset/init Rx DMA */
  3492. write_reg(info, RXDMA + DIR, 0); /* disable Rx DMA interrupts */
  3493. info->rx_enabled = 0;
  3494. info->rx_overflow = 0;
  3495. }
  3496. /* enable the receiver
  3497. */
  3498. void rx_start(SLMP_INFO *info)
  3499. {
  3500. int i;
  3501. if (debug_level >= DEBUG_LEVEL_ISR)
  3502. printk("%s(%d):%s rx_start()\n",
  3503. __FILE__,__LINE__, info->device_name );
  3504. write_reg(info, CMD, RXRESET);
  3505. if ( info->params.mode == MGSL_MODE_HDLC ) {
  3506. /* HDLC, disabe IRQ on rxdata */
  3507. info->ie0_value &= ~RXRDYE;
  3508. write_reg(info, IE0, info->ie0_value);
  3509. /* Reset all Rx DMA buffers and program rx dma */
  3510. write_reg(info, RXDMA + DSR, 0); /* disable Rx DMA */
  3511. write_reg(info, RXDMA + DCMD, SWABORT); /* reset/init Rx DMA */
  3512. for (i = 0; i < info->rx_buf_count; i++) {
  3513. info->rx_buf_list[i].status = 0xff;
  3514. // throttle to 4 shared memory writes at a time to prevent
  3515. // hogging local bus (keep latency time for DMA requests low).
  3516. if (!(i % 4))
  3517. read_status_reg(info);
  3518. }
  3519. info->current_rx_buf = 0;
  3520. /* set current/1st descriptor address */
  3521. write_reg16(info, RXDMA + CDA,
  3522. info->rx_buf_list_ex[0].phys_entry);
  3523. /* set new last rx descriptor address */
  3524. write_reg16(info, RXDMA + EDA,
  3525. info->rx_buf_list_ex[info->rx_buf_count - 1].phys_entry);
  3526. /* set buffer length (shared by all rx dma data buffers) */
  3527. write_reg16(info, RXDMA + BFL, SCABUFSIZE);
  3528. write_reg(info, RXDMA + DIR, 0x60); /* enable Rx DMA interrupts (EOM/BOF) */
  3529. write_reg(info, RXDMA + DSR, 0xf2); /* clear Rx DMA IRQs, enable Rx DMA */
  3530. } else {
  3531. /* async, enable IRQ on rxdata */
  3532. info->ie0_value |= RXRDYE;
  3533. write_reg(info, IE0, info->ie0_value);
  3534. }
  3535. write_reg(info, CMD, RXENABLE);
  3536. info->rx_overflow = FALSE;
  3537. info->rx_enabled = 1;
  3538. }
  3539. /* Enable the transmitter and send a transmit frame if
  3540. * one is loaded in the DMA buffers.
  3541. */
  3542. void tx_start(SLMP_INFO *info)
  3543. {
  3544. if (debug_level >= DEBUG_LEVEL_ISR)
  3545. printk("%s(%d):%s tx_start() tx_count=%d\n",
  3546. __FILE__,__LINE__, info->device_name,info->tx_count );
  3547. if (!info->tx_enabled ) {
  3548. write_reg(info, CMD, TXRESET);
  3549. write_reg(info, CMD, TXENABLE);
  3550. info->tx_enabled = TRUE;
  3551. }
  3552. if ( info->tx_count ) {
  3553. /* If auto RTS enabled and RTS is inactive, then assert */
  3554. /* RTS and set a flag indicating that the driver should */
  3555. /* negate RTS when the transmission completes. */
  3556. info->drop_rts_on_tx_done = 0;
  3557. if (info->params.mode != MGSL_MODE_ASYNC) {
  3558. if ( info->params.flags & HDLC_FLAG_AUTO_RTS ) {
  3559. get_signals( info );
  3560. if ( !(info->serial_signals & SerialSignal_RTS) ) {
  3561. info->serial_signals |= SerialSignal_RTS;
  3562. set_signals( info );
  3563. info->drop_rts_on_tx_done = 1;
  3564. }
  3565. }
  3566. write_reg16(info, TRC0,
  3567. (unsigned short)(((tx_negate_fifo_level-1)<<8) + tx_active_fifo_level));
  3568. write_reg(info, TXDMA + DSR, 0); /* disable DMA channel */
  3569. write_reg(info, TXDMA + DCMD, SWABORT); /* reset/init DMA channel */
  3570. /* set TX CDA (current descriptor address) */
  3571. write_reg16(info, TXDMA + CDA,
  3572. info->tx_buf_list_ex[0].phys_entry);
  3573. /* set TX EDA (last descriptor address) */
  3574. write_reg16(info, TXDMA + EDA,
  3575. info->tx_buf_list_ex[info->last_tx_buf].phys_entry);
  3576. /* enable underrun IRQ */
  3577. info->ie1_value &= ~IDLE;
  3578. info->ie1_value |= UDRN;
  3579. write_reg(info, IE1, info->ie1_value);
  3580. write_reg(info, SR1, (unsigned char)(IDLE + UDRN));
  3581. write_reg(info, TXDMA + DIR, 0x40); /* enable Tx DMA interrupts (EOM) */
  3582. write_reg(info, TXDMA + DSR, 0xf2); /* clear Tx DMA IRQs, enable Tx DMA */
  3583. info->tx_timer.expires = jiffies + msecs_to_jiffies(5000);
  3584. add_timer(&info->tx_timer);
  3585. }
  3586. else {
  3587. tx_load_fifo(info);
  3588. /* async, enable IRQ on txdata */
  3589. info->ie0_value |= TXRDYE;
  3590. write_reg(info, IE0, info->ie0_value);
  3591. }
  3592. info->tx_active = 1;
  3593. }
  3594. }
  3595. /* stop the transmitter and DMA
  3596. */
  3597. void tx_stop( SLMP_INFO *info )
  3598. {
  3599. if (debug_level >= DEBUG_LEVEL_ISR)
  3600. printk("%s(%d):%s tx_stop()\n",
  3601. __FILE__,__LINE__, info->device_name );
  3602. del_timer(&info->tx_timer);
  3603. write_reg(info, TXDMA + DSR, 0); /* disable DMA channel */
  3604. write_reg(info, TXDMA + DCMD, SWABORT); /* reset/init DMA channel */
  3605. write_reg(info, CMD, TXRESET);
  3606. info->ie1_value &= ~(UDRN + IDLE);
  3607. write_reg(info, IE1, info->ie1_value); /* disable tx status interrupts */
  3608. write_reg(info, SR1, (unsigned char)(IDLE + UDRN)); /* clear pending */
  3609. info->ie0_value &= ~TXRDYE;
  3610. write_reg(info, IE0, info->ie0_value); /* disable tx data interrupts */
  3611. info->tx_enabled = 0;
  3612. info->tx_active = 0;
  3613. }
  3614. /* Fill the transmit FIFO until the FIFO is full or
  3615. * there is no more data to load.
  3616. */
  3617. void tx_load_fifo(SLMP_INFO *info)
  3618. {
  3619. u8 TwoBytes[2];
  3620. /* do nothing is now tx data available and no XON/XOFF pending */
  3621. if ( !info->tx_count && !info->x_char )
  3622. return;
  3623. /* load the Transmit FIFO until FIFOs full or all data sent */
  3624. while( info->tx_count && (read_reg(info,SR0) & BIT1) ) {
  3625. /* there is more space in the transmit FIFO and */
  3626. /* there is more data in transmit buffer */
  3627. if ( (info->tx_count > 1) && !info->x_char ) {
  3628. /* write 16-bits */
  3629. TwoBytes[0] = info->tx_buf[info->tx_get++];
  3630. if (info->tx_get >= info->max_frame_size)
  3631. info->tx_get -= info->max_frame_size;
  3632. TwoBytes[1] = info->tx_buf[info->tx_get++];
  3633. if (info->tx_get >= info->max_frame_size)
  3634. info->tx_get -= info->max_frame_size;
  3635. write_reg16(info, TRB, *((u16 *)TwoBytes));
  3636. info->tx_count -= 2;
  3637. info->icount.tx += 2;
  3638. } else {
  3639. /* only 1 byte left to transmit or 1 FIFO slot left */
  3640. if (info->x_char) {
  3641. /* transmit pending high priority char */
  3642. write_reg(info, TRB, info->x_char);
  3643. info->x_char = 0;
  3644. } else {
  3645. write_reg(info, TRB, info->tx_buf[info->tx_get++]);
  3646. if (info->tx_get >= info->max_frame_size)
  3647. info->tx_get -= info->max_frame_size;
  3648. info->tx_count--;
  3649. }
  3650. info->icount.tx++;
  3651. }
  3652. }
  3653. }
  3654. /* Reset a port to a known state
  3655. */
  3656. void reset_port(SLMP_INFO *info)
  3657. {
  3658. if (info->sca_base) {
  3659. tx_stop(info);
  3660. rx_stop(info);
  3661. info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
  3662. set_signals(info);
  3663. /* disable all port interrupts */
  3664. info->ie0_value = 0;
  3665. info->ie1_value = 0;
  3666. info->ie2_value = 0;
  3667. write_reg(info, IE0, info->ie0_value);
  3668. write_reg(info, IE1, info->ie1_value);
  3669. write_reg(info, IE2, info->ie2_value);
  3670. write_reg(info, CMD, CHRESET);
  3671. }
  3672. }
  3673. /* Reset all the ports to a known state.
  3674. */
  3675. void reset_adapter(SLMP_INFO *info)
  3676. {
  3677. int i;
  3678. for ( i=0; i < SCA_MAX_PORTS; ++i) {
  3679. if (info->port_array[i])
  3680. reset_port(info->port_array[i]);
  3681. }
  3682. }
  3683. /* Program port for asynchronous communications.
  3684. */
  3685. void async_mode(SLMP_INFO *info)
  3686. {
  3687. unsigned char RegValue;
  3688. tx_stop(info);
  3689. rx_stop(info);
  3690. /* MD0, Mode Register 0
  3691. *
  3692. * 07..05 PRCTL<2..0>, Protocol Mode, 000=async
  3693. * 04 AUTO, Auto-enable (RTS/CTS/DCD)
  3694. * 03 Reserved, must be 0
  3695. * 02 CRCCC, CRC Calculation, 0=disabled
  3696. * 01..00 STOP<1..0> Stop bits (00=1,10=2)
  3697. *
  3698. * 0000 0000
  3699. */
  3700. RegValue = 0x00;
  3701. if (info->params.stop_bits != 1)
  3702. RegValue |= BIT1;
  3703. write_reg(info, MD0, RegValue);
  3704. /* MD1, Mode Register 1
  3705. *
  3706. * 07..06 BRATE<1..0>, bit rate, 00=1/1 01=1/16 10=1/32 11=1/64
  3707. * 05..04 TXCHR<1..0>, tx char size, 00=8 bits,01=7,10=6,11=5
  3708. * 03..02 RXCHR<1..0>, rx char size
  3709. * 01..00 PMPM<1..0>, Parity mode, 00=none 10=even 11=odd
  3710. *
  3711. * 0100 0000
  3712. */
  3713. RegValue = 0x40;
  3714. switch (info->params.data_bits) {
  3715. case 7: RegValue |= BIT4 + BIT2; break;
  3716. case 6: RegValue |= BIT5 + BIT3; break;
  3717. case 5: RegValue |= BIT5 + BIT4 + BIT3 + BIT2; break;
  3718. }
  3719. if (info->params.parity != ASYNC_PARITY_NONE) {
  3720. RegValue |= BIT1;
  3721. if (info->params.parity == ASYNC_PARITY_ODD)
  3722. RegValue |= BIT0;
  3723. }
  3724. write_reg(info, MD1, RegValue);
  3725. /* MD2, Mode Register 2
  3726. *
  3727. * 07..02 Reserved, must be 0
  3728. * 01..00 CNCT<1..0> Channel connection, 00=normal 11=local loopback
  3729. *
  3730. * 0000 0000
  3731. */
  3732. RegValue = 0x00;
  3733. if (info->params.loopback)
  3734. RegValue |= (BIT1 + BIT0);
  3735. write_reg(info, MD2, RegValue);
  3736. /* RXS, Receive clock source
  3737. *
  3738. * 07 Reserved, must be 0
  3739. * 06..04 RXCS<2..0>, clock source, 000=RxC Pin, 100=BRG, 110=DPLL
  3740. * 03..00 RXBR<3..0>, rate divisor, 0000=1
  3741. */
  3742. RegValue=BIT6;
  3743. write_reg(info, RXS, RegValue);
  3744. /* TXS, Transmit clock source
  3745. *
  3746. * 07 Reserved, must be 0
  3747. * 06..04 RXCS<2..0>, clock source, 000=TxC Pin, 100=BRG, 110=Receive Clock
  3748. * 03..00 RXBR<3..0>, rate divisor, 0000=1
  3749. */
  3750. RegValue=BIT6;
  3751. write_reg(info, TXS, RegValue);
  3752. /* Control Register
  3753. *
  3754. * 6,4,2,0 CLKSEL<3..0>, 0 = TcCLK in, 1 = Auxclk out
  3755. */
  3756. info->port_array[0]->ctrlreg_value |= (BIT0 << (info->port_num * 2));
  3757. write_control_reg(info);
  3758. tx_set_idle(info);
  3759. /* RRC Receive Ready Control 0
  3760. *
  3761. * 07..05 Reserved, must be 0
  3762. * 04..00 RRC<4..0> Rx FIFO trigger active 0x00 = 1 byte
  3763. */
  3764. write_reg(info, RRC, 0x00);
  3765. /* TRC0 Transmit Ready Control 0
  3766. *
  3767. * 07..05 Reserved, must be 0
  3768. * 04..00 TRC<4..0> Tx FIFO trigger active 0x10 = 16 bytes
  3769. */
  3770. write_reg(info, TRC0, 0x10);
  3771. /* TRC1 Transmit Ready Control 1
  3772. *
  3773. * 07..05 Reserved, must be 0
  3774. * 04..00 TRC<4..0> Tx FIFO trigger inactive 0x1e = 31 bytes (full-1)
  3775. */
  3776. write_reg(info, TRC1, 0x1e);
  3777. /* CTL, MSCI control register
  3778. *
  3779. * 07..06 Reserved, set to 0
  3780. * 05 UDRNC, underrun control, 0=abort 1=CRC+flag (HDLC/BSC)
  3781. * 04 IDLC, idle control, 0=mark 1=idle register
  3782. * 03 BRK, break, 0=off 1 =on (async)
  3783. * 02 SYNCLD, sync char load enable (BSC) 1=enabled
  3784. * 01 GOP, go active on poll (LOOP mode) 1=enabled
  3785. * 00 RTS, RTS output control, 0=active 1=inactive
  3786. *
  3787. * 0001 0001
  3788. */
  3789. RegValue = 0x10;
  3790. if (!(info->serial_signals & SerialSignal_RTS))
  3791. RegValue |= 0x01;
  3792. write_reg(info, CTL, RegValue);
  3793. /* enable status interrupts */
  3794. info->ie0_value |= TXINTE + RXINTE;
  3795. write_reg(info, IE0, info->ie0_value);
  3796. /* enable break detect interrupt */
  3797. info->ie1_value = BRKD;
  3798. write_reg(info, IE1, info->ie1_value);
  3799. /* enable rx overrun interrupt */
  3800. info->ie2_value = OVRN;
  3801. write_reg(info, IE2, info->ie2_value);
  3802. set_rate( info, info->params.data_rate * 16 );
  3803. }
  3804. /* Program the SCA for HDLC communications.
  3805. */
  3806. void hdlc_mode(SLMP_INFO *info)
  3807. {
  3808. unsigned char RegValue;
  3809. u32 DpllDivisor;
  3810. // Can't use DPLL because SCA outputs recovered clock on RxC when
  3811. // DPLL mode selected. This causes output contention with RxC receiver.
  3812. // Use of DPLL would require external hardware to disable RxC receiver
  3813. // when DPLL mode selected.
  3814. info->params.flags &= ~(HDLC_FLAG_TXC_DPLL + HDLC_FLAG_RXC_DPLL);
  3815. /* disable DMA interrupts */
  3816. write_reg(info, TXDMA + DIR, 0);
  3817. write_reg(info, RXDMA + DIR, 0);
  3818. /* MD0, Mode Register 0
  3819. *
  3820. * 07..05 PRCTL<2..0>, Protocol Mode, 100=HDLC
  3821. * 04 AUTO, Auto-enable (RTS/CTS/DCD)
  3822. * 03 Reserved, must be 0
  3823. * 02 CRCCC, CRC Calculation, 1=enabled
  3824. * 01 CRC1, CRC selection, 0=CRC-16,1=CRC-CCITT-16
  3825. * 00 CRC0, CRC initial value, 1 = all 1s
  3826. *
  3827. * 1000 0001
  3828. */
  3829. RegValue = 0x81;
  3830. if (info->params.flags & HDLC_FLAG_AUTO_CTS)
  3831. RegValue |= BIT4;
  3832. if (info->params.flags & HDLC_FLAG_AUTO_DCD)
  3833. RegValue |= BIT4;
  3834. if (info->params.crc_type == HDLC_CRC_16_CCITT)
  3835. RegValue |= BIT2 + BIT1;
  3836. write_reg(info, MD0, RegValue);
  3837. /* MD1, Mode Register 1
  3838. *
  3839. * 07..06 ADDRS<1..0>, Address detect, 00=no addr check
  3840. * 05..04 TXCHR<1..0>, tx char size, 00=8 bits
  3841. * 03..02 RXCHR<1..0>, rx char size, 00=8 bits
  3842. * 01..00 PMPM<1..0>, Parity mode, 00=no parity
  3843. *
  3844. * 0000 0000
  3845. */
  3846. RegValue = 0x00;
  3847. write_reg(info, MD1, RegValue);
  3848. /* MD2, Mode Register 2
  3849. *
  3850. * 07 NRZFM, 0=NRZ, 1=FM
  3851. * 06..05 CODE<1..0> Encoding, 00=NRZ
  3852. * 04..03 DRATE<1..0> DPLL Divisor, 00=8
  3853. * 02 Reserved, must be 0
  3854. * 01..00 CNCT<1..0> Channel connection, 0=normal
  3855. *
  3856. * 0000 0000
  3857. */
  3858. RegValue = 0x00;
  3859. switch(info->params.encoding) {
  3860. case HDLC_ENCODING_NRZI: RegValue |= BIT5; break;
  3861. case HDLC_ENCODING_BIPHASE_MARK: RegValue |= BIT7 + BIT5; break; /* aka FM1 */
  3862. case HDLC_ENCODING_BIPHASE_SPACE: RegValue |= BIT7 + BIT6; break; /* aka FM0 */
  3863. case HDLC_ENCODING_BIPHASE_LEVEL: RegValue |= BIT7; break; /* aka Manchester */
  3864. #if 0
  3865. case HDLC_ENCODING_NRZB: /* not supported */
  3866. case HDLC_ENCODING_NRZI_MARK: /* not supported */
  3867. case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: /* not supported */
  3868. #endif
  3869. }
  3870. if ( info->params.flags & HDLC_FLAG_DPLL_DIV16 ) {
  3871. DpllDivisor = 16;
  3872. RegValue |= BIT3;
  3873. } else if ( info->params.flags & HDLC_FLAG_DPLL_DIV8 ) {
  3874. DpllDivisor = 8;
  3875. } else {
  3876. DpllDivisor = 32;
  3877. RegValue |= BIT4;
  3878. }
  3879. write_reg(info, MD2, RegValue);
  3880. /* RXS, Receive clock source
  3881. *
  3882. * 07 Reserved, must be 0
  3883. * 06..04 RXCS<2..0>, clock source, 000=RxC Pin, 100=BRG, 110=DPLL
  3884. * 03..00 RXBR<3..0>, rate divisor, 0000=1
  3885. */
  3886. RegValue=0;
  3887. if (info->params.flags & HDLC_FLAG_RXC_BRG)
  3888. RegValue |= BIT6;
  3889. if (info->params.flags & HDLC_FLAG_RXC_DPLL)
  3890. RegValue |= BIT6 + BIT5;
  3891. write_reg(info, RXS, RegValue);
  3892. /* TXS, Transmit clock source
  3893. *
  3894. * 07 Reserved, must be 0
  3895. * 06..04 RXCS<2..0>, clock source, 000=TxC Pin, 100=BRG, 110=Receive Clock
  3896. * 03..00 RXBR<3..0>, rate divisor, 0000=1
  3897. */
  3898. RegValue=0;
  3899. if (info->params.flags & HDLC_FLAG_TXC_BRG)
  3900. RegValue |= BIT6;
  3901. if (info->params.flags & HDLC_FLAG_TXC_DPLL)
  3902. RegValue |= BIT6 + BIT5;
  3903. write_reg(info, TXS, RegValue);
  3904. if (info->params.flags & HDLC_FLAG_RXC_DPLL)
  3905. set_rate(info, info->params.clock_speed * DpllDivisor);
  3906. else
  3907. set_rate(info, info->params.clock_speed);
  3908. /* GPDATA (General Purpose I/O Data Register)
  3909. *
  3910. * 6,4,2,0 CLKSEL<3..0>, 0 = TcCLK in, 1 = Auxclk out
  3911. */
  3912. if (info->params.flags & HDLC_FLAG_TXC_BRG)
  3913. info->port_array[0]->ctrlreg_value |= (BIT0 << (info->port_num * 2));
  3914. else
  3915. info->port_array[0]->ctrlreg_value &= ~(BIT0 << (info->port_num * 2));
  3916. write_control_reg(info);
  3917. /* RRC Receive Ready Control 0
  3918. *
  3919. * 07..05 Reserved, must be 0
  3920. * 04..00 RRC<4..0> Rx FIFO trigger active
  3921. */
  3922. write_reg(info, RRC, rx_active_fifo_level);
  3923. /* TRC0 Transmit Ready Control 0
  3924. *
  3925. * 07..05 Reserved, must be 0
  3926. * 04..00 TRC<4..0> Tx FIFO trigger active
  3927. */
  3928. write_reg(info, TRC0, tx_active_fifo_level);
  3929. /* TRC1 Transmit Ready Control 1
  3930. *
  3931. * 07..05 Reserved, must be 0
  3932. * 04..00 TRC<4..0> Tx FIFO trigger inactive 0x1f = 32 bytes (full)
  3933. */
  3934. write_reg(info, TRC1, (unsigned char)(tx_negate_fifo_level - 1));
  3935. /* DMR, DMA Mode Register
  3936. *
  3937. * 07..05 Reserved, must be 0
  3938. * 04 TMOD, Transfer Mode: 1=chained-block
  3939. * 03 Reserved, must be 0
  3940. * 02 NF, Number of Frames: 1=multi-frame
  3941. * 01 CNTE, Frame End IRQ Counter enable: 0=disabled
  3942. * 00 Reserved, must be 0
  3943. *
  3944. * 0001 0100
  3945. */
  3946. write_reg(info, TXDMA + DMR, 0x14);
  3947. write_reg(info, RXDMA + DMR, 0x14);
  3948. /* Set chain pointer base (upper 8 bits of 24 bit addr) */
  3949. write_reg(info, RXDMA + CPB,
  3950. (unsigned char)(info->buffer_list_phys >> 16));
  3951. /* Set chain pointer base (upper 8 bits of 24 bit addr) */
  3952. write_reg(info, TXDMA + CPB,
  3953. (unsigned char)(info->buffer_list_phys >> 16));
  3954. /* enable status interrupts. other code enables/disables
  3955. * the individual sources for these two interrupt classes.
  3956. */
  3957. info->ie0_value |= TXINTE + RXINTE;
  3958. write_reg(info, IE0, info->ie0_value);
  3959. /* CTL, MSCI control register
  3960. *
  3961. * 07..06 Reserved, set to 0
  3962. * 05 UDRNC, underrun control, 0=abort 1=CRC+flag (HDLC/BSC)
  3963. * 04 IDLC, idle control, 0=mark 1=idle register
  3964. * 03 BRK, break, 0=off 1 =on (async)
  3965. * 02 SYNCLD, sync char load enable (BSC) 1=enabled
  3966. * 01 GOP, go active on poll (LOOP mode) 1=enabled
  3967. * 00 RTS, RTS output control, 0=active 1=inactive
  3968. *
  3969. * 0001 0001
  3970. */
  3971. RegValue = 0x10;
  3972. if (!(info->serial_signals & SerialSignal_RTS))
  3973. RegValue |= 0x01;
  3974. write_reg(info, CTL, RegValue);
  3975. /* preamble not supported ! */
  3976. tx_set_idle(info);
  3977. tx_stop(info);
  3978. rx_stop(info);
  3979. set_rate(info, info->params.clock_speed);
  3980. if (info->params.loopback)
  3981. enable_loopback(info,1);
  3982. }
  3983. /* Set the transmit HDLC idle mode
  3984. */
  3985. void tx_set_idle(SLMP_INFO *info)
  3986. {
  3987. unsigned char RegValue = 0xff;
  3988. /* Map API idle mode to SCA register bits */
  3989. switch(info->idle_mode) {
  3990. case HDLC_TXIDLE_FLAGS: RegValue = 0x7e; break;
  3991. case HDLC_TXIDLE_ALT_ZEROS_ONES: RegValue = 0xaa; break;
  3992. case HDLC_TXIDLE_ZEROS: RegValue = 0x00; break;
  3993. case HDLC_TXIDLE_ONES: RegValue = 0xff; break;
  3994. case HDLC_TXIDLE_ALT_MARK_SPACE: RegValue = 0xaa; break;
  3995. case HDLC_TXIDLE_SPACE: RegValue = 0x00; break;
  3996. case HDLC_TXIDLE_MARK: RegValue = 0xff; break;
  3997. }
  3998. write_reg(info, IDL, RegValue);
  3999. }
  4000. /* Query the adapter for the state of the V24 status (input) signals.
  4001. */
  4002. void get_signals(SLMP_INFO *info)
  4003. {
  4004. u16 status = read_reg(info, SR3);
  4005. u16 gpstatus = read_status_reg(info);
  4006. u16 testbit;
  4007. /* clear all serial signals except DTR and RTS */
  4008. info->serial_signals &= SerialSignal_DTR + SerialSignal_RTS;
  4009. /* set serial signal bits to reflect MISR */
  4010. if (!(status & BIT3))
  4011. info->serial_signals |= SerialSignal_CTS;
  4012. if ( !(status & BIT2))
  4013. info->serial_signals |= SerialSignal_DCD;
  4014. testbit = BIT1 << (info->port_num * 2); // Port 0..3 RI is GPDATA<1,3,5,7>
  4015. if (!(gpstatus & testbit))
  4016. info->serial_signals |= SerialSignal_RI;
  4017. testbit = BIT0 << (info->port_num * 2); // Port 0..3 DSR is GPDATA<0,2,4,6>
  4018. if (!(gpstatus & testbit))
  4019. info->serial_signals |= SerialSignal_DSR;
  4020. }
  4021. /* Set the state of DTR and RTS based on contents of
  4022. * serial_signals member of device context.
  4023. */
  4024. void set_signals(SLMP_INFO *info)
  4025. {
  4026. unsigned char RegValue;
  4027. u16 EnableBit;
  4028. RegValue = read_reg(info, CTL);
  4029. if (info->serial_signals & SerialSignal_RTS)
  4030. RegValue &= ~BIT0;
  4031. else
  4032. RegValue |= BIT0;
  4033. write_reg(info, CTL, RegValue);
  4034. // Port 0..3 DTR is ctrl reg <1,3,5,7>
  4035. EnableBit = BIT1 << (info->port_num*2);
  4036. if (info->serial_signals & SerialSignal_DTR)
  4037. info->port_array[0]->ctrlreg_value &= ~EnableBit;
  4038. else
  4039. info->port_array[0]->ctrlreg_value |= EnableBit;
  4040. write_control_reg(info);
  4041. }
  4042. /*******************/
  4043. /* DMA Buffer Code */
  4044. /*******************/
  4045. /* Set the count for all receive buffers to SCABUFSIZE
  4046. * and set the current buffer to the first buffer. This effectively
  4047. * makes all buffers free and discards any data in buffers.
  4048. */
  4049. void rx_reset_buffers(SLMP_INFO *info)
  4050. {
  4051. rx_free_frame_buffers(info, 0, info->rx_buf_count - 1);
  4052. }
  4053. /* Free the buffers used by a received frame
  4054. *
  4055. * info pointer to device instance data
  4056. * first index of 1st receive buffer of frame
  4057. * last index of last receive buffer of frame
  4058. */
  4059. void rx_free_frame_buffers(SLMP_INFO *info, unsigned int first, unsigned int last)
  4060. {
  4061. int done = 0;
  4062. while(!done) {
  4063. /* reset current buffer for reuse */
  4064. info->rx_buf_list[first].status = 0xff;
  4065. if (first == last) {
  4066. done = 1;
  4067. /* set new last rx descriptor address */
  4068. write_reg16(info, RXDMA + EDA, info->rx_buf_list_ex[first].phys_entry);
  4069. }
  4070. first++;
  4071. if (first == info->rx_buf_count)
  4072. first = 0;
  4073. }
  4074. /* set current buffer to next buffer after last buffer of frame */
  4075. info->current_rx_buf = first;
  4076. }
  4077. /* Return a received frame from the receive DMA buffers.
  4078. * Only frames received without errors are returned.
  4079. *
  4080. * Return Value: 1 if frame returned, otherwise 0
  4081. */
  4082. int rx_get_frame(SLMP_INFO *info)
  4083. {
  4084. unsigned int StartIndex, EndIndex; /* index of 1st and last buffers of Rx frame */
  4085. unsigned short status;
  4086. unsigned int framesize = 0;
  4087. int ReturnCode = 0;
  4088. unsigned long flags;
  4089. struct tty_struct *tty = info->tty;
  4090. unsigned char addr_field = 0xff;
  4091. SCADESC *desc;
  4092. SCADESC_EX *desc_ex;
  4093. CheckAgain:
  4094. /* assume no frame returned, set zero length */
  4095. framesize = 0;
  4096. addr_field = 0xff;
  4097. /*
  4098. * current_rx_buf points to the 1st buffer of the next available
  4099. * receive frame. To find the last buffer of the frame look for
  4100. * a non-zero status field in the buffer entries. (The status
  4101. * field is set by the 16C32 after completing a receive frame.
  4102. */
  4103. StartIndex = EndIndex = info->current_rx_buf;
  4104. for ( ;; ) {
  4105. desc = &info->rx_buf_list[EndIndex];
  4106. desc_ex = &info->rx_buf_list_ex[EndIndex];
  4107. if (desc->status == 0xff)
  4108. goto Cleanup; /* current desc still in use, no frames available */
  4109. if (framesize == 0 && info->params.addr_filter != 0xff)
  4110. addr_field = desc_ex->virt_addr[0];
  4111. framesize += desc->length;
  4112. /* Status != 0 means last buffer of frame */
  4113. if (desc->status)
  4114. break;
  4115. EndIndex++;
  4116. if (EndIndex == info->rx_buf_count)
  4117. EndIndex = 0;
  4118. if (EndIndex == info->current_rx_buf) {
  4119. /* all buffers have been 'used' but none mark */
  4120. /* the end of a frame. Reset buffers and receiver. */
  4121. if ( info->rx_enabled ){
  4122. spin_lock_irqsave(&info->lock,flags);
  4123. rx_start(info);
  4124. spin_unlock_irqrestore(&info->lock,flags);
  4125. }
  4126. goto Cleanup;
  4127. }
  4128. }
  4129. /* check status of receive frame */
  4130. /* frame status is byte stored after frame data
  4131. *
  4132. * 7 EOM (end of msg), 1 = last buffer of frame
  4133. * 6 Short Frame, 1 = short frame
  4134. * 5 Abort, 1 = frame aborted
  4135. * 4 Residue, 1 = last byte is partial
  4136. * 3 Overrun, 1 = overrun occurred during frame reception
  4137. * 2 CRC, 1 = CRC error detected
  4138. *
  4139. */
  4140. status = desc->status;
  4141. /* ignore CRC bit if not using CRC (bit is undefined) */
  4142. /* Note:CRC is not save to data buffer */
  4143. if (info->params.crc_type == HDLC_CRC_NONE)
  4144. status &= ~BIT2;
  4145. if (framesize == 0 ||
  4146. (addr_field != 0xff && addr_field != info->params.addr_filter)) {
  4147. /* discard 0 byte frames, this seems to occur sometime
  4148. * when remote is idling flags.
  4149. */
  4150. rx_free_frame_buffers(info, StartIndex, EndIndex);
  4151. goto CheckAgain;
  4152. }
  4153. if (framesize < 2)
  4154. status |= BIT6;
  4155. if (status & (BIT6+BIT5+BIT3+BIT2)) {
  4156. /* received frame has errors,
  4157. * update counts and mark frame size as 0
  4158. */
  4159. if (status & BIT6)
  4160. info->icount.rxshort++;
  4161. else if (status & BIT5)
  4162. info->icount.rxabort++;
  4163. else if (status & BIT3)
  4164. info->icount.rxover++;
  4165. else
  4166. info->icount.rxcrc++;
  4167. framesize = 0;
  4168. #ifdef CONFIG_HDLC
  4169. {
  4170. struct net_device_stats *stats = hdlc_stats(info->netdev);
  4171. stats->rx_errors++;
  4172. stats->rx_frame_errors++;
  4173. }
  4174. #endif
  4175. }
  4176. if ( debug_level >= DEBUG_LEVEL_BH )
  4177. printk("%s(%d):%s rx_get_frame() status=%04X size=%d\n",
  4178. __FILE__,__LINE__,info->device_name,status,framesize);
  4179. if ( debug_level >= DEBUG_LEVEL_DATA )
  4180. trace_block(info,info->rx_buf_list_ex[StartIndex].virt_addr,
  4181. min_t(int, framesize,SCABUFSIZE),0);
  4182. if (framesize) {
  4183. if (framesize > info->max_frame_size)
  4184. info->icount.rxlong++;
  4185. else {
  4186. /* copy dma buffer(s) to contiguous intermediate buffer */
  4187. int copy_count = framesize;
  4188. int index = StartIndex;
  4189. unsigned char *ptmp = info->tmp_rx_buf;
  4190. info->tmp_rx_buf_count = framesize;
  4191. info->icount.rxok++;
  4192. while(copy_count) {
  4193. int partial_count = min(copy_count,SCABUFSIZE);
  4194. memcpy( ptmp,
  4195. info->rx_buf_list_ex[index].virt_addr,
  4196. partial_count );
  4197. ptmp += partial_count;
  4198. copy_count -= partial_count;
  4199. if ( ++index == info->rx_buf_count )
  4200. index = 0;
  4201. }
  4202. #ifdef CONFIG_HDLC
  4203. if (info->netcount)
  4204. hdlcdev_rx(info,info->tmp_rx_buf,framesize);
  4205. else
  4206. #endif
  4207. ldisc_receive_buf(tty,info->tmp_rx_buf,
  4208. info->flag_buf, framesize);
  4209. }
  4210. }
  4211. /* Free the buffers used by this frame. */
  4212. rx_free_frame_buffers( info, StartIndex, EndIndex );
  4213. ReturnCode = 1;
  4214. Cleanup:
  4215. if ( info->rx_enabled && info->rx_overflow ) {
  4216. /* Receiver is enabled, but needs to restarted due to
  4217. * rx buffer overflow. If buffers are empty, restart receiver.
  4218. */
  4219. if (info->rx_buf_list[EndIndex].status == 0xff) {
  4220. spin_lock_irqsave(&info->lock,flags);
  4221. rx_start(info);
  4222. spin_unlock_irqrestore(&info->lock,flags);
  4223. }
  4224. }
  4225. return ReturnCode;
  4226. }
  4227. /* load the transmit DMA buffer with data
  4228. */
  4229. void tx_load_dma_buffer(SLMP_INFO *info, const char *buf, unsigned int count)
  4230. {
  4231. unsigned short copy_count;
  4232. unsigned int i = 0;
  4233. SCADESC *desc;
  4234. SCADESC_EX *desc_ex;
  4235. if ( debug_level >= DEBUG_LEVEL_DATA )
  4236. trace_block(info,buf, min_t(int, count,SCABUFSIZE), 1);
  4237. /* Copy source buffer to one or more DMA buffers, starting with
  4238. * the first transmit dma buffer.
  4239. */
  4240. for(i=0;;)
  4241. {
  4242. copy_count = min_t(unsigned short,count,SCABUFSIZE);
  4243. desc = &info->tx_buf_list[i];
  4244. desc_ex = &info->tx_buf_list_ex[i];
  4245. load_pci_memory(info, desc_ex->virt_addr,buf,copy_count);
  4246. desc->length = copy_count;
  4247. desc->status = 0;
  4248. buf += copy_count;
  4249. count -= copy_count;
  4250. if (!count)
  4251. break;
  4252. i++;
  4253. if (i >= info->tx_buf_count)
  4254. i = 0;
  4255. }
  4256. info->tx_buf_list[i].status = 0x81; /* set EOM and EOT status */
  4257. info->last_tx_buf = ++i;
  4258. }
  4259. int register_test(SLMP_INFO *info)
  4260. {
  4261. static unsigned char testval[] = {0x00, 0xff, 0xaa, 0x55, 0x69, 0x96};
  4262. static unsigned int count = sizeof(testval)/sizeof(unsigned char);
  4263. unsigned int i;
  4264. int rc = TRUE;
  4265. unsigned long flags;
  4266. spin_lock_irqsave(&info->lock,flags);
  4267. reset_port(info);
  4268. /* assume failure */
  4269. info->init_error = DiagStatus_AddressFailure;
  4270. /* Write bit patterns to various registers but do it out of */
  4271. /* sync, then read back and verify values. */
  4272. for (i = 0 ; i < count ; i++) {
  4273. write_reg(info, TMC, testval[i]);
  4274. write_reg(info, IDL, testval[(i+1)%count]);
  4275. write_reg(info, SA0, testval[(i+2)%count]);
  4276. write_reg(info, SA1, testval[(i+3)%count]);
  4277. if ( (read_reg(info, TMC) != testval[i]) ||
  4278. (read_reg(info, IDL) != testval[(i+1)%count]) ||
  4279. (read_reg(info, SA0) != testval[(i+2)%count]) ||
  4280. (read_reg(info, SA1) != testval[(i+3)%count]) )
  4281. {
  4282. rc = FALSE;
  4283. break;
  4284. }
  4285. }
  4286. reset_port(info);
  4287. spin_unlock_irqrestore(&info->lock,flags);
  4288. return rc;
  4289. }
  4290. int irq_test(SLMP_INFO *info)
  4291. {
  4292. unsigned long timeout;
  4293. unsigned long flags;
  4294. unsigned char timer = (info->port_num & 1) ? TIMER2 : TIMER0;
  4295. spin_lock_irqsave(&info->lock,flags);
  4296. reset_port(info);
  4297. /* assume failure */
  4298. info->init_error = DiagStatus_IrqFailure;
  4299. info->irq_occurred = FALSE;
  4300. /* setup timer0 on SCA0 to interrupt */
  4301. /* IER2<7..4> = timer<3..0> interrupt enables (1=enabled) */
  4302. write_reg(info, IER2, (unsigned char)((info->port_num & 1) ? BIT6 : BIT4));
  4303. write_reg(info, (unsigned char)(timer + TEPR), 0); /* timer expand prescale */
  4304. write_reg16(info, (unsigned char)(timer + TCONR), 1); /* timer constant */
  4305. /* TMCS, Timer Control/Status Register
  4306. *
  4307. * 07 CMF, Compare match flag (read only) 1=match
  4308. * 06 ECMI, CMF Interrupt Enable: 1=enabled
  4309. * 05 Reserved, must be 0
  4310. * 04 TME, Timer Enable
  4311. * 03..00 Reserved, must be 0
  4312. *
  4313. * 0101 0000
  4314. */
  4315. write_reg(info, (unsigned char)(timer + TMCS), 0x50);
  4316. spin_unlock_irqrestore(&info->lock,flags);
  4317. timeout=100;
  4318. while( timeout-- && !info->irq_occurred ) {
  4319. msleep_interruptible(10);
  4320. }
  4321. spin_lock_irqsave(&info->lock,flags);
  4322. reset_port(info);
  4323. spin_unlock_irqrestore(&info->lock,flags);
  4324. return info->irq_occurred;
  4325. }
  4326. /* initialize individual SCA device (2 ports)
  4327. */
  4328. static int sca_init(SLMP_INFO *info)
  4329. {
  4330. /* set wait controller to single mem partition (low), no wait states */
  4331. write_reg(info, PABR0, 0); /* wait controller addr boundary 0 */
  4332. write_reg(info, PABR1, 0); /* wait controller addr boundary 1 */
  4333. write_reg(info, WCRL, 0); /* wait controller low range */
  4334. write_reg(info, WCRM, 0); /* wait controller mid range */
  4335. write_reg(info, WCRH, 0); /* wait controller high range */
  4336. /* DPCR, DMA Priority Control
  4337. *
  4338. * 07..05 Not used, must be 0
  4339. * 04 BRC, bus release condition: 0=all transfers complete
  4340. * 03 CCC, channel change condition: 0=every cycle
  4341. * 02..00 PR<2..0>, priority 100=round robin
  4342. *
  4343. * 00000100 = 0x04
  4344. */
  4345. write_reg(info, DPCR, dma_priority);
  4346. /* DMA Master Enable, BIT7: 1=enable all channels */
  4347. write_reg(info, DMER, 0x80);
  4348. /* enable all interrupt classes */
  4349. write_reg(info, IER0, 0xff); /* TxRDY,RxRDY,TxINT,RxINT (ports 0-1) */
  4350. write_reg(info, IER1, 0xff); /* DMIB,DMIA (channels 0-3) */
  4351. write_reg(info, IER2, 0xf0); /* TIRQ (timers 0-3) */
  4352. /* ITCR, interrupt control register
  4353. * 07 IPC, interrupt priority, 0=MSCI->DMA
  4354. * 06..05 IAK<1..0>, Acknowledge cycle, 00=non-ack cycle
  4355. * 04 VOS, Vector Output, 0=unmodified vector
  4356. * 03..00 Reserved, must be 0
  4357. */
  4358. write_reg(info, ITCR, 0);
  4359. return TRUE;
  4360. }
  4361. /* initialize adapter hardware
  4362. */
  4363. int init_adapter(SLMP_INFO *info)
  4364. {
  4365. int i;
  4366. /* Set BIT30 of Local Control Reg 0x50 to reset SCA */
  4367. volatile u32 *MiscCtrl = (u32 *)(info->lcr_base + 0x50);
  4368. u32 readval;
  4369. info->misc_ctrl_value |= BIT30;
  4370. *MiscCtrl = info->misc_ctrl_value;
  4371. /*
  4372. * Force at least 170ns delay before clearing
  4373. * reset bit. Each read from LCR takes at least
  4374. * 30ns so 10 times for 300ns to be safe.
  4375. */
  4376. for(i=0;i<10;i++)
  4377. readval = *MiscCtrl;
  4378. info->misc_ctrl_value &= ~BIT30;
  4379. *MiscCtrl = info->misc_ctrl_value;
  4380. /* init control reg (all DTRs off, all clksel=input) */
  4381. info->ctrlreg_value = 0xaa;
  4382. write_control_reg(info);
  4383. {
  4384. volatile u32 *LCR1BRDR = (u32 *)(info->lcr_base + 0x2c);
  4385. lcr1_brdr_value &= ~(BIT5 + BIT4 + BIT3);
  4386. switch(read_ahead_count)
  4387. {
  4388. case 16:
  4389. lcr1_brdr_value |= BIT5 + BIT4 + BIT3;
  4390. break;
  4391. case 8:
  4392. lcr1_brdr_value |= BIT5 + BIT4;
  4393. break;
  4394. case 4:
  4395. lcr1_brdr_value |= BIT5 + BIT3;
  4396. break;
  4397. case 0:
  4398. lcr1_brdr_value |= BIT5;
  4399. break;
  4400. }
  4401. *LCR1BRDR = lcr1_brdr_value;
  4402. *MiscCtrl = misc_ctrl_value;
  4403. }
  4404. sca_init(info->port_array[0]);
  4405. sca_init(info->port_array[2]);
  4406. return TRUE;
  4407. }
  4408. /* Loopback an HDLC frame to test the hardware
  4409. * interrupt and DMA functions.
  4410. */
  4411. int loopback_test(SLMP_INFO *info)
  4412. {
  4413. #define TESTFRAMESIZE 20
  4414. unsigned long timeout;
  4415. u16 count = TESTFRAMESIZE;
  4416. unsigned char buf[TESTFRAMESIZE];
  4417. int rc = FALSE;
  4418. unsigned long flags;
  4419. struct tty_struct *oldtty = info->tty;
  4420. u32 speed = info->params.clock_speed;
  4421. info->params.clock_speed = 3686400;
  4422. info->tty = NULL;
  4423. /* assume failure */
  4424. info->init_error = DiagStatus_DmaFailure;
  4425. /* build and send transmit frame */
  4426. for (count = 0; count < TESTFRAMESIZE;++count)
  4427. buf[count] = (unsigned char)count;
  4428. memset(info->tmp_rx_buf,0,TESTFRAMESIZE);
  4429. /* program hardware for HDLC and enabled receiver */
  4430. spin_lock_irqsave(&info->lock,flags);
  4431. hdlc_mode(info);
  4432. enable_loopback(info,1);
  4433. rx_start(info);
  4434. info->tx_count = count;
  4435. tx_load_dma_buffer(info,buf,count);
  4436. tx_start(info);
  4437. spin_unlock_irqrestore(&info->lock,flags);
  4438. /* wait for receive complete */
  4439. /* Set a timeout for waiting for interrupt. */
  4440. for ( timeout = 100; timeout; --timeout ) {
  4441. msleep_interruptible(10);
  4442. if (rx_get_frame(info)) {
  4443. rc = TRUE;
  4444. break;
  4445. }
  4446. }
  4447. /* verify received frame length and contents */
  4448. if (rc == TRUE &&
  4449. ( info->tmp_rx_buf_count != count ||
  4450. memcmp(buf, info->tmp_rx_buf,count))) {
  4451. rc = FALSE;
  4452. }
  4453. spin_lock_irqsave(&info->lock,flags);
  4454. reset_adapter(info);
  4455. spin_unlock_irqrestore(&info->lock,flags);
  4456. info->params.clock_speed = speed;
  4457. info->tty = oldtty;
  4458. return rc;
  4459. }
  4460. /* Perform diagnostics on hardware
  4461. */
  4462. int adapter_test( SLMP_INFO *info )
  4463. {
  4464. unsigned long flags;
  4465. if ( debug_level >= DEBUG_LEVEL_INFO )
  4466. printk( "%s(%d):Testing device %s\n",
  4467. __FILE__,__LINE__,info->device_name );
  4468. spin_lock_irqsave(&info->lock,flags);
  4469. init_adapter(info);
  4470. spin_unlock_irqrestore(&info->lock,flags);
  4471. info->port_array[0]->port_count = 0;
  4472. if ( register_test(info->port_array[0]) &&
  4473. register_test(info->port_array[1])) {
  4474. info->port_array[0]->port_count = 2;
  4475. if ( register_test(info->port_array[2]) &&
  4476. register_test(info->port_array[3]) )
  4477. info->port_array[0]->port_count += 2;
  4478. }
  4479. else {
  4480. printk( "%s(%d):Register test failure for device %s Addr=%08lX\n",
  4481. __FILE__,__LINE__,info->device_name, (unsigned long)(info->phys_sca_base));
  4482. return -ENODEV;
  4483. }
  4484. if ( !irq_test(info->port_array[0]) ||
  4485. !irq_test(info->port_array[1]) ||
  4486. (info->port_count == 4 && !irq_test(info->port_array[2])) ||
  4487. (info->port_count == 4 && !irq_test(info->port_array[3]))) {
  4488. printk( "%s(%d):Interrupt test failure for device %s IRQ=%d\n",
  4489. __FILE__,__LINE__,info->device_name, (unsigned short)(info->irq_level) );
  4490. return -ENODEV;
  4491. }
  4492. if (!loopback_test(info->port_array[0]) ||
  4493. !loopback_test(info->port_array[1]) ||
  4494. (info->port_count == 4 && !loopback_test(info->port_array[2])) ||
  4495. (info->port_count == 4 && !loopback_test(info->port_array[3]))) {
  4496. printk( "%s(%d):DMA test failure for device %s\n",
  4497. __FILE__,__LINE__,info->device_name);
  4498. return -ENODEV;
  4499. }
  4500. if ( debug_level >= DEBUG_LEVEL_INFO )
  4501. printk( "%s(%d):device %s passed diagnostics\n",
  4502. __FILE__,__LINE__,info->device_name );
  4503. info->port_array[0]->init_error = 0;
  4504. info->port_array[1]->init_error = 0;
  4505. if ( info->port_count > 2 ) {
  4506. info->port_array[2]->init_error = 0;
  4507. info->port_array[3]->init_error = 0;
  4508. }
  4509. return 0;
  4510. }
  4511. /* Test the shared memory on a PCI adapter.
  4512. */
  4513. int memory_test(SLMP_INFO *info)
  4514. {
  4515. static unsigned long testval[] = { 0x0, 0x55555555, 0xaaaaaaaa,
  4516. 0x66666666, 0x99999999, 0xffffffff, 0x12345678 };
  4517. unsigned long count = sizeof(testval)/sizeof(unsigned long);
  4518. unsigned long i;
  4519. unsigned long limit = SCA_MEM_SIZE/sizeof(unsigned long);
  4520. unsigned long * addr = (unsigned long *)info->memory_base;
  4521. /* Test data lines with test pattern at one location. */
  4522. for ( i = 0 ; i < count ; i++ ) {
  4523. *addr = testval[i];
  4524. if ( *addr != testval[i] )
  4525. return FALSE;
  4526. }
  4527. /* Test address lines with incrementing pattern over */
  4528. /* entire address range. */
  4529. for ( i = 0 ; i < limit ; i++ ) {
  4530. *addr = i * 4;
  4531. addr++;
  4532. }
  4533. addr = (unsigned long *)info->memory_base;
  4534. for ( i = 0 ; i < limit ; i++ ) {
  4535. if ( *addr != i * 4 )
  4536. return FALSE;
  4537. addr++;
  4538. }
  4539. memset( info->memory_base, 0, SCA_MEM_SIZE );
  4540. return TRUE;
  4541. }
  4542. /* Load data into PCI adapter shared memory.
  4543. *
  4544. * The PCI9050 releases control of the local bus
  4545. * after completing the current read or write operation.
  4546. *
  4547. * While the PCI9050 write FIFO not empty, the
  4548. * PCI9050 treats all of the writes as a single transaction
  4549. * and does not release the bus. This causes DMA latency problems
  4550. * at high speeds when copying large data blocks to the shared memory.
  4551. *
  4552. * This function breaks a write into multiple transations by
  4553. * interleaving a read which flushes the write FIFO and 'completes'
  4554. * the write transation. This allows any pending DMA request to gain control
  4555. * of the local bus in a timely fasion.
  4556. */
  4557. void load_pci_memory(SLMP_INFO *info, char* dest, const char* src, unsigned short count)
  4558. {
  4559. /* A load interval of 16 allows for 4 32-bit writes at */
  4560. /* 136ns each for a maximum latency of 542ns on the local bus.*/
  4561. unsigned short interval = count / sca_pci_load_interval;
  4562. unsigned short i;
  4563. for ( i = 0 ; i < interval ; i++ )
  4564. {
  4565. memcpy(dest, src, sca_pci_load_interval);
  4566. read_status_reg(info);
  4567. dest += sca_pci_load_interval;
  4568. src += sca_pci_load_interval;
  4569. }
  4570. memcpy(dest, src, count % sca_pci_load_interval);
  4571. }
  4572. void trace_block(SLMP_INFO *info,const char* data, int count, int xmit)
  4573. {
  4574. int i;
  4575. int linecount;
  4576. if (xmit)
  4577. printk("%s tx data:\n",info->device_name);
  4578. else
  4579. printk("%s rx data:\n",info->device_name);
  4580. while(count) {
  4581. if (count > 16)
  4582. linecount = 16;
  4583. else
  4584. linecount = count;
  4585. for(i=0;i<linecount;i++)
  4586. printk("%02X ",(unsigned char)data[i]);
  4587. for(;i<17;i++)
  4588. printk(" ");
  4589. for(i=0;i<linecount;i++) {
  4590. if (data[i]>=040 && data[i]<=0176)
  4591. printk("%c",data[i]);
  4592. else
  4593. printk(".");
  4594. }
  4595. printk("\n");
  4596. data += linecount;
  4597. count -= linecount;
  4598. }
  4599. } /* end of trace_block() */
  4600. /* called when HDLC frame times out
  4601. * update stats and do tx completion processing
  4602. */
  4603. void tx_timeout(unsigned long context)
  4604. {
  4605. SLMP_INFO *info = (SLMP_INFO*)context;
  4606. unsigned long flags;
  4607. if ( debug_level >= DEBUG_LEVEL_INFO )
  4608. printk( "%s(%d):%s tx_timeout()\n",
  4609. __FILE__,__LINE__,info->device_name);
  4610. if(info->tx_active && info->params.mode == MGSL_MODE_HDLC) {
  4611. info->icount.txtimeout++;
  4612. }
  4613. spin_lock_irqsave(&info->lock,flags);
  4614. info->tx_active = 0;
  4615. info->tx_count = info->tx_put = info->tx_get = 0;
  4616. spin_unlock_irqrestore(&info->lock,flags);
  4617. #ifdef CONFIG_HDLC
  4618. if (info->netcount)
  4619. hdlcdev_tx_done(info);
  4620. else
  4621. #endif
  4622. bh_transmit(info);
  4623. }
  4624. /* called to periodically check the DSR/RI modem signal input status
  4625. */
  4626. void status_timeout(unsigned long context)
  4627. {
  4628. u16 status = 0;
  4629. SLMP_INFO *info = (SLMP_INFO*)context;
  4630. unsigned long flags;
  4631. unsigned char delta;
  4632. spin_lock_irqsave(&info->lock,flags);
  4633. get_signals(info);
  4634. spin_unlock_irqrestore(&info->lock,flags);
  4635. /* check for DSR/RI state change */
  4636. delta = info->old_signals ^ info->serial_signals;
  4637. info->old_signals = info->serial_signals;
  4638. if (delta & SerialSignal_DSR)
  4639. status |= MISCSTATUS_DSR_LATCHED|(info->serial_signals&SerialSignal_DSR);
  4640. if (delta & SerialSignal_RI)
  4641. status |= MISCSTATUS_RI_LATCHED|(info->serial_signals&SerialSignal_RI);
  4642. if (delta & SerialSignal_DCD)
  4643. status |= MISCSTATUS_DCD_LATCHED|(info->serial_signals&SerialSignal_DCD);
  4644. if (delta & SerialSignal_CTS)
  4645. status |= MISCSTATUS_CTS_LATCHED|(info->serial_signals&SerialSignal_CTS);
  4646. if (status)
  4647. isr_io_pin(info,status);
  4648. info->status_timer.data = (unsigned long)info;
  4649. info->status_timer.function = status_timeout;
  4650. info->status_timer.expires = jiffies + msecs_to_jiffies(10);
  4651. add_timer(&info->status_timer);
  4652. }
  4653. /* Register Access Routines -
  4654. * All registers are memory mapped
  4655. */
  4656. #define CALC_REGADDR() \
  4657. unsigned char * RegAddr = (unsigned char*)(info->sca_base + Addr); \
  4658. if (info->port_num > 1) \
  4659. RegAddr += 256; /* port 0-1 SCA0, 2-3 SCA1 */ \
  4660. if ( info->port_num & 1) { \
  4661. if (Addr > 0x7f) \
  4662. RegAddr += 0x40; /* DMA access */ \
  4663. else if (Addr > 0x1f && Addr < 0x60) \
  4664. RegAddr += 0x20; /* MSCI access */ \
  4665. }
  4666. unsigned char read_reg(SLMP_INFO * info, unsigned char Addr)
  4667. {
  4668. CALC_REGADDR();
  4669. return *RegAddr;
  4670. }
  4671. void write_reg(SLMP_INFO * info, unsigned char Addr, unsigned char Value)
  4672. {
  4673. CALC_REGADDR();
  4674. *RegAddr = Value;
  4675. }
  4676. u16 read_reg16(SLMP_INFO * info, unsigned char Addr)
  4677. {
  4678. CALC_REGADDR();
  4679. return *((u16 *)RegAddr);
  4680. }
  4681. void write_reg16(SLMP_INFO * info, unsigned char Addr, u16 Value)
  4682. {
  4683. CALC_REGADDR();
  4684. *((u16 *)RegAddr) = Value;
  4685. }
  4686. unsigned char read_status_reg(SLMP_INFO * info)
  4687. {
  4688. unsigned char *RegAddr = (unsigned char *)info->statctrl_base;
  4689. return *RegAddr;
  4690. }
  4691. void write_control_reg(SLMP_INFO * info)
  4692. {
  4693. unsigned char *RegAddr = (unsigned char *)info->statctrl_base;
  4694. *RegAddr = info->port_array[0]->ctrlreg_value;
  4695. }
  4696. static int __devinit synclinkmp_init_one (struct pci_dev *dev,
  4697. const struct pci_device_id *ent)
  4698. {
  4699. if (pci_enable_device(dev)) {
  4700. printk("error enabling pci device %p\n", dev);
  4701. return -EIO;
  4702. }
  4703. device_init( ++synclinkmp_adapter_count, dev );
  4704. return 0;
  4705. }
  4706. static void __devexit synclinkmp_remove_one (struct pci_dev *dev)
  4707. {
  4708. }