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