istallion.c 123 KB

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