istallion.c 122 KB

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