synclinkmp.c 149 KB

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