synclinkmp.c 147 KB

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