synclinkmp.c 148 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651
  1. /*
  2. * $Id: synclinkmp.c,v 4.38 2005/07/15 13:29:44 paulkf Exp $
  3. *
  4. * Device driver for Microgate SyncLink Multiport
  5. * high speed multiprotocol serial adapter.
  6. *
  7. * written by Paul Fulghum for Microgate Corporation
  8. * paulkf@microgate.com
  9. *
  10. * Microgate and SyncLink are trademarks of Microgate Corporation
  11. *
  12. * Derived from serial.c written by Theodore Ts'o and Linus Torvalds
  13. * This code is released under the GNU General Public License (GPL)
  14. *
  15. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  16. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  19. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  25. * OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #define VERSION(ver,rel,seq) (((ver)<<16) | ((rel)<<8) | (seq))
  28. #if defined(__i386__)
  29. # define BREAKPOINT() asm(" int $3");
  30. #else
  31. # define BREAKPOINT() { }
  32. #endif
  33. #define MAX_DEVICES 12
  34. #include <linux/module.h>
  35. #include <linux/errno.h>
  36. #include <linux/signal.h>
  37. #include <linux/sched.h>
  38. #include <linux/timer.h>
  39. #include <linux/interrupt.h>
  40. #include <linux/pci.h>
  41. #include <linux/tty.h>
  42. #include <linux/tty_flip.h>
  43. #include <linux/serial.h>
  44. #include <linux/major.h>
  45. #include <linux/string.h>
  46. #include <linux/fcntl.h>
  47. #include <linux/ptrace.h>
  48. #include <linux/ioport.h>
  49. #include <linux/mm.h>
  50. #include <linux/slab.h>
  51. #include <linux/netdevice.h>
  52. #include <linux/vmalloc.h>
  53. #include <linux/init.h>
  54. #include <linux/delay.h>
  55. #include <linux/ioctl.h>
  56. #include <asm/system.h>
  57. #include <asm/io.h>
  58. #include <asm/irq.h>
  59. #include <asm/dma.h>
  60. #include <linux/bitops.h>
  61. #include <asm/types.h>
  62. #include <linux/termios.h>
  63. #include <linux/workqueue.h>
  64. #include <linux/hdlc.h>
  65. #if defined(CONFIG_HDLC) || (defined(CONFIG_HDLC_MODULE) && defined(CONFIG_SYNCLINKMP_MODULE))
  66. #define SYNCLINK_GENERIC_HDLC 1
  67. #else
  68. #define SYNCLINK_GENERIC_HDLC 0
  69. #endif
  70. #define GET_USER(error,value,addr) error = get_user(value,addr)
  71. #define COPY_FROM_USER(error,dest,src,size) error = copy_from_user(dest,src,size) ? -EFAULT : 0
  72. #define PUT_USER(error,value,addr) error = put_user(value,addr)
  73. #define COPY_TO_USER(error,dest,src,size) error = copy_to_user(dest,src,size) ? -EFAULT : 0
  74. #include <asm/uaccess.h>
  75. #include "linux/synclink.h"
  76. static MGSL_PARAMS default_params = {
  77. MGSL_MODE_HDLC, /* unsigned long mode */
  78. 0, /* unsigned char loopback; */
  79. HDLC_FLAG_UNDERRUN_ABORT15, /* unsigned short flags; */
  80. HDLC_ENCODING_NRZI_SPACE, /* unsigned char encoding; */
  81. 0, /* unsigned long clock_speed; */
  82. 0xff, /* unsigned char addr_filter; */
  83. HDLC_CRC_16_CCITT, /* unsigned short crc_type; */
  84. HDLC_PREAMBLE_LENGTH_8BITS, /* unsigned char preamble_length; */
  85. HDLC_PREAMBLE_PATTERN_NONE, /* unsigned char preamble; */
  86. 9600, /* unsigned long data_rate; */
  87. 8, /* unsigned char data_bits; */
  88. 1, /* unsigned char stop_bits; */
  89. ASYNC_PARITY_NONE /* unsigned char parity; */
  90. };
  91. /* size in bytes of DMA data buffers */
  92. #define SCABUFSIZE 1024
  93. #define SCA_MEM_SIZE 0x40000
  94. #define SCA_BASE_SIZE 512
  95. #define SCA_REG_SIZE 16
  96. #define SCA_MAX_PORTS 4
  97. #define SCAMAXDESC 128
  98. #define BUFFERLISTSIZE 4096
  99. /* SCA-I style DMA buffer descriptor */
  100. typedef struct _SCADESC
  101. {
  102. u16 next; /* lower l6 bits of next descriptor addr */
  103. u16 buf_ptr; /* lower 16 bits of buffer addr */
  104. u8 buf_base; /* upper 8 bits of buffer addr */
  105. u8 pad1;
  106. u16 length; /* length of buffer */
  107. u8 status; /* status of buffer */
  108. u8 pad2;
  109. } SCADESC, *PSCADESC;
  110. typedef struct _SCADESC_EX
  111. {
  112. /* device driver bookkeeping section */
  113. char *virt_addr; /* virtual address of data buffer */
  114. u16 phys_entry; /* lower 16-bits of physical address of this descriptor */
  115. } SCADESC_EX, *PSCADESC_EX;
  116. /* The queue of BH actions to be performed */
  117. #define BH_RECEIVE 1
  118. #define BH_TRANSMIT 2
  119. #define BH_STATUS 4
  120. #define IO_PIN_SHUTDOWN_LIMIT 100
  121. #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
  122. struct _input_signal_events {
  123. int ri_up;
  124. int ri_down;
  125. int dsr_up;
  126. int dsr_down;
  127. int dcd_up;
  128. int dcd_down;
  129. int cts_up;
  130. int cts_down;
  131. };
  132. /*
  133. * Device instance data structure
  134. */
  135. typedef struct _synclinkmp_info {
  136. void *if_ptr; /* General purpose pointer (used by SPPP) */
  137. int magic;
  138. int flags;
  139. int count; /* count of opens */
  140. int line;
  141. unsigned short close_delay;
  142. unsigned short closing_wait; /* time to wait before closing */
  143. struct mgsl_icount icount;
  144. struct tty_struct *tty;
  145. int timeout;
  146. int x_char; /* xon/xoff character */
  147. int blocked_open; /* # of blocked opens */
  148. u16 read_status_mask1; /* break detection (SR1 indications) */
  149. u16 read_status_mask2; /* parity/framing/overun (SR2 indications) */
  150. unsigned char ignore_status_mask1; /* break detection (SR1 indications) */
  151. unsigned char ignore_status_mask2; /* parity/framing/overun (SR2 indications) */
  152. unsigned char *tx_buf;
  153. int tx_put;
  154. int tx_get;
  155. int tx_count;
  156. wait_queue_head_t open_wait;
  157. wait_queue_head_t close_wait;
  158. wait_queue_head_t status_event_wait_q;
  159. wait_queue_head_t event_wait_q;
  160. struct timer_list tx_timer; /* HDLC transmit timeout timer */
  161. struct _synclinkmp_info *next_device; /* device list link */
  162. struct timer_list status_timer; /* input signal status check timer */
  163. spinlock_t lock; /* spinlock for synchronizing with ISR */
  164. struct work_struct task; /* task structure for scheduling bh */
  165. u32 max_frame_size; /* as set by device config */
  166. u32 pending_bh;
  167. int bh_running; /* Protection from multiple */
  168. int isr_overflow;
  169. int bh_requested;
  170. int dcd_chkcount; /* check counts to prevent */
  171. int cts_chkcount; /* too many IRQs if a signal */
  172. int dsr_chkcount; /* is floating */
  173. int ri_chkcount;
  174. char *buffer_list; /* virtual address of Rx & Tx buffer lists */
  175. unsigned long buffer_list_phys;
  176. unsigned int rx_buf_count; /* count of total allocated Rx buffers */
  177. SCADESC *rx_buf_list; /* list of receive buffer entries */
  178. SCADESC_EX rx_buf_list_ex[SCAMAXDESC]; /* list of receive buffer entries */
  179. unsigned int current_rx_buf;
  180. unsigned int tx_buf_count; /* count of total allocated Tx buffers */
  181. SCADESC *tx_buf_list; /* list of transmit buffer entries */
  182. SCADESC_EX tx_buf_list_ex[SCAMAXDESC]; /* list of transmit buffer entries */
  183. unsigned int last_tx_buf;
  184. unsigned char *tmp_rx_buf;
  185. unsigned int tmp_rx_buf_count;
  186. int rx_enabled;
  187. int rx_overflow;
  188. int tx_enabled;
  189. int tx_active;
  190. u32 idle_mode;
  191. unsigned char ie0_value;
  192. unsigned char ie1_value;
  193. unsigned char ie2_value;
  194. unsigned char ctrlreg_value;
  195. unsigned char old_signals;
  196. char device_name[25]; /* device instance name */
  197. int port_count;
  198. int adapter_num;
  199. int port_num;
  200. struct _synclinkmp_info *port_array[SCA_MAX_PORTS];
  201. unsigned int bus_type; /* expansion bus type (ISA,EISA,PCI) */
  202. unsigned int irq_level; /* interrupt level */
  203. unsigned long irq_flags;
  204. int irq_requested; /* nonzero if IRQ requested */
  205. MGSL_PARAMS params; /* communications parameters */
  206. unsigned char serial_signals; /* current serial signal states */
  207. int irq_occurred; /* for diagnostics use */
  208. unsigned int init_error; /* Initialization startup error */
  209. u32 last_mem_alloc;
  210. unsigned char* memory_base; /* shared memory address (PCI only) */
  211. u32 phys_memory_base;
  212. int shared_mem_requested;
  213. unsigned char* sca_base; /* HD64570 SCA Memory address */
  214. u32 phys_sca_base;
  215. u32 sca_offset;
  216. int sca_base_requested;
  217. unsigned char* lcr_base; /* local config registers (PCI only) */
  218. u32 phys_lcr_base;
  219. u32 lcr_offset;
  220. int lcr_mem_requested;
  221. unsigned char* statctrl_base; /* status/control register memory */
  222. u32 phys_statctrl_base;
  223. u32 statctrl_offset;
  224. int sca_statctrl_requested;
  225. u32 misc_ctrl_value;
  226. char flag_buf[MAX_ASYNC_BUFFER_SIZE];
  227. char char_buf[MAX_ASYNC_BUFFER_SIZE];
  228. BOOLEAN drop_rts_on_tx_done;
  229. struct _input_signal_events input_signal_events;
  230. /* SPPP/Cisco HDLC device parts */
  231. int netcount;
  232. int dosyncppp;
  233. spinlock_t netlock;
  234. #if SYNCLINK_GENERIC_HDLC
  235. struct net_device *netdev;
  236. #endif
  237. } SLMP_INFO;
  238. #define MGSL_MAGIC 0x5401
  239. /*
  240. * define serial signal status change macros
  241. */
  242. #define MISCSTATUS_DCD_LATCHED (SerialSignal_DCD<<8) /* indicates change in DCD */
  243. #define MISCSTATUS_RI_LATCHED (SerialSignal_RI<<8) /* indicates change in RI */
  244. #define MISCSTATUS_CTS_LATCHED (SerialSignal_CTS<<8) /* indicates change in CTS */
  245. #define MISCSTATUS_DSR_LATCHED (SerialSignal_DSR<<8) /* change in DSR */
  246. /* Common Register macros */
  247. #define LPR 0x00
  248. #define PABR0 0x02
  249. #define PABR1 0x03
  250. #define WCRL 0x04
  251. #define WCRM 0x05
  252. #define WCRH 0x06
  253. #define DPCR 0x08
  254. #define DMER 0x09
  255. #define ISR0 0x10
  256. #define ISR1 0x11
  257. #define ISR2 0x12
  258. #define IER0 0x14
  259. #define IER1 0x15
  260. #define IER2 0x16
  261. #define ITCR 0x18
  262. #define INTVR 0x1a
  263. #define IMVR 0x1c
  264. /* MSCI Register macros */
  265. #define TRB 0x20
  266. #define TRBL 0x20
  267. #define TRBH 0x21
  268. #define SR0 0x22
  269. #define SR1 0x23
  270. #define SR2 0x24
  271. #define SR3 0x25
  272. #define FST 0x26
  273. #define IE0 0x28
  274. #define IE1 0x29
  275. #define IE2 0x2a
  276. #define FIE 0x2b
  277. #define CMD 0x2c
  278. #define MD0 0x2e
  279. #define MD1 0x2f
  280. #define MD2 0x30
  281. #define CTL 0x31
  282. #define SA0 0x32
  283. #define SA1 0x33
  284. #define IDL 0x34
  285. #define TMC 0x35
  286. #define RXS 0x36
  287. #define TXS 0x37
  288. #define TRC0 0x38
  289. #define TRC1 0x39
  290. #define RRC 0x3a
  291. #define CST0 0x3c
  292. #define CST1 0x3d
  293. /* Timer Register Macros */
  294. #define TCNT 0x60
  295. #define TCNTL 0x60
  296. #define TCNTH 0x61
  297. #define TCONR 0x62
  298. #define TCONRL 0x62
  299. #define TCONRH 0x63
  300. #define TMCS 0x64
  301. #define TEPR 0x65
  302. /* DMA Controller Register macros */
  303. #define DARL 0x80
  304. #define DARH 0x81
  305. #define DARB 0x82
  306. #define BAR 0x80
  307. #define BARL 0x80
  308. #define BARH 0x81
  309. #define BARB 0x82
  310. #define SAR 0x84
  311. #define SARL 0x84
  312. #define SARH 0x85
  313. #define SARB 0x86
  314. #define CPB 0x86
  315. #define CDA 0x88
  316. #define CDAL 0x88
  317. #define CDAH 0x89
  318. #define EDA 0x8a
  319. #define EDAL 0x8a
  320. #define EDAH 0x8b
  321. #define BFL 0x8c
  322. #define BFLL 0x8c
  323. #define BFLH 0x8d
  324. #define BCR 0x8e
  325. #define BCRL 0x8e
  326. #define BCRH 0x8f
  327. #define DSR 0x90
  328. #define DMR 0x91
  329. #define FCT 0x93
  330. #define DIR 0x94
  331. #define DCMD 0x95
  332. /* combine with timer or DMA register address */
  333. #define TIMER0 0x00
  334. #define TIMER1 0x08
  335. #define TIMER2 0x10
  336. #define TIMER3 0x18
  337. #define RXDMA 0x00
  338. #define TXDMA 0x20
  339. /* SCA Command Codes */
  340. #define NOOP 0x00
  341. #define TXRESET 0x01
  342. #define TXENABLE 0x02
  343. #define TXDISABLE 0x03
  344. #define TXCRCINIT 0x04
  345. #define TXCRCEXCL 0x05
  346. #define TXEOM 0x06
  347. #define TXABORT 0x07
  348. #define MPON 0x08
  349. #define TXBUFCLR 0x09
  350. #define RXRESET 0x11
  351. #define RXENABLE 0x12
  352. #define RXDISABLE 0x13
  353. #define RXCRCINIT 0x14
  354. #define RXREJECT 0x15
  355. #define SEARCHMP 0x16
  356. #define RXCRCEXCL 0x17
  357. #define RXCRCCALC 0x18
  358. #define CHRESET 0x21
  359. #define HUNT 0x31
  360. /* DMA command codes */
  361. #define SWABORT 0x01
  362. #define FEICLEAR 0x02
  363. /* IE0 */
  364. #define TXINTE BIT7
  365. #define RXINTE BIT6
  366. #define TXRDYE BIT1
  367. #define RXRDYE BIT0
  368. /* IE1 & SR1 */
  369. #define UDRN BIT7
  370. #define IDLE BIT6
  371. #define SYNCD BIT4
  372. #define FLGD BIT4
  373. #define CCTS BIT3
  374. #define CDCD BIT2
  375. #define BRKD BIT1
  376. #define ABTD BIT1
  377. #define GAPD BIT1
  378. #define BRKE BIT0
  379. #define IDLD BIT0
  380. /* IE2 & SR2 */
  381. #define EOM BIT7
  382. #define PMP BIT6
  383. #define SHRT BIT6
  384. #define PE BIT5
  385. #define ABT BIT5
  386. #define FRME BIT4
  387. #define RBIT BIT4
  388. #define OVRN BIT3
  389. #define CRCE BIT2
  390. /*
  391. * Global linked list of SyncLink devices
  392. */
  393. static SLMP_INFO *synclinkmp_device_list = NULL;
  394. static int synclinkmp_adapter_count = -1;
  395. static int synclinkmp_device_count = 0;
  396. /*
  397. * Set this param to non-zero to load eax with the
  398. * .text section address and breakpoint on module load.
  399. * This is useful for use with gdb and add-symbol-file command.
  400. */
  401. static int break_on_load=0;
  402. /*
  403. * Driver major number, defaults to zero to get auto
  404. * assigned major number. May be forced as module parameter.
  405. */
  406. static int ttymajor=0;
  407. /*
  408. * Array of user specified options for ISA adapters.
  409. */
  410. static int debug_level = 0;
  411. static int maxframe[MAX_DEVICES] = {0,};
  412. static int dosyncppp[MAX_DEVICES] = {0,};
  413. module_param(break_on_load, bool, 0);
  414. module_param(ttymajor, int, 0);
  415. module_param(debug_level, int, 0);
  416. module_param_array(maxframe, int, NULL, 0);
  417. module_param_array(dosyncppp, int, NULL, 0);
  418. static char *driver_name = "SyncLink MultiPort driver";
  419. static char *driver_version = "$Revision: 4.38 $";
  420. static int synclinkmp_init_one(struct pci_dev *dev,const struct pci_device_id *ent);
  421. static void synclinkmp_remove_one(struct pci_dev *dev);
  422. static struct pci_device_id synclinkmp_pci_tbl[] = {
  423. { PCI_VENDOR_ID_MICROGATE, PCI_DEVICE_ID_MICROGATE_SCA, PCI_ANY_ID, PCI_ANY_ID, },
  424. { 0, }, /* terminate list */
  425. };
  426. MODULE_DEVICE_TABLE(pci, synclinkmp_pci_tbl);
  427. MODULE_LICENSE("GPL");
  428. static struct pci_driver synclinkmp_pci_driver = {
  429. .name = "synclinkmp",
  430. .id_table = synclinkmp_pci_tbl,
  431. .probe = synclinkmp_init_one,
  432. .remove = __devexit_p(synclinkmp_remove_one),
  433. };
  434. static struct tty_driver *serial_driver;
  435. /* number of characters left in xmit buffer before we ask for more */
  436. #define WAKEUP_CHARS 256
  437. /* tty callbacks */
  438. static int open(struct tty_struct *tty, struct file * filp);
  439. static void close(struct tty_struct *tty, struct file * filp);
  440. static void hangup(struct tty_struct *tty);
  441. static void set_termios(struct tty_struct *tty, struct ktermios *old_termios);
  442. static int write(struct tty_struct *tty, const unsigned char *buf, int count);
  443. static void put_char(struct tty_struct *tty, unsigned char ch);
  444. static void send_xchar(struct tty_struct *tty, char ch);
  445. static void wait_until_sent(struct tty_struct *tty, int timeout);
  446. static int write_room(struct tty_struct *tty);
  447. static void flush_chars(struct tty_struct *tty);
  448. static void flush_buffer(struct tty_struct *tty);
  449. static void tx_hold(struct tty_struct *tty);
  450. static void tx_release(struct tty_struct *tty);
  451. static int ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
  452. static int read_proc(char *page, char **start, off_t off, int count,int *eof, void *data);
  453. static int chars_in_buffer(struct tty_struct *tty);
  454. static void throttle(struct tty_struct * tty);
  455. static void unthrottle(struct tty_struct * tty);
  456. static void set_break(struct tty_struct *tty, int break_state);
  457. #if SYNCLINK_GENERIC_HDLC
  458. #define dev_to_port(D) (dev_to_hdlc(D)->priv)
  459. static void hdlcdev_tx_done(SLMP_INFO *info);
  460. static void hdlcdev_rx(SLMP_INFO *info, char *buf, int size);
  461. static int hdlcdev_init(SLMP_INFO *info);
  462. static void hdlcdev_exit(SLMP_INFO *info);
  463. #endif
  464. /* ioctl handlers */
  465. static int get_stats(SLMP_INFO *info, struct mgsl_icount __user *user_icount);
  466. static int get_params(SLMP_INFO *info, MGSL_PARAMS __user *params);
  467. static int set_params(SLMP_INFO *info, MGSL_PARAMS __user *params);
  468. static int get_txidle(SLMP_INFO *info, int __user *idle_mode);
  469. static int set_txidle(SLMP_INFO *info, int idle_mode);
  470. static int tx_enable(SLMP_INFO *info, int enable);
  471. static int tx_abort(SLMP_INFO *info);
  472. static int rx_enable(SLMP_INFO *info, int enable);
  473. static int modem_input_wait(SLMP_INFO *info,int arg);
  474. static int wait_mgsl_event(SLMP_INFO *info, int __user *mask_ptr);
  475. static int tiocmget(struct tty_struct *tty, struct file *file);
  476. static int tiocmset(struct tty_struct *tty, struct file *file,
  477. unsigned int set, unsigned int clear);
  478. static void set_break(struct tty_struct *tty, int break_state);
  479. static void add_device(SLMP_INFO *info);
  480. static void device_init(int adapter_num, struct pci_dev *pdev);
  481. static int claim_resources(SLMP_INFO *info);
  482. static void release_resources(SLMP_INFO *info);
  483. static int startup(SLMP_INFO *info);
  484. static int block_til_ready(struct tty_struct *tty, struct file * filp,SLMP_INFO *info);
  485. static void shutdown(SLMP_INFO *info);
  486. static void program_hw(SLMP_INFO *info);
  487. static void change_params(SLMP_INFO *info);
  488. static int init_adapter(SLMP_INFO *info);
  489. static int register_test(SLMP_INFO *info);
  490. static int irq_test(SLMP_INFO *info);
  491. static int loopback_test(SLMP_INFO *info);
  492. static int adapter_test(SLMP_INFO *info);
  493. static int memory_test(SLMP_INFO *info);
  494. static void reset_adapter(SLMP_INFO *info);
  495. static void reset_port(SLMP_INFO *info);
  496. static void async_mode(SLMP_INFO *info);
  497. static void hdlc_mode(SLMP_INFO *info);
  498. static void rx_stop(SLMP_INFO *info);
  499. static void rx_start(SLMP_INFO *info);
  500. static void rx_reset_buffers(SLMP_INFO *info);
  501. static void rx_free_frame_buffers(SLMP_INFO *info, unsigned int first, unsigned int last);
  502. static int rx_get_frame(SLMP_INFO *info);
  503. static void tx_start(SLMP_INFO *info);
  504. static void tx_stop(SLMP_INFO *info);
  505. static void tx_load_fifo(SLMP_INFO *info);
  506. static void tx_set_idle(SLMP_INFO *info);
  507. static void tx_load_dma_buffer(SLMP_INFO *info, const char *buf, unsigned int count);
  508. static void get_signals(SLMP_INFO *info);
  509. static void set_signals(SLMP_INFO *info);
  510. static void enable_loopback(SLMP_INFO *info, int enable);
  511. static void set_rate(SLMP_INFO *info, u32 data_rate);
  512. static int bh_action(SLMP_INFO *info);
  513. static void bh_handler(struct work_struct *work);
  514. static void bh_receive(SLMP_INFO *info);
  515. static void bh_transmit(SLMP_INFO *info);
  516. static void bh_status(SLMP_INFO *info);
  517. static void isr_timer(SLMP_INFO *info);
  518. static void isr_rxint(SLMP_INFO *info);
  519. static void isr_rxrdy(SLMP_INFO *info);
  520. static void isr_txint(SLMP_INFO *info);
  521. static void isr_txrdy(SLMP_INFO *info);
  522. static void isr_rxdmaok(SLMP_INFO *info);
  523. static void isr_rxdmaerror(SLMP_INFO *info);
  524. static void isr_txdmaok(SLMP_INFO *info);
  525. static void isr_txdmaerror(SLMP_INFO *info);
  526. static void isr_io_pin(SLMP_INFO *info, u16 status);
  527. static int alloc_dma_bufs(SLMP_INFO *info);
  528. static void free_dma_bufs(SLMP_INFO *info);
  529. static int alloc_buf_list(SLMP_INFO *info);
  530. static int alloc_frame_bufs(SLMP_INFO *info, SCADESC *list, SCADESC_EX *list_ex,int count);
  531. static int alloc_tmp_rx_buf(SLMP_INFO *info);
  532. static void free_tmp_rx_buf(SLMP_INFO *info);
  533. static void load_pci_memory(SLMP_INFO *info, char* dest, const char* src, unsigned short count);
  534. static void trace_block(SLMP_INFO *info, const char* data, int count, int xmit);
  535. static void tx_timeout(unsigned long context);
  536. static void status_timeout(unsigned long context);
  537. static unsigned char read_reg(SLMP_INFO *info, unsigned char addr);
  538. static void write_reg(SLMP_INFO *info, unsigned char addr, unsigned char val);
  539. static u16 read_reg16(SLMP_INFO *info, unsigned char addr);
  540. static void write_reg16(SLMP_INFO *info, unsigned char addr, u16 val);
  541. static unsigned char read_status_reg(SLMP_INFO * info);
  542. static void write_control_reg(SLMP_INFO * info);
  543. static unsigned char rx_active_fifo_level = 16; // rx request FIFO activation level in bytes
  544. static unsigned char tx_active_fifo_level = 16; // tx request FIFO activation level in bytes
  545. static unsigned char tx_negate_fifo_level = 32; // tx request FIFO negation level in bytes
  546. static u32 misc_ctrl_value = 0x007e4040;
  547. static u32 lcr1_brdr_value = 0x00800028;
  548. static u32 read_ahead_count = 8;
  549. /* DPCR, DMA Priority Control
  550. *
  551. * 07..05 Not used, must be 0
  552. * 04 BRC, bus release condition: 0=all transfers complete
  553. * 1=release after 1 xfer on all channels
  554. * 03 CCC, channel change condition: 0=every cycle
  555. * 1=after each channel completes all xfers
  556. * 02..00 PR<2..0>, priority 100=round robin
  557. *
  558. * 00000100 = 0x00
  559. */
  560. static unsigned char dma_priority = 0x04;
  561. // Number of bytes that can be written to shared RAM
  562. // in a single write operation
  563. static u32 sca_pci_load_interval = 64;
  564. /*
  565. * 1st function defined in .text section. Calling this function in
  566. * init_module() followed by a breakpoint allows a remote debugger
  567. * (gdb) to get the .text address for the add-symbol-file command.
  568. * This allows remote debugging of dynamically loadable modules.
  569. */
  570. static void* synclinkmp_get_text_ptr(void);
  571. static void* synclinkmp_get_text_ptr(void) {return synclinkmp_get_text_ptr;}
  572. static inline int sanity_check(SLMP_INFO *info,
  573. char *name, const char *routine)
  574. {
  575. #ifdef SANITY_CHECK
  576. static const char *badmagic =
  577. "Warning: bad magic number for synclinkmp_struct (%s) in %s\n";
  578. static const char *badinfo =
  579. "Warning: null synclinkmp_struct for (%s) in %s\n";
  580. if (!info) {
  581. printk(badinfo, name, routine);
  582. return 1;
  583. }
  584. if (info->magic != MGSL_MAGIC) {
  585. printk(badmagic, name, routine);
  586. return 1;
  587. }
  588. #else
  589. if (!info)
  590. return 1;
  591. #endif
  592. return 0;
  593. }
  594. /**
  595. * line discipline callback wrappers
  596. *
  597. * The wrappers maintain line discipline references
  598. * while calling into the line discipline.
  599. *
  600. * ldisc_receive_buf - pass receive data to line discipline
  601. */
  602. static void ldisc_receive_buf(struct tty_struct *tty,
  603. const __u8 *data, char *flags, int count)
  604. {
  605. struct tty_ldisc *ld;
  606. if (!tty)
  607. return;
  608. ld = tty_ldisc_ref(tty);
  609. if (ld) {
  610. if (ld->receive_buf)
  611. ld->receive_buf(tty, data, flags, count);
  612. tty_ldisc_deref(ld);
  613. }
  614. }
  615. /* tty callbacks */
  616. /* Called when a port is opened. Init and enable port.
  617. */
  618. static int open(struct tty_struct *tty, struct file *filp)
  619. {
  620. SLMP_INFO *info;
  621. int retval, line;
  622. unsigned long flags;
  623. line = tty->index;
  624. if ((line < 0) || (line >= synclinkmp_device_count)) {
  625. printk("%s(%d): open with invalid line #%d.\n",
  626. __FILE__,__LINE__,line);
  627. return -ENODEV;
  628. }
  629. info = synclinkmp_device_list;
  630. while(info && info->line != line)
  631. info = info->next_device;
  632. if (sanity_check(info, tty->name, "open"))
  633. return -ENODEV;
  634. if ( info->init_error ) {
  635. printk("%s(%d):%s device is not allocated, init error=%d\n",
  636. __FILE__,__LINE__,info->device_name,info->init_error);
  637. return -ENODEV;
  638. }
  639. tty->driver_data = info;
  640. info->tty = tty;
  641. if (debug_level >= DEBUG_LEVEL_INFO)
  642. printk("%s(%d):%s open(), old ref count = %d\n",
  643. __FILE__,__LINE__,tty->driver->name, info->count);
  644. /* If port is closing, signal caller to try again */
  645. if (tty_hung_up_p(filp) || info->flags & ASYNC_CLOSING){
  646. if (info->flags & ASYNC_CLOSING)
  647. interruptible_sleep_on(&info->close_wait);
  648. retval = ((info->flags & ASYNC_HUP_NOTIFY) ?
  649. -EAGAIN : -ERESTARTSYS);
  650. goto cleanup;
  651. }
  652. info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  653. spin_lock_irqsave(&info->netlock, flags);
  654. if (info->netcount) {
  655. retval = -EBUSY;
  656. spin_unlock_irqrestore(&info->netlock, flags);
  657. goto cleanup;
  658. }
  659. info->count++;
  660. spin_unlock_irqrestore(&info->netlock, flags);
  661. if (info->count == 1) {
  662. /* 1st open on this device, init hardware */
  663. retval = startup(info);
  664. if (retval < 0)
  665. goto cleanup;
  666. }
  667. retval = block_til_ready(tty, filp, info);
  668. if (retval) {
  669. if (debug_level >= DEBUG_LEVEL_INFO)
  670. printk("%s(%d):%s block_til_ready() returned %d\n",
  671. __FILE__,__LINE__, info->device_name, retval);
  672. goto cleanup;
  673. }
  674. if (debug_level >= DEBUG_LEVEL_INFO)
  675. printk("%s(%d):%s open() success\n",
  676. __FILE__,__LINE__, info->device_name);
  677. retval = 0;
  678. cleanup:
  679. if (retval) {
  680. if (tty->count == 1)
  681. info->tty = NULL; /* tty layer will release tty struct */
  682. if(info->count)
  683. info->count--;
  684. }
  685. return retval;
  686. }
  687. /* Called when port is closed. Wait for remaining data to be
  688. * sent. Disable port and free resources.
  689. */
  690. static void close(struct tty_struct *tty, struct file *filp)
  691. {
  692. SLMP_INFO * info = (SLMP_INFO *)tty->driver_data;
  693. if (sanity_check(info, tty->name, "close"))
  694. return;
  695. if (debug_level >= DEBUG_LEVEL_INFO)
  696. printk("%s(%d):%s close() entry, count=%d\n",
  697. __FILE__,__LINE__, info->device_name, info->count);
  698. if (!info->count)
  699. return;
  700. if (tty_hung_up_p(filp))
  701. goto cleanup;
  702. if ((tty->count == 1) && (info->count != 1)) {
  703. /*
  704. * tty->count is 1 and the tty structure will be freed.
  705. * info->count should be one in this case.
  706. * if it's not, correct it so that the port is shutdown.
  707. */
  708. printk("%s(%d):%s close: bad refcount; tty->count is 1, "
  709. "info->count is %d\n",
  710. __FILE__,__LINE__, info->device_name, info->count);
  711. info->count = 1;
  712. }
  713. info->count--;
  714. /* if at least one open remaining, leave hardware active */
  715. if (info->count)
  716. goto cleanup;
  717. info->flags |= ASYNC_CLOSING;
  718. /* set tty->closing to notify line discipline to
  719. * only process XON/XOFF characters. Only the N_TTY
  720. * discipline appears to use this (ppp does not).
  721. */
  722. tty->closing = 1;
  723. /* wait for transmit data to clear all layers */
  724. if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE) {
  725. if (debug_level >= DEBUG_LEVEL_INFO)
  726. printk("%s(%d):%s close() calling tty_wait_until_sent\n",
  727. __FILE__,__LINE__, info->device_name );
  728. tty_wait_until_sent(tty, info->closing_wait);
  729. }
  730. if (info->flags & ASYNC_INITIALIZED)
  731. wait_until_sent(tty, info->timeout);
  732. if (tty->driver->flush_buffer)
  733. tty->driver->flush_buffer(tty);
  734. tty_ldisc_flush(tty);
  735. shutdown(info);
  736. tty->closing = 0;
  737. info->tty = NULL;
  738. if (info->blocked_open) {
  739. if (info->close_delay) {
  740. msleep_interruptible(jiffies_to_msecs(info->close_delay));
  741. }
  742. wake_up_interruptible(&info->open_wait);
  743. }
  744. info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
  745. wake_up_interruptible(&info->close_wait);
  746. cleanup:
  747. if (debug_level >= DEBUG_LEVEL_INFO)
  748. printk("%s(%d):%s close() exit, count=%d\n", __FILE__,__LINE__,
  749. tty->driver->name, info->count);
  750. }
  751. /* Called by tty_hangup() when a hangup is signaled.
  752. * This is the same as closing all open descriptors for the port.
  753. */
  754. static void hangup(struct tty_struct *tty)
  755. {
  756. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  757. if (debug_level >= DEBUG_LEVEL_INFO)
  758. printk("%s(%d):%s hangup()\n",
  759. __FILE__,__LINE__, info->device_name );
  760. if (sanity_check(info, tty->name, "hangup"))
  761. return;
  762. flush_buffer(tty);
  763. shutdown(info);
  764. info->count = 0;
  765. info->flags &= ~ASYNC_NORMAL_ACTIVE;
  766. info->tty = NULL;
  767. wake_up_interruptible(&info->open_wait);
  768. }
  769. /* Set new termios settings
  770. */
  771. static void set_termios(struct tty_struct *tty, struct ktermios *old_termios)
  772. {
  773. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  774. unsigned long flags;
  775. if (debug_level >= DEBUG_LEVEL_INFO)
  776. printk("%s(%d):%s set_termios()\n", __FILE__,__LINE__,
  777. tty->driver->name );
  778. /* just return if nothing has changed */
  779. if ((tty->termios->c_cflag == old_termios->c_cflag)
  780. && (RELEVANT_IFLAG(tty->termios->c_iflag)
  781. == RELEVANT_IFLAG(old_termios->c_iflag)))
  782. return;
  783. change_params(info);
  784. /* Handle transition to B0 status */
  785. if (old_termios->c_cflag & CBAUD &&
  786. !(tty->termios->c_cflag & CBAUD)) {
  787. info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
  788. spin_lock_irqsave(&info->lock,flags);
  789. set_signals(info);
  790. spin_unlock_irqrestore(&info->lock,flags);
  791. }
  792. /* Handle transition away from B0 status */
  793. if (!(old_termios->c_cflag & CBAUD) &&
  794. tty->termios->c_cflag & CBAUD) {
  795. info->serial_signals |= SerialSignal_DTR;
  796. if (!(tty->termios->c_cflag & CRTSCTS) ||
  797. !test_bit(TTY_THROTTLED, &tty->flags)) {
  798. info->serial_signals |= SerialSignal_RTS;
  799. }
  800. spin_lock_irqsave(&info->lock,flags);
  801. set_signals(info);
  802. spin_unlock_irqrestore(&info->lock,flags);
  803. }
  804. /* Handle turning off CRTSCTS */
  805. if (old_termios->c_cflag & CRTSCTS &&
  806. !(tty->termios->c_cflag & CRTSCTS)) {
  807. tty->hw_stopped = 0;
  808. tx_release(tty);
  809. }
  810. }
  811. /* Send a block of data
  812. *
  813. * Arguments:
  814. *
  815. * tty pointer to tty information structure
  816. * buf pointer to buffer containing send data
  817. * count size of send data in bytes
  818. *
  819. * Return Value: number of characters written
  820. */
  821. static int write(struct tty_struct *tty,
  822. const unsigned char *buf, int count)
  823. {
  824. int c, ret = 0;
  825. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  826. unsigned long flags;
  827. if (debug_level >= DEBUG_LEVEL_INFO)
  828. printk("%s(%d):%s write() count=%d\n",
  829. __FILE__,__LINE__,info->device_name,count);
  830. if (sanity_check(info, tty->name, "write"))
  831. goto cleanup;
  832. if (!info->tx_buf)
  833. goto cleanup;
  834. if (info->params.mode == MGSL_MODE_HDLC) {
  835. if (count > info->max_frame_size) {
  836. ret = -EIO;
  837. goto cleanup;
  838. }
  839. if (info->tx_active)
  840. goto cleanup;
  841. if (info->tx_count) {
  842. /* send accumulated data from send_char() calls */
  843. /* as frame and wait before accepting more data. */
  844. tx_load_dma_buffer(info, info->tx_buf, info->tx_count);
  845. goto start;
  846. }
  847. ret = info->tx_count = count;
  848. tx_load_dma_buffer(info, buf, count);
  849. goto start;
  850. }
  851. for (;;) {
  852. c = min_t(int, count,
  853. min(info->max_frame_size - info->tx_count - 1,
  854. info->max_frame_size - info->tx_put));
  855. if (c <= 0)
  856. break;
  857. memcpy(info->tx_buf + info->tx_put, buf, c);
  858. spin_lock_irqsave(&info->lock,flags);
  859. info->tx_put += c;
  860. if (info->tx_put >= info->max_frame_size)
  861. info->tx_put -= info->max_frame_size;
  862. info->tx_count += c;
  863. spin_unlock_irqrestore(&info->lock,flags);
  864. buf += c;
  865. count -= c;
  866. ret += c;
  867. }
  868. if (info->params.mode == MGSL_MODE_HDLC) {
  869. if (count) {
  870. ret = info->tx_count = 0;
  871. goto cleanup;
  872. }
  873. tx_load_dma_buffer(info, info->tx_buf, info->tx_count);
  874. }
  875. start:
  876. if (info->tx_count && !tty->stopped && !tty->hw_stopped) {
  877. spin_lock_irqsave(&info->lock,flags);
  878. if (!info->tx_active)
  879. tx_start(info);
  880. spin_unlock_irqrestore(&info->lock,flags);
  881. }
  882. cleanup:
  883. if (debug_level >= DEBUG_LEVEL_INFO)
  884. printk( "%s(%d):%s write() returning=%d\n",
  885. __FILE__,__LINE__,info->device_name,ret);
  886. return ret;
  887. }
  888. /* Add a character to the transmit buffer.
  889. */
  890. static void put_char(struct tty_struct *tty, unsigned char ch)
  891. {
  892. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  893. unsigned long flags;
  894. if ( debug_level >= DEBUG_LEVEL_INFO ) {
  895. printk( "%s(%d):%s put_char(%d)\n",
  896. __FILE__,__LINE__,info->device_name,ch);
  897. }
  898. if (sanity_check(info, tty->name, "put_char"))
  899. return;
  900. if (!info->tx_buf)
  901. return;
  902. spin_lock_irqsave(&info->lock,flags);
  903. if ( (info->params.mode != MGSL_MODE_HDLC) ||
  904. !info->tx_active ) {
  905. if (info->tx_count < info->max_frame_size - 1) {
  906. info->tx_buf[info->tx_put++] = ch;
  907. if (info->tx_put >= info->max_frame_size)
  908. info->tx_put -= info->max_frame_size;
  909. info->tx_count++;
  910. }
  911. }
  912. spin_unlock_irqrestore(&info->lock,flags);
  913. }
  914. /* Send a high-priority XON/XOFF character
  915. */
  916. static void send_xchar(struct tty_struct *tty, char ch)
  917. {
  918. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  919. unsigned long flags;
  920. if (debug_level >= DEBUG_LEVEL_INFO)
  921. printk("%s(%d):%s send_xchar(%d)\n",
  922. __FILE__,__LINE__, info->device_name, ch );
  923. if (sanity_check(info, tty->name, "send_xchar"))
  924. return;
  925. info->x_char = ch;
  926. if (ch) {
  927. /* Make sure transmit interrupts are on */
  928. spin_lock_irqsave(&info->lock,flags);
  929. if (!info->tx_enabled)
  930. tx_start(info);
  931. spin_unlock_irqrestore(&info->lock,flags);
  932. }
  933. }
  934. /* Wait until the transmitter is empty.
  935. */
  936. static void wait_until_sent(struct tty_struct *tty, int timeout)
  937. {
  938. SLMP_INFO * info = (SLMP_INFO *)tty->driver_data;
  939. unsigned long orig_jiffies, char_time;
  940. if (!info )
  941. return;
  942. if (debug_level >= DEBUG_LEVEL_INFO)
  943. printk("%s(%d):%s wait_until_sent() entry\n",
  944. __FILE__,__LINE__, info->device_name );
  945. if (sanity_check(info, tty->name, "wait_until_sent"))
  946. return;
  947. if (!(info->flags & ASYNC_INITIALIZED))
  948. goto exit;
  949. orig_jiffies = jiffies;
  950. /* Set check interval to 1/5 of estimated time to
  951. * send a character, and make it at least 1. The check
  952. * interval should also be less than the timeout.
  953. * Note: use tight timings here to satisfy the NIST-PCTS.
  954. */
  955. if ( info->params.data_rate ) {
  956. char_time = info->timeout/(32 * 5);
  957. if (!char_time)
  958. char_time++;
  959. } else
  960. char_time = 1;
  961. if (timeout)
  962. char_time = min_t(unsigned long, char_time, timeout);
  963. if ( info->params.mode == MGSL_MODE_HDLC ) {
  964. while (info->tx_active) {
  965. msleep_interruptible(jiffies_to_msecs(char_time));
  966. if (signal_pending(current))
  967. break;
  968. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  969. break;
  970. }
  971. } else {
  972. //TODO: determine if there is something similar to USC16C32
  973. // TXSTATUS_ALL_SENT status
  974. while ( info->tx_active && info->tx_enabled) {
  975. msleep_interruptible(jiffies_to_msecs(char_time));
  976. if (signal_pending(current))
  977. break;
  978. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  979. break;
  980. }
  981. }
  982. exit:
  983. if (debug_level >= DEBUG_LEVEL_INFO)
  984. printk("%s(%d):%s wait_until_sent() exit\n",
  985. __FILE__,__LINE__, info->device_name );
  986. }
  987. /* Return the count of free bytes in transmit buffer
  988. */
  989. static int write_room(struct tty_struct *tty)
  990. {
  991. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  992. int ret;
  993. if (sanity_check(info, tty->name, "write_room"))
  994. return 0;
  995. if (info->params.mode == MGSL_MODE_HDLC) {
  996. ret = (info->tx_active) ? 0 : HDLC_MAX_FRAME_SIZE;
  997. } else {
  998. ret = info->max_frame_size - info->tx_count - 1;
  999. if (ret < 0)
  1000. ret = 0;
  1001. }
  1002. if (debug_level >= DEBUG_LEVEL_INFO)
  1003. printk("%s(%d):%s write_room()=%d\n",
  1004. __FILE__, __LINE__, info->device_name, ret);
  1005. return ret;
  1006. }
  1007. /* enable transmitter and send remaining buffered characters
  1008. */
  1009. static void flush_chars(struct tty_struct *tty)
  1010. {
  1011. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  1012. unsigned long flags;
  1013. if ( debug_level >= DEBUG_LEVEL_INFO )
  1014. printk( "%s(%d):%s flush_chars() entry tx_count=%d\n",
  1015. __FILE__,__LINE__,info->device_name,info->tx_count);
  1016. if (sanity_check(info, tty->name, "flush_chars"))
  1017. return;
  1018. if (info->tx_count <= 0 || tty->stopped || tty->hw_stopped ||
  1019. !info->tx_buf)
  1020. return;
  1021. if ( debug_level >= DEBUG_LEVEL_INFO )
  1022. printk( "%s(%d):%s flush_chars() entry, starting transmitter\n",
  1023. __FILE__,__LINE__,info->device_name );
  1024. spin_lock_irqsave(&info->lock,flags);
  1025. if (!info->tx_active) {
  1026. if ( (info->params.mode == MGSL_MODE_HDLC) &&
  1027. info->tx_count ) {
  1028. /* operating in synchronous (frame oriented) mode */
  1029. /* copy data from circular tx_buf to */
  1030. /* transmit DMA buffer. */
  1031. tx_load_dma_buffer(info,
  1032. info->tx_buf,info->tx_count);
  1033. }
  1034. tx_start(info);
  1035. }
  1036. spin_unlock_irqrestore(&info->lock,flags);
  1037. }
  1038. /* Discard all data in the send buffer
  1039. */
  1040. static void flush_buffer(struct tty_struct *tty)
  1041. {
  1042. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  1043. unsigned long flags;
  1044. if (debug_level >= DEBUG_LEVEL_INFO)
  1045. printk("%s(%d):%s flush_buffer() entry\n",
  1046. __FILE__,__LINE__, info->device_name );
  1047. if (sanity_check(info, tty->name, "flush_buffer"))
  1048. return;
  1049. spin_lock_irqsave(&info->lock,flags);
  1050. info->tx_count = info->tx_put = info->tx_get = 0;
  1051. del_timer(&info->tx_timer);
  1052. spin_unlock_irqrestore(&info->lock,flags);
  1053. wake_up_interruptible(&tty->write_wait);
  1054. tty_wakeup(tty);
  1055. }
  1056. /* throttle (stop) transmitter
  1057. */
  1058. static void tx_hold(struct tty_struct *tty)
  1059. {
  1060. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  1061. unsigned long flags;
  1062. if (sanity_check(info, tty->name, "tx_hold"))
  1063. return;
  1064. if ( debug_level >= DEBUG_LEVEL_INFO )
  1065. printk("%s(%d):%s tx_hold()\n",
  1066. __FILE__,__LINE__,info->device_name);
  1067. spin_lock_irqsave(&info->lock,flags);
  1068. if (info->tx_enabled)
  1069. tx_stop(info);
  1070. spin_unlock_irqrestore(&info->lock,flags);
  1071. }
  1072. /* release (start) transmitter
  1073. */
  1074. static void tx_release(struct tty_struct *tty)
  1075. {
  1076. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  1077. unsigned long flags;
  1078. if (sanity_check(info, tty->name, "tx_release"))
  1079. return;
  1080. if ( debug_level >= DEBUG_LEVEL_INFO )
  1081. printk("%s(%d):%s tx_release()\n",
  1082. __FILE__,__LINE__,info->device_name);
  1083. spin_lock_irqsave(&info->lock,flags);
  1084. if (!info->tx_enabled)
  1085. tx_start(info);
  1086. spin_unlock_irqrestore(&info->lock,flags);
  1087. }
  1088. /* Service an IOCTL request
  1089. *
  1090. * Arguments:
  1091. *
  1092. * tty pointer to tty instance data
  1093. * file pointer to associated file object for device
  1094. * cmd IOCTL command code
  1095. * arg command argument/context
  1096. *
  1097. * Return Value: 0 if success, otherwise error code
  1098. */
  1099. static int ioctl(struct tty_struct *tty, struct file *file,
  1100. unsigned int cmd, unsigned long arg)
  1101. {
  1102. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  1103. int error;
  1104. struct mgsl_icount cnow; /* kernel counter temps */
  1105. struct serial_icounter_struct __user *p_cuser; /* user space */
  1106. unsigned long flags;
  1107. void __user *argp = (void __user *)arg;
  1108. if (debug_level >= DEBUG_LEVEL_INFO)
  1109. printk("%s(%d):%s ioctl() cmd=%08X\n", __FILE__,__LINE__,
  1110. info->device_name, cmd );
  1111. if (sanity_check(info, tty->name, "ioctl"))
  1112. return -ENODEV;
  1113. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  1114. (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
  1115. if (tty->flags & (1 << TTY_IO_ERROR))
  1116. return -EIO;
  1117. }
  1118. switch (cmd) {
  1119. case MGSL_IOCGPARAMS:
  1120. return get_params(info, argp);
  1121. case MGSL_IOCSPARAMS:
  1122. return set_params(info, argp);
  1123. case MGSL_IOCGTXIDLE:
  1124. return get_txidle(info, argp);
  1125. case MGSL_IOCSTXIDLE:
  1126. return set_txidle(info, (int)arg);
  1127. case MGSL_IOCTXENABLE:
  1128. return tx_enable(info, (int)arg);
  1129. case MGSL_IOCRXENABLE:
  1130. return rx_enable(info, (int)arg);
  1131. case MGSL_IOCTXABORT:
  1132. return tx_abort(info);
  1133. case MGSL_IOCGSTATS:
  1134. return get_stats(info, argp);
  1135. case MGSL_IOCWAITEVENT:
  1136. return wait_mgsl_event(info, argp);
  1137. case MGSL_IOCLOOPTXDONE:
  1138. return 0; // TODO: Not supported, need to document
  1139. /* Wait for modem input (DCD,RI,DSR,CTS) change
  1140. * as specified by mask in arg (TIOCM_RNG/DSR/CD/CTS)
  1141. */
  1142. case TIOCMIWAIT:
  1143. return modem_input_wait(info,(int)arg);
  1144. /*
  1145. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1146. * Return: write counters to the user passed counter struct
  1147. * NB: both 1->0 and 0->1 transitions are counted except for
  1148. * RI where only 0->1 is counted.
  1149. */
  1150. case TIOCGICOUNT:
  1151. spin_lock_irqsave(&info->lock,flags);
  1152. cnow = info->icount;
  1153. spin_unlock_irqrestore(&info->lock,flags);
  1154. p_cuser = argp;
  1155. PUT_USER(error,cnow.cts, &p_cuser->cts);
  1156. if (error) return error;
  1157. PUT_USER(error,cnow.dsr, &p_cuser->dsr);
  1158. if (error) return error;
  1159. PUT_USER(error,cnow.rng, &p_cuser->rng);
  1160. if (error) return error;
  1161. PUT_USER(error,cnow.dcd, &p_cuser->dcd);
  1162. if (error) return error;
  1163. PUT_USER(error,cnow.rx, &p_cuser->rx);
  1164. if (error) return error;
  1165. PUT_USER(error,cnow.tx, &p_cuser->tx);
  1166. if (error) return error;
  1167. PUT_USER(error,cnow.frame, &p_cuser->frame);
  1168. if (error) return error;
  1169. PUT_USER(error,cnow.overrun, &p_cuser->overrun);
  1170. if (error) return error;
  1171. PUT_USER(error,cnow.parity, &p_cuser->parity);
  1172. if (error) return error;
  1173. PUT_USER(error,cnow.brk, &p_cuser->brk);
  1174. if (error) return error;
  1175. PUT_USER(error,cnow.buf_overrun, &p_cuser->buf_overrun);
  1176. if (error) return error;
  1177. return 0;
  1178. default:
  1179. return -ENOIOCTLCMD;
  1180. }
  1181. return 0;
  1182. }
  1183. /*
  1184. * /proc fs routines....
  1185. */
  1186. static inline int line_info(char *buf, SLMP_INFO *info)
  1187. {
  1188. char stat_buf[30];
  1189. int ret;
  1190. unsigned long flags;
  1191. ret = sprintf(buf, "%s: SCABase=%08x Mem=%08X StatusControl=%08x LCR=%08X\n"
  1192. "\tIRQ=%d MaxFrameSize=%u\n",
  1193. info->device_name,
  1194. info->phys_sca_base,
  1195. info->phys_memory_base,
  1196. info->phys_statctrl_base,
  1197. info->phys_lcr_base,
  1198. info->irq_level,
  1199. info->max_frame_size );
  1200. /* output current serial signal states */
  1201. spin_lock_irqsave(&info->lock,flags);
  1202. get_signals(info);
  1203. spin_unlock_irqrestore(&info->lock,flags);
  1204. stat_buf[0] = 0;
  1205. stat_buf[1] = 0;
  1206. if (info->serial_signals & SerialSignal_RTS)
  1207. strcat(stat_buf, "|RTS");
  1208. if (info->serial_signals & SerialSignal_CTS)
  1209. strcat(stat_buf, "|CTS");
  1210. if (info->serial_signals & SerialSignal_DTR)
  1211. strcat(stat_buf, "|DTR");
  1212. if (info->serial_signals & SerialSignal_DSR)
  1213. strcat(stat_buf, "|DSR");
  1214. if (info->serial_signals & SerialSignal_DCD)
  1215. strcat(stat_buf, "|CD");
  1216. if (info->serial_signals & SerialSignal_RI)
  1217. strcat(stat_buf, "|RI");
  1218. if (info->params.mode == MGSL_MODE_HDLC) {
  1219. ret += sprintf(buf+ret, "\tHDLC txok:%d rxok:%d",
  1220. info->icount.txok, info->icount.rxok);
  1221. if (info->icount.txunder)
  1222. ret += sprintf(buf+ret, " txunder:%d", info->icount.txunder);
  1223. if (info->icount.txabort)
  1224. ret += sprintf(buf+ret, " txabort:%d", info->icount.txabort);
  1225. if (info->icount.rxshort)
  1226. ret += sprintf(buf+ret, " rxshort:%d", info->icount.rxshort);
  1227. if (info->icount.rxlong)
  1228. ret += sprintf(buf+ret, " rxlong:%d", info->icount.rxlong);
  1229. if (info->icount.rxover)
  1230. ret += sprintf(buf+ret, " rxover:%d", info->icount.rxover);
  1231. if (info->icount.rxcrc)
  1232. ret += sprintf(buf+ret, " rxlong:%d", info->icount.rxcrc);
  1233. } else {
  1234. ret += sprintf(buf+ret, "\tASYNC tx:%d rx:%d",
  1235. info->icount.tx, info->icount.rx);
  1236. if (info->icount.frame)
  1237. ret += sprintf(buf+ret, " fe:%d", info->icount.frame);
  1238. if (info->icount.parity)
  1239. ret += sprintf(buf+ret, " pe:%d", info->icount.parity);
  1240. if (info->icount.brk)
  1241. ret += sprintf(buf+ret, " brk:%d", info->icount.brk);
  1242. if (info->icount.overrun)
  1243. ret += sprintf(buf+ret, " oe:%d", info->icount.overrun);
  1244. }
  1245. /* Append serial signal status to end */
  1246. ret += sprintf(buf+ret, " %s\n", stat_buf+1);
  1247. ret += sprintf(buf+ret, "\ttxactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
  1248. info->tx_active,info->bh_requested,info->bh_running,
  1249. info->pending_bh);
  1250. return ret;
  1251. }
  1252. /* Called to print information about devices
  1253. */
  1254. int read_proc(char *page, char **start, off_t off, int count,
  1255. int *eof, void *data)
  1256. {
  1257. int len = 0, l;
  1258. off_t begin = 0;
  1259. SLMP_INFO *info;
  1260. len += sprintf(page, "synclinkmp driver:%s\n", driver_version);
  1261. info = synclinkmp_device_list;
  1262. while( info ) {
  1263. l = line_info(page + len, info);
  1264. len += l;
  1265. if (len+begin > off+count)
  1266. goto done;
  1267. if (len+begin < off) {
  1268. begin += len;
  1269. len = 0;
  1270. }
  1271. info = info->next_device;
  1272. }
  1273. *eof = 1;
  1274. done:
  1275. if (off >= len+begin)
  1276. return 0;
  1277. *start = page + (off-begin);
  1278. return ((count < begin+len-off) ? count : begin+len-off);
  1279. }
  1280. /* Return the count of bytes in transmit buffer
  1281. */
  1282. static int chars_in_buffer(struct tty_struct *tty)
  1283. {
  1284. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  1285. if (sanity_check(info, tty->name, "chars_in_buffer"))
  1286. return 0;
  1287. if (debug_level >= DEBUG_LEVEL_INFO)
  1288. printk("%s(%d):%s chars_in_buffer()=%d\n",
  1289. __FILE__, __LINE__, info->device_name, info->tx_count);
  1290. return info->tx_count;
  1291. }
  1292. /* Signal remote device to throttle send data (our receive data)
  1293. */
  1294. static void throttle(struct tty_struct * tty)
  1295. {
  1296. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  1297. unsigned long flags;
  1298. if (debug_level >= DEBUG_LEVEL_INFO)
  1299. printk("%s(%d):%s throttle() entry\n",
  1300. __FILE__,__LINE__, info->device_name );
  1301. if (sanity_check(info, tty->name, "throttle"))
  1302. return;
  1303. if (I_IXOFF(tty))
  1304. send_xchar(tty, STOP_CHAR(tty));
  1305. if (tty->termios->c_cflag & CRTSCTS) {
  1306. spin_lock_irqsave(&info->lock,flags);
  1307. info->serial_signals &= ~SerialSignal_RTS;
  1308. set_signals(info);
  1309. spin_unlock_irqrestore(&info->lock,flags);
  1310. }
  1311. }
  1312. /* Signal remote device to stop throttling send data (our receive data)
  1313. */
  1314. static void unthrottle(struct tty_struct * tty)
  1315. {
  1316. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  1317. unsigned long flags;
  1318. if (debug_level >= DEBUG_LEVEL_INFO)
  1319. printk("%s(%d):%s unthrottle() entry\n",
  1320. __FILE__,__LINE__, info->device_name );
  1321. if (sanity_check(info, tty->name, "unthrottle"))
  1322. return;
  1323. if (I_IXOFF(tty)) {
  1324. if (info->x_char)
  1325. info->x_char = 0;
  1326. else
  1327. send_xchar(tty, START_CHAR(tty));
  1328. }
  1329. if (tty->termios->c_cflag & CRTSCTS) {
  1330. spin_lock_irqsave(&info->lock,flags);
  1331. info->serial_signals |= SerialSignal_RTS;
  1332. set_signals(info);
  1333. spin_unlock_irqrestore(&info->lock,flags);
  1334. }
  1335. }
  1336. /* set or clear transmit break condition
  1337. * break_state -1=set break condition, 0=clear
  1338. */
  1339. static void set_break(struct tty_struct *tty, int break_state)
  1340. {
  1341. unsigned char RegValue;
  1342. SLMP_INFO * info = (SLMP_INFO *)tty->driver_data;
  1343. unsigned long flags;
  1344. if (debug_level >= DEBUG_LEVEL_INFO)
  1345. printk("%s(%d):%s set_break(%d)\n",
  1346. __FILE__,__LINE__, info->device_name, break_state);
  1347. if (sanity_check(info, tty->name, "set_break"))
  1348. return;
  1349. spin_lock_irqsave(&info->lock,flags);
  1350. RegValue = read_reg(info, CTL);
  1351. if (break_state == -1)
  1352. RegValue |= BIT3;
  1353. else
  1354. RegValue &= ~BIT3;
  1355. write_reg(info, CTL, RegValue);
  1356. spin_unlock_irqrestore(&info->lock,flags);
  1357. }
  1358. #if SYNCLINK_GENERIC_HDLC
  1359. /**
  1360. * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
  1361. * set encoding and frame check sequence (FCS) options
  1362. *
  1363. * dev pointer to network device structure
  1364. * encoding serial encoding setting
  1365. * parity FCS setting
  1366. *
  1367. * returns 0 if success, otherwise error code
  1368. */
  1369. static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
  1370. unsigned short parity)
  1371. {
  1372. SLMP_INFO *info = dev_to_port(dev);
  1373. unsigned char new_encoding;
  1374. unsigned short new_crctype;
  1375. /* return error if TTY interface open */
  1376. if (info->count)
  1377. return -EBUSY;
  1378. switch (encoding)
  1379. {
  1380. case ENCODING_NRZ: new_encoding = HDLC_ENCODING_NRZ; break;
  1381. case ENCODING_NRZI: new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
  1382. case ENCODING_FM_MARK: new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
  1383. case ENCODING_FM_SPACE: new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
  1384. case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
  1385. default: return -EINVAL;
  1386. }
  1387. switch (parity)
  1388. {
  1389. case PARITY_NONE: new_crctype = HDLC_CRC_NONE; break;
  1390. case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
  1391. case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
  1392. default: return -EINVAL;
  1393. }
  1394. info->params.encoding = new_encoding;
  1395. info->params.crc_type = new_crctype;
  1396. /* if network interface up, reprogram hardware */
  1397. if (info->netcount)
  1398. program_hw(info);
  1399. return 0;
  1400. }
  1401. /**
  1402. * called by generic HDLC layer to send frame
  1403. *
  1404. * skb socket buffer containing HDLC frame
  1405. * dev pointer to network device structure
  1406. *
  1407. * returns 0 if success, otherwise error code
  1408. */
  1409. static int hdlcdev_xmit(struct sk_buff *skb, struct net_device *dev)
  1410. {
  1411. SLMP_INFO *info = dev_to_port(dev);
  1412. struct net_device_stats *stats = hdlc_stats(dev);
  1413. unsigned long flags;
  1414. if (debug_level >= DEBUG_LEVEL_INFO)
  1415. printk(KERN_INFO "%s:hdlc_xmit(%s)\n",__FILE__,dev->name);
  1416. /* stop sending until this frame completes */
  1417. netif_stop_queue(dev);
  1418. /* copy data to device buffers */
  1419. info->tx_count = skb->len;
  1420. tx_load_dma_buffer(info, skb->data, skb->len);
  1421. /* update network statistics */
  1422. stats->tx_packets++;
  1423. stats->tx_bytes += skb->len;
  1424. /* done with socket buffer, so free it */
  1425. dev_kfree_skb(skb);
  1426. /* save start time for transmit timeout detection */
  1427. dev->trans_start = jiffies;
  1428. /* start hardware transmitter if necessary */
  1429. spin_lock_irqsave(&info->lock,flags);
  1430. if (!info->tx_active)
  1431. tx_start(info);
  1432. spin_unlock_irqrestore(&info->lock,flags);
  1433. return 0;
  1434. }
  1435. /**
  1436. * called by network layer when interface enabled
  1437. * claim resources and initialize hardware
  1438. *
  1439. * dev pointer to network device structure
  1440. *
  1441. * returns 0 if success, otherwise error code
  1442. */
  1443. static int hdlcdev_open(struct net_device *dev)
  1444. {
  1445. SLMP_INFO *info = dev_to_port(dev);
  1446. int rc;
  1447. unsigned long flags;
  1448. if (debug_level >= DEBUG_LEVEL_INFO)
  1449. printk("%s:hdlcdev_open(%s)\n",__FILE__,dev->name);
  1450. /* generic HDLC layer open processing */
  1451. if ((rc = hdlc_open(dev)))
  1452. return rc;
  1453. /* arbitrate between network and tty opens */
  1454. spin_lock_irqsave(&info->netlock, flags);
  1455. if (info->count != 0 || info->netcount != 0) {
  1456. printk(KERN_WARNING "%s: hdlc_open returning busy\n", dev->name);
  1457. spin_unlock_irqrestore(&info->netlock, flags);
  1458. return -EBUSY;
  1459. }
  1460. info->netcount=1;
  1461. spin_unlock_irqrestore(&info->netlock, flags);
  1462. /* claim resources and init adapter */
  1463. if ((rc = startup(info)) != 0) {
  1464. spin_lock_irqsave(&info->netlock, flags);
  1465. info->netcount=0;
  1466. spin_unlock_irqrestore(&info->netlock, flags);
  1467. return rc;
  1468. }
  1469. /* assert DTR and RTS, apply hardware settings */
  1470. info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
  1471. program_hw(info);
  1472. /* enable network layer transmit */
  1473. dev->trans_start = jiffies;
  1474. netif_start_queue(dev);
  1475. /* inform generic HDLC layer of current DCD status */
  1476. spin_lock_irqsave(&info->lock, flags);
  1477. get_signals(info);
  1478. spin_unlock_irqrestore(&info->lock, flags);
  1479. if (info->serial_signals & SerialSignal_DCD)
  1480. netif_carrier_on(dev);
  1481. else
  1482. netif_carrier_off(dev);
  1483. return 0;
  1484. }
  1485. /**
  1486. * called by network layer when interface is disabled
  1487. * shutdown hardware and release resources
  1488. *
  1489. * dev pointer to network device structure
  1490. *
  1491. * returns 0 if success, otherwise error code
  1492. */
  1493. static int hdlcdev_close(struct net_device *dev)
  1494. {
  1495. SLMP_INFO *info = dev_to_port(dev);
  1496. unsigned long flags;
  1497. if (debug_level >= DEBUG_LEVEL_INFO)
  1498. printk("%s:hdlcdev_close(%s)\n",__FILE__,dev->name);
  1499. netif_stop_queue(dev);
  1500. /* shutdown adapter and release resources */
  1501. shutdown(info);
  1502. hdlc_close(dev);
  1503. spin_lock_irqsave(&info->netlock, flags);
  1504. info->netcount=0;
  1505. spin_unlock_irqrestore(&info->netlock, flags);
  1506. return 0;
  1507. }
  1508. /**
  1509. * called by network layer to process IOCTL call to network device
  1510. *
  1511. * dev pointer to network device structure
  1512. * ifr pointer to network interface request structure
  1513. * cmd IOCTL command code
  1514. *
  1515. * returns 0 if success, otherwise error code
  1516. */
  1517. static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1518. {
  1519. const size_t size = sizeof(sync_serial_settings);
  1520. sync_serial_settings new_line;
  1521. sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
  1522. SLMP_INFO *info = dev_to_port(dev);
  1523. unsigned int flags;
  1524. if (debug_level >= DEBUG_LEVEL_INFO)
  1525. printk("%s:hdlcdev_ioctl(%s)\n",__FILE__,dev->name);
  1526. /* return error if TTY interface open */
  1527. if (info->count)
  1528. return -EBUSY;
  1529. if (cmd != SIOCWANDEV)
  1530. return hdlc_ioctl(dev, ifr, cmd);
  1531. switch(ifr->ifr_settings.type) {
  1532. case IF_GET_IFACE: /* return current sync_serial_settings */
  1533. ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
  1534. if (ifr->ifr_settings.size < size) {
  1535. ifr->ifr_settings.size = size; /* data size wanted */
  1536. return -ENOBUFS;
  1537. }
  1538. flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
  1539. HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
  1540. HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
  1541. HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
  1542. switch (flags){
  1543. case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
  1544. case (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_INT; break;
  1545. case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_TXINT; break;
  1546. case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
  1547. default: new_line.clock_type = CLOCK_DEFAULT;
  1548. }
  1549. new_line.clock_rate = info->params.clock_speed;
  1550. new_line.loopback = info->params.loopback ? 1:0;
  1551. if (copy_to_user(line, &new_line, size))
  1552. return -EFAULT;
  1553. return 0;
  1554. case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
  1555. if(!capable(CAP_NET_ADMIN))
  1556. return -EPERM;
  1557. if (copy_from_user(&new_line, line, size))
  1558. return -EFAULT;
  1559. switch (new_line.clock_type)
  1560. {
  1561. case CLOCK_EXT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
  1562. case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
  1563. case CLOCK_INT: flags = HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG; break;
  1564. case CLOCK_TXINT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG; break;
  1565. case CLOCK_DEFAULT: flags = info->params.flags &
  1566. (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
  1567. HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
  1568. HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
  1569. HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN); break;
  1570. default: return -EINVAL;
  1571. }
  1572. if (new_line.loopback != 0 && new_line.loopback != 1)
  1573. return -EINVAL;
  1574. info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
  1575. HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
  1576. HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
  1577. HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
  1578. info->params.flags |= flags;
  1579. info->params.loopback = new_line.loopback;
  1580. if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
  1581. info->params.clock_speed = new_line.clock_rate;
  1582. else
  1583. info->params.clock_speed = 0;
  1584. /* if network interface up, reprogram hardware */
  1585. if (info->netcount)
  1586. program_hw(info);
  1587. return 0;
  1588. default:
  1589. return hdlc_ioctl(dev, ifr, cmd);
  1590. }
  1591. }
  1592. /**
  1593. * called by network layer when transmit timeout is detected
  1594. *
  1595. * dev pointer to network device structure
  1596. */
  1597. static void hdlcdev_tx_timeout(struct net_device *dev)
  1598. {
  1599. SLMP_INFO *info = dev_to_port(dev);
  1600. struct net_device_stats *stats = hdlc_stats(dev);
  1601. unsigned long flags;
  1602. if (debug_level >= DEBUG_LEVEL_INFO)
  1603. printk("hdlcdev_tx_timeout(%s)\n",dev->name);
  1604. stats->tx_errors++;
  1605. stats->tx_aborted_errors++;
  1606. spin_lock_irqsave(&info->lock,flags);
  1607. tx_stop(info);
  1608. spin_unlock_irqrestore(&info->lock,flags);
  1609. netif_wake_queue(dev);
  1610. }
  1611. /**
  1612. * called by device driver when transmit completes
  1613. * reenable network layer transmit if stopped
  1614. *
  1615. * info pointer to device instance information
  1616. */
  1617. static void hdlcdev_tx_done(SLMP_INFO *info)
  1618. {
  1619. if (netif_queue_stopped(info->netdev))
  1620. netif_wake_queue(info->netdev);
  1621. }
  1622. /**
  1623. * called by device driver when frame received
  1624. * pass frame to network layer
  1625. *
  1626. * info pointer to device instance information
  1627. * buf pointer to buffer contianing frame data
  1628. * size count of data bytes in buf
  1629. */
  1630. static void hdlcdev_rx(SLMP_INFO *info, char *buf, int size)
  1631. {
  1632. struct sk_buff *skb = dev_alloc_skb(size);
  1633. struct net_device *dev = info->netdev;
  1634. struct net_device_stats *stats = hdlc_stats(dev);
  1635. if (debug_level >= DEBUG_LEVEL_INFO)
  1636. printk("hdlcdev_rx(%s)\n",dev->name);
  1637. if (skb == NULL) {
  1638. printk(KERN_NOTICE "%s: can't alloc skb, dropping packet\n", dev->name);
  1639. stats->rx_dropped++;
  1640. return;
  1641. }
  1642. memcpy(skb_put(skb, size),buf,size);
  1643. skb->protocol = hdlc_type_trans(skb, info->netdev);
  1644. stats->rx_packets++;
  1645. stats->rx_bytes += size;
  1646. netif_rx(skb);
  1647. info->netdev->last_rx = jiffies;
  1648. }
  1649. /**
  1650. * called by device driver when adding device instance
  1651. * do generic HDLC initialization
  1652. *
  1653. * info pointer to device instance information
  1654. *
  1655. * returns 0 if success, otherwise error code
  1656. */
  1657. static int hdlcdev_init(SLMP_INFO *info)
  1658. {
  1659. int rc;
  1660. struct net_device *dev;
  1661. hdlc_device *hdlc;
  1662. /* allocate and initialize network and HDLC layer objects */
  1663. if (!(dev = alloc_hdlcdev(info))) {
  1664. printk(KERN_ERR "%s:hdlc device allocation failure\n",__FILE__);
  1665. return -ENOMEM;
  1666. }
  1667. /* for network layer reporting purposes only */
  1668. dev->mem_start = info->phys_sca_base;
  1669. dev->mem_end = info->phys_sca_base + SCA_BASE_SIZE - 1;
  1670. dev->irq = info->irq_level;
  1671. /* network layer callbacks and settings */
  1672. dev->do_ioctl = hdlcdev_ioctl;
  1673. dev->open = hdlcdev_open;
  1674. dev->stop = hdlcdev_close;
  1675. dev->tx_timeout = hdlcdev_tx_timeout;
  1676. dev->watchdog_timeo = 10*HZ;
  1677. dev->tx_queue_len = 50;
  1678. /* generic HDLC layer callbacks and settings */
  1679. hdlc = dev_to_hdlc(dev);
  1680. hdlc->attach = hdlcdev_attach;
  1681. hdlc->xmit = hdlcdev_xmit;
  1682. /* register objects with HDLC layer */
  1683. if ((rc = register_hdlc_device(dev))) {
  1684. printk(KERN_WARNING "%s:unable to register hdlc device\n",__FILE__);
  1685. free_netdev(dev);
  1686. return rc;
  1687. }
  1688. info->netdev = dev;
  1689. return 0;
  1690. }
  1691. /**
  1692. * called by device driver when removing device instance
  1693. * do generic HDLC cleanup
  1694. *
  1695. * info pointer to device instance information
  1696. */
  1697. static void hdlcdev_exit(SLMP_INFO *info)
  1698. {
  1699. unregister_hdlc_device(info->netdev);
  1700. free_netdev(info->netdev);
  1701. info->netdev = NULL;
  1702. }
  1703. #endif /* CONFIG_HDLC */
  1704. /* Return next bottom half action to perform.
  1705. * Return Value: BH action code or 0 if nothing to do.
  1706. */
  1707. int bh_action(SLMP_INFO *info)
  1708. {
  1709. unsigned long flags;
  1710. int rc = 0;
  1711. spin_lock_irqsave(&info->lock,flags);
  1712. if (info->pending_bh & BH_RECEIVE) {
  1713. info->pending_bh &= ~BH_RECEIVE;
  1714. rc = BH_RECEIVE;
  1715. } else if (info->pending_bh & BH_TRANSMIT) {
  1716. info->pending_bh &= ~BH_TRANSMIT;
  1717. rc = BH_TRANSMIT;
  1718. } else if (info->pending_bh & BH_STATUS) {
  1719. info->pending_bh &= ~BH_STATUS;
  1720. rc = BH_STATUS;
  1721. }
  1722. if (!rc) {
  1723. /* Mark BH routine as complete */
  1724. info->bh_running = 0;
  1725. info->bh_requested = 0;
  1726. }
  1727. spin_unlock_irqrestore(&info->lock,flags);
  1728. return rc;
  1729. }
  1730. /* Perform bottom half processing of work items queued by ISR.
  1731. */
  1732. void bh_handler(struct work_struct *work)
  1733. {
  1734. SLMP_INFO *info = container_of(work, SLMP_INFO, task);
  1735. int action;
  1736. if (!info)
  1737. return;
  1738. if ( debug_level >= DEBUG_LEVEL_BH )
  1739. printk( "%s(%d):%s bh_handler() entry\n",
  1740. __FILE__,__LINE__,info->device_name);
  1741. info->bh_running = 1;
  1742. while((action = bh_action(info)) != 0) {
  1743. /* Process work item */
  1744. if ( debug_level >= DEBUG_LEVEL_BH )
  1745. printk( "%s(%d):%s bh_handler() work item action=%d\n",
  1746. __FILE__,__LINE__,info->device_name, action);
  1747. switch (action) {
  1748. case BH_RECEIVE:
  1749. bh_receive(info);
  1750. break;
  1751. case BH_TRANSMIT:
  1752. bh_transmit(info);
  1753. break;
  1754. case BH_STATUS:
  1755. bh_status(info);
  1756. break;
  1757. default:
  1758. /* unknown work item ID */
  1759. printk("%s(%d):%s Unknown work item ID=%08X!\n",
  1760. __FILE__,__LINE__,info->device_name,action);
  1761. break;
  1762. }
  1763. }
  1764. if ( debug_level >= DEBUG_LEVEL_BH )
  1765. printk( "%s(%d):%s bh_handler() exit\n",
  1766. __FILE__,__LINE__,info->device_name);
  1767. }
  1768. void bh_receive(SLMP_INFO *info)
  1769. {
  1770. if ( debug_level >= DEBUG_LEVEL_BH )
  1771. printk( "%s(%d):%s bh_receive()\n",
  1772. __FILE__,__LINE__,info->device_name);
  1773. while( rx_get_frame(info) );
  1774. }
  1775. void bh_transmit(SLMP_INFO *info)
  1776. {
  1777. struct tty_struct *tty = info->tty;
  1778. if ( debug_level >= DEBUG_LEVEL_BH )
  1779. printk( "%s(%d):%s bh_transmit() entry\n",
  1780. __FILE__,__LINE__,info->device_name);
  1781. if (tty) {
  1782. tty_wakeup(tty);
  1783. wake_up_interruptible(&tty->write_wait);
  1784. }
  1785. }
  1786. void bh_status(SLMP_INFO *info)
  1787. {
  1788. if ( debug_level >= DEBUG_LEVEL_BH )
  1789. printk( "%s(%d):%s bh_status() entry\n",
  1790. __FILE__,__LINE__,info->device_name);
  1791. info->ri_chkcount = 0;
  1792. info->dsr_chkcount = 0;
  1793. info->dcd_chkcount = 0;
  1794. info->cts_chkcount = 0;
  1795. }
  1796. void isr_timer(SLMP_INFO * info)
  1797. {
  1798. unsigned char timer = (info->port_num & 1) ? TIMER2 : TIMER0;
  1799. /* IER2<7..4> = timer<3..0> interrupt enables (0=disabled) */
  1800. write_reg(info, IER2, 0);
  1801. /* TMCS, Timer Control/Status Register
  1802. *
  1803. * 07 CMF, Compare match flag (read only) 1=match
  1804. * 06 ECMI, CMF Interrupt Enable: 0=disabled
  1805. * 05 Reserved, must be 0
  1806. * 04 TME, Timer Enable
  1807. * 03..00 Reserved, must be 0
  1808. *
  1809. * 0000 0000
  1810. */
  1811. write_reg(info, (unsigned char)(timer + TMCS), 0);
  1812. info->irq_occurred = TRUE;
  1813. if ( debug_level >= DEBUG_LEVEL_ISR )
  1814. printk("%s(%d):%s isr_timer()\n",
  1815. __FILE__,__LINE__,info->device_name);
  1816. }
  1817. void isr_rxint(SLMP_INFO * info)
  1818. {
  1819. struct tty_struct *tty = info->tty;
  1820. struct mgsl_icount *icount = &info->icount;
  1821. unsigned char status = read_reg(info, SR1) & info->ie1_value & (FLGD + IDLD + CDCD + BRKD);
  1822. unsigned char status2 = read_reg(info, SR2) & info->ie2_value & OVRN;
  1823. /* clear status bits */
  1824. if (status)
  1825. write_reg(info, SR1, status);
  1826. if (status2)
  1827. write_reg(info, SR2, status2);
  1828. if ( debug_level >= DEBUG_LEVEL_ISR )
  1829. printk("%s(%d):%s isr_rxint status=%02X %02x\n",
  1830. __FILE__,__LINE__,info->device_name,status,status2);
  1831. if (info->params.mode == MGSL_MODE_ASYNC) {
  1832. if (status & BRKD) {
  1833. icount->brk++;
  1834. /* process break detection if tty control
  1835. * is not set to ignore it
  1836. */
  1837. if ( tty ) {
  1838. if (!(status & info->ignore_status_mask1)) {
  1839. if (info->read_status_mask1 & BRKD) {
  1840. tty_insert_flip_char(tty, 0, TTY_BREAK);
  1841. if (info->flags & ASYNC_SAK)
  1842. do_SAK(tty);
  1843. }
  1844. }
  1845. }
  1846. }
  1847. }
  1848. else {
  1849. if (status & (FLGD|IDLD)) {
  1850. if (status & FLGD)
  1851. info->icount.exithunt++;
  1852. else if (status & IDLD)
  1853. info->icount.rxidle++;
  1854. wake_up_interruptible(&info->event_wait_q);
  1855. }
  1856. }
  1857. if (status & CDCD) {
  1858. /* simulate a common modem status change interrupt
  1859. * for our handler
  1860. */
  1861. get_signals( info );
  1862. isr_io_pin(info,
  1863. MISCSTATUS_DCD_LATCHED|(info->serial_signals&SerialSignal_DCD));
  1864. }
  1865. }
  1866. /*
  1867. * handle async rx data interrupts
  1868. */
  1869. void isr_rxrdy(SLMP_INFO * info)
  1870. {
  1871. u16 status;
  1872. unsigned char DataByte;
  1873. struct tty_struct *tty = info->tty;
  1874. struct mgsl_icount *icount = &info->icount;
  1875. if ( debug_level >= DEBUG_LEVEL_ISR )
  1876. printk("%s(%d):%s isr_rxrdy\n",
  1877. __FILE__,__LINE__,info->device_name);
  1878. while((status = read_reg(info,CST0)) & BIT0)
  1879. {
  1880. int flag = 0;
  1881. int over = 0;
  1882. DataByte = read_reg(info,TRB);
  1883. icount->rx++;
  1884. if ( status & (PE + FRME + OVRN) ) {
  1885. printk("%s(%d):%s rxerr=%04X\n",
  1886. __FILE__,__LINE__,info->device_name,status);
  1887. /* update error statistics */
  1888. if (status & PE)
  1889. icount->parity++;
  1890. else if (status & FRME)
  1891. icount->frame++;
  1892. else if (status & OVRN)
  1893. icount->overrun++;
  1894. /* discard char if tty control flags say so */
  1895. if (status & info->ignore_status_mask2)
  1896. continue;
  1897. status &= info->read_status_mask2;
  1898. if ( tty ) {
  1899. if (status & PE)
  1900. flag = TTY_PARITY;
  1901. else if (status & FRME)
  1902. flag = TTY_FRAME;
  1903. if (status & OVRN) {
  1904. /* Overrun is special, since it's
  1905. * reported immediately, and doesn't
  1906. * affect the current character
  1907. */
  1908. over = 1;
  1909. }
  1910. }
  1911. } /* end of if (error) */
  1912. if ( tty ) {
  1913. tty_insert_flip_char(tty, DataByte, flag);
  1914. if (over)
  1915. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  1916. }
  1917. }
  1918. if ( debug_level >= DEBUG_LEVEL_ISR ) {
  1919. printk("%s(%d):%s rx=%d brk=%d parity=%d frame=%d overrun=%d\n",
  1920. __FILE__,__LINE__,info->device_name,
  1921. icount->rx,icount->brk,icount->parity,
  1922. icount->frame,icount->overrun);
  1923. }
  1924. if ( tty )
  1925. tty_flip_buffer_push(tty);
  1926. }
  1927. static void isr_txeom(SLMP_INFO * info, unsigned char status)
  1928. {
  1929. if ( debug_level >= DEBUG_LEVEL_ISR )
  1930. printk("%s(%d):%s isr_txeom status=%02x\n",
  1931. __FILE__,__LINE__,info->device_name,status);
  1932. write_reg(info, TXDMA + DIR, 0x00); /* disable Tx DMA IRQs */
  1933. write_reg(info, TXDMA + DSR, 0xc0); /* clear IRQs and disable DMA */
  1934. write_reg(info, TXDMA + DCMD, SWABORT); /* reset/init DMA channel */
  1935. if (status & UDRN) {
  1936. write_reg(info, CMD, TXRESET);
  1937. write_reg(info, CMD, TXENABLE);
  1938. } else
  1939. write_reg(info, CMD, TXBUFCLR);
  1940. /* disable and clear tx interrupts */
  1941. info->ie0_value &= ~TXRDYE;
  1942. info->ie1_value &= ~(IDLE + UDRN);
  1943. write_reg16(info, IE0, (unsigned short)((info->ie1_value << 8) + info->ie0_value));
  1944. write_reg(info, SR1, (unsigned char)(UDRN + IDLE));
  1945. if ( info->tx_active ) {
  1946. if (info->params.mode != MGSL_MODE_ASYNC) {
  1947. if (status & UDRN)
  1948. info->icount.txunder++;
  1949. else if (status & IDLE)
  1950. info->icount.txok++;
  1951. }
  1952. info->tx_active = 0;
  1953. info->tx_count = info->tx_put = info->tx_get = 0;
  1954. del_timer(&info->tx_timer);
  1955. if (info->params.mode != MGSL_MODE_ASYNC && info->drop_rts_on_tx_done ) {
  1956. info->serial_signals &= ~SerialSignal_RTS;
  1957. info->drop_rts_on_tx_done = 0;
  1958. set_signals(info);
  1959. }
  1960. #if SYNCLINK_GENERIC_HDLC
  1961. if (info->netcount)
  1962. hdlcdev_tx_done(info);
  1963. else
  1964. #endif
  1965. {
  1966. if (info->tty && (info->tty->stopped || info->tty->hw_stopped)) {
  1967. tx_stop(info);
  1968. return;
  1969. }
  1970. info->pending_bh |= BH_TRANSMIT;
  1971. }
  1972. }
  1973. }
  1974. /*
  1975. * handle tx status interrupts
  1976. */
  1977. void isr_txint(SLMP_INFO * info)
  1978. {
  1979. unsigned char status = read_reg(info, SR1) & info->ie1_value & (UDRN + IDLE + CCTS);
  1980. /* clear status bits */
  1981. write_reg(info, SR1, status);
  1982. if ( debug_level >= DEBUG_LEVEL_ISR )
  1983. printk("%s(%d):%s isr_txint status=%02x\n",
  1984. __FILE__,__LINE__,info->device_name,status);
  1985. if (status & (UDRN + IDLE))
  1986. isr_txeom(info, status);
  1987. if (status & CCTS) {
  1988. /* simulate a common modem status change interrupt
  1989. * for our handler
  1990. */
  1991. get_signals( info );
  1992. isr_io_pin(info,
  1993. MISCSTATUS_CTS_LATCHED|(info->serial_signals&SerialSignal_CTS));
  1994. }
  1995. }
  1996. /*
  1997. * handle async tx data interrupts
  1998. */
  1999. void isr_txrdy(SLMP_INFO * info)
  2000. {
  2001. if ( debug_level >= DEBUG_LEVEL_ISR )
  2002. printk("%s(%d):%s isr_txrdy() tx_count=%d\n",
  2003. __FILE__,__LINE__,info->device_name,info->tx_count);
  2004. if (info->params.mode != MGSL_MODE_ASYNC) {
  2005. /* disable TXRDY IRQ, enable IDLE IRQ */
  2006. info->ie0_value &= ~TXRDYE;
  2007. info->ie1_value |= IDLE;
  2008. write_reg16(info, IE0, (unsigned short)((info->ie1_value << 8) + info->ie0_value));
  2009. return;
  2010. }
  2011. if (info->tty && (info->tty->stopped || info->tty->hw_stopped)) {
  2012. tx_stop(info);
  2013. return;
  2014. }
  2015. if ( info->tx_count )
  2016. tx_load_fifo( info );
  2017. else {
  2018. info->tx_active = 0;
  2019. info->ie0_value &= ~TXRDYE;
  2020. write_reg(info, IE0, info->ie0_value);
  2021. }
  2022. if (info->tx_count < WAKEUP_CHARS)
  2023. info->pending_bh |= BH_TRANSMIT;
  2024. }
  2025. void isr_rxdmaok(SLMP_INFO * info)
  2026. {
  2027. /* BIT7 = EOT (end of transfer)
  2028. * BIT6 = EOM (end of message/frame)
  2029. */
  2030. unsigned char status = read_reg(info,RXDMA + DSR) & 0xc0;
  2031. /* clear IRQ (BIT0 must be 1 to prevent clearing DE bit) */
  2032. write_reg(info, RXDMA + DSR, (unsigned char)(status | 1));
  2033. if ( debug_level >= DEBUG_LEVEL_ISR )
  2034. printk("%s(%d):%s isr_rxdmaok(), status=%02x\n",
  2035. __FILE__,__LINE__,info->device_name,status);
  2036. info->pending_bh |= BH_RECEIVE;
  2037. }
  2038. void isr_rxdmaerror(SLMP_INFO * info)
  2039. {
  2040. /* BIT5 = BOF (buffer overflow)
  2041. * BIT4 = COF (counter overflow)
  2042. */
  2043. unsigned char status = read_reg(info,RXDMA + DSR) & 0x30;
  2044. /* clear IRQ (BIT0 must be 1 to prevent clearing DE bit) */
  2045. write_reg(info, RXDMA + DSR, (unsigned char)(status | 1));
  2046. if ( debug_level >= DEBUG_LEVEL_ISR )
  2047. printk("%s(%d):%s isr_rxdmaerror(), status=%02x\n",
  2048. __FILE__,__LINE__,info->device_name,status);
  2049. info->rx_overflow = TRUE;
  2050. info->pending_bh |= BH_RECEIVE;
  2051. }
  2052. void isr_txdmaok(SLMP_INFO * info)
  2053. {
  2054. unsigned char status_reg1 = read_reg(info, SR1);
  2055. write_reg(info, TXDMA + DIR, 0x00); /* disable Tx DMA IRQs */
  2056. write_reg(info, TXDMA + DSR, 0xc0); /* clear IRQs and disable DMA */
  2057. write_reg(info, TXDMA + DCMD, SWABORT); /* reset/init DMA channel */
  2058. if ( debug_level >= DEBUG_LEVEL_ISR )
  2059. printk("%s(%d):%s isr_txdmaok(), status=%02x\n",
  2060. __FILE__,__LINE__,info->device_name,status_reg1);
  2061. /* program TXRDY as FIFO empty flag, enable TXRDY IRQ */
  2062. write_reg16(info, TRC0, 0);
  2063. info->ie0_value |= TXRDYE;
  2064. write_reg(info, IE0, info->ie0_value);
  2065. }
  2066. void isr_txdmaerror(SLMP_INFO * info)
  2067. {
  2068. /* BIT5 = BOF (buffer overflow)
  2069. * BIT4 = COF (counter overflow)
  2070. */
  2071. unsigned char status = read_reg(info,TXDMA + DSR) & 0x30;
  2072. /* clear IRQ (BIT0 must be 1 to prevent clearing DE bit) */
  2073. write_reg(info, TXDMA + DSR, (unsigned char)(status | 1));
  2074. if ( debug_level >= DEBUG_LEVEL_ISR )
  2075. printk("%s(%d):%s isr_txdmaerror(), status=%02x\n",
  2076. __FILE__,__LINE__,info->device_name,status);
  2077. }
  2078. /* handle input serial signal changes
  2079. */
  2080. void isr_io_pin( SLMP_INFO *info, u16 status )
  2081. {
  2082. struct mgsl_icount *icount;
  2083. if ( debug_level >= DEBUG_LEVEL_ISR )
  2084. printk("%s(%d):isr_io_pin status=%04X\n",
  2085. __FILE__,__LINE__,status);
  2086. if (status & (MISCSTATUS_CTS_LATCHED | MISCSTATUS_DCD_LATCHED |
  2087. MISCSTATUS_DSR_LATCHED | MISCSTATUS_RI_LATCHED) ) {
  2088. icount = &info->icount;
  2089. /* update input line counters */
  2090. if (status & MISCSTATUS_RI_LATCHED) {
  2091. icount->rng++;
  2092. if ( status & SerialSignal_RI )
  2093. info->input_signal_events.ri_up++;
  2094. else
  2095. info->input_signal_events.ri_down++;
  2096. }
  2097. if (status & MISCSTATUS_DSR_LATCHED) {
  2098. icount->dsr++;
  2099. if ( status & SerialSignal_DSR )
  2100. info->input_signal_events.dsr_up++;
  2101. else
  2102. info->input_signal_events.dsr_down++;
  2103. }
  2104. if (status & MISCSTATUS_DCD_LATCHED) {
  2105. if ((info->dcd_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT) {
  2106. info->ie1_value &= ~CDCD;
  2107. write_reg(info, IE1, info->ie1_value);
  2108. }
  2109. icount->dcd++;
  2110. if (status & SerialSignal_DCD) {
  2111. info->input_signal_events.dcd_up++;
  2112. } else
  2113. info->input_signal_events.dcd_down++;
  2114. #if SYNCLINK_GENERIC_HDLC
  2115. if (info->netcount) {
  2116. if (status & SerialSignal_DCD)
  2117. netif_carrier_on(info->netdev);
  2118. else
  2119. netif_carrier_off(info->netdev);
  2120. }
  2121. #endif
  2122. }
  2123. if (status & MISCSTATUS_CTS_LATCHED)
  2124. {
  2125. if ((info->cts_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT) {
  2126. info->ie1_value &= ~CCTS;
  2127. write_reg(info, IE1, info->ie1_value);
  2128. }
  2129. icount->cts++;
  2130. if ( status & SerialSignal_CTS )
  2131. info->input_signal_events.cts_up++;
  2132. else
  2133. info->input_signal_events.cts_down++;
  2134. }
  2135. wake_up_interruptible(&info->status_event_wait_q);
  2136. wake_up_interruptible(&info->event_wait_q);
  2137. if ( (info->flags & ASYNC_CHECK_CD) &&
  2138. (status & MISCSTATUS_DCD_LATCHED) ) {
  2139. if ( debug_level >= DEBUG_LEVEL_ISR )
  2140. printk("%s CD now %s...", info->device_name,
  2141. (status & SerialSignal_DCD) ? "on" : "off");
  2142. if (status & SerialSignal_DCD)
  2143. wake_up_interruptible(&info->open_wait);
  2144. else {
  2145. if ( debug_level >= DEBUG_LEVEL_ISR )
  2146. printk("doing serial hangup...");
  2147. if (info->tty)
  2148. tty_hangup(info->tty);
  2149. }
  2150. }
  2151. if ( (info->flags & ASYNC_CTS_FLOW) &&
  2152. (status & MISCSTATUS_CTS_LATCHED) ) {
  2153. if ( info->tty ) {
  2154. if (info->tty->hw_stopped) {
  2155. if (status & SerialSignal_CTS) {
  2156. if ( debug_level >= DEBUG_LEVEL_ISR )
  2157. printk("CTS tx start...");
  2158. info->tty->hw_stopped = 0;
  2159. tx_start(info);
  2160. info->pending_bh |= BH_TRANSMIT;
  2161. return;
  2162. }
  2163. } else {
  2164. if (!(status & SerialSignal_CTS)) {
  2165. if ( debug_level >= DEBUG_LEVEL_ISR )
  2166. printk("CTS tx stop...");
  2167. info->tty->hw_stopped = 1;
  2168. tx_stop(info);
  2169. }
  2170. }
  2171. }
  2172. }
  2173. }
  2174. info->pending_bh |= BH_STATUS;
  2175. }
  2176. /* Interrupt service routine entry point.
  2177. *
  2178. * Arguments:
  2179. * irq interrupt number that caused interrupt
  2180. * dev_id device ID supplied during interrupt registration
  2181. * regs interrupted processor context
  2182. */
  2183. static irqreturn_t synclinkmp_interrupt(int irq, void *dev_id)
  2184. {
  2185. SLMP_INFO * info;
  2186. unsigned char status, status0, status1=0;
  2187. unsigned char dmastatus, dmastatus0, dmastatus1=0;
  2188. unsigned char timerstatus0, timerstatus1=0;
  2189. unsigned char shift;
  2190. unsigned int i;
  2191. unsigned short tmp;
  2192. if ( debug_level >= DEBUG_LEVEL_ISR )
  2193. printk("%s(%d): synclinkmp_interrupt(%d)entry.\n",
  2194. __FILE__,__LINE__,irq);
  2195. info = (SLMP_INFO *)dev_id;
  2196. if (!info)
  2197. return IRQ_NONE;
  2198. spin_lock(&info->lock);
  2199. for(;;) {
  2200. /* get status for SCA0 (ports 0-1) */
  2201. tmp = read_reg16(info, ISR0); /* get ISR0 and ISR1 in one read */
  2202. status0 = (unsigned char)tmp;
  2203. dmastatus0 = (unsigned char)(tmp>>8);
  2204. timerstatus0 = read_reg(info, ISR2);
  2205. if ( debug_level >= DEBUG_LEVEL_ISR )
  2206. printk("%s(%d):%s status0=%02x, dmastatus0=%02x, timerstatus0=%02x\n",
  2207. __FILE__,__LINE__,info->device_name,
  2208. status0,dmastatus0,timerstatus0);
  2209. if (info->port_count == 4) {
  2210. /* get status for SCA1 (ports 2-3) */
  2211. tmp = read_reg16(info->port_array[2], ISR0);
  2212. status1 = (unsigned char)tmp;
  2213. dmastatus1 = (unsigned char)(tmp>>8);
  2214. timerstatus1 = read_reg(info->port_array[2], ISR2);
  2215. if ( debug_level >= DEBUG_LEVEL_ISR )
  2216. printk("%s(%d):%s status1=%02x, dmastatus1=%02x, timerstatus1=%02x\n",
  2217. __FILE__,__LINE__,info->device_name,
  2218. status1,dmastatus1,timerstatus1);
  2219. }
  2220. if (!status0 && !dmastatus0 && !timerstatus0 &&
  2221. !status1 && !dmastatus1 && !timerstatus1)
  2222. break;
  2223. for(i=0; i < info->port_count ; i++) {
  2224. if (info->port_array[i] == NULL)
  2225. continue;
  2226. if (i < 2) {
  2227. status = status0;
  2228. dmastatus = dmastatus0;
  2229. } else {
  2230. status = status1;
  2231. dmastatus = dmastatus1;
  2232. }
  2233. shift = i & 1 ? 4 :0;
  2234. if (status & BIT0 << shift)
  2235. isr_rxrdy(info->port_array[i]);
  2236. if (status & BIT1 << shift)
  2237. isr_txrdy(info->port_array[i]);
  2238. if (status & BIT2 << shift)
  2239. isr_rxint(info->port_array[i]);
  2240. if (status & BIT3 << shift)
  2241. isr_txint(info->port_array[i]);
  2242. if (dmastatus & BIT0 << shift)
  2243. isr_rxdmaerror(info->port_array[i]);
  2244. if (dmastatus & BIT1 << shift)
  2245. isr_rxdmaok(info->port_array[i]);
  2246. if (dmastatus & BIT2 << shift)
  2247. isr_txdmaerror(info->port_array[i]);
  2248. if (dmastatus & BIT3 << shift)
  2249. isr_txdmaok(info->port_array[i]);
  2250. }
  2251. if (timerstatus0 & (BIT5 | BIT4))
  2252. isr_timer(info->port_array[0]);
  2253. if (timerstatus0 & (BIT7 | BIT6))
  2254. isr_timer(info->port_array[1]);
  2255. if (timerstatus1 & (BIT5 | BIT4))
  2256. isr_timer(info->port_array[2]);
  2257. if (timerstatus1 & (BIT7 | BIT6))
  2258. isr_timer(info->port_array[3]);
  2259. }
  2260. for(i=0; i < info->port_count ; i++) {
  2261. SLMP_INFO * port = info->port_array[i];
  2262. /* Request bottom half processing if there's something
  2263. * for it to do and the bh is not already running.
  2264. *
  2265. * Note: startup adapter diags require interrupts.
  2266. * do not request bottom half processing if the
  2267. * device is not open in a normal mode.
  2268. */
  2269. if ( port && (port->count || port->netcount) &&
  2270. port->pending_bh && !port->bh_running &&
  2271. !port->bh_requested ) {
  2272. if ( debug_level >= DEBUG_LEVEL_ISR )
  2273. printk("%s(%d):%s queueing bh task.\n",
  2274. __FILE__,__LINE__,port->device_name);
  2275. schedule_work(&port->task);
  2276. port->bh_requested = 1;
  2277. }
  2278. }
  2279. spin_unlock(&info->lock);
  2280. if ( debug_level >= DEBUG_LEVEL_ISR )
  2281. printk("%s(%d):synclinkmp_interrupt(%d)exit.\n",
  2282. __FILE__,__LINE__,irq);
  2283. return IRQ_HANDLED;
  2284. }
  2285. /* Initialize and start device.
  2286. */
  2287. static int startup(SLMP_INFO * info)
  2288. {
  2289. if ( debug_level >= DEBUG_LEVEL_INFO )
  2290. printk("%s(%d):%s tx_releaseup()\n",__FILE__,__LINE__,info->device_name);
  2291. if (info->flags & ASYNC_INITIALIZED)
  2292. return 0;
  2293. if (!info->tx_buf) {
  2294. info->tx_buf = kmalloc(info->max_frame_size, GFP_KERNEL);
  2295. if (!info->tx_buf) {
  2296. printk(KERN_ERR"%s(%d):%s can't allocate transmit buffer\n",
  2297. __FILE__,__LINE__,info->device_name);
  2298. return -ENOMEM;
  2299. }
  2300. }
  2301. info->pending_bh = 0;
  2302. memset(&info->icount, 0, sizeof(info->icount));
  2303. /* program hardware for current parameters */
  2304. reset_port(info);
  2305. change_params(info);
  2306. info->status_timer.expires = jiffies + msecs_to_jiffies(10);
  2307. add_timer(&info->status_timer);
  2308. if (info->tty)
  2309. clear_bit(TTY_IO_ERROR, &info->tty->flags);
  2310. info->flags |= ASYNC_INITIALIZED;
  2311. return 0;
  2312. }
  2313. /* Called by close() and hangup() to shutdown hardware
  2314. */
  2315. static void shutdown(SLMP_INFO * info)
  2316. {
  2317. unsigned long flags;
  2318. if (!(info->flags & ASYNC_INITIALIZED))
  2319. return;
  2320. if (debug_level >= DEBUG_LEVEL_INFO)
  2321. printk("%s(%d):%s synclinkmp_shutdown()\n",
  2322. __FILE__,__LINE__, info->device_name );
  2323. /* clear status wait queue because status changes */
  2324. /* can't happen after shutting down the hardware */
  2325. wake_up_interruptible(&info->status_event_wait_q);
  2326. wake_up_interruptible(&info->event_wait_q);
  2327. del_timer(&info->tx_timer);
  2328. del_timer(&info->status_timer);
  2329. kfree(info->tx_buf);
  2330. info->tx_buf = NULL;
  2331. spin_lock_irqsave(&info->lock,flags);
  2332. reset_port(info);
  2333. if (!info->tty || info->tty->termios->c_cflag & HUPCL) {
  2334. info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
  2335. set_signals(info);
  2336. }
  2337. spin_unlock_irqrestore(&info->lock,flags);
  2338. if (info->tty)
  2339. set_bit(TTY_IO_ERROR, &info->tty->flags);
  2340. info->flags &= ~ASYNC_INITIALIZED;
  2341. }
  2342. static void program_hw(SLMP_INFO *info)
  2343. {
  2344. unsigned long flags;
  2345. spin_lock_irqsave(&info->lock,flags);
  2346. rx_stop(info);
  2347. tx_stop(info);
  2348. info->tx_count = info->tx_put = info->tx_get = 0;
  2349. if (info->params.mode == MGSL_MODE_HDLC || info->netcount)
  2350. hdlc_mode(info);
  2351. else
  2352. async_mode(info);
  2353. set_signals(info);
  2354. info->dcd_chkcount = 0;
  2355. info->cts_chkcount = 0;
  2356. info->ri_chkcount = 0;
  2357. info->dsr_chkcount = 0;
  2358. info->ie1_value |= (CDCD|CCTS);
  2359. write_reg(info, IE1, info->ie1_value);
  2360. get_signals(info);
  2361. if (info->netcount || (info->tty && info->tty->termios->c_cflag & CREAD) )
  2362. rx_start(info);
  2363. spin_unlock_irqrestore(&info->lock,flags);
  2364. }
  2365. /* Reconfigure adapter based on new parameters
  2366. */
  2367. static void change_params(SLMP_INFO *info)
  2368. {
  2369. unsigned cflag;
  2370. int bits_per_char;
  2371. if (!info->tty || !info->tty->termios)
  2372. return;
  2373. if (debug_level >= DEBUG_LEVEL_INFO)
  2374. printk("%s(%d):%s change_params()\n",
  2375. __FILE__,__LINE__, info->device_name );
  2376. cflag = info->tty->termios->c_cflag;
  2377. /* if B0 rate (hangup) specified then negate DTR and RTS */
  2378. /* otherwise assert DTR and RTS */
  2379. if (cflag & CBAUD)
  2380. info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
  2381. else
  2382. info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
  2383. /* byte size and parity */
  2384. switch (cflag & CSIZE) {
  2385. case CS5: info->params.data_bits = 5; break;
  2386. case CS6: info->params.data_bits = 6; break;
  2387. case CS7: info->params.data_bits = 7; break;
  2388. case CS8: info->params.data_bits = 8; break;
  2389. /* Never happens, but GCC is too dumb to figure it out */
  2390. default: info->params.data_bits = 7; break;
  2391. }
  2392. if (cflag & CSTOPB)
  2393. info->params.stop_bits = 2;
  2394. else
  2395. info->params.stop_bits = 1;
  2396. info->params.parity = ASYNC_PARITY_NONE;
  2397. if (cflag & PARENB) {
  2398. if (cflag & PARODD)
  2399. info->params.parity = ASYNC_PARITY_ODD;
  2400. else
  2401. info->params.parity = ASYNC_PARITY_EVEN;
  2402. #ifdef CMSPAR
  2403. if (cflag & CMSPAR)
  2404. info->params.parity = ASYNC_PARITY_SPACE;
  2405. #endif
  2406. }
  2407. /* calculate number of jiffies to transmit a full
  2408. * FIFO (32 bytes) at specified data rate
  2409. */
  2410. bits_per_char = info->params.data_bits +
  2411. info->params.stop_bits + 1;
  2412. /* if port data rate is set to 460800 or less then
  2413. * allow tty settings to override, otherwise keep the
  2414. * current data rate.
  2415. */
  2416. if (info->params.data_rate <= 460800) {
  2417. info->params.data_rate = tty_get_baud_rate(info->tty);
  2418. }
  2419. if ( info->params.data_rate ) {
  2420. info->timeout = (32*HZ*bits_per_char) /
  2421. info->params.data_rate;
  2422. }
  2423. info->timeout += HZ/50; /* Add .02 seconds of slop */
  2424. if (cflag & CRTSCTS)
  2425. info->flags |= ASYNC_CTS_FLOW;
  2426. else
  2427. info->flags &= ~ASYNC_CTS_FLOW;
  2428. if (cflag & CLOCAL)
  2429. info->flags &= ~ASYNC_CHECK_CD;
  2430. else
  2431. info->flags |= ASYNC_CHECK_CD;
  2432. /* process tty input control flags */
  2433. info->read_status_mask2 = OVRN;
  2434. if (I_INPCK(info->tty))
  2435. info->read_status_mask2 |= PE | FRME;
  2436. if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
  2437. info->read_status_mask1 |= BRKD;
  2438. if (I_IGNPAR(info->tty))
  2439. info->ignore_status_mask2 |= PE | FRME;
  2440. if (I_IGNBRK(info->tty)) {
  2441. info->ignore_status_mask1 |= BRKD;
  2442. /* If ignoring parity and break indicators, ignore
  2443. * overruns too. (For real raw support).
  2444. */
  2445. if (I_IGNPAR(info->tty))
  2446. info->ignore_status_mask2 |= OVRN;
  2447. }
  2448. program_hw(info);
  2449. }
  2450. static int get_stats(SLMP_INFO * info, struct mgsl_icount __user *user_icount)
  2451. {
  2452. int err;
  2453. if (debug_level >= DEBUG_LEVEL_INFO)
  2454. printk("%s(%d):%s get_params()\n",
  2455. __FILE__,__LINE__, info->device_name);
  2456. if (!user_icount) {
  2457. memset(&info->icount, 0, sizeof(info->icount));
  2458. } else {
  2459. COPY_TO_USER(err, user_icount, &info->icount, sizeof(struct mgsl_icount));
  2460. if (err)
  2461. return -EFAULT;
  2462. }
  2463. return 0;
  2464. }
  2465. static int get_params(SLMP_INFO * info, MGSL_PARAMS __user *user_params)
  2466. {
  2467. int err;
  2468. if (debug_level >= DEBUG_LEVEL_INFO)
  2469. printk("%s(%d):%s get_params()\n",
  2470. __FILE__,__LINE__, info->device_name);
  2471. COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS));
  2472. if (err) {
  2473. if ( debug_level >= DEBUG_LEVEL_INFO )
  2474. printk( "%s(%d):%s get_params() user buffer copy failed\n",
  2475. __FILE__,__LINE__,info->device_name);
  2476. return -EFAULT;
  2477. }
  2478. return 0;
  2479. }
  2480. static int set_params(SLMP_INFO * info, MGSL_PARAMS __user *new_params)
  2481. {
  2482. unsigned long flags;
  2483. MGSL_PARAMS tmp_params;
  2484. int err;
  2485. if (debug_level >= DEBUG_LEVEL_INFO)
  2486. printk("%s(%d):%s set_params\n",
  2487. __FILE__,__LINE__,info->device_name );
  2488. COPY_FROM_USER(err,&tmp_params, new_params, sizeof(MGSL_PARAMS));
  2489. if (err) {
  2490. if ( debug_level >= DEBUG_LEVEL_INFO )
  2491. printk( "%s(%d):%s set_params() user buffer copy failed\n",
  2492. __FILE__,__LINE__,info->device_name);
  2493. return -EFAULT;
  2494. }
  2495. spin_lock_irqsave(&info->lock,flags);
  2496. memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
  2497. spin_unlock_irqrestore(&info->lock,flags);
  2498. change_params(info);
  2499. return 0;
  2500. }
  2501. static int get_txidle(SLMP_INFO * info, int __user *idle_mode)
  2502. {
  2503. int err;
  2504. if (debug_level >= DEBUG_LEVEL_INFO)
  2505. printk("%s(%d):%s get_txidle()=%d\n",
  2506. __FILE__,__LINE__, info->device_name, info->idle_mode);
  2507. COPY_TO_USER(err,idle_mode, &info->idle_mode, sizeof(int));
  2508. if (err) {
  2509. if ( debug_level >= DEBUG_LEVEL_INFO )
  2510. printk( "%s(%d):%s get_txidle() user buffer copy failed\n",
  2511. __FILE__,__LINE__,info->device_name);
  2512. return -EFAULT;
  2513. }
  2514. return 0;
  2515. }
  2516. static int set_txidle(SLMP_INFO * info, int idle_mode)
  2517. {
  2518. unsigned long flags;
  2519. if (debug_level >= DEBUG_LEVEL_INFO)
  2520. printk("%s(%d):%s set_txidle(%d)\n",
  2521. __FILE__,__LINE__,info->device_name, idle_mode );
  2522. spin_lock_irqsave(&info->lock,flags);
  2523. info->idle_mode = idle_mode;
  2524. tx_set_idle( info );
  2525. spin_unlock_irqrestore(&info->lock,flags);
  2526. return 0;
  2527. }
  2528. static int tx_enable(SLMP_INFO * info, int enable)
  2529. {
  2530. unsigned long flags;
  2531. if (debug_level >= DEBUG_LEVEL_INFO)
  2532. printk("%s(%d):%s tx_enable(%d)\n",
  2533. __FILE__,__LINE__,info->device_name, enable);
  2534. spin_lock_irqsave(&info->lock,flags);
  2535. if ( enable ) {
  2536. if ( !info->tx_enabled ) {
  2537. tx_start(info);
  2538. }
  2539. } else {
  2540. if ( info->tx_enabled )
  2541. tx_stop(info);
  2542. }
  2543. spin_unlock_irqrestore(&info->lock,flags);
  2544. return 0;
  2545. }
  2546. /* abort send HDLC frame
  2547. */
  2548. static int tx_abort(SLMP_INFO * info)
  2549. {
  2550. unsigned long flags;
  2551. if (debug_level >= DEBUG_LEVEL_INFO)
  2552. printk("%s(%d):%s tx_abort()\n",
  2553. __FILE__,__LINE__,info->device_name);
  2554. spin_lock_irqsave(&info->lock,flags);
  2555. if ( info->tx_active && info->params.mode == MGSL_MODE_HDLC ) {
  2556. info->ie1_value &= ~UDRN;
  2557. info->ie1_value |= IDLE;
  2558. write_reg(info, IE1, info->ie1_value); /* disable tx status interrupts */
  2559. write_reg(info, SR1, (unsigned char)(IDLE + UDRN)); /* clear pending */
  2560. write_reg(info, TXDMA + DSR, 0); /* disable DMA channel */
  2561. write_reg(info, TXDMA + DCMD, SWABORT); /* reset/init DMA channel */
  2562. write_reg(info, CMD, TXABORT);
  2563. }
  2564. spin_unlock_irqrestore(&info->lock,flags);
  2565. return 0;
  2566. }
  2567. static int rx_enable(SLMP_INFO * info, int enable)
  2568. {
  2569. unsigned long flags;
  2570. if (debug_level >= DEBUG_LEVEL_INFO)
  2571. printk("%s(%d):%s rx_enable(%d)\n",
  2572. __FILE__,__LINE__,info->device_name,enable);
  2573. spin_lock_irqsave(&info->lock,flags);
  2574. if ( enable ) {
  2575. if ( !info->rx_enabled )
  2576. rx_start(info);
  2577. } else {
  2578. if ( info->rx_enabled )
  2579. rx_stop(info);
  2580. }
  2581. spin_unlock_irqrestore(&info->lock,flags);
  2582. return 0;
  2583. }
  2584. /* wait for specified event to occur
  2585. */
  2586. static int wait_mgsl_event(SLMP_INFO * info, int __user *mask_ptr)
  2587. {
  2588. unsigned long flags;
  2589. int s;
  2590. int rc=0;
  2591. struct mgsl_icount cprev, cnow;
  2592. int events;
  2593. int mask;
  2594. struct _input_signal_events oldsigs, newsigs;
  2595. DECLARE_WAITQUEUE(wait, current);
  2596. COPY_FROM_USER(rc,&mask, mask_ptr, sizeof(int));
  2597. if (rc) {
  2598. return -EFAULT;
  2599. }
  2600. if (debug_level >= DEBUG_LEVEL_INFO)
  2601. printk("%s(%d):%s wait_mgsl_event(%d)\n",
  2602. __FILE__,__LINE__,info->device_name,mask);
  2603. spin_lock_irqsave(&info->lock,flags);
  2604. /* return immediately if state matches requested events */
  2605. get_signals(info);
  2606. s = info->serial_signals;
  2607. events = mask &
  2608. ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
  2609. ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
  2610. ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
  2611. ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
  2612. if (events) {
  2613. spin_unlock_irqrestore(&info->lock,flags);
  2614. goto exit;
  2615. }
  2616. /* save current irq counts */
  2617. cprev = info->icount;
  2618. oldsigs = info->input_signal_events;
  2619. /* enable hunt and idle irqs if needed */
  2620. if (mask & (MgslEvent_ExitHuntMode+MgslEvent_IdleReceived)) {
  2621. unsigned char oldval = info->ie1_value;
  2622. unsigned char newval = oldval +
  2623. (mask & MgslEvent_ExitHuntMode ? FLGD:0) +
  2624. (mask & MgslEvent_IdleReceived ? IDLD:0);
  2625. if ( oldval != newval ) {
  2626. info->ie1_value = newval;
  2627. write_reg(info, IE1, info->ie1_value);
  2628. }
  2629. }
  2630. set_current_state(TASK_INTERRUPTIBLE);
  2631. add_wait_queue(&info->event_wait_q, &wait);
  2632. spin_unlock_irqrestore(&info->lock,flags);
  2633. for(;;) {
  2634. schedule();
  2635. if (signal_pending(current)) {
  2636. rc = -ERESTARTSYS;
  2637. break;
  2638. }
  2639. /* get current irq counts */
  2640. spin_lock_irqsave(&info->lock,flags);
  2641. cnow = info->icount;
  2642. newsigs = info->input_signal_events;
  2643. set_current_state(TASK_INTERRUPTIBLE);
  2644. spin_unlock_irqrestore(&info->lock,flags);
  2645. /* if no change, wait aborted for some reason */
  2646. if (newsigs.dsr_up == oldsigs.dsr_up &&
  2647. newsigs.dsr_down == oldsigs.dsr_down &&
  2648. newsigs.dcd_up == oldsigs.dcd_up &&
  2649. newsigs.dcd_down == oldsigs.dcd_down &&
  2650. newsigs.cts_up == oldsigs.cts_up &&
  2651. newsigs.cts_down == oldsigs.cts_down &&
  2652. newsigs.ri_up == oldsigs.ri_up &&
  2653. newsigs.ri_down == oldsigs.ri_down &&
  2654. cnow.exithunt == cprev.exithunt &&
  2655. cnow.rxidle == cprev.rxidle) {
  2656. rc = -EIO;
  2657. break;
  2658. }
  2659. events = mask &
  2660. ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
  2661. (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
  2662. (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
  2663. (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
  2664. (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
  2665. (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
  2666. (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
  2667. (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
  2668. (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
  2669. (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
  2670. if (events)
  2671. break;
  2672. cprev = cnow;
  2673. oldsigs = newsigs;
  2674. }
  2675. remove_wait_queue(&info->event_wait_q, &wait);
  2676. set_current_state(TASK_RUNNING);
  2677. if (mask & (MgslEvent_ExitHuntMode + MgslEvent_IdleReceived)) {
  2678. spin_lock_irqsave(&info->lock,flags);
  2679. if (!waitqueue_active(&info->event_wait_q)) {
  2680. /* disable enable exit hunt mode/idle rcvd IRQs */
  2681. info->ie1_value &= ~(FLGD|IDLD);
  2682. write_reg(info, IE1, info->ie1_value);
  2683. }
  2684. spin_unlock_irqrestore(&info->lock,flags);
  2685. }
  2686. exit:
  2687. if ( rc == 0 )
  2688. PUT_USER(rc, events, mask_ptr);
  2689. return rc;
  2690. }
  2691. static int modem_input_wait(SLMP_INFO *info,int arg)
  2692. {
  2693. unsigned long flags;
  2694. int rc;
  2695. struct mgsl_icount cprev, cnow;
  2696. DECLARE_WAITQUEUE(wait, current);
  2697. /* save current irq counts */
  2698. spin_lock_irqsave(&info->lock,flags);
  2699. cprev = info->icount;
  2700. add_wait_queue(&info->status_event_wait_q, &wait);
  2701. set_current_state(TASK_INTERRUPTIBLE);
  2702. spin_unlock_irqrestore(&info->lock,flags);
  2703. for(;;) {
  2704. schedule();
  2705. if (signal_pending(current)) {
  2706. rc = -ERESTARTSYS;
  2707. break;
  2708. }
  2709. /* get new irq counts */
  2710. spin_lock_irqsave(&info->lock,flags);
  2711. cnow = info->icount;
  2712. set_current_state(TASK_INTERRUPTIBLE);
  2713. spin_unlock_irqrestore(&info->lock,flags);
  2714. /* if no change, wait aborted for some reason */
  2715. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  2716. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
  2717. rc = -EIO;
  2718. break;
  2719. }
  2720. /* check for change in caller specified modem input */
  2721. if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
  2722. (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
  2723. (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
  2724. (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
  2725. rc = 0;
  2726. break;
  2727. }
  2728. cprev = cnow;
  2729. }
  2730. remove_wait_queue(&info->status_event_wait_q, &wait);
  2731. set_current_state(TASK_RUNNING);
  2732. return rc;
  2733. }
  2734. /* return the state of the serial control and status signals
  2735. */
  2736. static int tiocmget(struct tty_struct *tty, struct file *file)
  2737. {
  2738. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  2739. unsigned int result;
  2740. unsigned long flags;
  2741. spin_lock_irqsave(&info->lock,flags);
  2742. get_signals(info);
  2743. spin_unlock_irqrestore(&info->lock,flags);
  2744. result = ((info->serial_signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
  2745. ((info->serial_signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
  2746. ((info->serial_signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
  2747. ((info->serial_signals & SerialSignal_RI) ? TIOCM_RNG:0) +
  2748. ((info->serial_signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
  2749. ((info->serial_signals & SerialSignal_CTS) ? TIOCM_CTS:0);
  2750. if (debug_level >= DEBUG_LEVEL_INFO)
  2751. printk("%s(%d):%s tiocmget() value=%08X\n",
  2752. __FILE__,__LINE__, info->device_name, result );
  2753. return result;
  2754. }
  2755. /* set modem control signals (DTR/RTS)
  2756. */
  2757. static int tiocmset(struct tty_struct *tty, struct file *file,
  2758. unsigned int set, unsigned int clear)
  2759. {
  2760. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  2761. unsigned long flags;
  2762. if (debug_level >= DEBUG_LEVEL_INFO)
  2763. printk("%s(%d):%s tiocmset(%x,%x)\n",
  2764. __FILE__,__LINE__,info->device_name, set, clear);
  2765. if (set & TIOCM_RTS)
  2766. info->serial_signals |= SerialSignal_RTS;
  2767. if (set & TIOCM_DTR)
  2768. info->serial_signals |= SerialSignal_DTR;
  2769. if (clear & TIOCM_RTS)
  2770. info->serial_signals &= ~SerialSignal_RTS;
  2771. if (clear & TIOCM_DTR)
  2772. info->serial_signals &= ~SerialSignal_DTR;
  2773. spin_lock_irqsave(&info->lock,flags);
  2774. set_signals(info);
  2775. spin_unlock_irqrestore(&info->lock,flags);
  2776. return 0;
  2777. }
  2778. /* Block the current process until the specified port is ready to open.
  2779. */
  2780. static int block_til_ready(struct tty_struct *tty, struct file *filp,
  2781. SLMP_INFO *info)
  2782. {
  2783. DECLARE_WAITQUEUE(wait, current);
  2784. int retval;
  2785. int do_clocal = 0, extra_count = 0;
  2786. unsigned long flags;
  2787. if (debug_level >= DEBUG_LEVEL_INFO)
  2788. printk("%s(%d):%s block_til_ready()\n",
  2789. __FILE__,__LINE__, tty->driver->name );
  2790. if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
  2791. /* nonblock mode is set or port is not enabled */
  2792. /* just verify that callout device is not active */
  2793. info->flags |= ASYNC_NORMAL_ACTIVE;
  2794. return 0;
  2795. }
  2796. if (tty->termios->c_cflag & CLOCAL)
  2797. do_clocal = 1;
  2798. /* Wait for carrier detect and the line to become
  2799. * free (i.e., not in use by the callout). While we are in
  2800. * this loop, info->count is dropped by one, so that
  2801. * close() knows when to free things. We restore it upon
  2802. * exit, either normal or abnormal.
  2803. */
  2804. retval = 0;
  2805. add_wait_queue(&info->open_wait, &wait);
  2806. if (debug_level >= DEBUG_LEVEL_INFO)
  2807. printk("%s(%d):%s block_til_ready() before block, count=%d\n",
  2808. __FILE__,__LINE__, tty->driver->name, info->count );
  2809. spin_lock_irqsave(&info->lock, flags);
  2810. if (!tty_hung_up_p(filp)) {
  2811. extra_count = 1;
  2812. info->count--;
  2813. }
  2814. spin_unlock_irqrestore(&info->lock, flags);
  2815. info->blocked_open++;
  2816. while (1) {
  2817. if ((tty->termios->c_cflag & CBAUD)) {
  2818. spin_lock_irqsave(&info->lock,flags);
  2819. info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
  2820. set_signals(info);
  2821. spin_unlock_irqrestore(&info->lock,flags);
  2822. }
  2823. set_current_state(TASK_INTERRUPTIBLE);
  2824. if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)){
  2825. retval = (info->flags & ASYNC_HUP_NOTIFY) ?
  2826. -EAGAIN : -ERESTARTSYS;
  2827. break;
  2828. }
  2829. spin_lock_irqsave(&info->lock,flags);
  2830. get_signals(info);
  2831. spin_unlock_irqrestore(&info->lock,flags);
  2832. if (!(info->flags & ASYNC_CLOSING) &&
  2833. (do_clocal || (info->serial_signals & SerialSignal_DCD)) ) {
  2834. break;
  2835. }
  2836. if (signal_pending(current)) {
  2837. retval = -ERESTARTSYS;
  2838. break;
  2839. }
  2840. if (debug_level >= DEBUG_LEVEL_INFO)
  2841. printk("%s(%d):%s block_til_ready() count=%d\n",
  2842. __FILE__,__LINE__, tty->driver->name, info->count );
  2843. schedule();
  2844. }
  2845. set_current_state(TASK_RUNNING);
  2846. remove_wait_queue(&info->open_wait, &wait);
  2847. if (extra_count)
  2848. info->count++;
  2849. info->blocked_open--;
  2850. if (debug_level >= DEBUG_LEVEL_INFO)
  2851. printk("%s(%d):%s block_til_ready() after, count=%d\n",
  2852. __FILE__,__LINE__, tty->driver->name, info->count );
  2853. if (!retval)
  2854. info->flags |= ASYNC_NORMAL_ACTIVE;
  2855. return retval;
  2856. }
  2857. int alloc_dma_bufs(SLMP_INFO *info)
  2858. {
  2859. unsigned short BuffersPerFrame;
  2860. unsigned short BufferCount;
  2861. // Force allocation to start at 64K boundary for each port.
  2862. // This is necessary because *all* buffer descriptors for a port
  2863. // *must* be in the same 64K block. All descriptors on a port
  2864. // share a common 'base' address (upper 8 bits of 24 bits) programmed
  2865. // into the CBP register.
  2866. info->port_array[0]->last_mem_alloc = (SCA_MEM_SIZE/4) * info->port_num;
  2867. /* Calculate the number of DMA buffers necessary to hold the */
  2868. /* largest allowable frame size. Note: If the max frame size is */
  2869. /* not an even multiple of the DMA buffer size then we need to */
  2870. /* round the buffer count per frame up one. */
  2871. BuffersPerFrame = (unsigned short)(info->max_frame_size/SCABUFSIZE);
  2872. if ( info->max_frame_size % SCABUFSIZE )
  2873. BuffersPerFrame++;
  2874. /* calculate total number of data buffers (SCABUFSIZE) possible
  2875. * in one ports memory (SCA_MEM_SIZE/4) after allocating memory
  2876. * for the descriptor list (BUFFERLISTSIZE).
  2877. */
  2878. BufferCount = (SCA_MEM_SIZE/4 - BUFFERLISTSIZE)/SCABUFSIZE;
  2879. /* limit number of buffers to maximum amount of descriptors */
  2880. if (BufferCount > BUFFERLISTSIZE/sizeof(SCADESC))
  2881. BufferCount = BUFFERLISTSIZE/sizeof(SCADESC);
  2882. /* use enough buffers to transmit one max size frame */
  2883. info->tx_buf_count = BuffersPerFrame + 1;
  2884. /* never use more than half the available buffers for transmit */
  2885. if (info->tx_buf_count > (BufferCount/2))
  2886. info->tx_buf_count = BufferCount/2;
  2887. if (info->tx_buf_count > SCAMAXDESC)
  2888. info->tx_buf_count = SCAMAXDESC;
  2889. /* use remaining buffers for receive */
  2890. info->rx_buf_count = BufferCount - info->tx_buf_count;
  2891. if (info->rx_buf_count > SCAMAXDESC)
  2892. info->rx_buf_count = SCAMAXDESC;
  2893. if ( debug_level >= DEBUG_LEVEL_INFO )
  2894. printk("%s(%d):%s Allocating %d TX and %d RX DMA buffers.\n",
  2895. __FILE__,__LINE__, info->device_name,
  2896. info->tx_buf_count,info->rx_buf_count);
  2897. if ( alloc_buf_list( info ) < 0 ||
  2898. alloc_frame_bufs(info,
  2899. info->rx_buf_list,
  2900. info->rx_buf_list_ex,
  2901. info->rx_buf_count) < 0 ||
  2902. alloc_frame_bufs(info,
  2903. info->tx_buf_list,
  2904. info->tx_buf_list_ex,
  2905. info->tx_buf_count) < 0 ||
  2906. alloc_tmp_rx_buf(info) < 0 ) {
  2907. printk("%s(%d):%s Can't allocate DMA buffer memory\n",
  2908. __FILE__,__LINE__, info->device_name);
  2909. return -ENOMEM;
  2910. }
  2911. rx_reset_buffers( info );
  2912. return 0;
  2913. }
  2914. /* Allocate DMA buffers for the transmit and receive descriptor lists.
  2915. */
  2916. int alloc_buf_list(SLMP_INFO *info)
  2917. {
  2918. unsigned int i;
  2919. /* build list in adapter shared memory */
  2920. info->buffer_list = info->memory_base + info->port_array[0]->last_mem_alloc;
  2921. info->buffer_list_phys = info->port_array[0]->last_mem_alloc;
  2922. info->port_array[0]->last_mem_alloc += BUFFERLISTSIZE;
  2923. memset(info->buffer_list, 0, BUFFERLISTSIZE);
  2924. /* Save virtual address pointers to the receive and */
  2925. /* transmit buffer lists. (Receive 1st). These pointers will */
  2926. /* be used by the processor to access the lists. */
  2927. info->rx_buf_list = (SCADESC *)info->buffer_list;
  2928. info->tx_buf_list = (SCADESC *)info->buffer_list;
  2929. info->tx_buf_list += info->rx_buf_count;
  2930. /* Build links for circular buffer entry lists (tx and rx)
  2931. *
  2932. * Note: links are physical addresses read by the SCA device
  2933. * to determine the next buffer entry to use.
  2934. */
  2935. for ( i = 0; i < info->rx_buf_count; i++ ) {
  2936. /* calculate and store physical address of this buffer entry */
  2937. info->rx_buf_list_ex[i].phys_entry =
  2938. info->buffer_list_phys + (i * sizeof(SCABUFSIZE));
  2939. /* calculate and store physical address of */
  2940. /* next entry in cirular list of entries */
  2941. info->rx_buf_list[i].next = info->buffer_list_phys;
  2942. if ( i < info->rx_buf_count - 1 )
  2943. info->rx_buf_list[i].next += (i + 1) * sizeof(SCADESC);
  2944. info->rx_buf_list[i].length = SCABUFSIZE;
  2945. }
  2946. for ( i = 0; i < info->tx_buf_count; i++ ) {
  2947. /* calculate and store physical address of this buffer entry */
  2948. info->tx_buf_list_ex[i].phys_entry = info->buffer_list_phys +
  2949. ((info->rx_buf_count + i) * sizeof(SCADESC));
  2950. /* calculate and store physical address of */
  2951. /* next entry in cirular list of entries */
  2952. info->tx_buf_list[i].next = info->buffer_list_phys +
  2953. info->rx_buf_count * sizeof(SCADESC);
  2954. if ( i < info->tx_buf_count - 1 )
  2955. info->tx_buf_list[i].next += (i + 1) * sizeof(SCADESC);
  2956. }
  2957. return 0;
  2958. }
  2959. /* Allocate the frame DMA buffers used by the specified buffer list.
  2960. */
  2961. int alloc_frame_bufs(SLMP_INFO *info, SCADESC *buf_list,SCADESC_EX *buf_list_ex,int count)
  2962. {
  2963. int i;
  2964. unsigned long phys_addr;
  2965. for ( i = 0; i < count; i++ ) {
  2966. buf_list_ex[i].virt_addr = info->memory_base + info->port_array[0]->last_mem_alloc;
  2967. phys_addr = info->port_array[0]->last_mem_alloc;
  2968. info->port_array[0]->last_mem_alloc += SCABUFSIZE;
  2969. buf_list[i].buf_ptr = (unsigned short)phys_addr;
  2970. buf_list[i].buf_base = (unsigned char)(phys_addr >> 16);
  2971. }
  2972. return 0;
  2973. }
  2974. void free_dma_bufs(SLMP_INFO *info)
  2975. {
  2976. info->buffer_list = NULL;
  2977. info->rx_buf_list = NULL;
  2978. info->tx_buf_list = NULL;
  2979. }
  2980. /* allocate buffer large enough to hold max_frame_size.
  2981. * This buffer is used to pass an assembled frame to the line discipline.
  2982. */
  2983. int alloc_tmp_rx_buf(SLMP_INFO *info)
  2984. {
  2985. info->tmp_rx_buf = kmalloc(info->max_frame_size, GFP_KERNEL);
  2986. if (info->tmp_rx_buf == NULL)
  2987. return -ENOMEM;
  2988. return 0;
  2989. }
  2990. void free_tmp_rx_buf(SLMP_INFO *info)
  2991. {
  2992. kfree(info->tmp_rx_buf);
  2993. info->tmp_rx_buf = NULL;
  2994. }
  2995. int claim_resources(SLMP_INFO *info)
  2996. {
  2997. if (request_mem_region(info->phys_memory_base,SCA_MEM_SIZE,"synclinkmp") == NULL) {
  2998. printk( "%s(%d):%s mem addr conflict, Addr=%08X\n",
  2999. __FILE__,__LINE__,info->device_name, info->phys_memory_base);
  3000. info->init_error = DiagStatus_AddressConflict;
  3001. goto errout;
  3002. }
  3003. else
  3004. info->shared_mem_requested = 1;
  3005. if (request_mem_region(info->phys_lcr_base + info->lcr_offset,128,"synclinkmp") == NULL) {
  3006. printk( "%s(%d):%s lcr mem addr conflict, Addr=%08X\n",
  3007. __FILE__,__LINE__,info->device_name, info->phys_lcr_base);
  3008. info->init_error = DiagStatus_AddressConflict;
  3009. goto errout;
  3010. }
  3011. else
  3012. info->lcr_mem_requested = 1;
  3013. if (request_mem_region(info->phys_sca_base + info->sca_offset,SCA_BASE_SIZE,"synclinkmp") == NULL) {
  3014. printk( "%s(%d):%s sca mem addr conflict, Addr=%08X\n",
  3015. __FILE__,__LINE__,info->device_name, info->phys_sca_base);
  3016. info->init_error = DiagStatus_AddressConflict;
  3017. goto errout;
  3018. }
  3019. else
  3020. info->sca_base_requested = 1;
  3021. if (request_mem_region(info->phys_statctrl_base + info->statctrl_offset,SCA_REG_SIZE,"synclinkmp") == NULL) {
  3022. printk( "%s(%d):%s stat/ctrl mem addr conflict, Addr=%08X\n",
  3023. __FILE__,__LINE__,info->device_name, info->phys_statctrl_base);
  3024. info->init_error = DiagStatus_AddressConflict;
  3025. goto errout;
  3026. }
  3027. else
  3028. info->sca_statctrl_requested = 1;
  3029. info->memory_base = ioremap(info->phys_memory_base,SCA_MEM_SIZE);
  3030. if (!info->memory_base) {
  3031. printk( "%s(%d):%s Cant map shared memory, MemAddr=%08X\n",
  3032. __FILE__,__LINE__,info->device_name, info->phys_memory_base );
  3033. info->init_error = DiagStatus_CantAssignPciResources;
  3034. goto errout;
  3035. }
  3036. info->lcr_base = ioremap(info->phys_lcr_base,PAGE_SIZE);
  3037. if (!info->lcr_base) {
  3038. printk( "%s(%d):%s Cant map LCR memory, MemAddr=%08X\n",
  3039. __FILE__,__LINE__,info->device_name, info->phys_lcr_base );
  3040. info->init_error = DiagStatus_CantAssignPciResources;
  3041. goto errout;
  3042. }
  3043. info->lcr_base += info->lcr_offset;
  3044. info->sca_base = ioremap(info->phys_sca_base,PAGE_SIZE);
  3045. if (!info->sca_base) {
  3046. printk( "%s(%d):%s Cant map SCA memory, MemAddr=%08X\n",
  3047. __FILE__,__LINE__,info->device_name, info->phys_sca_base );
  3048. info->init_error = DiagStatus_CantAssignPciResources;
  3049. goto errout;
  3050. }
  3051. info->sca_base += info->sca_offset;
  3052. info->statctrl_base = ioremap(info->phys_statctrl_base,PAGE_SIZE);
  3053. if (!info->statctrl_base) {
  3054. printk( "%s(%d):%s Cant map SCA Status/Control memory, MemAddr=%08X\n",
  3055. __FILE__,__LINE__,info->device_name, info->phys_statctrl_base );
  3056. info->init_error = DiagStatus_CantAssignPciResources;
  3057. goto errout;
  3058. }
  3059. info->statctrl_base += info->statctrl_offset;
  3060. if ( !memory_test(info) ) {
  3061. printk( "%s(%d):Shared Memory Test failed for device %s MemAddr=%08X\n",
  3062. __FILE__,__LINE__,info->device_name, info->phys_memory_base );
  3063. info->init_error = DiagStatus_MemoryError;
  3064. goto errout;
  3065. }
  3066. return 0;
  3067. errout:
  3068. release_resources( info );
  3069. return -ENODEV;
  3070. }
  3071. void release_resources(SLMP_INFO *info)
  3072. {
  3073. if ( debug_level >= DEBUG_LEVEL_INFO )
  3074. printk( "%s(%d):%s release_resources() entry\n",
  3075. __FILE__,__LINE__,info->device_name );
  3076. if ( info->irq_requested ) {
  3077. free_irq(info->irq_level, info);
  3078. info->irq_requested = 0;
  3079. }
  3080. if ( info->shared_mem_requested ) {
  3081. release_mem_region(info->phys_memory_base,SCA_MEM_SIZE);
  3082. info->shared_mem_requested = 0;
  3083. }
  3084. if ( info->lcr_mem_requested ) {
  3085. release_mem_region(info->phys_lcr_base + info->lcr_offset,128);
  3086. info->lcr_mem_requested = 0;
  3087. }
  3088. if ( info->sca_base_requested ) {
  3089. release_mem_region(info->phys_sca_base + info->sca_offset,SCA_BASE_SIZE);
  3090. info->sca_base_requested = 0;
  3091. }
  3092. if ( info->sca_statctrl_requested ) {
  3093. release_mem_region(info->phys_statctrl_base + info->statctrl_offset,SCA_REG_SIZE);
  3094. info->sca_statctrl_requested = 0;
  3095. }
  3096. if (info->memory_base){
  3097. iounmap(info->memory_base);
  3098. info->memory_base = NULL;
  3099. }
  3100. if (info->sca_base) {
  3101. iounmap(info->sca_base - info->sca_offset);
  3102. info->sca_base=NULL;
  3103. }
  3104. if (info->statctrl_base) {
  3105. iounmap(info->statctrl_base - info->statctrl_offset);
  3106. info->statctrl_base=NULL;
  3107. }
  3108. if (info->lcr_base){
  3109. iounmap(info->lcr_base - info->lcr_offset);
  3110. info->lcr_base = NULL;
  3111. }
  3112. if ( debug_level >= DEBUG_LEVEL_INFO )
  3113. printk( "%s(%d):%s release_resources() exit\n",
  3114. __FILE__,__LINE__,info->device_name );
  3115. }
  3116. /* Add the specified device instance data structure to the
  3117. * global linked list of devices and increment the device count.
  3118. */
  3119. void add_device(SLMP_INFO *info)
  3120. {
  3121. info->next_device = NULL;
  3122. info->line = synclinkmp_device_count;
  3123. sprintf(info->device_name,"ttySLM%dp%d",info->adapter_num,info->port_num);
  3124. if (info->line < MAX_DEVICES) {
  3125. if (maxframe[info->line])
  3126. info->max_frame_size = maxframe[info->line];
  3127. info->dosyncppp = dosyncppp[info->line];
  3128. }
  3129. synclinkmp_device_count++;
  3130. if ( !synclinkmp_device_list )
  3131. synclinkmp_device_list = info;
  3132. else {
  3133. SLMP_INFO *current_dev = synclinkmp_device_list;
  3134. while( current_dev->next_device )
  3135. current_dev = current_dev->next_device;
  3136. current_dev->next_device = info;
  3137. }
  3138. if ( info->max_frame_size < 4096 )
  3139. info->max_frame_size = 4096;
  3140. else if ( info->max_frame_size > 65535 )
  3141. info->max_frame_size = 65535;
  3142. printk( "SyncLink MultiPort %s: "
  3143. "Mem=(%08x %08X %08x %08X) IRQ=%d MaxFrameSize=%u\n",
  3144. info->device_name,
  3145. info->phys_sca_base,
  3146. info->phys_memory_base,
  3147. info->phys_statctrl_base,
  3148. info->phys_lcr_base,
  3149. info->irq_level,
  3150. info->max_frame_size );
  3151. #if SYNCLINK_GENERIC_HDLC
  3152. hdlcdev_init(info);
  3153. #endif
  3154. }
  3155. /* Allocate and initialize a device instance structure
  3156. *
  3157. * Return Value: pointer to SLMP_INFO if success, otherwise NULL
  3158. */
  3159. static SLMP_INFO *alloc_dev(int adapter_num, int port_num, struct pci_dev *pdev)
  3160. {
  3161. SLMP_INFO *info;
  3162. info = kmalloc(sizeof(SLMP_INFO),
  3163. GFP_KERNEL);
  3164. if (!info) {
  3165. printk("%s(%d) Error can't allocate device instance data for adapter %d, port %d\n",
  3166. __FILE__,__LINE__, adapter_num, port_num);
  3167. } else {
  3168. memset(info, 0, sizeof(SLMP_INFO));
  3169. info->magic = MGSL_MAGIC;
  3170. INIT_WORK(&info->task, bh_handler);
  3171. info->max_frame_size = 4096;
  3172. info->close_delay = 5*HZ/10;
  3173. info->closing_wait = 30*HZ;
  3174. init_waitqueue_head(&info->open_wait);
  3175. init_waitqueue_head(&info->close_wait);
  3176. init_waitqueue_head(&info->status_event_wait_q);
  3177. init_waitqueue_head(&info->event_wait_q);
  3178. spin_lock_init(&info->netlock);
  3179. memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
  3180. info->idle_mode = HDLC_TXIDLE_FLAGS;
  3181. info->adapter_num = adapter_num;
  3182. info->port_num = port_num;
  3183. /* Copy configuration info to device instance data */
  3184. info->irq_level = pdev->irq;
  3185. info->phys_lcr_base = pci_resource_start(pdev,0);
  3186. info->phys_sca_base = pci_resource_start(pdev,2);
  3187. info->phys_memory_base = pci_resource_start(pdev,3);
  3188. info->phys_statctrl_base = pci_resource_start(pdev,4);
  3189. /* Because veremap only works on page boundaries we must map
  3190. * a larger area than is actually implemented for the LCR
  3191. * memory range. We map a full page starting at the page boundary.
  3192. */
  3193. info->lcr_offset = info->phys_lcr_base & (PAGE_SIZE-1);
  3194. info->phys_lcr_base &= ~(PAGE_SIZE-1);
  3195. info->sca_offset = info->phys_sca_base & (PAGE_SIZE-1);
  3196. info->phys_sca_base &= ~(PAGE_SIZE-1);
  3197. info->statctrl_offset = info->phys_statctrl_base & (PAGE_SIZE-1);
  3198. info->phys_statctrl_base &= ~(PAGE_SIZE-1);
  3199. info->bus_type = MGSL_BUS_TYPE_PCI;
  3200. info->irq_flags = IRQF_SHARED;
  3201. init_timer(&info->tx_timer);
  3202. info->tx_timer.data = (unsigned long)info;
  3203. info->tx_timer.function = tx_timeout;
  3204. init_timer(&info->status_timer);
  3205. info->status_timer.data = (unsigned long)info;
  3206. info->status_timer.function = status_timeout;
  3207. /* Store the PCI9050 misc control register value because a flaw
  3208. * in the PCI9050 prevents LCR registers from being read if
  3209. * BIOS assigns an LCR base address with bit 7 set.
  3210. *
  3211. * Only the misc control register is accessed for which only
  3212. * write access is needed, so set an initial value and change
  3213. * bits to the device instance data as we write the value
  3214. * to the actual misc control register.
  3215. */
  3216. info->misc_ctrl_value = 0x087e4546;
  3217. /* initial port state is unknown - if startup errors
  3218. * occur, init_error will be set to indicate the
  3219. * problem. Once the port is fully initialized,
  3220. * this value will be set to 0 to indicate the
  3221. * port is available.
  3222. */
  3223. info->init_error = -1;
  3224. }
  3225. return info;
  3226. }
  3227. void device_init(int adapter_num, struct pci_dev *pdev)
  3228. {
  3229. SLMP_INFO *port_array[SCA_MAX_PORTS];
  3230. int port;
  3231. /* allocate device instances for up to SCA_MAX_PORTS devices */
  3232. for ( port = 0; port < SCA_MAX_PORTS; ++port ) {
  3233. port_array[port] = alloc_dev(adapter_num,port,pdev);
  3234. if( port_array[port] == NULL ) {
  3235. for ( --port; port >= 0; --port )
  3236. kfree(port_array[port]);
  3237. return;
  3238. }
  3239. }
  3240. /* give copy of port_array to all ports and add to device list */
  3241. for ( port = 0; port < SCA_MAX_PORTS; ++port ) {
  3242. memcpy(port_array[port]->port_array,port_array,sizeof(port_array));
  3243. add_device( port_array[port] );
  3244. spin_lock_init(&port_array[port]->lock);
  3245. }
  3246. /* Allocate and claim adapter resources */
  3247. if ( !claim_resources(port_array[0]) ) {
  3248. alloc_dma_bufs(port_array[0]);
  3249. /* copy resource information from first port to others */
  3250. for ( port = 1; port < SCA_MAX_PORTS; ++port ) {
  3251. port_array[port]->lock = port_array[0]->lock;
  3252. port_array[port]->irq_level = port_array[0]->irq_level;
  3253. port_array[port]->memory_base = port_array[0]->memory_base;
  3254. port_array[port]->sca_base = port_array[0]->sca_base;
  3255. port_array[port]->statctrl_base = port_array[0]->statctrl_base;
  3256. port_array[port]->lcr_base = port_array[0]->lcr_base;
  3257. alloc_dma_bufs(port_array[port]);
  3258. }
  3259. if ( request_irq(port_array[0]->irq_level,
  3260. synclinkmp_interrupt,
  3261. port_array[0]->irq_flags,
  3262. port_array[0]->device_name,
  3263. port_array[0]) < 0 ) {
  3264. printk( "%s(%d):%s Cant request interrupt, IRQ=%d\n",
  3265. __FILE__,__LINE__,
  3266. port_array[0]->device_name,
  3267. port_array[0]->irq_level );
  3268. }
  3269. else {
  3270. port_array[0]->irq_requested = 1;
  3271. adapter_test(port_array[0]);
  3272. }
  3273. }
  3274. }
  3275. static const struct tty_operations ops = {
  3276. .open = open,
  3277. .close = close,
  3278. .write = write,
  3279. .put_char = put_char,
  3280. .flush_chars = flush_chars,
  3281. .write_room = write_room,
  3282. .chars_in_buffer = chars_in_buffer,
  3283. .flush_buffer = flush_buffer,
  3284. .ioctl = ioctl,
  3285. .throttle = throttle,
  3286. .unthrottle = unthrottle,
  3287. .send_xchar = send_xchar,
  3288. .break_ctl = set_break,
  3289. .wait_until_sent = wait_until_sent,
  3290. .read_proc = read_proc,
  3291. .set_termios = set_termios,
  3292. .stop = tx_hold,
  3293. .start = tx_release,
  3294. .hangup = hangup,
  3295. .tiocmget = tiocmget,
  3296. .tiocmset = tiocmset,
  3297. };
  3298. static void synclinkmp_cleanup(void)
  3299. {
  3300. int rc;
  3301. SLMP_INFO *info;
  3302. SLMP_INFO *tmp;
  3303. printk("Unloading %s %s\n", driver_name, driver_version);
  3304. if (serial_driver) {
  3305. if ((rc = tty_unregister_driver(serial_driver)))
  3306. printk("%s(%d) failed to unregister tty driver err=%d\n",
  3307. __FILE__,__LINE__,rc);
  3308. put_tty_driver(serial_driver);
  3309. }
  3310. /* reset devices */
  3311. info = synclinkmp_device_list;
  3312. while(info) {
  3313. reset_port(info);
  3314. info = info->next_device;
  3315. }
  3316. /* release devices */
  3317. info = synclinkmp_device_list;
  3318. while(info) {
  3319. #if SYNCLINK_GENERIC_HDLC
  3320. hdlcdev_exit(info);
  3321. #endif
  3322. free_dma_bufs(info);
  3323. free_tmp_rx_buf(info);
  3324. if ( info->port_num == 0 ) {
  3325. if (info->sca_base)
  3326. write_reg(info, LPR, 1); /* set low power mode */
  3327. release_resources(info);
  3328. }
  3329. tmp = info;
  3330. info = info->next_device;
  3331. kfree(tmp);
  3332. }
  3333. pci_unregister_driver(&synclinkmp_pci_driver);
  3334. }
  3335. /* Driver initialization entry point.
  3336. */
  3337. static int __init synclinkmp_init(void)
  3338. {
  3339. int rc;
  3340. if (break_on_load) {
  3341. synclinkmp_get_text_ptr();
  3342. BREAKPOINT();
  3343. }
  3344. printk("%s %s\n", driver_name, driver_version);
  3345. if ((rc = pci_register_driver(&synclinkmp_pci_driver)) < 0) {
  3346. printk("%s:failed to register PCI driver, error=%d\n",__FILE__,rc);
  3347. return rc;
  3348. }
  3349. serial_driver = alloc_tty_driver(128);
  3350. if (!serial_driver) {
  3351. rc = -ENOMEM;
  3352. goto error;
  3353. }
  3354. /* Initialize the tty_driver structure */
  3355. serial_driver->owner = THIS_MODULE;
  3356. serial_driver->driver_name = "synclinkmp";
  3357. serial_driver->name = "ttySLM";
  3358. serial_driver->major = ttymajor;
  3359. serial_driver->minor_start = 64;
  3360. serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  3361. serial_driver->subtype = SERIAL_TYPE_NORMAL;
  3362. serial_driver->init_termios = tty_std_termios;
  3363. serial_driver->init_termios.c_cflag =
  3364. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  3365. serial_driver->init_termios.c_ispeed = 9600;
  3366. serial_driver->init_termios.c_ospeed = 9600;
  3367. serial_driver->flags = TTY_DRIVER_REAL_RAW;
  3368. tty_set_operations(serial_driver, &ops);
  3369. if ((rc = tty_register_driver(serial_driver)) < 0) {
  3370. printk("%s(%d):Couldn't register serial driver\n",
  3371. __FILE__,__LINE__);
  3372. put_tty_driver(serial_driver);
  3373. serial_driver = NULL;
  3374. goto error;
  3375. }
  3376. printk("%s %s, tty major#%d\n",
  3377. driver_name, driver_version,
  3378. serial_driver->major);
  3379. return 0;
  3380. error:
  3381. synclinkmp_cleanup();
  3382. return rc;
  3383. }
  3384. static void __exit synclinkmp_exit(void)
  3385. {
  3386. synclinkmp_cleanup();
  3387. }
  3388. module_init(synclinkmp_init);
  3389. module_exit(synclinkmp_exit);
  3390. /* Set the port for internal loopback mode.
  3391. * The TxCLK and RxCLK signals are generated from the BRG and
  3392. * the TxD is looped back to the RxD internally.
  3393. */
  3394. void enable_loopback(SLMP_INFO *info, int enable)
  3395. {
  3396. if (enable) {
  3397. /* MD2 (Mode Register 2)
  3398. * 01..00 CNCT<1..0> Channel Connection 11=Local Loopback
  3399. */
  3400. write_reg(info, MD2, (unsigned char)(read_reg(info, MD2) | (BIT1 + BIT0)));
  3401. /* degate external TxC clock source */
  3402. info->port_array[0]->ctrlreg_value |= (BIT0 << (info->port_num * 2));
  3403. write_control_reg(info);
  3404. /* RXS/TXS (Rx/Tx clock source)
  3405. * 07 Reserved, must be 0
  3406. * 06..04 Clock Source, 100=BRG
  3407. * 03..00 Clock Divisor, 0000=1
  3408. */
  3409. write_reg(info, RXS, 0x40);
  3410. write_reg(info, TXS, 0x40);
  3411. } else {
  3412. /* MD2 (Mode Register 2)
  3413. * 01..00 CNCT<1..0> Channel connection, 0=normal
  3414. */
  3415. write_reg(info, MD2, (unsigned char)(read_reg(info, MD2) & ~(BIT1 + BIT0)));
  3416. /* RXS/TXS (Rx/Tx clock source)
  3417. * 07 Reserved, must be 0
  3418. * 06..04 Clock Source, 000=RxC/TxC Pin
  3419. * 03..00 Clock Divisor, 0000=1
  3420. */
  3421. write_reg(info, RXS, 0x00);
  3422. write_reg(info, TXS, 0x00);
  3423. }
  3424. /* set LinkSpeed if available, otherwise default to 2Mbps */
  3425. if (info->params.clock_speed)
  3426. set_rate(info, info->params.clock_speed);
  3427. else
  3428. set_rate(info, 3686400);
  3429. }
  3430. /* Set the baud rate register to the desired speed
  3431. *
  3432. * data_rate data rate of clock in bits per second
  3433. * A data rate of 0 disables the AUX clock.
  3434. */
  3435. void set_rate( SLMP_INFO *info, u32 data_rate )
  3436. {
  3437. u32 TMCValue;
  3438. unsigned char BRValue;
  3439. u32 Divisor=0;
  3440. /* fBRG = fCLK/(TMC * 2^BR)
  3441. */
  3442. if (data_rate != 0) {
  3443. Divisor = 14745600/data_rate;
  3444. if (!Divisor)
  3445. Divisor = 1;
  3446. TMCValue = Divisor;
  3447. BRValue = 0;
  3448. if (TMCValue != 1 && TMCValue != 2) {
  3449. /* BRValue of 0 provides 50/50 duty cycle *only* when
  3450. * TMCValue is 1 or 2. BRValue of 1 to 9 always provides
  3451. * 50/50 duty cycle.
  3452. */
  3453. BRValue = 1;
  3454. TMCValue >>= 1;
  3455. }
  3456. /* while TMCValue is too big for TMC register, divide
  3457. * by 2 and increment BR exponent.
  3458. */
  3459. for(; TMCValue > 256 && BRValue < 10; BRValue++)
  3460. TMCValue >>= 1;
  3461. write_reg(info, TXS,
  3462. (unsigned char)((read_reg(info, TXS) & 0xf0) | BRValue));
  3463. write_reg(info, RXS,
  3464. (unsigned char)((read_reg(info, RXS) & 0xf0) | BRValue));
  3465. write_reg(info, TMC, (unsigned char)TMCValue);
  3466. }
  3467. else {
  3468. write_reg(info, TXS,0);
  3469. write_reg(info, RXS,0);
  3470. write_reg(info, TMC, 0);
  3471. }
  3472. }
  3473. /* Disable receiver
  3474. */
  3475. void rx_stop(SLMP_INFO *info)
  3476. {
  3477. if (debug_level >= DEBUG_LEVEL_ISR)
  3478. printk("%s(%d):%s rx_stop()\n",
  3479. __FILE__,__LINE__, info->device_name );
  3480. write_reg(info, CMD, RXRESET);
  3481. info->ie0_value &= ~RXRDYE;
  3482. write_reg(info, IE0, info->ie0_value); /* disable Rx data interrupts */
  3483. write_reg(info, RXDMA + DSR, 0); /* disable Rx DMA */
  3484. write_reg(info, RXDMA + DCMD, SWABORT); /* reset/init Rx DMA */
  3485. write_reg(info, RXDMA + DIR, 0); /* disable Rx DMA interrupts */
  3486. info->rx_enabled = 0;
  3487. info->rx_overflow = 0;
  3488. }
  3489. /* enable the receiver
  3490. */
  3491. void rx_start(SLMP_INFO *info)
  3492. {
  3493. int i;
  3494. if (debug_level >= DEBUG_LEVEL_ISR)
  3495. printk("%s(%d):%s rx_start()\n",
  3496. __FILE__,__LINE__, info->device_name );
  3497. write_reg(info, CMD, RXRESET);
  3498. if ( info->params.mode == MGSL_MODE_HDLC ) {
  3499. /* HDLC, disabe IRQ on rxdata */
  3500. info->ie0_value &= ~RXRDYE;
  3501. write_reg(info, IE0, info->ie0_value);
  3502. /* Reset all Rx DMA buffers and program rx dma */
  3503. write_reg(info, RXDMA + DSR, 0); /* disable Rx DMA */
  3504. write_reg(info, RXDMA + DCMD, SWABORT); /* reset/init Rx DMA */
  3505. for (i = 0; i < info->rx_buf_count; i++) {
  3506. info->rx_buf_list[i].status = 0xff;
  3507. // throttle to 4 shared memory writes at a time to prevent
  3508. // hogging local bus (keep latency time for DMA requests low).
  3509. if (!(i % 4))
  3510. read_status_reg(info);
  3511. }
  3512. info->current_rx_buf = 0;
  3513. /* set current/1st descriptor address */
  3514. write_reg16(info, RXDMA + CDA,
  3515. info->rx_buf_list_ex[0].phys_entry);
  3516. /* set new last rx descriptor address */
  3517. write_reg16(info, RXDMA + EDA,
  3518. info->rx_buf_list_ex[info->rx_buf_count - 1].phys_entry);
  3519. /* set buffer length (shared by all rx dma data buffers) */
  3520. write_reg16(info, RXDMA + BFL, SCABUFSIZE);
  3521. write_reg(info, RXDMA + DIR, 0x60); /* enable Rx DMA interrupts (EOM/BOF) */
  3522. write_reg(info, RXDMA + DSR, 0xf2); /* clear Rx DMA IRQs, enable Rx DMA */
  3523. } else {
  3524. /* async, enable IRQ on rxdata */
  3525. info->ie0_value |= RXRDYE;
  3526. write_reg(info, IE0, info->ie0_value);
  3527. }
  3528. write_reg(info, CMD, RXENABLE);
  3529. info->rx_overflow = FALSE;
  3530. info->rx_enabled = 1;
  3531. }
  3532. /* Enable the transmitter and send a transmit frame if
  3533. * one is loaded in the DMA buffers.
  3534. */
  3535. void tx_start(SLMP_INFO *info)
  3536. {
  3537. if (debug_level >= DEBUG_LEVEL_ISR)
  3538. printk("%s(%d):%s tx_start() tx_count=%d\n",
  3539. __FILE__,__LINE__, info->device_name,info->tx_count );
  3540. if (!info->tx_enabled ) {
  3541. write_reg(info, CMD, TXRESET);
  3542. write_reg(info, CMD, TXENABLE);
  3543. info->tx_enabled = TRUE;
  3544. }
  3545. if ( info->tx_count ) {
  3546. /* If auto RTS enabled and RTS is inactive, then assert */
  3547. /* RTS and set a flag indicating that the driver should */
  3548. /* negate RTS when the transmission completes. */
  3549. info->drop_rts_on_tx_done = 0;
  3550. if (info->params.mode != MGSL_MODE_ASYNC) {
  3551. if ( info->params.flags & HDLC_FLAG_AUTO_RTS ) {
  3552. get_signals( info );
  3553. if ( !(info->serial_signals & SerialSignal_RTS) ) {
  3554. info->serial_signals |= SerialSignal_RTS;
  3555. set_signals( info );
  3556. info->drop_rts_on_tx_done = 1;
  3557. }
  3558. }
  3559. write_reg16(info, TRC0,
  3560. (unsigned short)(((tx_negate_fifo_level-1)<<8) + tx_active_fifo_level));
  3561. write_reg(info, TXDMA + DSR, 0); /* disable DMA channel */
  3562. write_reg(info, TXDMA + DCMD, SWABORT); /* reset/init DMA channel */
  3563. /* set TX CDA (current descriptor address) */
  3564. write_reg16(info, TXDMA + CDA,
  3565. info->tx_buf_list_ex[0].phys_entry);
  3566. /* set TX EDA (last descriptor address) */
  3567. write_reg16(info, TXDMA + EDA,
  3568. info->tx_buf_list_ex[info->last_tx_buf].phys_entry);
  3569. /* enable underrun IRQ */
  3570. info->ie1_value &= ~IDLE;
  3571. info->ie1_value |= UDRN;
  3572. write_reg(info, IE1, info->ie1_value);
  3573. write_reg(info, SR1, (unsigned char)(IDLE + UDRN));
  3574. write_reg(info, TXDMA + DIR, 0x40); /* enable Tx DMA interrupts (EOM) */
  3575. write_reg(info, TXDMA + DSR, 0xf2); /* clear Tx DMA IRQs, enable Tx DMA */
  3576. info->tx_timer.expires = jiffies + msecs_to_jiffies(5000);
  3577. add_timer(&info->tx_timer);
  3578. }
  3579. else {
  3580. tx_load_fifo(info);
  3581. /* async, enable IRQ on txdata */
  3582. info->ie0_value |= TXRDYE;
  3583. write_reg(info, IE0, info->ie0_value);
  3584. }
  3585. info->tx_active = 1;
  3586. }
  3587. }
  3588. /* stop the transmitter and DMA
  3589. */
  3590. void tx_stop( SLMP_INFO *info )
  3591. {
  3592. if (debug_level >= DEBUG_LEVEL_ISR)
  3593. printk("%s(%d):%s tx_stop()\n",
  3594. __FILE__,__LINE__, info->device_name );
  3595. del_timer(&info->tx_timer);
  3596. write_reg(info, TXDMA + DSR, 0); /* disable DMA channel */
  3597. write_reg(info, TXDMA + DCMD, SWABORT); /* reset/init DMA channel */
  3598. write_reg(info, CMD, TXRESET);
  3599. info->ie1_value &= ~(UDRN + IDLE);
  3600. write_reg(info, IE1, info->ie1_value); /* disable tx status interrupts */
  3601. write_reg(info, SR1, (unsigned char)(IDLE + UDRN)); /* clear pending */
  3602. info->ie0_value &= ~TXRDYE;
  3603. write_reg(info, IE0, info->ie0_value); /* disable tx data interrupts */
  3604. info->tx_enabled = 0;
  3605. info->tx_active = 0;
  3606. }
  3607. /* Fill the transmit FIFO until the FIFO is full or
  3608. * there is no more data to load.
  3609. */
  3610. void tx_load_fifo(SLMP_INFO *info)
  3611. {
  3612. u8 TwoBytes[2];
  3613. /* do nothing is now tx data available and no XON/XOFF pending */
  3614. if ( !info->tx_count && !info->x_char )
  3615. return;
  3616. /* load the Transmit FIFO until FIFOs full or all data sent */
  3617. while( info->tx_count && (read_reg(info,SR0) & BIT1) ) {
  3618. /* there is more space in the transmit FIFO and */
  3619. /* there is more data in transmit buffer */
  3620. if ( (info->tx_count > 1) && !info->x_char ) {
  3621. /* write 16-bits */
  3622. TwoBytes[0] = info->tx_buf[info->tx_get++];
  3623. if (info->tx_get >= info->max_frame_size)
  3624. info->tx_get -= info->max_frame_size;
  3625. TwoBytes[1] = info->tx_buf[info->tx_get++];
  3626. if (info->tx_get >= info->max_frame_size)
  3627. info->tx_get -= info->max_frame_size;
  3628. write_reg16(info, TRB, *((u16 *)TwoBytes));
  3629. info->tx_count -= 2;
  3630. info->icount.tx += 2;
  3631. } else {
  3632. /* only 1 byte left to transmit or 1 FIFO slot left */
  3633. if (info->x_char) {
  3634. /* transmit pending high priority char */
  3635. write_reg(info, TRB, info->x_char);
  3636. info->x_char = 0;
  3637. } else {
  3638. write_reg(info, TRB, info->tx_buf[info->tx_get++]);
  3639. if (info->tx_get >= info->max_frame_size)
  3640. info->tx_get -= info->max_frame_size;
  3641. info->tx_count--;
  3642. }
  3643. info->icount.tx++;
  3644. }
  3645. }
  3646. }
  3647. /* Reset a port to a known state
  3648. */
  3649. void reset_port(SLMP_INFO *info)
  3650. {
  3651. if (info->sca_base) {
  3652. tx_stop(info);
  3653. rx_stop(info);
  3654. info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
  3655. set_signals(info);
  3656. /* disable all port interrupts */
  3657. info->ie0_value = 0;
  3658. info->ie1_value = 0;
  3659. info->ie2_value = 0;
  3660. write_reg(info, IE0, info->ie0_value);
  3661. write_reg(info, IE1, info->ie1_value);
  3662. write_reg(info, IE2, info->ie2_value);
  3663. write_reg(info, CMD, CHRESET);
  3664. }
  3665. }
  3666. /* Reset all the ports to a known state.
  3667. */
  3668. void reset_adapter(SLMP_INFO *info)
  3669. {
  3670. int i;
  3671. for ( i=0; i < SCA_MAX_PORTS; ++i) {
  3672. if (info->port_array[i])
  3673. reset_port(info->port_array[i]);
  3674. }
  3675. }
  3676. /* Program port for asynchronous communications.
  3677. */
  3678. void async_mode(SLMP_INFO *info)
  3679. {
  3680. unsigned char RegValue;
  3681. tx_stop(info);
  3682. rx_stop(info);
  3683. /* MD0, Mode Register 0
  3684. *
  3685. * 07..05 PRCTL<2..0>, Protocol Mode, 000=async
  3686. * 04 AUTO, Auto-enable (RTS/CTS/DCD)
  3687. * 03 Reserved, must be 0
  3688. * 02 CRCCC, CRC Calculation, 0=disabled
  3689. * 01..00 STOP<1..0> Stop bits (00=1,10=2)
  3690. *
  3691. * 0000 0000
  3692. */
  3693. RegValue = 0x00;
  3694. if (info->params.stop_bits != 1)
  3695. RegValue |= BIT1;
  3696. write_reg(info, MD0, RegValue);
  3697. /* MD1, Mode Register 1
  3698. *
  3699. * 07..06 BRATE<1..0>, bit rate, 00=1/1 01=1/16 10=1/32 11=1/64
  3700. * 05..04 TXCHR<1..0>, tx char size, 00=8 bits,01=7,10=6,11=5
  3701. * 03..02 RXCHR<1..0>, rx char size
  3702. * 01..00 PMPM<1..0>, Parity mode, 00=none 10=even 11=odd
  3703. *
  3704. * 0100 0000
  3705. */
  3706. RegValue = 0x40;
  3707. switch (info->params.data_bits) {
  3708. case 7: RegValue |= BIT4 + BIT2; break;
  3709. case 6: RegValue |= BIT5 + BIT3; break;
  3710. case 5: RegValue |= BIT5 + BIT4 + BIT3 + BIT2; break;
  3711. }
  3712. if (info->params.parity != ASYNC_PARITY_NONE) {
  3713. RegValue |= BIT1;
  3714. if (info->params.parity == ASYNC_PARITY_ODD)
  3715. RegValue |= BIT0;
  3716. }
  3717. write_reg(info, MD1, RegValue);
  3718. /* MD2, Mode Register 2
  3719. *
  3720. * 07..02 Reserved, must be 0
  3721. * 01..00 CNCT<1..0> Channel connection, 00=normal 11=local loopback
  3722. *
  3723. * 0000 0000
  3724. */
  3725. RegValue = 0x00;
  3726. if (info->params.loopback)
  3727. RegValue |= (BIT1 + BIT0);
  3728. write_reg(info, MD2, RegValue);
  3729. /* RXS, Receive clock source
  3730. *
  3731. * 07 Reserved, must be 0
  3732. * 06..04 RXCS<2..0>, clock source, 000=RxC Pin, 100=BRG, 110=DPLL
  3733. * 03..00 RXBR<3..0>, rate divisor, 0000=1
  3734. */
  3735. RegValue=BIT6;
  3736. write_reg(info, RXS, RegValue);
  3737. /* TXS, Transmit clock source
  3738. *
  3739. * 07 Reserved, must be 0
  3740. * 06..04 RXCS<2..0>, clock source, 000=TxC Pin, 100=BRG, 110=Receive Clock
  3741. * 03..00 RXBR<3..0>, rate divisor, 0000=1
  3742. */
  3743. RegValue=BIT6;
  3744. write_reg(info, TXS, RegValue);
  3745. /* Control Register
  3746. *
  3747. * 6,4,2,0 CLKSEL<3..0>, 0 = TcCLK in, 1 = Auxclk out
  3748. */
  3749. info->port_array[0]->ctrlreg_value |= (BIT0 << (info->port_num * 2));
  3750. write_control_reg(info);
  3751. tx_set_idle(info);
  3752. /* RRC Receive Ready Control 0
  3753. *
  3754. * 07..05 Reserved, must be 0
  3755. * 04..00 RRC<4..0> Rx FIFO trigger active 0x00 = 1 byte
  3756. */
  3757. write_reg(info, RRC, 0x00);
  3758. /* TRC0 Transmit Ready Control 0
  3759. *
  3760. * 07..05 Reserved, must be 0
  3761. * 04..00 TRC<4..0> Tx FIFO trigger active 0x10 = 16 bytes
  3762. */
  3763. write_reg(info, TRC0, 0x10);
  3764. /* TRC1 Transmit Ready Control 1
  3765. *
  3766. * 07..05 Reserved, must be 0
  3767. * 04..00 TRC<4..0> Tx FIFO trigger inactive 0x1e = 31 bytes (full-1)
  3768. */
  3769. write_reg(info, TRC1, 0x1e);
  3770. /* CTL, MSCI control register
  3771. *
  3772. * 07..06 Reserved, set to 0
  3773. * 05 UDRNC, underrun control, 0=abort 1=CRC+flag (HDLC/BSC)
  3774. * 04 IDLC, idle control, 0=mark 1=idle register
  3775. * 03 BRK, break, 0=off 1 =on (async)
  3776. * 02 SYNCLD, sync char load enable (BSC) 1=enabled
  3777. * 01 GOP, go active on poll (LOOP mode) 1=enabled
  3778. * 00 RTS, RTS output control, 0=active 1=inactive
  3779. *
  3780. * 0001 0001
  3781. */
  3782. RegValue = 0x10;
  3783. if (!(info->serial_signals & SerialSignal_RTS))
  3784. RegValue |= 0x01;
  3785. write_reg(info, CTL, RegValue);
  3786. /* enable status interrupts */
  3787. info->ie0_value |= TXINTE + RXINTE;
  3788. write_reg(info, IE0, info->ie0_value);
  3789. /* enable break detect interrupt */
  3790. info->ie1_value = BRKD;
  3791. write_reg(info, IE1, info->ie1_value);
  3792. /* enable rx overrun interrupt */
  3793. info->ie2_value = OVRN;
  3794. write_reg(info, IE2, info->ie2_value);
  3795. set_rate( info, info->params.data_rate * 16 );
  3796. }
  3797. /* Program the SCA for HDLC communications.
  3798. */
  3799. void hdlc_mode(SLMP_INFO *info)
  3800. {
  3801. unsigned char RegValue;
  3802. u32 DpllDivisor;
  3803. // Can't use DPLL because SCA outputs recovered clock on RxC when
  3804. // DPLL mode selected. This causes output contention with RxC receiver.
  3805. // Use of DPLL would require external hardware to disable RxC receiver
  3806. // when DPLL mode selected.
  3807. info->params.flags &= ~(HDLC_FLAG_TXC_DPLL + HDLC_FLAG_RXC_DPLL);
  3808. /* disable DMA interrupts */
  3809. write_reg(info, TXDMA + DIR, 0);
  3810. write_reg(info, RXDMA + DIR, 0);
  3811. /* MD0, Mode Register 0
  3812. *
  3813. * 07..05 PRCTL<2..0>, Protocol Mode, 100=HDLC
  3814. * 04 AUTO, Auto-enable (RTS/CTS/DCD)
  3815. * 03 Reserved, must be 0
  3816. * 02 CRCCC, CRC Calculation, 1=enabled
  3817. * 01 CRC1, CRC selection, 0=CRC-16,1=CRC-CCITT-16
  3818. * 00 CRC0, CRC initial value, 1 = all 1s
  3819. *
  3820. * 1000 0001
  3821. */
  3822. RegValue = 0x81;
  3823. if (info->params.flags & HDLC_FLAG_AUTO_CTS)
  3824. RegValue |= BIT4;
  3825. if (info->params.flags & HDLC_FLAG_AUTO_DCD)
  3826. RegValue |= BIT4;
  3827. if (info->params.crc_type == HDLC_CRC_16_CCITT)
  3828. RegValue |= BIT2 + BIT1;
  3829. write_reg(info, MD0, RegValue);
  3830. /* MD1, Mode Register 1
  3831. *
  3832. * 07..06 ADDRS<1..0>, Address detect, 00=no addr check
  3833. * 05..04 TXCHR<1..0>, tx char size, 00=8 bits
  3834. * 03..02 RXCHR<1..0>, rx char size, 00=8 bits
  3835. * 01..00 PMPM<1..0>, Parity mode, 00=no parity
  3836. *
  3837. * 0000 0000
  3838. */
  3839. RegValue = 0x00;
  3840. write_reg(info, MD1, RegValue);
  3841. /* MD2, Mode Register 2
  3842. *
  3843. * 07 NRZFM, 0=NRZ, 1=FM
  3844. * 06..05 CODE<1..0> Encoding, 00=NRZ
  3845. * 04..03 DRATE<1..0> DPLL Divisor, 00=8
  3846. * 02 Reserved, must be 0
  3847. * 01..00 CNCT<1..0> Channel connection, 0=normal
  3848. *
  3849. * 0000 0000
  3850. */
  3851. RegValue = 0x00;
  3852. switch(info->params.encoding) {
  3853. case HDLC_ENCODING_NRZI: RegValue |= BIT5; break;
  3854. case HDLC_ENCODING_BIPHASE_MARK: RegValue |= BIT7 + BIT5; break; /* aka FM1 */
  3855. case HDLC_ENCODING_BIPHASE_SPACE: RegValue |= BIT7 + BIT6; break; /* aka FM0 */
  3856. case HDLC_ENCODING_BIPHASE_LEVEL: RegValue |= BIT7; break; /* aka Manchester */
  3857. #if 0
  3858. case HDLC_ENCODING_NRZB: /* not supported */
  3859. case HDLC_ENCODING_NRZI_MARK: /* not supported */
  3860. case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: /* not supported */
  3861. #endif
  3862. }
  3863. if ( info->params.flags & HDLC_FLAG_DPLL_DIV16 ) {
  3864. DpllDivisor = 16;
  3865. RegValue |= BIT3;
  3866. } else if ( info->params.flags & HDLC_FLAG_DPLL_DIV8 ) {
  3867. DpllDivisor = 8;
  3868. } else {
  3869. DpllDivisor = 32;
  3870. RegValue |= BIT4;
  3871. }
  3872. write_reg(info, MD2, RegValue);
  3873. /* RXS, Receive clock source
  3874. *
  3875. * 07 Reserved, must be 0
  3876. * 06..04 RXCS<2..0>, clock source, 000=RxC Pin, 100=BRG, 110=DPLL
  3877. * 03..00 RXBR<3..0>, rate divisor, 0000=1
  3878. */
  3879. RegValue=0;
  3880. if (info->params.flags & HDLC_FLAG_RXC_BRG)
  3881. RegValue |= BIT6;
  3882. if (info->params.flags & HDLC_FLAG_RXC_DPLL)
  3883. RegValue |= BIT6 + BIT5;
  3884. write_reg(info, RXS, RegValue);
  3885. /* TXS, Transmit clock source
  3886. *
  3887. * 07 Reserved, must be 0
  3888. * 06..04 RXCS<2..0>, clock source, 000=TxC Pin, 100=BRG, 110=Receive Clock
  3889. * 03..00 RXBR<3..0>, rate divisor, 0000=1
  3890. */
  3891. RegValue=0;
  3892. if (info->params.flags & HDLC_FLAG_TXC_BRG)
  3893. RegValue |= BIT6;
  3894. if (info->params.flags & HDLC_FLAG_TXC_DPLL)
  3895. RegValue |= BIT6 + BIT5;
  3896. write_reg(info, TXS, RegValue);
  3897. if (info->params.flags & HDLC_FLAG_RXC_DPLL)
  3898. set_rate(info, info->params.clock_speed * DpllDivisor);
  3899. else
  3900. set_rate(info, info->params.clock_speed);
  3901. /* GPDATA (General Purpose I/O Data Register)
  3902. *
  3903. * 6,4,2,0 CLKSEL<3..0>, 0 = TcCLK in, 1 = Auxclk out
  3904. */
  3905. if (info->params.flags & HDLC_FLAG_TXC_BRG)
  3906. info->port_array[0]->ctrlreg_value |= (BIT0 << (info->port_num * 2));
  3907. else
  3908. info->port_array[0]->ctrlreg_value &= ~(BIT0 << (info->port_num * 2));
  3909. write_control_reg(info);
  3910. /* RRC Receive Ready Control 0
  3911. *
  3912. * 07..05 Reserved, must be 0
  3913. * 04..00 RRC<4..0> Rx FIFO trigger active
  3914. */
  3915. write_reg(info, RRC, rx_active_fifo_level);
  3916. /* TRC0 Transmit Ready Control 0
  3917. *
  3918. * 07..05 Reserved, must be 0
  3919. * 04..00 TRC<4..0> Tx FIFO trigger active
  3920. */
  3921. write_reg(info, TRC0, tx_active_fifo_level);
  3922. /* TRC1 Transmit Ready Control 1
  3923. *
  3924. * 07..05 Reserved, must be 0
  3925. * 04..00 TRC<4..0> Tx FIFO trigger inactive 0x1f = 32 bytes (full)
  3926. */
  3927. write_reg(info, TRC1, (unsigned char)(tx_negate_fifo_level - 1));
  3928. /* DMR, DMA Mode Register
  3929. *
  3930. * 07..05 Reserved, must be 0
  3931. * 04 TMOD, Transfer Mode: 1=chained-block
  3932. * 03 Reserved, must be 0
  3933. * 02 NF, Number of Frames: 1=multi-frame
  3934. * 01 CNTE, Frame End IRQ Counter enable: 0=disabled
  3935. * 00 Reserved, must be 0
  3936. *
  3937. * 0001 0100
  3938. */
  3939. write_reg(info, TXDMA + DMR, 0x14);
  3940. write_reg(info, RXDMA + DMR, 0x14);
  3941. /* Set chain pointer base (upper 8 bits of 24 bit addr) */
  3942. write_reg(info, RXDMA + CPB,
  3943. (unsigned char)(info->buffer_list_phys >> 16));
  3944. /* Set chain pointer base (upper 8 bits of 24 bit addr) */
  3945. write_reg(info, TXDMA + CPB,
  3946. (unsigned char)(info->buffer_list_phys >> 16));
  3947. /* enable status interrupts. other code enables/disables
  3948. * the individual sources for these two interrupt classes.
  3949. */
  3950. info->ie0_value |= TXINTE + RXINTE;
  3951. write_reg(info, IE0, info->ie0_value);
  3952. /* CTL, MSCI control register
  3953. *
  3954. * 07..06 Reserved, set to 0
  3955. * 05 UDRNC, underrun control, 0=abort 1=CRC+flag (HDLC/BSC)
  3956. * 04 IDLC, idle control, 0=mark 1=idle register
  3957. * 03 BRK, break, 0=off 1 =on (async)
  3958. * 02 SYNCLD, sync char load enable (BSC) 1=enabled
  3959. * 01 GOP, go active on poll (LOOP mode) 1=enabled
  3960. * 00 RTS, RTS output control, 0=active 1=inactive
  3961. *
  3962. * 0001 0001
  3963. */
  3964. RegValue = 0x10;
  3965. if (!(info->serial_signals & SerialSignal_RTS))
  3966. RegValue |= 0x01;
  3967. write_reg(info, CTL, RegValue);
  3968. /* preamble not supported ! */
  3969. tx_set_idle(info);
  3970. tx_stop(info);
  3971. rx_stop(info);
  3972. set_rate(info, info->params.clock_speed);
  3973. if (info->params.loopback)
  3974. enable_loopback(info,1);
  3975. }
  3976. /* Set the transmit HDLC idle mode
  3977. */
  3978. void tx_set_idle(SLMP_INFO *info)
  3979. {
  3980. unsigned char RegValue = 0xff;
  3981. /* Map API idle mode to SCA register bits */
  3982. switch(info->idle_mode) {
  3983. case HDLC_TXIDLE_FLAGS: RegValue = 0x7e; break;
  3984. case HDLC_TXIDLE_ALT_ZEROS_ONES: RegValue = 0xaa; break;
  3985. case HDLC_TXIDLE_ZEROS: RegValue = 0x00; break;
  3986. case HDLC_TXIDLE_ONES: RegValue = 0xff; break;
  3987. case HDLC_TXIDLE_ALT_MARK_SPACE: RegValue = 0xaa; break;
  3988. case HDLC_TXIDLE_SPACE: RegValue = 0x00; break;
  3989. case HDLC_TXIDLE_MARK: RegValue = 0xff; break;
  3990. }
  3991. write_reg(info, IDL, RegValue);
  3992. }
  3993. /* Query the adapter for the state of the V24 status (input) signals.
  3994. */
  3995. void get_signals(SLMP_INFO *info)
  3996. {
  3997. u16 status = read_reg(info, SR3);
  3998. u16 gpstatus = read_status_reg(info);
  3999. u16 testbit;
  4000. /* clear all serial signals except DTR and RTS */
  4001. info->serial_signals &= SerialSignal_DTR + SerialSignal_RTS;
  4002. /* set serial signal bits to reflect MISR */
  4003. if (!(status & BIT3))
  4004. info->serial_signals |= SerialSignal_CTS;
  4005. if ( !(status & BIT2))
  4006. info->serial_signals |= SerialSignal_DCD;
  4007. testbit = BIT1 << (info->port_num * 2); // Port 0..3 RI is GPDATA<1,3,5,7>
  4008. if (!(gpstatus & testbit))
  4009. info->serial_signals |= SerialSignal_RI;
  4010. testbit = BIT0 << (info->port_num * 2); // Port 0..3 DSR is GPDATA<0,2,4,6>
  4011. if (!(gpstatus & testbit))
  4012. info->serial_signals |= SerialSignal_DSR;
  4013. }
  4014. /* Set the state of DTR and RTS based on contents of
  4015. * serial_signals member of device context.
  4016. */
  4017. void set_signals(SLMP_INFO *info)
  4018. {
  4019. unsigned char RegValue;
  4020. u16 EnableBit;
  4021. RegValue = read_reg(info, CTL);
  4022. if (info->serial_signals & SerialSignal_RTS)
  4023. RegValue &= ~BIT0;
  4024. else
  4025. RegValue |= BIT0;
  4026. write_reg(info, CTL, RegValue);
  4027. // Port 0..3 DTR is ctrl reg <1,3,5,7>
  4028. EnableBit = BIT1 << (info->port_num*2);
  4029. if (info->serial_signals & SerialSignal_DTR)
  4030. info->port_array[0]->ctrlreg_value &= ~EnableBit;
  4031. else
  4032. info->port_array[0]->ctrlreg_value |= EnableBit;
  4033. write_control_reg(info);
  4034. }
  4035. /*******************/
  4036. /* DMA Buffer Code */
  4037. /*******************/
  4038. /* Set the count for all receive buffers to SCABUFSIZE
  4039. * and set the current buffer to the first buffer. This effectively
  4040. * makes all buffers free and discards any data in buffers.
  4041. */
  4042. void rx_reset_buffers(SLMP_INFO *info)
  4043. {
  4044. rx_free_frame_buffers(info, 0, info->rx_buf_count - 1);
  4045. }
  4046. /* Free the buffers used by a received frame
  4047. *
  4048. * info pointer to device instance data
  4049. * first index of 1st receive buffer of frame
  4050. * last index of last receive buffer of frame
  4051. */
  4052. void rx_free_frame_buffers(SLMP_INFO *info, unsigned int first, unsigned int last)
  4053. {
  4054. int done = 0;
  4055. while(!done) {
  4056. /* reset current buffer for reuse */
  4057. info->rx_buf_list[first].status = 0xff;
  4058. if (first == last) {
  4059. done = 1;
  4060. /* set new last rx descriptor address */
  4061. write_reg16(info, RXDMA + EDA, info->rx_buf_list_ex[first].phys_entry);
  4062. }
  4063. first++;
  4064. if (first == info->rx_buf_count)
  4065. first = 0;
  4066. }
  4067. /* set current buffer to next buffer after last buffer of frame */
  4068. info->current_rx_buf = first;
  4069. }
  4070. /* Return a received frame from the receive DMA buffers.
  4071. * Only frames received without errors are returned.
  4072. *
  4073. * Return Value: 1 if frame returned, otherwise 0
  4074. */
  4075. int rx_get_frame(SLMP_INFO *info)
  4076. {
  4077. unsigned int StartIndex, EndIndex; /* index of 1st and last buffers of Rx frame */
  4078. unsigned short status;
  4079. unsigned int framesize = 0;
  4080. int ReturnCode = 0;
  4081. unsigned long flags;
  4082. struct tty_struct *tty = info->tty;
  4083. unsigned char addr_field = 0xff;
  4084. SCADESC *desc;
  4085. SCADESC_EX *desc_ex;
  4086. CheckAgain:
  4087. /* assume no frame returned, set zero length */
  4088. framesize = 0;
  4089. addr_field = 0xff;
  4090. /*
  4091. * current_rx_buf points to the 1st buffer of the next available
  4092. * receive frame. To find the last buffer of the frame look for
  4093. * a non-zero status field in the buffer entries. (The status
  4094. * field is set by the 16C32 after completing a receive frame.
  4095. */
  4096. StartIndex = EndIndex = info->current_rx_buf;
  4097. for ( ;; ) {
  4098. desc = &info->rx_buf_list[EndIndex];
  4099. desc_ex = &info->rx_buf_list_ex[EndIndex];
  4100. if (desc->status == 0xff)
  4101. goto Cleanup; /* current desc still in use, no frames available */
  4102. if (framesize == 0 && info->params.addr_filter != 0xff)
  4103. addr_field = desc_ex->virt_addr[0];
  4104. framesize += desc->length;
  4105. /* Status != 0 means last buffer of frame */
  4106. if (desc->status)
  4107. break;
  4108. EndIndex++;
  4109. if (EndIndex == info->rx_buf_count)
  4110. EndIndex = 0;
  4111. if (EndIndex == info->current_rx_buf) {
  4112. /* all buffers have been 'used' but none mark */
  4113. /* the end of a frame. Reset buffers and receiver. */
  4114. if ( info->rx_enabled ){
  4115. spin_lock_irqsave(&info->lock,flags);
  4116. rx_start(info);
  4117. spin_unlock_irqrestore(&info->lock,flags);
  4118. }
  4119. goto Cleanup;
  4120. }
  4121. }
  4122. /* check status of receive frame */
  4123. /* frame status is byte stored after frame data
  4124. *
  4125. * 7 EOM (end of msg), 1 = last buffer of frame
  4126. * 6 Short Frame, 1 = short frame
  4127. * 5 Abort, 1 = frame aborted
  4128. * 4 Residue, 1 = last byte is partial
  4129. * 3 Overrun, 1 = overrun occurred during frame reception
  4130. * 2 CRC, 1 = CRC error detected
  4131. *
  4132. */
  4133. status = desc->status;
  4134. /* ignore CRC bit if not using CRC (bit is undefined) */
  4135. /* Note:CRC is not save to data buffer */
  4136. if (info->params.crc_type == HDLC_CRC_NONE)
  4137. status &= ~BIT2;
  4138. if (framesize == 0 ||
  4139. (addr_field != 0xff && addr_field != info->params.addr_filter)) {
  4140. /* discard 0 byte frames, this seems to occur sometime
  4141. * when remote is idling flags.
  4142. */
  4143. rx_free_frame_buffers(info, StartIndex, EndIndex);
  4144. goto CheckAgain;
  4145. }
  4146. if (framesize < 2)
  4147. status |= BIT6;
  4148. if (status & (BIT6+BIT5+BIT3+BIT2)) {
  4149. /* received frame has errors,
  4150. * update counts and mark frame size as 0
  4151. */
  4152. if (status & BIT6)
  4153. info->icount.rxshort++;
  4154. else if (status & BIT5)
  4155. info->icount.rxabort++;
  4156. else if (status & BIT3)
  4157. info->icount.rxover++;
  4158. else
  4159. info->icount.rxcrc++;
  4160. framesize = 0;
  4161. #if SYNCLINK_GENERIC_HDLC
  4162. {
  4163. struct net_device_stats *stats = hdlc_stats(info->netdev);
  4164. stats->rx_errors++;
  4165. stats->rx_frame_errors++;
  4166. }
  4167. #endif
  4168. }
  4169. if ( debug_level >= DEBUG_LEVEL_BH )
  4170. printk("%s(%d):%s rx_get_frame() status=%04X size=%d\n",
  4171. __FILE__,__LINE__,info->device_name,status,framesize);
  4172. if ( debug_level >= DEBUG_LEVEL_DATA )
  4173. trace_block(info,info->rx_buf_list_ex[StartIndex].virt_addr,
  4174. min_t(int, framesize,SCABUFSIZE),0);
  4175. if (framesize) {
  4176. if (framesize > info->max_frame_size)
  4177. info->icount.rxlong++;
  4178. else {
  4179. /* copy dma buffer(s) to contiguous intermediate buffer */
  4180. int copy_count = framesize;
  4181. int index = StartIndex;
  4182. unsigned char *ptmp = info->tmp_rx_buf;
  4183. info->tmp_rx_buf_count = framesize;
  4184. info->icount.rxok++;
  4185. while(copy_count) {
  4186. int partial_count = min(copy_count,SCABUFSIZE);
  4187. memcpy( ptmp,
  4188. info->rx_buf_list_ex[index].virt_addr,
  4189. partial_count );
  4190. ptmp += partial_count;
  4191. copy_count -= partial_count;
  4192. if ( ++index == info->rx_buf_count )
  4193. index = 0;
  4194. }
  4195. #if SYNCLINK_GENERIC_HDLC
  4196. if (info->netcount)
  4197. hdlcdev_rx(info,info->tmp_rx_buf,framesize);
  4198. else
  4199. #endif
  4200. ldisc_receive_buf(tty,info->tmp_rx_buf,
  4201. info->flag_buf, framesize);
  4202. }
  4203. }
  4204. /* Free the buffers used by this frame. */
  4205. rx_free_frame_buffers( info, StartIndex, EndIndex );
  4206. ReturnCode = 1;
  4207. Cleanup:
  4208. if ( info->rx_enabled && info->rx_overflow ) {
  4209. /* Receiver is enabled, but needs to restarted due to
  4210. * rx buffer overflow. If buffers are empty, restart receiver.
  4211. */
  4212. if (info->rx_buf_list[EndIndex].status == 0xff) {
  4213. spin_lock_irqsave(&info->lock,flags);
  4214. rx_start(info);
  4215. spin_unlock_irqrestore(&info->lock,flags);
  4216. }
  4217. }
  4218. return ReturnCode;
  4219. }
  4220. /* load the transmit DMA buffer with data
  4221. */
  4222. void tx_load_dma_buffer(SLMP_INFO *info, const char *buf, unsigned int count)
  4223. {
  4224. unsigned short copy_count;
  4225. unsigned int i = 0;
  4226. SCADESC *desc;
  4227. SCADESC_EX *desc_ex;
  4228. if ( debug_level >= DEBUG_LEVEL_DATA )
  4229. trace_block(info,buf, min_t(int, count,SCABUFSIZE), 1);
  4230. /* Copy source buffer to one or more DMA buffers, starting with
  4231. * the first transmit dma buffer.
  4232. */
  4233. for(i=0;;)
  4234. {
  4235. copy_count = min_t(unsigned short,count,SCABUFSIZE);
  4236. desc = &info->tx_buf_list[i];
  4237. desc_ex = &info->tx_buf_list_ex[i];
  4238. load_pci_memory(info, desc_ex->virt_addr,buf,copy_count);
  4239. desc->length = copy_count;
  4240. desc->status = 0;
  4241. buf += copy_count;
  4242. count -= copy_count;
  4243. if (!count)
  4244. break;
  4245. i++;
  4246. if (i >= info->tx_buf_count)
  4247. i = 0;
  4248. }
  4249. info->tx_buf_list[i].status = 0x81; /* set EOM and EOT status */
  4250. info->last_tx_buf = ++i;
  4251. }
  4252. int register_test(SLMP_INFO *info)
  4253. {
  4254. static unsigned char testval[] = {0x00, 0xff, 0xaa, 0x55, 0x69, 0x96};
  4255. static unsigned int count = ARRAY_SIZE(testval);
  4256. unsigned int i;
  4257. int rc = TRUE;
  4258. unsigned long flags;
  4259. spin_lock_irqsave(&info->lock,flags);
  4260. reset_port(info);
  4261. /* assume failure */
  4262. info->init_error = DiagStatus_AddressFailure;
  4263. /* Write bit patterns to various registers but do it out of */
  4264. /* sync, then read back and verify values. */
  4265. for (i = 0 ; i < count ; i++) {
  4266. write_reg(info, TMC, testval[i]);
  4267. write_reg(info, IDL, testval[(i+1)%count]);
  4268. write_reg(info, SA0, testval[(i+2)%count]);
  4269. write_reg(info, SA1, testval[(i+3)%count]);
  4270. if ( (read_reg(info, TMC) != testval[i]) ||
  4271. (read_reg(info, IDL) != testval[(i+1)%count]) ||
  4272. (read_reg(info, SA0) != testval[(i+2)%count]) ||
  4273. (read_reg(info, SA1) != testval[(i+3)%count]) )
  4274. {
  4275. rc = FALSE;
  4276. break;
  4277. }
  4278. }
  4279. reset_port(info);
  4280. spin_unlock_irqrestore(&info->lock,flags);
  4281. return rc;
  4282. }
  4283. int irq_test(SLMP_INFO *info)
  4284. {
  4285. unsigned long timeout;
  4286. unsigned long flags;
  4287. unsigned char timer = (info->port_num & 1) ? TIMER2 : TIMER0;
  4288. spin_lock_irqsave(&info->lock,flags);
  4289. reset_port(info);
  4290. /* assume failure */
  4291. info->init_error = DiagStatus_IrqFailure;
  4292. info->irq_occurred = FALSE;
  4293. /* setup timer0 on SCA0 to interrupt */
  4294. /* IER2<7..4> = timer<3..0> interrupt enables (1=enabled) */
  4295. write_reg(info, IER2, (unsigned char)((info->port_num & 1) ? BIT6 : BIT4));
  4296. write_reg(info, (unsigned char)(timer + TEPR), 0); /* timer expand prescale */
  4297. write_reg16(info, (unsigned char)(timer + TCONR), 1); /* timer constant */
  4298. /* TMCS, Timer Control/Status Register
  4299. *
  4300. * 07 CMF, Compare match flag (read only) 1=match
  4301. * 06 ECMI, CMF Interrupt Enable: 1=enabled
  4302. * 05 Reserved, must be 0
  4303. * 04 TME, Timer Enable
  4304. * 03..00 Reserved, must be 0
  4305. *
  4306. * 0101 0000
  4307. */
  4308. write_reg(info, (unsigned char)(timer + TMCS), 0x50);
  4309. spin_unlock_irqrestore(&info->lock,flags);
  4310. timeout=100;
  4311. while( timeout-- && !info->irq_occurred ) {
  4312. msleep_interruptible(10);
  4313. }
  4314. spin_lock_irqsave(&info->lock,flags);
  4315. reset_port(info);
  4316. spin_unlock_irqrestore(&info->lock,flags);
  4317. return info->irq_occurred;
  4318. }
  4319. /* initialize individual SCA device (2 ports)
  4320. */
  4321. static int sca_init(SLMP_INFO *info)
  4322. {
  4323. /* set wait controller to single mem partition (low), no wait states */
  4324. write_reg(info, PABR0, 0); /* wait controller addr boundary 0 */
  4325. write_reg(info, PABR1, 0); /* wait controller addr boundary 1 */
  4326. write_reg(info, WCRL, 0); /* wait controller low range */
  4327. write_reg(info, WCRM, 0); /* wait controller mid range */
  4328. write_reg(info, WCRH, 0); /* wait controller high range */
  4329. /* DPCR, DMA Priority Control
  4330. *
  4331. * 07..05 Not used, must be 0
  4332. * 04 BRC, bus release condition: 0=all transfers complete
  4333. * 03 CCC, channel change condition: 0=every cycle
  4334. * 02..00 PR<2..0>, priority 100=round robin
  4335. *
  4336. * 00000100 = 0x04
  4337. */
  4338. write_reg(info, DPCR, dma_priority);
  4339. /* DMA Master Enable, BIT7: 1=enable all channels */
  4340. write_reg(info, DMER, 0x80);
  4341. /* enable all interrupt classes */
  4342. write_reg(info, IER0, 0xff); /* TxRDY,RxRDY,TxINT,RxINT (ports 0-1) */
  4343. write_reg(info, IER1, 0xff); /* DMIB,DMIA (channels 0-3) */
  4344. write_reg(info, IER2, 0xf0); /* TIRQ (timers 0-3) */
  4345. /* ITCR, interrupt control register
  4346. * 07 IPC, interrupt priority, 0=MSCI->DMA
  4347. * 06..05 IAK<1..0>, Acknowledge cycle, 00=non-ack cycle
  4348. * 04 VOS, Vector Output, 0=unmodified vector
  4349. * 03..00 Reserved, must be 0
  4350. */
  4351. write_reg(info, ITCR, 0);
  4352. return TRUE;
  4353. }
  4354. /* initialize adapter hardware
  4355. */
  4356. int init_adapter(SLMP_INFO *info)
  4357. {
  4358. int i;
  4359. /* Set BIT30 of Local Control Reg 0x50 to reset SCA */
  4360. volatile u32 *MiscCtrl = (u32 *)(info->lcr_base + 0x50);
  4361. u32 readval;
  4362. info->misc_ctrl_value |= BIT30;
  4363. *MiscCtrl = info->misc_ctrl_value;
  4364. /*
  4365. * Force at least 170ns delay before clearing
  4366. * reset bit. Each read from LCR takes at least
  4367. * 30ns so 10 times for 300ns to be safe.
  4368. */
  4369. for(i=0;i<10;i++)
  4370. readval = *MiscCtrl;
  4371. info->misc_ctrl_value &= ~BIT30;
  4372. *MiscCtrl = info->misc_ctrl_value;
  4373. /* init control reg (all DTRs off, all clksel=input) */
  4374. info->ctrlreg_value = 0xaa;
  4375. write_control_reg(info);
  4376. {
  4377. volatile u32 *LCR1BRDR = (u32 *)(info->lcr_base + 0x2c);
  4378. lcr1_brdr_value &= ~(BIT5 + BIT4 + BIT3);
  4379. switch(read_ahead_count)
  4380. {
  4381. case 16:
  4382. lcr1_brdr_value |= BIT5 + BIT4 + BIT3;
  4383. break;
  4384. case 8:
  4385. lcr1_brdr_value |= BIT5 + BIT4;
  4386. break;
  4387. case 4:
  4388. lcr1_brdr_value |= BIT5 + BIT3;
  4389. break;
  4390. case 0:
  4391. lcr1_brdr_value |= BIT5;
  4392. break;
  4393. }
  4394. *LCR1BRDR = lcr1_brdr_value;
  4395. *MiscCtrl = misc_ctrl_value;
  4396. }
  4397. sca_init(info->port_array[0]);
  4398. sca_init(info->port_array[2]);
  4399. return TRUE;
  4400. }
  4401. /* Loopback an HDLC frame to test the hardware
  4402. * interrupt and DMA functions.
  4403. */
  4404. int loopback_test(SLMP_INFO *info)
  4405. {
  4406. #define TESTFRAMESIZE 20
  4407. unsigned long timeout;
  4408. u16 count = TESTFRAMESIZE;
  4409. unsigned char buf[TESTFRAMESIZE];
  4410. int rc = FALSE;
  4411. unsigned long flags;
  4412. struct tty_struct *oldtty = info->tty;
  4413. u32 speed = info->params.clock_speed;
  4414. info->params.clock_speed = 3686400;
  4415. info->tty = NULL;
  4416. /* assume failure */
  4417. info->init_error = DiagStatus_DmaFailure;
  4418. /* build and send transmit frame */
  4419. for (count = 0; count < TESTFRAMESIZE;++count)
  4420. buf[count] = (unsigned char)count;
  4421. memset(info->tmp_rx_buf,0,TESTFRAMESIZE);
  4422. /* program hardware for HDLC and enabled receiver */
  4423. spin_lock_irqsave(&info->lock,flags);
  4424. hdlc_mode(info);
  4425. enable_loopback(info,1);
  4426. rx_start(info);
  4427. info->tx_count = count;
  4428. tx_load_dma_buffer(info,buf,count);
  4429. tx_start(info);
  4430. spin_unlock_irqrestore(&info->lock,flags);
  4431. /* wait for receive complete */
  4432. /* Set a timeout for waiting for interrupt. */
  4433. for ( timeout = 100; timeout; --timeout ) {
  4434. msleep_interruptible(10);
  4435. if (rx_get_frame(info)) {
  4436. rc = TRUE;
  4437. break;
  4438. }
  4439. }
  4440. /* verify received frame length and contents */
  4441. if (rc == TRUE &&
  4442. ( info->tmp_rx_buf_count != count ||
  4443. memcmp(buf, info->tmp_rx_buf,count))) {
  4444. rc = FALSE;
  4445. }
  4446. spin_lock_irqsave(&info->lock,flags);
  4447. reset_adapter(info);
  4448. spin_unlock_irqrestore(&info->lock,flags);
  4449. info->params.clock_speed = speed;
  4450. info->tty = oldtty;
  4451. return rc;
  4452. }
  4453. /* Perform diagnostics on hardware
  4454. */
  4455. int adapter_test( SLMP_INFO *info )
  4456. {
  4457. unsigned long flags;
  4458. if ( debug_level >= DEBUG_LEVEL_INFO )
  4459. printk( "%s(%d):Testing device %s\n",
  4460. __FILE__,__LINE__,info->device_name );
  4461. spin_lock_irqsave(&info->lock,flags);
  4462. init_adapter(info);
  4463. spin_unlock_irqrestore(&info->lock,flags);
  4464. info->port_array[0]->port_count = 0;
  4465. if ( register_test(info->port_array[0]) &&
  4466. register_test(info->port_array[1])) {
  4467. info->port_array[0]->port_count = 2;
  4468. if ( register_test(info->port_array[2]) &&
  4469. register_test(info->port_array[3]) )
  4470. info->port_array[0]->port_count += 2;
  4471. }
  4472. else {
  4473. printk( "%s(%d):Register test failure for device %s Addr=%08lX\n",
  4474. __FILE__,__LINE__,info->device_name, (unsigned long)(info->phys_sca_base));
  4475. return -ENODEV;
  4476. }
  4477. if ( !irq_test(info->port_array[0]) ||
  4478. !irq_test(info->port_array[1]) ||
  4479. (info->port_count == 4 && !irq_test(info->port_array[2])) ||
  4480. (info->port_count == 4 && !irq_test(info->port_array[3]))) {
  4481. printk( "%s(%d):Interrupt test failure for device %s IRQ=%d\n",
  4482. __FILE__,__LINE__,info->device_name, (unsigned short)(info->irq_level) );
  4483. return -ENODEV;
  4484. }
  4485. if (!loopback_test(info->port_array[0]) ||
  4486. !loopback_test(info->port_array[1]) ||
  4487. (info->port_count == 4 && !loopback_test(info->port_array[2])) ||
  4488. (info->port_count == 4 && !loopback_test(info->port_array[3]))) {
  4489. printk( "%s(%d):DMA test failure for device %s\n",
  4490. __FILE__,__LINE__,info->device_name);
  4491. return -ENODEV;
  4492. }
  4493. if ( debug_level >= DEBUG_LEVEL_INFO )
  4494. printk( "%s(%d):device %s passed diagnostics\n",
  4495. __FILE__,__LINE__,info->device_name );
  4496. info->port_array[0]->init_error = 0;
  4497. info->port_array[1]->init_error = 0;
  4498. if ( info->port_count > 2 ) {
  4499. info->port_array[2]->init_error = 0;
  4500. info->port_array[3]->init_error = 0;
  4501. }
  4502. return 0;
  4503. }
  4504. /* Test the shared memory on a PCI adapter.
  4505. */
  4506. int memory_test(SLMP_INFO *info)
  4507. {
  4508. static unsigned long testval[] = { 0x0, 0x55555555, 0xaaaaaaaa,
  4509. 0x66666666, 0x99999999, 0xffffffff, 0x12345678 };
  4510. unsigned long count = ARRAY_SIZE(testval);
  4511. unsigned long i;
  4512. unsigned long limit = SCA_MEM_SIZE/sizeof(unsigned long);
  4513. unsigned long * addr = (unsigned long *)info->memory_base;
  4514. /* Test data lines with test pattern at one location. */
  4515. for ( i = 0 ; i < count ; i++ ) {
  4516. *addr = testval[i];
  4517. if ( *addr != testval[i] )
  4518. return FALSE;
  4519. }
  4520. /* Test address lines with incrementing pattern over */
  4521. /* entire address range. */
  4522. for ( i = 0 ; i < limit ; i++ ) {
  4523. *addr = i * 4;
  4524. addr++;
  4525. }
  4526. addr = (unsigned long *)info->memory_base;
  4527. for ( i = 0 ; i < limit ; i++ ) {
  4528. if ( *addr != i * 4 )
  4529. return FALSE;
  4530. addr++;
  4531. }
  4532. memset( info->memory_base, 0, SCA_MEM_SIZE );
  4533. return TRUE;
  4534. }
  4535. /* Load data into PCI adapter shared memory.
  4536. *
  4537. * The PCI9050 releases control of the local bus
  4538. * after completing the current read or write operation.
  4539. *
  4540. * While the PCI9050 write FIFO not empty, the
  4541. * PCI9050 treats all of the writes as a single transaction
  4542. * and does not release the bus. This causes DMA latency problems
  4543. * at high speeds when copying large data blocks to the shared memory.
  4544. *
  4545. * This function breaks a write into multiple transations by
  4546. * interleaving a read which flushes the write FIFO and 'completes'
  4547. * the write transation. This allows any pending DMA request to gain control
  4548. * of the local bus in a timely fasion.
  4549. */
  4550. void load_pci_memory(SLMP_INFO *info, char* dest, const char* src, unsigned short count)
  4551. {
  4552. /* A load interval of 16 allows for 4 32-bit writes at */
  4553. /* 136ns each for a maximum latency of 542ns on the local bus.*/
  4554. unsigned short interval = count / sca_pci_load_interval;
  4555. unsigned short i;
  4556. for ( i = 0 ; i < interval ; i++ )
  4557. {
  4558. memcpy(dest, src, sca_pci_load_interval);
  4559. read_status_reg(info);
  4560. dest += sca_pci_load_interval;
  4561. src += sca_pci_load_interval;
  4562. }
  4563. memcpy(dest, src, count % sca_pci_load_interval);
  4564. }
  4565. void trace_block(SLMP_INFO *info,const char* data, int count, int xmit)
  4566. {
  4567. int i;
  4568. int linecount;
  4569. if (xmit)
  4570. printk("%s tx data:\n",info->device_name);
  4571. else
  4572. printk("%s rx data:\n",info->device_name);
  4573. while(count) {
  4574. if (count > 16)
  4575. linecount = 16;
  4576. else
  4577. linecount = count;
  4578. for(i=0;i<linecount;i++)
  4579. printk("%02X ",(unsigned char)data[i]);
  4580. for(;i<17;i++)
  4581. printk(" ");
  4582. for(i=0;i<linecount;i++) {
  4583. if (data[i]>=040 && data[i]<=0176)
  4584. printk("%c",data[i]);
  4585. else
  4586. printk(".");
  4587. }
  4588. printk("\n");
  4589. data += linecount;
  4590. count -= linecount;
  4591. }
  4592. } /* end of trace_block() */
  4593. /* called when HDLC frame times out
  4594. * update stats and do tx completion processing
  4595. */
  4596. void tx_timeout(unsigned long context)
  4597. {
  4598. SLMP_INFO *info = (SLMP_INFO*)context;
  4599. unsigned long flags;
  4600. if ( debug_level >= DEBUG_LEVEL_INFO )
  4601. printk( "%s(%d):%s tx_timeout()\n",
  4602. __FILE__,__LINE__,info->device_name);
  4603. if(info->tx_active && info->params.mode == MGSL_MODE_HDLC) {
  4604. info->icount.txtimeout++;
  4605. }
  4606. spin_lock_irqsave(&info->lock,flags);
  4607. info->tx_active = 0;
  4608. info->tx_count = info->tx_put = info->tx_get = 0;
  4609. spin_unlock_irqrestore(&info->lock,flags);
  4610. #if SYNCLINK_GENERIC_HDLC
  4611. if (info->netcount)
  4612. hdlcdev_tx_done(info);
  4613. else
  4614. #endif
  4615. bh_transmit(info);
  4616. }
  4617. /* called to periodically check the DSR/RI modem signal input status
  4618. */
  4619. void status_timeout(unsigned long context)
  4620. {
  4621. u16 status = 0;
  4622. SLMP_INFO *info = (SLMP_INFO*)context;
  4623. unsigned long flags;
  4624. unsigned char delta;
  4625. spin_lock_irqsave(&info->lock,flags);
  4626. get_signals(info);
  4627. spin_unlock_irqrestore(&info->lock,flags);
  4628. /* check for DSR/RI state change */
  4629. delta = info->old_signals ^ info->serial_signals;
  4630. info->old_signals = info->serial_signals;
  4631. if (delta & SerialSignal_DSR)
  4632. status |= MISCSTATUS_DSR_LATCHED|(info->serial_signals&SerialSignal_DSR);
  4633. if (delta & SerialSignal_RI)
  4634. status |= MISCSTATUS_RI_LATCHED|(info->serial_signals&SerialSignal_RI);
  4635. if (delta & SerialSignal_DCD)
  4636. status |= MISCSTATUS_DCD_LATCHED|(info->serial_signals&SerialSignal_DCD);
  4637. if (delta & SerialSignal_CTS)
  4638. status |= MISCSTATUS_CTS_LATCHED|(info->serial_signals&SerialSignal_CTS);
  4639. if (status)
  4640. isr_io_pin(info,status);
  4641. info->status_timer.data = (unsigned long)info;
  4642. info->status_timer.function = status_timeout;
  4643. info->status_timer.expires = jiffies + msecs_to_jiffies(10);
  4644. add_timer(&info->status_timer);
  4645. }
  4646. /* Register Access Routines -
  4647. * All registers are memory mapped
  4648. */
  4649. #define CALC_REGADDR() \
  4650. unsigned char * RegAddr = (unsigned char*)(info->sca_base + Addr); \
  4651. if (info->port_num > 1) \
  4652. RegAddr += 256; /* port 0-1 SCA0, 2-3 SCA1 */ \
  4653. if ( info->port_num & 1) { \
  4654. if (Addr > 0x7f) \
  4655. RegAddr += 0x40; /* DMA access */ \
  4656. else if (Addr > 0x1f && Addr < 0x60) \
  4657. RegAddr += 0x20; /* MSCI access */ \
  4658. }
  4659. unsigned char read_reg(SLMP_INFO * info, unsigned char Addr)
  4660. {
  4661. CALC_REGADDR();
  4662. return *RegAddr;
  4663. }
  4664. void write_reg(SLMP_INFO * info, unsigned char Addr, unsigned char Value)
  4665. {
  4666. CALC_REGADDR();
  4667. *RegAddr = Value;
  4668. }
  4669. u16 read_reg16(SLMP_INFO * info, unsigned char Addr)
  4670. {
  4671. CALC_REGADDR();
  4672. return *((u16 *)RegAddr);
  4673. }
  4674. void write_reg16(SLMP_INFO * info, unsigned char Addr, u16 Value)
  4675. {
  4676. CALC_REGADDR();
  4677. *((u16 *)RegAddr) = Value;
  4678. }
  4679. unsigned char read_status_reg(SLMP_INFO * info)
  4680. {
  4681. unsigned char *RegAddr = (unsigned char *)info->statctrl_base;
  4682. return *RegAddr;
  4683. }
  4684. void write_control_reg(SLMP_INFO * info)
  4685. {
  4686. unsigned char *RegAddr = (unsigned char *)info->statctrl_base;
  4687. *RegAddr = info->port_array[0]->ctrlreg_value;
  4688. }
  4689. static int __devinit synclinkmp_init_one (struct pci_dev *dev,
  4690. const struct pci_device_id *ent)
  4691. {
  4692. if (pci_enable_device(dev)) {
  4693. printk("error enabling pci device %p\n", dev);
  4694. return -EIO;
  4695. }
  4696. device_init( ++synclinkmp_adapter_count, dev );
  4697. return 0;
  4698. }
  4699. static void __devexit synclinkmp_remove_one (struct pci_dev *dev)
  4700. {
  4701. }