synclinkmp.c 147 KB

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