istallion.c 123 KB

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