stallion.c 132 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199
  1. /*****************************************************************************/
  2. /*
  3. * stallion.c -- stallion multiport serial driver.
  4. *
  5. * Copyright (C) 1996-1999 Stallion Technologies
  6. * Copyright (C) 1994-1996 Greg Ungerer.
  7. *
  8. * This code is loosely based on the Linux serial driver, written by
  9. * Linus Torvalds, Theodore T'so and others.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. /*****************************************************************************/
  26. #include <linux/config.h>
  27. #include <linux/module.h>
  28. #include <linux/slab.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/tty.h>
  31. #include <linux/tty_flip.h>
  32. #include <linux/serial.h>
  33. #include <linux/cd1400.h>
  34. #include <linux/sc26198.h>
  35. #include <linux/comstats.h>
  36. #include <linux/stallion.h>
  37. #include <linux/ioport.h>
  38. #include <linux/init.h>
  39. #include <linux/smp_lock.h>
  40. #include <linux/devfs_fs_kernel.h>
  41. #include <linux/device.h>
  42. #include <linux/delay.h>
  43. #include <asm/io.h>
  44. #include <asm/uaccess.h>
  45. #ifdef CONFIG_PCI
  46. #include <linux/pci.h>
  47. #endif
  48. /*****************************************************************************/
  49. /*
  50. * Define different board types. Use the standard Stallion "assigned"
  51. * board numbers. Boards supported in this driver are abbreviated as
  52. * EIO = EasyIO and ECH = EasyConnection 8/32.
  53. */
  54. #define BRD_EASYIO 20
  55. #define BRD_ECH 21
  56. #define BRD_ECHMC 22
  57. #define BRD_ECHPCI 26
  58. #define BRD_ECH64PCI 27
  59. #define BRD_EASYIOPCI 28
  60. /*
  61. * Define a configuration structure to hold the board configuration.
  62. * Need to set this up in the code (for now) with the boards that are
  63. * to be configured into the system. This is what needs to be modified
  64. * when adding/removing/modifying boards. Each line entry in the
  65. * stl_brdconf[] array is a board. Each line contains io/irq/memory
  66. * ranges for that board (as well as what type of board it is).
  67. * Some examples:
  68. * { BRD_EASYIO, 0x2a0, 0, 0, 10, 0 },
  69. * This line would configure an EasyIO board (4 or 8, no difference),
  70. * at io address 2a0 and irq 10.
  71. * Another example:
  72. * { BRD_ECH, 0x2a8, 0x280, 0, 12, 0 },
  73. * This line will configure an EasyConnection 8/32 board at primary io
  74. * address 2a8, secondary io address 280 and irq 12.
  75. * Enter as many lines into this array as you want (only the first 4
  76. * will actually be used!). Any combination of EasyIO and EasyConnection
  77. * boards can be specified. EasyConnection 8/32 boards can share their
  78. * secondary io addresses between each other.
  79. *
  80. * NOTE: there is no need to put any entries in this table for PCI
  81. * boards. They will be found automatically by the driver - provided
  82. * PCI BIOS32 support is compiled into the kernel.
  83. */
  84. typedef struct {
  85. int brdtype;
  86. int ioaddr1;
  87. int ioaddr2;
  88. unsigned long memaddr;
  89. int irq;
  90. int irqtype;
  91. } stlconf_t;
  92. static stlconf_t stl_brdconf[] = {
  93. /*{ BRD_EASYIO, 0x2a0, 0, 0, 10, 0 },*/
  94. };
  95. static int stl_nrbrds = sizeof(stl_brdconf) / sizeof(stlconf_t);
  96. /*****************************************************************************/
  97. /*
  98. * Define some important driver characteristics. Device major numbers
  99. * allocated as per Linux Device Registry.
  100. */
  101. #ifndef STL_SIOMEMMAJOR
  102. #define STL_SIOMEMMAJOR 28
  103. #endif
  104. #ifndef STL_SERIALMAJOR
  105. #define STL_SERIALMAJOR 24
  106. #endif
  107. #ifndef STL_CALLOUTMAJOR
  108. #define STL_CALLOUTMAJOR 25
  109. #endif
  110. /*
  111. * Set the TX buffer size. Bigger is better, but we don't want
  112. * to chew too much memory with buffers!
  113. */
  114. #define STL_TXBUFLOW 512
  115. #define STL_TXBUFSIZE 4096
  116. /*****************************************************************************/
  117. /*
  118. * Define our local driver identity first. Set up stuff to deal with
  119. * all the local structures required by a serial tty driver.
  120. */
  121. static char *stl_drvtitle = "Stallion Multiport Serial Driver";
  122. static char *stl_drvname = "stallion";
  123. static char *stl_drvversion = "5.6.0";
  124. static struct tty_driver *stl_serial;
  125. /*
  126. * We will need to allocate a temporary write buffer for chars that
  127. * come direct from user space. The problem is that a copy from user
  128. * space might cause a page fault (typically on a system that is
  129. * swapping!). All ports will share one buffer - since if the system
  130. * is already swapping a shared buffer won't make things any worse.
  131. */
  132. static char *stl_tmpwritebuf;
  133. static DECLARE_MUTEX(stl_tmpwritesem);
  134. /*
  135. * Define a local default termios struct. All ports will be created
  136. * with this termios initially. Basically all it defines is a raw port
  137. * at 9600, 8 data bits, 1 stop bit.
  138. */
  139. static struct termios stl_deftermios = {
  140. .c_cflag = (B9600 | CS8 | CREAD | HUPCL | CLOCAL),
  141. .c_cc = INIT_C_CC,
  142. };
  143. /*
  144. * Define global stats structures. Not used often, and can be
  145. * re-used for each stats call.
  146. */
  147. static comstats_t stl_comstats;
  148. static combrd_t stl_brdstats;
  149. static stlbrd_t stl_dummybrd;
  150. static stlport_t stl_dummyport;
  151. /*
  152. * Define global place to put buffer overflow characters.
  153. */
  154. static char stl_unwanted[SC26198_RXFIFOSIZE];
  155. /*****************************************************************************/
  156. static stlbrd_t *stl_brds[STL_MAXBRDS];
  157. /*
  158. * Per board state flags. Used with the state field of the board struct.
  159. * Not really much here!
  160. */
  161. #define BRD_FOUND 0x1
  162. /*
  163. * Define the port structure istate flags. These set of flags are
  164. * modified at interrupt time - so setting and reseting them needs
  165. * to be atomic. Use the bit clear/setting routines for this.
  166. */
  167. #define ASYI_TXBUSY 1
  168. #define ASYI_TXLOW 2
  169. #define ASYI_DCDCHANGE 3
  170. #define ASYI_TXFLOWED 4
  171. /*
  172. * Define an array of board names as printable strings. Handy for
  173. * referencing boards when printing trace and stuff.
  174. */
  175. static char *stl_brdnames[] = {
  176. (char *) NULL,
  177. (char *) NULL,
  178. (char *) NULL,
  179. (char *) NULL,
  180. (char *) NULL,
  181. (char *) NULL,
  182. (char *) NULL,
  183. (char *) NULL,
  184. (char *) NULL,
  185. (char *) NULL,
  186. (char *) NULL,
  187. (char *) NULL,
  188. (char *) NULL,
  189. (char *) NULL,
  190. (char *) NULL,
  191. (char *) NULL,
  192. (char *) NULL,
  193. (char *) NULL,
  194. (char *) NULL,
  195. (char *) NULL,
  196. "EasyIO",
  197. "EC8/32-AT",
  198. "EC8/32-MC",
  199. (char *) NULL,
  200. (char *) NULL,
  201. (char *) NULL,
  202. "EC8/32-PCI",
  203. "EC8/64-PCI",
  204. "EasyIO-PCI",
  205. };
  206. /*****************************************************************************/
  207. /*
  208. * Define some string labels for arguments passed from the module
  209. * load line. These allow for easy board definitions, and easy
  210. * modification of the io, memory and irq resoucres.
  211. */
  212. static int stl_nargs = 0;
  213. static char *board0[4];
  214. static char *board1[4];
  215. static char *board2[4];
  216. static char *board3[4];
  217. static char **stl_brdsp[] = {
  218. (char **) &board0,
  219. (char **) &board1,
  220. (char **) &board2,
  221. (char **) &board3
  222. };
  223. /*
  224. * Define a set of common board names, and types. This is used to
  225. * parse any module arguments.
  226. */
  227. typedef struct stlbrdtype {
  228. char *name;
  229. int type;
  230. } stlbrdtype_t;
  231. static stlbrdtype_t stl_brdstr[] = {
  232. { "easyio", BRD_EASYIO },
  233. { "eio", BRD_EASYIO },
  234. { "20", BRD_EASYIO },
  235. { "ec8/32", BRD_ECH },
  236. { "ec8/32-at", BRD_ECH },
  237. { "ec8/32-isa", BRD_ECH },
  238. { "ech", BRD_ECH },
  239. { "echat", BRD_ECH },
  240. { "21", BRD_ECH },
  241. { "ec8/32-mc", BRD_ECHMC },
  242. { "ec8/32-mca", BRD_ECHMC },
  243. { "echmc", BRD_ECHMC },
  244. { "echmca", BRD_ECHMC },
  245. { "22", BRD_ECHMC },
  246. { "ec8/32-pc", BRD_ECHPCI },
  247. { "ec8/32-pci", BRD_ECHPCI },
  248. { "26", BRD_ECHPCI },
  249. { "ec8/64-pc", BRD_ECH64PCI },
  250. { "ec8/64-pci", BRD_ECH64PCI },
  251. { "ech-pci", BRD_ECH64PCI },
  252. { "echpci", BRD_ECH64PCI },
  253. { "echpc", BRD_ECH64PCI },
  254. { "27", BRD_ECH64PCI },
  255. { "easyio-pc", BRD_EASYIOPCI },
  256. { "easyio-pci", BRD_EASYIOPCI },
  257. { "eio-pci", BRD_EASYIOPCI },
  258. { "eiopci", BRD_EASYIOPCI },
  259. { "28", BRD_EASYIOPCI },
  260. };
  261. /*
  262. * Define the module agruments.
  263. */
  264. MODULE_AUTHOR("Greg Ungerer");
  265. MODULE_DESCRIPTION("Stallion Multiport Serial Driver");
  266. MODULE_LICENSE("GPL");
  267. module_param_array(board0, charp, &stl_nargs, 0);
  268. MODULE_PARM_DESC(board0, "Board 0 config -> name[,ioaddr[,ioaddr2][,irq]]");
  269. module_param_array(board1, charp, &stl_nargs, 0);
  270. MODULE_PARM_DESC(board1, "Board 1 config -> name[,ioaddr[,ioaddr2][,irq]]");
  271. module_param_array(board2, charp, &stl_nargs, 0);
  272. MODULE_PARM_DESC(board2, "Board 2 config -> name[,ioaddr[,ioaddr2][,irq]]");
  273. module_param_array(board3, charp, &stl_nargs, 0);
  274. MODULE_PARM_DESC(board3, "Board 3 config -> name[,ioaddr[,ioaddr2][,irq]]");
  275. /*****************************************************************************/
  276. /*
  277. * Hardware ID bits for the EasyIO and ECH boards. These defines apply
  278. * to the directly accessible io ports of these boards (not the uarts -
  279. * they are in cd1400.h and sc26198.h).
  280. */
  281. #define EIO_8PORTRS 0x04
  282. #define EIO_4PORTRS 0x05
  283. #define EIO_8PORTDI 0x00
  284. #define EIO_8PORTM 0x06
  285. #define EIO_MK3 0x03
  286. #define EIO_IDBITMASK 0x07
  287. #define EIO_BRDMASK 0xf0
  288. #define ID_BRD4 0x10
  289. #define ID_BRD8 0x20
  290. #define ID_BRD16 0x30
  291. #define EIO_INTRPEND 0x08
  292. #define EIO_INTEDGE 0x00
  293. #define EIO_INTLEVEL 0x08
  294. #define EIO_0WS 0x10
  295. #define ECH_ID 0xa0
  296. #define ECH_IDBITMASK 0xe0
  297. #define ECH_BRDENABLE 0x08
  298. #define ECH_BRDDISABLE 0x00
  299. #define ECH_INTENABLE 0x01
  300. #define ECH_INTDISABLE 0x00
  301. #define ECH_INTLEVEL 0x02
  302. #define ECH_INTEDGE 0x00
  303. #define ECH_INTRPEND 0x01
  304. #define ECH_BRDRESET 0x01
  305. #define ECHMC_INTENABLE 0x01
  306. #define ECHMC_BRDRESET 0x02
  307. #define ECH_PNLSTATUS 2
  308. #define ECH_PNL16PORT 0x20
  309. #define ECH_PNLIDMASK 0x07
  310. #define ECH_PNLXPID 0x40
  311. #define ECH_PNLINTRPEND 0x80
  312. #define ECH_ADDR2MASK 0x1e0
  313. /*
  314. * Define the vector mapping bits for the programmable interrupt board
  315. * hardware. These bits encode the interrupt for the board to use - it
  316. * is software selectable (except the EIO-8M).
  317. */
  318. static unsigned char stl_vecmap[] = {
  319. 0xff, 0xff, 0xff, 0x04, 0x06, 0x05, 0xff, 0x07,
  320. 0xff, 0xff, 0x00, 0x02, 0x01, 0xff, 0xff, 0x03
  321. };
  322. /*
  323. * Set up enable and disable macros for the ECH boards. They require
  324. * the secondary io address space to be activated and deactivated.
  325. * This way all ECH boards can share their secondary io region.
  326. * If this is an ECH-PCI board then also need to set the page pointer
  327. * to point to the correct page.
  328. */
  329. #define BRDENABLE(brdnr,pagenr) \
  330. if (stl_brds[(brdnr)]->brdtype == BRD_ECH) \
  331. outb((stl_brds[(brdnr)]->ioctrlval | ECH_BRDENABLE), \
  332. stl_brds[(brdnr)]->ioctrl); \
  333. else if (stl_brds[(brdnr)]->brdtype == BRD_ECHPCI) \
  334. outb((pagenr), stl_brds[(brdnr)]->ioctrl);
  335. #define BRDDISABLE(brdnr) \
  336. if (stl_brds[(brdnr)]->brdtype == BRD_ECH) \
  337. outb((stl_brds[(brdnr)]->ioctrlval | ECH_BRDDISABLE), \
  338. stl_brds[(brdnr)]->ioctrl);
  339. #define STL_CD1400MAXBAUD 230400
  340. #define STL_SC26198MAXBAUD 460800
  341. #define STL_BAUDBASE 115200
  342. #define STL_CLOSEDELAY (5 * HZ / 10)
  343. /*****************************************************************************/
  344. #ifdef CONFIG_PCI
  345. /*
  346. * Define the Stallion PCI vendor and device IDs.
  347. */
  348. #ifndef PCI_VENDOR_ID_STALLION
  349. #define PCI_VENDOR_ID_STALLION 0x124d
  350. #endif
  351. #ifndef PCI_DEVICE_ID_ECHPCI832
  352. #define PCI_DEVICE_ID_ECHPCI832 0x0000
  353. #endif
  354. #ifndef PCI_DEVICE_ID_ECHPCI864
  355. #define PCI_DEVICE_ID_ECHPCI864 0x0002
  356. #endif
  357. #ifndef PCI_DEVICE_ID_EIOPCI
  358. #define PCI_DEVICE_ID_EIOPCI 0x0003
  359. #endif
  360. /*
  361. * Define structure to hold all Stallion PCI boards.
  362. */
  363. typedef struct stlpcibrd {
  364. unsigned short vendid;
  365. unsigned short devid;
  366. int brdtype;
  367. } stlpcibrd_t;
  368. static stlpcibrd_t stl_pcibrds[] = {
  369. { PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_ECHPCI864, BRD_ECH64PCI },
  370. { PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_EIOPCI, BRD_EASYIOPCI },
  371. { PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_ECHPCI832, BRD_ECHPCI },
  372. { PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_87410, BRD_ECHPCI },
  373. };
  374. static int stl_nrpcibrds = sizeof(stl_pcibrds) / sizeof(stlpcibrd_t);
  375. #endif
  376. /*****************************************************************************/
  377. /*
  378. * Define macros to extract a brd/port number from a minor number.
  379. */
  380. #define MINOR2BRD(min) (((min) & 0xc0) >> 6)
  381. #define MINOR2PORT(min) ((min) & 0x3f)
  382. /*
  383. * Define a baud rate table that converts termios baud rate selector
  384. * into the actual baud rate value. All baud rate calculations are
  385. * based on the actual baud rate required.
  386. */
  387. static unsigned int stl_baudrates[] = {
  388. 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
  389. 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
  390. };
  391. /*
  392. * Define some handy local macros...
  393. */
  394. #undef MIN
  395. #define MIN(a,b) (((a) <= (b)) ? (a) : (b))
  396. #undef TOLOWER
  397. #define TOLOWER(x) ((((x) >= 'A') && ((x) <= 'Z')) ? ((x) + 0x20) : (x))
  398. /*****************************************************************************/
  399. /*
  400. * Declare all those functions in this driver!
  401. */
  402. static void stl_argbrds(void);
  403. static int stl_parsebrd(stlconf_t *confp, char **argp);
  404. static unsigned long stl_atol(char *str);
  405. static int stl_init(void);
  406. static int stl_open(struct tty_struct *tty, struct file *filp);
  407. static void stl_close(struct tty_struct *tty, struct file *filp);
  408. static int stl_write(struct tty_struct *tty, const unsigned char *buf, int count);
  409. static void stl_putchar(struct tty_struct *tty, unsigned char ch);
  410. static void stl_flushchars(struct tty_struct *tty);
  411. static int stl_writeroom(struct tty_struct *tty);
  412. static int stl_charsinbuffer(struct tty_struct *tty);
  413. static int stl_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
  414. static void stl_settermios(struct tty_struct *tty, struct termios *old);
  415. static void stl_throttle(struct tty_struct *tty);
  416. static void stl_unthrottle(struct tty_struct *tty);
  417. static void stl_stop(struct tty_struct *tty);
  418. static void stl_start(struct tty_struct *tty);
  419. static void stl_flushbuffer(struct tty_struct *tty);
  420. static void stl_breakctl(struct tty_struct *tty, int state);
  421. static void stl_waituntilsent(struct tty_struct *tty, int timeout);
  422. static void stl_sendxchar(struct tty_struct *tty, char ch);
  423. static void stl_hangup(struct tty_struct *tty);
  424. static int stl_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg);
  425. static int stl_portinfo(stlport_t *portp, int portnr, char *pos);
  426. static int stl_readproc(char *page, char **start, off_t off, int count, int *eof, void *data);
  427. static int stl_brdinit(stlbrd_t *brdp);
  428. static int stl_initports(stlbrd_t *brdp, stlpanel_t *panelp);
  429. static int stl_getserial(stlport_t *portp, struct serial_struct __user *sp);
  430. static int stl_setserial(stlport_t *portp, struct serial_struct __user *sp);
  431. static int stl_getbrdstats(combrd_t __user *bp);
  432. static int stl_getportstats(stlport_t *portp, comstats_t __user *cp);
  433. static int stl_clrportstats(stlport_t *portp, comstats_t __user *cp);
  434. static int stl_getportstruct(stlport_t __user *arg);
  435. static int stl_getbrdstruct(stlbrd_t __user *arg);
  436. static int stl_waitcarrier(stlport_t *portp, struct file *filp);
  437. static int stl_eiointr(stlbrd_t *brdp);
  438. static int stl_echatintr(stlbrd_t *brdp);
  439. static int stl_echmcaintr(stlbrd_t *brdp);
  440. static int stl_echpciintr(stlbrd_t *brdp);
  441. static int stl_echpci64intr(stlbrd_t *brdp);
  442. static void stl_offintr(void *private);
  443. static void *stl_memalloc(int len);
  444. static stlbrd_t *stl_allocbrd(void);
  445. static stlport_t *stl_getport(int brdnr, int panelnr, int portnr);
  446. static inline int stl_initbrds(void);
  447. static inline int stl_initeio(stlbrd_t *brdp);
  448. static inline int stl_initech(stlbrd_t *brdp);
  449. static inline int stl_getbrdnr(void);
  450. #ifdef CONFIG_PCI
  451. static inline int stl_findpcibrds(void);
  452. static inline int stl_initpcibrd(int brdtype, struct pci_dev *devp);
  453. #endif
  454. /*
  455. * CD1400 uart specific handling functions.
  456. */
  457. static void stl_cd1400setreg(stlport_t *portp, int regnr, int value);
  458. static int stl_cd1400getreg(stlport_t *portp, int regnr);
  459. static int stl_cd1400updatereg(stlport_t *portp, int regnr, int value);
  460. static int stl_cd1400panelinit(stlbrd_t *brdp, stlpanel_t *panelp);
  461. static void stl_cd1400portinit(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp);
  462. static void stl_cd1400setport(stlport_t *portp, struct termios *tiosp);
  463. static int stl_cd1400getsignals(stlport_t *portp);
  464. static void stl_cd1400setsignals(stlport_t *portp, int dtr, int rts);
  465. static void stl_cd1400ccrwait(stlport_t *portp);
  466. static void stl_cd1400enablerxtx(stlport_t *portp, int rx, int tx);
  467. static void stl_cd1400startrxtx(stlport_t *portp, int rx, int tx);
  468. static void stl_cd1400disableintrs(stlport_t *portp);
  469. static void stl_cd1400sendbreak(stlport_t *portp, int len);
  470. static void stl_cd1400flowctrl(stlport_t *portp, int state);
  471. static void stl_cd1400sendflow(stlport_t *portp, int state);
  472. static void stl_cd1400flush(stlport_t *portp);
  473. static int stl_cd1400datastate(stlport_t *portp);
  474. static void stl_cd1400eiointr(stlpanel_t *panelp, unsigned int iobase);
  475. static void stl_cd1400echintr(stlpanel_t *panelp, unsigned int iobase);
  476. static void stl_cd1400txisr(stlpanel_t *panelp, int ioaddr);
  477. static void stl_cd1400rxisr(stlpanel_t *panelp, int ioaddr);
  478. static void stl_cd1400mdmisr(stlpanel_t *panelp, int ioaddr);
  479. static inline int stl_cd1400breakisr(stlport_t *portp, int ioaddr);
  480. /*
  481. * SC26198 uart specific handling functions.
  482. */
  483. static void stl_sc26198setreg(stlport_t *portp, int regnr, int value);
  484. static int stl_sc26198getreg(stlport_t *portp, int regnr);
  485. static int stl_sc26198updatereg(stlport_t *portp, int regnr, int value);
  486. static int stl_sc26198getglobreg(stlport_t *portp, int regnr);
  487. static int stl_sc26198panelinit(stlbrd_t *brdp, stlpanel_t *panelp);
  488. static void stl_sc26198portinit(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp);
  489. static void stl_sc26198setport(stlport_t *portp, struct termios *tiosp);
  490. static int stl_sc26198getsignals(stlport_t *portp);
  491. static void stl_sc26198setsignals(stlport_t *portp, int dtr, int rts);
  492. static void stl_sc26198enablerxtx(stlport_t *portp, int rx, int tx);
  493. static void stl_sc26198startrxtx(stlport_t *portp, int rx, int tx);
  494. static void stl_sc26198disableintrs(stlport_t *portp);
  495. static void stl_sc26198sendbreak(stlport_t *portp, int len);
  496. static void stl_sc26198flowctrl(stlport_t *portp, int state);
  497. static void stl_sc26198sendflow(stlport_t *portp, int state);
  498. static void stl_sc26198flush(stlport_t *portp);
  499. static int stl_sc26198datastate(stlport_t *portp);
  500. static void stl_sc26198wait(stlport_t *portp);
  501. static void stl_sc26198txunflow(stlport_t *portp, struct tty_struct *tty);
  502. static void stl_sc26198intr(stlpanel_t *panelp, unsigned int iobase);
  503. static void stl_sc26198txisr(stlport_t *port);
  504. static void stl_sc26198rxisr(stlport_t *port, unsigned int iack);
  505. static void stl_sc26198rxbadch(stlport_t *portp, unsigned char status, char ch);
  506. static void stl_sc26198rxbadchars(stlport_t *portp);
  507. static void stl_sc26198otherisr(stlport_t *port, unsigned int iack);
  508. /*****************************************************************************/
  509. /*
  510. * Generic UART support structure.
  511. */
  512. typedef struct uart {
  513. int (*panelinit)(stlbrd_t *brdp, stlpanel_t *panelp);
  514. void (*portinit)(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp);
  515. void (*setport)(stlport_t *portp, struct termios *tiosp);
  516. int (*getsignals)(stlport_t *portp);
  517. void (*setsignals)(stlport_t *portp, int dtr, int rts);
  518. void (*enablerxtx)(stlport_t *portp, int rx, int tx);
  519. void (*startrxtx)(stlport_t *portp, int rx, int tx);
  520. void (*disableintrs)(stlport_t *portp);
  521. void (*sendbreak)(stlport_t *portp, int len);
  522. void (*flowctrl)(stlport_t *portp, int state);
  523. void (*sendflow)(stlport_t *portp, int state);
  524. void (*flush)(stlport_t *portp);
  525. int (*datastate)(stlport_t *portp);
  526. void (*intr)(stlpanel_t *panelp, unsigned int iobase);
  527. } uart_t;
  528. /*
  529. * Define some macros to make calling these functions nice and clean.
  530. */
  531. #define stl_panelinit (* ((uart_t *) panelp->uartp)->panelinit)
  532. #define stl_portinit (* ((uart_t *) portp->uartp)->portinit)
  533. #define stl_setport (* ((uart_t *) portp->uartp)->setport)
  534. #define stl_getsignals (* ((uart_t *) portp->uartp)->getsignals)
  535. #define stl_setsignals (* ((uart_t *) portp->uartp)->setsignals)
  536. #define stl_enablerxtx (* ((uart_t *) portp->uartp)->enablerxtx)
  537. #define stl_startrxtx (* ((uart_t *) portp->uartp)->startrxtx)
  538. #define stl_disableintrs (* ((uart_t *) portp->uartp)->disableintrs)
  539. #define stl_sendbreak (* ((uart_t *) portp->uartp)->sendbreak)
  540. #define stl_flowctrl (* ((uart_t *) portp->uartp)->flowctrl)
  541. #define stl_sendflow (* ((uart_t *) portp->uartp)->sendflow)
  542. #define stl_flush (* ((uart_t *) portp->uartp)->flush)
  543. #define stl_datastate (* ((uart_t *) portp->uartp)->datastate)
  544. /*****************************************************************************/
  545. /*
  546. * CD1400 UART specific data initialization.
  547. */
  548. static uart_t stl_cd1400uart = {
  549. stl_cd1400panelinit,
  550. stl_cd1400portinit,
  551. stl_cd1400setport,
  552. stl_cd1400getsignals,
  553. stl_cd1400setsignals,
  554. stl_cd1400enablerxtx,
  555. stl_cd1400startrxtx,
  556. stl_cd1400disableintrs,
  557. stl_cd1400sendbreak,
  558. stl_cd1400flowctrl,
  559. stl_cd1400sendflow,
  560. stl_cd1400flush,
  561. stl_cd1400datastate,
  562. stl_cd1400eiointr
  563. };
  564. /*
  565. * Define the offsets within the register bank of a cd1400 based panel.
  566. * These io address offsets are common to the EasyIO board as well.
  567. */
  568. #define EREG_ADDR 0
  569. #define EREG_DATA 4
  570. #define EREG_RXACK 5
  571. #define EREG_TXACK 6
  572. #define EREG_MDACK 7
  573. #define EREG_BANKSIZE 8
  574. #define CD1400_CLK 25000000
  575. #define CD1400_CLK8M 20000000
  576. /*
  577. * Define the cd1400 baud rate clocks. These are used when calculating
  578. * what clock and divisor to use for the required baud rate. Also
  579. * define the maximum baud rate allowed, and the default base baud.
  580. */
  581. static int stl_cd1400clkdivs[] = {
  582. CD1400_CLK0, CD1400_CLK1, CD1400_CLK2, CD1400_CLK3, CD1400_CLK4
  583. };
  584. /*****************************************************************************/
  585. /*
  586. * SC26198 UART specific data initization.
  587. */
  588. static uart_t stl_sc26198uart = {
  589. stl_sc26198panelinit,
  590. stl_sc26198portinit,
  591. stl_sc26198setport,
  592. stl_sc26198getsignals,
  593. stl_sc26198setsignals,
  594. stl_sc26198enablerxtx,
  595. stl_sc26198startrxtx,
  596. stl_sc26198disableintrs,
  597. stl_sc26198sendbreak,
  598. stl_sc26198flowctrl,
  599. stl_sc26198sendflow,
  600. stl_sc26198flush,
  601. stl_sc26198datastate,
  602. stl_sc26198intr
  603. };
  604. /*
  605. * Define the offsets within the register bank of a sc26198 based panel.
  606. */
  607. #define XP_DATA 0
  608. #define XP_ADDR 1
  609. #define XP_MODID 2
  610. #define XP_STATUS 2
  611. #define XP_IACK 3
  612. #define XP_BANKSIZE 4
  613. /*
  614. * Define the sc26198 baud rate table. Offsets within the table
  615. * represent the actual baud rate selector of sc26198 registers.
  616. */
  617. static unsigned int sc26198_baudtable[] = {
  618. 50, 75, 150, 200, 300, 450, 600, 900, 1200, 1800, 2400, 3600,
  619. 4800, 7200, 9600, 14400, 19200, 28800, 38400, 57600, 115200,
  620. 230400, 460800, 921600
  621. };
  622. #define SC26198_NRBAUDS (sizeof(sc26198_baudtable) / sizeof(unsigned int))
  623. /*****************************************************************************/
  624. /*
  625. * Define the driver info for a user level control device. Used mainly
  626. * to get at port stats - only not using the port device itself.
  627. */
  628. static struct file_operations stl_fsiomem = {
  629. .owner = THIS_MODULE,
  630. .ioctl = stl_memioctl,
  631. };
  632. /*****************************************************************************/
  633. static struct class *stallion_class;
  634. /*
  635. * Loadable module initialization stuff.
  636. */
  637. static int __init stallion_module_init(void)
  638. {
  639. unsigned long flags;
  640. #ifdef DEBUG
  641. printk("init_module()\n");
  642. #endif
  643. save_flags(flags);
  644. cli();
  645. stl_init();
  646. restore_flags(flags);
  647. return(0);
  648. }
  649. /*****************************************************************************/
  650. static void __exit stallion_module_exit(void)
  651. {
  652. stlbrd_t *brdp;
  653. stlpanel_t *panelp;
  654. stlport_t *portp;
  655. unsigned long flags;
  656. int i, j, k;
  657. #ifdef DEBUG
  658. printk("cleanup_module()\n");
  659. #endif
  660. printk(KERN_INFO "Unloading %s: version %s\n", stl_drvtitle,
  661. stl_drvversion);
  662. save_flags(flags);
  663. cli();
  664. /*
  665. * Free up all allocated resources used by the ports. This includes
  666. * memory and interrupts. As part of this process we will also do
  667. * a hangup on every open port - to try to flush out any processes
  668. * hanging onto ports.
  669. */
  670. i = tty_unregister_driver(stl_serial);
  671. put_tty_driver(stl_serial);
  672. if (i) {
  673. printk("STALLION: failed to un-register tty driver, "
  674. "errno=%d\n", -i);
  675. restore_flags(flags);
  676. return;
  677. }
  678. for (i = 0; i < 4; i++) {
  679. devfs_remove("staliomem/%d", i);
  680. class_device_destroy(stallion_class, MKDEV(STL_SIOMEMMAJOR, i));
  681. }
  682. devfs_remove("staliomem");
  683. if ((i = unregister_chrdev(STL_SIOMEMMAJOR, "staliomem")))
  684. printk("STALLION: failed to un-register serial memory device, "
  685. "errno=%d\n", -i);
  686. class_destroy(stallion_class);
  687. if (stl_tmpwritebuf != (char *) NULL)
  688. kfree(stl_tmpwritebuf);
  689. for (i = 0; (i < stl_nrbrds); i++) {
  690. if ((brdp = stl_brds[i]) == (stlbrd_t *) NULL)
  691. continue;
  692. free_irq(brdp->irq, brdp);
  693. for (j = 0; (j < STL_MAXPANELS); j++) {
  694. panelp = brdp->panels[j];
  695. if (panelp == (stlpanel_t *) NULL)
  696. continue;
  697. for (k = 0; (k < STL_PORTSPERPANEL); k++) {
  698. portp = panelp->ports[k];
  699. if (portp == (stlport_t *) NULL)
  700. continue;
  701. if (portp->tty != (struct tty_struct *) NULL)
  702. stl_hangup(portp->tty);
  703. if (portp->tx.buf != (char *) NULL)
  704. kfree(portp->tx.buf);
  705. kfree(portp);
  706. }
  707. kfree(panelp);
  708. }
  709. release_region(brdp->ioaddr1, brdp->iosize1);
  710. if (brdp->iosize2 > 0)
  711. release_region(brdp->ioaddr2, brdp->iosize2);
  712. kfree(brdp);
  713. stl_brds[i] = (stlbrd_t *) NULL;
  714. }
  715. restore_flags(flags);
  716. }
  717. module_init(stallion_module_init);
  718. module_exit(stallion_module_exit);
  719. /*****************************************************************************/
  720. /*
  721. * Check for any arguments passed in on the module load command line.
  722. */
  723. static void stl_argbrds(void)
  724. {
  725. stlconf_t conf;
  726. stlbrd_t *brdp;
  727. int i;
  728. #ifdef DEBUG
  729. printk("stl_argbrds()\n");
  730. #endif
  731. for (i = stl_nrbrds; (i < stl_nargs); i++) {
  732. memset(&conf, 0, sizeof(conf));
  733. if (stl_parsebrd(&conf, stl_brdsp[i]) == 0)
  734. continue;
  735. if ((brdp = stl_allocbrd()) == (stlbrd_t *) NULL)
  736. continue;
  737. stl_nrbrds = i + 1;
  738. brdp->brdnr = i;
  739. brdp->brdtype = conf.brdtype;
  740. brdp->ioaddr1 = conf.ioaddr1;
  741. brdp->ioaddr2 = conf.ioaddr2;
  742. brdp->irq = conf.irq;
  743. brdp->irqtype = conf.irqtype;
  744. stl_brdinit(brdp);
  745. }
  746. }
  747. /*****************************************************************************/
  748. /*
  749. * Convert an ascii string number into an unsigned long.
  750. */
  751. static unsigned long stl_atol(char *str)
  752. {
  753. unsigned long val;
  754. int base, c;
  755. char *sp;
  756. val = 0;
  757. sp = str;
  758. if ((*sp == '0') && (*(sp+1) == 'x')) {
  759. base = 16;
  760. sp += 2;
  761. } else if (*sp == '0') {
  762. base = 8;
  763. sp++;
  764. } else {
  765. base = 10;
  766. }
  767. for (; (*sp != 0); sp++) {
  768. c = (*sp > '9') ? (TOLOWER(*sp) - 'a' + 10) : (*sp - '0');
  769. if ((c < 0) || (c >= base)) {
  770. printk("STALLION: invalid argument %s\n", str);
  771. val = 0;
  772. break;
  773. }
  774. val = (val * base) + c;
  775. }
  776. return(val);
  777. }
  778. /*****************************************************************************/
  779. /*
  780. * Parse the supplied argument string, into the board conf struct.
  781. */
  782. static int stl_parsebrd(stlconf_t *confp, char **argp)
  783. {
  784. char *sp;
  785. int nrbrdnames, i;
  786. #ifdef DEBUG
  787. printk("stl_parsebrd(confp=%x,argp=%x)\n", (int) confp, (int) argp);
  788. #endif
  789. if ((argp[0] == (char *) NULL) || (*argp[0] == 0))
  790. return(0);
  791. for (sp = argp[0], i = 0; ((*sp != 0) && (i < 25)); sp++, i++)
  792. *sp = TOLOWER(*sp);
  793. nrbrdnames = sizeof(stl_brdstr) / sizeof(stlbrdtype_t);
  794. for (i = 0; (i < nrbrdnames); i++) {
  795. if (strcmp(stl_brdstr[i].name, argp[0]) == 0)
  796. break;
  797. }
  798. if (i >= nrbrdnames) {
  799. printk("STALLION: unknown board name, %s?\n", argp[0]);
  800. return(0);
  801. }
  802. confp->brdtype = stl_brdstr[i].type;
  803. i = 1;
  804. if ((argp[i] != (char *) NULL) && (*argp[i] != 0))
  805. confp->ioaddr1 = stl_atol(argp[i]);
  806. i++;
  807. if (confp->brdtype == BRD_ECH) {
  808. if ((argp[i] != (char *) NULL) && (*argp[i] != 0))
  809. confp->ioaddr2 = stl_atol(argp[i]);
  810. i++;
  811. }
  812. if ((argp[i] != (char *) NULL) && (*argp[i] != 0))
  813. confp->irq = stl_atol(argp[i]);
  814. return(1);
  815. }
  816. /*****************************************************************************/
  817. /*
  818. * Local driver kernel memory allocation routine.
  819. */
  820. static void *stl_memalloc(int len)
  821. {
  822. return((void *) kmalloc(len, GFP_KERNEL));
  823. }
  824. /*****************************************************************************/
  825. /*
  826. * Allocate a new board structure. Fill out the basic info in it.
  827. */
  828. static stlbrd_t *stl_allocbrd(void)
  829. {
  830. stlbrd_t *brdp;
  831. brdp = (stlbrd_t *) stl_memalloc(sizeof(stlbrd_t));
  832. if (brdp == (stlbrd_t *) NULL) {
  833. printk("STALLION: failed to allocate memory (size=%d)\n",
  834. sizeof(stlbrd_t));
  835. return((stlbrd_t *) NULL);
  836. }
  837. memset(brdp, 0, sizeof(stlbrd_t));
  838. brdp->magic = STL_BOARDMAGIC;
  839. return(brdp);
  840. }
  841. /*****************************************************************************/
  842. static int stl_open(struct tty_struct *tty, struct file *filp)
  843. {
  844. stlport_t *portp;
  845. stlbrd_t *brdp;
  846. unsigned int minordev;
  847. int brdnr, panelnr, portnr, rc;
  848. #ifdef DEBUG
  849. printk("stl_open(tty=%x,filp=%x): device=%s\n", (int) tty,
  850. (int) filp, tty->name);
  851. #endif
  852. minordev = tty->index;
  853. brdnr = MINOR2BRD(minordev);
  854. if (brdnr >= stl_nrbrds)
  855. return(-ENODEV);
  856. brdp = stl_brds[brdnr];
  857. if (brdp == (stlbrd_t *) NULL)
  858. return(-ENODEV);
  859. minordev = MINOR2PORT(minordev);
  860. for (portnr = -1, panelnr = 0; (panelnr < STL_MAXPANELS); panelnr++) {
  861. if (brdp->panels[panelnr] == (stlpanel_t *) NULL)
  862. break;
  863. if (minordev < brdp->panels[panelnr]->nrports) {
  864. portnr = minordev;
  865. break;
  866. }
  867. minordev -= brdp->panels[panelnr]->nrports;
  868. }
  869. if (portnr < 0)
  870. return(-ENODEV);
  871. portp = brdp->panels[panelnr]->ports[portnr];
  872. if (portp == (stlport_t *) NULL)
  873. return(-ENODEV);
  874. /*
  875. * On the first open of the device setup the port hardware, and
  876. * initialize the per port data structure.
  877. */
  878. portp->tty = tty;
  879. tty->driver_data = portp;
  880. portp->refcount++;
  881. if ((portp->flags & ASYNC_INITIALIZED) == 0) {
  882. if (portp->tx.buf == (char *) NULL) {
  883. portp->tx.buf = (char *) stl_memalloc(STL_TXBUFSIZE);
  884. if (portp->tx.buf == (char *) NULL)
  885. return(-ENOMEM);
  886. portp->tx.head = portp->tx.buf;
  887. portp->tx.tail = portp->tx.buf;
  888. }
  889. stl_setport(portp, tty->termios);
  890. portp->sigs = stl_getsignals(portp);
  891. stl_setsignals(portp, 1, 1);
  892. stl_enablerxtx(portp, 1, 1);
  893. stl_startrxtx(portp, 1, 0);
  894. clear_bit(TTY_IO_ERROR, &tty->flags);
  895. portp->flags |= ASYNC_INITIALIZED;
  896. }
  897. /*
  898. * Check if this port is in the middle of closing. If so then wait
  899. * until it is closed then return error status, based on flag settings.
  900. * The sleep here does not need interrupt protection since the wakeup
  901. * for it is done with the same context.
  902. */
  903. if (portp->flags & ASYNC_CLOSING) {
  904. interruptible_sleep_on(&portp->close_wait);
  905. if (portp->flags & ASYNC_HUP_NOTIFY)
  906. return(-EAGAIN);
  907. return(-ERESTARTSYS);
  908. }
  909. /*
  910. * Based on type of open being done check if it can overlap with any
  911. * previous opens still in effect. If we are a normal serial device
  912. * then also we might have to wait for carrier.
  913. */
  914. if (!(filp->f_flags & O_NONBLOCK)) {
  915. if ((rc = stl_waitcarrier(portp, filp)) != 0)
  916. return(rc);
  917. }
  918. portp->flags |= ASYNC_NORMAL_ACTIVE;
  919. return(0);
  920. }
  921. /*****************************************************************************/
  922. /*
  923. * Possibly need to wait for carrier (DCD signal) to come high. Say
  924. * maybe because if we are clocal then we don't need to wait...
  925. */
  926. static int stl_waitcarrier(stlport_t *portp, struct file *filp)
  927. {
  928. unsigned long flags;
  929. int rc, doclocal;
  930. #ifdef DEBUG
  931. printk("stl_waitcarrier(portp=%x,filp=%x)\n", (int) portp, (int) filp);
  932. #endif
  933. rc = 0;
  934. doclocal = 0;
  935. if (portp->tty->termios->c_cflag & CLOCAL)
  936. doclocal++;
  937. save_flags(flags);
  938. cli();
  939. portp->openwaitcnt++;
  940. if (! tty_hung_up_p(filp))
  941. portp->refcount--;
  942. for (;;) {
  943. stl_setsignals(portp, 1, 1);
  944. if (tty_hung_up_p(filp) ||
  945. ((portp->flags & ASYNC_INITIALIZED) == 0)) {
  946. if (portp->flags & ASYNC_HUP_NOTIFY)
  947. rc = -EBUSY;
  948. else
  949. rc = -ERESTARTSYS;
  950. break;
  951. }
  952. if (((portp->flags & ASYNC_CLOSING) == 0) &&
  953. (doclocal || (portp->sigs & TIOCM_CD))) {
  954. break;
  955. }
  956. if (signal_pending(current)) {
  957. rc = -ERESTARTSYS;
  958. break;
  959. }
  960. interruptible_sleep_on(&portp->open_wait);
  961. }
  962. if (! tty_hung_up_p(filp))
  963. portp->refcount++;
  964. portp->openwaitcnt--;
  965. restore_flags(flags);
  966. return(rc);
  967. }
  968. /*****************************************************************************/
  969. static void stl_close(struct tty_struct *tty, struct file *filp)
  970. {
  971. stlport_t *portp;
  972. unsigned long flags;
  973. #ifdef DEBUG
  974. printk("stl_close(tty=%x,filp=%x)\n", (int) tty, (int) filp);
  975. #endif
  976. portp = tty->driver_data;
  977. if (portp == (stlport_t *) NULL)
  978. return;
  979. save_flags(flags);
  980. cli();
  981. if (tty_hung_up_p(filp)) {
  982. restore_flags(flags);
  983. return;
  984. }
  985. if ((tty->count == 1) && (portp->refcount != 1))
  986. portp->refcount = 1;
  987. if (portp->refcount-- > 1) {
  988. restore_flags(flags);
  989. return;
  990. }
  991. portp->refcount = 0;
  992. portp->flags |= ASYNC_CLOSING;
  993. /*
  994. * May want to wait for any data to drain before closing. The BUSY
  995. * flag keeps track of whether we are still sending or not - it is
  996. * very accurate for the cd1400, not quite so for the sc26198.
  997. * (The sc26198 has no "end-of-data" interrupt only empty FIFO)
  998. */
  999. tty->closing = 1;
  1000. if (portp->closing_wait != ASYNC_CLOSING_WAIT_NONE)
  1001. tty_wait_until_sent(tty, portp->closing_wait);
  1002. stl_waituntilsent(tty, (HZ / 2));
  1003. portp->flags &= ~ASYNC_INITIALIZED;
  1004. stl_disableintrs(portp);
  1005. if (tty->termios->c_cflag & HUPCL)
  1006. stl_setsignals(portp, 0, 0);
  1007. stl_enablerxtx(portp, 0, 0);
  1008. stl_flushbuffer(tty);
  1009. portp->istate = 0;
  1010. if (portp->tx.buf != (char *) NULL) {
  1011. kfree(portp->tx.buf);
  1012. portp->tx.buf = (char *) NULL;
  1013. portp->tx.head = (char *) NULL;
  1014. portp->tx.tail = (char *) NULL;
  1015. }
  1016. set_bit(TTY_IO_ERROR, &tty->flags);
  1017. tty_ldisc_flush(tty);
  1018. tty->closing = 0;
  1019. portp->tty = (struct tty_struct *) NULL;
  1020. if (portp->openwaitcnt) {
  1021. if (portp->close_delay)
  1022. msleep_interruptible(jiffies_to_msecs(portp->close_delay));
  1023. wake_up_interruptible(&portp->open_wait);
  1024. }
  1025. portp->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
  1026. wake_up_interruptible(&portp->close_wait);
  1027. restore_flags(flags);
  1028. }
  1029. /*****************************************************************************/
  1030. /*
  1031. * Write routine. Take data and stuff it in to the TX ring queue.
  1032. * If transmit interrupts are not running then start them.
  1033. */
  1034. static int stl_write(struct tty_struct *tty, const unsigned char *buf, int count)
  1035. {
  1036. stlport_t *portp;
  1037. unsigned int len, stlen;
  1038. unsigned char *chbuf;
  1039. char *head, *tail;
  1040. #ifdef DEBUG
  1041. printk("stl_write(tty=%x,buf=%x,count=%d)\n",
  1042. (int) tty, (int) buf, count);
  1043. #endif
  1044. if ((tty == (struct tty_struct *) NULL) ||
  1045. (stl_tmpwritebuf == (char *) NULL))
  1046. return(0);
  1047. portp = tty->driver_data;
  1048. if (portp == (stlport_t *) NULL)
  1049. return(0);
  1050. if (portp->tx.buf == (char *) NULL)
  1051. return(0);
  1052. /*
  1053. * If copying direct from user space we must cater for page faults,
  1054. * causing us to "sleep" here for a while. To handle this copy in all
  1055. * the data we need now, into a local buffer. Then when we got it all
  1056. * copy it into the TX buffer.
  1057. */
  1058. chbuf = (unsigned char *) buf;
  1059. head = portp->tx.head;
  1060. tail = portp->tx.tail;
  1061. if (head >= tail) {
  1062. len = STL_TXBUFSIZE - (head - tail) - 1;
  1063. stlen = STL_TXBUFSIZE - (head - portp->tx.buf);
  1064. } else {
  1065. len = tail - head - 1;
  1066. stlen = len;
  1067. }
  1068. len = MIN(len, count);
  1069. count = 0;
  1070. while (len > 0) {
  1071. stlen = MIN(len, stlen);
  1072. memcpy(head, chbuf, stlen);
  1073. len -= stlen;
  1074. chbuf += stlen;
  1075. count += stlen;
  1076. head += stlen;
  1077. if (head >= (portp->tx.buf + STL_TXBUFSIZE)) {
  1078. head = portp->tx.buf;
  1079. stlen = tail - head;
  1080. }
  1081. }
  1082. portp->tx.head = head;
  1083. clear_bit(ASYI_TXLOW, &portp->istate);
  1084. stl_startrxtx(portp, -1, 1);
  1085. return(count);
  1086. }
  1087. /*****************************************************************************/
  1088. static void stl_putchar(struct tty_struct *tty, unsigned char ch)
  1089. {
  1090. stlport_t *portp;
  1091. unsigned int len;
  1092. char *head, *tail;
  1093. #ifdef DEBUG
  1094. printk("stl_putchar(tty=%x,ch=%x)\n", (int) tty, (int) ch);
  1095. #endif
  1096. if (tty == (struct tty_struct *) NULL)
  1097. return;
  1098. portp = tty->driver_data;
  1099. if (portp == (stlport_t *) NULL)
  1100. return;
  1101. if (portp->tx.buf == (char *) NULL)
  1102. return;
  1103. head = portp->tx.head;
  1104. tail = portp->tx.tail;
  1105. len = (head >= tail) ? (STL_TXBUFSIZE - (head - tail)) : (tail - head);
  1106. len--;
  1107. if (len > 0) {
  1108. *head++ = ch;
  1109. if (head >= (portp->tx.buf + STL_TXBUFSIZE))
  1110. head = portp->tx.buf;
  1111. }
  1112. portp->tx.head = head;
  1113. }
  1114. /*****************************************************************************/
  1115. /*
  1116. * If there are any characters in the buffer then make sure that TX
  1117. * interrupts are on and get'em out. Normally used after the putchar
  1118. * routine has been called.
  1119. */
  1120. static void stl_flushchars(struct tty_struct *tty)
  1121. {
  1122. stlport_t *portp;
  1123. #ifdef DEBUG
  1124. printk("stl_flushchars(tty=%x)\n", (int) tty);
  1125. #endif
  1126. if (tty == (struct tty_struct *) NULL)
  1127. return;
  1128. portp = tty->driver_data;
  1129. if (portp == (stlport_t *) NULL)
  1130. return;
  1131. if (portp->tx.buf == (char *) NULL)
  1132. return;
  1133. #if 0
  1134. if (tty->stopped || tty->hw_stopped ||
  1135. (portp->tx.head == portp->tx.tail))
  1136. return;
  1137. #endif
  1138. stl_startrxtx(portp, -1, 1);
  1139. }
  1140. /*****************************************************************************/
  1141. static int stl_writeroom(struct tty_struct *tty)
  1142. {
  1143. stlport_t *portp;
  1144. char *head, *tail;
  1145. #ifdef DEBUG
  1146. printk("stl_writeroom(tty=%x)\n", (int) tty);
  1147. #endif
  1148. if (tty == (struct tty_struct *) NULL)
  1149. return(0);
  1150. portp = tty->driver_data;
  1151. if (portp == (stlport_t *) NULL)
  1152. return(0);
  1153. if (portp->tx.buf == (char *) NULL)
  1154. return(0);
  1155. head = portp->tx.head;
  1156. tail = portp->tx.tail;
  1157. return((head >= tail) ? (STL_TXBUFSIZE - (head - tail) - 1) : (tail - head - 1));
  1158. }
  1159. /*****************************************************************************/
  1160. /*
  1161. * Return number of chars in the TX buffer. Normally we would just
  1162. * calculate the number of chars in the buffer and return that, but if
  1163. * the buffer is empty and TX interrupts are still on then we return
  1164. * that the buffer still has 1 char in it. This way whoever called us
  1165. * will not think that ALL chars have drained - since the UART still
  1166. * must have some chars in it (we are busy after all).
  1167. */
  1168. static int stl_charsinbuffer(struct tty_struct *tty)
  1169. {
  1170. stlport_t *portp;
  1171. unsigned int size;
  1172. char *head, *tail;
  1173. #ifdef DEBUG
  1174. printk("stl_charsinbuffer(tty=%x)\n", (int) tty);
  1175. #endif
  1176. if (tty == (struct tty_struct *) NULL)
  1177. return(0);
  1178. portp = tty->driver_data;
  1179. if (portp == (stlport_t *) NULL)
  1180. return(0);
  1181. if (portp->tx.buf == (char *) NULL)
  1182. return(0);
  1183. head = portp->tx.head;
  1184. tail = portp->tx.tail;
  1185. size = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
  1186. if ((size == 0) && test_bit(ASYI_TXBUSY, &portp->istate))
  1187. size = 1;
  1188. return(size);
  1189. }
  1190. /*****************************************************************************/
  1191. /*
  1192. * Generate the serial struct info.
  1193. */
  1194. static int stl_getserial(stlport_t *portp, struct serial_struct __user *sp)
  1195. {
  1196. struct serial_struct sio;
  1197. stlbrd_t *brdp;
  1198. #ifdef DEBUG
  1199. printk("stl_getserial(portp=%x,sp=%x)\n", (int) portp, (int) sp);
  1200. #endif
  1201. memset(&sio, 0, sizeof(struct serial_struct));
  1202. sio.line = portp->portnr;
  1203. sio.port = portp->ioaddr;
  1204. sio.flags = portp->flags;
  1205. sio.baud_base = portp->baud_base;
  1206. sio.close_delay = portp->close_delay;
  1207. sio.closing_wait = portp->closing_wait;
  1208. sio.custom_divisor = portp->custom_divisor;
  1209. sio.hub6 = 0;
  1210. if (portp->uartp == &stl_cd1400uart) {
  1211. sio.type = PORT_CIRRUS;
  1212. sio.xmit_fifo_size = CD1400_TXFIFOSIZE;
  1213. } else {
  1214. sio.type = PORT_UNKNOWN;
  1215. sio.xmit_fifo_size = SC26198_TXFIFOSIZE;
  1216. }
  1217. brdp = stl_brds[portp->brdnr];
  1218. if (brdp != (stlbrd_t *) NULL)
  1219. sio.irq = brdp->irq;
  1220. return copy_to_user(sp, &sio, sizeof(struct serial_struct)) ? -EFAULT : 0;
  1221. }
  1222. /*****************************************************************************/
  1223. /*
  1224. * Set port according to the serial struct info.
  1225. * At this point we do not do any auto-configure stuff, so we will
  1226. * just quietly ignore any requests to change irq, etc.
  1227. */
  1228. static int stl_setserial(stlport_t *portp, struct serial_struct __user *sp)
  1229. {
  1230. struct serial_struct sio;
  1231. #ifdef DEBUG
  1232. printk("stl_setserial(portp=%x,sp=%x)\n", (int) portp, (int) sp);
  1233. #endif
  1234. if (copy_from_user(&sio, sp, sizeof(struct serial_struct)))
  1235. return -EFAULT;
  1236. if (!capable(CAP_SYS_ADMIN)) {
  1237. if ((sio.baud_base != portp->baud_base) ||
  1238. (sio.close_delay != portp->close_delay) ||
  1239. ((sio.flags & ~ASYNC_USR_MASK) !=
  1240. (portp->flags & ~ASYNC_USR_MASK)))
  1241. return(-EPERM);
  1242. }
  1243. portp->flags = (portp->flags & ~ASYNC_USR_MASK) |
  1244. (sio.flags & ASYNC_USR_MASK);
  1245. portp->baud_base = sio.baud_base;
  1246. portp->close_delay = sio.close_delay;
  1247. portp->closing_wait = sio.closing_wait;
  1248. portp->custom_divisor = sio.custom_divisor;
  1249. stl_setport(portp, portp->tty->termios);
  1250. return(0);
  1251. }
  1252. /*****************************************************************************/
  1253. static int stl_tiocmget(struct tty_struct *tty, struct file *file)
  1254. {
  1255. stlport_t *portp;
  1256. if (tty == (struct tty_struct *) NULL)
  1257. return(-ENODEV);
  1258. portp = tty->driver_data;
  1259. if (portp == (stlport_t *) NULL)
  1260. return(-ENODEV);
  1261. if (tty->flags & (1 << TTY_IO_ERROR))
  1262. return(-EIO);
  1263. return stl_getsignals(portp);
  1264. }
  1265. static int stl_tiocmset(struct tty_struct *tty, struct file *file,
  1266. unsigned int set, unsigned int clear)
  1267. {
  1268. stlport_t *portp;
  1269. int rts = -1, dtr = -1;
  1270. if (tty == (struct tty_struct *) NULL)
  1271. return(-ENODEV);
  1272. portp = tty->driver_data;
  1273. if (portp == (stlport_t *) NULL)
  1274. return(-ENODEV);
  1275. if (tty->flags & (1 << TTY_IO_ERROR))
  1276. return(-EIO);
  1277. if (set & TIOCM_RTS)
  1278. rts = 1;
  1279. if (set & TIOCM_DTR)
  1280. dtr = 1;
  1281. if (clear & TIOCM_RTS)
  1282. rts = 0;
  1283. if (clear & TIOCM_DTR)
  1284. dtr = 0;
  1285. stl_setsignals(portp, dtr, rts);
  1286. return 0;
  1287. }
  1288. static int stl_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
  1289. {
  1290. stlport_t *portp;
  1291. unsigned int ival;
  1292. int rc;
  1293. void __user *argp = (void __user *)arg;
  1294. #ifdef DEBUG
  1295. printk("stl_ioctl(tty=%x,file=%x,cmd=%x,arg=%x)\n",
  1296. (int) tty, (int) file, cmd, (int) arg);
  1297. #endif
  1298. if (tty == (struct tty_struct *) NULL)
  1299. return(-ENODEV);
  1300. portp = tty->driver_data;
  1301. if (portp == (stlport_t *) NULL)
  1302. return(-ENODEV);
  1303. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  1304. (cmd != COM_GETPORTSTATS) && (cmd != COM_CLRPORTSTATS)) {
  1305. if (tty->flags & (1 << TTY_IO_ERROR))
  1306. return(-EIO);
  1307. }
  1308. rc = 0;
  1309. switch (cmd) {
  1310. case TIOCGSOFTCAR:
  1311. rc = put_user(((tty->termios->c_cflag & CLOCAL) ? 1 : 0),
  1312. (unsigned __user *) argp);
  1313. break;
  1314. case TIOCSSOFTCAR:
  1315. if (get_user(ival, (unsigned int __user *) arg))
  1316. return -EFAULT;
  1317. tty->termios->c_cflag =
  1318. (tty->termios->c_cflag & ~CLOCAL) |
  1319. (ival ? CLOCAL : 0);
  1320. break;
  1321. case TIOCGSERIAL:
  1322. rc = stl_getserial(portp, argp);
  1323. break;
  1324. case TIOCSSERIAL:
  1325. rc = stl_setserial(portp, argp);
  1326. break;
  1327. case COM_GETPORTSTATS:
  1328. rc = stl_getportstats(portp, argp);
  1329. break;
  1330. case COM_CLRPORTSTATS:
  1331. rc = stl_clrportstats(portp, argp);
  1332. break;
  1333. case TIOCSERCONFIG:
  1334. case TIOCSERGWILD:
  1335. case TIOCSERSWILD:
  1336. case TIOCSERGETLSR:
  1337. case TIOCSERGSTRUCT:
  1338. case TIOCSERGETMULTI:
  1339. case TIOCSERSETMULTI:
  1340. default:
  1341. rc = -ENOIOCTLCMD;
  1342. break;
  1343. }
  1344. return(rc);
  1345. }
  1346. /*****************************************************************************/
  1347. static void stl_settermios(struct tty_struct *tty, struct termios *old)
  1348. {
  1349. stlport_t *portp;
  1350. struct termios *tiosp;
  1351. #ifdef DEBUG
  1352. printk("stl_settermios(tty=%x,old=%x)\n", (int) tty, (int) old);
  1353. #endif
  1354. if (tty == (struct tty_struct *) NULL)
  1355. return;
  1356. portp = tty->driver_data;
  1357. if (portp == (stlport_t *) NULL)
  1358. return;
  1359. tiosp = tty->termios;
  1360. if ((tiosp->c_cflag == old->c_cflag) &&
  1361. (tiosp->c_iflag == old->c_iflag))
  1362. return;
  1363. stl_setport(portp, tiosp);
  1364. stl_setsignals(portp, ((tiosp->c_cflag & (CBAUD & ~CBAUDEX)) ? 1 : 0),
  1365. -1);
  1366. if ((old->c_cflag & CRTSCTS) && ((tiosp->c_cflag & CRTSCTS) == 0)) {
  1367. tty->hw_stopped = 0;
  1368. stl_start(tty);
  1369. }
  1370. if (((old->c_cflag & CLOCAL) == 0) && (tiosp->c_cflag & CLOCAL))
  1371. wake_up_interruptible(&portp->open_wait);
  1372. }
  1373. /*****************************************************************************/
  1374. /*
  1375. * Attempt to flow control who ever is sending us data. Based on termios
  1376. * settings use software or/and hardware flow control.
  1377. */
  1378. static void stl_throttle(struct tty_struct *tty)
  1379. {
  1380. stlport_t *portp;
  1381. #ifdef DEBUG
  1382. printk("stl_throttle(tty=%x)\n", (int) tty);
  1383. #endif
  1384. if (tty == (struct tty_struct *) NULL)
  1385. return;
  1386. portp = tty->driver_data;
  1387. if (portp == (stlport_t *) NULL)
  1388. return;
  1389. stl_flowctrl(portp, 0);
  1390. }
  1391. /*****************************************************************************/
  1392. /*
  1393. * Unflow control the device sending us data...
  1394. */
  1395. static void stl_unthrottle(struct tty_struct *tty)
  1396. {
  1397. stlport_t *portp;
  1398. #ifdef DEBUG
  1399. printk("stl_unthrottle(tty=%x)\n", (int) tty);
  1400. #endif
  1401. if (tty == (struct tty_struct *) NULL)
  1402. return;
  1403. portp = tty->driver_data;
  1404. if (portp == (stlport_t *) NULL)
  1405. return;
  1406. stl_flowctrl(portp, 1);
  1407. }
  1408. /*****************************************************************************/
  1409. /*
  1410. * Stop the transmitter. Basically to do this we will just turn TX
  1411. * interrupts off.
  1412. */
  1413. static void stl_stop(struct tty_struct *tty)
  1414. {
  1415. stlport_t *portp;
  1416. #ifdef DEBUG
  1417. printk("stl_stop(tty=%x)\n", (int) tty);
  1418. #endif
  1419. if (tty == (struct tty_struct *) NULL)
  1420. return;
  1421. portp = tty->driver_data;
  1422. if (portp == (stlport_t *) NULL)
  1423. return;
  1424. stl_startrxtx(portp, -1, 0);
  1425. }
  1426. /*****************************************************************************/
  1427. /*
  1428. * Start the transmitter again. Just turn TX interrupts back on.
  1429. */
  1430. static void stl_start(struct tty_struct *tty)
  1431. {
  1432. stlport_t *portp;
  1433. #ifdef DEBUG
  1434. printk("stl_start(tty=%x)\n", (int) tty);
  1435. #endif
  1436. if (tty == (struct tty_struct *) NULL)
  1437. return;
  1438. portp = tty->driver_data;
  1439. if (portp == (stlport_t *) NULL)
  1440. return;
  1441. stl_startrxtx(portp, -1, 1);
  1442. }
  1443. /*****************************************************************************/
  1444. /*
  1445. * Hangup this port. This is pretty much like closing the port, only
  1446. * a little more brutal. No waiting for data to drain. Shutdown the
  1447. * port and maybe drop signals.
  1448. */
  1449. static void stl_hangup(struct tty_struct *tty)
  1450. {
  1451. stlport_t *portp;
  1452. #ifdef DEBUG
  1453. printk("stl_hangup(tty=%x)\n", (int) tty);
  1454. #endif
  1455. if (tty == (struct tty_struct *) NULL)
  1456. return;
  1457. portp = tty->driver_data;
  1458. if (portp == (stlport_t *) NULL)
  1459. return;
  1460. portp->flags &= ~ASYNC_INITIALIZED;
  1461. stl_disableintrs(portp);
  1462. if (tty->termios->c_cflag & HUPCL)
  1463. stl_setsignals(portp, 0, 0);
  1464. stl_enablerxtx(portp, 0, 0);
  1465. stl_flushbuffer(tty);
  1466. portp->istate = 0;
  1467. set_bit(TTY_IO_ERROR, &tty->flags);
  1468. if (portp->tx.buf != (char *) NULL) {
  1469. kfree(portp->tx.buf);
  1470. portp->tx.buf = (char *) NULL;
  1471. portp->tx.head = (char *) NULL;
  1472. portp->tx.tail = (char *) NULL;
  1473. }
  1474. portp->tty = (struct tty_struct *) NULL;
  1475. portp->flags &= ~ASYNC_NORMAL_ACTIVE;
  1476. portp->refcount = 0;
  1477. wake_up_interruptible(&portp->open_wait);
  1478. }
  1479. /*****************************************************************************/
  1480. static void stl_flushbuffer(struct tty_struct *tty)
  1481. {
  1482. stlport_t *portp;
  1483. #ifdef DEBUG
  1484. printk("stl_flushbuffer(tty=%x)\n", (int) tty);
  1485. #endif
  1486. if (tty == (struct tty_struct *) NULL)
  1487. return;
  1488. portp = tty->driver_data;
  1489. if (portp == (stlport_t *) NULL)
  1490. return;
  1491. stl_flush(portp);
  1492. tty_wakeup(tty);
  1493. }
  1494. /*****************************************************************************/
  1495. static void stl_breakctl(struct tty_struct *tty, int state)
  1496. {
  1497. stlport_t *portp;
  1498. #ifdef DEBUG
  1499. printk("stl_breakctl(tty=%x,state=%d)\n", (int) tty, state);
  1500. #endif
  1501. if (tty == (struct tty_struct *) NULL)
  1502. return;
  1503. portp = tty->driver_data;
  1504. if (portp == (stlport_t *) NULL)
  1505. return;
  1506. stl_sendbreak(portp, ((state == -1) ? 1 : 2));
  1507. }
  1508. /*****************************************************************************/
  1509. static void stl_waituntilsent(struct tty_struct *tty, int timeout)
  1510. {
  1511. stlport_t *portp;
  1512. unsigned long tend;
  1513. #ifdef DEBUG
  1514. printk("stl_waituntilsent(tty=%x,timeout=%d)\n", (int) tty, timeout);
  1515. #endif
  1516. if (tty == (struct tty_struct *) NULL)
  1517. return;
  1518. portp = tty->driver_data;
  1519. if (portp == (stlport_t *) NULL)
  1520. return;
  1521. if (timeout == 0)
  1522. timeout = HZ;
  1523. tend = jiffies + timeout;
  1524. while (stl_datastate(portp)) {
  1525. if (signal_pending(current))
  1526. break;
  1527. msleep_interruptible(20);
  1528. if (time_after_eq(jiffies, tend))
  1529. break;
  1530. }
  1531. }
  1532. /*****************************************************************************/
  1533. static void stl_sendxchar(struct tty_struct *tty, char ch)
  1534. {
  1535. stlport_t *portp;
  1536. #ifdef DEBUG
  1537. printk("stl_sendxchar(tty=%x,ch=%x)\n", (int) tty, ch);
  1538. #endif
  1539. if (tty == (struct tty_struct *) NULL)
  1540. return;
  1541. portp = tty->driver_data;
  1542. if (portp == (stlport_t *) NULL)
  1543. return;
  1544. if (ch == STOP_CHAR(tty))
  1545. stl_sendflow(portp, 0);
  1546. else if (ch == START_CHAR(tty))
  1547. stl_sendflow(portp, 1);
  1548. else
  1549. stl_putchar(tty, ch);
  1550. }
  1551. /*****************************************************************************/
  1552. #define MAXLINE 80
  1553. /*
  1554. * Format info for a specified port. The line is deliberately limited
  1555. * to 80 characters. (If it is too long it will be truncated, if too
  1556. * short then padded with spaces).
  1557. */
  1558. static int stl_portinfo(stlport_t *portp, int portnr, char *pos)
  1559. {
  1560. char *sp;
  1561. int sigs, cnt;
  1562. sp = pos;
  1563. sp += sprintf(sp, "%d: uart:%s tx:%d rx:%d",
  1564. portnr, (portp->hwid == 1) ? "SC26198" : "CD1400",
  1565. (int) portp->stats.txtotal, (int) portp->stats.rxtotal);
  1566. if (portp->stats.rxframing)
  1567. sp += sprintf(sp, " fe:%d", (int) portp->stats.rxframing);
  1568. if (portp->stats.rxparity)
  1569. sp += sprintf(sp, " pe:%d", (int) portp->stats.rxparity);
  1570. if (portp->stats.rxbreaks)
  1571. sp += sprintf(sp, " brk:%d", (int) portp->stats.rxbreaks);
  1572. if (portp->stats.rxoverrun)
  1573. sp += sprintf(sp, " oe:%d", (int) portp->stats.rxoverrun);
  1574. sigs = stl_getsignals(portp);
  1575. cnt = sprintf(sp, "%s%s%s%s%s ",
  1576. (sigs & TIOCM_RTS) ? "|RTS" : "",
  1577. (sigs & TIOCM_CTS) ? "|CTS" : "",
  1578. (sigs & TIOCM_DTR) ? "|DTR" : "",
  1579. (sigs & TIOCM_CD) ? "|DCD" : "",
  1580. (sigs & TIOCM_DSR) ? "|DSR" : "");
  1581. *sp = ' ';
  1582. sp += cnt;
  1583. for (cnt = (sp - pos); (cnt < (MAXLINE - 1)); cnt++)
  1584. *sp++ = ' ';
  1585. if (cnt >= MAXLINE)
  1586. pos[(MAXLINE - 2)] = '+';
  1587. pos[(MAXLINE - 1)] = '\n';
  1588. return(MAXLINE);
  1589. }
  1590. /*****************************************************************************/
  1591. /*
  1592. * Port info, read from the /proc file system.
  1593. */
  1594. static int stl_readproc(char *page, char **start, off_t off, int count, int *eof, void *data)
  1595. {
  1596. stlbrd_t *brdp;
  1597. stlpanel_t *panelp;
  1598. stlport_t *portp;
  1599. int brdnr, panelnr, portnr, totalport;
  1600. int curoff, maxoff;
  1601. char *pos;
  1602. #ifdef DEBUG
  1603. printk("stl_readproc(page=%x,start=%x,off=%x,count=%d,eof=%x,"
  1604. "data=%x\n", (int) page, (int) start, (int) off, count,
  1605. (int) eof, (int) data);
  1606. #endif
  1607. pos = page;
  1608. totalport = 0;
  1609. curoff = 0;
  1610. if (off == 0) {
  1611. pos += sprintf(pos, "%s: version %s", stl_drvtitle,
  1612. stl_drvversion);
  1613. while (pos < (page + MAXLINE - 1))
  1614. *pos++ = ' ';
  1615. *pos++ = '\n';
  1616. }
  1617. curoff = MAXLINE;
  1618. /*
  1619. * We scan through for each board, panel and port. The offset is
  1620. * calculated on the fly, and irrelevant ports are skipped.
  1621. */
  1622. for (brdnr = 0; (brdnr < stl_nrbrds); brdnr++) {
  1623. brdp = stl_brds[brdnr];
  1624. if (brdp == (stlbrd_t *) NULL)
  1625. continue;
  1626. if (brdp->state == 0)
  1627. continue;
  1628. maxoff = curoff + (brdp->nrports * MAXLINE);
  1629. if (off >= maxoff) {
  1630. curoff = maxoff;
  1631. continue;
  1632. }
  1633. totalport = brdnr * STL_MAXPORTS;
  1634. for (panelnr = 0; (panelnr < brdp->nrpanels); panelnr++) {
  1635. panelp = brdp->panels[panelnr];
  1636. if (panelp == (stlpanel_t *) NULL)
  1637. continue;
  1638. maxoff = curoff + (panelp->nrports * MAXLINE);
  1639. if (off >= maxoff) {
  1640. curoff = maxoff;
  1641. totalport += panelp->nrports;
  1642. continue;
  1643. }
  1644. for (portnr = 0; (portnr < panelp->nrports); portnr++,
  1645. totalport++) {
  1646. portp = panelp->ports[portnr];
  1647. if (portp == (stlport_t *) NULL)
  1648. continue;
  1649. if (off >= (curoff += MAXLINE))
  1650. continue;
  1651. if ((pos - page + MAXLINE) > count)
  1652. goto stl_readdone;
  1653. pos += stl_portinfo(portp, totalport, pos);
  1654. }
  1655. }
  1656. }
  1657. *eof = 1;
  1658. stl_readdone:
  1659. *start = page;
  1660. return(pos - page);
  1661. }
  1662. /*****************************************************************************/
  1663. /*
  1664. * All board interrupts are vectored through here first. This code then
  1665. * calls off to the approrpriate board interrupt handlers.
  1666. */
  1667. static irqreturn_t stl_intr(int irq, void *dev_id, struct pt_regs *regs)
  1668. {
  1669. stlbrd_t *brdp = (stlbrd_t *) dev_id;
  1670. #ifdef DEBUG
  1671. printk("stl_intr(brdp=%x,irq=%d,regs=%x)\n", (int) brdp, irq,
  1672. (int) regs);
  1673. #endif
  1674. return IRQ_RETVAL((* brdp->isr)(brdp));
  1675. }
  1676. /*****************************************************************************/
  1677. /*
  1678. * Interrupt service routine for EasyIO board types.
  1679. */
  1680. static int stl_eiointr(stlbrd_t *brdp)
  1681. {
  1682. stlpanel_t *panelp;
  1683. unsigned int iobase;
  1684. int handled = 0;
  1685. panelp = brdp->panels[0];
  1686. iobase = panelp->iobase;
  1687. while (inb(brdp->iostatus) & EIO_INTRPEND) {
  1688. handled = 1;
  1689. (* panelp->isr)(panelp, iobase);
  1690. }
  1691. return handled;
  1692. }
  1693. /*****************************************************************************/
  1694. /*
  1695. * Interrupt service routine for ECH-AT board types.
  1696. */
  1697. static int stl_echatintr(stlbrd_t *brdp)
  1698. {
  1699. stlpanel_t *panelp;
  1700. unsigned int ioaddr;
  1701. int bnknr;
  1702. int handled = 0;
  1703. outb((brdp->ioctrlval | ECH_BRDENABLE), brdp->ioctrl);
  1704. while (inb(brdp->iostatus) & ECH_INTRPEND) {
  1705. handled = 1;
  1706. for (bnknr = 0; (bnknr < brdp->nrbnks); bnknr++) {
  1707. ioaddr = brdp->bnkstataddr[bnknr];
  1708. if (inb(ioaddr) & ECH_PNLINTRPEND) {
  1709. panelp = brdp->bnk2panel[bnknr];
  1710. (* panelp->isr)(panelp, (ioaddr & 0xfffc));
  1711. }
  1712. }
  1713. }
  1714. outb((brdp->ioctrlval | ECH_BRDDISABLE), brdp->ioctrl);
  1715. return handled;
  1716. }
  1717. /*****************************************************************************/
  1718. /*
  1719. * Interrupt service routine for ECH-MCA board types.
  1720. */
  1721. static int stl_echmcaintr(stlbrd_t *brdp)
  1722. {
  1723. stlpanel_t *panelp;
  1724. unsigned int ioaddr;
  1725. int bnknr;
  1726. int handled = 0;
  1727. while (inb(brdp->iostatus) & ECH_INTRPEND) {
  1728. handled = 1;
  1729. for (bnknr = 0; (bnknr < brdp->nrbnks); bnknr++) {
  1730. ioaddr = brdp->bnkstataddr[bnknr];
  1731. if (inb(ioaddr) & ECH_PNLINTRPEND) {
  1732. panelp = brdp->bnk2panel[bnknr];
  1733. (* panelp->isr)(panelp, (ioaddr & 0xfffc));
  1734. }
  1735. }
  1736. }
  1737. return handled;
  1738. }
  1739. /*****************************************************************************/
  1740. /*
  1741. * Interrupt service routine for ECH-PCI board types.
  1742. */
  1743. static int stl_echpciintr(stlbrd_t *brdp)
  1744. {
  1745. stlpanel_t *panelp;
  1746. unsigned int ioaddr;
  1747. int bnknr, recheck;
  1748. int handled = 0;
  1749. while (1) {
  1750. recheck = 0;
  1751. for (bnknr = 0; (bnknr < brdp->nrbnks); bnknr++) {
  1752. outb(brdp->bnkpageaddr[bnknr], brdp->ioctrl);
  1753. ioaddr = brdp->bnkstataddr[bnknr];
  1754. if (inb(ioaddr) & ECH_PNLINTRPEND) {
  1755. panelp = brdp->bnk2panel[bnknr];
  1756. (* panelp->isr)(panelp, (ioaddr & 0xfffc));
  1757. recheck++;
  1758. handled = 1;
  1759. }
  1760. }
  1761. if (! recheck)
  1762. break;
  1763. }
  1764. return handled;
  1765. }
  1766. /*****************************************************************************/
  1767. /*
  1768. * Interrupt service routine for ECH-8/64-PCI board types.
  1769. */
  1770. static int stl_echpci64intr(stlbrd_t *brdp)
  1771. {
  1772. stlpanel_t *panelp;
  1773. unsigned int ioaddr;
  1774. int bnknr;
  1775. int handled = 0;
  1776. while (inb(brdp->ioctrl) & 0x1) {
  1777. handled = 1;
  1778. for (bnknr = 0; (bnknr < brdp->nrbnks); bnknr++) {
  1779. ioaddr = brdp->bnkstataddr[bnknr];
  1780. if (inb(ioaddr) & ECH_PNLINTRPEND) {
  1781. panelp = brdp->bnk2panel[bnknr];
  1782. (* panelp->isr)(panelp, (ioaddr & 0xfffc));
  1783. }
  1784. }
  1785. }
  1786. return handled;
  1787. }
  1788. /*****************************************************************************/
  1789. /*
  1790. * Service an off-level request for some channel.
  1791. */
  1792. static void stl_offintr(void *private)
  1793. {
  1794. stlport_t *portp;
  1795. struct tty_struct *tty;
  1796. unsigned int oldsigs;
  1797. portp = private;
  1798. #ifdef DEBUG
  1799. printk("stl_offintr(portp=%x)\n", (int) portp);
  1800. #endif
  1801. if (portp == (stlport_t *) NULL)
  1802. return;
  1803. tty = portp->tty;
  1804. if (tty == (struct tty_struct *) NULL)
  1805. return;
  1806. lock_kernel();
  1807. if (test_bit(ASYI_TXLOW, &portp->istate)) {
  1808. tty_wakeup(tty);
  1809. }
  1810. if (test_bit(ASYI_DCDCHANGE, &portp->istate)) {
  1811. clear_bit(ASYI_DCDCHANGE, &portp->istate);
  1812. oldsigs = portp->sigs;
  1813. portp->sigs = stl_getsignals(portp);
  1814. if ((portp->sigs & TIOCM_CD) && ((oldsigs & TIOCM_CD) == 0))
  1815. wake_up_interruptible(&portp->open_wait);
  1816. if ((oldsigs & TIOCM_CD) && ((portp->sigs & TIOCM_CD) == 0)) {
  1817. if (portp->flags & ASYNC_CHECK_CD)
  1818. tty_hangup(tty); /* FIXME: module removal race here - AKPM */
  1819. }
  1820. }
  1821. unlock_kernel();
  1822. }
  1823. /*****************************************************************************/
  1824. /*
  1825. * Initialize all the ports on a panel.
  1826. */
  1827. static int __init stl_initports(stlbrd_t *brdp, stlpanel_t *panelp)
  1828. {
  1829. stlport_t *portp;
  1830. int chipmask, i;
  1831. #ifdef DEBUG
  1832. printk("stl_initports(brdp=%x,panelp=%x)\n", (int) brdp, (int) panelp);
  1833. #endif
  1834. chipmask = stl_panelinit(brdp, panelp);
  1835. /*
  1836. * All UART's are initialized (if found!). Now go through and setup
  1837. * each ports data structures.
  1838. */
  1839. for (i = 0; (i < panelp->nrports); i++) {
  1840. portp = (stlport_t *) stl_memalloc(sizeof(stlport_t));
  1841. if (portp == (stlport_t *) NULL) {
  1842. printk("STALLION: failed to allocate memory "
  1843. "(size=%d)\n", sizeof(stlport_t));
  1844. break;
  1845. }
  1846. memset(portp, 0, sizeof(stlport_t));
  1847. portp->magic = STL_PORTMAGIC;
  1848. portp->portnr = i;
  1849. portp->brdnr = panelp->brdnr;
  1850. portp->panelnr = panelp->panelnr;
  1851. portp->uartp = panelp->uartp;
  1852. portp->clk = brdp->clk;
  1853. portp->baud_base = STL_BAUDBASE;
  1854. portp->close_delay = STL_CLOSEDELAY;
  1855. portp->closing_wait = 30 * HZ;
  1856. INIT_WORK(&portp->tqueue, stl_offintr, portp);
  1857. init_waitqueue_head(&portp->open_wait);
  1858. init_waitqueue_head(&portp->close_wait);
  1859. portp->stats.brd = portp->brdnr;
  1860. portp->stats.panel = portp->panelnr;
  1861. portp->stats.port = portp->portnr;
  1862. panelp->ports[i] = portp;
  1863. stl_portinit(brdp, panelp, portp);
  1864. }
  1865. return(0);
  1866. }
  1867. /*****************************************************************************/
  1868. /*
  1869. * Try to find and initialize an EasyIO board.
  1870. */
  1871. static inline int stl_initeio(stlbrd_t *brdp)
  1872. {
  1873. stlpanel_t *panelp;
  1874. unsigned int status;
  1875. char *name;
  1876. int rc;
  1877. #ifdef DEBUG
  1878. printk("stl_initeio(brdp=%x)\n", (int) brdp);
  1879. #endif
  1880. brdp->ioctrl = brdp->ioaddr1 + 1;
  1881. brdp->iostatus = brdp->ioaddr1 + 2;
  1882. status = inb(brdp->iostatus);
  1883. if ((status & EIO_IDBITMASK) == EIO_MK3)
  1884. brdp->ioctrl++;
  1885. /*
  1886. * Handle board specific stuff now. The real difference is PCI
  1887. * or not PCI.
  1888. */
  1889. if (brdp->brdtype == BRD_EASYIOPCI) {
  1890. brdp->iosize1 = 0x80;
  1891. brdp->iosize2 = 0x80;
  1892. name = "serial(EIO-PCI)";
  1893. outb(0x41, (brdp->ioaddr2 + 0x4c));
  1894. } else {
  1895. brdp->iosize1 = 8;
  1896. name = "serial(EIO)";
  1897. if ((brdp->irq < 0) || (brdp->irq > 15) ||
  1898. (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
  1899. printk("STALLION: invalid irq=%d for brd=%d\n",
  1900. brdp->irq, brdp->brdnr);
  1901. return(-EINVAL);
  1902. }
  1903. outb((stl_vecmap[brdp->irq] | EIO_0WS |
  1904. ((brdp->irqtype) ? EIO_INTLEVEL : EIO_INTEDGE)),
  1905. brdp->ioctrl);
  1906. }
  1907. if (!request_region(brdp->ioaddr1, brdp->iosize1, name)) {
  1908. printk(KERN_WARNING "STALLION: Warning, board %d I/O address "
  1909. "%x conflicts with another device\n", brdp->brdnr,
  1910. brdp->ioaddr1);
  1911. return(-EBUSY);
  1912. }
  1913. if (brdp->iosize2 > 0)
  1914. if (!request_region(brdp->ioaddr2, brdp->iosize2, name)) {
  1915. printk(KERN_WARNING "STALLION: Warning, board %d I/O "
  1916. "address %x conflicts with another device\n",
  1917. brdp->brdnr, brdp->ioaddr2);
  1918. printk(KERN_WARNING "STALLION: Warning, also "
  1919. "releasing board %d I/O address %x \n",
  1920. brdp->brdnr, brdp->ioaddr1);
  1921. release_region(brdp->ioaddr1, brdp->iosize1);
  1922. return(-EBUSY);
  1923. }
  1924. /*
  1925. * Everything looks OK, so let's go ahead and probe for the hardware.
  1926. */
  1927. brdp->clk = CD1400_CLK;
  1928. brdp->isr = stl_eiointr;
  1929. switch (status & EIO_IDBITMASK) {
  1930. case EIO_8PORTM:
  1931. brdp->clk = CD1400_CLK8M;
  1932. /* fall thru */
  1933. case EIO_8PORTRS:
  1934. case EIO_8PORTDI:
  1935. brdp->nrports = 8;
  1936. break;
  1937. case EIO_4PORTRS:
  1938. brdp->nrports = 4;
  1939. break;
  1940. case EIO_MK3:
  1941. switch (status & EIO_BRDMASK) {
  1942. case ID_BRD4:
  1943. brdp->nrports = 4;
  1944. break;
  1945. case ID_BRD8:
  1946. brdp->nrports = 8;
  1947. break;
  1948. case ID_BRD16:
  1949. brdp->nrports = 16;
  1950. break;
  1951. default:
  1952. return(-ENODEV);
  1953. }
  1954. break;
  1955. default:
  1956. return(-ENODEV);
  1957. }
  1958. /*
  1959. * We have verified that the board is actually present, so now we
  1960. * can complete the setup.
  1961. */
  1962. panelp = (stlpanel_t *) stl_memalloc(sizeof(stlpanel_t));
  1963. if (panelp == (stlpanel_t *) NULL) {
  1964. printk(KERN_WARNING "STALLION: failed to allocate memory "
  1965. "(size=%d)\n", sizeof(stlpanel_t));
  1966. return(-ENOMEM);
  1967. }
  1968. memset(panelp, 0, sizeof(stlpanel_t));
  1969. panelp->magic = STL_PANELMAGIC;
  1970. panelp->brdnr = brdp->brdnr;
  1971. panelp->panelnr = 0;
  1972. panelp->nrports = brdp->nrports;
  1973. panelp->iobase = brdp->ioaddr1;
  1974. panelp->hwid = status;
  1975. if ((status & EIO_IDBITMASK) == EIO_MK3) {
  1976. panelp->uartp = (void *) &stl_sc26198uart;
  1977. panelp->isr = stl_sc26198intr;
  1978. } else {
  1979. panelp->uartp = (void *) &stl_cd1400uart;
  1980. panelp->isr = stl_cd1400eiointr;
  1981. }
  1982. brdp->panels[0] = panelp;
  1983. brdp->nrpanels = 1;
  1984. brdp->state |= BRD_FOUND;
  1985. brdp->hwid = status;
  1986. if (request_irq(brdp->irq, stl_intr, SA_SHIRQ, name, brdp) != 0) {
  1987. printk("STALLION: failed to register interrupt "
  1988. "routine for %s irq=%d\n", name, brdp->irq);
  1989. rc = -ENODEV;
  1990. } else {
  1991. rc = 0;
  1992. }
  1993. return(rc);
  1994. }
  1995. /*****************************************************************************/
  1996. /*
  1997. * Try to find an ECH board and initialize it. This code is capable of
  1998. * dealing with all types of ECH board.
  1999. */
  2000. static inline int stl_initech(stlbrd_t *brdp)
  2001. {
  2002. stlpanel_t *panelp;
  2003. unsigned int status, nxtid, ioaddr, conflict;
  2004. int panelnr, banknr, i;
  2005. char *name;
  2006. #ifdef DEBUG
  2007. printk("stl_initech(brdp=%x)\n", (int) brdp);
  2008. #endif
  2009. status = 0;
  2010. conflict = 0;
  2011. /*
  2012. * Set up the initial board register contents for boards. This varies a
  2013. * bit between the different board types. So we need to handle each
  2014. * separately. Also do a check that the supplied IRQ is good.
  2015. */
  2016. switch (brdp->brdtype) {
  2017. case BRD_ECH:
  2018. brdp->isr = stl_echatintr;
  2019. brdp->ioctrl = brdp->ioaddr1 + 1;
  2020. brdp->iostatus = brdp->ioaddr1 + 1;
  2021. status = inb(brdp->iostatus);
  2022. if ((status & ECH_IDBITMASK) != ECH_ID)
  2023. return(-ENODEV);
  2024. if ((brdp->irq < 0) || (brdp->irq > 15) ||
  2025. (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
  2026. printk("STALLION: invalid irq=%d for brd=%d\n",
  2027. brdp->irq, brdp->brdnr);
  2028. return(-EINVAL);
  2029. }
  2030. status = ((brdp->ioaddr2 & ECH_ADDR2MASK) >> 1);
  2031. status |= (stl_vecmap[brdp->irq] << 1);
  2032. outb((status | ECH_BRDRESET), brdp->ioaddr1);
  2033. brdp->ioctrlval = ECH_INTENABLE |
  2034. ((brdp->irqtype) ? ECH_INTLEVEL : ECH_INTEDGE);
  2035. for (i = 0; (i < 10); i++)
  2036. outb((brdp->ioctrlval | ECH_BRDENABLE), brdp->ioctrl);
  2037. brdp->iosize1 = 2;
  2038. brdp->iosize2 = 32;
  2039. name = "serial(EC8/32)";
  2040. outb(status, brdp->ioaddr1);
  2041. break;
  2042. case BRD_ECHMC:
  2043. brdp->isr = stl_echmcaintr;
  2044. brdp->ioctrl = brdp->ioaddr1 + 0x20;
  2045. brdp->iostatus = brdp->ioctrl;
  2046. status = inb(brdp->iostatus);
  2047. if ((status & ECH_IDBITMASK) != ECH_ID)
  2048. return(-ENODEV);
  2049. if ((brdp->irq < 0) || (brdp->irq > 15) ||
  2050. (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
  2051. printk("STALLION: invalid irq=%d for brd=%d\n",
  2052. brdp->irq, brdp->brdnr);
  2053. return(-EINVAL);
  2054. }
  2055. outb(ECHMC_BRDRESET, brdp->ioctrl);
  2056. outb(ECHMC_INTENABLE, brdp->ioctrl);
  2057. brdp->iosize1 = 64;
  2058. name = "serial(EC8/32-MC)";
  2059. break;
  2060. case BRD_ECHPCI:
  2061. brdp->isr = stl_echpciintr;
  2062. brdp->ioctrl = brdp->ioaddr1 + 2;
  2063. brdp->iosize1 = 4;
  2064. brdp->iosize2 = 8;
  2065. name = "serial(EC8/32-PCI)";
  2066. break;
  2067. case BRD_ECH64PCI:
  2068. brdp->isr = stl_echpci64intr;
  2069. brdp->ioctrl = brdp->ioaddr2 + 0x40;
  2070. outb(0x43, (brdp->ioaddr1 + 0x4c));
  2071. brdp->iosize1 = 0x80;
  2072. brdp->iosize2 = 0x80;
  2073. name = "serial(EC8/64-PCI)";
  2074. break;
  2075. default:
  2076. printk("STALLION: unknown board type=%d\n", brdp->brdtype);
  2077. return(-EINVAL);
  2078. break;
  2079. }
  2080. /*
  2081. * Check boards for possible IO address conflicts and return fail status
  2082. * if an IO conflict found.
  2083. */
  2084. if (!request_region(brdp->ioaddr1, brdp->iosize1, name)) {
  2085. printk(KERN_WARNING "STALLION: Warning, board %d I/O address "
  2086. "%x conflicts with another device\n", brdp->brdnr,
  2087. brdp->ioaddr1);
  2088. return(-EBUSY);
  2089. }
  2090. if (brdp->iosize2 > 0)
  2091. if (!request_region(brdp->ioaddr2, brdp->iosize2, name)) {
  2092. printk(KERN_WARNING "STALLION: Warning, board %d I/O "
  2093. "address %x conflicts with another device\n",
  2094. brdp->brdnr, brdp->ioaddr2);
  2095. printk(KERN_WARNING "STALLION: Warning, also "
  2096. "releasing board %d I/O address %x \n",
  2097. brdp->brdnr, brdp->ioaddr1);
  2098. release_region(brdp->ioaddr1, brdp->iosize1);
  2099. return(-EBUSY);
  2100. }
  2101. /*
  2102. * Scan through the secondary io address space looking for panels.
  2103. * As we find'em allocate and initialize panel structures for each.
  2104. */
  2105. brdp->clk = CD1400_CLK;
  2106. brdp->hwid = status;
  2107. ioaddr = brdp->ioaddr2;
  2108. banknr = 0;
  2109. panelnr = 0;
  2110. nxtid = 0;
  2111. for (i = 0; (i < STL_MAXPANELS); i++) {
  2112. if (brdp->brdtype == BRD_ECHPCI) {
  2113. outb(nxtid, brdp->ioctrl);
  2114. ioaddr = brdp->ioaddr2;
  2115. }
  2116. status = inb(ioaddr + ECH_PNLSTATUS);
  2117. if ((status & ECH_PNLIDMASK) != nxtid)
  2118. break;
  2119. panelp = (stlpanel_t *) stl_memalloc(sizeof(stlpanel_t));
  2120. if (panelp == (stlpanel_t *) NULL) {
  2121. printk("STALLION: failed to allocate memory "
  2122. "(size=%d)\n", sizeof(stlpanel_t));
  2123. break;
  2124. }
  2125. memset(panelp, 0, sizeof(stlpanel_t));
  2126. panelp->magic = STL_PANELMAGIC;
  2127. panelp->brdnr = brdp->brdnr;
  2128. panelp->panelnr = panelnr;
  2129. panelp->iobase = ioaddr;
  2130. panelp->pagenr = nxtid;
  2131. panelp->hwid = status;
  2132. brdp->bnk2panel[banknr] = panelp;
  2133. brdp->bnkpageaddr[banknr] = nxtid;
  2134. brdp->bnkstataddr[banknr++] = ioaddr + ECH_PNLSTATUS;
  2135. if (status & ECH_PNLXPID) {
  2136. panelp->uartp = (void *) &stl_sc26198uart;
  2137. panelp->isr = stl_sc26198intr;
  2138. if (status & ECH_PNL16PORT) {
  2139. panelp->nrports = 16;
  2140. brdp->bnk2panel[banknr] = panelp;
  2141. brdp->bnkpageaddr[banknr] = nxtid;
  2142. brdp->bnkstataddr[banknr++] = ioaddr + 4 +
  2143. ECH_PNLSTATUS;
  2144. } else {
  2145. panelp->nrports = 8;
  2146. }
  2147. } else {
  2148. panelp->uartp = (void *) &stl_cd1400uart;
  2149. panelp->isr = stl_cd1400echintr;
  2150. if (status & ECH_PNL16PORT) {
  2151. panelp->nrports = 16;
  2152. panelp->ackmask = 0x80;
  2153. if (brdp->brdtype != BRD_ECHPCI)
  2154. ioaddr += EREG_BANKSIZE;
  2155. brdp->bnk2panel[banknr] = panelp;
  2156. brdp->bnkpageaddr[banknr] = ++nxtid;
  2157. brdp->bnkstataddr[banknr++] = ioaddr +
  2158. ECH_PNLSTATUS;
  2159. } else {
  2160. panelp->nrports = 8;
  2161. panelp->ackmask = 0xc0;
  2162. }
  2163. }
  2164. nxtid++;
  2165. ioaddr += EREG_BANKSIZE;
  2166. brdp->nrports += panelp->nrports;
  2167. brdp->panels[panelnr++] = panelp;
  2168. if ((brdp->brdtype != BRD_ECHPCI) &&
  2169. (ioaddr >= (brdp->ioaddr2 + brdp->iosize2)))
  2170. break;
  2171. }
  2172. brdp->nrpanels = panelnr;
  2173. brdp->nrbnks = banknr;
  2174. if (brdp->brdtype == BRD_ECH)
  2175. outb((brdp->ioctrlval | ECH_BRDDISABLE), brdp->ioctrl);
  2176. brdp->state |= BRD_FOUND;
  2177. if (request_irq(brdp->irq, stl_intr, SA_SHIRQ, name, brdp) != 0) {
  2178. printk("STALLION: failed to register interrupt "
  2179. "routine for %s irq=%d\n", name, brdp->irq);
  2180. i = -ENODEV;
  2181. } else {
  2182. i = 0;
  2183. }
  2184. return(i);
  2185. }
  2186. /*****************************************************************************/
  2187. /*
  2188. * Initialize and configure the specified board.
  2189. * Scan through all the boards in the configuration and see what we
  2190. * can find. Handle EIO and the ECH boards a little differently here
  2191. * since the initial search and setup is very different.
  2192. */
  2193. static int __init stl_brdinit(stlbrd_t *brdp)
  2194. {
  2195. int i;
  2196. #ifdef DEBUG
  2197. printk("stl_brdinit(brdp=%x)\n", (int) brdp);
  2198. #endif
  2199. switch (brdp->brdtype) {
  2200. case BRD_EASYIO:
  2201. case BRD_EASYIOPCI:
  2202. stl_initeio(brdp);
  2203. break;
  2204. case BRD_ECH:
  2205. case BRD_ECHMC:
  2206. case BRD_ECHPCI:
  2207. case BRD_ECH64PCI:
  2208. stl_initech(brdp);
  2209. break;
  2210. default:
  2211. printk("STALLION: board=%d is unknown board type=%d\n",
  2212. brdp->brdnr, brdp->brdtype);
  2213. return(ENODEV);
  2214. }
  2215. stl_brds[brdp->brdnr] = brdp;
  2216. if ((brdp->state & BRD_FOUND) == 0) {
  2217. printk("STALLION: %s board not found, board=%d io=%x irq=%d\n",
  2218. stl_brdnames[brdp->brdtype], brdp->brdnr,
  2219. brdp->ioaddr1, brdp->irq);
  2220. return(ENODEV);
  2221. }
  2222. for (i = 0; (i < STL_MAXPANELS); i++)
  2223. if (brdp->panels[i] != (stlpanel_t *) NULL)
  2224. stl_initports(brdp, brdp->panels[i]);
  2225. printk("STALLION: %s found, board=%d io=%x irq=%d "
  2226. "nrpanels=%d nrports=%d\n", stl_brdnames[brdp->brdtype],
  2227. brdp->brdnr, brdp->ioaddr1, brdp->irq, brdp->nrpanels,
  2228. brdp->nrports);
  2229. return(0);
  2230. }
  2231. /*****************************************************************************/
  2232. /*
  2233. * Find the next available board number that is free.
  2234. */
  2235. static inline int stl_getbrdnr(void)
  2236. {
  2237. int i;
  2238. for (i = 0; (i < STL_MAXBRDS); i++) {
  2239. if (stl_brds[i] == (stlbrd_t *) NULL) {
  2240. if (i >= stl_nrbrds)
  2241. stl_nrbrds = i + 1;
  2242. return(i);
  2243. }
  2244. }
  2245. return(-1);
  2246. }
  2247. /*****************************************************************************/
  2248. #ifdef CONFIG_PCI
  2249. /*
  2250. * We have a Stallion board. Allocate a board structure and
  2251. * initialize it. Read its IO and IRQ resources from PCI
  2252. * configuration space.
  2253. */
  2254. static inline int stl_initpcibrd(int brdtype, struct pci_dev *devp)
  2255. {
  2256. stlbrd_t *brdp;
  2257. #ifdef DEBUG
  2258. printk("stl_initpcibrd(brdtype=%d,busnr=%x,devnr=%x)\n", brdtype,
  2259. devp->bus->number, devp->devfn);
  2260. #endif
  2261. if (pci_enable_device(devp))
  2262. return(-EIO);
  2263. if ((brdp = stl_allocbrd()) == (stlbrd_t *) NULL)
  2264. return(-ENOMEM);
  2265. if ((brdp->brdnr = stl_getbrdnr()) < 0) {
  2266. printk("STALLION: too many boards found, "
  2267. "maximum supported %d\n", STL_MAXBRDS);
  2268. return(0);
  2269. }
  2270. brdp->brdtype = brdtype;
  2271. /*
  2272. * Different Stallion boards use the BAR registers in different ways,
  2273. * so set up io addresses based on board type.
  2274. */
  2275. #ifdef DEBUG
  2276. printk("%s(%d): BAR[]=%x,%x,%x,%x IRQ=%x\n", __FILE__, __LINE__,
  2277. pci_resource_start(devp, 0), pci_resource_start(devp, 1),
  2278. pci_resource_start(devp, 2), pci_resource_start(devp, 3), devp->irq);
  2279. #endif
  2280. /*
  2281. * We have all resources from the board, so let's setup the actual
  2282. * board structure now.
  2283. */
  2284. switch (brdtype) {
  2285. case BRD_ECHPCI:
  2286. brdp->ioaddr2 = pci_resource_start(devp, 0);
  2287. brdp->ioaddr1 = pci_resource_start(devp, 1);
  2288. break;
  2289. case BRD_ECH64PCI:
  2290. brdp->ioaddr2 = pci_resource_start(devp, 2);
  2291. brdp->ioaddr1 = pci_resource_start(devp, 1);
  2292. break;
  2293. case BRD_EASYIOPCI:
  2294. brdp->ioaddr1 = pci_resource_start(devp, 2);
  2295. brdp->ioaddr2 = pci_resource_start(devp, 1);
  2296. break;
  2297. default:
  2298. printk("STALLION: unknown PCI board type=%d\n", brdtype);
  2299. break;
  2300. }
  2301. brdp->irq = devp->irq;
  2302. stl_brdinit(brdp);
  2303. return(0);
  2304. }
  2305. /*****************************************************************************/
  2306. /*
  2307. * Find all Stallion PCI boards that might be installed. Initialize each
  2308. * one as it is found.
  2309. */
  2310. static inline int stl_findpcibrds(void)
  2311. {
  2312. struct pci_dev *dev = NULL;
  2313. int i, rc;
  2314. #ifdef DEBUG
  2315. printk("stl_findpcibrds()\n");
  2316. #endif
  2317. for (i = 0; (i < stl_nrpcibrds); i++)
  2318. while ((dev = pci_find_device(stl_pcibrds[i].vendid,
  2319. stl_pcibrds[i].devid, dev))) {
  2320. /*
  2321. * Found a device on the PCI bus that has our vendor and
  2322. * device ID. Need to check now that it is really us.
  2323. */
  2324. if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE)
  2325. continue;
  2326. rc = stl_initpcibrd(stl_pcibrds[i].brdtype, dev);
  2327. if (rc)
  2328. return(rc);
  2329. }
  2330. return(0);
  2331. }
  2332. #endif
  2333. /*****************************************************************************/
  2334. /*
  2335. * Scan through all the boards in the configuration and see what we
  2336. * can find. Handle EIO and the ECH boards a little differently here
  2337. * since the initial search and setup is too different.
  2338. */
  2339. static inline int stl_initbrds(void)
  2340. {
  2341. stlbrd_t *brdp;
  2342. stlconf_t *confp;
  2343. int i;
  2344. #ifdef DEBUG
  2345. printk("stl_initbrds()\n");
  2346. #endif
  2347. if (stl_nrbrds > STL_MAXBRDS) {
  2348. printk("STALLION: too many boards in configuration table, "
  2349. "truncating to %d\n", STL_MAXBRDS);
  2350. stl_nrbrds = STL_MAXBRDS;
  2351. }
  2352. /*
  2353. * Firstly scan the list of static boards configured. Allocate
  2354. * resources and initialize the boards as found.
  2355. */
  2356. for (i = 0; (i < stl_nrbrds); i++) {
  2357. confp = &stl_brdconf[i];
  2358. stl_parsebrd(confp, stl_brdsp[i]);
  2359. if ((brdp = stl_allocbrd()) == (stlbrd_t *) NULL)
  2360. return(-ENOMEM);
  2361. brdp->brdnr = i;
  2362. brdp->brdtype = confp->brdtype;
  2363. brdp->ioaddr1 = confp->ioaddr1;
  2364. brdp->ioaddr2 = confp->ioaddr2;
  2365. brdp->irq = confp->irq;
  2366. brdp->irqtype = confp->irqtype;
  2367. stl_brdinit(brdp);
  2368. }
  2369. /*
  2370. * Find any dynamically supported boards. That is via module load
  2371. * line options or auto-detected on the PCI bus.
  2372. */
  2373. stl_argbrds();
  2374. #ifdef CONFIG_PCI
  2375. stl_findpcibrds();
  2376. #endif
  2377. return(0);
  2378. }
  2379. /*****************************************************************************/
  2380. /*
  2381. * Return the board stats structure to user app.
  2382. */
  2383. static int stl_getbrdstats(combrd_t __user *bp)
  2384. {
  2385. stlbrd_t *brdp;
  2386. stlpanel_t *panelp;
  2387. int i;
  2388. if (copy_from_user(&stl_brdstats, bp, sizeof(combrd_t)))
  2389. return -EFAULT;
  2390. if (stl_brdstats.brd >= STL_MAXBRDS)
  2391. return(-ENODEV);
  2392. brdp = stl_brds[stl_brdstats.brd];
  2393. if (brdp == (stlbrd_t *) NULL)
  2394. return(-ENODEV);
  2395. memset(&stl_brdstats, 0, sizeof(combrd_t));
  2396. stl_brdstats.brd = brdp->brdnr;
  2397. stl_brdstats.type = brdp->brdtype;
  2398. stl_brdstats.hwid = brdp->hwid;
  2399. stl_brdstats.state = brdp->state;
  2400. stl_brdstats.ioaddr = brdp->ioaddr1;
  2401. stl_brdstats.ioaddr2 = brdp->ioaddr2;
  2402. stl_brdstats.irq = brdp->irq;
  2403. stl_brdstats.nrpanels = brdp->nrpanels;
  2404. stl_brdstats.nrports = brdp->nrports;
  2405. for (i = 0; (i < brdp->nrpanels); i++) {
  2406. panelp = brdp->panels[i];
  2407. stl_brdstats.panels[i].panel = i;
  2408. stl_brdstats.panels[i].hwid = panelp->hwid;
  2409. stl_brdstats.panels[i].nrports = panelp->nrports;
  2410. }
  2411. return copy_to_user(bp, &stl_brdstats, sizeof(combrd_t)) ? -EFAULT : 0;
  2412. }
  2413. /*****************************************************************************/
  2414. /*
  2415. * Resolve the referenced port number into a port struct pointer.
  2416. */
  2417. static stlport_t *stl_getport(int brdnr, int panelnr, int portnr)
  2418. {
  2419. stlbrd_t *brdp;
  2420. stlpanel_t *panelp;
  2421. if ((brdnr < 0) || (brdnr >= STL_MAXBRDS))
  2422. return((stlport_t *) NULL);
  2423. brdp = stl_brds[brdnr];
  2424. if (brdp == (stlbrd_t *) NULL)
  2425. return((stlport_t *) NULL);
  2426. if ((panelnr < 0) || (panelnr >= brdp->nrpanels))
  2427. return((stlport_t *) NULL);
  2428. panelp = brdp->panels[panelnr];
  2429. if (panelp == (stlpanel_t *) NULL)
  2430. return((stlport_t *) NULL);
  2431. if ((portnr < 0) || (portnr >= panelp->nrports))
  2432. return((stlport_t *) NULL);
  2433. return(panelp->ports[portnr]);
  2434. }
  2435. /*****************************************************************************/
  2436. /*
  2437. * Return the port stats structure to user app. A NULL port struct
  2438. * pointer passed in means that we need to find out from the app
  2439. * what port to get stats for (used through board control device).
  2440. */
  2441. static int stl_getportstats(stlport_t *portp, comstats_t __user *cp)
  2442. {
  2443. unsigned char *head, *tail;
  2444. unsigned long flags;
  2445. if (!portp) {
  2446. if (copy_from_user(&stl_comstats, cp, sizeof(comstats_t)))
  2447. return -EFAULT;
  2448. portp = stl_getport(stl_comstats.brd, stl_comstats.panel,
  2449. stl_comstats.port);
  2450. if (portp == (stlport_t *) NULL)
  2451. return(-ENODEV);
  2452. }
  2453. portp->stats.state = portp->istate;
  2454. portp->stats.flags = portp->flags;
  2455. portp->stats.hwid = portp->hwid;
  2456. portp->stats.ttystate = 0;
  2457. portp->stats.cflags = 0;
  2458. portp->stats.iflags = 0;
  2459. portp->stats.oflags = 0;
  2460. portp->stats.lflags = 0;
  2461. portp->stats.rxbuffered = 0;
  2462. save_flags(flags);
  2463. cli();
  2464. if (portp->tty != (struct tty_struct *) NULL) {
  2465. if (portp->tty->driver_data == portp) {
  2466. portp->stats.ttystate = portp->tty->flags;
  2467. portp->stats.rxbuffered = portp->tty->flip.count;
  2468. if (portp->tty->termios != (struct termios *) NULL) {
  2469. portp->stats.cflags = portp->tty->termios->c_cflag;
  2470. portp->stats.iflags = portp->tty->termios->c_iflag;
  2471. portp->stats.oflags = portp->tty->termios->c_oflag;
  2472. portp->stats.lflags = portp->tty->termios->c_lflag;
  2473. }
  2474. }
  2475. }
  2476. restore_flags(flags);
  2477. head = portp->tx.head;
  2478. tail = portp->tx.tail;
  2479. portp->stats.txbuffered = ((head >= tail) ? (head - tail) :
  2480. (STL_TXBUFSIZE - (tail - head)));
  2481. portp->stats.signals = (unsigned long) stl_getsignals(portp);
  2482. return copy_to_user(cp, &portp->stats,
  2483. sizeof(comstats_t)) ? -EFAULT : 0;
  2484. }
  2485. /*****************************************************************************/
  2486. /*
  2487. * Clear the port stats structure. We also return it zeroed out...
  2488. */
  2489. static int stl_clrportstats(stlport_t *portp, comstats_t __user *cp)
  2490. {
  2491. if (!portp) {
  2492. if (copy_from_user(&stl_comstats, cp, sizeof(comstats_t)))
  2493. return -EFAULT;
  2494. portp = stl_getport(stl_comstats.brd, stl_comstats.panel,
  2495. stl_comstats.port);
  2496. if (portp == (stlport_t *) NULL)
  2497. return(-ENODEV);
  2498. }
  2499. memset(&portp->stats, 0, sizeof(comstats_t));
  2500. portp->stats.brd = portp->brdnr;
  2501. portp->stats.panel = portp->panelnr;
  2502. portp->stats.port = portp->portnr;
  2503. return copy_to_user(cp, &portp->stats,
  2504. sizeof(comstats_t)) ? -EFAULT : 0;
  2505. }
  2506. /*****************************************************************************/
  2507. /*
  2508. * Return the entire driver ports structure to a user app.
  2509. */
  2510. static int stl_getportstruct(stlport_t __user *arg)
  2511. {
  2512. stlport_t *portp;
  2513. if (copy_from_user(&stl_dummyport, arg, sizeof(stlport_t)))
  2514. return -EFAULT;
  2515. portp = stl_getport(stl_dummyport.brdnr, stl_dummyport.panelnr,
  2516. stl_dummyport.portnr);
  2517. if (!portp)
  2518. return -ENODEV;
  2519. return copy_to_user(arg, portp, sizeof(stlport_t)) ? -EFAULT : 0;
  2520. }
  2521. /*****************************************************************************/
  2522. /*
  2523. * Return the entire driver board structure to a user app.
  2524. */
  2525. static int stl_getbrdstruct(stlbrd_t __user *arg)
  2526. {
  2527. stlbrd_t *brdp;
  2528. if (copy_from_user(&stl_dummybrd, arg, sizeof(stlbrd_t)))
  2529. return -EFAULT;
  2530. if ((stl_dummybrd.brdnr < 0) || (stl_dummybrd.brdnr >= STL_MAXBRDS))
  2531. return -ENODEV;
  2532. brdp = stl_brds[stl_dummybrd.brdnr];
  2533. if (!brdp)
  2534. return(-ENODEV);
  2535. return copy_to_user(arg, brdp, sizeof(stlbrd_t)) ? -EFAULT : 0;
  2536. }
  2537. /*****************************************************************************/
  2538. /*
  2539. * The "staliomem" device is also required to do some special operations
  2540. * on the board and/or ports. In this driver it is mostly used for stats
  2541. * collection.
  2542. */
  2543. static int stl_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg)
  2544. {
  2545. int brdnr, rc;
  2546. void __user *argp = (void __user *)arg;
  2547. #ifdef DEBUG
  2548. printk("stl_memioctl(ip=%x,fp=%x,cmd=%x,arg=%x)\n", (int) ip,
  2549. (int) fp, cmd, (int) arg);
  2550. #endif
  2551. brdnr = iminor(ip);
  2552. if (brdnr >= STL_MAXBRDS)
  2553. return(-ENODEV);
  2554. rc = 0;
  2555. switch (cmd) {
  2556. case COM_GETPORTSTATS:
  2557. rc = stl_getportstats(NULL, argp);
  2558. break;
  2559. case COM_CLRPORTSTATS:
  2560. rc = stl_clrportstats(NULL, argp);
  2561. break;
  2562. case COM_GETBRDSTATS:
  2563. rc = stl_getbrdstats(argp);
  2564. break;
  2565. case COM_READPORT:
  2566. rc = stl_getportstruct(argp);
  2567. break;
  2568. case COM_READBOARD:
  2569. rc = stl_getbrdstruct(argp);
  2570. break;
  2571. default:
  2572. rc = -ENOIOCTLCMD;
  2573. break;
  2574. }
  2575. return(rc);
  2576. }
  2577. static struct tty_operations stl_ops = {
  2578. .open = stl_open,
  2579. .close = stl_close,
  2580. .write = stl_write,
  2581. .put_char = stl_putchar,
  2582. .flush_chars = stl_flushchars,
  2583. .write_room = stl_writeroom,
  2584. .chars_in_buffer = stl_charsinbuffer,
  2585. .ioctl = stl_ioctl,
  2586. .set_termios = stl_settermios,
  2587. .throttle = stl_throttle,
  2588. .unthrottle = stl_unthrottle,
  2589. .stop = stl_stop,
  2590. .start = stl_start,
  2591. .hangup = stl_hangup,
  2592. .flush_buffer = stl_flushbuffer,
  2593. .break_ctl = stl_breakctl,
  2594. .wait_until_sent = stl_waituntilsent,
  2595. .send_xchar = stl_sendxchar,
  2596. .read_proc = stl_readproc,
  2597. .tiocmget = stl_tiocmget,
  2598. .tiocmset = stl_tiocmset,
  2599. };
  2600. /*****************************************************************************/
  2601. static int __init stl_init(void)
  2602. {
  2603. int i;
  2604. printk(KERN_INFO "%s: version %s\n", stl_drvtitle, stl_drvversion);
  2605. stl_initbrds();
  2606. stl_serial = alloc_tty_driver(STL_MAXBRDS * STL_MAXPORTS);
  2607. if (!stl_serial)
  2608. return -1;
  2609. /*
  2610. * Allocate a temporary write buffer.
  2611. */
  2612. stl_tmpwritebuf = (char *) stl_memalloc(STL_TXBUFSIZE);
  2613. if (stl_tmpwritebuf == (char *) NULL)
  2614. printk("STALLION: failed to allocate memory (size=%d)\n",
  2615. STL_TXBUFSIZE);
  2616. /*
  2617. * Set up a character driver for per board stuff. This is mainly used
  2618. * to do stats ioctls on the ports.
  2619. */
  2620. if (register_chrdev(STL_SIOMEMMAJOR, "staliomem", &stl_fsiomem))
  2621. printk("STALLION: failed to register serial board device\n");
  2622. devfs_mk_dir("staliomem");
  2623. stallion_class = class_create(THIS_MODULE, "staliomem");
  2624. for (i = 0; i < 4; i++) {
  2625. devfs_mk_cdev(MKDEV(STL_SIOMEMMAJOR, i),
  2626. S_IFCHR|S_IRUSR|S_IWUSR,
  2627. "staliomem/%d", i);
  2628. class_device_create(stallion_class, NULL,
  2629. MKDEV(STL_SIOMEMMAJOR, i), NULL,
  2630. "staliomem%d", i);
  2631. }
  2632. stl_serial->owner = THIS_MODULE;
  2633. stl_serial->driver_name = stl_drvname;
  2634. stl_serial->name = "ttyE";
  2635. stl_serial->devfs_name = "tts/E";
  2636. stl_serial->major = STL_SERIALMAJOR;
  2637. stl_serial->minor_start = 0;
  2638. stl_serial->type = TTY_DRIVER_TYPE_SERIAL;
  2639. stl_serial->subtype = SERIAL_TYPE_NORMAL;
  2640. stl_serial->init_termios = stl_deftermios;
  2641. stl_serial->flags = TTY_DRIVER_REAL_RAW;
  2642. tty_set_operations(stl_serial, &stl_ops);
  2643. if (tty_register_driver(stl_serial)) {
  2644. put_tty_driver(stl_serial);
  2645. printk("STALLION: failed to register serial driver\n");
  2646. return -1;
  2647. }
  2648. return(0);
  2649. }
  2650. /*****************************************************************************/
  2651. /* CD1400 HARDWARE FUNCTIONS */
  2652. /*****************************************************************************/
  2653. /*
  2654. * These functions get/set/update the registers of the cd1400 UARTs.
  2655. * Access to the cd1400 registers is via an address/data io port pair.
  2656. * (Maybe should make this inline...)
  2657. */
  2658. static int stl_cd1400getreg(stlport_t *portp, int regnr)
  2659. {
  2660. outb((regnr + portp->uartaddr), portp->ioaddr);
  2661. return(inb(portp->ioaddr + EREG_DATA));
  2662. }
  2663. static void stl_cd1400setreg(stlport_t *portp, int regnr, int value)
  2664. {
  2665. outb((regnr + portp->uartaddr), portp->ioaddr);
  2666. outb(value, portp->ioaddr + EREG_DATA);
  2667. }
  2668. static int stl_cd1400updatereg(stlport_t *portp, int regnr, int value)
  2669. {
  2670. outb((regnr + portp->uartaddr), portp->ioaddr);
  2671. if (inb(portp->ioaddr + EREG_DATA) != value) {
  2672. outb(value, portp->ioaddr + EREG_DATA);
  2673. return(1);
  2674. }
  2675. return(0);
  2676. }
  2677. /*****************************************************************************/
  2678. /*
  2679. * Inbitialize the UARTs in a panel. We don't care what sort of board
  2680. * these ports are on - since the port io registers are almost
  2681. * identical when dealing with ports.
  2682. */
  2683. static int stl_cd1400panelinit(stlbrd_t *brdp, stlpanel_t *panelp)
  2684. {
  2685. unsigned int gfrcr;
  2686. int chipmask, i, j;
  2687. int nrchips, uartaddr, ioaddr;
  2688. #ifdef DEBUG
  2689. printk("stl_panelinit(brdp=%x,panelp=%x)\n", (int) brdp, (int) panelp);
  2690. #endif
  2691. BRDENABLE(panelp->brdnr, panelp->pagenr);
  2692. /*
  2693. * Check that each chip is present and started up OK.
  2694. */
  2695. chipmask = 0;
  2696. nrchips = panelp->nrports / CD1400_PORTS;
  2697. for (i = 0; (i < nrchips); i++) {
  2698. if (brdp->brdtype == BRD_ECHPCI) {
  2699. outb((panelp->pagenr + (i >> 1)), brdp->ioctrl);
  2700. ioaddr = panelp->iobase;
  2701. } else {
  2702. ioaddr = panelp->iobase + (EREG_BANKSIZE * (i >> 1));
  2703. }
  2704. uartaddr = (i & 0x01) ? 0x080 : 0;
  2705. outb((GFRCR + uartaddr), ioaddr);
  2706. outb(0, (ioaddr + EREG_DATA));
  2707. outb((CCR + uartaddr), ioaddr);
  2708. outb(CCR_RESETFULL, (ioaddr + EREG_DATA));
  2709. outb(CCR_RESETFULL, (ioaddr + EREG_DATA));
  2710. outb((GFRCR + uartaddr), ioaddr);
  2711. for (j = 0; (j < CCR_MAXWAIT); j++) {
  2712. if ((gfrcr = inb(ioaddr + EREG_DATA)) != 0)
  2713. break;
  2714. }
  2715. if ((j >= CCR_MAXWAIT) || (gfrcr < 0x40) || (gfrcr > 0x60)) {
  2716. printk("STALLION: cd1400 not responding, "
  2717. "brd=%d panel=%d chip=%d\n",
  2718. panelp->brdnr, panelp->panelnr, i);
  2719. continue;
  2720. }
  2721. chipmask |= (0x1 << i);
  2722. outb((PPR + uartaddr), ioaddr);
  2723. outb(PPR_SCALAR, (ioaddr + EREG_DATA));
  2724. }
  2725. BRDDISABLE(panelp->brdnr);
  2726. return(chipmask);
  2727. }
  2728. /*****************************************************************************/
  2729. /*
  2730. * Initialize hardware specific port registers.
  2731. */
  2732. static void stl_cd1400portinit(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp)
  2733. {
  2734. #ifdef DEBUG
  2735. printk("stl_cd1400portinit(brdp=%x,panelp=%x,portp=%x)\n",
  2736. (int) brdp, (int) panelp, (int) portp);
  2737. #endif
  2738. if ((brdp == (stlbrd_t *) NULL) || (panelp == (stlpanel_t *) NULL) ||
  2739. (portp == (stlport_t *) NULL))
  2740. return;
  2741. portp->ioaddr = panelp->iobase + (((brdp->brdtype == BRD_ECHPCI) ||
  2742. (portp->portnr < 8)) ? 0 : EREG_BANKSIZE);
  2743. portp->uartaddr = (portp->portnr & 0x04) << 5;
  2744. portp->pagenr = panelp->pagenr + (portp->portnr >> 3);
  2745. BRDENABLE(portp->brdnr, portp->pagenr);
  2746. stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
  2747. stl_cd1400setreg(portp, LIVR, (portp->portnr << 3));
  2748. portp->hwid = stl_cd1400getreg(portp, GFRCR);
  2749. BRDDISABLE(portp->brdnr);
  2750. }
  2751. /*****************************************************************************/
  2752. /*
  2753. * Wait for the command register to be ready. We will poll this,
  2754. * since it won't usually take too long to be ready.
  2755. */
  2756. static void stl_cd1400ccrwait(stlport_t *portp)
  2757. {
  2758. int i;
  2759. for (i = 0; (i < CCR_MAXWAIT); i++) {
  2760. if (stl_cd1400getreg(portp, CCR) == 0) {
  2761. return;
  2762. }
  2763. }
  2764. printk("STALLION: cd1400 not responding, port=%d panel=%d brd=%d\n",
  2765. portp->portnr, portp->panelnr, portp->brdnr);
  2766. }
  2767. /*****************************************************************************/
  2768. /*
  2769. * Set up the cd1400 registers for a port based on the termios port
  2770. * settings.
  2771. */
  2772. static void stl_cd1400setport(stlport_t *portp, struct termios *tiosp)
  2773. {
  2774. stlbrd_t *brdp;
  2775. unsigned long flags;
  2776. unsigned int clkdiv, baudrate;
  2777. unsigned char cor1, cor2, cor3;
  2778. unsigned char cor4, cor5, ccr;
  2779. unsigned char srer, sreron, sreroff;
  2780. unsigned char mcor1, mcor2, rtpr;
  2781. unsigned char clk, div;
  2782. cor1 = 0;
  2783. cor2 = 0;
  2784. cor3 = 0;
  2785. cor4 = 0;
  2786. cor5 = 0;
  2787. ccr = 0;
  2788. rtpr = 0;
  2789. clk = 0;
  2790. div = 0;
  2791. mcor1 = 0;
  2792. mcor2 = 0;
  2793. sreron = 0;
  2794. sreroff = 0;
  2795. brdp = stl_brds[portp->brdnr];
  2796. if (brdp == (stlbrd_t *) NULL)
  2797. return;
  2798. /*
  2799. * Set up the RX char ignore mask with those RX error types we
  2800. * can ignore. We can get the cd1400 to help us out a little here,
  2801. * it will ignore parity errors and breaks for us.
  2802. */
  2803. portp->rxignoremsk = 0;
  2804. if (tiosp->c_iflag & IGNPAR) {
  2805. portp->rxignoremsk |= (ST_PARITY | ST_FRAMING | ST_OVERRUN);
  2806. cor1 |= COR1_PARIGNORE;
  2807. }
  2808. if (tiosp->c_iflag & IGNBRK) {
  2809. portp->rxignoremsk |= ST_BREAK;
  2810. cor4 |= COR4_IGNBRK;
  2811. }
  2812. portp->rxmarkmsk = ST_OVERRUN;
  2813. if (tiosp->c_iflag & (INPCK | PARMRK))
  2814. portp->rxmarkmsk |= (ST_PARITY | ST_FRAMING);
  2815. if (tiosp->c_iflag & BRKINT)
  2816. portp->rxmarkmsk |= ST_BREAK;
  2817. /*
  2818. * Go through the char size, parity and stop bits and set all the
  2819. * option register appropriately.
  2820. */
  2821. switch (tiosp->c_cflag & CSIZE) {
  2822. case CS5:
  2823. cor1 |= COR1_CHL5;
  2824. break;
  2825. case CS6:
  2826. cor1 |= COR1_CHL6;
  2827. break;
  2828. case CS7:
  2829. cor1 |= COR1_CHL7;
  2830. break;
  2831. default:
  2832. cor1 |= COR1_CHL8;
  2833. break;
  2834. }
  2835. if (tiosp->c_cflag & CSTOPB)
  2836. cor1 |= COR1_STOP2;
  2837. else
  2838. cor1 |= COR1_STOP1;
  2839. if (tiosp->c_cflag & PARENB) {
  2840. if (tiosp->c_cflag & PARODD)
  2841. cor1 |= (COR1_PARENB | COR1_PARODD);
  2842. else
  2843. cor1 |= (COR1_PARENB | COR1_PAREVEN);
  2844. } else {
  2845. cor1 |= COR1_PARNONE;
  2846. }
  2847. /*
  2848. * Set the RX FIFO threshold at 6 chars. This gives a bit of breathing
  2849. * space for hardware flow control and the like. This should be set to
  2850. * VMIN. Also here we will set the RX data timeout to 10ms - this should
  2851. * really be based on VTIME.
  2852. */
  2853. cor3 |= FIFO_RXTHRESHOLD;
  2854. rtpr = 2;
  2855. /*
  2856. * Calculate the baud rate timers. For now we will just assume that
  2857. * the input and output baud are the same. Could have used a baud
  2858. * table here, but this way we can generate virtually any baud rate
  2859. * we like!
  2860. */
  2861. baudrate = tiosp->c_cflag & CBAUD;
  2862. if (baudrate & CBAUDEX) {
  2863. baudrate &= ~CBAUDEX;
  2864. if ((baudrate < 1) || (baudrate > 4))
  2865. tiosp->c_cflag &= ~CBAUDEX;
  2866. else
  2867. baudrate += 15;
  2868. }
  2869. baudrate = stl_baudrates[baudrate];
  2870. if ((tiosp->c_cflag & CBAUD) == B38400) {
  2871. if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  2872. baudrate = 57600;
  2873. else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  2874. baudrate = 115200;
  2875. else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  2876. baudrate = 230400;
  2877. else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  2878. baudrate = 460800;
  2879. else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
  2880. baudrate = (portp->baud_base / portp->custom_divisor);
  2881. }
  2882. if (baudrate > STL_CD1400MAXBAUD)
  2883. baudrate = STL_CD1400MAXBAUD;
  2884. if (baudrate > 0) {
  2885. for (clk = 0; (clk < CD1400_NUMCLKS); clk++) {
  2886. clkdiv = ((portp->clk / stl_cd1400clkdivs[clk]) / baudrate);
  2887. if (clkdiv < 0x100)
  2888. break;
  2889. }
  2890. div = (unsigned char) clkdiv;
  2891. }
  2892. /*
  2893. * Check what form of modem signaling is required and set it up.
  2894. */
  2895. if ((tiosp->c_cflag & CLOCAL) == 0) {
  2896. mcor1 |= MCOR1_DCD;
  2897. mcor2 |= MCOR2_DCD;
  2898. sreron |= SRER_MODEM;
  2899. portp->flags |= ASYNC_CHECK_CD;
  2900. } else {
  2901. portp->flags &= ~ASYNC_CHECK_CD;
  2902. }
  2903. /*
  2904. * Setup cd1400 enhanced modes if we can. In particular we want to
  2905. * handle as much of the flow control as possible automatically. As
  2906. * well as saving a few CPU cycles it will also greatly improve flow
  2907. * control reliability.
  2908. */
  2909. if (tiosp->c_iflag & IXON) {
  2910. cor2 |= COR2_TXIBE;
  2911. cor3 |= COR3_SCD12;
  2912. if (tiosp->c_iflag & IXANY)
  2913. cor2 |= COR2_IXM;
  2914. }
  2915. if (tiosp->c_cflag & CRTSCTS) {
  2916. cor2 |= COR2_CTSAE;
  2917. mcor1 |= FIFO_RTSTHRESHOLD;
  2918. }
  2919. /*
  2920. * All cd1400 register values calculated so go through and set
  2921. * them all up.
  2922. */
  2923. #ifdef DEBUG
  2924. printk("SETPORT: portnr=%d panelnr=%d brdnr=%d\n",
  2925. portp->portnr, portp->panelnr, portp->brdnr);
  2926. printk(" cor1=%x cor2=%x cor3=%x cor4=%x cor5=%x\n",
  2927. cor1, cor2, cor3, cor4, cor5);
  2928. printk(" mcor1=%x mcor2=%x rtpr=%x sreron=%x sreroff=%x\n",
  2929. mcor1, mcor2, rtpr, sreron, sreroff);
  2930. printk(" tcor=%x tbpr=%x rcor=%x rbpr=%x\n", clk, div, clk, div);
  2931. printk(" schr1=%x schr2=%x schr3=%x schr4=%x\n",
  2932. tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP],
  2933. tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP]);
  2934. #endif
  2935. save_flags(flags);
  2936. cli();
  2937. BRDENABLE(portp->brdnr, portp->pagenr);
  2938. stl_cd1400setreg(portp, CAR, (portp->portnr & 0x3));
  2939. srer = stl_cd1400getreg(portp, SRER);
  2940. stl_cd1400setreg(portp, SRER, 0);
  2941. if (stl_cd1400updatereg(portp, COR1, cor1))
  2942. ccr = 1;
  2943. if (stl_cd1400updatereg(portp, COR2, cor2))
  2944. ccr = 1;
  2945. if (stl_cd1400updatereg(portp, COR3, cor3))
  2946. ccr = 1;
  2947. if (ccr) {
  2948. stl_cd1400ccrwait(portp);
  2949. stl_cd1400setreg(portp, CCR, CCR_CORCHANGE);
  2950. }
  2951. stl_cd1400setreg(portp, COR4, cor4);
  2952. stl_cd1400setreg(portp, COR5, cor5);
  2953. stl_cd1400setreg(portp, MCOR1, mcor1);
  2954. stl_cd1400setreg(portp, MCOR2, mcor2);
  2955. if (baudrate > 0) {
  2956. stl_cd1400setreg(portp, TCOR, clk);
  2957. stl_cd1400setreg(portp, TBPR, div);
  2958. stl_cd1400setreg(portp, RCOR, clk);
  2959. stl_cd1400setreg(portp, RBPR, div);
  2960. }
  2961. stl_cd1400setreg(portp, SCHR1, tiosp->c_cc[VSTART]);
  2962. stl_cd1400setreg(portp, SCHR2, tiosp->c_cc[VSTOP]);
  2963. stl_cd1400setreg(portp, SCHR3, tiosp->c_cc[VSTART]);
  2964. stl_cd1400setreg(portp, SCHR4, tiosp->c_cc[VSTOP]);
  2965. stl_cd1400setreg(portp, RTPR, rtpr);
  2966. mcor1 = stl_cd1400getreg(portp, MSVR1);
  2967. if (mcor1 & MSVR1_DCD)
  2968. portp->sigs |= TIOCM_CD;
  2969. else
  2970. portp->sigs &= ~TIOCM_CD;
  2971. stl_cd1400setreg(portp, SRER, ((srer & ~sreroff) | sreron));
  2972. BRDDISABLE(portp->brdnr);
  2973. restore_flags(flags);
  2974. }
  2975. /*****************************************************************************/
  2976. /*
  2977. * Set the state of the DTR and RTS signals.
  2978. */
  2979. static void stl_cd1400setsignals(stlport_t *portp, int dtr, int rts)
  2980. {
  2981. unsigned char msvr1, msvr2;
  2982. unsigned long flags;
  2983. #ifdef DEBUG
  2984. printk("stl_cd1400setsignals(portp=%x,dtr=%d,rts=%d)\n",
  2985. (int) portp, dtr, rts);
  2986. #endif
  2987. msvr1 = 0;
  2988. msvr2 = 0;
  2989. if (dtr > 0)
  2990. msvr1 = MSVR1_DTR;
  2991. if (rts > 0)
  2992. msvr2 = MSVR2_RTS;
  2993. save_flags(flags);
  2994. cli();
  2995. BRDENABLE(portp->brdnr, portp->pagenr);
  2996. stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
  2997. if (rts >= 0)
  2998. stl_cd1400setreg(portp, MSVR2, msvr2);
  2999. if (dtr >= 0)
  3000. stl_cd1400setreg(portp, MSVR1, msvr1);
  3001. BRDDISABLE(portp->brdnr);
  3002. restore_flags(flags);
  3003. }
  3004. /*****************************************************************************/
  3005. /*
  3006. * Return the state of the signals.
  3007. */
  3008. static int stl_cd1400getsignals(stlport_t *portp)
  3009. {
  3010. unsigned char msvr1, msvr2;
  3011. unsigned long flags;
  3012. int sigs;
  3013. #ifdef DEBUG
  3014. printk("stl_cd1400getsignals(portp=%x)\n", (int) portp);
  3015. #endif
  3016. save_flags(flags);
  3017. cli();
  3018. BRDENABLE(portp->brdnr, portp->pagenr);
  3019. stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
  3020. msvr1 = stl_cd1400getreg(portp, MSVR1);
  3021. msvr2 = stl_cd1400getreg(portp, MSVR2);
  3022. BRDDISABLE(portp->brdnr);
  3023. restore_flags(flags);
  3024. sigs = 0;
  3025. sigs |= (msvr1 & MSVR1_DCD) ? TIOCM_CD : 0;
  3026. sigs |= (msvr1 & MSVR1_CTS) ? TIOCM_CTS : 0;
  3027. sigs |= (msvr1 & MSVR1_DTR) ? TIOCM_DTR : 0;
  3028. sigs |= (msvr2 & MSVR2_RTS) ? TIOCM_RTS : 0;
  3029. #if 0
  3030. sigs |= (msvr1 & MSVR1_RI) ? TIOCM_RI : 0;
  3031. sigs |= (msvr1 & MSVR1_DSR) ? TIOCM_DSR : 0;
  3032. #else
  3033. sigs |= TIOCM_DSR;
  3034. #endif
  3035. return(sigs);
  3036. }
  3037. /*****************************************************************************/
  3038. /*
  3039. * Enable/Disable the Transmitter and/or Receiver.
  3040. */
  3041. static void stl_cd1400enablerxtx(stlport_t *portp, int rx, int tx)
  3042. {
  3043. unsigned char ccr;
  3044. unsigned long flags;
  3045. #ifdef DEBUG
  3046. printk("stl_cd1400enablerxtx(portp=%x,rx=%d,tx=%d)\n",
  3047. (int) portp, rx, tx);
  3048. #endif
  3049. ccr = 0;
  3050. if (tx == 0)
  3051. ccr |= CCR_TXDISABLE;
  3052. else if (tx > 0)
  3053. ccr |= CCR_TXENABLE;
  3054. if (rx == 0)
  3055. ccr |= CCR_RXDISABLE;
  3056. else if (rx > 0)
  3057. ccr |= CCR_RXENABLE;
  3058. save_flags(flags);
  3059. cli();
  3060. BRDENABLE(portp->brdnr, portp->pagenr);
  3061. stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
  3062. stl_cd1400ccrwait(portp);
  3063. stl_cd1400setreg(portp, CCR, ccr);
  3064. stl_cd1400ccrwait(portp);
  3065. BRDDISABLE(portp->brdnr);
  3066. restore_flags(flags);
  3067. }
  3068. /*****************************************************************************/
  3069. /*
  3070. * Start/stop the Transmitter and/or Receiver.
  3071. */
  3072. static void stl_cd1400startrxtx(stlport_t *portp, int rx, int tx)
  3073. {
  3074. unsigned char sreron, sreroff;
  3075. unsigned long flags;
  3076. #ifdef DEBUG
  3077. printk("stl_cd1400startrxtx(portp=%x,rx=%d,tx=%d)\n",
  3078. (int) portp, rx, tx);
  3079. #endif
  3080. sreron = 0;
  3081. sreroff = 0;
  3082. if (tx == 0)
  3083. sreroff |= (SRER_TXDATA | SRER_TXEMPTY);
  3084. else if (tx == 1)
  3085. sreron |= SRER_TXDATA;
  3086. else if (tx >= 2)
  3087. sreron |= SRER_TXEMPTY;
  3088. if (rx == 0)
  3089. sreroff |= SRER_RXDATA;
  3090. else if (rx > 0)
  3091. sreron |= SRER_RXDATA;
  3092. save_flags(flags);
  3093. cli();
  3094. BRDENABLE(portp->brdnr, portp->pagenr);
  3095. stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
  3096. stl_cd1400setreg(portp, SRER,
  3097. ((stl_cd1400getreg(portp, SRER) & ~sreroff) | sreron));
  3098. BRDDISABLE(portp->brdnr);
  3099. if (tx > 0)
  3100. set_bit(ASYI_TXBUSY, &portp->istate);
  3101. restore_flags(flags);
  3102. }
  3103. /*****************************************************************************/
  3104. /*
  3105. * Disable all interrupts from this port.
  3106. */
  3107. static void stl_cd1400disableintrs(stlport_t *portp)
  3108. {
  3109. unsigned long flags;
  3110. #ifdef DEBUG
  3111. printk("stl_cd1400disableintrs(portp=%x)\n", (int) portp);
  3112. #endif
  3113. save_flags(flags);
  3114. cli();
  3115. BRDENABLE(portp->brdnr, portp->pagenr);
  3116. stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
  3117. stl_cd1400setreg(portp, SRER, 0);
  3118. BRDDISABLE(portp->brdnr);
  3119. restore_flags(flags);
  3120. }
  3121. /*****************************************************************************/
  3122. static void stl_cd1400sendbreak(stlport_t *portp, int len)
  3123. {
  3124. unsigned long flags;
  3125. #ifdef DEBUG
  3126. printk("stl_cd1400sendbreak(portp=%x,len=%d)\n", (int) portp, len);
  3127. #endif
  3128. save_flags(flags);
  3129. cli();
  3130. BRDENABLE(portp->brdnr, portp->pagenr);
  3131. stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
  3132. stl_cd1400setreg(portp, SRER,
  3133. ((stl_cd1400getreg(portp, SRER) & ~SRER_TXDATA) |
  3134. SRER_TXEMPTY));
  3135. BRDDISABLE(portp->brdnr);
  3136. portp->brklen = len;
  3137. if (len == 1)
  3138. portp->stats.txbreaks++;
  3139. restore_flags(flags);
  3140. }
  3141. /*****************************************************************************/
  3142. /*
  3143. * Take flow control actions...
  3144. */
  3145. static void stl_cd1400flowctrl(stlport_t *portp, int state)
  3146. {
  3147. struct tty_struct *tty;
  3148. unsigned long flags;
  3149. #ifdef DEBUG
  3150. printk("stl_cd1400flowctrl(portp=%x,state=%x)\n", (int) portp, state);
  3151. #endif
  3152. if (portp == (stlport_t *) NULL)
  3153. return;
  3154. tty = portp->tty;
  3155. if (tty == (struct tty_struct *) NULL)
  3156. return;
  3157. save_flags(flags);
  3158. cli();
  3159. BRDENABLE(portp->brdnr, portp->pagenr);
  3160. stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
  3161. if (state) {
  3162. if (tty->termios->c_iflag & IXOFF) {
  3163. stl_cd1400ccrwait(portp);
  3164. stl_cd1400setreg(portp, CCR, CCR_SENDSCHR1);
  3165. portp->stats.rxxon++;
  3166. stl_cd1400ccrwait(portp);
  3167. }
  3168. /*
  3169. * Question: should we return RTS to what it was before? It may
  3170. * have been set by an ioctl... Suppose not, since if you have
  3171. * hardware flow control set then it is pretty silly to go and
  3172. * set the RTS line by hand.
  3173. */
  3174. if (tty->termios->c_cflag & CRTSCTS) {
  3175. stl_cd1400setreg(portp, MCOR1,
  3176. (stl_cd1400getreg(portp, MCOR1) |
  3177. FIFO_RTSTHRESHOLD));
  3178. stl_cd1400setreg(portp, MSVR2, MSVR2_RTS);
  3179. portp->stats.rxrtson++;
  3180. }
  3181. } else {
  3182. if (tty->termios->c_iflag & IXOFF) {
  3183. stl_cd1400ccrwait(portp);
  3184. stl_cd1400setreg(portp, CCR, CCR_SENDSCHR2);
  3185. portp->stats.rxxoff++;
  3186. stl_cd1400ccrwait(portp);
  3187. }
  3188. if (tty->termios->c_cflag & CRTSCTS) {
  3189. stl_cd1400setreg(portp, MCOR1,
  3190. (stl_cd1400getreg(portp, MCOR1) & 0xf0));
  3191. stl_cd1400setreg(portp, MSVR2, 0);
  3192. portp->stats.rxrtsoff++;
  3193. }
  3194. }
  3195. BRDDISABLE(portp->brdnr);
  3196. restore_flags(flags);
  3197. }
  3198. /*****************************************************************************/
  3199. /*
  3200. * Send a flow control character...
  3201. */
  3202. static void stl_cd1400sendflow(stlport_t *portp, int state)
  3203. {
  3204. struct tty_struct *tty;
  3205. unsigned long flags;
  3206. #ifdef DEBUG
  3207. printk("stl_cd1400sendflow(portp=%x,state=%x)\n", (int) portp, state);
  3208. #endif
  3209. if (portp == (stlport_t *) NULL)
  3210. return;
  3211. tty = portp->tty;
  3212. if (tty == (struct tty_struct *) NULL)
  3213. return;
  3214. save_flags(flags);
  3215. cli();
  3216. BRDENABLE(portp->brdnr, portp->pagenr);
  3217. stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
  3218. if (state) {
  3219. stl_cd1400ccrwait(portp);
  3220. stl_cd1400setreg(portp, CCR, CCR_SENDSCHR1);
  3221. portp->stats.rxxon++;
  3222. stl_cd1400ccrwait(portp);
  3223. } else {
  3224. stl_cd1400ccrwait(portp);
  3225. stl_cd1400setreg(portp, CCR, CCR_SENDSCHR2);
  3226. portp->stats.rxxoff++;
  3227. stl_cd1400ccrwait(portp);
  3228. }
  3229. BRDDISABLE(portp->brdnr);
  3230. restore_flags(flags);
  3231. }
  3232. /*****************************************************************************/
  3233. static void stl_cd1400flush(stlport_t *portp)
  3234. {
  3235. unsigned long flags;
  3236. #ifdef DEBUG
  3237. printk("stl_cd1400flush(portp=%x)\n", (int) portp);
  3238. #endif
  3239. if (portp == (stlport_t *) NULL)
  3240. return;
  3241. save_flags(flags);
  3242. cli();
  3243. BRDENABLE(portp->brdnr, portp->pagenr);
  3244. stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
  3245. stl_cd1400ccrwait(portp);
  3246. stl_cd1400setreg(portp, CCR, CCR_TXFLUSHFIFO);
  3247. stl_cd1400ccrwait(portp);
  3248. portp->tx.tail = portp->tx.head;
  3249. BRDDISABLE(portp->brdnr);
  3250. restore_flags(flags);
  3251. }
  3252. /*****************************************************************************/
  3253. /*
  3254. * Return the current state of data flow on this port. This is only
  3255. * really interresting when determining if data has fully completed
  3256. * transmission or not... This is easy for the cd1400, it accurately
  3257. * maintains the busy port flag.
  3258. */
  3259. static int stl_cd1400datastate(stlport_t *portp)
  3260. {
  3261. #ifdef DEBUG
  3262. printk("stl_cd1400datastate(portp=%x)\n", (int) portp);
  3263. #endif
  3264. if (portp == (stlport_t *) NULL)
  3265. return(0);
  3266. return(test_bit(ASYI_TXBUSY, &portp->istate) ? 1 : 0);
  3267. }
  3268. /*****************************************************************************/
  3269. /*
  3270. * Interrupt service routine for cd1400 EasyIO boards.
  3271. */
  3272. static void stl_cd1400eiointr(stlpanel_t *panelp, unsigned int iobase)
  3273. {
  3274. unsigned char svrtype;
  3275. #ifdef DEBUG
  3276. printk("stl_cd1400eiointr(panelp=%x,iobase=%x)\n",
  3277. (int) panelp, iobase);
  3278. #endif
  3279. outb(SVRR, iobase);
  3280. svrtype = inb(iobase + EREG_DATA);
  3281. if (panelp->nrports > 4) {
  3282. outb((SVRR + 0x80), iobase);
  3283. svrtype |= inb(iobase + EREG_DATA);
  3284. }
  3285. if (svrtype & SVRR_RX)
  3286. stl_cd1400rxisr(panelp, iobase);
  3287. else if (svrtype & SVRR_TX)
  3288. stl_cd1400txisr(panelp, iobase);
  3289. else if (svrtype & SVRR_MDM)
  3290. stl_cd1400mdmisr(panelp, iobase);
  3291. }
  3292. /*****************************************************************************/
  3293. /*
  3294. * Interrupt service routine for cd1400 panels.
  3295. */
  3296. static void stl_cd1400echintr(stlpanel_t *panelp, unsigned int iobase)
  3297. {
  3298. unsigned char svrtype;
  3299. #ifdef DEBUG
  3300. printk("stl_cd1400echintr(panelp=%x,iobase=%x)\n", (int) panelp,
  3301. iobase);
  3302. #endif
  3303. outb(SVRR, iobase);
  3304. svrtype = inb(iobase + EREG_DATA);
  3305. outb((SVRR + 0x80), iobase);
  3306. svrtype |= inb(iobase + EREG_DATA);
  3307. if (svrtype & SVRR_RX)
  3308. stl_cd1400rxisr(panelp, iobase);
  3309. else if (svrtype & SVRR_TX)
  3310. stl_cd1400txisr(panelp, iobase);
  3311. else if (svrtype & SVRR_MDM)
  3312. stl_cd1400mdmisr(panelp, iobase);
  3313. }
  3314. /*****************************************************************************/
  3315. /*
  3316. * Unfortunately we need to handle breaks in the TX data stream, since
  3317. * this is the only way to generate them on the cd1400.
  3318. */
  3319. static inline int stl_cd1400breakisr(stlport_t *portp, int ioaddr)
  3320. {
  3321. if (portp->brklen == 1) {
  3322. outb((COR2 + portp->uartaddr), ioaddr);
  3323. outb((inb(ioaddr + EREG_DATA) | COR2_ETC),
  3324. (ioaddr + EREG_DATA));
  3325. outb((TDR + portp->uartaddr), ioaddr);
  3326. outb(ETC_CMD, (ioaddr + EREG_DATA));
  3327. outb(ETC_STARTBREAK, (ioaddr + EREG_DATA));
  3328. outb((SRER + portp->uartaddr), ioaddr);
  3329. outb((inb(ioaddr + EREG_DATA) & ~(SRER_TXDATA | SRER_TXEMPTY)),
  3330. (ioaddr + EREG_DATA));
  3331. return(1);
  3332. } else if (portp->brklen > 1) {
  3333. outb((TDR + portp->uartaddr), ioaddr);
  3334. outb(ETC_CMD, (ioaddr + EREG_DATA));
  3335. outb(ETC_STOPBREAK, (ioaddr + EREG_DATA));
  3336. portp->brklen = -1;
  3337. return(1);
  3338. } else {
  3339. outb((COR2 + portp->uartaddr), ioaddr);
  3340. outb((inb(ioaddr + EREG_DATA) & ~COR2_ETC),
  3341. (ioaddr + EREG_DATA));
  3342. portp->brklen = 0;
  3343. }
  3344. return(0);
  3345. }
  3346. /*****************************************************************************/
  3347. /*
  3348. * Transmit interrupt handler. This has gotta be fast! Handling TX
  3349. * chars is pretty simple, stuff as many as possible from the TX buffer
  3350. * into the cd1400 FIFO. Must also handle TX breaks here, since they
  3351. * are embedded as commands in the data stream. Oh no, had to use a goto!
  3352. * This could be optimized more, will do when I get time...
  3353. * In practice it is possible that interrupts are enabled but that the
  3354. * port has been hung up. Need to handle not having any TX buffer here,
  3355. * this is done by using the side effect that head and tail will also
  3356. * be NULL if the buffer has been freed.
  3357. */
  3358. static void stl_cd1400txisr(stlpanel_t *panelp, int ioaddr)
  3359. {
  3360. stlport_t *portp;
  3361. int len, stlen;
  3362. char *head, *tail;
  3363. unsigned char ioack, srer;
  3364. #ifdef DEBUG
  3365. printk("stl_cd1400txisr(panelp=%x,ioaddr=%x)\n", (int) panelp, ioaddr);
  3366. #endif
  3367. ioack = inb(ioaddr + EREG_TXACK);
  3368. if (((ioack & panelp->ackmask) != 0) ||
  3369. ((ioack & ACK_TYPMASK) != ACK_TYPTX)) {
  3370. printk("STALLION: bad TX interrupt ack value=%x\n", ioack);
  3371. return;
  3372. }
  3373. portp = panelp->ports[(ioack >> 3)];
  3374. /*
  3375. * Unfortunately we need to handle breaks in the data stream, since
  3376. * this is the only way to generate them on the cd1400. Do it now if
  3377. * a break is to be sent.
  3378. */
  3379. if (portp->brklen != 0)
  3380. if (stl_cd1400breakisr(portp, ioaddr))
  3381. goto stl_txalldone;
  3382. head = portp->tx.head;
  3383. tail = portp->tx.tail;
  3384. len = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
  3385. if ((len == 0) || ((len < STL_TXBUFLOW) &&
  3386. (test_bit(ASYI_TXLOW, &portp->istate) == 0))) {
  3387. set_bit(ASYI_TXLOW, &portp->istate);
  3388. schedule_work(&portp->tqueue);
  3389. }
  3390. if (len == 0) {
  3391. outb((SRER + portp->uartaddr), ioaddr);
  3392. srer = inb(ioaddr + EREG_DATA);
  3393. if (srer & SRER_TXDATA) {
  3394. srer = (srer & ~SRER_TXDATA) | SRER_TXEMPTY;
  3395. } else {
  3396. srer &= ~(SRER_TXDATA | SRER_TXEMPTY);
  3397. clear_bit(ASYI_TXBUSY, &portp->istate);
  3398. }
  3399. outb(srer, (ioaddr + EREG_DATA));
  3400. } else {
  3401. len = MIN(len, CD1400_TXFIFOSIZE);
  3402. portp->stats.txtotal += len;
  3403. stlen = MIN(len, ((portp->tx.buf + STL_TXBUFSIZE) - tail));
  3404. outb((TDR + portp->uartaddr), ioaddr);
  3405. outsb((ioaddr + EREG_DATA), tail, stlen);
  3406. len -= stlen;
  3407. tail += stlen;
  3408. if (tail >= (portp->tx.buf + STL_TXBUFSIZE))
  3409. tail = portp->tx.buf;
  3410. if (len > 0) {
  3411. outsb((ioaddr + EREG_DATA), tail, len);
  3412. tail += len;
  3413. }
  3414. portp->tx.tail = tail;
  3415. }
  3416. stl_txalldone:
  3417. outb((EOSRR + portp->uartaddr), ioaddr);
  3418. outb(0, (ioaddr + EREG_DATA));
  3419. }
  3420. /*****************************************************************************/
  3421. /*
  3422. * Receive character interrupt handler. Determine if we have good chars
  3423. * or bad chars and then process appropriately. Good chars are easy
  3424. * just shove the lot into the RX buffer and set all status byte to 0.
  3425. * If a bad RX char then process as required. This routine needs to be
  3426. * fast! In practice it is possible that we get an interrupt on a port
  3427. * that is closed. This can happen on hangups - since they completely
  3428. * shutdown a port not in user context. Need to handle this case.
  3429. */
  3430. static void stl_cd1400rxisr(stlpanel_t *panelp, int ioaddr)
  3431. {
  3432. stlport_t *portp;
  3433. struct tty_struct *tty;
  3434. unsigned int ioack, len, buflen;
  3435. unsigned char status;
  3436. char ch;
  3437. #ifdef DEBUG
  3438. printk("stl_cd1400rxisr(panelp=%x,ioaddr=%x)\n", (int) panelp, ioaddr);
  3439. #endif
  3440. ioack = inb(ioaddr + EREG_RXACK);
  3441. if ((ioack & panelp->ackmask) != 0) {
  3442. printk("STALLION: bad RX interrupt ack value=%x\n", ioack);
  3443. return;
  3444. }
  3445. portp = panelp->ports[(ioack >> 3)];
  3446. tty = portp->tty;
  3447. if ((ioack & ACK_TYPMASK) == ACK_TYPRXGOOD) {
  3448. outb((RDCR + portp->uartaddr), ioaddr);
  3449. len = inb(ioaddr + EREG_DATA);
  3450. if ((tty == (struct tty_struct *) NULL) ||
  3451. (tty->flip.char_buf_ptr == (char *) NULL) ||
  3452. ((buflen = TTY_FLIPBUF_SIZE - tty->flip.count) == 0)) {
  3453. len = MIN(len, sizeof(stl_unwanted));
  3454. outb((RDSR + portp->uartaddr), ioaddr);
  3455. insb((ioaddr + EREG_DATA), &stl_unwanted[0], len);
  3456. portp->stats.rxlost += len;
  3457. portp->stats.rxtotal += len;
  3458. } else {
  3459. len = MIN(len, buflen);
  3460. if (len > 0) {
  3461. outb((RDSR + portp->uartaddr), ioaddr);
  3462. insb((ioaddr + EREG_DATA), tty->flip.char_buf_ptr, len);
  3463. memset(tty->flip.flag_buf_ptr, 0, len);
  3464. tty->flip.flag_buf_ptr += len;
  3465. tty->flip.char_buf_ptr += len;
  3466. tty->flip.count += len;
  3467. tty_schedule_flip(tty);
  3468. portp->stats.rxtotal += len;
  3469. }
  3470. }
  3471. } else if ((ioack & ACK_TYPMASK) == ACK_TYPRXBAD) {
  3472. outb((RDSR + portp->uartaddr), ioaddr);
  3473. status = inb(ioaddr + EREG_DATA);
  3474. ch = inb(ioaddr + EREG_DATA);
  3475. if (status & ST_PARITY)
  3476. portp->stats.rxparity++;
  3477. if (status & ST_FRAMING)
  3478. portp->stats.rxframing++;
  3479. if (status & ST_OVERRUN)
  3480. portp->stats.rxoverrun++;
  3481. if (status & ST_BREAK)
  3482. portp->stats.rxbreaks++;
  3483. if (status & ST_SCHARMASK) {
  3484. if ((status & ST_SCHARMASK) == ST_SCHAR1)
  3485. portp->stats.txxon++;
  3486. if ((status & ST_SCHARMASK) == ST_SCHAR2)
  3487. portp->stats.txxoff++;
  3488. goto stl_rxalldone;
  3489. }
  3490. if ((tty != (struct tty_struct *) NULL) &&
  3491. ((portp->rxignoremsk & status) == 0)) {
  3492. if (portp->rxmarkmsk & status) {
  3493. if (status & ST_BREAK) {
  3494. status = TTY_BREAK;
  3495. if (portp->flags & ASYNC_SAK) {
  3496. do_SAK(tty);
  3497. BRDENABLE(portp->brdnr, portp->pagenr);
  3498. }
  3499. } else if (status & ST_PARITY) {
  3500. status = TTY_PARITY;
  3501. } else if (status & ST_FRAMING) {
  3502. status = TTY_FRAME;
  3503. } else if(status & ST_OVERRUN) {
  3504. status = TTY_OVERRUN;
  3505. } else {
  3506. status = 0;
  3507. }
  3508. } else {
  3509. status = 0;
  3510. }
  3511. if (tty->flip.char_buf_ptr != (char *) NULL) {
  3512. if (tty->flip.count < TTY_FLIPBUF_SIZE) {
  3513. *tty->flip.flag_buf_ptr++ = status;
  3514. *tty->flip.char_buf_ptr++ = ch;
  3515. tty->flip.count++;
  3516. }
  3517. tty_schedule_flip(tty);
  3518. }
  3519. }
  3520. } else {
  3521. printk("STALLION: bad RX interrupt ack value=%x\n", ioack);
  3522. return;
  3523. }
  3524. stl_rxalldone:
  3525. outb((EOSRR + portp->uartaddr), ioaddr);
  3526. outb(0, (ioaddr + EREG_DATA));
  3527. }
  3528. /*****************************************************************************/
  3529. /*
  3530. * Modem interrupt handler. The is called when the modem signal line
  3531. * (DCD) has changed state. Leave most of the work to the off-level
  3532. * processing routine.
  3533. */
  3534. static void stl_cd1400mdmisr(stlpanel_t *panelp, int ioaddr)
  3535. {
  3536. stlport_t *portp;
  3537. unsigned int ioack;
  3538. unsigned char misr;
  3539. #ifdef DEBUG
  3540. printk("stl_cd1400mdmisr(panelp=%x)\n", (int) panelp);
  3541. #endif
  3542. ioack = inb(ioaddr + EREG_MDACK);
  3543. if (((ioack & panelp->ackmask) != 0) ||
  3544. ((ioack & ACK_TYPMASK) != ACK_TYPMDM)) {
  3545. printk("STALLION: bad MODEM interrupt ack value=%x\n", ioack);
  3546. return;
  3547. }
  3548. portp = panelp->ports[(ioack >> 3)];
  3549. outb((MISR + portp->uartaddr), ioaddr);
  3550. misr = inb(ioaddr + EREG_DATA);
  3551. if (misr & MISR_DCD) {
  3552. set_bit(ASYI_DCDCHANGE, &portp->istate);
  3553. schedule_work(&portp->tqueue);
  3554. portp->stats.modem++;
  3555. }
  3556. outb((EOSRR + portp->uartaddr), ioaddr);
  3557. outb(0, (ioaddr + EREG_DATA));
  3558. }
  3559. /*****************************************************************************/
  3560. /* SC26198 HARDWARE FUNCTIONS */
  3561. /*****************************************************************************/
  3562. /*
  3563. * These functions get/set/update the registers of the sc26198 UARTs.
  3564. * Access to the sc26198 registers is via an address/data io port pair.
  3565. * (Maybe should make this inline...)
  3566. */
  3567. static int stl_sc26198getreg(stlport_t *portp, int regnr)
  3568. {
  3569. outb((regnr | portp->uartaddr), (portp->ioaddr + XP_ADDR));
  3570. return(inb(portp->ioaddr + XP_DATA));
  3571. }
  3572. static void stl_sc26198setreg(stlport_t *portp, int regnr, int value)
  3573. {
  3574. outb((regnr | portp->uartaddr), (portp->ioaddr + XP_ADDR));
  3575. outb(value, (portp->ioaddr + XP_DATA));
  3576. }
  3577. static int stl_sc26198updatereg(stlport_t *portp, int regnr, int value)
  3578. {
  3579. outb((regnr | portp->uartaddr), (portp->ioaddr + XP_ADDR));
  3580. if (inb(portp->ioaddr + XP_DATA) != value) {
  3581. outb(value, (portp->ioaddr + XP_DATA));
  3582. return(1);
  3583. }
  3584. return(0);
  3585. }
  3586. /*****************************************************************************/
  3587. /*
  3588. * Functions to get and set the sc26198 global registers.
  3589. */
  3590. static int stl_sc26198getglobreg(stlport_t *portp, int regnr)
  3591. {
  3592. outb(regnr, (portp->ioaddr + XP_ADDR));
  3593. return(inb(portp->ioaddr + XP_DATA));
  3594. }
  3595. #if 0
  3596. static void stl_sc26198setglobreg(stlport_t *portp, int regnr, int value)
  3597. {
  3598. outb(regnr, (portp->ioaddr + XP_ADDR));
  3599. outb(value, (portp->ioaddr + XP_DATA));
  3600. }
  3601. #endif
  3602. /*****************************************************************************/
  3603. /*
  3604. * Inbitialize the UARTs in a panel. We don't care what sort of board
  3605. * these ports are on - since the port io registers are almost
  3606. * identical when dealing with ports.
  3607. */
  3608. static int stl_sc26198panelinit(stlbrd_t *brdp, stlpanel_t *panelp)
  3609. {
  3610. int chipmask, i;
  3611. int nrchips, ioaddr;
  3612. #ifdef DEBUG
  3613. printk("stl_sc26198panelinit(brdp=%x,panelp=%x)\n",
  3614. (int) brdp, (int) panelp);
  3615. #endif
  3616. BRDENABLE(panelp->brdnr, panelp->pagenr);
  3617. /*
  3618. * Check that each chip is present and started up OK.
  3619. */
  3620. chipmask = 0;
  3621. nrchips = (panelp->nrports + 4) / SC26198_PORTS;
  3622. if (brdp->brdtype == BRD_ECHPCI)
  3623. outb(panelp->pagenr, brdp->ioctrl);
  3624. for (i = 0; (i < nrchips); i++) {
  3625. ioaddr = panelp->iobase + (i * 4);
  3626. outb(SCCR, (ioaddr + XP_ADDR));
  3627. outb(CR_RESETALL, (ioaddr + XP_DATA));
  3628. outb(TSTR, (ioaddr + XP_ADDR));
  3629. if (inb(ioaddr + XP_DATA) != 0) {
  3630. printk("STALLION: sc26198 not responding, "
  3631. "brd=%d panel=%d chip=%d\n",
  3632. panelp->brdnr, panelp->panelnr, i);
  3633. continue;
  3634. }
  3635. chipmask |= (0x1 << i);
  3636. outb(GCCR, (ioaddr + XP_ADDR));
  3637. outb(GCCR_IVRTYPCHANACK, (ioaddr + XP_DATA));
  3638. outb(WDTRCR, (ioaddr + XP_ADDR));
  3639. outb(0xff, (ioaddr + XP_DATA));
  3640. }
  3641. BRDDISABLE(panelp->brdnr);
  3642. return(chipmask);
  3643. }
  3644. /*****************************************************************************/
  3645. /*
  3646. * Initialize hardware specific port registers.
  3647. */
  3648. static void stl_sc26198portinit(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp)
  3649. {
  3650. #ifdef DEBUG
  3651. printk("stl_sc26198portinit(brdp=%x,panelp=%x,portp=%x)\n",
  3652. (int) brdp, (int) panelp, (int) portp);
  3653. #endif
  3654. if ((brdp == (stlbrd_t *) NULL) || (panelp == (stlpanel_t *) NULL) ||
  3655. (portp == (stlport_t *) NULL))
  3656. return;
  3657. portp->ioaddr = panelp->iobase + ((portp->portnr < 8) ? 0 : 4);
  3658. portp->uartaddr = (portp->portnr & 0x07) << 4;
  3659. portp->pagenr = panelp->pagenr;
  3660. portp->hwid = 0x1;
  3661. BRDENABLE(portp->brdnr, portp->pagenr);
  3662. stl_sc26198setreg(portp, IOPCR, IOPCR_SETSIGS);
  3663. BRDDISABLE(portp->brdnr);
  3664. }
  3665. /*****************************************************************************/
  3666. /*
  3667. * Set up the sc26198 registers for a port based on the termios port
  3668. * settings.
  3669. */
  3670. static void stl_sc26198setport(stlport_t *portp, struct termios *tiosp)
  3671. {
  3672. stlbrd_t *brdp;
  3673. unsigned long flags;
  3674. unsigned int baudrate;
  3675. unsigned char mr0, mr1, mr2, clk;
  3676. unsigned char imron, imroff, iopr, ipr;
  3677. mr0 = 0;
  3678. mr1 = 0;
  3679. mr2 = 0;
  3680. clk = 0;
  3681. iopr = 0;
  3682. imron = 0;
  3683. imroff = 0;
  3684. brdp = stl_brds[portp->brdnr];
  3685. if (brdp == (stlbrd_t *) NULL)
  3686. return;
  3687. /*
  3688. * Set up the RX char ignore mask with those RX error types we
  3689. * can ignore.
  3690. */
  3691. portp->rxignoremsk = 0;
  3692. if (tiosp->c_iflag & IGNPAR)
  3693. portp->rxignoremsk |= (SR_RXPARITY | SR_RXFRAMING |
  3694. SR_RXOVERRUN);
  3695. if (tiosp->c_iflag & IGNBRK)
  3696. portp->rxignoremsk |= SR_RXBREAK;
  3697. portp->rxmarkmsk = SR_RXOVERRUN;
  3698. if (tiosp->c_iflag & (INPCK | PARMRK))
  3699. portp->rxmarkmsk |= (SR_RXPARITY | SR_RXFRAMING);
  3700. if (tiosp->c_iflag & BRKINT)
  3701. portp->rxmarkmsk |= SR_RXBREAK;
  3702. /*
  3703. * Go through the char size, parity and stop bits and set all the
  3704. * option register appropriately.
  3705. */
  3706. switch (tiosp->c_cflag & CSIZE) {
  3707. case CS5:
  3708. mr1 |= MR1_CS5;
  3709. break;
  3710. case CS6:
  3711. mr1 |= MR1_CS6;
  3712. break;
  3713. case CS7:
  3714. mr1 |= MR1_CS7;
  3715. break;
  3716. default:
  3717. mr1 |= MR1_CS8;
  3718. break;
  3719. }
  3720. if (tiosp->c_cflag & CSTOPB)
  3721. mr2 |= MR2_STOP2;
  3722. else
  3723. mr2 |= MR2_STOP1;
  3724. if (tiosp->c_cflag & PARENB) {
  3725. if (tiosp->c_cflag & PARODD)
  3726. mr1 |= (MR1_PARENB | MR1_PARODD);
  3727. else
  3728. mr1 |= (MR1_PARENB | MR1_PAREVEN);
  3729. } else {
  3730. mr1 |= MR1_PARNONE;
  3731. }
  3732. mr1 |= MR1_ERRBLOCK;
  3733. /*
  3734. * Set the RX FIFO threshold at 8 chars. This gives a bit of breathing
  3735. * space for hardware flow control and the like. This should be set to
  3736. * VMIN.
  3737. */
  3738. mr2 |= MR2_RXFIFOHALF;
  3739. /*
  3740. * Calculate the baud rate timers. For now we will just assume that
  3741. * the input and output baud are the same. The sc26198 has a fixed
  3742. * baud rate table, so only discrete baud rates possible.
  3743. */
  3744. baudrate = tiosp->c_cflag & CBAUD;
  3745. if (baudrate & CBAUDEX) {
  3746. baudrate &= ~CBAUDEX;
  3747. if ((baudrate < 1) || (baudrate > 4))
  3748. tiosp->c_cflag &= ~CBAUDEX;
  3749. else
  3750. baudrate += 15;
  3751. }
  3752. baudrate = stl_baudrates[baudrate];
  3753. if ((tiosp->c_cflag & CBAUD) == B38400) {
  3754. if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  3755. baudrate = 57600;
  3756. else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  3757. baudrate = 115200;
  3758. else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  3759. baudrate = 230400;
  3760. else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  3761. baudrate = 460800;
  3762. else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
  3763. baudrate = (portp->baud_base / portp->custom_divisor);
  3764. }
  3765. if (baudrate > STL_SC26198MAXBAUD)
  3766. baudrate = STL_SC26198MAXBAUD;
  3767. if (baudrate > 0) {
  3768. for (clk = 0; (clk < SC26198_NRBAUDS); clk++) {
  3769. if (baudrate <= sc26198_baudtable[clk])
  3770. break;
  3771. }
  3772. }
  3773. /*
  3774. * Check what form of modem signaling is required and set it up.
  3775. */
  3776. if (tiosp->c_cflag & CLOCAL) {
  3777. portp->flags &= ~ASYNC_CHECK_CD;
  3778. } else {
  3779. iopr |= IOPR_DCDCOS;
  3780. imron |= IR_IOPORT;
  3781. portp->flags |= ASYNC_CHECK_CD;
  3782. }
  3783. /*
  3784. * Setup sc26198 enhanced modes if we can. In particular we want to
  3785. * handle as much of the flow control as possible automatically. As
  3786. * well as saving a few CPU cycles it will also greatly improve flow
  3787. * control reliability.
  3788. */
  3789. if (tiosp->c_iflag & IXON) {
  3790. mr0 |= MR0_SWFTX | MR0_SWFT;
  3791. imron |= IR_XONXOFF;
  3792. } else {
  3793. imroff |= IR_XONXOFF;
  3794. }
  3795. if (tiosp->c_iflag & IXOFF)
  3796. mr0 |= MR0_SWFRX;
  3797. if (tiosp->c_cflag & CRTSCTS) {
  3798. mr2 |= MR2_AUTOCTS;
  3799. mr1 |= MR1_AUTORTS;
  3800. }
  3801. /*
  3802. * All sc26198 register values calculated so go through and set
  3803. * them all up.
  3804. */
  3805. #ifdef DEBUG
  3806. printk("SETPORT: portnr=%d panelnr=%d brdnr=%d\n",
  3807. portp->portnr, portp->panelnr, portp->brdnr);
  3808. printk(" mr0=%x mr1=%x mr2=%x clk=%x\n", mr0, mr1, mr2, clk);
  3809. printk(" iopr=%x imron=%x imroff=%x\n", iopr, imron, imroff);
  3810. printk(" schr1=%x schr2=%x schr3=%x schr4=%x\n",
  3811. tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP],
  3812. tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP]);
  3813. #endif
  3814. save_flags(flags);
  3815. cli();
  3816. BRDENABLE(portp->brdnr, portp->pagenr);
  3817. stl_sc26198setreg(portp, IMR, 0);
  3818. stl_sc26198updatereg(portp, MR0, mr0);
  3819. stl_sc26198updatereg(portp, MR1, mr1);
  3820. stl_sc26198setreg(portp, SCCR, CR_RXERRBLOCK);
  3821. stl_sc26198updatereg(portp, MR2, mr2);
  3822. stl_sc26198updatereg(portp, IOPIOR,
  3823. ((stl_sc26198getreg(portp, IOPIOR) & ~IPR_CHANGEMASK) | iopr));
  3824. if (baudrate > 0) {
  3825. stl_sc26198setreg(portp, TXCSR, clk);
  3826. stl_sc26198setreg(portp, RXCSR, clk);
  3827. }
  3828. stl_sc26198setreg(portp, XONCR, tiosp->c_cc[VSTART]);
  3829. stl_sc26198setreg(portp, XOFFCR, tiosp->c_cc[VSTOP]);
  3830. ipr = stl_sc26198getreg(portp, IPR);
  3831. if (ipr & IPR_DCD)
  3832. portp->sigs &= ~TIOCM_CD;
  3833. else
  3834. portp->sigs |= TIOCM_CD;
  3835. portp->imr = (portp->imr & ~imroff) | imron;
  3836. stl_sc26198setreg(portp, IMR, portp->imr);
  3837. BRDDISABLE(portp->brdnr);
  3838. restore_flags(flags);
  3839. }
  3840. /*****************************************************************************/
  3841. /*
  3842. * Set the state of the DTR and RTS signals.
  3843. */
  3844. static void stl_sc26198setsignals(stlport_t *portp, int dtr, int rts)
  3845. {
  3846. unsigned char iopioron, iopioroff;
  3847. unsigned long flags;
  3848. #ifdef DEBUG
  3849. printk("stl_sc26198setsignals(portp=%x,dtr=%d,rts=%d)\n",
  3850. (int) portp, dtr, rts);
  3851. #endif
  3852. iopioron = 0;
  3853. iopioroff = 0;
  3854. if (dtr == 0)
  3855. iopioroff |= IPR_DTR;
  3856. else if (dtr > 0)
  3857. iopioron |= IPR_DTR;
  3858. if (rts == 0)
  3859. iopioroff |= IPR_RTS;
  3860. else if (rts > 0)
  3861. iopioron |= IPR_RTS;
  3862. save_flags(flags);
  3863. cli();
  3864. BRDENABLE(portp->brdnr, portp->pagenr);
  3865. stl_sc26198setreg(portp, IOPIOR,
  3866. ((stl_sc26198getreg(portp, IOPIOR) & ~iopioroff) | iopioron));
  3867. BRDDISABLE(portp->brdnr);
  3868. restore_flags(flags);
  3869. }
  3870. /*****************************************************************************/
  3871. /*
  3872. * Return the state of the signals.
  3873. */
  3874. static int stl_sc26198getsignals(stlport_t *portp)
  3875. {
  3876. unsigned char ipr;
  3877. unsigned long flags;
  3878. int sigs;
  3879. #ifdef DEBUG
  3880. printk("stl_sc26198getsignals(portp=%x)\n", (int) portp);
  3881. #endif
  3882. save_flags(flags);
  3883. cli();
  3884. BRDENABLE(portp->brdnr, portp->pagenr);
  3885. ipr = stl_sc26198getreg(portp, IPR);
  3886. BRDDISABLE(portp->brdnr);
  3887. restore_flags(flags);
  3888. sigs = 0;
  3889. sigs |= (ipr & IPR_DCD) ? 0 : TIOCM_CD;
  3890. sigs |= (ipr & IPR_CTS) ? 0 : TIOCM_CTS;
  3891. sigs |= (ipr & IPR_DTR) ? 0: TIOCM_DTR;
  3892. sigs |= (ipr & IPR_RTS) ? 0: TIOCM_RTS;
  3893. sigs |= TIOCM_DSR;
  3894. return(sigs);
  3895. }
  3896. /*****************************************************************************/
  3897. /*
  3898. * Enable/Disable the Transmitter and/or Receiver.
  3899. */
  3900. static void stl_sc26198enablerxtx(stlport_t *portp, int rx, int tx)
  3901. {
  3902. unsigned char ccr;
  3903. unsigned long flags;
  3904. #ifdef DEBUG
  3905. printk("stl_sc26198enablerxtx(portp=%x,rx=%d,tx=%d)\n",
  3906. (int) portp, rx, tx);
  3907. #endif
  3908. ccr = portp->crenable;
  3909. if (tx == 0)
  3910. ccr &= ~CR_TXENABLE;
  3911. else if (tx > 0)
  3912. ccr |= CR_TXENABLE;
  3913. if (rx == 0)
  3914. ccr &= ~CR_RXENABLE;
  3915. else if (rx > 0)
  3916. ccr |= CR_RXENABLE;
  3917. save_flags(flags);
  3918. cli();
  3919. BRDENABLE(portp->brdnr, portp->pagenr);
  3920. stl_sc26198setreg(portp, SCCR, ccr);
  3921. BRDDISABLE(portp->brdnr);
  3922. portp->crenable = ccr;
  3923. restore_flags(flags);
  3924. }
  3925. /*****************************************************************************/
  3926. /*
  3927. * Start/stop the Transmitter and/or Receiver.
  3928. */
  3929. static void stl_sc26198startrxtx(stlport_t *portp, int rx, int tx)
  3930. {
  3931. unsigned char imr;
  3932. unsigned long flags;
  3933. #ifdef DEBUG
  3934. printk("stl_sc26198startrxtx(portp=%x,rx=%d,tx=%d)\n",
  3935. (int) portp, rx, tx);
  3936. #endif
  3937. imr = portp->imr;
  3938. if (tx == 0)
  3939. imr &= ~IR_TXRDY;
  3940. else if (tx == 1)
  3941. imr |= IR_TXRDY;
  3942. if (rx == 0)
  3943. imr &= ~(IR_RXRDY | IR_RXBREAK | IR_RXWATCHDOG);
  3944. else if (rx > 0)
  3945. imr |= IR_RXRDY | IR_RXBREAK | IR_RXWATCHDOG;
  3946. save_flags(flags);
  3947. cli();
  3948. BRDENABLE(portp->brdnr, portp->pagenr);
  3949. stl_sc26198setreg(portp, IMR, imr);
  3950. BRDDISABLE(portp->brdnr);
  3951. portp->imr = imr;
  3952. if (tx > 0)
  3953. set_bit(ASYI_TXBUSY, &portp->istate);
  3954. restore_flags(flags);
  3955. }
  3956. /*****************************************************************************/
  3957. /*
  3958. * Disable all interrupts from this port.
  3959. */
  3960. static void stl_sc26198disableintrs(stlport_t *portp)
  3961. {
  3962. unsigned long flags;
  3963. #ifdef DEBUG
  3964. printk("stl_sc26198disableintrs(portp=%x)\n", (int) portp);
  3965. #endif
  3966. save_flags(flags);
  3967. cli();
  3968. BRDENABLE(portp->brdnr, portp->pagenr);
  3969. portp->imr = 0;
  3970. stl_sc26198setreg(portp, IMR, 0);
  3971. BRDDISABLE(portp->brdnr);
  3972. restore_flags(flags);
  3973. }
  3974. /*****************************************************************************/
  3975. static void stl_sc26198sendbreak(stlport_t *portp, int len)
  3976. {
  3977. unsigned long flags;
  3978. #ifdef DEBUG
  3979. printk("stl_sc26198sendbreak(portp=%x,len=%d)\n", (int) portp, len);
  3980. #endif
  3981. save_flags(flags);
  3982. cli();
  3983. BRDENABLE(portp->brdnr, portp->pagenr);
  3984. if (len == 1) {
  3985. stl_sc26198setreg(portp, SCCR, CR_TXSTARTBREAK);
  3986. portp->stats.txbreaks++;
  3987. } else {
  3988. stl_sc26198setreg(portp, SCCR, CR_TXSTOPBREAK);
  3989. }
  3990. BRDDISABLE(portp->brdnr);
  3991. restore_flags(flags);
  3992. }
  3993. /*****************************************************************************/
  3994. /*
  3995. * Take flow control actions...
  3996. */
  3997. static void stl_sc26198flowctrl(stlport_t *portp, int state)
  3998. {
  3999. struct tty_struct *tty;
  4000. unsigned long flags;
  4001. unsigned char mr0;
  4002. #ifdef DEBUG
  4003. printk("stl_sc26198flowctrl(portp=%x,state=%x)\n", (int) portp, state);
  4004. #endif
  4005. if (portp == (stlport_t *) NULL)
  4006. return;
  4007. tty = portp->tty;
  4008. if (tty == (struct tty_struct *) NULL)
  4009. return;
  4010. save_flags(flags);
  4011. cli();
  4012. BRDENABLE(portp->brdnr, portp->pagenr);
  4013. if (state) {
  4014. if (tty->termios->c_iflag & IXOFF) {
  4015. mr0 = stl_sc26198getreg(portp, MR0);
  4016. stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
  4017. stl_sc26198setreg(portp, SCCR, CR_TXSENDXON);
  4018. mr0 |= MR0_SWFRX;
  4019. portp->stats.rxxon++;
  4020. stl_sc26198wait(portp);
  4021. stl_sc26198setreg(portp, MR0, mr0);
  4022. }
  4023. /*
  4024. * Question: should we return RTS to what it was before? It may
  4025. * have been set by an ioctl... Suppose not, since if you have
  4026. * hardware flow control set then it is pretty silly to go and
  4027. * set the RTS line by hand.
  4028. */
  4029. if (tty->termios->c_cflag & CRTSCTS) {
  4030. stl_sc26198setreg(portp, MR1,
  4031. (stl_sc26198getreg(portp, MR1) | MR1_AUTORTS));
  4032. stl_sc26198setreg(portp, IOPIOR,
  4033. (stl_sc26198getreg(portp, IOPIOR) | IOPR_RTS));
  4034. portp->stats.rxrtson++;
  4035. }
  4036. } else {
  4037. if (tty->termios->c_iflag & IXOFF) {
  4038. mr0 = stl_sc26198getreg(portp, MR0);
  4039. stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
  4040. stl_sc26198setreg(portp, SCCR, CR_TXSENDXOFF);
  4041. mr0 &= ~MR0_SWFRX;
  4042. portp->stats.rxxoff++;
  4043. stl_sc26198wait(portp);
  4044. stl_sc26198setreg(portp, MR0, mr0);
  4045. }
  4046. if (tty->termios->c_cflag & CRTSCTS) {
  4047. stl_sc26198setreg(portp, MR1,
  4048. (stl_sc26198getreg(portp, MR1) & ~MR1_AUTORTS));
  4049. stl_sc26198setreg(portp, IOPIOR,
  4050. (stl_sc26198getreg(portp, IOPIOR) & ~IOPR_RTS));
  4051. portp->stats.rxrtsoff++;
  4052. }
  4053. }
  4054. BRDDISABLE(portp->brdnr);
  4055. restore_flags(flags);
  4056. }
  4057. /*****************************************************************************/
  4058. /*
  4059. * Send a flow control character.
  4060. */
  4061. static void stl_sc26198sendflow(stlport_t *portp, int state)
  4062. {
  4063. struct tty_struct *tty;
  4064. unsigned long flags;
  4065. unsigned char mr0;
  4066. #ifdef DEBUG
  4067. printk("stl_sc26198sendflow(portp=%x,state=%x)\n", (int) portp, state);
  4068. #endif
  4069. if (portp == (stlport_t *) NULL)
  4070. return;
  4071. tty = portp->tty;
  4072. if (tty == (struct tty_struct *) NULL)
  4073. return;
  4074. save_flags(flags);
  4075. cli();
  4076. BRDENABLE(portp->brdnr, portp->pagenr);
  4077. if (state) {
  4078. mr0 = stl_sc26198getreg(portp, MR0);
  4079. stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
  4080. stl_sc26198setreg(portp, SCCR, CR_TXSENDXON);
  4081. mr0 |= MR0_SWFRX;
  4082. portp->stats.rxxon++;
  4083. stl_sc26198wait(portp);
  4084. stl_sc26198setreg(portp, MR0, mr0);
  4085. } else {
  4086. mr0 = stl_sc26198getreg(portp, MR0);
  4087. stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
  4088. stl_sc26198setreg(portp, SCCR, CR_TXSENDXOFF);
  4089. mr0 &= ~MR0_SWFRX;
  4090. portp->stats.rxxoff++;
  4091. stl_sc26198wait(portp);
  4092. stl_sc26198setreg(portp, MR0, mr0);
  4093. }
  4094. BRDDISABLE(portp->brdnr);
  4095. restore_flags(flags);
  4096. }
  4097. /*****************************************************************************/
  4098. static void stl_sc26198flush(stlport_t *portp)
  4099. {
  4100. unsigned long flags;
  4101. #ifdef DEBUG
  4102. printk("stl_sc26198flush(portp=%x)\n", (int) portp);
  4103. #endif
  4104. if (portp == (stlport_t *) NULL)
  4105. return;
  4106. save_flags(flags);
  4107. cli();
  4108. BRDENABLE(portp->brdnr, portp->pagenr);
  4109. stl_sc26198setreg(portp, SCCR, CR_TXRESET);
  4110. stl_sc26198setreg(portp, SCCR, portp->crenable);
  4111. BRDDISABLE(portp->brdnr);
  4112. portp->tx.tail = portp->tx.head;
  4113. restore_flags(flags);
  4114. }
  4115. /*****************************************************************************/
  4116. /*
  4117. * Return the current state of data flow on this port. This is only
  4118. * really interresting when determining if data has fully completed
  4119. * transmission or not... The sc26198 interrupt scheme cannot
  4120. * determine when all data has actually drained, so we need to
  4121. * check the port statusy register to be sure.
  4122. */
  4123. static int stl_sc26198datastate(stlport_t *portp)
  4124. {
  4125. unsigned long flags;
  4126. unsigned char sr;
  4127. #ifdef DEBUG
  4128. printk("stl_sc26198datastate(portp=%x)\n", (int) portp);
  4129. #endif
  4130. if (portp == (stlport_t *) NULL)
  4131. return(0);
  4132. if (test_bit(ASYI_TXBUSY, &portp->istate))
  4133. return(1);
  4134. save_flags(flags);
  4135. cli();
  4136. BRDENABLE(portp->brdnr, portp->pagenr);
  4137. sr = stl_sc26198getreg(portp, SR);
  4138. BRDDISABLE(portp->brdnr);
  4139. restore_flags(flags);
  4140. return((sr & SR_TXEMPTY) ? 0 : 1);
  4141. }
  4142. /*****************************************************************************/
  4143. /*
  4144. * Delay for a small amount of time, to give the sc26198 a chance
  4145. * to process a command...
  4146. */
  4147. static void stl_sc26198wait(stlport_t *portp)
  4148. {
  4149. int i;
  4150. #ifdef DEBUG
  4151. printk("stl_sc26198wait(portp=%x)\n", (int) portp);
  4152. #endif
  4153. if (portp == (stlport_t *) NULL)
  4154. return;
  4155. for (i = 0; (i < 20); i++)
  4156. stl_sc26198getglobreg(portp, TSTR);
  4157. }
  4158. /*****************************************************************************/
  4159. /*
  4160. * If we are TX flow controlled and in IXANY mode then we may
  4161. * need to unflow control here. We gotta do this because of the
  4162. * automatic flow control modes of the sc26198.
  4163. */
  4164. static inline void stl_sc26198txunflow(stlport_t *portp, struct tty_struct *tty)
  4165. {
  4166. unsigned char mr0;
  4167. mr0 = stl_sc26198getreg(portp, MR0);
  4168. stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
  4169. stl_sc26198setreg(portp, SCCR, CR_HOSTXON);
  4170. stl_sc26198wait(portp);
  4171. stl_sc26198setreg(portp, MR0, mr0);
  4172. clear_bit(ASYI_TXFLOWED, &portp->istate);
  4173. }
  4174. /*****************************************************************************/
  4175. /*
  4176. * Interrupt service routine for sc26198 panels.
  4177. */
  4178. static void stl_sc26198intr(stlpanel_t *panelp, unsigned int iobase)
  4179. {
  4180. stlport_t *portp;
  4181. unsigned int iack;
  4182. /*
  4183. * Work around bug in sc26198 chip... Cannot have A6 address
  4184. * line of UART high, else iack will be returned as 0.
  4185. */
  4186. outb(0, (iobase + 1));
  4187. iack = inb(iobase + XP_IACK);
  4188. portp = panelp->ports[(iack & IVR_CHANMASK) + ((iobase & 0x4) << 1)];
  4189. if (iack & IVR_RXDATA)
  4190. stl_sc26198rxisr(portp, iack);
  4191. else if (iack & IVR_TXDATA)
  4192. stl_sc26198txisr(portp);
  4193. else
  4194. stl_sc26198otherisr(portp, iack);
  4195. }
  4196. /*****************************************************************************/
  4197. /*
  4198. * Transmit interrupt handler. This has gotta be fast! Handling TX
  4199. * chars is pretty simple, stuff as many as possible from the TX buffer
  4200. * into the sc26198 FIFO.
  4201. * In practice it is possible that interrupts are enabled but that the
  4202. * port has been hung up. Need to handle not having any TX buffer here,
  4203. * this is done by using the side effect that head and tail will also
  4204. * be NULL if the buffer has been freed.
  4205. */
  4206. static void stl_sc26198txisr(stlport_t *portp)
  4207. {
  4208. unsigned int ioaddr;
  4209. unsigned char mr0;
  4210. int len, stlen;
  4211. char *head, *tail;
  4212. #ifdef DEBUG
  4213. printk("stl_sc26198txisr(portp=%x)\n", (int) portp);
  4214. #endif
  4215. ioaddr = portp->ioaddr;
  4216. head = portp->tx.head;
  4217. tail = portp->tx.tail;
  4218. len = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
  4219. if ((len == 0) || ((len < STL_TXBUFLOW) &&
  4220. (test_bit(ASYI_TXLOW, &portp->istate) == 0))) {
  4221. set_bit(ASYI_TXLOW, &portp->istate);
  4222. schedule_work(&portp->tqueue);
  4223. }
  4224. if (len == 0) {
  4225. outb((MR0 | portp->uartaddr), (ioaddr + XP_ADDR));
  4226. mr0 = inb(ioaddr + XP_DATA);
  4227. if ((mr0 & MR0_TXMASK) == MR0_TXEMPTY) {
  4228. portp->imr &= ~IR_TXRDY;
  4229. outb((IMR | portp->uartaddr), (ioaddr + XP_ADDR));
  4230. outb(portp->imr, (ioaddr + XP_DATA));
  4231. clear_bit(ASYI_TXBUSY, &portp->istate);
  4232. } else {
  4233. mr0 |= ((mr0 & ~MR0_TXMASK) | MR0_TXEMPTY);
  4234. outb(mr0, (ioaddr + XP_DATA));
  4235. }
  4236. } else {
  4237. len = MIN(len, SC26198_TXFIFOSIZE);
  4238. portp->stats.txtotal += len;
  4239. stlen = MIN(len, ((portp->tx.buf + STL_TXBUFSIZE) - tail));
  4240. outb(GTXFIFO, (ioaddr + XP_ADDR));
  4241. outsb((ioaddr + XP_DATA), tail, stlen);
  4242. len -= stlen;
  4243. tail += stlen;
  4244. if (tail >= (portp->tx.buf + STL_TXBUFSIZE))
  4245. tail = portp->tx.buf;
  4246. if (len > 0) {
  4247. outsb((ioaddr + XP_DATA), tail, len);
  4248. tail += len;
  4249. }
  4250. portp->tx.tail = tail;
  4251. }
  4252. }
  4253. /*****************************************************************************/
  4254. /*
  4255. * Receive character interrupt handler. Determine if we have good chars
  4256. * or bad chars and then process appropriately. Good chars are easy
  4257. * just shove the lot into the RX buffer and set all status byte to 0.
  4258. * If a bad RX char then process as required. This routine needs to be
  4259. * fast! In practice it is possible that we get an interrupt on a port
  4260. * that is closed. This can happen on hangups - since they completely
  4261. * shutdown a port not in user context. Need to handle this case.
  4262. */
  4263. static void stl_sc26198rxisr(stlport_t *portp, unsigned int iack)
  4264. {
  4265. struct tty_struct *tty;
  4266. unsigned int len, buflen, ioaddr;
  4267. #ifdef DEBUG
  4268. printk("stl_sc26198rxisr(portp=%x,iack=%x)\n", (int) portp, iack);
  4269. #endif
  4270. tty = portp->tty;
  4271. ioaddr = portp->ioaddr;
  4272. outb(GIBCR, (ioaddr + XP_ADDR));
  4273. len = inb(ioaddr + XP_DATA) + 1;
  4274. if ((iack & IVR_TYPEMASK) == IVR_RXDATA) {
  4275. if ((tty == (struct tty_struct *) NULL) ||
  4276. (tty->flip.char_buf_ptr == (char *) NULL) ||
  4277. ((buflen = TTY_FLIPBUF_SIZE - tty->flip.count) == 0)) {
  4278. len = MIN(len, sizeof(stl_unwanted));
  4279. outb(GRXFIFO, (ioaddr + XP_ADDR));
  4280. insb((ioaddr + XP_DATA), &stl_unwanted[0], len);
  4281. portp->stats.rxlost += len;
  4282. portp->stats.rxtotal += len;
  4283. } else {
  4284. len = MIN(len, buflen);
  4285. if (len > 0) {
  4286. outb(GRXFIFO, (ioaddr + XP_ADDR));
  4287. insb((ioaddr + XP_DATA), tty->flip.char_buf_ptr, len);
  4288. memset(tty->flip.flag_buf_ptr, 0, len);
  4289. tty->flip.flag_buf_ptr += len;
  4290. tty->flip.char_buf_ptr += len;
  4291. tty->flip.count += len;
  4292. tty_schedule_flip(tty);
  4293. portp->stats.rxtotal += len;
  4294. }
  4295. }
  4296. } else {
  4297. stl_sc26198rxbadchars(portp);
  4298. }
  4299. /*
  4300. * If we are TX flow controlled and in IXANY mode then we may need
  4301. * to unflow control here. We gotta do this because of the automatic
  4302. * flow control modes of the sc26198.
  4303. */
  4304. if (test_bit(ASYI_TXFLOWED, &portp->istate)) {
  4305. if ((tty != (struct tty_struct *) NULL) &&
  4306. (tty->termios != (struct termios *) NULL) &&
  4307. (tty->termios->c_iflag & IXANY)) {
  4308. stl_sc26198txunflow(portp, tty);
  4309. }
  4310. }
  4311. }
  4312. /*****************************************************************************/
  4313. /*
  4314. * Process an RX bad character.
  4315. */
  4316. static inline void stl_sc26198rxbadch(stlport_t *portp, unsigned char status, char ch)
  4317. {
  4318. struct tty_struct *tty;
  4319. unsigned int ioaddr;
  4320. tty = portp->tty;
  4321. ioaddr = portp->ioaddr;
  4322. if (status & SR_RXPARITY)
  4323. portp->stats.rxparity++;
  4324. if (status & SR_RXFRAMING)
  4325. portp->stats.rxframing++;
  4326. if (status & SR_RXOVERRUN)
  4327. portp->stats.rxoverrun++;
  4328. if (status & SR_RXBREAK)
  4329. portp->stats.rxbreaks++;
  4330. if ((tty != (struct tty_struct *) NULL) &&
  4331. ((portp->rxignoremsk & status) == 0)) {
  4332. if (portp->rxmarkmsk & status) {
  4333. if (status & SR_RXBREAK) {
  4334. status = TTY_BREAK;
  4335. if (portp->flags & ASYNC_SAK) {
  4336. do_SAK(tty);
  4337. BRDENABLE(portp->brdnr, portp->pagenr);
  4338. }
  4339. } else if (status & SR_RXPARITY) {
  4340. status = TTY_PARITY;
  4341. } else if (status & SR_RXFRAMING) {
  4342. status = TTY_FRAME;
  4343. } else if(status & SR_RXOVERRUN) {
  4344. status = TTY_OVERRUN;
  4345. } else {
  4346. status = 0;
  4347. }
  4348. } else {
  4349. status = 0;
  4350. }
  4351. if (tty->flip.char_buf_ptr != (char *) NULL) {
  4352. if (tty->flip.count < TTY_FLIPBUF_SIZE) {
  4353. *tty->flip.flag_buf_ptr++ = status;
  4354. *tty->flip.char_buf_ptr++ = ch;
  4355. tty->flip.count++;
  4356. }
  4357. tty_schedule_flip(tty);
  4358. }
  4359. if (status == 0)
  4360. portp->stats.rxtotal++;
  4361. }
  4362. }
  4363. /*****************************************************************************/
  4364. /*
  4365. * Process all characters in the RX FIFO of the UART. Check all char
  4366. * status bytes as well, and process as required. We need to check
  4367. * all bytes in the FIFO, in case some more enter the FIFO while we
  4368. * are here. To get the exact character error type we need to switch
  4369. * into CHAR error mode (that is why we need to make sure we empty
  4370. * the FIFO).
  4371. */
  4372. static void stl_sc26198rxbadchars(stlport_t *portp)
  4373. {
  4374. unsigned char status, mr1;
  4375. char ch;
  4376. /*
  4377. * To get the precise error type for each character we must switch
  4378. * back into CHAR error mode.
  4379. */
  4380. mr1 = stl_sc26198getreg(portp, MR1);
  4381. stl_sc26198setreg(portp, MR1, (mr1 & ~MR1_ERRBLOCK));
  4382. while ((status = stl_sc26198getreg(portp, SR)) & SR_RXRDY) {
  4383. stl_sc26198setreg(portp, SCCR, CR_CLEARRXERR);
  4384. ch = stl_sc26198getreg(portp, RXFIFO);
  4385. stl_sc26198rxbadch(portp, status, ch);
  4386. }
  4387. /*
  4388. * To get correct interrupt class we must switch back into BLOCK
  4389. * error mode.
  4390. */
  4391. stl_sc26198setreg(portp, MR1, mr1);
  4392. }
  4393. /*****************************************************************************/
  4394. /*
  4395. * Other interrupt handler. This includes modem signals, flow
  4396. * control actions, etc. Most stuff is left to off-level interrupt
  4397. * processing time.
  4398. */
  4399. static void stl_sc26198otherisr(stlport_t *portp, unsigned int iack)
  4400. {
  4401. unsigned char cir, ipr, xisr;
  4402. #ifdef DEBUG
  4403. printk("stl_sc26198otherisr(portp=%x,iack=%x)\n", (int) portp, iack);
  4404. #endif
  4405. cir = stl_sc26198getglobreg(portp, CIR);
  4406. switch (cir & CIR_SUBTYPEMASK) {
  4407. case CIR_SUBCOS:
  4408. ipr = stl_sc26198getreg(portp, IPR);
  4409. if (ipr & IPR_DCDCHANGE) {
  4410. set_bit(ASYI_DCDCHANGE, &portp->istate);
  4411. schedule_work(&portp->tqueue);
  4412. portp->stats.modem++;
  4413. }
  4414. break;
  4415. case CIR_SUBXONXOFF:
  4416. xisr = stl_sc26198getreg(portp, XISR);
  4417. if (xisr & XISR_RXXONGOT) {
  4418. set_bit(ASYI_TXFLOWED, &portp->istate);
  4419. portp->stats.txxoff++;
  4420. }
  4421. if (xisr & XISR_RXXOFFGOT) {
  4422. clear_bit(ASYI_TXFLOWED, &portp->istate);
  4423. portp->stats.txxon++;
  4424. }
  4425. break;
  4426. case CIR_SUBBREAK:
  4427. stl_sc26198setreg(portp, SCCR, CR_BREAKRESET);
  4428. stl_sc26198rxbadchars(portp);
  4429. break;
  4430. default:
  4431. break;
  4432. }
  4433. }
  4434. /*****************************************************************************/