synclink_gt.c 124 KB

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