synclink_gt.c 128 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022
  1. /*
  2. * Device driver for Microgate SyncLink GT serial adapters.
  3. *
  4. * written by Paul Fulghum for Microgate Corporation
  5. * paulkf@microgate.com
  6. *
  7. * Microgate and SyncLink are trademarks of Microgate Corporation
  8. *
  9. * This code is released under the GNU General Public License (GPL)
  10. *
  11. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  12. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  13. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  14. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  15. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  16. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  17. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  18. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  19. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  20. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  21. * OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. /*
  24. * DEBUG OUTPUT DEFINITIONS
  25. *
  26. * uncomment lines below to enable specific types of debug output
  27. *
  28. * DBGINFO information - most verbose output
  29. * DBGERR serious errors
  30. * DBGBH bottom half service routine debugging
  31. * DBGISR interrupt service routine debugging
  32. * DBGDATA output receive and transmit data
  33. * DBGTBUF output transmit DMA buffers and registers
  34. * DBGRBUF output receive DMA buffers and registers
  35. */
  36. #define DBGINFO(fmt) if (debug_level >= DEBUG_LEVEL_INFO) printk fmt
  37. #define DBGERR(fmt) if (debug_level >= DEBUG_LEVEL_ERROR) printk fmt
  38. #define DBGBH(fmt) if (debug_level >= DEBUG_LEVEL_BH) printk fmt
  39. #define DBGISR(fmt) if (debug_level >= DEBUG_LEVEL_ISR) printk fmt
  40. #define DBGDATA(info, buf, size, label) if (debug_level >= DEBUG_LEVEL_DATA) trace_block((info), (buf), (size), (label))
  41. //#define DBGTBUF(info) dump_tbufs(info)
  42. //#define DBGRBUF(info) dump_rbufs(info)
  43. #include <linux/module.h>
  44. #include <linux/errno.h>
  45. #include <linux/signal.h>
  46. #include <linux/sched.h>
  47. #include <linux/timer.h>
  48. #include <linux/interrupt.h>
  49. #include <linux/pci.h>
  50. #include <linux/tty.h>
  51. #include <linux/tty_flip.h>
  52. #include <linux/serial.h>
  53. #include <linux/major.h>
  54. #include <linux/string.h>
  55. #include <linux/fcntl.h>
  56. #include <linux/ptrace.h>
  57. #include <linux/ioport.h>
  58. #include <linux/mm.h>
  59. #include <linux/seq_file.h>
  60. #include <linux/slab.h>
  61. #include <linux/smp_lock.h>
  62. #include <linux/netdevice.h>
  63. #include <linux/vmalloc.h>
  64. #include <linux/init.h>
  65. #include <linux/delay.h>
  66. #include <linux/ioctl.h>
  67. #include <linux/termios.h>
  68. #include <linux/bitops.h>
  69. #include <linux/workqueue.h>
  70. #include <linux/hdlc.h>
  71. #include <linux/synclink.h>
  72. #include <asm/system.h>
  73. #include <asm/io.h>
  74. #include <asm/irq.h>
  75. #include <asm/dma.h>
  76. #include <asm/types.h>
  77. #include <asm/uaccess.h>
  78. #if defined(CONFIG_HDLC) || (defined(CONFIG_HDLC_MODULE) && defined(CONFIG_SYNCLINK_GT_MODULE))
  79. #define SYNCLINK_GENERIC_HDLC 1
  80. #else
  81. #define SYNCLINK_GENERIC_HDLC 0
  82. #endif
  83. /*
  84. * module identification
  85. */
  86. static char *driver_name = "SyncLink GT";
  87. static char *tty_driver_name = "synclink_gt";
  88. static char *tty_dev_prefix = "ttySLG";
  89. MODULE_LICENSE("GPL");
  90. #define MGSL_MAGIC 0x5401
  91. #define MAX_DEVICES 32
  92. static struct pci_device_id pci_table[] = {
  93. {PCI_VENDOR_ID_MICROGATE, SYNCLINK_GT_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
  94. {PCI_VENDOR_ID_MICROGATE, SYNCLINK_GT2_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
  95. {PCI_VENDOR_ID_MICROGATE, SYNCLINK_GT4_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
  96. {PCI_VENDOR_ID_MICROGATE, SYNCLINK_AC_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
  97. {0,}, /* terminate list */
  98. };
  99. MODULE_DEVICE_TABLE(pci, pci_table);
  100. static int init_one(struct pci_dev *dev,const struct pci_device_id *ent);
  101. static void remove_one(struct pci_dev *dev);
  102. static struct pci_driver pci_driver = {
  103. .name = "synclink_gt",
  104. .id_table = pci_table,
  105. .probe = init_one,
  106. .remove = __devexit_p(remove_one),
  107. };
  108. static bool pci_registered;
  109. /*
  110. * module configuration and status
  111. */
  112. static struct slgt_info *slgt_device_list;
  113. static int slgt_device_count;
  114. static int ttymajor;
  115. static int debug_level;
  116. static int maxframe[MAX_DEVICES];
  117. module_param(ttymajor, int, 0);
  118. module_param(debug_level, int, 0);
  119. module_param_array(maxframe, int, NULL, 0);
  120. MODULE_PARM_DESC(ttymajor, "TTY major device number override: 0=auto assigned");
  121. MODULE_PARM_DESC(debug_level, "Debug syslog output: 0=disabled, 1 to 5=increasing detail");
  122. MODULE_PARM_DESC(maxframe, "Maximum frame size used by device (4096 to 65535)");
  123. /*
  124. * tty support and callbacks
  125. */
  126. static struct tty_driver *serial_driver;
  127. static int open(struct tty_struct *tty, struct file * filp);
  128. static void close(struct tty_struct *tty, struct file * filp);
  129. static void hangup(struct tty_struct *tty);
  130. static void set_termios(struct tty_struct *tty, struct ktermios *old_termios);
  131. static int write(struct tty_struct *tty, const unsigned char *buf, int count);
  132. static int put_char(struct tty_struct *tty, unsigned char ch);
  133. static void send_xchar(struct tty_struct *tty, char ch);
  134. static void wait_until_sent(struct tty_struct *tty, int timeout);
  135. static int write_room(struct tty_struct *tty);
  136. static void flush_chars(struct tty_struct *tty);
  137. static void flush_buffer(struct tty_struct *tty);
  138. static void tx_hold(struct tty_struct *tty);
  139. static void tx_release(struct tty_struct *tty);
  140. static int ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
  141. static int chars_in_buffer(struct tty_struct *tty);
  142. static void throttle(struct tty_struct * tty);
  143. static void unthrottle(struct tty_struct * tty);
  144. static int set_break(struct tty_struct *tty, int break_state);
  145. /*
  146. * generic HDLC support and callbacks
  147. */
  148. #if SYNCLINK_GENERIC_HDLC
  149. #define dev_to_port(D) (dev_to_hdlc(D)->priv)
  150. static void hdlcdev_tx_done(struct slgt_info *info);
  151. static void hdlcdev_rx(struct slgt_info *info, char *buf, int size);
  152. static int hdlcdev_init(struct slgt_info *info);
  153. static void hdlcdev_exit(struct slgt_info *info);
  154. #endif
  155. /*
  156. * device specific structures, macros and functions
  157. */
  158. #define SLGT_MAX_PORTS 4
  159. #define SLGT_REG_SIZE 256
  160. /*
  161. * conditional wait facility
  162. */
  163. struct cond_wait {
  164. struct cond_wait *next;
  165. wait_queue_head_t q;
  166. wait_queue_t wait;
  167. unsigned int data;
  168. };
  169. static void init_cond_wait(struct cond_wait *w, unsigned int data);
  170. static void add_cond_wait(struct cond_wait **head, struct cond_wait *w);
  171. static void remove_cond_wait(struct cond_wait **head, struct cond_wait *w);
  172. static void flush_cond_wait(struct cond_wait **head);
  173. /*
  174. * DMA buffer descriptor and access macros
  175. */
  176. struct slgt_desc
  177. {
  178. __le16 count;
  179. __le16 status;
  180. __le32 pbuf; /* physical address of data buffer */
  181. __le32 next; /* physical address of next descriptor */
  182. /* driver book keeping */
  183. char *buf; /* virtual address of data buffer */
  184. unsigned int pdesc; /* physical address of this descriptor */
  185. dma_addr_t buf_dma_addr;
  186. unsigned short buf_count;
  187. };
  188. #define set_desc_buffer(a,b) (a).pbuf = cpu_to_le32((unsigned int)(b))
  189. #define set_desc_next(a,b) (a).next = cpu_to_le32((unsigned int)(b))
  190. #define set_desc_count(a,b)(a).count = cpu_to_le16((unsigned short)(b))
  191. #define set_desc_eof(a,b) (a).status = cpu_to_le16((b) ? (le16_to_cpu((a).status) | BIT0) : (le16_to_cpu((a).status) & ~BIT0))
  192. #define set_desc_status(a, b) (a).status = cpu_to_le16((unsigned short)(b))
  193. #define desc_count(a) (le16_to_cpu((a).count))
  194. #define desc_status(a) (le16_to_cpu((a).status))
  195. #define desc_complete(a) (le16_to_cpu((a).status) & BIT15)
  196. #define desc_eof(a) (le16_to_cpu((a).status) & BIT2)
  197. #define desc_crc_error(a) (le16_to_cpu((a).status) & BIT1)
  198. #define desc_abort(a) (le16_to_cpu((a).status) & BIT0)
  199. #define desc_residue(a) ((le16_to_cpu((a).status) & 0x38) >> 3)
  200. struct _input_signal_events {
  201. int ri_up;
  202. int ri_down;
  203. int dsr_up;
  204. int dsr_down;
  205. int dcd_up;
  206. int dcd_down;
  207. int cts_up;
  208. int cts_down;
  209. };
  210. /*
  211. * device instance data structure
  212. */
  213. struct slgt_info {
  214. void *if_ptr; /* General purpose pointer (used by SPPP) */
  215. struct tty_port port;
  216. struct slgt_info *next_device; /* device list link */
  217. int magic;
  218. char device_name[25];
  219. struct pci_dev *pdev;
  220. int port_count; /* count of ports on adapter */
  221. int adapter_num; /* adapter instance number */
  222. int port_num; /* port instance number */
  223. /* array of pointers to port contexts on this adapter */
  224. struct slgt_info *port_array[SLGT_MAX_PORTS];
  225. int line; /* tty line instance number */
  226. struct mgsl_icount icount;
  227. int timeout;
  228. int x_char; /* xon/xoff character */
  229. unsigned int read_status_mask;
  230. unsigned int ignore_status_mask;
  231. wait_queue_head_t status_event_wait_q;
  232. wait_queue_head_t event_wait_q;
  233. struct timer_list tx_timer;
  234. struct timer_list rx_timer;
  235. unsigned int gpio_present;
  236. struct cond_wait *gpio_wait_q;
  237. spinlock_t lock; /* spinlock for synchronizing with ISR */
  238. struct work_struct task;
  239. u32 pending_bh;
  240. bool bh_requested;
  241. bool bh_running;
  242. int isr_overflow;
  243. bool irq_requested; /* true if IRQ requested */
  244. bool irq_occurred; /* for diagnostics use */
  245. /* device configuration */
  246. unsigned int bus_type;
  247. unsigned int irq_level;
  248. unsigned long irq_flags;
  249. unsigned char __iomem * reg_addr; /* memory mapped registers address */
  250. u32 phys_reg_addr;
  251. bool reg_addr_requested;
  252. MGSL_PARAMS params; /* communications parameters */
  253. u32 idle_mode;
  254. u32 max_frame_size; /* as set by device config */
  255. unsigned int rbuf_fill_level;
  256. unsigned int rx_pio;
  257. unsigned int if_mode;
  258. unsigned int base_clock;
  259. /* device status */
  260. bool rx_enabled;
  261. bool rx_restart;
  262. bool tx_enabled;
  263. bool tx_active;
  264. unsigned char signals; /* serial signal states */
  265. int init_error; /* initialization error */
  266. unsigned char *tx_buf;
  267. int tx_count;
  268. char flag_buf[MAX_ASYNC_BUFFER_SIZE];
  269. char char_buf[MAX_ASYNC_BUFFER_SIZE];
  270. bool drop_rts_on_tx_done;
  271. struct _input_signal_events input_signal_events;
  272. int dcd_chkcount; /* check counts to prevent */
  273. int cts_chkcount; /* too many IRQs if a signal */
  274. int dsr_chkcount; /* is floating */
  275. int ri_chkcount;
  276. char *bufs; /* virtual address of DMA buffer lists */
  277. dma_addr_t bufs_dma_addr; /* physical address of buffer descriptors */
  278. unsigned int rbuf_count;
  279. struct slgt_desc *rbufs;
  280. unsigned int rbuf_current;
  281. unsigned int rbuf_index;
  282. unsigned int rbuf_fill_index;
  283. unsigned short rbuf_fill_count;
  284. unsigned int tbuf_count;
  285. struct slgt_desc *tbufs;
  286. unsigned int tbuf_current;
  287. unsigned int tbuf_start;
  288. unsigned char *tmp_rbuf;
  289. unsigned int tmp_rbuf_count;
  290. /* SPPP/Cisco HDLC device parts */
  291. int netcount;
  292. spinlock_t netlock;
  293. #if SYNCLINK_GENERIC_HDLC
  294. struct net_device *netdev;
  295. #endif
  296. };
  297. static MGSL_PARAMS default_params = {
  298. .mode = MGSL_MODE_HDLC,
  299. .loopback = 0,
  300. .flags = HDLC_FLAG_UNDERRUN_ABORT15,
  301. .encoding = HDLC_ENCODING_NRZI_SPACE,
  302. .clock_speed = 0,
  303. .addr_filter = 0xff,
  304. .crc_type = HDLC_CRC_16_CCITT,
  305. .preamble_length = HDLC_PREAMBLE_LENGTH_8BITS,
  306. .preamble = HDLC_PREAMBLE_PATTERN_NONE,
  307. .data_rate = 9600,
  308. .data_bits = 8,
  309. .stop_bits = 1,
  310. .parity = ASYNC_PARITY_NONE
  311. };
  312. #define BH_RECEIVE 1
  313. #define BH_TRANSMIT 2
  314. #define BH_STATUS 4
  315. #define IO_PIN_SHUTDOWN_LIMIT 100
  316. #define DMABUFSIZE 256
  317. #define DESC_LIST_SIZE 4096
  318. #define MASK_PARITY BIT1
  319. #define MASK_FRAMING BIT0
  320. #define MASK_BREAK BIT14
  321. #define MASK_OVERRUN BIT4
  322. #define GSR 0x00 /* global status */
  323. #define JCR 0x04 /* JTAG control */
  324. #define IODR 0x08 /* GPIO direction */
  325. #define IOER 0x0c /* GPIO interrupt enable */
  326. #define IOVR 0x10 /* GPIO value */
  327. #define IOSR 0x14 /* GPIO interrupt status */
  328. #define TDR 0x80 /* tx data */
  329. #define RDR 0x80 /* rx data */
  330. #define TCR 0x82 /* tx control */
  331. #define TIR 0x84 /* tx idle */
  332. #define TPR 0x85 /* tx preamble */
  333. #define RCR 0x86 /* rx control */
  334. #define VCR 0x88 /* V.24 control */
  335. #define CCR 0x89 /* clock control */
  336. #define BDR 0x8a /* baud divisor */
  337. #define SCR 0x8c /* serial control */
  338. #define SSR 0x8e /* serial status */
  339. #define RDCSR 0x90 /* rx DMA control/status */
  340. #define TDCSR 0x94 /* tx DMA control/status */
  341. #define RDDAR 0x98 /* rx DMA descriptor address */
  342. #define TDDAR 0x9c /* tx DMA descriptor address */
  343. #define RXIDLE BIT14
  344. #define RXBREAK BIT14
  345. #define IRQ_TXDATA BIT13
  346. #define IRQ_TXIDLE BIT12
  347. #define IRQ_TXUNDER BIT11 /* HDLC */
  348. #define IRQ_RXDATA BIT10
  349. #define IRQ_RXIDLE BIT9 /* HDLC */
  350. #define IRQ_RXBREAK BIT9 /* async */
  351. #define IRQ_RXOVER BIT8
  352. #define IRQ_DSR BIT7
  353. #define IRQ_CTS BIT6
  354. #define IRQ_DCD BIT5
  355. #define IRQ_RI BIT4
  356. #define IRQ_ALL 0x3ff0
  357. #define IRQ_MASTER BIT0
  358. #define slgt_irq_on(info, mask) \
  359. wr_reg16((info), SCR, (unsigned short)(rd_reg16((info), SCR) | (mask)))
  360. #define slgt_irq_off(info, mask) \
  361. wr_reg16((info), SCR, (unsigned short)(rd_reg16((info), SCR) & ~(mask)))
  362. static __u8 rd_reg8(struct slgt_info *info, unsigned int addr);
  363. static void wr_reg8(struct slgt_info *info, unsigned int addr, __u8 value);
  364. static __u16 rd_reg16(struct slgt_info *info, unsigned int addr);
  365. static void wr_reg16(struct slgt_info *info, unsigned int addr, __u16 value);
  366. static __u32 rd_reg32(struct slgt_info *info, unsigned int addr);
  367. static void wr_reg32(struct slgt_info *info, unsigned int addr, __u32 value);
  368. static void msc_set_vcr(struct slgt_info *info);
  369. static int startup(struct slgt_info *info);
  370. static int block_til_ready(struct tty_struct *tty, struct file * filp,struct slgt_info *info);
  371. static void shutdown(struct slgt_info *info);
  372. static void program_hw(struct slgt_info *info);
  373. static void change_params(struct slgt_info *info);
  374. static int register_test(struct slgt_info *info);
  375. static int irq_test(struct slgt_info *info);
  376. static int loopback_test(struct slgt_info *info);
  377. static int adapter_test(struct slgt_info *info);
  378. static void reset_adapter(struct slgt_info *info);
  379. static void reset_port(struct slgt_info *info);
  380. static void async_mode(struct slgt_info *info);
  381. static void sync_mode(struct slgt_info *info);
  382. static void rx_stop(struct slgt_info *info);
  383. static void rx_start(struct slgt_info *info);
  384. static void reset_rbufs(struct slgt_info *info);
  385. static void free_rbufs(struct slgt_info *info, unsigned int first, unsigned int last);
  386. static void rdma_reset(struct slgt_info *info);
  387. static bool rx_get_frame(struct slgt_info *info);
  388. static bool rx_get_buf(struct slgt_info *info);
  389. static void tx_start(struct slgt_info *info);
  390. static void tx_stop(struct slgt_info *info);
  391. static void tx_set_idle(struct slgt_info *info);
  392. static unsigned int free_tbuf_count(struct slgt_info *info);
  393. static unsigned int tbuf_bytes(struct slgt_info *info);
  394. static void reset_tbufs(struct slgt_info *info);
  395. static void tdma_reset(struct slgt_info *info);
  396. static void tx_load(struct slgt_info *info, const char *buf, unsigned int count);
  397. static void get_signals(struct slgt_info *info);
  398. static void set_signals(struct slgt_info *info);
  399. static void enable_loopback(struct slgt_info *info);
  400. static void set_rate(struct slgt_info *info, u32 data_rate);
  401. static int bh_action(struct slgt_info *info);
  402. static void bh_handler(struct work_struct *work);
  403. static void bh_transmit(struct slgt_info *info);
  404. static void isr_serial(struct slgt_info *info);
  405. static void isr_rdma(struct slgt_info *info);
  406. static void isr_txeom(struct slgt_info *info, unsigned short status);
  407. static void isr_tdma(struct slgt_info *info);
  408. static int alloc_dma_bufs(struct slgt_info *info);
  409. static void free_dma_bufs(struct slgt_info *info);
  410. static int alloc_desc(struct slgt_info *info);
  411. static void free_desc(struct slgt_info *info);
  412. static int alloc_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count);
  413. static void free_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count);
  414. static int alloc_tmp_rbuf(struct slgt_info *info);
  415. static void free_tmp_rbuf(struct slgt_info *info);
  416. static void tx_timeout(unsigned long context);
  417. static void rx_timeout(unsigned long context);
  418. /*
  419. * ioctl handlers
  420. */
  421. static int get_stats(struct slgt_info *info, struct mgsl_icount __user *user_icount);
  422. static int get_params(struct slgt_info *info, MGSL_PARAMS __user *params);
  423. static int set_params(struct slgt_info *info, MGSL_PARAMS __user *params);
  424. static int get_txidle(struct slgt_info *info, int __user *idle_mode);
  425. static int set_txidle(struct slgt_info *info, int idle_mode);
  426. static int tx_enable(struct slgt_info *info, int enable);
  427. static int tx_abort(struct slgt_info *info);
  428. static int rx_enable(struct slgt_info *info, int enable);
  429. static int modem_input_wait(struct slgt_info *info,int arg);
  430. static int wait_mgsl_event(struct slgt_info *info, int __user *mask_ptr);
  431. static int tiocmget(struct tty_struct *tty, struct file *file);
  432. static int tiocmset(struct tty_struct *tty, struct file *file,
  433. unsigned int set, unsigned int clear);
  434. static int set_break(struct tty_struct *tty, int break_state);
  435. static int get_interface(struct slgt_info *info, int __user *if_mode);
  436. static int set_interface(struct slgt_info *info, int if_mode);
  437. static int set_gpio(struct slgt_info *info, struct gpio_desc __user *gpio);
  438. static int get_gpio(struct slgt_info *info, struct gpio_desc __user *gpio);
  439. static int wait_gpio(struct slgt_info *info, struct gpio_desc __user *gpio);
  440. /*
  441. * driver functions
  442. */
  443. static void add_device(struct slgt_info *info);
  444. static void device_init(int adapter_num, struct pci_dev *pdev);
  445. static int claim_resources(struct slgt_info *info);
  446. static void release_resources(struct slgt_info *info);
  447. /*
  448. * DEBUG OUTPUT CODE
  449. */
  450. #ifndef DBGINFO
  451. #define DBGINFO(fmt)
  452. #endif
  453. #ifndef DBGERR
  454. #define DBGERR(fmt)
  455. #endif
  456. #ifndef DBGBH
  457. #define DBGBH(fmt)
  458. #endif
  459. #ifndef DBGISR
  460. #define DBGISR(fmt)
  461. #endif
  462. #ifdef DBGDATA
  463. static void trace_block(struct slgt_info *info, const char *data, int count, const char *label)
  464. {
  465. int i;
  466. int linecount;
  467. printk("%s %s data:\n",info->device_name, label);
  468. while(count) {
  469. linecount = (count > 16) ? 16 : count;
  470. for(i=0; i < linecount; i++)
  471. printk("%02X ",(unsigned char)data[i]);
  472. for(;i<17;i++)
  473. printk(" ");
  474. for(i=0;i<linecount;i++) {
  475. if (data[i]>=040 && data[i]<=0176)
  476. printk("%c",data[i]);
  477. else
  478. printk(".");
  479. }
  480. printk("\n");
  481. data += linecount;
  482. count -= linecount;
  483. }
  484. }
  485. #else
  486. #define DBGDATA(info, buf, size, label)
  487. #endif
  488. #ifdef DBGTBUF
  489. static void dump_tbufs(struct slgt_info *info)
  490. {
  491. int i;
  492. printk("tbuf_current=%d\n", info->tbuf_current);
  493. for (i=0 ; i < info->tbuf_count ; i++) {
  494. printk("%d: count=%04X status=%04X\n",
  495. i, le16_to_cpu(info->tbufs[i].count), le16_to_cpu(info->tbufs[i].status));
  496. }
  497. }
  498. #else
  499. #define DBGTBUF(info)
  500. #endif
  501. #ifdef DBGRBUF
  502. static void dump_rbufs(struct slgt_info *info)
  503. {
  504. int i;
  505. printk("rbuf_current=%d\n", info->rbuf_current);
  506. for (i=0 ; i < info->rbuf_count ; i++) {
  507. printk("%d: count=%04X status=%04X\n",
  508. i, le16_to_cpu(info->rbufs[i].count), le16_to_cpu(info->rbufs[i].status));
  509. }
  510. }
  511. #else
  512. #define DBGRBUF(info)
  513. #endif
  514. static inline int sanity_check(struct slgt_info *info, char *devname, const char *name)
  515. {
  516. #ifdef SANITY_CHECK
  517. if (!info) {
  518. printk("null struct slgt_info for (%s) in %s\n", devname, name);
  519. return 1;
  520. }
  521. if (info->magic != MGSL_MAGIC) {
  522. printk("bad magic number struct slgt_info (%s) in %s\n", devname, name);
  523. return 1;
  524. }
  525. #else
  526. if (!info)
  527. return 1;
  528. #endif
  529. return 0;
  530. }
  531. /**
  532. * line discipline callback wrappers
  533. *
  534. * The wrappers maintain line discipline references
  535. * while calling into the line discipline.
  536. *
  537. * ldisc_receive_buf - pass receive data to line discipline
  538. */
  539. static void ldisc_receive_buf(struct tty_struct *tty,
  540. const __u8 *data, char *flags, int count)
  541. {
  542. struct tty_ldisc *ld;
  543. if (!tty)
  544. return;
  545. ld = tty_ldisc_ref(tty);
  546. if (ld) {
  547. if (ld->ops->receive_buf)
  548. ld->ops->receive_buf(tty, data, flags, count);
  549. tty_ldisc_deref(ld);
  550. }
  551. }
  552. /* tty callbacks */
  553. static int open(struct tty_struct *tty, struct file *filp)
  554. {
  555. struct slgt_info *info;
  556. int retval, line;
  557. unsigned long flags;
  558. line = tty->index;
  559. if ((line < 0) || (line >= slgt_device_count)) {
  560. DBGERR(("%s: open with invalid line #%d.\n", driver_name, line));
  561. return -ENODEV;
  562. }
  563. info = slgt_device_list;
  564. while(info && info->line != line)
  565. info = info->next_device;
  566. if (sanity_check(info, tty->name, "open"))
  567. return -ENODEV;
  568. if (info->init_error) {
  569. DBGERR(("%s init error=%d\n", info->device_name, info->init_error));
  570. return -ENODEV;
  571. }
  572. tty->driver_data = info;
  573. info->port.tty = tty;
  574. DBGINFO(("%s open, old ref count = %d\n", info->device_name, info->port.count));
  575. /* If port is closing, signal caller to try again */
  576. if (tty_hung_up_p(filp) || info->port.flags & ASYNC_CLOSING){
  577. if (info->port.flags & ASYNC_CLOSING)
  578. interruptible_sleep_on(&info->port.close_wait);
  579. retval = ((info->port.flags & ASYNC_HUP_NOTIFY) ?
  580. -EAGAIN : -ERESTARTSYS);
  581. goto cleanup;
  582. }
  583. info->port.tty->low_latency = (info->port.flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  584. spin_lock_irqsave(&info->netlock, flags);
  585. if (info->netcount) {
  586. retval = -EBUSY;
  587. spin_unlock_irqrestore(&info->netlock, flags);
  588. goto cleanup;
  589. }
  590. info->port.count++;
  591. spin_unlock_irqrestore(&info->netlock, flags);
  592. if (info->port.count == 1) {
  593. /* 1st open on this device, init hardware */
  594. retval = startup(info);
  595. if (retval < 0)
  596. goto cleanup;
  597. }
  598. retval = block_til_ready(tty, filp, info);
  599. if (retval) {
  600. DBGINFO(("%s block_til_ready rc=%d\n", info->device_name, retval));
  601. goto cleanup;
  602. }
  603. retval = 0;
  604. cleanup:
  605. if (retval) {
  606. if (tty->count == 1)
  607. info->port.tty = NULL; /* tty layer will release tty struct */
  608. if(info->port.count)
  609. info->port.count--;
  610. }
  611. DBGINFO(("%s open rc=%d\n", info->device_name, retval));
  612. return retval;
  613. }
  614. static void close(struct tty_struct *tty, struct file *filp)
  615. {
  616. struct slgt_info *info = tty->driver_data;
  617. if (sanity_check(info, tty->name, "close"))
  618. return;
  619. DBGINFO(("%s close entry, count=%d\n", info->device_name, info->port.count));
  620. if (tty_port_close_start(&info->port, tty, filp) == 0)
  621. goto cleanup;
  622. if (info->port.flags & ASYNC_INITIALIZED)
  623. wait_until_sent(tty, info->timeout);
  624. flush_buffer(tty);
  625. tty_ldisc_flush(tty);
  626. shutdown(info);
  627. tty_port_close_end(&info->port, tty);
  628. info->port.tty = NULL;
  629. cleanup:
  630. DBGINFO(("%s close exit, count=%d\n", tty->driver->name, info->port.count));
  631. }
  632. static void hangup(struct tty_struct *tty)
  633. {
  634. struct slgt_info *info = tty->driver_data;
  635. if (sanity_check(info, tty->name, "hangup"))
  636. return;
  637. DBGINFO(("%s hangup\n", info->device_name));
  638. flush_buffer(tty);
  639. shutdown(info);
  640. info->port.count = 0;
  641. info->port.flags &= ~ASYNC_NORMAL_ACTIVE;
  642. info->port.tty = NULL;
  643. wake_up_interruptible(&info->port.open_wait);
  644. }
  645. static void set_termios(struct tty_struct *tty, struct ktermios *old_termios)
  646. {
  647. struct slgt_info *info = tty->driver_data;
  648. unsigned long flags;
  649. DBGINFO(("%s set_termios\n", tty->driver->name));
  650. change_params(info);
  651. /* Handle transition to B0 status */
  652. if (old_termios->c_cflag & CBAUD &&
  653. !(tty->termios->c_cflag & CBAUD)) {
  654. info->signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
  655. spin_lock_irqsave(&info->lock,flags);
  656. set_signals(info);
  657. spin_unlock_irqrestore(&info->lock,flags);
  658. }
  659. /* Handle transition away from B0 status */
  660. if (!(old_termios->c_cflag & CBAUD) &&
  661. tty->termios->c_cflag & CBAUD) {
  662. info->signals |= SerialSignal_DTR;
  663. if (!(tty->termios->c_cflag & CRTSCTS) ||
  664. !test_bit(TTY_THROTTLED, &tty->flags)) {
  665. info->signals |= SerialSignal_RTS;
  666. }
  667. spin_lock_irqsave(&info->lock,flags);
  668. set_signals(info);
  669. spin_unlock_irqrestore(&info->lock,flags);
  670. }
  671. /* Handle turning off CRTSCTS */
  672. if (old_termios->c_cflag & CRTSCTS &&
  673. !(tty->termios->c_cflag & CRTSCTS)) {
  674. tty->hw_stopped = 0;
  675. tx_release(tty);
  676. }
  677. }
  678. static void update_tx_timer(struct slgt_info *info)
  679. {
  680. /*
  681. * use worst case speed of 1200bps to calculate transmit timeout
  682. * based on data in buffers (tbuf_bytes) and FIFO (128 bytes)
  683. */
  684. if (info->params.mode == MGSL_MODE_HDLC) {
  685. int timeout = (tbuf_bytes(info) * 7) + 1000;
  686. mod_timer(&info->tx_timer, jiffies + msecs_to_jiffies(timeout));
  687. }
  688. }
  689. static int write(struct tty_struct *tty,
  690. const unsigned char *buf, int count)
  691. {
  692. int ret = 0;
  693. struct slgt_info *info = tty->driver_data;
  694. unsigned long flags;
  695. unsigned int bufs_needed;
  696. if (sanity_check(info, tty->name, "write"))
  697. goto cleanup;
  698. DBGINFO(("%s write count=%d\n", info->device_name, count));
  699. if (!info->tx_buf)
  700. goto cleanup;
  701. if (count > info->max_frame_size) {
  702. ret = -EIO;
  703. goto cleanup;
  704. }
  705. if (!count)
  706. goto cleanup;
  707. if (!info->tx_active && info->tx_count) {
  708. /* send accumulated data from send_char() */
  709. tx_load(info, info->tx_buf, info->tx_count);
  710. goto start;
  711. }
  712. bufs_needed = (count/DMABUFSIZE);
  713. if (count % DMABUFSIZE)
  714. ++bufs_needed;
  715. if (bufs_needed > free_tbuf_count(info))
  716. goto cleanup;
  717. ret = info->tx_count = count;
  718. tx_load(info, buf, count);
  719. goto start;
  720. start:
  721. if (info->tx_count && !tty->stopped && !tty->hw_stopped) {
  722. spin_lock_irqsave(&info->lock,flags);
  723. if (!info->tx_active)
  724. tx_start(info);
  725. else if (!(rd_reg32(info, TDCSR) & BIT0)) {
  726. /* transmit still active but transmit DMA stopped */
  727. unsigned int i = info->tbuf_current;
  728. if (!i)
  729. i = info->tbuf_count;
  730. i--;
  731. /* if DMA buf unsent must try later after tx idle */
  732. if (desc_count(info->tbufs[i]))
  733. ret = 0;
  734. }
  735. if (ret > 0)
  736. update_tx_timer(info);
  737. spin_unlock_irqrestore(&info->lock,flags);
  738. }
  739. cleanup:
  740. DBGINFO(("%s write rc=%d\n", info->device_name, ret));
  741. return ret;
  742. }
  743. static int put_char(struct tty_struct *tty, unsigned char ch)
  744. {
  745. struct slgt_info *info = tty->driver_data;
  746. unsigned long flags;
  747. int ret = 0;
  748. if (sanity_check(info, tty->name, "put_char"))
  749. return 0;
  750. DBGINFO(("%s put_char(%d)\n", info->device_name, ch));
  751. if (!info->tx_buf)
  752. return 0;
  753. spin_lock_irqsave(&info->lock,flags);
  754. if (!info->tx_active && (info->tx_count < info->max_frame_size)) {
  755. info->tx_buf[info->tx_count++] = ch;
  756. ret = 1;
  757. }
  758. spin_unlock_irqrestore(&info->lock,flags);
  759. return ret;
  760. }
  761. static void send_xchar(struct tty_struct *tty, char ch)
  762. {
  763. struct slgt_info *info = tty->driver_data;
  764. unsigned long flags;
  765. if (sanity_check(info, tty->name, "send_xchar"))
  766. return;
  767. DBGINFO(("%s send_xchar(%d)\n", info->device_name, ch));
  768. info->x_char = ch;
  769. if (ch) {
  770. spin_lock_irqsave(&info->lock,flags);
  771. if (!info->tx_enabled)
  772. tx_start(info);
  773. spin_unlock_irqrestore(&info->lock,flags);
  774. }
  775. }
  776. static void wait_until_sent(struct tty_struct *tty, int timeout)
  777. {
  778. struct slgt_info *info = tty->driver_data;
  779. unsigned long orig_jiffies, char_time;
  780. if (!info )
  781. return;
  782. if (sanity_check(info, tty->name, "wait_until_sent"))
  783. return;
  784. DBGINFO(("%s wait_until_sent entry\n", info->device_name));
  785. if (!(info->port.flags & ASYNC_INITIALIZED))
  786. goto exit;
  787. orig_jiffies = jiffies;
  788. /* Set check interval to 1/5 of estimated time to
  789. * send a character, and make it at least 1. The check
  790. * interval should also be less than the timeout.
  791. * Note: use tight timings here to satisfy the NIST-PCTS.
  792. */
  793. lock_kernel();
  794. if (info->params.data_rate) {
  795. char_time = info->timeout/(32 * 5);
  796. if (!char_time)
  797. char_time++;
  798. } else
  799. char_time = 1;
  800. if (timeout)
  801. char_time = min_t(unsigned long, char_time, timeout);
  802. while (info->tx_active) {
  803. msleep_interruptible(jiffies_to_msecs(char_time));
  804. if (signal_pending(current))
  805. break;
  806. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  807. break;
  808. }
  809. unlock_kernel();
  810. exit:
  811. DBGINFO(("%s wait_until_sent exit\n", info->device_name));
  812. }
  813. static int write_room(struct tty_struct *tty)
  814. {
  815. struct slgt_info *info = tty->driver_data;
  816. int ret;
  817. if (sanity_check(info, tty->name, "write_room"))
  818. return 0;
  819. ret = (info->tx_active) ? 0 : HDLC_MAX_FRAME_SIZE;
  820. DBGINFO(("%s write_room=%d\n", info->device_name, ret));
  821. return ret;
  822. }
  823. static void flush_chars(struct tty_struct *tty)
  824. {
  825. struct slgt_info *info = tty->driver_data;
  826. unsigned long flags;
  827. if (sanity_check(info, tty->name, "flush_chars"))
  828. return;
  829. DBGINFO(("%s flush_chars entry tx_count=%d\n", info->device_name, info->tx_count));
  830. if (info->tx_count <= 0 || tty->stopped ||
  831. tty->hw_stopped || !info->tx_buf)
  832. return;
  833. DBGINFO(("%s flush_chars start transmit\n", info->device_name));
  834. spin_lock_irqsave(&info->lock,flags);
  835. if (!info->tx_active && info->tx_count) {
  836. tx_load(info, info->tx_buf,info->tx_count);
  837. tx_start(info);
  838. }
  839. spin_unlock_irqrestore(&info->lock,flags);
  840. }
  841. static void flush_buffer(struct tty_struct *tty)
  842. {
  843. struct slgt_info *info = tty->driver_data;
  844. unsigned long flags;
  845. if (sanity_check(info, tty->name, "flush_buffer"))
  846. return;
  847. DBGINFO(("%s flush_buffer\n", info->device_name));
  848. spin_lock_irqsave(&info->lock,flags);
  849. if (!info->tx_active)
  850. info->tx_count = 0;
  851. spin_unlock_irqrestore(&info->lock,flags);
  852. tty_wakeup(tty);
  853. }
  854. /*
  855. * throttle (stop) transmitter
  856. */
  857. static void tx_hold(struct tty_struct *tty)
  858. {
  859. struct slgt_info *info = tty->driver_data;
  860. unsigned long flags;
  861. if (sanity_check(info, tty->name, "tx_hold"))
  862. return;
  863. DBGINFO(("%s tx_hold\n", info->device_name));
  864. spin_lock_irqsave(&info->lock,flags);
  865. if (info->tx_enabled && info->params.mode == MGSL_MODE_ASYNC)
  866. tx_stop(info);
  867. spin_unlock_irqrestore(&info->lock,flags);
  868. }
  869. /*
  870. * release (start) transmitter
  871. */
  872. static void tx_release(struct tty_struct *tty)
  873. {
  874. struct slgt_info *info = tty->driver_data;
  875. unsigned long flags;
  876. if (sanity_check(info, tty->name, "tx_release"))
  877. return;
  878. DBGINFO(("%s tx_release\n", info->device_name));
  879. spin_lock_irqsave(&info->lock,flags);
  880. if (!info->tx_active && info->tx_count) {
  881. tx_load(info, info->tx_buf, info->tx_count);
  882. tx_start(info);
  883. }
  884. spin_unlock_irqrestore(&info->lock,flags);
  885. }
  886. /*
  887. * Service an IOCTL request
  888. *
  889. * Arguments
  890. *
  891. * tty pointer to tty instance data
  892. * file pointer to associated file object for device
  893. * cmd IOCTL command code
  894. * arg command argument/context
  895. *
  896. * Return 0 if success, otherwise error code
  897. */
  898. static int ioctl(struct tty_struct *tty, struct file *file,
  899. unsigned int cmd, unsigned long arg)
  900. {
  901. struct slgt_info *info = tty->driver_data;
  902. struct mgsl_icount cnow; /* kernel counter temps */
  903. struct serial_icounter_struct __user *p_cuser; /* user space */
  904. unsigned long flags;
  905. void __user *argp = (void __user *)arg;
  906. int ret;
  907. if (sanity_check(info, tty->name, "ioctl"))
  908. return -ENODEV;
  909. DBGINFO(("%s ioctl() cmd=%08X\n", info->device_name, cmd));
  910. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  911. (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
  912. if (tty->flags & (1 << TTY_IO_ERROR))
  913. return -EIO;
  914. }
  915. lock_kernel();
  916. switch (cmd) {
  917. case MGSL_IOCGPARAMS:
  918. ret = get_params(info, argp);
  919. break;
  920. case MGSL_IOCSPARAMS:
  921. ret = set_params(info, argp);
  922. break;
  923. case MGSL_IOCGTXIDLE:
  924. ret = get_txidle(info, argp);
  925. break;
  926. case MGSL_IOCSTXIDLE:
  927. ret = set_txidle(info, (int)arg);
  928. break;
  929. case MGSL_IOCTXENABLE:
  930. ret = tx_enable(info, (int)arg);
  931. break;
  932. case MGSL_IOCRXENABLE:
  933. ret = rx_enable(info, (int)arg);
  934. break;
  935. case MGSL_IOCTXABORT:
  936. ret = tx_abort(info);
  937. break;
  938. case MGSL_IOCGSTATS:
  939. ret = get_stats(info, argp);
  940. break;
  941. case MGSL_IOCWAITEVENT:
  942. ret = wait_mgsl_event(info, argp);
  943. break;
  944. case TIOCMIWAIT:
  945. ret = modem_input_wait(info,(int)arg);
  946. break;
  947. case MGSL_IOCGIF:
  948. ret = get_interface(info, argp);
  949. break;
  950. case MGSL_IOCSIF:
  951. ret = set_interface(info,(int)arg);
  952. break;
  953. case MGSL_IOCSGPIO:
  954. ret = set_gpio(info, argp);
  955. break;
  956. case MGSL_IOCGGPIO:
  957. ret = get_gpio(info, argp);
  958. break;
  959. case MGSL_IOCWAITGPIO:
  960. ret = wait_gpio(info, argp);
  961. break;
  962. case TIOCGICOUNT:
  963. spin_lock_irqsave(&info->lock,flags);
  964. cnow = info->icount;
  965. spin_unlock_irqrestore(&info->lock,flags);
  966. p_cuser = argp;
  967. if (put_user(cnow.cts, &p_cuser->cts) ||
  968. put_user(cnow.dsr, &p_cuser->dsr) ||
  969. put_user(cnow.rng, &p_cuser->rng) ||
  970. put_user(cnow.dcd, &p_cuser->dcd) ||
  971. put_user(cnow.rx, &p_cuser->rx) ||
  972. put_user(cnow.tx, &p_cuser->tx) ||
  973. put_user(cnow.frame, &p_cuser->frame) ||
  974. put_user(cnow.overrun, &p_cuser->overrun) ||
  975. put_user(cnow.parity, &p_cuser->parity) ||
  976. put_user(cnow.brk, &p_cuser->brk) ||
  977. put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
  978. ret = -EFAULT;
  979. ret = 0;
  980. break;
  981. default:
  982. ret = -ENOIOCTLCMD;
  983. }
  984. unlock_kernel();
  985. return ret;
  986. }
  987. /*
  988. * support for 32 bit ioctl calls on 64 bit systems
  989. */
  990. #ifdef CONFIG_COMPAT
  991. static long get_params32(struct slgt_info *info, struct MGSL_PARAMS32 __user *user_params)
  992. {
  993. struct MGSL_PARAMS32 tmp_params;
  994. DBGINFO(("%s get_params32\n", info->device_name));
  995. tmp_params.mode = (compat_ulong_t)info->params.mode;
  996. tmp_params.loopback = info->params.loopback;
  997. tmp_params.flags = info->params.flags;
  998. tmp_params.encoding = info->params.encoding;
  999. tmp_params.clock_speed = (compat_ulong_t)info->params.clock_speed;
  1000. tmp_params.addr_filter = info->params.addr_filter;
  1001. tmp_params.crc_type = info->params.crc_type;
  1002. tmp_params.preamble_length = info->params.preamble_length;
  1003. tmp_params.preamble = info->params.preamble;
  1004. tmp_params.data_rate = (compat_ulong_t)info->params.data_rate;
  1005. tmp_params.data_bits = info->params.data_bits;
  1006. tmp_params.stop_bits = info->params.stop_bits;
  1007. tmp_params.parity = info->params.parity;
  1008. if (copy_to_user(user_params, &tmp_params, sizeof(struct MGSL_PARAMS32)))
  1009. return -EFAULT;
  1010. return 0;
  1011. }
  1012. static long set_params32(struct slgt_info *info, struct MGSL_PARAMS32 __user *new_params)
  1013. {
  1014. struct MGSL_PARAMS32 tmp_params;
  1015. DBGINFO(("%s set_params32\n", info->device_name));
  1016. if (copy_from_user(&tmp_params, new_params, sizeof(struct MGSL_PARAMS32)))
  1017. return -EFAULT;
  1018. spin_lock(&info->lock);
  1019. if (tmp_params.mode == MGSL_MODE_BASE_CLOCK) {
  1020. info->base_clock = tmp_params.clock_speed;
  1021. } else {
  1022. info->params.mode = tmp_params.mode;
  1023. info->params.loopback = tmp_params.loopback;
  1024. info->params.flags = tmp_params.flags;
  1025. info->params.encoding = tmp_params.encoding;
  1026. info->params.clock_speed = tmp_params.clock_speed;
  1027. info->params.addr_filter = tmp_params.addr_filter;
  1028. info->params.crc_type = tmp_params.crc_type;
  1029. info->params.preamble_length = tmp_params.preamble_length;
  1030. info->params.preamble = tmp_params.preamble;
  1031. info->params.data_rate = tmp_params.data_rate;
  1032. info->params.data_bits = tmp_params.data_bits;
  1033. info->params.stop_bits = tmp_params.stop_bits;
  1034. info->params.parity = tmp_params.parity;
  1035. }
  1036. spin_unlock(&info->lock);
  1037. program_hw(info);
  1038. return 0;
  1039. }
  1040. static long slgt_compat_ioctl(struct tty_struct *tty, struct file *file,
  1041. unsigned int cmd, unsigned long arg)
  1042. {
  1043. struct slgt_info *info = tty->driver_data;
  1044. int rc = -ENOIOCTLCMD;
  1045. if (sanity_check(info, tty->name, "compat_ioctl"))
  1046. return -ENODEV;
  1047. DBGINFO(("%s compat_ioctl() cmd=%08X\n", info->device_name, cmd));
  1048. switch (cmd) {
  1049. case MGSL_IOCSPARAMS32:
  1050. rc = set_params32(info, compat_ptr(arg));
  1051. break;
  1052. case MGSL_IOCGPARAMS32:
  1053. rc = get_params32(info, compat_ptr(arg));
  1054. break;
  1055. case MGSL_IOCGPARAMS:
  1056. case MGSL_IOCSPARAMS:
  1057. case MGSL_IOCGTXIDLE:
  1058. case MGSL_IOCGSTATS:
  1059. case MGSL_IOCWAITEVENT:
  1060. case MGSL_IOCGIF:
  1061. case MGSL_IOCSGPIO:
  1062. case MGSL_IOCGGPIO:
  1063. case MGSL_IOCWAITGPIO:
  1064. case TIOCGICOUNT:
  1065. rc = ioctl(tty, file, cmd, (unsigned long)(compat_ptr(arg)));
  1066. break;
  1067. case MGSL_IOCSTXIDLE:
  1068. case MGSL_IOCTXENABLE:
  1069. case MGSL_IOCRXENABLE:
  1070. case MGSL_IOCTXABORT:
  1071. case TIOCMIWAIT:
  1072. case MGSL_IOCSIF:
  1073. rc = ioctl(tty, file, cmd, arg);
  1074. break;
  1075. }
  1076. DBGINFO(("%s compat_ioctl() cmd=%08X rc=%d\n", info->device_name, cmd, rc));
  1077. return rc;
  1078. }
  1079. #else
  1080. #define slgt_compat_ioctl NULL
  1081. #endif /* ifdef CONFIG_COMPAT */
  1082. /*
  1083. * proc fs support
  1084. */
  1085. static inline void line_info(struct seq_file *m, struct slgt_info *info)
  1086. {
  1087. char stat_buf[30];
  1088. unsigned long flags;
  1089. seq_printf(m, "%s: IO=%08X IRQ=%d MaxFrameSize=%u\n",
  1090. info->device_name, info->phys_reg_addr,
  1091. info->irq_level, info->max_frame_size);
  1092. /* output current serial signal states */
  1093. spin_lock_irqsave(&info->lock,flags);
  1094. get_signals(info);
  1095. spin_unlock_irqrestore(&info->lock,flags);
  1096. stat_buf[0] = 0;
  1097. stat_buf[1] = 0;
  1098. if (info->signals & SerialSignal_RTS)
  1099. strcat(stat_buf, "|RTS");
  1100. if (info->signals & SerialSignal_CTS)
  1101. strcat(stat_buf, "|CTS");
  1102. if (info->signals & SerialSignal_DTR)
  1103. strcat(stat_buf, "|DTR");
  1104. if (info->signals & SerialSignal_DSR)
  1105. strcat(stat_buf, "|DSR");
  1106. if (info->signals & SerialSignal_DCD)
  1107. strcat(stat_buf, "|CD");
  1108. if (info->signals & SerialSignal_RI)
  1109. strcat(stat_buf, "|RI");
  1110. if (info->params.mode != MGSL_MODE_ASYNC) {
  1111. seq_printf(m, "\tHDLC txok:%d rxok:%d",
  1112. info->icount.txok, info->icount.rxok);
  1113. if (info->icount.txunder)
  1114. seq_printf(m, " txunder:%d", info->icount.txunder);
  1115. if (info->icount.txabort)
  1116. seq_printf(m, " txabort:%d", info->icount.txabort);
  1117. if (info->icount.rxshort)
  1118. seq_printf(m, " rxshort:%d", info->icount.rxshort);
  1119. if (info->icount.rxlong)
  1120. seq_printf(m, " rxlong:%d", info->icount.rxlong);
  1121. if (info->icount.rxover)
  1122. seq_printf(m, " rxover:%d", info->icount.rxover);
  1123. if (info->icount.rxcrc)
  1124. seq_printf(m, " rxcrc:%d", info->icount.rxcrc);
  1125. } else {
  1126. seq_printf(m, "\tASYNC tx:%d rx:%d",
  1127. info->icount.tx, info->icount.rx);
  1128. if (info->icount.frame)
  1129. seq_printf(m, " fe:%d", info->icount.frame);
  1130. if (info->icount.parity)
  1131. seq_printf(m, " pe:%d", info->icount.parity);
  1132. if (info->icount.brk)
  1133. seq_printf(m, " brk:%d", info->icount.brk);
  1134. if (info->icount.overrun)
  1135. seq_printf(m, " oe:%d", info->icount.overrun);
  1136. }
  1137. /* Append serial signal status to end */
  1138. seq_printf(m, " %s\n", stat_buf+1);
  1139. seq_printf(m, "\ttxactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
  1140. info->tx_active,info->bh_requested,info->bh_running,
  1141. info->pending_bh);
  1142. }
  1143. /* Called to print information about devices
  1144. */
  1145. static int synclink_gt_proc_show(struct seq_file *m, void *v)
  1146. {
  1147. struct slgt_info *info;
  1148. seq_puts(m, "synclink_gt driver\n");
  1149. info = slgt_device_list;
  1150. while( info ) {
  1151. line_info(m, info);
  1152. info = info->next_device;
  1153. }
  1154. return 0;
  1155. }
  1156. static int synclink_gt_proc_open(struct inode *inode, struct file *file)
  1157. {
  1158. return single_open(file, synclink_gt_proc_show, NULL);
  1159. }
  1160. static const struct file_operations synclink_gt_proc_fops = {
  1161. .owner = THIS_MODULE,
  1162. .open = synclink_gt_proc_open,
  1163. .read = seq_read,
  1164. .llseek = seq_lseek,
  1165. .release = single_release,
  1166. };
  1167. /*
  1168. * return count of bytes in transmit buffer
  1169. */
  1170. static int chars_in_buffer(struct tty_struct *tty)
  1171. {
  1172. struct slgt_info *info = tty->driver_data;
  1173. int count;
  1174. if (sanity_check(info, tty->name, "chars_in_buffer"))
  1175. return 0;
  1176. count = tbuf_bytes(info);
  1177. DBGINFO(("%s chars_in_buffer()=%d\n", info->device_name, count));
  1178. return count;
  1179. }
  1180. /*
  1181. * signal remote device to throttle send data (our receive data)
  1182. */
  1183. static void throttle(struct tty_struct * tty)
  1184. {
  1185. struct slgt_info *info = tty->driver_data;
  1186. unsigned long flags;
  1187. if (sanity_check(info, tty->name, "throttle"))
  1188. return;
  1189. DBGINFO(("%s throttle\n", info->device_name));
  1190. if (I_IXOFF(tty))
  1191. send_xchar(tty, STOP_CHAR(tty));
  1192. if (tty->termios->c_cflag & CRTSCTS) {
  1193. spin_lock_irqsave(&info->lock,flags);
  1194. info->signals &= ~SerialSignal_RTS;
  1195. set_signals(info);
  1196. spin_unlock_irqrestore(&info->lock,flags);
  1197. }
  1198. }
  1199. /*
  1200. * signal remote device to stop throttling send data (our receive data)
  1201. */
  1202. static void unthrottle(struct tty_struct * tty)
  1203. {
  1204. struct slgt_info *info = tty->driver_data;
  1205. unsigned long flags;
  1206. if (sanity_check(info, tty->name, "unthrottle"))
  1207. return;
  1208. DBGINFO(("%s unthrottle\n", info->device_name));
  1209. if (I_IXOFF(tty)) {
  1210. if (info->x_char)
  1211. info->x_char = 0;
  1212. else
  1213. send_xchar(tty, START_CHAR(tty));
  1214. }
  1215. if (tty->termios->c_cflag & CRTSCTS) {
  1216. spin_lock_irqsave(&info->lock,flags);
  1217. info->signals |= SerialSignal_RTS;
  1218. set_signals(info);
  1219. spin_unlock_irqrestore(&info->lock,flags);
  1220. }
  1221. }
  1222. /*
  1223. * set or clear transmit break condition
  1224. * break_state -1=set break condition, 0=clear
  1225. */
  1226. static int set_break(struct tty_struct *tty, int break_state)
  1227. {
  1228. struct slgt_info *info = tty->driver_data;
  1229. unsigned short value;
  1230. unsigned long flags;
  1231. if (sanity_check(info, tty->name, "set_break"))
  1232. return -EINVAL;
  1233. DBGINFO(("%s set_break(%d)\n", info->device_name, break_state));
  1234. spin_lock_irqsave(&info->lock,flags);
  1235. value = rd_reg16(info, TCR);
  1236. if (break_state == -1)
  1237. value |= BIT6;
  1238. else
  1239. value &= ~BIT6;
  1240. wr_reg16(info, TCR, value);
  1241. spin_unlock_irqrestore(&info->lock,flags);
  1242. return 0;
  1243. }
  1244. #if SYNCLINK_GENERIC_HDLC
  1245. /**
  1246. * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
  1247. * set encoding and frame check sequence (FCS) options
  1248. *
  1249. * dev pointer to network device structure
  1250. * encoding serial encoding setting
  1251. * parity FCS setting
  1252. *
  1253. * returns 0 if success, otherwise error code
  1254. */
  1255. static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
  1256. unsigned short parity)
  1257. {
  1258. struct slgt_info *info = dev_to_port(dev);
  1259. unsigned char new_encoding;
  1260. unsigned short new_crctype;
  1261. /* return error if TTY interface open */
  1262. if (info->port.count)
  1263. return -EBUSY;
  1264. DBGINFO(("%s hdlcdev_attach\n", info->device_name));
  1265. switch (encoding)
  1266. {
  1267. case ENCODING_NRZ: new_encoding = HDLC_ENCODING_NRZ; break;
  1268. case ENCODING_NRZI: new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
  1269. case ENCODING_FM_MARK: new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
  1270. case ENCODING_FM_SPACE: new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
  1271. case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
  1272. default: return -EINVAL;
  1273. }
  1274. switch (parity)
  1275. {
  1276. case PARITY_NONE: new_crctype = HDLC_CRC_NONE; break;
  1277. case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
  1278. case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
  1279. default: return -EINVAL;
  1280. }
  1281. info->params.encoding = new_encoding;
  1282. info->params.crc_type = new_crctype;
  1283. /* if network interface up, reprogram hardware */
  1284. if (info->netcount)
  1285. program_hw(info);
  1286. return 0;
  1287. }
  1288. /**
  1289. * called by generic HDLC layer to send frame
  1290. *
  1291. * skb socket buffer containing HDLC frame
  1292. * dev pointer to network device structure
  1293. *
  1294. * returns 0 if success, otherwise error code
  1295. */
  1296. static int hdlcdev_xmit(struct sk_buff *skb, struct net_device *dev)
  1297. {
  1298. struct slgt_info *info = dev_to_port(dev);
  1299. unsigned long flags;
  1300. DBGINFO(("%s hdlc_xmit\n", dev->name));
  1301. /* stop sending until this frame completes */
  1302. netif_stop_queue(dev);
  1303. /* copy data to device buffers */
  1304. info->tx_count = skb->len;
  1305. tx_load(info, skb->data, skb->len);
  1306. /* update network statistics */
  1307. dev->stats.tx_packets++;
  1308. dev->stats.tx_bytes += skb->len;
  1309. /* done with socket buffer, so free it */
  1310. dev_kfree_skb(skb);
  1311. /* save start time for transmit timeout detection */
  1312. dev->trans_start = jiffies;
  1313. spin_lock_irqsave(&info->lock,flags);
  1314. tx_start(info);
  1315. update_tx_timer(info);
  1316. spin_unlock_irqrestore(&info->lock,flags);
  1317. return 0;
  1318. }
  1319. /**
  1320. * called by network layer when interface enabled
  1321. * claim resources and initialize hardware
  1322. *
  1323. * dev pointer to network device structure
  1324. *
  1325. * returns 0 if success, otherwise error code
  1326. */
  1327. static int hdlcdev_open(struct net_device *dev)
  1328. {
  1329. struct slgt_info *info = dev_to_port(dev);
  1330. int rc;
  1331. unsigned long flags;
  1332. if (!try_module_get(THIS_MODULE))
  1333. return -EBUSY;
  1334. DBGINFO(("%s hdlcdev_open\n", dev->name));
  1335. /* generic HDLC layer open processing */
  1336. if ((rc = hdlc_open(dev)))
  1337. return rc;
  1338. /* arbitrate between network and tty opens */
  1339. spin_lock_irqsave(&info->netlock, flags);
  1340. if (info->port.count != 0 || info->netcount != 0) {
  1341. DBGINFO(("%s hdlc_open busy\n", dev->name));
  1342. spin_unlock_irqrestore(&info->netlock, flags);
  1343. return -EBUSY;
  1344. }
  1345. info->netcount=1;
  1346. spin_unlock_irqrestore(&info->netlock, flags);
  1347. /* claim resources and init adapter */
  1348. if ((rc = startup(info)) != 0) {
  1349. spin_lock_irqsave(&info->netlock, flags);
  1350. info->netcount=0;
  1351. spin_unlock_irqrestore(&info->netlock, flags);
  1352. return rc;
  1353. }
  1354. /* assert DTR and RTS, apply hardware settings */
  1355. info->signals |= SerialSignal_RTS + SerialSignal_DTR;
  1356. program_hw(info);
  1357. /* enable network layer transmit */
  1358. dev->trans_start = jiffies;
  1359. netif_start_queue(dev);
  1360. /* inform generic HDLC layer of current DCD status */
  1361. spin_lock_irqsave(&info->lock, flags);
  1362. get_signals(info);
  1363. spin_unlock_irqrestore(&info->lock, flags);
  1364. if (info->signals & SerialSignal_DCD)
  1365. netif_carrier_on(dev);
  1366. else
  1367. netif_carrier_off(dev);
  1368. return 0;
  1369. }
  1370. /**
  1371. * called by network layer when interface is disabled
  1372. * shutdown hardware and release resources
  1373. *
  1374. * dev pointer to network device structure
  1375. *
  1376. * returns 0 if success, otherwise error code
  1377. */
  1378. static int hdlcdev_close(struct net_device *dev)
  1379. {
  1380. struct slgt_info *info = dev_to_port(dev);
  1381. unsigned long flags;
  1382. DBGINFO(("%s hdlcdev_close\n", dev->name));
  1383. netif_stop_queue(dev);
  1384. /* shutdown adapter and release resources */
  1385. shutdown(info);
  1386. hdlc_close(dev);
  1387. spin_lock_irqsave(&info->netlock, flags);
  1388. info->netcount=0;
  1389. spin_unlock_irqrestore(&info->netlock, flags);
  1390. module_put(THIS_MODULE);
  1391. return 0;
  1392. }
  1393. /**
  1394. * called by network layer to process IOCTL call to network device
  1395. *
  1396. * dev pointer to network device structure
  1397. * ifr pointer to network interface request structure
  1398. * cmd IOCTL command code
  1399. *
  1400. * returns 0 if success, otherwise error code
  1401. */
  1402. static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1403. {
  1404. const size_t size = sizeof(sync_serial_settings);
  1405. sync_serial_settings new_line;
  1406. sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
  1407. struct slgt_info *info = dev_to_port(dev);
  1408. unsigned int flags;
  1409. DBGINFO(("%s hdlcdev_ioctl\n", dev->name));
  1410. /* return error if TTY interface open */
  1411. if (info->port.count)
  1412. return -EBUSY;
  1413. if (cmd != SIOCWANDEV)
  1414. return hdlc_ioctl(dev, ifr, cmd);
  1415. switch(ifr->ifr_settings.type) {
  1416. case IF_GET_IFACE: /* return current sync_serial_settings */
  1417. ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
  1418. if (ifr->ifr_settings.size < size) {
  1419. ifr->ifr_settings.size = size; /* data size wanted */
  1420. return -ENOBUFS;
  1421. }
  1422. flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
  1423. HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
  1424. HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
  1425. HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
  1426. switch (flags){
  1427. case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
  1428. case (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_INT; break;
  1429. case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_TXINT; break;
  1430. case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
  1431. default: new_line.clock_type = CLOCK_DEFAULT;
  1432. }
  1433. new_line.clock_rate = info->params.clock_speed;
  1434. new_line.loopback = info->params.loopback ? 1:0;
  1435. if (copy_to_user(line, &new_line, size))
  1436. return -EFAULT;
  1437. return 0;
  1438. case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
  1439. if(!capable(CAP_NET_ADMIN))
  1440. return -EPERM;
  1441. if (copy_from_user(&new_line, line, size))
  1442. return -EFAULT;
  1443. switch (new_line.clock_type)
  1444. {
  1445. case CLOCK_EXT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
  1446. case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
  1447. case CLOCK_INT: flags = HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG; break;
  1448. case CLOCK_TXINT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG; break;
  1449. case CLOCK_DEFAULT: flags = info->params.flags &
  1450. (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
  1451. HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
  1452. HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
  1453. HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN); break;
  1454. default: return -EINVAL;
  1455. }
  1456. if (new_line.loopback != 0 && new_line.loopback != 1)
  1457. return -EINVAL;
  1458. info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
  1459. HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
  1460. HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
  1461. HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
  1462. info->params.flags |= flags;
  1463. info->params.loopback = new_line.loopback;
  1464. if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
  1465. info->params.clock_speed = new_line.clock_rate;
  1466. else
  1467. info->params.clock_speed = 0;
  1468. /* if network interface up, reprogram hardware */
  1469. if (info->netcount)
  1470. program_hw(info);
  1471. return 0;
  1472. default:
  1473. return hdlc_ioctl(dev, ifr, cmd);
  1474. }
  1475. }
  1476. /**
  1477. * called by network layer when transmit timeout is detected
  1478. *
  1479. * dev pointer to network device structure
  1480. */
  1481. static void hdlcdev_tx_timeout(struct net_device *dev)
  1482. {
  1483. struct slgt_info *info = dev_to_port(dev);
  1484. unsigned long flags;
  1485. DBGINFO(("%s hdlcdev_tx_timeout\n", dev->name));
  1486. dev->stats.tx_errors++;
  1487. dev->stats.tx_aborted_errors++;
  1488. spin_lock_irqsave(&info->lock,flags);
  1489. tx_stop(info);
  1490. spin_unlock_irqrestore(&info->lock,flags);
  1491. netif_wake_queue(dev);
  1492. }
  1493. /**
  1494. * called by device driver when transmit completes
  1495. * reenable network layer transmit if stopped
  1496. *
  1497. * info pointer to device instance information
  1498. */
  1499. static void hdlcdev_tx_done(struct slgt_info *info)
  1500. {
  1501. if (netif_queue_stopped(info->netdev))
  1502. netif_wake_queue(info->netdev);
  1503. }
  1504. /**
  1505. * called by device driver when frame received
  1506. * pass frame to network layer
  1507. *
  1508. * info pointer to device instance information
  1509. * buf pointer to buffer contianing frame data
  1510. * size count of data bytes in buf
  1511. */
  1512. static void hdlcdev_rx(struct slgt_info *info, char *buf, int size)
  1513. {
  1514. struct sk_buff *skb = dev_alloc_skb(size);
  1515. struct net_device *dev = info->netdev;
  1516. DBGINFO(("%s hdlcdev_rx\n", dev->name));
  1517. if (skb == NULL) {
  1518. DBGERR(("%s: can't alloc skb, drop packet\n", dev->name));
  1519. dev->stats.rx_dropped++;
  1520. return;
  1521. }
  1522. memcpy(skb_put(skb, size), buf, size);
  1523. skb->protocol = hdlc_type_trans(skb, dev);
  1524. dev->stats.rx_packets++;
  1525. dev->stats.rx_bytes += size;
  1526. netif_rx(skb);
  1527. }
  1528. static const struct net_device_ops hdlcdev_ops = {
  1529. .ndo_open = hdlcdev_open,
  1530. .ndo_stop = hdlcdev_close,
  1531. .ndo_change_mtu = hdlc_change_mtu,
  1532. .ndo_start_xmit = hdlc_start_xmit,
  1533. .ndo_do_ioctl = hdlcdev_ioctl,
  1534. .ndo_tx_timeout = hdlcdev_tx_timeout,
  1535. };
  1536. /**
  1537. * called by device driver when adding device instance
  1538. * do generic HDLC initialization
  1539. *
  1540. * info pointer to device instance information
  1541. *
  1542. * returns 0 if success, otherwise error code
  1543. */
  1544. static int hdlcdev_init(struct slgt_info *info)
  1545. {
  1546. int rc;
  1547. struct net_device *dev;
  1548. hdlc_device *hdlc;
  1549. /* allocate and initialize network and HDLC layer objects */
  1550. if (!(dev = alloc_hdlcdev(info))) {
  1551. printk(KERN_ERR "%s hdlc device alloc failure\n", info->device_name);
  1552. return -ENOMEM;
  1553. }
  1554. /* for network layer reporting purposes only */
  1555. dev->mem_start = info->phys_reg_addr;
  1556. dev->mem_end = info->phys_reg_addr + SLGT_REG_SIZE - 1;
  1557. dev->irq = info->irq_level;
  1558. /* network layer callbacks and settings */
  1559. dev->netdev_ops = &hdlcdev_ops;
  1560. dev->watchdog_timeo = 10 * HZ;
  1561. dev->tx_queue_len = 50;
  1562. /* generic HDLC layer callbacks and settings */
  1563. hdlc = dev_to_hdlc(dev);
  1564. hdlc->attach = hdlcdev_attach;
  1565. hdlc->xmit = hdlcdev_xmit;
  1566. /* register objects with HDLC layer */
  1567. if ((rc = register_hdlc_device(dev))) {
  1568. printk(KERN_WARNING "%s:unable to register hdlc device\n",__FILE__);
  1569. free_netdev(dev);
  1570. return rc;
  1571. }
  1572. info->netdev = dev;
  1573. return 0;
  1574. }
  1575. /**
  1576. * called by device driver when removing device instance
  1577. * do generic HDLC cleanup
  1578. *
  1579. * info pointer to device instance information
  1580. */
  1581. static void hdlcdev_exit(struct slgt_info *info)
  1582. {
  1583. unregister_hdlc_device(info->netdev);
  1584. free_netdev(info->netdev);
  1585. info->netdev = NULL;
  1586. }
  1587. #endif /* ifdef CONFIG_HDLC */
  1588. /*
  1589. * get async data from rx DMA buffers
  1590. */
  1591. static void rx_async(struct slgt_info *info)
  1592. {
  1593. struct tty_struct *tty = info->port.tty;
  1594. struct mgsl_icount *icount = &info->icount;
  1595. unsigned int start, end;
  1596. unsigned char *p;
  1597. unsigned char status;
  1598. struct slgt_desc *bufs = info->rbufs;
  1599. int i, count;
  1600. int chars = 0;
  1601. int stat;
  1602. unsigned char ch;
  1603. start = end = info->rbuf_current;
  1604. while(desc_complete(bufs[end])) {
  1605. count = desc_count(bufs[end]) - info->rbuf_index;
  1606. p = bufs[end].buf + info->rbuf_index;
  1607. DBGISR(("%s rx_async count=%d\n", info->device_name, count));
  1608. DBGDATA(info, p, count, "rx");
  1609. for(i=0 ; i < count; i+=2, p+=2) {
  1610. ch = *p;
  1611. icount->rx++;
  1612. stat = 0;
  1613. if ((status = *(p+1) & (BIT1 + BIT0))) {
  1614. if (status & BIT1)
  1615. icount->parity++;
  1616. else if (status & BIT0)
  1617. icount->frame++;
  1618. /* discard char if tty control flags say so */
  1619. if (status & info->ignore_status_mask)
  1620. continue;
  1621. if (status & BIT1)
  1622. stat = TTY_PARITY;
  1623. else if (status & BIT0)
  1624. stat = TTY_FRAME;
  1625. }
  1626. if (tty) {
  1627. tty_insert_flip_char(tty, ch, stat);
  1628. chars++;
  1629. }
  1630. }
  1631. if (i < count) {
  1632. /* receive buffer not completed */
  1633. info->rbuf_index += i;
  1634. mod_timer(&info->rx_timer, jiffies + 1);
  1635. break;
  1636. }
  1637. info->rbuf_index = 0;
  1638. free_rbufs(info, end, end);
  1639. if (++end == info->rbuf_count)
  1640. end = 0;
  1641. /* if entire list searched then no frame available */
  1642. if (end == start)
  1643. break;
  1644. }
  1645. if (tty && chars)
  1646. tty_flip_buffer_push(tty);
  1647. }
  1648. /*
  1649. * return next bottom half action to perform
  1650. */
  1651. static int bh_action(struct slgt_info *info)
  1652. {
  1653. unsigned long flags;
  1654. int rc;
  1655. spin_lock_irqsave(&info->lock,flags);
  1656. if (info->pending_bh & BH_RECEIVE) {
  1657. info->pending_bh &= ~BH_RECEIVE;
  1658. rc = BH_RECEIVE;
  1659. } else if (info->pending_bh & BH_TRANSMIT) {
  1660. info->pending_bh &= ~BH_TRANSMIT;
  1661. rc = BH_TRANSMIT;
  1662. } else if (info->pending_bh & BH_STATUS) {
  1663. info->pending_bh &= ~BH_STATUS;
  1664. rc = BH_STATUS;
  1665. } else {
  1666. /* Mark BH routine as complete */
  1667. info->bh_running = false;
  1668. info->bh_requested = false;
  1669. rc = 0;
  1670. }
  1671. spin_unlock_irqrestore(&info->lock,flags);
  1672. return rc;
  1673. }
  1674. /*
  1675. * perform bottom half processing
  1676. */
  1677. static void bh_handler(struct work_struct *work)
  1678. {
  1679. struct slgt_info *info = container_of(work, struct slgt_info, task);
  1680. int action;
  1681. if (!info)
  1682. return;
  1683. info->bh_running = true;
  1684. while((action = bh_action(info))) {
  1685. switch (action) {
  1686. case BH_RECEIVE:
  1687. DBGBH(("%s bh receive\n", info->device_name));
  1688. switch(info->params.mode) {
  1689. case MGSL_MODE_ASYNC:
  1690. rx_async(info);
  1691. break;
  1692. case MGSL_MODE_HDLC:
  1693. while(rx_get_frame(info));
  1694. break;
  1695. case MGSL_MODE_RAW:
  1696. case MGSL_MODE_MONOSYNC:
  1697. case MGSL_MODE_BISYNC:
  1698. while(rx_get_buf(info));
  1699. break;
  1700. }
  1701. /* restart receiver if rx DMA buffers exhausted */
  1702. if (info->rx_restart)
  1703. rx_start(info);
  1704. break;
  1705. case BH_TRANSMIT:
  1706. bh_transmit(info);
  1707. break;
  1708. case BH_STATUS:
  1709. DBGBH(("%s bh status\n", info->device_name));
  1710. info->ri_chkcount = 0;
  1711. info->dsr_chkcount = 0;
  1712. info->dcd_chkcount = 0;
  1713. info->cts_chkcount = 0;
  1714. break;
  1715. default:
  1716. DBGBH(("%s unknown action\n", info->device_name));
  1717. break;
  1718. }
  1719. }
  1720. DBGBH(("%s bh_handler exit\n", info->device_name));
  1721. }
  1722. static void bh_transmit(struct slgt_info *info)
  1723. {
  1724. struct tty_struct *tty = info->port.tty;
  1725. DBGBH(("%s bh_transmit\n", info->device_name));
  1726. if (tty)
  1727. tty_wakeup(tty);
  1728. }
  1729. static void dsr_change(struct slgt_info *info, unsigned short status)
  1730. {
  1731. if (status & BIT3) {
  1732. info->signals |= SerialSignal_DSR;
  1733. info->input_signal_events.dsr_up++;
  1734. } else {
  1735. info->signals &= ~SerialSignal_DSR;
  1736. info->input_signal_events.dsr_down++;
  1737. }
  1738. DBGISR(("dsr_change %s signals=%04X\n", info->device_name, info->signals));
  1739. if ((info->dsr_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
  1740. slgt_irq_off(info, IRQ_DSR);
  1741. return;
  1742. }
  1743. info->icount.dsr++;
  1744. wake_up_interruptible(&info->status_event_wait_q);
  1745. wake_up_interruptible(&info->event_wait_q);
  1746. info->pending_bh |= BH_STATUS;
  1747. }
  1748. static void cts_change(struct slgt_info *info, unsigned short status)
  1749. {
  1750. if (status & BIT2) {
  1751. info->signals |= SerialSignal_CTS;
  1752. info->input_signal_events.cts_up++;
  1753. } else {
  1754. info->signals &= ~SerialSignal_CTS;
  1755. info->input_signal_events.cts_down++;
  1756. }
  1757. DBGISR(("cts_change %s signals=%04X\n", info->device_name, info->signals));
  1758. if ((info->cts_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
  1759. slgt_irq_off(info, IRQ_CTS);
  1760. return;
  1761. }
  1762. info->icount.cts++;
  1763. wake_up_interruptible(&info->status_event_wait_q);
  1764. wake_up_interruptible(&info->event_wait_q);
  1765. info->pending_bh |= BH_STATUS;
  1766. if (info->port.flags & ASYNC_CTS_FLOW) {
  1767. if (info->port.tty) {
  1768. if (info->port.tty->hw_stopped) {
  1769. if (info->signals & SerialSignal_CTS) {
  1770. info->port.tty->hw_stopped = 0;
  1771. info->pending_bh |= BH_TRANSMIT;
  1772. return;
  1773. }
  1774. } else {
  1775. if (!(info->signals & SerialSignal_CTS))
  1776. info->port.tty->hw_stopped = 1;
  1777. }
  1778. }
  1779. }
  1780. }
  1781. static void dcd_change(struct slgt_info *info, unsigned short status)
  1782. {
  1783. if (status & BIT1) {
  1784. info->signals |= SerialSignal_DCD;
  1785. info->input_signal_events.dcd_up++;
  1786. } else {
  1787. info->signals &= ~SerialSignal_DCD;
  1788. info->input_signal_events.dcd_down++;
  1789. }
  1790. DBGISR(("dcd_change %s signals=%04X\n", info->device_name, info->signals));
  1791. if ((info->dcd_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
  1792. slgt_irq_off(info, IRQ_DCD);
  1793. return;
  1794. }
  1795. info->icount.dcd++;
  1796. #if SYNCLINK_GENERIC_HDLC
  1797. if (info->netcount) {
  1798. if (info->signals & SerialSignal_DCD)
  1799. netif_carrier_on(info->netdev);
  1800. else
  1801. netif_carrier_off(info->netdev);
  1802. }
  1803. #endif
  1804. wake_up_interruptible(&info->status_event_wait_q);
  1805. wake_up_interruptible(&info->event_wait_q);
  1806. info->pending_bh |= BH_STATUS;
  1807. if (info->port.flags & ASYNC_CHECK_CD) {
  1808. if (info->signals & SerialSignal_DCD)
  1809. wake_up_interruptible(&info->port.open_wait);
  1810. else {
  1811. if (info->port.tty)
  1812. tty_hangup(info->port.tty);
  1813. }
  1814. }
  1815. }
  1816. static void ri_change(struct slgt_info *info, unsigned short status)
  1817. {
  1818. if (status & BIT0) {
  1819. info->signals |= SerialSignal_RI;
  1820. info->input_signal_events.ri_up++;
  1821. } else {
  1822. info->signals &= ~SerialSignal_RI;
  1823. info->input_signal_events.ri_down++;
  1824. }
  1825. DBGISR(("ri_change %s signals=%04X\n", info->device_name, info->signals));
  1826. if ((info->ri_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
  1827. slgt_irq_off(info, IRQ_RI);
  1828. return;
  1829. }
  1830. info->icount.rng++;
  1831. wake_up_interruptible(&info->status_event_wait_q);
  1832. wake_up_interruptible(&info->event_wait_q);
  1833. info->pending_bh |= BH_STATUS;
  1834. }
  1835. static void isr_rxdata(struct slgt_info *info)
  1836. {
  1837. unsigned int count = info->rbuf_fill_count;
  1838. unsigned int i = info->rbuf_fill_index;
  1839. unsigned short reg;
  1840. while (rd_reg16(info, SSR) & IRQ_RXDATA) {
  1841. reg = rd_reg16(info, RDR);
  1842. DBGISR(("isr_rxdata %s RDR=%04X\n", info->device_name, reg));
  1843. if (desc_complete(info->rbufs[i])) {
  1844. /* all buffers full */
  1845. rx_stop(info);
  1846. info->rx_restart = 1;
  1847. continue;
  1848. }
  1849. info->rbufs[i].buf[count++] = (unsigned char)reg;
  1850. /* async mode saves status byte to buffer for each data byte */
  1851. if (info->params.mode == MGSL_MODE_ASYNC)
  1852. info->rbufs[i].buf[count++] = (unsigned char)(reg >> 8);
  1853. if (count == info->rbuf_fill_level || (reg & BIT10)) {
  1854. /* buffer full or end of frame */
  1855. set_desc_count(info->rbufs[i], count);
  1856. set_desc_status(info->rbufs[i], BIT15 | (reg >> 8));
  1857. info->rbuf_fill_count = count = 0;
  1858. if (++i == info->rbuf_count)
  1859. i = 0;
  1860. info->pending_bh |= BH_RECEIVE;
  1861. }
  1862. }
  1863. info->rbuf_fill_index = i;
  1864. info->rbuf_fill_count = count;
  1865. }
  1866. static void isr_serial(struct slgt_info *info)
  1867. {
  1868. unsigned short status = rd_reg16(info, SSR);
  1869. DBGISR(("%s isr_serial status=%04X\n", info->device_name, status));
  1870. wr_reg16(info, SSR, status); /* clear pending */
  1871. info->irq_occurred = true;
  1872. if (info->params.mode == MGSL_MODE_ASYNC) {
  1873. if (status & IRQ_TXIDLE) {
  1874. if (info->tx_count)
  1875. isr_txeom(info, status);
  1876. }
  1877. if (info->rx_pio && (status & IRQ_RXDATA))
  1878. isr_rxdata(info);
  1879. if ((status & IRQ_RXBREAK) && (status & RXBREAK)) {
  1880. info->icount.brk++;
  1881. /* process break detection if tty control allows */
  1882. if (info->port.tty) {
  1883. if (!(status & info->ignore_status_mask)) {
  1884. if (info->read_status_mask & MASK_BREAK) {
  1885. tty_insert_flip_char(info->port.tty, 0, TTY_BREAK);
  1886. if (info->port.flags & ASYNC_SAK)
  1887. do_SAK(info->port.tty);
  1888. }
  1889. }
  1890. }
  1891. }
  1892. } else {
  1893. if (status & (IRQ_TXIDLE + IRQ_TXUNDER))
  1894. isr_txeom(info, status);
  1895. if (info->rx_pio && (status & IRQ_RXDATA))
  1896. isr_rxdata(info);
  1897. if (status & IRQ_RXIDLE) {
  1898. if (status & RXIDLE)
  1899. info->icount.rxidle++;
  1900. else
  1901. info->icount.exithunt++;
  1902. wake_up_interruptible(&info->event_wait_q);
  1903. }
  1904. if (status & IRQ_RXOVER)
  1905. rx_start(info);
  1906. }
  1907. if (status & IRQ_DSR)
  1908. dsr_change(info, status);
  1909. if (status & IRQ_CTS)
  1910. cts_change(info, status);
  1911. if (status & IRQ_DCD)
  1912. dcd_change(info, status);
  1913. if (status & IRQ_RI)
  1914. ri_change(info, status);
  1915. }
  1916. static void isr_rdma(struct slgt_info *info)
  1917. {
  1918. unsigned int status = rd_reg32(info, RDCSR);
  1919. DBGISR(("%s isr_rdma status=%08x\n", info->device_name, status));
  1920. /* RDCSR (rx DMA control/status)
  1921. *
  1922. * 31..07 reserved
  1923. * 06 save status byte to DMA buffer
  1924. * 05 error
  1925. * 04 eol (end of list)
  1926. * 03 eob (end of buffer)
  1927. * 02 IRQ enable
  1928. * 01 reset
  1929. * 00 enable
  1930. */
  1931. wr_reg32(info, RDCSR, status); /* clear pending */
  1932. if (status & (BIT5 + BIT4)) {
  1933. DBGISR(("%s isr_rdma rx_restart=1\n", info->device_name));
  1934. info->rx_restart = true;
  1935. }
  1936. info->pending_bh |= BH_RECEIVE;
  1937. }
  1938. static void isr_tdma(struct slgt_info *info)
  1939. {
  1940. unsigned int status = rd_reg32(info, TDCSR);
  1941. DBGISR(("%s isr_tdma status=%08x\n", info->device_name, status));
  1942. /* TDCSR (tx DMA control/status)
  1943. *
  1944. * 31..06 reserved
  1945. * 05 error
  1946. * 04 eol (end of list)
  1947. * 03 eob (end of buffer)
  1948. * 02 IRQ enable
  1949. * 01 reset
  1950. * 00 enable
  1951. */
  1952. wr_reg32(info, TDCSR, status); /* clear pending */
  1953. if (status & (BIT5 + BIT4 + BIT3)) {
  1954. // another transmit buffer has completed
  1955. // run bottom half to get more send data from user
  1956. info->pending_bh |= BH_TRANSMIT;
  1957. }
  1958. }
  1959. static void isr_txeom(struct slgt_info *info, unsigned short status)
  1960. {
  1961. DBGISR(("%s txeom status=%04x\n", info->device_name, status));
  1962. slgt_irq_off(info, IRQ_TXDATA + IRQ_TXIDLE + IRQ_TXUNDER);
  1963. tdma_reset(info);
  1964. reset_tbufs(info);
  1965. if (status & IRQ_TXUNDER) {
  1966. unsigned short val = rd_reg16(info, TCR);
  1967. wr_reg16(info, TCR, (unsigned short)(val | BIT2)); /* set reset bit */
  1968. wr_reg16(info, TCR, val); /* clear reset bit */
  1969. }
  1970. if (info->tx_active) {
  1971. if (info->params.mode != MGSL_MODE_ASYNC) {
  1972. if (status & IRQ_TXUNDER)
  1973. info->icount.txunder++;
  1974. else if (status & IRQ_TXIDLE)
  1975. info->icount.txok++;
  1976. }
  1977. info->tx_active = false;
  1978. info->tx_count = 0;
  1979. del_timer(&info->tx_timer);
  1980. if (info->params.mode != MGSL_MODE_ASYNC && info->drop_rts_on_tx_done) {
  1981. info->signals &= ~SerialSignal_RTS;
  1982. info->drop_rts_on_tx_done = false;
  1983. set_signals(info);
  1984. }
  1985. #if SYNCLINK_GENERIC_HDLC
  1986. if (info->netcount)
  1987. hdlcdev_tx_done(info);
  1988. else
  1989. #endif
  1990. {
  1991. if (info->port.tty && (info->port.tty->stopped || info->port.tty->hw_stopped)) {
  1992. tx_stop(info);
  1993. return;
  1994. }
  1995. info->pending_bh |= BH_TRANSMIT;
  1996. }
  1997. }
  1998. }
  1999. static void isr_gpio(struct slgt_info *info, unsigned int changed, unsigned int state)
  2000. {
  2001. struct cond_wait *w, *prev;
  2002. /* wake processes waiting for specific transitions */
  2003. for (w = info->gpio_wait_q, prev = NULL ; w != NULL ; w = w->next) {
  2004. if (w->data & changed) {
  2005. w->data = state;
  2006. wake_up_interruptible(&w->q);
  2007. if (prev != NULL)
  2008. prev->next = w->next;
  2009. else
  2010. info->gpio_wait_q = w->next;
  2011. } else
  2012. prev = w;
  2013. }
  2014. }
  2015. /* interrupt service routine
  2016. *
  2017. * irq interrupt number
  2018. * dev_id device ID supplied during interrupt registration
  2019. */
  2020. static irqreturn_t slgt_interrupt(int dummy, void *dev_id)
  2021. {
  2022. struct slgt_info *info = dev_id;
  2023. unsigned int gsr;
  2024. unsigned int i;
  2025. DBGISR(("slgt_interrupt irq=%d entry\n", info->irq_level));
  2026. spin_lock(&info->lock);
  2027. while((gsr = rd_reg32(info, GSR) & 0xffffff00)) {
  2028. DBGISR(("%s gsr=%08x\n", info->device_name, gsr));
  2029. info->irq_occurred = true;
  2030. for(i=0; i < info->port_count ; i++) {
  2031. if (info->port_array[i] == NULL)
  2032. continue;
  2033. if (gsr & (BIT8 << i))
  2034. isr_serial(info->port_array[i]);
  2035. if (gsr & (BIT16 << (i*2)))
  2036. isr_rdma(info->port_array[i]);
  2037. if (gsr & (BIT17 << (i*2)))
  2038. isr_tdma(info->port_array[i]);
  2039. }
  2040. }
  2041. if (info->gpio_present) {
  2042. unsigned int state;
  2043. unsigned int changed;
  2044. while ((changed = rd_reg32(info, IOSR)) != 0) {
  2045. DBGISR(("%s iosr=%08x\n", info->device_name, changed));
  2046. /* read latched state of GPIO signals */
  2047. state = rd_reg32(info, IOVR);
  2048. /* clear pending GPIO interrupt bits */
  2049. wr_reg32(info, IOSR, changed);
  2050. for (i=0 ; i < info->port_count ; i++) {
  2051. if (info->port_array[i] != NULL)
  2052. isr_gpio(info->port_array[i], changed, state);
  2053. }
  2054. }
  2055. }
  2056. for(i=0; i < info->port_count ; i++) {
  2057. struct slgt_info *port = info->port_array[i];
  2058. if (port && (port->port.count || port->netcount) &&
  2059. port->pending_bh && !port->bh_running &&
  2060. !port->bh_requested) {
  2061. DBGISR(("%s bh queued\n", port->device_name));
  2062. schedule_work(&port->task);
  2063. port->bh_requested = true;
  2064. }
  2065. }
  2066. spin_unlock(&info->lock);
  2067. DBGISR(("slgt_interrupt irq=%d exit\n", info->irq_level));
  2068. return IRQ_HANDLED;
  2069. }
  2070. static int startup(struct slgt_info *info)
  2071. {
  2072. DBGINFO(("%s startup\n", info->device_name));
  2073. if (info->port.flags & ASYNC_INITIALIZED)
  2074. return 0;
  2075. if (!info->tx_buf) {
  2076. info->tx_buf = kmalloc(info->max_frame_size, GFP_KERNEL);
  2077. if (!info->tx_buf) {
  2078. DBGERR(("%s can't allocate tx buffer\n", info->device_name));
  2079. return -ENOMEM;
  2080. }
  2081. }
  2082. info->pending_bh = 0;
  2083. memset(&info->icount, 0, sizeof(info->icount));
  2084. /* program hardware for current parameters */
  2085. change_params(info);
  2086. if (info->port.tty)
  2087. clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
  2088. info->port.flags |= ASYNC_INITIALIZED;
  2089. return 0;
  2090. }
  2091. /*
  2092. * called by close() and hangup() to shutdown hardware
  2093. */
  2094. static void shutdown(struct slgt_info *info)
  2095. {
  2096. unsigned long flags;
  2097. if (!(info->port.flags & ASYNC_INITIALIZED))
  2098. return;
  2099. DBGINFO(("%s shutdown\n", info->device_name));
  2100. /* clear status wait queue because status changes */
  2101. /* can't happen after shutting down the hardware */
  2102. wake_up_interruptible(&info->status_event_wait_q);
  2103. wake_up_interruptible(&info->event_wait_q);
  2104. del_timer_sync(&info->tx_timer);
  2105. del_timer_sync(&info->rx_timer);
  2106. kfree(info->tx_buf);
  2107. info->tx_buf = NULL;
  2108. spin_lock_irqsave(&info->lock,flags);
  2109. tx_stop(info);
  2110. rx_stop(info);
  2111. slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
  2112. if (!info->port.tty || info->port.tty->termios->c_cflag & HUPCL) {
  2113. info->signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
  2114. set_signals(info);
  2115. }
  2116. flush_cond_wait(&info->gpio_wait_q);
  2117. spin_unlock_irqrestore(&info->lock,flags);
  2118. if (info->port.tty)
  2119. set_bit(TTY_IO_ERROR, &info->port.tty->flags);
  2120. info->port.flags &= ~ASYNC_INITIALIZED;
  2121. }
  2122. static void program_hw(struct slgt_info *info)
  2123. {
  2124. unsigned long flags;
  2125. spin_lock_irqsave(&info->lock,flags);
  2126. rx_stop(info);
  2127. tx_stop(info);
  2128. if (info->params.mode != MGSL_MODE_ASYNC ||
  2129. info->netcount)
  2130. sync_mode(info);
  2131. else
  2132. async_mode(info);
  2133. set_signals(info);
  2134. info->dcd_chkcount = 0;
  2135. info->cts_chkcount = 0;
  2136. info->ri_chkcount = 0;
  2137. info->dsr_chkcount = 0;
  2138. slgt_irq_on(info, IRQ_DCD | IRQ_CTS | IRQ_DSR | IRQ_RI);
  2139. get_signals(info);
  2140. if (info->netcount ||
  2141. (info->port.tty && info->port.tty->termios->c_cflag & CREAD))
  2142. rx_start(info);
  2143. spin_unlock_irqrestore(&info->lock,flags);
  2144. }
  2145. /*
  2146. * reconfigure adapter based on new parameters
  2147. */
  2148. static void change_params(struct slgt_info *info)
  2149. {
  2150. unsigned cflag;
  2151. int bits_per_char;
  2152. if (!info->port.tty || !info->port.tty->termios)
  2153. return;
  2154. DBGINFO(("%s change_params\n", info->device_name));
  2155. cflag = info->port.tty->termios->c_cflag;
  2156. /* if B0 rate (hangup) specified then negate DTR and RTS */
  2157. /* otherwise assert DTR and RTS */
  2158. if (cflag & CBAUD)
  2159. info->signals |= SerialSignal_RTS + SerialSignal_DTR;
  2160. else
  2161. info->signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
  2162. /* byte size and parity */
  2163. switch (cflag & CSIZE) {
  2164. case CS5: info->params.data_bits = 5; break;
  2165. case CS6: info->params.data_bits = 6; break;
  2166. case CS7: info->params.data_bits = 7; break;
  2167. case CS8: info->params.data_bits = 8; break;
  2168. default: info->params.data_bits = 7; break;
  2169. }
  2170. info->params.stop_bits = (cflag & CSTOPB) ? 2 : 1;
  2171. if (cflag & PARENB)
  2172. info->params.parity = (cflag & PARODD) ? ASYNC_PARITY_ODD : ASYNC_PARITY_EVEN;
  2173. else
  2174. info->params.parity = ASYNC_PARITY_NONE;
  2175. /* calculate number of jiffies to transmit a full
  2176. * FIFO (32 bytes) at specified data rate
  2177. */
  2178. bits_per_char = info->params.data_bits +
  2179. info->params.stop_bits + 1;
  2180. info->params.data_rate = tty_get_baud_rate(info->port.tty);
  2181. if (info->params.data_rate) {
  2182. info->timeout = (32*HZ*bits_per_char) /
  2183. info->params.data_rate;
  2184. }
  2185. info->timeout += HZ/50; /* Add .02 seconds of slop */
  2186. if (cflag & CRTSCTS)
  2187. info->port.flags |= ASYNC_CTS_FLOW;
  2188. else
  2189. info->port.flags &= ~ASYNC_CTS_FLOW;
  2190. if (cflag & CLOCAL)
  2191. info->port.flags &= ~ASYNC_CHECK_CD;
  2192. else
  2193. info->port.flags |= ASYNC_CHECK_CD;
  2194. /* process tty input control flags */
  2195. info->read_status_mask = IRQ_RXOVER;
  2196. if (I_INPCK(info->port.tty))
  2197. info->read_status_mask |= MASK_PARITY | MASK_FRAMING;
  2198. if (I_BRKINT(info->port.tty) || I_PARMRK(info->port.tty))
  2199. info->read_status_mask |= MASK_BREAK;
  2200. if (I_IGNPAR(info->port.tty))
  2201. info->ignore_status_mask |= MASK_PARITY | MASK_FRAMING;
  2202. if (I_IGNBRK(info->port.tty)) {
  2203. info->ignore_status_mask |= MASK_BREAK;
  2204. /* If ignoring parity and break indicators, ignore
  2205. * overruns too. (For real raw support).
  2206. */
  2207. if (I_IGNPAR(info->port.tty))
  2208. info->ignore_status_mask |= MASK_OVERRUN;
  2209. }
  2210. program_hw(info);
  2211. }
  2212. static int get_stats(struct slgt_info *info, struct mgsl_icount __user *user_icount)
  2213. {
  2214. DBGINFO(("%s get_stats\n", info->device_name));
  2215. if (!user_icount) {
  2216. memset(&info->icount, 0, sizeof(info->icount));
  2217. } else {
  2218. if (copy_to_user(user_icount, &info->icount, sizeof(struct mgsl_icount)))
  2219. return -EFAULT;
  2220. }
  2221. return 0;
  2222. }
  2223. static int get_params(struct slgt_info *info, MGSL_PARAMS __user *user_params)
  2224. {
  2225. DBGINFO(("%s get_params\n", info->device_name));
  2226. if (copy_to_user(user_params, &info->params, sizeof(MGSL_PARAMS)))
  2227. return -EFAULT;
  2228. return 0;
  2229. }
  2230. static int set_params(struct slgt_info *info, MGSL_PARAMS __user *new_params)
  2231. {
  2232. unsigned long flags;
  2233. MGSL_PARAMS tmp_params;
  2234. DBGINFO(("%s set_params\n", info->device_name));
  2235. if (copy_from_user(&tmp_params, new_params, sizeof(MGSL_PARAMS)))
  2236. return -EFAULT;
  2237. spin_lock_irqsave(&info->lock, flags);
  2238. if (tmp_params.mode == MGSL_MODE_BASE_CLOCK)
  2239. info->base_clock = tmp_params.clock_speed;
  2240. else
  2241. memcpy(&info->params, &tmp_params, sizeof(MGSL_PARAMS));
  2242. spin_unlock_irqrestore(&info->lock, flags);
  2243. program_hw(info);
  2244. return 0;
  2245. }
  2246. static int get_txidle(struct slgt_info *info, int __user *idle_mode)
  2247. {
  2248. DBGINFO(("%s get_txidle=%d\n", info->device_name, info->idle_mode));
  2249. if (put_user(info->idle_mode, idle_mode))
  2250. return -EFAULT;
  2251. return 0;
  2252. }
  2253. static int set_txidle(struct slgt_info *info, int idle_mode)
  2254. {
  2255. unsigned long flags;
  2256. DBGINFO(("%s set_txidle(%d)\n", info->device_name, idle_mode));
  2257. spin_lock_irqsave(&info->lock,flags);
  2258. info->idle_mode = idle_mode;
  2259. if (info->params.mode != MGSL_MODE_ASYNC)
  2260. tx_set_idle(info);
  2261. spin_unlock_irqrestore(&info->lock,flags);
  2262. return 0;
  2263. }
  2264. static int tx_enable(struct slgt_info *info, int enable)
  2265. {
  2266. unsigned long flags;
  2267. DBGINFO(("%s tx_enable(%d)\n", info->device_name, enable));
  2268. spin_lock_irqsave(&info->lock,flags);
  2269. if (enable) {
  2270. if (!info->tx_enabled)
  2271. tx_start(info);
  2272. } else {
  2273. if (info->tx_enabled)
  2274. tx_stop(info);
  2275. }
  2276. spin_unlock_irqrestore(&info->lock,flags);
  2277. return 0;
  2278. }
  2279. /*
  2280. * abort transmit HDLC frame
  2281. */
  2282. static int tx_abort(struct slgt_info *info)
  2283. {
  2284. unsigned long flags;
  2285. DBGINFO(("%s tx_abort\n", info->device_name));
  2286. spin_lock_irqsave(&info->lock,flags);
  2287. tdma_reset(info);
  2288. spin_unlock_irqrestore(&info->lock,flags);
  2289. return 0;
  2290. }
  2291. static int rx_enable(struct slgt_info *info, int enable)
  2292. {
  2293. unsigned long flags;
  2294. unsigned int rbuf_fill_level;
  2295. DBGINFO(("%s rx_enable(%08x)\n", info->device_name, enable));
  2296. spin_lock_irqsave(&info->lock,flags);
  2297. /*
  2298. * enable[31..16] = receive DMA buffer fill level
  2299. * 0 = noop (leave fill level unchanged)
  2300. * fill level must be multiple of 4 and <= buffer size
  2301. */
  2302. rbuf_fill_level = ((unsigned int)enable) >> 16;
  2303. if (rbuf_fill_level) {
  2304. if ((rbuf_fill_level > DMABUFSIZE) || (rbuf_fill_level % 4)) {
  2305. spin_unlock_irqrestore(&info->lock, flags);
  2306. return -EINVAL;
  2307. }
  2308. info->rbuf_fill_level = rbuf_fill_level;
  2309. if (rbuf_fill_level < 128)
  2310. info->rx_pio = 1; /* PIO mode */
  2311. else
  2312. info->rx_pio = 0; /* DMA mode */
  2313. rx_stop(info); /* restart receiver to use new fill level */
  2314. }
  2315. /*
  2316. * enable[1..0] = receiver enable command
  2317. * 0 = disable
  2318. * 1 = enable
  2319. * 2 = enable or force hunt mode if already enabled
  2320. */
  2321. enable &= 3;
  2322. if (enable) {
  2323. if (!info->rx_enabled)
  2324. rx_start(info);
  2325. else if (enable == 2) {
  2326. /* force hunt mode (write 1 to RCR[3]) */
  2327. wr_reg16(info, RCR, rd_reg16(info, RCR) | BIT3);
  2328. }
  2329. } else {
  2330. if (info->rx_enabled)
  2331. rx_stop(info);
  2332. }
  2333. spin_unlock_irqrestore(&info->lock,flags);
  2334. return 0;
  2335. }
  2336. /*
  2337. * wait for specified event to occur
  2338. */
  2339. static int wait_mgsl_event(struct slgt_info *info, int __user *mask_ptr)
  2340. {
  2341. unsigned long flags;
  2342. int s;
  2343. int rc=0;
  2344. struct mgsl_icount cprev, cnow;
  2345. int events;
  2346. int mask;
  2347. struct _input_signal_events oldsigs, newsigs;
  2348. DECLARE_WAITQUEUE(wait, current);
  2349. if (get_user(mask, mask_ptr))
  2350. return -EFAULT;
  2351. DBGINFO(("%s wait_mgsl_event(%d)\n", info->device_name, mask));
  2352. spin_lock_irqsave(&info->lock,flags);
  2353. /* return immediately if state matches requested events */
  2354. get_signals(info);
  2355. s = info->signals;
  2356. events = mask &
  2357. ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
  2358. ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
  2359. ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
  2360. ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
  2361. if (events) {
  2362. spin_unlock_irqrestore(&info->lock,flags);
  2363. goto exit;
  2364. }
  2365. /* save current irq counts */
  2366. cprev = info->icount;
  2367. oldsigs = info->input_signal_events;
  2368. /* enable hunt and idle irqs if needed */
  2369. if (mask & (MgslEvent_ExitHuntMode+MgslEvent_IdleReceived)) {
  2370. unsigned short val = rd_reg16(info, SCR);
  2371. if (!(val & IRQ_RXIDLE))
  2372. wr_reg16(info, SCR, (unsigned short)(val | IRQ_RXIDLE));
  2373. }
  2374. set_current_state(TASK_INTERRUPTIBLE);
  2375. add_wait_queue(&info->event_wait_q, &wait);
  2376. spin_unlock_irqrestore(&info->lock,flags);
  2377. for(;;) {
  2378. schedule();
  2379. if (signal_pending(current)) {
  2380. rc = -ERESTARTSYS;
  2381. break;
  2382. }
  2383. /* get current irq counts */
  2384. spin_lock_irqsave(&info->lock,flags);
  2385. cnow = info->icount;
  2386. newsigs = info->input_signal_events;
  2387. set_current_state(TASK_INTERRUPTIBLE);
  2388. spin_unlock_irqrestore(&info->lock,flags);
  2389. /* if no change, wait aborted for some reason */
  2390. if (newsigs.dsr_up == oldsigs.dsr_up &&
  2391. newsigs.dsr_down == oldsigs.dsr_down &&
  2392. newsigs.dcd_up == oldsigs.dcd_up &&
  2393. newsigs.dcd_down == oldsigs.dcd_down &&
  2394. newsigs.cts_up == oldsigs.cts_up &&
  2395. newsigs.cts_down == oldsigs.cts_down &&
  2396. newsigs.ri_up == oldsigs.ri_up &&
  2397. newsigs.ri_down == oldsigs.ri_down &&
  2398. cnow.exithunt == cprev.exithunt &&
  2399. cnow.rxidle == cprev.rxidle) {
  2400. rc = -EIO;
  2401. break;
  2402. }
  2403. events = mask &
  2404. ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
  2405. (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
  2406. (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
  2407. (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
  2408. (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
  2409. (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
  2410. (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
  2411. (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
  2412. (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
  2413. (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
  2414. if (events)
  2415. break;
  2416. cprev = cnow;
  2417. oldsigs = newsigs;
  2418. }
  2419. remove_wait_queue(&info->event_wait_q, &wait);
  2420. set_current_state(TASK_RUNNING);
  2421. if (mask & (MgslEvent_ExitHuntMode + MgslEvent_IdleReceived)) {
  2422. spin_lock_irqsave(&info->lock,flags);
  2423. if (!waitqueue_active(&info->event_wait_q)) {
  2424. /* disable enable exit hunt mode/idle rcvd IRQs */
  2425. wr_reg16(info, SCR,
  2426. (unsigned short)(rd_reg16(info, SCR) & ~IRQ_RXIDLE));
  2427. }
  2428. spin_unlock_irqrestore(&info->lock,flags);
  2429. }
  2430. exit:
  2431. if (rc == 0)
  2432. rc = put_user(events, mask_ptr);
  2433. return rc;
  2434. }
  2435. static int get_interface(struct slgt_info *info, int __user *if_mode)
  2436. {
  2437. DBGINFO(("%s get_interface=%x\n", info->device_name, info->if_mode));
  2438. if (put_user(info->if_mode, if_mode))
  2439. return -EFAULT;
  2440. return 0;
  2441. }
  2442. static int set_interface(struct slgt_info *info, int if_mode)
  2443. {
  2444. unsigned long flags;
  2445. unsigned short val;
  2446. DBGINFO(("%s set_interface=%x)\n", info->device_name, if_mode));
  2447. spin_lock_irqsave(&info->lock,flags);
  2448. info->if_mode = if_mode;
  2449. msc_set_vcr(info);
  2450. /* TCR (tx control) 07 1=RTS driver control */
  2451. val = rd_reg16(info, TCR);
  2452. if (info->if_mode & MGSL_INTERFACE_RTS_EN)
  2453. val |= BIT7;
  2454. else
  2455. val &= ~BIT7;
  2456. wr_reg16(info, TCR, val);
  2457. spin_unlock_irqrestore(&info->lock,flags);
  2458. return 0;
  2459. }
  2460. /*
  2461. * set general purpose IO pin state and direction
  2462. *
  2463. * user_gpio fields:
  2464. * state each bit indicates a pin state
  2465. * smask set bit indicates pin state to set
  2466. * dir each bit indicates a pin direction (0=input, 1=output)
  2467. * dmask set bit indicates pin direction to set
  2468. */
  2469. static int set_gpio(struct slgt_info *info, struct gpio_desc __user *user_gpio)
  2470. {
  2471. unsigned long flags;
  2472. struct gpio_desc gpio;
  2473. __u32 data;
  2474. if (!info->gpio_present)
  2475. return -EINVAL;
  2476. if (copy_from_user(&gpio, user_gpio, sizeof(gpio)))
  2477. return -EFAULT;
  2478. DBGINFO(("%s set_gpio state=%08x smask=%08x dir=%08x dmask=%08x\n",
  2479. info->device_name, gpio.state, gpio.smask,
  2480. gpio.dir, gpio.dmask));
  2481. spin_lock_irqsave(&info->lock,flags);
  2482. if (gpio.dmask) {
  2483. data = rd_reg32(info, IODR);
  2484. data |= gpio.dmask & gpio.dir;
  2485. data &= ~(gpio.dmask & ~gpio.dir);
  2486. wr_reg32(info, IODR, data);
  2487. }
  2488. if (gpio.smask) {
  2489. data = rd_reg32(info, IOVR);
  2490. data |= gpio.smask & gpio.state;
  2491. data &= ~(gpio.smask & ~gpio.state);
  2492. wr_reg32(info, IOVR, data);
  2493. }
  2494. spin_unlock_irqrestore(&info->lock,flags);
  2495. return 0;
  2496. }
  2497. /*
  2498. * get general purpose IO pin state and direction
  2499. */
  2500. static int get_gpio(struct slgt_info *info, struct gpio_desc __user *user_gpio)
  2501. {
  2502. struct gpio_desc gpio;
  2503. if (!info->gpio_present)
  2504. return -EINVAL;
  2505. gpio.state = rd_reg32(info, IOVR);
  2506. gpio.smask = 0xffffffff;
  2507. gpio.dir = rd_reg32(info, IODR);
  2508. gpio.dmask = 0xffffffff;
  2509. if (copy_to_user(user_gpio, &gpio, sizeof(gpio)))
  2510. return -EFAULT;
  2511. DBGINFO(("%s get_gpio state=%08x dir=%08x\n",
  2512. info->device_name, gpio.state, gpio.dir));
  2513. return 0;
  2514. }
  2515. /*
  2516. * conditional wait facility
  2517. */
  2518. static void init_cond_wait(struct cond_wait *w, unsigned int data)
  2519. {
  2520. init_waitqueue_head(&w->q);
  2521. init_waitqueue_entry(&w->wait, current);
  2522. w->data = data;
  2523. }
  2524. static void add_cond_wait(struct cond_wait **head, struct cond_wait *w)
  2525. {
  2526. set_current_state(TASK_INTERRUPTIBLE);
  2527. add_wait_queue(&w->q, &w->wait);
  2528. w->next = *head;
  2529. *head = w;
  2530. }
  2531. static void remove_cond_wait(struct cond_wait **head, struct cond_wait *cw)
  2532. {
  2533. struct cond_wait *w, *prev;
  2534. remove_wait_queue(&cw->q, &cw->wait);
  2535. set_current_state(TASK_RUNNING);
  2536. for (w = *head, prev = NULL ; w != NULL ; prev = w, w = w->next) {
  2537. if (w == cw) {
  2538. if (prev != NULL)
  2539. prev->next = w->next;
  2540. else
  2541. *head = w->next;
  2542. break;
  2543. }
  2544. }
  2545. }
  2546. static void flush_cond_wait(struct cond_wait **head)
  2547. {
  2548. while (*head != NULL) {
  2549. wake_up_interruptible(&(*head)->q);
  2550. *head = (*head)->next;
  2551. }
  2552. }
  2553. /*
  2554. * wait for general purpose I/O pin(s) to enter specified state
  2555. *
  2556. * user_gpio fields:
  2557. * state - bit indicates target pin state
  2558. * smask - set bit indicates watched pin
  2559. *
  2560. * The wait ends when at least one watched pin enters the specified
  2561. * state. When 0 (no error) is returned, user_gpio->state is set to the
  2562. * state of all GPIO pins when the wait ends.
  2563. *
  2564. * Note: Each pin may be a dedicated input, dedicated output, or
  2565. * configurable input/output. The number and configuration of pins
  2566. * varies with the specific adapter model. Only input pins (dedicated
  2567. * or configured) can be monitored with this function.
  2568. */
  2569. static int wait_gpio(struct slgt_info *info, struct gpio_desc __user *user_gpio)
  2570. {
  2571. unsigned long flags;
  2572. int rc = 0;
  2573. struct gpio_desc gpio;
  2574. struct cond_wait wait;
  2575. u32 state;
  2576. if (!info->gpio_present)
  2577. return -EINVAL;
  2578. if (copy_from_user(&gpio, user_gpio, sizeof(gpio)))
  2579. return -EFAULT;
  2580. DBGINFO(("%s wait_gpio() state=%08x smask=%08x\n",
  2581. info->device_name, gpio.state, gpio.smask));
  2582. /* ignore output pins identified by set IODR bit */
  2583. if ((gpio.smask &= ~rd_reg32(info, IODR)) == 0)
  2584. return -EINVAL;
  2585. init_cond_wait(&wait, gpio.smask);
  2586. spin_lock_irqsave(&info->lock, flags);
  2587. /* enable interrupts for watched pins */
  2588. wr_reg32(info, IOER, rd_reg32(info, IOER) | gpio.smask);
  2589. /* get current pin states */
  2590. state = rd_reg32(info, IOVR);
  2591. if (gpio.smask & ~(state ^ gpio.state)) {
  2592. /* already in target state */
  2593. gpio.state = state;
  2594. } else {
  2595. /* wait for target state */
  2596. add_cond_wait(&info->gpio_wait_q, &wait);
  2597. spin_unlock_irqrestore(&info->lock, flags);
  2598. schedule();
  2599. if (signal_pending(current))
  2600. rc = -ERESTARTSYS;
  2601. else
  2602. gpio.state = wait.data;
  2603. spin_lock_irqsave(&info->lock, flags);
  2604. remove_cond_wait(&info->gpio_wait_q, &wait);
  2605. }
  2606. /* disable all GPIO interrupts if no waiting processes */
  2607. if (info->gpio_wait_q == NULL)
  2608. wr_reg32(info, IOER, 0);
  2609. spin_unlock_irqrestore(&info->lock,flags);
  2610. if ((rc == 0) && copy_to_user(user_gpio, &gpio, sizeof(gpio)))
  2611. rc = -EFAULT;
  2612. return rc;
  2613. }
  2614. static int modem_input_wait(struct slgt_info *info,int arg)
  2615. {
  2616. unsigned long flags;
  2617. int rc;
  2618. struct mgsl_icount cprev, cnow;
  2619. DECLARE_WAITQUEUE(wait, current);
  2620. /* save current irq counts */
  2621. spin_lock_irqsave(&info->lock,flags);
  2622. cprev = info->icount;
  2623. add_wait_queue(&info->status_event_wait_q, &wait);
  2624. set_current_state(TASK_INTERRUPTIBLE);
  2625. spin_unlock_irqrestore(&info->lock,flags);
  2626. for(;;) {
  2627. schedule();
  2628. if (signal_pending(current)) {
  2629. rc = -ERESTARTSYS;
  2630. break;
  2631. }
  2632. /* get new irq counts */
  2633. spin_lock_irqsave(&info->lock,flags);
  2634. cnow = info->icount;
  2635. set_current_state(TASK_INTERRUPTIBLE);
  2636. spin_unlock_irqrestore(&info->lock,flags);
  2637. /* if no change, wait aborted for some reason */
  2638. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  2639. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
  2640. rc = -EIO;
  2641. break;
  2642. }
  2643. /* check for change in caller specified modem input */
  2644. if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
  2645. (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
  2646. (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
  2647. (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
  2648. rc = 0;
  2649. break;
  2650. }
  2651. cprev = cnow;
  2652. }
  2653. remove_wait_queue(&info->status_event_wait_q, &wait);
  2654. set_current_state(TASK_RUNNING);
  2655. return rc;
  2656. }
  2657. /*
  2658. * return state of serial control and status signals
  2659. */
  2660. static int tiocmget(struct tty_struct *tty, struct file *file)
  2661. {
  2662. struct slgt_info *info = tty->driver_data;
  2663. unsigned int result;
  2664. unsigned long flags;
  2665. spin_lock_irqsave(&info->lock,flags);
  2666. get_signals(info);
  2667. spin_unlock_irqrestore(&info->lock,flags);
  2668. result = ((info->signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
  2669. ((info->signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
  2670. ((info->signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
  2671. ((info->signals & SerialSignal_RI) ? TIOCM_RNG:0) +
  2672. ((info->signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
  2673. ((info->signals & SerialSignal_CTS) ? TIOCM_CTS:0);
  2674. DBGINFO(("%s tiocmget value=%08X\n", info->device_name, result));
  2675. return result;
  2676. }
  2677. /*
  2678. * set modem control signals (DTR/RTS)
  2679. *
  2680. * cmd signal command: TIOCMBIS = set bit TIOCMBIC = clear bit
  2681. * TIOCMSET = set/clear signal values
  2682. * value bit mask for command
  2683. */
  2684. static int tiocmset(struct tty_struct *tty, struct file *file,
  2685. unsigned int set, unsigned int clear)
  2686. {
  2687. struct slgt_info *info = tty->driver_data;
  2688. unsigned long flags;
  2689. DBGINFO(("%s tiocmset(%x,%x)\n", info->device_name, set, clear));
  2690. if (set & TIOCM_RTS)
  2691. info->signals |= SerialSignal_RTS;
  2692. if (set & TIOCM_DTR)
  2693. info->signals |= SerialSignal_DTR;
  2694. if (clear & TIOCM_RTS)
  2695. info->signals &= ~SerialSignal_RTS;
  2696. if (clear & TIOCM_DTR)
  2697. info->signals &= ~SerialSignal_DTR;
  2698. spin_lock_irqsave(&info->lock,flags);
  2699. set_signals(info);
  2700. spin_unlock_irqrestore(&info->lock,flags);
  2701. return 0;
  2702. }
  2703. static int carrier_raised(struct tty_port *port)
  2704. {
  2705. unsigned long flags;
  2706. struct slgt_info *info = container_of(port, struct slgt_info, port);
  2707. spin_lock_irqsave(&info->lock,flags);
  2708. get_signals(info);
  2709. spin_unlock_irqrestore(&info->lock,flags);
  2710. return (info->signals & SerialSignal_DCD) ? 1 : 0;
  2711. }
  2712. static void dtr_rts(struct tty_port *port, int on)
  2713. {
  2714. unsigned long flags;
  2715. struct slgt_info *info = container_of(port, struct slgt_info, port);
  2716. spin_lock_irqsave(&info->lock,flags);
  2717. if (on)
  2718. info->signals |= SerialSignal_RTS + SerialSignal_DTR;
  2719. else
  2720. info->signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
  2721. set_signals(info);
  2722. spin_unlock_irqrestore(&info->lock,flags);
  2723. }
  2724. /*
  2725. * block current process until the device is ready to open
  2726. */
  2727. static int block_til_ready(struct tty_struct *tty, struct file *filp,
  2728. struct slgt_info *info)
  2729. {
  2730. DECLARE_WAITQUEUE(wait, current);
  2731. int retval;
  2732. bool do_clocal = false;
  2733. bool extra_count = false;
  2734. unsigned long flags;
  2735. int cd;
  2736. struct tty_port *port = &info->port;
  2737. DBGINFO(("%s block_til_ready\n", tty->driver->name));
  2738. if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
  2739. /* nonblock mode is set or port is not enabled */
  2740. port->flags |= ASYNC_NORMAL_ACTIVE;
  2741. return 0;
  2742. }
  2743. if (tty->termios->c_cflag & CLOCAL)
  2744. do_clocal = true;
  2745. /* Wait for carrier detect and the line to become
  2746. * free (i.e., not in use by the callout). While we are in
  2747. * this loop, port->count is dropped by one, so that
  2748. * close() knows when to free things. We restore it upon
  2749. * exit, either normal or abnormal.
  2750. */
  2751. retval = 0;
  2752. add_wait_queue(&port->open_wait, &wait);
  2753. spin_lock_irqsave(&info->lock, flags);
  2754. if (!tty_hung_up_p(filp)) {
  2755. extra_count = true;
  2756. port->count--;
  2757. }
  2758. spin_unlock_irqrestore(&info->lock, flags);
  2759. port->blocked_open++;
  2760. while (1) {
  2761. if ((tty->termios->c_cflag & CBAUD))
  2762. tty_port_raise_dtr_rts(port);
  2763. set_current_state(TASK_INTERRUPTIBLE);
  2764. if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)){
  2765. retval = (port->flags & ASYNC_HUP_NOTIFY) ?
  2766. -EAGAIN : -ERESTARTSYS;
  2767. break;
  2768. }
  2769. cd = tty_port_carrier_raised(port);
  2770. if (!(port->flags & ASYNC_CLOSING) && (do_clocal || cd ))
  2771. break;
  2772. if (signal_pending(current)) {
  2773. retval = -ERESTARTSYS;
  2774. break;
  2775. }
  2776. DBGINFO(("%s block_til_ready wait\n", tty->driver->name));
  2777. schedule();
  2778. }
  2779. set_current_state(TASK_RUNNING);
  2780. remove_wait_queue(&port->open_wait, &wait);
  2781. if (extra_count)
  2782. port->count++;
  2783. port->blocked_open--;
  2784. if (!retval)
  2785. port->flags |= ASYNC_NORMAL_ACTIVE;
  2786. DBGINFO(("%s block_til_ready ready, rc=%d\n", tty->driver->name, retval));
  2787. return retval;
  2788. }
  2789. static int alloc_tmp_rbuf(struct slgt_info *info)
  2790. {
  2791. info->tmp_rbuf = kmalloc(info->max_frame_size + 5, GFP_KERNEL);
  2792. if (info->tmp_rbuf == NULL)
  2793. return -ENOMEM;
  2794. return 0;
  2795. }
  2796. static void free_tmp_rbuf(struct slgt_info *info)
  2797. {
  2798. kfree(info->tmp_rbuf);
  2799. info->tmp_rbuf = NULL;
  2800. }
  2801. /*
  2802. * allocate DMA descriptor lists.
  2803. */
  2804. static int alloc_desc(struct slgt_info *info)
  2805. {
  2806. unsigned int i;
  2807. unsigned int pbufs;
  2808. /* allocate memory to hold descriptor lists */
  2809. info->bufs = pci_alloc_consistent(info->pdev, DESC_LIST_SIZE, &info->bufs_dma_addr);
  2810. if (info->bufs == NULL)
  2811. return -ENOMEM;
  2812. memset(info->bufs, 0, DESC_LIST_SIZE);
  2813. info->rbufs = (struct slgt_desc*)info->bufs;
  2814. info->tbufs = ((struct slgt_desc*)info->bufs) + info->rbuf_count;
  2815. pbufs = (unsigned int)info->bufs_dma_addr;
  2816. /*
  2817. * Build circular lists of descriptors
  2818. */
  2819. for (i=0; i < info->rbuf_count; i++) {
  2820. /* physical address of this descriptor */
  2821. info->rbufs[i].pdesc = pbufs + (i * sizeof(struct slgt_desc));
  2822. /* physical address of next descriptor */
  2823. if (i == info->rbuf_count - 1)
  2824. info->rbufs[i].next = cpu_to_le32(pbufs);
  2825. else
  2826. info->rbufs[i].next = cpu_to_le32(pbufs + ((i+1) * sizeof(struct slgt_desc)));
  2827. set_desc_count(info->rbufs[i], DMABUFSIZE);
  2828. }
  2829. for (i=0; i < info->tbuf_count; i++) {
  2830. /* physical address of this descriptor */
  2831. info->tbufs[i].pdesc = pbufs + ((info->rbuf_count + i) * sizeof(struct slgt_desc));
  2832. /* physical address of next descriptor */
  2833. if (i == info->tbuf_count - 1)
  2834. info->tbufs[i].next = cpu_to_le32(pbufs + info->rbuf_count * sizeof(struct slgt_desc));
  2835. else
  2836. info->tbufs[i].next = cpu_to_le32(pbufs + ((info->rbuf_count + i + 1) * sizeof(struct slgt_desc)));
  2837. }
  2838. return 0;
  2839. }
  2840. static void free_desc(struct slgt_info *info)
  2841. {
  2842. if (info->bufs != NULL) {
  2843. pci_free_consistent(info->pdev, DESC_LIST_SIZE, info->bufs, info->bufs_dma_addr);
  2844. info->bufs = NULL;
  2845. info->rbufs = NULL;
  2846. info->tbufs = NULL;
  2847. }
  2848. }
  2849. static int alloc_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count)
  2850. {
  2851. int i;
  2852. for (i=0; i < count; i++) {
  2853. if ((bufs[i].buf = pci_alloc_consistent(info->pdev, DMABUFSIZE, &bufs[i].buf_dma_addr)) == NULL)
  2854. return -ENOMEM;
  2855. bufs[i].pbuf = cpu_to_le32((unsigned int)bufs[i].buf_dma_addr);
  2856. }
  2857. return 0;
  2858. }
  2859. static void free_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count)
  2860. {
  2861. int i;
  2862. for (i=0; i < count; i++) {
  2863. if (bufs[i].buf == NULL)
  2864. continue;
  2865. pci_free_consistent(info->pdev, DMABUFSIZE, bufs[i].buf, bufs[i].buf_dma_addr);
  2866. bufs[i].buf = NULL;
  2867. }
  2868. }
  2869. static int alloc_dma_bufs(struct slgt_info *info)
  2870. {
  2871. info->rbuf_count = 32;
  2872. info->tbuf_count = 32;
  2873. if (alloc_desc(info) < 0 ||
  2874. alloc_bufs(info, info->rbufs, info->rbuf_count) < 0 ||
  2875. alloc_bufs(info, info->tbufs, info->tbuf_count) < 0 ||
  2876. alloc_tmp_rbuf(info) < 0) {
  2877. DBGERR(("%s DMA buffer alloc fail\n", info->device_name));
  2878. return -ENOMEM;
  2879. }
  2880. reset_rbufs(info);
  2881. return 0;
  2882. }
  2883. static void free_dma_bufs(struct slgt_info *info)
  2884. {
  2885. if (info->bufs) {
  2886. free_bufs(info, info->rbufs, info->rbuf_count);
  2887. free_bufs(info, info->tbufs, info->tbuf_count);
  2888. free_desc(info);
  2889. }
  2890. free_tmp_rbuf(info);
  2891. }
  2892. static int claim_resources(struct slgt_info *info)
  2893. {
  2894. if (request_mem_region(info->phys_reg_addr, SLGT_REG_SIZE, "synclink_gt") == NULL) {
  2895. DBGERR(("%s reg addr conflict, addr=%08X\n",
  2896. info->device_name, info->phys_reg_addr));
  2897. info->init_error = DiagStatus_AddressConflict;
  2898. goto errout;
  2899. }
  2900. else
  2901. info->reg_addr_requested = true;
  2902. info->reg_addr = ioremap_nocache(info->phys_reg_addr, SLGT_REG_SIZE);
  2903. if (!info->reg_addr) {
  2904. DBGERR(("%s cant map device registers, addr=%08X\n",
  2905. info->device_name, info->phys_reg_addr));
  2906. info->init_error = DiagStatus_CantAssignPciResources;
  2907. goto errout;
  2908. }
  2909. return 0;
  2910. errout:
  2911. release_resources(info);
  2912. return -ENODEV;
  2913. }
  2914. static void release_resources(struct slgt_info *info)
  2915. {
  2916. if (info->irq_requested) {
  2917. free_irq(info->irq_level, info);
  2918. info->irq_requested = false;
  2919. }
  2920. if (info->reg_addr_requested) {
  2921. release_mem_region(info->phys_reg_addr, SLGT_REG_SIZE);
  2922. info->reg_addr_requested = false;
  2923. }
  2924. if (info->reg_addr) {
  2925. iounmap(info->reg_addr);
  2926. info->reg_addr = NULL;
  2927. }
  2928. }
  2929. /* Add the specified device instance data structure to the
  2930. * global linked list of devices and increment the device count.
  2931. */
  2932. static void add_device(struct slgt_info *info)
  2933. {
  2934. char *devstr;
  2935. info->next_device = NULL;
  2936. info->line = slgt_device_count;
  2937. sprintf(info->device_name, "%s%d", tty_dev_prefix, info->line);
  2938. if (info->line < MAX_DEVICES) {
  2939. if (maxframe[info->line])
  2940. info->max_frame_size = maxframe[info->line];
  2941. }
  2942. slgt_device_count++;
  2943. if (!slgt_device_list)
  2944. slgt_device_list = info;
  2945. else {
  2946. struct slgt_info *current_dev = slgt_device_list;
  2947. while(current_dev->next_device)
  2948. current_dev = current_dev->next_device;
  2949. current_dev->next_device = info;
  2950. }
  2951. if (info->max_frame_size < 4096)
  2952. info->max_frame_size = 4096;
  2953. else if (info->max_frame_size > 65535)
  2954. info->max_frame_size = 65535;
  2955. switch(info->pdev->device) {
  2956. case SYNCLINK_GT_DEVICE_ID:
  2957. devstr = "GT";
  2958. break;
  2959. case SYNCLINK_GT2_DEVICE_ID:
  2960. devstr = "GT2";
  2961. break;
  2962. case SYNCLINK_GT4_DEVICE_ID:
  2963. devstr = "GT4";
  2964. break;
  2965. case SYNCLINK_AC_DEVICE_ID:
  2966. devstr = "AC";
  2967. info->params.mode = MGSL_MODE_ASYNC;
  2968. break;
  2969. default:
  2970. devstr = "(unknown model)";
  2971. }
  2972. printk("SyncLink %s %s IO=%08x IRQ=%d MaxFrameSize=%u\n",
  2973. devstr, info->device_name, info->phys_reg_addr,
  2974. info->irq_level, info->max_frame_size);
  2975. #if SYNCLINK_GENERIC_HDLC
  2976. hdlcdev_init(info);
  2977. #endif
  2978. }
  2979. static const struct tty_port_operations slgt_port_ops = {
  2980. .carrier_raised = carrier_raised,
  2981. .dtr_rts = dtr_rts,
  2982. };
  2983. /*
  2984. * allocate device instance structure, return NULL on failure
  2985. */
  2986. static struct slgt_info *alloc_dev(int adapter_num, int port_num, struct pci_dev *pdev)
  2987. {
  2988. struct slgt_info *info;
  2989. info = kzalloc(sizeof(struct slgt_info), GFP_KERNEL);
  2990. if (!info) {
  2991. DBGERR(("%s device alloc failed adapter=%d port=%d\n",
  2992. driver_name, adapter_num, port_num));
  2993. } else {
  2994. tty_port_init(&info->port);
  2995. info->port.ops = &slgt_port_ops;
  2996. info->magic = MGSL_MAGIC;
  2997. INIT_WORK(&info->task, bh_handler);
  2998. info->max_frame_size = 4096;
  2999. info->base_clock = 14745600;
  3000. info->rbuf_fill_level = DMABUFSIZE;
  3001. info->port.close_delay = 5*HZ/10;
  3002. info->port.closing_wait = 30*HZ;
  3003. init_waitqueue_head(&info->status_event_wait_q);
  3004. init_waitqueue_head(&info->event_wait_q);
  3005. spin_lock_init(&info->netlock);
  3006. memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
  3007. info->idle_mode = HDLC_TXIDLE_FLAGS;
  3008. info->adapter_num = adapter_num;
  3009. info->port_num = port_num;
  3010. setup_timer(&info->tx_timer, tx_timeout, (unsigned long)info);
  3011. setup_timer(&info->rx_timer, rx_timeout, (unsigned long)info);
  3012. /* Copy configuration info to device instance data */
  3013. info->pdev = pdev;
  3014. info->irq_level = pdev->irq;
  3015. info->phys_reg_addr = pci_resource_start(pdev,0);
  3016. info->bus_type = MGSL_BUS_TYPE_PCI;
  3017. info->irq_flags = IRQF_SHARED;
  3018. info->init_error = -1; /* assume error, set to 0 on successful init */
  3019. }
  3020. return info;
  3021. }
  3022. static void device_init(int adapter_num, struct pci_dev *pdev)
  3023. {
  3024. struct slgt_info *port_array[SLGT_MAX_PORTS];
  3025. int i;
  3026. int port_count = 1;
  3027. if (pdev->device == SYNCLINK_GT2_DEVICE_ID)
  3028. port_count = 2;
  3029. else if (pdev->device == SYNCLINK_GT4_DEVICE_ID)
  3030. port_count = 4;
  3031. /* allocate device instances for all ports */
  3032. for (i=0; i < port_count; ++i) {
  3033. port_array[i] = alloc_dev(adapter_num, i, pdev);
  3034. if (port_array[i] == NULL) {
  3035. for (--i; i >= 0; --i)
  3036. kfree(port_array[i]);
  3037. return;
  3038. }
  3039. }
  3040. /* give copy of port_array to all ports and add to device list */
  3041. for (i=0; i < port_count; ++i) {
  3042. memcpy(port_array[i]->port_array, port_array, sizeof(port_array));
  3043. add_device(port_array[i]);
  3044. port_array[i]->port_count = port_count;
  3045. spin_lock_init(&port_array[i]->lock);
  3046. }
  3047. /* Allocate and claim adapter resources */
  3048. if (!claim_resources(port_array[0])) {
  3049. alloc_dma_bufs(port_array[0]);
  3050. /* copy resource information from first port to others */
  3051. for (i = 1; i < port_count; ++i) {
  3052. port_array[i]->lock = port_array[0]->lock;
  3053. port_array[i]->irq_level = port_array[0]->irq_level;
  3054. port_array[i]->reg_addr = port_array[0]->reg_addr;
  3055. alloc_dma_bufs(port_array[i]);
  3056. }
  3057. if (request_irq(port_array[0]->irq_level,
  3058. slgt_interrupt,
  3059. port_array[0]->irq_flags,
  3060. port_array[0]->device_name,
  3061. port_array[0]) < 0) {
  3062. DBGERR(("%s request_irq failed IRQ=%d\n",
  3063. port_array[0]->device_name,
  3064. port_array[0]->irq_level));
  3065. } else {
  3066. port_array[0]->irq_requested = true;
  3067. adapter_test(port_array[0]);
  3068. for (i=1 ; i < port_count ; i++) {
  3069. port_array[i]->init_error = port_array[0]->init_error;
  3070. port_array[i]->gpio_present = port_array[0]->gpio_present;
  3071. }
  3072. }
  3073. }
  3074. for (i=0; i < port_count; ++i)
  3075. tty_register_device(serial_driver, port_array[i]->line, &(port_array[i]->pdev->dev));
  3076. }
  3077. static int __devinit init_one(struct pci_dev *dev,
  3078. const struct pci_device_id *ent)
  3079. {
  3080. if (pci_enable_device(dev)) {
  3081. printk("error enabling pci device %p\n", dev);
  3082. return -EIO;
  3083. }
  3084. pci_set_master(dev);
  3085. device_init(slgt_device_count, dev);
  3086. return 0;
  3087. }
  3088. static void __devexit remove_one(struct pci_dev *dev)
  3089. {
  3090. }
  3091. static const struct tty_operations ops = {
  3092. .open = open,
  3093. .close = close,
  3094. .write = write,
  3095. .put_char = put_char,
  3096. .flush_chars = flush_chars,
  3097. .write_room = write_room,
  3098. .chars_in_buffer = chars_in_buffer,
  3099. .flush_buffer = flush_buffer,
  3100. .ioctl = ioctl,
  3101. .compat_ioctl = slgt_compat_ioctl,
  3102. .throttle = throttle,
  3103. .unthrottle = unthrottle,
  3104. .send_xchar = send_xchar,
  3105. .break_ctl = set_break,
  3106. .wait_until_sent = wait_until_sent,
  3107. .set_termios = set_termios,
  3108. .stop = tx_hold,
  3109. .start = tx_release,
  3110. .hangup = hangup,
  3111. .tiocmget = tiocmget,
  3112. .tiocmset = tiocmset,
  3113. .proc_fops = &synclink_gt_proc_fops,
  3114. };
  3115. static void slgt_cleanup(void)
  3116. {
  3117. int rc;
  3118. struct slgt_info *info;
  3119. struct slgt_info *tmp;
  3120. printk(KERN_INFO "unload %s\n", driver_name);
  3121. if (serial_driver) {
  3122. for (info=slgt_device_list ; info != NULL ; info=info->next_device)
  3123. tty_unregister_device(serial_driver, info->line);
  3124. if ((rc = tty_unregister_driver(serial_driver)))
  3125. DBGERR(("tty_unregister_driver error=%d\n", rc));
  3126. put_tty_driver(serial_driver);
  3127. }
  3128. /* reset devices */
  3129. info = slgt_device_list;
  3130. while(info) {
  3131. reset_port(info);
  3132. info = info->next_device;
  3133. }
  3134. /* release devices */
  3135. info = slgt_device_list;
  3136. while(info) {
  3137. #if SYNCLINK_GENERIC_HDLC
  3138. hdlcdev_exit(info);
  3139. #endif
  3140. free_dma_bufs(info);
  3141. free_tmp_rbuf(info);
  3142. if (info->port_num == 0)
  3143. release_resources(info);
  3144. tmp = info;
  3145. info = info->next_device;
  3146. kfree(tmp);
  3147. }
  3148. if (pci_registered)
  3149. pci_unregister_driver(&pci_driver);
  3150. }
  3151. /*
  3152. * Driver initialization entry point.
  3153. */
  3154. static int __init slgt_init(void)
  3155. {
  3156. int rc;
  3157. printk(KERN_INFO "%s\n", driver_name);
  3158. serial_driver = alloc_tty_driver(MAX_DEVICES);
  3159. if (!serial_driver) {
  3160. printk("%s can't allocate tty driver\n", driver_name);
  3161. return -ENOMEM;
  3162. }
  3163. /* Initialize the tty_driver structure */
  3164. serial_driver->owner = THIS_MODULE;
  3165. serial_driver->driver_name = tty_driver_name;
  3166. serial_driver->name = tty_dev_prefix;
  3167. serial_driver->major = ttymajor;
  3168. serial_driver->minor_start = 64;
  3169. serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  3170. serial_driver->subtype = SERIAL_TYPE_NORMAL;
  3171. serial_driver->init_termios = tty_std_termios;
  3172. serial_driver->init_termios.c_cflag =
  3173. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  3174. serial_driver->init_termios.c_ispeed = 9600;
  3175. serial_driver->init_termios.c_ospeed = 9600;
  3176. serial_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  3177. tty_set_operations(serial_driver, &ops);
  3178. if ((rc = tty_register_driver(serial_driver)) < 0) {
  3179. DBGERR(("%s can't register serial driver\n", driver_name));
  3180. put_tty_driver(serial_driver);
  3181. serial_driver = NULL;
  3182. goto error;
  3183. }
  3184. printk(KERN_INFO "%s, tty major#%d\n",
  3185. driver_name, serial_driver->major);
  3186. slgt_device_count = 0;
  3187. if ((rc = pci_register_driver(&pci_driver)) < 0) {
  3188. printk("%s pci_register_driver error=%d\n", driver_name, rc);
  3189. goto error;
  3190. }
  3191. pci_registered = true;
  3192. if (!slgt_device_list)
  3193. printk("%s no devices found\n",driver_name);
  3194. return 0;
  3195. error:
  3196. slgt_cleanup();
  3197. return rc;
  3198. }
  3199. static void __exit slgt_exit(void)
  3200. {
  3201. slgt_cleanup();
  3202. }
  3203. module_init(slgt_init);
  3204. module_exit(slgt_exit);
  3205. /*
  3206. * register access routines
  3207. */
  3208. #define CALC_REGADDR() \
  3209. unsigned long reg_addr = ((unsigned long)info->reg_addr) + addr; \
  3210. if (addr >= 0x80) \
  3211. reg_addr += (info->port_num) * 32;
  3212. static __u8 rd_reg8(struct slgt_info *info, unsigned int addr)
  3213. {
  3214. CALC_REGADDR();
  3215. return readb((void __iomem *)reg_addr);
  3216. }
  3217. static void wr_reg8(struct slgt_info *info, unsigned int addr, __u8 value)
  3218. {
  3219. CALC_REGADDR();
  3220. writeb(value, (void __iomem *)reg_addr);
  3221. }
  3222. static __u16 rd_reg16(struct slgt_info *info, unsigned int addr)
  3223. {
  3224. CALC_REGADDR();
  3225. return readw((void __iomem *)reg_addr);
  3226. }
  3227. static void wr_reg16(struct slgt_info *info, unsigned int addr, __u16 value)
  3228. {
  3229. CALC_REGADDR();
  3230. writew(value, (void __iomem *)reg_addr);
  3231. }
  3232. static __u32 rd_reg32(struct slgt_info *info, unsigned int addr)
  3233. {
  3234. CALC_REGADDR();
  3235. return readl((void __iomem *)reg_addr);
  3236. }
  3237. static void wr_reg32(struct slgt_info *info, unsigned int addr, __u32 value)
  3238. {
  3239. CALC_REGADDR();
  3240. writel(value, (void __iomem *)reg_addr);
  3241. }
  3242. static void rdma_reset(struct slgt_info *info)
  3243. {
  3244. unsigned int i;
  3245. /* set reset bit */
  3246. wr_reg32(info, RDCSR, BIT1);
  3247. /* wait for enable bit cleared */
  3248. for(i=0 ; i < 1000 ; i++)
  3249. if (!(rd_reg32(info, RDCSR) & BIT0))
  3250. break;
  3251. }
  3252. static void tdma_reset(struct slgt_info *info)
  3253. {
  3254. unsigned int i;
  3255. /* set reset bit */
  3256. wr_reg32(info, TDCSR, BIT1);
  3257. /* wait for enable bit cleared */
  3258. for(i=0 ; i < 1000 ; i++)
  3259. if (!(rd_reg32(info, TDCSR) & BIT0))
  3260. break;
  3261. }
  3262. /*
  3263. * enable internal loopback
  3264. * TxCLK and RxCLK are generated from BRG
  3265. * and TxD is looped back to RxD internally.
  3266. */
  3267. static void enable_loopback(struct slgt_info *info)
  3268. {
  3269. /* SCR (serial control) BIT2=looopback enable */
  3270. wr_reg16(info, SCR, (unsigned short)(rd_reg16(info, SCR) | BIT2));
  3271. if (info->params.mode != MGSL_MODE_ASYNC) {
  3272. /* CCR (clock control)
  3273. * 07..05 tx clock source (010 = BRG)
  3274. * 04..02 rx clock source (010 = BRG)
  3275. * 01 auxclk enable (0 = disable)
  3276. * 00 BRG enable (1 = enable)
  3277. *
  3278. * 0100 1001
  3279. */
  3280. wr_reg8(info, CCR, 0x49);
  3281. /* set speed if available, otherwise use default */
  3282. if (info->params.clock_speed)
  3283. set_rate(info, info->params.clock_speed);
  3284. else
  3285. set_rate(info, 3686400);
  3286. }
  3287. }
  3288. /*
  3289. * set baud rate generator to specified rate
  3290. */
  3291. static void set_rate(struct slgt_info *info, u32 rate)
  3292. {
  3293. unsigned int div;
  3294. unsigned int osc = info->base_clock;
  3295. /* div = osc/rate - 1
  3296. *
  3297. * Round div up if osc/rate is not integer to
  3298. * force to next slowest rate.
  3299. */
  3300. if (rate) {
  3301. div = osc/rate;
  3302. if (!(osc % rate) && div)
  3303. div--;
  3304. wr_reg16(info, BDR, (unsigned short)div);
  3305. }
  3306. }
  3307. static void rx_stop(struct slgt_info *info)
  3308. {
  3309. unsigned short val;
  3310. /* disable and reset receiver */
  3311. val = rd_reg16(info, RCR) & ~BIT1; /* clear enable bit */
  3312. wr_reg16(info, RCR, (unsigned short)(val | BIT2)); /* set reset bit */
  3313. wr_reg16(info, RCR, val); /* clear reset bit */
  3314. slgt_irq_off(info, IRQ_RXOVER + IRQ_RXDATA + IRQ_RXIDLE);
  3315. /* clear pending rx interrupts */
  3316. wr_reg16(info, SSR, IRQ_RXIDLE + IRQ_RXOVER);
  3317. rdma_reset(info);
  3318. info->rx_enabled = false;
  3319. info->rx_restart = false;
  3320. }
  3321. static void rx_start(struct slgt_info *info)
  3322. {
  3323. unsigned short val;
  3324. slgt_irq_off(info, IRQ_RXOVER + IRQ_RXDATA);
  3325. /* clear pending rx overrun IRQ */
  3326. wr_reg16(info, SSR, IRQ_RXOVER);
  3327. /* reset and disable receiver */
  3328. val = rd_reg16(info, RCR) & ~BIT1; /* clear enable bit */
  3329. wr_reg16(info, RCR, (unsigned short)(val | BIT2)); /* set reset bit */
  3330. wr_reg16(info, RCR, val); /* clear reset bit */
  3331. rdma_reset(info);
  3332. reset_rbufs(info);
  3333. if (info->rx_pio) {
  3334. /* rx request when rx FIFO not empty */
  3335. wr_reg16(info, SCR, (unsigned short)(rd_reg16(info, SCR) & ~BIT14));
  3336. slgt_irq_on(info, IRQ_RXDATA);
  3337. if (info->params.mode == MGSL_MODE_ASYNC) {
  3338. /* enable saving of rx status */
  3339. wr_reg32(info, RDCSR, BIT6);
  3340. }
  3341. } else {
  3342. /* rx request when rx FIFO half full */
  3343. wr_reg16(info, SCR, (unsigned short)(rd_reg16(info, SCR) | BIT14));
  3344. /* set 1st descriptor address */
  3345. wr_reg32(info, RDDAR, info->rbufs[0].pdesc);
  3346. if (info->params.mode != MGSL_MODE_ASYNC) {
  3347. /* enable rx DMA and DMA interrupt */
  3348. wr_reg32(info, RDCSR, (BIT2 + BIT0));
  3349. } else {
  3350. /* enable saving of rx status, rx DMA and DMA interrupt */
  3351. wr_reg32(info, RDCSR, (BIT6 + BIT2 + BIT0));
  3352. }
  3353. }
  3354. slgt_irq_on(info, IRQ_RXOVER);
  3355. /* enable receiver */
  3356. wr_reg16(info, RCR, (unsigned short)(rd_reg16(info, RCR) | BIT1));
  3357. info->rx_restart = false;
  3358. info->rx_enabled = true;
  3359. }
  3360. static void tx_start(struct slgt_info *info)
  3361. {
  3362. if (!info->tx_enabled) {
  3363. wr_reg16(info, TCR,
  3364. (unsigned short)((rd_reg16(info, TCR) | BIT1) & ~BIT2));
  3365. info->tx_enabled = true;
  3366. }
  3367. if (info->tx_count) {
  3368. info->drop_rts_on_tx_done = false;
  3369. if (info->params.mode != MGSL_MODE_ASYNC) {
  3370. if (info->params.flags & HDLC_FLAG_AUTO_RTS) {
  3371. get_signals(info);
  3372. if (!(info->signals & SerialSignal_RTS)) {
  3373. info->signals |= SerialSignal_RTS;
  3374. set_signals(info);
  3375. info->drop_rts_on_tx_done = true;
  3376. }
  3377. }
  3378. slgt_irq_off(info, IRQ_TXDATA);
  3379. slgt_irq_on(info, IRQ_TXUNDER + IRQ_TXIDLE);
  3380. /* clear tx idle and underrun status bits */
  3381. wr_reg16(info, SSR, (unsigned short)(IRQ_TXIDLE + IRQ_TXUNDER));
  3382. } else {
  3383. slgt_irq_off(info, IRQ_TXDATA);
  3384. slgt_irq_on(info, IRQ_TXIDLE);
  3385. /* clear tx idle status bit */
  3386. wr_reg16(info, SSR, IRQ_TXIDLE);
  3387. }
  3388. /* set 1st descriptor address and start DMA */
  3389. wr_reg32(info, TDDAR, info->tbufs[info->tbuf_start].pdesc);
  3390. wr_reg32(info, TDCSR, BIT2 + BIT0);
  3391. info->tx_active = true;
  3392. }
  3393. }
  3394. static void tx_stop(struct slgt_info *info)
  3395. {
  3396. unsigned short val;
  3397. del_timer(&info->tx_timer);
  3398. tdma_reset(info);
  3399. /* reset and disable transmitter */
  3400. val = rd_reg16(info, TCR) & ~BIT1; /* clear enable bit */
  3401. wr_reg16(info, TCR, (unsigned short)(val | BIT2)); /* set reset bit */
  3402. slgt_irq_off(info, IRQ_TXDATA + IRQ_TXIDLE + IRQ_TXUNDER);
  3403. /* clear tx idle and underrun status bit */
  3404. wr_reg16(info, SSR, (unsigned short)(IRQ_TXIDLE + IRQ_TXUNDER));
  3405. reset_tbufs(info);
  3406. info->tx_enabled = false;
  3407. info->tx_active = false;
  3408. }
  3409. static void reset_port(struct slgt_info *info)
  3410. {
  3411. if (!info->reg_addr)
  3412. return;
  3413. tx_stop(info);
  3414. rx_stop(info);
  3415. info->signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
  3416. set_signals(info);
  3417. slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
  3418. }
  3419. static void reset_adapter(struct slgt_info *info)
  3420. {
  3421. int i;
  3422. for (i=0; i < info->port_count; ++i) {
  3423. if (info->port_array[i])
  3424. reset_port(info->port_array[i]);
  3425. }
  3426. }
  3427. static void async_mode(struct slgt_info *info)
  3428. {
  3429. unsigned short val;
  3430. slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
  3431. tx_stop(info);
  3432. rx_stop(info);
  3433. /* TCR (tx control)
  3434. *
  3435. * 15..13 mode, 010=async
  3436. * 12..10 encoding, 000=NRZ
  3437. * 09 parity enable
  3438. * 08 1=odd parity, 0=even parity
  3439. * 07 1=RTS driver control
  3440. * 06 1=break enable
  3441. * 05..04 character length
  3442. * 00=5 bits
  3443. * 01=6 bits
  3444. * 10=7 bits
  3445. * 11=8 bits
  3446. * 03 0=1 stop bit, 1=2 stop bits
  3447. * 02 reset
  3448. * 01 enable
  3449. * 00 auto-CTS enable
  3450. */
  3451. val = 0x4000;
  3452. if (info->if_mode & MGSL_INTERFACE_RTS_EN)
  3453. val |= BIT7;
  3454. if (info->params.parity != ASYNC_PARITY_NONE) {
  3455. val |= BIT9;
  3456. if (info->params.parity == ASYNC_PARITY_ODD)
  3457. val |= BIT8;
  3458. }
  3459. switch (info->params.data_bits)
  3460. {
  3461. case 6: val |= BIT4; break;
  3462. case 7: val |= BIT5; break;
  3463. case 8: val |= BIT5 + BIT4; break;
  3464. }
  3465. if (info->params.stop_bits != 1)
  3466. val |= BIT3;
  3467. if (info->params.flags & HDLC_FLAG_AUTO_CTS)
  3468. val |= BIT0;
  3469. wr_reg16(info, TCR, val);
  3470. /* RCR (rx control)
  3471. *
  3472. * 15..13 mode, 010=async
  3473. * 12..10 encoding, 000=NRZ
  3474. * 09 parity enable
  3475. * 08 1=odd parity, 0=even parity
  3476. * 07..06 reserved, must be 0
  3477. * 05..04 character length
  3478. * 00=5 bits
  3479. * 01=6 bits
  3480. * 10=7 bits
  3481. * 11=8 bits
  3482. * 03 reserved, must be zero
  3483. * 02 reset
  3484. * 01 enable
  3485. * 00 auto-DCD enable
  3486. */
  3487. val = 0x4000;
  3488. if (info->params.parity != ASYNC_PARITY_NONE) {
  3489. val |= BIT9;
  3490. if (info->params.parity == ASYNC_PARITY_ODD)
  3491. val |= BIT8;
  3492. }
  3493. switch (info->params.data_bits)
  3494. {
  3495. case 6: val |= BIT4; break;
  3496. case 7: val |= BIT5; break;
  3497. case 8: val |= BIT5 + BIT4; break;
  3498. }
  3499. if (info->params.flags & HDLC_FLAG_AUTO_DCD)
  3500. val |= BIT0;
  3501. wr_reg16(info, RCR, val);
  3502. /* CCR (clock control)
  3503. *
  3504. * 07..05 011 = tx clock source is BRG/16
  3505. * 04..02 010 = rx clock source is BRG
  3506. * 01 0 = auxclk disabled
  3507. * 00 1 = BRG enabled
  3508. *
  3509. * 0110 1001
  3510. */
  3511. wr_reg8(info, CCR, 0x69);
  3512. msc_set_vcr(info);
  3513. /* SCR (serial control)
  3514. *
  3515. * 15 1=tx req on FIFO half empty
  3516. * 14 1=rx req on FIFO half full
  3517. * 13 tx data IRQ enable
  3518. * 12 tx idle IRQ enable
  3519. * 11 rx break on IRQ enable
  3520. * 10 rx data IRQ enable
  3521. * 09 rx break off IRQ enable
  3522. * 08 overrun IRQ enable
  3523. * 07 DSR IRQ enable
  3524. * 06 CTS IRQ enable
  3525. * 05 DCD IRQ enable
  3526. * 04 RI IRQ enable
  3527. * 03 0=16x sampling, 1=8x sampling
  3528. * 02 1=txd->rxd internal loopback enable
  3529. * 01 reserved, must be zero
  3530. * 00 1=master IRQ enable
  3531. */
  3532. val = BIT15 + BIT14 + BIT0;
  3533. /* JCR[8] : 1 = x8 async mode feature available */
  3534. if ((rd_reg32(info, JCR) & BIT8) && info->params.data_rate &&
  3535. ((info->base_clock < (info->params.data_rate * 16)) ||
  3536. (info->base_clock % (info->params.data_rate * 16)))) {
  3537. /* use 8x sampling */
  3538. val |= BIT3;
  3539. set_rate(info, info->params.data_rate * 8);
  3540. } else {
  3541. /* use 16x sampling */
  3542. set_rate(info, info->params.data_rate * 16);
  3543. }
  3544. wr_reg16(info, SCR, val);
  3545. slgt_irq_on(info, IRQ_RXBREAK | IRQ_RXOVER);
  3546. if (info->params.loopback)
  3547. enable_loopback(info);
  3548. }
  3549. static void sync_mode(struct slgt_info *info)
  3550. {
  3551. unsigned short val;
  3552. slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
  3553. tx_stop(info);
  3554. rx_stop(info);
  3555. /* TCR (tx control)
  3556. *
  3557. * 15..13 mode, 000=HDLC 001=raw 010=async 011=monosync 100=bisync
  3558. * 12..10 encoding
  3559. * 09 CRC enable
  3560. * 08 CRC32
  3561. * 07 1=RTS driver control
  3562. * 06 preamble enable
  3563. * 05..04 preamble length
  3564. * 03 share open/close flag
  3565. * 02 reset
  3566. * 01 enable
  3567. * 00 auto-CTS enable
  3568. */
  3569. val = BIT2;
  3570. switch(info->params.mode) {
  3571. case MGSL_MODE_MONOSYNC: val |= BIT14 + BIT13; break;
  3572. case MGSL_MODE_BISYNC: val |= BIT15; break;
  3573. case MGSL_MODE_RAW: val |= BIT13; break;
  3574. }
  3575. if (info->if_mode & MGSL_INTERFACE_RTS_EN)
  3576. val |= BIT7;
  3577. switch(info->params.encoding)
  3578. {
  3579. case HDLC_ENCODING_NRZB: val |= BIT10; break;
  3580. case HDLC_ENCODING_NRZI_MARK: val |= BIT11; break;
  3581. case HDLC_ENCODING_NRZI: val |= BIT11 + BIT10; break;
  3582. case HDLC_ENCODING_BIPHASE_MARK: val |= BIT12; break;
  3583. case HDLC_ENCODING_BIPHASE_SPACE: val |= BIT12 + BIT10; break;
  3584. case HDLC_ENCODING_BIPHASE_LEVEL: val |= BIT12 + BIT11; break;
  3585. case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: val |= BIT12 + BIT11 + BIT10; break;
  3586. }
  3587. switch (info->params.crc_type & HDLC_CRC_MASK)
  3588. {
  3589. case HDLC_CRC_16_CCITT: val |= BIT9; break;
  3590. case HDLC_CRC_32_CCITT: val |= BIT9 + BIT8; break;
  3591. }
  3592. if (info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE)
  3593. val |= BIT6;
  3594. switch (info->params.preamble_length)
  3595. {
  3596. case HDLC_PREAMBLE_LENGTH_16BITS: val |= BIT5; break;
  3597. case HDLC_PREAMBLE_LENGTH_32BITS: val |= BIT4; break;
  3598. case HDLC_PREAMBLE_LENGTH_64BITS: val |= BIT5 + BIT4; break;
  3599. }
  3600. if (info->params.flags & HDLC_FLAG_AUTO_CTS)
  3601. val |= BIT0;
  3602. wr_reg16(info, TCR, val);
  3603. /* TPR (transmit preamble) */
  3604. switch (info->params.preamble)
  3605. {
  3606. case HDLC_PREAMBLE_PATTERN_FLAGS: val = 0x7e; break;
  3607. case HDLC_PREAMBLE_PATTERN_ONES: val = 0xff; break;
  3608. case HDLC_PREAMBLE_PATTERN_ZEROS: val = 0x00; break;
  3609. case HDLC_PREAMBLE_PATTERN_10: val = 0x55; break;
  3610. case HDLC_PREAMBLE_PATTERN_01: val = 0xaa; break;
  3611. default: val = 0x7e; break;
  3612. }
  3613. wr_reg8(info, TPR, (unsigned char)val);
  3614. /* RCR (rx control)
  3615. *
  3616. * 15..13 mode, 000=HDLC 001=raw 010=async 011=monosync 100=bisync
  3617. * 12..10 encoding
  3618. * 09 CRC enable
  3619. * 08 CRC32
  3620. * 07..03 reserved, must be 0
  3621. * 02 reset
  3622. * 01 enable
  3623. * 00 auto-DCD enable
  3624. */
  3625. val = 0;
  3626. switch(info->params.mode) {
  3627. case MGSL_MODE_MONOSYNC: val |= BIT14 + BIT13; break;
  3628. case MGSL_MODE_BISYNC: val |= BIT15; break;
  3629. case MGSL_MODE_RAW: val |= BIT13; break;
  3630. }
  3631. switch(info->params.encoding)
  3632. {
  3633. case HDLC_ENCODING_NRZB: val |= BIT10; break;
  3634. case HDLC_ENCODING_NRZI_MARK: val |= BIT11; break;
  3635. case HDLC_ENCODING_NRZI: val |= BIT11 + BIT10; break;
  3636. case HDLC_ENCODING_BIPHASE_MARK: val |= BIT12; break;
  3637. case HDLC_ENCODING_BIPHASE_SPACE: val |= BIT12 + BIT10; break;
  3638. case HDLC_ENCODING_BIPHASE_LEVEL: val |= BIT12 + BIT11; break;
  3639. case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: val |= BIT12 + BIT11 + BIT10; break;
  3640. }
  3641. switch (info->params.crc_type & HDLC_CRC_MASK)
  3642. {
  3643. case HDLC_CRC_16_CCITT: val |= BIT9; break;
  3644. case HDLC_CRC_32_CCITT: val |= BIT9 + BIT8; break;
  3645. }
  3646. if (info->params.flags & HDLC_FLAG_AUTO_DCD)
  3647. val |= BIT0;
  3648. wr_reg16(info, RCR, val);
  3649. /* CCR (clock control)
  3650. *
  3651. * 07..05 tx clock source
  3652. * 04..02 rx clock source
  3653. * 01 auxclk enable
  3654. * 00 BRG enable
  3655. */
  3656. val = 0;
  3657. if (info->params.flags & HDLC_FLAG_TXC_BRG)
  3658. {
  3659. // when RxC source is DPLL, BRG generates 16X DPLL
  3660. // reference clock, so take TxC from BRG/16 to get
  3661. // transmit clock at actual data rate
  3662. if (info->params.flags & HDLC_FLAG_RXC_DPLL)
  3663. val |= BIT6 + BIT5; /* 011, txclk = BRG/16 */
  3664. else
  3665. val |= BIT6; /* 010, txclk = BRG */
  3666. }
  3667. else if (info->params.flags & HDLC_FLAG_TXC_DPLL)
  3668. val |= BIT7; /* 100, txclk = DPLL Input */
  3669. else if (info->params.flags & HDLC_FLAG_TXC_RXCPIN)
  3670. val |= BIT5; /* 001, txclk = RXC Input */
  3671. if (info->params.flags & HDLC_FLAG_RXC_BRG)
  3672. val |= BIT3; /* 010, rxclk = BRG */
  3673. else if (info->params.flags & HDLC_FLAG_RXC_DPLL)
  3674. val |= BIT4; /* 100, rxclk = DPLL */
  3675. else if (info->params.flags & HDLC_FLAG_RXC_TXCPIN)
  3676. val |= BIT2; /* 001, rxclk = TXC Input */
  3677. if (info->params.clock_speed)
  3678. val |= BIT1 + BIT0;
  3679. wr_reg8(info, CCR, (unsigned char)val);
  3680. if (info->params.flags & (HDLC_FLAG_TXC_DPLL + HDLC_FLAG_RXC_DPLL))
  3681. {
  3682. // program DPLL mode
  3683. switch(info->params.encoding)
  3684. {
  3685. case HDLC_ENCODING_BIPHASE_MARK:
  3686. case HDLC_ENCODING_BIPHASE_SPACE:
  3687. val = BIT7; break;
  3688. case HDLC_ENCODING_BIPHASE_LEVEL:
  3689. case HDLC_ENCODING_DIFF_BIPHASE_LEVEL:
  3690. val = BIT7 + BIT6; break;
  3691. default: val = BIT6; // NRZ encodings
  3692. }
  3693. wr_reg16(info, RCR, (unsigned short)(rd_reg16(info, RCR) | val));
  3694. // DPLL requires a 16X reference clock from BRG
  3695. set_rate(info, info->params.clock_speed * 16);
  3696. }
  3697. else
  3698. set_rate(info, info->params.clock_speed);
  3699. tx_set_idle(info);
  3700. msc_set_vcr(info);
  3701. /* SCR (serial control)
  3702. *
  3703. * 15 1=tx req on FIFO half empty
  3704. * 14 1=rx req on FIFO half full
  3705. * 13 tx data IRQ enable
  3706. * 12 tx idle IRQ enable
  3707. * 11 underrun IRQ enable
  3708. * 10 rx data IRQ enable
  3709. * 09 rx idle IRQ enable
  3710. * 08 overrun IRQ enable
  3711. * 07 DSR IRQ enable
  3712. * 06 CTS IRQ enable
  3713. * 05 DCD IRQ enable
  3714. * 04 RI IRQ enable
  3715. * 03 reserved, must be zero
  3716. * 02 1=txd->rxd internal loopback enable
  3717. * 01 reserved, must be zero
  3718. * 00 1=master IRQ enable
  3719. */
  3720. wr_reg16(info, SCR, BIT15 + BIT14 + BIT0);
  3721. if (info->params.loopback)
  3722. enable_loopback(info);
  3723. }
  3724. /*
  3725. * set transmit idle mode
  3726. */
  3727. static void tx_set_idle(struct slgt_info *info)
  3728. {
  3729. unsigned char val;
  3730. unsigned short tcr;
  3731. /* if preamble enabled (tcr[6] == 1) then tx idle size = 8 bits
  3732. * else tcr[5:4] = tx idle size: 00 = 8 bits, 01 = 16 bits
  3733. */
  3734. tcr = rd_reg16(info, TCR);
  3735. if (info->idle_mode & HDLC_TXIDLE_CUSTOM_16) {
  3736. /* disable preamble, set idle size to 16 bits */
  3737. tcr = (tcr & ~(BIT6 + BIT5)) | BIT4;
  3738. /* MSB of 16 bit idle specified in tx preamble register (TPR) */
  3739. wr_reg8(info, TPR, (unsigned char)((info->idle_mode >> 8) & 0xff));
  3740. } else if (!(tcr & BIT6)) {
  3741. /* preamble is disabled, set idle size to 8 bits */
  3742. tcr &= ~(BIT5 + BIT4);
  3743. }
  3744. wr_reg16(info, TCR, tcr);
  3745. if (info->idle_mode & (HDLC_TXIDLE_CUSTOM_8 | HDLC_TXIDLE_CUSTOM_16)) {
  3746. /* LSB of custom tx idle specified in tx idle register */
  3747. val = (unsigned char)(info->idle_mode & 0xff);
  3748. } else {
  3749. /* standard 8 bit idle patterns */
  3750. switch(info->idle_mode)
  3751. {
  3752. case HDLC_TXIDLE_FLAGS: val = 0x7e; break;
  3753. case HDLC_TXIDLE_ALT_ZEROS_ONES:
  3754. case HDLC_TXIDLE_ALT_MARK_SPACE: val = 0xaa; break;
  3755. case HDLC_TXIDLE_ZEROS:
  3756. case HDLC_TXIDLE_SPACE: val = 0x00; break;
  3757. default: val = 0xff;
  3758. }
  3759. }
  3760. wr_reg8(info, TIR, val);
  3761. }
  3762. /*
  3763. * get state of V24 status (input) signals
  3764. */
  3765. static void get_signals(struct slgt_info *info)
  3766. {
  3767. unsigned short status = rd_reg16(info, SSR);
  3768. /* clear all serial signals except DTR and RTS */
  3769. info->signals &= SerialSignal_DTR + SerialSignal_RTS;
  3770. if (status & BIT3)
  3771. info->signals |= SerialSignal_DSR;
  3772. if (status & BIT2)
  3773. info->signals |= SerialSignal_CTS;
  3774. if (status & BIT1)
  3775. info->signals |= SerialSignal_DCD;
  3776. if (status & BIT0)
  3777. info->signals |= SerialSignal_RI;
  3778. }
  3779. /*
  3780. * set V.24 Control Register based on current configuration
  3781. */
  3782. static void msc_set_vcr(struct slgt_info *info)
  3783. {
  3784. unsigned char val = 0;
  3785. /* VCR (V.24 control)
  3786. *
  3787. * 07..04 serial IF select
  3788. * 03 DTR
  3789. * 02 RTS
  3790. * 01 LL
  3791. * 00 RL
  3792. */
  3793. switch(info->if_mode & MGSL_INTERFACE_MASK)
  3794. {
  3795. case MGSL_INTERFACE_RS232:
  3796. val |= BIT5; /* 0010 */
  3797. break;
  3798. case MGSL_INTERFACE_V35:
  3799. val |= BIT7 + BIT6 + BIT5; /* 1110 */
  3800. break;
  3801. case MGSL_INTERFACE_RS422:
  3802. val |= BIT6; /* 0100 */
  3803. break;
  3804. }
  3805. if (info->if_mode & MGSL_INTERFACE_MSB_FIRST)
  3806. val |= BIT4;
  3807. if (info->signals & SerialSignal_DTR)
  3808. val |= BIT3;
  3809. if (info->signals & SerialSignal_RTS)
  3810. val |= BIT2;
  3811. if (info->if_mode & MGSL_INTERFACE_LL)
  3812. val |= BIT1;
  3813. if (info->if_mode & MGSL_INTERFACE_RL)
  3814. val |= BIT0;
  3815. wr_reg8(info, VCR, val);
  3816. }
  3817. /*
  3818. * set state of V24 control (output) signals
  3819. */
  3820. static void set_signals(struct slgt_info *info)
  3821. {
  3822. unsigned char val = rd_reg8(info, VCR);
  3823. if (info->signals & SerialSignal_DTR)
  3824. val |= BIT3;
  3825. else
  3826. val &= ~BIT3;
  3827. if (info->signals & SerialSignal_RTS)
  3828. val |= BIT2;
  3829. else
  3830. val &= ~BIT2;
  3831. wr_reg8(info, VCR, val);
  3832. }
  3833. /*
  3834. * free range of receive DMA buffers (i to last)
  3835. */
  3836. static void free_rbufs(struct slgt_info *info, unsigned int i, unsigned int last)
  3837. {
  3838. int done = 0;
  3839. while(!done) {
  3840. /* reset current buffer for reuse */
  3841. info->rbufs[i].status = 0;
  3842. set_desc_count(info->rbufs[i], info->rbuf_fill_level);
  3843. if (i == last)
  3844. done = 1;
  3845. if (++i == info->rbuf_count)
  3846. i = 0;
  3847. }
  3848. info->rbuf_current = i;
  3849. }
  3850. /*
  3851. * mark all receive DMA buffers as free
  3852. */
  3853. static void reset_rbufs(struct slgt_info *info)
  3854. {
  3855. free_rbufs(info, 0, info->rbuf_count - 1);
  3856. info->rbuf_fill_index = 0;
  3857. info->rbuf_fill_count = 0;
  3858. }
  3859. /*
  3860. * pass receive HDLC frame to upper layer
  3861. *
  3862. * return true if frame available, otherwise false
  3863. */
  3864. static bool rx_get_frame(struct slgt_info *info)
  3865. {
  3866. unsigned int start, end;
  3867. unsigned short status;
  3868. unsigned int framesize = 0;
  3869. unsigned long flags;
  3870. struct tty_struct *tty = info->port.tty;
  3871. unsigned char addr_field = 0xff;
  3872. unsigned int crc_size = 0;
  3873. switch (info->params.crc_type & HDLC_CRC_MASK) {
  3874. case HDLC_CRC_16_CCITT: crc_size = 2; break;
  3875. case HDLC_CRC_32_CCITT: crc_size = 4; break;
  3876. }
  3877. check_again:
  3878. framesize = 0;
  3879. addr_field = 0xff;
  3880. start = end = info->rbuf_current;
  3881. for (;;) {
  3882. if (!desc_complete(info->rbufs[end]))
  3883. goto cleanup;
  3884. if (framesize == 0 && info->params.addr_filter != 0xff)
  3885. addr_field = info->rbufs[end].buf[0];
  3886. framesize += desc_count(info->rbufs[end]);
  3887. if (desc_eof(info->rbufs[end]))
  3888. break;
  3889. if (++end == info->rbuf_count)
  3890. end = 0;
  3891. if (end == info->rbuf_current) {
  3892. if (info->rx_enabled){
  3893. spin_lock_irqsave(&info->lock,flags);
  3894. rx_start(info);
  3895. spin_unlock_irqrestore(&info->lock,flags);
  3896. }
  3897. goto cleanup;
  3898. }
  3899. }
  3900. /* status
  3901. *
  3902. * 15 buffer complete
  3903. * 14..06 reserved
  3904. * 05..04 residue
  3905. * 02 eof (end of frame)
  3906. * 01 CRC error
  3907. * 00 abort
  3908. */
  3909. status = desc_status(info->rbufs[end]);
  3910. /* ignore CRC bit if not using CRC (bit is undefined) */
  3911. if ((info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_NONE)
  3912. status &= ~BIT1;
  3913. if (framesize == 0 ||
  3914. (addr_field != 0xff && addr_field != info->params.addr_filter)) {
  3915. free_rbufs(info, start, end);
  3916. goto check_again;
  3917. }
  3918. if (framesize < (2 + crc_size) || status & BIT0) {
  3919. info->icount.rxshort++;
  3920. framesize = 0;
  3921. } else if (status & BIT1) {
  3922. info->icount.rxcrc++;
  3923. if (!(info->params.crc_type & HDLC_CRC_RETURN_EX))
  3924. framesize = 0;
  3925. }
  3926. #if SYNCLINK_GENERIC_HDLC
  3927. if (framesize == 0) {
  3928. info->netdev->stats.rx_errors++;
  3929. info->netdev->stats.rx_frame_errors++;
  3930. }
  3931. #endif
  3932. DBGBH(("%s rx frame status=%04X size=%d\n",
  3933. info->device_name, status, framesize));
  3934. DBGDATA(info, info->rbufs[start].buf, min_t(int, framesize, info->rbuf_fill_level), "rx");
  3935. if (framesize) {
  3936. if (!(info->params.crc_type & HDLC_CRC_RETURN_EX)) {
  3937. framesize -= crc_size;
  3938. crc_size = 0;
  3939. }
  3940. if (framesize > info->max_frame_size + crc_size)
  3941. info->icount.rxlong++;
  3942. else {
  3943. /* copy dma buffer(s) to contiguous temp buffer */
  3944. int copy_count = framesize;
  3945. int i = start;
  3946. unsigned char *p = info->tmp_rbuf;
  3947. info->tmp_rbuf_count = framesize;
  3948. info->icount.rxok++;
  3949. while(copy_count) {
  3950. int partial_count = min_t(int, copy_count, info->rbuf_fill_level);
  3951. memcpy(p, info->rbufs[i].buf, partial_count);
  3952. p += partial_count;
  3953. copy_count -= partial_count;
  3954. if (++i == info->rbuf_count)
  3955. i = 0;
  3956. }
  3957. if (info->params.crc_type & HDLC_CRC_RETURN_EX) {
  3958. *p = (status & BIT1) ? RX_CRC_ERROR : RX_OK;
  3959. framesize++;
  3960. }
  3961. #if SYNCLINK_GENERIC_HDLC
  3962. if (info->netcount)
  3963. hdlcdev_rx(info,info->tmp_rbuf, framesize);
  3964. else
  3965. #endif
  3966. ldisc_receive_buf(tty, info->tmp_rbuf, info->flag_buf, framesize);
  3967. }
  3968. }
  3969. free_rbufs(info, start, end);
  3970. return true;
  3971. cleanup:
  3972. return false;
  3973. }
  3974. /*
  3975. * pass receive buffer (RAW synchronous mode) to tty layer
  3976. * return true if buffer available, otherwise false
  3977. */
  3978. static bool rx_get_buf(struct slgt_info *info)
  3979. {
  3980. unsigned int i = info->rbuf_current;
  3981. unsigned int count;
  3982. if (!desc_complete(info->rbufs[i]))
  3983. return false;
  3984. count = desc_count(info->rbufs[i]);
  3985. switch(info->params.mode) {
  3986. case MGSL_MODE_MONOSYNC:
  3987. case MGSL_MODE_BISYNC:
  3988. /* ignore residue in byte synchronous modes */
  3989. if (desc_residue(info->rbufs[i]))
  3990. count--;
  3991. break;
  3992. }
  3993. DBGDATA(info, info->rbufs[i].buf, count, "rx");
  3994. DBGINFO(("rx_get_buf size=%d\n", count));
  3995. if (count)
  3996. ldisc_receive_buf(info->port.tty, info->rbufs[i].buf,
  3997. info->flag_buf, count);
  3998. free_rbufs(info, i, i);
  3999. return true;
  4000. }
  4001. static void reset_tbufs(struct slgt_info *info)
  4002. {
  4003. unsigned int i;
  4004. info->tbuf_current = 0;
  4005. for (i=0 ; i < info->tbuf_count ; i++) {
  4006. info->tbufs[i].status = 0;
  4007. info->tbufs[i].count = 0;
  4008. }
  4009. }
  4010. /*
  4011. * return number of free transmit DMA buffers
  4012. */
  4013. static unsigned int free_tbuf_count(struct slgt_info *info)
  4014. {
  4015. unsigned int count = 0;
  4016. unsigned int i = info->tbuf_current;
  4017. do
  4018. {
  4019. if (desc_count(info->tbufs[i]))
  4020. break; /* buffer in use */
  4021. ++count;
  4022. if (++i == info->tbuf_count)
  4023. i=0;
  4024. } while (i != info->tbuf_current);
  4025. /* if tx DMA active, last zero count buffer is in use */
  4026. if (count && (rd_reg32(info, TDCSR) & BIT0))
  4027. --count;
  4028. return count;
  4029. }
  4030. /*
  4031. * return number of bytes in unsent transmit DMA buffers
  4032. * and the serial controller tx FIFO
  4033. */
  4034. static unsigned int tbuf_bytes(struct slgt_info *info)
  4035. {
  4036. unsigned int total_count = 0;
  4037. unsigned int i = info->tbuf_current;
  4038. unsigned int reg_value;
  4039. unsigned int count;
  4040. unsigned int active_buf_count = 0;
  4041. /*
  4042. * Add descriptor counts for all tx DMA buffers.
  4043. * If count is zero (cleared by DMA controller after read),
  4044. * the buffer is complete or is actively being read from.
  4045. *
  4046. * Record buf_count of last buffer with zero count starting
  4047. * from current ring position. buf_count is mirror
  4048. * copy of count and is not cleared by serial controller.
  4049. * If DMA controller is active, that buffer is actively
  4050. * being read so add to total.
  4051. */
  4052. do {
  4053. count = desc_count(info->tbufs[i]);
  4054. if (count)
  4055. total_count += count;
  4056. else if (!total_count)
  4057. active_buf_count = info->tbufs[i].buf_count;
  4058. if (++i == info->tbuf_count)
  4059. i = 0;
  4060. } while (i != info->tbuf_current);
  4061. /* read tx DMA status register */
  4062. reg_value = rd_reg32(info, TDCSR);
  4063. /* if tx DMA active, last zero count buffer is in use */
  4064. if (reg_value & BIT0)
  4065. total_count += active_buf_count;
  4066. /* add tx FIFO count = reg_value[15..8] */
  4067. total_count += (reg_value >> 8) & 0xff;
  4068. /* if transmitter active add one byte for shift register */
  4069. if (info->tx_active)
  4070. total_count++;
  4071. return total_count;
  4072. }
  4073. /*
  4074. * load transmit DMA buffer(s) with data
  4075. */
  4076. static void tx_load(struct slgt_info *info, const char *buf, unsigned int size)
  4077. {
  4078. unsigned short count;
  4079. unsigned int i;
  4080. struct slgt_desc *d;
  4081. if (size == 0)
  4082. return;
  4083. DBGDATA(info, buf, size, "tx");
  4084. info->tbuf_start = i = info->tbuf_current;
  4085. while (size) {
  4086. d = &info->tbufs[i];
  4087. if (++i == info->tbuf_count)
  4088. i = 0;
  4089. count = (unsigned short)((size > DMABUFSIZE) ? DMABUFSIZE : size);
  4090. memcpy(d->buf, buf, count);
  4091. size -= count;
  4092. buf += count;
  4093. /*
  4094. * set EOF bit for last buffer of HDLC frame or
  4095. * for every buffer in raw mode
  4096. */
  4097. if ((!size && info->params.mode == MGSL_MODE_HDLC) ||
  4098. info->params.mode == MGSL_MODE_RAW)
  4099. set_desc_eof(*d, 1);
  4100. else
  4101. set_desc_eof(*d, 0);
  4102. set_desc_count(*d, count);
  4103. d->buf_count = count;
  4104. }
  4105. info->tbuf_current = i;
  4106. }
  4107. static int register_test(struct slgt_info *info)
  4108. {
  4109. static unsigned short patterns[] =
  4110. {0x0000, 0xffff, 0xaaaa, 0x5555, 0x6969, 0x9696};
  4111. static unsigned int count = sizeof(patterns)/sizeof(patterns[0]);
  4112. unsigned int i;
  4113. int rc = 0;
  4114. for (i=0 ; i < count ; i++) {
  4115. wr_reg16(info, TIR, patterns[i]);
  4116. wr_reg16(info, BDR, patterns[(i+1)%count]);
  4117. if ((rd_reg16(info, TIR) != patterns[i]) ||
  4118. (rd_reg16(info, BDR) != patterns[(i+1)%count])) {
  4119. rc = -ENODEV;
  4120. break;
  4121. }
  4122. }
  4123. info->gpio_present = (rd_reg32(info, JCR) & BIT5) ? 1 : 0;
  4124. info->init_error = rc ? 0 : DiagStatus_AddressFailure;
  4125. return rc;
  4126. }
  4127. static int irq_test(struct slgt_info *info)
  4128. {
  4129. unsigned long timeout;
  4130. unsigned long flags;
  4131. struct tty_struct *oldtty = info->port.tty;
  4132. u32 speed = info->params.data_rate;
  4133. info->params.data_rate = 921600;
  4134. info->port.tty = NULL;
  4135. spin_lock_irqsave(&info->lock, flags);
  4136. async_mode(info);
  4137. slgt_irq_on(info, IRQ_TXIDLE);
  4138. /* enable transmitter */
  4139. wr_reg16(info, TCR,
  4140. (unsigned short)(rd_reg16(info, TCR) | BIT1));
  4141. /* write one byte and wait for tx idle */
  4142. wr_reg16(info, TDR, 0);
  4143. /* assume failure */
  4144. info->init_error = DiagStatus_IrqFailure;
  4145. info->irq_occurred = false;
  4146. spin_unlock_irqrestore(&info->lock, flags);
  4147. timeout=100;
  4148. while(timeout-- && !info->irq_occurred)
  4149. msleep_interruptible(10);
  4150. spin_lock_irqsave(&info->lock,flags);
  4151. reset_port(info);
  4152. spin_unlock_irqrestore(&info->lock,flags);
  4153. info->params.data_rate = speed;
  4154. info->port.tty = oldtty;
  4155. info->init_error = info->irq_occurred ? 0 : DiagStatus_IrqFailure;
  4156. return info->irq_occurred ? 0 : -ENODEV;
  4157. }
  4158. static int loopback_test_rx(struct slgt_info *info)
  4159. {
  4160. unsigned char *src, *dest;
  4161. int count;
  4162. if (desc_complete(info->rbufs[0])) {
  4163. count = desc_count(info->rbufs[0]);
  4164. src = info->rbufs[0].buf;
  4165. dest = info->tmp_rbuf;
  4166. for( ; count ; count-=2, src+=2) {
  4167. /* src=data byte (src+1)=status byte */
  4168. if (!(*(src+1) & (BIT9 + BIT8))) {
  4169. *dest = *src;
  4170. dest++;
  4171. info->tmp_rbuf_count++;
  4172. }
  4173. }
  4174. DBGDATA(info, info->tmp_rbuf, info->tmp_rbuf_count, "rx");
  4175. return 1;
  4176. }
  4177. return 0;
  4178. }
  4179. static int loopback_test(struct slgt_info *info)
  4180. {
  4181. #define TESTFRAMESIZE 20
  4182. unsigned long timeout;
  4183. u16 count = TESTFRAMESIZE;
  4184. unsigned char buf[TESTFRAMESIZE];
  4185. int rc = -ENODEV;
  4186. unsigned long flags;
  4187. struct tty_struct *oldtty = info->port.tty;
  4188. MGSL_PARAMS params;
  4189. memcpy(&params, &info->params, sizeof(params));
  4190. info->params.mode = MGSL_MODE_ASYNC;
  4191. info->params.data_rate = 921600;
  4192. info->params.loopback = 1;
  4193. info->port.tty = NULL;
  4194. /* build and send transmit frame */
  4195. for (count = 0; count < TESTFRAMESIZE; ++count)
  4196. buf[count] = (unsigned char)count;
  4197. info->tmp_rbuf_count = 0;
  4198. memset(info->tmp_rbuf, 0, TESTFRAMESIZE);
  4199. /* program hardware for HDLC and enabled receiver */
  4200. spin_lock_irqsave(&info->lock,flags);
  4201. async_mode(info);
  4202. rx_start(info);
  4203. info->tx_count = count;
  4204. tx_load(info, buf, count);
  4205. tx_start(info);
  4206. spin_unlock_irqrestore(&info->lock, flags);
  4207. /* wait for receive complete */
  4208. for (timeout = 100; timeout; --timeout) {
  4209. msleep_interruptible(10);
  4210. if (loopback_test_rx(info)) {
  4211. rc = 0;
  4212. break;
  4213. }
  4214. }
  4215. /* verify received frame length and contents */
  4216. if (!rc && (info->tmp_rbuf_count != count ||
  4217. memcmp(buf, info->tmp_rbuf, count))) {
  4218. rc = -ENODEV;
  4219. }
  4220. spin_lock_irqsave(&info->lock,flags);
  4221. reset_adapter(info);
  4222. spin_unlock_irqrestore(&info->lock,flags);
  4223. memcpy(&info->params, &params, sizeof(info->params));
  4224. info->port.tty = oldtty;
  4225. info->init_error = rc ? DiagStatus_DmaFailure : 0;
  4226. return rc;
  4227. }
  4228. static int adapter_test(struct slgt_info *info)
  4229. {
  4230. DBGINFO(("testing %s\n", info->device_name));
  4231. if (register_test(info) < 0) {
  4232. printk("register test failure %s addr=%08X\n",
  4233. info->device_name, info->phys_reg_addr);
  4234. } else if (irq_test(info) < 0) {
  4235. printk("IRQ test failure %s IRQ=%d\n",
  4236. info->device_name, info->irq_level);
  4237. } else if (loopback_test(info) < 0) {
  4238. printk("loopback test failure %s\n", info->device_name);
  4239. }
  4240. return info->init_error;
  4241. }
  4242. /*
  4243. * transmit timeout handler
  4244. */
  4245. static void tx_timeout(unsigned long context)
  4246. {
  4247. struct slgt_info *info = (struct slgt_info*)context;
  4248. unsigned long flags;
  4249. DBGINFO(("%s tx_timeout\n", info->device_name));
  4250. if(info->tx_active && info->params.mode == MGSL_MODE_HDLC) {
  4251. info->icount.txtimeout++;
  4252. }
  4253. spin_lock_irqsave(&info->lock,flags);
  4254. tx_stop(info);
  4255. spin_unlock_irqrestore(&info->lock,flags);
  4256. #if SYNCLINK_GENERIC_HDLC
  4257. if (info->netcount)
  4258. hdlcdev_tx_done(info);
  4259. else
  4260. #endif
  4261. bh_transmit(info);
  4262. }
  4263. /*
  4264. * receive buffer polling timer
  4265. */
  4266. static void rx_timeout(unsigned long context)
  4267. {
  4268. struct slgt_info *info = (struct slgt_info*)context;
  4269. unsigned long flags;
  4270. DBGINFO(("%s rx_timeout\n", info->device_name));
  4271. spin_lock_irqsave(&info->lock, flags);
  4272. info->pending_bh |= BH_RECEIVE;
  4273. spin_unlock_irqrestore(&info->lock, flags);
  4274. bh_handler(&info->task);
  4275. }