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