synclink_gt.c 113 KB

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