synclink_gt.c 131 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151
  1. /*
  2. * Device driver for Microgate SyncLink GT serial adapters.
  3. *
  4. * written by Paul Fulghum for Microgate Corporation
  5. * paulkf@microgate.com
  6. *
  7. * Microgate and SyncLink are trademarks of Microgate Corporation
  8. *
  9. * This code is released under the GNU General Public License (GPL)
  10. *
  11. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  12. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  13. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  14. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  15. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  16. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  17. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  18. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  19. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  20. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  21. * OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. /*
  24. * DEBUG OUTPUT DEFINITIONS
  25. *
  26. * uncomment lines below to enable specific types of debug output
  27. *
  28. * DBGINFO information - most verbose output
  29. * DBGERR serious errors
  30. * DBGBH bottom half service routine debugging
  31. * DBGISR interrupt service routine debugging
  32. * DBGDATA output receive and transmit data
  33. * DBGTBUF output transmit DMA buffers and registers
  34. * DBGRBUF output receive DMA buffers and registers
  35. */
  36. #define DBGINFO(fmt) if (debug_level >= DEBUG_LEVEL_INFO) printk fmt
  37. #define DBGERR(fmt) if (debug_level >= DEBUG_LEVEL_ERROR) printk fmt
  38. #define DBGBH(fmt) if (debug_level >= DEBUG_LEVEL_BH) printk fmt
  39. #define DBGISR(fmt) if (debug_level >= DEBUG_LEVEL_ISR) printk fmt
  40. #define DBGDATA(info, buf, size, label) if (debug_level >= DEBUG_LEVEL_DATA) trace_block((info), (buf), (size), (label))
  41. /*#define DBGTBUF(info) dump_tbufs(info)*/
  42. /*#define DBGRBUF(info) dump_rbufs(info)*/
  43. #include <linux/module.h>
  44. #include <linux/errno.h>
  45. #include <linux/signal.h>
  46. #include <linux/sched.h>
  47. #include <linux/timer.h>
  48. #include <linux/interrupt.h>
  49. #include <linux/pci.h>
  50. #include <linux/tty.h>
  51. #include <linux/tty_flip.h>
  52. #include <linux/serial.h>
  53. #include <linux/major.h>
  54. #include <linux/string.h>
  55. #include <linux/fcntl.h>
  56. #include <linux/ptrace.h>
  57. #include <linux/ioport.h>
  58. #include <linux/mm.h>
  59. #include <linux/seq_file.h>
  60. #include <linux/slab.h>
  61. #include <linux/netdevice.h>
  62. #include <linux/vmalloc.h>
  63. #include <linux/init.h>
  64. #include <linux/delay.h>
  65. #include <linux/ioctl.h>
  66. #include <linux/termios.h>
  67. #include <linux/bitops.h>
  68. #include <linux/workqueue.h>
  69. #include <linux/hdlc.h>
  70. #include <linux/synclink.h>
  71. #include <asm/io.h>
  72. #include <asm/irq.h>
  73. #include <asm/dma.h>
  74. #include <asm/types.h>
  75. #include <linux/uaccess.h>
  76. #if defined(CONFIG_HDLC) || (defined(CONFIG_HDLC_MODULE) && defined(CONFIG_SYNCLINK_GT_MODULE))
  77. #define SYNCLINK_GENERIC_HDLC 1
  78. #else
  79. #define SYNCLINK_GENERIC_HDLC 0
  80. #endif
  81. /*
  82. * module identification
  83. */
  84. static char *driver_name = "SyncLink GT";
  85. static char *slgt_driver_name = "synclink_gt";
  86. static char *tty_dev_prefix = "ttySLG";
  87. MODULE_LICENSE("GPL");
  88. #define MGSL_MAGIC 0x5401
  89. #define MAX_DEVICES 32
  90. static struct pci_device_id pci_table[] = {
  91. {PCI_VENDOR_ID_MICROGATE, SYNCLINK_GT_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
  92. {PCI_VENDOR_ID_MICROGATE, SYNCLINK_GT2_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
  93. {PCI_VENDOR_ID_MICROGATE, SYNCLINK_GT4_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
  94. {PCI_VENDOR_ID_MICROGATE, SYNCLINK_AC_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
  95. {0,}, /* terminate list */
  96. };
  97. MODULE_DEVICE_TABLE(pci, pci_table);
  98. static int init_one(struct pci_dev *dev,const struct pci_device_id *ent);
  99. static void remove_one(struct pci_dev *dev);
  100. static struct pci_driver pci_driver = {
  101. .name = "synclink_gt",
  102. .id_table = pci_table,
  103. .probe = init_one,
  104. .remove = remove_one,
  105. };
  106. static bool pci_registered;
  107. /*
  108. * module configuration and status
  109. */
  110. static struct slgt_info *slgt_device_list;
  111. static int slgt_device_count;
  112. static int ttymajor;
  113. static int debug_level;
  114. static int maxframe[MAX_DEVICES];
  115. module_param(ttymajor, int, 0);
  116. module_param(debug_level, int, 0);
  117. module_param_array(maxframe, int, NULL, 0);
  118. MODULE_PARM_DESC(ttymajor, "TTY major device number override: 0=auto assigned");
  119. MODULE_PARM_DESC(debug_level, "Debug syslog output: 0=disabled, 1 to 5=increasing detail");
  120. MODULE_PARM_DESC(maxframe, "Maximum frame size used by device (4096 to 65535)");
  121. /*
  122. * tty support and callbacks
  123. */
  124. static struct tty_driver *serial_driver;
  125. static int open(struct tty_struct *tty, struct file * filp);
  126. static void close(struct tty_struct *tty, struct file * filp);
  127. static void hangup(struct tty_struct *tty);
  128. static void set_termios(struct tty_struct *tty, struct ktermios *old_termios);
  129. static int write(struct tty_struct *tty, const unsigned char *buf, int count);
  130. static int put_char(struct tty_struct *tty, unsigned char ch);
  131. static void send_xchar(struct tty_struct *tty, char ch);
  132. static void wait_until_sent(struct tty_struct *tty, int timeout);
  133. static int write_room(struct tty_struct *tty);
  134. static void flush_chars(struct tty_struct *tty);
  135. static void flush_buffer(struct tty_struct *tty);
  136. static void tx_hold(struct tty_struct *tty);
  137. static void tx_release(struct tty_struct *tty);
  138. static int ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg);
  139. static int chars_in_buffer(struct tty_struct *tty);
  140. static void throttle(struct tty_struct * tty);
  141. static void unthrottle(struct tty_struct * tty);
  142. static int set_break(struct tty_struct *tty, int break_state);
  143. /*
  144. * generic HDLC support and callbacks
  145. */
  146. #if SYNCLINK_GENERIC_HDLC
  147. #define dev_to_port(D) (dev_to_hdlc(D)->priv)
  148. static void hdlcdev_tx_done(struct slgt_info *info);
  149. static void hdlcdev_rx(struct slgt_info *info, char *buf, int size);
  150. static int hdlcdev_init(struct slgt_info *info);
  151. static void hdlcdev_exit(struct slgt_info *info);
  152. #endif
  153. /*
  154. * device specific structures, macros and functions
  155. */
  156. #define SLGT_MAX_PORTS 4
  157. #define SLGT_REG_SIZE 256
  158. /*
  159. * conditional wait facility
  160. */
  161. struct cond_wait {
  162. struct cond_wait *next;
  163. wait_queue_head_t q;
  164. wait_queue_t wait;
  165. unsigned int data;
  166. };
  167. static void init_cond_wait(struct cond_wait *w, unsigned int data);
  168. static void add_cond_wait(struct cond_wait **head, struct cond_wait *w);
  169. static void remove_cond_wait(struct cond_wait **head, struct cond_wait *w);
  170. static void flush_cond_wait(struct cond_wait **head);
  171. /*
  172. * DMA buffer descriptor and access macros
  173. */
  174. struct slgt_desc
  175. {
  176. __le16 count;
  177. __le16 status;
  178. __le32 pbuf; /* physical address of data buffer */
  179. __le32 next; /* physical address of next descriptor */
  180. /* driver book keeping */
  181. char *buf; /* virtual address of data buffer */
  182. unsigned int pdesc; /* physical address of this descriptor */
  183. dma_addr_t buf_dma_addr;
  184. unsigned short buf_count;
  185. };
  186. #define set_desc_buffer(a,b) (a).pbuf = cpu_to_le32((unsigned int)(b))
  187. #define set_desc_next(a,b) (a).next = cpu_to_le32((unsigned int)(b))
  188. #define set_desc_count(a,b)(a).count = cpu_to_le16((unsigned short)(b))
  189. #define set_desc_eof(a,b) (a).status = cpu_to_le16((b) ? (le16_to_cpu((a).status) | BIT0) : (le16_to_cpu((a).status) & ~BIT0))
  190. #define set_desc_status(a, b) (a).status = cpu_to_le16((unsigned short)(b))
  191. #define desc_count(a) (le16_to_cpu((a).count))
  192. #define desc_status(a) (le16_to_cpu((a).status))
  193. #define desc_complete(a) (le16_to_cpu((a).status) & BIT15)
  194. #define desc_eof(a) (le16_to_cpu((a).status) & BIT2)
  195. #define desc_crc_error(a) (le16_to_cpu((a).status) & BIT1)
  196. #define desc_abort(a) (le16_to_cpu((a).status) & BIT0)
  197. #define desc_residue(a) ((le16_to_cpu((a).status) & 0x38) >> 3)
  198. struct _input_signal_events {
  199. int ri_up;
  200. int ri_down;
  201. int dsr_up;
  202. int dsr_down;
  203. int dcd_up;
  204. int dcd_down;
  205. int cts_up;
  206. int cts_down;
  207. };
  208. /*
  209. * device instance data structure
  210. */
  211. struct slgt_info {
  212. void *if_ptr; /* General purpose pointer (used by SPPP) */
  213. struct tty_port port;
  214. struct slgt_info *next_device; /* device list link */
  215. int magic;
  216. char device_name[25];
  217. struct pci_dev *pdev;
  218. int port_count; /* count of ports on adapter */
  219. int adapter_num; /* adapter instance number */
  220. int port_num; /* port instance number */
  221. /* array of pointers to port contexts on this adapter */
  222. struct slgt_info *port_array[SLGT_MAX_PORTS];
  223. int line; /* tty line instance number */
  224. struct mgsl_icount icount;
  225. int timeout;
  226. int x_char; /* xon/xoff character */
  227. unsigned int read_status_mask;
  228. unsigned int ignore_status_mask;
  229. wait_queue_head_t status_event_wait_q;
  230. wait_queue_head_t event_wait_q;
  231. struct timer_list tx_timer;
  232. struct timer_list rx_timer;
  233. unsigned int gpio_present;
  234. struct cond_wait *gpio_wait_q;
  235. spinlock_t lock; /* spinlock for synchronizing with ISR */
  236. struct work_struct task;
  237. u32 pending_bh;
  238. bool bh_requested;
  239. bool bh_running;
  240. int isr_overflow;
  241. bool irq_requested; /* true if IRQ requested */
  242. bool irq_occurred; /* for diagnostics use */
  243. /* device configuration */
  244. unsigned int bus_type;
  245. unsigned int irq_level;
  246. unsigned long irq_flags;
  247. unsigned char __iomem * reg_addr; /* memory mapped registers address */
  248. u32 phys_reg_addr;
  249. bool reg_addr_requested;
  250. MGSL_PARAMS params; /* communications parameters */
  251. u32 idle_mode;
  252. u32 max_frame_size; /* as set by device config */
  253. unsigned int rbuf_fill_level;
  254. unsigned int rx_pio;
  255. unsigned int if_mode;
  256. unsigned int base_clock;
  257. unsigned int xsync;
  258. unsigned int xctrl;
  259. /* device status */
  260. bool rx_enabled;
  261. bool rx_restart;
  262. bool tx_enabled;
  263. bool tx_active;
  264. unsigned char signals; /* serial signal states */
  265. int init_error; /* initialization error */
  266. unsigned char *tx_buf;
  267. int tx_count;
  268. char *flag_buf;
  269. bool drop_rts_on_tx_done;
  270. struct _input_signal_events input_signal_events;
  271. int dcd_chkcount; /* check counts to prevent */
  272. int cts_chkcount; /* too many IRQs if a signal */
  273. int dsr_chkcount; /* is floating */
  274. int ri_chkcount;
  275. char *bufs; /* virtual address of DMA buffer lists */
  276. dma_addr_t bufs_dma_addr; /* physical address of buffer descriptors */
  277. unsigned int rbuf_count;
  278. struct slgt_desc *rbufs;
  279. unsigned int rbuf_current;
  280. unsigned int rbuf_index;
  281. unsigned int rbuf_fill_index;
  282. unsigned short rbuf_fill_count;
  283. unsigned int tbuf_count;
  284. struct slgt_desc *tbufs;
  285. unsigned int tbuf_current;
  286. unsigned int tbuf_start;
  287. unsigned char *tmp_rbuf;
  288. unsigned int tmp_rbuf_count;
  289. /* SPPP/Cisco HDLC device parts */
  290. int netcount;
  291. spinlock_t netlock;
  292. #if SYNCLINK_GENERIC_HDLC
  293. struct net_device *netdev;
  294. #endif
  295. };
  296. static MGSL_PARAMS default_params = {
  297. .mode = MGSL_MODE_HDLC,
  298. .loopback = 0,
  299. .flags = HDLC_FLAG_UNDERRUN_ABORT15,
  300. .encoding = HDLC_ENCODING_NRZI_SPACE,
  301. .clock_speed = 0,
  302. .addr_filter = 0xff,
  303. .crc_type = HDLC_CRC_16_CCITT,
  304. .preamble_length = HDLC_PREAMBLE_LENGTH_8BITS,
  305. .preamble = HDLC_PREAMBLE_PATTERN_NONE,
  306. .data_rate = 9600,
  307. .data_bits = 8,
  308. .stop_bits = 1,
  309. .parity = ASYNC_PARITY_NONE
  310. };
  311. #define BH_RECEIVE 1
  312. #define BH_TRANSMIT 2
  313. #define BH_STATUS 4
  314. #define IO_PIN_SHUTDOWN_LIMIT 100
  315. #define DMABUFSIZE 256
  316. #define DESC_LIST_SIZE 4096
  317. #define MASK_PARITY BIT1
  318. #define MASK_FRAMING BIT0
  319. #define MASK_BREAK BIT14
  320. #define MASK_OVERRUN BIT4
  321. #define GSR 0x00 /* global status */
  322. #define JCR 0x04 /* JTAG control */
  323. #define IODR 0x08 /* GPIO direction */
  324. #define IOER 0x0c /* GPIO interrupt enable */
  325. #define IOVR 0x10 /* GPIO value */
  326. #define IOSR 0x14 /* GPIO interrupt status */
  327. #define TDR 0x80 /* tx data */
  328. #define RDR 0x80 /* rx data */
  329. #define TCR 0x82 /* tx control */
  330. #define TIR 0x84 /* tx idle */
  331. #define TPR 0x85 /* tx preamble */
  332. #define RCR 0x86 /* rx control */
  333. #define VCR 0x88 /* V.24 control */
  334. #define CCR 0x89 /* clock control */
  335. #define BDR 0x8a /* baud divisor */
  336. #define SCR 0x8c /* serial control */
  337. #define SSR 0x8e /* serial status */
  338. #define RDCSR 0x90 /* rx DMA control/status */
  339. #define TDCSR 0x94 /* tx DMA control/status */
  340. #define RDDAR 0x98 /* rx DMA descriptor address */
  341. #define TDDAR 0x9c /* tx DMA descriptor address */
  342. #define XSR 0x40 /* extended sync pattern */
  343. #define XCR 0x44 /* extended control */
  344. #define RXIDLE BIT14
  345. #define RXBREAK BIT14
  346. #define IRQ_TXDATA BIT13
  347. #define IRQ_TXIDLE BIT12
  348. #define IRQ_TXUNDER BIT11 /* HDLC */
  349. #define IRQ_RXDATA BIT10
  350. #define IRQ_RXIDLE BIT9 /* HDLC */
  351. #define IRQ_RXBREAK BIT9 /* async */
  352. #define IRQ_RXOVER BIT8
  353. #define IRQ_DSR BIT7
  354. #define IRQ_CTS BIT6
  355. #define IRQ_DCD BIT5
  356. #define IRQ_RI BIT4
  357. #define IRQ_ALL 0x3ff0
  358. #define IRQ_MASTER BIT0
  359. #define slgt_irq_on(info, mask) \
  360. wr_reg16((info), SCR, (unsigned short)(rd_reg16((info), SCR) | (mask)))
  361. #define slgt_irq_off(info, mask) \
  362. wr_reg16((info), SCR, (unsigned short)(rd_reg16((info), SCR) & ~(mask)))
  363. static __u8 rd_reg8(struct slgt_info *info, unsigned int addr);
  364. static void wr_reg8(struct slgt_info *info, unsigned int addr, __u8 value);
  365. static __u16 rd_reg16(struct slgt_info *info, unsigned int addr);
  366. static void wr_reg16(struct slgt_info *info, unsigned int addr, __u16 value);
  367. static __u32 rd_reg32(struct slgt_info *info, unsigned int addr);
  368. static void wr_reg32(struct slgt_info *info, unsigned int addr, __u32 value);
  369. static void msc_set_vcr(struct slgt_info *info);
  370. static int startup(struct slgt_info *info);
  371. static int block_til_ready(struct tty_struct *tty, struct file * filp,struct slgt_info *info);
  372. static void shutdown(struct slgt_info *info);
  373. static void program_hw(struct slgt_info *info);
  374. static void change_params(struct slgt_info *info);
  375. static int register_test(struct slgt_info *info);
  376. static int irq_test(struct slgt_info *info);
  377. static int loopback_test(struct slgt_info *info);
  378. static int adapter_test(struct slgt_info *info);
  379. static void reset_adapter(struct slgt_info *info);
  380. static void reset_port(struct slgt_info *info);
  381. static void async_mode(struct slgt_info *info);
  382. static void sync_mode(struct slgt_info *info);
  383. static void rx_stop(struct slgt_info *info);
  384. static void rx_start(struct slgt_info *info);
  385. static void reset_rbufs(struct slgt_info *info);
  386. static void free_rbufs(struct slgt_info *info, unsigned int first, unsigned int last);
  387. static void rdma_reset(struct slgt_info *info);
  388. static bool rx_get_frame(struct slgt_info *info);
  389. static bool rx_get_buf(struct slgt_info *info);
  390. static void tx_start(struct slgt_info *info);
  391. static void tx_stop(struct slgt_info *info);
  392. static void tx_set_idle(struct slgt_info *info);
  393. static unsigned int free_tbuf_count(struct slgt_info *info);
  394. static unsigned int tbuf_bytes(struct slgt_info *info);
  395. static void reset_tbufs(struct slgt_info *info);
  396. static void tdma_reset(struct slgt_info *info);
  397. static bool tx_load(struct slgt_info *info, const char *buf, unsigned int count);
  398. static void get_signals(struct slgt_info *info);
  399. static void set_signals(struct slgt_info *info);
  400. static void enable_loopback(struct slgt_info *info);
  401. static void set_rate(struct slgt_info *info, u32 data_rate);
  402. static int bh_action(struct slgt_info *info);
  403. static void bh_handler(struct work_struct *work);
  404. static void bh_transmit(struct slgt_info *info);
  405. static void isr_serial(struct slgt_info *info);
  406. static void isr_rdma(struct slgt_info *info);
  407. static void isr_txeom(struct slgt_info *info, unsigned short status);
  408. static void isr_tdma(struct slgt_info *info);
  409. static int alloc_dma_bufs(struct slgt_info *info);
  410. static void free_dma_bufs(struct slgt_info *info);
  411. static int alloc_desc(struct slgt_info *info);
  412. static void free_desc(struct slgt_info *info);
  413. static int alloc_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count);
  414. static void free_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count);
  415. static int alloc_tmp_rbuf(struct slgt_info *info);
  416. static void free_tmp_rbuf(struct slgt_info *info);
  417. static void tx_timeout(unsigned long context);
  418. static void rx_timeout(unsigned long context);
  419. /*
  420. * ioctl handlers
  421. */
  422. static int get_stats(struct slgt_info *info, struct mgsl_icount __user *user_icount);
  423. static int get_params(struct slgt_info *info, MGSL_PARAMS __user *params);
  424. static int set_params(struct slgt_info *info, MGSL_PARAMS __user *params);
  425. static int get_txidle(struct slgt_info *info, int __user *idle_mode);
  426. static int set_txidle(struct slgt_info *info, int idle_mode);
  427. static int tx_enable(struct slgt_info *info, int enable);
  428. static int tx_abort(struct slgt_info *info);
  429. static int rx_enable(struct slgt_info *info, int enable);
  430. static int modem_input_wait(struct slgt_info *info,int arg);
  431. static int wait_mgsl_event(struct slgt_info *info, int __user *mask_ptr);
  432. static int tiocmget(struct tty_struct *tty);
  433. static int tiocmset(struct tty_struct *tty,
  434. unsigned int set, unsigned int clear);
  435. static int set_break(struct tty_struct *tty, int break_state);
  436. static int get_interface(struct slgt_info *info, int __user *if_mode);
  437. static int set_interface(struct slgt_info *info, int if_mode);
  438. static int set_gpio(struct slgt_info *info, struct gpio_desc __user *gpio);
  439. static int get_gpio(struct slgt_info *info, struct gpio_desc __user *gpio);
  440. static int wait_gpio(struct slgt_info *info, struct gpio_desc __user *gpio);
  441. static int get_xsync(struct slgt_info *info, int __user *if_mode);
  442. static int set_xsync(struct slgt_info *info, int if_mode);
  443. static int get_xctrl(struct slgt_info *info, int __user *if_mode);
  444. static int set_xctrl(struct slgt_info *info, int if_mode);
  445. /*
  446. * driver functions
  447. */
  448. static void add_device(struct slgt_info *info);
  449. static void device_init(int adapter_num, struct pci_dev *pdev);
  450. static int claim_resources(struct slgt_info *info);
  451. static void release_resources(struct slgt_info *info);
  452. /*
  453. * DEBUG OUTPUT CODE
  454. */
  455. #ifndef DBGINFO
  456. #define DBGINFO(fmt)
  457. #endif
  458. #ifndef DBGERR
  459. #define DBGERR(fmt)
  460. #endif
  461. #ifndef DBGBH
  462. #define DBGBH(fmt)
  463. #endif
  464. #ifndef DBGISR
  465. #define DBGISR(fmt)
  466. #endif
  467. #ifdef DBGDATA
  468. static void trace_block(struct slgt_info *info, const char *data, int count, const char *label)
  469. {
  470. int i;
  471. int linecount;
  472. printk("%s %s data:\n",info->device_name, label);
  473. while(count) {
  474. linecount = (count > 16) ? 16 : count;
  475. for(i=0; i < linecount; i++)
  476. printk("%02X ",(unsigned char)data[i]);
  477. for(;i<17;i++)
  478. printk(" ");
  479. for(i=0;i<linecount;i++) {
  480. if (data[i]>=040 && data[i]<=0176)
  481. printk("%c",data[i]);
  482. else
  483. printk(".");
  484. }
  485. printk("\n");
  486. data += linecount;
  487. count -= linecount;
  488. }
  489. }
  490. #else
  491. #define DBGDATA(info, buf, size, label)
  492. #endif
  493. #ifdef DBGTBUF
  494. static void dump_tbufs(struct slgt_info *info)
  495. {
  496. int i;
  497. printk("tbuf_current=%d\n", info->tbuf_current);
  498. for (i=0 ; i < info->tbuf_count ; i++) {
  499. printk("%d: count=%04X status=%04X\n",
  500. i, le16_to_cpu(info->tbufs[i].count), le16_to_cpu(info->tbufs[i].status));
  501. }
  502. }
  503. #else
  504. #define DBGTBUF(info)
  505. #endif
  506. #ifdef DBGRBUF
  507. static void dump_rbufs(struct slgt_info *info)
  508. {
  509. int i;
  510. printk("rbuf_current=%d\n", info->rbuf_current);
  511. for (i=0 ; i < info->rbuf_count ; i++) {
  512. printk("%d: count=%04X status=%04X\n",
  513. i, le16_to_cpu(info->rbufs[i].count), le16_to_cpu(info->rbufs[i].status));
  514. }
  515. }
  516. #else
  517. #define DBGRBUF(info)
  518. #endif
  519. static inline int sanity_check(struct slgt_info *info, char *devname, const char *name)
  520. {
  521. #ifdef SANITY_CHECK
  522. if (!info) {
  523. printk("null struct slgt_info for (%s) in %s\n", devname, name);
  524. return 1;
  525. }
  526. if (info->magic != MGSL_MAGIC) {
  527. printk("bad magic number struct slgt_info (%s) in %s\n", devname, name);
  528. return 1;
  529. }
  530. #else
  531. if (!info)
  532. return 1;
  533. #endif
  534. return 0;
  535. }
  536. /**
  537. * line discipline callback wrappers
  538. *
  539. * The wrappers maintain line discipline references
  540. * while calling into the line discipline.
  541. *
  542. * ldisc_receive_buf - pass receive data to line discipline
  543. */
  544. static void ldisc_receive_buf(struct tty_struct *tty,
  545. const __u8 *data, char *flags, int count)
  546. {
  547. struct tty_ldisc *ld;
  548. if (!tty)
  549. return;
  550. ld = tty_ldisc_ref(tty);
  551. if (ld) {
  552. if (ld->ops->receive_buf)
  553. ld->ops->receive_buf(tty, data, flags, count);
  554. tty_ldisc_deref(ld);
  555. }
  556. }
  557. /* tty callbacks */
  558. static int open(struct tty_struct *tty, struct file *filp)
  559. {
  560. struct slgt_info *info;
  561. int retval, line;
  562. unsigned long flags;
  563. line = tty->index;
  564. if (line >= slgt_device_count) {
  565. DBGERR(("%s: open with invalid line #%d.\n", driver_name, line));
  566. return -ENODEV;
  567. }
  568. info = slgt_device_list;
  569. while(info && info->line != line)
  570. info = info->next_device;
  571. if (sanity_check(info, tty->name, "open"))
  572. return -ENODEV;
  573. if (info->init_error) {
  574. DBGERR(("%s init error=%d\n", info->device_name, info->init_error));
  575. return -ENODEV;
  576. }
  577. tty->driver_data = info;
  578. info->port.tty = tty;
  579. DBGINFO(("%s open, old ref count = %d\n", info->device_name, info->port.count));
  580. mutex_lock(&info->port.mutex);
  581. info->port.low_latency = (info->port.flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  582. spin_lock_irqsave(&info->netlock, flags);
  583. if (info->netcount) {
  584. retval = -EBUSY;
  585. spin_unlock_irqrestore(&info->netlock, flags);
  586. mutex_unlock(&info->port.mutex);
  587. goto cleanup;
  588. }
  589. info->port.count++;
  590. spin_unlock_irqrestore(&info->netlock, flags);
  591. if (info->port.count == 1) {
  592. /* 1st open on this device, init hardware */
  593. retval = startup(info);
  594. if (retval < 0) {
  595. mutex_unlock(&info->port.mutex);
  596. goto cleanup;
  597. }
  598. }
  599. mutex_unlock(&info->port.mutex);
  600. retval = block_til_ready(tty, filp, info);
  601. if (retval) {
  602. DBGINFO(("%s block_til_ready rc=%d\n", info->device_name, retval));
  603. goto cleanup;
  604. }
  605. retval = 0;
  606. cleanup:
  607. if (retval) {
  608. if (tty->count == 1)
  609. info->port.tty = NULL; /* tty layer will release tty struct */
  610. if(info->port.count)
  611. info->port.count--;
  612. }
  613. DBGINFO(("%s open rc=%d\n", info->device_name, retval));
  614. return retval;
  615. }
  616. static void close(struct tty_struct *tty, struct file *filp)
  617. {
  618. struct slgt_info *info = tty->driver_data;
  619. if (sanity_check(info, tty->name, "close"))
  620. return;
  621. DBGINFO(("%s close entry, count=%d\n", info->device_name, info->port.count));
  622. if (tty_port_close_start(&info->port, tty, filp) == 0)
  623. goto cleanup;
  624. mutex_lock(&info->port.mutex);
  625. if (tty_port_initialized(&info->port))
  626. wait_until_sent(tty, info->timeout);
  627. flush_buffer(tty);
  628. tty_ldisc_flush(tty);
  629. shutdown(info);
  630. mutex_unlock(&info->port.mutex);
  631. tty_port_close_end(&info->port, tty);
  632. info->port.tty = NULL;
  633. cleanup:
  634. DBGINFO(("%s close exit, count=%d\n", tty->driver->name, info->port.count));
  635. }
  636. static void hangup(struct tty_struct *tty)
  637. {
  638. struct slgt_info *info = tty->driver_data;
  639. unsigned long flags;
  640. if (sanity_check(info, tty->name, "hangup"))
  641. return;
  642. DBGINFO(("%s hangup\n", info->device_name));
  643. flush_buffer(tty);
  644. mutex_lock(&info->port.mutex);
  645. shutdown(info);
  646. spin_lock_irqsave(&info->port.lock, flags);
  647. info->port.count = 0;
  648. info->port.tty = NULL;
  649. spin_unlock_irqrestore(&info->port.lock, flags);
  650. tty_port_set_active(&info->port, 0);
  651. mutex_unlock(&info->port.mutex);
  652. wake_up_interruptible(&info->port.open_wait);
  653. }
  654. static void set_termios(struct tty_struct *tty, struct ktermios *old_termios)
  655. {
  656. struct slgt_info *info = tty->driver_data;
  657. unsigned long flags;
  658. DBGINFO(("%s set_termios\n", tty->driver->name));
  659. change_params(info);
  660. /* Handle transition to B0 status */
  661. if ((old_termios->c_cflag & CBAUD) && !C_BAUD(tty)) {
  662. info->signals &= ~(SerialSignal_RTS | SerialSignal_DTR);
  663. spin_lock_irqsave(&info->lock,flags);
  664. set_signals(info);
  665. spin_unlock_irqrestore(&info->lock,flags);
  666. }
  667. /* Handle transition away from B0 status */
  668. if (!(old_termios->c_cflag & CBAUD) && C_BAUD(tty)) {
  669. info->signals |= SerialSignal_DTR;
  670. if (!C_CRTSCTS(tty) || !tty_throttled(tty))
  671. info->signals |= SerialSignal_RTS;
  672. spin_lock_irqsave(&info->lock,flags);
  673. set_signals(info);
  674. spin_unlock_irqrestore(&info->lock,flags);
  675. }
  676. /* Handle turning off CRTSCTS */
  677. if ((old_termios->c_cflag & CRTSCTS) && !C_CRTSCTS(tty)) {
  678. tty->hw_stopped = 0;
  679. tx_release(tty);
  680. }
  681. }
  682. static void update_tx_timer(struct slgt_info *info)
  683. {
  684. /*
  685. * use worst case speed of 1200bps to calculate transmit timeout
  686. * based on data in buffers (tbuf_bytes) and FIFO (128 bytes)
  687. */
  688. if (info->params.mode == MGSL_MODE_HDLC) {
  689. int timeout = (tbuf_bytes(info) * 7) + 1000;
  690. mod_timer(&info->tx_timer, jiffies + msecs_to_jiffies(timeout));
  691. }
  692. }
  693. static int write(struct tty_struct *tty,
  694. const unsigned char *buf, int count)
  695. {
  696. int ret = 0;
  697. struct slgt_info *info = tty->driver_data;
  698. unsigned long flags;
  699. if (sanity_check(info, tty->name, "write"))
  700. return -EIO;
  701. DBGINFO(("%s write count=%d\n", info->device_name, count));
  702. if (!info->tx_buf || (count > info->max_frame_size))
  703. return -EIO;
  704. if (!count || tty->stopped || tty->hw_stopped)
  705. return 0;
  706. spin_lock_irqsave(&info->lock, flags);
  707. if (info->tx_count) {
  708. /* send accumulated data from send_char() */
  709. if (!tx_load(info, info->tx_buf, info->tx_count))
  710. goto cleanup;
  711. info->tx_count = 0;
  712. }
  713. if (tx_load(info, buf, count))
  714. ret = count;
  715. cleanup:
  716. spin_unlock_irqrestore(&info->lock, flags);
  717. DBGINFO(("%s write rc=%d\n", info->device_name, ret));
  718. return ret;
  719. }
  720. static int put_char(struct tty_struct *tty, unsigned char ch)
  721. {
  722. struct slgt_info *info = tty->driver_data;
  723. unsigned long flags;
  724. int ret = 0;
  725. if (sanity_check(info, tty->name, "put_char"))
  726. return 0;
  727. DBGINFO(("%s put_char(%d)\n", info->device_name, ch));
  728. if (!info->tx_buf)
  729. return 0;
  730. spin_lock_irqsave(&info->lock,flags);
  731. if (info->tx_count < info->max_frame_size) {
  732. info->tx_buf[info->tx_count++] = ch;
  733. ret = 1;
  734. }
  735. spin_unlock_irqrestore(&info->lock,flags);
  736. return ret;
  737. }
  738. static void send_xchar(struct tty_struct *tty, char ch)
  739. {
  740. struct slgt_info *info = tty->driver_data;
  741. unsigned long flags;
  742. if (sanity_check(info, tty->name, "send_xchar"))
  743. return;
  744. DBGINFO(("%s send_xchar(%d)\n", info->device_name, ch));
  745. info->x_char = ch;
  746. if (ch) {
  747. spin_lock_irqsave(&info->lock,flags);
  748. if (!info->tx_enabled)
  749. tx_start(info);
  750. spin_unlock_irqrestore(&info->lock,flags);
  751. }
  752. }
  753. static void wait_until_sent(struct tty_struct *tty, int timeout)
  754. {
  755. struct slgt_info *info = tty->driver_data;
  756. unsigned long orig_jiffies, char_time;
  757. if (!info )
  758. return;
  759. if (sanity_check(info, tty->name, "wait_until_sent"))
  760. return;
  761. DBGINFO(("%s wait_until_sent entry\n", info->device_name));
  762. if (!tty_port_initialized(&info->port))
  763. goto exit;
  764. orig_jiffies = jiffies;
  765. /* Set check interval to 1/5 of estimated time to
  766. * send a character, and make it at least 1. The check
  767. * interval should also be less than the timeout.
  768. * Note: use tight timings here to satisfy the NIST-PCTS.
  769. */
  770. if (info->params.data_rate) {
  771. char_time = info->timeout/(32 * 5);
  772. if (!char_time)
  773. char_time++;
  774. } else
  775. char_time = 1;
  776. if (timeout)
  777. char_time = min_t(unsigned long, char_time, timeout);
  778. while (info->tx_active) {
  779. msleep_interruptible(jiffies_to_msecs(char_time));
  780. if (signal_pending(current))
  781. break;
  782. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  783. break;
  784. }
  785. exit:
  786. DBGINFO(("%s wait_until_sent exit\n", info->device_name));
  787. }
  788. static int write_room(struct tty_struct *tty)
  789. {
  790. struct slgt_info *info = tty->driver_data;
  791. int ret;
  792. if (sanity_check(info, tty->name, "write_room"))
  793. return 0;
  794. ret = (info->tx_active) ? 0 : HDLC_MAX_FRAME_SIZE;
  795. DBGINFO(("%s write_room=%d\n", info->device_name, ret));
  796. return ret;
  797. }
  798. static void flush_chars(struct tty_struct *tty)
  799. {
  800. struct slgt_info *info = tty->driver_data;
  801. unsigned long flags;
  802. if (sanity_check(info, tty->name, "flush_chars"))
  803. return;
  804. DBGINFO(("%s flush_chars entry tx_count=%d\n", info->device_name, info->tx_count));
  805. if (info->tx_count <= 0 || tty->stopped ||
  806. tty->hw_stopped || !info->tx_buf)
  807. return;
  808. DBGINFO(("%s flush_chars start transmit\n", info->device_name));
  809. spin_lock_irqsave(&info->lock,flags);
  810. if (info->tx_count && tx_load(info, info->tx_buf, info->tx_count))
  811. info->tx_count = 0;
  812. spin_unlock_irqrestore(&info->lock,flags);
  813. }
  814. static void flush_buffer(struct tty_struct *tty)
  815. {
  816. struct slgt_info *info = tty->driver_data;
  817. unsigned long flags;
  818. if (sanity_check(info, tty->name, "flush_buffer"))
  819. return;
  820. DBGINFO(("%s flush_buffer\n", info->device_name));
  821. spin_lock_irqsave(&info->lock, flags);
  822. info->tx_count = 0;
  823. spin_unlock_irqrestore(&info->lock, flags);
  824. tty_wakeup(tty);
  825. }
  826. /*
  827. * throttle (stop) transmitter
  828. */
  829. static void tx_hold(struct tty_struct *tty)
  830. {
  831. struct slgt_info *info = tty->driver_data;
  832. unsigned long flags;
  833. if (sanity_check(info, tty->name, "tx_hold"))
  834. return;
  835. DBGINFO(("%s tx_hold\n", info->device_name));
  836. spin_lock_irqsave(&info->lock,flags);
  837. if (info->tx_enabled && info->params.mode == MGSL_MODE_ASYNC)
  838. tx_stop(info);
  839. spin_unlock_irqrestore(&info->lock,flags);
  840. }
  841. /*
  842. * release (start) transmitter
  843. */
  844. static void tx_release(struct tty_struct *tty)
  845. {
  846. struct slgt_info *info = tty->driver_data;
  847. unsigned long flags;
  848. if (sanity_check(info, tty->name, "tx_release"))
  849. return;
  850. DBGINFO(("%s tx_release\n", info->device_name));
  851. spin_lock_irqsave(&info->lock, flags);
  852. if (info->tx_count && tx_load(info, info->tx_buf, info->tx_count))
  853. info->tx_count = 0;
  854. spin_unlock_irqrestore(&info->lock, flags);
  855. }
  856. /*
  857. * Service an IOCTL request
  858. *
  859. * Arguments
  860. *
  861. * tty pointer to tty instance data
  862. * cmd IOCTL command code
  863. * arg command argument/context
  864. *
  865. * Return 0 if success, otherwise error code
  866. */
  867. static int ioctl(struct tty_struct *tty,
  868. unsigned int cmd, unsigned long arg)
  869. {
  870. struct slgt_info *info = tty->driver_data;
  871. void __user *argp = (void __user *)arg;
  872. int ret;
  873. if (sanity_check(info, tty->name, "ioctl"))
  874. return -ENODEV;
  875. DBGINFO(("%s ioctl() cmd=%08X\n", info->device_name, cmd));
  876. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  877. (cmd != TIOCMIWAIT)) {
  878. if (tty_io_error(tty))
  879. return -EIO;
  880. }
  881. switch (cmd) {
  882. case MGSL_IOCWAITEVENT:
  883. return wait_mgsl_event(info, argp);
  884. case TIOCMIWAIT:
  885. return modem_input_wait(info,(int)arg);
  886. case MGSL_IOCSGPIO:
  887. return set_gpio(info, argp);
  888. case MGSL_IOCGGPIO:
  889. return get_gpio(info, argp);
  890. case MGSL_IOCWAITGPIO:
  891. return wait_gpio(info, argp);
  892. case MGSL_IOCGXSYNC:
  893. return get_xsync(info, argp);
  894. case MGSL_IOCSXSYNC:
  895. return set_xsync(info, (int)arg);
  896. case MGSL_IOCGXCTRL:
  897. return get_xctrl(info, argp);
  898. case MGSL_IOCSXCTRL:
  899. return set_xctrl(info, (int)arg);
  900. }
  901. mutex_lock(&info->port.mutex);
  902. switch (cmd) {
  903. case MGSL_IOCGPARAMS:
  904. ret = get_params(info, argp);
  905. break;
  906. case MGSL_IOCSPARAMS:
  907. ret = set_params(info, argp);
  908. break;
  909. case MGSL_IOCGTXIDLE:
  910. ret = get_txidle(info, argp);
  911. break;
  912. case MGSL_IOCSTXIDLE:
  913. ret = set_txidle(info, (int)arg);
  914. break;
  915. case MGSL_IOCTXENABLE:
  916. ret = tx_enable(info, (int)arg);
  917. break;
  918. case MGSL_IOCRXENABLE:
  919. ret = rx_enable(info, (int)arg);
  920. break;
  921. case MGSL_IOCTXABORT:
  922. ret = tx_abort(info);
  923. break;
  924. case MGSL_IOCGSTATS:
  925. ret = get_stats(info, argp);
  926. break;
  927. case MGSL_IOCGIF:
  928. ret = get_interface(info, argp);
  929. break;
  930. case MGSL_IOCSIF:
  931. ret = set_interface(info,(int)arg);
  932. break;
  933. default:
  934. ret = -ENOIOCTLCMD;
  935. }
  936. mutex_unlock(&info->port.mutex);
  937. return ret;
  938. }
  939. static int get_icount(struct tty_struct *tty,
  940. struct serial_icounter_struct *icount)
  941. {
  942. struct slgt_info *info = tty->driver_data;
  943. struct mgsl_icount cnow; /* kernel counter temps */
  944. unsigned long flags;
  945. spin_lock_irqsave(&info->lock,flags);
  946. cnow = info->icount;
  947. spin_unlock_irqrestore(&info->lock,flags);
  948. icount->cts = cnow.cts;
  949. icount->dsr = cnow.dsr;
  950. icount->rng = cnow.rng;
  951. icount->dcd = cnow.dcd;
  952. icount->rx = cnow.rx;
  953. icount->tx = cnow.tx;
  954. icount->frame = cnow.frame;
  955. icount->overrun = cnow.overrun;
  956. icount->parity = cnow.parity;
  957. icount->brk = cnow.brk;
  958. icount->buf_overrun = cnow.buf_overrun;
  959. return 0;
  960. }
  961. /*
  962. * support for 32 bit ioctl calls on 64 bit systems
  963. */
  964. #ifdef CONFIG_COMPAT
  965. static long get_params32(struct slgt_info *info, struct MGSL_PARAMS32 __user *user_params)
  966. {
  967. struct MGSL_PARAMS32 tmp_params;
  968. DBGINFO(("%s get_params32\n", info->device_name));
  969. memset(&tmp_params, 0, sizeof(tmp_params));
  970. tmp_params.mode = (compat_ulong_t)info->params.mode;
  971. tmp_params.loopback = info->params.loopback;
  972. tmp_params.flags = info->params.flags;
  973. tmp_params.encoding = info->params.encoding;
  974. tmp_params.clock_speed = (compat_ulong_t)info->params.clock_speed;
  975. tmp_params.addr_filter = info->params.addr_filter;
  976. tmp_params.crc_type = info->params.crc_type;
  977. tmp_params.preamble_length = info->params.preamble_length;
  978. tmp_params.preamble = info->params.preamble;
  979. tmp_params.data_rate = (compat_ulong_t)info->params.data_rate;
  980. tmp_params.data_bits = info->params.data_bits;
  981. tmp_params.stop_bits = info->params.stop_bits;
  982. tmp_params.parity = info->params.parity;
  983. if (copy_to_user(user_params, &tmp_params, sizeof(struct MGSL_PARAMS32)))
  984. return -EFAULT;
  985. return 0;
  986. }
  987. static long set_params32(struct slgt_info *info, struct MGSL_PARAMS32 __user *new_params)
  988. {
  989. struct MGSL_PARAMS32 tmp_params;
  990. DBGINFO(("%s set_params32\n", info->device_name));
  991. if (copy_from_user(&tmp_params, new_params, sizeof(struct MGSL_PARAMS32)))
  992. return -EFAULT;
  993. spin_lock(&info->lock);
  994. if (tmp_params.mode == MGSL_MODE_BASE_CLOCK) {
  995. info->base_clock = tmp_params.clock_speed;
  996. } else {
  997. info->params.mode = tmp_params.mode;
  998. info->params.loopback = tmp_params.loopback;
  999. info->params.flags = tmp_params.flags;
  1000. info->params.encoding = tmp_params.encoding;
  1001. info->params.clock_speed = tmp_params.clock_speed;
  1002. info->params.addr_filter = tmp_params.addr_filter;
  1003. info->params.crc_type = tmp_params.crc_type;
  1004. info->params.preamble_length = tmp_params.preamble_length;
  1005. info->params.preamble = tmp_params.preamble;
  1006. info->params.data_rate = tmp_params.data_rate;
  1007. info->params.data_bits = tmp_params.data_bits;
  1008. info->params.stop_bits = tmp_params.stop_bits;
  1009. info->params.parity = tmp_params.parity;
  1010. }
  1011. spin_unlock(&info->lock);
  1012. program_hw(info);
  1013. return 0;
  1014. }
  1015. static long slgt_compat_ioctl(struct tty_struct *tty,
  1016. unsigned int cmd, unsigned long arg)
  1017. {
  1018. struct slgt_info *info = tty->driver_data;
  1019. int rc = -ENOIOCTLCMD;
  1020. if (sanity_check(info, tty->name, "compat_ioctl"))
  1021. return -ENODEV;
  1022. DBGINFO(("%s compat_ioctl() cmd=%08X\n", info->device_name, cmd));
  1023. switch (cmd) {
  1024. case MGSL_IOCSPARAMS32:
  1025. rc = set_params32(info, compat_ptr(arg));
  1026. break;
  1027. case MGSL_IOCGPARAMS32:
  1028. rc = get_params32(info, compat_ptr(arg));
  1029. break;
  1030. case MGSL_IOCGPARAMS:
  1031. case MGSL_IOCSPARAMS:
  1032. case MGSL_IOCGTXIDLE:
  1033. case MGSL_IOCGSTATS:
  1034. case MGSL_IOCWAITEVENT:
  1035. case MGSL_IOCGIF:
  1036. case MGSL_IOCSGPIO:
  1037. case MGSL_IOCGGPIO:
  1038. case MGSL_IOCWAITGPIO:
  1039. case MGSL_IOCGXSYNC:
  1040. case MGSL_IOCGXCTRL:
  1041. case MGSL_IOCSTXIDLE:
  1042. case MGSL_IOCTXENABLE:
  1043. case MGSL_IOCRXENABLE:
  1044. case MGSL_IOCTXABORT:
  1045. case TIOCMIWAIT:
  1046. case MGSL_IOCSIF:
  1047. case MGSL_IOCSXSYNC:
  1048. case MGSL_IOCSXCTRL:
  1049. rc = ioctl(tty, cmd, arg);
  1050. break;
  1051. }
  1052. DBGINFO(("%s compat_ioctl() cmd=%08X rc=%d\n", info->device_name, cmd, rc));
  1053. return rc;
  1054. }
  1055. #else
  1056. #define slgt_compat_ioctl NULL
  1057. #endif /* ifdef CONFIG_COMPAT */
  1058. /*
  1059. * proc fs support
  1060. */
  1061. static inline void line_info(struct seq_file *m, struct slgt_info *info)
  1062. {
  1063. char stat_buf[30];
  1064. unsigned long flags;
  1065. seq_printf(m, "%s: IO=%08X IRQ=%d MaxFrameSize=%u\n",
  1066. info->device_name, info->phys_reg_addr,
  1067. info->irq_level, info->max_frame_size);
  1068. /* output current serial signal states */
  1069. spin_lock_irqsave(&info->lock,flags);
  1070. get_signals(info);
  1071. spin_unlock_irqrestore(&info->lock,flags);
  1072. stat_buf[0] = 0;
  1073. stat_buf[1] = 0;
  1074. if (info->signals & SerialSignal_RTS)
  1075. strcat(stat_buf, "|RTS");
  1076. if (info->signals & SerialSignal_CTS)
  1077. strcat(stat_buf, "|CTS");
  1078. if (info->signals & SerialSignal_DTR)
  1079. strcat(stat_buf, "|DTR");
  1080. if (info->signals & SerialSignal_DSR)
  1081. strcat(stat_buf, "|DSR");
  1082. if (info->signals & SerialSignal_DCD)
  1083. strcat(stat_buf, "|CD");
  1084. if (info->signals & SerialSignal_RI)
  1085. strcat(stat_buf, "|RI");
  1086. if (info->params.mode != MGSL_MODE_ASYNC) {
  1087. seq_printf(m, "\tHDLC txok:%d rxok:%d",
  1088. info->icount.txok, info->icount.rxok);
  1089. if (info->icount.txunder)
  1090. seq_printf(m, " txunder:%d", info->icount.txunder);
  1091. if (info->icount.txabort)
  1092. seq_printf(m, " txabort:%d", info->icount.txabort);
  1093. if (info->icount.rxshort)
  1094. seq_printf(m, " rxshort:%d", info->icount.rxshort);
  1095. if (info->icount.rxlong)
  1096. seq_printf(m, " rxlong:%d", info->icount.rxlong);
  1097. if (info->icount.rxover)
  1098. seq_printf(m, " rxover:%d", info->icount.rxover);
  1099. if (info->icount.rxcrc)
  1100. seq_printf(m, " rxcrc:%d", info->icount.rxcrc);
  1101. } else {
  1102. seq_printf(m, "\tASYNC tx:%d rx:%d",
  1103. info->icount.tx, info->icount.rx);
  1104. if (info->icount.frame)
  1105. seq_printf(m, " fe:%d", info->icount.frame);
  1106. if (info->icount.parity)
  1107. seq_printf(m, " pe:%d", info->icount.parity);
  1108. if (info->icount.brk)
  1109. seq_printf(m, " brk:%d", info->icount.brk);
  1110. if (info->icount.overrun)
  1111. seq_printf(m, " oe:%d", info->icount.overrun);
  1112. }
  1113. /* Append serial signal status to end */
  1114. seq_printf(m, " %s\n", stat_buf+1);
  1115. seq_printf(m, "\ttxactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
  1116. info->tx_active,info->bh_requested,info->bh_running,
  1117. info->pending_bh);
  1118. }
  1119. /* Called to print information about devices
  1120. */
  1121. static int synclink_gt_proc_show(struct seq_file *m, void *v)
  1122. {
  1123. struct slgt_info *info;
  1124. seq_puts(m, "synclink_gt driver\n");
  1125. info = slgt_device_list;
  1126. while( info ) {
  1127. line_info(m, info);
  1128. info = info->next_device;
  1129. }
  1130. return 0;
  1131. }
  1132. static int synclink_gt_proc_open(struct inode *inode, struct file *file)
  1133. {
  1134. return single_open(file, synclink_gt_proc_show, NULL);
  1135. }
  1136. static const struct file_operations synclink_gt_proc_fops = {
  1137. .owner = THIS_MODULE,
  1138. .open = synclink_gt_proc_open,
  1139. .read = seq_read,
  1140. .llseek = seq_lseek,
  1141. .release = single_release,
  1142. };
  1143. /*
  1144. * return count of bytes in transmit buffer
  1145. */
  1146. static int chars_in_buffer(struct tty_struct *tty)
  1147. {
  1148. struct slgt_info *info = tty->driver_data;
  1149. int count;
  1150. if (sanity_check(info, tty->name, "chars_in_buffer"))
  1151. return 0;
  1152. count = tbuf_bytes(info);
  1153. DBGINFO(("%s chars_in_buffer()=%d\n", info->device_name, count));
  1154. return count;
  1155. }
  1156. /*
  1157. * signal remote device to throttle send data (our receive data)
  1158. */
  1159. static void throttle(struct tty_struct * tty)
  1160. {
  1161. struct slgt_info *info = tty->driver_data;
  1162. unsigned long flags;
  1163. if (sanity_check(info, tty->name, "throttle"))
  1164. return;
  1165. DBGINFO(("%s throttle\n", info->device_name));
  1166. if (I_IXOFF(tty))
  1167. send_xchar(tty, STOP_CHAR(tty));
  1168. if (C_CRTSCTS(tty)) {
  1169. spin_lock_irqsave(&info->lock,flags);
  1170. info->signals &= ~SerialSignal_RTS;
  1171. set_signals(info);
  1172. spin_unlock_irqrestore(&info->lock,flags);
  1173. }
  1174. }
  1175. /*
  1176. * signal remote device to stop throttling send data (our receive data)
  1177. */
  1178. static void unthrottle(struct tty_struct * tty)
  1179. {
  1180. struct slgt_info *info = tty->driver_data;
  1181. unsigned long flags;
  1182. if (sanity_check(info, tty->name, "unthrottle"))
  1183. return;
  1184. DBGINFO(("%s unthrottle\n", info->device_name));
  1185. if (I_IXOFF(tty)) {
  1186. if (info->x_char)
  1187. info->x_char = 0;
  1188. else
  1189. send_xchar(tty, START_CHAR(tty));
  1190. }
  1191. if (C_CRTSCTS(tty)) {
  1192. spin_lock_irqsave(&info->lock,flags);
  1193. info->signals |= SerialSignal_RTS;
  1194. set_signals(info);
  1195. spin_unlock_irqrestore(&info->lock,flags);
  1196. }
  1197. }
  1198. /*
  1199. * set or clear transmit break condition
  1200. * break_state -1=set break condition, 0=clear
  1201. */
  1202. static int set_break(struct tty_struct *tty, int break_state)
  1203. {
  1204. struct slgt_info *info = tty->driver_data;
  1205. unsigned short value;
  1206. unsigned long flags;
  1207. if (sanity_check(info, tty->name, "set_break"))
  1208. return -EINVAL;
  1209. DBGINFO(("%s set_break(%d)\n", info->device_name, break_state));
  1210. spin_lock_irqsave(&info->lock,flags);
  1211. value = rd_reg16(info, TCR);
  1212. if (break_state == -1)
  1213. value |= BIT6;
  1214. else
  1215. value &= ~BIT6;
  1216. wr_reg16(info, TCR, value);
  1217. spin_unlock_irqrestore(&info->lock,flags);
  1218. return 0;
  1219. }
  1220. #if SYNCLINK_GENERIC_HDLC
  1221. /**
  1222. * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
  1223. * set encoding and frame check sequence (FCS) options
  1224. *
  1225. * dev pointer to network device structure
  1226. * encoding serial encoding setting
  1227. * parity FCS setting
  1228. *
  1229. * returns 0 if success, otherwise error code
  1230. */
  1231. static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
  1232. unsigned short parity)
  1233. {
  1234. struct slgt_info *info = dev_to_port(dev);
  1235. unsigned char new_encoding;
  1236. unsigned short new_crctype;
  1237. /* return error if TTY interface open */
  1238. if (info->port.count)
  1239. return -EBUSY;
  1240. DBGINFO(("%s hdlcdev_attach\n", info->device_name));
  1241. switch (encoding)
  1242. {
  1243. case ENCODING_NRZ: new_encoding = HDLC_ENCODING_NRZ; break;
  1244. case ENCODING_NRZI: new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
  1245. case ENCODING_FM_MARK: new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
  1246. case ENCODING_FM_SPACE: new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
  1247. case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
  1248. default: return -EINVAL;
  1249. }
  1250. switch (parity)
  1251. {
  1252. case PARITY_NONE: new_crctype = HDLC_CRC_NONE; break;
  1253. case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
  1254. case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
  1255. default: return -EINVAL;
  1256. }
  1257. info->params.encoding = new_encoding;
  1258. info->params.crc_type = new_crctype;
  1259. /* if network interface up, reprogram hardware */
  1260. if (info->netcount)
  1261. program_hw(info);
  1262. return 0;
  1263. }
  1264. /**
  1265. * called by generic HDLC layer to send frame
  1266. *
  1267. * skb socket buffer containing HDLC frame
  1268. * dev pointer to network device structure
  1269. */
  1270. static netdev_tx_t hdlcdev_xmit(struct sk_buff *skb,
  1271. struct net_device *dev)
  1272. {
  1273. struct slgt_info *info = dev_to_port(dev);
  1274. unsigned long flags;
  1275. DBGINFO(("%s hdlc_xmit\n", dev->name));
  1276. if (!skb->len)
  1277. return NETDEV_TX_OK;
  1278. /* stop sending until this frame completes */
  1279. netif_stop_queue(dev);
  1280. /* update network statistics */
  1281. dev->stats.tx_packets++;
  1282. dev->stats.tx_bytes += skb->len;
  1283. /* save start time for transmit timeout detection */
  1284. netif_trans_update(dev);
  1285. spin_lock_irqsave(&info->lock, flags);
  1286. tx_load(info, skb->data, skb->len);
  1287. spin_unlock_irqrestore(&info->lock, flags);
  1288. /* done with socket buffer, so free it */
  1289. dev_kfree_skb(skb);
  1290. return NETDEV_TX_OK;
  1291. }
  1292. /**
  1293. * called by network layer when interface enabled
  1294. * claim resources and initialize hardware
  1295. *
  1296. * dev pointer to network device structure
  1297. *
  1298. * returns 0 if success, otherwise error code
  1299. */
  1300. static int hdlcdev_open(struct net_device *dev)
  1301. {
  1302. struct slgt_info *info = dev_to_port(dev);
  1303. int rc;
  1304. unsigned long flags;
  1305. if (!try_module_get(THIS_MODULE))
  1306. return -EBUSY;
  1307. DBGINFO(("%s hdlcdev_open\n", dev->name));
  1308. /* generic HDLC layer open processing */
  1309. rc = hdlc_open(dev);
  1310. if (rc)
  1311. return rc;
  1312. /* arbitrate between network and tty opens */
  1313. spin_lock_irqsave(&info->netlock, flags);
  1314. if (info->port.count != 0 || info->netcount != 0) {
  1315. DBGINFO(("%s hdlc_open busy\n", dev->name));
  1316. spin_unlock_irqrestore(&info->netlock, flags);
  1317. return -EBUSY;
  1318. }
  1319. info->netcount=1;
  1320. spin_unlock_irqrestore(&info->netlock, flags);
  1321. /* claim resources and init adapter */
  1322. if ((rc = startup(info)) != 0) {
  1323. spin_lock_irqsave(&info->netlock, flags);
  1324. info->netcount=0;
  1325. spin_unlock_irqrestore(&info->netlock, flags);
  1326. return rc;
  1327. }
  1328. /* assert RTS and DTR, apply hardware settings */
  1329. info->signals |= SerialSignal_RTS | SerialSignal_DTR;
  1330. program_hw(info);
  1331. /* enable network layer transmit */
  1332. netif_trans_update(dev);
  1333. netif_start_queue(dev);
  1334. /* inform generic HDLC layer of current DCD status */
  1335. spin_lock_irqsave(&info->lock, flags);
  1336. get_signals(info);
  1337. spin_unlock_irqrestore(&info->lock, flags);
  1338. if (info->signals & SerialSignal_DCD)
  1339. netif_carrier_on(dev);
  1340. else
  1341. netif_carrier_off(dev);
  1342. return 0;
  1343. }
  1344. /**
  1345. * called by network layer when interface is disabled
  1346. * shutdown hardware and release resources
  1347. *
  1348. * dev pointer to network device structure
  1349. *
  1350. * returns 0 if success, otherwise error code
  1351. */
  1352. static int hdlcdev_close(struct net_device *dev)
  1353. {
  1354. struct slgt_info *info = dev_to_port(dev);
  1355. unsigned long flags;
  1356. DBGINFO(("%s hdlcdev_close\n", dev->name));
  1357. netif_stop_queue(dev);
  1358. /* shutdown adapter and release resources */
  1359. shutdown(info);
  1360. hdlc_close(dev);
  1361. spin_lock_irqsave(&info->netlock, flags);
  1362. info->netcount=0;
  1363. spin_unlock_irqrestore(&info->netlock, flags);
  1364. module_put(THIS_MODULE);
  1365. return 0;
  1366. }
  1367. /**
  1368. * called by network layer to process IOCTL call to network device
  1369. *
  1370. * dev pointer to network device structure
  1371. * ifr pointer to network interface request structure
  1372. * cmd IOCTL command code
  1373. *
  1374. * returns 0 if success, otherwise error code
  1375. */
  1376. static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1377. {
  1378. const size_t size = sizeof(sync_serial_settings);
  1379. sync_serial_settings new_line;
  1380. sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
  1381. struct slgt_info *info = dev_to_port(dev);
  1382. unsigned int flags;
  1383. DBGINFO(("%s hdlcdev_ioctl\n", dev->name));
  1384. /* return error if TTY interface open */
  1385. if (info->port.count)
  1386. return -EBUSY;
  1387. if (cmd != SIOCWANDEV)
  1388. return hdlc_ioctl(dev, ifr, cmd);
  1389. memset(&new_line, 0, sizeof(new_line));
  1390. switch(ifr->ifr_settings.type) {
  1391. case IF_GET_IFACE: /* return current sync_serial_settings */
  1392. ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
  1393. if (ifr->ifr_settings.size < size) {
  1394. ifr->ifr_settings.size = size; /* data size wanted */
  1395. return -ENOBUFS;
  1396. }
  1397. flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
  1398. HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
  1399. HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
  1400. HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
  1401. switch (flags){
  1402. case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
  1403. case (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_INT; break;
  1404. case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_TXINT; break;
  1405. case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
  1406. default: new_line.clock_type = CLOCK_DEFAULT;
  1407. }
  1408. new_line.clock_rate = info->params.clock_speed;
  1409. new_line.loopback = info->params.loopback ? 1:0;
  1410. if (copy_to_user(line, &new_line, size))
  1411. return -EFAULT;
  1412. return 0;
  1413. case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
  1414. if(!capable(CAP_NET_ADMIN))
  1415. return -EPERM;
  1416. if (copy_from_user(&new_line, line, size))
  1417. return -EFAULT;
  1418. switch (new_line.clock_type)
  1419. {
  1420. case CLOCK_EXT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
  1421. case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
  1422. case CLOCK_INT: flags = HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG; break;
  1423. case CLOCK_TXINT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG; break;
  1424. case CLOCK_DEFAULT: flags = info->params.flags &
  1425. (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
  1426. HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
  1427. HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
  1428. HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN); break;
  1429. default: return -EINVAL;
  1430. }
  1431. if (new_line.loopback != 0 && new_line.loopback != 1)
  1432. return -EINVAL;
  1433. info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
  1434. HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
  1435. HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
  1436. HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
  1437. info->params.flags |= flags;
  1438. info->params.loopback = new_line.loopback;
  1439. if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
  1440. info->params.clock_speed = new_line.clock_rate;
  1441. else
  1442. info->params.clock_speed = 0;
  1443. /* if network interface up, reprogram hardware */
  1444. if (info->netcount)
  1445. program_hw(info);
  1446. return 0;
  1447. default:
  1448. return hdlc_ioctl(dev, ifr, cmd);
  1449. }
  1450. }
  1451. /**
  1452. * called by network layer when transmit timeout is detected
  1453. *
  1454. * dev pointer to network device structure
  1455. */
  1456. static void hdlcdev_tx_timeout(struct net_device *dev)
  1457. {
  1458. struct slgt_info *info = dev_to_port(dev);
  1459. unsigned long flags;
  1460. DBGINFO(("%s hdlcdev_tx_timeout\n", dev->name));
  1461. dev->stats.tx_errors++;
  1462. dev->stats.tx_aborted_errors++;
  1463. spin_lock_irqsave(&info->lock,flags);
  1464. tx_stop(info);
  1465. spin_unlock_irqrestore(&info->lock,flags);
  1466. netif_wake_queue(dev);
  1467. }
  1468. /**
  1469. * called by device driver when transmit completes
  1470. * reenable network layer transmit if stopped
  1471. *
  1472. * info pointer to device instance information
  1473. */
  1474. static void hdlcdev_tx_done(struct slgt_info *info)
  1475. {
  1476. if (netif_queue_stopped(info->netdev))
  1477. netif_wake_queue(info->netdev);
  1478. }
  1479. /**
  1480. * called by device driver when frame received
  1481. * pass frame to network layer
  1482. *
  1483. * info pointer to device instance information
  1484. * buf pointer to buffer contianing frame data
  1485. * size count of data bytes in buf
  1486. */
  1487. static void hdlcdev_rx(struct slgt_info *info, char *buf, int size)
  1488. {
  1489. struct sk_buff *skb = dev_alloc_skb(size);
  1490. struct net_device *dev = info->netdev;
  1491. DBGINFO(("%s hdlcdev_rx\n", dev->name));
  1492. if (skb == NULL) {
  1493. DBGERR(("%s: can't alloc skb, drop packet\n", dev->name));
  1494. dev->stats.rx_dropped++;
  1495. return;
  1496. }
  1497. memcpy(skb_put(skb, size), buf, size);
  1498. skb->protocol = hdlc_type_trans(skb, dev);
  1499. dev->stats.rx_packets++;
  1500. dev->stats.rx_bytes += size;
  1501. netif_rx(skb);
  1502. }
  1503. static const struct net_device_ops hdlcdev_ops = {
  1504. .ndo_open = hdlcdev_open,
  1505. .ndo_stop = hdlcdev_close,
  1506. .ndo_start_xmit = hdlc_start_xmit,
  1507. .ndo_do_ioctl = hdlcdev_ioctl,
  1508. .ndo_tx_timeout = hdlcdev_tx_timeout,
  1509. };
  1510. /**
  1511. * called by device driver when adding device instance
  1512. * do generic HDLC initialization
  1513. *
  1514. * info pointer to device instance information
  1515. *
  1516. * returns 0 if success, otherwise error code
  1517. */
  1518. static int hdlcdev_init(struct slgt_info *info)
  1519. {
  1520. int rc;
  1521. struct net_device *dev;
  1522. hdlc_device *hdlc;
  1523. /* allocate and initialize network and HDLC layer objects */
  1524. dev = alloc_hdlcdev(info);
  1525. if (!dev) {
  1526. printk(KERN_ERR "%s hdlc device alloc failure\n", info->device_name);
  1527. return -ENOMEM;
  1528. }
  1529. /* for network layer reporting purposes only */
  1530. dev->mem_start = info->phys_reg_addr;
  1531. dev->mem_end = info->phys_reg_addr + SLGT_REG_SIZE - 1;
  1532. dev->irq = info->irq_level;
  1533. /* network layer callbacks and settings */
  1534. dev->netdev_ops = &hdlcdev_ops;
  1535. dev->watchdog_timeo = 10 * HZ;
  1536. dev->tx_queue_len = 50;
  1537. /* generic HDLC layer callbacks and settings */
  1538. hdlc = dev_to_hdlc(dev);
  1539. hdlc->attach = hdlcdev_attach;
  1540. hdlc->xmit = hdlcdev_xmit;
  1541. /* register objects with HDLC layer */
  1542. rc = register_hdlc_device(dev);
  1543. if (rc) {
  1544. printk(KERN_WARNING "%s:unable to register hdlc device\n",__FILE__);
  1545. free_netdev(dev);
  1546. return rc;
  1547. }
  1548. info->netdev = dev;
  1549. return 0;
  1550. }
  1551. /**
  1552. * called by device driver when removing device instance
  1553. * do generic HDLC cleanup
  1554. *
  1555. * info pointer to device instance information
  1556. */
  1557. static void hdlcdev_exit(struct slgt_info *info)
  1558. {
  1559. unregister_hdlc_device(info->netdev);
  1560. free_netdev(info->netdev);
  1561. info->netdev = NULL;
  1562. }
  1563. #endif /* ifdef CONFIG_HDLC */
  1564. /*
  1565. * get async data from rx DMA buffers
  1566. */
  1567. static void rx_async(struct slgt_info *info)
  1568. {
  1569. struct mgsl_icount *icount = &info->icount;
  1570. unsigned int start, end;
  1571. unsigned char *p;
  1572. unsigned char status;
  1573. struct slgt_desc *bufs = info->rbufs;
  1574. int i, count;
  1575. int chars = 0;
  1576. int stat;
  1577. unsigned char ch;
  1578. start = end = info->rbuf_current;
  1579. while(desc_complete(bufs[end])) {
  1580. count = desc_count(bufs[end]) - info->rbuf_index;
  1581. p = bufs[end].buf + info->rbuf_index;
  1582. DBGISR(("%s rx_async count=%d\n", info->device_name, count));
  1583. DBGDATA(info, p, count, "rx");
  1584. for(i=0 ; i < count; i+=2, p+=2) {
  1585. ch = *p;
  1586. icount->rx++;
  1587. stat = 0;
  1588. status = *(p + 1) & (BIT1 + BIT0);
  1589. if (status) {
  1590. if (status & BIT1)
  1591. icount->parity++;
  1592. else if (status & BIT0)
  1593. icount->frame++;
  1594. /* discard char if tty control flags say so */
  1595. if (status & info->ignore_status_mask)
  1596. continue;
  1597. if (status & BIT1)
  1598. stat = TTY_PARITY;
  1599. else if (status & BIT0)
  1600. stat = TTY_FRAME;
  1601. }
  1602. tty_insert_flip_char(&info->port, ch, stat);
  1603. chars++;
  1604. }
  1605. if (i < count) {
  1606. /* receive buffer not completed */
  1607. info->rbuf_index += i;
  1608. mod_timer(&info->rx_timer, jiffies + 1);
  1609. break;
  1610. }
  1611. info->rbuf_index = 0;
  1612. free_rbufs(info, end, end);
  1613. if (++end == info->rbuf_count)
  1614. end = 0;
  1615. /* if entire list searched then no frame available */
  1616. if (end == start)
  1617. break;
  1618. }
  1619. if (chars)
  1620. tty_flip_buffer_push(&info->port);
  1621. }
  1622. /*
  1623. * return next bottom half action to perform
  1624. */
  1625. static int bh_action(struct slgt_info *info)
  1626. {
  1627. unsigned long flags;
  1628. int rc;
  1629. spin_lock_irqsave(&info->lock,flags);
  1630. if (info->pending_bh & BH_RECEIVE) {
  1631. info->pending_bh &= ~BH_RECEIVE;
  1632. rc = BH_RECEIVE;
  1633. } else if (info->pending_bh & BH_TRANSMIT) {
  1634. info->pending_bh &= ~BH_TRANSMIT;
  1635. rc = BH_TRANSMIT;
  1636. } else if (info->pending_bh & BH_STATUS) {
  1637. info->pending_bh &= ~BH_STATUS;
  1638. rc = BH_STATUS;
  1639. } else {
  1640. /* Mark BH routine as complete */
  1641. info->bh_running = false;
  1642. info->bh_requested = false;
  1643. rc = 0;
  1644. }
  1645. spin_unlock_irqrestore(&info->lock,flags);
  1646. return rc;
  1647. }
  1648. /*
  1649. * perform bottom half processing
  1650. */
  1651. static void bh_handler(struct work_struct *work)
  1652. {
  1653. struct slgt_info *info = container_of(work, struct slgt_info, task);
  1654. int action;
  1655. info->bh_running = true;
  1656. while((action = bh_action(info))) {
  1657. switch (action) {
  1658. case BH_RECEIVE:
  1659. DBGBH(("%s bh receive\n", info->device_name));
  1660. switch(info->params.mode) {
  1661. case MGSL_MODE_ASYNC:
  1662. rx_async(info);
  1663. break;
  1664. case MGSL_MODE_HDLC:
  1665. while(rx_get_frame(info));
  1666. break;
  1667. case MGSL_MODE_RAW:
  1668. case MGSL_MODE_MONOSYNC:
  1669. case MGSL_MODE_BISYNC:
  1670. case MGSL_MODE_XSYNC:
  1671. while(rx_get_buf(info));
  1672. break;
  1673. }
  1674. /* restart receiver if rx DMA buffers exhausted */
  1675. if (info->rx_restart)
  1676. rx_start(info);
  1677. break;
  1678. case BH_TRANSMIT:
  1679. bh_transmit(info);
  1680. break;
  1681. case BH_STATUS:
  1682. DBGBH(("%s bh status\n", info->device_name));
  1683. info->ri_chkcount = 0;
  1684. info->dsr_chkcount = 0;
  1685. info->dcd_chkcount = 0;
  1686. info->cts_chkcount = 0;
  1687. break;
  1688. default:
  1689. DBGBH(("%s unknown action\n", info->device_name));
  1690. break;
  1691. }
  1692. }
  1693. DBGBH(("%s bh_handler exit\n", info->device_name));
  1694. }
  1695. static void bh_transmit(struct slgt_info *info)
  1696. {
  1697. struct tty_struct *tty = info->port.tty;
  1698. DBGBH(("%s bh_transmit\n", info->device_name));
  1699. if (tty)
  1700. tty_wakeup(tty);
  1701. }
  1702. static void dsr_change(struct slgt_info *info, unsigned short status)
  1703. {
  1704. if (status & BIT3) {
  1705. info->signals |= SerialSignal_DSR;
  1706. info->input_signal_events.dsr_up++;
  1707. } else {
  1708. info->signals &= ~SerialSignal_DSR;
  1709. info->input_signal_events.dsr_down++;
  1710. }
  1711. DBGISR(("dsr_change %s signals=%04X\n", info->device_name, info->signals));
  1712. if ((info->dsr_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
  1713. slgt_irq_off(info, IRQ_DSR);
  1714. return;
  1715. }
  1716. info->icount.dsr++;
  1717. wake_up_interruptible(&info->status_event_wait_q);
  1718. wake_up_interruptible(&info->event_wait_q);
  1719. info->pending_bh |= BH_STATUS;
  1720. }
  1721. static void cts_change(struct slgt_info *info, unsigned short status)
  1722. {
  1723. if (status & BIT2) {
  1724. info->signals |= SerialSignal_CTS;
  1725. info->input_signal_events.cts_up++;
  1726. } else {
  1727. info->signals &= ~SerialSignal_CTS;
  1728. info->input_signal_events.cts_down++;
  1729. }
  1730. DBGISR(("cts_change %s signals=%04X\n", info->device_name, info->signals));
  1731. if ((info->cts_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
  1732. slgt_irq_off(info, IRQ_CTS);
  1733. return;
  1734. }
  1735. info->icount.cts++;
  1736. wake_up_interruptible(&info->status_event_wait_q);
  1737. wake_up_interruptible(&info->event_wait_q);
  1738. info->pending_bh |= BH_STATUS;
  1739. if (tty_port_cts_enabled(&info->port)) {
  1740. if (info->port.tty) {
  1741. if (info->port.tty->hw_stopped) {
  1742. if (info->signals & SerialSignal_CTS) {
  1743. info->port.tty->hw_stopped = 0;
  1744. info->pending_bh |= BH_TRANSMIT;
  1745. return;
  1746. }
  1747. } else {
  1748. if (!(info->signals & SerialSignal_CTS))
  1749. info->port.tty->hw_stopped = 1;
  1750. }
  1751. }
  1752. }
  1753. }
  1754. static void dcd_change(struct slgt_info *info, unsigned short status)
  1755. {
  1756. if (status & BIT1) {
  1757. info->signals |= SerialSignal_DCD;
  1758. info->input_signal_events.dcd_up++;
  1759. } else {
  1760. info->signals &= ~SerialSignal_DCD;
  1761. info->input_signal_events.dcd_down++;
  1762. }
  1763. DBGISR(("dcd_change %s signals=%04X\n", info->device_name, info->signals));
  1764. if ((info->dcd_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
  1765. slgt_irq_off(info, IRQ_DCD);
  1766. return;
  1767. }
  1768. info->icount.dcd++;
  1769. #if SYNCLINK_GENERIC_HDLC
  1770. if (info->netcount) {
  1771. if (info->signals & SerialSignal_DCD)
  1772. netif_carrier_on(info->netdev);
  1773. else
  1774. netif_carrier_off(info->netdev);
  1775. }
  1776. #endif
  1777. wake_up_interruptible(&info->status_event_wait_q);
  1778. wake_up_interruptible(&info->event_wait_q);
  1779. info->pending_bh |= BH_STATUS;
  1780. if (tty_port_check_carrier(&info->port)) {
  1781. if (info->signals & SerialSignal_DCD)
  1782. wake_up_interruptible(&info->port.open_wait);
  1783. else {
  1784. if (info->port.tty)
  1785. tty_hangup(info->port.tty);
  1786. }
  1787. }
  1788. }
  1789. static void ri_change(struct slgt_info *info, unsigned short status)
  1790. {
  1791. if (status & BIT0) {
  1792. info->signals |= SerialSignal_RI;
  1793. info->input_signal_events.ri_up++;
  1794. } else {
  1795. info->signals &= ~SerialSignal_RI;
  1796. info->input_signal_events.ri_down++;
  1797. }
  1798. DBGISR(("ri_change %s signals=%04X\n", info->device_name, info->signals));
  1799. if ((info->ri_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
  1800. slgt_irq_off(info, IRQ_RI);
  1801. return;
  1802. }
  1803. info->icount.rng++;
  1804. wake_up_interruptible(&info->status_event_wait_q);
  1805. wake_up_interruptible(&info->event_wait_q);
  1806. info->pending_bh |= BH_STATUS;
  1807. }
  1808. static void isr_rxdata(struct slgt_info *info)
  1809. {
  1810. unsigned int count = info->rbuf_fill_count;
  1811. unsigned int i = info->rbuf_fill_index;
  1812. unsigned short reg;
  1813. while (rd_reg16(info, SSR) & IRQ_RXDATA) {
  1814. reg = rd_reg16(info, RDR);
  1815. DBGISR(("isr_rxdata %s RDR=%04X\n", info->device_name, reg));
  1816. if (desc_complete(info->rbufs[i])) {
  1817. /* all buffers full */
  1818. rx_stop(info);
  1819. info->rx_restart = 1;
  1820. continue;
  1821. }
  1822. info->rbufs[i].buf[count++] = (unsigned char)reg;
  1823. /* async mode saves status byte to buffer for each data byte */
  1824. if (info->params.mode == MGSL_MODE_ASYNC)
  1825. info->rbufs[i].buf[count++] = (unsigned char)(reg >> 8);
  1826. if (count == info->rbuf_fill_level || (reg & BIT10)) {
  1827. /* buffer full or end of frame */
  1828. set_desc_count(info->rbufs[i], count);
  1829. set_desc_status(info->rbufs[i], BIT15 | (reg >> 8));
  1830. info->rbuf_fill_count = count = 0;
  1831. if (++i == info->rbuf_count)
  1832. i = 0;
  1833. info->pending_bh |= BH_RECEIVE;
  1834. }
  1835. }
  1836. info->rbuf_fill_index = i;
  1837. info->rbuf_fill_count = count;
  1838. }
  1839. static void isr_serial(struct slgt_info *info)
  1840. {
  1841. unsigned short status = rd_reg16(info, SSR);
  1842. DBGISR(("%s isr_serial status=%04X\n", info->device_name, status));
  1843. wr_reg16(info, SSR, status); /* clear pending */
  1844. info->irq_occurred = true;
  1845. if (info->params.mode == MGSL_MODE_ASYNC) {
  1846. if (status & IRQ_TXIDLE) {
  1847. if (info->tx_active)
  1848. isr_txeom(info, status);
  1849. }
  1850. if (info->rx_pio && (status & IRQ_RXDATA))
  1851. isr_rxdata(info);
  1852. if ((status & IRQ_RXBREAK) && (status & RXBREAK)) {
  1853. info->icount.brk++;
  1854. /* process break detection if tty control allows */
  1855. if (info->port.tty) {
  1856. if (!(status & info->ignore_status_mask)) {
  1857. if (info->read_status_mask & MASK_BREAK) {
  1858. tty_insert_flip_char(&info->port, 0, TTY_BREAK);
  1859. if (info->port.flags & ASYNC_SAK)
  1860. do_SAK(info->port.tty);
  1861. }
  1862. }
  1863. }
  1864. }
  1865. } else {
  1866. if (status & (IRQ_TXIDLE + IRQ_TXUNDER))
  1867. isr_txeom(info, status);
  1868. if (info->rx_pio && (status & IRQ_RXDATA))
  1869. isr_rxdata(info);
  1870. if (status & IRQ_RXIDLE) {
  1871. if (status & RXIDLE)
  1872. info->icount.rxidle++;
  1873. else
  1874. info->icount.exithunt++;
  1875. wake_up_interruptible(&info->event_wait_q);
  1876. }
  1877. if (status & IRQ_RXOVER)
  1878. rx_start(info);
  1879. }
  1880. if (status & IRQ_DSR)
  1881. dsr_change(info, status);
  1882. if (status & IRQ_CTS)
  1883. cts_change(info, status);
  1884. if (status & IRQ_DCD)
  1885. dcd_change(info, status);
  1886. if (status & IRQ_RI)
  1887. ri_change(info, status);
  1888. }
  1889. static void isr_rdma(struct slgt_info *info)
  1890. {
  1891. unsigned int status = rd_reg32(info, RDCSR);
  1892. DBGISR(("%s isr_rdma status=%08x\n", info->device_name, status));
  1893. /* RDCSR (rx DMA control/status)
  1894. *
  1895. * 31..07 reserved
  1896. * 06 save status byte to DMA buffer
  1897. * 05 error
  1898. * 04 eol (end of list)
  1899. * 03 eob (end of buffer)
  1900. * 02 IRQ enable
  1901. * 01 reset
  1902. * 00 enable
  1903. */
  1904. wr_reg32(info, RDCSR, status); /* clear pending */
  1905. if (status & (BIT5 + BIT4)) {
  1906. DBGISR(("%s isr_rdma rx_restart=1\n", info->device_name));
  1907. info->rx_restart = true;
  1908. }
  1909. info->pending_bh |= BH_RECEIVE;
  1910. }
  1911. static void isr_tdma(struct slgt_info *info)
  1912. {
  1913. unsigned int status = rd_reg32(info, TDCSR);
  1914. DBGISR(("%s isr_tdma status=%08x\n", info->device_name, status));
  1915. /* TDCSR (tx DMA control/status)
  1916. *
  1917. * 31..06 reserved
  1918. * 05 error
  1919. * 04 eol (end of list)
  1920. * 03 eob (end of buffer)
  1921. * 02 IRQ enable
  1922. * 01 reset
  1923. * 00 enable
  1924. */
  1925. wr_reg32(info, TDCSR, status); /* clear pending */
  1926. if (status & (BIT5 + BIT4 + BIT3)) {
  1927. // another transmit buffer has completed
  1928. // run bottom half to get more send data from user
  1929. info->pending_bh |= BH_TRANSMIT;
  1930. }
  1931. }
  1932. /*
  1933. * return true if there are unsent tx DMA buffers, otherwise false
  1934. *
  1935. * if there are unsent buffers then info->tbuf_start
  1936. * is set to index of first unsent buffer
  1937. */
  1938. static bool unsent_tbufs(struct slgt_info *info)
  1939. {
  1940. unsigned int i = info->tbuf_current;
  1941. bool rc = false;
  1942. /*
  1943. * search backwards from last loaded buffer (precedes tbuf_current)
  1944. * for first unsent buffer (desc_count > 0)
  1945. */
  1946. do {
  1947. if (i)
  1948. i--;
  1949. else
  1950. i = info->tbuf_count - 1;
  1951. if (!desc_count(info->tbufs[i]))
  1952. break;
  1953. info->tbuf_start = i;
  1954. rc = true;
  1955. } while (i != info->tbuf_current);
  1956. return rc;
  1957. }
  1958. static void isr_txeom(struct slgt_info *info, unsigned short status)
  1959. {
  1960. DBGISR(("%s txeom status=%04x\n", info->device_name, status));
  1961. slgt_irq_off(info, IRQ_TXDATA + IRQ_TXIDLE + IRQ_TXUNDER);
  1962. tdma_reset(info);
  1963. if (status & IRQ_TXUNDER) {
  1964. unsigned short val = rd_reg16(info, TCR);
  1965. wr_reg16(info, TCR, (unsigned short)(val | BIT2)); /* set reset bit */
  1966. wr_reg16(info, TCR, val); /* clear reset bit */
  1967. }
  1968. if (info->tx_active) {
  1969. if (info->params.mode != MGSL_MODE_ASYNC) {
  1970. if (status & IRQ_TXUNDER)
  1971. info->icount.txunder++;
  1972. else if (status & IRQ_TXIDLE)
  1973. info->icount.txok++;
  1974. }
  1975. if (unsent_tbufs(info)) {
  1976. tx_start(info);
  1977. update_tx_timer(info);
  1978. return;
  1979. }
  1980. info->tx_active = false;
  1981. del_timer(&info->tx_timer);
  1982. if (info->params.mode != MGSL_MODE_ASYNC && info->drop_rts_on_tx_done) {
  1983. info->signals &= ~SerialSignal_RTS;
  1984. info->drop_rts_on_tx_done = false;
  1985. set_signals(info);
  1986. }
  1987. #if SYNCLINK_GENERIC_HDLC
  1988. if (info->netcount)
  1989. hdlcdev_tx_done(info);
  1990. else
  1991. #endif
  1992. {
  1993. if (info->port.tty && (info->port.tty->stopped || info->port.tty->hw_stopped)) {
  1994. tx_stop(info);
  1995. return;
  1996. }
  1997. info->pending_bh |= BH_TRANSMIT;
  1998. }
  1999. }
  2000. }
  2001. static void isr_gpio(struct slgt_info *info, unsigned int changed, unsigned int state)
  2002. {
  2003. struct cond_wait *w, *prev;
  2004. /* wake processes waiting for specific transitions */
  2005. for (w = info->gpio_wait_q, prev = NULL ; w != NULL ; w = w->next) {
  2006. if (w->data & changed) {
  2007. w->data = state;
  2008. wake_up_interruptible(&w->q);
  2009. if (prev != NULL)
  2010. prev->next = w->next;
  2011. else
  2012. info->gpio_wait_q = w->next;
  2013. } else
  2014. prev = w;
  2015. }
  2016. }
  2017. /* interrupt service routine
  2018. *
  2019. * irq interrupt number
  2020. * dev_id device ID supplied during interrupt registration
  2021. */
  2022. static irqreturn_t slgt_interrupt(int dummy, void *dev_id)
  2023. {
  2024. struct slgt_info *info = dev_id;
  2025. unsigned int gsr;
  2026. unsigned int i;
  2027. DBGISR(("slgt_interrupt irq=%d entry\n", info->irq_level));
  2028. while((gsr = rd_reg32(info, GSR) & 0xffffff00)) {
  2029. DBGISR(("%s gsr=%08x\n", info->device_name, gsr));
  2030. info->irq_occurred = true;
  2031. for(i=0; i < info->port_count ; i++) {
  2032. if (info->port_array[i] == NULL)
  2033. continue;
  2034. spin_lock(&info->port_array[i]->lock);
  2035. if (gsr & (BIT8 << i))
  2036. isr_serial(info->port_array[i]);
  2037. if (gsr & (BIT16 << (i*2)))
  2038. isr_rdma(info->port_array[i]);
  2039. if (gsr & (BIT17 << (i*2)))
  2040. isr_tdma(info->port_array[i]);
  2041. spin_unlock(&info->port_array[i]->lock);
  2042. }
  2043. }
  2044. if (info->gpio_present) {
  2045. unsigned int state;
  2046. unsigned int changed;
  2047. spin_lock(&info->lock);
  2048. while ((changed = rd_reg32(info, IOSR)) != 0) {
  2049. DBGISR(("%s iosr=%08x\n", info->device_name, changed));
  2050. /* read latched state of GPIO signals */
  2051. state = rd_reg32(info, IOVR);
  2052. /* clear pending GPIO interrupt bits */
  2053. wr_reg32(info, IOSR, changed);
  2054. for (i=0 ; i < info->port_count ; i++) {
  2055. if (info->port_array[i] != NULL)
  2056. isr_gpio(info->port_array[i], changed, state);
  2057. }
  2058. }
  2059. spin_unlock(&info->lock);
  2060. }
  2061. for(i=0; i < info->port_count ; i++) {
  2062. struct slgt_info *port = info->port_array[i];
  2063. if (port == NULL)
  2064. continue;
  2065. spin_lock(&port->lock);
  2066. if ((port->port.count || port->netcount) &&
  2067. port->pending_bh && !port->bh_running &&
  2068. !port->bh_requested) {
  2069. DBGISR(("%s bh queued\n", port->device_name));
  2070. schedule_work(&port->task);
  2071. port->bh_requested = true;
  2072. }
  2073. spin_unlock(&port->lock);
  2074. }
  2075. DBGISR(("slgt_interrupt irq=%d exit\n", info->irq_level));
  2076. return IRQ_HANDLED;
  2077. }
  2078. static int startup(struct slgt_info *info)
  2079. {
  2080. DBGINFO(("%s startup\n", info->device_name));
  2081. if (tty_port_initialized(&info->port))
  2082. return 0;
  2083. if (!info->tx_buf) {
  2084. info->tx_buf = kmalloc(info->max_frame_size, GFP_KERNEL);
  2085. if (!info->tx_buf) {
  2086. DBGERR(("%s can't allocate tx buffer\n", info->device_name));
  2087. return -ENOMEM;
  2088. }
  2089. }
  2090. info->pending_bh = 0;
  2091. memset(&info->icount, 0, sizeof(info->icount));
  2092. /* program hardware for current parameters */
  2093. change_params(info);
  2094. if (info->port.tty)
  2095. clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
  2096. tty_port_set_initialized(&info->port, 1);
  2097. return 0;
  2098. }
  2099. /*
  2100. * called by close() and hangup() to shutdown hardware
  2101. */
  2102. static void shutdown(struct slgt_info *info)
  2103. {
  2104. unsigned long flags;
  2105. if (!tty_port_initialized(&info->port))
  2106. return;
  2107. DBGINFO(("%s shutdown\n", info->device_name));
  2108. /* clear status wait queue because status changes */
  2109. /* can't happen after shutting down the hardware */
  2110. wake_up_interruptible(&info->status_event_wait_q);
  2111. wake_up_interruptible(&info->event_wait_q);
  2112. del_timer_sync(&info->tx_timer);
  2113. del_timer_sync(&info->rx_timer);
  2114. kfree(info->tx_buf);
  2115. info->tx_buf = NULL;
  2116. spin_lock_irqsave(&info->lock,flags);
  2117. tx_stop(info);
  2118. rx_stop(info);
  2119. slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
  2120. if (!info->port.tty || info->port.tty->termios.c_cflag & HUPCL) {
  2121. info->signals &= ~(SerialSignal_RTS | SerialSignal_DTR);
  2122. set_signals(info);
  2123. }
  2124. flush_cond_wait(&info->gpio_wait_q);
  2125. spin_unlock_irqrestore(&info->lock,flags);
  2126. if (info->port.tty)
  2127. set_bit(TTY_IO_ERROR, &info->port.tty->flags);
  2128. tty_port_set_initialized(&info->port, 0);
  2129. }
  2130. static void program_hw(struct slgt_info *info)
  2131. {
  2132. unsigned long flags;
  2133. spin_lock_irqsave(&info->lock,flags);
  2134. rx_stop(info);
  2135. tx_stop(info);
  2136. if (info->params.mode != MGSL_MODE_ASYNC ||
  2137. info->netcount)
  2138. sync_mode(info);
  2139. else
  2140. async_mode(info);
  2141. set_signals(info);
  2142. info->dcd_chkcount = 0;
  2143. info->cts_chkcount = 0;
  2144. info->ri_chkcount = 0;
  2145. info->dsr_chkcount = 0;
  2146. slgt_irq_on(info, IRQ_DCD | IRQ_CTS | IRQ_DSR | IRQ_RI);
  2147. get_signals(info);
  2148. if (info->netcount ||
  2149. (info->port.tty && info->port.tty->termios.c_cflag & CREAD))
  2150. rx_start(info);
  2151. spin_unlock_irqrestore(&info->lock,flags);
  2152. }
  2153. /*
  2154. * reconfigure adapter based on new parameters
  2155. */
  2156. static void change_params(struct slgt_info *info)
  2157. {
  2158. unsigned cflag;
  2159. int bits_per_char;
  2160. if (!info->port.tty)
  2161. return;
  2162. DBGINFO(("%s change_params\n", info->device_name));
  2163. cflag = info->port.tty->termios.c_cflag;
  2164. /* if B0 rate (hangup) specified then negate RTS and DTR */
  2165. /* otherwise assert RTS and DTR */
  2166. if (cflag & CBAUD)
  2167. info->signals |= SerialSignal_RTS | SerialSignal_DTR;
  2168. else
  2169. info->signals &= ~(SerialSignal_RTS | SerialSignal_DTR);
  2170. /* byte size and parity */
  2171. switch (cflag & CSIZE) {
  2172. case CS5: info->params.data_bits = 5; break;
  2173. case CS6: info->params.data_bits = 6; break;
  2174. case CS7: info->params.data_bits = 7; break;
  2175. case CS8: info->params.data_bits = 8; break;
  2176. default: info->params.data_bits = 7; break;
  2177. }
  2178. info->params.stop_bits = (cflag & CSTOPB) ? 2 : 1;
  2179. if (cflag & PARENB)
  2180. info->params.parity = (cflag & PARODD) ? ASYNC_PARITY_ODD : ASYNC_PARITY_EVEN;
  2181. else
  2182. info->params.parity = ASYNC_PARITY_NONE;
  2183. /* calculate number of jiffies to transmit a full
  2184. * FIFO (32 bytes) at specified data rate
  2185. */
  2186. bits_per_char = info->params.data_bits +
  2187. info->params.stop_bits + 1;
  2188. info->params.data_rate = tty_get_baud_rate(info->port.tty);
  2189. if (info->params.data_rate) {
  2190. info->timeout = (32*HZ*bits_per_char) /
  2191. info->params.data_rate;
  2192. }
  2193. info->timeout += HZ/50; /* Add .02 seconds of slop */
  2194. tty_port_set_cts_flow(&info->port, cflag & CRTSCTS);
  2195. tty_port_set_check_carrier(&info->port, ~cflag & CLOCAL);
  2196. /* process tty input control flags */
  2197. info->read_status_mask = IRQ_RXOVER;
  2198. if (I_INPCK(info->port.tty))
  2199. info->read_status_mask |= MASK_PARITY | MASK_FRAMING;
  2200. if (I_BRKINT(info->port.tty) || I_PARMRK(info->port.tty))
  2201. info->read_status_mask |= MASK_BREAK;
  2202. if (I_IGNPAR(info->port.tty))
  2203. info->ignore_status_mask |= MASK_PARITY | MASK_FRAMING;
  2204. if (I_IGNBRK(info->port.tty)) {
  2205. info->ignore_status_mask |= MASK_BREAK;
  2206. /* If ignoring parity and break indicators, ignore
  2207. * overruns too. (For real raw support).
  2208. */
  2209. if (I_IGNPAR(info->port.tty))
  2210. info->ignore_status_mask |= MASK_OVERRUN;
  2211. }
  2212. program_hw(info);
  2213. }
  2214. static int get_stats(struct slgt_info *info, struct mgsl_icount __user *user_icount)
  2215. {
  2216. DBGINFO(("%s get_stats\n", info->device_name));
  2217. if (!user_icount) {
  2218. memset(&info->icount, 0, sizeof(info->icount));
  2219. } else {
  2220. if (copy_to_user(user_icount, &info->icount, sizeof(struct mgsl_icount)))
  2221. return -EFAULT;
  2222. }
  2223. return 0;
  2224. }
  2225. static int get_params(struct slgt_info *info, MGSL_PARAMS __user *user_params)
  2226. {
  2227. DBGINFO(("%s get_params\n", info->device_name));
  2228. if (copy_to_user(user_params, &info->params, sizeof(MGSL_PARAMS)))
  2229. return -EFAULT;
  2230. return 0;
  2231. }
  2232. static int set_params(struct slgt_info *info, MGSL_PARAMS __user *new_params)
  2233. {
  2234. unsigned long flags;
  2235. MGSL_PARAMS tmp_params;
  2236. DBGINFO(("%s set_params\n", info->device_name));
  2237. if (copy_from_user(&tmp_params, new_params, sizeof(MGSL_PARAMS)))
  2238. return -EFAULT;
  2239. spin_lock_irqsave(&info->lock, flags);
  2240. if (tmp_params.mode == MGSL_MODE_BASE_CLOCK)
  2241. info->base_clock = tmp_params.clock_speed;
  2242. else
  2243. memcpy(&info->params, &tmp_params, sizeof(MGSL_PARAMS));
  2244. spin_unlock_irqrestore(&info->lock, flags);
  2245. program_hw(info);
  2246. return 0;
  2247. }
  2248. static int get_txidle(struct slgt_info *info, int __user *idle_mode)
  2249. {
  2250. DBGINFO(("%s get_txidle=%d\n", info->device_name, info->idle_mode));
  2251. if (put_user(info->idle_mode, idle_mode))
  2252. return -EFAULT;
  2253. return 0;
  2254. }
  2255. static int set_txidle(struct slgt_info *info, int idle_mode)
  2256. {
  2257. unsigned long flags;
  2258. DBGINFO(("%s set_txidle(%d)\n", info->device_name, idle_mode));
  2259. spin_lock_irqsave(&info->lock,flags);
  2260. info->idle_mode = idle_mode;
  2261. if (info->params.mode != MGSL_MODE_ASYNC)
  2262. tx_set_idle(info);
  2263. spin_unlock_irqrestore(&info->lock,flags);
  2264. return 0;
  2265. }
  2266. static int tx_enable(struct slgt_info *info, int enable)
  2267. {
  2268. unsigned long flags;
  2269. DBGINFO(("%s tx_enable(%d)\n", info->device_name, enable));
  2270. spin_lock_irqsave(&info->lock,flags);
  2271. if (enable) {
  2272. if (!info->tx_enabled)
  2273. tx_start(info);
  2274. } else {
  2275. if (info->tx_enabled)
  2276. tx_stop(info);
  2277. }
  2278. spin_unlock_irqrestore(&info->lock,flags);
  2279. return 0;
  2280. }
  2281. /*
  2282. * abort transmit HDLC frame
  2283. */
  2284. static int tx_abort(struct slgt_info *info)
  2285. {
  2286. unsigned long flags;
  2287. DBGINFO(("%s tx_abort\n", info->device_name));
  2288. spin_lock_irqsave(&info->lock,flags);
  2289. tdma_reset(info);
  2290. spin_unlock_irqrestore(&info->lock,flags);
  2291. return 0;
  2292. }
  2293. static int rx_enable(struct slgt_info *info, int enable)
  2294. {
  2295. unsigned long flags;
  2296. unsigned int rbuf_fill_level;
  2297. DBGINFO(("%s rx_enable(%08x)\n", info->device_name, enable));
  2298. spin_lock_irqsave(&info->lock,flags);
  2299. /*
  2300. * enable[31..16] = receive DMA buffer fill level
  2301. * 0 = noop (leave fill level unchanged)
  2302. * fill level must be multiple of 4 and <= buffer size
  2303. */
  2304. rbuf_fill_level = ((unsigned int)enable) >> 16;
  2305. if (rbuf_fill_level) {
  2306. if ((rbuf_fill_level > DMABUFSIZE) || (rbuf_fill_level % 4)) {
  2307. spin_unlock_irqrestore(&info->lock, flags);
  2308. return -EINVAL;
  2309. }
  2310. info->rbuf_fill_level = rbuf_fill_level;
  2311. if (rbuf_fill_level < 128)
  2312. info->rx_pio = 1; /* PIO mode */
  2313. else
  2314. info->rx_pio = 0; /* DMA mode */
  2315. rx_stop(info); /* restart receiver to use new fill level */
  2316. }
  2317. /*
  2318. * enable[1..0] = receiver enable command
  2319. * 0 = disable
  2320. * 1 = enable
  2321. * 2 = enable or force hunt mode if already enabled
  2322. */
  2323. enable &= 3;
  2324. if (enable) {
  2325. if (!info->rx_enabled)
  2326. rx_start(info);
  2327. else if (enable == 2) {
  2328. /* force hunt mode (write 1 to RCR[3]) */
  2329. wr_reg16(info, RCR, rd_reg16(info, RCR) | BIT3);
  2330. }
  2331. } else {
  2332. if (info->rx_enabled)
  2333. rx_stop(info);
  2334. }
  2335. spin_unlock_irqrestore(&info->lock,flags);
  2336. return 0;
  2337. }
  2338. /*
  2339. * wait for specified event to occur
  2340. */
  2341. static int wait_mgsl_event(struct slgt_info *info, int __user *mask_ptr)
  2342. {
  2343. unsigned long flags;
  2344. int s;
  2345. int rc=0;
  2346. struct mgsl_icount cprev, cnow;
  2347. int events;
  2348. int mask;
  2349. struct _input_signal_events oldsigs, newsigs;
  2350. DECLARE_WAITQUEUE(wait, current);
  2351. if (get_user(mask, mask_ptr))
  2352. return -EFAULT;
  2353. DBGINFO(("%s wait_mgsl_event(%d)\n", info->device_name, mask));
  2354. spin_lock_irqsave(&info->lock,flags);
  2355. /* return immediately if state matches requested events */
  2356. get_signals(info);
  2357. s = info->signals;
  2358. events = mask &
  2359. ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
  2360. ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
  2361. ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
  2362. ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
  2363. if (events) {
  2364. spin_unlock_irqrestore(&info->lock,flags);
  2365. goto exit;
  2366. }
  2367. /* save current irq counts */
  2368. cprev = info->icount;
  2369. oldsigs = info->input_signal_events;
  2370. /* enable hunt and idle irqs if needed */
  2371. if (mask & (MgslEvent_ExitHuntMode+MgslEvent_IdleReceived)) {
  2372. unsigned short val = rd_reg16(info, SCR);
  2373. if (!(val & IRQ_RXIDLE))
  2374. wr_reg16(info, SCR, (unsigned short)(val | IRQ_RXIDLE));
  2375. }
  2376. set_current_state(TASK_INTERRUPTIBLE);
  2377. add_wait_queue(&info->event_wait_q, &wait);
  2378. spin_unlock_irqrestore(&info->lock,flags);
  2379. for(;;) {
  2380. schedule();
  2381. if (signal_pending(current)) {
  2382. rc = -ERESTARTSYS;
  2383. break;
  2384. }
  2385. /* get current irq counts */
  2386. spin_lock_irqsave(&info->lock,flags);
  2387. cnow = info->icount;
  2388. newsigs = info->input_signal_events;
  2389. set_current_state(TASK_INTERRUPTIBLE);
  2390. spin_unlock_irqrestore(&info->lock,flags);
  2391. /* if no change, wait aborted for some reason */
  2392. if (newsigs.dsr_up == oldsigs.dsr_up &&
  2393. newsigs.dsr_down == oldsigs.dsr_down &&
  2394. newsigs.dcd_up == oldsigs.dcd_up &&
  2395. newsigs.dcd_down == oldsigs.dcd_down &&
  2396. newsigs.cts_up == oldsigs.cts_up &&
  2397. newsigs.cts_down == oldsigs.cts_down &&
  2398. newsigs.ri_up == oldsigs.ri_up &&
  2399. newsigs.ri_down == oldsigs.ri_down &&
  2400. cnow.exithunt == cprev.exithunt &&
  2401. cnow.rxidle == cprev.rxidle) {
  2402. rc = -EIO;
  2403. break;
  2404. }
  2405. events = mask &
  2406. ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
  2407. (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
  2408. (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
  2409. (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
  2410. (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
  2411. (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
  2412. (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
  2413. (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
  2414. (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
  2415. (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
  2416. if (events)
  2417. break;
  2418. cprev = cnow;
  2419. oldsigs = newsigs;
  2420. }
  2421. remove_wait_queue(&info->event_wait_q, &wait);
  2422. set_current_state(TASK_RUNNING);
  2423. if (mask & (MgslEvent_ExitHuntMode + MgslEvent_IdleReceived)) {
  2424. spin_lock_irqsave(&info->lock,flags);
  2425. if (!waitqueue_active(&info->event_wait_q)) {
  2426. /* disable enable exit hunt mode/idle rcvd IRQs */
  2427. wr_reg16(info, SCR,
  2428. (unsigned short)(rd_reg16(info, SCR) & ~IRQ_RXIDLE));
  2429. }
  2430. spin_unlock_irqrestore(&info->lock,flags);
  2431. }
  2432. exit:
  2433. if (rc == 0)
  2434. rc = put_user(events, mask_ptr);
  2435. return rc;
  2436. }
  2437. static int get_interface(struct slgt_info *info, int __user *if_mode)
  2438. {
  2439. DBGINFO(("%s get_interface=%x\n", info->device_name, info->if_mode));
  2440. if (put_user(info->if_mode, if_mode))
  2441. return -EFAULT;
  2442. return 0;
  2443. }
  2444. static int set_interface(struct slgt_info *info, int if_mode)
  2445. {
  2446. unsigned long flags;
  2447. unsigned short val;
  2448. DBGINFO(("%s set_interface=%x)\n", info->device_name, if_mode));
  2449. spin_lock_irqsave(&info->lock,flags);
  2450. info->if_mode = if_mode;
  2451. msc_set_vcr(info);
  2452. /* TCR (tx control) 07 1=RTS driver control */
  2453. val = rd_reg16(info, TCR);
  2454. if (info->if_mode & MGSL_INTERFACE_RTS_EN)
  2455. val |= BIT7;
  2456. else
  2457. val &= ~BIT7;
  2458. wr_reg16(info, TCR, val);
  2459. spin_unlock_irqrestore(&info->lock,flags);
  2460. return 0;
  2461. }
  2462. static int get_xsync(struct slgt_info *info, int __user *xsync)
  2463. {
  2464. DBGINFO(("%s get_xsync=%x\n", info->device_name, info->xsync));
  2465. if (put_user(info->xsync, xsync))
  2466. return -EFAULT;
  2467. return 0;
  2468. }
  2469. /*
  2470. * set extended sync pattern (1 to 4 bytes) for extended sync mode
  2471. *
  2472. * sync pattern is contained in least significant bytes of value
  2473. * most significant byte of sync pattern is oldest (1st sent/detected)
  2474. */
  2475. static int set_xsync(struct slgt_info *info, int xsync)
  2476. {
  2477. unsigned long flags;
  2478. DBGINFO(("%s set_xsync=%x)\n", info->device_name, xsync));
  2479. spin_lock_irqsave(&info->lock, flags);
  2480. info->xsync = xsync;
  2481. wr_reg32(info, XSR, xsync);
  2482. spin_unlock_irqrestore(&info->lock, flags);
  2483. return 0;
  2484. }
  2485. static int get_xctrl(struct slgt_info *info, int __user *xctrl)
  2486. {
  2487. DBGINFO(("%s get_xctrl=%x\n", info->device_name, info->xctrl));
  2488. if (put_user(info->xctrl, xctrl))
  2489. return -EFAULT;
  2490. return 0;
  2491. }
  2492. /*
  2493. * set extended control options
  2494. *
  2495. * xctrl[31:19] reserved, must be zero
  2496. * xctrl[18:17] extended sync pattern length in bytes
  2497. * 00 = 1 byte in xsr[7:0]
  2498. * 01 = 2 bytes in xsr[15:0]
  2499. * 10 = 3 bytes in xsr[23:0]
  2500. * 11 = 4 bytes in xsr[31:0]
  2501. * xctrl[16] 1 = enable terminal count, 0=disabled
  2502. * xctrl[15:0] receive terminal count for fixed length packets
  2503. * value is count minus one (0 = 1 byte packet)
  2504. * when terminal count is reached, receiver
  2505. * automatically returns to hunt mode and receive
  2506. * FIFO contents are flushed to DMA buffers with
  2507. * end of frame (EOF) status
  2508. */
  2509. static int set_xctrl(struct slgt_info *info, int xctrl)
  2510. {
  2511. unsigned long flags;
  2512. DBGINFO(("%s set_xctrl=%x)\n", info->device_name, xctrl));
  2513. spin_lock_irqsave(&info->lock, flags);
  2514. info->xctrl = xctrl;
  2515. wr_reg32(info, XCR, xctrl);
  2516. spin_unlock_irqrestore(&info->lock, flags);
  2517. return 0;
  2518. }
  2519. /*
  2520. * set general purpose IO pin state and direction
  2521. *
  2522. * user_gpio fields:
  2523. * state each bit indicates a pin state
  2524. * smask set bit indicates pin state to set
  2525. * dir each bit indicates a pin direction (0=input, 1=output)
  2526. * dmask set bit indicates pin direction to set
  2527. */
  2528. static int set_gpio(struct slgt_info *info, struct gpio_desc __user *user_gpio)
  2529. {
  2530. unsigned long flags;
  2531. struct gpio_desc gpio;
  2532. __u32 data;
  2533. if (!info->gpio_present)
  2534. return -EINVAL;
  2535. if (copy_from_user(&gpio, user_gpio, sizeof(gpio)))
  2536. return -EFAULT;
  2537. DBGINFO(("%s set_gpio state=%08x smask=%08x dir=%08x dmask=%08x\n",
  2538. info->device_name, gpio.state, gpio.smask,
  2539. gpio.dir, gpio.dmask));
  2540. spin_lock_irqsave(&info->port_array[0]->lock, flags);
  2541. if (gpio.dmask) {
  2542. data = rd_reg32(info, IODR);
  2543. data |= gpio.dmask & gpio.dir;
  2544. data &= ~(gpio.dmask & ~gpio.dir);
  2545. wr_reg32(info, IODR, data);
  2546. }
  2547. if (gpio.smask) {
  2548. data = rd_reg32(info, IOVR);
  2549. data |= gpio.smask & gpio.state;
  2550. data &= ~(gpio.smask & ~gpio.state);
  2551. wr_reg32(info, IOVR, data);
  2552. }
  2553. spin_unlock_irqrestore(&info->port_array[0]->lock, flags);
  2554. return 0;
  2555. }
  2556. /*
  2557. * get general purpose IO pin state and direction
  2558. */
  2559. static int get_gpio(struct slgt_info *info, struct gpio_desc __user *user_gpio)
  2560. {
  2561. struct gpio_desc gpio;
  2562. if (!info->gpio_present)
  2563. return -EINVAL;
  2564. gpio.state = rd_reg32(info, IOVR);
  2565. gpio.smask = 0xffffffff;
  2566. gpio.dir = rd_reg32(info, IODR);
  2567. gpio.dmask = 0xffffffff;
  2568. if (copy_to_user(user_gpio, &gpio, sizeof(gpio)))
  2569. return -EFAULT;
  2570. DBGINFO(("%s get_gpio state=%08x dir=%08x\n",
  2571. info->device_name, gpio.state, gpio.dir));
  2572. return 0;
  2573. }
  2574. /*
  2575. * conditional wait facility
  2576. */
  2577. static void init_cond_wait(struct cond_wait *w, unsigned int data)
  2578. {
  2579. init_waitqueue_head(&w->q);
  2580. init_waitqueue_entry(&w->wait, current);
  2581. w->data = data;
  2582. }
  2583. static void add_cond_wait(struct cond_wait **head, struct cond_wait *w)
  2584. {
  2585. set_current_state(TASK_INTERRUPTIBLE);
  2586. add_wait_queue(&w->q, &w->wait);
  2587. w->next = *head;
  2588. *head = w;
  2589. }
  2590. static void remove_cond_wait(struct cond_wait **head, struct cond_wait *cw)
  2591. {
  2592. struct cond_wait *w, *prev;
  2593. remove_wait_queue(&cw->q, &cw->wait);
  2594. set_current_state(TASK_RUNNING);
  2595. for (w = *head, prev = NULL ; w != NULL ; prev = w, w = w->next) {
  2596. if (w == cw) {
  2597. if (prev != NULL)
  2598. prev->next = w->next;
  2599. else
  2600. *head = w->next;
  2601. break;
  2602. }
  2603. }
  2604. }
  2605. static void flush_cond_wait(struct cond_wait **head)
  2606. {
  2607. while (*head != NULL) {
  2608. wake_up_interruptible(&(*head)->q);
  2609. *head = (*head)->next;
  2610. }
  2611. }
  2612. /*
  2613. * wait for general purpose I/O pin(s) to enter specified state
  2614. *
  2615. * user_gpio fields:
  2616. * state - bit indicates target pin state
  2617. * smask - set bit indicates watched pin
  2618. *
  2619. * The wait ends when at least one watched pin enters the specified
  2620. * state. When 0 (no error) is returned, user_gpio->state is set to the
  2621. * state of all GPIO pins when the wait ends.
  2622. *
  2623. * Note: Each pin may be a dedicated input, dedicated output, or
  2624. * configurable input/output. The number and configuration of pins
  2625. * varies with the specific adapter model. Only input pins (dedicated
  2626. * or configured) can be monitored with this function.
  2627. */
  2628. static int wait_gpio(struct slgt_info *info, struct gpio_desc __user *user_gpio)
  2629. {
  2630. unsigned long flags;
  2631. int rc = 0;
  2632. struct gpio_desc gpio;
  2633. struct cond_wait wait;
  2634. u32 state;
  2635. if (!info->gpio_present)
  2636. return -EINVAL;
  2637. if (copy_from_user(&gpio, user_gpio, sizeof(gpio)))
  2638. return -EFAULT;
  2639. DBGINFO(("%s wait_gpio() state=%08x smask=%08x\n",
  2640. info->device_name, gpio.state, gpio.smask));
  2641. /* ignore output pins identified by set IODR bit */
  2642. if ((gpio.smask &= ~rd_reg32(info, IODR)) == 0)
  2643. return -EINVAL;
  2644. init_cond_wait(&wait, gpio.smask);
  2645. spin_lock_irqsave(&info->port_array[0]->lock, flags);
  2646. /* enable interrupts for watched pins */
  2647. wr_reg32(info, IOER, rd_reg32(info, IOER) | gpio.smask);
  2648. /* get current pin states */
  2649. state = rd_reg32(info, IOVR);
  2650. if (gpio.smask & ~(state ^ gpio.state)) {
  2651. /* already in target state */
  2652. gpio.state = state;
  2653. } else {
  2654. /* wait for target state */
  2655. add_cond_wait(&info->gpio_wait_q, &wait);
  2656. spin_unlock_irqrestore(&info->port_array[0]->lock, flags);
  2657. schedule();
  2658. if (signal_pending(current))
  2659. rc = -ERESTARTSYS;
  2660. else
  2661. gpio.state = wait.data;
  2662. spin_lock_irqsave(&info->port_array[0]->lock, flags);
  2663. remove_cond_wait(&info->gpio_wait_q, &wait);
  2664. }
  2665. /* disable all GPIO interrupts if no waiting processes */
  2666. if (info->gpio_wait_q == NULL)
  2667. wr_reg32(info, IOER, 0);
  2668. spin_unlock_irqrestore(&info->port_array[0]->lock, flags);
  2669. if ((rc == 0) && copy_to_user(user_gpio, &gpio, sizeof(gpio)))
  2670. rc = -EFAULT;
  2671. return rc;
  2672. }
  2673. static int modem_input_wait(struct slgt_info *info,int arg)
  2674. {
  2675. unsigned long flags;
  2676. int rc;
  2677. struct mgsl_icount cprev, cnow;
  2678. DECLARE_WAITQUEUE(wait, current);
  2679. /* save current irq counts */
  2680. spin_lock_irqsave(&info->lock,flags);
  2681. cprev = info->icount;
  2682. add_wait_queue(&info->status_event_wait_q, &wait);
  2683. set_current_state(TASK_INTERRUPTIBLE);
  2684. spin_unlock_irqrestore(&info->lock,flags);
  2685. for(;;) {
  2686. schedule();
  2687. if (signal_pending(current)) {
  2688. rc = -ERESTARTSYS;
  2689. break;
  2690. }
  2691. /* get new irq counts */
  2692. spin_lock_irqsave(&info->lock,flags);
  2693. cnow = info->icount;
  2694. set_current_state(TASK_INTERRUPTIBLE);
  2695. spin_unlock_irqrestore(&info->lock,flags);
  2696. /* if no change, wait aborted for some reason */
  2697. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  2698. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
  2699. rc = -EIO;
  2700. break;
  2701. }
  2702. /* check for change in caller specified modem input */
  2703. if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
  2704. (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
  2705. (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
  2706. (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
  2707. rc = 0;
  2708. break;
  2709. }
  2710. cprev = cnow;
  2711. }
  2712. remove_wait_queue(&info->status_event_wait_q, &wait);
  2713. set_current_state(TASK_RUNNING);
  2714. return rc;
  2715. }
  2716. /*
  2717. * return state of serial control and status signals
  2718. */
  2719. static int tiocmget(struct tty_struct *tty)
  2720. {
  2721. struct slgt_info *info = tty->driver_data;
  2722. unsigned int result;
  2723. unsigned long flags;
  2724. spin_lock_irqsave(&info->lock,flags);
  2725. get_signals(info);
  2726. spin_unlock_irqrestore(&info->lock,flags);
  2727. result = ((info->signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
  2728. ((info->signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
  2729. ((info->signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
  2730. ((info->signals & SerialSignal_RI) ? TIOCM_RNG:0) +
  2731. ((info->signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
  2732. ((info->signals & SerialSignal_CTS) ? TIOCM_CTS:0);
  2733. DBGINFO(("%s tiocmget value=%08X\n", info->device_name, result));
  2734. return result;
  2735. }
  2736. /*
  2737. * set modem control signals (DTR/RTS)
  2738. *
  2739. * cmd signal command: TIOCMBIS = set bit TIOCMBIC = clear bit
  2740. * TIOCMSET = set/clear signal values
  2741. * value bit mask for command
  2742. */
  2743. static int tiocmset(struct tty_struct *tty,
  2744. unsigned int set, unsigned int clear)
  2745. {
  2746. struct slgt_info *info = tty->driver_data;
  2747. unsigned long flags;
  2748. DBGINFO(("%s tiocmset(%x,%x)\n", info->device_name, set, clear));
  2749. if (set & TIOCM_RTS)
  2750. info->signals |= SerialSignal_RTS;
  2751. if (set & TIOCM_DTR)
  2752. info->signals |= SerialSignal_DTR;
  2753. if (clear & TIOCM_RTS)
  2754. info->signals &= ~SerialSignal_RTS;
  2755. if (clear & TIOCM_DTR)
  2756. info->signals &= ~SerialSignal_DTR;
  2757. spin_lock_irqsave(&info->lock,flags);
  2758. set_signals(info);
  2759. spin_unlock_irqrestore(&info->lock,flags);
  2760. return 0;
  2761. }
  2762. static int carrier_raised(struct tty_port *port)
  2763. {
  2764. unsigned long flags;
  2765. struct slgt_info *info = container_of(port, struct slgt_info, port);
  2766. spin_lock_irqsave(&info->lock,flags);
  2767. get_signals(info);
  2768. spin_unlock_irqrestore(&info->lock,flags);
  2769. return (info->signals & SerialSignal_DCD) ? 1 : 0;
  2770. }
  2771. static void dtr_rts(struct tty_port *port, int on)
  2772. {
  2773. unsigned long flags;
  2774. struct slgt_info *info = container_of(port, struct slgt_info, port);
  2775. spin_lock_irqsave(&info->lock,flags);
  2776. if (on)
  2777. info->signals |= SerialSignal_RTS | SerialSignal_DTR;
  2778. else
  2779. info->signals &= ~(SerialSignal_RTS | SerialSignal_DTR);
  2780. set_signals(info);
  2781. spin_unlock_irqrestore(&info->lock,flags);
  2782. }
  2783. /*
  2784. * block current process until the device is ready to open
  2785. */
  2786. static int block_til_ready(struct tty_struct *tty, struct file *filp,
  2787. struct slgt_info *info)
  2788. {
  2789. DECLARE_WAITQUEUE(wait, current);
  2790. int retval;
  2791. bool do_clocal = false;
  2792. unsigned long flags;
  2793. int cd;
  2794. struct tty_port *port = &info->port;
  2795. DBGINFO(("%s block_til_ready\n", tty->driver->name));
  2796. if (filp->f_flags & O_NONBLOCK || tty_io_error(tty)) {
  2797. /* nonblock mode is set or port is not enabled */
  2798. tty_port_set_active(port, 1);
  2799. return 0;
  2800. }
  2801. if (C_CLOCAL(tty))
  2802. do_clocal = true;
  2803. /* Wait for carrier detect and the line to become
  2804. * free (i.e., not in use by the callout). While we are in
  2805. * this loop, port->count is dropped by one, so that
  2806. * close() knows when to free things. We restore it upon
  2807. * exit, either normal or abnormal.
  2808. */
  2809. retval = 0;
  2810. add_wait_queue(&port->open_wait, &wait);
  2811. spin_lock_irqsave(&info->lock, flags);
  2812. port->count--;
  2813. spin_unlock_irqrestore(&info->lock, flags);
  2814. port->blocked_open++;
  2815. while (1) {
  2816. if (C_BAUD(tty) && tty_port_initialized(port))
  2817. tty_port_raise_dtr_rts(port);
  2818. set_current_state(TASK_INTERRUPTIBLE);
  2819. if (tty_hung_up_p(filp) || !tty_port_initialized(port)) {
  2820. retval = (port->flags & ASYNC_HUP_NOTIFY) ?
  2821. -EAGAIN : -ERESTARTSYS;
  2822. break;
  2823. }
  2824. cd = tty_port_carrier_raised(port);
  2825. if (do_clocal || cd)
  2826. break;
  2827. if (signal_pending(current)) {
  2828. retval = -ERESTARTSYS;
  2829. break;
  2830. }
  2831. DBGINFO(("%s block_til_ready wait\n", tty->driver->name));
  2832. tty_unlock(tty);
  2833. schedule();
  2834. tty_lock(tty);
  2835. }
  2836. set_current_state(TASK_RUNNING);
  2837. remove_wait_queue(&port->open_wait, &wait);
  2838. if (!tty_hung_up_p(filp))
  2839. port->count++;
  2840. port->blocked_open--;
  2841. if (!retval)
  2842. tty_port_set_active(port, 1);
  2843. DBGINFO(("%s block_til_ready ready, rc=%d\n", tty->driver->name, retval));
  2844. return retval;
  2845. }
  2846. /*
  2847. * allocate buffers used for calling line discipline receive_buf
  2848. * directly in synchronous mode
  2849. * note: add 5 bytes to max frame size to allow appending
  2850. * 32-bit CRC and status byte when configured to do so
  2851. */
  2852. static int alloc_tmp_rbuf(struct slgt_info *info)
  2853. {
  2854. info->tmp_rbuf = kmalloc(info->max_frame_size + 5, GFP_KERNEL);
  2855. if (info->tmp_rbuf == NULL)
  2856. return -ENOMEM;
  2857. /* unused flag buffer to satisfy receive_buf calling interface */
  2858. info->flag_buf = kzalloc(info->max_frame_size + 5, GFP_KERNEL);
  2859. if (!info->flag_buf) {
  2860. kfree(info->tmp_rbuf);
  2861. info->tmp_rbuf = NULL;
  2862. return -ENOMEM;
  2863. }
  2864. return 0;
  2865. }
  2866. static void free_tmp_rbuf(struct slgt_info *info)
  2867. {
  2868. kfree(info->tmp_rbuf);
  2869. info->tmp_rbuf = NULL;
  2870. kfree(info->flag_buf);
  2871. info->flag_buf = NULL;
  2872. }
  2873. /*
  2874. * allocate DMA descriptor lists.
  2875. */
  2876. static int alloc_desc(struct slgt_info *info)
  2877. {
  2878. unsigned int i;
  2879. unsigned int pbufs;
  2880. /* allocate memory to hold descriptor lists */
  2881. info->bufs = pci_zalloc_consistent(info->pdev, DESC_LIST_SIZE,
  2882. &info->bufs_dma_addr);
  2883. if (info->bufs == NULL)
  2884. return -ENOMEM;
  2885. info->rbufs = (struct slgt_desc*)info->bufs;
  2886. info->tbufs = ((struct slgt_desc*)info->bufs) + info->rbuf_count;
  2887. pbufs = (unsigned int)info->bufs_dma_addr;
  2888. /*
  2889. * Build circular lists of descriptors
  2890. */
  2891. for (i=0; i < info->rbuf_count; i++) {
  2892. /* physical address of this descriptor */
  2893. info->rbufs[i].pdesc = pbufs + (i * sizeof(struct slgt_desc));
  2894. /* physical address of next descriptor */
  2895. if (i == info->rbuf_count - 1)
  2896. info->rbufs[i].next = cpu_to_le32(pbufs);
  2897. else
  2898. info->rbufs[i].next = cpu_to_le32(pbufs + ((i+1) * sizeof(struct slgt_desc)));
  2899. set_desc_count(info->rbufs[i], DMABUFSIZE);
  2900. }
  2901. for (i=0; i < info->tbuf_count; i++) {
  2902. /* physical address of this descriptor */
  2903. info->tbufs[i].pdesc = pbufs + ((info->rbuf_count + i) * sizeof(struct slgt_desc));
  2904. /* physical address of next descriptor */
  2905. if (i == info->tbuf_count - 1)
  2906. info->tbufs[i].next = cpu_to_le32(pbufs + info->rbuf_count * sizeof(struct slgt_desc));
  2907. else
  2908. info->tbufs[i].next = cpu_to_le32(pbufs + ((info->rbuf_count + i + 1) * sizeof(struct slgt_desc)));
  2909. }
  2910. return 0;
  2911. }
  2912. static void free_desc(struct slgt_info *info)
  2913. {
  2914. if (info->bufs != NULL) {
  2915. pci_free_consistent(info->pdev, DESC_LIST_SIZE, info->bufs, info->bufs_dma_addr);
  2916. info->bufs = NULL;
  2917. info->rbufs = NULL;
  2918. info->tbufs = NULL;
  2919. }
  2920. }
  2921. static int alloc_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count)
  2922. {
  2923. int i;
  2924. for (i=0; i < count; i++) {
  2925. if ((bufs[i].buf = pci_alloc_consistent(info->pdev, DMABUFSIZE, &bufs[i].buf_dma_addr)) == NULL)
  2926. return -ENOMEM;
  2927. bufs[i].pbuf = cpu_to_le32((unsigned int)bufs[i].buf_dma_addr);
  2928. }
  2929. return 0;
  2930. }
  2931. static void free_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count)
  2932. {
  2933. int i;
  2934. for (i=0; i < count; i++) {
  2935. if (bufs[i].buf == NULL)
  2936. continue;
  2937. pci_free_consistent(info->pdev, DMABUFSIZE, bufs[i].buf, bufs[i].buf_dma_addr);
  2938. bufs[i].buf = NULL;
  2939. }
  2940. }
  2941. static int alloc_dma_bufs(struct slgt_info *info)
  2942. {
  2943. info->rbuf_count = 32;
  2944. info->tbuf_count = 32;
  2945. if (alloc_desc(info) < 0 ||
  2946. alloc_bufs(info, info->rbufs, info->rbuf_count) < 0 ||
  2947. alloc_bufs(info, info->tbufs, info->tbuf_count) < 0 ||
  2948. alloc_tmp_rbuf(info) < 0) {
  2949. DBGERR(("%s DMA buffer alloc fail\n", info->device_name));
  2950. return -ENOMEM;
  2951. }
  2952. reset_rbufs(info);
  2953. return 0;
  2954. }
  2955. static void free_dma_bufs(struct slgt_info *info)
  2956. {
  2957. if (info->bufs) {
  2958. free_bufs(info, info->rbufs, info->rbuf_count);
  2959. free_bufs(info, info->tbufs, info->tbuf_count);
  2960. free_desc(info);
  2961. }
  2962. free_tmp_rbuf(info);
  2963. }
  2964. static int claim_resources(struct slgt_info *info)
  2965. {
  2966. if (request_mem_region(info->phys_reg_addr, SLGT_REG_SIZE, "synclink_gt") == NULL) {
  2967. DBGERR(("%s reg addr conflict, addr=%08X\n",
  2968. info->device_name, info->phys_reg_addr));
  2969. info->init_error = DiagStatus_AddressConflict;
  2970. goto errout;
  2971. }
  2972. else
  2973. info->reg_addr_requested = true;
  2974. info->reg_addr = ioremap_nocache(info->phys_reg_addr, SLGT_REG_SIZE);
  2975. if (!info->reg_addr) {
  2976. DBGERR(("%s can't map device registers, addr=%08X\n",
  2977. info->device_name, info->phys_reg_addr));
  2978. info->init_error = DiagStatus_CantAssignPciResources;
  2979. goto errout;
  2980. }
  2981. return 0;
  2982. errout:
  2983. release_resources(info);
  2984. return -ENODEV;
  2985. }
  2986. static void release_resources(struct slgt_info *info)
  2987. {
  2988. if (info->irq_requested) {
  2989. free_irq(info->irq_level, info);
  2990. info->irq_requested = false;
  2991. }
  2992. if (info->reg_addr_requested) {
  2993. release_mem_region(info->phys_reg_addr, SLGT_REG_SIZE);
  2994. info->reg_addr_requested = false;
  2995. }
  2996. if (info->reg_addr) {
  2997. iounmap(info->reg_addr);
  2998. info->reg_addr = NULL;
  2999. }
  3000. }
  3001. /* Add the specified device instance data structure to the
  3002. * global linked list of devices and increment the device count.
  3003. */
  3004. static void add_device(struct slgt_info *info)
  3005. {
  3006. char *devstr;
  3007. info->next_device = NULL;
  3008. info->line = slgt_device_count;
  3009. sprintf(info->device_name, "%s%d", tty_dev_prefix, info->line);
  3010. if (info->line < MAX_DEVICES) {
  3011. if (maxframe[info->line])
  3012. info->max_frame_size = maxframe[info->line];
  3013. }
  3014. slgt_device_count++;
  3015. if (!slgt_device_list)
  3016. slgt_device_list = info;
  3017. else {
  3018. struct slgt_info *current_dev = slgt_device_list;
  3019. while(current_dev->next_device)
  3020. current_dev = current_dev->next_device;
  3021. current_dev->next_device = info;
  3022. }
  3023. if (info->max_frame_size < 4096)
  3024. info->max_frame_size = 4096;
  3025. else if (info->max_frame_size > 65535)
  3026. info->max_frame_size = 65535;
  3027. switch(info->pdev->device) {
  3028. case SYNCLINK_GT_DEVICE_ID:
  3029. devstr = "GT";
  3030. break;
  3031. case SYNCLINK_GT2_DEVICE_ID:
  3032. devstr = "GT2";
  3033. break;
  3034. case SYNCLINK_GT4_DEVICE_ID:
  3035. devstr = "GT4";
  3036. break;
  3037. case SYNCLINK_AC_DEVICE_ID:
  3038. devstr = "AC";
  3039. info->params.mode = MGSL_MODE_ASYNC;
  3040. break;
  3041. default:
  3042. devstr = "(unknown model)";
  3043. }
  3044. printk("SyncLink %s %s IO=%08x IRQ=%d MaxFrameSize=%u\n",
  3045. devstr, info->device_name, info->phys_reg_addr,
  3046. info->irq_level, info->max_frame_size);
  3047. #if SYNCLINK_GENERIC_HDLC
  3048. hdlcdev_init(info);
  3049. #endif
  3050. }
  3051. static const struct tty_port_operations slgt_port_ops = {
  3052. .carrier_raised = carrier_raised,
  3053. .dtr_rts = dtr_rts,
  3054. };
  3055. /*
  3056. * allocate device instance structure, return NULL on failure
  3057. */
  3058. static struct slgt_info *alloc_dev(int adapter_num, int port_num, struct pci_dev *pdev)
  3059. {
  3060. struct slgt_info *info;
  3061. info = kzalloc(sizeof(struct slgt_info), GFP_KERNEL);
  3062. if (!info) {
  3063. DBGERR(("%s device alloc failed adapter=%d port=%d\n",
  3064. driver_name, adapter_num, port_num));
  3065. } else {
  3066. tty_port_init(&info->port);
  3067. info->port.ops = &slgt_port_ops;
  3068. info->magic = MGSL_MAGIC;
  3069. INIT_WORK(&info->task, bh_handler);
  3070. info->max_frame_size = 4096;
  3071. info->base_clock = 14745600;
  3072. info->rbuf_fill_level = DMABUFSIZE;
  3073. info->port.close_delay = 5*HZ/10;
  3074. info->port.closing_wait = 30*HZ;
  3075. init_waitqueue_head(&info->status_event_wait_q);
  3076. init_waitqueue_head(&info->event_wait_q);
  3077. spin_lock_init(&info->netlock);
  3078. memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
  3079. info->idle_mode = HDLC_TXIDLE_FLAGS;
  3080. info->adapter_num = adapter_num;
  3081. info->port_num = port_num;
  3082. setup_timer(&info->tx_timer, tx_timeout, (unsigned long)info);
  3083. setup_timer(&info->rx_timer, rx_timeout, (unsigned long)info);
  3084. /* Copy configuration info to device instance data */
  3085. info->pdev = pdev;
  3086. info->irq_level = pdev->irq;
  3087. info->phys_reg_addr = pci_resource_start(pdev,0);
  3088. info->bus_type = MGSL_BUS_TYPE_PCI;
  3089. info->irq_flags = IRQF_SHARED;
  3090. info->init_error = -1; /* assume error, set to 0 on successful init */
  3091. }
  3092. return info;
  3093. }
  3094. static void device_init(int adapter_num, struct pci_dev *pdev)
  3095. {
  3096. struct slgt_info *port_array[SLGT_MAX_PORTS];
  3097. int i;
  3098. int port_count = 1;
  3099. if (pdev->device == SYNCLINK_GT2_DEVICE_ID)
  3100. port_count = 2;
  3101. else if (pdev->device == SYNCLINK_GT4_DEVICE_ID)
  3102. port_count = 4;
  3103. /* allocate device instances for all ports */
  3104. for (i=0; i < port_count; ++i) {
  3105. port_array[i] = alloc_dev(adapter_num, i, pdev);
  3106. if (port_array[i] == NULL) {
  3107. for (--i; i >= 0; --i) {
  3108. tty_port_destroy(&port_array[i]->port);
  3109. kfree(port_array[i]);
  3110. }
  3111. return;
  3112. }
  3113. }
  3114. /* give copy of port_array to all ports and add to device list */
  3115. for (i=0; i < port_count; ++i) {
  3116. memcpy(port_array[i]->port_array, port_array, sizeof(port_array));
  3117. add_device(port_array[i]);
  3118. port_array[i]->port_count = port_count;
  3119. spin_lock_init(&port_array[i]->lock);
  3120. }
  3121. /* Allocate and claim adapter resources */
  3122. if (!claim_resources(port_array[0])) {
  3123. alloc_dma_bufs(port_array[0]);
  3124. /* copy resource information from first port to others */
  3125. for (i = 1; i < port_count; ++i) {
  3126. port_array[i]->irq_level = port_array[0]->irq_level;
  3127. port_array[i]->reg_addr = port_array[0]->reg_addr;
  3128. alloc_dma_bufs(port_array[i]);
  3129. }
  3130. if (request_irq(port_array[0]->irq_level,
  3131. slgt_interrupt,
  3132. port_array[0]->irq_flags,
  3133. port_array[0]->device_name,
  3134. port_array[0]) < 0) {
  3135. DBGERR(("%s request_irq failed IRQ=%d\n",
  3136. port_array[0]->device_name,
  3137. port_array[0]->irq_level));
  3138. } else {
  3139. port_array[0]->irq_requested = true;
  3140. adapter_test(port_array[0]);
  3141. for (i=1 ; i < port_count ; i++) {
  3142. port_array[i]->init_error = port_array[0]->init_error;
  3143. port_array[i]->gpio_present = port_array[0]->gpio_present;
  3144. }
  3145. }
  3146. }
  3147. for (i = 0; i < port_count; ++i) {
  3148. struct slgt_info *info = port_array[i];
  3149. tty_port_register_device(&info->port, serial_driver, info->line,
  3150. &info->pdev->dev);
  3151. }
  3152. }
  3153. static int init_one(struct pci_dev *dev,
  3154. const struct pci_device_id *ent)
  3155. {
  3156. if (pci_enable_device(dev)) {
  3157. printk("error enabling pci device %p\n", dev);
  3158. return -EIO;
  3159. }
  3160. pci_set_master(dev);
  3161. device_init(slgt_device_count, dev);
  3162. return 0;
  3163. }
  3164. static void remove_one(struct pci_dev *dev)
  3165. {
  3166. }
  3167. static const struct tty_operations ops = {
  3168. .open = open,
  3169. .close = close,
  3170. .write = write,
  3171. .put_char = put_char,
  3172. .flush_chars = flush_chars,
  3173. .write_room = write_room,
  3174. .chars_in_buffer = chars_in_buffer,
  3175. .flush_buffer = flush_buffer,
  3176. .ioctl = ioctl,
  3177. .compat_ioctl = slgt_compat_ioctl,
  3178. .throttle = throttle,
  3179. .unthrottle = unthrottle,
  3180. .send_xchar = send_xchar,
  3181. .break_ctl = set_break,
  3182. .wait_until_sent = wait_until_sent,
  3183. .set_termios = set_termios,
  3184. .stop = tx_hold,
  3185. .start = tx_release,
  3186. .hangup = hangup,
  3187. .tiocmget = tiocmget,
  3188. .tiocmset = tiocmset,
  3189. .get_icount = get_icount,
  3190. .proc_fops = &synclink_gt_proc_fops,
  3191. };
  3192. static void slgt_cleanup(void)
  3193. {
  3194. int rc;
  3195. struct slgt_info *info;
  3196. struct slgt_info *tmp;
  3197. printk(KERN_INFO "unload %s\n", driver_name);
  3198. if (serial_driver) {
  3199. for (info=slgt_device_list ; info != NULL ; info=info->next_device)
  3200. tty_unregister_device(serial_driver, info->line);
  3201. rc = tty_unregister_driver(serial_driver);
  3202. if (rc)
  3203. DBGERR(("tty_unregister_driver error=%d\n", rc));
  3204. put_tty_driver(serial_driver);
  3205. }
  3206. /* reset devices */
  3207. info = slgt_device_list;
  3208. while(info) {
  3209. reset_port(info);
  3210. info = info->next_device;
  3211. }
  3212. /* release devices */
  3213. info = slgt_device_list;
  3214. while(info) {
  3215. #if SYNCLINK_GENERIC_HDLC
  3216. hdlcdev_exit(info);
  3217. #endif
  3218. free_dma_bufs(info);
  3219. free_tmp_rbuf(info);
  3220. if (info->port_num == 0)
  3221. release_resources(info);
  3222. tmp = info;
  3223. info = info->next_device;
  3224. tty_port_destroy(&tmp->port);
  3225. kfree(tmp);
  3226. }
  3227. if (pci_registered)
  3228. pci_unregister_driver(&pci_driver);
  3229. }
  3230. /*
  3231. * Driver initialization entry point.
  3232. */
  3233. static int __init slgt_init(void)
  3234. {
  3235. int rc;
  3236. printk(KERN_INFO "%s\n", driver_name);
  3237. serial_driver = alloc_tty_driver(MAX_DEVICES);
  3238. if (!serial_driver) {
  3239. printk("%s can't allocate tty driver\n", driver_name);
  3240. return -ENOMEM;
  3241. }
  3242. /* Initialize the tty_driver structure */
  3243. serial_driver->driver_name = slgt_driver_name;
  3244. serial_driver->name = tty_dev_prefix;
  3245. serial_driver->major = ttymajor;
  3246. serial_driver->minor_start = 64;
  3247. serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  3248. serial_driver->subtype = SERIAL_TYPE_NORMAL;
  3249. serial_driver->init_termios = tty_std_termios;
  3250. serial_driver->init_termios.c_cflag =
  3251. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  3252. serial_driver->init_termios.c_ispeed = 9600;
  3253. serial_driver->init_termios.c_ospeed = 9600;
  3254. serial_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  3255. tty_set_operations(serial_driver, &ops);
  3256. if ((rc = tty_register_driver(serial_driver)) < 0) {
  3257. DBGERR(("%s can't register serial driver\n", driver_name));
  3258. put_tty_driver(serial_driver);
  3259. serial_driver = NULL;
  3260. goto error;
  3261. }
  3262. printk(KERN_INFO "%s, tty major#%d\n",
  3263. driver_name, serial_driver->major);
  3264. slgt_device_count = 0;
  3265. if ((rc = pci_register_driver(&pci_driver)) < 0) {
  3266. printk("%s pci_register_driver error=%d\n", driver_name, rc);
  3267. goto error;
  3268. }
  3269. pci_registered = true;
  3270. if (!slgt_device_list)
  3271. printk("%s no devices found\n",driver_name);
  3272. return 0;
  3273. error:
  3274. slgt_cleanup();
  3275. return rc;
  3276. }
  3277. static void __exit slgt_exit(void)
  3278. {
  3279. slgt_cleanup();
  3280. }
  3281. module_init(slgt_init);
  3282. module_exit(slgt_exit);
  3283. /*
  3284. * register access routines
  3285. */
  3286. #define CALC_REGADDR() \
  3287. unsigned long reg_addr = ((unsigned long)info->reg_addr) + addr; \
  3288. if (addr >= 0x80) \
  3289. reg_addr += (info->port_num) * 32; \
  3290. else if (addr >= 0x40) \
  3291. reg_addr += (info->port_num) * 16;
  3292. static __u8 rd_reg8(struct slgt_info *info, unsigned int addr)
  3293. {
  3294. CALC_REGADDR();
  3295. return readb((void __iomem *)reg_addr);
  3296. }
  3297. static void wr_reg8(struct slgt_info *info, unsigned int addr, __u8 value)
  3298. {
  3299. CALC_REGADDR();
  3300. writeb(value, (void __iomem *)reg_addr);
  3301. }
  3302. static __u16 rd_reg16(struct slgt_info *info, unsigned int addr)
  3303. {
  3304. CALC_REGADDR();
  3305. return readw((void __iomem *)reg_addr);
  3306. }
  3307. static void wr_reg16(struct slgt_info *info, unsigned int addr, __u16 value)
  3308. {
  3309. CALC_REGADDR();
  3310. writew(value, (void __iomem *)reg_addr);
  3311. }
  3312. static __u32 rd_reg32(struct slgt_info *info, unsigned int addr)
  3313. {
  3314. CALC_REGADDR();
  3315. return readl((void __iomem *)reg_addr);
  3316. }
  3317. static void wr_reg32(struct slgt_info *info, unsigned int addr, __u32 value)
  3318. {
  3319. CALC_REGADDR();
  3320. writel(value, (void __iomem *)reg_addr);
  3321. }
  3322. static void rdma_reset(struct slgt_info *info)
  3323. {
  3324. unsigned int i;
  3325. /* set reset bit */
  3326. wr_reg32(info, RDCSR, BIT1);
  3327. /* wait for enable bit cleared */
  3328. for(i=0 ; i < 1000 ; i++)
  3329. if (!(rd_reg32(info, RDCSR) & BIT0))
  3330. break;
  3331. }
  3332. static void tdma_reset(struct slgt_info *info)
  3333. {
  3334. unsigned int i;
  3335. /* set reset bit */
  3336. wr_reg32(info, TDCSR, BIT1);
  3337. /* wait for enable bit cleared */
  3338. for(i=0 ; i < 1000 ; i++)
  3339. if (!(rd_reg32(info, TDCSR) & BIT0))
  3340. break;
  3341. }
  3342. /*
  3343. * enable internal loopback
  3344. * TxCLK and RxCLK are generated from BRG
  3345. * and TxD is looped back to RxD internally.
  3346. */
  3347. static void enable_loopback(struct slgt_info *info)
  3348. {
  3349. /* SCR (serial control) BIT2=loopback enable */
  3350. wr_reg16(info, SCR, (unsigned short)(rd_reg16(info, SCR) | BIT2));
  3351. if (info->params.mode != MGSL_MODE_ASYNC) {
  3352. /* CCR (clock control)
  3353. * 07..05 tx clock source (010 = BRG)
  3354. * 04..02 rx clock source (010 = BRG)
  3355. * 01 auxclk enable (0 = disable)
  3356. * 00 BRG enable (1 = enable)
  3357. *
  3358. * 0100 1001
  3359. */
  3360. wr_reg8(info, CCR, 0x49);
  3361. /* set speed if available, otherwise use default */
  3362. if (info->params.clock_speed)
  3363. set_rate(info, info->params.clock_speed);
  3364. else
  3365. set_rate(info, 3686400);
  3366. }
  3367. }
  3368. /*
  3369. * set baud rate generator to specified rate
  3370. */
  3371. static void set_rate(struct slgt_info *info, u32 rate)
  3372. {
  3373. unsigned int div;
  3374. unsigned int osc = info->base_clock;
  3375. /* div = osc/rate - 1
  3376. *
  3377. * Round div up if osc/rate is not integer to
  3378. * force to next slowest rate.
  3379. */
  3380. if (rate) {
  3381. div = osc/rate;
  3382. if (!(osc % rate) && div)
  3383. div--;
  3384. wr_reg16(info, BDR, (unsigned short)div);
  3385. }
  3386. }
  3387. static void rx_stop(struct slgt_info *info)
  3388. {
  3389. unsigned short val;
  3390. /* disable and reset receiver */
  3391. val = rd_reg16(info, RCR) & ~BIT1; /* clear enable bit */
  3392. wr_reg16(info, RCR, (unsigned short)(val | BIT2)); /* set reset bit */
  3393. wr_reg16(info, RCR, val); /* clear reset bit */
  3394. slgt_irq_off(info, IRQ_RXOVER + IRQ_RXDATA + IRQ_RXIDLE);
  3395. /* clear pending rx interrupts */
  3396. wr_reg16(info, SSR, IRQ_RXIDLE + IRQ_RXOVER);
  3397. rdma_reset(info);
  3398. info->rx_enabled = false;
  3399. info->rx_restart = false;
  3400. }
  3401. static void rx_start(struct slgt_info *info)
  3402. {
  3403. unsigned short val;
  3404. slgt_irq_off(info, IRQ_RXOVER + IRQ_RXDATA);
  3405. /* clear pending rx overrun IRQ */
  3406. wr_reg16(info, SSR, IRQ_RXOVER);
  3407. /* reset and disable receiver */
  3408. val = rd_reg16(info, RCR) & ~BIT1; /* clear enable bit */
  3409. wr_reg16(info, RCR, (unsigned short)(val | BIT2)); /* set reset bit */
  3410. wr_reg16(info, RCR, val); /* clear reset bit */
  3411. rdma_reset(info);
  3412. reset_rbufs(info);
  3413. if (info->rx_pio) {
  3414. /* rx request when rx FIFO not empty */
  3415. wr_reg16(info, SCR, (unsigned short)(rd_reg16(info, SCR) & ~BIT14));
  3416. slgt_irq_on(info, IRQ_RXDATA);
  3417. if (info->params.mode == MGSL_MODE_ASYNC) {
  3418. /* enable saving of rx status */
  3419. wr_reg32(info, RDCSR, BIT6);
  3420. }
  3421. } else {
  3422. /* rx request when rx FIFO half full */
  3423. wr_reg16(info, SCR, (unsigned short)(rd_reg16(info, SCR) | BIT14));
  3424. /* set 1st descriptor address */
  3425. wr_reg32(info, RDDAR, info->rbufs[0].pdesc);
  3426. if (info->params.mode != MGSL_MODE_ASYNC) {
  3427. /* enable rx DMA and DMA interrupt */
  3428. wr_reg32(info, RDCSR, (BIT2 + BIT0));
  3429. } else {
  3430. /* enable saving of rx status, rx DMA and DMA interrupt */
  3431. wr_reg32(info, RDCSR, (BIT6 + BIT2 + BIT0));
  3432. }
  3433. }
  3434. slgt_irq_on(info, IRQ_RXOVER);
  3435. /* enable receiver */
  3436. wr_reg16(info, RCR, (unsigned short)(rd_reg16(info, RCR) | BIT1));
  3437. info->rx_restart = false;
  3438. info->rx_enabled = true;
  3439. }
  3440. static void tx_start(struct slgt_info *info)
  3441. {
  3442. if (!info->tx_enabled) {
  3443. wr_reg16(info, TCR,
  3444. (unsigned short)((rd_reg16(info, TCR) | BIT1) & ~BIT2));
  3445. info->tx_enabled = true;
  3446. }
  3447. if (desc_count(info->tbufs[info->tbuf_start])) {
  3448. info->drop_rts_on_tx_done = false;
  3449. if (info->params.mode != MGSL_MODE_ASYNC) {
  3450. if (info->params.flags & HDLC_FLAG_AUTO_RTS) {
  3451. get_signals(info);
  3452. if (!(info->signals & SerialSignal_RTS)) {
  3453. info->signals |= SerialSignal_RTS;
  3454. set_signals(info);
  3455. info->drop_rts_on_tx_done = true;
  3456. }
  3457. }
  3458. slgt_irq_off(info, IRQ_TXDATA);
  3459. slgt_irq_on(info, IRQ_TXUNDER + IRQ_TXIDLE);
  3460. /* clear tx idle and underrun status bits */
  3461. wr_reg16(info, SSR, (unsigned short)(IRQ_TXIDLE + IRQ_TXUNDER));
  3462. } else {
  3463. slgt_irq_off(info, IRQ_TXDATA);
  3464. slgt_irq_on(info, IRQ_TXIDLE);
  3465. /* clear tx idle status bit */
  3466. wr_reg16(info, SSR, IRQ_TXIDLE);
  3467. }
  3468. /* set 1st descriptor address and start DMA */
  3469. wr_reg32(info, TDDAR, info->tbufs[info->tbuf_start].pdesc);
  3470. wr_reg32(info, TDCSR, BIT2 + BIT0);
  3471. info->tx_active = true;
  3472. }
  3473. }
  3474. static void tx_stop(struct slgt_info *info)
  3475. {
  3476. unsigned short val;
  3477. del_timer(&info->tx_timer);
  3478. tdma_reset(info);
  3479. /* reset and disable transmitter */
  3480. val = rd_reg16(info, TCR) & ~BIT1; /* clear enable bit */
  3481. wr_reg16(info, TCR, (unsigned short)(val | BIT2)); /* set reset bit */
  3482. slgt_irq_off(info, IRQ_TXDATA + IRQ_TXIDLE + IRQ_TXUNDER);
  3483. /* clear tx idle and underrun status bit */
  3484. wr_reg16(info, SSR, (unsigned short)(IRQ_TXIDLE + IRQ_TXUNDER));
  3485. reset_tbufs(info);
  3486. info->tx_enabled = false;
  3487. info->tx_active = false;
  3488. }
  3489. static void reset_port(struct slgt_info *info)
  3490. {
  3491. if (!info->reg_addr)
  3492. return;
  3493. tx_stop(info);
  3494. rx_stop(info);
  3495. info->signals &= ~(SerialSignal_RTS | SerialSignal_DTR);
  3496. set_signals(info);
  3497. slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
  3498. }
  3499. static void reset_adapter(struct slgt_info *info)
  3500. {
  3501. int i;
  3502. for (i=0; i < info->port_count; ++i) {
  3503. if (info->port_array[i])
  3504. reset_port(info->port_array[i]);
  3505. }
  3506. }
  3507. static void async_mode(struct slgt_info *info)
  3508. {
  3509. unsigned short val;
  3510. slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
  3511. tx_stop(info);
  3512. rx_stop(info);
  3513. /* TCR (tx control)
  3514. *
  3515. * 15..13 mode, 010=async
  3516. * 12..10 encoding, 000=NRZ
  3517. * 09 parity enable
  3518. * 08 1=odd parity, 0=even parity
  3519. * 07 1=RTS driver control
  3520. * 06 1=break enable
  3521. * 05..04 character length
  3522. * 00=5 bits
  3523. * 01=6 bits
  3524. * 10=7 bits
  3525. * 11=8 bits
  3526. * 03 0=1 stop bit, 1=2 stop bits
  3527. * 02 reset
  3528. * 01 enable
  3529. * 00 auto-CTS enable
  3530. */
  3531. val = 0x4000;
  3532. if (info->if_mode & MGSL_INTERFACE_RTS_EN)
  3533. val |= BIT7;
  3534. if (info->params.parity != ASYNC_PARITY_NONE) {
  3535. val |= BIT9;
  3536. if (info->params.parity == ASYNC_PARITY_ODD)
  3537. val |= BIT8;
  3538. }
  3539. switch (info->params.data_bits)
  3540. {
  3541. case 6: val |= BIT4; break;
  3542. case 7: val |= BIT5; break;
  3543. case 8: val |= BIT5 + BIT4; break;
  3544. }
  3545. if (info->params.stop_bits != 1)
  3546. val |= BIT3;
  3547. if (info->params.flags & HDLC_FLAG_AUTO_CTS)
  3548. val |= BIT0;
  3549. wr_reg16(info, TCR, val);
  3550. /* RCR (rx control)
  3551. *
  3552. * 15..13 mode, 010=async
  3553. * 12..10 encoding, 000=NRZ
  3554. * 09 parity enable
  3555. * 08 1=odd parity, 0=even parity
  3556. * 07..06 reserved, must be 0
  3557. * 05..04 character length
  3558. * 00=5 bits
  3559. * 01=6 bits
  3560. * 10=7 bits
  3561. * 11=8 bits
  3562. * 03 reserved, must be zero
  3563. * 02 reset
  3564. * 01 enable
  3565. * 00 auto-DCD enable
  3566. */
  3567. val = 0x4000;
  3568. if (info->params.parity != ASYNC_PARITY_NONE) {
  3569. val |= BIT9;
  3570. if (info->params.parity == ASYNC_PARITY_ODD)
  3571. val |= BIT8;
  3572. }
  3573. switch (info->params.data_bits)
  3574. {
  3575. case 6: val |= BIT4; break;
  3576. case 7: val |= BIT5; break;
  3577. case 8: val |= BIT5 + BIT4; break;
  3578. }
  3579. if (info->params.flags & HDLC_FLAG_AUTO_DCD)
  3580. val |= BIT0;
  3581. wr_reg16(info, RCR, val);
  3582. /* CCR (clock control)
  3583. *
  3584. * 07..05 011 = tx clock source is BRG/16
  3585. * 04..02 010 = rx clock source is BRG
  3586. * 01 0 = auxclk disabled
  3587. * 00 1 = BRG enabled
  3588. *
  3589. * 0110 1001
  3590. */
  3591. wr_reg8(info, CCR, 0x69);
  3592. msc_set_vcr(info);
  3593. /* SCR (serial control)
  3594. *
  3595. * 15 1=tx req on FIFO half empty
  3596. * 14 1=rx req on FIFO half full
  3597. * 13 tx data IRQ enable
  3598. * 12 tx idle IRQ enable
  3599. * 11 rx break on IRQ enable
  3600. * 10 rx data IRQ enable
  3601. * 09 rx break off IRQ enable
  3602. * 08 overrun IRQ enable
  3603. * 07 DSR IRQ enable
  3604. * 06 CTS IRQ enable
  3605. * 05 DCD IRQ enable
  3606. * 04 RI IRQ enable
  3607. * 03 0=16x sampling, 1=8x sampling
  3608. * 02 1=txd->rxd internal loopback enable
  3609. * 01 reserved, must be zero
  3610. * 00 1=master IRQ enable
  3611. */
  3612. val = BIT15 + BIT14 + BIT0;
  3613. /* JCR[8] : 1 = x8 async mode feature available */
  3614. if ((rd_reg32(info, JCR) & BIT8) && info->params.data_rate &&
  3615. ((info->base_clock < (info->params.data_rate * 16)) ||
  3616. (info->base_clock % (info->params.data_rate * 16)))) {
  3617. /* use 8x sampling */
  3618. val |= BIT3;
  3619. set_rate(info, info->params.data_rate * 8);
  3620. } else {
  3621. /* use 16x sampling */
  3622. set_rate(info, info->params.data_rate * 16);
  3623. }
  3624. wr_reg16(info, SCR, val);
  3625. slgt_irq_on(info, IRQ_RXBREAK | IRQ_RXOVER);
  3626. if (info->params.loopback)
  3627. enable_loopback(info);
  3628. }
  3629. static void sync_mode(struct slgt_info *info)
  3630. {
  3631. unsigned short val;
  3632. slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
  3633. tx_stop(info);
  3634. rx_stop(info);
  3635. /* TCR (tx control)
  3636. *
  3637. * 15..13 mode
  3638. * 000=HDLC/SDLC
  3639. * 001=raw bit synchronous
  3640. * 010=asynchronous/isochronous
  3641. * 011=monosync byte synchronous
  3642. * 100=bisync byte synchronous
  3643. * 101=xsync byte synchronous
  3644. * 12..10 encoding
  3645. * 09 CRC enable
  3646. * 08 CRC32
  3647. * 07 1=RTS driver control
  3648. * 06 preamble enable
  3649. * 05..04 preamble length
  3650. * 03 share open/close flag
  3651. * 02 reset
  3652. * 01 enable
  3653. * 00 auto-CTS enable
  3654. */
  3655. val = BIT2;
  3656. switch(info->params.mode) {
  3657. case MGSL_MODE_XSYNC:
  3658. val |= BIT15 + BIT13;
  3659. break;
  3660. case MGSL_MODE_MONOSYNC: val |= BIT14 + BIT13; break;
  3661. case MGSL_MODE_BISYNC: val |= BIT15; break;
  3662. case MGSL_MODE_RAW: val |= BIT13; break;
  3663. }
  3664. if (info->if_mode & MGSL_INTERFACE_RTS_EN)
  3665. val |= BIT7;
  3666. switch(info->params.encoding)
  3667. {
  3668. case HDLC_ENCODING_NRZB: val |= BIT10; break;
  3669. case HDLC_ENCODING_NRZI_MARK: val |= BIT11; break;
  3670. case HDLC_ENCODING_NRZI: val |= BIT11 + BIT10; break;
  3671. case HDLC_ENCODING_BIPHASE_MARK: val |= BIT12; break;
  3672. case HDLC_ENCODING_BIPHASE_SPACE: val |= BIT12 + BIT10; break;
  3673. case HDLC_ENCODING_BIPHASE_LEVEL: val |= BIT12 + BIT11; break;
  3674. case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: val |= BIT12 + BIT11 + BIT10; break;
  3675. }
  3676. switch (info->params.crc_type & HDLC_CRC_MASK)
  3677. {
  3678. case HDLC_CRC_16_CCITT: val |= BIT9; break;
  3679. case HDLC_CRC_32_CCITT: val |= BIT9 + BIT8; break;
  3680. }
  3681. if (info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE)
  3682. val |= BIT6;
  3683. switch (info->params.preamble_length)
  3684. {
  3685. case HDLC_PREAMBLE_LENGTH_16BITS: val |= BIT5; break;
  3686. case HDLC_PREAMBLE_LENGTH_32BITS: val |= BIT4; break;
  3687. case HDLC_PREAMBLE_LENGTH_64BITS: val |= BIT5 + BIT4; break;
  3688. }
  3689. if (info->params.flags & HDLC_FLAG_AUTO_CTS)
  3690. val |= BIT0;
  3691. wr_reg16(info, TCR, val);
  3692. /* TPR (transmit preamble) */
  3693. switch (info->params.preamble)
  3694. {
  3695. case HDLC_PREAMBLE_PATTERN_FLAGS: val = 0x7e; break;
  3696. case HDLC_PREAMBLE_PATTERN_ONES: val = 0xff; break;
  3697. case HDLC_PREAMBLE_PATTERN_ZEROS: val = 0x00; break;
  3698. case HDLC_PREAMBLE_PATTERN_10: val = 0x55; break;
  3699. case HDLC_PREAMBLE_PATTERN_01: val = 0xaa; break;
  3700. default: val = 0x7e; break;
  3701. }
  3702. wr_reg8(info, TPR, (unsigned char)val);
  3703. /* RCR (rx control)
  3704. *
  3705. * 15..13 mode
  3706. * 000=HDLC/SDLC
  3707. * 001=raw bit synchronous
  3708. * 010=asynchronous/isochronous
  3709. * 011=monosync byte synchronous
  3710. * 100=bisync byte synchronous
  3711. * 101=xsync byte synchronous
  3712. * 12..10 encoding
  3713. * 09 CRC enable
  3714. * 08 CRC32
  3715. * 07..03 reserved, must be 0
  3716. * 02 reset
  3717. * 01 enable
  3718. * 00 auto-DCD enable
  3719. */
  3720. val = 0;
  3721. switch(info->params.mode) {
  3722. case MGSL_MODE_XSYNC:
  3723. val |= BIT15 + BIT13;
  3724. break;
  3725. case MGSL_MODE_MONOSYNC: val |= BIT14 + BIT13; break;
  3726. case MGSL_MODE_BISYNC: val |= BIT15; break;
  3727. case MGSL_MODE_RAW: val |= BIT13; break;
  3728. }
  3729. switch(info->params.encoding)
  3730. {
  3731. case HDLC_ENCODING_NRZB: val |= BIT10; break;
  3732. case HDLC_ENCODING_NRZI_MARK: val |= BIT11; break;
  3733. case HDLC_ENCODING_NRZI: val |= BIT11 + BIT10; break;
  3734. case HDLC_ENCODING_BIPHASE_MARK: val |= BIT12; break;
  3735. case HDLC_ENCODING_BIPHASE_SPACE: val |= BIT12 + BIT10; break;
  3736. case HDLC_ENCODING_BIPHASE_LEVEL: val |= BIT12 + BIT11; break;
  3737. case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: val |= BIT12 + BIT11 + BIT10; break;
  3738. }
  3739. switch (info->params.crc_type & HDLC_CRC_MASK)
  3740. {
  3741. case HDLC_CRC_16_CCITT: val |= BIT9; break;
  3742. case HDLC_CRC_32_CCITT: val |= BIT9 + BIT8; break;
  3743. }
  3744. if (info->params.flags & HDLC_FLAG_AUTO_DCD)
  3745. val |= BIT0;
  3746. wr_reg16(info, RCR, val);
  3747. /* CCR (clock control)
  3748. *
  3749. * 07..05 tx clock source
  3750. * 04..02 rx clock source
  3751. * 01 auxclk enable
  3752. * 00 BRG enable
  3753. */
  3754. val = 0;
  3755. if (info->params.flags & HDLC_FLAG_TXC_BRG)
  3756. {
  3757. // when RxC source is DPLL, BRG generates 16X DPLL
  3758. // reference clock, so take TxC from BRG/16 to get
  3759. // transmit clock at actual data rate
  3760. if (info->params.flags & HDLC_FLAG_RXC_DPLL)
  3761. val |= BIT6 + BIT5; /* 011, txclk = BRG/16 */
  3762. else
  3763. val |= BIT6; /* 010, txclk = BRG */
  3764. }
  3765. else if (info->params.flags & HDLC_FLAG_TXC_DPLL)
  3766. val |= BIT7; /* 100, txclk = DPLL Input */
  3767. else if (info->params.flags & HDLC_FLAG_TXC_RXCPIN)
  3768. val |= BIT5; /* 001, txclk = RXC Input */
  3769. if (info->params.flags & HDLC_FLAG_RXC_BRG)
  3770. val |= BIT3; /* 010, rxclk = BRG */
  3771. else if (info->params.flags & HDLC_FLAG_RXC_DPLL)
  3772. val |= BIT4; /* 100, rxclk = DPLL */
  3773. else if (info->params.flags & HDLC_FLAG_RXC_TXCPIN)
  3774. val |= BIT2; /* 001, rxclk = TXC Input */
  3775. if (info->params.clock_speed)
  3776. val |= BIT1 + BIT0;
  3777. wr_reg8(info, CCR, (unsigned char)val);
  3778. if (info->params.flags & (HDLC_FLAG_TXC_DPLL + HDLC_FLAG_RXC_DPLL))
  3779. {
  3780. // program DPLL mode
  3781. switch(info->params.encoding)
  3782. {
  3783. case HDLC_ENCODING_BIPHASE_MARK:
  3784. case HDLC_ENCODING_BIPHASE_SPACE:
  3785. val = BIT7; break;
  3786. case HDLC_ENCODING_BIPHASE_LEVEL:
  3787. case HDLC_ENCODING_DIFF_BIPHASE_LEVEL:
  3788. val = BIT7 + BIT6; break;
  3789. default: val = BIT6; // NRZ encodings
  3790. }
  3791. wr_reg16(info, RCR, (unsigned short)(rd_reg16(info, RCR) | val));
  3792. // DPLL requires a 16X reference clock from BRG
  3793. set_rate(info, info->params.clock_speed * 16);
  3794. }
  3795. else
  3796. set_rate(info, info->params.clock_speed);
  3797. tx_set_idle(info);
  3798. msc_set_vcr(info);
  3799. /* SCR (serial control)
  3800. *
  3801. * 15 1=tx req on FIFO half empty
  3802. * 14 1=rx req on FIFO half full
  3803. * 13 tx data IRQ enable
  3804. * 12 tx idle IRQ enable
  3805. * 11 underrun IRQ enable
  3806. * 10 rx data IRQ enable
  3807. * 09 rx idle IRQ enable
  3808. * 08 overrun IRQ enable
  3809. * 07 DSR IRQ enable
  3810. * 06 CTS IRQ enable
  3811. * 05 DCD IRQ enable
  3812. * 04 RI IRQ enable
  3813. * 03 reserved, must be zero
  3814. * 02 1=txd->rxd internal loopback enable
  3815. * 01 reserved, must be zero
  3816. * 00 1=master IRQ enable
  3817. */
  3818. wr_reg16(info, SCR, BIT15 + BIT14 + BIT0);
  3819. if (info->params.loopback)
  3820. enable_loopback(info);
  3821. }
  3822. /*
  3823. * set transmit idle mode
  3824. */
  3825. static void tx_set_idle(struct slgt_info *info)
  3826. {
  3827. unsigned char val;
  3828. unsigned short tcr;
  3829. /* if preamble enabled (tcr[6] == 1) then tx idle size = 8 bits
  3830. * else tcr[5:4] = tx idle size: 00 = 8 bits, 01 = 16 bits
  3831. */
  3832. tcr = rd_reg16(info, TCR);
  3833. if (info->idle_mode & HDLC_TXIDLE_CUSTOM_16) {
  3834. /* disable preamble, set idle size to 16 bits */
  3835. tcr = (tcr & ~(BIT6 + BIT5)) | BIT4;
  3836. /* MSB of 16 bit idle specified in tx preamble register (TPR) */
  3837. wr_reg8(info, TPR, (unsigned char)((info->idle_mode >> 8) & 0xff));
  3838. } else if (!(tcr & BIT6)) {
  3839. /* preamble is disabled, set idle size to 8 bits */
  3840. tcr &= ~(BIT5 + BIT4);
  3841. }
  3842. wr_reg16(info, TCR, tcr);
  3843. if (info->idle_mode & (HDLC_TXIDLE_CUSTOM_8 | HDLC_TXIDLE_CUSTOM_16)) {
  3844. /* LSB of custom tx idle specified in tx idle register */
  3845. val = (unsigned char)(info->idle_mode & 0xff);
  3846. } else {
  3847. /* standard 8 bit idle patterns */
  3848. switch(info->idle_mode)
  3849. {
  3850. case HDLC_TXIDLE_FLAGS: val = 0x7e; break;
  3851. case HDLC_TXIDLE_ALT_ZEROS_ONES:
  3852. case HDLC_TXIDLE_ALT_MARK_SPACE: val = 0xaa; break;
  3853. case HDLC_TXIDLE_ZEROS:
  3854. case HDLC_TXIDLE_SPACE: val = 0x00; break;
  3855. default: val = 0xff;
  3856. }
  3857. }
  3858. wr_reg8(info, TIR, val);
  3859. }
  3860. /*
  3861. * get state of V24 status (input) signals
  3862. */
  3863. static void get_signals(struct slgt_info *info)
  3864. {
  3865. unsigned short status = rd_reg16(info, SSR);
  3866. /* clear all serial signals except RTS and DTR */
  3867. info->signals &= SerialSignal_RTS | SerialSignal_DTR;
  3868. if (status & BIT3)
  3869. info->signals |= SerialSignal_DSR;
  3870. if (status & BIT2)
  3871. info->signals |= SerialSignal_CTS;
  3872. if (status & BIT1)
  3873. info->signals |= SerialSignal_DCD;
  3874. if (status & BIT0)
  3875. info->signals |= SerialSignal_RI;
  3876. }
  3877. /*
  3878. * set V.24 Control Register based on current configuration
  3879. */
  3880. static void msc_set_vcr(struct slgt_info *info)
  3881. {
  3882. unsigned char val = 0;
  3883. /* VCR (V.24 control)
  3884. *
  3885. * 07..04 serial IF select
  3886. * 03 DTR
  3887. * 02 RTS
  3888. * 01 LL
  3889. * 00 RL
  3890. */
  3891. switch(info->if_mode & MGSL_INTERFACE_MASK)
  3892. {
  3893. case MGSL_INTERFACE_RS232:
  3894. val |= BIT5; /* 0010 */
  3895. break;
  3896. case MGSL_INTERFACE_V35:
  3897. val |= BIT7 + BIT6 + BIT5; /* 1110 */
  3898. break;
  3899. case MGSL_INTERFACE_RS422:
  3900. val |= BIT6; /* 0100 */
  3901. break;
  3902. }
  3903. if (info->if_mode & MGSL_INTERFACE_MSB_FIRST)
  3904. val |= BIT4;
  3905. if (info->signals & SerialSignal_DTR)
  3906. val |= BIT3;
  3907. if (info->signals & SerialSignal_RTS)
  3908. val |= BIT2;
  3909. if (info->if_mode & MGSL_INTERFACE_LL)
  3910. val |= BIT1;
  3911. if (info->if_mode & MGSL_INTERFACE_RL)
  3912. val |= BIT0;
  3913. wr_reg8(info, VCR, val);
  3914. }
  3915. /*
  3916. * set state of V24 control (output) signals
  3917. */
  3918. static void set_signals(struct slgt_info *info)
  3919. {
  3920. unsigned char val = rd_reg8(info, VCR);
  3921. if (info->signals & SerialSignal_DTR)
  3922. val |= BIT3;
  3923. else
  3924. val &= ~BIT3;
  3925. if (info->signals & SerialSignal_RTS)
  3926. val |= BIT2;
  3927. else
  3928. val &= ~BIT2;
  3929. wr_reg8(info, VCR, val);
  3930. }
  3931. /*
  3932. * free range of receive DMA buffers (i to last)
  3933. */
  3934. static void free_rbufs(struct slgt_info *info, unsigned int i, unsigned int last)
  3935. {
  3936. int done = 0;
  3937. while(!done) {
  3938. /* reset current buffer for reuse */
  3939. info->rbufs[i].status = 0;
  3940. set_desc_count(info->rbufs[i], info->rbuf_fill_level);
  3941. if (i == last)
  3942. done = 1;
  3943. if (++i == info->rbuf_count)
  3944. i = 0;
  3945. }
  3946. info->rbuf_current = i;
  3947. }
  3948. /*
  3949. * mark all receive DMA buffers as free
  3950. */
  3951. static void reset_rbufs(struct slgt_info *info)
  3952. {
  3953. free_rbufs(info, 0, info->rbuf_count - 1);
  3954. info->rbuf_fill_index = 0;
  3955. info->rbuf_fill_count = 0;
  3956. }
  3957. /*
  3958. * pass receive HDLC frame to upper layer
  3959. *
  3960. * return true if frame available, otherwise false
  3961. */
  3962. static bool rx_get_frame(struct slgt_info *info)
  3963. {
  3964. unsigned int start, end;
  3965. unsigned short status;
  3966. unsigned int framesize = 0;
  3967. unsigned long flags;
  3968. struct tty_struct *tty = info->port.tty;
  3969. unsigned char addr_field = 0xff;
  3970. unsigned int crc_size = 0;
  3971. switch (info->params.crc_type & HDLC_CRC_MASK) {
  3972. case HDLC_CRC_16_CCITT: crc_size = 2; break;
  3973. case HDLC_CRC_32_CCITT: crc_size = 4; break;
  3974. }
  3975. check_again:
  3976. framesize = 0;
  3977. addr_field = 0xff;
  3978. start = end = info->rbuf_current;
  3979. for (;;) {
  3980. if (!desc_complete(info->rbufs[end]))
  3981. goto cleanup;
  3982. if (framesize == 0 && info->params.addr_filter != 0xff)
  3983. addr_field = info->rbufs[end].buf[0];
  3984. framesize += desc_count(info->rbufs[end]);
  3985. if (desc_eof(info->rbufs[end]))
  3986. break;
  3987. if (++end == info->rbuf_count)
  3988. end = 0;
  3989. if (end == info->rbuf_current) {
  3990. if (info->rx_enabled){
  3991. spin_lock_irqsave(&info->lock,flags);
  3992. rx_start(info);
  3993. spin_unlock_irqrestore(&info->lock,flags);
  3994. }
  3995. goto cleanup;
  3996. }
  3997. }
  3998. /* status
  3999. *
  4000. * 15 buffer complete
  4001. * 14..06 reserved
  4002. * 05..04 residue
  4003. * 02 eof (end of frame)
  4004. * 01 CRC error
  4005. * 00 abort
  4006. */
  4007. status = desc_status(info->rbufs[end]);
  4008. /* ignore CRC bit if not using CRC (bit is undefined) */
  4009. if ((info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_NONE)
  4010. status &= ~BIT1;
  4011. if (framesize == 0 ||
  4012. (addr_field != 0xff && addr_field != info->params.addr_filter)) {
  4013. free_rbufs(info, start, end);
  4014. goto check_again;
  4015. }
  4016. if (framesize < (2 + crc_size) || status & BIT0) {
  4017. info->icount.rxshort++;
  4018. framesize = 0;
  4019. } else if (status & BIT1) {
  4020. info->icount.rxcrc++;
  4021. if (!(info->params.crc_type & HDLC_CRC_RETURN_EX))
  4022. framesize = 0;
  4023. }
  4024. #if SYNCLINK_GENERIC_HDLC
  4025. if (framesize == 0) {
  4026. info->netdev->stats.rx_errors++;
  4027. info->netdev->stats.rx_frame_errors++;
  4028. }
  4029. #endif
  4030. DBGBH(("%s rx frame status=%04X size=%d\n",
  4031. info->device_name, status, framesize));
  4032. DBGDATA(info, info->rbufs[start].buf, min_t(int, framesize, info->rbuf_fill_level), "rx");
  4033. if (framesize) {
  4034. if (!(info->params.crc_type & HDLC_CRC_RETURN_EX)) {
  4035. framesize -= crc_size;
  4036. crc_size = 0;
  4037. }
  4038. if (framesize > info->max_frame_size + crc_size)
  4039. info->icount.rxlong++;
  4040. else {
  4041. /* copy dma buffer(s) to contiguous temp buffer */
  4042. int copy_count = framesize;
  4043. int i = start;
  4044. unsigned char *p = info->tmp_rbuf;
  4045. info->tmp_rbuf_count = framesize;
  4046. info->icount.rxok++;
  4047. while(copy_count) {
  4048. int partial_count = min_t(int, copy_count, info->rbuf_fill_level);
  4049. memcpy(p, info->rbufs[i].buf, partial_count);
  4050. p += partial_count;
  4051. copy_count -= partial_count;
  4052. if (++i == info->rbuf_count)
  4053. i = 0;
  4054. }
  4055. if (info->params.crc_type & HDLC_CRC_RETURN_EX) {
  4056. *p = (status & BIT1) ? RX_CRC_ERROR : RX_OK;
  4057. framesize++;
  4058. }
  4059. #if SYNCLINK_GENERIC_HDLC
  4060. if (info->netcount)
  4061. hdlcdev_rx(info,info->tmp_rbuf, framesize);
  4062. else
  4063. #endif
  4064. ldisc_receive_buf(tty, info->tmp_rbuf, info->flag_buf, framesize);
  4065. }
  4066. }
  4067. free_rbufs(info, start, end);
  4068. return true;
  4069. cleanup:
  4070. return false;
  4071. }
  4072. /*
  4073. * pass receive buffer (RAW synchronous mode) to tty layer
  4074. * return true if buffer available, otherwise false
  4075. */
  4076. static bool rx_get_buf(struct slgt_info *info)
  4077. {
  4078. unsigned int i = info->rbuf_current;
  4079. unsigned int count;
  4080. if (!desc_complete(info->rbufs[i]))
  4081. return false;
  4082. count = desc_count(info->rbufs[i]);
  4083. switch(info->params.mode) {
  4084. case MGSL_MODE_MONOSYNC:
  4085. case MGSL_MODE_BISYNC:
  4086. case MGSL_MODE_XSYNC:
  4087. /* ignore residue in byte synchronous modes */
  4088. if (desc_residue(info->rbufs[i]))
  4089. count--;
  4090. break;
  4091. }
  4092. DBGDATA(info, info->rbufs[i].buf, count, "rx");
  4093. DBGINFO(("rx_get_buf size=%d\n", count));
  4094. if (count)
  4095. ldisc_receive_buf(info->port.tty, info->rbufs[i].buf,
  4096. info->flag_buf, count);
  4097. free_rbufs(info, i, i);
  4098. return true;
  4099. }
  4100. static void reset_tbufs(struct slgt_info *info)
  4101. {
  4102. unsigned int i;
  4103. info->tbuf_current = 0;
  4104. for (i=0 ; i < info->tbuf_count ; i++) {
  4105. info->tbufs[i].status = 0;
  4106. info->tbufs[i].count = 0;
  4107. }
  4108. }
  4109. /*
  4110. * return number of free transmit DMA buffers
  4111. */
  4112. static unsigned int free_tbuf_count(struct slgt_info *info)
  4113. {
  4114. unsigned int count = 0;
  4115. unsigned int i = info->tbuf_current;
  4116. do
  4117. {
  4118. if (desc_count(info->tbufs[i]))
  4119. break; /* buffer in use */
  4120. ++count;
  4121. if (++i == info->tbuf_count)
  4122. i=0;
  4123. } while (i != info->tbuf_current);
  4124. /* if tx DMA active, last zero count buffer is in use */
  4125. if (count && (rd_reg32(info, TDCSR) & BIT0))
  4126. --count;
  4127. return count;
  4128. }
  4129. /*
  4130. * return number of bytes in unsent transmit DMA buffers
  4131. * and the serial controller tx FIFO
  4132. */
  4133. static unsigned int tbuf_bytes(struct slgt_info *info)
  4134. {
  4135. unsigned int total_count = 0;
  4136. unsigned int i = info->tbuf_current;
  4137. unsigned int reg_value;
  4138. unsigned int count;
  4139. unsigned int active_buf_count = 0;
  4140. /*
  4141. * Add descriptor counts for all tx DMA buffers.
  4142. * If count is zero (cleared by DMA controller after read),
  4143. * the buffer is complete or is actively being read from.
  4144. *
  4145. * Record buf_count of last buffer with zero count starting
  4146. * from current ring position. buf_count is mirror
  4147. * copy of count and is not cleared by serial controller.
  4148. * If DMA controller is active, that buffer is actively
  4149. * being read so add to total.
  4150. */
  4151. do {
  4152. count = desc_count(info->tbufs[i]);
  4153. if (count)
  4154. total_count += count;
  4155. else if (!total_count)
  4156. active_buf_count = info->tbufs[i].buf_count;
  4157. if (++i == info->tbuf_count)
  4158. i = 0;
  4159. } while (i != info->tbuf_current);
  4160. /* read tx DMA status register */
  4161. reg_value = rd_reg32(info, TDCSR);
  4162. /* if tx DMA active, last zero count buffer is in use */
  4163. if (reg_value & BIT0)
  4164. total_count += active_buf_count;
  4165. /* add tx FIFO count = reg_value[15..8] */
  4166. total_count += (reg_value >> 8) & 0xff;
  4167. /* if transmitter active add one byte for shift register */
  4168. if (info->tx_active)
  4169. total_count++;
  4170. return total_count;
  4171. }
  4172. /*
  4173. * load data into transmit DMA buffer ring and start transmitter if needed
  4174. * return true if data accepted, otherwise false (buffers full)
  4175. */
  4176. static bool tx_load(struct slgt_info *info, const char *buf, unsigned int size)
  4177. {
  4178. unsigned short count;
  4179. unsigned int i;
  4180. struct slgt_desc *d;
  4181. /* check required buffer space */
  4182. if (DIV_ROUND_UP(size, DMABUFSIZE) > free_tbuf_count(info))
  4183. return false;
  4184. DBGDATA(info, buf, size, "tx");
  4185. /*
  4186. * copy data to one or more DMA buffers in circular ring
  4187. * tbuf_start = first buffer for this data
  4188. * tbuf_current = next free buffer
  4189. *
  4190. * Copy all data before making data visible to DMA controller by
  4191. * setting descriptor count of the first buffer.
  4192. * This prevents an active DMA controller from reading the first DMA
  4193. * buffers of a frame and stopping before the final buffers are filled.
  4194. */
  4195. info->tbuf_start = i = info->tbuf_current;
  4196. while (size) {
  4197. d = &info->tbufs[i];
  4198. count = (unsigned short)((size > DMABUFSIZE) ? DMABUFSIZE : size);
  4199. memcpy(d->buf, buf, count);
  4200. size -= count;
  4201. buf += count;
  4202. /*
  4203. * set EOF bit for last buffer of HDLC frame or
  4204. * for every buffer in raw mode
  4205. */
  4206. if ((!size && info->params.mode == MGSL_MODE_HDLC) ||
  4207. info->params.mode == MGSL_MODE_RAW)
  4208. set_desc_eof(*d, 1);
  4209. else
  4210. set_desc_eof(*d, 0);
  4211. /* set descriptor count for all but first buffer */
  4212. if (i != info->tbuf_start)
  4213. set_desc_count(*d, count);
  4214. d->buf_count = count;
  4215. if (++i == info->tbuf_count)
  4216. i = 0;
  4217. }
  4218. info->tbuf_current = i;
  4219. /* set first buffer count to make new data visible to DMA controller */
  4220. d = &info->tbufs[info->tbuf_start];
  4221. set_desc_count(*d, d->buf_count);
  4222. /* start transmitter if needed and update transmit timeout */
  4223. if (!info->tx_active)
  4224. tx_start(info);
  4225. update_tx_timer(info);
  4226. return true;
  4227. }
  4228. static int register_test(struct slgt_info *info)
  4229. {
  4230. static unsigned short patterns[] =
  4231. {0x0000, 0xffff, 0xaaaa, 0x5555, 0x6969, 0x9696};
  4232. static unsigned int count = ARRAY_SIZE(patterns);
  4233. unsigned int i;
  4234. int rc = 0;
  4235. for (i=0 ; i < count ; i++) {
  4236. wr_reg16(info, TIR, patterns[i]);
  4237. wr_reg16(info, BDR, patterns[(i+1)%count]);
  4238. if ((rd_reg16(info, TIR) != patterns[i]) ||
  4239. (rd_reg16(info, BDR) != patterns[(i+1)%count])) {
  4240. rc = -ENODEV;
  4241. break;
  4242. }
  4243. }
  4244. info->gpio_present = (rd_reg32(info, JCR) & BIT5) ? 1 : 0;
  4245. info->init_error = rc ? 0 : DiagStatus_AddressFailure;
  4246. return rc;
  4247. }
  4248. static int irq_test(struct slgt_info *info)
  4249. {
  4250. unsigned long timeout;
  4251. unsigned long flags;
  4252. struct tty_struct *oldtty = info->port.tty;
  4253. u32 speed = info->params.data_rate;
  4254. info->params.data_rate = 921600;
  4255. info->port.tty = NULL;
  4256. spin_lock_irqsave(&info->lock, flags);
  4257. async_mode(info);
  4258. slgt_irq_on(info, IRQ_TXIDLE);
  4259. /* enable transmitter */
  4260. wr_reg16(info, TCR,
  4261. (unsigned short)(rd_reg16(info, TCR) | BIT1));
  4262. /* write one byte and wait for tx idle */
  4263. wr_reg16(info, TDR, 0);
  4264. /* assume failure */
  4265. info->init_error = DiagStatus_IrqFailure;
  4266. info->irq_occurred = false;
  4267. spin_unlock_irqrestore(&info->lock, flags);
  4268. timeout=100;
  4269. while(timeout-- && !info->irq_occurred)
  4270. msleep_interruptible(10);
  4271. spin_lock_irqsave(&info->lock,flags);
  4272. reset_port(info);
  4273. spin_unlock_irqrestore(&info->lock,flags);
  4274. info->params.data_rate = speed;
  4275. info->port.tty = oldtty;
  4276. info->init_error = info->irq_occurred ? 0 : DiagStatus_IrqFailure;
  4277. return info->irq_occurred ? 0 : -ENODEV;
  4278. }
  4279. static int loopback_test_rx(struct slgt_info *info)
  4280. {
  4281. unsigned char *src, *dest;
  4282. int count;
  4283. if (desc_complete(info->rbufs[0])) {
  4284. count = desc_count(info->rbufs[0]);
  4285. src = info->rbufs[0].buf;
  4286. dest = info->tmp_rbuf;
  4287. for( ; count ; count-=2, src+=2) {
  4288. /* src=data byte (src+1)=status byte */
  4289. if (!(*(src+1) & (BIT9 + BIT8))) {
  4290. *dest = *src;
  4291. dest++;
  4292. info->tmp_rbuf_count++;
  4293. }
  4294. }
  4295. DBGDATA(info, info->tmp_rbuf, info->tmp_rbuf_count, "rx");
  4296. return 1;
  4297. }
  4298. return 0;
  4299. }
  4300. static int loopback_test(struct slgt_info *info)
  4301. {
  4302. #define TESTFRAMESIZE 20
  4303. unsigned long timeout;
  4304. u16 count = TESTFRAMESIZE;
  4305. unsigned char buf[TESTFRAMESIZE];
  4306. int rc = -ENODEV;
  4307. unsigned long flags;
  4308. struct tty_struct *oldtty = info->port.tty;
  4309. MGSL_PARAMS params;
  4310. memcpy(&params, &info->params, sizeof(params));
  4311. info->params.mode = MGSL_MODE_ASYNC;
  4312. info->params.data_rate = 921600;
  4313. info->params.loopback = 1;
  4314. info->port.tty = NULL;
  4315. /* build and send transmit frame */
  4316. for (count = 0; count < TESTFRAMESIZE; ++count)
  4317. buf[count] = (unsigned char)count;
  4318. info->tmp_rbuf_count = 0;
  4319. memset(info->tmp_rbuf, 0, TESTFRAMESIZE);
  4320. /* program hardware for HDLC and enabled receiver */
  4321. spin_lock_irqsave(&info->lock,flags);
  4322. async_mode(info);
  4323. rx_start(info);
  4324. tx_load(info, buf, count);
  4325. spin_unlock_irqrestore(&info->lock, flags);
  4326. /* wait for receive complete */
  4327. for (timeout = 100; timeout; --timeout) {
  4328. msleep_interruptible(10);
  4329. if (loopback_test_rx(info)) {
  4330. rc = 0;
  4331. break;
  4332. }
  4333. }
  4334. /* verify received frame length and contents */
  4335. if (!rc && (info->tmp_rbuf_count != count ||
  4336. memcmp(buf, info->tmp_rbuf, count))) {
  4337. rc = -ENODEV;
  4338. }
  4339. spin_lock_irqsave(&info->lock,flags);
  4340. reset_adapter(info);
  4341. spin_unlock_irqrestore(&info->lock,flags);
  4342. memcpy(&info->params, &params, sizeof(info->params));
  4343. info->port.tty = oldtty;
  4344. info->init_error = rc ? DiagStatus_DmaFailure : 0;
  4345. return rc;
  4346. }
  4347. static int adapter_test(struct slgt_info *info)
  4348. {
  4349. DBGINFO(("testing %s\n", info->device_name));
  4350. if (register_test(info) < 0) {
  4351. printk("register test failure %s addr=%08X\n",
  4352. info->device_name, info->phys_reg_addr);
  4353. } else if (irq_test(info) < 0) {
  4354. printk("IRQ test failure %s IRQ=%d\n",
  4355. info->device_name, info->irq_level);
  4356. } else if (loopback_test(info) < 0) {
  4357. printk("loopback test failure %s\n", info->device_name);
  4358. }
  4359. return info->init_error;
  4360. }
  4361. /*
  4362. * transmit timeout handler
  4363. */
  4364. static void tx_timeout(unsigned long context)
  4365. {
  4366. struct slgt_info *info = (struct slgt_info*)context;
  4367. unsigned long flags;
  4368. DBGINFO(("%s tx_timeout\n", info->device_name));
  4369. if(info->tx_active && info->params.mode == MGSL_MODE_HDLC) {
  4370. info->icount.txtimeout++;
  4371. }
  4372. spin_lock_irqsave(&info->lock,flags);
  4373. tx_stop(info);
  4374. spin_unlock_irqrestore(&info->lock,flags);
  4375. #if SYNCLINK_GENERIC_HDLC
  4376. if (info->netcount)
  4377. hdlcdev_tx_done(info);
  4378. else
  4379. #endif
  4380. bh_transmit(info);
  4381. }
  4382. /*
  4383. * receive buffer polling timer
  4384. */
  4385. static void rx_timeout(unsigned long context)
  4386. {
  4387. struct slgt_info *info = (struct slgt_info*)context;
  4388. unsigned long flags;
  4389. DBGINFO(("%s rx_timeout\n", info->device_name));
  4390. spin_lock_irqsave(&info->lock, flags);
  4391. info->pending_bh |= BH_RECEIVE;
  4392. spin_unlock_irqrestore(&info->lock, flags);
  4393. bh_handler(&info->task);
  4394. }