synclink_gt.c 132 KB

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