synclink_gt.c 119 KB

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