synclinkmp.c 149 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639
  1. /*
  2. * $Id: synclinkmp.c,v 4.38 2005/07/15 13:29:44 paulkf Exp $
  3. *
  4. * Device driver for Microgate SyncLink Multiport
  5. * high speed multiprotocol serial adapter.
  6. *
  7. * written by Paul Fulghum for Microgate Corporation
  8. * paulkf@microgate.com
  9. *
  10. * Microgate and SyncLink are trademarks of Microgate Corporation
  11. *
  12. * Derived from serial.c written by Theodore Ts'o and Linus Torvalds
  13. * This code is released under the GNU General Public License (GPL)
  14. *
  15. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  16. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  19. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  25. * OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #define VERSION(ver,rel,seq) (((ver)<<16) | ((rel)<<8) | (seq))
  28. #if defined(__i386__)
  29. # define BREAKPOINT() asm(" int $3");
  30. #else
  31. # define BREAKPOINT() { }
  32. #endif
  33. #define MAX_DEVICES 12
  34. #include <linux/module.h>
  35. #include <linux/errno.h>
  36. #include <linux/signal.h>
  37. #include <linux/sched.h>
  38. #include <linux/timer.h>
  39. #include <linux/interrupt.h>
  40. #include <linux/pci.h>
  41. #include <linux/tty.h>
  42. #include <linux/tty_flip.h>
  43. #include <linux/serial.h>
  44. #include <linux/major.h>
  45. #include <linux/string.h>
  46. #include <linux/fcntl.h>
  47. #include <linux/ptrace.h>
  48. #include <linux/ioport.h>
  49. #include <linux/mm.h>
  50. #include <linux/slab.h>
  51. #include <linux/netdevice.h>
  52. #include <linux/vmalloc.h>
  53. #include <linux/init.h>
  54. #include <linux/delay.h>
  55. #include <linux/ioctl.h>
  56. #include <asm/system.h>
  57. #include <asm/io.h>
  58. #include <asm/irq.h>
  59. #include <asm/dma.h>
  60. #include <linux/bitops.h>
  61. #include <asm/types.h>
  62. #include <linux/termios.h>
  63. #include <linux/workqueue.h>
  64. #include <linux/hdlc.h>
  65. #include <linux/synclink.h>
  66. #if defined(CONFIG_HDLC) || (defined(CONFIG_HDLC_MODULE) && defined(CONFIG_SYNCLINKMP_MODULE))
  67. #define SYNCLINK_GENERIC_HDLC 1
  68. #else
  69. #define SYNCLINK_GENERIC_HDLC 0
  70. #endif
  71. #define GET_USER(error,value,addr) error = get_user(value,addr)
  72. #define COPY_FROM_USER(error,dest,src,size) error = copy_from_user(dest,src,size) ? -EFAULT : 0
  73. #define PUT_USER(error,value,addr) error = put_user(value,addr)
  74. #define COPY_TO_USER(error,dest,src,size) error = copy_to_user(dest,src,size) ? -EFAULT : 0
  75. #include <asm/uaccess.h>
  76. static MGSL_PARAMS default_params = {
  77. MGSL_MODE_HDLC, /* unsigned long mode */
  78. 0, /* unsigned char loopback; */
  79. HDLC_FLAG_UNDERRUN_ABORT15, /* unsigned short flags; */
  80. HDLC_ENCODING_NRZI_SPACE, /* unsigned char encoding; */
  81. 0, /* unsigned long clock_speed; */
  82. 0xff, /* unsigned char addr_filter; */
  83. HDLC_CRC_16_CCITT, /* unsigned short crc_type; */
  84. HDLC_PREAMBLE_LENGTH_8BITS, /* unsigned char preamble_length; */
  85. HDLC_PREAMBLE_PATTERN_NONE, /* unsigned char preamble; */
  86. 9600, /* unsigned long data_rate; */
  87. 8, /* unsigned char data_bits; */
  88. 1, /* unsigned char stop_bits; */
  89. ASYNC_PARITY_NONE /* unsigned char parity; */
  90. };
  91. /* size in bytes of DMA data buffers */
  92. #define SCABUFSIZE 1024
  93. #define SCA_MEM_SIZE 0x40000
  94. #define SCA_BASE_SIZE 512
  95. #define SCA_REG_SIZE 16
  96. #define SCA_MAX_PORTS 4
  97. #define SCAMAXDESC 128
  98. #define BUFFERLISTSIZE 4096
  99. /* SCA-I style DMA buffer descriptor */
  100. typedef struct _SCADESC
  101. {
  102. u16 next; /* lower l6 bits of next descriptor addr */
  103. u16 buf_ptr; /* lower 16 bits of buffer addr */
  104. u8 buf_base; /* upper 8 bits of buffer addr */
  105. u8 pad1;
  106. u16 length; /* length of buffer */
  107. u8 status; /* status of buffer */
  108. u8 pad2;
  109. } SCADESC, *PSCADESC;
  110. typedef struct _SCADESC_EX
  111. {
  112. /* device driver bookkeeping section */
  113. char *virt_addr; /* virtual address of data buffer */
  114. u16 phys_entry; /* lower 16-bits of physical address of this descriptor */
  115. } SCADESC_EX, *PSCADESC_EX;
  116. /* The queue of BH actions to be performed */
  117. #define BH_RECEIVE 1
  118. #define BH_TRANSMIT 2
  119. #define BH_STATUS 4
  120. #define IO_PIN_SHUTDOWN_LIMIT 100
  121. struct _input_signal_events {
  122. int ri_up;
  123. int ri_down;
  124. int dsr_up;
  125. int dsr_down;
  126. int dcd_up;
  127. int dcd_down;
  128. int cts_up;
  129. int cts_down;
  130. };
  131. /*
  132. * Device instance data structure
  133. */
  134. typedef struct _synclinkmp_info {
  135. void *if_ptr; /* General purpose pointer (used by SPPP) */
  136. int magic;
  137. struct tty_port port;
  138. int line;
  139. unsigned short close_delay;
  140. unsigned short closing_wait; /* time to wait before closing */
  141. struct mgsl_icount icount;
  142. int timeout;
  143. int x_char; /* xon/xoff character */
  144. u16 read_status_mask1; /* break detection (SR1 indications) */
  145. u16 read_status_mask2; /* parity/framing/overun (SR2 indications) */
  146. unsigned char ignore_status_mask1; /* break detection (SR1 indications) */
  147. unsigned char ignore_status_mask2; /* parity/framing/overun (SR2 indications) */
  148. unsigned char *tx_buf;
  149. int tx_put;
  150. int tx_get;
  151. int tx_count;
  152. wait_queue_head_t status_event_wait_q;
  153. wait_queue_head_t event_wait_q;
  154. struct timer_list tx_timer; /* HDLC transmit timeout timer */
  155. struct _synclinkmp_info *next_device; /* device list link */
  156. struct timer_list status_timer; /* input signal status check timer */
  157. spinlock_t lock; /* spinlock for synchronizing with ISR */
  158. struct work_struct task; /* task structure for scheduling bh */
  159. u32 max_frame_size; /* as set by device config */
  160. u32 pending_bh;
  161. bool bh_running; /* Protection from multiple */
  162. int isr_overflow;
  163. bool bh_requested;
  164. int dcd_chkcount; /* check counts to prevent */
  165. int cts_chkcount; /* too many IRQs if a signal */
  166. int dsr_chkcount; /* is floating */
  167. int ri_chkcount;
  168. char *buffer_list; /* virtual address of Rx & Tx buffer lists */
  169. unsigned long buffer_list_phys;
  170. unsigned int rx_buf_count; /* count of total allocated Rx buffers */
  171. SCADESC *rx_buf_list; /* list of receive buffer entries */
  172. SCADESC_EX rx_buf_list_ex[SCAMAXDESC]; /* list of receive buffer entries */
  173. unsigned int current_rx_buf;
  174. unsigned int tx_buf_count; /* count of total allocated Tx buffers */
  175. SCADESC *tx_buf_list; /* list of transmit buffer entries */
  176. SCADESC_EX tx_buf_list_ex[SCAMAXDESC]; /* list of transmit buffer entries */
  177. unsigned int last_tx_buf;
  178. unsigned char *tmp_rx_buf;
  179. unsigned int tmp_rx_buf_count;
  180. bool rx_enabled;
  181. bool rx_overflow;
  182. bool tx_enabled;
  183. bool tx_active;
  184. u32 idle_mode;
  185. unsigned char ie0_value;
  186. unsigned char ie1_value;
  187. unsigned char ie2_value;
  188. unsigned char ctrlreg_value;
  189. unsigned char old_signals;
  190. char device_name[25]; /* device instance name */
  191. int port_count;
  192. int adapter_num;
  193. int port_num;
  194. struct _synclinkmp_info *port_array[SCA_MAX_PORTS];
  195. unsigned int bus_type; /* expansion bus type (ISA,EISA,PCI) */
  196. unsigned int irq_level; /* interrupt level */
  197. unsigned long irq_flags;
  198. bool irq_requested; /* true if IRQ requested */
  199. MGSL_PARAMS params; /* communications parameters */
  200. unsigned char serial_signals; /* current serial signal states */
  201. bool irq_occurred; /* for diagnostics use */
  202. unsigned int init_error; /* Initialization startup error */
  203. u32 last_mem_alloc;
  204. unsigned char* memory_base; /* shared memory address (PCI only) */
  205. u32 phys_memory_base;
  206. int shared_mem_requested;
  207. unsigned char* sca_base; /* HD64570 SCA Memory address */
  208. u32 phys_sca_base;
  209. u32 sca_offset;
  210. bool sca_base_requested;
  211. unsigned char* lcr_base; /* local config registers (PCI only) */
  212. u32 phys_lcr_base;
  213. u32 lcr_offset;
  214. int lcr_mem_requested;
  215. unsigned char* statctrl_base; /* status/control register memory */
  216. u32 phys_statctrl_base;
  217. u32 statctrl_offset;
  218. bool sca_statctrl_requested;
  219. u32 misc_ctrl_value;
  220. char flag_buf[MAX_ASYNC_BUFFER_SIZE];
  221. char char_buf[MAX_ASYNC_BUFFER_SIZE];
  222. bool drop_rts_on_tx_done;
  223. struct _input_signal_events input_signal_events;
  224. /* SPPP/Cisco HDLC device parts */
  225. int netcount;
  226. int dosyncppp;
  227. spinlock_t netlock;
  228. #if SYNCLINK_GENERIC_HDLC
  229. struct net_device *netdev;
  230. #endif
  231. } SLMP_INFO;
  232. #define MGSL_MAGIC 0x5401
  233. /*
  234. * define serial signal status change macros
  235. */
  236. #define MISCSTATUS_DCD_LATCHED (SerialSignal_DCD<<8) /* indicates change in DCD */
  237. #define MISCSTATUS_RI_LATCHED (SerialSignal_RI<<8) /* indicates change in RI */
  238. #define MISCSTATUS_CTS_LATCHED (SerialSignal_CTS<<8) /* indicates change in CTS */
  239. #define MISCSTATUS_DSR_LATCHED (SerialSignal_DSR<<8) /* change in DSR */
  240. /* Common Register macros */
  241. #define LPR 0x00
  242. #define PABR0 0x02
  243. #define PABR1 0x03
  244. #define WCRL 0x04
  245. #define WCRM 0x05
  246. #define WCRH 0x06
  247. #define DPCR 0x08
  248. #define DMER 0x09
  249. #define ISR0 0x10
  250. #define ISR1 0x11
  251. #define ISR2 0x12
  252. #define IER0 0x14
  253. #define IER1 0x15
  254. #define IER2 0x16
  255. #define ITCR 0x18
  256. #define INTVR 0x1a
  257. #define IMVR 0x1c
  258. /* MSCI Register macros */
  259. #define TRB 0x20
  260. #define TRBL 0x20
  261. #define TRBH 0x21
  262. #define SR0 0x22
  263. #define SR1 0x23
  264. #define SR2 0x24
  265. #define SR3 0x25
  266. #define FST 0x26
  267. #define IE0 0x28
  268. #define IE1 0x29
  269. #define IE2 0x2a
  270. #define FIE 0x2b
  271. #define CMD 0x2c
  272. #define MD0 0x2e
  273. #define MD1 0x2f
  274. #define MD2 0x30
  275. #define CTL 0x31
  276. #define SA0 0x32
  277. #define SA1 0x33
  278. #define IDL 0x34
  279. #define TMC 0x35
  280. #define RXS 0x36
  281. #define TXS 0x37
  282. #define TRC0 0x38
  283. #define TRC1 0x39
  284. #define RRC 0x3a
  285. #define CST0 0x3c
  286. #define CST1 0x3d
  287. /* Timer Register Macros */
  288. #define TCNT 0x60
  289. #define TCNTL 0x60
  290. #define TCNTH 0x61
  291. #define TCONR 0x62
  292. #define TCONRL 0x62
  293. #define TCONRH 0x63
  294. #define TMCS 0x64
  295. #define TEPR 0x65
  296. /* DMA Controller Register macros */
  297. #define DARL 0x80
  298. #define DARH 0x81
  299. #define DARB 0x82
  300. #define BAR 0x80
  301. #define BARL 0x80
  302. #define BARH 0x81
  303. #define BARB 0x82
  304. #define SAR 0x84
  305. #define SARL 0x84
  306. #define SARH 0x85
  307. #define SARB 0x86
  308. #define CPB 0x86
  309. #define CDA 0x88
  310. #define CDAL 0x88
  311. #define CDAH 0x89
  312. #define EDA 0x8a
  313. #define EDAL 0x8a
  314. #define EDAH 0x8b
  315. #define BFL 0x8c
  316. #define BFLL 0x8c
  317. #define BFLH 0x8d
  318. #define BCR 0x8e
  319. #define BCRL 0x8e
  320. #define BCRH 0x8f
  321. #define DSR 0x90
  322. #define DMR 0x91
  323. #define FCT 0x93
  324. #define DIR 0x94
  325. #define DCMD 0x95
  326. /* combine with timer or DMA register address */
  327. #define TIMER0 0x00
  328. #define TIMER1 0x08
  329. #define TIMER2 0x10
  330. #define TIMER3 0x18
  331. #define RXDMA 0x00
  332. #define TXDMA 0x20
  333. /* SCA Command Codes */
  334. #define NOOP 0x00
  335. #define TXRESET 0x01
  336. #define TXENABLE 0x02
  337. #define TXDISABLE 0x03
  338. #define TXCRCINIT 0x04
  339. #define TXCRCEXCL 0x05
  340. #define TXEOM 0x06
  341. #define TXABORT 0x07
  342. #define MPON 0x08
  343. #define TXBUFCLR 0x09
  344. #define RXRESET 0x11
  345. #define RXENABLE 0x12
  346. #define RXDISABLE 0x13
  347. #define RXCRCINIT 0x14
  348. #define RXREJECT 0x15
  349. #define SEARCHMP 0x16
  350. #define RXCRCEXCL 0x17
  351. #define RXCRCCALC 0x18
  352. #define CHRESET 0x21
  353. #define HUNT 0x31
  354. /* DMA command codes */
  355. #define SWABORT 0x01
  356. #define FEICLEAR 0x02
  357. /* IE0 */
  358. #define TXINTE BIT7
  359. #define RXINTE BIT6
  360. #define TXRDYE BIT1
  361. #define RXRDYE BIT0
  362. /* IE1 & SR1 */
  363. #define UDRN BIT7
  364. #define IDLE BIT6
  365. #define SYNCD BIT4
  366. #define FLGD BIT4
  367. #define CCTS BIT3
  368. #define CDCD BIT2
  369. #define BRKD BIT1
  370. #define ABTD BIT1
  371. #define GAPD BIT1
  372. #define BRKE BIT0
  373. #define IDLD BIT0
  374. /* IE2 & SR2 */
  375. #define EOM BIT7
  376. #define PMP BIT6
  377. #define SHRT BIT6
  378. #define PE BIT5
  379. #define ABT BIT5
  380. #define FRME BIT4
  381. #define RBIT BIT4
  382. #define OVRN BIT3
  383. #define CRCE BIT2
  384. /*
  385. * Global linked list of SyncLink devices
  386. */
  387. static SLMP_INFO *synclinkmp_device_list = NULL;
  388. static int synclinkmp_adapter_count = -1;
  389. static int synclinkmp_device_count = 0;
  390. /*
  391. * Set this param to non-zero to load eax with the
  392. * .text section address and breakpoint on module load.
  393. * This is useful for use with gdb and add-symbol-file command.
  394. */
  395. static int break_on_load = 0;
  396. /*
  397. * Driver major number, defaults to zero to get auto
  398. * assigned major number. May be forced as module parameter.
  399. */
  400. static int ttymajor = 0;
  401. /*
  402. * Array of user specified options for ISA adapters.
  403. */
  404. static int debug_level = 0;
  405. static int maxframe[MAX_DEVICES] = {0,};
  406. static int dosyncppp[MAX_DEVICES] = {0,};
  407. module_param(break_on_load, bool, 0);
  408. module_param(ttymajor, int, 0);
  409. module_param(debug_level, int, 0);
  410. module_param_array(maxframe, int, NULL, 0);
  411. module_param_array(dosyncppp, int, NULL, 0);
  412. static char *driver_name = "SyncLink MultiPort driver";
  413. static char *driver_version = "$Revision: 4.38 $";
  414. static int synclinkmp_init_one(struct pci_dev *dev,const struct pci_device_id *ent);
  415. static void synclinkmp_remove_one(struct pci_dev *dev);
  416. static struct pci_device_id synclinkmp_pci_tbl[] = {
  417. { PCI_VENDOR_ID_MICROGATE, PCI_DEVICE_ID_MICROGATE_SCA, PCI_ANY_ID, PCI_ANY_ID, },
  418. { 0, }, /* terminate list */
  419. };
  420. MODULE_DEVICE_TABLE(pci, synclinkmp_pci_tbl);
  421. MODULE_LICENSE("GPL");
  422. static struct pci_driver synclinkmp_pci_driver = {
  423. .name = "synclinkmp",
  424. .id_table = synclinkmp_pci_tbl,
  425. .probe = synclinkmp_init_one,
  426. .remove = __devexit_p(synclinkmp_remove_one),
  427. };
  428. static struct tty_driver *serial_driver;
  429. /* number of characters left in xmit buffer before we ask for more */
  430. #define WAKEUP_CHARS 256
  431. /* tty callbacks */
  432. static int open(struct tty_struct *tty, struct file * filp);
  433. static void close(struct tty_struct *tty, struct file * filp);
  434. static void hangup(struct tty_struct *tty);
  435. static void set_termios(struct tty_struct *tty, struct ktermios *old_termios);
  436. static int write(struct tty_struct *tty, const unsigned char *buf, int count);
  437. static int put_char(struct tty_struct *tty, unsigned char ch);
  438. static void send_xchar(struct tty_struct *tty, char ch);
  439. static void wait_until_sent(struct tty_struct *tty, int timeout);
  440. static int write_room(struct tty_struct *tty);
  441. static void flush_chars(struct tty_struct *tty);
  442. static void flush_buffer(struct tty_struct *tty);
  443. static void tx_hold(struct tty_struct *tty);
  444. static void tx_release(struct tty_struct *tty);
  445. static int ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
  446. static int read_proc(char *page, char **start, off_t off, int count,int *eof, void *data);
  447. static int chars_in_buffer(struct tty_struct *tty);
  448. static void throttle(struct tty_struct * tty);
  449. static void unthrottle(struct tty_struct * tty);
  450. static void set_break(struct tty_struct *tty, int break_state);
  451. #if SYNCLINK_GENERIC_HDLC
  452. #define dev_to_port(D) (dev_to_hdlc(D)->priv)
  453. static void hdlcdev_tx_done(SLMP_INFO *info);
  454. static void hdlcdev_rx(SLMP_INFO *info, char *buf, int size);
  455. static int hdlcdev_init(SLMP_INFO *info);
  456. static void hdlcdev_exit(SLMP_INFO *info);
  457. #endif
  458. /* ioctl handlers */
  459. static int get_stats(SLMP_INFO *info, struct mgsl_icount __user *user_icount);
  460. static int get_params(SLMP_INFO *info, MGSL_PARAMS __user *params);
  461. static int set_params(SLMP_INFO *info, MGSL_PARAMS __user *params);
  462. static int get_txidle(SLMP_INFO *info, int __user *idle_mode);
  463. static int set_txidle(SLMP_INFO *info, int idle_mode);
  464. static int tx_enable(SLMP_INFO *info, int enable);
  465. static int tx_abort(SLMP_INFO *info);
  466. static int rx_enable(SLMP_INFO *info, int enable);
  467. static int modem_input_wait(SLMP_INFO *info,int arg);
  468. static int wait_mgsl_event(SLMP_INFO *info, int __user *mask_ptr);
  469. static int tiocmget(struct tty_struct *tty, struct file *file);
  470. static int tiocmset(struct tty_struct *tty, struct file *file,
  471. unsigned int set, unsigned int clear);
  472. static void set_break(struct tty_struct *tty, int break_state);
  473. static void add_device(SLMP_INFO *info);
  474. static void device_init(int adapter_num, struct pci_dev *pdev);
  475. static int claim_resources(SLMP_INFO *info);
  476. static void release_resources(SLMP_INFO *info);
  477. static int startup(SLMP_INFO *info);
  478. static int block_til_ready(struct tty_struct *tty, struct file * filp,SLMP_INFO *info);
  479. static void shutdown(SLMP_INFO *info);
  480. static void program_hw(SLMP_INFO *info);
  481. static void change_params(SLMP_INFO *info);
  482. static bool init_adapter(SLMP_INFO *info);
  483. static bool register_test(SLMP_INFO *info);
  484. static bool irq_test(SLMP_INFO *info);
  485. static bool loopback_test(SLMP_INFO *info);
  486. static int adapter_test(SLMP_INFO *info);
  487. static bool memory_test(SLMP_INFO *info);
  488. static void reset_adapter(SLMP_INFO *info);
  489. static void reset_port(SLMP_INFO *info);
  490. static void async_mode(SLMP_INFO *info);
  491. static void hdlc_mode(SLMP_INFO *info);
  492. static void rx_stop(SLMP_INFO *info);
  493. static void rx_start(SLMP_INFO *info);
  494. static void rx_reset_buffers(SLMP_INFO *info);
  495. static void rx_free_frame_buffers(SLMP_INFO *info, unsigned int first, unsigned int last);
  496. static bool rx_get_frame(SLMP_INFO *info);
  497. static void tx_start(SLMP_INFO *info);
  498. static void tx_stop(SLMP_INFO *info);
  499. static void tx_load_fifo(SLMP_INFO *info);
  500. static void tx_set_idle(SLMP_INFO *info);
  501. static void tx_load_dma_buffer(SLMP_INFO *info, const char *buf, unsigned int count);
  502. static void get_signals(SLMP_INFO *info);
  503. static void set_signals(SLMP_INFO *info);
  504. static void enable_loopback(SLMP_INFO *info, int enable);
  505. static void set_rate(SLMP_INFO *info, u32 data_rate);
  506. static int bh_action(SLMP_INFO *info);
  507. static void bh_handler(struct work_struct *work);
  508. static void bh_receive(SLMP_INFO *info);
  509. static void bh_transmit(SLMP_INFO *info);
  510. static void bh_status(SLMP_INFO *info);
  511. static void isr_timer(SLMP_INFO *info);
  512. static void isr_rxint(SLMP_INFO *info);
  513. static void isr_rxrdy(SLMP_INFO *info);
  514. static void isr_txint(SLMP_INFO *info);
  515. static void isr_txrdy(SLMP_INFO *info);
  516. static void isr_rxdmaok(SLMP_INFO *info);
  517. static void isr_rxdmaerror(SLMP_INFO *info);
  518. static void isr_txdmaok(SLMP_INFO *info);
  519. static void isr_txdmaerror(SLMP_INFO *info);
  520. static void isr_io_pin(SLMP_INFO *info, u16 status);
  521. static int alloc_dma_bufs(SLMP_INFO *info);
  522. static void free_dma_bufs(SLMP_INFO *info);
  523. static int alloc_buf_list(SLMP_INFO *info);
  524. static int alloc_frame_bufs(SLMP_INFO *info, SCADESC *list, SCADESC_EX *list_ex,int count);
  525. static int alloc_tmp_rx_buf(SLMP_INFO *info);
  526. static void free_tmp_rx_buf(SLMP_INFO *info);
  527. static void load_pci_memory(SLMP_INFO *info, char* dest, const char* src, unsigned short count);
  528. static void trace_block(SLMP_INFO *info, const char* data, int count, int xmit);
  529. static void tx_timeout(unsigned long context);
  530. static void status_timeout(unsigned long context);
  531. static unsigned char read_reg(SLMP_INFO *info, unsigned char addr);
  532. static void write_reg(SLMP_INFO *info, unsigned char addr, unsigned char val);
  533. static u16 read_reg16(SLMP_INFO *info, unsigned char addr);
  534. static void write_reg16(SLMP_INFO *info, unsigned char addr, u16 val);
  535. static unsigned char read_status_reg(SLMP_INFO * info);
  536. static void write_control_reg(SLMP_INFO * info);
  537. static unsigned char rx_active_fifo_level = 16; // rx request FIFO activation level in bytes
  538. static unsigned char tx_active_fifo_level = 16; // tx request FIFO activation level in bytes
  539. static unsigned char tx_negate_fifo_level = 32; // tx request FIFO negation level in bytes
  540. static u32 misc_ctrl_value = 0x007e4040;
  541. static u32 lcr1_brdr_value = 0x00800028;
  542. static u32 read_ahead_count = 8;
  543. /* DPCR, DMA Priority Control
  544. *
  545. * 07..05 Not used, must be 0
  546. * 04 BRC, bus release condition: 0=all transfers complete
  547. * 1=release after 1 xfer on all channels
  548. * 03 CCC, channel change condition: 0=every cycle
  549. * 1=after each channel completes all xfers
  550. * 02..00 PR<2..0>, priority 100=round robin
  551. *
  552. * 00000100 = 0x00
  553. */
  554. static unsigned char dma_priority = 0x04;
  555. // Number of bytes that can be written to shared RAM
  556. // in a single write operation
  557. static u32 sca_pci_load_interval = 64;
  558. /*
  559. * 1st function defined in .text section. Calling this function in
  560. * init_module() followed by a breakpoint allows a remote debugger
  561. * (gdb) to get the .text address for the add-symbol-file command.
  562. * This allows remote debugging of dynamically loadable modules.
  563. */
  564. static void* synclinkmp_get_text_ptr(void);
  565. static void* synclinkmp_get_text_ptr(void) {return synclinkmp_get_text_ptr;}
  566. static inline int sanity_check(SLMP_INFO *info,
  567. char *name, const char *routine)
  568. {
  569. #ifdef SANITY_CHECK
  570. static const char *badmagic =
  571. "Warning: bad magic number for synclinkmp_struct (%s) in %s\n";
  572. static const char *badinfo =
  573. "Warning: null synclinkmp_struct for (%s) in %s\n";
  574. if (!info) {
  575. printk(badinfo, name, routine);
  576. return 1;
  577. }
  578. if (info->magic != MGSL_MAGIC) {
  579. printk(badmagic, name, routine);
  580. return 1;
  581. }
  582. #else
  583. if (!info)
  584. return 1;
  585. #endif
  586. return 0;
  587. }
  588. /**
  589. * line discipline callback wrappers
  590. *
  591. * The wrappers maintain line discipline references
  592. * while calling into the line discipline.
  593. *
  594. * ldisc_receive_buf - pass receive data to line discipline
  595. */
  596. static void ldisc_receive_buf(struct tty_struct *tty,
  597. const __u8 *data, char *flags, int count)
  598. {
  599. struct tty_ldisc *ld;
  600. if (!tty)
  601. return;
  602. ld = tty_ldisc_ref(tty);
  603. if (ld) {
  604. if (ld->ops->receive_buf)
  605. ld->ops->receive_buf(tty, data, flags, count);
  606. tty_ldisc_deref(ld);
  607. }
  608. }
  609. /* tty callbacks */
  610. /* Called when a port is opened. Init and enable port.
  611. */
  612. static int open(struct tty_struct *tty, struct file *filp)
  613. {
  614. SLMP_INFO *info;
  615. int retval, line;
  616. unsigned long flags;
  617. line = tty->index;
  618. if ((line < 0) || (line >= synclinkmp_device_count)) {
  619. printk("%s(%d): open with invalid line #%d.\n",
  620. __FILE__,__LINE__,line);
  621. return -ENODEV;
  622. }
  623. info = synclinkmp_device_list;
  624. while(info && info->line != line)
  625. info = info->next_device;
  626. if (sanity_check(info, tty->name, "open"))
  627. return -ENODEV;
  628. if ( info->init_error ) {
  629. printk("%s(%d):%s device is not allocated, init error=%d\n",
  630. __FILE__,__LINE__,info->device_name,info->init_error);
  631. return -ENODEV;
  632. }
  633. tty->driver_data = info;
  634. info->port.tty = tty;
  635. if (debug_level >= DEBUG_LEVEL_INFO)
  636. printk("%s(%d):%s open(), old ref count = %d\n",
  637. __FILE__,__LINE__,tty->driver->name, info->port.count);
  638. /* If port is closing, signal caller to try again */
  639. if (tty_hung_up_p(filp) || info->port.flags & ASYNC_CLOSING){
  640. if (info->port.flags & ASYNC_CLOSING)
  641. interruptible_sleep_on(&info->port.close_wait);
  642. retval = ((info->port.flags & ASYNC_HUP_NOTIFY) ?
  643. -EAGAIN : -ERESTARTSYS);
  644. goto cleanup;
  645. }
  646. info->port.tty->low_latency = (info->port.flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  647. spin_lock_irqsave(&info->netlock, flags);
  648. if (info->netcount) {
  649. retval = -EBUSY;
  650. spin_unlock_irqrestore(&info->netlock, flags);
  651. goto cleanup;
  652. }
  653. info->port.count++;
  654. spin_unlock_irqrestore(&info->netlock, flags);
  655. if (info->port.count == 1) {
  656. /* 1st open on this device, init hardware */
  657. retval = startup(info);
  658. if (retval < 0)
  659. goto cleanup;
  660. }
  661. retval = block_til_ready(tty, filp, info);
  662. if (retval) {
  663. if (debug_level >= DEBUG_LEVEL_INFO)
  664. printk("%s(%d):%s block_til_ready() returned %d\n",
  665. __FILE__,__LINE__, info->device_name, retval);
  666. goto cleanup;
  667. }
  668. if (debug_level >= DEBUG_LEVEL_INFO)
  669. printk("%s(%d):%s open() success\n",
  670. __FILE__,__LINE__, info->device_name);
  671. retval = 0;
  672. cleanup:
  673. if (retval) {
  674. if (tty->count == 1)
  675. info->port.tty = NULL; /* tty layer will release tty struct */
  676. if(info->port.count)
  677. info->port.count--;
  678. }
  679. return retval;
  680. }
  681. /* Called when port is closed. Wait for remaining data to be
  682. * sent. Disable port and free resources.
  683. */
  684. static void close(struct tty_struct *tty, struct file *filp)
  685. {
  686. SLMP_INFO * info = (SLMP_INFO *)tty->driver_data;
  687. if (sanity_check(info, tty->name, "close"))
  688. return;
  689. if (debug_level >= DEBUG_LEVEL_INFO)
  690. printk("%s(%d):%s close() entry, count=%d\n",
  691. __FILE__,__LINE__, info->device_name, info->port.count);
  692. if (!info->port.count)
  693. return;
  694. if (tty_hung_up_p(filp))
  695. goto cleanup;
  696. if ((tty->count == 1) && (info->port.count != 1)) {
  697. /*
  698. * tty->count is 1 and the tty structure will be freed.
  699. * info->port.count should be one in this case.
  700. * if it's not, correct it so that the port is shutdown.
  701. */
  702. printk("%s(%d):%s close: bad refcount; tty->count is 1, "
  703. "info->port.count is %d\n",
  704. __FILE__,__LINE__, info->device_name, info->port.count);
  705. info->port.count = 1;
  706. }
  707. info->port.count--;
  708. /* if at least one open remaining, leave hardware active */
  709. if (info->port.count)
  710. goto cleanup;
  711. info->port.flags |= ASYNC_CLOSING;
  712. /* set tty->closing to notify line discipline to
  713. * only process XON/XOFF characters. Only the N_TTY
  714. * discipline appears to use this (ppp does not).
  715. */
  716. tty->closing = 1;
  717. /* wait for transmit data to clear all layers */
  718. if (info->port.closing_wait != ASYNC_CLOSING_WAIT_NONE) {
  719. if (debug_level >= DEBUG_LEVEL_INFO)
  720. printk("%s(%d):%s close() calling tty_wait_until_sent\n",
  721. __FILE__,__LINE__, info->device_name );
  722. tty_wait_until_sent(tty, info->port.closing_wait);
  723. }
  724. if (info->port.flags & ASYNC_INITIALIZED)
  725. wait_until_sent(tty, info->timeout);
  726. flush_buffer(tty);
  727. tty_ldisc_flush(tty);
  728. shutdown(info);
  729. tty->closing = 0;
  730. info->port.tty = NULL;
  731. if (info->port.blocked_open) {
  732. if (info->port.close_delay) {
  733. msleep_interruptible(jiffies_to_msecs(info->port.close_delay));
  734. }
  735. wake_up_interruptible(&info->port.open_wait);
  736. }
  737. info->port.flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
  738. wake_up_interruptible(&info->port.close_wait);
  739. cleanup:
  740. if (debug_level >= DEBUG_LEVEL_INFO)
  741. printk("%s(%d):%s close() exit, count=%d\n", __FILE__,__LINE__,
  742. tty->driver->name, info->port.count);
  743. }
  744. /* Called by tty_hangup() when a hangup is signaled.
  745. * This is the same as closing all open descriptors for the port.
  746. */
  747. static void hangup(struct tty_struct *tty)
  748. {
  749. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  750. if (debug_level >= DEBUG_LEVEL_INFO)
  751. printk("%s(%d):%s hangup()\n",
  752. __FILE__,__LINE__, info->device_name );
  753. if (sanity_check(info, tty->name, "hangup"))
  754. return;
  755. flush_buffer(tty);
  756. shutdown(info);
  757. info->port.count = 0;
  758. info->port.flags &= ~ASYNC_NORMAL_ACTIVE;
  759. info->port.tty = NULL;
  760. wake_up_interruptible(&info->port.open_wait);
  761. }
  762. /* Set new termios settings
  763. */
  764. static void set_termios(struct tty_struct *tty, struct ktermios *old_termios)
  765. {
  766. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  767. unsigned long flags;
  768. if (debug_level >= DEBUG_LEVEL_INFO)
  769. printk("%s(%d):%s set_termios()\n", __FILE__,__LINE__,
  770. tty->driver->name );
  771. change_params(info);
  772. /* Handle transition to B0 status */
  773. if (old_termios->c_cflag & CBAUD &&
  774. !(tty->termios->c_cflag & CBAUD)) {
  775. info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
  776. spin_lock_irqsave(&info->lock,flags);
  777. set_signals(info);
  778. spin_unlock_irqrestore(&info->lock,flags);
  779. }
  780. /* Handle transition away from B0 status */
  781. if (!(old_termios->c_cflag & CBAUD) &&
  782. tty->termios->c_cflag & CBAUD) {
  783. info->serial_signals |= SerialSignal_DTR;
  784. if (!(tty->termios->c_cflag & CRTSCTS) ||
  785. !test_bit(TTY_THROTTLED, &tty->flags)) {
  786. info->serial_signals |= SerialSignal_RTS;
  787. }
  788. spin_lock_irqsave(&info->lock,flags);
  789. set_signals(info);
  790. spin_unlock_irqrestore(&info->lock,flags);
  791. }
  792. /* Handle turning off CRTSCTS */
  793. if (old_termios->c_cflag & CRTSCTS &&
  794. !(tty->termios->c_cflag & CRTSCTS)) {
  795. tty->hw_stopped = 0;
  796. tx_release(tty);
  797. }
  798. }
  799. /* Send a block of data
  800. *
  801. * Arguments:
  802. *
  803. * tty pointer to tty information structure
  804. * buf pointer to buffer containing send data
  805. * count size of send data in bytes
  806. *
  807. * Return Value: number of characters written
  808. */
  809. static int write(struct tty_struct *tty,
  810. const unsigned char *buf, int count)
  811. {
  812. int c, ret = 0;
  813. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  814. unsigned long flags;
  815. if (debug_level >= DEBUG_LEVEL_INFO)
  816. printk("%s(%d):%s write() count=%d\n",
  817. __FILE__,__LINE__,info->device_name,count);
  818. if (sanity_check(info, tty->name, "write"))
  819. goto cleanup;
  820. if (!info->tx_buf)
  821. goto cleanup;
  822. if (info->params.mode == MGSL_MODE_HDLC) {
  823. if (count > info->max_frame_size) {
  824. ret = -EIO;
  825. goto cleanup;
  826. }
  827. if (info->tx_active)
  828. goto cleanup;
  829. if (info->tx_count) {
  830. /* send accumulated data from send_char() calls */
  831. /* as frame and wait before accepting more data. */
  832. tx_load_dma_buffer(info, info->tx_buf, info->tx_count);
  833. goto start;
  834. }
  835. ret = info->tx_count = count;
  836. tx_load_dma_buffer(info, buf, count);
  837. goto start;
  838. }
  839. for (;;) {
  840. c = min_t(int, count,
  841. min(info->max_frame_size - info->tx_count - 1,
  842. info->max_frame_size - info->tx_put));
  843. if (c <= 0)
  844. break;
  845. memcpy(info->tx_buf + info->tx_put, buf, c);
  846. spin_lock_irqsave(&info->lock,flags);
  847. info->tx_put += c;
  848. if (info->tx_put >= info->max_frame_size)
  849. info->tx_put -= info->max_frame_size;
  850. info->tx_count += c;
  851. spin_unlock_irqrestore(&info->lock,flags);
  852. buf += c;
  853. count -= c;
  854. ret += c;
  855. }
  856. if (info->params.mode == MGSL_MODE_HDLC) {
  857. if (count) {
  858. ret = info->tx_count = 0;
  859. goto cleanup;
  860. }
  861. tx_load_dma_buffer(info, info->tx_buf, info->tx_count);
  862. }
  863. start:
  864. if (info->tx_count && !tty->stopped && !tty->hw_stopped) {
  865. spin_lock_irqsave(&info->lock,flags);
  866. if (!info->tx_active)
  867. tx_start(info);
  868. spin_unlock_irqrestore(&info->lock,flags);
  869. }
  870. cleanup:
  871. if (debug_level >= DEBUG_LEVEL_INFO)
  872. printk( "%s(%d):%s write() returning=%d\n",
  873. __FILE__,__LINE__,info->device_name,ret);
  874. return ret;
  875. }
  876. /* Add a character to the transmit buffer.
  877. */
  878. static int put_char(struct tty_struct *tty, unsigned char ch)
  879. {
  880. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  881. unsigned long flags;
  882. int ret = 0;
  883. if ( debug_level >= DEBUG_LEVEL_INFO ) {
  884. printk( "%s(%d):%s put_char(%d)\n",
  885. __FILE__,__LINE__,info->device_name,ch);
  886. }
  887. if (sanity_check(info, tty->name, "put_char"))
  888. return 0;
  889. if (!info->tx_buf)
  890. return 0;
  891. spin_lock_irqsave(&info->lock,flags);
  892. if ( (info->params.mode != MGSL_MODE_HDLC) ||
  893. !info->tx_active ) {
  894. if (info->tx_count < info->max_frame_size - 1) {
  895. info->tx_buf[info->tx_put++] = ch;
  896. if (info->tx_put >= info->max_frame_size)
  897. info->tx_put -= info->max_frame_size;
  898. info->tx_count++;
  899. ret = 1;
  900. }
  901. }
  902. spin_unlock_irqrestore(&info->lock,flags);
  903. return ret;
  904. }
  905. /* Send a high-priority XON/XOFF character
  906. */
  907. static void send_xchar(struct tty_struct *tty, char ch)
  908. {
  909. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  910. unsigned long flags;
  911. if (debug_level >= DEBUG_LEVEL_INFO)
  912. printk("%s(%d):%s send_xchar(%d)\n",
  913. __FILE__,__LINE__, info->device_name, ch );
  914. if (sanity_check(info, tty->name, "send_xchar"))
  915. return;
  916. info->x_char = ch;
  917. if (ch) {
  918. /* Make sure transmit interrupts are on */
  919. spin_lock_irqsave(&info->lock,flags);
  920. if (!info->tx_enabled)
  921. tx_start(info);
  922. spin_unlock_irqrestore(&info->lock,flags);
  923. }
  924. }
  925. /* Wait until the transmitter is empty.
  926. */
  927. static void wait_until_sent(struct tty_struct *tty, int timeout)
  928. {
  929. SLMP_INFO * info = (SLMP_INFO *)tty->driver_data;
  930. unsigned long orig_jiffies, char_time;
  931. if (!info )
  932. return;
  933. if (debug_level >= DEBUG_LEVEL_INFO)
  934. printk("%s(%d):%s wait_until_sent() entry\n",
  935. __FILE__,__LINE__, info->device_name );
  936. if (sanity_check(info, tty->name, "wait_until_sent"))
  937. return;
  938. lock_kernel();
  939. if (!(info->port.flags & ASYNC_INITIALIZED))
  940. goto exit;
  941. orig_jiffies = jiffies;
  942. /* Set check interval to 1/5 of estimated time to
  943. * send a character, and make it at least 1. The check
  944. * interval should also be less than the timeout.
  945. * Note: use tight timings here to satisfy the NIST-PCTS.
  946. */
  947. if ( info->params.data_rate ) {
  948. char_time = info->timeout/(32 * 5);
  949. if (!char_time)
  950. char_time++;
  951. } else
  952. char_time = 1;
  953. if (timeout)
  954. char_time = min_t(unsigned long, char_time, timeout);
  955. if ( info->params.mode == MGSL_MODE_HDLC ) {
  956. while (info->tx_active) {
  957. msleep_interruptible(jiffies_to_msecs(char_time));
  958. if (signal_pending(current))
  959. break;
  960. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  961. break;
  962. }
  963. } else {
  964. //TODO: determine if there is something similar to USC16C32
  965. // TXSTATUS_ALL_SENT status
  966. while ( info->tx_active && info->tx_enabled) {
  967. msleep_interruptible(jiffies_to_msecs(char_time));
  968. if (signal_pending(current))
  969. break;
  970. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  971. break;
  972. }
  973. }
  974. exit:
  975. unlock_kernel();
  976. if (debug_level >= DEBUG_LEVEL_INFO)
  977. printk("%s(%d):%s wait_until_sent() exit\n",
  978. __FILE__,__LINE__, info->device_name );
  979. }
  980. /* Return the count of free bytes in transmit buffer
  981. */
  982. static int write_room(struct tty_struct *tty)
  983. {
  984. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  985. int ret;
  986. if (sanity_check(info, tty->name, "write_room"))
  987. return 0;
  988. lock_kernel();
  989. if (info->params.mode == MGSL_MODE_HDLC) {
  990. ret = (info->tx_active) ? 0 : HDLC_MAX_FRAME_SIZE;
  991. } else {
  992. ret = info->max_frame_size - info->tx_count - 1;
  993. if (ret < 0)
  994. ret = 0;
  995. }
  996. unlock_kernel();
  997. if (debug_level >= DEBUG_LEVEL_INFO)
  998. printk("%s(%d):%s write_room()=%d\n",
  999. __FILE__, __LINE__, info->device_name, ret);
  1000. return ret;
  1001. }
  1002. /* enable transmitter and send remaining buffered characters
  1003. */
  1004. static void flush_chars(struct tty_struct *tty)
  1005. {
  1006. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  1007. unsigned long flags;
  1008. if ( debug_level >= DEBUG_LEVEL_INFO )
  1009. printk( "%s(%d):%s flush_chars() entry tx_count=%d\n",
  1010. __FILE__,__LINE__,info->device_name,info->tx_count);
  1011. if (sanity_check(info, tty->name, "flush_chars"))
  1012. return;
  1013. if (info->tx_count <= 0 || tty->stopped || tty->hw_stopped ||
  1014. !info->tx_buf)
  1015. return;
  1016. if ( debug_level >= DEBUG_LEVEL_INFO )
  1017. printk( "%s(%d):%s flush_chars() entry, starting transmitter\n",
  1018. __FILE__,__LINE__,info->device_name );
  1019. spin_lock_irqsave(&info->lock,flags);
  1020. if (!info->tx_active) {
  1021. if ( (info->params.mode == MGSL_MODE_HDLC) &&
  1022. info->tx_count ) {
  1023. /* operating in synchronous (frame oriented) mode */
  1024. /* copy data from circular tx_buf to */
  1025. /* transmit DMA buffer. */
  1026. tx_load_dma_buffer(info,
  1027. info->tx_buf,info->tx_count);
  1028. }
  1029. tx_start(info);
  1030. }
  1031. spin_unlock_irqrestore(&info->lock,flags);
  1032. }
  1033. /* Discard all data in the send buffer
  1034. */
  1035. static void flush_buffer(struct tty_struct *tty)
  1036. {
  1037. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  1038. unsigned long flags;
  1039. if (debug_level >= DEBUG_LEVEL_INFO)
  1040. printk("%s(%d):%s flush_buffer() entry\n",
  1041. __FILE__,__LINE__, info->device_name );
  1042. if (sanity_check(info, tty->name, "flush_buffer"))
  1043. return;
  1044. spin_lock_irqsave(&info->lock,flags);
  1045. info->tx_count = info->tx_put = info->tx_get = 0;
  1046. del_timer(&info->tx_timer);
  1047. spin_unlock_irqrestore(&info->lock,flags);
  1048. tty_wakeup(tty);
  1049. }
  1050. /* throttle (stop) transmitter
  1051. */
  1052. static void tx_hold(struct tty_struct *tty)
  1053. {
  1054. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  1055. unsigned long flags;
  1056. if (sanity_check(info, tty->name, "tx_hold"))
  1057. return;
  1058. if ( debug_level >= DEBUG_LEVEL_INFO )
  1059. printk("%s(%d):%s tx_hold()\n",
  1060. __FILE__,__LINE__,info->device_name);
  1061. spin_lock_irqsave(&info->lock,flags);
  1062. if (info->tx_enabled)
  1063. tx_stop(info);
  1064. spin_unlock_irqrestore(&info->lock,flags);
  1065. }
  1066. /* release (start) transmitter
  1067. */
  1068. static void tx_release(struct tty_struct *tty)
  1069. {
  1070. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  1071. unsigned long flags;
  1072. if (sanity_check(info, tty->name, "tx_release"))
  1073. return;
  1074. if ( debug_level >= DEBUG_LEVEL_INFO )
  1075. printk("%s(%d):%s tx_release()\n",
  1076. __FILE__,__LINE__,info->device_name);
  1077. spin_lock_irqsave(&info->lock,flags);
  1078. if (!info->tx_enabled)
  1079. tx_start(info);
  1080. spin_unlock_irqrestore(&info->lock,flags);
  1081. }
  1082. /* Service an IOCTL request
  1083. *
  1084. * Arguments:
  1085. *
  1086. * tty pointer to tty instance data
  1087. * file pointer to associated file object for device
  1088. * cmd IOCTL command code
  1089. * arg command argument/context
  1090. *
  1091. * Return Value: 0 if success, otherwise error code
  1092. */
  1093. static int do_ioctl(struct tty_struct *tty, struct file *file,
  1094. unsigned int cmd, unsigned long arg)
  1095. {
  1096. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  1097. int error;
  1098. struct mgsl_icount cnow; /* kernel counter temps */
  1099. struct serial_icounter_struct __user *p_cuser; /* user space */
  1100. unsigned long flags;
  1101. void __user *argp = (void __user *)arg;
  1102. if (debug_level >= DEBUG_LEVEL_INFO)
  1103. printk("%s(%d):%s ioctl() cmd=%08X\n", __FILE__,__LINE__,
  1104. info->device_name, cmd );
  1105. if (sanity_check(info, tty->name, "ioctl"))
  1106. return -ENODEV;
  1107. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  1108. (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
  1109. if (tty->flags & (1 << TTY_IO_ERROR))
  1110. return -EIO;
  1111. }
  1112. switch (cmd) {
  1113. case MGSL_IOCGPARAMS:
  1114. return get_params(info, argp);
  1115. case MGSL_IOCSPARAMS:
  1116. return set_params(info, argp);
  1117. case MGSL_IOCGTXIDLE:
  1118. return get_txidle(info, argp);
  1119. case MGSL_IOCSTXIDLE:
  1120. return set_txidle(info, (int)arg);
  1121. case MGSL_IOCTXENABLE:
  1122. return tx_enable(info, (int)arg);
  1123. case MGSL_IOCRXENABLE:
  1124. return rx_enable(info, (int)arg);
  1125. case MGSL_IOCTXABORT:
  1126. return tx_abort(info);
  1127. case MGSL_IOCGSTATS:
  1128. return get_stats(info, argp);
  1129. case MGSL_IOCWAITEVENT:
  1130. return wait_mgsl_event(info, argp);
  1131. case MGSL_IOCLOOPTXDONE:
  1132. return 0; // TODO: Not supported, need to document
  1133. /* Wait for modem input (DCD,RI,DSR,CTS) change
  1134. * as specified by mask in arg (TIOCM_RNG/DSR/CD/CTS)
  1135. */
  1136. case TIOCMIWAIT:
  1137. return modem_input_wait(info,(int)arg);
  1138. /*
  1139. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1140. * Return: write counters to the user passed counter struct
  1141. * NB: both 1->0 and 0->1 transitions are counted except for
  1142. * RI where only 0->1 is counted.
  1143. */
  1144. case TIOCGICOUNT:
  1145. spin_lock_irqsave(&info->lock,flags);
  1146. cnow = info->icount;
  1147. spin_unlock_irqrestore(&info->lock,flags);
  1148. p_cuser = argp;
  1149. PUT_USER(error,cnow.cts, &p_cuser->cts);
  1150. if (error) return error;
  1151. PUT_USER(error,cnow.dsr, &p_cuser->dsr);
  1152. if (error) return error;
  1153. PUT_USER(error,cnow.rng, &p_cuser->rng);
  1154. if (error) return error;
  1155. PUT_USER(error,cnow.dcd, &p_cuser->dcd);
  1156. if (error) return error;
  1157. PUT_USER(error,cnow.rx, &p_cuser->rx);
  1158. if (error) return error;
  1159. PUT_USER(error,cnow.tx, &p_cuser->tx);
  1160. if (error) return error;
  1161. PUT_USER(error,cnow.frame, &p_cuser->frame);
  1162. if (error) return error;
  1163. PUT_USER(error,cnow.overrun, &p_cuser->overrun);
  1164. if (error) return error;
  1165. PUT_USER(error,cnow.parity, &p_cuser->parity);
  1166. if (error) return error;
  1167. PUT_USER(error,cnow.brk, &p_cuser->brk);
  1168. if (error) return error;
  1169. PUT_USER(error,cnow.buf_overrun, &p_cuser->buf_overrun);
  1170. if (error) return error;
  1171. return 0;
  1172. default:
  1173. return -ENOIOCTLCMD;
  1174. }
  1175. return 0;
  1176. }
  1177. static int ioctl(struct tty_struct *tty, struct file *file,
  1178. unsigned int cmd, unsigned long arg)
  1179. {
  1180. int ret;
  1181. lock_kernel();
  1182. ret = do_ioctl(tty, file, cmd, arg);
  1183. unlock_kernel();
  1184. return ret;
  1185. }
  1186. /*
  1187. * /proc fs routines....
  1188. */
  1189. static inline int line_info(char *buf, SLMP_INFO *info)
  1190. {
  1191. char stat_buf[30];
  1192. int ret;
  1193. unsigned long flags;
  1194. ret = sprintf(buf, "%s: SCABase=%08x Mem=%08X StatusControl=%08x LCR=%08X\n"
  1195. "\tIRQ=%d MaxFrameSize=%u\n",
  1196. info->device_name,
  1197. info->phys_sca_base,
  1198. info->phys_memory_base,
  1199. info->phys_statctrl_base,
  1200. info->phys_lcr_base,
  1201. info->irq_level,
  1202. info->max_frame_size );
  1203. /* output current serial signal states */
  1204. spin_lock_irqsave(&info->lock,flags);
  1205. get_signals(info);
  1206. spin_unlock_irqrestore(&info->lock,flags);
  1207. stat_buf[0] = 0;
  1208. stat_buf[1] = 0;
  1209. if (info->serial_signals & SerialSignal_RTS)
  1210. strcat(stat_buf, "|RTS");
  1211. if (info->serial_signals & SerialSignal_CTS)
  1212. strcat(stat_buf, "|CTS");
  1213. if (info->serial_signals & SerialSignal_DTR)
  1214. strcat(stat_buf, "|DTR");
  1215. if (info->serial_signals & SerialSignal_DSR)
  1216. strcat(stat_buf, "|DSR");
  1217. if (info->serial_signals & SerialSignal_DCD)
  1218. strcat(stat_buf, "|CD");
  1219. if (info->serial_signals & SerialSignal_RI)
  1220. strcat(stat_buf, "|RI");
  1221. if (info->params.mode == MGSL_MODE_HDLC) {
  1222. ret += sprintf(buf+ret, "\tHDLC txok:%d rxok:%d",
  1223. info->icount.txok, info->icount.rxok);
  1224. if (info->icount.txunder)
  1225. ret += sprintf(buf+ret, " txunder:%d", info->icount.txunder);
  1226. if (info->icount.txabort)
  1227. ret += sprintf(buf+ret, " txabort:%d", info->icount.txabort);
  1228. if (info->icount.rxshort)
  1229. ret += sprintf(buf+ret, " rxshort:%d", info->icount.rxshort);
  1230. if (info->icount.rxlong)
  1231. ret += sprintf(buf+ret, " rxlong:%d", info->icount.rxlong);
  1232. if (info->icount.rxover)
  1233. ret += sprintf(buf+ret, " rxover:%d", info->icount.rxover);
  1234. if (info->icount.rxcrc)
  1235. ret += sprintf(buf+ret, " rxlong:%d", info->icount.rxcrc);
  1236. } else {
  1237. ret += sprintf(buf+ret, "\tASYNC tx:%d rx:%d",
  1238. info->icount.tx, info->icount.rx);
  1239. if (info->icount.frame)
  1240. ret += sprintf(buf+ret, " fe:%d", info->icount.frame);
  1241. if (info->icount.parity)
  1242. ret += sprintf(buf+ret, " pe:%d", info->icount.parity);
  1243. if (info->icount.brk)
  1244. ret += sprintf(buf+ret, " brk:%d", info->icount.brk);
  1245. if (info->icount.overrun)
  1246. ret += sprintf(buf+ret, " oe:%d", info->icount.overrun);
  1247. }
  1248. /* Append serial signal status to end */
  1249. ret += sprintf(buf+ret, " %s\n", stat_buf+1);
  1250. ret += sprintf(buf+ret, "\ttxactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
  1251. info->tx_active,info->bh_requested,info->bh_running,
  1252. info->pending_bh);
  1253. return ret;
  1254. }
  1255. /* Called to print information about devices
  1256. */
  1257. static int read_proc(char *page, char **start, off_t off, int count,
  1258. int *eof, void *data)
  1259. {
  1260. int len = 0, l;
  1261. off_t begin = 0;
  1262. SLMP_INFO *info;
  1263. len += sprintf(page, "synclinkmp driver:%s\n", driver_version);
  1264. info = synclinkmp_device_list;
  1265. while( info ) {
  1266. l = line_info(page + len, info);
  1267. len += l;
  1268. if (len+begin > off+count)
  1269. goto done;
  1270. if (len+begin < off) {
  1271. begin += len;
  1272. len = 0;
  1273. }
  1274. info = info->next_device;
  1275. }
  1276. *eof = 1;
  1277. done:
  1278. if (off >= len+begin)
  1279. return 0;
  1280. *start = page + (off-begin);
  1281. return ((count < begin+len-off) ? count : begin+len-off);
  1282. }
  1283. /* Return the count of bytes in transmit buffer
  1284. */
  1285. static int chars_in_buffer(struct tty_struct *tty)
  1286. {
  1287. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  1288. if (sanity_check(info, tty->name, "chars_in_buffer"))
  1289. return 0;
  1290. if (debug_level >= DEBUG_LEVEL_INFO)
  1291. printk("%s(%d):%s chars_in_buffer()=%d\n",
  1292. __FILE__, __LINE__, info->device_name, info->tx_count);
  1293. return info->tx_count;
  1294. }
  1295. /* Signal remote device to throttle send data (our receive data)
  1296. */
  1297. static void throttle(struct tty_struct * tty)
  1298. {
  1299. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  1300. unsigned long flags;
  1301. if (debug_level >= DEBUG_LEVEL_INFO)
  1302. printk("%s(%d):%s throttle() entry\n",
  1303. __FILE__,__LINE__, info->device_name );
  1304. if (sanity_check(info, tty->name, "throttle"))
  1305. return;
  1306. if (I_IXOFF(tty))
  1307. send_xchar(tty, STOP_CHAR(tty));
  1308. if (tty->termios->c_cflag & CRTSCTS) {
  1309. spin_lock_irqsave(&info->lock,flags);
  1310. info->serial_signals &= ~SerialSignal_RTS;
  1311. set_signals(info);
  1312. spin_unlock_irqrestore(&info->lock,flags);
  1313. }
  1314. }
  1315. /* Signal remote device to stop throttling send data (our receive data)
  1316. */
  1317. static void unthrottle(struct tty_struct * tty)
  1318. {
  1319. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  1320. unsigned long flags;
  1321. if (debug_level >= DEBUG_LEVEL_INFO)
  1322. printk("%s(%d):%s unthrottle() entry\n",
  1323. __FILE__,__LINE__, info->device_name );
  1324. if (sanity_check(info, tty->name, "unthrottle"))
  1325. return;
  1326. if (I_IXOFF(tty)) {
  1327. if (info->x_char)
  1328. info->x_char = 0;
  1329. else
  1330. send_xchar(tty, START_CHAR(tty));
  1331. }
  1332. if (tty->termios->c_cflag & CRTSCTS) {
  1333. spin_lock_irqsave(&info->lock,flags);
  1334. info->serial_signals |= SerialSignal_RTS;
  1335. set_signals(info);
  1336. spin_unlock_irqrestore(&info->lock,flags);
  1337. }
  1338. }
  1339. /* set or clear transmit break condition
  1340. * break_state -1=set break condition, 0=clear
  1341. */
  1342. static void set_break(struct tty_struct *tty, int break_state)
  1343. {
  1344. unsigned char RegValue;
  1345. SLMP_INFO * info = (SLMP_INFO *)tty->driver_data;
  1346. unsigned long flags;
  1347. if (debug_level >= DEBUG_LEVEL_INFO)
  1348. printk("%s(%d):%s set_break(%d)\n",
  1349. __FILE__,__LINE__, info->device_name, break_state);
  1350. if (sanity_check(info, tty->name, "set_break"))
  1351. return;
  1352. spin_lock_irqsave(&info->lock,flags);
  1353. RegValue = read_reg(info, CTL);
  1354. if (break_state == -1)
  1355. RegValue |= BIT3;
  1356. else
  1357. RegValue &= ~BIT3;
  1358. write_reg(info, CTL, RegValue);
  1359. spin_unlock_irqrestore(&info->lock,flags);
  1360. }
  1361. #if SYNCLINK_GENERIC_HDLC
  1362. /**
  1363. * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
  1364. * set encoding and frame check sequence (FCS) options
  1365. *
  1366. * dev pointer to network device structure
  1367. * encoding serial encoding setting
  1368. * parity FCS setting
  1369. *
  1370. * returns 0 if success, otherwise error code
  1371. */
  1372. static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
  1373. unsigned short parity)
  1374. {
  1375. SLMP_INFO *info = dev_to_port(dev);
  1376. unsigned char new_encoding;
  1377. unsigned short new_crctype;
  1378. /* return error if TTY interface open */
  1379. if (info->port.count)
  1380. return -EBUSY;
  1381. switch (encoding)
  1382. {
  1383. case ENCODING_NRZ: new_encoding = HDLC_ENCODING_NRZ; break;
  1384. case ENCODING_NRZI: new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
  1385. case ENCODING_FM_MARK: new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
  1386. case ENCODING_FM_SPACE: new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
  1387. case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
  1388. default: return -EINVAL;
  1389. }
  1390. switch (parity)
  1391. {
  1392. case PARITY_NONE: new_crctype = HDLC_CRC_NONE; break;
  1393. case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
  1394. case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
  1395. default: return -EINVAL;
  1396. }
  1397. info->params.encoding = new_encoding;
  1398. info->params.crc_type = new_crctype;
  1399. /* if network interface up, reprogram hardware */
  1400. if (info->netcount)
  1401. program_hw(info);
  1402. return 0;
  1403. }
  1404. /**
  1405. * called by generic HDLC layer to send frame
  1406. *
  1407. * skb socket buffer containing HDLC frame
  1408. * dev pointer to network device structure
  1409. *
  1410. * returns 0 if success, otherwise error code
  1411. */
  1412. static int hdlcdev_xmit(struct sk_buff *skb, struct net_device *dev)
  1413. {
  1414. SLMP_INFO *info = dev_to_port(dev);
  1415. struct net_device_stats *stats = hdlc_stats(dev);
  1416. unsigned long flags;
  1417. if (debug_level >= DEBUG_LEVEL_INFO)
  1418. printk(KERN_INFO "%s:hdlc_xmit(%s)\n",__FILE__,dev->name);
  1419. /* stop sending until this frame completes */
  1420. netif_stop_queue(dev);
  1421. /* copy data to device buffers */
  1422. info->tx_count = skb->len;
  1423. tx_load_dma_buffer(info, skb->data, skb->len);
  1424. /* update network statistics */
  1425. stats->tx_packets++;
  1426. stats->tx_bytes += skb->len;
  1427. /* done with socket buffer, so free it */
  1428. dev_kfree_skb(skb);
  1429. /* save start time for transmit timeout detection */
  1430. dev->trans_start = jiffies;
  1431. /* start hardware transmitter if necessary */
  1432. spin_lock_irqsave(&info->lock,flags);
  1433. if (!info->tx_active)
  1434. tx_start(info);
  1435. spin_unlock_irqrestore(&info->lock,flags);
  1436. return 0;
  1437. }
  1438. /**
  1439. * called by network layer when interface enabled
  1440. * claim resources and initialize hardware
  1441. *
  1442. * dev pointer to network device structure
  1443. *
  1444. * returns 0 if success, otherwise error code
  1445. */
  1446. static int hdlcdev_open(struct net_device *dev)
  1447. {
  1448. SLMP_INFO *info = dev_to_port(dev);
  1449. int rc;
  1450. unsigned long flags;
  1451. if (debug_level >= DEBUG_LEVEL_INFO)
  1452. printk("%s:hdlcdev_open(%s)\n",__FILE__,dev->name);
  1453. /* generic HDLC layer open processing */
  1454. if ((rc = hdlc_open(dev)))
  1455. return rc;
  1456. /* arbitrate between network and tty opens */
  1457. spin_lock_irqsave(&info->netlock, flags);
  1458. if (info->port.count != 0 || info->netcount != 0) {
  1459. printk(KERN_WARNING "%s: hdlc_open returning busy\n", dev->name);
  1460. spin_unlock_irqrestore(&info->netlock, flags);
  1461. return -EBUSY;
  1462. }
  1463. info->netcount=1;
  1464. spin_unlock_irqrestore(&info->netlock, flags);
  1465. /* claim resources and init adapter */
  1466. if ((rc = startup(info)) != 0) {
  1467. spin_lock_irqsave(&info->netlock, flags);
  1468. info->netcount=0;
  1469. spin_unlock_irqrestore(&info->netlock, flags);
  1470. return rc;
  1471. }
  1472. /* assert DTR and RTS, apply hardware settings */
  1473. info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
  1474. program_hw(info);
  1475. /* enable network layer transmit */
  1476. dev->trans_start = jiffies;
  1477. netif_start_queue(dev);
  1478. /* inform generic HDLC layer of current DCD status */
  1479. spin_lock_irqsave(&info->lock, flags);
  1480. get_signals(info);
  1481. spin_unlock_irqrestore(&info->lock, flags);
  1482. if (info->serial_signals & SerialSignal_DCD)
  1483. netif_carrier_on(dev);
  1484. else
  1485. netif_carrier_off(dev);
  1486. return 0;
  1487. }
  1488. /**
  1489. * called by network layer when interface is disabled
  1490. * shutdown hardware and release resources
  1491. *
  1492. * dev pointer to network device structure
  1493. *
  1494. * returns 0 if success, otherwise error code
  1495. */
  1496. static int hdlcdev_close(struct net_device *dev)
  1497. {
  1498. SLMP_INFO *info = dev_to_port(dev);
  1499. unsigned long flags;
  1500. if (debug_level >= DEBUG_LEVEL_INFO)
  1501. printk("%s:hdlcdev_close(%s)\n",__FILE__,dev->name);
  1502. netif_stop_queue(dev);
  1503. /* shutdown adapter and release resources */
  1504. shutdown(info);
  1505. hdlc_close(dev);
  1506. spin_lock_irqsave(&info->netlock, flags);
  1507. info->netcount=0;
  1508. spin_unlock_irqrestore(&info->netlock, flags);
  1509. return 0;
  1510. }
  1511. /**
  1512. * called by network layer to process IOCTL call to network device
  1513. *
  1514. * dev pointer to network device structure
  1515. * ifr pointer to network interface request structure
  1516. * cmd IOCTL command code
  1517. *
  1518. * returns 0 if success, otherwise error code
  1519. */
  1520. static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1521. {
  1522. const size_t size = sizeof(sync_serial_settings);
  1523. sync_serial_settings new_line;
  1524. sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
  1525. SLMP_INFO *info = dev_to_port(dev);
  1526. unsigned int flags;
  1527. if (debug_level >= DEBUG_LEVEL_INFO)
  1528. printk("%s:hdlcdev_ioctl(%s)\n",__FILE__,dev->name);
  1529. /* return error if TTY interface open */
  1530. if (info->port.count)
  1531. return -EBUSY;
  1532. if (cmd != SIOCWANDEV)
  1533. return hdlc_ioctl(dev, ifr, cmd);
  1534. switch(ifr->ifr_settings.type) {
  1535. case IF_GET_IFACE: /* return current sync_serial_settings */
  1536. ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
  1537. if (ifr->ifr_settings.size < size) {
  1538. ifr->ifr_settings.size = size; /* data size wanted */
  1539. return -ENOBUFS;
  1540. }
  1541. flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
  1542. HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
  1543. HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
  1544. HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
  1545. switch (flags){
  1546. case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
  1547. case (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_INT; break;
  1548. case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_TXINT; break;
  1549. case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
  1550. default: new_line.clock_type = CLOCK_DEFAULT;
  1551. }
  1552. new_line.clock_rate = info->params.clock_speed;
  1553. new_line.loopback = info->params.loopback ? 1:0;
  1554. if (copy_to_user(line, &new_line, size))
  1555. return -EFAULT;
  1556. return 0;
  1557. case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
  1558. if(!capable(CAP_NET_ADMIN))
  1559. return -EPERM;
  1560. if (copy_from_user(&new_line, line, size))
  1561. return -EFAULT;
  1562. switch (new_line.clock_type)
  1563. {
  1564. case CLOCK_EXT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
  1565. case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
  1566. case CLOCK_INT: flags = HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG; break;
  1567. case CLOCK_TXINT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG; break;
  1568. case CLOCK_DEFAULT: flags = info->params.flags &
  1569. (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
  1570. HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
  1571. HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
  1572. HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN); break;
  1573. default: return -EINVAL;
  1574. }
  1575. if (new_line.loopback != 0 && new_line.loopback != 1)
  1576. return -EINVAL;
  1577. info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
  1578. HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
  1579. HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
  1580. HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
  1581. info->params.flags |= flags;
  1582. info->params.loopback = new_line.loopback;
  1583. if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
  1584. info->params.clock_speed = new_line.clock_rate;
  1585. else
  1586. info->params.clock_speed = 0;
  1587. /* if network interface up, reprogram hardware */
  1588. if (info->netcount)
  1589. program_hw(info);
  1590. return 0;
  1591. default:
  1592. return hdlc_ioctl(dev, ifr, cmd);
  1593. }
  1594. }
  1595. /**
  1596. * called by network layer when transmit timeout is detected
  1597. *
  1598. * dev pointer to network device structure
  1599. */
  1600. static void hdlcdev_tx_timeout(struct net_device *dev)
  1601. {
  1602. SLMP_INFO *info = dev_to_port(dev);
  1603. struct net_device_stats *stats = hdlc_stats(dev);
  1604. unsigned long flags;
  1605. if (debug_level >= DEBUG_LEVEL_INFO)
  1606. printk("hdlcdev_tx_timeout(%s)\n",dev->name);
  1607. stats->tx_errors++;
  1608. stats->tx_aborted_errors++;
  1609. spin_lock_irqsave(&info->lock,flags);
  1610. tx_stop(info);
  1611. spin_unlock_irqrestore(&info->lock,flags);
  1612. netif_wake_queue(dev);
  1613. }
  1614. /**
  1615. * called by device driver when transmit completes
  1616. * reenable network layer transmit if stopped
  1617. *
  1618. * info pointer to device instance information
  1619. */
  1620. static void hdlcdev_tx_done(SLMP_INFO *info)
  1621. {
  1622. if (netif_queue_stopped(info->netdev))
  1623. netif_wake_queue(info->netdev);
  1624. }
  1625. /**
  1626. * called by device driver when frame received
  1627. * pass frame to network layer
  1628. *
  1629. * info pointer to device instance information
  1630. * buf pointer to buffer contianing frame data
  1631. * size count of data bytes in buf
  1632. */
  1633. static void hdlcdev_rx(SLMP_INFO *info, char *buf, int size)
  1634. {
  1635. struct sk_buff *skb = dev_alloc_skb(size);
  1636. struct net_device *dev = info->netdev;
  1637. struct net_device_stats *stats = hdlc_stats(dev);
  1638. if (debug_level >= DEBUG_LEVEL_INFO)
  1639. printk("hdlcdev_rx(%s)\n",dev->name);
  1640. if (skb == NULL) {
  1641. printk(KERN_NOTICE "%s: can't alloc skb, dropping packet\n", dev->name);
  1642. stats->rx_dropped++;
  1643. return;
  1644. }
  1645. memcpy(skb_put(skb, size),buf,size);
  1646. skb->protocol = hdlc_type_trans(skb, info->netdev);
  1647. stats->rx_packets++;
  1648. stats->rx_bytes += size;
  1649. netif_rx(skb);
  1650. info->netdev->last_rx = jiffies;
  1651. }
  1652. /**
  1653. * called by device driver when adding device instance
  1654. * do generic HDLC initialization
  1655. *
  1656. * info pointer to device instance information
  1657. *
  1658. * returns 0 if success, otherwise error code
  1659. */
  1660. static int hdlcdev_init(SLMP_INFO *info)
  1661. {
  1662. int rc;
  1663. struct net_device *dev;
  1664. hdlc_device *hdlc;
  1665. /* allocate and initialize network and HDLC layer objects */
  1666. if (!(dev = alloc_hdlcdev(info))) {
  1667. printk(KERN_ERR "%s:hdlc device allocation failure\n",__FILE__);
  1668. return -ENOMEM;
  1669. }
  1670. /* for network layer reporting purposes only */
  1671. dev->mem_start = info->phys_sca_base;
  1672. dev->mem_end = info->phys_sca_base + SCA_BASE_SIZE - 1;
  1673. dev->irq = info->irq_level;
  1674. /* network layer callbacks and settings */
  1675. dev->do_ioctl = hdlcdev_ioctl;
  1676. dev->open = hdlcdev_open;
  1677. dev->stop = hdlcdev_close;
  1678. dev->tx_timeout = hdlcdev_tx_timeout;
  1679. dev->watchdog_timeo = 10*HZ;
  1680. dev->tx_queue_len = 50;
  1681. /* generic HDLC layer callbacks and settings */
  1682. hdlc = dev_to_hdlc(dev);
  1683. hdlc->attach = hdlcdev_attach;
  1684. hdlc->xmit = hdlcdev_xmit;
  1685. /* register objects with HDLC layer */
  1686. if ((rc = register_hdlc_device(dev))) {
  1687. printk(KERN_WARNING "%s:unable to register hdlc device\n",__FILE__);
  1688. free_netdev(dev);
  1689. return rc;
  1690. }
  1691. info->netdev = dev;
  1692. return 0;
  1693. }
  1694. /**
  1695. * called by device driver when removing device instance
  1696. * do generic HDLC cleanup
  1697. *
  1698. * info pointer to device instance information
  1699. */
  1700. static void hdlcdev_exit(SLMP_INFO *info)
  1701. {
  1702. unregister_hdlc_device(info->netdev);
  1703. free_netdev(info->netdev);
  1704. info->netdev = NULL;
  1705. }
  1706. #endif /* CONFIG_HDLC */
  1707. /* Return next bottom half action to perform.
  1708. * Return Value: BH action code or 0 if nothing to do.
  1709. */
  1710. static int bh_action(SLMP_INFO *info)
  1711. {
  1712. unsigned long flags;
  1713. int rc = 0;
  1714. spin_lock_irqsave(&info->lock,flags);
  1715. if (info->pending_bh & BH_RECEIVE) {
  1716. info->pending_bh &= ~BH_RECEIVE;
  1717. rc = BH_RECEIVE;
  1718. } else if (info->pending_bh & BH_TRANSMIT) {
  1719. info->pending_bh &= ~BH_TRANSMIT;
  1720. rc = BH_TRANSMIT;
  1721. } else if (info->pending_bh & BH_STATUS) {
  1722. info->pending_bh &= ~BH_STATUS;
  1723. rc = BH_STATUS;
  1724. }
  1725. if (!rc) {
  1726. /* Mark BH routine as complete */
  1727. info->bh_running = false;
  1728. info->bh_requested = false;
  1729. }
  1730. spin_unlock_irqrestore(&info->lock,flags);
  1731. return rc;
  1732. }
  1733. /* Perform bottom half processing of work items queued by ISR.
  1734. */
  1735. static void bh_handler(struct work_struct *work)
  1736. {
  1737. SLMP_INFO *info = container_of(work, SLMP_INFO, task);
  1738. int action;
  1739. if (!info)
  1740. return;
  1741. if ( debug_level >= DEBUG_LEVEL_BH )
  1742. printk( "%s(%d):%s bh_handler() entry\n",
  1743. __FILE__,__LINE__,info->device_name);
  1744. info->bh_running = true;
  1745. while((action = bh_action(info)) != 0) {
  1746. /* Process work item */
  1747. if ( debug_level >= DEBUG_LEVEL_BH )
  1748. printk( "%s(%d):%s bh_handler() work item action=%d\n",
  1749. __FILE__,__LINE__,info->device_name, action);
  1750. switch (action) {
  1751. case BH_RECEIVE:
  1752. bh_receive(info);
  1753. break;
  1754. case BH_TRANSMIT:
  1755. bh_transmit(info);
  1756. break;
  1757. case BH_STATUS:
  1758. bh_status(info);
  1759. break;
  1760. default:
  1761. /* unknown work item ID */
  1762. printk("%s(%d):%s Unknown work item ID=%08X!\n",
  1763. __FILE__,__LINE__,info->device_name,action);
  1764. break;
  1765. }
  1766. }
  1767. if ( debug_level >= DEBUG_LEVEL_BH )
  1768. printk( "%s(%d):%s bh_handler() exit\n",
  1769. __FILE__,__LINE__,info->device_name);
  1770. }
  1771. static void bh_receive(SLMP_INFO *info)
  1772. {
  1773. if ( debug_level >= DEBUG_LEVEL_BH )
  1774. printk( "%s(%d):%s bh_receive()\n",
  1775. __FILE__,__LINE__,info->device_name);
  1776. while( rx_get_frame(info) );
  1777. }
  1778. static void bh_transmit(SLMP_INFO *info)
  1779. {
  1780. struct tty_struct *tty = info->port.tty;
  1781. if ( debug_level >= DEBUG_LEVEL_BH )
  1782. printk( "%s(%d):%s bh_transmit() entry\n",
  1783. __FILE__,__LINE__,info->device_name);
  1784. if (tty)
  1785. tty_wakeup(tty);
  1786. }
  1787. static void bh_status(SLMP_INFO *info)
  1788. {
  1789. if ( debug_level >= DEBUG_LEVEL_BH )
  1790. printk( "%s(%d):%s bh_status() entry\n",
  1791. __FILE__,__LINE__,info->device_name);
  1792. info->ri_chkcount = 0;
  1793. info->dsr_chkcount = 0;
  1794. info->dcd_chkcount = 0;
  1795. info->cts_chkcount = 0;
  1796. }
  1797. static void isr_timer(SLMP_INFO * info)
  1798. {
  1799. unsigned char timer = (info->port_num & 1) ? TIMER2 : TIMER0;
  1800. /* IER2<7..4> = timer<3..0> interrupt enables (0=disabled) */
  1801. write_reg(info, IER2, 0);
  1802. /* TMCS, Timer Control/Status Register
  1803. *
  1804. * 07 CMF, Compare match flag (read only) 1=match
  1805. * 06 ECMI, CMF Interrupt Enable: 0=disabled
  1806. * 05 Reserved, must be 0
  1807. * 04 TME, Timer Enable
  1808. * 03..00 Reserved, must be 0
  1809. *
  1810. * 0000 0000
  1811. */
  1812. write_reg(info, (unsigned char)(timer + TMCS), 0);
  1813. info->irq_occurred = true;
  1814. if ( debug_level >= DEBUG_LEVEL_ISR )
  1815. printk("%s(%d):%s isr_timer()\n",
  1816. __FILE__,__LINE__,info->device_name);
  1817. }
  1818. static void isr_rxint(SLMP_INFO * info)
  1819. {
  1820. struct tty_struct *tty = info->port.tty;
  1821. struct mgsl_icount *icount = &info->icount;
  1822. unsigned char status = read_reg(info, SR1) & info->ie1_value & (FLGD + IDLD + CDCD + BRKD);
  1823. unsigned char status2 = read_reg(info, SR2) & info->ie2_value & OVRN;
  1824. /* clear status bits */
  1825. if (status)
  1826. write_reg(info, SR1, status);
  1827. if (status2)
  1828. write_reg(info, SR2, status2);
  1829. if ( debug_level >= DEBUG_LEVEL_ISR )
  1830. printk("%s(%d):%s isr_rxint status=%02X %02x\n",
  1831. __FILE__,__LINE__,info->device_name,status,status2);
  1832. if (info->params.mode == MGSL_MODE_ASYNC) {
  1833. if (status & BRKD) {
  1834. icount->brk++;
  1835. /* process break detection if tty control
  1836. * is not set to ignore it
  1837. */
  1838. if ( tty ) {
  1839. if (!(status & info->ignore_status_mask1)) {
  1840. if (info->read_status_mask1 & BRKD) {
  1841. tty_insert_flip_char(tty, 0, TTY_BREAK);
  1842. if (info->port.flags & ASYNC_SAK)
  1843. do_SAK(tty);
  1844. }
  1845. }
  1846. }
  1847. }
  1848. }
  1849. else {
  1850. if (status & (FLGD|IDLD)) {
  1851. if (status & FLGD)
  1852. info->icount.exithunt++;
  1853. else if (status & IDLD)
  1854. info->icount.rxidle++;
  1855. wake_up_interruptible(&info->event_wait_q);
  1856. }
  1857. }
  1858. if (status & CDCD) {
  1859. /* simulate a common modem status change interrupt
  1860. * for our handler
  1861. */
  1862. get_signals( info );
  1863. isr_io_pin(info,
  1864. MISCSTATUS_DCD_LATCHED|(info->serial_signals&SerialSignal_DCD));
  1865. }
  1866. }
  1867. /*
  1868. * handle async rx data interrupts
  1869. */
  1870. static void isr_rxrdy(SLMP_INFO * info)
  1871. {
  1872. u16 status;
  1873. unsigned char DataByte;
  1874. struct tty_struct *tty = info->port.tty;
  1875. struct mgsl_icount *icount = &info->icount;
  1876. if ( debug_level >= DEBUG_LEVEL_ISR )
  1877. printk("%s(%d):%s isr_rxrdy\n",
  1878. __FILE__,__LINE__,info->device_name);
  1879. while((status = read_reg(info,CST0)) & BIT0)
  1880. {
  1881. int flag = 0;
  1882. bool over = false;
  1883. DataByte = read_reg(info,TRB);
  1884. icount->rx++;
  1885. if ( status & (PE + FRME + OVRN) ) {
  1886. printk("%s(%d):%s rxerr=%04X\n",
  1887. __FILE__,__LINE__,info->device_name,status);
  1888. /* update error statistics */
  1889. if (status & PE)
  1890. icount->parity++;
  1891. else if (status & FRME)
  1892. icount->frame++;
  1893. else if (status & OVRN)
  1894. icount->overrun++;
  1895. /* discard char if tty control flags say so */
  1896. if (status & info->ignore_status_mask2)
  1897. continue;
  1898. status &= info->read_status_mask2;
  1899. if ( tty ) {
  1900. if (status & PE)
  1901. flag = TTY_PARITY;
  1902. else if (status & FRME)
  1903. flag = TTY_FRAME;
  1904. if (status & OVRN) {
  1905. /* Overrun is special, since it's
  1906. * reported immediately, and doesn't
  1907. * affect the current character
  1908. */
  1909. over = true;
  1910. }
  1911. }
  1912. } /* end of if (error) */
  1913. if ( tty ) {
  1914. tty_insert_flip_char(tty, DataByte, flag);
  1915. if (over)
  1916. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  1917. }
  1918. }
  1919. if ( debug_level >= DEBUG_LEVEL_ISR ) {
  1920. printk("%s(%d):%s rx=%d brk=%d parity=%d frame=%d overrun=%d\n",
  1921. __FILE__,__LINE__,info->device_name,
  1922. icount->rx,icount->brk,icount->parity,
  1923. icount->frame,icount->overrun);
  1924. }
  1925. if ( tty )
  1926. tty_flip_buffer_push(tty);
  1927. }
  1928. static void isr_txeom(SLMP_INFO * info, unsigned char status)
  1929. {
  1930. if ( debug_level >= DEBUG_LEVEL_ISR )
  1931. printk("%s(%d):%s isr_txeom status=%02x\n",
  1932. __FILE__,__LINE__,info->device_name,status);
  1933. write_reg(info, TXDMA + DIR, 0x00); /* disable Tx DMA IRQs */
  1934. write_reg(info, TXDMA + DSR, 0xc0); /* clear IRQs and disable DMA */
  1935. write_reg(info, TXDMA + DCMD, SWABORT); /* reset/init DMA channel */
  1936. if (status & UDRN) {
  1937. write_reg(info, CMD, TXRESET);
  1938. write_reg(info, CMD, TXENABLE);
  1939. } else
  1940. write_reg(info, CMD, TXBUFCLR);
  1941. /* disable and clear tx interrupts */
  1942. info->ie0_value &= ~TXRDYE;
  1943. info->ie1_value &= ~(IDLE + UDRN);
  1944. write_reg16(info, IE0, (unsigned short)((info->ie1_value << 8) + info->ie0_value));
  1945. write_reg(info, SR1, (unsigned char)(UDRN + IDLE));
  1946. if ( info->tx_active ) {
  1947. if (info->params.mode != MGSL_MODE_ASYNC) {
  1948. if (status & UDRN)
  1949. info->icount.txunder++;
  1950. else if (status & IDLE)
  1951. info->icount.txok++;
  1952. }
  1953. info->tx_active = false;
  1954. info->tx_count = info->tx_put = info->tx_get = 0;
  1955. del_timer(&info->tx_timer);
  1956. if (info->params.mode != MGSL_MODE_ASYNC && info->drop_rts_on_tx_done ) {
  1957. info->serial_signals &= ~SerialSignal_RTS;
  1958. info->drop_rts_on_tx_done = false;
  1959. set_signals(info);
  1960. }
  1961. #if SYNCLINK_GENERIC_HDLC
  1962. if (info->netcount)
  1963. hdlcdev_tx_done(info);
  1964. else
  1965. #endif
  1966. {
  1967. if (info->port.tty && (info->port.tty->stopped || info->port.tty->hw_stopped)) {
  1968. tx_stop(info);
  1969. return;
  1970. }
  1971. info->pending_bh |= BH_TRANSMIT;
  1972. }
  1973. }
  1974. }
  1975. /*
  1976. * handle tx status interrupts
  1977. */
  1978. static void isr_txint(SLMP_INFO * info)
  1979. {
  1980. unsigned char status = read_reg(info, SR1) & info->ie1_value & (UDRN + IDLE + CCTS);
  1981. /* clear status bits */
  1982. write_reg(info, SR1, status);
  1983. if ( debug_level >= DEBUG_LEVEL_ISR )
  1984. printk("%s(%d):%s isr_txint status=%02x\n",
  1985. __FILE__,__LINE__,info->device_name,status);
  1986. if (status & (UDRN + IDLE))
  1987. isr_txeom(info, status);
  1988. if (status & CCTS) {
  1989. /* simulate a common modem status change interrupt
  1990. * for our handler
  1991. */
  1992. get_signals( info );
  1993. isr_io_pin(info,
  1994. MISCSTATUS_CTS_LATCHED|(info->serial_signals&SerialSignal_CTS));
  1995. }
  1996. }
  1997. /*
  1998. * handle async tx data interrupts
  1999. */
  2000. static void isr_txrdy(SLMP_INFO * info)
  2001. {
  2002. if ( debug_level >= DEBUG_LEVEL_ISR )
  2003. printk("%s(%d):%s isr_txrdy() tx_count=%d\n",
  2004. __FILE__,__LINE__,info->device_name,info->tx_count);
  2005. if (info->params.mode != MGSL_MODE_ASYNC) {
  2006. /* disable TXRDY IRQ, enable IDLE IRQ */
  2007. info->ie0_value &= ~TXRDYE;
  2008. info->ie1_value |= IDLE;
  2009. write_reg16(info, IE0, (unsigned short)((info->ie1_value << 8) + info->ie0_value));
  2010. return;
  2011. }
  2012. if (info->port.tty && (info->port.tty->stopped || info->port.tty->hw_stopped)) {
  2013. tx_stop(info);
  2014. return;
  2015. }
  2016. if ( info->tx_count )
  2017. tx_load_fifo( info );
  2018. else {
  2019. info->tx_active = false;
  2020. info->ie0_value &= ~TXRDYE;
  2021. write_reg(info, IE0, info->ie0_value);
  2022. }
  2023. if (info->tx_count < WAKEUP_CHARS)
  2024. info->pending_bh |= BH_TRANSMIT;
  2025. }
  2026. static void isr_rxdmaok(SLMP_INFO * info)
  2027. {
  2028. /* BIT7 = EOT (end of transfer)
  2029. * BIT6 = EOM (end of message/frame)
  2030. */
  2031. unsigned char status = read_reg(info,RXDMA + DSR) & 0xc0;
  2032. /* clear IRQ (BIT0 must be 1 to prevent clearing DE bit) */
  2033. write_reg(info, RXDMA + DSR, (unsigned char)(status | 1));
  2034. if ( debug_level >= DEBUG_LEVEL_ISR )
  2035. printk("%s(%d):%s isr_rxdmaok(), status=%02x\n",
  2036. __FILE__,__LINE__,info->device_name,status);
  2037. info->pending_bh |= BH_RECEIVE;
  2038. }
  2039. static void isr_rxdmaerror(SLMP_INFO * info)
  2040. {
  2041. /* BIT5 = BOF (buffer overflow)
  2042. * BIT4 = COF (counter overflow)
  2043. */
  2044. unsigned char status = read_reg(info,RXDMA + DSR) & 0x30;
  2045. /* clear IRQ (BIT0 must be 1 to prevent clearing DE bit) */
  2046. write_reg(info, RXDMA + DSR, (unsigned char)(status | 1));
  2047. if ( debug_level >= DEBUG_LEVEL_ISR )
  2048. printk("%s(%d):%s isr_rxdmaerror(), status=%02x\n",
  2049. __FILE__,__LINE__,info->device_name,status);
  2050. info->rx_overflow = true;
  2051. info->pending_bh |= BH_RECEIVE;
  2052. }
  2053. static void isr_txdmaok(SLMP_INFO * info)
  2054. {
  2055. unsigned char status_reg1 = read_reg(info, SR1);
  2056. write_reg(info, TXDMA + DIR, 0x00); /* disable Tx DMA IRQs */
  2057. write_reg(info, TXDMA + DSR, 0xc0); /* clear IRQs and disable DMA */
  2058. write_reg(info, TXDMA + DCMD, SWABORT); /* reset/init DMA channel */
  2059. if ( debug_level >= DEBUG_LEVEL_ISR )
  2060. printk("%s(%d):%s isr_txdmaok(), status=%02x\n",
  2061. __FILE__,__LINE__,info->device_name,status_reg1);
  2062. /* program TXRDY as FIFO empty flag, enable TXRDY IRQ */
  2063. write_reg16(info, TRC0, 0);
  2064. info->ie0_value |= TXRDYE;
  2065. write_reg(info, IE0, info->ie0_value);
  2066. }
  2067. static void isr_txdmaerror(SLMP_INFO * info)
  2068. {
  2069. /* BIT5 = BOF (buffer overflow)
  2070. * BIT4 = COF (counter overflow)
  2071. */
  2072. unsigned char status = read_reg(info,TXDMA + DSR) & 0x30;
  2073. /* clear IRQ (BIT0 must be 1 to prevent clearing DE bit) */
  2074. write_reg(info, TXDMA + DSR, (unsigned char)(status | 1));
  2075. if ( debug_level >= DEBUG_LEVEL_ISR )
  2076. printk("%s(%d):%s isr_txdmaerror(), status=%02x\n",
  2077. __FILE__,__LINE__,info->device_name,status);
  2078. }
  2079. /* handle input serial signal changes
  2080. */
  2081. static void isr_io_pin( SLMP_INFO *info, u16 status )
  2082. {
  2083. struct mgsl_icount *icount;
  2084. if ( debug_level >= DEBUG_LEVEL_ISR )
  2085. printk("%s(%d):isr_io_pin status=%04X\n",
  2086. __FILE__,__LINE__,status);
  2087. if (status & (MISCSTATUS_CTS_LATCHED | MISCSTATUS_DCD_LATCHED |
  2088. MISCSTATUS_DSR_LATCHED | MISCSTATUS_RI_LATCHED) ) {
  2089. icount = &info->icount;
  2090. /* update input line counters */
  2091. if (status & MISCSTATUS_RI_LATCHED) {
  2092. icount->rng++;
  2093. if ( status & SerialSignal_RI )
  2094. info->input_signal_events.ri_up++;
  2095. else
  2096. info->input_signal_events.ri_down++;
  2097. }
  2098. if (status & MISCSTATUS_DSR_LATCHED) {
  2099. icount->dsr++;
  2100. if ( status & SerialSignal_DSR )
  2101. info->input_signal_events.dsr_up++;
  2102. else
  2103. info->input_signal_events.dsr_down++;
  2104. }
  2105. if (status & MISCSTATUS_DCD_LATCHED) {
  2106. if ((info->dcd_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT) {
  2107. info->ie1_value &= ~CDCD;
  2108. write_reg(info, IE1, info->ie1_value);
  2109. }
  2110. icount->dcd++;
  2111. if (status & SerialSignal_DCD) {
  2112. info->input_signal_events.dcd_up++;
  2113. } else
  2114. info->input_signal_events.dcd_down++;
  2115. #if SYNCLINK_GENERIC_HDLC
  2116. if (info->netcount) {
  2117. if (status & SerialSignal_DCD)
  2118. netif_carrier_on(info->netdev);
  2119. else
  2120. netif_carrier_off(info->netdev);
  2121. }
  2122. #endif
  2123. }
  2124. if (status & MISCSTATUS_CTS_LATCHED)
  2125. {
  2126. if ((info->cts_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT) {
  2127. info->ie1_value &= ~CCTS;
  2128. write_reg(info, IE1, info->ie1_value);
  2129. }
  2130. icount->cts++;
  2131. if ( status & SerialSignal_CTS )
  2132. info->input_signal_events.cts_up++;
  2133. else
  2134. info->input_signal_events.cts_down++;
  2135. }
  2136. wake_up_interruptible(&info->status_event_wait_q);
  2137. wake_up_interruptible(&info->event_wait_q);
  2138. if ( (info->port.flags & ASYNC_CHECK_CD) &&
  2139. (status & MISCSTATUS_DCD_LATCHED) ) {
  2140. if ( debug_level >= DEBUG_LEVEL_ISR )
  2141. printk("%s CD now %s...", info->device_name,
  2142. (status & SerialSignal_DCD) ? "on" : "off");
  2143. if (status & SerialSignal_DCD)
  2144. wake_up_interruptible(&info->port.open_wait);
  2145. else {
  2146. if ( debug_level >= DEBUG_LEVEL_ISR )
  2147. printk("doing serial hangup...");
  2148. if (info->port.tty)
  2149. tty_hangup(info->port.tty);
  2150. }
  2151. }
  2152. if ( (info->port.flags & ASYNC_CTS_FLOW) &&
  2153. (status & MISCSTATUS_CTS_LATCHED) ) {
  2154. if ( info->port.tty ) {
  2155. if (info->port.tty->hw_stopped) {
  2156. if (status & SerialSignal_CTS) {
  2157. if ( debug_level >= DEBUG_LEVEL_ISR )
  2158. printk("CTS tx start...");
  2159. info->port.tty->hw_stopped = 0;
  2160. tx_start(info);
  2161. info->pending_bh |= BH_TRANSMIT;
  2162. return;
  2163. }
  2164. } else {
  2165. if (!(status & SerialSignal_CTS)) {
  2166. if ( debug_level >= DEBUG_LEVEL_ISR )
  2167. printk("CTS tx stop...");
  2168. info->port.tty->hw_stopped = 1;
  2169. tx_stop(info);
  2170. }
  2171. }
  2172. }
  2173. }
  2174. }
  2175. info->pending_bh |= BH_STATUS;
  2176. }
  2177. /* Interrupt service routine entry point.
  2178. *
  2179. * Arguments:
  2180. * irq interrupt number that caused interrupt
  2181. * dev_id device ID supplied during interrupt registration
  2182. * regs interrupted processor context
  2183. */
  2184. static irqreturn_t synclinkmp_interrupt(int dummy, void *dev_id)
  2185. {
  2186. SLMP_INFO *info = dev_id;
  2187. unsigned char status, status0, status1=0;
  2188. unsigned char dmastatus, dmastatus0, dmastatus1=0;
  2189. unsigned char timerstatus0, timerstatus1=0;
  2190. unsigned char shift;
  2191. unsigned int i;
  2192. unsigned short tmp;
  2193. if ( debug_level >= DEBUG_LEVEL_ISR )
  2194. printk(KERN_DEBUG "%s(%d): synclinkmp_interrupt(%d)entry.\n",
  2195. __FILE__, __LINE__, info->irq_level);
  2196. spin_lock(&info->lock);
  2197. for(;;) {
  2198. /* get status for SCA0 (ports 0-1) */
  2199. tmp = read_reg16(info, ISR0); /* get ISR0 and ISR1 in one read */
  2200. status0 = (unsigned char)tmp;
  2201. dmastatus0 = (unsigned char)(tmp>>8);
  2202. timerstatus0 = read_reg(info, ISR2);
  2203. if ( debug_level >= DEBUG_LEVEL_ISR )
  2204. printk(KERN_DEBUG "%s(%d):%s status0=%02x, dmastatus0=%02x, timerstatus0=%02x\n",
  2205. __FILE__, __LINE__, info->device_name,
  2206. status0, dmastatus0, timerstatus0);
  2207. if (info->port_count == 4) {
  2208. /* get status for SCA1 (ports 2-3) */
  2209. tmp = read_reg16(info->port_array[2], ISR0);
  2210. status1 = (unsigned char)tmp;
  2211. dmastatus1 = (unsigned char)(tmp>>8);
  2212. timerstatus1 = read_reg(info->port_array[2], ISR2);
  2213. if ( debug_level >= DEBUG_LEVEL_ISR )
  2214. printk("%s(%d):%s status1=%02x, dmastatus1=%02x, timerstatus1=%02x\n",
  2215. __FILE__,__LINE__,info->device_name,
  2216. status1,dmastatus1,timerstatus1);
  2217. }
  2218. if (!status0 && !dmastatus0 && !timerstatus0 &&
  2219. !status1 && !dmastatus1 && !timerstatus1)
  2220. break;
  2221. for(i=0; i < info->port_count ; i++) {
  2222. if (info->port_array[i] == NULL)
  2223. continue;
  2224. if (i < 2) {
  2225. status = status0;
  2226. dmastatus = dmastatus0;
  2227. } else {
  2228. status = status1;
  2229. dmastatus = dmastatus1;
  2230. }
  2231. shift = i & 1 ? 4 :0;
  2232. if (status & BIT0 << shift)
  2233. isr_rxrdy(info->port_array[i]);
  2234. if (status & BIT1 << shift)
  2235. isr_txrdy(info->port_array[i]);
  2236. if (status & BIT2 << shift)
  2237. isr_rxint(info->port_array[i]);
  2238. if (status & BIT3 << shift)
  2239. isr_txint(info->port_array[i]);
  2240. if (dmastatus & BIT0 << shift)
  2241. isr_rxdmaerror(info->port_array[i]);
  2242. if (dmastatus & BIT1 << shift)
  2243. isr_rxdmaok(info->port_array[i]);
  2244. if (dmastatus & BIT2 << shift)
  2245. isr_txdmaerror(info->port_array[i]);
  2246. if (dmastatus & BIT3 << shift)
  2247. isr_txdmaok(info->port_array[i]);
  2248. }
  2249. if (timerstatus0 & (BIT5 | BIT4))
  2250. isr_timer(info->port_array[0]);
  2251. if (timerstatus0 & (BIT7 | BIT6))
  2252. isr_timer(info->port_array[1]);
  2253. if (timerstatus1 & (BIT5 | BIT4))
  2254. isr_timer(info->port_array[2]);
  2255. if (timerstatus1 & (BIT7 | BIT6))
  2256. isr_timer(info->port_array[3]);
  2257. }
  2258. for(i=0; i < info->port_count ; i++) {
  2259. SLMP_INFO * port = info->port_array[i];
  2260. /* Request bottom half processing if there's something
  2261. * for it to do and the bh is not already running.
  2262. *
  2263. * Note: startup adapter diags require interrupts.
  2264. * do not request bottom half processing if the
  2265. * device is not open in a normal mode.
  2266. */
  2267. if ( port && (port->port.count || port->netcount) &&
  2268. port->pending_bh && !port->bh_running &&
  2269. !port->bh_requested ) {
  2270. if ( debug_level >= DEBUG_LEVEL_ISR )
  2271. printk("%s(%d):%s queueing bh task.\n",
  2272. __FILE__,__LINE__,port->device_name);
  2273. schedule_work(&port->task);
  2274. port->bh_requested = true;
  2275. }
  2276. }
  2277. spin_unlock(&info->lock);
  2278. if ( debug_level >= DEBUG_LEVEL_ISR )
  2279. printk(KERN_DEBUG "%s(%d):synclinkmp_interrupt(%d)exit.\n",
  2280. __FILE__, __LINE__, info->irq_level);
  2281. return IRQ_HANDLED;
  2282. }
  2283. /* Initialize and start device.
  2284. */
  2285. static int startup(SLMP_INFO * info)
  2286. {
  2287. if ( debug_level >= DEBUG_LEVEL_INFO )
  2288. printk("%s(%d):%s tx_releaseup()\n",__FILE__,__LINE__,info->device_name);
  2289. if (info->port.flags & ASYNC_INITIALIZED)
  2290. return 0;
  2291. if (!info->tx_buf) {
  2292. info->tx_buf = kmalloc(info->max_frame_size, GFP_KERNEL);
  2293. if (!info->tx_buf) {
  2294. printk(KERN_ERR"%s(%d):%s can't allocate transmit buffer\n",
  2295. __FILE__,__LINE__,info->device_name);
  2296. return -ENOMEM;
  2297. }
  2298. }
  2299. info->pending_bh = 0;
  2300. memset(&info->icount, 0, sizeof(info->icount));
  2301. /* program hardware for current parameters */
  2302. reset_port(info);
  2303. change_params(info);
  2304. mod_timer(&info->status_timer, jiffies + msecs_to_jiffies(10));
  2305. if (info->port.tty)
  2306. clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
  2307. info->port.flags |= ASYNC_INITIALIZED;
  2308. return 0;
  2309. }
  2310. /* Called by close() and hangup() to shutdown hardware
  2311. */
  2312. static void shutdown(SLMP_INFO * info)
  2313. {
  2314. unsigned long flags;
  2315. if (!(info->port.flags & ASYNC_INITIALIZED))
  2316. return;
  2317. if (debug_level >= DEBUG_LEVEL_INFO)
  2318. printk("%s(%d):%s synclinkmp_shutdown()\n",
  2319. __FILE__,__LINE__, info->device_name );
  2320. /* clear status wait queue because status changes */
  2321. /* can't happen after shutting down the hardware */
  2322. wake_up_interruptible(&info->status_event_wait_q);
  2323. wake_up_interruptible(&info->event_wait_q);
  2324. del_timer(&info->tx_timer);
  2325. del_timer(&info->status_timer);
  2326. kfree(info->tx_buf);
  2327. info->tx_buf = NULL;
  2328. spin_lock_irqsave(&info->lock,flags);
  2329. reset_port(info);
  2330. if (!info->port.tty || info->port.tty->termios->c_cflag & HUPCL) {
  2331. info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
  2332. set_signals(info);
  2333. }
  2334. spin_unlock_irqrestore(&info->lock,flags);
  2335. if (info->port.tty)
  2336. set_bit(TTY_IO_ERROR, &info->port.tty->flags);
  2337. info->port.flags &= ~ASYNC_INITIALIZED;
  2338. }
  2339. static void program_hw(SLMP_INFO *info)
  2340. {
  2341. unsigned long flags;
  2342. spin_lock_irqsave(&info->lock,flags);
  2343. rx_stop(info);
  2344. tx_stop(info);
  2345. info->tx_count = info->tx_put = info->tx_get = 0;
  2346. if (info->params.mode == MGSL_MODE_HDLC || info->netcount)
  2347. hdlc_mode(info);
  2348. else
  2349. async_mode(info);
  2350. set_signals(info);
  2351. info->dcd_chkcount = 0;
  2352. info->cts_chkcount = 0;
  2353. info->ri_chkcount = 0;
  2354. info->dsr_chkcount = 0;
  2355. info->ie1_value |= (CDCD|CCTS);
  2356. write_reg(info, IE1, info->ie1_value);
  2357. get_signals(info);
  2358. if (info->netcount || (info->port.tty && info->port.tty->termios->c_cflag & CREAD) )
  2359. rx_start(info);
  2360. spin_unlock_irqrestore(&info->lock,flags);
  2361. }
  2362. /* Reconfigure adapter based on new parameters
  2363. */
  2364. static void change_params(SLMP_INFO *info)
  2365. {
  2366. unsigned cflag;
  2367. int bits_per_char;
  2368. if (!info->port.tty || !info->port.tty->termios)
  2369. return;
  2370. if (debug_level >= DEBUG_LEVEL_INFO)
  2371. printk("%s(%d):%s change_params()\n",
  2372. __FILE__,__LINE__, info->device_name );
  2373. cflag = info->port.tty->termios->c_cflag;
  2374. /* if B0 rate (hangup) specified then negate DTR and RTS */
  2375. /* otherwise assert DTR and RTS */
  2376. if (cflag & CBAUD)
  2377. info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
  2378. else
  2379. info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
  2380. /* byte size and parity */
  2381. switch (cflag & CSIZE) {
  2382. case CS5: info->params.data_bits = 5; break;
  2383. case CS6: info->params.data_bits = 6; break;
  2384. case CS7: info->params.data_bits = 7; break;
  2385. case CS8: info->params.data_bits = 8; break;
  2386. /* Never happens, but GCC is too dumb to figure it out */
  2387. default: info->params.data_bits = 7; break;
  2388. }
  2389. if (cflag & CSTOPB)
  2390. info->params.stop_bits = 2;
  2391. else
  2392. info->params.stop_bits = 1;
  2393. info->params.parity = ASYNC_PARITY_NONE;
  2394. if (cflag & PARENB) {
  2395. if (cflag & PARODD)
  2396. info->params.parity = ASYNC_PARITY_ODD;
  2397. else
  2398. info->params.parity = ASYNC_PARITY_EVEN;
  2399. #ifdef CMSPAR
  2400. if (cflag & CMSPAR)
  2401. info->params.parity = ASYNC_PARITY_SPACE;
  2402. #endif
  2403. }
  2404. /* calculate number of jiffies to transmit a full
  2405. * FIFO (32 bytes) at specified data rate
  2406. */
  2407. bits_per_char = info->params.data_bits +
  2408. info->params.stop_bits + 1;
  2409. /* if port data rate is set to 460800 or less then
  2410. * allow tty settings to override, otherwise keep the
  2411. * current data rate.
  2412. */
  2413. if (info->params.data_rate <= 460800) {
  2414. info->params.data_rate = tty_get_baud_rate(info->port.tty);
  2415. }
  2416. if ( info->params.data_rate ) {
  2417. info->timeout = (32*HZ*bits_per_char) /
  2418. info->params.data_rate;
  2419. }
  2420. info->timeout += HZ/50; /* Add .02 seconds of slop */
  2421. if (cflag & CRTSCTS)
  2422. info->port.flags |= ASYNC_CTS_FLOW;
  2423. else
  2424. info->port.flags &= ~ASYNC_CTS_FLOW;
  2425. if (cflag & CLOCAL)
  2426. info->port.flags &= ~ASYNC_CHECK_CD;
  2427. else
  2428. info->port.flags |= ASYNC_CHECK_CD;
  2429. /* process tty input control flags */
  2430. info->read_status_mask2 = OVRN;
  2431. if (I_INPCK(info->port.tty))
  2432. info->read_status_mask2 |= PE | FRME;
  2433. if (I_BRKINT(info->port.tty) || I_PARMRK(info->port.tty))
  2434. info->read_status_mask1 |= BRKD;
  2435. if (I_IGNPAR(info->port.tty))
  2436. info->ignore_status_mask2 |= PE | FRME;
  2437. if (I_IGNBRK(info->port.tty)) {
  2438. info->ignore_status_mask1 |= BRKD;
  2439. /* If ignoring parity and break indicators, ignore
  2440. * overruns too. (For real raw support).
  2441. */
  2442. if (I_IGNPAR(info->port.tty))
  2443. info->ignore_status_mask2 |= OVRN;
  2444. }
  2445. program_hw(info);
  2446. }
  2447. static int get_stats(SLMP_INFO * info, struct mgsl_icount __user *user_icount)
  2448. {
  2449. int err;
  2450. if (debug_level >= DEBUG_LEVEL_INFO)
  2451. printk("%s(%d):%s get_params()\n",
  2452. __FILE__,__LINE__, info->device_name);
  2453. if (!user_icount) {
  2454. memset(&info->icount, 0, sizeof(info->icount));
  2455. } else {
  2456. COPY_TO_USER(err, user_icount, &info->icount, sizeof(struct mgsl_icount));
  2457. if (err)
  2458. return -EFAULT;
  2459. }
  2460. return 0;
  2461. }
  2462. static int get_params(SLMP_INFO * info, MGSL_PARAMS __user *user_params)
  2463. {
  2464. int err;
  2465. if (debug_level >= DEBUG_LEVEL_INFO)
  2466. printk("%s(%d):%s get_params()\n",
  2467. __FILE__,__LINE__, info->device_name);
  2468. COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS));
  2469. if (err) {
  2470. if ( debug_level >= DEBUG_LEVEL_INFO )
  2471. printk( "%s(%d):%s get_params() user buffer copy failed\n",
  2472. __FILE__,__LINE__,info->device_name);
  2473. return -EFAULT;
  2474. }
  2475. return 0;
  2476. }
  2477. static int set_params(SLMP_INFO * info, MGSL_PARAMS __user *new_params)
  2478. {
  2479. unsigned long flags;
  2480. MGSL_PARAMS tmp_params;
  2481. int err;
  2482. if (debug_level >= DEBUG_LEVEL_INFO)
  2483. printk("%s(%d):%s set_params\n",
  2484. __FILE__,__LINE__,info->device_name );
  2485. COPY_FROM_USER(err,&tmp_params, new_params, sizeof(MGSL_PARAMS));
  2486. if (err) {
  2487. if ( debug_level >= DEBUG_LEVEL_INFO )
  2488. printk( "%s(%d):%s set_params() user buffer copy failed\n",
  2489. __FILE__,__LINE__,info->device_name);
  2490. return -EFAULT;
  2491. }
  2492. spin_lock_irqsave(&info->lock,flags);
  2493. memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
  2494. spin_unlock_irqrestore(&info->lock,flags);
  2495. change_params(info);
  2496. return 0;
  2497. }
  2498. static int get_txidle(SLMP_INFO * info, int __user *idle_mode)
  2499. {
  2500. int err;
  2501. if (debug_level >= DEBUG_LEVEL_INFO)
  2502. printk("%s(%d):%s get_txidle()=%d\n",
  2503. __FILE__,__LINE__, info->device_name, info->idle_mode);
  2504. COPY_TO_USER(err,idle_mode, &info->idle_mode, sizeof(int));
  2505. if (err) {
  2506. if ( debug_level >= DEBUG_LEVEL_INFO )
  2507. printk( "%s(%d):%s get_txidle() user buffer copy failed\n",
  2508. __FILE__,__LINE__,info->device_name);
  2509. return -EFAULT;
  2510. }
  2511. return 0;
  2512. }
  2513. static int set_txidle(SLMP_INFO * info, int idle_mode)
  2514. {
  2515. unsigned long flags;
  2516. if (debug_level >= DEBUG_LEVEL_INFO)
  2517. printk("%s(%d):%s set_txidle(%d)\n",
  2518. __FILE__,__LINE__,info->device_name, idle_mode );
  2519. spin_lock_irqsave(&info->lock,flags);
  2520. info->idle_mode = idle_mode;
  2521. tx_set_idle( info );
  2522. spin_unlock_irqrestore(&info->lock,flags);
  2523. return 0;
  2524. }
  2525. static int tx_enable(SLMP_INFO * info, int enable)
  2526. {
  2527. unsigned long flags;
  2528. if (debug_level >= DEBUG_LEVEL_INFO)
  2529. printk("%s(%d):%s tx_enable(%d)\n",
  2530. __FILE__,__LINE__,info->device_name, enable);
  2531. spin_lock_irqsave(&info->lock,flags);
  2532. if ( enable ) {
  2533. if ( !info->tx_enabled ) {
  2534. tx_start(info);
  2535. }
  2536. } else {
  2537. if ( info->tx_enabled )
  2538. tx_stop(info);
  2539. }
  2540. spin_unlock_irqrestore(&info->lock,flags);
  2541. return 0;
  2542. }
  2543. /* abort send HDLC frame
  2544. */
  2545. static int tx_abort(SLMP_INFO * info)
  2546. {
  2547. unsigned long flags;
  2548. if (debug_level >= DEBUG_LEVEL_INFO)
  2549. printk("%s(%d):%s tx_abort()\n",
  2550. __FILE__,__LINE__,info->device_name);
  2551. spin_lock_irqsave(&info->lock,flags);
  2552. if ( info->tx_active && info->params.mode == MGSL_MODE_HDLC ) {
  2553. info->ie1_value &= ~UDRN;
  2554. info->ie1_value |= IDLE;
  2555. write_reg(info, IE1, info->ie1_value); /* disable tx status interrupts */
  2556. write_reg(info, SR1, (unsigned char)(IDLE + UDRN)); /* clear pending */
  2557. write_reg(info, TXDMA + DSR, 0); /* disable DMA channel */
  2558. write_reg(info, TXDMA + DCMD, SWABORT); /* reset/init DMA channel */
  2559. write_reg(info, CMD, TXABORT);
  2560. }
  2561. spin_unlock_irqrestore(&info->lock,flags);
  2562. return 0;
  2563. }
  2564. static int rx_enable(SLMP_INFO * info, int enable)
  2565. {
  2566. unsigned long flags;
  2567. if (debug_level >= DEBUG_LEVEL_INFO)
  2568. printk("%s(%d):%s rx_enable(%d)\n",
  2569. __FILE__,__LINE__,info->device_name,enable);
  2570. spin_lock_irqsave(&info->lock,flags);
  2571. if ( enable ) {
  2572. if ( !info->rx_enabled )
  2573. rx_start(info);
  2574. } else {
  2575. if ( info->rx_enabled )
  2576. rx_stop(info);
  2577. }
  2578. spin_unlock_irqrestore(&info->lock,flags);
  2579. return 0;
  2580. }
  2581. /* wait for specified event to occur
  2582. */
  2583. static int wait_mgsl_event(SLMP_INFO * info, int __user *mask_ptr)
  2584. {
  2585. unsigned long flags;
  2586. int s;
  2587. int rc=0;
  2588. struct mgsl_icount cprev, cnow;
  2589. int events;
  2590. int mask;
  2591. struct _input_signal_events oldsigs, newsigs;
  2592. DECLARE_WAITQUEUE(wait, current);
  2593. COPY_FROM_USER(rc,&mask, mask_ptr, sizeof(int));
  2594. if (rc) {
  2595. return -EFAULT;
  2596. }
  2597. if (debug_level >= DEBUG_LEVEL_INFO)
  2598. printk("%s(%d):%s wait_mgsl_event(%d)\n",
  2599. __FILE__,__LINE__,info->device_name,mask);
  2600. spin_lock_irqsave(&info->lock,flags);
  2601. /* return immediately if state matches requested events */
  2602. get_signals(info);
  2603. s = info->serial_signals;
  2604. events = mask &
  2605. ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
  2606. ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
  2607. ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
  2608. ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
  2609. if (events) {
  2610. spin_unlock_irqrestore(&info->lock,flags);
  2611. goto exit;
  2612. }
  2613. /* save current irq counts */
  2614. cprev = info->icount;
  2615. oldsigs = info->input_signal_events;
  2616. /* enable hunt and idle irqs if needed */
  2617. if (mask & (MgslEvent_ExitHuntMode+MgslEvent_IdleReceived)) {
  2618. unsigned char oldval = info->ie1_value;
  2619. unsigned char newval = oldval +
  2620. (mask & MgslEvent_ExitHuntMode ? FLGD:0) +
  2621. (mask & MgslEvent_IdleReceived ? IDLD:0);
  2622. if ( oldval != newval ) {
  2623. info->ie1_value = newval;
  2624. write_reg(info, IE1, info->ie1_value);
  2625. }
  2626. }
  2627. set_current_state(TASK_INTERRUPTIBLE);
  2628. add_wait_queue(&info->event_wait_q, &wait);
  2629. spin_unlock_irqrestore(&info->lock,flags);
  2630. for(;;) {
  2631. schedule();
  2632. if (signal_pending(current)) {
  2633. rc = -ERESTARTSYS;
  2634. break;
  2635. }
  2636. /* get current irq counts */
  2637. spin_lock_irqsave(&info->lock,flags);
  2638. cnow = info->icount;
  2639. newsigs = info->input_signal_events;
  2640. set_current_state(TASK_INTERRUPTIBLE);
  2641. spin_unlock_irqrestore(&info->lock,flags);
  2642. /* if no change, wait aborted for some reason */
  2643. if (newsigs.dsr_up == oldsigs.dsr_up &&
  2644. newsigs.dsr_down == oldsigs.dsr_down &&
  2645. newsigs.dcd_up == oldsigs.dcd_up &&
  2646. newsigs.dcd_down == oldsigs.dcd_down &&
  2647. newsigs.cts_up == oldsigs.cts_up &&
  2648. newsigs.cts_down == oldsigs.cts_down &&
  2649. newsigs.ri_up == oldsigs.ri_up &&
  2650. newsigs.ri_down == oldsigs.ri_down &&
  2651. cnow.exithunt == cprev.exithunt &&
  2652. cnow.rxidle == cprev.rxidle) {
  2653. rc = -EIO;
  2654. break;
  2655. }
  2656. events = mask &
  2657. ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
  2658. (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
  2659. (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
  2660. (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
  2661. (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
  2662. (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
  2663. (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
  2664. (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
  2665. (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
  2666. (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
  2667. if (events)
  2668. break;
  2669. cprev = cnow;
  2670. oldsigs = newsigs;
  2671. }
  2672. remove_wait_queue(&info->event_wait_q, &wait);
  2673. set_current_state(TASK_RUNNING);
  2674. if (mask & (MgslEvent_ExitHuntMode + MgslEvent_IdleReceived)) {
  2675. spin_lock_irqsave(&info->lock,flags);
  2676. if (!waitqueue_active(&info->event_wait_q)) {
  2677. /* disable enable exit hunt mode/idle rcvd IRQs */
  2678. info->ie1_value &= ~(FLGD|IDLD);
  2679. write_reg(info, IE1, info->ie1_value);
  2680. }
  2681. spin_unlock_irqrestore(&info->lock,flags);
  2682. }
  2683. exit:
  2684. if ( rc == 0 )
  2685. PUT_USER(rc, events, mask_ptr);
  2686. return rc;
  2687. }
  2688. static int modem_input_wait(SLMP_INFO *info,int arg)
  2689. {
  2690. unsigned long flags;
  2691. int rc;
  2692. struct mgsl_icount cprev, cnow;
  2693. DECLARE_WAITQUEUE(wait, current);
  2694. /* save current irq counts */
  2695. spin_lock_irqsave(&info->lock,flags);
  2696. cprev = info->icount;
  2697. add_wait_queue(&info->status_event_wait_q, &wait);
  2698. set_current_state(TASK_INTERRUPTIBLE);
  2699. spin_unlock_irqrestore(&info->lock,flags);
  2700. for(;;) {
  2701. schedule();
  2702. if (signal_pending(current)) {
  2703. rc = -ERESTARTSYS;
  2704. break;
  2705. }
  2706. /* get new irq counts */
  2707. spin_lock_irqsave(&info->lock,flags);
  2708. cnow = info->icount;
  2709. set_current_state(TASK_INTERRUPTIBLE);
  2710. spin_unlock_irqrestore(&info->lock,flags);
  2711. /* if no change, wait aborted for some reason */
  2712. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  2713. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
  2714. rc = -EIO;
  2715. break;
  2716. }
  2717. /* check for change in caller specified modem input */
  2718. if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
  2719. (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
  2720. (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
  2721. (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
  2722. rc = 0;
  2723. break;
  2724. }
  2725. cprev = cnow;
  2726. }
  2727. remove_wait_queue(&info->status_event_wait_q, &wait);
  2728. set_current_state(TASK_RUNNING);
  2729. return rc;
  2730. }
  2731. /* return the state of the serial control and status signals
  2732. */
  2733. static int tiocmget(struct tty_struct *tty, struct file *file)
  2734. {
  2735. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  2736. unsigned int result;
  2737. unsigned long flags;
  2738. spin_lock_irqsave(&info->lock,flags);
  2739. get_signals(info);
  2740. spin_unlock_irqrestore(&info->lock,flags);
  2741. result = ((info->serial_signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
  2742. ((info->serial_signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
  2743. ((info->serial_signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
  2744. ((info->serial_signals & SerialSignal_RI) ? TIOCM_RNG:0) +
  2745. ((info->serial_signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
  2746. ((info->serial_signals & SerialSignal_CTS) ? TIOCM_CTS:0);
  2747. if (debug_level >= DEBUG_LEVEL_INFO)
  2748. printk("%s(%d):%s tiocmget() value=%08X\n",
  2749. __FILE__,__LINE__, info->device_name, result );
  2750. return result;
  2751. }
  2752. /* set modem control signals (DTR/RTS)
  2753. */
  2754. static int tiocmset(struct tty_struct *tty, struct file *file,
  2755. unsigned int set, unsigned int clear)
  2756. {
  2757. SLMP_INFO *info = (SLMP_INFO *)tty->driver_data;
  2758. unsigned long flags;
  2759. if (debug_level >= DEBUG_LEVEL_INFO)
  2760. printk("%s(%d):%s tiocmset(%x,%x)\n",
  2761. __FILE__,__LINE__,info->device_name, set, clear);
  2762. if (set & TIOCM_RTS)
  2763. info->serial_signals |= SerialSignal_RTS;
  2764. if (set & TIOCM_DTR)
  2765. info->serial_signals |= SerialSignal_DTR;
  2766. if (clear & TIOCM_RTS)
  2767. info->serial_signals &= ~SerialSignal_RTS;
  2768. if (clear & TIOCM_DTR)
  2769. info->serial_signals &= ~SerialSignal_DTR;
  2770. spin_lock_irqsave(&info->lock,flags);
  2771. set_signals(info);
  2772. spin_unlock_irqrestore(&info->lock,flags);
  2773. return 0;
  2774. }
  2775. /* Block the current process until the specified port is ready to open.
  2776. */
  2777. static int block_til_ready(struct tty_struct *tty, struct file *filp,
  2778. SLMP_INFO *info)
  2779. {
  2780. DECLARE_WAITQUEUE(wait, current);
  2781. int retval;
  2782. bool do_clocal = false;
  2783. bool extra_count = false;
  2784. unsigned long flags;
  2785. if (debug_level >= DEBUG_LEVEL_INFO)
  2786. printk("%s(%d):%s block_til_ready()\n",
  2787. __FILE__,__LINE__, tty->driver->name );
  2788. if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
  2789. /* nonblock mode is set or port is not enabled */
  2790. /* just verify that callout device is not active */
  2791. info->port.flags |= ASYNC_NORMAL_ACTIVE;
  2792. return 0;
  2793. }
  2794. if (tty->termios->c_cflag & CLOCAL)
  2795. do_clocal = true;
  2796. /* Wait for carrier detect and the line to become
  2797. * free (i.e., not in use by the callout). While we are in
  2798. * this loop, info->port.count is dropped by one, so that
  2799. * close() knows when to free things. We restore it upon
  2800. * exit, either normal or abnormal.
  2801. */
  2802. retval = 0;
  2803. add_wait_queue(&info->port.open_wait, &wait);
  2804. if (debug_level >= DEBUG_LEVEL_INFO)
  2805. printk("%s(%d):%s block_til_ready() before block, count=%d\n",
  2806. __FILE__,__LINE__, tty->driver->name, info->port.count );
  2807. spin_lock_irqsave(&info->lock, flags);
  2808. if (!tty_hung_up_p(filp)) {
  2809. extra_count = true;
  2810. info->port.count--;
  2811. }
  2812. spin_unlock_irqrestore(&info->lock, flags);
  2813. info->port.blocked_open++;
  2814. while (1) {
  2815. if ((tty->termios->c_cflag & CBAUD)) {
  2816. spin_lock_irqsave(&info->lock,flags);
  2817. info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
  2818. set_signals(info);
  2819. spin_unlock_irqrestore(&info->lock,flags);
  2820. }
  2821. set_current_state(TASK_INTERRUPTIBLE);
  2822. if (tty_hung_up_p(filp) || !(info->port.flags & ASYNC_INITIALIZED)){
  2823. retval = (info->port.flags & ASYNC_HUP_NOTIFY) ?
  2824. -EAGAIN : -ERESTARTSYS;
  2825. break;
  2826. }
  2827. spin_lock_irqsave(&info->lock,flags);
  2828. get_signals(info);
  2829. spin_unlock_irqrestore(&info->lock,flags);
  2830. if (!(info->port.flags & ASYNC_CLOSING) &&
  2831. (do_clocal || (info->serial_signals & SerialSignal_DCD)) ) {
  2832. break;
  2833. }
  2834. if (signal_pending(current)) {
  2835. retval = -ERESTARTSYS;
  2836. break;
  2837. }
  2838. if (debug_level >= DEBUG_LEVEL_INFO)
  2839. printk("%s(%d):%s block_til_ready() count=%d\n",
  2840. __FILE__,__LINE__, tty->driver->name, info->port.count );
  2841. schedule();
  2842. }
  2843. set_current_state(TASK_RUNNING);
  2844. remove_wait_queue(&info->port.open_wait, &wait);
  2845. if (extra_count)
  2846. info->port.count++;
  2847. info->port.blocked_open--;
  2848. if (debug_level >= DEBUG_LEVEL_INFO)
  2849. printk("%s(%d):%s block_til_ready() after, count=%d\n",
  2850. __FILE__,__LINE__, tty->driver->name, info->port.count );
  2851. if (!retval)
  2852. info->port.flags |= ASYNC_NORMAL_ACTIVE;
  2853. return retval;
  2854. }
  2855. static int alloc_dma_bufs(SLMP_INFO *info)
  2856. {
  2857. unsigned short BuffersPerFrame;
  2858. unsigned short BufferCount;
  2859. // Force allocation to start at 64K boundary for each port.
  2860. // This is necessary because *all* buffer descriptors for a port
  2861. // *must* be in the same 64K block. All descriptors on a port
  2862. // share a common 'base' address (upper 8 bits of 24 bits) programmed
  2863. // into the CBP register.
  2864. info->port_array[0]->last_mem_alloc = (SCA_MEM_SIZE/4) * info->port_num;
  2865. /* Calculate the number of DMA buffers necessary to hold the */
  2866. /* largest allowable frame size. Note: If the max frame size is */
  2867. /* not an even multiple of the DMA buffer size then we need to */
  2868. /* round the buffer count per frame up one. */
  2869. BuffersPerFrame = (unsigned short)(info->max_frame_size/SCABUFSIZE);
  2870. if ( info->max_frame_size % SCABUFSIZE )
  2871. BuffersPerFrame++;
  2872. /* calculate total number of data buffers (SCABUFSIZE) possible
  2873. * in one ports memory (SCA_MEM_SIZE/4) after allocating memory
  2874. * for the descriptor list (BUFFERLISTSIZE).
  2875. */
  2876. BufferCount = (SCA_MEM_SIZE/4 - BUFFERLISTSIZE)/SCABUFSIZE;
  2877. /* limit number of buffers to maximum amount of descriptors */
  2878. if (BufferCount > BUFFERLISTSIZE/sizeof(SCADESC))
  2879. BufferCount = BUFFERLISTSIZE/sizeof(SCADESC);
  2880. /* use enough buffers to transmit one max size frame */
  2881. info->tx_buf_count = BuffersPerFrame + 1;
  2882. /* never use more than half the available buffers for transmit */
  2883. if (info->tx_buf_count > (BufferCount/2))
  2884. info->tx_buf_count = BufferCount/2;
  2885. if (info->tx_buf_count > SCAMAXDESC)
  2886. info->tx_buf_count = SCAMAXDESC;
  2887. /* use remaining buffers for receive */
  2888. info->rx_buf_count = BufferCount - info->tx_buf_count;
  2889. if (info->rx_buf_count > SCAMAXDESC)
  2890. info->rx_buf_count = SCAMAXDESC;
  2891. if ( debug_level >= DEBUG_LEVEL_INFO )
  2892. printk("%s(%d):%s Allocating %d TX and %d RX DMA buffers.\n",
  2893. __FILE__,__LINE__, info->device_name,
  2894. info->tx_buf_count,info->rx_buf_count);
  2895. if ( alloc_buf_list( info ) < 0 ||
  2896. alloc_frame_bufs(info,
  2897. info->rx_buf_list,
  2898. info->rx_buf_list_ex,
  2899. info->rx_buf_count) < 0 ||
  2900. alloc_frame_bufs(info,
  2901. info->tx_buf_list,
  2902. info->tx_buf_list_ex,
  2903. info->tx_buf_count) < 0 ||
  2904. alloc_tmp_rx_buf(info) < 0 ) {
  2905. printk("%s(%d):%s Can't allocate DMA buffer memory\n",
  2906. __FILE__,__LINE__, info->device_name);
  2907. return -ENOMEM;
  2908. }
  2909. rx_reset_buffers( info );
  2910. return 0;
  2911. }
  2912. /* Allocate DMA buffers for the transmit and receive descriptor lists.
  2913. */
  2914. static int alloc_buf_list(SLMP_INFO *info)
  2915. {
  2916. unsigned int i;
  2917. /* build list in adapter shared memory */
  2918. info->buffer_list = info->memory_base + info->port_array[0]->last_mem_alloc;
  2919. info->buffer_list_phys = info->port_array[0]->last_mem_alloc;
  2920. info->port_array[0]->last_mem_alloc += BUFFERLISTSIZE;
  2921. memset(info->buffer_list, 0, BUFFERLISTSIZE);
  2922. /* Save virtual address pointers to the receive and */
  2923. /* transmit buffer lists. (Receive 1st). These pointers will */
  2924. /* be used by the processor to access the lists. */
  2925. info->rx_buf_list = (SCADESC *)info->buffer_list;
  2926. info->tx_buf_list = (SCADESC *)info->buffer_list;
  2927. info->tx_buf_list += info->rx_buf_count;
  2928. /* Build links for circular buffer entry lists (tx and rx)
  2929. *
  2930. * Note: links are physical addresses read by the SCA device
  2931. * to determine the next buffer entry to use.
  2932. */
  2933. for ( i = 0; i < info->rx_buf_count; i++ ) {
  2934. /* calculate and store physical address of this buffer entry */
  2935. info->rx_buf_list_ex[i].phys_entry =
  2936. info->buffer_list_phys + (i * sizeof(SCABUFSIZE));
  2937. /* calculate and store physical address of */
  2938. /* next entry in cirular list of entries */
  2939. info->rx_buf_list[i].next = info->buffer_list_phys;
  2940. if ( i < info->rx_buf_count - 1 )
  2941. info->rx_buf_list[i].next += (i + 1) * sizeof(SCADESC);
  2942. info->rx_buf_list[i].length = SCABUFSIZE;
  2943. }
  2944. for ( i = 0; i < info->tx_buf_count; i++ ) {
  2945. /* calculate and store physical address of this buffer entry */
  2946. info->tx_buf_list_ex[i].phys_entry = info->buffer_list_phys +
  2947. ((info->rx_buf_count + i) * sizeof(SCADESC));
  2948. /* calculate and store physical address of */
  2949. /* next entry in cirular list of entries */
  2950. info->tx_buf_list[i].next = info->buffer_list_phys +
  2951. info->rx_buf_count * sizeof(SCADESC);
  2952. if ( i < info->tx_buf_count - 1 )
  2953. info->tx_buf_list[i].next += (i + 1) * sizeof(SCADESC);
  2954. }
  2955. return 0;
  2956. }
  2957. /* Allocate the frame DMA buffers used by the specified buffer list.
  2958. */
  2959. static int alloc_frame_bufs(SLMP_INFO *info, SCADESC *buf_list,SCADESC_EX *buf_list_ex,int count)
  2960. {
  2961. int i;
  2962. unsigned long phys_addr;
  2963. for ( i = 0; i < count; i++ ) {
  2964. buf_list_ex[i].virt_addr = info->memory_base + info->port_array[0]->last_mem_alloc;
  2965. phys_addr = info->port_array[0]->last_mem_alloc;
  2966. info->port_array[0]->last_mem_alloc += SCABUFSIZE;
  2967. buf_list[i].buf_ptr = (unsigned short)phys_addr;
  2968. buf_list[i].buf_base = (unsigned char)(phys_addr >> 16);
  2969. }
  2970. return 0;
  2971. }
  2972. static void free_dma_bufs(SLMP_INFO *info)
  2973. {
  2974. info->buffer_list = NULL;
  2975. info->rx_buf_list = NULL;
  2976. info->tx_buf_list = NULL;
  2977. }
  2978. /* allocate buffer large enough to hold max_frame_size.
  2979. * This buffer is used to pass an assembled frame to the line discipline.
  2980. */
  2981. static int alloc_tmp_rx_buf(SLMP_INFO *info)
  2982. {
  2983. info->tmp_rx_buf = kmalloc(info->max_frame_size, GFP_KERNEL);
  2984. if (info->tmp_rx_buf == NULL)
  2985. return -ENOMEM;
  2986. return 0;
  2987. }
  2988. static void free_tmp_rx_buf(SLMP_INFO *info)
  2989. {
  2990. kfree(info->tmp_rx_buf);
  2991. info->tmp_rx_buf = NULL;
  2992. }
  2993. static int claim_resources(SLMP_INFO *info)
  2994. {
  2995. if (request_mem_region(info->phys_memory_base,SCA_MEM_SIZE,"synclinkmp") == NULL) {
  2996. printk( "%s(%d):%s mem addr conflict, Addr=%08X\n",
  2997. __FILE__,__LINE__,info->device_name, info->phys_memory_base);
  2998. info->init_error = DiagStatus_AddressConflict;
  2999. goto errout;
  3000. }
  3001. else
  3002. info->shared_mem_requested = true;
  3003. if (request_mem_region(info->phys_lcr_base + info->lcr_offset,128,"synclinkmp") == NULL) {
  3004. printk( "%s(%d):%s lcr mem addr conflict, Addr=%08X\n",
  3005. __FILE__,__LINE__,info->device_name, info->phys_lcr_base);
  3006. info->init_error = DiagStatus_AddressConflict;
  3007. goto errout;
  3008. }
  3009. else
  3010. info->lcr_mem_requested = true;
  3011. if (request_mem_region(info->phys_sca_base + info->sca_offset,SCA_BASE_SIZE,"synclinkmp") == NULL) {
  3012. printk( "%s(%d):%s sca mem addr conflict, Addr=%08X\n",
  3013. __FILE__,__LINE__,info->device_name, info->phys_sca_base);
  3014. info->init_error = DiagStatus_AddressConflict;
  3015. goto errout;
  3016. }
  3017. else
  3018. info->sca_base_requested = true;
  3019. if (request_mem_region(info->phys_statctrl_base + info->statctrl_offset,SCA_REG_SIZE,"synclinkmp") == NULL) {
  3020. printk( "%s(%d):%s stat/ctrl mem addr conflict, Addr=%08X\n",
  3021. __FILE__,__LINE__,info->device_name, info->phys_statctrl_base);
  3022. info->init_error = DiagStatus_AddressConflict;
  3023. goto errout;
  3024. }
  3025. else
  3026. info->sca_statctrl_requested = true;
  3027. info->memory_base = ioremap_nocache(info->phys_memory_base,
  3028. SCA_MEM_SIZE);
  3029. if (!info->memory_base) {
  3030. printk( "%s(%d):%s Cant map shared memory, MemAddr=%08X\n",
  3031. __FILE__,__LINE__,info->device_name, info->phys_memory_base );
  3032. info->init_error = DiagStatus_CantAssignPciResources;
  3033. goto errout;
  3034. }
  3035. info->lcr_base = ioremap_nocache(info->phys_lcr_base, PAGE_SIZE);
  3036. if (!info->lcr_base) {
  3037. printk( "%s(%d):%s Cant map LCR memory, MemAddr=%08X\n",
  3038. __FILE__,__LINE__,info->device_name, info->phys_lcr_base );
  3039. info->init_error = DiagStatus_CantAssignPciResources;
  3040. goto errout;
  3041. }
  3042. info->lcr_base += info->lcr_offset;
  3043. info->sca_base = ioremap_nocache(info->phys_sca_base, PAGE_SIZE);
  3044. if (!info->sca_base) {
  3045. printk( "%s(%d):%s Cant map SCA memory, MemAddr=%08X\n",
  3046. __FILE__,__LINE__,info->device_name, info->phys_sca_base );
  3047. info->init_error = DiagStatus_CantAssignPciResources;
  3048. goto errout;
  3049. }
  3050. info->sca_base += info->sca_offset;
  3051. info->statctrl_base = ioremap_nocache(info->phys_statctrl_base,
  3052. PAGE_SIZE);
  3053. if (!info->statctrl_base) {
  3054. printk( "%s(%d):%s Cant map SCA Status/Control memory, MemAddr=%08X\n",
  3055. __FILE__,__LINE__,info->device_name, info->phys_statctrl_base );
  3056. info->init_error = DiagStatus_CantAssignPciResources;
  3057. goto errout;
  3058. }
  3059. info->statctrl_base += info->statctrl_offset;
  3060. if ( !memory_test(info) ) {
  3061. printk( "%s(%d):Shared Memory Test failed for device %s MemAddr=%08X\n",
  3062. __FILE__,__LINE__,info->device_name, info->phys_memory_base );
  3063. info->init_error = DiagStatus_MemoryError;
  3064. goto errout;
  3065. }
  3066. return 0;
  3067. errout:
  3068. release_resources( info );
  3069. return -ENODEV;
  3070. }
  3071. static void release_resources(SLMP_INFO *info)
  3072. {
  3073. if ( debug_level >= DEBUG_LEVEL_INFO )
  3074. printk( "%s(%d):%s release_resources() entry\n",
  3075. __FILE__,__LINE__,info->device_name );
  3076. if ( info->irq_requested ) {
  3077. free_irq(info->irq_level, info);
  3078. info->irq_requested = false;
  3079. }
  3080. if ( info->shared_mem_requested ) {
  3081. release_mem_region(info->phys_memory_base,SCA_MEM_SIZE);
  3082. info->shared_mem_requested = false;
  3083. }
  3084. if ( info->lcr_mem_requested ) {
  3085. release_mem_region(info->phys_lcr_base + info->lcr_offset,128);
  3086. info->lcr_mem_requested = false;
  3087. }
  3088. if ( info->sca_base_requested ) {
  3089. release_mem_region(info->phys_sca_base + info->sca_offset,SCA_BASE_SIZE);
  3090. info->sca_base_requested = false;
  3091. }
  3092. if ( info->sca_statctrl_requested ) {
  3093. release_mem_region(info->phys_statctrl_base + info->statctrl_offset,SCA_REG_SIZE);
  3094. info->sca_statctrl_requested = false;
  3095. }
  3096. if (info->memory_base){
  3097. iounmap(info->memory_base);
  3098. info->memory_base = NULL;
  3099. }
  3100. if (info->sca_base) {
  3101. iounmap(info->sca_base - info->sca_offset);
  3102. info->sca_base=NULL;
  3103. }
  3104. if (info->statctrl_base) {
  3105. iounmap(info->statctrl_base - info->statctrl_offset);
  3106. info->statctrl_base=NULL;
  3107. }
  3108. if (info->lcr_base){
  3109. iounmap(info->lcr_base - info->lcr_offset);
  3110. info->lcr_base = NULL;
  3111. }
  3112. if ( debug_level >= DEBUG_LEVEL_INFO )
  3113. printk( "%s(%d):%s release_resources() exit\n",
  3114. __FILE__,__LINE__,info->device_name );
  3115. }
  3116. /* Add the specified device instance data structure to the
  3117. * global linked list of devices and increment the device count.
  3118. */
  3119. static void add_device(SLMP_INFO *info)
  3120. {
  3121. info->next_device = NULL;
  3122. info->line = synclinkmp_device_count;
  3123. sprintf(info->device_name,"ttySLM%dp%d",info->adapter_num,info->port_num);
  3124. if (info->line < MAX_DEVICES) {
  3125. if (maxframe[info->line])
  3126. info->max_frame_size = maxframe[info->line];
  3127. info->dosyncppp = dosyncppp[info->line];
  3128. }
  3129. synclinkmp_device_count++;
  3130. if ( !synclinkmp_device_list )
  3131. synclinkmp_device_list = info;
  3132. else {
  3133. SLMP_INFO *current_dev = synclinkmp_device_list;
  3134. while( current_dev->next_device )
  3135. current_dev = current_dev->next_device;
  3136. current_dev->next_device = info;
  3137. }
  3138. if ( info->max_frame_size < 4096 )
  3139. info->max_frame_size = 4096;
  3140. else if ( info->max_frame_size > 65535 )
  3141. info->max_frame_size = 65535;
  3142. printk( "SyncLink MultiPort %s: "
  3143. "Mem=(%08x %08X %08x %08X) IRQ=%d MaxFrameSize=%u\n",
  3144. info->device_name,
  3145. info->phys_sca_base,
  3146. info->phys_memory_base,
  3147. info->phys_statctrl_base,
  3148. info->phys_lcr_base,
  3149. info->irq_level,
  3150. info->max_frame_size );
  3151. #if SYNCLINK_GENERIC_HDLC
  3152. hdlcdev_init(info);
  3153. #endif
  3154. }
  3155. /* Allocate and initialize a device instance structure
  3156. *
  3157. * Return Value: pointer to SLMP_INFO if success, otherwise NULL
  3158. */
  3159. static SLMP_INFO *alloc_dev(int adapter_num, int port_num, struct pci_dev *pdev)
  3160. {
  3161. SLMP_INFO *info;
  3162. info = kzalloc(sizeof(SLMP_INFO),
  3163. GFP_KERNEL);
  3164. if (!info) {
  3165. printk("%s(%d) Error can't allocate device instance data for adapter %d, port %d\n",
  3166. __FILE__,__LINE__, adapter_num, port_num);
  3167. } else {
  3168. tty_port_init(&info->port);
  3169. info->magic = MGSL_MAGIC;
  3170. INIT_WORK(&info->task, bh_handler);
  3171. info->max_frame_size = 4096;
  3172. info->port.close_delay = 5*HZ/10;
  3173. info->port.closing_wait = 30*HZ;
  3174. init_waitqueue_head(&info->status_event_wait_q);
  3175. init_waitqueue_head(&info->event_wait_q);
  3176. spin_lock_init(&info->netlock);
  3177. memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
  3178. info->idle_mode = HDLC_TXIDLE_FLAGS;
  3179. info->adapter_num = adapter_num;
  3180. info->port_num = port_num;
  3181. /* Copy configuration info to device instance data */
  3182. info->irq_level = pdev->irq;
  3183. info->phys_lcr_base = pci_resource_start(pdev,0);
  3184. info->phys_sca_base = pci_resource_start(pdev,2);
  3185. info->phys_memory_base = pci_resource_start(pdev,3);
  3186. info->phys_statctrl_base = pci_resource_start(pdev,4);
  3187. /* Because veremap only works on page boundaries we must map
  3188. * a larger area than is actually implemented for the LCR
  3189. * memory range. We map a full page starting at the page boundary.
  3190. */
  3191. info->lcr_offset = info->phys_lcr_base & (PAGE_SIZE-1);
  3192. info->phys_lcr_base &= ~(PAGE_SIZE-1);
  3193. info->sca_offset = info->phys_sca_base & (PAGE_SIZE-1);
  3194. info->phys_sca_base &= ~(PAGE_SIZE-1);
  3195. info->statctrl_offset = info->phys_statctrl_base & (PAGE_SIZE-1);
  3196. info->phys_statctrl_base &= ~(PAGE_SIZE-1);
  3197. info->bus_type = MGSL_BUS_TYPE_PCI;
  3198. info->irq_flags = IRQF_SHARED;
  3199. setup_timer(&info->tx_timer, tx_timeout, (unsigned long)info);
  3200. setup_timer(&info->status_timer, status_timeout,
  3201. (unsigned long)info);
  3202. /* Store the PCI9050 misc control register value because a flaw
  3203. * in the PCI9050 prevents LCR registers from being read if
  3204. * BIOS assigns an LCR base address with bit 7 set.
  3205. *
  3206. * Only the misc control register is accessed for which only
  3207. * write access is needed, so set an initial value and change
  3208. * bits to the device instance data as we write the value
  3209. * to the actual misc control register.
  3210. */
  3211. info->misc_ctrl_value = 0x087e4546;
  3212. /* initial port state is unknown - if startup errors
  3213. * occur, init_error will be set to indicate the
  3214. * problem. Once the port is fully initialized,
  3215. * this value will be set to 0 to indicate the
  3216. * port is available.
  3217. */
  3218. info->init_error = -1;
  3219. }
  3220. return info;
  3221. }
  3222. static void device_init(int adapter_num, struct pci_dev *pdev)
  3223. {
  3224. SLMP_INFO *port_array[SCA_MAX_PORTS];
  3225. int port;
  3226. /* allocate device instances for up to SCA_MAX_PORTS devices */
  3227. for ( port = 0; port < SCA_MAX_PORTS; ++port ) {
  3228. port_array[port] = alloc_dev(adapter_num,port,pdev);
  3229. if( port_array[port] == NULL ) {
  3230. for ( --port; port >= 0; --port )
  3231. kfree(port_array[port]);
  3232. return;
  3233. }
  3234. }
  3235. /* give copy of port_array to all ports and add to device list */
  3236. for ( port = 0; port < SCA_MAX_PORTS; ++port ) {
  3237. memcpy(port_array[port]->port_array,port_array,sizeof(port_array));
  3238. add_device( port_array[port] );
  3239. spin_lock_init(&port_array[port]->lock);
  3240. }
  3241. /* Allocate and claim adapter resources */
  3242. if ( !claim_resources(port_array[0]) ) {
  3243. alloc_dma_bufs(port_array[0]);
  3244. /* copy resource information from first port to others */
  3245. for ( port = 1; port < SCA_MAX_PORTS; ++port ) {
  3246. port_array[port]->lock = port_array[0]->lock;
  3247. port_array[port]->irq_level = port_array[0]->irq_level;
  3248. port_array[port]->memory_base = port_array[0]->memory_base;
  3249. port_array[port]->sca_base = port_array[0]->sca_base;
  3250. port_array[port]->statctrl_base = port_array[0]->statctrl_base;
  3251. port_array[port]->lcr_base = port_array[0]->lcr_base;
  3252. alloc_dma_bufs(port_array[port]);
  3253. }
  3254. if ( request_irq(port_array[0]->irq_level,
  3255. synclinkmp_interrupt,
  3256. port_array[0]->irq_flags,
  3257. port_array[0]->device_name,
  3258. port_array[0]) < 0 ) {
  3259. printk( "%s(%d):%s Cant request interrupt, IRQ=%d\n",
  3260. __FILE__,__LINE__,
  3261. port_array[0]->device_name,
  3262. port_array[0]->irq_level );
  3263. }
  3264. else {
  3265. port_array[0]->irq_requested = true;
  3266. adapter_test(port_array[0]);
  3267. }
  3268. }
  3269. }
  3270. static const struct tty_operations ops = {
  3271. .open = open,
  3272. .close = close,
  3273. .write = write,
  3274. .put_char = put_char,
  3275. .flush_chars = flush_chars,
  3276. .write_room = write_room,
  3277. .chars_in_buffer = chars_in_buffer,
  3278. .flush_buffer = flush_buffer,
  3279. .ioctl = ioctl,
  3280. .throttle = throttle,
  3281. .unthrottle = unthrottle,
  3282. .send_xchar = send_xchar,
  3283. .break_ctl = set_break,
  3284. .wait_until_sent = wait_until_sent,
  3285. .read_proc = read_proc,
  3286. .set_termios = set_termios,
  3287. .stop = tx_hold,
  3288. .start = tx_release,
  3289. .hangup = hangup,
  3290. .tiocmget = tiocmget,
  3291. .tiocmset = tiocmset,
  3292. };
  3293. static void synclinkmp_cleanup(void)
  3294. {
  3295. int rc;
  3296. SLMP_INFO *info;
  3297. SLMP_INFO *tmp;
  3298. printk("Unloading %s %s\n", driver_name, driver_version);
  3299. if (serial_driver) {
  3300. if ((rc = tty_unregister_driver(serial_driver)))
  3301. printk("%s(%d) failed to unregister tty driver err=%d\n",
  3302. __FILE__,__LINE__,rc);
  3303. put_tty_driver(serial_driver);
  3304. }
  3305. /* reset devices */
  3306. info = synclinkmp_device_list;
  3307. while(info) {
  3308. reset_port(info);
  3309. info = info->next_device;
  3310. }
  3311. /* release devices */
  3312. info = synclinkmp_device_list;
  3313. while(info) {
  3314. #if SYNCLINK_GENERIC_HDLC
  3315. hdlcdev_exit(info);
  3316. #endif
  3317. free_dma_bufs(info);
  3318. free_tmp_rx_buf(info);
  3319. if ( info->port_num == 0 ) {
  3320. if (info->sca_base)
  3321. write_reg(info, LPR, 1); /* set low power mode */
  3322. release_resources(info);
  3323. }
  3324. tmp = info;
  3325. info = info->next_device;
  3326. kfree(tmp);
  3327. }
  3328. pci_unregister_driver(&synclinkmp_pci_driver);
  3329. }
  3330. /* Driver initialization entry point.
  3331. */
  3332. static int __init synclinkmp_init(void)
  3333. {
  3334. int rc;
  3335. if (break_on_load) {
  3336. synclinkmp_get_text_ptr();
  3337. BREAKPOINT();
  3338. }
  3339. printk("%s %s\n", driver_name, driver_version);
  3340. if ((rc = pci_register_driver(&synclinkmp_pci_driver)) < 0) {
  3341. printk("%s:failed to register PCI driver, error=%d\n",__FILE__,rc);
  3342. return rc;
  3343. }
  3344. serial_driver = alloc_tty_driver(128);
  3345. if (!serial_driver) {
  3346. rc = -ENOMEM;
  3347. goto error;
  3348. }
  3349. /* Initialize the tty_driver structure */
  3350. serial_driver->owner = THIS_MODULE;
  3351. serial_driver->driver_name = "synclinkmp";
  3352. serial_driver->name = "ttySLM";
  3353. serial_driver->major = ttymajor;
  3354. serial_driver->minor_start = 64;
  3355. serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  3356. serial_driver->subtype = SERIAL_TYPE_NORMAL;
  3357. serial_driver->init_termios = tty_std_termios;
  3358. serial_driver->init_termios.c_cflag =
  3359. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  3360. serial_driver->init_termios.c_ispeed = 9600;
  3361. serial_driver->init_termios.c_ospeed = 9600;
  3362. serial_driver->flags = TTY_DRIVER_REAL_RAW;
  3363. tty_set_operations(serial_driver, &ops);
  3364. if ((rc = tty_register_driver(serial_driver)) < 0) {
  3365. printk("%s(%d):Couldn't register serial driver\n",
  3366. __FILE__,__LINE__);
  3367. put_tty_driver(serial_driver);
  3368. serial_driver = NULL;
  3369. goto error;
  3370. }
  3371. printk("%s %s, tty major#%d\n",
  3372. driver_name, driver_version,
  3373. serial_driver->major);
  3374. return 0;
  3375. error:
  3376. synclinkmp_cleanup();
  3377. return rc;
  3378. }
  3379. static void __exit synclinkmp_exit(void)
  3380. {
  3381. synclinkmp_cleanup();
  3382. }
  3383. module_init(synclinkmp_init);
  3384. module_exit(synclinkmp_exit);
  3385. /* Set the port for internal loopback mode.
  3386. * The TxCLK and RxCLK signals are generated from the BRG and
  3387. * the TxD is looped back to the RxD internally.
  3388. */
  3389. static void enable_loopback(SLMP_INFO *info, int enable)
  3390. {
  3391. if (enable) {
  3392. /* MD2 (Mode Register 2)
  3393. * 01..00 CNCT<1..0> Channel Connection 11=Local Loopback
  3394. */
  3395. write_reg(info, MD2, (unsigned char)(read_reg(info, MD2) | (BIT1 + BIT0)));
  3396. /* degate external TxC clock source */
  3397. info->port_array[0]->ctrlreg_value |= (BIT0 << (info->port_num * 2));
  3398. write_control_reg(info);
  3399. /* RXS/TXS (Rx/Tx clock source)
  3400. * 07 Reserved, must be 0
  3401. * 06..04 Clock Source, 100=BRG
  3402. * 03..00 Clock Divisor, 0000=1
  3403. */
  3404. write_reg(info, RXS, 0x40);
  3405. write_reg(info, TXS, 0x40);
  3406. } else {
  3407. /* MD2 (Mode Register 2)
  3408. * 01..00 CNCT<1..0> Channel connection, 0=normal
  3409. */
  3410. write_reg(info, MD2, (unsigned char)(read_reg(info, MD2) & ~(BIT1 + BIT0)));
  3411. /* RXS/TXS (Rx/Tx clock source)
  3412. * 07 Reserved, must be 0
  3413. * 06..04 Clock Source, 000=RxC/TxC Pin
  3414. * 03..00 Clock Divisor, 0000=1
  3415. */
  3416. write_reg(info, RXS, 0x00);
  3417. write_reg(info, TXS, 0x00);
  3418. }
  3419. /* set LinkSpeed if available, otherwise default to 2Mbps */
  3420. if (info->params.clock_speed)
  3421. set_rate(info, info->params.clock_speed);
  3422. else
  3423. set_rate(info, 3686400);
  3424. }
  3425. /* Set the baud rate register to the desired speed
  3426. *
  3427. * data_rate data rate of clock in bits per second
  3428. * A data rate of 0 disables the AUX clock.
  3429. */
  3430. static void set_rate( SLMP_INFO *info, u32 data_rate )
  3431. {
  3432. u32 TMCValue;
  3433. unsigned char BRValue;
  3434. u32 Divisor=0;
  3435. /* fBRG = fCLK/(TMC * 2^BR)
  3436. */
  3437. if (data_rate != 0) {
  3438. Divisor = 14745600/data_rate;
  3439. if (!Divisor)
  3440. Divisor = 1;
  3441. TMCValue = Divisor;
  3442. BRValue = 0;
  3443. if (TMCValue != 1 && TMCValue != 2) {
  3444. /* BRValue of 0 provides 50/50 duty cycle *only* when
  3445. * TMCValue is 1 or 2. BRValue of 1 to 9 always provides
  3446. * 50/50 duty cycle.
  3447. */
  3448. BRValue = 1;
  3449. TMCValue >>= 1;
  3450. }
  3451. /* while TMCValue is too big for TMC register, divide
  3452. * by 2 and increment BR exponent.
  3453. */
  3454. for(; TMCValue > 256 && BRValue < 10; BRValue++)
  3455. TMCValue >>= 1;
  3456. write_reg(info, TXS,
  3457. (unsigned char)((read_reg(info, TXS) & 0xf0) | BRValue));
  3458. write_reg(info, RXS,
  3459. (unsigned char)((read_reg(info, RXS) & 0xf0) | BRValue));
  3460. write_reg(info, TMC, (unsigned char)TMCValue);
  3461. }
  3462. else {
  3463. write_reg(info, TXS,0);
  3464. write_reg(info, RXS,0);
  3465. write_reg(info, TMC, 0);
  3466. }
  3467. }
  3468. /* Disable receiver
  3469. */
  3470. static void rx_stop(SLMP_INFO *info)
  3471. {
  3472. if (debug_level >= DEBUG_LEVEL_ISR)
  3473. printk("%s(%d):%s rx_stop()\n",
  3474. __FILE__,__LINE__, info->device_name );
  3475. write_reg(info, CMD, RXRESET);
  3476. info->ie0_value &= ~RXRDYE;
  3477. write_reg(info, IE0, info->ie0_value); /* disable Rx data interrupts */
  3478. write_reg(info, RXDMA + DSR, 0); /* disable Rx DMA */
  3479. write_reg(info, RXDMA + DCMD, SWABORT); /* reset/init Rx DMA */
  3480. write_reg(info, RXDMA + DIR, 0); /* disable Rx DMA interrupts */
  3481. info->rx_enabled = false;
  3482. info->rx_overflow = false;
  3483. }
  3484. /* enable the receiver
  3485. */
  3486. static void rx_start(SLMP_INFO *info)
  3487. {
  3488. int i;
  3489. if (debug_level >= DEBUG_LEVEL_ISR)
  3490. printk("%s(%d):%s rx_start()\n",
  3491. __FILE__,__LINE__, info->device_name );
  3492. write_reg(info, CMD, RXRESET);
  3493. if ( info->params.mode == MGSL_MODE_HDLC ) {
  3494. /* HDLC, disabe IRQ on rxdata */
  3495. info->ie0_value &= ~RXRDYE;
  3496. write_reg(info, IE0, info->ie0_value);
  3497. /* Reset all Rx DMA buffers and program rx dma */
  3498. write_reg(info, RXDMA + DSR, 0); /* disable Rx DMA */
  3499. write_reg(info, RXDMA + DCMD, SWABORT); /* reset/init Rx DMA */
  3500. for (i = 0; i < info->rx_buf_count; i++) {
  3501. info->rx_buf_list[i].status = 0xff;
  3502. // throttle to 4 shared memory writes at a time to prevent
  3503. // hogging local bus (keep latency time for DMA requests low).
  3504. if (!(i % 4))
  3505. read_status_reg(info);
  3506. }
  3507. info->current_rx_buf = 0;
  3508. /* set current/1st descriptor address */
  3509. write_reg16(info, RXDMA + CDA,
  3510. info->rx_buf_list_ex[0].phys_entry);
  3511. /* set new last rx descriptor address */
  3512. write_reg16(info, RXDMA + EDA,
  3513. info->rx_buf_list_ex[info->rx_buf_count - 1].phys_entry);
  3514. /* set buffer length (shared by all rx dma data buffers) */
  3515. write_reg16(info, RXDMA + BFL, SCABUFSIZE);
  3516. write_reg(info, RXDMA + DIR, 0x60); /* enable Rx DMA interrupts (EOM/BOF) */
  3517. write_reg(info, RXDMA + DSR, 0xf2); /* clear Rx DMA IRQs, enable Rx DMA */
  3518. } else {
  3519. /* async, enable IRQ on rxdata */
  3520. info->ie0_value |= RXRDYE;
  3521. write_reg(info, IE0, info->ie0_value);
  3522. }
  3523. write_reg(info, CMD, RXENABLE);
  3524. info->rx_overflow = false;
  3525. info->rx_enabled = true;
  3526. }
  3527. /* Enable the transmitter and send a transmit frame if
  3528. * one is loaded in the DMA buffers.
  3529. */
  3530. static void tx_start(SLMP_INFO *info)
  3531. {
  3532. if (debug_level >= DEBUG_LEVEL_ISR)
  3533. printk("%s(%d):%s tx_start() tx_count=%d\n",
  3534. __FILE__,__LINE__, info->device_name,info->tx_count );
  3535. if (!info->tx_enabled ) {
  3536. write_reg(info, CMD, TXRESET);
  3537. write_reg(info, CMD, TXENABLE);
  3538. info->tx_enabled = true;
  3539. }
  3540. if ( info->tx_count ) {
  3541. /* If auto RTS enabled and RTS is inactive, then assert */
  3542. /* RTS and set a flag indicating that the driver should */
  3543. /* negate RTS when the transmission completes. */
  3544. info->drop_rts_on_tx_done = false;
  3545. if (info->params.mode != MGSL_MODE_ASYNC) {
  3546. if ( info->params.flags & HDLC_FLAG_AUTO_RTS ) {
  3547. get_signals( info );
  3548. if ( !(info->serial_signals & SerialSignal_RTS) ) {
  3549. info->serial_signals |= SerialSignal_RTS;
  3550. set_signals( info );
  3551. info->drop_rts_on_tx_done = true;
  3552. }
  3553. }
  3554. write_reg16(info, TRC0,
  3555. (unsigned short)(((tx_negate_fifo_level-1)<<8) + tx_active_fifo_level));
  3556. write_reg(info, TXDMA + DSR, 0); /* disable DMA channel */
  3557. write_reg(info, TXDMA + DCMD, SWABORT); /* reset/init DMA channel */
  3558. /* set TX CDA (current descriptor address) */
  3559. write_reg16(info, TXDMA + CDA,
  3560. info->tx_buf_list_ex[0].phys_entry);
  3561. /* set TX EDA (last descriptor address) */
  3562. write_reg16(info, TXDMA + EDA,
  3563. info->tx_buf_list_ex[info->last_tx_buf].phys_entry);
  3564. /* enable underrun IRQ */
  3565. info->ie1_value &= ~IDLE;
  3566. info->ie1_value |= UDRN;
  3567. write_reg(info, IE1, info->ie1_value);
  3568. write_reg(info, SR1, (unsigned char)(IDLE + UDRN));
  3569. write_reg(info, TXDMA + DIR, 0x40); /* enable Tx DMA interrupts (EOM) */
  3570. write_reg(info, TXDMA + DSR, 0xf2); /* clear Tx DMA IRQs, enable Tx DMA */
  3571. mod_timer(&info->tx_timer, jiffies +
  3572. msecs_to_jiffies(5000));
  3573. }
  3574. else {
  3575. tx_load_fifo(info);
  3576. /* async, enable IRQ on txdata */
  3577. info->ie0_value |= TXRDYE;
  3578. write_reg(info, IE0, info->ie0_value);
  3579. }
  3580. info->tx_active = true;
  3581. }
  3582. }
  3583. /* stop the transmitter and DMA
  3584. */
  3585. static void tx_stop( SLMP_INFO *info )
  3586. {
  3587. if (debug_level >= DEBUG_LEVEL_ISR)
  3588. printk("%s(%d):%s tx_stop()\n",
  3589. __FILE__,__LINE__, info->device_name );
  3590. del_timer(&info->tx_timer);
  3591. write_reg(info, TXDMA + DSR, 0); /* disable DMA channel */
  3592. write_reg(info, TXDMA + DCMD, SWABORT); /* reset/init DMA channel */
  3593. write_reg(info, CMD, TXRESET);
  3594. info->ie1_value &= ~(UDRN + IDLE);
  3595. write_reg(info, IE1, info->ie1_value); /* disable tx status interrupts */
  3596. write_reg(info, SR1, (unsigned char)(IDLE + UDRN)); /* clear pending */
  3597. info->ie0_value &= ~TXRDYE;
  3598. write_reg(info, IE0, info->ie0_value); /* disable tx data interrupts */
  3599. info->tx_enabled = false;
  3600. info->tx_active = false;
  3601. }
  3602. /* Fill the transmit FIFO until the FIFO is full or
  3603. * there is no more data to load.
  3604. */
  3605. static void tx_load_fifo(SLMP_INFO *info)
  3606. {
  3607. u8 TwoBytes[2];
  3608. /* do nothing is now tx data available and no XON/XOFF pending */
  3609. if ( !info->tx_count && !info->x_char )
  3610. return;
  3611. /* load the Transmit FIFO until FIFOs full or all data sent */
  3612. while( info->tx_count && (read_reg(info,SR0) & BIT1) ) {
  3613. /* there is more space in the transmit FIFO and */
  3614. /* there is more data in transmit buffer */
  3615. if ( (info->tx_count > 1) && !info->x_char ) {
  3616. /* write 16-bits */
  3617. TwoBytes[0] = info->tx_buf[info->tx_get++];
  3618. if (info->tx_get >= info->max_frame_size)
  3619. info->tx_get -= info->max_frame_size;
  3620. TwoBytes[1] = info->tx_buf[info->tx_get++];
  3621. if (info->tx_get >= info->max_frame_size)
  3622. info->tx_get -= info->max_frame_size;
  3623. write_reg16(info, TRB, *((u16 *)TwoBytes));
  3624. info->tx_count -= 2;
  3625. info->icount.tx += 2;
  3626. } else {
  3627. /* only 1 byte left to transmit or 1 FIFO slot left */
  3628. if (info->x_char) {
  3629. /* transmit pending high priority char */
  3630. write_reg(info, TRB, info->x_char);
  3631. info->x_char = 0;
  3632. } else {
  3633. write_reg(info, TRB, info->tx_buf[info->tx_get++]);
  3634. if (info->tx_get >= info->max_frame_size)
  3635. info->tx_get -= info->max_frame_size;
  3636. info->tx_count--;
  3637. }
  3638. info->icount.tx++;
  3639. }
  3640. }
  3641. }
  3642. /* Reset a port to a known state
  3643. */
  3644. static void reset_port(SLMP_INFO *info)
  3645. {
  3646. if (info->sca_base) {
  3647. tx_stop(info);
  3648. rx_stop(info);
  3649. info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
  3650. set_signals(info);
  3651. /* disable all port interrupts */
  3652. info->ie0_value = 0;
  3653. info->ie1_value = 0;
  3654. info->ie2_value = 0;
  3655. write_reg(info, IE0, info->ie0_value);
  3656. write_reg(info, IE1, info->ie1_value);
  3657. write_reg(info, IE2, info->ie2_value);
  3658. write_reg(info, CMD, CHRESET);
  3659. }
  3660. }
  3661. /* Reset all the ports to a known state.
  3662. */
  3663. static void reset_adapter(SLMP_INFO *info)
  3664. {
  3665. int i;
  3666. for ( i=0; i < SCA_MAX_PORTS; ++i) {
  3667. if (info->port_array[i])
  3668. reset_port(info->port_array[i]);
  3669. }
  3670. }
  3671. /* Program port for asynchronous communications.
  3672. */
  3673. static void async_mode(SLMP_INFO *info)
  3674. {
  3675. unsigned char RegValue;
  3676. tx_stop(info);
  3677. rx_stop(info);
  3678. /* MD0, Mode Register 0
  3679. *
  3680. * 07..05 PRCTL<2..0>, Protocol Mode, 000=async
  3681. * 04 AUTO, Auto-enable (RTS/CTS/DCD)
  3682. * 03 Reserved, must be 0
  3683. * 02 CRCCC, CRC Calculation, 0=disabled
  3684. * 01..00 STOP<1..0> Stop bits (00=1,10=2)
  3685. *
  3686. * 0000 0000
  3687. */
  3688. RegValue = 0x00;
  3689. if (info->params.stop_bits != 1)
  3690. RegValue |= BIT1;
  3691. write_reg(info, MD0, RegValue);
  3692. /* MD1, Mode Register 1
  3693. *
  3694. * 07..06 BRATE<1..0>, bit rate, 00=1/1 01=1/16 10=1/32 11=1/64
  3695. * 05..04 TXCHR<1..0>, tx char size, 00=8 bits,01=7,10=6,11=5
  3696. * 03..02 RXCHR<1..0>, rx char size
  3697. * 01..00 PMPM<1..0>, Parity mode, 00=none 10=even 11=odd
  3698. *
  3699. * 0100 0000
  3700. */
  3701. RegValue = 0x40;
  3702. switch (info->params.data_bits) {
  3703. case 7: RegValue |= BIT4 + BIT2; break;
  3704. case 6: RegValue |= BIT5 + BIT3; break;
  3705. case 5: RegValue |= BIT5 + BIT4 + BIT3 + BIT2; break;
  3706. }
  3707. if (info->params.parity != ASYNC_PARITY_NONE) {
  3708. RegValue |= BIT1;
  3709. if (info->params.parity == ASYNC_PARITY_ODD)
  3710. RegValue |= BIT0;
  3711. }
  3712. write_reg(info, MD1, RegValue);
  3713. /* MD2, Mode Register 2
  3714. *
  3715. * 07..02 Reserved, must be 0
  3716. * 01..00 CNCT<1..0> Channel connection, 00=normal 11=local loopback
  3717. *
  3718. * 0000 0000
  3719. */
  3720. RegValue = 0x00;
  3721. if (info->params.loopback)
  3722. RegValue |= (BIT1 + BIT0);
  3723. write_reg(info, MD2, RegValue);
  3724. /* RXS, Receive clock source
  3725. *
  3726. * 07 Reserved, must be 0
  3727. * 06..04 RXCS<2..0>, clock source, 000=RxC Pin, 100=BRG, 110=DPLL
  3728. * 03..00 RXBR<3..0>, rate divisor, 0000=1
  3729. */
  3730. RegValue=BIT6;
  3731. write_reg(info, RXS, RegValue);
  3732. /* TXS, Transmit clock source
  3733. *
  3734. * 07 Reserved, must be 0
  3735. * 06..04 RXCS<2..0>, clock source, 000=TxC Pin, 100=BRG, 110=Receive Clock
  3736. * 03..00 RXBR<3..0>, rate divisor, 0000=1
  3737. */
  3738. RegValue=BIT6;
  3739. write_reg(info, TXS, RegValue);
  3740. /* Control Register
  3741. *
  3742. * 6,4,2,0 CLKSEL<3..0>, 0 = TcCLK in, 1 = Auxclk out
  3743. */
  3744. info->port_array[0]->ctrlreg_value |= (BIT0 << (info->port_num * 2));
  3745. write_control_reg(info);
  3746. tx_set_idle(info);
  3747. /* RRC Receive Ready Control 0
  3748. *
  3749. * 07..05 Reserved, must be 0
  3750. * 04..00 RRC<4..0> Rx FIFO trigger active 0x00 = 1 byte
  3751. */
  3752. write_reg(info, RRC, 0x00);
  3753. /* TRC0 Transmit Ready Control 0
  3754. *
  3755. * 07..05 Reserved, must be 0
  3756. * 04..00 TRC<4..0> Tx FIFO trigger active 0x10 = 16 bytes
  3757. */
  3758. write_reg(info, TRC0, 0x10);
  3759. /* TRC1 Transmit Ready Control 1
  3760. *
  3761. * 07..05 Reserved, must be 0
  3762. * 04..00 TRC<4..0> Tx FIFO trigger inactive 0x1e = 31 bytes (full-1)
  3763. */
  3764. write_reg(info, TRC1, 0x1e);
  3765. /* CTL, MSCI control register
  3766. *
  3767. * 07..06 Reserved, set to 0
  3768. * 05 UDRNC, underrun control, 0=abort 1=CRC+flag (HDLC/BSC)
  3769. * 04 IDLC, idle control, 0=mark 1=idle register
  3770. * 03 BRK, break, 0=off 1 =on (async)
  3771. * 02 SYNCLD, sync char load enable (BSC) 1=enabled
  3772. * 01 GOP, go active on poll (LOOP mode) 1=enabled
  3773. * 00 RTS, RTS output control, 0=active 1=inactive
  3774. *
  3775. * 0001 0001
  3776. */
  3777. RegValue = 0x10;
  3778. if (!(info->serial_signals & SerialSignal_RTS))
  3779. RegValue |= 0x01;
  3780. write_reg(info, CTL, RegValue);
  3781. /* enable status interrupts */
  3782. info->ie0_value |= TXINTE + RXINTE;
  3783. write_reg(info, IE0, info->ie0_value);
  3784. /* enable break detect interrupt */
  3785. info->ie1_value = BRKD;
  3786. write_reg(info, IE1, info->ie1_value);
  3787. /* enable rx overrun interrupt */
  3788. info->ie2_value = OVRN;
  3789. write_reg(info, IE2, info->ie2_value);
  3790. set_rate( info, info->params.data_rate * 16 );
  3791. }
  3792. /* Program the SCA for HDLC communications.
  3793. */
  3794. static void hdlc_mode(SLMP_INFO *info)
  3795. {
  3796. unsigned char RegValue;
  3797. u32 DpllDivisor;
  3798. // Can't use DPLL because SCA outputs recovered clock on RxC when
  3799. // DPLL mode selected. This causes output contention with RxC receiver.
  3800. // Use of DPLL would require external hardware to disable RxC receiver
  3801. // when DPLL mode selected.
  3802. info->params.flags &= ~(HDLC_FLAG_TXC_DPLL + HDLC_FLAG_RXC_DPLL);
  3803. /* disable DMA interrupts */
  3804. write_reg(info, TXDMA + DIR, 0);
  3805. write_reg(info, RXDMA + DIR, 0);
  3806. /* MD0, Mode Register 0
  3807. *
  3808. * 07..05 PRCTL<2..0>, Protocol Mode, 100=HDLC
  3809. * 04 AUTO, Auto-enable (RTS/CTS/DCD)
  3810. * 03 Reserved, must be 0
  3811. * 02 CRCCC, CRC Calculation, 1=enabled
  3812. * 01 CRC1, CRC selection, 0=CRC-16,1=CRC-CCITT-16
  3813. * 00 CRC0, CRC initial value, 1 = all 1s
  3814. *
  3815. * 1000 0001
  3816. */
  3817. RegValue = 0x81;
  3818. if (info->params.flags & HDLC_FLAG_AUTO_CTS)
  3819. RegValue |= BIT4;
  3820. if (info->params.flags & HDLC_FLAG_AUTO_DCD)
  3821. RegValue |= BIT4;
  3822. if (info->params.crc_type == HDLC_CRC_16_CCITT)
  3823. RegValue |= BIT2 + BIT1;
  3824. write_reg(info, MD0, RegValue);
  3825. /* MD1, Mode Register 1
  3826. *
  3827. * 07..06 ADDRS<1..0>, Address detect, 00=no addr check
  3828. * 05..04 TXCHR<1..0>, tx char size, 00=8 bits
  3829. * 03..02 RXCHR<1..0>, rx char size, 00=8 bits
  3830. * 01..00 PMPM<1..0>, Parity mode, 00=no parity
  3831. *
  3832. * 0000 0000
  3833. */
  3834. RegValue = 0x00;
  3835. write_reg(info, MD1, RegValue);
  3836. /* MD2, Mode Register 2
  3837. *
  3838. * 07 NRZFM, 0=NRZ, 1=FM
  3839. * 06..05 CODE<1..0> Encoding, 00=NRZ
  3840. * 04..03 DRATE<1..0> DPLL Divisor, 00=8
  3841. * 02 Reserved, must be 0
  3842. * 01..00 CNCT<1..0> Channel connection, 0=normal
  3843. *
  3844. * 0000 0000
  3845. */
  3846. RegValue = 0x00;
  3847. switch(info->params.encoding) {
  3848. case HDLC_ENCODING_NRZI: RegValue |= BIT5; break;
  3849. case HDLC_ENCODING_BIPHASE_MARK: RegValue |= BIT7 + BIT5; break; /* aka FM1 */
  3850. case HDLC_ENCODING_BIPHASE_SPACE: RegValue |= BIT7 + BIT6; break; /* aka FM0 */
  3851. case HDLC_ENCODING_BIPHASE_LEVEL: RegValue |= BIT7; break; /* aka Manchester */
  3852. #if 0
  3853. case HDLC_ENCODING_NRZB: /* not supported */
  3854. case HDLC_ENCODING_NRZI_MARK: /* not supported */
  3855. case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: /* not supported */
  3856. #endif
  3857. }
  3858. if ( info->params.flags & HDLC_FLAG_DPLL_DIV16 ) {
  3859. DpllDivisor = 16;
  3860. RegValue |= BIT3;
  3861. } else if ( info->params.flags & HDLC_FLAG_DPLL_DIV8 ) {
  3862. DpllDivisor = 8;
  3863. } else {
  3864. DpllDivisor = 32;
  3865. RegValue |= BIT4;
  3866. }
  3867. write_reg(info, MD2, RegValue);
  3868. /* RXS, Receive clock source
  3869. *
  3870. * 07 Reserved, must be 0
  3871. * 06..04 RXCS<2..0>, clock source, 000=RxC Pin, 100=BRG, 110=DPLL
  3872. * 03..00 RXBR<3..0>, rate divisor, 0000=1
  3873. */
  3874. RegValue=0;
  3875. if (info->params.flags & HDLC_FLAG_RXC_BRG)
  3876. RegValue |= BIT6;
  3877. if (info->params.flags & HDLC_FLAG_RXC_DPLL)
  3878. RegValue |= BIT6 + BIT5;
  3879. write_reg(info, RXS, RegValue);
  3880. /* TXS, Transmit clock source
  3881. *
  3882. * 07 Reserved, must be 0
  3883. * 06..04 RXCS<2..0>, clock source, 000=TxC Pin, 100=BRG, 110=Receive Clock
  3884. * 03..00 RXBR<3..0>, rate divisor, 0000=1
  3885. */
  3886. RegValue=0;
  3887. if (info->params.flags & HDLC_FLAG_TXC_BRG)
  3888. RegValue |= BIT6;
  3889. if (info->params.flags & HDLC_FLAG_TXC_DPLL)
  3890. RegValue |= BIT6 + BIT5;
  3891. write_reg(info, TXS, RegValue);
  3892. if (info->params.flags & HDLC_FLAG_RXC_DPLL)
  3893. set_rate(info, info->params.clock_speed * DpllDivisor);
  3894. else
  3895. set_rate(info, info->params.clock_speed);
  3896. /* GPDATA (General Purpose I/O Data Register)
  3897. *
  3898. * 6,4,2,0 CLKSEL<3..0>, 0 = TcCLK in, 1 = Auxclk out
  3899. */
  3900. if (info->params.flags & HDLC_FLAG_TXC_BRG)
  3901. info->port_array[0]->ctrlreg_value |= (BIT0 << (info->port_num * 2));
  3902. else
  3903. info->port_array[0]->ctrlreg_value &= ~(BIT0 << (info->port_num * 2));
  3904. write_control_reg(info);
  3905. /* RRC Receive Ready Control 0
  3906. *
  3907. * 07..05 Reserved, must be 0
  3908. * 04..00 RRC<4..0> Rx FIFO trigger active
  3909. */
  3910. write_reg(info, RRC, rx_active_fifo_level);
  3911. /* TRC0 Transmit Ready Control 0
  3912. *
  3913. * 07..05 Reserved, must be 0
  3914. * 04..00 TRC<4..0> Tx FIFO trigger active
  3915. */
  3916. write_reg(info, TRC0, tx_active_fifo_level);
  3917. /* TRC1 Transmit Ready Control 1
  3918. *
  3919. * 07..05 Reserved, must be 0
  3920. * 04..00 TRC<4..0> Tx FIFO trigger inactive 0x1f = 32 bytes (full)
  3921. */
  3922. write_reg(info, TRC1, (unsigned char)(tx_negate_fifo_level - 1));
  3923. /* DMR, DMA Mode Register
  3924. *
  3925. * 07..05 Reserved, must be 0
  3926. * 04 TMOD, Transfer Mode: 1=chained-block
  3927. * 03 Reserved, must be 0
  3928. * 02 NF, Number of Frames: 1=multi-frame
  3929. * 01 CNTE, Frame End IRQ Counter enable: 0=disabled
  3930. * 00 Reserved, must be 0
  3931. *
  3932. * 0001 0100
  3933. */
  3934. write_reg(info, TXDMA + DMR, 0x14);
  3935. write_reg(info, RXDMA + DMR, 0x14);
  3936. /* Set chain pointer base (upper 8 bits of 24 bit addr) */
  3937. write_reg(info, RXDMA + CPB,
  3938. (unsigned char)(info->buffer_list_phys >> 16));
  3939. /* Set chain pointer base (upper 8 bits of 24 bit addr) */
  3940. write_reg(info, TXDMA + CPB,
  3941. (unsigned char)(info->buffer_list_phys >> 16));
  3942. /* enable status interrupts. other code enables/disables
  3943. * the individual sources for these two interrupt classes.
  3944. */
  3945. info->ie0_value |= TXINTE + RXINTE;
  3946. write_reg(info, IE0, info->ie0_value);
  3947. /* CTL, MSCI control register
  3948. *
  3949. * 07..06 Reserved, set to 0
  3950. * 05 UDRNC, underrun control, 0=abort 1=CRC+flag (HDLC/BSC)
  3951. * 04 IDLC, idle control, 0=mark 1=idle register
  3952. * 03 BRK, break, 0=off 1 =on (async)
  3953. * 02 SYNCLD, sync char load enable (BSC) 1=enabled
  3954. * 01 GOP, go active on poll (LOOP mode) 1=enabled
  3955. * 00 RTS, RTS output control, 0=active 1=inactive
  3956. *
  3957. * 0001 0001
  3958. */
  3959. RegValue = 0x10;
  3960. if (!(info->serial_signals & SerialSignal_RTS))
  3961. RegValue |= 0x01;
  3962. write_reg(info, CTL, RegValue);
  3963. /* preamble not supported ! */
  3964. tx_set_idle(info);
  3965. tx_stop(info);
  3966. rx_stop(info);
  3967. set_rate(info, info->params.clock_speed);
  3968. if (info->params.loopback)
  3969. enable_loopback(info,1);
  3970. }
  3971. /* Set the transmit HDLC idle mode
  3972. */
  3973. static void tx_set_idle(SLMP_INFO *info)
  3974. {
  3975. unsigned char RegValue = 0xff;
  3976. /* Map API idle mode to SCA register bits */
  3977. switch(info->idle_mode) {
  3978. case HDLC_TXIDLE_FLAGS: RegValue = 0x7e; break;
  3979. case HDLC_TXIDLE_ALT_ZEROS_ONES: RegValue = 0xaa; break;
  3980. case HDLC_TXIDLE_ZEROS: RegValue = 0x00; break;
  3981. case HDLC_TXIDLE_ONES: RegValue = 0xff; break;
  3982. case HDLC_TXIDLE_ALT_MARK_SPACE: RegValue = 0xaa; break;
  3983. case HDLC_TXIDLE_SPACE: RegValue = 0x00; break;
  3984. case HDLC_TXIDLE_MARK: RegValue = 0xff; break;
  3985. }
  3986. write_reg(info, IDL, RegValue);
  3987. }
  3988. /* Query the adapter for the state of the V24 status (input) signals.
  3989. */
  3990. static void get_signals(SLMP_INFO *info)
  3991. {
  3992. u16 status = read_reg(info, SR3);
  3993. u16 gpstatus = read_status_reg(info);
  3994. u16 testbit;
  3995. /* clear all serial signals except DTR and RTS */
  3996. info->serial_signals &= SerialSignal_DTR + SerialSignal_RTS;
  3997. /* set serial signal bits to reflect MISR */
  3998. if (!(status & BIT3))
  3999. info->serial_signals |= SerialSignal_CTS;
  4000. if ( !(status & BIT2))
  4001. info->serial_signals |= SerialSignal_DCD;
  4002. testbit = BIT1 << (info->port_num * 2); // Port 0..3 RI is GPDATA<1,3,5,7>
  4003. if (!(gpstatus & testbit))
  4004. info->serial_signals |= SerialSignal_RI;
  4005. testbit = BIT0 << (info->port_num * 2); // Port 0..3 DSR is GPDATA<0,2,4,6>
  4006. if (!(gpstatus & testbit))
  4007. info->serial_signals |= SerialSignal_DSR;
  4008. }
  4009. /* Set the state of DTR and RTS based on contents of
  4010. * serial_signals member of device context.
  4011. */
  4012. static void set_signals(SLMP_INFO *info)
  4013. {
  4014. unsigned char RegValue;
  4015. u16 EnableBit;
  4016. RegValue = read_reg(info, CTL);
  4017. if (info->serial_signals & SerialSignal_RTS)
  4018. RegValue &= ~BIT0;
  4019. else
  4020. RegValue |= BIT0;
  4021. write_reg(info, CTL, RegValue);
  4022. // Port 0..3 DTR is ctrl reg <1,3,5,7>
  4023. EnableBit = BIT1 << (info->port_num*2);
  4024. if (info->serial_signals & SerialSignal_DTR)
  4025. info->port_array[0]->ctrlreg_value &= ~EnableBit;
  4026. else
  4027. info->port_array[0]->ctrlreg_value |= EnableBit;
  4028. write_control_reg(info);
  4029. }
  4030. /*******************/
  4031. /* DMA Buffer Code */
  4032. /*******************/
  4033. /* Set the count for all receive buffers to SCABUFSIZE
  4034. * and set the current buffer to the first buffer. This effectively
  4035. * makes all buffers free and discards any data in buffers.
  4036. */
  4037. static void rx_reset_buffers(SLMP_INFO *info)
  4038. {
  4039. rx_free_frame_buffers(info, 0, info->rx_buf_count - 1);
  4040. }
  4041. /* Free the buffers used by a received frame
  4042. *
  4043. * info pointer to device instance data
  4044. * first index of 1st receive buffer of frame
  4045. * last index of last receive buffer of frame
  4046. */
  4047. static void rx_free_frame_buffers(SLMP_INFO *info, unsigned int first, unsigned int last)
  4048. {
  4049. bool done = false;
  4050. while(!done) {
  4051. /* reset current buffer for reuse */
  4052. info->rx_buf_list[first].status = 0xff;
  4053. if (first == last) {
  4054. done = true;
  4055. /* set new last rx descriptor address */
  4056. write_reg16(info, RXDMA + EDA, info->rx_buf_list_ex[first].phys_entry);
  4057. }
  4058. first++;
  4059. if (first == info->rx_buf_count)
  4060. first = 0;
  4061. }
  4062. /* set current buffer to next buffer after last buffer of frame */
  4063. info->current_rx_buf = first;
  4064. }
  4065. /* Return a received frame from the receive DMA buffers.
  4066. * Only frames received without errors are returned.
  4067. *
  4068. * Return Value: true if frame returned, otherwise false
  4069. */
  4070. static bool rx_get_frame(SLMP_INFO *info)
  4071. {
  4072. unsigned int StartIndex, EndIndex; /* index of 1st and last buffers of Rx frame */
  4073. unsigned short status;
  4074. unsigned int framesize = 0;
  4075. bool ReturnCode = false;
  4076. unsigned long flags;
  4077. struct tty_struct *tty = info->port.tty;
  4078. unsigned char addr_field = 0xff;
  4079. SCADESC *desc;
  4080. SCADESC_EX *desc_ex;
  4081. CheckAgain:
  4082. /* assume no frame returned, set zero length */
  4083. framesize = 0;
  4084. addr_field = 0xff;
  4085. /*
  4086. * current_rx_buf points to the 1st buffer of the next available
  4087. * receive frame. To find the last buffer of the frame look for
  4088. * a non-zero status field in the buffer entries. (The status
  4089. * field is set by the 16C32 after completing a receive frame.
  4090. */
  4091. StartIndex = EndIndex = info->current_rx_buf;
  4092. for ( ;; ) {
  4093. desc = &info->rx_buf_list[EndIndex];
  4094. desc_ex = &info->rx_buf_list_ex[EndIndex];
  4095. if (desc->status == 0xff)
  4096. goto Cleanup; /* current desc still in use, no frames available */
  4097. if (framesize == 0 && info->params.addr_filter != 0xff)
  4098. addr_field = desc_ex->virt_addr[0];
  4099. framesize += desc->length;
  4100. /* Status != 0 means last buffer of frame */
  4101. if (desc->status)
  4102. break;
  4103. EndIndex++;
  4104. if (EndIndex == info->rx_buf_count)
  4105. EndIndex = 0;
  4106. if (EndIndex == info->current_rx_buf) {
  4107. /* all buffers have been 'used' but none mark */
  4108. /* the end of a frame. Reset buffers and receiver. */
  4109. if ( info->rx_enabled ){
  4110. spin_lock_irqsave(&info->lock,flags);
  4111. rx_start(info);
  4112. spin_unlock_irqrestore(&info->lock,flags);
  4113. }
  4114. goto Cleanup;
  4115. }
  4116. }
  4117. /* check status of receive frame */
  4118. /* frame status is byte stored after frame data
  4119. *
  4120. * 7 EOM (end of msg), 1 = last buffer of frame
  4121. * 6 Short Frame, 1 = short frame
  4122. * 5 Abort, 1 = frame aborted
  4123. * 4 Residue, 1 = last byte is partial
  4124. * 3 Overrun, 1 = overrun occurred during frame reception
  4125. * 2 CRC, 1 = CRC error detected
  4126. *
  4127. */
  4128. status = desc->status;
  4129. /* ignore CRC bit if not using CRC (bit is undefined) */
  4130. /* Note:CRC is not save to data buffer */
  4131. if (info->params.crc_type == HDLC_CRC_NONE)
  4132. status &= ~BIT2;
  4133. if (framesize == 0 ||
  4134. (addr_field != 0xff && addr_field != info->params.addr_filter)) {
  4135. /* discard 0 byte frames, this seems to occur sometime
  4136. * when remote is idling flags.
  4137. */
  4138. rx_free_frame_buffers(info, StartIndex, EndIndex);
  4139. goto CheckAgain;
  4140. }
  4141. if (framesize < 2)
  4142. status |= BIT6;
  4143. if (status & (BIT6+BIT5+BIT3+BIT2)) {
  4144. /* received frame has errors,
  4145. * update counts and mark frame size as 0
  4146. */
  4147. if (status & BIT6)
  4148. info->icount.rxshort++;
  4149. else if (status & BIT5)
  4150. info->icount.rxabort++;
  4151. else if (status & BIT3)
  4152. info->icount.rxover++;
  4153. else
  4154. info->icount.rxcrc++;
  4155. framesize = 0;
  4156. #if SYNCLINK_GENERIC_HDLC
  4157. {
  4158. struct net_device_stats *stats = hdlc_stats(info->netdev);
  4159. stats->rx_errors++;
  4160. stats->rx_frame_errors++;
  4161. }
  4162. #endif
  4163. }
  4164. if ( debug_level >= DEBUG_LEVEL_BH )
  4165. printk("%s(%d):%s rx_get_frame() status=%04X size=%d\n",
  4166. __FILE__,__LINE__,info->device_name,status,framesize);
  4167. if ( debug_level >= DEBUG_LEVEL_DATA )
  4168. trace_block(info,info->rx_buf_list_ex[StartIndex].virt_addr,
  4169. min_t(int, framesize,SCABUFSIZE),0);
  4170. if (framesize) {
  4171. if (framesize > info->max_frame_size)
  4172. info->icount.rxlong++;
  4173. else {
  4174. /* copy dma buffer(s) to contiguous intermediate buffer */
  4175. int copy_count = framesize;
  4176. int index = StartIndex;
  4177. unsigned char *ptmp = info->tmp_rx_buf;
  4178. info->tmp_rx_buf_count = framesize;
  4179. info->icount.rxok++;
  4180. while(copy_count) {
  4181. int partial_count = min(copy_count,SCABUFSIZE);
  4182. memcpy( ptmp,
  4183. info->rx_buf_list_ex[index].virt_addr,
  4184. partial_count );
  4185. ptmp += partial_count;
  4186. copy_count -= partial_count;
  4187. if ( ++index == info->rx_buf_count )
  4188. index = 0;
  4189. }
  4190. #if SYNCLINK_GENERIC_HDLC
  4191. if (info->netcount)
  4192. hdlcdev_rx(info,info->tmp_rx_buf,framesize);
  4193. else
  4194. #endif
  4195. ldisc_receive_buf(tty,info->tmp_rx_buf,
  4196. info->flag_buf, framesize);
  4197. }
  4198. }
  4199. /* Free the buffers used by this frame. */
  4200. rx_free_frame_buffers( info, StartIndex, EndIndex );
  4201. ReturnCode = true;
  4202. Cleanup:
  4203. if ( info->rx_enabled && info->rx_overflow ) {
  4204. /* Receiver is enabled, but needs to restarted due to
  4205. * rx buffer overflow. If buffers are empty, restart receiver.
  4206. */
  4207. if (info->rx_buf_list[EndIndex].status == 0xff) {
  4208. spin_lock_irqsave(&info->lock,flags);
  4209. rx_start(info);
  4210. spin_unlock_irqrestore(&info->lock,flags);
  4211. }
  4212. }
  4213. return ReturnCode;
  4214. }
  4215. /* load the transmit DMA buffer with data
  4216. */
  4217. static void tx_load_dma_buffer(SLMP_INFO *info, const char *buf, unsigned int count)
  4218. {
  4219. unsigned short copy_count;
  4220. unsigned int i = 0;
  4221. SCADESC *desc;
  4222. SCADESC_EX *desc_ex;
  4223. if ( debug_level >= DEBUG_LEVEL_DATA )
  4224. trace_block(info,buf, min_t(int, count,SCABUFSIZE), 1);
  4225. /* Copy source buffer to one or more DMA buffers, starting with
  4226. * the first transmit dma buffer.
  4227. */
  4228. for(i=0;;)
  4229. {
  4230. copy_count = min_t(unsigned short,count,SCABUFSIZE);
  4231. desc = &info->tx_buf_list[i];
  4232. desc_ex = &info->tx_buf_list_ex[i];
  4233. load_pci_memory(info, desc_ex->virt_addr,buf,copy_count);
  4234. desc->length = copy_count;
  4235. desc->status = 0;
  4236. buf += copy_count;
  4237. count -= copy_count;
  4238. if (!count)
  4239. break;
  4240. i++;
  4241. if (i >= info->tx_buf_count)
  4242. i = 0;
  4243. }
  4244. info->tx_buf_list[i].status = 0x81; /* set EOM and EOT status */
  4245. info->last_tx_buf = ++i;
  4246. }
  4247. static bool register_test(SLMP_INFO *info)
  4248. {
  4249. static unsigned char testval[] = {0x00, 0xff, 0xaa, 0x55, 0x69, 0x96};
  4250. static unsigned int count = ARRAY_SIZE(testval);
  4251. unsigned int i;
  4252. bool rc = true;
  4253. unsigned long flags;
  4254. spin_lock_irqsave(&info->lock,flags);
  4255. reset_port(info);
  4256. /* assume failure */
  4257. info->init_error = DiagStatus_AddressFailure;
  4258. /* Write bit patterns to various registers but do it out of */
  4259. /* sync, then read back and verify values. */
  4260. for (i = 0 ; i < count ; i++) {
  4261. write_reg(info, TMC, testval[i]);
  4262. write_reg(info, IDL, testval[(i+1)%count]);
  4263. write_reg(info, SA0, testval[(i+2)%count]);
  4264. write_reg(info, SA1, testval[(i+3)%count]);
  4265. if ( (read_reg(info, TMC) != testval[i]) ||
  4266. (read_reg(info, IDL) != testval[(i+1)%count]) ||
  4267. (read_reg(info, SA0) != testval[(i+2)%count]) ||
  4268. (read_reg(info, SA1) != testval[(i+3)%count]) )
  4269. {
  4270. rc = false;
  4271. break;
  4272. }
  4273. }
  4274. reset_port(info);
  4275. spin_unlock_irqrestore(&info->lock,flags);
  4276. return rc;
  4277. }
  4278. static bool irq_test(SLMP_INFO *info)
  4279. {
  4280. unsigned long timeout;
  4281. unsigned long flags;
  4282. unsigned char timer = (info->port_num & 1) ? TIMER2 : TIMER0;
  4283. spin_lock_irqsave(&info->lock,flags);
  4284. reset_port(info);
  4285. /* assume failure */
  4286. info->init_error = DiagStatus_IrqFailure;
  4287. info->irq_occurred = false;
  4288. /* setup timer0 on SCA0 to interrupt */
  4289. /* IER2<7..4> = timer<3..0> interrupt enables (1=enabled) */
  4290. write_reg(info, IER2, (unsigned char)((info->port_num & 1) ? BIT6 : BIT4));
  4291. write_reg(info, (unsigned char)(timer + TEPR), 0); /* timer expand prescale */
  4292. write_reg16(info, (unsigned char)(timer + TCONR), 1); /* timer constant */
  4293. /* TMCS, Timer Control/Status Register
  4294. *
  4295. * 07 CMF, Compare match flag (read only) 1=match
  4296. * 06 ECMI, CMF Interrupt Enable: 1=enabled
  4297. * 05 Reserved, must be 0
  4298. * 04 TME, Timer Enable
  4299. * 03..00 Reserved, must be 0
  4300. *
  4301. * 0101 0000
  4302. */
  4303. write_reg(info, (unsigned char)(timer + TMCS), 0x50);
  4304. spin_unlock_irqrestore(&info->lock,flags);
  4305. timeout=100;
  4306. while( timeout-- && !info->irq_occurred ) {
  4307. msleep_interruptible(10);
  4308. }
  4309. spin_lock_irqsave(&info->lock,flags);
  4310. reset_port(info);
  4311. spin_unlock_irqrestore(&info->lock,flags);
  4312. return info->irq_occurred;
  4313. }
  4314. /* initialize individual SCA device (2 ports)
  4315. */
  4316. static bool sca_init(SLMP_INFO *info)
  4317. {
  4318. /* set wait controller to single mem partition (low), no wait states */
  4319. write_reg(info, PABR0, 0); /* wait controller addr boundary 0 */
  4320. write_reg(info, PABR1, 0); /* wait controller addr boundary 1 */
  4321. write_reg(info, WCRL, 0); /* wait controller low range */
  4322. write_reg(info, WCRM, 0); /* wait controller mid range */
  4323. write_reg(info, WCRH, 0); /* wait controller high range */
  4324. /* DPCR, DMA Priority Control
  4325. *
  4326. * 07..05 Not used, must be 0
  4327. * 04 BRC, bus release condition: 0=all transfers complete
  4328. * 03 CCC, channel change condition: 0=every cycle
  4329. * 02..00 PR<2..0>, priority 100=round robin
  4330. *
  4331. * 00000100 = 0x04
  4332. */
  4333. write_reg(info, DPCR, dma_priority);
  4334. /* DMA Master Enable, BIT7: 1=enable all channels */
  4335. write_reg(info, DMER, 0x80);
  4336. /* enable all interrupt classes */
  4337. write_reg(info, IER0, 0xff); /* TxRDY,RxRDY,TxINT,RxINT (ports 0-1) */
  4338. write_reg(info, IER1, 0xff); /* DMIB,DMIA (channels 0-3) */
  4339. write_reg(info, IER2, 0xf0); /* TIRQ (timers 0-3) */
  4340. /* ITCR, interrupt control register
  4341. * 07 IPC, interrupt priority, 0=MSCI->DMA
  4342. * 06..05 IAK<1..0>, Acknowledge cycle, 00=non-ack cycle
  4343. * 04 VOS, Vector Output, 0=unmodified vector
  4344. * 03..00 Reserved, must be 0
  4345. */
  4346. write_reg(info, ITCR, 0);
  4347. return true;
  4348. }
  4349. /* initialize adapter hardware
  4350. */
  4351. static bool init_adapter(SLMP_INFO *info)
  4352. {
  4353. int i;
  4354. /* Set BIT30 of Local Control Reg 0x50 to reset SCA */
  4355. volatile u32 *MiscCtrl = (u32 *)(info->lcr_base + 0x50);
  4356. u32 readval;
  4357. info->misc_ctrl_value |= BIT30;
  4358. *MiscCtrl = info->misc_ctrl_value;
  4359. /*
  4360. * Force at least 170ns delay before clearing
  4361. * reset bit. Each read from LCR takes at least
  4362. * 30ns so 10 times for 300ns to be safe.
  4363. */
  4364. for(i=0;i<10;i++)
  4365. readval = *MiscCtrl;
  4366. info->misc_ctrl_value &= ~BIT30;
  4367. *MiscCtrl = info->misc_ctrl_value;
  4368. /* init control reg (all DTRs off, all clksel=input) */
  4369. info->ctrlreg_value = 0xaa;
  4370. write_control_reg(info);
  4371. {
  4372. volatile u32 *LCR1BRDR = (u32 *)(info->lcr_base + 0x2c);
  4373. lcr1_brdr_value &= ~(BIT5 + BIT4 + BIT3);
  4374. switch(read_ahead_count)
  4375. {
  4376. case 16:
  4377. lcr1_brdr_value |= BIT5 + BIT4 + BIT3;
  4378. break;
  4379. case 8:
  4380. lcr1_brdr_value |= BIT5 + BIT4;
  4381. break;
  4382. case 4:
  4383. lcr1_brdr_value |= BIT5 + BIT3;
  4384. break;
  4385. case 0:
  4386. lcr1_brdr_value |= BIT5;
  4387. break;
  4388. }
  4389. *LCR1BRDR = lcr1_brdr_value;
  4390. *MiscCtrl = misc_ctrl_value;
  4391. }
  4392. sca_init(info->port_array[0]);
  4393. sca_init(info->port_array[2]);
  4394. return true;
  4395. }
  4396. /* Loopback an HDLC frame to test the hardware
  4397. * interrupt and DMA functions.
  4398. */
  4399. static bool loopback_test(SLMP_INFO *info)
  4400. {
  4401. #define TESTFRAMESIZE 20
  4402. unsigned long timeout;
  4403. u16 count = TESTFRAMESIZE;
  4404. unsigned char buf[TESTFRAMESIZE];
  4405. bool rc = false;
  4406. unsigned long flags;
  4407. struct tty_struct *oldtty = info->port.tty;
  4408. u32 speed = info->params.clock_speed;
  4409. info->params.clock_speed = 3686400;
  4410. info->port.tty = NULL;
  4411. /* assume failure */
  4412. info->init_error = DiagStatus_DmaFailure;
  4413. /* build and send transmit frame */
  4414. for (count = 0; count < TESTFRAMESIZE;++count)
  4415. buf[count] = (unsigned char)count;
  4416. memset(info->tmp_rx_buf,0,TESTFRAMESIZE);
  4417. /* program hardware for HDLC and enabled receiver */
  4418. spin_lock_irqsave(&info->lock,flags);
  4419. hdlc_mode(info);
  4420. enable_loopback(info,1);
  4421. rx_start(info);
  4422. info->tx_count = count;
  4423. tx_load_dma_buffer(info,buf,count);
  4424. tx_start(info);
  4425. spin_unlock_irqrestore(&info->lock,flags);
  4426. /* wait for receive complete */
  4427. /* Set a timeout for waiting for interrupt. */
  4428. for ( timeout = 100; timeout; --timeout ) {
  4429. msleep_interruptible(10);
  4430. if (rx_get_frame(info)) {
  4431. rc = true;
  4432. break;
  4433. }
  4434. }
  4435. /* verify received frame length and contents */
  4436. if (rc &&
  4437. ( info->tmp_rx_buf_count != count ||
  4438. memcmp(buf, info->tmp_rx_buf,count))) {
  4439. rc = false;
  4440. }
  4441. spin_lock_irqsave(&info->lock,flags);
  4442. reset_adapter(info);
  4443. spin_unlock_irqrestore(&info->lock,flags);
  4444. info->params.clock_speed = speed;
  4445. info->port.tty = oldtty;
  4446. return rc;
  4447. }
  4448. /* Perform diagnostics on hardware
  4449. */
  4450. static int adapter_test( SLMP_INFO *info )
  4451. {
  4452. unsigned long flags;
  4453. if ( debug_level >= DEBUG_LEVEL_INFO )
  4454. printk( "%s(%d):Testing device %s\n",
  4455. __FILE__,__LINE__,info->device_name );
  4456. spin_lock_irqsave(&info->lock,flags);
  4457. init_adapter(info);
  4458. spin_unlock_irqrestore(&info->lock,flags);
  4459. info->port_array[0]->port_count = 0;
  4460. if ( register_test(info->port_array[0]) &&
  4461. register_test(info->port_array[1])) {
  4462. info->port_array[0]->port_count = 2;
  4463. if ( register_test(info->port_array[2]) &&
  4464. register_test(info->port_array[3]) )
  4465. info->port_array[0]->port_count += 2;
  4466. }
  4467. else {
  4468. printk( "%s(%d):Register test failure for device %s Addr=%08lX\n",
  4469. __FILE__,__LINE__,info->device_name, (unsigned long)(info->phys_sca_base));
  4470. return -ENODEV;
  4471. }
  4472. if ( !irq_test(info->port_array[0]) ||
  4473. !irq_test(info->port_array[1]) ||
  4474. (info->port_count == 4 && !irq_test(info->port_array[2])) ||
  4475. (info->port_count == 4 && !irq_test(info->port_array[3]))) {
  4476. printk( "%s(%d):Interrupt test failure for device %s IRQ=%d\n",
  4477. __FILE__,__LINE__,info->device_name, (unsigned short)(info->irq_level) );
  4478. return -ENODEV;
  4479. }
  4480. if (!loopback_test(info->port_array[0]) ||
  4481. !loopback_test(info->port_array[1]) ||
  4482. (info->port_count == 4 && !loopback_test(info->port_array[2])) ||
  4483. (info->port_count == 4 && !loopback_test(info->port_array[3]))) {
  4484. printk( "%s(%d):DMA test failure for device %s\n",
  4485. __FILE__,__LINE__,info->device_name);
  4486. return -ENODEV;
  4487. }
  4488. if ( debug_level >= DEBUG_LEVEL_INFO )
  4489. printk( "%s(%d):device %s passed diagnostics\n",
  4490. __FILE__,__LINE__,info->device_name );
  4491. info->port_array[0]->init_error = 0;
  4492. info->port_array[1]->init_error = 0;
  4493. if ( info->port_count > 2 ) {
  4494. info->port_array[2]->init_error = 0;
  4495. info->port_array[3]->init_error = 0;
  4496. }
  4497. return 0;
  4498. }
  4499. /* Test the shared memory on a PCI adapter.
  4500. */
  4501. static bool memory_test(SLMP_INFO *info)
  4502. {
  4503. static unsigned long testval[] = { 0x0, 0x55555555, 0xaaaaaaaa,
  4504. 0x66666666, 0x99999999, 0xffffffff, 0x12345678 };
  4505. unsigned long count = ARRAY_SIZE(testval);
  4506. unsigned long i;
  4507. unsigned long limit = SCA_MEM_SIZE/sizeof(unsigned long);
  4508. unsigned long * addr = (unsigned long *)info->memory_base;
  4509. /* Test data lines with test pattern at one location. */
  4510. for ( i = 0 ; i < count ; i++ ) {
  4511. *addr = testval[i];
  4512. if ( *addr != testval[i] )
  4513. return false;
  4514. }
  4515. /* Test address lines with incrementing pattern over */
  4516. /* entire address range. */
  4517. for ( i = 0 ; i < limit ; i++ ) {
  4518. *addr = i * 4;
  4519. addr++;
  4520. }
  4521. addr = (unsigned long *)info->memory_base;
  4522. for ( i = 0 ; i < limit ; i++ ) {
  4523. if ( *addr != i * 4 )
  4524. return false;
  4525. addr++;
  4526. }
  4527. memset( info->memory_base, 0, SCA_MEM_SIZE );
  4528. return true;
  4529. }
  4530. /* Load data into PCI adapter shared memory.
  4531. *
  4532. * The PCI9050 releases control of the local bus
  4533. * after completing the current read or write operation.
  4534. *
  4535. * While the PCI9050 write FIFO not empty, the
  4536. * PCI9050 treats all of the writes as a single transaction
  4537. * and does not release the bus. This causes DMA latency problems
  4538. * at high speeds when copying large data blocks to the shared memory.
  4539. *
  4540. * This function breaks a write into multiple transations by
  4541. * interleaving a read which flushes the write FIFO and 'completes'
  4542. * the write transation. This allows any pending DMA request to gain control
  4543. * of the local bus in a timely fasion.
  4544. */
  4545. static void load_pci_memory(SLMP_INFO *info, char* dest, const char* src, unsigned short count)
  4546. {
  4547. /* A load interval of 16 allows for 4 32-bit writes at */
  4548. /* 136ns each for a maximum latency of 542ns on the local bus.*/
  4549. unsigned short interval = count / sca_pci_load_interval;
  4550. unsigned short i;
  4551. for ( i = 0 ; i < interval ; i++ )
  4552. {
  4553. memcpy(dest, src, sca_pci_load_interval);
  4554. read_status_reg(info);
  4555. dest += sca_pci_load_interval;
  4556. src += sca_pci_load_interval;
  4557. }
  4558. memcpy(dest, src, count % sca_pci_load_interval);
  4559. }
  4560. static void trace_block(SLMP_INFO *info,const char* data, int count, int xmit)
  4561. {
  4562. int i;
  4563. int linecount;
  4564. if (xmit)
  4565. printk("%s tx data:\n",info->device_name);
  4566. else
  4567. printk("%s rx data:\n",info->device_name);
  4568. while(count) {
  4569. if (count > 16)
  4570. linecount = 16;
  4571. else
  4572. linecount = count;
  4573. for(i=0;i<linecount;i++)
  4574. printk("%02X ",(unsigned char)data[i]);
  4575. for(;i<17;i++)
  4576. printk(" ");
  4577. for(i=0;i<linecount;i++) {
  4578. if (data[i]>=040 && data[i]<=0176)
  4579. printk("%c",data[i]);
  4580. else
  4581. printk(".");
  4582. }
  4583. printk("\n");
  4584. data += linecount;
  4585. count -= linecount;
  4586. }
  4587. } /* end of trace_block() */
  4588. /* called when HDLC frame times out
  4589. * update stats and do tx completion processing
  4590. */
  4591. static void tx_timeout(unsigned long context)
  4592. {
  4593. SLMP_INFO *info = (SLMP_INFO*)context;
  4594. unsigned long flags;
  4595. if ( debug_level >= DEBUG_LEVEL_INFO )
  4596. printk( "%s(%d):%s tx_timeout()\n",
  4597. __FILE__,__LINE__,info->device_name);
  4598. if(info->tx_active && info->params.mode == MGSL_MODE_HDLC) {
  4599. info->icount.txtimeout++;
  4600. }
  4601. spin_lock_irqsave(&info->lock,flags);
  4602. info->tx_active = false;
  4603. info->tx_count = info->tx_put = info->tx_get = 0;
  4604. spin_unlock_irqrestore(&info->lock,flags);
  4605. #if SYNCLINK_GENERIC_HDLC
  4606. if (info->netcount)
  4607. hdlcdev_tx_done(info);
  4608. else
  4609. #endif
  4610. bh_transmit(info);
  4611. }
  4612. /* called to periodically check the DSR/RI modem signal input status
  4613. */
  4614. static void status_timeout(unsigned long context)
  4615. {
  4616. u16 status = 0;
  4617. SLMP_INFO *info = (SLMP_INFO*)context;
  4618. unsigned long flags;
  4619. unsigned char delta;
  4620. spin_lock_irqsave(&info->lock,flags);
  4621. get_signals(info);
  4622. spin_unlock_irqrestore(&info->lock,flags);
  4623. /* check for DSR/RI state change */
  4624. delta = info->old_signals ^ info->serial_signals;
  4625. info->old_signals = info->serial_signals;
  4626. if (delta & SerialSignal_DSR)
  4627. status |= MISCSTATUS_DSR_LATCHED|(info->serial_signals&SerialSignal_DSR);
  4628. if (delta & SerialSignal_RI)
  4629. status |= MISCSTATUS_RI_LATCHED|(info->serial_signals&SerialSignal_RI);
  4630. if (delta & SerialSignal_DCD)
  4631. status |= MISCSTATUS_DCD_LATCHED|(info->serial_signals&SerialSignal_DCD);
  4632. if (delta & SerialSignal_CTS)
  4633. status |= MISCSTATUS_CTS_LATCHED|(info->serial_signals&SerialSignal_CTS);
  4634. if (status)
  4635. isr_io_pin(info,status);
  4636. mod_timer(&info->status_timer, jiffies + msecs_to_jiffies(10));
  4637. }
  4638. /* Register Access Routines -
  4639. * All registers are memory mapped
  4640. */
  4641. #define CALC_REGADDR() \
  4642. unsigned char * RegAddr = (unsigned char*)(info->sca_base + Addr); \
  4643. if (info->port_num > 1) \
  4644. RegAddr += 256; /* port 0-1 SCA0, 2-3 SCA1 */ \
  4645. if ( info->port_num & 1) { \
  4646. if (Addr > 0x7f) \
  4647. RegAddr += 0x40; /* DMA access */ \
  4648. else if (Addr > 0x1f && Addr < 0x60) \
  4649. RegAddr += 0x20; /* MSCI access */ \
  4650. }
  4651. static unsigned char read_reg(SLMP_INFO * info, unsigned char Addr)
  4652. {
  4653. CALC_REGADDR();
  4654. return *RegAddr;
  4655. }
  4656. static void write_reg(SLMP_INFO * info, unsigned char Addr, unsigned char Value)
  4657. {
  4658. CALC_REGADDR();
  4659. *RegAddr = Value;
  4660. }
  4661. static u16 read_reg16(SLMP_INFO * info, unsigned char Addr)
  4662. {
  4663. CALC_REGADDR();
  4664. return *((u16 *)RegAddr);
  4665. }
  4666. static void write_reg16(SLMP_INFO * info, unsigned char Addr, u16 Value)
  4667. {
  4668. CALC_REGADDR();
  4669. *((u16 *)RegAddr) = Value;
  4670. }
  4671. static unsigned char read_status_reg(SLMP_INFO * info)
  4672. {
  4673. unsigned char *RegAddr = (unsigned char *)info->statctrl_base;
  4674. return *RegAddr;
  4675. }
  4676. static void write_control_reg(SLMP_INFO * info)
  4677. {
  4678. unsigned char *RegAddr = (unsigned char *)info->statctrl_base;
  4679. *RegAddr = info->port_array[0]->ctrlreg_value;
  4680. }
  4681. static int __devinit synclinkmp_init_one (struct pci_dev *dev,
  4682. const struct pci_device_id *ent)
  4683. {
  4684. if (pci_enable_device(dev)) {
  4685. printk("error enabling pci device %p\n", dev);
  4686. return -EIO;
  4687. }
  4688. device_init( ++synclinkmp_adapter_count, dev );
  4689. return 0;
  4690. }
  4691. static void __devexit synclinkmp_remove_one (struct pci_dev *dev)
  4692. {
  4693. }