synclink_gt.c 132 KB

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