synclink_gt.c 129 KB

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