synclink_gt.c 121 KB

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