synclink_gt.c 127 KB

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