synclinkmp.c 148 KB

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