synclinkmp.c 148 KB

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