synclink_gt.c 126 KB

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