synclink_gt.c 132 KB

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