synclink_gt.c 127 KB

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