synclink_gt.c 128 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074
  1. // SPDX-License-Identifier: GPL-1.0+
  2. /*
  3. * Device driver for Microgate SyncLink GT serial adapters.
  4. *
  5. * written by Paul Fulghum for Microgate Corporation
  6. * paulkf@microgate.com
  7. *
  8. * Microgate and SyncLink are trademarks of Microgate Corporation
  9. *
  10. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  11. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  12. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  13. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  14. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  15. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  16. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  17. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  18. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  19. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  20. * OF THE POSSIBILITY OF SUCH DAMAGE.
  21. */
  22. /*
  23. * DEBUG OUTPUT DEFINITIONS
  24. *
  25. * uncomment lines below to enable specific types of debug output
  26. *
  27. * DBGINFO information - most verbose output
  28. * DBGERR serious errors
  29. * DBGBH bottom half service routine debugging
  30. * DBGISR interrupt service routine debugging
  31. * DBGDATA output receive and transmit data
  32. * DBGTBUF output transmit DMA buffers and registers
  33. * DBGRBUF output receive DMA buffers and registers
  34. */
  35. #define DBGINFO(fmt) if (debug_level >= DEBUG_LEVEL_INFO) printk fmt
  36. #define DBGERR(fmt) if (debug_level >= DEBUG_LEVEL_ERROR) printk fmt
  37. #define DBGBH(fmt) if (debug_level >= DEBUG_LEVEL_BH) printk fmt
  38. #define DBGISR(fmt) if (debug_level >= DEBUG_LEVEL_ISR) printk fmt
  39. #define DBGDATA(info, buf, size, label) if (debug_level >= DEBUG_LEVEL_DATA) trace_block((info), (buf), (size), (label))
  40. /*#define DBGTBUF(info) dump_tbufs(info)*/
  41. /*#define DBGRBUF(info) dump_rbufs(info)*/
  42. #include <linux/module.h>
  43. #include <linux/errno.h>
  44. #include <linux/signal.h>
  45. #include <linux/sched.h>
  46. #include <linux/timer.h>
  47. #include <linux/interrupt.h>
  48. #include <linux/pci.h>
  49. #include <linux/tty.h>
  50. #include <linux/tty_flip.h>
  51. #include <linux/serial.h>
  52. #include <linux/major.h>
  53. #include <linux/string.h>
  54. #include <linux/fcntl.h>
  55. #include <linux/ptrace.h>
  56. #include <linux/ioport.h>
  57. #include <linux/mm.h>
  58. #include <linux/seq_file.h>
  59. #include <linux/slab.h>
  60. #include <linux/netdevice.h>
  61. #include <linux/vmalloc.h>
  62. #include <linux/init.h>
  63. #include <linux/delay.h>
  64. #include <linux/ioctl.h>
  65. #include <linux/termios.h>
  66. #include <linux/bitops.h>
  67. #include <linux/workqueue.h>
  68. #include <linux/hdlc.h>
  69. #include <linux/synclink.h>
  70. #include <asm/io.h>
  71. #include <asm/irq.h>
  72. #include <asm/dma.h>
  73. #include <asm/types.h>
  74. #include <linux/uaccess.h>
  75. #if defined(CONFIG_HDLC) || (defined(CONFIG_HDLC_MODULE) && defined(CONFIG_SYNCLINK_GT_MODULE))
  76. #define SYNCLINK_GENERIC_HDLC 1
  77. #else
  78. #define SYNCLINK_GENERIC_HDLC 0
  79. #endif
  80. /*
  81. * module identification
  82. */
  83. static char *driver_name = "SyncLink GT";
  84. static char *slgt_driver_name = "synclink_gt";
  85. static char *tty_dev_prefix = "ttySLG";
  86. MODULE_LICENSE("GPL");
  87. #define MGSL_MAGIC 0x5401
  88. #define MAX_DEVICES 32
  89. static const struct pci_device_id pci_table[] = {
  90. {PCI_VENDOR_ID_MICROGATE, SYNCLINK_GT_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
  91. {PCI_VENDOR_ID_MICROGATE, SYNCLINK_GT2_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
  92. {PCI_VENDOR_ID_MICROGATE, SYNCLINK_GT4_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
  93. {PCI_VENDOR_ID_MICROGATE, SYNCLINK_AC_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
  94. {0,}, /* terminate list */
  95. };
  96. MODULE_DEVICE_TABLE(pci, pci_table);
  97. static int init_one(struct pci_dev *dev,const struct pci_device_id *ent);
  98. static void remove_one(struct pci_dev *dev);
  99. static struct pci_driver pci_driver = {
  100. .name = "synclink_gt",
  101. .id_table = pci_table,
  102. .probe = init_one,
  103. .remove = remove_one,
  104. };
  105. static bool pci_registered;
  106. /*
  107. * module configuration and status
  108. */
  109. static struct slgt_info *slgt_device_list;
  110. static int slgt_device_count;
  111. static int ttymajor;
  112. static int debug_level;
  113. static int maxframe[MAX_DEVICES];
  114. module_param(ttymajor, int, 0);
  115. module_param(debug_level, int, 0);
  116. module_param_array(maxframe, int, NULL, 0);
  117. MODULE_PARM_DESC(ttymajor, "TTY major device number override: 0=auto assigned");
  118. MODULE_PARM_DESC(debug_level, "Debug syslog output: 0=disabled, 1 to 5=increasing detail");
  119. MODULE_PARM_DESC(maxframe, "Maximum frame size used by device (4096 to 65535)");
  120. /*
  121. * tty support and callbacks
  122. */
  123. static struct tty_driver *serial_driver;
  124. static void wait_until_sent(struct tty_struct *tty, int timeout);
  125. static void flush_buffer(struct tty_struct *tty);
  126. static void tx_release(struct tty_struct *tty);
  127. /*
  128. * generic HDLC support
  129. */
  130. #define dev_to_port(D) (dev_to_hdlc(D)->priv)
  131. /*
  132. * device specific structures, macros and functions
  133. */
  134. #define SLGT_MAX_PORTS 4
  135. #define SLGT_REG_SIZE 256
  136. /*
  137. * conditional wait facility
  138. */
  139. struct cond_wait {
  140. struct cond_wait *next;
  141. wait_queue_head_t q;
  142. wait_queue_entry_t wait;
  143. unsigned int data;
  144. };
  145. static void flush_cond_wait(struct cond_wait **head);
  146. /*
  147. * DMA buffer descriptor and access macros
  148. */
  149. struct slgt_desc
  150. {
  151. __le16 count;
  152. __le16 status;
  153. __le32 pbuf; /* physical address of data buffer */
  154. __le32 next; /* physical address of next descriptor */
  155. /* driver book keeping */
  156. char *buf; /* virtual address of data buffer */
  157. unsigned int pdesc; /* physical address of this descriptor */
  158. dma_addr_t buf_dma_addr;
  159. unsigned short buf_count;
  160. };
  161. #define set_desc_buffer(a,b) (a).pbuf = cpu_to_le32((unsigned int)(b))
  162. #define set_desc_next(a,b) (a).next = cpu_to_le32((unsigned int)(b))
  163. #define set_desc_count(a,b)(a).count = cpu_to_le16((unsigned short)(b))
  164. #define set_desc_eof(a,b) (a).status = cpu_to_le16((b) ? (le16_to_cpu((a).status) | BIT0) : (le16_to_cpu((a).status) & ~BIT0))
  165. #define set_desc_status(a, b) (a).status = cpu_to_le16((unsigned short)(b))
  166. #define desc_count(a) (le16_to_cpu((a).count))
  167. #define desc_status(a) (le16_to_cpu((a).status))
  168. #define desc_complete(a) (le16_to_cpu((a).status) & BIT15)
  169. #define desc_eof(a) (le16_to_cpu((a).status) & BIT2)
  170. #define desc_crc_error(a) (le16_to_cpu((a).status) & BIT1)
  171. #define desc_abort(a) (le16_to_cpu((a).status) & BIT0)
  172. #define desc_residue(a) ((le16_to_cpu((a).status) & 0x38) >> 3)
  173. struct _input_signal_events {
  174. int ri_up;
  175. int ri_down;
  176. int dsr_up;
  177. int dsr_down;
  178. int dcd_up;
  179. int dcd_down;
  180. int cts_up;
  181. int cts_down;
  182. };
  183. /*
  184. * device instance data structure
  185. */
  186. struct slgt_info {
  187. void *if_ptr; /* General purpose pointer (used by SPPP) */
  188. struct tty_port port;
  189. struct slgt_info *next_device; /* device list link */
  190. int magic;
  191. char device_name[25];
  192. struct pci_dev *pdev;
  193. int port_count; /* count of ports on adapter */
  194. int adapter_num; /* adapter instance number */
  195. int port_num; /* port instance number */
  196. /* array of pointers to port contexts on this adapter */
  197. struct slgt_info *port_array[SLGT_MAX_PORTS];
  198. int line; /* tty line instance number */
  199. struct mgsl_icount icount;
  200. int timeout;
  201. int x_char; /* xon/xoff character */
  202. unsigned int read_status_mask;
  203. unsigned int ignore_status_mask;
  204. wait_queue_head_t status_event_wait_q;
  205. wait_queue_head_t event_wait_q;
  206. struct timer_list tx_timer;
  207. struct timer_list rx_timer;
  208. unsigned int gpio_present;
  209. struct cond_wait *gpio_wait_q;
  210. spinlock_t lock; /* spinlock for synchronizing with ISR */
  211. struct work_struct task;
  212. u32 pending_bh;
  213. bool bh_requested;
  214. bool bh_running;
  215. int isr_overflow;
  216. bool irq_requested; /* true if IRQ requested */
  217. bool irq_occurred; /* for diagnostics use */
  218. /* device configuration */
  219. unsigned int bus_type;
  220. unsigned int irq_level;
  221. unsigned long irq_flags;
  222. unsigned char __iomem * reg_addr; /* memory mapped registers address */
  223. u32 phys_reg_addr;
  224. bool reg_addr_requested;
  225. MGSL_PARAMS params; /* communications parameters */
  226. u32 idle_mode;
  227. u32 max_frame_size; /* as set by device config */
  228. unsigned int rbuf_fill_level;
  229. unsigned int rx_pio;
  230. unsigned int if_mode;
  231. unsigned int base_clock;
  232. unsigned int xsync;
  233. unsigned int xctrl;
  234. /* device status */
  235. bool rx_enabled;
  236. bool rx_restart;
  237. bool tx_enabled;
  238. bool tx_active;
  239. unsigned char signals; /* serial signal states */
  240. int init_error; /* initialization error */
  241. unsigned char *tx_buf;
  242. int tx_count;
  243. char *flag_buf;
  244. bool drop_rts_on_tx_done;
  245. struct _input_signal_events input_signal_events;
  246. int dcd_chkcount; /* check counts to prevent */
  247. int cts_chkcount; /* too many IRQs if a signal */
  248. int dsr_chkcount; /* is floating */
  249. int ri_chkcount;
  250. char *bufs; /* virtual address of DMA buffer lists */
  251. dma_addr_t bufs_dma_addr; /* physical address of buffer descriptors */
  252. unsigned int rbuf_count;
  253. struct slgt_desc *rbufs;
  254. unsigned int rbuf_current;
  255. unsigned int rbuf_index;
  256. unsigned int rbuf_fill_index;
  257. unsigned short rbuf_fill_count;
  258. unsigned int tbuf_count;
  259. struct slgt_desc *tbufs;
  260. unsigned int tbuf_current;
  261. unsigned int tbuf_start;
  262. unsigned char *tmp_rbuf;
  263. unsigned int tmp_rbuf_count;
  264. /* SPPP/Cisco HDLC device parts */
  265. int netcount;
  266. spinlock_t netlock;
  267. #if SYNCLINK_GENERIC_HDLC
  268. struct net_device *netdev;
  269. #endif
  270. };
  271. static MGSL_PARAMS default_params = {
  272. .mode = MGSL_MODE_HDLC,
  273. .loopback = 0,
  274. .flags = HDLC_FLAG_UNDERRUN_ABORT15,
  275. .encoding = HDLC_ENCODING_NRZI_SPACE,
  276. .clock_speed = 0,
  277. .addr_filter = 0xff,
  278. .crc_type = HDLC_CRC_16_CCITT,
  279. .preamble_length = HDLC_PREAMBLE_LENGTH_8BITS,
  280. .preamble = HDLC_PREAMBLE_PATTERN_NONE,
  281. .data_rate = 9600,
  282. .data_bits = 8,
  283. .stop_bits = 1,
  284. .parity = ASYNC_PARITY_NONE
  285. };
  286. #define BH_RECEIVE 1
  287. #define BH_TRANSMIT 2
  288. #define BH_STATUS 4
  289. #define IO_PIN_SHUTDOWN_LIMIT 100
  290. #define DMABUFSIZE 256
  291. #define DESC_LIST_SIZE 4096
  292. #define MASK_PARITY BIT1
  293. #define MASK_FRAMING BIT0
  294. #define MASK_BREAK BIT14
  295. #define MASK_OVERRUN BIT4
  296. #define GSR 0x00 /* global status */
  297. #define JCR 0x04 /* JTAG control */
  298. #define IODR 0x08 /* GPIO direction */
  299. #define IOER 0x0c /* GPIO interrupt enable */
  300. #define IOVR 0x10 /* GPIO value */
  301. #define IOSR 0x14 /* GPIO interrupt status */
  302. #define TDR 0x80 /* tx data */
  303. #define RDR 0x80 /* rx data */
  304. #define TCR 0x82 /* tx control */
  305. #define TIR 0x84 /* tx idle */
  306. #define TPR 0x85 /* tx preamble */
  307. #define RCR 0x86 /* rx control */
  308. #define VCR 0x88 /* V.24 control */
  309. #define CCR 0x89 /* clock control */
  310. #define BDR 0x8a /* baud divisor */
  311. #define SCR 0x8c /* serial control */
  312. #define SSR 0x8e /* serial status */
  313. #define RDCSR 0x90 /* rx DMA control/status */
  314. #define TDCSR 0x94 /* tx DMA control/status */
  315. #define RDDAR 0x98 /* rx DMA descriptor address */
  316. #define TDDAR 0x9c /* tx DMA descriptor address */
  317. #define XSR 0x40 /* extended sync pattern */
  318. #define XCR 0x44 /* extended control */
  319. #define RXIDLE BIT14
  320. #define RXBREAK BIT14
  321. #define IRQ_TXDATA BIT13
  322. #define IRQ_TXIDLE BIT12
  323. #define IRQ_TXUNDER BIT11 /* HDLC */
  324. #define IRQ_RXDATA BIT10
  325. #define IRQ_RXIDLE BIT9 /* HDLC */
  326. #define IRQ_RXBREAK BIT9 /* async */
  327. #define IRQ_RXOVER BIT8
  328. #define IRQ_DSR BIT7
  329. #define IRQ_CTS BIT6
  330. #define IRQ_DCD BIT5
  331. #define IRQ_RI BIT4
  332. #define IRQ_ALL 0x3ff0
  333. #define IRQ_MASTER BIT0
  334. #define slgt_irq_on(info, mask) \
  335. wr_reg16((info), SCR, (unsigned short)(rd_reg16((info), SCR) | (mask)))
  336. #define slgt_irq_off(info, mask) \
  337. wr_reg16((info), SCR, (unsigned short)(rd_reg16((info), SCR) & ~(mask)))
  338. static __u8 rd_reg8(struct slgt_info *info, unsigned int addr);
  339. static void wr_reg8(struct slgt_info *info, unsigned int addr, __u8 value);
  340. static __u16 rd_reg16(struct slgt_info *info, unsigned int addr);
  341. static void wr_reg16(struct slgt_info *info, unsigned int addr, __u16 value);
  342. static __u32 rd_reg32(struct slgt_info *info, unsigned int addr);
  343. static void wr_reg32(struct slgt_info *info, unsigned int addr, __u32 value);
  344. static void msc_set_vcr(struct slgt_info *info);
  345. static int startup(struct slgt_info *info);
  346. static int block_til_ready(struct tty_struct *tty, struct file * filp,struct slgt_info *info);
  347. static void shutdown(struct slgt_info *info);
  348. static void program_hw(struct slgt_info *info);
  349. static void change_params(struct slgt_info *info);
  350. static int adapter_test(struct slgt_info *info);
  351. static void reset_port(struct slgt_info *info);
  352. static void async_mode(struct slgt_info *info);
  353. static void sync_mode(struct slgt_info *info);
  354. static void rx_stop(struct slgt_info *info);
  355. static void rx_start(struct slgt_info *info);
  356. static void reset_rbufs(struct slgt_info *info);
  357. static void free_rbufs(struct slgt_info *info, unsigned int first, unsigned int last);
  358. static bool rx_get_frame(struct slgt_info *info);
  359. static bool rx_get_buf(struct slgt_info *info);
  360. static void tx_start(struct slgt_info *info);
  361. static void tx_stop(struct slgt_info *info);
  362. static void tx_set_idle(struct slgt_info *info);
  363. static unsigned int tbuf_bytes(struct slgt_info *info);
  364. static void reset_tbufs(struct slgt_info *info);
  365. static void tdma_reset(struct slgt_info *info);
  366. static bool tx_load(struct slgt_info *info, const char *buf, unsigned int count);
  367. static void get_gtsignals(struct slgt_info *info);
  368. static void set_gtsignals(struct slgt_info *info);
  369. static void set_rate(struct slgt_info *info, u32 data_rate);
  370. static void bh_transmit(struct slgt_info *info);
  371. static void isr_txeom(struct slgt_info *info, unsigned short status);
  372. static void tx_timeout(struct timer_list *t);
  373. static void rx_timeout(struct timer_list *t);
  374. /*
  375. * ioctl handlers
  376. */
  377. static int get_stats(struct slgt_info *info, struct mgsl_icount __user *user_icount);
  378. static int get_params(struct slgt_info *info, MGSL_PARAMS __user *params);
  379. static int set_params(struct slgt_info *info, MGSL_PARAMS __user *params);
  380. static int get_txidle(struct slgt_info *info, int __user *idle_mode);
  381. static int set_txidle(struct slgt_info *info, int idle_mode);
  382. static int tx_enable(struct slgt_info *info, int enable);
  383. static int tx_abort(struct slgt_info *info);
  384. static int rx_enable(struct slgt_info *info, int enable);
  385. static int modem_input_wait(struct slgt_info *info,int arg);
  386. static int wait_mgsl_event(struct slgt_info *info, int __user *mask_ptr);
  387. static int get_interface(struct slgt_info *info, int __user *if_mode);
  388. static int set_interface(struct slgt_info *info, int if_mode);
  389. static int set_gpio(struct slgt_info *info, struct gpio_desc __user *gpio);
  390. static int get_gpio(struct slgt_info *info, struct gpio_desc __user *gpio);
  391. static int wait_gpio(struct slgt_info *info, struct gpio_desc __user *gpio);
  392. static int get_xsync(struct slgt_info *info, int __user *if_mode);
  393. static int set_xsync(struct slgt_info *info, int if_mode);
  394. static int get_xctrl(struct slgt_info *info, int __user *if_mode);
  395. static int set_xctrl(struct slgt_info *info, int if_mode);
  396. /*
  397. * driver functions
  398. */
  399. static void release_resources(struct slgt_info *info);
  400. /*
  401. * DEBUG OUTPUT CODE
  402. */
  403. #ifndef DBGINFO
  404. #define DBGINFO(fmt)
  405. #endif
  406. #ifndef DBGERR
  407. #define DBGERR(fmt)
  408. #endif
  409. #ifndef DBGBH
  410. #define DBGBH(fmt)
  411. #endif
  412. #ifndef DBGISR
  413. #define DBGISR(fmt)
  414. #endif
  415. #ifdef DBGDATA
  416. static void trace_block(struct slgt_info *info, const char *data, int count, const char *label)
  417. {
  418. int i;
  419. int linecount;
  420. printk("%s %s data:\n",info->device_name, label);
  421. while(count) {
  422. linecount = (count > 16) ? 16 : count;
  423. for(i=0; i < linecount; i++)
  424. printk("%02X ",(unsigned char)data[i]);
  425. for(;i<17;i++)
  426. printk(" ");
  427. for(i=0;i<linecount;i++) {
  428. if (data[i]>=040 && data[i]<=0176)
  429. printk("%c",data[i]);
  430. else
  431. printk(".");
  432. }
  433. printk("\n");
  434. data += linecount;
  435. count -= linecount;
  436. }
  437. }
  438. #else
  439. #define DBGDATA(info, buf, size, label)
  440. #endif
  441. #ifdef DBGTBUF
  442. static void dump_tbufs(struct slgt_info *info)
  443. {
  444. int i;
  445. printk("tbuf_current=%d\n", info->tbuf_current);
  446. for (i=0 ; i < info->tbuf_count ; i++) {
  447. printk("%d: count=%04X status=%04X\n",
  448. i, le16_to_cpu(info->tbufs[i].count), le16_to_cpu(info->tbufs[i].status));
  449. }
  450. }
  451. #else
  452. #define DBGTBUF(info)
  453. #endif
  454. #ifdef DBGRBUF
  455. static void dump_rbufs(struct slgt_info *info)
  456. {
  457. int i;
  458. printk("rbuf_current=%d\n", info->rbuf_current);
  459. for (i=0 ; i < info->rbuf_count ; i++) {
  460. printk("%d: count=%04X status=%04X\n",
  461. i, le16_to_cpu(info->rbufs[i].count), le16_to_cpu(info->rbufs[i].status));
  462. }
  463. }
  464. #else
  465. #define DBGRBUF(info)
  466. #endif
  467. static inline int sanity_check(struct slgt_info *info, char *devname, const char *name)
  468. {
  469. #ifdef SANITY_CHECK
  470. if (!info) {
  471. printk("null struct slgt_info for (%s) in %s\n", devname, name);
  472. return 1;
  473. }
  474. if (info->magic != MGSL_MAGIC) {
  475. printk("bad magic number struct slgt_info (%s) in %s\n", devname, name);
  476. return 1;
  477. }
  478. #else
  479. if (!info)
  480. return 1;
  481. #endif
  482. return 0;
  483. }
  484. /**
  485. * line discipline callback wrappers
  486. *
  487. * The wrappers maintain line discipline references
  488. * while calling into the line discipline.
  489. *
  490. * ldisc_receive_buf - pass receive data to line discipline
  491. */
  492. static void ldisc_receive_buf(struct tty_struct *tty,
  493. const __u8 *data, char *flags, int count)
  494. {
  495. struct tty_ldisc *ld;
  496. if (!tty)
  497. return;
  498. ld = tty_ldisc_ref(tty);
  499. if (ld) {
  500. if (ld->ops->receive_buf)
  501. ld->ops->receive_buf(tty, data, flags, count);
  502. tty_ldisc_deref(ld);
  503. }
  504. }
  505. /* tty callbacks */
  506. static int open(struct tty_struct *tty, struct file *filp)
  507. {
  508. struct slgt_info *info;
  509. int retval, line;
  510. unsigned long flags;
  511. line = tty->index;
  512. if (line >= slgt_device_count) {
  513. DBGERR(("%s: open with invalid line #%d.\n", driver_name, line));
  514. return -ENODEV;
  515. }
  516. info = slgt_device_list;
  517. while(info && info->line != line)
  518. info = info->next_device;
  519. if (sanity_check(info, tty->name, "open"))
  520. return -ENODEV;
  521. if (info->init_error) {
  522. DBGERR(("%s init error=%d\n", info->device_name, info->init_error));
  523. return -ENODEV;
  524. }
  525. tty->driver_data = info;
  526. info->port.tty = tty;
  527. DBGINFO(("%s open, old ref count = %d\n", info->device_name, info->port.count));
  528. mutex_lock(&info->port.mutex);
  529. info->port.low_latency = (info->port.flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  530. spin_lock_irqsave(&info->netlock, flags);
  531. if (info->netcount) {
  532. retval = -EBUSY;
  533. spin_unlock_irqrestore(&info->netlock, flags);
  534. mutex_unlock(&info->port.mutex);
  535. goto cleanup;
  536. }
  537. info->port.count++;
  538. spin_unlock_irqrestore(&info->netlock, flags);
  539. if (info->port.count == 1) {
  540. /* 1st open on this device, init hardware */
  541. retval = startup(info);
  542. if (retval < 0) {
  543. mutex_unlock(&info->port.mutex);
  544. goto cleanup;
  545. }
  546. }
  547. mutex_unlock(&info->port.mutex);
  548. retval = block_til_ready(tty, filp, info);
  549. if (retval) {
  550. DBGINFO(("%s block_til_ready rc=%d\n", info->device_name, retval));
  551. goto cleanup;
  552. }
  553. retval = 0;
  554. cleanup:
  555. if (retval) {
  556. if (tty->count == 1)
  557. info->port.tty = NULL; /* tty layer will release tty struct */
  558. if(info->port.count)
  559. info->port.count--;
  560. }
  561. DBGINFO(("%s open rc=%d\n", info->device_name, retval));
  562. return retval;
  563. }
  564. static void close(struct tty_struct *tty, struct file *filp)
  565. {
  566. struct slgt_info *info = tty->driver_data;
  567. if (sanity_check(info, tty->name, "close"))
  568. return;
  569. DBGINFO(("%s close entry, count=%d\n", info->device_name, info->port.count));
  570. if (tty_port_close_start(&info->port, tty, filp) == 0)
  571. goto cleanup;
  572. mutex_lock(&info->port.mutex);
  573. if (tty_port_initialized(&info->port))
  574. wait_until_sent(tty, info->timeout);
  575. flush_buffer(tty);
  576. tty_ldisc_flush(tty);
  577. shutdown(info);
  578. mutex_unlock(&info->port.mutex);
  579. tty_port_close_end(&info->port, tty);
  580. info->port.tty = NULL;
  581. cleanup:
  582. DBGINFO(("%s close exit, count=%d\n", tty->driver->name, info->port.count));
  583. }
  584. static void hangup(struct tty_struct *tty)
  585. {
  586. struct slgt_info *info = tty->driver_data;
  587. unsigned long flags;
  588. if (sanity_check(info, tty->name, "hangup"))
  589. return;
  590. DBGINFO(("%s hangup\n", info->device_name));
  591. flush_buffer(tty);
  592. mutex_lock(&info->port.mutex);
  593. shutdown(info);
  594. spin_lock_irqsave(&info->port.lock, flags);
  595. info->port.count = 0;
  596. info->port.tty = NULL;
  597. spin_unlock_irqrestore(&info->port.lock, flags);
  598. tty_port_set_active(&info->port, 0);
  599. mutex_unlock(&info->port.mutex);
  600. wake_up_interruptible(&info->port.open_wait);
  601. }
  602. static void set_termios(struct tty_struct *tty, struct ktermios *old_termios)
  603. {
  604. struct slgt_info *info = tty->driver_data;
  605. unsigned long flags;
  606. DBGINFO(("%s set_termios\n", tty->driver->name));
  607. change_params(info);
  608. /* Handle transition to B0 status */
  609. if ((old_termios->c_cflag & CBAUD) && !C_BAUD(tty)) {
  610. info->signals &= ~(SerialSignal_RTS | SerialSignal_DTR);
  611. spin_lock_irqsave(&info->lock,flags);
  612. set_gtsignals(info);
  613. spin_unlock_irqrestore(&info->lock,flags);
  614. }
  615. /* Handle transition away from B0 status */
  616. if (!(old_termios->c_cflag & CBAUD) && C_BAUD(tty)) {
  617. info->signals |= SerialSignal_DTR;
  618. if (!C_CRTSCTS(tty) || !tty_throttled(tty))
  619. info->signals |= SerialSignal_RTS;
  620. spin_lock_irqsave(&info->lock,flags);
  621. set_gtsignals(info);
  622. spin_unlock_irqrestore(&info->lock,flags);
  623. }
  624. /* Handle turning off CRTSCTS */
  625. if ((old_termios->c_cflag & CRTSCTS) && !C_CRTSCTS(tty)) {
  626. tty->hw_stopped = 0;
  627. tx_release(tty);
  628. }
  629. }
  630. static void update_tx_timer(struct slgt_info *info)
  631. {
  632. /*
  633. * use worst case speed of 1200bps to calculate transmit timeout
  634. * based on data in buffers (tbuf_bytes) and FIFO (128 bytes)
  635. */
  636. if (info->params.mode == MGSL_MODE_HDLC) {
  637. int timeout = (tbuf_bytes(info) * 7) + 1000;
  638. mod_timer(&info->tx_timer, jiffies + msecs_to_jiffies(timeout));
  639. }
  640. }
  641. static int write(struct tty_struct *tty,
  642. const unsigned char *buf, int count)
  643. {
  644. int ret = 0;
  645. struct slgt_info *info = tty->driver_data;
  646. unsigned long flags;
  647. if (sanity_check(info, tty->name, "write"))
  648. return -EIO;
  649. DBGINFO(("%s write count=%d\n", info->device_name, count));
  650. if (!info->tx_buf || (count > info->max_frame_size))
  651. return -EIO;
  652. if (!count || tty->stopped || tty->hw_stopped)
  653. return 0;
  654. spin_lock_irqsave(&info->lock, flags);
  655. if (info->tx_count) {
  656. /* send accumulated data from send_char() */
  657. if (!tx_load(info, info->tx_buf, info->tx_count))
  658. goto cleanup;
  659. info->tx_count = 0;
  660. }
  661. if (tx_load(info, buf, count))
  662. ret = count;
  663. cleanup:
  664. spin_unlock_irqrestore(&info->lock, flags);
  665. DBGINFO(("%s write rc=%d\n", info->device_name, ret));
  666. return ret;
  667. }
  668. static int put_char(struct tty_struct *tty, unsigned char ch)
  669. {
  670. struct slgt_info *info = tty->driver_data;
  671. unsigned long flags;
  672. int ret = 0;
  673. if (sanity_check(info, tty->name, "put_char"))
  674. return 0;
  675. DBGINFO(("%s put_char(%d)\n", info->device_name, ch));
  676. if (!info->tx_buf)
  677. return 0;
  678. spin_lock_irqsave(&info->lock,flags);
  679. if (info->tx_count < info->max_frame_size) {
  680. info->tx_buf[info->tx_count++] = ch;
  681. ret = 1;
  682. }
  683. spin_unlock_irqrestore(&info->lock,flags);
  684. return ret;
  685. }
  686. static void send_xchar(struct tty_struct *tty, char ch)
  687. {
  688. struct slgt_info *info = tty->driver_data;
  689. unsigned long flags;
  690. if (sanity_check(info, tty->name, "send_xchar"))
  691. return;
  692. DBGINFO(("%s send_xchar(%d)\n", info->device_name, ch));
  693. info->x_char = ch;
  694. if (ch) {
  695. spin_lock_irqsave(&info->lock,flags);
  696. if (!info->tx_enabled)
  697. tx_start(info);
  698. spin_unlock_irqrestore(&info->lock,flags);
  699. }
  700. }
  701. static void wait_until_sent(struct tty_struct *tty, int timeout)
  702. {
  703. struct slgt_info *info = tty->driver_data;
  704. unsigned long orig_jiffies, char_time;
  705. if (!info )
  706. return;
  707. if (sanity_check(info, tty->name, "wait_until_sent"))
  708. return;
  709. DBGINFO(("%s wait_until_sent entry\n", info->device_name));
  710. if (!tty_port_initialized(&info->port))
  711. goto exit;
  712. orig_jiffies = jiffies;
  713. /* Set check interval to 1/5 of estimated time to
  714. * send a character, and make it at least 1. The check
  715. * interval should also be less than the timeout.
  716. * Note: use tight timings here to satisfy the NIST-PCTS.
  717. */
  718. if (info->params.data_rate) {
  719. char_time = info->timeout/(32 * 5);
  720. if (!char_time)
  721. char_time++;
  722. } else
  723. char_time = 1;
  724. if (timeout)
  725. char_time = min_t(unsigned long, char_time, timeout);
  726. while (info->tx_active) {
  727. msleep_interruptible(jiffies_to_msecs(char_time));
  728. if (signal_pending(current))
  729. break;
  730. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  731. break;
  732. }
  733. exit:
  734. DBGINFO(("%s wait_until_sent exit\n", info->device_name));
  735. }
  736. static int write_room(struct tty_struct *tty)
  737. {
  738. struct slgt_info *info = tty->driver_data;
  739. int ret;
  740. if (sanity_check(info, tty->name, "write_room"))
  741. return 0;
  742. ret = (info->tx_active) ? 0 : HDLC_MAX_FRAME_SIZE;
  743. DBGINFO(("%s write_room=%d\n", info->device_name, ret));
  744. return ret;
  745. }
  746. static void flush_chars(struct tty_struct *tty)
  747. {
  748. struct slgt_info *info = tty->driver_data;
  749. unsigned long flags;
  750. if (sanity_check(info, tty->name, "flush_chars"))
  751. return;
  752. DBGINFO(("%s flush_chars entry tx_count=%d\n", info->device_name, info->tx_count));
  753. if (info->tx_count <= 0 || tty->stopped ||
  754. tty->hw_stopped || !info->tx_buf)
  755. return;
  756. DBGINFO(("%s flush_chars start transmit\n", info->device_name));
  757. spin_lock_irqsave(&info->lock,flags);
  758. if (info->tx_count && tx_load(info, info->tx_buf, info->tx_count))
  759. info->tx_count = 0;
  760. spin_unlock_irqrestore(&info->lock,flags);
  761. }
  762. static void flush_buffer(struct tty_struct *tty)
  763. {
  764. struct slgt_info *info = tty->driver_data;
  765. unsigned long flags;
  766. if (sanity_check(info, tty->name, "flush_buffer"))
  767. return;
  768. DBGINFO(("%s flush_buffer\n", info->device_name));
  769. spin_lock_irqsave(&info->lock, flags);
  770. info->tx_count = 0;
  771. spin_unlock_irqrestore(&info->lock, flags);
  772. tty_wakeup(tty);
  773. }
  774. /*
  775. * throttle (stop) transmitter
  776. */
  777. static void tx_hold(struct tty_struct *tty)
  778. {
  779. struct slgt_info *info = tty->driver_data;
  780. unsigned long flags;
  781. if (sanity_check(info, tty->name, "tx_hold"))
  782. return;
  783. DBGINFO(("%s tx_hold\n", info->device_name));
  784. spin_lock_irqsave(&info->lock,flags);
  785. if (info->tx_enabled && info->params.mode == MGSL_MODE_ASYNC)
  786. tx_stop(info);
  787. spin_unlock_irqrestore(&info->lock,flags);
  788. }
  789. /*
  790. * release (start) transmitter
  791. */
  792. static void tx_release(struct tty_struct *tty)
  793. {
  794. struct slgt_info *info = tty->driver_data;
  795. unsigned long flags;
  796. if (sanity_check(info, tty->name, "tx_release"))
  797. return;
  798. DBGINFO(("%s tx_release\n", info->device_name));
  799. spin_lock_irqsave(&info->lock, flags);
  800. if (info->tx_count && tx_load(info, info->tx_buf, info->tx_count))
  801. info->tx_count = 0;
  802. spin_unlock_irqrestore(&info->lock, flags);
  803. }
  804. /*
  805. * Service an IOCTL request
  806. *
  807. * Arguments
  808. *
  809. * tty pointer to tty instance data
  810. * cmd IOCTL command code
  811. * arg command argument/context
  812. *
  813. * Return 0 if success, otherwise error code
  814. */
  815. static int ioctl(struct tty_struct *tty,
  816. unsigned int cmd, unsigned long arg)
  817. {
  818. struct slgt_info *info = tty->driver_data;
  819. void __user *argp = (void __user *)arg;
  820. int ret;
  821. if (sanity_check(info, tty->name, "ioctl"))
  822. return -ENODEV;
  823. DBGINFO(("%s ioctl() cmd=%08X\n", info->device_name, cmd));
  824. if (cmd != TIOCMIWAIT) {
  825. if (tty_io_error(tty))
  826. return -EIO;
  827. }
  828. switch (cmd) {
  829. case MGSL_IOCWAITEVENT:
  830. return wait_mgsl_event(info, argp);
  831. case TIOCMIWAIT:
  832. return modem_input_wait(info,(int)arg);
  833. case MGSL_IOCSGPIO:
  834. return set_gpio(info, argp);
  835. case MGSL_IOCGGPIO:
  836. return get_gpio(info, argp);
  837. case MGSL_IOCWAITGPIO:
  838. return wait_gpio(info, argp);
  839. case MGSL_IOCGXSYNC:
  840. return get_xsync(info, argp);
  841. case MGSL_IOCSXSYNC:
  842. return set_xsync(info, (int)arg);
  843. case MGSL_IOCGXCTRL:
  844. return get_xctrl(info, argp);
  845. case MGSL_IOCSXCTRL:
  846. return set_xctrl(info, (int)arg);
  847. }
  848. mutex_lock(&info->port.mutex);
  849. switch (cmd) {
  850. case MGSL_IOCGPARAMS:
  851. ret = get_params(info, argp);
  852. break;
  853. case MGSL_IOCSPARAMS:
  854. ret = set_params(info, argp);
  855. break;
  856. case MGSL_IOCGTXIDLE:
  857. ret = get_txidle(info, argp);
  858. break;
  859. case MGSL_IOCSTXIDLE:
  860. ret = set_txidle(info, (int)arg);
  861. break;
  862. case MGSL_IOCTXENABLE:
  863. ret = tx_enable(info, (int)arg);
  864. break;
  865. case MGSL_IOCRXENABLE:
  866. ret = rx_enable(info, (int)arg);
  867. break;
  868. case MGSL_IOCTXABORT:
  869. ret = tx_abort(info);
  870. break;
  871. case MGSL_IOCGSTATS:
  872. ret = get_stats(info, argp);
  873. break;
  874. case MGSL_IOCGIF:
  875. ret = get_interface(info, argp);
  876. break;
  877. case MGSL_IOCSIF:
  878. ret = set_interface(info,(int)arg);
  879. break;
  880. default:
  881. ret = -ENOIOCTLCMD;
  882. }
  883. mutex_unlock(&info->port.mutex);
  884. return ret;
  885. }
  886. static int get_icount(struct tty_struct *tty,
  887. struct serial_icounter_struct *icount)
  888. {
  889. struct slgt_info *info = tty->driver_data;
  890. struct mgsl_icount cnow; /* kernel counter temps */
  891. unsigned long flags;
  892. spin_lock_irqsave(&info->lock,flags);
  893. cnow = info->icount;
  894. spin_unlock_irqrestore(&info->lock,flags);
  895. icount->cts = cnow.cts;
  896. icount->dsr = cnow.dsr;
  897. icount->rng = cnow.rng;
  898. icount->dcd = cnow.dcd;
  899. icount->rx = cnow.rx;
  900. icount->tx = cnow.tx;
  901. icount->frame = cnow.frame;
  902. icount->overrun = cnow.overrun;
  903. icount->parity = cnow.parity;
  904. icount->brk = cnow.brk;
  905. icount->buf_overrun = cnow.buf_overrun;
  906. return 0;
  907. }
  908. /*
  909. * support for 32 bit ioctl calls on 64 bit systems
  910. */
  911. #ifdef CONFIG_COMPAT
  912. static long get_params32(struct slgt_info *info, struct MGSL_PARAMS32 __user *user_params)
  913. {
  914. struct MGSL_PARAMS32 tmp_params;
  915. DBGINFO(("%s get_params32\n", info->device_name));
  916. memset(&tmp_params, 0, sizeof(tmp_params));
  917. tmp_params.mode = (compat_ulong_t)info->params.mode;
  918. tmp_params.loopback = info->params.loopback;
  919. tmp_params.flags = info->params.flags;
  920. tmp_params.encoding = info->params.encoding;
  921. tmp_params.clock_speed = (compat_ulong_t)info->params.clock_speed;
  922. tmp_params.addr_filter = info->params.addr_filter;
  923. tmp_params.crc_type = info->params.crc_type;
  924. tmp_params.preamble_length = info->params.preamble_length;
  925. tmp_params.preamble = info->params.preamble;
  926. tmp_params.data_rate = (compat_ulong_t)info->params.data_rate;
  927. tmp_params.data_bits = info->params.data_bits;
  928. tmp_params.stop_bits = info->params.stop_bits;
  929. tmp_params.parity = info->params.parity;
  930. if (copy_to_user(user_params, &tmp_params, sizeof(struct MGSL_PARAMS32)))
  931. return -EFAULT;
  932. return 0;
  933. }
  934. static long set_params32(struct slgt_info *info, struct MGSL_PARAMS32 __user *new_params)
  935. {
  936. struct MGSL_PARAMS32 tmp_params;
  937. DBGINFO(("%s set_params32\n", info->device_name));
  938. if (copy_from_user(&tmp_params, new_params, sizeof(struct MGSL_PARAMS32)))
  939. return -EFAULT;
  940. spin_lock(&info->lock);
  941. if (tmp_params.mode == MGSL_MODE_BASE_CLOCK) {
  942. info->base_clock = tmp_params.clock_speed;
  943. } else {
  944. info->params.mode = tmp_params.mode;
  945. info->params.loopback = tmp_params.loopback;
  946. info->params.flags = tmp_params.flags;
  947. info->params.encoding = tmp_params.encoding;
  948. info->params.clock_speed = tmp_params.clock_speed;
  949. info->params.addr_filter = tmp_params.addr_filter;
  950. info->params.crc_type = tmp_params.crc_type;
  951. info->params.preamble_length = tmp_params.preamble_length;
  952. info->params.preamble = tmp_params.preamble;
  953. info->params.data_rate = tmp_params.data_rate;
  954. info->params.data_bits = tmp_params.data_bits;
  955. info->params.stop_bits = tmp_params.stop_bits;
  956. info->params.parity = tmp_params.parity;
  957. }
  958. spin_unlock(&info->lock);
  959. program_hw(info);
  960. return 0;
  961. }
  962. static long slgt_compat_ioctl(struct tty_struct *tty,
  963. unsigned int cmd, unsigned long arg)
  964. {
  965. struct slgt_info *info = tty->driver_data;
  966. int rc;
  967. if (sanity_check(info, tty->name, "compat_ioctl"))
  968. return -ENODEV;
  969. DBGINFO(("%s compat_ioctl() cmd=%08X\n", info->device_name, cmd));
  970. switch (cmd) {
  971. case MGSL_IOCSPARAMS32:
  972. rc = set_params32(info, compat_ptr(arg));
  973. break;
  974. case MGSL_IOCGPARAMS32:
  975. rc = get_params32(info, compat_ptr(arg));
  976. break;
  977. case MGSL_IOCGPARAMS:
  978. case MGSL_IOCSPARAMS:
  979. case MGSL_IOCGTXIDLE:
  980. case MGSL_IOCGSTATS:
  981. case MGSL_IOCWAITEVENT:
  982. case MGSL_IOCGIF:
  983. case MGSL_IOCSGPIO:
  984. case MGSL_IOCGGPIO:
  985. case MGSL_IOCWAITGPIO:
  986. case MGSL_IOCGXSYNC:
  987. case MGSL_IOCGXCTRL:
  988. rc = ioctl(tty, cmd, (unsigned long)compat_ptr(arg));
  989. break;
  990. default:
  991. rc = ioctl(tty, cmd, arg);
  992. }
  993. DBGINFO(("%s compat_ioctl() cmd=%08X rc=%d\n", info->device_name, cmd, rc));
  994. return rc;
  995. }
  996. #else
  997. #define slgt_compat_ioctl NULL
  998. #endif /* ifdef CONFIG_COMPAT */
  999. /*
  1000. * proc fs support
  1001. */
  1002. static inline void line_info(struct seq_file *m, struct slgt_info *info)
  1003. {
  1004. char stat_buf[30];
  1005. unsigned long flags;
  1006. seq_printf(m, "%s: IO=%08X IRQ=%d MaxFrameSize=%u\n",
  1007. info->device_name, info->phys_reg_addr,
  1008. info->irq_level, info->max_frame_size);
  1009. /* output current serial signal states */
  1010. spin_lock_irqsave(&info->lock,flags);
  1011. get_gtsignals(info);
  1012. spin_unlock_irqrestore(&info->lock,flags);
  1013. stat_buf[0] = 0;
  1014. stat_buf[1] = 0;
  1015. if (info->signals & SerialSignal_RTS)
  1016. strcat(stat_buf, "|RTS");
  1017. if (info->signals & SerialSignal_CTS)
  1018. strcat(stat_buf, "|CTS");
  1019. if (info->signals & SerialSignal_DTR)
  1020. strcat(stat_buf, "|DTR");
  1021. if (info->signals & SerialSignal_DSR)
  1022. strcat(stat_buf, "|DSR");
  1023. if (info->signals & SerialSignal_DCD)
  1024. strcat(stat_buf, "|CD");
  1025. if (info->signals & SerialSignal_RI)
  1026. strcat(stat_buf, "|RI");
  1027. if (info->params.mode != MGSL_MODE_ASYNC) {
  1028. seq_printf(m, "\tHDLC txok:%d rxok:%d",
  1029. info->icount.txok, info->icount.rxok);
  1030. if (info->icount.txunder)
  1031. seq_printf(m, " txunder:%d", info->icount.txunder);
  1032. if (info->icount.txabort)
  1033. seq_printf(m, " txabort:%d", info->icount.txabort);
  1034. if (info->icount.rxshort)
  1035. seq_printf(m, " rxshort:%d", info->icount.rxshort);
  1036. if (info->icount.rxlong)
  1037. seq_printf(m, " rxlong:%d", info->icount.rxlong);
  1038. if (info->icount.rxover)
  1039. seq_printf(m, " rxover:%d", info->icount.rxover);
  1040. if (info->icount.rxcrc)
  1041. seq_printf(m, " rxcrc:%d", info->icount.rxcrc);
  1042. } else {
  1043. seq_printf(m, "\tASYNC tx:%d rx:%d",
  1044. info->icount.tx, info->icount.rx);
  1045. if (info->icount.frame)
  1046. seq_printf(m, " fe:%d", info->icount.frame);
  1047. if (info->icount.parity)
  1048. seq_printf(m, " pe:%d", info->icount.parity);
  1049. if (info->icount.brk)
  1050. seq_printf(m, " brk:%d", info->icount.brk);
  1051. if (info->icount.overrun)
  1052. seq_printf(m, " oe:%d", info->icount.overrun);
  1053. }
  1054. /* Append serial signal status to end */
  1055. seq_printf(m, " %s\n", stat_buf+1);
  1056. seq_printf(m, "\ttxactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
  1057. info->tx_active,info->bh_requested,info->bh_running,
  1058. info->pending_bh);
  1059. }
  1060. /* Called to print information about devices
  1061. */
  1062. static int synclink_gt_proc_show(struct seq_file *m, void *v)
  1063. {
  1064. struct slgt_info *info;
  1065. seq_puts(m, "synclink_gt driver\n");
  1066. info = slgt_device_list;
  1067. while( info ) {
  1068. line_info(m, info);
  1069. info = info->next_device;
  1070. }
  1071. return 0;
  1072. }
  1073. /*
  1074. * return count of bytes in transmit buffer
  1075. */
  1076. static int chars_in_buffer(struct tty_struct *tty)
  1077. {
  1078. struct slgt_info *info = tty->driver_data;
  1079. int count;
  1080. if (sanity_check(info, tty->name, "chars_in_buffer"))
  1081. return 0;
  1082. count = tbuf_bytes(info);
  1083. DBGINFO(("%s chars_in_buffer()=%d\n", info->device_name, count));
  1084. return count;
  1085. }
  1086. /*
  1087. * signal remote device to throttle send data (our receive data)
  1088. */
  1089. static void throttle(struct tty_struct * tty)
  1090. {
  1091. struct slgt_info *info = tty->driver_data;
  1092. unsigned long flags;
  1093. if (sanity_check(info, tty->name, "throttle"))
  1094. return;
  1095. DBGINFO(("%s throttle\n", info->device_name));
  1096. if (I_IXOFF(tty))
  1097. send_xchar(tty, STOP_CHAR(tty));
  1098. if (C_CRTSCTS(tty)) {
  1099. spin_lock_irqsave(&info->lock,flags);
  1100. info->signals &= ~SerialSignal_RTS;
  1101. set_gtsignals(info);
  1102. spin_unlock_irqrestore(&info->lock,flags);
  1103. }
  1104. }
  1105. /*
  1106. * signal remote device to stop throttling send data (our receive data)
  1107. */
  1108. static void unthrottle(struct tty_struct * tty)
  1109. {
  1110. struct slgt_info *info = tty->driver_data;
  1111. unsigned long flags;
  1112. if (sanity_check(info, tty->name, "unthrottle"))
  1113. return;
  1114. DBGINFO(("%s unthrottle\n", info->device_name));
  1115. if (I_IXOFF(tty)) {
  1116. if (info->x_char)
  1117. info->x_char = 0;
  1118. else
  1119. send_xchar(tty, START_CHAR(tty));
  1120. }
  1121. if (C_CRTSCTS(tty)) {
  1122. spin_lock_irqsave(&info->lock,flags);
  1123. info->signals |= SerialSignal_RTS;
  1124. set_gtsignals(info);
  1125. spin_unlock_irqrestore(&info->lock,flags);
  1126. }
  1127. }
  1128. /*
  1129. * set or clear transmit break condition
  1130. * break_state -1=set break condition, 0=clear
  1131. */
  1132. static int set_break(struct tty_struct *tty, int break_state)
  1133. {
  1134. struct slgt_info *info = tty->driver_data;
  1135. unsigned short value;
  1136. unsigned long flags;
  1137. if (sanity_check(info, tty->name, "set_break"))
  1138. return -EINVAL;
  1139. DBGINFO(("%s set_break(%d)\n", info->device_name, break_state));
  1140. spin_lock_irqsave(&info->lock,flags);
  1141. value = rd_reg16(info, TCR);
  1142. if (break_state == -1)
  1143. value |= BIT6;
  1144. else
  1145. value &= ~BIT6;
  1146. wr_reg16(info, TCR, value);
  1147. spin_unlock_irqrestore(&info->lock,flags);
  1148. return 0;
  1149. }
  1150. #if SYNCLINK_GENERIC_HDLC
  1151. /**
  1152. * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
  1153. * set encoding and frame check sequence (FCS) options
  1154. *
  1155. * dev pointer to network device structure
  1156. * encoding serial encoding setting
  1157. * parity FCS setting
  1158. *
  1159. * returns 0 if success, otherwise error code
  1160. */
  1161. static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
  1162. unsigned short parity)
  1163. {
  1164. struct slgt_info *info = dev_to_port(dev);
  1165. unsigned char new_encoding;
  1166. unsigned short new_crctype;
  1167. /* return error if TTY interface open */
  1168. if (info->port.count)
  1169. return -EBUSY;
  1170. DBGINFO(("%s hdlcdev_attach\n", info->device_name));
  1171. switch (encoding)
  1172. {
  1173. case ENCODING_NRZ: new_encoding = HDLC_ENCODING_NRZ; break;
  1174. case ENCODING_NRZI: new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
  1175. case ENCODING_FM_MARK: new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
  1176. case ENCODING_FM_SPACE: new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
  1177. case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
  1178. default: return -EINVAL;
  1179. }
  1180. switch (parity)
  1181. {
  1182. case PARITY_NONE: new_crctype = HDLC_CRC_NONE; break;
  1183. case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
  1184. case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
  1185. default: return -EINVAL;
  1186. }
  1187. info->params.encoding = new_encoding;
  1188. info->params.crc_type = new_crctype;
  1189. /* if network interface up, reprogram hardware */
  1190. if (info->netcount)
  1191. program_hw(info);
  1192. return 0;
  1193. }
  1194. /**
  1195. * called by generic HDLC layer to send frame
  1196. *
  1197. * skb socket buffer containing HDLC frame
  1198. * dev pointer to network device structure
  1199. */
  1200. static netdev_tx_t hdlcdev_xmit(struct sk_buff *skb,
  1201. struct net_device *dev)
  1202. {
  1203. struct slgt_info *info = dev_to_port(dev);
  1204. unsigned long flags;
  1205. DBGINFO(("%s hdlc_xmit\n", dev->name));
  1206. if (!skb->len)
  1207. return NETDEV_TX_OK;
  1208. /* stop sending until this frame completes */
  1209. netif_stop_queue(dev);
  1210. /* update network statistics */
  1211. dev->stats.tx_packets++;
  1212. dev->stats.tx_bytes += skb->len;
  1213. /* save start time for transmit timeout detection */
  1214. netif_trans_update(dev);
  1215. spin_lock_irqsave(&info->lock, flags);
  1216. tx_load(info, skb->data, skb->len);
  1217. spin_unlock_irqrestore(&info->lock, flags);
  1218. /* done with socket buffer, so free it */
  1219. dev_kfree_skb(skb);
  1220. return NETDEV_TX_OK;
  1221. }
  1222. /**
  1223. * called by network layer when interface enabled
  1224. * claim resources and initialize hardware
  1225. *
  1226. * dev pointer to network device structure
  1227. *
  1228. * returns 0 if success, otherwise error code
  1229. */
  1230. static int hdlcdev_open(struct net_device *dev)
  1231. {
  1232. struct slgt_info *info = dev_to_port(dev);
  1233. int rc;
  1234. unsigned long flags;
  1235. if (!try_module_get(THIS_MODULE))
  1236. return -EBUSY;
  1237. DBGINFO(("%s hdlcdev_open\n", dev->name));
  1238. /* generic HDLC layer open processing */
  1239. rc = hdlc_open(dev);
  1240. if (rc)
  1241. return rc;
  1242. /* arbitrate between network and tty opens */
  1243. spin_lock_irqsave(&info->netlock, flags);
  1244. if (info->port.count != 0 || info->netcount != 0) {
  1245. DBGINFO(("%s hdlc_open busy\n", dev->name));
  1246. spin_unlock_irqrestore(&info->netlock, flags);
  1247. return -EBUSY;
  1248. }
  1249. info->netcount=1;
  1250. spin_unlock_irqrestore(&info->netlock, flags);
  1251. /* claim resources and init adapter */
  1252. if ((rc = startup(info)) != 0) {
  1253. spin_lock_irqsave(&info->netlock, flags);
  1254. info->netcount=0;
  1255. spin_unlock_irqrestore(&info->netlock, flags);
  1256. return rc;
  1257. }
  1258. /* assert RTS and DTR, apply hardware settings */
  1259. info->signals |= SerialSignal_RTS | SerialSignal_DTR;
  1260. program_hw(info);
  1261. /* enable network layer transmit */
  1262. netif_trans_update(dev);
  1263. netif_start_queue(dev);
  1264. /* inform generic HDLC layer of current DCD status */
  1265. spin_lock_irqsave(&info->lock, flags);
  1266. get_gtsignals(info);
  1267. spin_unlock_irqrestore(&info->lock, flags);
  1268. if (info->signals & SerialSignal_DCD)
  1269. netif_carrier_on(dev);
  1270. else
  1271. netif_carrier_off(dev);
  1272. return 0;
  1273. }
  1274. /**
  1275. * called by network layer when interface is disabled
  1276. * shutdown hardware and release resources
  1277. *
  1278. * dev pointer to network device structure
  1279. *
  1280. * returns 0 if success, otherwise error code
  1281. */
  1282. static int hdlcdev_close(struct net_device *dev)
  1283. {
  1284. struct slgt_info *info = dev_to_port(dev);
  1285. unsigned long flags;
  1286. DBGINFO(("%s hdlcdev_close\n", dev->name));
  1287. netif_stop_queue(dev);
  1288. /* shutdown adapter and release resources */
  1289. shutdown(info);
  1290. hdlc_close(dev);
  1291. spin_lock_irqsave(&info->netlock, flags);
  1292. info->netcount=0;
  1293. spin_unlock_irqrestore(&info->netlock, flags);
  1294. module_put(THIS_MODULE);
  1295. return 0;
  1296. }
  1297. /**
  1298. * called by network layer to process IOCTL call to network device
  1299. *
  1300. * dev pointer to network device structure
  1301. * ifr pointer to network interface request structure
  1302. * cmd IOCTL command code
  1303. *
  1304. * returns 0 if success, otherwise error code
  1305. */
  1306. static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1307. {
  1308. const size_t size = sizeof(sync_serial_settings);
  1309. sync_serial_settings new_line;
  1310. sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
  1311. struct slgt_info *info = dev_to_port(dev);
  1312. unsigned int flags;
  1313. DBGINFO(("%s hdlcdev_ioctl\n", dev->name));
  1314. /* return error if TTY interface open */
  1315. if (info->port.count)
  1316. return -EBUSY;
  1317. if (cmd != SIOCWANDEV)
  1318. return hdlc_ioctl(dev, ifr, cmd);
  1319. memset(&new_line, 0, sizeof(new_line));
  1320. switch(ifr->ifr_settings.type) {
  1321. case IF_GET_IFACE: /* return current sync_serial_settings */
  1322. ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
  1323. if (ifr->ifr_settings.size < size) {
  1324. ifr->ifr_settings.size = size; /* data size wanted */
  1325. return -ENOBUFS;
  1326. }
  1327. flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
  1328. HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
  1329. HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
  1330. HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
  1331. switch (flags){
  1332. case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
  1333. case (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_INT; break;
  1334. case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_TXINT; break;
  1335. case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
  1336. default: new_line.clock_type = CLOCK_DEFAULT;
  1337. }
  1338. new_line.clock_rate = info->params.clock_speed;
  1339. new_line.loopback = info->params.loopback ? 1:0;
  1340. if (copy_to_user(line, &new_line, size))
  1341. return -EFAULT;
  1342. return 0;
  1343. case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
  1344. if(!capable(CAP_NET_ADMIN))
  1345. return -EPERM;
  1346. if (copy_from_user(&new_line, line, size))
  1347. return -EFAULT;
  1348. switch (new_line.clock_type)
  1349. {
  1350. case CLOCK_EXT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
  1351. case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
  1352. case CLOCK_INT: flags = HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG; break;
  1353. case CLOCK_TXINT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG; break;
  1354. case CLOCK_DEFAULT: flags = info->params.flags &
  1355. (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
  1356. HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
  1357. HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
  1358. HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN); break;
  1359. default: return -EINVAL;
  1360. }
  1361. if (new_line.loopback != 0 && new_line.loopback != 1)
  1362. return -EINVAL;
  1363. info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
  1364. HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
  1365. HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
  1366. HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
  1367. info->params.flags |= flags;
  1368. info->params.loopback = new_line.loopback;
  1369. if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
  1370. info->params.clock_speed = new_line.clock_rate;
  1371. else
  1372. info->params.clock_speed = 0;
  1373. /* if network interface up, reprogram hardware */
  1374. if (info->netcount)
  1375. program_hw(info);
  1376. return 0;
  1377. default:
  1378. return hdlc_ioctl(dev, ifr, cmd);
  1379. }
  1380. }
  1381. /**
  1382. * called by network layer when transmit timeout is detected
  1383. *
  1384. * dev pointer to network device structure
  1385. */
  1386. static void hdlcdev_tx_timeout(struct net_device *dev)
  1387. {
  1388. struct slgt_info *info = dev_to_port(dev);
  1389. unsigned long flags;
  1390. DBGINFO(("%s hdlcdev_tx_timeout\n", dev->name));
  1391. dev->stats.tx_errors++;
  1392. dev->stats.tx_aborted_errors++;
  1393. spin_lock_irqsave(&info->lock,flags);
  1394. tx_stop(info);
  1395. spin_unlock_irqrestore(&info->lock,flags);
  1396. netif_wake_queue(dev);
  1397. }
  1398. /**
  1399. * called by device driver when transmit completes
  1400. * reenable network layer transmit if stopped
  1401. *
  1402. * info pointer to device instance information
  1403. */
  1404. static void hdlcdev_tx_done(struct slgt_info *info)
  1405. {
  1406. if (netif_queue_stopped(info->netdev))
  1407. netif_wake_queue(info->netdev);
  1408. }
  1409. /**
  1410. * called by device driver when frame received
  1411. * pass frame to network layer
  1412. *
  1413. * info pointer to device instance information
  1414. * buf pointer to buffer contianing frame data
  1415. * size count of data bytes in buf
  1416. */
  1417. static void hdlcdev_rx(struct slgt_info *info, char *buf, int size)
  1418. {
  1419. struct sk_buff *skb = dev_alloc_skb(size);
  1420. struct net_device *dev = info->netdev;
  1421. DBGINFO(("%s hdlcdev_rx\n", dev->name));
  1422. if (skb == NULL) {
  1423. DBGERR(("%s: can't alloc skb, drop packet\n", dev->name));
  1424. dev->stats.rx_dropped++;
  1425. return;
  1426. }
  1427. skb_put_data(skb, buf, size);
  1428. skb->protocol = hdlc_type_trans(skb, dev);
  1429. dev->stats.rx_packets++;
  1430. dev->stats.rx_bytes += size;
  1431. netif_rx(skb);
  1432. }
  1433. static const struct net_device_ops hdlcdev_ops = {
  1434. .ndo_open = hdlcdev_open,
  1435. .ndo_stop = hdlcdev_close,
  1436. .ndo_start_xmit = hdlc_start_xmit,
  1437. .ndo_do_ioctl = hdlcdev_ioctl,
  1438. .ndo_tx_timeout = hdlcdev_tx_timeout,
  1439. };
  1440. /**
  1441. * called by device driver when adding device instance
  1442. * do generic HDLC initialization
  1443. *
  1444. * info pointer to device instance information
  1445. *
  1446. * returns 0 if success, otherwise error code
  1447. */
  1448. static int hdlcdev_init(struct slgt_info *info)
  1449. {
  1450. int rc;
  1451. struct net_device *dev;
  1452. hdlc_device *hdlc;
  1453. /* allocate and initialize network and HDLC layer objects */
  1454. dev = alloc_hdlcdev(info);
  1455. if (!dev) {
  1456. printk(KERN_ERR "%s hdlc device alloc failure\n", info->device_name);
  1457. return -ENOMEM;
  1458. }
  1459. /* for network layer reporting purposes only */
  1460. dev->mem_start = info->phys_reg_addr;
  1461. dev->mem_end = info->phys_reg_addr + SLGT_REG_SIZE - 1;
  1462. dev->irq = info->irq_level;
  1463. /* network layer callbacks and settings */
  1464. dev->netdev_ops = &hdlcdev_ops;
  1465. dev->watchdog_timeo = 10 * HZ;
  1466. dev->tx_queue_len = 50;
  1467. /* generic HDLC layer callbacks and settings */
  1468. hdlc = dev_to_hdlc(dev);
  1469. hdlc->attach = hdlcdev_attach;
  1470. hdlc->xmit = hdlcdev_xmit;
  1471. /* register objects with HDLC layer */
  1472. rc = register_hdlc_device(dev);
  1473. if (rc) {
  1474. printk(KERN_WARNING "%s:unable to register hdlc device\n",__FILE__);
  1475. free_netdev(dev);
  1476. return rc;
  1477. }
  1478. info->netdev = dev;
  1479. return 0;
  1480. }
  1481. /**
  1482. * called by device driver when removing device instance
  1483. * do generic HDLC cleanup
  1484. *
  1485. * info pointer to device instance information
  1486. */
  1487. static void hdlcdev_exit(struct slgt_info *info)
  1488. {
  1489. unregister_hdlc_device(info->netdev);
  1490. free_netdev(info->netdev);
  1491. info->netdev = NULL;
  1492. }
  1493. #endif /* ifdef CONFIG_HDLC */
  1494. /*
  1495. * get async data from rx DMA buffers
  1496. */
  1497. static void rx_async(struct slgt_info *info)
  1498. {
  1499. struct mgsl_icount *icount = &info->icount;
  1500. unsigned int start, end;
  1501. unsigned char *p;
  1502. unsigned char status;
  1503. struct slgt_desc *bufs = info->rbufs;
  1504. int i, count;
  1505. int chars = 0;
  1506. int stat;
  1507. unsigned char ch;
  1508. start = end = info->rbuf_current;
  1509. while(desc_complete(bufs[end])) {
  1510. count = desc_count(bufs[end]) - info->rbuf_index;
  1511. p = bufs[end].buf + info->rbuf_index;
  1512. DBGISR(("%s rx_async count=%d\n", info->device_name, count));
  1513. DBGDATA(info, p, count, "rx");
  1514. for(i=0 ; i < count; i+=2, p+=2) {
  1515. ch = *p;
  1516. icount->rx++;
  1517. stat = 0;
  1518. status = *(p + 1) & (BIT1 + BIT0);
  1519. if (status) {
  1520. if (status & BIT1)
  1521. icount->parity++;
  1522. else if (status & BIT0)
  1523. icount->frame++;
  1524. /* discard char if tty control flags say so */
  1525. if (status & info->ignore_status_mask)
  1526. continue;
  1527. if (status & BIT1)
  1528. stat = TTY_PARITY;
  1529. else if (status & BIT0)
  1530. stat = TTY_FRAME;
  1531. }
  1532. tty_insert_flip_char(&info->port, ch, stat);
  1533. chars++;
  1534. }
  1535. if (i < count) {
  1536. /* receive buffer not completed */
  1537. info->rbuf_index += i;
  1538. mod_timer(&info->rx_timer, jiffies + 1);
  1539. break;
  1540. }
  1541. info->rbuf_index = 0;
  1542. free_rbufs(info, end, end);
  1543. if (++end == info->rbuf_count)
  1544. end = 0;
  1545. /* if entire list searched then no frame available */
  1546. if (end == start)
  1547. break;
  1548. }
  1549. if (chars)
  1550. tty_flip_buffer_push(&info->port);
  1551. }
  1552. /*
  1553. * return next bottom half action to perform
  1554. */
  1555. static int bh_action(struct slgt_info *info)
  1556. {
  1557. unsigned long flags;
  1558. int rc;
  1559. spin_lock_irqsave(&info->lock,flags);
  1560. if (info->pending_bh & BH_RECEIVE) {
  1561. info->pending_bh &= ~BH_RECEIVE;
  1562. rc = BH_RECEIVE;
  1563. } else if (info->pending_bh & BH_TRANSMIT) {
  1564. info->pending_bh &= ~BH_TRANSMIT;
  1565. rc = BH_TRANSMIT;
  1566. } else if (info->pending_bh & BH_STATUS) {
  1567. info->pending_bh &= ~BH_STATUS;
  1568. rc = BH_STATUS;
  1569. } else {
  1570. /* Mark BH routine as complete */
  1571. info->bh_running = false;
  1572. info->bh_requested = false;
  1573. rc = 0;
  1574. }
  1575. spin_unlock_irqrestore(&info->lock,flags);
  1576. return rc;
  1577. }
  1578. /*
  1579. * perform bottom half processing
  1580. */
  1581. static void bh_handler(struct work_struct *work)
  1582. {
  1583. struct slgt_info *info = container_of(work, struct slgt_info, task);
  1584. int action;
  1585. info->bh_running = true;
  1586. while((action = bh_action(info))) {
  1587. switch (action) {
  1588. case BH_RECEIVE:
  1589. DBGBH(("%s bh receive\n", info->device_name));
  1590. switch(info->params.mode) {
  1591. case MGSL_MODE_ASYNC:
  1592. rx_async(info);
  1593. break;
  1594. case MGSL_MODE_HDLC:
  1595. while(rx_get_frame(info));
  1596. break;
  1597. case MGSL_MODE_RAW:
  1598. case MGSL_MODE_MONOSYNC:
  1599. case MGSL_MODE_BISYNC:
  1600. case MGSL_MODE_XSYNC:
  1601. while(rx_get_buf(info));
  1602. break;
  1603. }
  1604. /* restart receiver if rx DMA buffers exhausted */
  1605. if (info->rx_restart)
  1606. rx_start(info);
  1607. break;
  1608. case BH_TRANSMIT:
  1609. bh_transmit(info);
  1610. break;
  1611. case BH_STATUS:
  1612. DBGBH(("%s bh status\n", info->device_name));
  1613. info->ri_chkcount = 0;
  1614. info->dsr_chkcount = 0;
  1615. info->dcd_chkcount = 0;
  1616. info->cts_chkcount = 0;
  1617. break;
  1618. default:
  1619. DBGBH(("%s unknown action\n", info->device_name));
  1620. break;
  1621. }
  1622. }
  1623. DBGBH(("%s bh_handler exit\n", info->device_name));
  1624. }
  1625. static void bh_transmit(struct slgt_info *info)
  1626. {
  1627. struct tty_struct *tty = info->port.tty;
  1628. DBGBH(("%s bh_transmit\n", info->device_name));
  1629. if (tty)
  1630. tty_wakeup(tty);
  1631. }
  1632. static void dsr_change(struct slgt_info *info, unsigned short status)
  1633. {
  1634. if (status & BIT3) {
  1635. info->signals |= SerialSignal_DSR;
  1636. info->input_signal_events.dsr_up++;
  1637. } else {
  1638. info->signals &= ~SerialSignal_DSR;
  1639. info->input_signal_events.dsr_down++;
  1640. }
  1641. DBGISR(("dsr_change %s signals=%04X\n", info->device_name, info->signals));
  1642. if ((info->dsr_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
  1643. slgt_irq_off(info, IRQ_DSR);
  1644. return;
  1645. }
  1646. info->icount.dsr++;
  1647. wake_up_interruptible(&info->status_event_wait_q);
  1648. wake_up_interruptible(&info->event_wait_q);
  1649. info->pending_bh |= BH_STATUS;
  1650. }
  1651. static void cts_change(struct slgt_info *info, unsigned short status)
  1652. {
  1653. if (status & BIT2) {
  1654. info->signals |= SerialSignal_CTS;
  1655. info->input_signal_events.cts_up++;
  1656. } else {
  1657. info->signals &= ~SerialSignal_CTS;
  1658. info->input_signal_events.cts_down++;
  1659. }
  1660. DBGISR(("cts_change %s signals=%04X\n", info->device_name, info->signals));
  1661. if ((info->cts_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
  1662. slgt_irq_off(info, IRQ_CTS);
  1663. return;
  1664. }
  1665. info->icount.cts++;
  1666. wake_up_interruptible(&info->status_event_wait_q);
  1667. wake_up_interruptible(&info->event_wait_q);
  1668. info->pending_bh |= BH_STATUS;
  1669. if (tty_port_cts_enabled(&info->port)) {
  1670. if (info->port.tty) {
  1671. if (info->port.tty->hw_stopped) {
  1672. if (info->signals & SerialSignal_CTS) {
  1673. info->port.tty->hw_stopped = 0;
  1674. info->pending_bh |= BH_TRANSMIT;
  1675. return;
  1676. }
  1677. } else {
  1678. if (!(info->signals & SerialSignal_CTS))
  1679. info->port.tty->hw_stopped = 1;
  1680. }
  1681. }
  1682. }
  1683. }
  1684. static void dcd_change(struct slgt_info *info, unsigned short status)
  1685. {
  1686. if (status & BIT1) {
  1687. info->signals |= SerialSignal_DCD;
  1688. info->input_signal_events.dcd_up++;
  1689. } else {
  1690. info->signals &= ~SerialSignal_DCD;
  1691. info->input_signal_events.dcd_down++;
  1692. }
  1693. DBGISR(("dcd_change %s signals=%04X\n", info->device_name, info->signals));
  1694. if ((info->dcd_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
  1695. slgt_irq_off(info, IRQ_DCD);
  1696. return;
  1697. }
  1698. info->icount.dcd++;
  1699. #if SYNCLINK_GENERIC_HDLC
  1700. if (info->netcount) {
  1701. if (info->signals & SerialSignal_DCD)
  1702. netif_carrier_on(info->netdev);
  1703. else
  1704. netif_carrier_off(info->netdev);
  1705. }
  1706. #endif
  1707. wake_up_interruptible(&info->status_event_wait_q);
  1708. wake_up_interruptible(&info->event_wait_q);
  1709. info->pending_bh |= BH_STATUS;
  1710. if (tty_port_check_carrier(&info->port)) {
  1711. if (info->signals & SerialSignal_DCD)
  1712. wake_up_interruptible(&info->port.open_wait);
  1713. else {
  1714. if (info->port.tty)
  1715. tty_hangup(info->port.tty);
  1716. }
  1717. }
  1718. }
  1719. static void ri_change(struct slgt_info *info, unsigned short status)
  1720. {
  1721. if (status & BIT0) {
  1722. info->signals |= SerialSignal_RI;
  1723. info->input_signal_events.ri_up++;
  1724. } else {
  1725. info->signals &= ~SerialSignal_RI;
  1726. info->input_signal_events.ri_down++;
  1727. }
  1728. DBGISR(("ri_change %s signals=%04X\n", info->device_name, info->signals));
  1729. if ((info->ri_chkcount)++ == IO_PIN_SHUTDOWN_LIMIT) {
  1730. slgt_irq_off(info, IRQ_RI);
  1731. return;
  1732. }
  1733. info->icount.rng++;
  1734. wake_up_interruptible(&info->status_event_wait_q);
  1735. wake_up_interruptible(&info->event_wait_q);
  1736. info->pending_bh |= BH_STATUS;
  1737. }
  1738. static void isr_rxdata(struct slgt_info *info)
  1739. {
  1740. unsigned int count = info->rbuf_fill_count;
  1741. unsigned int i = info->rbuf_fill_index;
  1742. unsigned short reg;
  1743. while (rd_reg16(info, SSR) & IRQ_RXDATA) {
  1744. reg = rd_reg16(info, RDR);
  1745. DBGISR(("isr_rxdata %s RDR=%04X\n", info->device_name, reg));
  1746. if (desc_complete(info->rbufs[i])) {
  1747. /* all buffers full */
  1748. rx_stop(info);
  1749. info->rx_restart = 1;
  1750. continue;
  1751. }
  1752. info->rbufs[i].buf[count++] = (unsigned char)reg;
  1753. /* async mode saves status byte to buffer for each data byte */
  1754. if (info->params.mode == MGSL_MODE_ASYNC)
  1755. info->rbufs[i].buf[count++] = (unsigned char)(reg >> 8);
  1756. if (count == info->rbuf_fill_level || (reg & BIT10)) {
  1757. /* buffer full or end of frame */
  1758. set_desc_count(info->rbufs[i], count);
  1759. set_desc_status(info->rbufs[i], BIT15 | (reg >> 8));
  1760. info->rbuf_fill_count = count = 0;
  1761. if (++i == info->rbuf_count)
  1762. i = 0;
  1763. info->pending_bh |= BH_RECEIVE;
  1764. }
  1765. }
  1766. info->rbuf_fill_index = i;
  1767. info->rbuf_fill_count = count;
  1768. }
  1769. static void isr_serial(struct slgt_info *info)
  1770. {
  1771. unsigned short status = rd_reg16(info, SSR);
  1772. DBGISR(("%s isr_serial status=%04X\n", info->device_name, status));
  1773. wr_reg16(info, SSR, status); /* clear pending */
  1774. info->irq_occurred = true;
  1775. if (info->params.mode == MGSL_MODE_ASYNC) {
  1776. if (status & IRQ_TXIDLE) {
  1777. if (info->tx_active)
  1778. isr_txeom(info, status);
  1779. }
  1780. if (info->rx_pio && (status & IRQ_RXDATA))
  1781. isr_rxdata(info);
  1782. if ((status & IRQ_RXBREAK) && (status & RXBREAK)) {
  1783. info->icount.brk++;
  1784. /* process break detection if tty control allows */
  1785. if (info->port.tty) {
  1786. if (!(status & info->ignore_status_mask)) {
  1787. if (info->read_status_mask & MASK_BREAK) {
  1788. tty_insert_flip_char(&info->port, 0, TTY_BREAK);
  1789. if (info->port.flags & ASYNC_SAK)
  1790. do_SAK(info->port.tty);
  1791. }
  1792. }
  1793. }
  1794. }
  1795. } else {
  1796. if (status & (IRQ_TXIDLE + IRQ_TXUNDER))
  1797. isr_txeom(info, status);
  1798. if (info->rx_pio && (status & IRQ_RXDATA))
  1799. isr_rxdata(info);
  1800. if (status & IRQ_RXIDLE) {
  1801. if (status & RXIDLE)
  1802. info->icount.rxidle++;
  1803. else
  1804. info->icount.exithunt++;
  1805. wake_up_interruptible(&info->event_wait_q);
  1806. }
  1807. if (status & IRQ_RXOVER)
  1808. rx_start(info);
  1809. }
  1810. if (status & IRQ_DSR)
  1811. dsr_change(info, status);
  1812. if (status & IRQ_CTS)
  1813. cts_change(info, status);
  1814. if (status & IRQ_DCD)
  1815. dcd_change(info, status);
  1816. if (status & IRQ_RI)
  1817. ri_change(info, status);
  1818. }
  1819. static void isr_rdma(struct slgt_info *info)
  1820. {
  1821. unsigned int status = rd_reg32(info, RDCSR);
  1822. DBGISR(("%s isr_rdma status=%08x\n", info->device_name, status));
  1823. /* RDCSR (rx DMA control/status)
  1824. *
  1825. * 31..07 reserved
  1826. * 06 save status byte to DMA buffer
  1827. * 05 error
  1828. * 04 eol (end of list)
  1829. * 03 eob (end of buffer)
  1830. * 02 IRQ enable
  1831. * 01 reset
  1832. * 00 enable
  1833. */
  1834. wr_reg32(info, RDCSR, status); /* clear pending */
  1835. if (status & (BIT5 + BIT4)) {
  1836. DBGISR(("%s isr_rdma rx_restart=1\n", info->device_name));
  1837. info->rx_restart = true;
  1838. }
  1839. info->pending_bh |= BH_RECEIVE;
  1840. }
  1841. static void isr_tdma(struct slgt_info *info)
  1842. {
  1843. unsigned int status = rd_reg32(info, TDCSR);
  1844. DBGISR(("%s isr_tdma status=%08x\n", info->device_name, status));
  1845. /* TDCSR (tx DMA control/status)
  1846. *
  1847. * 31..06 reserved
  1848. * 05 error
  1849. * 04 eol (end of list)
  1850. * 03 eob (end of buffer)
  1851. * 02 IRQ enable
  1852. * 01 reset
  1853. * 00 enable
  1854. */
  1855. wr_reg32(info, TDCSR, status); /* clear pending */
  1856. if (status & (BIT5 + BIT4 + BIT3)) {
  1857. // another transmit buffer has completed
  1858. // run bottom half to get more send data from user
  1859. info->pending_bh |= BH_TRANSMIT;
  1860. }
  1861. }
  1862. /*
  1863. * return true if there are unsent tx DMA buffers, otherwise false
  1864. *
  1865. * if there are unsent buffers then info->tbuf_start
  1866. * is set to index of first unsent buffer
  1867. */
  1868. static bool unsent_tbufs(struct slgt_info *info)
  1869. {
  1870. unsigned int i = info->tbuf_current;
  1871. bool rc = false;
  1872. /*
  1873. * search backwards from last loaded buffer (precedes tbuf_current)
  1874. * for first unsent buffer (desc_count > 0)
  1875. */
  1876. do {
  1877. if (i)
  1878. i--;
  1879. else
  1880. i = info->tbuf_count - 1;
  1881. if (!desc_count(info->tbufs[i]))
  1882. break;
  1883. info->tbuf_start = i;
  1884. rc = true;
  1885. } while (i != info->tbuf_current);
  1886. return rc;
  1887. }
  1888. static void isr_txeom(struct slgt_info *info, unsigned short status)
  1889. {
  1890. DBGISR(("%s txeom status=%04x\n", info->device_name, status));
  1891. slgt_irq_off(info, IRQ_TXDATA + IRQ_TXIDLE + IRQ_TXUNDER);
  1892. tdma_reset(info);
  1893. if (status & IRQ_TXUNDER) {
  1894. unsigned short val = rd_reg16(info, TCR);
  1895. wr_reg16(info, TCR, (unsigned short)(val | BIT2)); /* set reset bit */
  1896. wr_reg16(info, TCR, val); /* clear reset bit */
  1897. }
  1898. if (info->tx_active) {
  1899. if (info->params.mode != MGSL_MODE_ASYNC) {
  1900. if (status & IRQ_TXUNDER)
  1901. info->icount.txunder++;
  1902. else if (status & IRQ_TXIDLE)
  1903. info->icount.txok++;
  1904. }
  1905. if (unsent_tbufs(info)) {
  1906. tx_start(info);
  1907. update_tx_timer(info);
  1908. return;
  1909. }
  1910. info->tx_active = false;
  1911. del_timer(&info->tx_timer);
  1912. if (info->params.mode != MGSL_MODE_ASYNC && info->drop_rts_on_tx_done) {
  1913. info->signals &= ~SerialSignal_RTS;
  1914. info->drop_rts_on_tx_done = false;
  1915. set_gtsignals(info);
  1916. }
  1917. #if SYNCLINK_GENERIC_HDLC
  1918. if (info->netcount)
  1919. hdlcdev_tx_done(info);
  1920. else
  1921. #endif
  1922. {
  1923. if (info->port.tty && (info->port.tty->stopped || info->port.tty->hw_stopped)) {
  1924. tx_stop(info);
  1925. return;
  1926. }
  1927. info->pending_bh |= BH_TRANSMIT;
  1928. }
  1929. }
  1930. }
  1931. static void isr_gpio(struct slgt_info *info, unsigned int changed, unsigned int state)
  1932. {
  1933. struct cond_wait *w, *prev;
  1934. /* wake processes waiting for specific transitions */
  1935. for (w = info->gpio_wait_q, prev = NULL ; w != NULL ; w = w->next) {
  1936. if (w->data & changed) {
  1937. w->data = state;
  1938. wake_up_interruptible(&w->q);
  1939. if (prev != NULL)
  1940. prev->next = w->next;
  1941. else
  1942. info->gpio_wait_q = w->next;
  1943. } else
  1944. prev = w;
  1945. }
  1946. }
  1947. /* interrupt service routine
  1948. *
  1949. * irq interrupt number
  1950. * dev_id device ID supplied during interrupt registration
  1951. */
  1952. static irqreturn_t slgt_interrupt(int dummy, void *dev_id)
  1953. {
  1954. struct slgt_info *info = dev_id;
  1955. unsigned int gsr;
  1956. unsigned int i;
  1957. DBGISR(("slgt_interrupt irq=%d entry\n", info->irq_level));
  1958. while((gsr = rd_reg32(info, GSR) & 0xffffff00)) {
  1959. DBGISR(("%s gsr=%08x\n", info->device_name, gsr));
  1960. info->irq_occurred = true;
  1961. for(i=0; i < info->port_count ; i++) {
  1962. if (info->port_array[i] == NULL)
  1963. continue;
  1964. spin_lock(&info->port_array[i]->lock);
  1965. if (gsr & (BIT8 << i))
  1966. isr_serial(info->port_array[i]);
  1967. if (gsr & (BIT16 << (i*2)))
  1968. isr_rdma(info->port_array[i]);
  1969. if (gsr & (BIT17 << (i*2)))
  1970. isr_tdma(info->port_array[i]);
  1971. spin_unlock(&info->port_array[i]->lock);
  1972. }
  1973. }
  1974. if (info->gpio_present) {
  1975. unsigned int state;
  1976. unsigned int changed;
  1977. spin_lock(&info->lock);
  1978. while ((changed = rd_reg32(info, IOSR)) != 0) {
  1979. DBGISR(("%s iosr=%08x\n", info->device_name, changed));
  1980. /* read latched state of GPIO signals */
  1981. state = rd_reg32(info, IOVR);
  1982. /* clear pending GPIO interrupt bits */
  1983. wr_reg32(info, IOSR, changed);
  1984. for (i=0 ; i < info->port_count ; i++) {
  1985. if (info->port_array[i] != NULL)
  1986. isr_gpio(info->port_array[i], changed, state);
  1987. }
  1988. }
  1989. spin_unlock(&info->lock);
  1990. }
  1991. for(i=0; i < info->port_count ; i++) {
  1992. struct slgt_info *port = info->port_array[i];
  1993. if (port == NULL)
  1994. continue;
  1995. spin_lock(&port->lock);
  1996. if ((port->port.count || port->netcount) &&
  1997. port->pending_bh && !port->bh_running &&
  1998. !port->bh_requested) {
  1999. DBGISR(("%s bh queued\n", port->device_name));
  2000. schedule_work(&port->task);
  2001. port->bh_requested = true;
  2002. }
  2003. spin_unlock(&port->lock);
  2004. }
  2005. DBGISR(("slgt_interrupt irq=%d exit\n", info->irq_level));
  2006. return IRQ_HANDLED;
  2007. }
  2008. static int startup(struct slgt_info *info)
  2009. {
  2010. DBGINFO(("%s startup\n", info->device_name));
  2011. if (tty_port_initialized(&info->port))
  2012. return 0;
  2013. if (!info->tx_buf) {
  2014. info->tx_buf = kmalloc(info->max_frame_size, GFP_KERNEL);
  2015. if (!info->tx_buf) {
  2016. DBGERR(("%s can't allocate tx buffer\n", info->device_name));
  2017. return -ENOMEM;
  2018. }
  2019. }
  2020. info->pending_bh = 0;
  2021. memset(&info->icount, 0, sizeof(info->icount));
  2022. /* program hardware for current parameters */
  2023. change_params(info);
  2024. if (info->port.tty)
  2025. clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
  2026. tty_port_set_initialized(&info->port, 1);
  2027. return 0;
  2028. }
  2029. /*
  2030. * called by close() and hangup() to shutdown hardware
  2031. */
  2032. static void shutdown(struct slgt_info *info)
  2033. {
  2034. unsigned long flags;
  2035. if (!tty_port_initialized(&info->port))
  2036. return;
  2037. DBGINFO(("%s shutdown\n", info->device_name));
  2038. /* clear status wait queue because status changes */
  2039. /* can't happen after shutting down the hardware */
  2040. wake_up_interruptible(&info->status_event_wait_q);
  2041. wake_up_interruptible(&info->event_wait_q);
  2042. del_timer_sync(&info->tx_timer);
  2043. del_timer_sync(&info->rx_timer);
  2044. kfree(info->tx_buf);
  2045. info->tx_buf = NULL;
  2046. spin_lock_irqsave(&info->lock,flags);
  2047. tx_stop(info);
  2048. rx_stop(info);
  2049. slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
  2050. if (!info->port.tty || info->port.tty->termios.c_cflag & HUPCL) {
  2051. info->signals &= ~(SerialSignal_RTS | SerialSignal_DTR);
  2052. set_gtsignals(info);
  2053. }
  2054. flush_cond_wait(&info->gpio_wait_q);
  2055. spin_unlock_irqrestore(&info->lock,flags);
  2056. if (info->port.tty)
  2057. set_bit(TTY_IO_ERROR, &info->port.tty->flags);
  2058. tty_port_set_initialized(&info->port, 0);
  2059. }
  2060. static void program_hw(struct slgt_info *info)
  2061. {
  2062. unsigned long flags;
  2063. spin_lock_irqsave(&info->lock,flags);
  2064. rx_stop(info);
  2065. tx_stop(info);
  2066. if (info->params.mode != MGSL_MODE_ASYNC ||
  2067. info->netcount)
  2068. sync_mode(info);
  2069. else
  2070. async_mode(info);
  2071. set_gtsignals(info);
  2072. info->dcd_chkcount = 0;
  2073. info->cts_chkcount = 0;
  2074. info->ri_chkcount = 0;
  2075. info->dsr_chkcount = 0;
  2076. slgt_irq_on(info, IRQ_DCD | IRQ_CTS | IRQ_DSR | IRQ_RI);
  2077. get_gtsignals(info);
  2078. if (info->netcount ||
  2079. (info->port.tty && info->port.tty->termios.c_cflag & CREAD))
  2080. rx_start(info);
  2081. spin_unlock_irqrestore(&info->lock,flags);
  2082. }
  2083. /*
  2084. * reconfigure adapter based on new parameters
  2085. */
  2086. static void change_params(struct slgt_info *info)
  2087. {
  2088. unsigned cflag;
  2089. int bits_per_char;
  2090. if (!info->port.tty)
  2091. return;
  2092. DBGINFO(("%s change_params\n", info->device_name));
  2093. cflag = info->port.tty->termios.c_cflag;
  2094. /* if B0 rate (hangup) specified then negate RTS and DTR */
  2095. /* otherwise assert RTS and DTR */
  2096. if (cflag & CBAUD)
  2097. info->signals |= SerialSignal_RTS | SerialSignal_DTR;
  2098. else
  2099. info->signals &= ~(SerialSignal_RTS | SerialSignal_DTR);
  2100. /* byte size and parity */
  2101. switch (cflag & CSIZE) {
  2102. case CS5: info->params.data_bits = 5; break;
  2103. case CS6: info->params.data_bits = 6; break;
  2104. case CS7: info->params.data_bits = 7; break;
  2105. case CS8: info->params.data_bits = 8; break;
  2106. default: info->params.data_bits = 7; break;
  2107. }
  2108. info->params.stop_bits = (cflag & CSTOPB) ? 2 : 1;
  2109. if (cflag & PARENB)
  2110. info->params.parity = (cflag & PARODD) ? ASYNC_PARITY_ODD : ASYNC_PARITY_EVEN;
  2111. else
  2112. info->params.parity = ASYNC_PARITY_NONE;
  2113. /* calculate number of jiffies to transmit a full
  2114. * FIFO (32 bytes) at specified data rate
  2115. */
  2116. bits_per_char = info->params.data_bits +
  2117. info->params.stop_bits + 1;
  2118. info->params.data_rate = tty_get_baud_rate(info->port.tty);
  2119. if (info->params.data_rate) {
  2120. info->timeout = (32*HZ*bits_per_char) /
  2121. info->params.data_rate;
  2122. }
  2123. info->timeout += HZ/50; /* Add .02 seconds of slop */
  2124. tty_port_set_cts_flow(&info->port, cflag & CRTSCTS);
  2125. tty_port_set_check_carrier(&info->port, ~cflag & CLOCAL);
  2126. /* process tty input control flags */
  2127. info->read_status_mask = IRQ_RXOVER;
  2128. if (I_INPCK(info->port.tty))
  2129. info->read_status_mask |= MASK_PARITY | MASK_FRAMING;
  2130. if (I_BRKINT(info->port.tty) || I_PARMRK(info->port.tty))
  2131. info->read_status_mask |= MASK_BREAK;
  2132. if (I_IGNPAR(info->port.tty))
  2133. info->ignore_status_mask |= MASK_PARITY | MASK_FRAMING;
  2134. if (I_IGNBRK(info->port.tty)) {
  2135. info->ignore_status_mask |= MASK_BREAK;
  2136. /* If ignoring parity and break indicators, ignore
  2137. * overruns too. (For real raw support).
  2138. */
  2139. if (I_IGNPAR(info->port.tty))
  2140. info->ignore_status_mask |= MASK_OVERRUN;
  2141. }
  2142. program_hw(info);
  2143. }
  2144. static int get_stats(struct slgt_info *info, struct mgsl_icount __user *user_icount)
  2145. {
  2146. DBGINFO(("%s get_stats\n", info->device_name));
  2147. if (!user_icount) {
  2148. memset(&info->icount, 0, sizeof(info->icount));
  2149. } else {
  2150. if (copy_to_user(user_icount, &info->icount, sizeof(struct mgsl_icount)))
  2151. return -EFAULT;
  2152. }
  2153. return 0;
  2154. }
  2155. static int get_params(struct slgt_info *info, MGSL_PARAMS __user *user_params)
  2156. {
  2157. DBGINFO(("%s get_params\n", info->device_name));
  2158. if (copy_to_user(user_params, &info->params, sizeof(MGSL_PARAMS)))
  2159. return -EFAULT;
  2160. return 0;
  2161. }
  2162. static int set_params(struct slgt_info *info, MGSL_PARAMS __user *new_params)
  2163. {
  2164. unsigned long flags;
  2165. MGSL_PARAMS tmp_params;
  2166. DBGINFO(("%s set_params\n", info->device_name));
  2167. if (copy_from_user(&tmp_params, new_params, sizeof(MGSL_PARAMS)))
  2168. return -EFAULT;
  2169. spin_lock_irqsave(&info->lock, flags);
  2170. if (tmp_params.mode == MGSL_MODE_BASE_CLOCK)
  2171. info->base_clock = tmp_params.clock_speed;
  2172. else
  2173. memcpy(&info->params, &tmp_params, sizeof(MGSL_PARAMS));
  2174. spin_unlock_irqrestore(&info->lock, flags);
  2175. program_hw(info);
  2176. return 0;
  2177. }
  2178. static int get_txidle(struct slgt_info *info, int __user *idle_mode)
  2179. {
  2180. DBGINFO(("%s get_txidle=%d\n", info->device_name, info->idle_mode));
  2181. if (put_user(info->idle_mode, idle_mode))
  2182. return -EFAULT;
  2183. return 0;
  2184. }
  2185. static int set_txidle(struct slgt_info *info, int idle_mode)
  2186. {
  2187. unsigned long flags;
  2188. DBGINFO(("%s set_txidle(%d)\n", info->device_name, idle_mode));
  2189. spin_lock_irqsave(&info->lock,flags);
  2190. info->idle_mode = idle_mode;
  2191. if (info->params.mode != MGSL_MODE_ASYNC)
  2192. tx_set_idle(info);
  2193. spin_unlock_irqrestore(&info->lock,flags);
  2194. return 0;
  2195. }
  2196. static int tx_enable(struct slgt_info *info, int enable)
  2197. {
  2198. unsigned long flags;
  2199. DBGINFO(("%s tx_enable(%d)\n", info->device_name, enable));
  2200. spin_lock_irqsave(&info->lock,flags);
  2201. if (enable) {
  2202. if (!info->tx_enabled)
  2203. tx_start(info);
  2204. } else {
  2205. if (info->tx_enabled)
  2206. tx_stop(info);
  2207. }
  2208. spin_unlock_irqrestore(&info->lock,flags);
  2209. return 0;
  2210. }
  2211. /*
  2212. * abort transmit HDLC frame
  2213. */
  2214. static int tx_abort(struct slgt_info *info)
  2215. {
  2216. unsigned long flags;
  2217. DBGINFO(("%s tx_abort\n", info->device_name));
  2218. spin_lock_irqsave(&info->lock,flags);
  2219. tdma_reset(info);
  2220. spin_unlock_irqrestore(&info->lock,flags);
  2221. return 0;
  2222. }
  2223. static int rx_enable(struct slgt_info *info, int enable)
  2224. {
  2225. unsigned long flags;
  2226. unsigned int rbuf_fill_level;
  2227. DBGINFO(("%s rx_enable(%08x)\n", info->device_name, enable));
  2228. spin_lock_irqsave(&info->lock,flags);
  2229. /*
  2230. * enable[31..16] = receive DMA buffer fill level
  2231. * 0 = noop (leave fill level unchanged)
  2232. * fill level must be multiple of 4 and <= buffer size
  2233. */
  2234. rbuf_fill_level = ((unsigned int)enable) >> 16;
  2235. if (rbuf_fill_level) {
  2236. if ((rbuf_fill_level > DMABUFSIZE) || (rbuf_fill_level % 4)) {
  2237. spin_unlock_irqrestore(&info->lock, flags);
  2238. return -EINVAL;
  2239. }
  2240. info->rbuf_fill_level = rbuf_fill_level;
  2241. if (rbuf_fill_level < 128)
  2242. info->rx_pio = 1; /* PIO mode */
  2243. else
  2244. info->rx_pio = 0; /* DMA mode */
  2245. rx_stop(info); /* restart receiver to use new fill level */
  2246. }
  2247. /*
  2248. * enable[1..0] = receiver enable command
  2249. * 0 = disable
  2250. * 1 = enable
  2251. * 2 = enable or force hunt mode if already enabled
  2252. */
  2253. enable &= 3;
  2254. if (enable) {
  2255. if (!info->rx_enabled)
  2256. rx_start(info);
  2257. else if (enable == 2) {
  2258. /* force hunt mode (write 1 to RCR[3]) */
  2259. wr_reg16(info, RCR, rd_reg16(info, RCR) | BIT3);
  2260. }
  2261. } else {
  2262. if (info->rx_enabled)
  2263. rx_stop(info);
  2264. }
  2265. spin_unlock_irqrestore(&info->lock,flags);
  2266. return 0;
  2267. }
  2268. /*
  2269. * wait for specified event to occur
  2270. */
  2271. static int wait_mgsl_event(struct slgt_info *info, int __user *mask_ptr)
  2272. {
  2273. unsigned long flags;
  2274. int s;
  2275. int rc=0;
  2276. struct mgsl_icount cprev, cnow;
  2277. int events;
  2278. int mask;
  2279. struct _input_signal_events oldsigs, newsigs;
  2280. DECLARE_WAITQUEUE(wait, current);
  2281. if (get_user(mask, mask_ptr))
  2282. return -EFAULT;
  2283. DBGINFO(("%s wait_mgsl_event(%d)\n", info->device_name, mask));
  2284. spin_lock_irqsave(&info->lock,flags);
  2285. /* return immediately if state matches requested events */
  2286. get_gtsignals(info);
  2287. s = info->signals;
  2288. events = mask &
  2289. ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
  2290. ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
  2291. ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
  2292. ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
  2293. if (events) {
  2294. spin_unlock_irqrestore(&info->lock,flags);
  2295. goto exit;
  2296. }
  2297. /* save current irq counts */
  2298. cprev = info->icount;
  2299. oldsigs = info->input_signal_events;
  2300. /* enable hunt and idle irqs if needed */
  2301. if (mask & (MgslEvent_ExitHuntMode+MgslEvent_IdleReceived)) {
  2302. unsigned short val = rd_reg16(info, SCR);
  2303. if (!(val & IRQ_RXIDLE))
  2304. wr_reg16(info, SCR, (unsigned short)(val | IRQ_RXIDLE));
  2305. }
  2306. set_current_state(TASK_INTERRUPTIBLE);
  2307. add_wait_queue(&info->event_wait_q, &wait);
  2308. spin_unlock_irqrestore(&info->lock,flags);
  2309. for(;;) {
  2310. schedule();
  2311. if (signal_pending(current)) {
  2312. rc = -ERESTARTSYS;
  2313. break;
  2314. }
  2315. /* get current irq counts */
  2316. spin_lock_irqsave(&info->lock,flags);
  2317. cnow = info->icount;
  2318. newsigs = info->input_signal_events;
  2319. set_current_state(TASK_INTERRUPTIBLE);
  2320. spin_unlock_irqrestore(&info->lock,flags);
  2321. /* if no change, wait aborted for some reason */
  2322. if (newsigs.dsr_up == oldsigs.dsr_up &&
  2323. newsigs.dsr_down == oldsigs.dsr_down &&
  2324. newsigs.dcd_up == oldsigs.dcd_up &&
  2325. newsigs.dcd_down == oldsigs.dcd_down &&
  2326. newsigs.cts_up == oldsigs.cts_up &&
  2327. newsigs.cts_down == oldsigs.cts_down &&
  2328. newsigs.ri_up == oldsigs.ri_up &&
  2329. newsigs.ri_down == oldsigs.ri_down &&
  2330. cnow.exithunt == cprev.exithunt &&
  2331. cnow.rxidle == cprev.rxidle) {
  2332. rc = -EIO;
  2333. break;
  2334. }
  2335. events = mask &
  2336. ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
  2337. (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
  2338. (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
  2339. (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
  2340. (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
  2341. (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
  2342. (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
  2343. (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
  2344. (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
  2345. (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
  2346. if (events)
  2347. break;
  2348. cprev = cnow;
  2349. oldsigs = newsigs;
  2350. }
  2351. remove_wait_queue(&info->event_wait_q, &wait);
  2352. set_current_state(TASK_RUNNING);
  2353. if (mask & (MgslEvent_ExitHuntMode + MgslEvent_IdleReceived)) {
  2354. spin_lock_irqsave(&info->lock,flags);
  2355. if (!waitqueue_active(&info->event_wait_q)) {
  2356. /* disable enable exit hunt mode/idle rcvd IRQs */
  2357. wr_reg16(info, SCR,
  2358. (unsigned short)(rd_reg16(info, SCR) & ~IRQ_RXIDLE));
  2359. }
  2360. spin_unlock_irqrestore(&info->lock,flags);
  2361. }
  2362. exit:
  2363. if (rc == 0)
  2364. rc = put_user(events, mask_ptr);
  2365. return rc;
  2366. }
  2367. static int get_interface(struct slgt_info *info, int __user *if_mode)
  2368. {
  2369. DBGINFO(("%s get_interface=%x\n", info->device_name, info->if_mode));
  2370. if (put_user(info->if_mode, if_mode))
  2371. return -EFAULT;
  2372. return 0;
  2373. }
  2374. static int set_interface(struct slgt_info *info, int if_mode)
  2375. {
  2376. unsigned long flags;
  2377. unsigned short val;
  2378. DBGINFO(("%s set_interface=%x)\n", info->device_name, if_mode));
  2379. spin_lock_irqsave(&info->lock,flags);
  2380. info->if_mode = if_mode;
  2381. msc_set_vcr(info);
  2382. /* TCR (tx control) 07 1=RTS driver control */
  2383. val = rd_reg16(info, TCR);
  2384. if (info->if_mode & MGSL_INTERFACE_RTS_EN)
  2385. val |= BIT7;
  2386. else
  2387. val &= ~BIT7;
  2388. wr_reg16(info, TCR, val);
  2389. spin_unlock_irqrestore(&info->lock,flags);
  2390. return 0;
  2391. }
  2392. static int get_xsync(struct slgt_info *info, int __user *xsync)
  2393. {
  2394. DBGINFO(("%s get_xsync=%x\n", info->device_name, info->xsync));
  2395. if (put_user(info->xsync, xsync))
  2396. return -EFAULT;
  2397. return 0;
  2398. }
  2399. /*
  2400. * set extended sync pattern (1 to 4 bytes) for extended sync mode
  2401. *
  2402. * sync pattern is contained in least significant bytes of value
  2403. * most significant byte of sync pattern is oldest (1st sent/detected)
  2404. */
  2405. static int set_xsync(struct slgt_info *info, int xsync)
  2406. {
  2407. unsigned long flags;
  2408. DBGINFO(("%s set_xsync=%x)\n", info->device_name, xsync));
  2409. spin_lock_irqsave(&info->lock, flags);
  2410. info->xsync = xsync;
  2411. wr_reg32(info, XSR, xsync);
  2412. spin_unlock_irqrestore(&info->lock, flags);
  2413. return 0;
  2414. }
  2415. static int get_xctrl(struct slgt_info *info, int __user *xctrl)
  2416. {
  2417. DBGINFO(("%s get_xctrl=%x\n", info->device_name, info->xctrl));
  2418. if (put_user(info->xctrl, xctrl))
  2419. return -EFAULT;
  2420. return 0;
  2421. }
  2422. /*
  2423. * set extended control options
  2424. *
  2425. * xctrl[31:19] reserved, must be zero
  2426. * xctrl[18:17] extended sync pattern length in bytes
  2427. * 00 = 1 byte in xsr[7:0]
  2428. * 01 = 2 bytes in xsr[15:0]
  2429. * 10 = 3 bytes in xsr[23:0]
  2430. * 11 = 4 bytes in xsr[31:0]
  2431. * xctrl[16] 1 = enable terminal count, 0=disabled
  2432. * xctrl[15:0] receive terminal count for fixed length packets
  2433. * value is count minus one (0 = 1 byte packet)
  2434. * when terminal count is reached, receiver
  2435. * automatically returns to hunt mode and receive
  2436. * FIFO contents are flushed to DMA buffers with
  2437. * end of frame (EOF) status
  2438. */
  2439. static int set_xctrl(struct slgt_info *info, int xctrl)
  2440. {
  2441. unsigned long flags;
  2442. DBGINFO(("%s set_xctrl=%x)\n", info->device_name, xctrl));
  2443. spin_lock_irqsave(&info->lock, flags);
  2444. info->xctrl = xctrl;
  2445. wr_reg32(info, XCR, xctrl);
  2446. spin_unlock_irqrestore(&info->lock, flags);
  2447. return 0;
  2448. }
  2449. /*
  2450. * set general purpose IO pin state and direction
  2451. *
  2452. * user_gpio fields:
  2453. * state each bit indicates a pin state
  2454. * smask set bit indicates pin state to set
  2455. * dir each bit indicates a pin direction (0=input, 1=output)
  2456. * dmask set bit indicates pin direction to set
  2457. */
  2458. static int set_gpio(struct slgt_info *info, struct gpio_desc __user *user_gpio)
  2459. {
  2460. unsigned long flags;
  2461. struct gpio_desc gpio;
  2462. __u32 data;
  2463. if (!info->gpio_present)
  2464. return -EINVAL;
  2465. if (copy_from_user(&gpio, user_gpio, sizeof(gpio)))
  2466. return -EFAULT;
  2467. DBGINFO(("%s set_gpio state=%08x smask=%08x dir=%08x dmask=%08x\n",
  2468. info->device_name, gpio.state, gpio.smask,
  2469. gpio.dir, gpio.dmask));
  2470. spin_lock_irqsave(&info->port_array[0]->lock, flags);
  2471. if (gpio.dmask) {
  2472. data = rd_reg32(info, IODR);
  2473. data |= gpio.dmask & gpio.dir;
  2474. data &= ~(gpio.dmask & ~gpio.dir);
  2475. wr_reg32(info, IODR, data);
  2476. }
  2477. if (gpio.smask) {
  2478. data = rd_reg32(info, IOVR);
  2479. data |= gpio.smask & gpio.state;
  2480. data &= ~(gpio.smask & ~gpio.state);
  2481. wr_reg32(info, IOVR, data);
  2482. }
  2483. spin_unlock_irqrestore(&info->port_array[0]->lock, flags);
  2484. return 0;
  2485. }
  2486. /*
  2487. * get general purpose IO pin state and direction
  2488. */
  2489. static int get_gpio(struct slgt_info *info, struct gpio_desc __user *user_gpio)
  2490. {
  2491. struct gpio_desc gpio;
  2492. if (!info->gpio_present)
  2493. return -EINVAL;
  2494. gpio.state = rd_reg32(info, IOVR);
  2495. gpio.smask = 0xffffffff;
  2496. gpio.dir = rd_reg32(info, IODR);
  2497. gpio.dmask = 0xffffffff;
  2498. if (copy_to_user(user_gpio, &gpio, sizeof(gpio)))
  2499. return -EFAULT;
  2500. DBGINFO(("%s get_gpio state=%08x dir=%08x\n",
  2501. info->device_name, gpio.state, gpio.dir));
  2502. return 0;
  2503. }
  2504. /*
  2505. * conditional wait facility
  2506. */
  2507. static void init_cond_wait(struct cond_wait *w, unsigned int data)
  2508. {
  2509. init_waitqueue_head(&w->q);
  2510. init_waitqueue_entry(&w->wait, current);
  2511. w->data = data;
  2512. }
  2513. static void add_cond_wait(struct cond_wait **head, struct cond_wait *w)
  2514. {
  2515. set_current_state(TASK_INTERRUPTIBLE);
  2516. add_wait_queue(&w->q, &w->wait);
  2517. w->next = *head;
  2518. *head = w;
  2519. }
  2520. static void remove_cond_wait(struct cond_wait **head, struct cond_wait *cw)
  2521. {
  2522. struct cond_wait *w, *prev;
  2523. remove_wait_queue(&cw->q, &cw->wait);
  2524. set_current_state(TASK_RUNNING);
  2525. for (w = *head, prev = NULL ; w != NULL ; prev = w, w = w->next) {
  2526. if (w == cw) {
  2527. if (prev != NULL)
  2528. prev->next = w->next;
  2529. else
  2530. *head = w->next;
  2531. break;
  2532. }
  2533. }
  2534. }
  2535. static void flush_cond_wait(struct cond_wait **head)
  2536. {
  2537. while (*head != NULL) {
  2538. wake_up_interruptible(&(*head)->q);
  2539. *head = (*head)->next;
  2540. }
  2541. }
  2542. /*
  2543. * wait for general purpose I/O pin(s) to enter specified state
  2544. *
  2545. * user_gpio fields:
  2546. * state - bit indicates target pin state
  2547. * smask - set bit indicates watched pin
  2548. *
  2549. * The wait ends when at least one watched pin enters the specified
  2550. * state. When 0 (no error) is returned, user_gpio->state is set to the
  2551. * state of all GPIO pins when the wait ends.
  2552. *
  2553. * Note: Each pin may be a dedicated input, dedicated output, or
  2554. * configurable input/output. The number and configuration of pins
  2555. * varies with the specific adapter model. Only input pins (dedicated
  2556. * or configured) can be monitored with this function.
  2557. */
  2558. static int wait_gpio(struct slgt_info *info, struct gpio_desc __user *user_gpio)
  2559. {
  2560. unsigned long flags;
  2561. int rc = 0;
  2562. struct gpio_desc gpio;
  2563. struct cond_wait wait;
  2564. u32 state;
  2565. if (!info->gpio_present)
  2566. return -EINVAL;
  2567. if (copy_from_user(&gpio, user_gpio, sizeof(gpio)))
  2568. return -EFAULT;
  2569. DBGINFO(("%s wait_gpio() state=%08x smask=%08x\n",
  2570. info->device_name, gpio.state, gpio.smask));
  2571. /* ignore output pins identified by set IODR bit */
  2572. if ((gpio.smask &= ~rd_reg32(info, IODR)) == 0)
  2573. return -EINVAL;
  2574. init_cond_wait(&wait, gpio.smask);
  2575. spin_lock_irqsave(&info->port_array[0]->lock, flags);
  2576. /* enable interrupts for watched pins */
  2577. wr_reg32(info, IOER, rd_reg32(info, IOER) | gpio.smask);
  2578. /* get current pin states */
  2579. state = rd_reg32(info, IOVR);
  2580. if (gpio.smask & ~(state ^ gpio.state)) {
  2581. /* already in target state */
  2582. gpio.state = state;
  2583. } else {
  2584. /* wait for target state */
  2585. add_cond_wait(&info->gpio_wait_q, &wait);
  2586. spin_unlock_irqrestore(&info->port_array[0]->lock, flags);
  2587. schedule();
  2588. if (signal_pending(current))
  2589. rc = -ERESTARTSYS;
  2590. else
  2591. gpio.state = wait.data;
  2592. spin_lock_irqsave(&info->port_array[0]->lock, flags);
  2593. remove_cond_wait(&info->gpio_wait_q, &wait);
  2594. }
  2595. /* disable all GPIO interrupts if no waiting processes */
  2596. if (info->gpio_wait_q == NULL)
  2597. wr_reg32(info, IOER, 0);
  2598. spin_unlock_irqrestore(&info->port_array[0]->lock, flags);
  2599. if ((rc == 0) && copy_to_user(user_gpio, &gpio, sizeof(gpio)))
  2600. rc = -EFAULT;
  2601. return rc;
  2602. }
  2603. static int modem_input_wait(struct slgt_info *info,int arg)
  2604. {
  2605. unsigned long flags;
  2606. int rc;
  2607. struct mgsl_icount cprev, cnow;
  2608. DECLARE_WAITQUEUE(wait, current);
  2609. /* save current irq counts */
  2610. spin_lock_irqsave(&info->lock,flags);
  2611. cprev = info->icount;
  2612. add_wait_queue(&info->status_event_wait_q, &wait);
  2613. set_current_state(TASK_INTERRUPTIBLE);
  2614. spin_unlock_irqrestore(&info->lock,flags);
  2615. for(;;) {
  2616. schedule();
  2617. if (signal_pending(current)) {
  2618. rc = -ERESTARTSYS;
  2619. break;
  2620. }
  2621. /* get new irq counts */
  2622. spin_lock_irqsave(&info->lock,flags);
  2623. cnow = info->icount;
  2624. set_current_state(TASK_INTERRUPTIBLE);
  2625. spin_unlock_irqrestore(&info->lock,flags);
  2626. /* if no change, wait aborted for some reason */
  2627. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  2628. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
  2629. rc = -EIO;
  2630. break;
  2631. }
  2632. /* check for change in caller specified modem input */
  2633. if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
  2634. (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
  2635. (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
  2636. (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
  2637. rc = 0;
  2638. break;
  2639. }
  2640. cprev = cnow;
  2641. }
  2642. remove_wait_queue(&info->status_event_wait_q, &wait);
  2643. set_current_state(TASK_RUNNING);
  2644. return rc;
  2645. }
  2646. /*
  2647. * return state of serial control and status signals
  2648. */
  2649. static int tiocmget(struct tty_struct *tty)
  2650. {
  2651. struct slgt_info *info = tty->driver_data;
  2652. unsigned int result;
  2653. unsigned long flags;
  2654. spin_lock_irqsave(&info->lock,flags);
  2655. get_gtsignals(info);
  2656. spin_unlock_irqrestore(&info->lock,flags);
  2657. result = ((info->signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
  2658. ((info->signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
  2659. ((info->signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
  2660. ((info->signals & SerialSignal_RI) ? TIOCM_RNG:0) +
  2661. ((info->signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
  2662. ((info->signals & SerialSignal_CTS) ? TIOCM_CTS:0);
  2663. DBGINFO(("%s tiocmget value=%08X\n", info->device_name, result));
  2664. return result;
  2665. }
  2666. /*
  2667. * set modem control signals (DTR/RTS)
  2668. *
  2669. * cmd signal command: TIOCMBIS = set bit TIOCMBIC = clear bit
  2670. * TIOCMSET = set/clear signal values
  2671. * value bit mask for command
  2672. */
  2673. static int tiocmset(struct tty_struct *tty,
  2674. unsigned int set, unsigned int clear)
  2675. {
  2676. struct slgt_info *info = tty->driver_data;
  2677. unsigned long flags;
  2678. DBGINFO(("%s tiocmset(%x,%x)\n", info->device_name, set, clear));
  2679. if (set & TIOCM_RTS)
  2680. info->signals |= SerialSignal_RTS;
  2681. if (set & TIOCM_DTR)
  2682. info->signals |= SerialSignal_DTR;
  2683. if (clear & TIOCM_RTS)
  2684. info->signals &= ~SerialSignal_RTS;
  2685. if (clear & TIOCM_DTR)
  2686. info->signals &= ~SerialSignal_DTR;
  2687. spin_lock_irqsave(&info->lock,flags);
  2688. set_gtsignals(info);
  2689. spin_unlock_irqrestore(&info->lock,flags);
  2690. return 0;
  2691. }
  2692. static int carrier_raised(struct tty_port *port)
  2693. {
  2694. unsigned long flags;
  2695. struct slgt_info *info = container_of(port, struct slgt_info, port);
  2696. spin_lock_irqsave(&info->lock,flags);
  2697. get_gtsignals(info);
  2698. spin_unlock_irqrestore(&info->lock,flags);
  2699. return (info->signals & SerialSignal_DCD) ? 1 : 0;
  2700. }
  2701. static void dtr_rts(struct tty_port *port, int on)
  2702. {
  2703. unsigned long flags;
  2704. struct slgt_info *info = container_of(port, struct slgt_info, port);
  2705. spin_lock_irqsave(&info->lock,flags);
  2706. if (on)
  2707. info->signals |= SerialSignal_RTS | SerialSignal_DTR;
  2708. else
  2709. info->signals &= ~(SerialSignal_RTS | SerialSignal_DTR);
  2710. set_gtsignals(info);
  2711. spin_unlock_irqrestore(&info->lock,flags);
  2712. }
  2713. /*
  2714. * block current process until the device is ready to open
  2715. */
  2716. static int block_til_ready(struct tty_struct *tty, struct file *filp,
  2717. struct slgt_info *info)
  2718. {
  2719. DECLARE_WAITQUEUE(wait, current);
  2720. int retval;
  2721. bool do_clocal = false;
  2722. unsigned long flags;
  2723. int cd;
  2724. struct tty_port *port = &info->port;
  2725. DBGINFO(("%s block_til_ready\n", tty->driver->name));
  2726. if (filp->f_flags & O_NONBLOCK || tty_io_error(tty)) {
  2727. /* nonblock mode is set or port is not enabled */
  2728. tty_port_set_active(port, 1);
  2729. return 0;
  2730. }
  2731. if (C_CLOCAL(tty))
  2732. do_clocal = true;
  2733. /* Wait for carrier detect and the line to become
  2734. * free (i.e., not in use by the callout). While we are in
  2735. * this loop, port->count is dropped by one, so that
  2736. * close() knows when to free things. We restore it upon
  2737. * exit, either normal or abnormal.
  2738. */
  2739. retval = 0;
  2740. add_wait_queue(&port->open_wait, &wait);
  2741. spin_lock_irqsave(&info->lock, flags);
  2742. port->count--;
  2743. spin_unlock_irqrestore(&info->lock, flags);
  2744. port->blocked_open++;
  2745. while (1) {
  2746. if (C_BAUD(tty) && tty_port_initialized(port))
  2747. tty_port_raise_dtr_rts(port);
  2748. set_current_state(TASK_INTERRUPTIBLE);
  2749. if (tty_hung_up_p(filp) || !tty_port_initialized(port)) {
  2750. retval = (port->flags & ASYNC_HUP_NOTIFY) ?
  2751. -EAGAIN : -ERESTARTSYS;
  2752. break;
  2753. }
  2754. cd = tty_port_carrier_raised(port);
  2755. if (do_clocal || cd)
  2756. break;
  2757. if (signal_pending(current)) {
  2758. retval = -ERESTARTSYS;
  2759. break;
  2760. }
  2761. DBGINFO(("%s block_til_ready wait\n", tty->driver->name));
  2762. tty_unlock(tty);
  2763. schedule();
  2764. tty_lock(tty);
  2765. }
  2766. set_current_state(TASK_RUNNING);
  2767. remove_wait_queue(&port->open_wait, &wait);
  2768. if (!tty_hung_up_p(filp))
  2769. port->count++;
  2770. port->blocked_open--;
  2771. if (!retval)
  2772. tty_port_set_active(port, 1);
  2773. DBGINFO(("%s block_til_ready ready, rc=%d\n", tty->driver->name, retval));
  2774. return retval;
  2775. }
  2776. /*
  2777. * allocate buffers used for calling line discipline receive_buf
  2778. * directly in synchronous mode
  2779. * note: add 5 bytes to max frame size to allow appending
  2780. * 32-bit CRC and status byte when configured to do so
  2781. */
  2782. static int alloc_tmp_rbuf(struct slgt_info *info)
  2783. {
  2784. info->tmp_rbuf = kmalloc(info->max_frame_size + 5, GFP_KERNEL);
  2785. if (info->tmp_rbuf == NULL)
  2786. return -ENOMEM;
  2787. /* unused flag buffer to satisfy receive_buf calling interface */
  2788. info->flag_buf = kzalloc(info->max_frame_size + 5, GFP_KERNEL);
  2789. if (!info->flag_buf) {
  2790. kfree(info->tmp_rbuf);
  2791. info->tmp_rbuf = NULL;
  2792. return -ENOMEM;
  2793. }
  2794. return 0;
  2795. }
  2796. static void free_tmp_rbuf(struct slgt_info *info)
  2797. {
  2798. kfree(info->tmp_rbuf);
  2799. info->tmp_rbuf = NULL;
  2800. kfree(info->flag_buf);
  2801. info->flag_buf = NULL;
  2802. }
  2803. /*
  2804. * allocate DMA descriptor lists.
  2805. */
  2806. static int alloc_desc(struct slgt_info *info)
  2807. {
  2808. unsigned int i;
  2809. unsigned int pbufs;
  2810. /* allocate memory to hold descriptor lists */
  2811. info->bufs = pci_zalloc_consistent(info->pdev, DESC_LIST_SIZE,
  2812. &info->bufs_dma_addr);
  2813. if (info->bufs == NULL)
  2814. return -ENOMEM;
  2815. info->rbufs = (struct slgt_desc*)info->bufs;
  2816. info->tbufs = ((struct slgt_desc*)info->bufs) + info->rbuf_count;
  2817. pbufs = (unsigned int)info->bufs_dma_addr;
  2818. /*
  2819. * Build circular lists of descriptors
  2820. */
  2821. for (i=0; i < info->rbuf_count; i++) {
  2822. /* physical address of this descriptor */
  2823. info->rbufs[i].pdesc = pbufs + (i * sizeof(struct slgt_desc));
  2824. /* physical address of next descriptor */
  2825. if (i == info->rbuf_count - 1)
  2826. info->rbufs[i].next = cpu_to_le32(pbufs);
  2827. else
  2828. info->rbufs[i].next = cpu_to_le32(pbufs + ((i+1) * sizeof(struct slgt_desc)));
  2829. set_desc_count(info->rbufs[i], DMABUFSIZE);
  2830. }
  2831. for (i=0; i < info->tbuf_count; i++) {
  2832. /* physical address of this descriptor */
  2833. info->tbufs[i].pdesc = pbufs + ((info->rbuf_count + i) * sizeof(struct slgt_desc));
  2834. /* physical address of next descriptor */
  2835. if (i == info->tbuf_count - 1)
  2836. info->tbufs[i].next = cpu_to_le32(pbufs + info->rbuf_count * sizeof(struct slgt_desc));
  2837. else
  2838. info->tbufs[i].next = cpu_to_le32(pbufs + ((info->rbuf_count + i + 1) * sizeof(struct slgt_desc)));
  2839. }
  2840. return 0;
  2841. }
  2842. static void free_desc(struct slgt_info *info)
  2843. {
  2844. if (info->bufs != NULL) {
  2845. pci_free_consistent(info->pdev, DESC_LIST_SIZE, info->bufs, info->bufs_dma_addr);
  2846. info->bufs = NULL;
  2847. info->rbufs = NULL;
  2848. info->tbufs = NULL;
  2849. }
  2850. }
  2851. static int alloc_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count)
  2852. {
  2853. int i;
  2854. for (i=0; i < count; i++) {
  2855. if ((bufs[i].buf = pci_alloc_consistent(info->pdev, DMABUFSIZE, &bufs[i].buf_dma_addr)) == NULL)
  2856. return -ENOMEM;
  2857. bufs[i].pbuf = cpu_to_le32((unsigned int)bufs[i].buf_dma_addr);
  2858. }
  2859. return 0;
  2860. }
  2861. static void free_bufs(struct slgt_info *info, struct slgt_desc *bufs, int count)
  2862. {
  2863. int i;
  2864. for (i=0; i < count; i++) {
  2865. if (bufs[i].buf == NULL)
  2866. continue;
  2867. pci_free_consistent(info->pdev, DMABUFSIZE, bufs[i].buf, bufs[i].buf_dma_addr);
  2868. bufs[i].buf = NULL;
  2869. }
  2870. }
  2871. static int alloc_dma_bufs(struct slgt_info *info)
  2872. {
  2873. info->rbuf_count = 32;
  2874. info->tbuf_count = 32;
  2875. if (alloc_desc(info) < 0 ||
  2876. alloc_bufs(info, info->rbufs, info->rbuf_count) < 0 ||
  2877. alloc_bufs(info, info->tbufs, info->tbuf_count) < 0 ||
  2878. alloc_tmp_rbuf(info) < 0) {
  2879. DBGERR(("%s DMA buffer alloc fail\n", info->device_name));
  2880. return -ENOMEM;
  2881. }
  2882. reset_rbufs(info);
  2883. return 0;
  2884. }
  2885. static void free_dma_bufs(struct slgt_info *info)
  2886. {
  2887. if (info->bufs) {
  2888. free_bufs(info, info->rbufs, info->rbuf_count);
  2889. free_bufs(info, info->tbufs, info->tbuf_count);
  2890. free_desc(info);
  2891. }
  2892. free_tmp_rbuf(info);
  2893. }
  2894. static int claim_resources(struct slgt_info *info)
  2895. {
  2896. if (request_mem_region(info->phys_reg_addr, SLGT_REG_SIZE, "synclink_gt") == NULL) {
  2897. DBGERR(("%s reg addr conflict, addr=%08X\n",
  2898. info->device_name, info->phys_reg_addr));
  2899. info->init_error = DiagStatus_AddressConflict;
  2900. goto errout;
  2901. }
  2902. else
  2903. info->reg_addr_requested = true;
  2904. info->reg_addr = ioremap_nocache(info->phys_reg_addr, SLGT_REG_SIZE);
  2905. if (!info->reg_addr) {
  2906. DBGERR(("%s can't map device registers, addr=%08X\n",
  2907. info->device_name, info->phys_reg_addr));
  2908. info->init_error = DiagStatus_CantAssignPciResources;
  2909. goto errout;
  2910. }
  2911. return 0;
  2912. errout:
  2913. release_resources(info);
  2914. return -ENODEV;
  2915. }
  2916. static void release_resources(struct slgt_info *info)
  2917. {
  2918. if (info->irq_requested) {
  2919. free_irq(info->irq_level, info);
  2920. info->irq_requested = false;
  2921. }
  2922. if (info->reg_addr_requested) {
  2923. release_mem_region(info->phys_reg_addr, SLGT_REG_SIZE);
  2924. info->reg_addr_requested = false;
  2925. }
  2926. if (info->reg_addr) {
  2927. iounmap(info->reg_addr);
  2928. info->reg_addr = NULL;
  2929. }
  2930. }
  2931. /* Add the specified device instance data structure to the
  2932. * global linked list of devices and increment the device count.
  2933. */
  2934. static void add_device(struct slgt_info *info)
  2935. {
  2936. char *devstr;
  2937. info->next_device = NULL;
  2938. info->line = slgt_device_count;
  2939. sprintf(info->device_name, "%s%d", tty_dev_prefix, info->line);
  2940. if (info->line < MAX_DEVICES) {
  2941. if (maxframe[info->line])
  2942. info->max_frame_size = maxframe[info->line];
  2943. }
  2944. slgt_device_count++;
  2945. if (!slgt_device_list)
  2946. slgt_device_list = info;
  2947. else {
  2948. struct slgt_info *current_dev = slgt_device_list;
  2949. while(current_dev->next_device)
  2950. current_dev = current_dev->next_device;
  2951. current_dev->next_device = info;
  2952. }
  2953. if (info->max_frame_size < 4096)
  2954. info->max_frame_size = 4096;
  2955. else if (info->max_frame_size > 65535)
  2956. info->max_frame_size = 65535;
  2957. switch(info->pdev->device) {
  2958. case SYNCLINK_GT_DEVICE_ID:
  2959. devstr = "GT";
  2960. break;
  2961. case SYNCLINK_GT2_DEVICE_ID:
  2962. devstr = "GT2";
  2963. break;
  2964. case SYNCLINK_GT4_DEVICE_ID:
  2965. devstr = "GT4";
  2966. break;
  2967. case SYNCLINK_AC_DEVICE_ID:
  2968. devstr = "AC";
  2969. info->params.mode = MGSL_MODE_ASYNC;
  2970. break;
  2971. default:
  2972. devstr = "(unknown model)";
  2973. }
  2974. printk("SyncLink %s %s IO=%08x IRQ=%d MaxFrameSize=%u\n",
  2975. devstr, info->device_name, info->phys_reg_addr,
  2976. info->irq_level, info->max_frame_size);
  2977. #if SYNCLINK_GENERIC_HDLC
  2978. hdlcdev_init(info);
  2979. #endif
  2980. }
  2981. static const struct tty_port_operations slgt_port_ops = {
  2982. .carrier_raised = carrier_raised,
  2983. .dtr_rts = dtr_rts,
  2984. };
  2985. /*
  2986. * allocate device instance structure, return NULL on failure
  2987. */
  2988. static struct slgt_info *alloc_dev(int adapter_num, int port_num, struct pci_dev *pdev)
  2989. {
  2990. struct slgt_info *info;
  2991. info = kzalloc(sizeof(struct slgt_info), GFP_KERNEL);
  2992. if (!info) {
  2993. DBGERR(("%s device alloc failed adapter=%d port=%d\n",
  2994. driver_name, adapter_num, port_num));
  2995. } else {
  2996. tty_port_init(&info->port);
  2997. info->port.ops = &slgt_port_ops;
  2998. info->magic = MGSL_MAGIC;
  2999. INIT_WORK(&info->task, bh_handler);
  3000. info->max_frame_size = 4096;
  3001. info->base_clock = 14745600;
  3002. info->rbuf_fill_level = DMABUFSIZE;
  3003. info->port.close_delay = 5*HZ/10;
  3004. info->port.closing_wait = 30*HZ;
  3005. init_waitqueue_head(&info->status_event_wait_q);
  3006. init_waitqueue_head(&info->event_wait_q);
  3007. spin_lock_init(&info->netlock);
  3008. memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
  3009. info->idle_mode = HDLC_TXIDLE_FLAGS;
  3010. info->adapter_num = adapter_num;
  3011. info->port_num = port_num;
  3012. timer_setup(&info->tx_timer, tx_timeout, 0);
  3013. timer_setup(&info->rx_timer, rx_timeout, 0);
  3014. /* Copy configuration info to device instance data */
  3015. info->pdev = pdev;
  3016. info->irq_level = pdev->irq;
  3017. info->phys_reg_addr = pci_resource_start(pdev,0);
  3018. info->bus_type = MGSL_BUS_TYPE_PCI;
  3019. info->irq_flags = IRQF_SHARED;
  3020. info->init_error = -1; /* assume error, set to 0 on successful init */
  3021. }
  3022. return info;
  3023. }
  3024. static void device_init(int adapter_num, struct pci_dev *pdev)
  3025. {
  3026. struct slgt_info *port_array[SLGT_MAX_PORTS];
  3027. int i;
  3028. int port_count = 1;
  3029. if (pdev->device == SYNCLINK_GT2_DEVICE_ID)
  3030. port_count = 2;
  3031. else if (pdev->device == SYNCLINK_GT4_DEVICE_ID)
  3032. port_count = 4;
  3033. /* allocate device instances for all ports */
  3034. for (i=0; i < port_count; ++i) {
  3035. port_array[i] = alloc_dev(adapter_num, i, pdev);
  3036. if (port_array[i] == NULL) {
  3037. for (--i; i >= 0; --i) {
  3038. tty_port_destroy(&port_array[i]->port);
  3039. kfree(port_array[i]);
  3040. }
  3041. return;
  3042. }
  3043. }
  3044. /* give copy of port_array to all ports and add to device list */
  3045. for (i=0; i < port_count; ++i) {
  3046. memcpy(port_array[i]->port_array, port_array, sizeof(port_array));
  3047. add_device(port_array[i]);
  3048. port_array[i]->port_count = port_count;
  3049. spin_lock_init(&port_array[i]->lock);
  3050. }
  3051. /* Allocate and claim adapter resources */
  3052. if (!claim_resources(port_array[0])) {
  3053. alloc_dma_bufs(port_array[0]);
  3054. /* copy resource information from first port to others */
  3055. for (i = 1; i < port_count; ++i) {
  3056. port_array[i]->irq_level = port_array[0]->irq_level;
  3057. port_array[i]->reg_addr = port_array[0]->reg_addr;
  3058. alloc_dma_bufs(port_array[i]);
  3059. }
  3060. if (request_irq(port_array[0]->irq_level,
  3061. slgt_interrupt,
  3062. port_array[0]->irq_flags,
  3063. port_array[0]->device_name,
  3064. port_array[0]) < 0) {
  3065. DBGERR(("%s request_irq failed IRQ=%d\n",
  3066. port_array[0]->device_name,
  3067. port_array[0]->irq_level));
  3068. } else {
  3069. port_array[0]->irq_requested = true;
  3070. adapter_test(port_array[0]);
  3071. for (i=1 ; i < port_count ; i++) {
  3072. port_array[i]->init_error = port_array[0]->init_error;
  3073. port_array[i]->gpio_present = port_array[0]->gpio_present;
  3074. }
  3075. }
  3076. }
  3077. for (i = 0; i < port_count; ++i) {
  3078. struct slgt_info *info = port_array[i];
  3079. tty_port_register_device(&info->port, serial_driver, info->line,
  3080. &info->pdev->dev);
  3081. }
  3082. }
  3083. static int init_one(struct pci_dev *dev,
  3084. const struct pci_device_id *ent)
  3085. {
  3086. if (pci_enable_device(dev)) {
  3087. printk("error enabling pci device %p\n", dev);
  3088. return -EIO;
  3089. }
  3090. pci_set_master(dev);
  3091. device_init(slgt_device_count, dev);
  3092. return 0;
  3093. }
  3094. static void remove_one(struct pci_dev *dev)
  3095. {
  3096. }
  3097. static const struct tty_operations ops = {
  3098. .open = open,
  3099. .close = close,
  3100. .write = write,
  3101. .put_char = put_char,
  3102. .flush_chars = flush_chars,
  3103. .write_room = write_room,
  3104. .chars_in_buffer = chars_in_buffer,
  3105. .flush_buffer = flush_buffer,
  3106. .ioctl = ioctl,
  3107. .compat_ioctl = slgt_compat_ioctl,
  3108. .throttle = throttle,
  3109. .unthrottle = unthrottle,
  3110. .send_xchar = send_xchar,
  3111. .break_ctl = set_break,
  3112. .wait_until_sent = wait_until_sent,
  3113. .set_termios = set_termios,
  3114. .stop = tx_hold,
  3115. .start = tx_release,
  3116. .hangup = hangup,
  3117. .tiocmget = tiocmget,
  3118. .tiocmset = tiocmset,
  3119. .get_icount = get_icount,
  3120. .proc_show = synclink_gt_proc_show,
  3121. };
  3122. static void slgt_cleanup(void)
  3123. {
  3124. int rc;
  3125. struct slgt_info *info;
  3126. struct slgt_info *tmp;
  3127. printk(KERN_INFO "unload %s\n", driver_name);
  3128. if (serial_driver) {
  3129. for (info=slgt_device_list ; info != NULL ; info=info->next_device)
  3130. tty_unregister_device(serial_driver, info->line);
  3131. rc = tty_unregister_driver(serial_driver);
  3132. if (rc)
  3133. DBGERR(("tty_unregister_driver error=%d\n", rc));
  3134. put_tty_driver(serial_driver);
  3135. }
  3136. /* reset devices */
  3137. info = slgt_device_list;
  3138. while(info) {
  3139. reset_port(info);
  3140. info = info->next_device;
  3141. }
  3142. /* release devices */
  3143. info = slgt_device_list;
  3144. while(info) {
  3145. #if SYNCLINK_GENERIC_HDLC
  3146. hdlcdev_exit(info);
  3147. #endif
  3148. free_dma_bufs(info);
  3149. free_tmp_rbuf(info);
  3150. if (info->port_num == 0)
  3151. release_resources(info);
  3152. tmp = info;
  3153. info = info->next_device;
  3154. tty_port_destroy(&tmp->port);
  3155. kfree(tmp);
  3156. }
  3157. if (pci_registered)
  3158. pci_unregister_driver(&pci_driver);
  3159. }
  3160. /*
  3161. * Driver initialization entry point.
  3162. */
  3163. static int __init slgt_init(void)
  3164. {
  3165. int rc;
  3166. printk(KERN_INFO "%s\n", driver_name);
  3167. serial_driver = alloc_tty_driver(MAX_DEVICES);
  3168. if (!serial_driver) {
  3169. printk("%s can't allocate tty driver\n", driver_name);
  3170. return -ENOMEM;
  3171. }
  3172. /* Initialize the tty_driver structure */
  3173. serial_driver->driver_name = slgt_driver_name;
  3174. serial_driver->name = tty_dev_prefix;
  3175. serial_driver->major = ttymajor;
  3176. serial_driver->minor_start = 64;
  3177. serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  3178. serial_driver->subtype = SERIAL_TYPE_NORMAL;
  3179. serial_driver->init_termios = tty_std_termios;
  3180. serial_driver->init_termios.c_cflag =
  3181. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  3182. serial_driver->init_termios.c_ispeed = 9600;
  3183. serial_driver->init_termios.c_ospeed = 9600;
  3184. serial_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
  3185. tty_set_operations(serial_driver, &ops);
  3186. if ((rc = tty_register_driver(serial_driver)) < 0) {
  3187. DBGERR(("%s can't register serial driver\n", driver_name));
  3188. put_tty_driver(serial_driver);
  3189. serial_driver = NULL;
  3190. goto error;
  3191. }
  3192. printk(KERN_INFO "%s, tty major#%d\n",
  3193. driver_name, serial_driver->major);
  3194. slgt_device_count = 0;
  3195. if ((rc = pci_register_driver(&pci_driver)) < 0) {
  3196. printk("%s pci_register_driver error=%d\n", driver_name, rc);
  3197. goto error;
  3198. }
  3199. pci_registered = true;
  3200. if (!slgt_device_list)
  3201. printk("%s no devices found\n",driver_name);
  3202. return 0;
  3203. error:
  3204. slgt_cleanup();
  3205. return rc;
  3206. }
  3207. static void __exit slgt_exit(void)
  3208. {
  3209. slgt_cleanup();
  3210. }
  3211. module_init(slgt_init);
  3212. module_exit(slgt_exit);
  3213. /*
  3214. * register access routines
  3215. */
  3216. #define CALC_REGADDR() \
  3217. unsigned long reg_addr = ((unsigned long)info->reg_addr) + addr; \
  3218. if (addr >= 0x80) \
  3219. reg_addr += (info->port_num) * 32; \
  3220. else if (addr >= 0x40) \
  3221. reg_addr += (info->port_num) * 16;
  3222. static __u8 rd_reg8(struct slgt_info *info, unsigned int addr)
  3223. {
  3224. CALC_REGADDR();
  3225. return readb((void __iomem *)reg_addr);
  3226. }
  3227. static void wr_reg8(struct slgt_info *info, unsigned int addr, __u8 value)
  3228. {
  3229. CALC_REGADDR();
  3230. writeb(value, (void __iomem *)reg_addr);
  3231. }
  3232. static __u16 rd_reg16(struct slgt_info *info, unsigned int addr)
  3233. {
  3234. CALC_REGADDR();
  3235. return readw((void __iomem *)reg_addr);
  3236. }
  3237. static void wr_reg16(struct slgt_info *info, unsigned int addr, __u16 value)
  3238. {
  3239. CALC_REGADDR();
  3240. writew(value, (void __iomem *)reg_addr);
  3241. }
  3242. static __u32 rd_reg32(struct slgt_info *info, unsigned int addr)
  3243. {
  3244. CALC_REGADDR();
  3245. return readl((void __iomem *)reg_addr);
  3246. }
  3247. static void wr_reg32(struct slgt_info *info, unsigned int addr, __u32 value)
  3248. {
  3249. CALC_REGADDR();
  3250. writel(value, (void __iomem *)reg_addr);
  3251. }
  3252. static void rdma_reset(struct slgt_info *info)
  3253. {
  3254. unsigned int i;
  3255. /* set reset bit */
  3256. wr_reg32(info, RDCSR, BIT1);
  3257. /* wait for enable bit cleared */
  3258. for(i=0 ; i < 1000 ; i++)
  3259. if (!(rd_reg32(info, RDCSR) & BIT0))
  3260. break;
  3261. }
  3262. static void tdma_reset(struct slgt_info *info)
  3263. {
  3264. unsigned int i;
  3265. /* set reset bit */
  3266. wr_reg32(info, TDCSR, BIT1);
  3267. /* wait for enable bit cleared */
  3268. for(i=0 ; i < 1000 ; i++)
  3269. if (!(rd_reg32(info, TDCSR) & BIT0))
  3270. break;
  3271. }
  3272. /*
  3273. * enable internal loopback
  3274. * TxCLK and RxCLK are generated from BRG
  3275. * and TxD is looped back to RxD internally.
  3276. */
  3277. static void enable_loopback(struct slgt_info *info)
  3278. {
  3279. /* SCR (serial control) BIT2=loopback enable */
  3280. wr_reg16(info, SCR, (unsigned short)(rd_reg16(info, SCR) | BIT2));
  3281. if (info->params.mode != MGSL_MODE_ASYNC) {
  3282. /* CCR (clock control)
  3283. * 07..05 tx clock source (010 = BRG)
  3284. * 04..02 rx clock source (010 = BRG)
  3285. * 01 auxclk enable (0 = disable)
  3286. * 00 BRG enable (1 = enable)
  3287. *
  3288. * 0100 1001
  3289. */
  3290. wr_reg8(info, CCR, 0x49);
  3291. /* set speed if available, otherwise use default */
  3292. if (info->params.clock_speed)
  3293. set_rate(info, info->params.clock_speed);
  3294. else
  3295. set_rate(info, 3686400);
  3296. }
  3297. }
  3298. /*
  3299. * set baud rate generator to specified rate
  3300. */
  3301. static void set_rate(struct slgt_info *info, u32 rate)
  3302. {
  3303. unsigned int div;
  3304. unsigned int osc = info->base_clock;
  3305. /* div = osc/rate - 1
  3306. *
  3307. * Round div up if osc/rate is not integer to
  3308. * force to next slowest rate.
  3309. */
  3310. if (rate) {
  3311. div = osc/rate;
  3312. if (!(osc % rate) && div)
  3313. div--;
  3314. wr_reg16(info, BDR, (unsigned short)div);
  3315. }
  3316. }
  3317. static void rx_stop(struct slgt_info *info)
  3318. {
  3319. unsigned short val;
  3320. /* disable and reset receiver */
  3321. val = rd_reg16(info, RCR) & ~BIT1; /* clear enable bit */
  3322. wr_reg16(info, RCR, (unsigned short)(val | BIT2)); /* set reset bit */
  3323. wr_reg16(info, RCR, val); /* clear reset bit */
  3324. slgt_irq_off(info, IRQ_RXOVER + IRQ_RXDATA + IRQ_RXIDLE);
  3325. /* clear pending rx interrupts */
  3326. wr_reg16(info, SSR, IRQ_RXIDLE + IRQ_RXOVER);
  3327. rdma_reset(info);
  3328. info->rx_enabled = false;
  3329. info->rx_restart = false;
  3330. }
  3331. static void rx_start(struct slgt_info *info)
  3332. {
  3333. unsigned short val;
  3334. slgt_irq_off(info, IRQ_RXOVER + IRQ_RXDATA);
  3335. /* clear pending rx overrun IRQ */
  3336. wr_reg16(info, SSR, IRQ_RXOVER);
  3337. /* reset and disable receiver */
  3338. val = rd_reg16(info, RCR) & ~BIT1; /* clear enable bit */
  3339. wr_reg16(info, RCR, (unsigned short)(val | BIT2)); /* set reset bit */
  3340. wr_reg16(info, RCR, val); /* clear reset bit */
  3341. rdma_reset(info);
  3342. reset_rbufs(info);
  3343. if (info->rx_pio) {
  3344. /* rx request when rx FIFO not empty */
  3345. wr_reg16(info, SCR, (unsigned short)(rd_reg16(info, SCR) & ~BIT14));
  3346. slgt_irq_on(info, IRQ_RXDATA);
  3347. if (info->params.mode == MGSL_MODE_ASYNC) {
  3348. /* enable saving of rx status */
  3349. wr_reg32(info, RDCSR, BIT6);
  3350. }
  3351. } else {
  3352. /* rx request when rx FIFO half full */
  3353. wr_reg16(info, SCR, (unsigned short)(rd_reg16(info, SCR) | BIT14));
  3354. /* set 1st descriptor address */
  3355. wr_reg32(info, RDDAR, info->rbufs[0].pdesc);
  3356. if (info->params.mode != MGSL_MODE_ASYNC) {
  3357. /* enable rx DMA and DMA interrupt */
  3358. wr_reg32(info, RDCSR, (BIT2 + BIT0));
  3359. } else {
  3360. /* enable saving of rx status, rx DMA and DMA interrupt */
  3361. wr_reg32(info, RDCSR, (BIT6 + BIT2 + BIT0));
  3362. }
  3363. }
  3364. slgt_irq_on(info, IRQ_RXOVER);
  3365. /* enable receiver */
  3366. wr_reg16(info, RCR, (unsigned short)(rd_reg16(info, RCR) | BIT1));
  3367. info->rx_restart = false;
  3368. info->rx_enabled = true;
  3369. }
  3370. static void tx_start(struct slgt_info *info)
  3371. {
  3372. if (!info->tx_enabled) {
  3373. wr_reg16(info, TCR,
  3374. (unsigned short)((rd_reg16(info, TCR) | BIT1) & ~BIT2));
  3375. info->tx_enabled = true;
  3376. }
  3377. if (desc_count(info->tbufs[info->tbuf_start])) {
  3378. info->drop_rts_on_tx_done = false;
  3379. if (info->params.mode != MGSL_MODE_ASYNC) {
  3380. if (info->params.flags & HDLC_FLAG_AUTO_RTS) {
  3381. get_gtsignals(info);
  3382. if (!(info->signals & SerialSignal_RTS)) {
  3383. info->signals |= SerialSignal_RTS;
  3384. set_gtsignals(info);
  3385. info->drop_rts_on_tx_done = true;
  3386. }
  3387. }
  3388. slgt_irq_off(info, IRQ_TXDATA);
  3389. slgt_irq_on(info, IRQ_TXUNDER + IRQ_TXIDLE);
  3390. /* clear tx idle and underrun status bits */
  3391. wr_reg16(info, SSR, (unsigned short)(IRQ_TXIDLE + IRQ_TXUNDER));
  3392. } else {
  3393. slgt_irq_off(info, IRQ_TXDATA);
  3394. slgt_irq_on(info, IRQ_TXIDLE);
  3395. /* clear tx idle status bit */
  3396. wr_reg16(info, SSR, IRQ_TXIDLE);
  3397. }
  3398. /* set 1st descriptor address and start DMA */
  3399. wr_reg32(info, TDDAR, info->tbufs[info->tbuf_start].pdesc);
  3400. wr_reg32(info, TDCSR, BIT2 + BIT0);
  3401. info->tx_active = true;
  3402. }
  3403. }
  3404. static void tx_stop(struct slgt_info *info)
  3405. {
  3406. unsigned short val;
  3407. del_timer(&info->tx_timer);
  3408. tdma_reset(info);
  3409. /* reset and disable transmitter */
  3410. val = rd_reg16(info, TCR) & ~BIT1; /* clear enable bit */
  3411. wr_reg16(info, TCR, (unsigned short)(val | BIT2)); /* set reset bit */
  3412. slgt_irq_off(info, IRQ_TXDATA + IRQ_TXIDLE + IRQ_TXUNDER);
  3413. /* clear tx idle and underrun status bit */
  3414. wr_reg16(info, SSR, (unsigned short)(IRQ_TXIDLE + IRQ_TXUNDER));
  3415. reset_tbufs(info);
  3416. info->tx_enabled = false;
  3417. info->tx_active = false;
  3418. }
  3419. static void reset_port(struct slgt_info *info)
  3420. {
  3421. if (!info->reg_addr)
  3422. return;
  3423. tx_stop(info);
  3424. rx_stop(info);
  3425. info->signals &= ~(SerialSignal_RTS | SerialSignal_DTR);
  3426. set_gtsignals(info);
  3427. slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
  3428. }
  3429. static void reset_adapter(struct slgt_info *info)
  3430. {
  3431. int i;
  3432. for (i=0; i < info->port_count; ++i) {
  3433. if (info->port_array[i])
  3434. reset_port(info->port_array[i]);
  3435. }
  3436. }
  3437. static void async_mode(struct slgt_info *info)
  3438. {
  3439. unsigned short val;
  3440. slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
  3441. tx_stop(info);
  3442. rx_stop(info);
  3443. /* TCR (tx control)
  3444. *
  3445. * 15..13 mode, 010=async
  3446. * 12..10 encoding, 000=NRZ
  3447. * 09 parity enable
  3448. * 08 1=odd parity, 0=even parity
  3449. * 07 1=RTS driver control
  3450. * 06 1=break enable
  3451. * 05..04 character length
  3452. * 00=5 bits
  3453. * 01=6 bits
  3454. * 10=7 bits
  3455. * 11=8 bits
  3456. * 03 0=1 stop bit, 1=2 stop bits
  3457. * 02 reset
  3458. * 01 enable
  3459. * 00 auto-CTS enable
  3460. */
  3461. val = 0x4000;
  3462. if (info->if_mode & MGSL_INTERFACE_RTS_EN)
  3463. val |= BIT7;
  3464. if (info->params.parity != ASYNC_PARITY_NONE) {
  3465. val |= BIT9;
  3466. if (info->params.parity == ASYNC_PARITY_ODD)
  3467. val |= BIT8;
  3468. }
  3469. switch (info->params.data_bits)
  3470. {
  3471. case 6: val |= BIT4; break;
  3472. case 7: val |= BIT5; break;
  3473. case 8: val |= BIT5 + BIT4; break;
  3474. }
  3475. if (info->params.stop_bits != 1)
  3476. val |= BIT3;
  3477. if (info->params.flags & HDLC_FLAG_AUTO_CTS)
  3478. val |= BIT0;
  3479. wr_reg16(info, TCR, val);
  3480. /* RCR (rx control)
  3481. *
  3482. * 15..13 mode, 010=async
  3483. * 12..10 encoding, 000=NRZ
  3484. * 09 parity enable
  3485. * 08 1=odd parity, 0=even parity
  3486. * 07..06 reserved, must be 0
  3487. * 05..04 character length
  3488. * 00=5 bits
  3489. * 01=6 bits
  3490. * 10=7 bits
  3491. * 11=8 bits
  3492. * 03 reserved, must be zero
  3493. * 02 reset
  3494. * 01 enable
  3495. * 00 auto-DCD enable
  3496. */
  3497. val = 0x4000;
  3498. if (info->params.parity != ASYNC_PARITY_NONE) {
  3499. val |= BIT9;
  3500. if (info->params.parity == ASYNC_PARITY_ODD)
  3501. val |= BIT8;
  3502. }
  3503. switch (info->params.data_bits)
  3504. {
  3505. case 6: val |= BIT4; break;
  3506. case 7: val |= BIT5; break;
  3507. case 8: val |= BIT5 + BIT4; break;
  3508. }
  3509. if (info->params.flags & HDLC_FLAG_AUTO_DCD)
  3510. val |= BIT0;
  3511. wr_reg16(info, RCR, val);
  3512. /* CCR (clock control)
  3513. *
  3514. * 07..05 011 = tx clock source is BRG/16
  3515. * 04..02 010 = rx clock source is BRG
  3516. * 01 0 = auxclk disabled
  3517. * 00 1 = BRG enabled
  3518. *
  3519. * 0110 1001
  3520. */
  3521. wr_reg8(info, CCR, 0x69);
  3522. msc_set_vcr(info);
  3523. /* SCR (serial control)
  3524. *
  3525. * 15 1=tx req on FIFO half empty
  3526. * 14 1=rx req on FIFO half full
  3527. * 13 tx data IRQ enable
  3528. * 12 tx idle IRQ enable
  3529. * 11 rx break on IRQ enable
  3530. * 10 rx data IRQ enable
  3531. * 09 rx break off IRQ enable
  3532. * 08 overrun IRQ enable
  3533. * 07 DSR IRQ enable
  3534. * 06 CTS IRQ enable
  3535. * 05 DCD IRQ enable
  3536. * 04 RI IRQ enable
  3537. * 03 0=16x sampling, 1=8x sampling
  3538. * 02 1=txd->rxd internal loopback enable
  3539. * 01 reserved, must be zero
  3540. * 00 1=master IRQ enable
  3541. */
  3542. val = BIT15 + BIT14 + BIT0;
  3543. /* JCR[8] : 1 = x8 async mode feature available */
  3544. if ((rd_reg32(info, JCR) & BIT8) && info->params.data_rate &&
  3545. ((info->base_clock < (info->params.data_rate * 16)) ||
  3546. (info->base_clock % (info->params.data_rate * 16)))) {
  3547. /* use 8x sampling */
  3548. val |= BIT3;
  3549. set_rate(info, info->params.data_rate * 8);
  3550. } else {
  3551. /* use 16x sampling */
  3552. set_rate(info, info->params.data_rate * 16);
  3553. }
  3554. wr_reg16(info, SCR, val);
  3555. slgt_irq_on(info, IRQ_RXBREAK | IRQ_RXOVER);
  3556. if (info->params.loopback)
  3557. enable_loopback(info);
  3558. }
  3559. static void sync_mode(struct slgt_info *info)
  3560. {
  3561. unsigned short val;
  3562. slgt_irq_off(info, IRQ_ALL | IRQ_MASTER);
  3563. tx_stop(info);
  3564. rx_stop(info);
  3565. /* TCR (tx control)
  3566. *
  3567. * 15..13 mode
  3568. * 000=HDLC/SDLC
  3569. * 001=raw bit synchronous
  3570. * 010=asynchronous/isochronous
  3571. * 011=monosync byte synchronous
  3572. * 100=bisync byte synchronous
  3573. * 101=xsync byte synchronous
  3574. * 12..10 encoding
  3575. * 09 CRC enable
  3576. * 08 CRC32
  3577. * 07 1=RTS driver control
  3578. * 06 preamble enable
  3579. * 05..04 preamble length
  3580. * 03 share open/close flag
  3581. * 02 reset
  3582. * 01 enable
  3583. * 00 auto-CTS enable
  3584. */
  3585. val = BIT2;
  3586. switch(info->params.mode) {
  3587. case MGSL_MODE_XSYNC:
  3588. val |= BIT15 + BIT13;
  3589. break;
  3590. case MGSL_MODE_MONOSYNC: val |= BIT14 + BIT13; break;
  3591. case MGSL_MODE_BISYNC: val |= BIT15; break;
  3592. case MGSL_MODE_RAW: val |= BIT13; break;
  3593. }
  3594. if (info->if_mode & MGSL_INTERFACE_RTS_EN)
  3595. val |= BIT7;
  3596. switch(info->params.encoding)
  3597. {
  3598. case HDLC_ENCODING_NRZB: val |= BIT10; break;
  3599. case HDLC_ENCODING_NRZI_MARK: val |= BIT11; break;
  3600. case HDLC_ENCODING_NRZI: val |= BIT11 + BIT10; break;
  3601. case HDLC_ENCODING_BIPHASE_MARK: val |= BIT12; break;
  3602. case HDLC_ENCODING_BIPHASE_SPACE: val |= BIT12 + BIT10; break;
  3603. case HDLC_ENCODING_BIPHASE_LEVEL: val |= BIT12 + BIT11; break;
  3604. case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: val |= BIT12 + BIT11 + BIT10; break;
  3605. }
  3606. switch (info->params.crc_type & HDLC_CRC_MASK)
  3607. {
  3608. case HDLC_CRC_16_CCITT: val |= BIT9; break;
  3609. case HDLC_CRC_32_CCITT: val |= BIT9 + BIT8; break;
  3610. }
  3611. if (info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE)
  3612. val |= BIT6;
  3613. switch (info->params.preamble_length)
  3614. {
  3615. case HDLC_PREAMBLE_LENGTH_16BITS: val |= BIT5; break;
  3616. case HDLC_PREAMBLE_LENGTH_32BITS: val |= BIT4; break;
  3617. case HDLC_PREAMBLE_LENGTH_64BITS: val |= BIT5 + BIT4; break;
  3618. }
  3619. if (info->params.flags & HDLC_FLAG_AUTO_CTS)
  3620. val |= BIT0;
  3621. wr_reg16(info, TCR, val);
  3622. /* TPR (transmit preamble) */
  3623. switch (info->params.preamble)
  3624. {
  3625. case HDLC_PREAMBLE_PATTERN_FLAGS: val = 0x7e; break;
  3626. case HDLC_PREAMBLE_PATTERN_ONES: val = 0xff; break;
  3627. case HDLC_PREAMBLE_PATTERN_ZEROS: val = 0x00; break;
  3628. case HDLC_PREAMBLE_PATTERN_10: val = 0x55; break;
  3629. case HDLC_PREAMBLE_PATTERN_01: val = 0xaa; break;
  3630. default: val = 0x7e; break;
  3631. }
  3632. wr_reg8(info, TPR, (unsigned char)val);
  3633. /* RCR (rx control)
  3634. *
  3635. * 15..13 mode
  3636. * 000=HDLC/SDLC
  3637. * 001=raw bit synchronous
  3638. * 010=asynchronous/isochronous
  3639. * 011=monosync byte synchronous
  3640. * 100=bisync byte synchronous
  3641. * 101=xsync byte synchronous
  3642. * 12..10 encoding
  3643. * 09 CRC enable
  3644. * 08 CRC32
  3645. * 07..03 reserved, must be 0
  3646. * 02 reset
  3647. * 01 enable
  3648. * 00 auto-DCD enable
  3649. */
  3650. val = 0;
  3651. switch(info->params.mode) {
  3652. case MGSL_MODE_XSYNC:
  3653. val |= BIT15 + BIT13;
  3654. break;
  3655. case MGSL_MODE_MONOSYNC: val |= BIT14 + BIT13; break;
  3656. case MGSL_MODE_BISYNC: val |= BIT15; break;
  3657. case MGSL_MODE_RAW: val |= BIT13; break;
  3658. }
  3659. switch(info->params.encoding)
  3660. {
  3661. case HDLC_ENCODING_NRZB: val |= BIT10; break;
  3662. case HDLC_ENCODING_NRZI_MARK: val |= BIT11; break;
  3663. case HDLC_ENCODING_NRZI: val |= BIT11 + BIT10; break;
  3664. case HDLC_ENCODING_BIPHASE_MARK: val |= BIT12; break;
  3665. case HDLC_ENCODING_BIPHASE_SPACE: val |= BIT12 + BIT10; break;
  3666. case HDLC_ENCODING_BIPHASE_LEVEL: val |= BIT12 + BIT11; break;
  3667. case HDLC_ENCODING_DIFF_BIPHASE_LEVEL: val |= BIT12 + BIT11 + BIT10; break;
  3668. }
  3669. switch (info->params.crc_type & HDLC_CRC_MASK)
  3670. {
  3671. case HDLC_CRC_16_CCITT: val |= BIT9; break;
  3672. case HDLC_CRC_32_CCITT: val |= BIT9 + BIT8; break;
  3673. }
  3674. if (info->params.flags & HDLC_FLAG_AUTO_DCD)
  3675. val |= BIT0;
  3676. wr_reg16(info, RCR, val);
  3677. /* CCR (clock control)
  3678. *
  3679. * 07..05 tx clock source
  3680. * 04..02 rx clock source
  3681. * 01 auxclk enable
  3682. * 00 BRG enable
  3683. */
  3684. val = 0;
  3685. if (info->params.flags & HDLC_FLAG_TXC_BRG)
  3686. {
  3687. // when RxC source is DPLL, BRG generates 16X DPLL
  3688. // reference clock, so take TxC from BRG/16 to get
  3689. // transmit clock at actual data rate
  3690. if (info->params.flags & HDLC_FLAG_RXC_DPLL)
  3691. val |= BIT6 + BIT5; /* 011, txclk = BRG/16 */
  3692. else
  3693. val |= BIT6; /* 010, txclk = BRG */
  3694. }
  3695. else if (info->params.flags & HDLC_FLAG_TXC_DPLL)
  3696. val |= BIT7; /* 100, txclk = DPLL Input */
  3697. else if (info->params.flags & HDLC_FLAG_TXC_RXCPIN)
  3698. val |= BIT5; /* 001, txclk = RXC Input */
  3699. if (info->params.flags & HDLC_FLAG_RXC_BRG)
  3700. val |= BIT3; /* 010, rxclk = BRG */
  3701. else if (info->params.flags & HDLC_FLAG_RXC_DPLL)
  3702. val |= BIT4; /* 100, rxclk = DPLL */
  3703. else if (info->params.flags & HDLC_FLAG_RXC_TXCPIN)
  3704. val |= BIT2; /* 001, rxclk = TXC Input */
  3705. if (info->params.clock_speed)
  3706. val |= BIT1 + BIT0;
  3707. wr_reg8(info, CCR, (unsigned char)val);
  3708. if (info->params.flags & (HDLC_FLAG_TXC_DPLL + HDLC_FLAG_RXC_DPLL))
  3709. {
  3710. // program DPLL mode
  3711. switch(info->params.encoding)
  3712. {
  3713. case HDLC_ENCODING_BIPHASE_MARK:
  3714. case HDLC_ENCODING_BIPHASE_SPACE:
  3715. val = BIT7; break;
  3716. case HDLC_ENCODING_BIPHASE_LEVEL:
  3717. case HDLC_ENCODING_DIFF_BIPHASE_LEVEL:
  3718. val = BIT7 + BIT6; break;
  3719. default: val = BIT6; // NRZ encodings
  3720. }
  3721. wr_reg16(info, RCR, (unsigned short)(rd_reg16(info, RCR) | val));
  3722. // DPLL requires a 16X reference clock from BRG
  3723. set_rate(info, info->params.clock_speed * 16);
  3724. }
  3725. else
  3726. set_rate(info, info->params.clock_speed);
  3727. tx_set_idle(info);
  3728. msc_set_vcr(info);
  3729. /* SCR (serial control)
  3730. *
  3731. * 15 1=tx req on FIFO half empty
  3732. * 14 1=rx req on FIFO half full
  3733. * 13 tx data IRQ enable
  3734. * 12 tx idle IRQ enable
  3735. * 11 underrun IRQ enable
  3736. * 10 rx data IRQ enable
  3737. * 09 rx idle IRQ enable
  3738. * 08 overrun IRQ enable
  3739. * 07 DSR IRQ enable
  3740. * 06 CTS IRQ enable
  3741. * 05 DCD IRQ enable
  3742. * 04 RI IRQ enable
  3743. * 03 reserved, must be zero
  3744. * 02 1=txd->rxd internal loopback enable
  3745. * 01 reserved, must be zero
  3746. * 00 1=master IRQ enable
  3747. */
  3748. wr_reg16(info, SCR, BIT15 + BIT14 + BIT0);
  3749. if (info->params.loopback)
  3750. enable_loopback(info);
  3751. }
  3752. /*
  3753. * set transmit idle mode
  3754. */
  3755. static void tx_set_idle(struct slgt_info *info)
  3756. {
  3757. unsigned char val;
  3758. unsigned short tcr;
  3759. /* if preamble enabled (tcr[6] == 1) then tx idle size = 8 bits
  3760. * else tcr[5:4] = tx idle size: 00 = 8 bits, 01 = 16 bits
  3761. */
  3762. tcr = rd_reg16(info, TCR);
  3763. if (info->idle_mode & HDLC_TXIDLE_CUSTOM_16) {
  3764. /* disable preamble, set idle size to 16 bits */
  3765. tcr = (tcr & ~(BIT6 + BIT5)) | BIT4;
  3766. /* MSB of 16 bit idle specified in tx preamble register (TPR) */
  3767. wr_reg8(info, TPR, (unsigned char)((info->idle_mode >> 8) & 0xff));
  3768. } else if (!(tcr & BIT6)) {
  3769. /* preamble is disabled, set idle size to 8 bits */
  3770. tcr &= ~(BIT5 + BIT4);
  3771. }
  3772. wr_reg16(info, TCR, tcr);
  3773. if (info->idle_mode & (HDLC_TXIDLE_CUSTOM_8 | HDLC_TXIDLE_CUSTOM_16)) {
  3774. /* LSB of custom tx idle specified in tx idle register */
  3775. val = (unsigned char)(info->idle_mode & 0xff);
  3776. } else {
  3777. /* standard 8 bit idle patterns */
  3778. switch(info->idle_mode)
  3779. {
  3780. case HDLC_TXIDLE_FLAGS: val = 0x7e; break;
  3781. case HDLC_TXIDLE_ALT_ZEROS_ONES:
  3782. case HDLC_TXIDLE_ALT_MARK_SPACE: val = 0xaa; break;
  3783. case HDLC_TXIDLE_ZEROS:
  3784. case HDLC_TXIDLE_SPACE: val = 0x00; break;
  3785. default: val = 0xff;
  3786. }
  3787. }
  3788. wr_reg8(info, TIR, val);
  3789. }
  3790. /*
  3791. * get state of V24 status (input) signals
  3792. */
  3793. static void get_gtsignals(struct slgt_info *info)
  3794. {
  3795. unsigned short status = rd_reg16(info, SSR);
  3796. /* clear all serial signals except RTS and DTR */
  3797. info->signals &= SerialSignal_RTS | SerialSignal_DTR;
  3798. if (status & BIT3)
  3799. info->signals |= SerialSignal_DSR;
  3800. if (status & BIT2)
  3801. info->signals |= SerialSignal_CTS;
  3802. if (status & BIT1)
  3803. info->signals |= SerialSignal_DCD;
  3804. if (status & BIT0)
  3805. info->signals |= SerialSignal_RI;
  3806. }
  3807. /*
  3808. * set V.24 Control Register based on current configuration
  3809. */
  3810. static void msc_set_vcr(struct slgt_info *info)
  3811. {
  3812. unsigned char val = 0;
  3813. /* VCR (V.24 control)
  3814. *
  3815. * 07..04 serial IF select
  3816. * 03 DTR
  3817. * 02 RTS
  3818. * 01 LL
  3819. * 00 RL
  3820. */
  3821. switch(info->if_mode & MGSL_INTERFACE_MASK)
  3822. {
  3823. case MGSL_INTERFACE_RS232:
  3824. val |= BIT5; /* 0010 */
  3825. break;
  3826. case MGSL_INTERFACE_V35:
  3827. val |= BIT7 + BIT6 + BIT5; /* 1110 */
  3828. break;
  3829. case MGSL_INTERFACE_RS422:
  3830. val |= BIT6; /* 0100 */
  3831. break;
  3832. }
  3833. if (info->if_mode & MGSL_INTERFACE_MSB_FIRST)
  3834. val |= BIT4;
  3835. if (info->signals & SerialSignal_DTR)
  3836. val |= BIT3;
  3837. if (info->signals & SerialSignal_RTS)
  3838. val |= BIT2;
  3839. if (info->if_mode & MGSL_INTERFACE_LL)
  3840. val |= BIT1;
  3841. if (info->if_mode & MGSL_INTERFACE_RL)
  3842. val |= BIT0;
  3843. wr_reg8(info, VCR, val);
  3844. }
  3845. /*
  3846. * set state of V24 control (output) signals
  3847. */
  3848. static void set_gtsignals(struct slgt_info *info)
  3849. {
  3850. unsigned char val = rd_reg8(info, VCR);
  3851. if (info->signals & SerialSignal_DTR)
  3852. val |= BIT3;
  3853. else
  3854. val &= ~BIT3;
  3855. if (info->signals & SerialSignal_RTS)
  3856. val |= BIT2;
  3857. else
  3858. val &= ~BIT2;
  3859. wr_reg8(info, VCR, val);
  3860. }
  3861. /*
  3862. * free range of receive DMA buffers (i to last)
  3863. */
  3864. static void free_rbufs(struct slgt_info *info, unsigned int i, unsigned int last)
  3865. {
  3866. int done = 0;
  3867. while(!done) {
  3868. /* reset current buffer for reuse */
  3869. info->rbufs[i].status = 0;
  3870. set_desc_count(info->rbufs[i], info->rbuf_fill_level);
  3871. if (i == last)
  3872. done = 1;
  3873. if (++i == info->rbuf_count)
  3874. i = 0;
  3875. }
  3876. info->rbuf_current = i;
  3877. }
  3878. /*
  3879. * mark all receive DMA buffers as free
  3880. */
  3881. static void reset_rbufs(struct slgt_info *info)
  3882. {
  3883. free_rbufs(info, 0, info->rbuf_count - 1);
  3884. info->rbuf_fill_index = 0;
  3885. info->rbuf_fill_count = 0;
  3886. }
  3887. /*
  3888. * pass receive HDLC frame to upper layer
  3889. *
  3890. * return true if frame available, otherwise false
  3891. */
  3892. static bool rx_get_frame(struct slgt_info *info)
  3893. {
  3894. unsigned int start, end;
  3895. unsigned short status;
  3896. unsigned int framesize = 0;
  3897. unsigned long flags;
  3898. struct tty_struct *tty = info->port.tty;
  3899. unsigned char addr_field = 0xff;
  3900. unsigned int crc_size = 0;
  3901. switch (info->params.crc_type & HDLC_CRC_MASK) {
  3902. case HDLC_CRC_16_CCITT: crc_size = 2; break;
  3903. case HDLC_CRC_32_CCITT: crc_size = 4; break;
  3904. }
  3905. check_again:
  3906. framesize = 0;
  3907. addr_field = 0xff;
  3908. start = end = info->rbuf_current;
  3909. for (;;) {
  3910. if (!desc_complete(info->rbufs[end]))
  3911. goto cleanup;
  3912. if (framesize == 0 && info->params.addr_filter != 0xff)
  3913. addr_field = info->rbufs[end].buf[0];
  3914. framesize += desc_count(info->rbufs[end]);
  3915. if (desc_eof(info->rbufs[end]))
  3916. break;
  3917. if (++end == info->rbuf_count)
  3918. end = 0;
  3919. if (end == info->rbuf_current) {
  3920. if (info->rx_enabled){
  3921. spin_lock_irqsave(&info->lock,flags);
  3922. rx_start(info);
  3923. spin_unlock_irqrestore(&info->lock,flags);
  3924. }
  3925. goto cleanup;
  3926. }
  3927. }
  3928. /* status
  3929. *
  3930. * 15 buffer complete
  3931. * 14..06 reserved
  3932. * 05..04 residue
  3933. * 02 eof (end of frame)
  3934. * 01 CRC error
  3935. * 00 abort
  3936. */
  3937. status = desc_status(info->rbufs[end]);
  3938. /* ignore CRC bit if not using CRC (bit is undefined) */
  3939. if ((info->params.crc_type & HDLC_CRC_MASK) == HDLC_CRC_NONE)
  3940. status &= ~BIT1;
  3941. if (framesize == 0 ||
  3942. (addr_field != 0xff && addr_field != info->params.addr_filter)) {
  3943. free_rbufs(info, start, end);
  3944. goto check_again;
  3945. }
  3946. if (framesize < (2 + crc_size) || status & BIT0) {
  3947. info->icount.rxshort++;
  3948. framesize = 0;
  3949. } else if (status & BIT1) {
  3950. info->icount.rxcrc++;
  3951. if (!(info->params.crc_type & HDLC_CRC_RETURN_EX))
  3952. framesize = 0;
  3953. }
  3954. #if SYNCLINK_GENERIC_HDLC
  3955. if (framesize == 0) {
  3956. info->netdev->stats.rx_errors++;
  3957. info->netdev->stats.rx_frame_errors++;
  3958. }
  3959. #endif
  3960. DBGBH(("%s rx frame status=%04X size=%d\n",
  3961. info->device_name, status, framesize));
  3962. DBGDATA(info, info->rbufs[start].buf, min_t(int, framesize, info->rbuf_fill_level), "rx");
  3963. if (framesize) {
  3964. if (!(info->params.crc_type & HDLC_CRC_RETURN_EX)) {
  3965. framesize -= crc_size;
  3966. crc_size = 0;
  3967. }
  3968. if (framesize > info->max_frame_size + crc_size)
  3969. info->icount.rxlong++;
  3970. else {
  3971. /* copy dma buffer(s) to contiguous temp buffer */
  3972. int copy_count = framesize;
  3973. int i = start;
  3974. unsigned char *p = info->tmp_rbuf;
  3975. info->tmp_rbuf_count = framesize;
  3976. info->icount.rxok++;
  3977. while(copy_count) {
  3978. int partial_count = min_t(int, copy_count, info->rbuf_fill_level);
  3979. memcpy(p, info->rbufs[i].buf, partial_count);
  3980. p += partial_count;
  3981. copy_count -= partial_count;
  3982. if (++i == info->rbuf_count)
  3983. i = 0;
  3984. }
  3985. if (info->params.crc_type & HDLC_CRC_RETURN_EX) {
  3986. *p = (status & BIT1) ? RX_CRC_ERROR : RX_OK;
  3987. framesize++;
  3988. }
  3989. #if SYNCLINK_GENERIC_HDLC
  3990. if (info->netcount)
  3991. hdlcdev_rx(info,info->tmp_rbuf, framesize);
  3992. else
  3993. #endif
  3994. ldisc_receive_buf(tty, info->tmp_rbuf, info->flag_buf, framesize);
  3995. }
  3996. }
  3997. free_rbufs(info, start, end);
  3998. return true;
  3999. cleanup:
  4000. return false;
  4001. }
  4002. /*
  4003. * pass receive buffer (RAW synchronous mode) to tty layer
  4004. * return true if buffer available, otherwise false
  4005. */
  4006. static bool rx_get_buf(struct slgt_info *info)
  4007. {
  4008. unsigned int i = info->rbuf_current;
  4009. unsigned int count;
  4010. if (!desc_complete(info->rbufs[i]))
  4011. return false;
  4012. count = desc_count(info->rbufs[i]);
  4013. switch(info->params.mode) {
  4014. case MGSL_MODE_MONOSYNC:
  4015. case MGSL_MODE_BISYNC:
  4016. case MGSL_MODE_XSYNC:
  4017. /* ignore residue in byte synchronous modes */
  4018. if (desc_residue(info->rbufs[i]))
  4019. count--;
  4020. break;
  4021. }
  4022. DBGDATA(info, info->rbufs[i].buf, count, "rx");
  4023. DBGINFO(("rx_get_buf size=%d\n", count));
  4024. if (count)
  4025. ldisc_receive_buf(info->port.tty, info->rbufs[i].buf,
  4026. info->flag_buf, count);
  4027. free_rbufs(info, i, i);
  4028. return true;
  4029. }
  4030. static void reset_tbufs(struct slgt_info *info)
  4031. {
  4032. unsigned int i;
  4033. info->tbuf_current = 0;
  4034. for (i=0 ; i < info->tbuf_count ; i++) {
  4035. info->tbufs[i].status = 0;
  4036. info->tbufs[i].count = 0;
  4037. }
  4038. }
  4039. /*
  4040. * return number of free transmit DMA buffers
  4041. */
  4042. static unsigned int free_tbuf_count(struct slgt_info *info)
  4043. {
  4044. unsigned int count = 0;
  4045. unsigned int i = info->tbuf_current;
  4046. do
  4047. {
  4048. if (desc_count(info->tbufs[i]))
  4049. break; /* buffer in use */
  4050. ++count;
  4051. if (++i == info->tbuf_count)
  4052. i=0;
  4053. } while (i != info->tbuf_current);
  4054. /* if tx DMA active, last zero count buffer is in use */
  4055. if (count && (rd_reg32(info, TDCSR) & BIT0))
  4056. --count;
  4057. return count;
  4058. }
  4059. /*
  4060. * return number of bytes in unsent transmit DMA buffers
  4061. * and the serial controller tx FIFO
  4062. */
  4063. static unsigned int tbuf_bytes(struct slgt_info *info)
  4064. {
  4065. unsigned int total_count = 0;
  4066. unsigned int i = info->tbuf_current;
  4067. unsigned int reg_value;
  4068. unsigned int count;
  4069. unsigned int active_buf_count = 0;
  4070. /*
  4071. * Add descriptor counts for all tx DMA buffers.
  4072. * If count is zero (cleared by DMA controller after read),
  4073. * the buffer is complete or is actively being read from.
  4074. *
  4075. * Record buf_count of last buffer with zero count starting
  4076. * from current ring position. buf_count is mirror
  4077. * copy of count and is not cleared by serial controller.
  4078. * If DMA controller is active, that buffer is actively
  4079. * being read so add to total.
  4080. */
  4081. do {
  4082. count = desc_count(info->tbufs[i]);
  4083. if (count)
  4084. total_count += count;
  4085. else if (!total_count)
  4086. active_buf_count = info->tbufs[i].buf_count;
  4087. if (++i == info->tbuf_count)
  4088. i = 0;
  4089. } while (i != info->tbuf_current);
  4090. /* read tx DMA status register */
  4091. reg_value = rd_reg32(info, TDCSR);
  4092. /* if tx DMA active, last zero count buffer is in use */
  4093. if (reg_value & BIT0)
  4094. total_count += active_buf_count;
  4095. /* add tx FIFO count = reg_value[15..8] */
  4096. total_count += (reg_value >> 8) & 0xff;
  4097. /* if transmitter active add one byte for shift register */
  4098. if (info->tx_active)
  4099. total_count++;
  4100. return total_count;
  4101. }
  4102. /*
  4103. * load data into transmit DMA buffer ring and start transmitter if needed
  4104. * return true if data accepted, otherwise false (buffers full)
  4105. */
  4106. static bool tx_load(struct slgt_info *info, const char *buf, unsigned int size)
  4107. {
  4108. unsigned short count;
  4109. unsigned int i;
  4110. struct slgt_desc *d;
  4111. /* check required buffer space */
  4112. if (DIV_ROUND_UP(size, DMABUFSIZE) > free_tbuf_count(info))
  4113. return false;
  4114. DBGDATA(info, buf, size, "tx");
  4115. /*
  4116. * copy data to one or more DMA buffers in circular ring
  4117. * tbuf_start = first buffer for this data
  4118. * tbuf_current = next free buffer
  4119. *
  4120. * Copy all data before making data visible to DMA controller by
  4121. * setting descriptor count of the first buffer.
  4122. * This prevents an active DMA controller from reading the first DMA
  4123. * buffers of a frame and stopping before the final buffers are filled.
  4124. */
  4125. info->tbuf_start = i = info->tbuf_current;
  4126. while (size) {
  4127. d = &info->tbufs[i];
  4128. count = (unsigned short)((size > DMABUFSIZE) ? DMABUFSIZE : size);
  4129. memcpy(d->buf, buf, count);
  4130. size -= count;
  4131. buf += count;
  4132. /*
  4133. * set EOF bit for last buffer of HDLC frame or
  4134. * for every buffer in raw mode
  4135. */
  4136. if ((!size && info->params.mode == MGSL_MODE_HDLC) ||
  4137. info->params.mode == MGSL_MODE_RAW)
  4138. set_desc_eof(*d, 1);
  4139. else
  4140. set_desc_eof(*d, 0);
  4141. /* set descriptor count for all but first buffer */
  4142. if (i != info->tbuf_start)
  4143. set_desc_count(*d, count);
  4144. d->buf_count = count;
  4145. if (++i == info->tbuf_count)
  4146. i = 0;
  4147. }
  4148. info->tbuf_current = i;
  4149. /* set first buffer count to make new data visible to DMA controller */
  4150. d = &info->tbufs[info->tbuf_start];
  4151. set_desc_count(*d, d->buf_count);
  4152. /* start transmitter if needed and update transmit timeout */
  4153. if (!info->tx_active)
  4154. tx_start(info);
  4155. update_tx_timer(info);
  4156. return true;
  4157. }
  4158. static int register_test(struct slgt_info *info)
  4159. {
  4160. static unsigned short patterns[] =
  4161. {0x0000, 0xffff, 0xaaaa, 0x5555, 0x6969, 0x9696};
  4162. static unsigned int count = ARRAY_SIZE(patterns);
  4163. unsigned int i;
  4164. int rc = 0;
  4165. for (i=0 ; i < count ; i++) {
  4166. wr_reg16(info, TIR, patterns[i]);
  4167. wr_reg16(info, BDR, patterns[(i+1)%count]);
  4168. if ((rd_reg16(info, TIR) != patterns[i]) ||
  4169. (rd_reg16(info, BDR) != patterns[(i+1)%count])) {
  4170. rc = -ENODEV;
  4171. break;
  4172. }
  4173. }
  4174. info->gpio_present = (rd_reg32(info, JCR) & BIT5) ? 1 : 0;
  4175. info->init_error = rc ? 0 : DiagStatus_AddressFailure;
  4176. return rc;
  4177. }
  4178. static int irq_test(struct slgt_info *info)
  4179. {
  4180. unsigned long timeout;
  4181. unsigned long flags;
  4182. struct tty_struct *oldtty = info->port.tty;
  4183. u32 speed = info->params.data_rate;
  4184. info->params.data_rate = 921600;
  4185. info->port.tty = NULL;
  4186. spin_lock_irqsave(&info->lock, flags);
  4187. async_mode(info);
  4188. slgt_irq_on(info, IRQ_TXIDLE);
  4189. /* enable transmitter */
  4190. wr_reg16(info, TCR,
  4191. (unsigned short)(rd_reg16(info, TCR) | BIT1));
  4192. /* write one byte and wait for tx idle */
  4193. wr_reg16(info, TDR, 0);
  4194. /* assume failure */
  4195. info->init_error = DiagStatus_IrqFailure;
  4196. info->irq_occurred = false;
  4197. spin_unlock_irqrestore(&info->lock, flags);
  4198. timeout=100;
  4199. while(timeout-- && !info->irq_occurred)
  4200. msleep_interruptible(10);
  4201. spin_lock_irqsave(&info->lock,flags);
  4202. reset_port(info);
  4203. spin_unlock_irqrestore(&info->lock,flags);
  4204. info->params.data_rate = speed;
  4205. info->port.tty = oldtty;
  4206. info->init_error = info->irq_occurred ? 0 : DiagStatus_IrqFailure;
  4207. return info->irq_occurred ? 0 : -ENODEV;
  4208. }
  4209. static int loopback_test_rx(struct slgt_info *info)
  4210. {
  4211. unsigned char *src, *dest;
  4212. int count;
  4213. if (desc_complete(info->rbufs[0])) {
  4214. count = desc_count(info->rbufs[0]);
  4215. src = info->rbufs[0].buf;
  4216. dest = info->tmp_rbuf;
  4217. for( ; count ; count-=2, src+=2) {
  4218. /* src=data byte (src+1)=status byte */
  4219. if (!(*(src+1) & (BIT9 + BIT8))) {
  4220. *dest = *src;
  4221. dest++;
  4222. info->tmp_rbuf_count++;
  4223. }
  4224. }
  4225. DBGDATA(info, info->tmp_rbuf, info->tmp_rbuf_count, "rx");
  4226. return 1;
  4227. }
  4228. return 0;
  4229. }
  4230. static int loopback_test(struct slgt_info *info)
  4231. {
  4232. #define TESTFRAMESIZE 20
  4233. unsigned long timeout;
  4234. u16 count = TESTFRAMESIZE;
  4235. unsigned char buf[TESTFRAMESIZE];
  4236. int rc = -ENODEV;
  4237. unsigned long flags;
  4238. struct tty_struct *oldtty = info->port.tty;
  4239. MGSL_PARAMS params;
  4240. memcpy(&params, &info->params, sizeof(params));
  4241. info->params.mode = MGSL_MODE_ASYNC;
  4242. info->params.data_rate = 921600;
  4243. info->params.loopback = 1;
  4244. info->port.tty = NULL;
  4245. /* build and send transmit frame */
  4246. for (count = 0; count < TESTFRAMESIZE; ++count)
  4247. buf[count] = (unsigned char)count;
  4248. info->tmp_rbuf_count = 0;
  4249. memset(info->tmp_rbuf, 0, TESTFRAMESIZE);
  4250. /* program hardware for HDLC and enabled receiver */
  4251. spin_lock_irqsave(&info->lock,flags);
  4252. async_mode(info);
  4253. rx_start(info);
  4254. tx_load(info, buf, count);
  4255. spin_unlock_irqrestore(&info->lock, flags);
  4256. /* wait for receive complete */
  4257. for (timeout = 100; timeout; --timeout) {
  4258. msleep_interruptible(10);
  4259. if (loopback_test_rx(info)) {
  4260. rc = 0;
  4261. break;
  4262. }
  4263. }
  4264. /* verify received frame length and contents */
  4265. if (!rc && (info->tmp_rbuf_count != count ||
  4266. memcmp(buf, info->tmp_rbuf, count))) {
  4267. rc = -ENODEV;
  4268. }
  4269. spin_lock_irqsave(&info->lock,flags);
  4270. reset_adapter(info);
  4271. spin_unlock_irqrestore(&info->lock,flags);
  4272. memcpy(&info->params, &params, sizeof(info->params));
  4273. info->port.tty = oldtty;
  4274. info->init_error = rc ? DiagStatus_DmaFailure : 0;
  4275. return rc;
  4276. }
  4277. static int adapter_test(struct slgt_info *info)
  4278. {
  4279. DBGINFO(("testing %s\n", info->device_name));
  4280. if (register_test(info) < 0) {
  4281. printk("register test failure %s addr=%08X\n",
  4282. info->device_name, info->phys_reg_addr);
  4283. } else if (irq_test(info) < 0) {
  4284. printk("IRQ test failure %s IRQ=%d\n",
  4285. info->device_name, info->irq_level);
  4286. } else if (loopback_test(info) < 0) {
  4287. printk("loopback test failure %s\n", info->device_name);
  4288. }
  4289. return info->init_error;
  4290. }
  4291. /*
  4292. * transmit timeout handler
  4293. */
  4294. static void tx_timeout(struct timer_list *t)
  4295. {
  4296. struct slgt_info *info = from_timer(info, t, tx_timer);
  4297. unsigned long flags;
  4298. DBGINFO(("%s tx_timeout\n", info->device_name));
  4299. if(info->tx_active && info->params.mode == MGSL_MODE_HDLC) {
  4300. info->icount.txtimeout++;
  4301. }
  4302. spin_lock_irqsave(&info->lock,flags);
  4303. tx_stop(info);
  4304. spin_unlock_irqrestore(&info->lock,flags);
  4305. #if SYNCLINK_GENERIC_HDLC
  4306. if (info->netcount)
  4307. hdlcdev_tx_done(info);
  4308. else
  4309. #endif
  4310. bh_transmit(info);
  4311. }
  4312. /*
  4313. * receive buffer polling timer
  4314. */
  4315. static void rx_timeout(struct timer_list *t)
  4316. {
  4317. struct slgt_info *info = from_timer(info, t, rx_timer);
  4318. unsigned long flags;
  4319. DBGINFO(("%s rx_timeout\n", info->device_name));
  4320. spin_lock_irqsave(&info->lock, flags);
  4321. info->pending_bh |= BH_RECEIVE;
  4322. spin_unlock_irqrestore(&info->lock, flags);
  4323. bh_handler(&info->task);
  4324. }