synclink_gt.c 126 KB

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