synclink_gt.c 125 KB

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