synclink_gt.c 125 KB

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