synclink_gt.c 125 KB

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