synclink_gt.c 132 KB

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