synclink_gt.c 125 KB

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