synclink_gt.c 119 KB

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