synclink_gt.c 126 KB

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