synclink_gt.c 124 KB

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