istallion.c 123 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674
  1. /*****************************************************************************/
  2. /*
  3. * istallion.c -- stallion intelligent 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. */
  17. /*****************************************************************************/
  18. #include <linux/module.h>
  19. #include <linux/slab.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/tty.h>
  22. #include <linux/tty_flip.h>
  23. #include <linux/serial.h>
  24. #include <linux/cdk.h>
  25. #include <linux/comstats.h>
  26. #include <linux/istallion.h>
  27. #include <linux/ioport.h>
  28. #include <linux/delay.h>
  29. #include <linux/init.h>
  30. #include <linux/device.h>
  31. #include <linux/wait.h>
  32. #include <linux/eisa.h>
  33. #include <linux/ctype.h>
  34. #include <asm/io.h>
  35. #include <asm/uaccess.h>
  36. #include <linux/pci.h>
  37. /*****************************************************************************/
  38. /*
  39. * Define different board types. Not all of the following board types
  40. * are supported by this driver. But I will use the standard "assigned"
  41. * board numbers. Currently supported boards are abbreviated as:
  42. * ECP = EasyConnection 8/64, ONB = ONboard, BBY = Brumby and
  43. * STAL = Stallion.
  44. */
  45. #define BRD_UNKNOWN 0
  46. #define BRD_STALLION 1
  47. #define BRD_BRUMBY4 2
  48. #define BRD_ONBOARD2 3
  49. #define BRD_ONBOARD 4
  50. #define BRD_ONBOARDE 7
  51. #define BRD_ECP 23
  52. #define BRD_ECPE 24
  53. #define BRD_ECPMC 25
  54. #define BRD_ECPPCI 29
  55. #define BRD_BRUMBY BRD_BRUMBY4
  56. /*
  57. * Define a configuration structure to hold the board configuration.
  58. * Need to set this up in the code (for now) with the boards that are
  59. * to be configured into the system. This is what needs to be modified
  60. * when adding/removing/modifying boards. Each line entry in the
  61. * stli_brdconf[] array is a board. Each line contains io/irq/memory
  62. * ranges for that board (as well as what type of board it is).
  63. * Some examples:
  64. * { BRD_ECP, 0x2a0, 0, 0xcc000, 0, 0 },
  65. * This line will configure an EasyConnection 8/64 at io address 2a0,
  66. * and shared memory address of cc000. Multiple EasyConnection 8/64
  67. * boards can share the same shared memory address space. No interrupt
  68. * is required for this board type.
  69. * Another example:
  70. * { BRD_ECPE, 0x5000, 0, 0x80000000, 0, 0 },
  71. * This line will configure an EasyConnection 8/64 EISA in slot 5 and
  72. * shared memory address of 0x80000000 (2 GByte). Multiple
  73. * EasyConnection 8/64 EISA boards can share the same shared memory
  74. * address space. No interrupt is required for this board type.
  75. * Another example:
  76. * { BRD_ONBOARD, 0x240, 0, 0xd0000, 0, 0 },
  77. * This line will configure an ONboard (ISA type) at io address 240,
  78. * and shared memory address of d0000. Multiple ONboards can share
  79. * the same shared memory address space. No interrupt required.
  80. * Another example:
  81. * { BRD_BRUMBY4, 0x360, 0, 0xc8000, 0, 0 },
  82. * This line will configure a Brumby board (any number of ports!) at
  83. * io address 360 and shared memory address of c8000. All Brumby boards
  84. * configured into a system must have their own separate io and memory
  85. * addresses. No interrupt is required.
  86. * Another example:
  87. * { BRD_STALLION, 0x330, 0, 0xd0000, 0, 0 },
  88. * This line will configure an original Stallion board at io address 330
  89. * and shared memory address d0000 (this would only be valid for a "V4.0"
  90. * or Rev.O Stallion board). All Stallion boards configured into the
  91. * system must have their own separate io and memory addresses. No
  92. * interrupt is required.
  93. */
  94. struct stlconf {
  95. int brdtype;
  96. int ioaddr1;
  97. int ioaddr2;
  98. unsigned long memaddr;
  99. int irq;
  100. int irqtype;
  101. };
  102. static unsigned int stli_nrbrds;
  103. /* stli_lock must NOT be taken holding brd_lock */
  104. static spinlock_t stli_lock; /* TTY logic lock */
  105. static spinlock_t brd_lock; /* Board logic lock */
  106. /*
  107. * There is some experimental EISA board detection code in this driver.
  108. * By default it is disabled, but for those that want to try it out,
  109. * then set the define below to be 1.
  110. */
  111. #define STLI_EISAPROBE 0
  112. /*****************************************************************************/
  113. /*
  114. * Define some important driver characteristics. Device major numbers
  115. * allocated as per Linux Device Registry.
  116. */
  117. #ifndef STL_SIOMEMMAJOR
  118. #define STL_SIOMEMMAJOR 28
  119. #endif
  120. #ifndef STL_SERIALMAJOR
  121. #define STL_SERIALMAJOR 24
  122. #endif
  123. #ifndef STL_CALLOUTMAJOR
  124. #define STL_CALLOUTMAJOR 25
  125. #endif
  126. /*****************************************************************************/
  127. /*
  128. * Define our local driver identity first. Set up stuff to deal with
  129. * all the local structures required by a serial tty driver.
  130. */
  131. static char *stli_drvtitle = "Stallion Intelligent Multiport Serial Driver";
  132. static char *stli_drvname = "istallion";
  133. static char *stli_drvversion = "5.6.0";
  134. static char *stli_serialname = "ttyE";
  135. static struct tty_driver *stli_serial;
  136. #define STLI_TXBUFSIZE 4096
  137. /*
  138. * Use a fast local buffer for cooked characters. Typically a whole
  139. * bunch of cooked characters come in for a port, 1 at a time. So we
  140. * save those up into a local buffer, then write out the whole lot
  141. * with a large memcpy. Just use 1 buffer for all ports, since its
  142. * use it is only need for short periods of time by each port.
  143. */
  144. static char *stli_txcookbuf;
  145. static int stli_txcooksize;
  146. static int stli_txcookrealsize;
  147. static struct tty_struct *stli_txcooktty;
  148. /*
  149. * Define a local default termios struct. All ports will be created
  150. * with this termios initially. Basically all it defines is a raw port
  151. * at 9600 baud, 8 data bits, no parity, 1 stop bit.
  152. */
  153. static struct ktermios stli_deftermios = {
  154. .c_cflag = (B9600 | CS8 | CREAD | HUPCL | CLOCAL),
  155. .c_cc = INIT_C_CC,
  156. .c_ispeed = 9600,
  157. .c_ospeed = 9600,
  158. };
  159. /*
  160. * Define global stats structures. Not used often, and can be
  161. * re-used for each stats call.
  162. */
  163. static comstats_t stli_comstats;
  164. static combrd_t stli_brdstats;
  165. static struct asystats stli_cdkstats;
  166. /*****************************************************************************/
  167. static DEFINE_MUTEX(stli_brdslock);
  168. static struct stlibrd *stli_brds[STL_MAXBRDS];
  169. static int stli_shared;
  170. /*
  171. * Per board state flags. Used with the state field of the board struct.
  172. * Not really much here... All we need to do is keep track of whether
  173. * the board has been detected, and whether it is actually running a slave
  174. * or not.
  175. */
  176. #define BST_FOUND 0x1
  177. #define BST_STARTED 0x2
  178. #define BST_PROBED 0x4
  179. /*
  180. * Define the set of port state flags. These are marked for internal
  181. * state purposes only, usually to do with the state of communications
  182. * with the slave. Most of them need to be updated atomically, so always
  183. * use the bit setting operations (unless protected by cli/sti).
  184. */
  185. #define ST_INITIALIZING 1
  186. #define ST_OPENING 2
  187. #define ST_CLOSING 3
  188. #define ST_CMDING 4
  189. #define ST_TXBUSY 5
  190. #define ST_RXING 6
  191. #define ST_DOFLUSHRX 7
  192. #define ST_DOFLUSHTX 8
  193. #define ST_DOSIGS 9
  194. #define ST_RXSTOP 10
  195. #define ST_GETSIGS 11
  196. /*
  197. * Define an array of board names as printable strings. Handy for
  198. * referencing boards when printing trace and stuff.
  199. */
  200. static char *stli_brdnames[] = {
  201. "Unknown",
  202. "Stallion",
  203. "Brumby",
  204. "ONboard-MC",
  205. "ONboard",
  206. "Brumby",
  207. "Brumby",
  208. "ONboard-EI",
  209. NULL,
  210. "ONboard",
  211. "ONboard-MC",
  212. "ONboard-MC",
  213. NULL,
  214. NULL,
  215. NULL,
  216. NULL,
  217. NULL,
  218. NULL,
  219. NULL,
  220. NULL,
  221. "EasyIO",
  222. "EC8/32-AT",
  223. "EC8/32-MC",
  224. "EC8/64-AT",
  225. "EC8/64-EI",
  226. "EC8/64-MC",
  227. "EC8/32-PCI",
  228. "EC8/64-PCI",
  229. "EasyIO-PCI",
  230. "EC/RA-PCI",
  231. };
  232. /*****************************************************************************/
  233. /*
  234. * Define some string labels for arguments passed from the module
  235. * load line. These allow for easy board definitions, and easy
  236. * modification of the io, memory and irq resoucres.
  237. */
  238. static char *board0[8];
  239. static char *board1[8];
  240. static char *board2[8];
  241. static char *board3[8];
  242. static char **stli_brdsp[] = {
  243. (char **) &board0,
  244. (char **) &board1,
  245. (char **) &board2,
  246. (char **) &board3
  247. };
  248. /*
  249. * Define a set of common board names, and types. This is used to
  250. * parse any module arguments.
  251. */
  252. static struct stlibrdtype {
  253. char *name;
  254. int type;
  255. } stli_brdstr[] = {
  256. { "stallion", BRD_STALLION },
  257. { "1", BRD_STALLION },
  258. { "brumby", BRD_BRUMBY },
  259. { "brumby4", BRD_BRUMBY },
  260. { "brumby/4", BRD_BRUMBY },
  261. { "brumby-4", BRD_BRUMBY },
  262. { "brumby8", BRD_BRUMBY },
  263. { "brumby/8", BRD_BRUMBY },
  264. { "brumby-8", BRD_BRUMBY },
  265. { "brumby16", BRD_BRUMBY },
  266. { "brumby/16", BRD_BRUMBY },
  267. { "brumby-16", BRD_BRUMBY },
  268. { "2", BRD_BRUMBY },
  269. { "onboard2", BRD_ONBOARD2 },
  270. { "onboard-2", BRD_ONBOARD2 },
  271. { "onboard/2", BRD_ONBOARD2 },
  272. { "onboard-mc", BRD_ONBOARD2 },
  273. { "onboard/mc", BRD_ONBOARD2 },
  274. { "onboard-mca", BRD_ONBOARD2 },
  275. { "onboard/mca", BRD_ONBOARD2 },
  276. { "3", BRD_ONBOARD2 },
  277. { "onboard", BRD_ONBOARD },
  278. { "onboardat", BRD_ONBOARD },
  279. { "4", BRD_ONBOARD },
  280. { "onboarde", BRD_ONBOARDE },
  281. { "onboard-e", BRD_ONBOARDE },
  282. { "onboard/e", BRD_ONBOARDE },
  283. { "onboard-ei", BRD_ONBOARDE },
  284. { "onboard/ei", BRD_ONBOARDE },
  285. { "7", BRD_ONBOARDE },
  286. { "ecp", BRD_ECP },
  287. { "ecpat", BRD_ECP },
  288. { "ec8/64", BRD_ECP },
  289. { "ec8/64-at", BRD_ECP },
  290. { "ec8/64-isa", BRD_ECP },
  291. { "23", BRD_ECP },
  292. { "ecpe", BRD_ECPE },
  293. { "ecpei", BRD_ECPE },
  294. { "ec8/64-e", BRD_ECPE },
  295. { "ec8/64-ei", BRD_ECPE },
  296. { "24", BRD_ECPE },
  297. { "ecpmc", BRD_ECPMC },
  298. { "ec8/64-mc", BRD_ECPMC },
  299. { "ec8/64-mca", BRD_ECPMC },
  300. { "25", BRD_ECPMC },
  301. { "ecppci", BRD_ECPPCI },
  302. { "ec/ra", BRD_ECPPCI },
  303. { "ec/ra-pc", BRD_ECPPCI },
  304. { "ec/ra-pci", BRD_ECPPCI },
  305. { "29", BRD_ECPPCI },
  306. };
  307. /*
  308. * Define the module agruments.
  309. */
  310. MODULE_AUTHOR("Greg Ungerer");
  311. MODULE_DESCRIPTION("Stallion Intelligent Multiport Serial Driver");
  312. MODULE_LICENSE("GPL");
  313. module_param_array(board0, charp, NULL, 0);
  314. MODULE_PARM_DESC(board0, "Board 0 config -> name[,ioaddr[,memaddr]");
  315. module_param_array(board1, charp, NULL, 0);
  316. MODULE_PARM_DESC(board1, "Board 1 config -> name[,ioaddr[,memaddr]");
  317. module_param_array(board2, charp, NULL, 0);
  318. MODULE_PARM_DESC(board2, "Board 2 config -> name[,ioaddr[,memaddr]");
  319. module_param_array(board3, charp, NULL, 0);
  320. MODULE_PARM_DESC(board3, "Board 3 config -> name[,ioaddr[,memaddr]");
  321. #if STLI_EISAPROBE != 0
  322. /*
  323. * Set up a default memory address table for EISA board probing.
  324. * The default addresses are all bellow 1Mbyte, which has to be the
  325. * case anyway. They should be safe, since we only read values from
  326. * them, and interrupts are disabled while we do it. If the higher
  327. * memory support is compiled in then we also try probing around
  328. * the 1Gb, 2Gb and 3Gb areas as well...
  329. */
  330. static unsigned long stli_eisamemprobeaddrs[] = {
  331. 0xc0000, 0xd0000, 0xe0000, 0xf0000,
  332. 0x80000000, 0x80010000, 0x80020000, 0x80030000,
  333. 0x40000000, 0x40010000, 0x40020000, 0x40030000,
  334. 0xc0000000, 0xc0010000, 0xc0020000, 0xc0030000,
  335. 0xff000000, 0xff010000, 0xff020000, 0xff030000,
  336. };
  337. static int stli_eisamempsize = ARRAY_SIZE(stli_eisamemprobeaddrs);
  338. #endif
  339. /*
  340. * Define the Stallion PCI vendor and device IDs.
  341. */
  342. #ifndef PCI_DEVICE_ID_ECRA
  343. #define PCI_DEVICE_ID_ECRA 0x0004
  344. #endif
  345. static struct pci_device_id istallion_pci_tbl[] = {
  346. { PCI_DEVICE(PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_ECRA), },
  347. { 0 }
  348. };
  349. MODULE_DEVICE_TABLE(pci, istallion_pci_tbl);
  350. static struct pci_driver stli_pcidriver;
  351. /*****************************************************************************/
  352. /*
  353. * Hardware configuration info for ECP boards. These defines apply
  354. * to the directly accessible io ports of the ECP. There is a set of
  355. * defines for each ECP board type, ISA, EISA, MCA and PCI.
  356. */
  357. #define ECP_IOSIZE 4
  358. #define ECP_MEMSIZE (128 * 1024)
  359. #define ECP_PCIMEMSIZE (256 * 1024)
  360. #define ECP_ATPAGESIZE (4 * 1024)
  361. #define ECP_MCPAGESIZE (4 * 1024)
  362. #define ECP_EIPAGESIZE (64 * 1024)
  363. #define ECP_PCIPAGESIZE (64 * 1024)
  364. #define STL_EISAID 0x8c4e
  365. /*
  366. * Important defines for the ISA class of ECP board.
  367. */
  368. #define ECP_ATIREG 0
  369. #define ECP_ATCONFR 1
  370. #define ECP_ATMEMAR 2
  371. #define ECP_ATMEMPR 3
  372. #define ECP_ATSTOP 0x1
  373. #define ECP_ATINTENAB 0x10
  374. #define ECP_ATENABLE 0x20
  375. #define ECP_ATDISABLE 0x00
  376. #define ECP_ATADDRMASK 0x3f000
  377. #define ECP_ATADDRSHFT 12
  378. /*
  379. * Important defines for the EISA class of ECP board.
  380. */
  381. #define ECP_EIIREG 0
  382. #define ECP_EIMEMARL 1
  383. #define ECP_EICONFR 2
  384. #define ECP_EIMEMARH 3
  385. #define ECP_EIENABLE 0x1
  386. #define ECP_EIDISABLE 0x0
  387. #define ECP_EISTOP 0x4
  388. #define ECP_EIEDGE 0x00
  389. #define ECP_EILEVEL 0x80
  390. #define ECP_EIADDRMASKL 0x00ff0000
  391. #define ECP_EIADDRSHFTL 16
  392. #define ECP_EIADDRMASKH 0xff000000
  393. #define ECP_EIADDRSHFTH 24
  394. #define ECP_EIBRDENAB 0xc84
  395. #define ECP_EISAID 0x4
  396. /*
  397. * Important defines for the Micro-channel class of ECP board.
  398. * (It has a lot in common with the ISA boards.)
  399. */
  400. #define ECP_MCIREG 0
  401. #define ECP_MCCONFR 1
  402. #define ECP_MCSTOP 0x20
  403. #define ECP_MCENABLE 0x80
  404. #define ECP_MCDISABLE 0x00
  405. /*
  406. * Important defines for the PCI class of ECP board.
  407. * (It has a lot in common with the other ECP boards.)
  408. */
  409. #define ECP_PCIIREG 0
  410. #define ECP_PCICONFR 1
  411. #define ECP_PCISTOP 0x01
  412. /*
  413. * Hardware configuration info for ONboard and Brumby boards. These
  414. * defines apply to the directly accessible io ports of these boards.
  415. */
  416. #define ONB_IOSIZE 16
  417. #define ONB_MEMSIZE (64 * 1024)
  418. #define ONB_ATPAGESIZE (64 * 1024)
  419. #define ONB_MCPAGESIZE (64 * 1024)
  420. #define ONB_EIMEMSIZE (128 * 1024)
  421. #define ONB_EIPAGESIZE (64 * 1024)
  422. /*
  423. * Important defines for the ISA class of ONboard board.
  424. */
  425. #define ONB_ATIREG 0
  426. #define ONB_ATMEMAR 1
  427. #define ONB_ATCONFR 2
  428. #define ONB_ATSTOP 0x4
  429. #define ONB_ATENABLE 0x01
  430. #define ONB_ATDISABLE 0x00
  431. #define ONB_ATADDRMASK 0xff0000
  432. #define ONB_ATADDRSHFT 16
  433. #define ONB_MEMENABLO 0
  434. #define ONB_MEMENABHI 0x02
  435. /*
  436. * Important defines for the EISA class of ONboard board.
  437. */
  438. #define ONB_EIIREG 0
  439. #define ONB_EIMEMARL 1
  440. #define ONB_EICONFR 2
  441. #define ONB_EIMEMARH 3
  442. #define ONB_EIENABLE 0x1
  443. #define ONB_EIDISABLE 0x0
  444. #define ONB_EISTOP 0x4
  445. #define ONB_EIEDGE 0x00
  446. #define ONB_EILEVEL 0x80
  447. #define ONB_EIADDRMASKL 0x00ff0000
  448. #define ONB_EIADDRSHFTL 16
  449. #define ONB_EIADDRMASKH 0xff000000
  450. #define ONB_EIADDRSHFTH 24
  451. #define ONB_EIBRDENAB 0xc84
  452. #define ONB_EISAID 0x1
  453. /*
  454. * Important defines for the Brumby boards. They are pretty simple,
  455. * there is not much that is programmably configurable.
  456. */
  457. #define BBY_IOSIZE 16
  458. #define BBY_MEMSIZE (64 * 1024)
  459. #define BBY_PAGESIZE (16 * 1024)
  460. #define BBY_ATIREG 0
  461. #define BBY_ATCONFR 1
  462. #define BBY_ATSTOP 0x4
  463. /*
  464. * Important defines for the Stallion boards. They are pretty simple,
  465. * there is not much that is programmably configurable.
  466. */
  467. #define STAL_IOSIZE 16
  468. #define STAL_MEMSIZE (64 * 1024)
  469. #define STAL_PAGESIZE (64 * 1024)
  470. /*
  471. * Define the set of status register values for EasyConnection panels.
  472. * The signature will return with the status value for each panel. From
  473. * this we can determine what is attached to the board - before we have
  474. * actually down loaded any code to it.
  475. */
  476. #define ECH_PNLSTATUS 2
  477. #define ECH_PNL16PORT 0x20
  478. #define ECH_PNLIDMASK 0x07
  479. #define ECH_PNLXPID 0x40
  480. #define ECH_PNLINTRPEND 0x80
  481. /*
  482. * Define some macros to do things to the board. Even those these boards
  483. * are somewhat related there is often significantly different ways of
  484. * doing some operation on it (like enable, paging, reset, etc). So each
  485. * board class has a set of functions which do the commonly required
  486. * operations. The macros below basically just call these functions,
  487. * generally checking for a NULL function - which means that the board
  488. * needs nothing done to it to achieve this operation!
  489. */
  490. #define EBRDINIT(brdp) \
  491. if (brdp->init != NULL) \
  492. (* brdp->init)(brdp)
  493. #define EBRDENABLE(brdp) \
  494. if (brdp->enable != NULL) \
  495. (* brdp->enable)(brdp);
  496. #define EBRDDISABLE(brdp) \
  497. if (brdp->disable != NULL) \
  498. (* brdp->disable)(brdp);
  499. #define EBRDINTR(brdp) \
  500. if (brdp->intr != NULL) \
  501. (* brdp->intr)(brdp);
  502. #define EBRDRESET(brdp) \
  503. if (brdp->reset != NULL) \
  504. (* brdp->reset)(brdp);
  505. #define EBRDGETMEMPTR(brdp,offset) \
  506. (* brdp->getmemptr)(brdp, offset, __LINE__)
  507. /*
  508. * Define the maximal baud rate, and the default baud base for ports.
  509. */
  510. #define STL_MAXBAUD 460800
  511. #define STL_BAUDBASE 115200
  512. #define STL_CLOSEDELAY (5 * HZ / 10)
  513. /*****************************************************************************/
  514. /*
  515. * Define macros to extract a brd or port number from a minor number.
  516. */
  517. #define MINOR2BRD(min) (((min) & 0xc0) >> 6)
  518. #define MINOR2PORT(min) ((min) & 0x3f)
  519. /*****************************************************************************/
  520. /*
  521. * Prototype all functions in this driver!
  522. */
  523. static int stli_parsebrd(struct stlconf *confp, char **argp);
  524. static int stli_open(struct tty_struct *tty, struct file *filp);
  525. static void stli_close(struct tty_struct *tty, struct file *filp);
  526. static int stli_write(struct tty_struct *tty, const unsigned char *buf, int count);
  527. static void stli_putchar(struct tty_struct *tty, unsigned char ch);
  528. static void stli_flushchars(struct tty_struct *tty);
  529. static int stli_writeroom(struct tty_struct *tty);
  530. static int stli_charsinbuffer(struct tty_struct *tty);
  531. static int stli_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
  532. static void stli_settermios(struct tty_struct *tty, struct ktermios *old);
  533. static void stli_throttle(struct tty_struct *tty);
  534. static void stli_unthrottle(struct tty_struct *tty);
  535. static void stli_stop(struct tty_struct *tty);
  536. static void stli_start(struct tty_struct *tty);
  537. static void stli_flushbuffer(struct tty_struct *tty);
  538. static void stli_breakctl(struct tty_struct *tty, int state);
  539. static void stli_waituntilsent(struct tty_struct *tty, int timeout);
  540. static void stli_sendxchar(struct tty_struct *tty, char ch);
  541. static void stli_hangup(struct tty_struct *tty);
  542. static int stli_portinfo(struct stlibrd *brdp, struct stliport *portp, int portnr, char *pos);
  543. static int stli_brdinit(struct stlibrd *brdp);
  544. static int stli_startbrd(struct stlibrd *brdp);
  545. static ssize_t stli_memread(struct file *fp, char __user *buf, size_t count, loff_t *offp);
  546. static ssize_t stli_memwrite(struct file *fp, const char __user *buf, size_t count, loff_t *offp);
  547. static int stli_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg);
  548. static void stli_brdpoll(struct stlibrd *brdp, cdkhdr_t __iomem *hdrp);
  549. static void stli_poll(unsigned long arg);
  550. static int stli_hostcmd(struct stlibrd *brdp, struct stliport *portp);
  551. static int stli_initopen(struct stlibrd *brdp, struct stliport *portp);
  552. static int stli_rawopen(struct stlibrd *brdp, struct stliport *portp, unsigned long arg, int wait);
  553. static int stli_rawclose(struct stlibrd *brdp, struct stliport *portp, unsigned long arg, int wait);
  554. static int stli_waitcarrier(struct stlibrd *brdp, struct stliport *portp, struct file *filp);
  555. static void stli_dohangup(struct work_struct *);
  556. static int stli_setport(struct stliport *portp);
  557. static int stli_cmdwait(struct stlibrd *brdp, struct stliport *portp, unsigned long cmd, void *arg, int size, int copyback);
  558. static void stli_sendcmd(struct stlibrd *brdp, struct stliport *portp, unsigned long cmd, void *arg, int size, int copyback);
  559. static void __stli_sendcmd(struct stlibrd *brdp, struct stliport *portp, unsigned long cmd, void *arg, int size, int copyback);
  560. static void stli_dodelaycmd(struct stliport *portp, cdkctrl_t __iomem *cp);
  561. static void stli_mkasyport(struct stliport *portp, asyport_t *pp, struct ktermios *tiosp);
  562. static void stli_mkasysigs(asysigs_t *sp, int dtr, int rts);
  563. static long stli_mktiocm(unsigned long sigvalue);
  564. static void stli_read(struct stlibrd *brdp, struct stliport *portp);
  565. static int stli_getserial(struct stliport *portp, struct serial_struct __user *sp);
  566. static int stli_setserial(struct stliport *portp, struct serial_struct __user *sp);
  567. static int stli_getbrdstats(combrd_t __user *bp);
  568. static int stli_getportstats(struct stliport *portp, comstats_t __user *cp);
  569. static int stli_portcmdstats(struct stliport *portp);
  570. static int stli_clrportstats(struct stliport *portp, comstats_t __user *cp);
  571. static int stli_getportstruct(struct stliport __user *arg);
  572. static int stli_getbrdstruct(struct stlibrd __user *arg);
  573. static struct stlibrd *stli_allocbrd(void);
  574. static void stli_ecpinit(struct stlibrd *brdp);
  575. static void stli_ecpenable(struct stlibrd *brdp);
  576. static void stli_ecpdisable(struct stlibrd *brdp);
  577. static void __iomem *stli_ecpgetmemptr(struct stlibrd *brdp, unsigned long offset, int line);
  578. static void stli_ecpreset(struct stlibrd *brdp);
  579. static void stli_ecpintr(struct stlibrd *brdp);
  580. static void stli_ecpeiinit(struct stlibrd *brdp);
  581. static void stli_ecpeienable(struct stlibrd *brdp);
  582. static void stli_ecpeidisable(struct stlibrd *brdp);
  583. static void __iomem *stli_ecpeigetmemptr(struct stlibrd *brdp, unsigned long offset, int line);
  584. static void stli_ecpeireset(struct stlibrd *brdp);
  585. static void stli_ecpmcenable(struct stlibrd *brdp);
  586. static void stli_ecpmcdisable(struct stlibrd *brdp);
  587. static void __iomem *stli_ecpmcgetmemptr(struct stlibrd *brdp, unsigned long offset, int line);
  588. static void stli_ecpmcreset(struct stlibrd *brdp);
  589. static void stli_ecppciinit(struct stlibrd *brdp);
  590. static void __iomem *stli_ecppcigetmemptr(struct stlibrd *brdp, unsigned long offset, int line);
  591. static void stli_ecppcireset(struct stlibrd *brdp);
  592. static void stli_onbinit(struct stlibrd *brdp);
  593. static void stli_onbenable(struct stlibrd *brdp);
  594. static void stli_onbdisable(struct stlibrd *brdp);
  595. static void __iomem *stli_onbgetmemptr(struct stlibrd *brdp, unsigned long offset, int line);
  596. static void stli_onbreset(struct stlibrd *brdp);
  597. static void stli_onbeinit(struct stlibrd *brdp);
  598. static void stli_onbeenable(struct stlibrd *brdp);
  599. static void stli_onbedisable(struct stlibrd *brdp);
  600. static void __iomem *stli_onbegetmemptr(struct stlibrd *brdp, unsigned long offset, int line);
  601. static void stli_onbereset(struct stlibrd *brdp);
  602. static void stli_bbyinit(struct stlibrd *brdp);
  603. static void __iomem *stli_bbygetmemptr(struct stlibrd *brdp, unsigned long offset, int line);
  604. static void stli_bbyreset(struct stlibrd *brdp);
  605. static void stli_stalinit(struct stlibrd *brdp);
  606. static void __iomem *stli_stalgetmemptr(struct stlibrd *brdp, unsigned long offset, int line);
  607. static void stli_stalreset(struct stlibrd *brdp);
  608. static struct stliport *stli_getport(unsigned int brdnr, unsigned int panelnr, unsigned int portnr);
  609. static int stli_initecp(struct stlibrd *brdp);
  610. static int stli_initonb(struct stlibrd *brdp);
  611. #if STLI_EISAPROBE != 0
  612. static int stli_eisamemprobe(struct stlibrd *brdp);
  613. #endif
  614. static int stli_initports(struct stlibrd *brdp);
  615. /*****************************************************************************/
  616. /*
  617. * Define the driver info for a user level shared memory device. This
  618. * device will work sort of like the /dev/kmem device - except that it
  619. * will give access to the shared memory on the Stallion intelligent
  620. * board. This is also a very useful debugging tool.
  621. */
  622. static const struct file_operations stli_fsiomem = {
  623. .owner = THIS_MODULE,
  624. .read = stli_memread,
  625. .write = stli_memwrite,
  626. .ioctl = stli_memioctl,
  627. };
  628. /*****************************************************************************/
  629. /*
  630. * Define a timer_list entry for our poll routine. The slave board
  631. * is polled every so often to see if anything needs doing. This is
  632. * much cheaper on host cpu than using interrupts. It turns out to
  633. * not increase character latency by much either...
  634. */
  635. static DEFINE_TIMER(stli_timerlist, stli_poll, 0, 0);
  636. static int stli_timeron;
  637. /*
  638. * Define the calculation for the timeout routine.
  639. */
  640. #define STLI_TIMEOUT (jiffies + 1)
  641. /*****************************************************************************/
  642. static struct class *istallion_class;
  643. static void stli_cleanup_ports(struct stlibrd *brdp)
  644. {
  645. struct stliport *portp;
  646. unsigned int j;
  647. for (j = 0; j < STL_MAXPORTS; j++) {
  648. portp = brdp->ports[j];
  649. if (portp != NULL) {
  650. if (portp->tty != NULL)
  651. tty_hangup(portp->tty);
  652. kfree(portp);
  653. }
  654. }
  655. }
  656. /*****************************************************************************/
  657. /*
  658. * Parse the supplied argument string, into the board conf struct.
  659. */
  660. static int stli_parsebrd(struct stlconf *confp, char **argp)
  661. {
  662. unsigned int i;
  663. char *sp;
  664. if (argp[0] == NULL || *argp[0] == 0)
  665. return 0;
  666. for (sp = argp[0], i = 0; ((*sp != 0) && (i < 25)); sp++, i++)
  667. *sp = tolower(*sp);
  668. for (i = 0; i < ARRAY_SIZE(stli_brdstr); i++) {
  669. if (strcmp(stli_brdstr[i].name, argp[0]) == 0)
  670. break;
  671. }
  672. if (i == ARRAY_SIZE(stli_brdstr)) {
  673. printk("STALLION: unknown board name, %s?\n", argp[0]);
  674. return 0;
  675. }
  676. confp->brdtype = stli_brdstr[i].type;
  677. if (argp[1] != NULL && *argp[1] != 0)
  678. confp->ioaddr1 = simple_strtoul(argp[1], NULL, 0);
  679. if (argp[2] != NULL && *argp[2] != 0)
  680. confp->memaddr = simple_strtoul(argp[2], NULL, 0);
  681. return(1);
  682. }
  683. /*****************************************************************************/
  684. static int stli_open(struct tty_struct *tty, struct file *filp)
  685. {
  686. struct stlibrd *brdp;
  687. struct stliport *portp;
  688. unsigned int minordev, brdnr, portnr;
  689. int rc;
  690. minordev = tty->index;
  691. brdnr = MINOR2BRD(minordev);
  692. if (brdnr >= stli_nrbrds)
  693. return -ENODEV;
  694. brdp = stli_brds[brdnr];
  695. if (brdp == NULL)
  696. return -ENODEV;
  697. if ((brdp->state & BST_STARTED) == 0)
  698. return -ENODEV;
  699. portnr = MINOR2PORT(minordev);
  700. if (portnr > brdp->nrports)
  701. return -ENODEV;
  702. portp = brdp->ports[portnr];
  703. if (portp == NULL)
  704. return -ENODEV;
  705. if (portp->devnr < 1)
  706. return -ENODEV;
  707. /*
  708. * Check if this port is in the middle of closing. If so then wait
  709. * until it is closed then return error status based on flag settings.
  710. * The sleep here does not need interrupt protection since the wakeup
  711. * for it is done with the same context.
  712. */
  713. if (portp->flags & ASYNC_CLOSING) {
  714. interruptible_sleep_on(&portp->close_wait);
  715. if (portp->flags & ASYNC_HUP_NOTIFY)
  716. return -EAGAIN;
  717. return -ERESTARTSYS;
  718. }
  719. /*
  720. * On the first open of the device setup the port hardware, and
  721. * initialize the per port data structure. Since initializing the port
  722. * requires several commands to the board we will need to wait for any
  723. * other open that is already initializing the port.
  724. */
  725. portp->tty = tty;
  726. tty->driver_data = portp;
  727. portp->refcount++;
  728. wait_event_interruptible(portp->raw_wait,
  729. !test_bit(ST_INITIALIZING, &portp->state));
  730. if (signal_pending(current))
  731. return -ERESTARTSYS;
  732. if ((portp->flags & ASYNC_INITIALIZED) == 0) {
  733. set_bit(ST_INITIALIZING, &portp->state);
  734. if ((rc = stli_initopen(brdp, portp)) >= 0) {
  735. portp->flags |= ASYNC_INITIALIZED;
  736. clear_bit(TTY_IO_ERROR, &tty->flags);
  737. }
  738. clear_bit(ST_INITIALIZING, &portp->state);
  739. wake_up_interruptible(&portp->raw_wait);
  740. if (rc < 0)
  741. return rc;
  742. }
  743. /*
  744. * Check if this port is in the middle of closing. If so then wait
  745. * until it is closed then return error status, based on flag settings.
  746. * The sleep here does not need interrupt protection since the wakeup
  747. * for it is done with the same context.
  748. */
  749. if (portp->flags & ASYNC_CLOSING) {
  750. interruptible_sleep_on(&portp->close_wait);
  751. if (portp->flags & ASYNC_HUP_NOTIFY)
  752. return -EAGAIN;
  753. return -ERESTARTSYS;
  754. }
  755. /*
  756. * Based on type of open being done check if it can overlap with any
  757. * previous opens still in effect. If we are a normal serial device
  758. * then also we might have to wait for carrier.
  759. */
  760. if (!(filp->f_flags & O_NONBLOCK)) {
  761. if ((rc = stli_waitcarrier(brdp, portp, filp)) != 0)
  762. return rc;
  763. }
  764. portp->flags |= ASYNC_NORMAL_ACTIVE;
  765. return 0;
  766. }
  767. /*****************************************************************************/
  768. static void stli_close(struct tty_struct *tty, struct file *filp)
  769. {
  770. struct stlibrd *brdp;
  771. struct stliport *portp;
  772. unsigned long flags;
  773. portp = tty->driver_data;
  774. if (portp == NULL)
  775. return;
  776. spin_lock_irqsave(&stli_lock, flags);
  777. if (tty_hung_up_p(filp)) {
  778. spin_unlock_irqrestore(&stli_lock, flags);
  779. return;
  780. }
  781. if ((tty->count == 1) && (portp->refcount != 1))
  782. portp->refcount = 1;
  783. if (portp->refcount-- > 1) {
  784. spin_unlock_irqrestore(&stli_lock, flags);
  785. return;
  786. }
  787. portp->flags |= ASYNC_CLOSING;
  788. /*
  789. * May want to wait for data to drain before closing. The BUSY flag
  790. * keeps track of whether we are still transmitting or not. It is
  791. * updated by messages from the slave - indicating when all chars
  792. * really have drained.
  793. */
  794. if (tty == stli_txcooktty)
  795. stli_flushchars(tty);
  796. tty->closing = 1;
  797. spin_unlock_irqrestore(&stli_lock, flags);
  798. if (portp->closing_wait != ASYNC_CLOSING_WAIT_NONE)
  799. tty_wait_until_sent(tty, portp->closing_wait);
  800. portp->flags &= ~ASYNC_INITIALIZED;
  801. brdp = stli_brds[portp->brdnr];
  802. stli_rawclose(brdp, portp, 0, 0);
  803. if (tty->termios->c_cflag & HUPCL) {
  804. stli_mkasysigs(&portp->asig, 0, 0);
  805. if (test_bit(ST_CMDING, &portp->state))
  806. set_bit(ST_DOSIGS, &portp->state);
  807. else
  808. stli_sendcmd(brdp, portp, A_SETSIGNALS, &portp->asig,
  809. sizeof(asysigs_t), 0);
  810. }
  811. clear_bit(ST_TXBUSY, &portp->state);
  812. clear_bit(ST_RXSTOP, &portp->state);
  813. set_bit(TTY_IO_ERROR, &tty->flags);
  814. if (tty->ldisc.flush_buffer)
  815. (tty->ldisc.flush_buffer)(tty);
  816. set_bit(ST_DOFLUSHRX, &portp->state);
  817. stli_flushbuffer(tty);
  818. tty->closing = 0;
  819. portp->tty = NULL;
  820. if (portp->openwaitcnt) {
  821. if (portp->close_delay)
  822. msleep_interruptible(jiffies_to_msecs(portp->close_delay));
  823. wake_up_interruptible(&portp->open_wait);
  824. }
  825. portp->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
  826. wake_up_interruptible(&portp->close_wait);
  827. }
  828. /*****************************************************************************/
  829. /*
  830. * Carry out first open operations on a port. This involves a number of
  831. * commands to be sent to the slave. We need to open the port, set the
  832. * notification events, set the initial port settings, get and set the
  833. * initial signal values. We sleep and wait in between each one. But
  834. * this still all happens pretty quickly.
  835. */
  836. static int stli_initopen(struct stlibrd *brdp, struct stliport *portp)
  837. {
  838. struct tty_struct *tty;
  839. asynotify_t nt;
  840. asyport_t aport;
  841. int rc;
  842. if ((rc = stli_rawopen(brdp, portp, 0, 1)) < 0)
  843. return rc;
  844. memset(&nt, 0, sizeof(asynotify_t));
  845. nt.data = (DT_TXLOW | DT_TXEMPTY | DT_RXBUSY | DT_RXBREAK);
  846. nt.signal = SG_DCD;
  847. if ((rc = stli_cmdwait(brdp, portp, A_SETNOTIFY, &nt,
  848. sizeof(asynotify_t), 0)) < 0)
  849. return rc;
  850. tty = portp->tty;
  851. if (tty == NULL)
  852. return -ENODEV;
  853. stli_mkasyport(portp, &aport, tty->termios);
  854. if ((rc = stli_cmdwait(brdp, portp, A_SETPORT, &aport,
  855. sizeof(asyport_t), 0)) < 0)
  856. return rc;
  857. set_bit(ST_GETSIGS, &portp->state);
  858. if ((rc = stli_cmdwait(brdp, portp, A_GETSIGNALS, &portp->asig,
  859. sizeof(asysigs_t), 1)) < 0)
  860. return rc;
  861. if (test_and_clear_bit(ST_GETSIGS, &portp->state))
  862. portp->sigs = stli_mktiocm(portp->asig.sigvalue);
  863. stli_mkasysigs(&portp->asig, 1, 1);
  864. if ((rc = stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig,
  865. sizeof(asysigs_t), 0)) < 0)
  866. return rc;
  867. return 0;
  868. }
  869. /*****************************************************************************/
  870. /*
  871. * Send an open message to the slave. This will sleep waiting for the
  872. * acknowledgement, so must have user context. We need to co-ordinate
  873. * with close events here, since we don't want open and close events
  874. * to overlap.
  875. */
  876. static int stli_rawopen(struct stlibrd *brdp, struct stliport *portp, unsigned long arg, int wait)
  877. {
  878. cdkhdr_t __iomem *hdrp;
  879. cdkctrl_t __iomem *cp;
  880. unsigned char __iomem *bits;
  881. unsigned long flags;
  882. int rc;
  883. /*
  884. * Send a message to the slave to open this port.
  885. */
  886. /*
  887. * Slave is already closing this port. This can happen if a hangup
  888. * occurs on this port. So we must wait until it is complete. The
  889. * order of opens and closes may not be preserved across shared
  890. * memory, so we must wait until it is complete.
  891. */
  892. wait_event_interruptible(portp->raw_wait,
  893. !test_bit(ST_CLOSING, &portp->state));
  894. if (signal_pending(current)) {
  895. return -ERESTARTSYS;
  896. }
  897. /*
  898. * Everything is ready now, so write the open message into shared
  899. * memory. Once the message is in set the service bits to say that
  900. * this port wants service.
  901. */
  902. spin_lock_irqsave(&brd_lock, flags);
  903. EBRDENABLE(brdp);
  904. cp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
  905. writel(arg, &cp->openarg);
  906. writeb(1, &cp->open);
  907. hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
  908. bits = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset +
  909. portp->portidx;
  910. writeb(readb(bits) | portp->portbit, bits);
  911. EBRDDISABLE(brdp);
  912. if (wait == 0) {
  913. spin_unlock_irqrestore(&brd_lock, flags);
  914. return 0;
  915. }
  916. /*
  917. * Slave is in action, so now we must wait for the open acknowledgment
  918. * to come back.
  919. */
  920. rc = 0;
  921. set_bit(ST_OPENING, &portp->state);
  922. spin_unlock_irqrestore(&brd_lock, flags);
  923. wait_event_interruptible(portp->raw_wait,
  924. !test_bit(ST_OPENING, &portp->state));
  925. if (signal_pending(current))
  926. rc = -ERESTARTSYS;
  927. if ((rc == 0) && (portp->rc != 0))
  928. rc = -EIO;
  929. return rc;
  930. }
  931. /*****************************************************************************/
  932. /*
  933. * Send a close message to the slave. Normally this will sleep waiting
  934. * for the acknowledgement, but if wait parameter is 0 it will not. If
  935. * wait is true then must have user context (to sleep).
  936. */
  937. static int stli_rawclose(struct stlibrd *brdp, struct stliport *portp, unsigned long arg, int wait)
  938. {
  939. cdkhdr_t __iomem *hdrp;
  940. cdkctrl_t __iomem *cp;
  941. unsigned char __iomem *bits;
  942. unsigned long flags;
  943. int rc;
  944. /*
  945. * Slave is already closing this port. This can happen if a hangup
  946. * occurs on this port.
  947. */
  948. if (wait) {
  949. wait_event_interruptible(portp->raw_wait,
  950. !test_bit(ST_CLOSING, &portp->state));
  951. if (signal_pending(current)) {
  952. return -ERESTARTSYS;
  953. }
  954. }
  955. /*
  956. * Write the close command into shared memory.
  957. */
  958. spin_lock_irqsave(&brd_lock, flags);
  959. EBRDENABLE(brdp);
  960. cp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
  961. writel(arg, &cp->closearg);
  962. writeb(1, &cp->close);
  963. hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
  964. bits = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset +
  965. portp->portidx;
  966. writeb(readb(bits) |portp->portbit, bits);
  967. EBRDDISABLE(brdp);
  968. set_bit(ST_CLOSING, &portp->state);
  969. spin_unlock_irqrestore(&brd_lock, flags);
  970. if (wait == 0)
  971. return 0;
  972. /*
  973. * Slave is in action, so now we must wait for the open acknowledgment
  974. * to come back.
  975. */
  976. rc = 0;
  977. wait_event_interruptible(portp->raw_wait,
  978. !test_bit(ST_CLOSING, &portp->state));
  979. if (signal_pending(current))
  980. rc = -ERESTARTSYS;
  981. if ((rc == 0) && (portp->rc != 0))
  982. rc = -EIO;
  983. return rc;
  984. }
  985. /*****************************************************************************/
  986. /*
  987. * Send a command to the slave and wait for the response. This must
  988. * have user context (it sleeps). This routine is generic in that it
  989. * can send any type of command. Its purpose is to wait for that command
  990. * to complete (as opposed to initiating the command then returning).
  991. */
  992. static int stli_cmdwait(struct stlibrd *brdp, struct stliport *portp, unsigned long cmd, void *arg, int size, int copyback)
  993. {
  994. wait_event_interruptible(portp->raw_wait,
  995. !test_bit(ST_CMDING, &portp->state));
  996. if (signal_pending(current))
  997. return -ERESTARTSYS;
  998. stli_sendcmd(brdp, portp, cmd, arg, size, copyback);
  999. wait_event_interruptible(portp->raw_wait,
  1000. !test_bit(ST_CMDING, &portp->state));
  1001. if (signal_pending(current))
  1002. return -ERESTARTSYS;
  1003. if (portp->rc != 0)
  1004. return -EIO;
  1005. return 0;
  1006. }
  1007. /*****************************************************************************/
  1008. /*
  1009. * Send the termios settings for this port to the slave. This sleeps
  1010. * waiting for the command to complete - so must have user context.
  1011. */
  1012. static int stli_setport(struct stliport *portp)
  1013. {
  1014. struct stlibrd *brdp;
  1015. asyport_t aport;
  1016. if (portp == NULL)
  1017. return -ENODEV;
  1018. if (portp->tty == NULL)
  1019. return -ENODEV;
  1020. if (portp->brdnr >= stli_nrbrds)
  1021. return -ENODEV;
  1022. brdp = stli_brds[portp->brdnr];
  1023. if (brdp == NULL)
  1024. return -ENODEV;
  1025. stli_mkasyport(portp, &aport, portp->tty->termios);
  1026. return(stli_cmdwait(brdp, portp, A_SETPORT, &aport, sizeof(asyport_t), 0));
  1027. }
  1028. /*****************************************************************************/
  1029. /*
  1030. * Possibly need to wait for carrier (DCD signal) to come high. Say
  1031. * maybe because if we are clocal then we don't need to wait...
  1032. */
  1033. static int stli_waitcarrier(struct stlibrd *brdp, struct stliport *portp, struct file *filp)
  1034. {
  1035. unsigned long flags;
  1036. int rc, doclocal;
  1037. rc = 0;
  1038. doclocal = 0;
  1039. if (portp->tty->termios->c_cflag & CLOCAL)
  1040. doclocal++;
  1041. spin_lock_irqsave(&stli_lock, flags);
  1042. portp->openwaitcnt++;
  1043. if (! tty_hung_up_p(filp))
  1044. portp->refcount--;
  1045. spin_unlock_irqrestore(&stli_lock, flags);
  1046. for (;;) {
  1047. stli_mkasysigs(&portp->asig, 1, 1);
  1048. if ((rc = stli_cmdwait(brdp, portp, A_SETSIGNALS,
  1049. &portp->asig, sizeof(asysigs_t), 0)) < 0)
  1050. break;
  1051. if (tty_hung_up_p(filp) ||
  1052. ((portp->flags & ASYNC_INITIALIZED) == 0)) {
  1053. if (portp->flags & ASYNC_HUP_NOTIFY)
  1054. rc = -EBUSY;
  1055. else
  1056. rc = -ERESTARTSYS;
  1057. break;
  1058. }
  1059. if (((portp->flags & ASYNC_CLOSING) == 0) &&
  1060. (doclocal || (portp->sigs & TIOCM_CD))) {
  1061. break;
  1062. }
  1063. if (signal_pending(current)) {
  1064. rc = -ERESTARTSYS;
  1065. break;
  1066. }
  1067. interruptible_sleep_on(&portp->open_wait);
  1068. }
  1069. spin_lock_irqsave(&stli_lock, flags);
  1070. if (! tty_hung_up_p(filp))
  1071. portp->refcount++;
  1072. portp->openwaitcnt--;
  1073. spin_unlock_irqrestore(&stli_lock, flags);
  1074. return rc;
  1075. }
  1076. /*****************************************************************************/
  1077. /*
  1078. * Write routine. Take the data and put it in the shared memory ring
  1079. * queue. If port is not already sending chars then need to mark the
  1080. * service bits for this port.
  1081. */
  1082. static int stli_write(struct tty_struct *tty, const unsigned char *buf, int count)
  1083. {
  1084. cdkasy_t __iomem *ap;
  1085. cdkhdr_t __iomem *hdrp;
  1086. unsigned char __iomem *bits;
  1087. unsigned char __iomem *shbuf;
  1088. unsigned char *chbuf;
  1089. struct stliport *portp;
  1090. struct stlibrd *brdp;
  1091. unsigned int len, stlen, head, tail, size;
  1092. unsigned long flags;
  1093. if (tty == stli_txcooktty)
  1094. stli_flushchars(tty);
  1095. portp = tty->driver_data;
  1096. if (portp == NULL)
  1097. return 0;
  1098. if (portp->brdnr >= stli_nrbrds)
  1099. return 0;
  1100. brdp = stli_brds[portp->brdnr];
  1101. if (brdp == NULL)
  1102. return 0;
  1103. chbuf = (unsigned char *) buf;
  1104. /*
  1105. * All data is now local, shove as much as possible into shared memory.
  1106. */
  1107. spin_lock_irqsave(&brd_lock, flags);
  1108. EBRDENABLE(brdp);
  1109. ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr);
  1110. head = (unsigned int) readw(&ap->txq.head);
  1111. tail = (unsigned int) readw(&ap->txq.tail);
  1112. if (tail != ((unsigned int) readw(&ap->txq.tail)))
  1113. tail = (unsigned int) readw(&ap->txq.tail);
  1114. size = portp->txsize;
  1115. if (head >= tail) {
  1116. len = size - (head - tail) - 1;
  1117. stlen = size - head;
  1118. } else {
  1119. len = tail - head - 1;
  1120. stlen = len;
  1121. }
  1122. len = min(len, (unsigned int)count);
  1123. count = 0;
  1124. shbuf = (char __iomem *) EBRDGETMEMPTR(brdp, portp->txoffset);
  1125. while (len > 0) {
  1126. stlen = min(len, stlen);
  1127. memcpy_toio(shbuf + head, chbuf, stlen);
  1128. chbuf += stlen;
  1129. len -= stlen;
  1130. count += stlen;
  1131. head += stlen;
  1132. if (head >= size) {
  1133. head = 0;
  1134. stlen = tail;
  1135. }
  1136. }
  1137. ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr);
  1138. writew(head, &ap->txq.head);
  1139. if (test_bit(ST_TXBUSY, &portp->state)) {
  1140. if (readl(&ap->changed.data) & DT_TXEMPTY)
  1141. writel(readl(&ap->changed.data) & ~DT_TXEMPTY, &ap->changed.data);
  1142. }
  1143. hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
  1144. bits = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset +
  1145. portp->portidx;
  1146. writeb(readb(bits) | portp->portbit, bits);
  1147. set_bit(ST_TXBUSY, &portp->state);
  1148. EBRDDISABLE(brdp);
  1149. spin_unlock_irqrestore(&brd_lock, flags);
  1150. return(count);
  1151. }
  1152. /*****************************************************************************/
  1153. /*
  1154. * Output a single character. We put it into a temporary local buffer
  1155. * (for speed) then write out that buffer when the flushchars routine
  1156. * is called. There is a safety catch here so that if some other port
  1157. * writes chars before the current buffer has been, then we write them
  1158. * first them do the new ports.
  1159. */
  1160. static void stli_putchar(struct tty_struct *tty, unsigned char ch)
  1161. {
  1162. if (tty != stli_txcooktty) {
  1163. if (stli_txcooktty != NULL)
  1164. stli_flushchars(stli_txcooktty);
  1165. stli_txcooktty = tty;
  1166. }
  1167. stli_txcookbuf[stli_txcooksize++] = ch;
  1168. }
  1169. /*****************************************************************************/
  1170. /*
  1171. * Transfer characters from the local TX cooking buffer to the board.
  1172. * We sort of ignore the tty that gets passed in here. We rely on the
  1173. * info stored with the TX cook buffer to tell us which port to flush
  1174. * the data on. In any case we clean out the TX cook buffer, for re-use
  1175. * by someone else.
  1176. */
  1177. static void stli_flushchars(struct tty_struct *tty)
  1178. {
  1179. cdkhdr_t __iomem *hdrp;
  1180. unsigned char __iomem *bits;
  1181. cdkasy_t __iomem *ap;
  1182. struct tty_struct *cooktty;
  1183. struct stliport *portp;
  1184. struct stlibrd *brdp;
  1185. unsigned int len, stlen, head, tail, size, count, cooksize;
  1186. unsigned char *buf;
  1187. unsigned char __iomem *shbuf;
  1188. unsigned long flags;
  1189. cooksize = stli_txcooksize;
  1190. cooktty = stli_txcooktty;
  1191. stli_txcooksize = 0;
  1192. stli_txcookrealsize = 0;
  1193. stli_txcooktty = NULL;
  1194. if (tty == NULL)
  1195. return;
  1196. if (cooktty == NULL)
  1197. return;
  1198. if (tty != cooktty)
  1199. tty = cooktty;
  1200. if (cooksize == 0)
  1201. return;
  1202. portp = tty->driver_data;
  1203. if (portp == NULL)
  1204. return;
  1205. if (portp->brdnr >= stli_nrbrds)
  1206. return;
  1207. brdp = stli_brds[portp->brdnr];
  1208. if (brdp == NULL)
  1209. return;
  1210. spin_lock_irqsave(&brd_lock, flags);
  1211. EBRDENABLE(brdp);
  1212. ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr);
  1213. head = (unsigned int) readw(&ap->txq.head);
  1214. tail = (unsigned int) readw(&ap->txq.tail);
  1215. if (tail != ((unsigned int) readw(&ap->txq.tail)))
  1216. tail = (unsigned int) readw(&ap->txq.tail);
  1217. size = portp->txsize;
  1218. if (head >= tail) {
  1219. len = size - (head - tail) - 1;
  1220. stlen = size - head;
  1221. } else {
  1222. len = tail - head - 1;
  1223. stlen = len;
  1224. }
  1225. len = min(len, cooksize);
  1226. count = 0;
  1227. shbuf = EBRDGETMEMPTR(brdp, portp->txoffset);
  1228. buf = stli_txcookbuf;
  1229. while (len > 0) {
  1230. stlen = min(len, stlen);
  1231. memcpy_toio(shbuf + head, buf, stlen);
  1232. buf += stlen;
  1233. len -= stlen;
  1234. count += stlen;
  1235. head += stlen;
  1236. if (head >= size) {
  1237. head = 0;
  1238. stlen = tail;
  1239. }
  1240. }
  1241. ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr);
  1242. writew(head, &ap->txq.head);
  1243. if (test_bit(ST_TXBUSY, &portp->state)) {
  1244. if (readl(&ap->changed.data) & DT_TXEMPTY)
  1245. writel(readl(&ap->changed.data) & ~DT_TXEMPTY, &ap->changed.data);
  1246. }
  1247. hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
  1248. bits = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset +
  1249. portp->portidx;
  1250. writeb(readb(bits) | portp->portbit, bits);
  1251. set_bit(ST_TXBUSY, &portp->state);
  1252. EBRDDISABLE(brdp);
  1253. spin_unlock_irqrestore(&brd_lock, flags);
  1254. }
  1255. /*****************************************************************************/
  1256. static int stli_writeroom(struct tty_struct *tty)
  1257. {
  1258. cdkasyrq_t __iomem *rp;
  1259. struct stliport *portp;
  1260. struct stlibrd *brdp;
  1261. unsigned int head, tail, len;
  1262. unsigned long flags;
  1263. if (tty == stli_txcooktty) {
  1264. if (stli_txcookrealsize != 0) {
  1265. len = stli_txcookrealsize - stli_txcooksize;
  1266. return len;
  1267. }
  1268. }
  1269. portp = tty->driver_data;
  1270. if (portp == NULL)
  1271. return 0;
  1272. if (portp->brdnr >= stli_nrbrds)
  1273. return 0;
  1274. brdp = stli_brds[portp->brdnr];
  1275. if (brdp == NULL)
  1276. return 0;
  1277. spin_lock_irqsave(&brd_lock, flags);
  1278. EBRDENABLE(brdp);
  1279. rp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->txq;
  1280. head = (unsigned int) readw(&rp->head);
  1281. tail = (unsigned int) readw(&rp->tail);
  1282. if (tail != ((unsigned int) readw(&rp->tail)))
  1283. tail = (unsigned int) readw(&rp->tail);
  1284. len = (head >= tail) ? (portp->txsize - (head - tail)) : (tail - head);
  1285. len--;
  1286. EBRDDISABLE(brdp);
  1287. spin_unlock_irqrestore(&brd_lock, flags);
  1288. if (tty == stli_txcooktty) {
  1289. stli_txcookrealsize = len;
  1290. len -= stli_txcooksize;
  1291. }
  1292. return len;
  1293. }
  1294. /*****************************************************************************/
  1295. /*
  1296. * Return the number of characters in the transmit buffer. Normally we
  1297. * will return the number of chars in the shared memory ring queue.
  1298. * We need to kludge around the case where the shared memory buffer is
  1299. * empty but not all characters have drained yet, for this case just
  1300. * return that there is 1 character in the buffer!
  1301. */
  1302. static int stli_charsinbuffer(struct tty_struct *tty)
  1303. {
  1304. cdkasyrq_t __iomem *rp;
  1305. struct stliport *portp;
  1306. struct stlibrd *brdp;
  1307. unsigned int head, tail, len;
  1308. unsigned long flags;
  1309. if (tty == stli_txcooktty)
  1310. stli_flushchars(tty);
  1311. portp = tty->driver_data;
  1312. if (portp == NULL)
  1313. return 0;
  1314. if (portp->brdnr >= stli_nrbrds)
  1315. return 0;
  1316. brdp = stli_brds[portp->brdnr];
  1317. if (brdp == NULL)
  1318. return 0;
  1319. spin_lock_irqsave(&brd_lock, flags);
  1320. EBRDENABLE(brdp);
  1321. rp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->txq;
  1322. head = (unsigned int) readw(&rp->head);
  1323. tail = (unsigned int) readw(&rp->tail);
  1324. if (tail != ((unsigned int) readw(&rp->tail)))
  1325. tail = (unsigned int) readw(&rp->tail);
  1326. len = (head >= tail) ? (head - tail) : (portp->txsize - (tail - head));
  1327. if ((len == 0) && test_bit(ST_TXBUSY, &portp->state))
  1328. len = 1;
  1329. EBRDDISABLE(brdp);
  1330. spin_unlock_irqrestore(&brd_lock, flags);
  1331. return len;
  1332. }
  1333. /*****************************************************************************/
  1334. /*
  1335. * Generate the serial struct info.
  1336. */
  1337. static int stli_getserial(struct stliport *portp, struct serial_struct __user *sp)
  1338. {
  1339. struct serial_struct sio;
  1340. struct stlibrd *brdp;
  1341. memset(&sio, 0, sizeof(struct serial_struct));
  1342. sio.type = PORT_UNKNOWN;
  1343. sio.line = portp->portnr;
  1344. sio.irq = 0;
  1345. sio.flags = portp->flags;
  1346. sio.baud_base = portp->baud_base;
  1347. sio.close_delay = portp->close_delay;
  1348. sio.closing_wait = portp->closing_wait;
  1349. sio.custom_divisor = portp->custom_divisor;
  1350. sio.xmit_fifo_size = 0;
  1351. sio.hub6 = 0;
  1352. brdp = stli_brds[portp->brdnr];
  1353. if (brdp != NULL)
  1354. sio.port = brdp->iobase;
  1355. return copy_to_user(sp, &sio, sizeof(struct serial_struct)) ?
  1356. -EFAULT : 0;
  1357. }
  1358. /*****************************************************************************/
  1359. /*
  1360. * Set port according to the serial struct info.
  1361. * At this point we do not do any auto-configure stuff, so we will
  1362. * just quietly ignore any requests to change irq, etc.
  1363. */
  1364. static int stli_setserial(struct stliport *portp, struct serial_struct __user *sp)
  1365. {
  1366. struct serial_struct sio;
  1367. int rc;
  1368. if (copy_from_user(&sio, sp, sizeof(struct serial_struct)))
  1369. return -EFAULT;
  1370. if (!capable(CAP_SYS_ADMIN)) {
  1371. if ((sio.baud_base != portp->baud_base) ||
  1372. (sio.close_delay != portp->close_delay) ||
  1373. ((sio.flags & ~ASYNC_USR_MASK) !=
  1374. (portp->flags & ~ASYNC_USR_MASK)))
  1375. return -EPERM;
  1376. }
  1377. portp->flags = (portp->flags & ~ASYNC_USR_MASK) |
  1378. (sio.flags & ASYNC_USR_MASK);
  1379. portp->baud_base = sio.baud_base;
  1380. portp->close_delay = sio.close_delay;
  1381. portp->closing_wait = sio.closing_wait;
  1382. portp->custom_divisor = sio.custom_divisor;
  1383. if ((rc = stli_setport(portp)) < 0)
  1384. return rc;
  1385. return 0;
  1386. }
  1387. /*****************************************************************************/
  1388. static int stli_tiocmget(struct tty_struct *tty, struct file *file)
  1389. {
  1390. struct stliport *portp = tty->driver_data;
  1391. struct stlibrd *brdp;
  1392. int rc;
  1393. if (portp == NULL)
  1394. return -ENODEV;
  1395. if (portp->brdnr >= stli_nrbrds)
  1396. return 0;
  1397. brdp = stli_brds[portp->brdnr];
  1398. if (brdp == NULL)
  1399. return 0;
  1400. if (tty->flags & (1 << TTY_IO_ERROR))
  1401. return -EIO;
  1402. if ((rc = stli_cmdwait(brdp, portp, A_GETSIGNALS,
  1403. &portp->asig, sizeof(asysigs_t), 1)) < 0)
  1404. return rc;
  1405. return stli_mktiocm(portp->asig.sigvalue);
  1406. }
  1407. static int stli_tiocmset(struct tty_struct *tty, struct file *file,
  1408. unsigned int set, unsigned int clear)
  1409. {
  1410. struct stliport *portp = tty->driver_data;
  1411. struct stlibrd *brdp;
  1412. int rts = -1, dtr = -1;
  1413. if (portp == NULL)
  1414. return -ENODEV;
  1415. if (portp->brdnr >= stli_nrbrds)
  1416. return 0;
  1417. brdp = stli_brds[portp->brdnr];
  1418. if (brdp == NULL)
  1419. return 0;
  1420. if (tty->flags & (1 << TTY_IO_ERROR))
  1421. return -EIO;
  1422. if (set & TIOCM_RTS)
  1423. rts = 1;
  1424. if (set & TIOCM_DTR)
  1425. dtr = 1;
  1426. if (clear & TIOCM_RTS)
  1427. rts = 0;
  1428. if (clear & TIOCM_DTR)
  1429. dtr = 0;
  1430. stli_mkasysigs(&portp->asig, dtr, rts);
  1431. return stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig,
  1432. sizeof(asysigs_t), 0);
  1433. }
  1434. static int stli_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
  1435. {
  1436. struct stliport *portp;
  1437. struct stlibrd *brdp;
  1438. unsigned int ival;
  1439. int rc;
  1440. void __user *argp = (void __user *)arg;
  1441. portp = tty->driver_data;
  1442. if (portp == NULL)
  1443. return -ENODEV;
  1444. if (portp->brdnr >= stli_nrbrds)
  1445. return 0;
  1446. brdp = stli_brds[portp->brdnr];
  1447. if (brdp == NULL)
  1448. return 0;
  1449. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  1450. (cmd != COM_GETPORTSTATS) && (cmd != COM_CLRPORTSTATS)) {
  1451. if (tty->flags & (1 << TTY_IO_ERROR))
  1452. return -EIO;
  1453. }
  1454. rc = 0;
  1455. switch (cmd) {
  1456. case TIOCGSOFTCAR:
  1457. rc = put_user(((tty->termios->c_cflag & CLOCAL) ? 1 : 0),
  1458. (unsigned __user *) arg);
  1459. break;
  1460. case TIOCSSOFTCAR:
  1461. if ((rc = get_user(ival, (unsigned __user *) arg)) == 0)
  1462. tty->termios->c_cflag =
  1463. (tty->termios->c_cflag & ~CLOCAL) |
  1464. (ival ? CLOCAL : 0);
  1465. break;
  1466. case TIOCGSERIAL:
  1467. rc = stli_getserial(portp, argp);
  1468. break;
  1469. case TIOCSSERIAL:
  1470. rc = stli_setserial(portp, argp);
  1471. break;
  1472. case STL_GETPFLAG:
  1473. rc = put_user(portp->pflag, (unsigned __user *)argp);
  1474. break;
  1475. case STL_SETPFLAG:
  1476. if ((rc = get_user(portp->pflag, (unsigned __user *)argp)) == 0)
  1477. stli_setport(portp);
  1478. break;
  1479. case COM_GETPORTSTATS:
  1480. rc = stli_getportstats(portp, argp);
  1481. break;
  1482. case COM_CLRPORTSTATS:
  1483. rc = stli_clrportstats(portp, argp);
  1484. break;
  1485. case TIOCSERCONFIG:
  1486. case TIOCSERGWILD:
  1487. case TIOCSERSWILD:
  1488. case TIOCSERGETLSR:
  1489. case TIOCSERGSTRUCT:
  1490. case TIOCSERGETMULTI:
  1491. case TIOCSERSETMULTI:
  1492. default:
  1493. rc = -ENOIOCTLCMD;
  1494. break;
  1495. }
  1496. return rc;
  1497. }
  1498. /*****************************************************************************/
  1499. /*
  1500. * This routine assumes that we have user context and can sleep.
  1501. * Looks like it is true for the current ttys implementation..!!
  1502. */
  1503. static void stli_settermios(struct tty_struct *tty, struct ktermios *old)
  1504. {
  1505. struct stliport *portp;
  1506. struct stlibrd *brdp;
  1507. struct ktermios *tiosp;
  1508. asyport_t aport;
  1509. if (tty == NULL)
  1510. return;
  1511. portp = tty->driver_data;
  1512. if (portp == NULL)
  1513. return;
  1514. if (portp->brdnr >= stli_nrbrds)
  1515. return;
  1516. brdp = stli_brds[portp->brdnr];
  1517. if (brdp == NULL)
  1518. return;
  1519. tiosp = tty->termios;
  1520. stli_mkasyport(portp, &aport, tiosp);
  1521. stli_cmdwait(brdp, portp, A_SETPORT, &aport, sizeof(asyport_t), 0);
  1522. stli_mkasysigs(&portp->asig, ((tiosp->c_cflag & CBAUD) ? 1 : 0), -1);
  1523. stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig,
  1524. sizeof(asysigs_t), 0);
  1525. if ((old->c_cflag & CRTSCTS) && ((tiosp->c_cflag & CRTSCTS) == 0))
  1526. tty->hw_stopped = 0;
  1527. if (((old->c_cflag & CLOCAL) == 0) && (tiosp->c_cflag & CLOCAL))
  1528. wake_up_interruptible(&portp->open_wait);
  1529. }
  1530. /*****************************************************************************/
  1531. /*
  1532. * Attempt to flow control who ever is sending us data. We won't really
  1533. * do any flow control action here. We can't directly, and even if we
  1534. * wanted to we would have to send a command to the slave. The slave
  1535. * knows how to flow control, and will do so when its buffers reach its
  1536. * internal high water marks. So what we will do is set a local state
  1537. * bit that will stop us sending any RX data up from the poll routine
  1538. * (which is the place where RX data from the slave is handled).
  1539. */
  1540. static void stli_throttle(struct tty_struct *tty)
  1541. {
  1542. struct stliport *portp = tty->driver_data;
  1543. if (portp == NULL)
  1544. return;
  1545. set_bit(ST_RXSTOP, &portp->state);
  1546. }
  1547. /*****************************************************************************/
  1548. /*
  1549. * Unflow control the device sending us data... That means that all
  1550. * we have to do is clear the RXSTOP state bit. The next poll call
  1551. * will then be able to pass the RX data back up.
  1552. */
  1553. static void stli_unthrottle(struct tty_struct *tty)
  1554. {
  1555. struct stliport *portp = tty->driver_data;
  1556. if (portp == NULL)
  1557. return;
  1558. clear_bit(ST_RXSTOP, &portp->state);
  1559. }
  1560. /*****************************************************************************/
  1561. /*
  1562. * Stop the transmitter.
  1563. */
  1564. static void stli_stop(struct tty_struct *tty)
  1565. {
  1566. }
  1567. /*****************************************************************************/
  1568. /*
  1569. * Start the transmitter again.
  1570. */
  1571. static void stli_start(struct tty_struct *tty)
  1572. {
  1573. }
  1574. /*****************************************************************************/
  1575. /*
  1576. * Scheduler called hang up routine. This is called from the scheduler,
  1577. * not direct from the driver "poll" routine. We can't call it there
  1578. * since the real local hangup code will enable/disable the board and
  1579. * other things that we can't do while handling the poll. Much easier
  1580. * to deal with it some time later (don't really care when, hangups
  1581. * aren't that time critical).
  1582. */
  1583. static void stli_dohangup(struct work_struct *ugly_api)
  1584. {
  1585. struct stliport *portp = container_of(ugly_api, struct stliport, tqhangup);
  1586. if (portp->tty != NULL) {
  1587. tty_hangup(portp->tty);
  1588. }
  1589. }
  1590. /*****************************************************************************/
  1591. /*
  1592. * Hangup this port. This is pretty much like closing the port, only
  1593. * a little more brutal. No waiting for data to drain. Shutdown the
  1594. * port and maybe drop signals. This is rather tricky really. We want
  1595. * to close the port as well.
  1596. */
  1597. static void stli_hangup(struct tty_struct *tty)
  1598. {
  1599. struct stliport *portp;
  1600. struct stlibrd *brdp;
  1601. unsigned long flags;
  1602. portp = tty->driver_data;
  1603. if (portp == NULL)
  1604. return;
  1605. if (portp->brdnr >= stli_nrbrds)
  1606. return;
  1607. brdp = stli_brds[portp->brdnr];
  1608. if (brdp == NULL)
  1609. return;
  1610. portp->flags &= ~ASYNC_INITIALIZED;
  1611. if (!test_bit(ST_CLOSING, &portp->state))
  1612. stli_rawclose(brdp, portp, 0, 0);
  1613. spin_lock_irqsave(&stli_lock, flags);
  1614. if (tty->termios->c_cflag & HUPCL) {
  1615. stli_mkasysigs(&portp->asig, 0, 0);
  1616. if (test_bit(ST_CMDING, &portp->state)) {
  1617. set_bit(ST_DOSIGS, &portp->state);
  1618. set_bit(ST_DOFLUSHTX, &portp->state);
  1619. set_bit(ST_DOFLUSHRX, &portp->state);
  1620. } else {
  1621. stli_sendcmd(brdp, portp, A_SETSIGNALSF,
  1622. &portp->asig, sizeof(asysigs_t), 0);
  1623. }
  1624. }
  1625. clear_bit(ST_TXBUSY, &portp->state);
  1626. clear_bit(ST_RXSTOP, &portp->state);
  1627. set_bit(TTY_IO_ERROR, &tty->flags);
  1628. portp->tty = NULL;
  1629. portp->flags &= ~ASYNC_NORMAL_ACTIVE;
  1630. portp->refcount = 0;
  1631. spin_unlock_irqrestore(&stli_lock, flags);
  1632. wake_up_interruptible(&portp->open_wait);
  1633. }
  1634. /*****************************************************************************/
  1635. /*
  1636. * Flush characters from the lower buffer. We may not have user context
  1637. * so we cannot sleep waiting for it to complete. Also we need to check
  1638. * if there is chars for this port in the TX cook buffer, and flush them
  1639. * as well.
  1640. */
  1641. static void stli_flushbuffer(struct tty_struct *tty)
  1642. {
  1643. struct stliport *portp;
  1644. struct stlibrd *brdp;
  1645. unsigned long ftype, flags;
  1646. portp = tty->driver_data;
  1647. if (portp == NULL)
  1648. return;
  1649. if (portp->brdnr >= stli_nrbrds)
  1650. return;
  1651. brdp = stli_brds[portp->brdnr];
  1652. if (brdp == NULL)
  1653. return;
  1654. spin_lock_irqsave(&brd_lock, flags);
  1655. if (tty == stli_txcooktty) {
  1656. stli_txcooktty = NULL;
  1657. stli_txcooksize = 0;
  1658. stli_txcookrealsize = 0;
  1659. }
  1660. if (test_bit(ST_CMDING, &portp->state)) {
  1661. set_bit(ST_DOFLUSHTX, &portp->state);
  1662. } else {
  1663. ftype = FLUSHTX;
  1664. if (test_bit(ST_DOFLUSHRX, &portp->state)) {
  1665. ftype |= FLUSHRX;
  1666. clear_bit(ST_DOFLUSHRX, &portp->state);
  1667. }
  1668. __stli_sendcmd(brdp, portp, A_FLUSH, &ftype, sizeof(u32), 0);
  1669. }
  1670. spin_unlock_irqrestore(&brd_lock, flags);
  1671. tty_wakeup(tty);
  1672. }
  1673. /*****************************************************************************/
  1674. static void stli_breakctl(struct tty_struct *tty, int state)
  1675. {
  1676. struct stlibrd *brdp;
  1677. struct stliport *portp;
  1678. long arg;
  1679. portp = tty->driver_data;
  1680. if (portp == NULL)
  1681. return;
  1682. if (portp->brdnr >= stli_nrbrds)
  1683. return;
  1684. brdp = stli_brds[portp->brdnr];
  1685. if (brdp == NULL)
  1686. return;
  1687. arg = (state == -1) ? BREAKON : BREAKOFF;
  1688. stli_cmdwait(brdp, portp, A_BREAK, &arg, sizeof(long), 0);
  1689. }
  1690. /*****************************************************************************/
  1691. static void stli_waituntilsent(struct tty_struct *tty, int timeout)
  1692. {
  1693. struct stliport *portp;
  1694. unsigned long tend;
  1695. if (tty == NULL)
  1696. return;
  1697. portp = tty->driver_data;
  1698. if (portp == NULL)
  1699. return;
  1700. if (timeout == 0)
  1701. timeout = HZ;
  1702. tend = jiffies + timeout;
  1703. while (test_bit(ST_TXBUSY, &portp->state)) {
  1704. if (signal_pending(current))
  1705. break;
  1706. msleep_interruptible(20);
  1707. if (time_after_eq(jiffies, tend))
  1708. break;
  1709. }
  1710. }
  1711. /*****************************************************************************/
  1712. static void stli_sendxchar(struct tty_struct *tty, char ch)
  1713. {
  1714. struct stlibrd *brdp;
  1715. struct stliport *portp;
  1716. asyctrl_t actrl;
  1717. portp = tty->driver_data;
  1718. if (portp == NULL)
  1719. return;
  1720. if (portp->brdnr >= stli_nrbrds)
  1721. return;
  1722. brdp = stli_brds[portp->brdnr];
  1723. if (brdp == NULL)
  1724. return;
  1725. memset(&actrl, 0, sizeof(asyctrl_t));
  1726. if (ch == STOP_CHAR(tty)) {
  1727. actrl.rxctrl = CT_STOPFLOW;
  1728. } else if (ch == START_CHAR(tty)) {
  1729. actrl.rxctrl = CT_STARTFLOW;
  1730. } else {
  1731. actrl.txctrl = CT_SENDCHR;
  1732. actrl.tximdch = ch;
  1733. }
  1734. stli_cmdwait(brdp, portp, A_PORTCTRL, &actrl, sizeof(asyctrl_t), 0);
  1735. }
  1736. /*****************************************************************************/
  1737. #define MAXLINE 80
  1738. /*
  1739. * Format info for a specified port. The line is deliberately limited
  1740. * to 80 characters. (If it is too long it will be truncated, if too
  1741. * short then padded with spaces).
  1742. */
  1743. static int stli_portinfo(struct stlibrd *brdp, struct stliport *portp, int portnr, char *pos)
  1744. {
  1745. char *sp, *uart;
  1746. int rc, cnt;
  1747. rc = stli_portcmdstats(portp);
  1748. uart = "UNKNOWN";
  1749. if (brdp->state & BST_STARTED) {
  1750. switch (stli_comstats.hwid) {
  1751. case 0: uart = "2681"; break;
  1752. case 1: uart = "SC26198"; break;
  1753. default:uart = "CD1400"; break;
  1754. }
  1755. }
  1756. sp = pos;
  1757. sp += sprintf(sp, "%d: uart:%s ", portnr, uart);
  1758. if ((brdp->state & BST_STARTED) && (rc >= 0)) {
  1759. sp += sprintf(sp, "tx:%d rx:%d", (int) stli_comstats.txtotal,
  1760. (int) stli_comstats.rxtotal);
  1761. if (stli_comstats.rxframing)
  1762. sp += sprintf(sp, " fe:%d",
  1763. (int) stli_comstats.rxframing);
  1764. if (stli_comstats.rxparity)
  1765. sp += sprintf(sp, " pe:%d",
  1766. (int) stli_comstats.rxparity);
  1767. if (stli_comstats.rxbreaks)
  1768. sp += sprintf(sp, " brk:%d",
  1769. (int) stli_comstats.rxbreaks);
  1770. if (stli_comstats.rxoverrun)
  1771. sp += sprintf(sp, " oe:%d",
  1772. (int) stli_comstats.rxoverrun);
  1773. cnt = sprintf(sp, "%s%s%s%s%s ",
  1774. (stli_comstats.signals & TIOCM_RTS) ? "|RTS" : "",
  1775. (stli_comstats.signals & TIOCM_CTS) ? "|CTS" : "",
  1776. (stli_comstats.signals & TIOCM_DTR) ? "|DTR" : "",
  1777. (stli_comstats.signals & TIOCM_CD) ? "|DCD" : "",
  1778. (stli_comstats.signals & TIOCM_DSR) ? "|DSR" : "");
  1779. *sp = ' ';
  1780. sp += cnt;
  1781. }
  1782. for (cnt = (sp - pos); (cnt < (MAXLINE - 1)); cnt++)
  1783. *sp++ = ' ';
  1784. if (cnt >= MAXLINE)
  1785. pos[(MAXLINE - 2)] = '+';
  1786. pos[(MAXLINE - 1)] = '\n';
  1787. return(MAXLINE);
  1788. }
  1789. /*****************************************************************************/
  1790. /*
  1791. * Port info, read from the /proc file system.
  1792. */
  1793. static int stli_readproc(char *page, char **start, off_t off, int count, int *eof, void *data)
  1794. {
  1795. struct stlibrd *brdp;
  1796. struct stliport *portp;
  1797. unsigned int brdnr, portnr, totalport;
  1798. int curoff, maxoff;
  1799. char *pos;
  1800. pos = page;
  1801. totalport = 0;
  1802. curoff = 0;
  1803. if (off == 0) {
  1804. pos += sprintf(pos, "%s: version %s", stli_drvtitle,
  1805. stli_drvversion);
  1806. while (pos < (page + MAXLINE - 1))
  1807. *pos++ = ' ';
  1808. *pos++ = '\n';
  1809. }
  1810. curoff = MAXLINE;
  1811. /*
  1812. * We scan through for each board, panel and port. The offset is
  1813. * calculated on the fly, and irrelevant ports are skipped.
  1814. */
  1815. for (brdnr = 0; (brdnr < stli_nrbrds); brdnr++) {
  1816. brdp = stli_brds[brdnr];
  1817. if (brdp == NULL)
  1818. continue;
  1819. if (brdp->state == 0)
  1820. continue;
  1821. maxoff = curoff + (brdp->nrports * MAXLINE);
  1822. if (off >= maxoff) {
  1823. curoff = maxoff;
  1824. continue;
  1825. }
  1826. totalport = brdnr * STL_MAXPORTS;
  1827. for (portnr = 0; (portnr < brdp->nrports); portnr++,
  1828. totalport++) {
  1829. portp = brdp->ports[portnr];
  1830. if (portp == NULL)
  1831. continue;
  1832. if (off >= (curoff += MAXLINE))
  1833. continue;
  1834. if ((pos - page + MAXLINE) > count)
  1835. goto stli_readdone;
  1836. pos += stli_portinfo(brdp, portp, totalport, pos);
  1837. }
  1838. }
  1839. *eof = 1;
  1840. stli_readdone:
  1841. *start = page;
  1842. return(pos - page);
  1843. }
  1844. /*****************************************************************************/
  1845. /*
  1846. * Generic send command routine. This will send a message to the slave,
  1847. * of the specified type with the specified argument. Must be very
  1848. * careful of data that will be copied out from shared memory -
  1849. * containing command results. The command completion is all done from
  1850. * a poll routine that does not have user context. Therefore you cannot
  1851. * copy back directly into user space, or to the kernel stack of a
  1852. * process. This routine does not sleep, so can be called from anywhere.
  1853. *
  1854. * The caller must hold the brd_lock (see also stli_sendcmd the usual
  1855. * entry point)
  1856. */
  1857. static void __stli_sendcmd(struct stlibrd *brdp, struct stliport *portp, unsigned long cmd, void *arg, int size, int copyback)
  1858. {
  1859. cdkhdr_t __iomem *hdrp;
  1860. cdkctrl_t __iomem *cp;
  1861. unsigned char __iomem *bits;
  1862. if (test_bit(ST_CMDING, &portp->state)) {
  1863. printk(KERN_ERR "STALLION: command already busy, cmd=%x!\n",
  1864. (int) cmd);
  1865. return;
  1866. }
  1867. EBRDENABLE(brdp);
  1868. cp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
  1869. if (size > 0) {
  1870. memcpy_toio((void __iomem *) &(cp->args[0]), arg, size);
  1871. if (copyback) {
  1872. portp->argp = arg;
  1873. portp->argsize = size;
  1874. }
  1875. }
  1876. writel(0, &cp->status);
  1877. writel(cmd, &cp->cmd);
  1878. hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
  1879. bits = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset +
  1880. portp->portidx;
  1881. writeb(readb(bits) | portp->portbit, bits);
  1882. set_bit(ST_CMDING, &portp->state);
  1883. EBRDDISABLE(brdp);
  1884. }
  1885. static void stli_sendcmd(struct stlibrd *brdp, struct stliport *portp, unsigned long cmd, void *arg, int size, int copyback)
  1886. {
  1887. unsigned long flags;
  1888. spin_lock_irqsave(&brd_lock, flags);
  1889. __stli_sendcmd(brdp, portp, cmd, arg, size, copyback);
  1890. spin_unlock_irqrestore(&brd_lock, flags);
  1891. }
  1892. /*****************************************************************************/
  1893. /*
  1894. * Read data from shared memory. This assumes that the shared memory
  1895. * is enabled and that interrupts are off. Basically we just empty out
  1896. * the shared memory buffer into the tty buffer. Must be careful to
  1897. * handle the case where we fill up the tty buffer, but still have
  1898. * more chars to unload.
  1899. */
  1900. static void stli_read(struct stlibrd *brdp, struct stliport *portp)
  1901. {
  1902. cdkasyrq_t __iomem *rp;
  1903. char __iomem *shbuf;
  1904. struct tty_struct *tty;
  1905. unsigned int head, tail, size;
  1906. unsigned int len, stlen;
  1907. if (test_bit(ST_RXSTOP, &portp->state))
  1908. return;
  1909. tty = portp->tty;
  1910. if (tty == NULL)
  1911. return;
  1912. rp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->rxq;
  1913. head = (unsigned int) readw(&rp->head);
  1914. if (head != ((unsigned int) readw(&rp->head)))
  1915. head = (unsigned int) readw(&rp->head);
  1916. tail = (unsigned int) readw(&rp->tail);
  1917. size = portp->rxsize;
  1918. if (head >= tail) {
  1919. len = head - tail;
  1920. stlen = len;
  1921. } else {
  1922. len = size - (tail - head);
  1923. stlen = size - tail;
  1924. }
  1925. len = tty_buffer_request_room(tty, len);
  1926. shbuf = (char __iomem *) EBRDGETMEMPTR(brdp, portp->rxoffset);
  1927. while (len > 0) {
  1928. unsigned char *cptr;
  1929. stlen = min(len, stlen);
  1930. tty_prepare_flip_string(tty, &cptr, stlen);
  1931. memcpy_fromio(cptr, shbuf + tail, stlen);
  1932. len -= stlen;
  1933. tail += stlen;
  1934. if (tail >= size) {
  1935. tail = 0;
  1936. stlen = head;
  1937. }
  1938. }
  1939. rp = &((cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr))->rxq;
  1940. writew(tail, &rp->tail);
  1941. if (head != tail)
  1942. set_bit(ST_RXING, &portp->state);
  1943. tty_schedule_flip(tty);
  1944. }
  1945. /*****************************************************************************/
  1946. /*
  1947. * Set up and carry out any delayed commands. There is only a small set
  1948. * of slave commands that can be done "off-level". So it is not too
  1949. * difficult to deal with them here.
  1950. */
  1951. static void stli_dodelaycmd(struct stliport *portp, cdkctrl_t __iomem *cp)
  1952. {
  1953. int cmd;
  1954. if (test_bit(ST_DOSIGS, &portp->state)) {
  1955. if (test_bit(ST_DOFLUSHTX, &portp->state) &&
  1956. test_bit(ST_DOFLUSHRX, &portp->state))
  1957. cmd = A_SETSIGNALSF;
  1958. else if (test_bit(ST_DOFLUSHTX, &portp->state))
  1959. cmd = A_SETSIGNALSFTX;
  1960. else if (test_bit(ST_DOFLUSHRX, &portp->state))
  1961. cmd = A_SETSIGNALSFRX;
  1962. else
  1963. cmd = A_SETSIGNALS;
  1964. clear_bit(ST_DOFLUSHTX, &portp->state);
  1965. clear_bit(ST_DOFLUSHRX, &portp->state);
  1966. clear_bit(ST_DOSIGS, &portp->state);
  1967. memcpy_toio((void __iomem *) &(cp->args[0]), (void *) &portp->asig,
  1968. sizeof(asysigs_t));
  1969. writel(0, &cp->status);
  1970. writel(cmd, &cp->cmd);
  1971. set_bit(ST_CMDING, &portp->state);
  1972. } else if (test_bit(ST_DOFLUSHTX, &portp->state) ||
  1973. test_bit(ST_DOFLUSHRX, &portp->state)) {
  1974. cmd = ((test_bit(ST_DOFLUSHTX, &portp->state)) ? FLUSHTX : 0);
  1975. cmd |= ((test_bit(ST_DOFLUSHRX, &portp->state)) ? FLUSHRX : 0);
  1976. clear_bit(ST_DOFLUSHTX, &portp->state);
  1977. clear_bit(ST_DOFLUSHRX, &portp->state);
  1978. memcpy_toio((void __iomem *) &(cp->args[0]), (void *) &cmd, sizeof(int));
  1979. writel(0, &cp->status);
  1980. writel(A_FLUSH, &cp->cmd);
  1981. set_bit(ST_CMDING, &portp->state);
  1982. }
  1983. }
  1984. /*****************************************************************************/
  1985. /*
  1986. * Host command service checking. This handles commands or messages
  1987. * coming from the slave to the host. Must have board shared memory
  1988. * enabled and interrupts off when called. Notice that by servicing the
  1989. * read data last we don't need to change the shared memory pointer
  1990. * during processing (which is a slow IO operation).
  1991. * Return value indicates if this port is still awaiting actions from
  1992. * the slave (like open, command, or even TX data being sent). If 0
  1993. * then port is still busy, otherwise no longer busy.
  1994. */
  1995. static int stli_hostcmd(struct stlibrd *brdp, struct stliport *portp)
  1996. {
  1997. cdkasy_t __iomem *ap;
  1998. cdkctrl_t __iomem *cp;
  1999. struct tty_struct *tty;
  2000. asynotify_t nt;
  2001. unsigned long oldsigs;
  2002. int rc, donerx;
  2003. ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr);
  2004. cp = &ap->ctrl;
  2005. /*
  2006. * Check if we are waiting for an open completion message.
  2007. */
  2008. if (test_bit(ST_OPENING, &portp->state)) {
  2009. rc = readl(&cp->openarg);
  2010. if (readb(&cp->open) == 0 && rc != 0) {
  2011. if (rc > 0)
  2012. rc--;
  2013. writel(0, &cp->openarg);
  2014. portp->rc = rc;
  2015. clear_bit(ST_OPENING, &portp->state);
  2016. wake_up_interruptible(&portp->raw_wait);
  2017. }
  2018. }
  2019. /*
  2020. * Check if we are waiting for a close completion message.
  2021. */
  2022. if (test_bit(ST_CLOSING, &portp->state)) {
  2023. rc = (int) readl(&cp->closearg);
  2024. if (readb(&cp->close) == 0 && rc != 0) {
  2025. if (rc > 0)
  2026. rc--;
  2027. writel(0, &cp->closearg);
  2028. portp->rc = rc;
  2029. clear_bit(ST_CLOSING, &portp->state);
  2030. wake_up_interruptible(&portp->raw_wait);
  2031. }
  2032. }
  2033. /*
  2034. * Check if we are waiting for a command completion message. We may
  2035. * need to copy out the command results associated with this command.
  2036. */
  2037. if (test_bit(ST_CMDING, &portp->state)) {
  2038. rc = readl(&cp->status);
  2039. if (readl(&cp->cmd) == 0 && rc != 0) {
  2040. if (rc > 0)
  2041. rc--;
  2042. if (portp->argp != NULL) {
  2043. memcpy_fromio(portp->argp, (void __iomem *) &(cp->args[0]),
  2044. portp->argsize);
  2045. portp->argp = NULL;
  2046. }
  2047. writel(0, &cp->status);
  2048. portp->rc = rc;
  2049. clear_bit(ST_CMDING, &portp->state);
  2050. stli_dodelaycmd(portp, cp);
  2051. wake_up_interruptible(&portp->raw_wait);
  2052. }
  2053. }
  2054. /*
  2055. * Check for any notification messages ready. This includes lots of
  2056. * different types of events - RX chars ready, RX break received,
  2057. * TX data low or empty in the slave, modem signals changed state.
  2058. */
  2059. donerx = 0;
  2060. if (ap->notify) {
  2061. nt = ap->changed;
  2062. ap->notify = 0;
  2063. tty = portp->tty;
  2064. if (nt.signal & SG_DCD) {
  2065. oldsigs = portp->sigs;
  2066. portp->sigs = stli_mktiocm(nt.sigvalue);
  2067. clear_bit(ST_GETSIGS, &portp->state);
  2068. if ((portp->sigs & TIOCM_CD) &&
  2069. ((oldsigs & TIOCM_CD) == 0))
  2070. wake_up_interruptible(&portp->open_wait);
  2071. if ((oldsigs & TIOCM_CD) &&
  2072. ((portp->sigs & TIOCM_CD) == 0)) {
  2073. if (portp->flags & ASYNC_CHECK_CD) {
  2074. if (tty)
  2075. schedule_work(&portp->tqhangup);
  2076. }
  2077. }
  2078. }
  2079. if (nt.data & DT_TXEMPTY)
  2080. clear_bit(ST_TXBUSY, &portp->state);
  2081. if (nt.data & (DT_TXEMPTY | DT_TXLOW)) {
  2082. if (tty != NULL) {
  2083. tty_wakeup(tty);
  2084. EBRDENABLE(brdp);
  2085. }
  2086. }
  2087. if ((nt.data & DT_RXBREAK) && (portp->rxmarkmsk & BRKINT)) {
  2088. if (tty != NULL) {
  2089. tty_insert_flip_char(tty, 0, TTY_BREAK);
  2090. if (portp->flags & ASYNC_SAK) {
  2091. do_SAK(tty);
  2092. EBRDENABLE(brdp);
  2093. }
  2094. tty_schedule_flip(tty);
  2095. }
  2096. }
  2097. if (nt.data & DT_RXBUSY) {
  2098. donerx++;
  2099. stli_read(brdp, portp);
  2100. }
  2101. }
  2102. /*
  2103. * It might seem odd that we are checking for more RX chars here.
  2104. * But, we need to handle the case where the tty buffer was previously
  2105. * filled, but we had more characters to pass up. The slave will not
  2106. * send any more RX notify messages until the RX buffer has been emptied.
  2107. * But it will leave the service bits on (since the buffer is not empty).
  2108. * So from here we can try to process more RX chars.
  2109. */
  2110. if ((!donerx) && test_bit(ST_RXING, &portp->state)) {
  2111. clear_bit(ST_RXING, &portp->state);
  2112. stli_read(brdp, portp);
  2113. }
  2114. return((test_bit(ST_OPENING, &portp->state) ||
  2115. test_bit(ST_CLOSING, &portp->state) ||
  2116. test_bit(ST_CMDING, &portp->state) ||
  2117. test_bit(ST_TXBUSY, &portp->state) ||
  2118. test_bit(ST_RXING, &portp->state)) ? 0 : 1);
  2119. }
  2120. /*****************************************************************************/
  2121. /*
  2122. * Service all ports on a particular board. Assumes that the boards
  2123. * shared memory is enabled, and that the page pointer is pointed
  2124. * at the cdk header structure.
  2125. */
  2126. static void stli_brdpoll(struct stlibrd *brdp, cdkhdr_t __iomem *hdrp)
  2127. {
  2128. struct stliport *portp;
  2129. unsigned char hostbits[(STL_MAXCHANS / 8) + 1];
  2130. unsigned char slavebits[(STL_MAXCHANS / 8) + 1];
  2131. unsigned char __iomem *slavep;
  2132. int bitpos, bitat, bitsize;
  2133. int channr, nrdevs, slavebitchange;
  2134. bitsize = brdp->bitsize;
  2135. nrdevs = brdp->nrdevs;
  2136. /*
  2137. * Check if slave wants any service. Basically we try to do as
  2138. * little work as possible here. There are 2 levels of service
  2139. * bits. So if there is nothing to do we bail early. We check
  2140. * 8 service bits at a time in the inner loop, so we can bypass
  2141. * the lot if none of them want service.
  2142. */
  2143. memcpy_fromio(&hostbits[0], (((unsigned char __iomem *) hdrp) + brdp->hostoffset),
  2144. bitsize);
  2145. memset(&slavebits[0], 0, bitsize);
  2146. slavebitchange = 0;
  2147. for (bitpos = 0; (bitpos < bitsize); bitpos++) {
  2148. if (hostbits[bitpos] == 0)
  2149. continue;
  2150. channr = bitpos * 8;
  2151. for (bitat = 0x1; (channr < nrdevs); channr++, bitat <<= 1) {
  2152. if (hostbits[bitpos] & bitat) {
  2153. portp = brdp->ports[(channr - 1)];
  2154. if (stli_hostcmd(brdp, portp)) {
  2155. slavebitchange++;
  2156. slavebits[bitpos] |= bitat;
  2157. }
  2158. }
  2159. }
  2160. }
  2161. /*
  2162. * If any of the ports are no longer busy then update them in the
  2163. * slave request bits. We need to do this after, since a host port
  2164. * service may initiate more slave requests.
  2165. */
  2166. if (slavebitchange) {
  2167. hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
  2168. slavep = ((unsigned char __iomem *) hdrp) + brdp->slaveoffset;
  2169. for (bitpos = 0; (bitpos < bitsize); bitpos++) {
  2170. if (readb(slavebits + bitpos))
  2171. writeb(readb(slavep + bitpos) & ~slavebits[bitpos], slavebits + bitpos);
  2172. }
  2173. }
  2174. }
  2175. /*****************************************************************************/
  2176. /*
  2177. * Driver poll routine. This routine polls the boards in use and passes
  2178. * messages back up to host when necessary. This is actually very
  2179. * CPU efficient, since we will always have the kernel poll clock, it
  2180. * adds only a few cycles when idle (since board service can be
  2181. * determined very easily), but when loaded generates no interrupts
  2182. * (with their expensive associated context change).
  2183. */
  2184. static void stli_poll(unsigned long arg)
  2185. {
  2186. cdkhdr_t __iomem *hdrp;
  2187. struct stlibrd *brdp;
  2188. unsigned int brdnr;
  2189. mod_timer(&stli_timerlist, STLI_TIMEOUT);
  2190. /*
  2191. * Check each board and do any servicing required.
  2192. */
  2193. for (brdnr = 0; (brdnr < stli_nrbrds); brdnr++) {
  2194. brdp = stli_brds[brdnr];
  2195. if (brdp == NULL)
  2196. continue;
  2197. if ((brdp->state & BST_STARTED) == 0)
  2198. continue;
  2199. spin_lock(&brd_lock);
  2200. EBRDENABLE(brdp);
  2201. hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
  2202. if (readb(&hdrp->hostreq))
  2203. stli_brdpoll(brdp, hdrp);
  2204. EBRDDISABLE(brdp);
  2205. spin_unlock(&brd_lock);
  2206. }
  2207. }
  2208. /*****************************************************************************/
  2209. /*
  2210. * Translate the termios settings into the port setting structure of
  2211. * the slave.
  2212. */
  2213. static void stli_mkasyport(struct stliport *portp, asyport_t *pp, struct ktermios *tiosp)
  2214. {
  2215. memset(pp, 0, sizeof(asyport_t));
  2216. /*
  2217. * Start of by setting the baud, char size, parity and stop bit info.
  2218. */
  2219. pp->baudout = tty_get_baud_rate(portp->tty);
  2220. if ((tiosp->c_cflag & CBAUD) == B38400) {
  2221. if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
  2222. pp->baudout = 57600;
  2223. else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
  2224. pp->baudout = 115200;
  2225. else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
  2226. pp->baudout = 230400;
  2227. else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
  2228. pp->baudout = 460800;
  2229. else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
  2230. pp->baudout = (portp->baud_base / portp->custom_divisor);
  2231. }
  2232. if (pp->baudout > STL_MAXBAUD)
  2233. pp->baudout = STL_MAXBAUD;
  2234. pp->baudin = pp->baudout;
  2235. switch (tiosp->c_cflag & CSIZE) {
  2236. case CS5:
  2237. pp->csize = 5;
  2238. break;
  2239. case CS6:
  2240. pp->csize = 6;
  2241. break;
  2242. case CS7:
  2243. pp->csize = 7;
  2244. break;
  2245. default:
  2246. pp->csize = 8;
  2247. break;
  2248. }
  2249. if (tiosp->c_cflag & CSTOPB)
  2250. pp->stopbs = PT_STOP2;
  2251. else
  2252. pp->stopbs = PT_STOP1;
  2253. if (tiosp->c_cflag & PARENB) {
  2254. if (tiosp->c_cflag & PARODD)
  2255. pp->parity = PT_ODDPARITY;
  2256. else
  2257. pp->parity = PT_EVENPARITY;
  2258. } else {
  2259. pp->parity = PT_NOPARITY;
  2260. }
  2261. /*
  2262. * Set up any flow control options enabled.
  2263. */
  2264. if (tiosp->c_iflag & IXON) {
  2265. pp->flow |= F_IXON;
  2266. if (tiosp->c_iflag & IXANY)
  2267. pp->flow |= F_IXANY;
  2268. }
  2269. if (tiosp->c_cflag & CRTSCTS)
  2270. pp->flow |= (F_RTSFLOW | F_CTSFLOW);
  2271. pp->startin = tiosp->c_cc[VSTART];
  2272. pp->stopin = tiosp->c_cc[VSTOP];
  2273. pp->startout = tiosp->c_cc[VSTART];
  2274. pp->stopout = tiosp->c_cc[VSTOP];
  2275. /*
  2276. * Set up the RX char marking mask with those RX error types we must
  2277. * catch. We can get the slave to help us out a little here, it will
  2278. * ignore parity errors and breaks for us, and mark parity errors in
  2279. * the data stream.
  2280. */
  2281. if (tiosp->c_iflag & IGNPAR)
  2282. pp->iflag |= FI_IGNRXERRS;
  2283. if (tiosp->c_iflag & IGNBRK)
  2284. pp->iflag |= FI_IGNBREAK;
  2285. portp->rxmarkmsk = 0;
  2286. if (tiosp->c_iflag & (INPCK | PARMRK))
  2287. pp->iflag |= FI_1MARKRXERRS;
  2288. if (tiosp->c_iflag & BRKINT)
  2289. portp->rxmarkmsk |= BRKINT;
  2290. /*
  2291. * Set up clocal processing as required.
  2292. */
  2293. if (tiosp->c_cflag & CLOCAL)
  2294. portp->flags &= ~ASYNC_CHECK_CD;
  2295. else
  2296. portp->flags |= ASYNC_CHECK_CD;
  2297. /*
  2298. * Transfer any persistent flags into the asyport structure.
  2299. */
  2300. pp->pflag = (portp->pflag & 0xffff);
  2301. pp->vmin = (portp->pflag & P_RXIMIN) ? 1 : 0;
  2302. pp->vtime = (portp->pflag & P_RXITIME) ? 1 : 0;
  2303. pp->cc[1] = (portp->pflag & P_RXTHOLD) ? 1 : 0;
  2304. }
  2305. /*****************************************************************************/
  2306. /*
  2307. * Construct a slave signals structure for setting the DTR and RTS
  2308. * signals as specified.
  2309. */
  2310. static void stli_mkasysigs(asysigs_t *sp, int dtr, int rts)
  2311. {
  2312. memset(sp, 0, sizeof(asysigs_t));
  2313. if (dtr >= 0) {
  2314. sp->signal |= SG_DTR;
  2315. sp->sigvalue |= ((dtr > 0) ? SG_DTR : 0);
  2316. }
  2317. if (rts >= 0) {
  2318. sp->signal |= SG_RTS;
  2319. sp->sigvalue |= ((rts > 0) ? SG_RTS : 0);
  2320. }
  2321. }
  2322. /*****************************************************************************/
  2323. /*
  2324. * Convert the signals returned from the slave into a local TIOCM type
  2325. * signals value. We keep them locally in TIOCM format.
  2326. */
  2327. static long stli_mktiocm(unsigned long sigvalue)
  2328. {
  2329. long tiocm = 0;
  2330. tiocm |= ((sigvalue & SG_DCD) ? TIOCM_CD : 0);
  2331. tiocm |= ((sigvalue & SG_CTS) ? TIOCM_CTS : 0);
  2332. tiocm |= ((sigvalue & SG_RI) ? TIOCM_RI : 0);
  2333. tiocm |= ((sigvalue & SG_DSR) ? TIOCM_DSR : 0);
  2334. tiocm |= ((sigvalue & SG_DTR) ? TIOCM_DTR : 0);
  2335. tiocm |= ((sigvalue & SG_RTS) ? TIOCM_RTS : 0);
  2336. return(tiocm);
  2337. }
  2338. /*****************************************************************************/
  2339. /*
  2340. * All panels and ports actually attached have been worked out. All
  2341. * we need to do here is set up the appropriate per port data structures.
  2342. */
  2343. static int stli_initports(struct stlibrd *brdp)
  2344. {
  2345. struct stliport *portp;
  2346. unsigned int i, panelnr, panelport;
  2347. for (i = 0, panelnr = 0, panelport = 0; (i < brdp->nrports); i++) {
  2348. portp = kzalloc(sizeof(struct stliport), GFP_KERNEL);
  2349. if (!portp) {
  2350. printk("STALLION: failed to allocate port structure\n");
  2351. continue;
  2352. }
  2353. portp->magic = STLI_PORTMAGIC;
  2354. portp->portnr = i;
  2355. portp->brdnr = brdp->brdnr;
  2356. portp->panelnr = panelnr;
  2357. portp->baud_base = STL_BAUDBASE;
  2358. portp->close_delay = STL_CLOSEDELAY;
  2359. portp->closing_wait = 30 * HZ;
  2360. INIT_WORK(&portp->tqhangup, stli_dohangup);
  2361. init_waitqueue_head(&portp->open_wait);
  2362. init_waitqueue_head(&portp->close_wait);
  2363. init_waitqueue_head(&portp->raw_wait);
  2364. panelport++;
  2365. if (panelport >= brdp->panels[panelnr]) {
  2366. panelport = 0;
  2367. panelnr++;
  2368. }
  2369. brdp->ports[i] = portp;
  2370. }
  2371. return 0;
  2372. }
  2373. /*****************************************************************************/
  2374. /*
  2375. * All the following routines are board specific hardware operations.
  2376. */
  2377. static void stli_ecpinit(struct stlibrd *brdp)
  2378. {
  2379. unsigned long memconf;
  2380. outb(ECP_ATSTOP, (brdp->iobase + ECP_ATCONFR));
  2381. udelay(10);
  2382. outb(ECP_ATDISABLE, (brdp->iobase + ECP_ATCONFR));
  2383. udelay(100);
  2384. memconf = (brdp->memaddr & ECP_ATADDRMASK) >> ECP_ATADDRSHFT;
  2385. outb(memconf, (brdp->iobase + ECP_ATMEMAR));
  2386. }
  2387. /*****************************************************************************/
  2388. static void stli_ecpenable(struct stlibrd *brdp)
  2389. {
  2390. outb(ECP_ATENABLE, (brdp->iobase + ECP_ATCONFR));
  2391. }
  2392. /*****************************************************************************/
  2393. static void stli_ecpdisable(struct stlibrd *brdp)
  2394. {
  2395. outb(ECP_ATDISABLE, (brdp->iobase + ECP_ATCONFR));
  2396. }
  2397. /*****************************************************************************/
  2398. static void __iomem *stli_ecpgetmemptr(struct stlibrd *brdp, unsigned long offset, int line)
  2399. {
  2400. void __iomem *ptr;
  2401. unsigned char val;
  2402. if (offset > brdp->memsize) {
  2403. printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
  2404. "range at line=%d(%d), brd=%d\n",
  2405. (int) offset, line, __LINE__, brdp->brdnr);
  2406. ptr = NULL;
  2407. val = 0;
  2408. } else {
  2409. ptr = brdp->membase + (offset % ECP_ATPAGESIZE);
  2410. val = (unsigned char) (offset / ECP_ATPAGESIZE);
  2411. }
  2412. outb(val, (brdp->iobase + ECP_ATMEMPR));
  2413. return(ptr);
  2414. }
  2415. /*****************************************************************************/
  2416. static void stli_ecpreset(struct stlibrd *brdp)
  2417. {
  2418. outb(ECP_ATSTOP, (brdp->iobase + ECP_ATCONFR));
  2419. udelay(10);
  2420. outb(ECP_ATDISABLE, (brdp->iobase + ECP_ATCONFR));
  2421. udelay(500);
  2422. }
  2423. /*****************************************************************************/
  2424. static void stli_ecpintr(struct stlibrd *brdp)
  2425. {
  2426. outb(0x1, brdp->iobase);
  2427. }
  2428. /*****************************************************************************/
  2429. /*
  2430. * The following set of functions act on ECP EISA boards.
  2431. */
  2432. static void stli_ecpeiinit(struct stlibrd *brdp)
  2433. {
  2434. unsigned long memconf;
  2435. outb(0x1, (brdp->iobase + ECP_EIBRDENAB));
  2436. outb(ECP_EISTOP, (brdp->iobase + ECP_EICONFR));
  2437. udelay(10);
  2438. outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
  2439. udelay(500);
  2440. memconf = (brdp->memaddr & ECP_EIADDRMASKL) >> ECP_EIADDRSHFTL;
  2441. outb(memconf, (brdp->iobase + ECP_EIMEMARL));
  2442. memconf = (brdp->memaddr & ECP_EIADDRMASKH) >> ECP_EIADDRSHFTH;
  2443. outb(memconf, (brdp->iobase + ECP_EIMEMARH));
  2444. }
  2445. /*****************************************************************************/
  2446. static void stli_ecpeienable(struct stlibrd *brdp)
  2447. {
  2448. outb(ECP_EIENABLE, (brdp->iobase + ECP_EICONFR));
  2449. }
  2450. /*****************************************************************************/
  2451. static void stli_ecpeidisable(struct stlibrd *brdp)
  2452. {
  2453. outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
  2454. }
  2455. /*****************************************************************************/
  2456. static void __iomem *stli_ecpeigetmemptr(struct stlibrd *brdp, unsigned long offset, int line)
  2457. {
  2458. void __iomem *ptr;
  2459. unsigned char val;
  2460. if (offset > brdp->memsize) {
  2461. printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
  2462. "range at line=%d(%d), brd=%d\n",
  2463. (int) offset, line, __LINE__, brdp->brdnr);
  2464. ptr = NULL;
  2465. val = 0;
  2466. } else {
  2467. ptr = brdp->membase + (offset % ECP_EIPAGESIZE);
  2468. if (offset < ECP_EIPAGESIZE)
  2469. val = ECP_EIENABLE;
  2470. else
  2471. val = ECP_EIENABLE | 0x40;
  2472. }
  2473. outb(val, (brdp->iobase + ECP_EICONFR));
  2474. return(ptr);
  2475. }
  2476. /*****************************************************************************/
  2477. static void stli_ecpeireset(struct stlibrd *brdp)
  2478. {
  2479. outb(ECP_EISTOP, (brdp->iobase + ECP_EICONFR));
  2480. udelay(10);
  2481. outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
  2482. udelay(500);
  2483. }
  2484. /*****************************************************************************/
  2485. /*
  2486. * The following set of functions act on ECP MCA boards.
  2487. */
  2488. static void stli_ecpmcenable(struct stlibrd *brdp)
  2489. {
  2490. outb(ECP_MCENABLE, (brdp->iobase + ECP_MCCONFR));
  2491. }
  2492. /*****************************************************************************/
  2493. static void stli_ecpmcdisable(struct stlibrd *brdp)
  2494. {
  2495. outb(ECP_MCDISABLE, (brdp->iobase + ECP_MCCONFR));
  2496. }
  2497. /*****************************************************************************/
  2498. static void __iomem *stli_ecpmcgetmemptr(struct stlibrd *brdp, unsigned long offset, int line)
  2499. {
  2500. void __iomem *ptr;
  2501. unsigned char val;
  2502. if (offset > brdp->memsize) {
  2503. printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
  2504. "range at line=%d(%d), brd=%d\n",
  2505. (int) offset, line, __LINE__, brdp->brdnr);
  2506. ptr = NULL;
  2507. val = 0;
  2508. } else {
  2509. ptr = brdp->membase + (offset % ECP_MCPAGESIZE);
  2510. val = ((unsigned char) (offset / ECP_MCPAGESIZE)) | ECP_MCENABLE;
  2511. }
  2512. outb(val, (brdp->iobase + ECP_MCCONFR));
  2513. return(ptr);
  2514. }
  2515. /*****************************************************************************/
  2516. static void stli_ecpmcreset(struct stlibrd *brdp)
  2517. {
  2518. outb(ECP_MCSTOP, (brdp->iobase + ECP_MCCONFR));
  2519. udelay(10);
  2520. outb(ECP_MCDISABLE, (brdp->iobase + ECP_MCCONFR));
  2521. udelay(500);
  2522. }
  2523. /*****************************************************************************/
  2524. /*
  2525. * The following set of functions act on ECP PCI boards.
  2526. */
  2527. static void stli_ecppciinit(struct stlibrd *brdp)
  2528. {
  2529. outb(ECP_PCISTOP, (brdp->iobase + ECP_PCICONFR));
  2530. udelay(10);
  2531. outb(0, (brdp->iobase + ECP_PCICONFR));
  2532. udelay(500);
  2533. }
  2534. /*****************************************************************************/
  2535. static void __iomem *stli_ecppcigetmemptr(struct stlibrd *brdp, unsigned long offset, int line)
  2536. {
  2537. void __iomem *ptr;
  2538. unsigned char val;
  2539. if (offset > brdp->memsize) {
  2540. printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
  2541. "range at line=%d(%d), board=%d\n",
  2542. (int) offset, line, __LINE__, brdp->brdnr);
  2543. ptr = NULL;
  2544. val = 0;
  2545. } else {
  2546. ptr = brdp->membase + (offset % ECP_PCIPAGESIZE);
  2547. val = (offset / ECP_PCIPAGESIZE) << 1;
  2548. }
  2549. outb(val, (brdp->iobase + ECP_PCICONFR));
  2550. return(ptr);
  2551. }
  2552. /*****************************************************************************/
  2553. static void stli_ecppcireset(struct stlibrd *brdp)
  2554. {
  2555. outb(ECP_PCISTOP, (brdp->iobase + ECP_PCICONFR));
  2556. udelay(10);
  2557. outb(0, (brdp->iobase + ECP_PCICONFR));
  2558. udelay(500);
  2559. }
  2560. /*****************************************************************************/
  2561. /*
  2562. * The following routines act on ONboards.
  2563. */
  2564. static void stli_onbinit(struct stlibrd *brdp)
  2565. {
  2566. unsigned long memconf;
  2567. outb(ONB_ATSTOP, (brdp->iobase + ONB_ATCONFR));
  2568. udelay(10);
  2569. outb(ONB_ATDISABLE, (brdp->iobase + ONB_ATCONFR));
  2570. mdelay(1000);
  2571. memconf = (brdp->memaddr & ONB_ATADDRMASK) >> ONB_ATADDRSHFT;
  2572. outb(memconf, (brdp->iobase + ONB_ATMEMAR));
  2573. outb(0x1, brdp->iobase);
  2574. mdelay(1);
  2575. }
  2576. /*****************************************************************************/
  2577. static void stli_onbenable(struct stlibrd *brdp)
  2578. {
  2579. outb((brdp->enabval | ONB_ATENABLE), (brdp->iobase + ONB_ATCONFR));
  2580. }
  2581. /*****************************************************************************/
  2582. static void stli_onbdisable(struct stlibrd *brdp)
  2583. {
  2584. outb((brdp->enabval | ONB_ATDISABLE), (brdp->iobase + ONB_ATCONFR));
  2585. }
  2586. /*****************************************************************************/
  2587. static void __iomem *stli_onbgetmemptr(struct stlibrd *brdp, unsigned long offset, int line)
  2588. {
  2589. void __iomem *ptr;
  2590. if (offset > brdp->memsize) {
  2591. printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
  2592. "range at line=%d(%d), brd=%d\n",
  2593. (int) offset, line, __LINE__, brdp->brdnr);
  2594. ptr = NULL;
  2595. } else {
  2596. ptr = brdp->membase + (offset % ONB_ATPAGESIZE);
  2597. }
  2598. return(ptr);
  2599. }
  2600. /*****************************************************************************/
  2601. static void stli_onbreset(struct stlibrd *brdp)
  2602. {
  2603. outb(ONB_ATSTOP, (brdp->iobase + ONB_ATCONFR));
  2604. udelay(10);
  2605. outb(ONB_ATDISABLE, (brdp->iobase + ONB_ATCONFR));
  2606. mdelay(1000);
  2607. }
  2608. /*****************************************************************************/
  2609. /*
  2610. * The following routines act on ONboard EISA.
  2611. */
  2612. static void stli_onbeinit(struct stlibrd *brdp)
  2613. {
  2614. unsigned long memconf;
  2615. outb(0x1, (brdp->iobase + ONB_EIBRDENAB));
  2616. outb(ONB_EISTOP, (brdp->iobase + ONB_EICONFR));
  2617. udelay(10);
  2618. outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
  2619. mdelay(1000);
  2620. memconf = (brdp->memaddr & ONB_EIADDRMASKL) >> ONB_EIADDRSHFTL;
  2621. outb(memconf, (brdp->iobase + ONB_EIMEMARL));
  2622. memconf = (brdp->memaddr & ONB_EIADDRMASKH) >> ONB_EIADDRSHFTH;
  2623. outb(memconf, (brdp->iobase + ONB_EIMEMARH));
  2624. outb(0x1, brdp->iobase);
  2625. mdelay(1);
  2626. }
  2627. /*****************************************************************************/
  2628. static void stli_onbeenable(struct stlibrd *brdp)
  2629. {
  2630. outb(ONB_EIENABLE, (brdp->iobase + ONB_EICONFR));
  2631. }
  2632. /*****************************************************************************/
  2633. static void stli_onbedisable(struct stlibrd *brdp)
  2634. {
  2635. outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
  2636. }
  2637. /*****************************************************************************/
  2638. static void __iomem *stli_onbegetmemptr(struct stlibrd *brdp, unsigned long offset, int line)
  2639. {
  2640. void __iomem *ptr;
  2641. unsigned char val;
  2642. if (offset > brdp->memsize) {
  2643. printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
  2644. "range at line=%d(%d), brd=%d\n",
  2645. (int) offset, line, __LINE__, brdp->brdnr);
  2646. ptr = NULL;
  2647. val = 0;
  2648. } else {
  2649. ptr = brdp->membase + (offset % ONB_EIPAGESIZE);
  2650. if (offset < ONB_EIPAGESIZE)
  2651. val = ONB_EIENABLE;
  2652. else
  2653. val = ONB_EIENABLE | 0x40;
  2654. }
  2655. outb(val, (brdp->iobase + ONB_EICONFR));
  2656. return(ptr);
  2657. }
  2658. /*****************************************************************************/
  2659. static void stli_onbereset(struct stlibrd *brdp)
  2660. {
  2661. outb(ONB_EISTOP, (brdp->iobase + ONB_EICONFR));
  2662. udelay(10);
  2663. outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
  2664. mdelay(1000);
  2665. }
  2666. /*****************************************************************************/
  2667. /*
  2668. * The following routines act on Brumby boards.
  2669. */
  2670. static void stli_bbyinit(struct stlibrd *brdp)
  2671. {
  2672. outb(BBY_ATSTOP, (brdp->iobase + BBY_ATCONFR));
  2673. udelay(10);
  2674. outb(0, (brdp->iobase + BBY_ATCONFR));
  2675. mdelay(1000);
  2676. outb(0x1, brdp->iobase);
  2677. mdelay(1);
  2678. }
  2679. /*****************************************************************************/
  2680. static void __iomem *stli_bbygetmemptr(struct stlibrd *brdp, unsigned long offset, int line)
  2681. {
  2682. void __iomem *ptr;
  2683. unsigned char val;
  2684. BUG_ON(offset > brdp->memsize);
  2685. ptr = brdp->membase + (offset % BBY_PAGESIZE);
  2686. val = (unsigned char) (offset / BBY_PAGESIZE);
  2687. outb(val, (brdp->iobase + BBY_ATCONFR));
  2688. return(ptr);
  2689. }
  2690. /*****************************************************************************/
  2691. static void stli_bbyreset(struct stlibrd *brdp)
  2692. {
  2693. outb(BBY_ATSTOP, (brdp->iobase + BBY_ATCONFR));
  2694. udelay(10);
  2695. outb(0, (brdp->iobase + BBY_ATCONFR));
  2696. mdelay(1000);
  2697. }
  2698. /*****************************************************************************/
  2699. /*
  2700. * The following routines act on original old Stallion boards.
  2701. */
  2702. static void stli_stalinit(struct stlibrd *brdp)
  2703. {
  2704. outb(0x1, brdp->iobase);
  2705. mdelay(1000);
  2706. }
  2707. /*****************************************************************************/
  2708. static void __iomem *stli_stalgetmemptr(struct stlibrd *brdp, unsigned long offset, int line)
  2709. {
  2710. BUG_ON(offset > brdp->memsize);
  2711. return brdp->membase + (offset % STAL_PAGESIZE);
  2712. }
  2713. /*****************************************************************************/
  2714. static void stli_stalreset(struct stlibrd *brdp)
  2715. {
  2716. u32 __iomem *vecp;
  2717. vecp = (u32 __iomem *) (brdp->membase + 0x30);
  2718. writel(0xffff0000, vecp);
  2719. outb(0, brdp->iobase);
  2720. mdelay(1000);
  2721. }
  2722. /*****************************************************************************/
  2723. /*
  2724. * Try to find an ECP board and initialize it. This handles only ECP
  2725. * board types.
  2726. */
  2727. static int stli_initecp(struct stlibrd *brdp)
  2728. {
  2729. cdkecpsig_t sig;
  2730. cdkecpsig_t __iomem *sigsp;
  2731. unsigned int status, nxtid;
  2732. char *name;
  2733. int retval, panelnr, nrports;
  2734. if ((brdp->iobase == 0) || (brdp->memaddr == 0)) {
  2735. retval = -ENODEV;
  2736. goto err;
  2737. }
  2738. brdp->iosize = ECP_IOSIZE;
  2739. if (!request_region(brdp->iobase, brdp->iosize, "istallion")) {
  2740. retval = -EIO;
  2741. goto err;
  2742. }
  2743. /*
  2744. * Based on the specific board type setup the common vars to access
  2745. * and enable shared memory. Set all board specific information now
  2746. * as well.
  2747. */
  2748. switch (brdp->brdtype) {
  2749. case BRD_ECP:
  2750. brdp->memsize = ECP_MEMSIZE;
  2751. brdp->pagesize = ECP_ATPAGESIZE;
  2752. brdp->init = stli_ecpinit;
  2753. brdp->enable = stli_ecpenable;
  2754. brdp->reenable = stli_ecpenable;
  2755. brdp->disable = stli_ecpdisable;
  2756. brdp->getmemptr = stli_ecpgetmemptr;
  2757. brdp->intr = stli_ecpintr;
  2758. brdp->reset = stli_ecpreset;
  2759. name = "serial(EC8/64)";
  2760. break;
  2761. case BRD_ECPE:
  2762. brdp->memsize = ECP_MEMSIZE;
  2763. brdp->pagesize = ECP_EIPAGESIZE;
  2764. brdp->init = stli_ecpeiinit;
  2765. brdp->enable = stli_ecpeienable;
  2766. brdp->reenable = stli_ecpeienable;
  2767. brdp->disable = stli_ecpeidisable;
  2768. brdp->getmemptr = stli_ecpeigetmemptr;
  2769. brdp->intr = stli_ecpintr;
  2770. brdp->reset = stli_ecpeireset;
  2771. name = "serial(EC8/64-EI)";
  2772. break;
  2773. case BRD_ECPMC:
  2774. brdp->memsize = ECP_MEMSIZE;
  2775. brdp->pagesize = ECP_MCPAGESIZE;
  2776. brdp->init = NULL;
  2777. brdp->enable = stli_ecpmcenable;
  2778. brdp->reenable = stli_ecpmcenable;
  2779. brdp->disable = stli_ecpmcdisable;
  2780. brdp->getmemptr = stli_ecpmcgetmemptr;
  2781. brdp->intr = stli_ecpintr;
  2782. brdp->reset = stli_ecpmcreset;
  2783. name = "serial(EC8/64-MCA)";
  2784. break;
  2785. case BRD_ECPPCI:
  2786. brdp->memsize = ECP_PCIMEMSIZE;
  2787. brdp->pagesize = ECP_PCIPAGESIZE;
  2788. brdp->init = stli_ecppciinit;
  2789. brdp->enable = NULL;
  2790. brdp->reenable = NULL;
  2791. brdp->disable = NULL;
  2792. brdp->getmemptr = stli_ecppcigetmemptr;
  2793. brdp->intr = stli_ecpintr;
  2794. brdp->reset = stli_ecppcireset;
  2795. name = "serial(EC/RA-PCI)";
  2796. break;
  2797. default:
  2798. retval = -EINVAL;
  2799. goto err_reg;
  2800. }
  2801. /*
  2802. * The per-board operations structure is all set up, so now let's go
  2803. * and get the board operational. Firstly initialize board configuration
  2804. * registers. Set the memory mapping info so we can get at the boards
  2805. * shared memory.
  2806. */
  2807. EBRDINIT(brdp);
  2808. brdp->membase = ioremap(brdp->memaddr, brdp->memsize);
  2809. if (brdp->membase == NULL) {
  2810. retval = -ENOMEM;
  2811. goto err_reg;
  2812. }
  2813. /*
  2814. * Now that all specific code is set up, enable the shared memory and
  2815. * look for the a signature area that will tell us exactly what board
  2816. * this is, and what it is connected to it.
  2817. */
  2818. EBRDENABLE(brdp);
  2819. sigsp = (cdkecpsig_t __iomem *) EBRDGETMEMPTR(brdp, CDK_SIGADDR);
  2820. memcpy_fromio(&sig, sigsp, sizeof(cdkecpsig_t));
  2821. EBRDDISABLE(brdp);
  2822. if (sig.magic != cpu_to_le32(ECP_MAGIC)) {
  2823. retval = -ENODEV;
  2824. goto err_unmap;
  2825. }
  2826. /*
  2827. * Scan through the signature looking at the panels connected to the
  2828. * board. Calculate the total number of ports as we go.
  2829. */
  2830. for (panelnr = 0, nxtid = 0; (panelnr < STL_MAXPANELS); panelnr++) {
  2831. status = sig.panelid[nxtid];
  2832. if ((status & ECH_PNLIDMASK) != nxtid)
  2833. break;
  2834. brdp->panelids[panelnr] = status;
  2835. nrports = (status & ECH_PNL16PORT) ? 16 : 8;
  2836. if ((nrports == 16) && ((status & ECH_PNLXPID) == 0))
  2837. nxtid++;
  2838. brdp->panels[panelnr] = nrports;
  2839. brdp->nrports += nrports;
  2840. nxtid++;
  2841. brdp->nrpanels++;
  2842. }
  2843. brdp->state |= BST_FOUND;
  2844. return 0;
  2845. err_unmap:
  2846. iounmap(brdp->membase);
  2847. brdp->membase = NULL;
  2848. err_reg:
  2849. release_region(brdp->iobase, brdp->iosize);
  2850. err:
  2851. return retval;
  2852. }
  2853. /*****************************************************************************/
  2854. /*
  2855. * Try to find an ONboard, Brumby or Stallion board and initialize it.
  2856. * This handles only these board types.
  2857. */
  2858. static int stli_initonb(struct stlibrd *brdp)
  2859. {
  2860. cdkonbsig_t sig;
  2861. cdkonbsig_t __iomem *sigsp;
  2862. char *name;
  2863. int i, retval;
  2864. /*
  2865. * Do a basic sanity check on the IO and memory addresses.
  2866. */
  2867. if (brdp->iobase == 0 || brdp->memaddr == 0) {
  2868. retval = -ENODEV;
  2869. goto err;
  2870. }
  2871. brdp->iosize = ONB_IOSIZE;
  2872. if (!request_region(brdp->iobase, brdp->iosize, "istallion")) {
  2873. retval = -EIO;
  2874. goto err;
  2875. }
  2876. /*
  2877. * Based on the specific board type setup the common vars to access
  2878. * and enable shared memory. Set all board specific information now
  2879. * as well.
  2880. */
  2881. switch (brdp->brdtype) {
  2882. case BRD_ONBOARD:
  2883. case BRD_ONBOARD2:
  2884. brdp->memsize = ONB_MEMSIZE;
  2885. brdp->pagesize = ONB_ATPAGESIZE;
  2886. brdp->init = stli_onbinit;
  2887. brdp->enable = stli_onbenable;
  2888. brdp->reenable = stli_onbenable;
  2889. brdp->disable = stli_onbdisable;
  2890. brdp->getmemptr = stli_onbgetmemptr;
  2891. brdp->intr = stli_ecpintr;
  2892. brdp->reset = stli_onbreset;
  2893. if (brdp->memaddr > 0x100000)
  2894. brdp->enabval = ONB_MEMENABHI;
  2895. else
  2896. brdp->enabval = ONB_MEMENABLO;
  2897. name = "serial(ONBoard)";
  2898. break;
  2899. case BRD_ONBOARDE:
  2900. brdp->memsize = ONB_EIMEMSIZE;
  2901. brdp->pagesize = ONB_EIPAGESIZE;
  2902. brdp->init = stli_onbeinit;
  2903. brdp->enable = stli_onbeenable;
  2904. brdp->reenable = stli_onbeenable;
  2905. brdp->disable = stli_onbedisable;
  2906. brdp->getmemptr = stli_onbegetmemptr;
  2907. brdp->intr = stli_ecpintr;
  2908. brdp->reset = stli_onbereset;
  2909. name = "serial(ONBoard/E)";
  2910. break;
  2911. case BRD_BRUMBY4:
  2912. brdp->memsize = BBY_MEMSIZE;
  2913. brdp->pagesize = BBY_PAGESIZE;
  2914. brdp->init = stli_bbyinit;
  2915. brdp->enable = NULL;
  2916. brdp->reenable = NULL;
  2917. brdp->disable = NULL;
  2918. brdp->getmemptr = stli_bbygetmemptr;
  2919. brdp->intr = stli_ecpintr;
  2920. brdp->reset = stli_bbyreset;
  2921. name = "serial(Brumby)";
  2922. break;
  2923. case BRD_STALLION:
  2924. brdp->memsize = STAL_MEMSIZE;
  2925. brdp->pagesize = STAL_PAGESIZE;
  2926. brdp->init = stli_stalinit;
  2927. brdp->enable = NULL;
  2928. brdp->reenable = NULL;
  2929. brdp->disable = NULL;
  2930. brdp->getmemptr = stli_stalgetmemptr;
  2931. brdp->intr = stli_ecpintr;
  2932. brdp->reset = stli_stalreset;
  2933. name = "serial(Stallion)";
  2934. break;
  2935. default:
  2936. retval = -EINVAL;
  2937. goto err_reg;
  2938. }
  2939. /*
  2940. * The per-board operations structure is all set up, so now let's go
  2941. * and get the board operational. Firstly initialize board configuration
  2942. * registers. Set the memory mapping info so we can get at the boards
  2943. * shared memory.
  2944. */
  2945. EBRDINIT(brdp);
  2946. brdp->membase = ioremap(brdp->memaddr, brdp->memsize);
  2947. if (brdp->membase == NULL) {
  2948. retval = -ENOMEM;
  2949. goto err_reg;
  2950. }
  2951. /*
  2952. * Now that all specific code is set up, enable the shared memory and
  2953. * look for the a signature area that will tell us exactly what board
  2954. * this is, and how many ports.
  2955. */
  2956. EBRDENABLE(brdp);
  2957. sigsp = (cdkonbsig_t __iomem *) EBRDGETMEMPTR(brdp, CDK_SIGADDR);
  2958. memcpy_fromio(&sig, sigsp, sizeof(cdkonbsig_t));
  2959. EBRDDISABLE(brdp);
  2960. if (sig.magic0 != cpu_to_le16(ONB_MAGIC0) ||
  2961. sig.magic1 != cpu_to_le16(ONB_MAGIC1) ||
  2962. sig.magic2 != cpu_to_le16(ONB_MAGIC2) ||
  2963. sig.magic3 != cpu_to_le16(ONB_MAGIC3)) {
  2964. retval = -ENODEV;
  2965. goto err_unmap;
  2966. }
  2967. /*
  2968. * Scan through the signature alive mask and calculate how many ports
  2969. * there are on this board.
  2970. */
  2971. brdp->nrpanels = 1;
  2972. if (sig.amask1) {
  2973. brdp->nrports = 32;
  2974. } else {
  2975. for (i = 0; (i < 16); i++) {
  2976. if (((sig.amask0 << i) & 0x8000) == 0)
  2977. break;
  2978. }
  2979. brdp->nrports = i;
  2980. }
  2981. brdp->panels[0] = brdp->nrports;
  2982. brdp->state |= BST_FOUND;
  2983. return 0;
  2984. err_unmap:
  2985. iounmap(brdp->membase);
  2986. brdp->membase = NULL;
  2987. err_reg:
  2988. release_region(brdp->iobase, brdp->iosize);
  2989. err:
  2990. return retval;
  2991. }
  2992. /*****************************************************************************/
  2993. /*
  2994. * Start up a running board. This routine is only called after the
  2995. * code has been down loaded to the board and is operational. It will
  2996. * read in the memory map, and get the show on the road...
  2997. */
  2998. static int stli_startbrd(struct stlibrd *brdp)
  2999. {
  3000. cdkhdr_t __iomem *hdrp;
  3001. cdkmem_t __iomem *memp;
  3002. cdkasy_t __iomem *ap;
  3003. unsigned long flags;
  3004. unsigned int portnr, nrdevs, i;
  3005. struct stliport *portp;
  3006. int rc = 0;
  3007. u32 memoff;
  3008. spin_lock_irqsave(&brd_lock, flags);
  3009. EBRDENABLE(brdp);
  3010. hdrp = (cdkhdr_t __iomem *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
  3011. nrdevs = hdrp->nrdevs;
  3012. #if 0
  3013. printk("%s(%d): CDK version %d.%d.%d --> "
  3014. "nrdevs=%d memp=%x hostp=%x slavep=%x\n",
  3015. __FILE__, __LINE__, readb(&hdrp->ver_release), readb(&hdrp->ver_modification),
  3016. readb(&hdrp->ver_fix), nrdevs, (int) readl(&hdrp->memp), readl(&hdrp->hostp),
  3017. readl(&hdrp->slavep));
  3018. #endif
  3019. if (nrdevs < (brdp->nrports + 1)) {
  3020. printk(KERN_ERR "STALLION: slave failed to allocate memory for "
  3021. "all devices, devices=%d\n", nrdevs);
  3022. brdp->nrports = nrdevs - 1;
  3023. }
  3024. brdp->nrdevs = nrdevs;
  3025. brdp->hostoffset = hdrp->hostp - CDK_CDKADDR;
  3026. brdp->slaveoffset = hdrp->slavep - CDK_CDKADDR;
  3027. brdp->bitsize = (nrdevs + 7) / 8;
  3028. memoff = readl(&hdrp->memp);
  3029. if (memoff > brdp->memsize) {
  3030. printk(KERN_ERR "STALLION: corrupted shared memory region?\n");
  3031. rc = -EIO;
  3032. goto stli_donestartup;
  3033. }
  3034. memp = (cdkmem_t __iomem *) EBRDGETMEMPTR(brdp, memoff);
  3035. if (readw(&memp->dtype) != TYP_ASYNCTRL) {
  3036. printk(KERN_ERR "STALLION: no slave control device found\n");
  3037. goto stli_donestartup;
  3038. }
  3039. memp++;
  3040. /*
  3041. * Cycle through memory allocation of each port. We are guaranteed to
  3042. * have all ports inside the first page of slave window, so no need to
  3043. * change pages while reading memory map.
  3044. */
  3045. for (i = 1, portnr = 0; (i < nrdevs); i++, portnr++, memp++) {
  3046. if (readw(&memp->dtype) != TYP_ASYNC)
  3047. break;
  3048. portp = brdp->ports[portnr];
  3049. if (portp == NULL)
  3050. break;
  3051. portp->devnr = i;
  3052. portp->addr = readl(&memp->offset);
  3053. portp->reqbit = (unsigned char) (0x1 << (i * 8 / nrdevs));
  3054. portp->portidx = (unsigned char) (i / 8);
  3055. portp->portbit = (unsigned char) (0x1 << (i % 8));
  3056. }
  3057. writeb(0xff, &hdrp->slavereq);
  3058. /*
  3059. * For each port setup a local copy of the RX and TX buffer offsets
  3060. * and sizes. We do this separate from the above, because we need to
  3061. * move the shared memory page...
  3062. */
  3063. for (i = 1, portnr = 0; (i < nrdevs); i++, portnr++) {
  3064. portp = brdp->ports[portnr];
  3065. if (portp == NULL)
  3066. break;
  3067. if (portp->addr == 0)
  3068. break;
  3069. ap = (cdkasy_t __iomem *) EBRDGETMEMPTR(brdp, portp->addr);
  3070. if (ap != NULL) {
  3071. portp->rxsize = readw(&ap->rxq.size);
  3072. portp->txsize = readw(&ap->txq.size);
  3073. portp->rxoffset = readl(&ap->rxq.offset);
  3074. portp->txoffset = readl(&ap->txq.offset);
  3075. }
  3076. }
  3077. stli_donestartup:
  3078. EBRDDISABLE(brdp);
  3079. spin_unlock_irqrestore(&brd_lock, flags);
  3080. if (rc == 0)
  3081. brdp->state |= BST_STARTED;
  3082. if (! stli_timeron) {
  3083. stli_timeron++;
  3084. mod_timer(&stli_timerlist, STLI_TIMEOUT);
  3085. }
  3086. return rc;
  3087. }
  3088. /*****************************************************************************/
  3089. /*
  3090. * Probe and initialize the specified board.
  3091. */
  3092. static int __devinit stli_brdinit(struct stlibrd *brdp)
  3093. {
  3094. int retval;
  3095. switch (brdp->brdtype) {
  3096. case BRD_ECP:
  3097. case BRD_ECPE:
  3098. case BRD_ECPMC:
  3099. case BRD_ECPPCI:
  3100. retval = stli_initecp(brdp);
  3101. break;
  3102. case BRD_ONBOARD:
  3103. case BRD_ONBOARDE:
  3104. case BRD_ONBOARD2:
  3105. case BRD_BRUMBY4:
  3106. case BRD_STALLION:
  3107. retval = stli_initonb(brdp);
  3108. break;
  3109. default:
  3110. printk(KERN_ERR "STALLION: board=%d is unknown board "
  3111. "type=%d\n", brdp->brdnr, brdp->brdtype);
  3112. retval = -ENODEV;
  3113. }
  3114. if (retval)
  3115. return retval;
  3116. stli_initports(brdp);
  3117. printk(KERN_INFO "STALLION: %s found, board=%d io=%x mem=%x "
  3118. "nrpanels=%d nrports=%d\n", stli_brdnames[brdp->brdtype],
  3119. brdp->brdnr, brdp->iobase, (int) brdp->memaddr,
  3120. brdp->nrpanels, brdp->nrports);
  3121. return 0;
  3122. }
  3123. #if STLI_EISAPROBE != 0
  3124. /*****************************************************************************/
  3125. /*
  3126. * Probe around trying to find where the EISA boards shared memory
  3127. * might be. This is a bit if hack, but it is the best we can do.
  3128. */
  3129. static int stli_eisamemprobe(struct stlibrd *brdp)
  3130. {
  3131. cdkecpsig_t ecpsig, __iomem *ecpsigp;
  3132. cdkonbsig_t onbsig, __iomem *onbsigp;
  3133. int i, foundit;
  3134. /*
  3135. * First up we reset the board, to get it into a known state. There
  3136. * is only 2 board types here we need to worry about. Don;t use the
  3137. * standard board init routine here, it programs up the shared
  3138. * memory address, and we don't know it yet...
  3139. */
  3140. if (brdp->brdtype == BRD_ECPE) {
  3141. outb(0x1, (brdp->iobase + ECP_EIBRDENAB));
  3142. outb(ECP_EISTOP, (brdp->iobase + ECP_EICONFR));
  3143. udelay(10);
  3144. outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
  3145. udelay(500);
  3146. stli_ecpeienable(brdp);
  3147. } else if (brdp->brdtype == BRD_ONBOARDE) {
  3148. outb(0x1, (brdp->iobase + ONB_EIBRDENAB));
  3149. outb(ONB_EISTOP, (brdp->iobase + ONB_EICONFR));
  3150. udelay(10);
  3151. outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
  3152. mdelay(100);
  3153. outb(0x1, brdp->iobase);
  3154. mdelay(1);
  3155. stli_onbeenable(brdp);
  3156. } else {
  3157. return -ENODEV;
  3158. }
  3159. foundit = 0;
  3160. brdp->memsize = ECP_MEMSIZE;
  3161. /*
  3162. * Board shared memory is enabled, so now we have a poke around and
  3163. * see if we can find it.
  3164. */
  3165. for (i = 0; (i < stli_eisamempsize); i++) {
  3166. brdp->memaddr = stli_eisamemprobeaddrs[i];
  3167. brdp->membase = ioremap(brdp->memaddr, brdp->memsize);
  3168. if (brdp->membase == NULL)
  3169. continue;
  3170. if (brdp->brdtype == BRD_ECPE) {
  3171. ecpsigp = stli_ecpeigetmemptr(brdp,
  3172. CDK_SIGADDR, __LINE__);
  3173. memcpy_fromio(&ecpsig, ecpsigp, sizeof(cdkecpsig_t));
  3174. if (ecpsig.magic == cpu_to_le32(ECP_MAGIC))
  3175. foundit = 1;
  3176. } else {
  3177. onbsigp = (cdkonbsig_t __iomem *) stli_onbegetmemptr(brdp,
  3178. CDK_SIGADDR, __LINE__);
  3179. memcpy_fromio(&onbsig, onbsigp, sizeof(cdkonbsig_t));
  3180. if ((onbsig.magic0 == cpu_to_le16(ONB_MAGIC0)) &&
  3181. (onbsig.magic1 == cpu_to_le16(ONB_MAGIC1)) &&
  3182. (onbsig.magic2 == cpu_to_le16(ONB_MAGIC2)) &&
  3183. (onbsig.magic3 == cpu_to_le16(ONB_MAGIC3)))
  3184. foundit = 1;
  3185. }
  3186. iounmap(brdp->membase);
  3187. if (foundit)
  3188. break;
  3189. }
  3190. /*
  3191. * Regardless of whether we found the shared memory or not we must
  3192. * disable the region. After that return success or failure.
  3193. */
  3194. if (brdp->brdtype == BRD_ECPE)
  3195. stli_ecpeidisable(brdp);
  3196. else
  3197. stli_onbedisable(brdp);
  3198. if (! foundit) {
  3199. brdp->memaddr = 0;
  3200. brdp->membase = NULL;
  3201. printk(KERN_ERR "STALLION: failed to probe shared memory "
  3202. "region for %s in EISA slot=%d\n",
  3203. stli_brdnames[brdp->brdtype], (brdp->iobase >> 12));
  3204. return -ENODEV;
  3205. }
  3206. return 0;
  3207. }
  3208. #endif
  3209. static int stli_getbrdnr(void)
  3210. {
  3211. unsigned int i;
  3212. for (i = 0; i < STL_MAXBRDS; i++) {
  3213. if (!stli_brds[i]) {
  3214. if (i >= stli_nrbrds)
  3215. stli_nrbrds = i + 1;
  3216. return i;
  3217. }
  3218. }
  3219. return -1;
  3220. }
  3221. #if STLI_EISAPROBE != 0
  3222. /*****************************************************************************/
  3223. /*
  3224. * Probe around and try to find any EISA boards in system. The biggest
  3225. * problem here is finding out what memory address is associated with
  3226. * an EISA board after it is found. The registers of the ECPE and
  3227. * ONboardE are not readable - so we can't read them from there. We
  3228. * don't have access to the EISA CMOS (or EISA BIOS) so we don't
  3229. * actually have any way to find out the real value. The best we can
  3230. * do is go probing around in the usual places hoping we can find it.
  3231. */
  3232. static int stli_findeisabrds(void)
  3233. {
  3234. struct stlibrd *brdp;
  3235. unsigned int iobase, eid, i;
  3236. int brdnr, found = 0;
  3237. /*
  3238. * Firstly check if this is an EISA system. If this is not an EISA system then
  3239. * don't bother going any further!
  3240. */
  3241. if (EISA_bus)
  3242. return 0;
  3243. /*
  3244. * Looks like an EISA system, so go searching for EISA boards.
  3245. */
  3246. for (iobase = 0x1000; (iobase <= 0xc000); iobase += 0x1000) {
  3247. outb(0xff, (iobase + 0xc80));
  3248. eid = inb(iobase + 0xc80);
  3249. eid |= inb(iobase + 0xc81) << 8;
  3250. if (eid != STL_EISAID)
  3251. continue;
  3252. /*
  3253. * We have found a board. Need to check if this board was
  3254. * statically configured already (just in case!).
  3255. */
  3256. for (i = 0; (i < STL_MAXBRDS); i++) {
  3257. brdp = stli_brds[i];
  3258. if (brdp == NULL)
  3259. continue;
  3260. if (brdp->iobase == iobase)
  3261. break;
  3262. }
  3263. if (i < STL_MAXBRDS)
  3264. continue;
  3265. /*
  3266. * We have found a Stallion board and it is not configured already.
  3267. * Allocate a board structure and initialize it.
  3268. */
  3269. if ((brdp = stli_allocbrd()) == NULL)
  3270. return found ? : -ENOMEM;
  3271. brdnr = stli_getbrdnr();
  3272. if (brdnr < 0)
  3273. return found ? : -ENOMEM;
  3274. brdp->brdnr = (unsigned int)brdnr;
  3275. eid = inb(iobase + 0xc82);
  3276. if (eid == ECP_EISAID)
  3277. brdp->brdtype = BRD_ECPE;
  3278. else if (eid == ONB_EISAID)
  3279. brdp->brdtype = BRD_ONBOARDE;
  3280. else
  3281. brdp->brdtype = BRD_UNKNOWN;
  3282. brdp->iobase = iobase;
  3283. outb(0x1, (iobase + 0xc84));
  3284. if (stli_eisamemprobe(brdp))
  3285. outb(0, (iobase + 0xc84));
  3286. if (stli_brdinit(brdp) < 0) {
  3287. kfree(brdp);
  3288. continue;
  3289. }
  3290. stli_brds[brdp->brdnr] = brdp;
  3291. found++;
  3292. for (i = 0; i < brdp->nrports; i++)
  3293. tty_register_device(stli_serial,
  3294. brdp->brdnr * STL_MAXPORTS + i, NULL);
  3295. }
  3296. return found;
  3297. }
  3298. #else
  3299. static inline int stli_findeisabrds(void) { return 0; }
  3300. #endif
  3301. /*****************************************************************************/
  3302. /*
  3303. * Find the next available board number that is free.
  3304. */
  3305. /*****************************************************************************/
  3306. /*
  3307. * We have a Stallion board. Allocate a board structure and
  3308. * initialize it. Read its IO and MEMORY resources from PCI
  3309. * configuration space.
  3310. */
  3311. static int __devinit stli_pciprobe(struct pci_dev *pdev,
  3312. const struct pci_device_id *ent)
  3313. {
  3314. struct stlibrd *brdp;
  3315. unsigned int i;
  3316. int brdnr, retval = -EIO;
  3317. retval = pci_enable_device(pdev);
  3318. if (retval)
  3319. goto err;
  3320. brdp = stli_allocbrd();
  3321. if (brdp == NULL) {
  3322. retval = -ENOMEM;
  3323. goto err;
  3324. }
  3325. mutex_lock(&stli_brdslock);
  3326. brdnr = stli_getbrdnr();
  3327. if (brdnr < 0) {
  3328. printk(KERN_INFO "STALLION: too many boards found, "
  3329. "maximum supported %d\n", STL_MAXBRDS);
  3330. mutex_unlock(&stli_brdslock);
  3331. retval = -EIO;
  3332. goto err_fr;
  3333. }
  3334. brdp->brdnr = (unsigned int)brdnr;
  3335. stli_brds[brdp->brdnr] = brdp;
  3336. mutex_unlock(&stli_brdslock);
  3337. brdp->brdtype = BRD_ECPPCI;
  3338. /*
  3339. * We have all resources from the board, so lets setup the actual
  3340. * board structure now.
  3341. */
  3342. brdp->iobase = pci_resource_start(pdev, 3);
  3343. brdp->memaddr = pci_resource_start(pdev, 2);
  3344. retval = stli_brdinit(brdp);
  3345. if (retval)
  3346. goto err_null;
  3347. brdp->state |= BST_PROBED;
  3348. pci_set_drvdata(pdev, brdp);
  3349. EBRDENABLE(brdp);
  3350. brdp->enable = NULL;
  3351. brdp->disable = NULL;
  3352. for (i = 0; i < brdp->nrports; i++)
  3353. tty_register_device(stli_serial, brdp->brdnr * STL_MAXPORTS + i,
  3354. &pdev->dev);
  3355. return 0;
  3356. err_null:
  3357. stli_brds[brdp->brdnr] = NULL;
  3358. err_fr:
  3359. kfree(brdp);
  3360. err:
  3361. return retval;
  3362. }
  3363. static void stli_pciremove(struct pci_dev *pdev)
  3364. {
  3365. struct stlibrd *brdp = pci_get_drvdata(pdev);
  3366. stli_cleanup_ports(brdp);
  3367. iounmap(brdp->membase);
  3368. if (brdp->iosize > 0)
  3369. release_region(brdp->iobase, brdp->iosize);
  3370. stli_brds[brdp->brdnr] = NULL;
  3371. kfree(brdp);
  3372. }
  3373. static struct pci_driver stli_pcidriver = {
  3374. .name = "istallion",
  3375. .id_table = istallion_pci_tbl,
  3376. .probe = stli_pciprobe,
  3377. .remove = __devexit_p(stli_pciremove)
  3378. };
  3379. /*****************************************************************************/
  3380. /*
  3381. * Allocate a new board structure. Fill out the basic info in it.
  3382. */
  3383. static struct stlibrd *stli_allocbrd(void)
  3384. {
  3385. struct stlibrd *brdp;
  3386. brdp = kzalloc(sizeof(struct stlibrd), GFP_KERNEL);
  3387. if (!brdp) {
  3388. printk(KERN_ERR "STALLION: failed to allocate memory "
  3389. "(size=%Zd)\n", sizeof(struct stlibrd));
  3390. return NULL;
  3391. }
  3392. brdp->magic = STLI_BOARDMAGIC;
  3393. return brdp;
  3394. }
  3395. /*****************************************************************************/
  3396. /*
  3397. * Scan through all the boards in the configuration and see what we
  3398. * can find.
  3399. */
  3400. static int stli_initbrds(void)
  3401. {
  3402. struct stlibrd *brdp, *nxtbrdp;
  3403. struct stlconf conf;
  3404. unsigned int i, j, found = 0;
  3405. int retval;
  3406. for (stli_nrbrds = 0; stli_nrbrds < ARRAY_SIZE(stli_brdsp);
  3407. stli_nrbrds++) {
  3408. memset(&conf, 0, sizeof(conf));
  3409. if (stli_parsebrd(&conf, stli_brdsp[stli_nrbrds]) == 0)
  3410. continue;
  3411. if ((brdp = stli_allocbrd()) == NULL)
  3412. continue;
  3413. brdp->brdnr = stli_nrbrds;
  3414. brdp->brdtype = conf.brdtype;
  3415. brdp->iobase = conf.ioaddr1;
  3416. brdp->memaddr = conf.memaddr;
  3417. if (stli_brdinit(brdp) < 0) {
  3418. kfree(brdp);
  3419. continue;
  3420. }
  3421. stli_brds[brdp->brdnr] = brdp;
  3422. found++;
  3423. for (i = 0; i < brdp->nrports; i++)
  3424. tty_register_device(stli_serial,
  3425. brdp->brdnr * STL_MAXPORTS + i, NULL);
  3426. }
  3427. retval = stli_findeisabrds();
  3428. if (retval > 0)
  3429. found += retval;
  3430. /*
  3431. * All found boards are initialized. Now for a little optimization, if
  3432. * no boards are sharing the "shared memory" regions then we can just
  3433. * leave them all enabled. This is in fact the usual case.
  3434. */
  3435. stli_shared = 0;
  3436. if (stli_nrbrds > 1) {
  3437. for (i = 0; (i < stli_nrbrds); i++) {
  3438. brdp = stli_brds[i];
  3439. if (brdp == NULL)
  3440. continue;
  3441. for (j = i + 1; (j < stli_nrbrds); j++) {
  3442. nxtbrdp = stli_brds[j];
  3443. if (nxtbrdp == NULL)
  3444. continue;
  3445. if ((brdp->membase >= nxtbrdp->membase) &&
  3446. (brdp->membase <= (nxtbrdp->membase +
  3447. nxtbrdp->memsize - 1))) {
  3448. stli_shared++;
  3449. break;
  3450. }
  3451. }
  3452. }
  3453. }
  3454. if (stli_shared == 0) {
  3455. for (i = 0; (i < stli_nrbrds); i++) {
  3456. brdp = stli_brds[i];
  3457. if (brdp == NULL)
  3458. continue;
  3459. if (brdp->state & BST_FOUND) {
  3460. EBRDENABLE(brdp);
  3461. brdp->enable = NULL;
  3462. brdp->disable = NULL;
  3463. }
  3464. }
  3465. }
  3466. retval = pci_register_driver(&stli_pcidriver);
  3467. if (retval && found == 0) {
  3468. printk(KERN_ERR "Neither isa nor eisa cards found nor pci "
  3469. "driver can be registered!\n");
  3470. goto err;
  3471. }
  3472. return 0;
  3473. err:
  3474. return retval;
  3475. }
  3476. /*****************************************************************************/
  3477. /*
  3478. * Code to handle an "staliomem" read operation. This device is the
  3479. * contents of the board shared memory. It is used for down loading
  3480. * the slave image (and debugging :-)
  3481. */
  3482. static ssize_t stli_memread(struct file *fp, char __user *buf, size_t count, loff_t *offp)
  3483. {
  3484. unsigned long flags;
  3485. void __iomem *memptr;
  3486. struct stlibrd *brdp;
  3487. unsigned int brdnr;
  3488. int size, n;
  3489. void *p;
  3490. loff_t off = *offp;
  3491. brdnr = iminor(fp->f_path.dentry->d_inode);
  3492. if (brdnr >= stli_nrbrds)
  3493. return -ENODEV;
  3494. brdp = stli_brds[brdnr];
  3495. if (brdp == NULL)
  3496. return -ENODEV;
  3497. if (brdp->state == 0)
  3498. return -ENODEV;
  3499. if (off >= brdp->memsize || off + count < off)
  3500. return 0;
  3501. size = min(count, (size_t)(brdp->memsize - off));
  3502. /*
  3503. * Copy the data a page at a time
  3504. */
  3505. p = (void *)__get_free_page(GFP_KERNEL);
  3506. if(p == NULL)
  3507. return -ENOMEM;
  3508. while (size > 0) {
  3509. spin_lock_irqsave(&brd_lock, flags);
  3510. EBRDENABLE(brdp);
  3511. memptr = EBRDGETMEMPTR(brdp, off);
  3512. n = min(size, (int)(brdp->pagesize - (((unsigned long) off) % brdp->pagesize)));
  3513. n = min(n, (int)PAGE_SIZE);
  3514. memcpy_fromio(p, memptr, n);
  3515. EBRDDISABLE(brdp);
  3516. spin_unlock_irqrestore(&brd_lock, flags);
  3517. if (copy_to_user(buf, p, n)) {
  3518. count = -EFAULT;
  3519. goto out;
  3520. }
  3521. off += n;
  3522. buf += n;
  3523. size -= n;
  3524. }
  3525. out:
  3526. *offp = off;
  3527. free_page((unsigned long)p);
  3528. return count;
  3529. }
  3530. /*****************************************************************************/
  3531. /*
  3532. * Code to handle an "staliomem" write operation. This device is the
  3533. * contents of the board shared memory. It is used for down loading
  3534. * the slave image (and debugging :-)
  3535. *
  3536. * FIXME: copy under lock
  3537. */
  3538. static ssize_t stli_memwrite(struct file *fp, const char __user *buf, size_t count, loff_t *offp)
  3539. {
  3540. unsigned long flags;
  3541. void __iomem *memptr;
  3542. struct stlibrd *brdp;
  3543. char __user *chbuf;
  3544. unsigned int brdnr;
  3545. int size, n;
  3546. void *p;
  3547. loff_t off = *offp;
  3548. brdnr = iminor(fp->f_path.dentry->d_inode);
  3549. if (brdnr >= stli_nrbrds)
  3550. return -ENODEV;
  3551. brdp = stli_brds[brdnr];
  3552. if (brdp == NULL)
  3553. return -ENODEV;
  3554. if (brdp->state == 0)
  3555. return -ENODEV;
  3556. if (off >= brdp->memsize || off + count < off)
  3557. return 0;
  3558. chbuf = (char __user *) buf;
  3559. size = min(count, (size_t)(brdp->memsize - off));
  3560. /*
  3561. * Copy the data a page at a time
  3562. */
  3563. p = (void *)__get_free_page(GFP_KERNEL);
  3564. if(p == NULL)
  3565. return -ENOMEM;
  3566. while (size > 0) {
  3567. n = min(size, (int)(brdp->pagesize - (((unsigned long) off) % brdp->pagesize)));
  3568. n = min(n, (int)PAGE_SIZE);
  3569. if (copy_from_user(p, chbuf, n)) {
  3570. if (count == 0)
  3571. count = -EFAULT;
  3572. goto out;
  3573. }
  3574. spin_lock_irqsave(&brd_lock, flags);
  3575. EBRDENABLE(brdp);
  3576. memptr = EBRDGETMEMPTR(brdp, off);
  3577. memcpy_toio(memptr, p, n);
  3578. EBRDDISABLE(brdp);
  3579. spin_unlock_irqrestore(&brd_lock, flags);
  3580. off += n;
  3581. chbuf += n;
  3582. size -= n;
  3583. }
  3584. out:
  3585. free_page((unsigned long) p);
  3586. *offp = off;
  3587. return count;
  3588. }
  3589. /*****************************************************************************/
  3590. /*
  3591. * Return the board stats structure to user app.
  3592. */
  3593. static int stli_getbrdstats(combrd_t __user *bp)
  3594. {
  3595. struct stlibrd *brdp;
  3596. unsigned int i;
  3597. if (copy_from_user(&stli_brdstats, bp, sizeof(combrd_t)))
  3598. return -EFAULT;
  3599. if (stli_brdstats.brd >= STL_MAXBRDS)
  3600. return -ENODEV;
  3601. brdp = stli_brds[stli_brdstats.brd];
  3602. if (brdp == NULL)
  3603. return -ENODEV;
  3604. memset(&stli_brdstats, 0, sizeof(combrd_t));
  3605. stli_brdstats.brd = brdp->brdnr;
  3606. stli_brdstats.type = brdp->brdtype;
  3607. stli_brdstats.hwid = 0;
  3608. stli_brdstats.state = brdp->state;
  3609. stli_brdstats.ioaddr = brdp->iobase;
  3610. stli_brdstats.memaddr = brdp->memaddr;
  3611. stli_brdstats.nrpanels = brdp->nrpanels;
  3612. stli_brdstats.nrports = brdp->nrports;
  3613. for (i = 0; (i < brdp->nrpanels); i++) {
  3614. stli_brdstats.panels[i].panel = i;
  3615. stli_brdstats.panels[i].hwid = brdp->panelids[i];
  3616. stli_brdstats.panels[i].nrports = brdp->panels[i];
  3617. }
  3618. if (copy_to_user(bp, &stli_brdstats, sizeof(combrd_t)))
  3619. return -EFAULT;
  3620. return 0;
  3621. }
  3622. /*****************************************************************************/
  3623. /*
  3624. * Resolve the referenced port number into a port struct pointer.
  3625. */
  3626. static struct stliport *stli_getport(unsigned int brdnr, unsigned int panelnr,
  3627. unsigned int portnr)
  3628. {
  3629. struct stlibrd *brdp;
  3630. unsigned int i;
  3631. if (brdnr >= STL_MAXBRDS)
  3632. return NULL;
  3633. brdp = stli_brds[brdnr];
  3634. if (brdp == NULL)
  3635. return NULL;
  3636. for (i = 0; (i < panelnr); i++)
  3637. portnr += brdp->panels[i];
  3638. if (portnr >= brdp->nrports)
  3639. return NULL;
  3640. return brdp->ports[portnr];
  3641. }
  3642. /*****************************************************************************/
  3643. /*
  3644. * Return the port stats structure to user app. A NULL port struct
  3645. * pointer passed in means that we need to find out from the app
  3646. * what port to get stats for (used through board control device).
  3647. */
  3648. static int stli_portcmdstats(struct stliport *portp)
  3649. {
  3650. unsigned long flags;
  3651. struct stlibrd *brdp;
  3652. int rc;
  3653. memset(&stli_comstats, 0, sizeof(comstats_t));
  3654. if (portp == NULL)
  3655. return -ENODEV;
  3656. brdp = stli_brds[portp->brdnr];
  3657. if (brdp == NULL)
  3658. return -ENODEV;
  3659. if (brdp->state & BST_STARTED) {
  3660. if ((rc = stli_cmdwait(brdp, portp, A_GETSTATS,
  3661. &stli_cdkstats, sizeof(asystats_t), 1)) < 0)
  3662. return rc;
  3663. } else {
  3664. memset(&stli_cdkstats, 0, sizeof(asystats_t));
  3665. }
  3666. stli_comstats.brd = portp->brdnr;
  3667. stli_comstats.panel = portp->panelnr;
  3668. stli_comstats.port = portp->portnr;
  3669. stli_comstats.state = portp->state;
  3670. stli_comstats.flags = portp->flags;
  3671. spin_lock_irqsave(&brd_lock, flags);
  3672. if (portp->tty != NULL) {
  3673. if (portp->tty->driver_data == portp) {
  3674. stli_comstats.ttystate = portp->tty->flags;
  3675. stli_comstats.rxbuffered = -1;
  3676. if (portp->tty->termios != NULL) {
  3677. stli_comstats.cflags = portp->tty->termios->c_cflag;
  3678. stli_comstats.iflags = portp->tty->termios->c_iflag;
  3679. stli_comstats.oflags = portp->tty->termios->c_oflag;
  3680. stli_comstats.lflags = portp->tty->termios->c_lflag;
  3681. }
  3682. }
  3683. }
  3684. spin_unlock_irqrestore(&brd_lock, flags);
  3685. stli_comstats.txtotal = stli_cdkstats.txchars;
  3686. stli_comstats.rxtotal = stli_cdkstats.rxchars + stli_cdkstats.ringover;
  3687. stli_comstats.txbuffered = stli_cdkstats.txringq;
  3688. stli_comstats.rxbuffered += stli_cdkstats.rxringq;
  3689. stli_comstats.rxoverrun = stli_cdkstats.overruns;
  3690. stli_comstats.rxparity = stli_cdkstats.parity;
  3691. stli_comstats.rxframing = stli_cdkstats.framing;
  3692. stli_comstats.rxlost = stli_cdkstats.ringover;
  3693. stli_comstats.rxbreaks = stli_cdkstats.rxbreaks;
  3694. stli_comstats.txbreaks = stli_cdkstats.txbreaks;
  3695. stli_comstats.txxon = stli_cdkstats.txstart;
  3696. stli_comstats.txxoff = stli_cdkstats.txstop;
  3697. stli_comstats.rxxon = stli_cdkstats.rxstart;
  3698. stli_comstats.rxxoff = stli_cdkstats.rxstop;
  3699. stli_comstats.rxrtsoff = stli_cdkstats.rtscnt / 2;
  3700. stli_comstats.rxrtson = stli_cdkstats.rtscnt - stli_comstats.rxrtsoff;
  3701. stli_comstats.modem = stli_cdkstats.dcdcnt;
  3702. stli_comstats.hwid = stli_cdkstats.hwid;
  3703. stli_comstats.signals = stli_mktiocm(stli_cdkstats.signals);
  3704. return 0;
  3705. }
  3706. /*****************************************************************************/
  3707. /*
  3708. * Return the port stats structure to user app. A NULL port struct
  3709. * pointer passed in means that we need to find out from the app
  3710. * what port to get stats for (used through board control device).
  3711. */
  3712. static int stli_getportstats(struct stliport *portp, comstats_t __user *cp)
  3713. {
  3714. struct stlibrd *brdp;
  3715. int rc;
  3716. if (!portp) {
  3717. if (copy_from_user(&stli_comstats, cp, sizeof(comstats_t)))
  3718. return -EFAULT;
  3719. portp = stli_getport(stli_comstats.brd, stli_comstats.panel,
  3720. stli_comstats.port);
  3721. if (!portp)
  3722. return -ENODEV;
  3723. }
  3724. brdp = stli_brds[portp->brdnr];
  3725. if (!brdp)
  3726. return -ENODEV;
  3727. if ((rc = stli_portcmdstats(portp)) < 0)
  3728. return rc;
  3729. return copy_to_user(cp, &stli_comstats, sizeof(comstats_t)) ?
  3730. -EFAULT : 0;
  3731. }
  3732. /*****************************************************************************/
  3733. /*
  3734. * Clear the port stats structure. We also return it zeroed out...
  3735. */
  3736. static int stli_clrportstats(struct stliport *portp, comstats_t __user *cp)
  3737. {
  3738. struct stlibrd *brdp;
  3739. int rc;
  3740. if (!portp) {
  3741. if (copy_from_user(&stli_comstats, cp, sizeof(comstats_t)))
  3742. return -EFAULT;
  3743. portp = stli_getport(stli_comstats.brd, stli_comstats.panel,
  3744. stli_comstats.port);
  3745. if (!portp)
  3746. return -ENODEV;
  3747. }
  3748. brdp = stli_brds[portp->brdnr];
  3749. if (!brdp)
  3750. return -ENODEV;
  3751. if (brdp->state & BST_STARTED) {
  3752. if ((rc = stli_cmdwait(brdp, portp, A_CLEARSTATS, NULL, 0, 0)) < 0)
  3753. return rc;
  3754. }
  3755. memset(&stli_comstats, 0, sizeof(comstats_t));
  3756. stli_comstats.brd = portp->brdnr;
  3757. stli_comstats.panel = portp->panelnr;
  3758. stli_comstats.port = portp->portnr;
  3759. if (copy_to_user(cp, &stli_comstats, sizeof(comstats_t)))
  3760. return -EFAULT;
  3761. return 0;
  3762. }
  3763. /*****************************************************************************/
  3764. /*
  3765. * Return the entire driver ports structure to a user app.
  3766. */
  3767. static int stli_getportstruct(struct stliport __user *arg)
  3768. {
  3769. struct stliport stli_dummyport;
  3770. struct stliport *portp;
  3771. if (copy_from_user(&stli_dummyport, arg, sizeof(struct stliport)))
  3772. return -EFAULT;
  3773. portp = stli_getport(stli_dummyport.brdnr, stli_dummyport.panelnr,
  3774. stli_dummyport.portnr);
  3775. if (!portp)
  3776. return -ENODEV;
  3777. if (copy_to_user(arg, portp, sizeof(struct stliport)))
  3778. return -EFAULT;
  3779. return 0;
  3780. }
  3781. /*****************************************************************************/
  3782. /*
  3783. * Return the entire driver board structure to a user app.
  3784. */
  3785. static int stli_getbrdstruct(struct stlibrd __user *arg)
  3786. {
  3787. struct stlibrd stli_dummybrd;
  3788. struct stlibrd *brdp;
  3789. if (copy_from_user(&stli_dummybrd, arg, sizeof(struct stlibrd)))
  3790. return -EFAULT;
  3791. if (stli_dummybrd.brdnr >= STL_MAXBRDS)
  3792. return -ENODEV;
  3793. brdp = stli_brds[stli_dummybrd.brdnr];
  3794. if (!brdp)
  3795. return -ENODEV;
  3796. if (copy_to_user(arg, brdp, sizeof(struct stlibrd)))
  3797. return -EFAULT;
  3798. return 0;
  3799. }
  3800. /*****************************************************************************/
  3801. /*
  3802. * The "staliomem" device is also required to do some special operations on
  3803. * the board. We need to be able to send an interrupt to the board,
  3804. * reset it, and start/stop it.
  3805. */
  3806. static int stli_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg)
  3807. {
  3808. struct stlibrd *brdp;
  3809. int brdnr, rc, done;
  3810. void __user *argp = (void __user *)arg;
  3811. /*
  3812. * First up handle the board independent ioctls.
  3813. */
  3814. done = 0;
  3815. rc = 0;
  3816. switch (cmd) {
  3817. case COM_GETPORTSTATS:
  3818. rc = stli_getportstats(NULL, argp);
  3819. done++;
  3820. break;
  3821. case COM_CLRPORTSTATS:
  3822. rc = stli_clrportstats(NULL, argp);
  3823. done++;
  3824. break;
  3825. case COM_GETBRDSTATS:
  3826. rc = stli_getbrdstats(argp);
  3827. done++;
  3828. break;
  3829. case COM_READPORT:
  3830. rc = stli_getportstruct(argp);
  3831. done++;
  3832. break;
  3833. case COM_READBOARD:
  3834. rc = stli_getbrdstruct(argp);
  3835. done++;
  3836. break;
  3837. }
  3838. if (done)
  3839. return rc;
  3840. /*
  3841. * Now handle the board specific ioctls. These all depend on the
  3842. * minor number of the device they were called from.
  3843. */
  3844. brdnr = iminor(ip);
  3845. if (brdnr >= STL_MAXBRDS)
  3846. return -ENODEV;
  3847. brdp = stli_brds[brdnr];
  3848. if (!brdp)
  3849. return -ENODEV;
  3850. if (brdp->state == 0)
  3851. return -ENODEV;
  3852. switch (cmd) {
  3853. case STL_BINTR:
  3854. EBRDINTR(brdp);
  3855. break;
  3856. case STL_BSTART:
  3857. rc = stli_startbrd(brdp);
  3858. break;
  3859. case STL_BSTOP:
  3860. brdp->state &= ~BST_STARTED;
  3861. break;
  3862. case STL_BRESET:
  3863. brdp->state &= ~BST_STARTED;
  3864. EBRDRESET(brdp);
  3865. if (stli_shared == 0) {
  3866. if (brdp->reenable != NULL)
  3867. (* brdp->reenable)(brdp);
  3868. }
  3869. break;
  3870. default:
  3871. rc = -ENOIOCTLCMD;
  3872. break;
  3873. }
  3874. return rc;
  3875. }
  3876. static const struct tty_operations stli_ops = {
  3877. .open = stli_open,
  3878. .close = stli_close,
  3879. .write = stli_write,
  3880. .put_char = stli_putchar,
  3881. .flush_chars = stli_flushchars,
  3882. .write_room = stli_writeroom,
  3883. .chars_in_buffer = stli_charsinbuffer,
  3884. .ioctl = stli_ioctl,
  3885. .set_termios = stli_settermios,
  3886. .throttle = stli_throttle,
  3887. .unthrottle = stli_unthrottle,
  3888. .stop = stli_stop,
  3889. .start = stli_start,
  3890. .hangup = stli_hangup,
  3891. .flush_buffer = stli_flushbuffer,
  3892. .break_ctl = stli_breakctl,
  3893. .wait_until_sent = stli_waituntilsent,
  3894. .send_xchar = stli_sendxchar,
  3895. .read_proc = stli_readproc,
  3896. .tiocmget = stli_tiocmget,
  3897. .tiocmset = stli_tiocmset,
  3898. };
  3899. /*****************************************************************************/
  3900. /*
  3901. * Loadable module initialization stuff.
  3902. */
  3903. static void istallion_cleanup_isa(void)
  3904. {
  3905. struct stlibrd *brdp;
  3906. unsigned int j;
  3907. for (j = 0; (j < stli_nrbrds); j++) {
  3908. if ((brdp = stli_brds[j]) == NULL || (brdp->state & BST_PROBED))
  3909. continue;
  3910. stli_cleanup_ports(brdp);
  3911. iounmap(brdp->membase);
  3912. if (brdp->iosize > 0)
  3913. release_region(brdp->iobase, brdp->iosize);
  3914. kfree(brdp);
  3915. stli_brds[j] = NULL;
  3916. }
  3917. }
  3918. static int __init istallion_module_init(void)
  3919. {
  3920. unsigned int i;
  3921. int retval;
  3922. printk(KERN_INFO "%s: version %s\n", stli_drvtitle, stli_drvversion);
  3923. spin_lock_init(&stli_lock);
  3924. spin_lock_init(&brd_lock);
  3925. stli_txcookbuf = kmalloc(STLI_TXBUFSIZE, GFP_KERNEL);
  3926. if (!stli_txcookbuf) {
  3927. printk(KERN_ERR "STALLION: failed to allocate memory "
  3928. "(size=%d)\n", STLI_TXBUFSIZE);
  3929. retval = -ENOMEM;
  3930. goto err;
  3931. }
  3932. stli_serial = alloc_tty_driver(STL_MAXBRDS * STL_MAXPORTS);
  3933. if (!stli_serial) {
  3934. retval = -ENOMEM;
  3935. goto err_free;
  3936. }
  3937. stli_serial->owner = THIS_MODULE;
  3938. stli_serial->driver_name = stli_drvname;
  3939. stli_serial->name = stli_serialname;
  3940. stli_serial->major = STL_SERIALMAJOR;
  3941. stli_serial->minor_start = 0;
  3942. stli_serial->type = TTY_DRIVER_TYPE_SERIAL;
  3943. stli_serial->subtype = SERIAL_TYPE_NORMAL;
  3944. stli_serial->init_termios = stli_deftermios;
  3945. stli_serial->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  3946. tty_set_operations(stli_serial, &stli_ops);
  3947. retval = tty_register_driver(stli_serial);
  3948. if (retval) {
  3949. printk(KERN_ERR "STALLION: failed to register serial driver\n");
  3950. goto err_ttyput;
  3951. }
  3952. retval = stli_initbrds();
  3953. if (retval)
  3954. goto err_ttyunr;
  3955. /*
  3956. * Set up a character driver for the shared memory region. We need this
  3957. * to down load the slave code image. Also it is a useful debugging tool.
  3958. */
  3959. retval = register_chrdev(STL_SIOMEMMAJOR, "staliomem", &stli_fsiomem);
  3960. if (retval) {
  3961. printk(KERN_ERR "STALLION: failed to register serial memory "
  3962. "device\n");
  3963. goto err_deinit;
  3964. }
  3965. istallion_class = class_create(THIS_MODULE, "staliomem");
  3966. for (i = 0; i < 4; i++)
  3967. device_create(istallion_class, NULL, MKDEV(STL_SIOMEMMAJOR, i),
  3968. "staliomem%d", i);
  3969. return 0;
  3970. err_deinit:
  3971. pci_unregister_driver(&stli_pcidriver);
  3972. istallion_cleanup_isa();
  3973. err_ttyunr:
  3974. tty_unregister_driver(stli_serial);
  3975. err_ttyput:
  3976. put_tty_driver(stli_serial);
  3977. err_free:
  3978. kfree(stli_txcookbuf);
  3979. err:
  3980. return retval;
  3981. }
  3982. /*****************************************************************************/
  3983. static void __exit istallion_module_exit(void)
  3984. {
  3985. unsigned int j;
  3986. printk(KERN_INFO "Unloading %s: version %s\n", stli_drvtitle,
  3987. stli_drvversion);
  3988. if (stli_timeron) {
  3989. stli_timeron = 0;
  3990. del_timer_sync(&stli_timerlist);
  3991. }
  3992. unregister_chrdev(STL_SIOMEMMAJOR, "staliomem");
  3993. for (j = 0; j < 4; j++)
  3994. device_destroy(istallion_class, MKDEV(STL_SIOMEMMAJOR, j));
  3995. class_destroy(istallion_class);
  3996. pci_unregister_driver(&stli_pcidriver);
  3997. istallion_cleanup_isa();
  3998. tty_unregister_driver(stli_serial);
  3999. put_tty_driver(stli_serial);
  4000. kfree(stli_txcookbuf);
  4001. }
  4002. module_init(istallion_module_init);
  4003. module_exit(istallion_module_exit);