istallion.c 123 KB

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