synclink_gt.c 127 KB

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