synclink_cs.c 119 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618
  1. /*
  2. * linux/drivers/char/pcmcia/synclink_cs.c
  3. *
  4. * $Id: synclink_cs.c,v 4.26 2004/08/11 19:30:02 paulkf Exp $
  5. *
  6. * Device driver for Microgate SyncLink PC Card
  7. * multiprotocol serial adapter.
  8. *
  9. * written by Paul Fulghum for Microgate Corporation
  10. * paulkf@microgate.com
  11. *
  12. * Microgate and SyncLink are trademarks of Microgate Corporation
  13. *
  14. * This code is released under the GNU General Public License (GPL)
  15. *
  16. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  20. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  24. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  26. * OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #define VERSION(ver,rel,seq) (((ver)<<16) | ((rel)<<8) | (seq))
  29. #if defined(__i386__)
  30. # define BREAKPOINT() asm(" int $3");
  31. #else
  32. # define BREAKPOINT() { }
  33. #endif
  34. #define MAX_DEVICE_COUNT 4
  35. #include <linux/config.h>
  36. #include <linux/module.h>
  37. #include <linux/errno.h>
  38. #include <linux/signal.h>
  39. #include <linux/sched.h>
  40. #include <linux/timer.h>
  41. #include <linux/time.h>
  42. #include <linux/interrupt.h>
  43. #include <linux/pci.h>
  44. #include <linux/tty.h>
  45. #include <linux/tty_flip.h>
  46. #include <linux/serial.h>
  47. #include <linux/major.h>
  48. #include <linux/string.h>
  49. #include <linux/fcntl.h>
  50. #include <linux/ptrace.h>
  51. #include <linux/ioport.h>
  52. #include <linux/mm.h>
  53. #include <linux/slab.h>
  54. #include <linux/netdevice.h>
  55. #include <linux/vmalloc.h>
  56. #include <linux/init.h>
  57. #include <asm/serial.h>
  58. #include <linux/delay.h>
  59. #include <linux/ioctl.h>
  60. #include <asm/system.h>
  61. #include <asm/io.h>
  62. #include <asm/irq.h>
  63. #include <asm/dma.h>
  64. #include <linux/bitops.h>
  65. #include <asm/types.h>
  66. #include <linux/termios.h>
  67. #include <linux/workqueue.h>
  68. #include <linux/hdlc.h>
  69. #include <pcmcia/version.h>
  70. #include <pcmcia/cs_types.h>
  71. #include <pcmcia/cs.h>
  72. #include <pcmcia/cistpl.h>
  73. #include <pcmcia/cisreg.h>
  74. #include <pcmcia/ds.h>
  75. #ifdef CONFIG_HDLC_MODULE
  76. #define CONFIG_HDLC 1
  77. #endif
  78. #define GET_USER(error,value,addr) error = get_user(value,addr)
  79. #define COPY_FROM_USER(error,dest,src,size) error = copy_from_user(dest,src,size) ? -EFAULT : 0
  80. #define PUT_USER(error,value,addr) error = put_user(value,addr)
  81. #define COPY_TO_USER(error,dest,src,size) error = copy_to_user(dest,src,size) ? -EFAULT : 0
  82. #include <asm/uaccess.h>
  83. #include "linux/synclink.h"
  84. static MGSL_PARAMS default_params = {
  85. MGSL_MODE_HDLC, /* unsigned long mode */
  86. 0, /* unsigned char loopback; */
  87. HDLC_FLAG_UNDERRUN_ABORT15, /* unsigned short flags; */
  88. HDLC_ENCODING_NRZI_SPACE, /* unsigned char encoding; */
  89. 0, /* unsigned long clock_speed; */
  90. 0xff, /* unsigned char addr_filter; */
  91. HDLC_CRC_16_CCITT, /* unsigned short crc_type; */
  92. HDLC_PREAMBLE_LENGTH_8BITS, /* unsigned char preamble_length; */
  93. HDLC_PREAMBLE_PATTERN_NONE, /* unsigned char preamble; */
  94. 9600, /* unsigned long data_rate; */
  95. 8, /* unsigned char data_bits; */
  96. 1, /* unsigned char stop_bits; */
  97. ASYNC_PARITY_NONE /* unsigned char parity; */
  98. };
  99. typedef struct
  100. {
  101. int count;
  102. unsigned char status;
  103. char data[1];
  104. } RXBUF;
  105. /* The queue of BH actions to be performed */
  106. #define BH_RECEIVE 1
  107. #define BH_TRANSMIT 2
  108. #define BH_STATUS 4
  109. #define IO_PIN_SHUTDOWN_LIMIT 100
  110. #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
  111. struct _input_signal_events {
  112. int ri_up;
  113. int ri_down;
  114. int dsr_up;
  115. int dsr_down;
  116. int dcd_up;
  117. int dcd_down;
  118. int cts_up;
  119. int cts_down;
  120. };
  121. /*
  122. * Device instance data structure
  123. */
  124. typedef struct _mgslpc_info {
  125. void *if_ptr; /* General purpose pointer (used by SPPP) */
  126. int magic;
  127. int flags;
  128. int count; /* count of opens */
  129. int line;
  130. unsigned short close_delay;
  131. unsigned short closing_wait; /* time to wait before closing */
  132. struct mgsl_icount icount;
  133. struct tty_struct *tty;
  134. int timeout;
  135. int x_char; /* xon/xoff character */
  136. int blocked_open; /* # of blocked opens */
  137. unsigned char read_status_mask;
  138. unsigned char ignore_status_mask;
  139. unsigned char *tx_buf;
  140. int tx_put;
  141. int tx_get;
  142. int tx_count;
  143. /* circular list of fixed length rx buffers */
  144. unsigned char *rx_buf; /* memory allocated for all rx buffers */
  145. int rx_buf_total_size; /* size of memory allocated for rx buffers */
  146. int rx_put; /* index of next empty rx buffer */
  147. int rx_get; /* index of next full rx buffer */
  148. int rx_buf_size; /* size in bytes of single rx buffer */
  149. int rx_buf_count; /* total number of rx buffers */
  150. int rx_frame_count; /* number of full rx buffers */
  151. wait_queue_head_t open_wait;
  152. wait_queue_head_t close_wait;
  153. wait_queue_head_t status_event_wait_q;
  154. wait_queue_head_t event_wait_q;
  155. struct timer_list tx_timer; /* HDLC transmit timeout timer */
  156. struct _mgslpc_info *next_device; /* device list link */
  157. unsigned short imra_value;
  158. unsigned short imrb_value;
  159. unsigned char pim_value;
  160. spinlock_t lock;
  161. struct work_struct task; /* task structure for scheduling bh */
  162. u32 max_frame_size;
  163. u32 pending_bh;
  164. int bh_running;
  165. int bh_requested;
  166. int dcd_chkcount; /* check counts to prevent */
  167. int cts_chkcount; /* too many IRQs if a signal */
  168. int dsr_chkcount; /* is floating */
  169. int ri_chkcount;
  170. int rx_enabled;
  171. int rx_overflow;
  172. int tx_enabled;
  173. int tx_active;
  174. int tx_aborting;
  175. u32 idle_mode;
  176. int if_mode; /* serial interface selection (RS-232, v.35 etc) */
  177. char device_name[25]; /* device instance name */
  178. unsigned int io_base; /* base I/O address of adapter */
  179. unsigned int irq_level;
  180. MGSL_PARAMS params; /* communications parameters */
  181. unsigned char serial_signals; /* current serial signal states */
  182. char irq_occurred; /* for diagnostics use */
  183. char testing_irq;
  184. unsigned int init_error; /* startup error (DIAGS) */
  185. char flag_buf[MAX_ASYNC_BUFFER_SIZE];
  186. BOOLEAN drop_rts_on_tx_done;
  187. struct _input_signal_events input_signal_events;
  188. /* PCMCIA support */
  189. dev_link_t link;
  190. dev_node_t node;
  191. int stop;
  192. /* SPPP/Cisco HDLC device parts */
  193. int netcount;
  194. int dosyncppp;
  195. spinlock_t netlock;
  196. #ifdef CONFIG_HDLC
  197. struct net_device *netdev;
  198. #endif
  199. } MGSLPC_INFO;
  200. #define MGSLPC_MAGIC 0x5402
  201. /*
  202. * The size of the serial xmit buffer is 1 page, or 4096 bytes
  203. */
  204. #define TXBUFSIZE 4096
  205. #define CHA 0x00 /* channel A offset */
  206. #define CHB 0x40 /* channel B offset */
  207. /*
  208. * FIXME: PPC has PVR defined in asm/reg.h. For now we just undef it.
  209. */
  210. #undef PVR
  211. #define RXFIFO 0
  212. #define TXFIFO 0
  213. #define STAR 0x20
  214. #define CMDR 0x20
  215. #define RSTA 0x21
  216. #define PRE 0x21
  217. #define MODE 0x22
  218. #define TIMR 0x23
  219. #define XAD1 0x24
  220. #define XAD2 0x25
  221. #define RAH1 0x26
  222. #define RAH2 0x27
  223. #define DAFO 0x27
  224. #define RAL1 0x28
  225. #define RFC 0x28
  226. #define RHCR 0x29
  227. #define RAL2 0x29
  228. #define RBCL 0x2a
  229. #define XBCL 0x2a
  230. #define RBCH 0x2b
  231. #define XBCH 0x2b
  232. #define CCR0 0x2c
  233. #define CCR1 0x2d
  234. #define CCR2 0x2e
  235. #define CCR3 0x2f
  236. #define VSTR 0x34
  237. #define BGR 0x34
  238. #define RLCR 0x35
  239. #define AML 0x36
  240. #define AMH 0x37
  241. #define GIS 0x38
  242. #define IVA 0x38
  243. #define IPC 0x39
  244. #define ISR 0x3a
  245. #define IMR 0x3a
  246. #define PVR 0x3c
  247. #define PIS 0x3d
  248. #define PIM 0x3d
  249. #define PCR 0x3e
  250. #define CCR4 0x3f
  251. // IMR/ISR
  252. #define IRQ_BREAK_ON BIT15 // rx break detected
  253. #define IRQ_DATAOVERRUN BIT14 // receive data overflow
  254. #define IRQ_ALLSENT BIT13 // all sent
  255. #define IRQ_UNDERRUN BIT12 // transmit data underrun
  256. #define IRQ_TIMER BIT11 // timer interrupt
  257. #define IRQ_CTS BIT10 // CTS status change
  258. #define IRQ_TXREPEAT BIT9 // tx message repeat
  259. #define IRQ_TXFIFO BIT8 // transmit pool ready
  260. #define IRQ_RXEOM BIT7 // receive message end
  261. #define IRQ_EXITHUNT BIT6 // receive frame start
  262. #define IRQ_RXTIME BIT6 // rx char timeout
  263. #define IRQ_DCD BIT2 // carrier detect status change
  264. #define IRQ_OVERRUN BIT1 // receive frame overflow
  265. #define IRQ_RXFIFO BIT0 // receive pool full
  266. // STAR
  267. #define XFW BIT6 // transmit FIFO write enable
  268. #define CEC BIT2 // command executing
  269. #define CTS BIT1 // CTS state
  270. #define PVR_DTR BIT0
  271. #define PVR_DSR BIT1
  272. #define PVR_RI BIT2
  273. #define PVR_AUTOCTS BIT3
  274. #define PVR_RS232 0x20 /* 0010b */
  275. #define PVR_V35 0xe0 /* 1110b */
  276. #define PVR_RS422 0x40 /* 0100b */
  277. /* Register access functions */
  278. #define write_reg(info, reg, val) outb((val),(info)->io_base + (reg))
  279. #define read_reg(info, reg) inb((info)->io_base + (reg))
  280. #define read_reg16(info, reg) inw((info)->io_base + (reg))
  281. #define write_reg16(info, reg, val) outw((val), (info)->io_base + (reg))
  282. #define set_reg_bits(info, reg, mask) \
  283. write_reg(info, (reg), \
  284. (unsigned char) (read_reg(info, (reg)) | (mask)))
  285. #define clear_reg_bits(info, reg, mask) \
  286. write_reg(info, (reg), \
  287. (unsigned char) (read_reg(info, (reg)) & ~(mask)))
  288. /*
  289. * interrupt enable/disable routines
  290. */
  291. static void irq_disable(MGSLPC_INFO *info, unsigned char channel, unsigned short mask)
  292. {
  293. if (channel == CHA) {
  294. info->imra_value |= mask;
  295. write_reg16(info, CHA + IMR, info->imra_value);
  296. } else {
  297. info->imrb_value |= mask;
  298. write_reg16(info, CHB + IMR, info->imrb_value);
  299. }
  300. }
  301. static void irq_enable(MGSLPC_INFO *info, unsigned char channel, unsigned short mask)
  302. {
  303. if (channel == CHA) {
  304. info->imra_value &= ~mask;
  305. write_reg16(info, CHA + IMR, info->imra_value);
  306. } else {
  307. info->imrb_value &= ~mask;
  308. write_reg16(info, CHB + IMR, info->imrb_value);
  309. }
  310. }
  311. #define port_irq_disable(info, mask) \
  312. { info->pim_value |= (mask); write_reg(info, PIM, info->pim_value); }
  313. #define port_irq_enable(info, mask) \
  314. { info->pim_value &= ~(mask); write_reg(info, PIM, info->pim_value); }
  315. static void rx_start(MGSLPC_INFO *info);
  316. static void rx_stop(MGSLPC_INFO *info);
  317. static void tx_start(MGSLPC_INFO *info);
  318. static void tx_stop(MGSLPC_INFO *info);
  319. static void tx_set_idle(MGSLPC_INFO *info);
  320. static void get_signals(MGSLPC_INFO *info);
  321. static void set_signals(MGSLPC_INFO *info);
  322. static void reset_device(MGSLPC_INFO *info);
  323. static void hdlc_mode(MGSLPC_INFO *info);
  324. static void async_mode(MGSLPC_INFO *info);
  325. static void tx_timeout(unsigned long context);
  326. static int ioctl_common(MGSLPC_INFO *info, unsigned int cmd, unsigned long arg);
  327. #ifdef CONFIG_HDLC
  328. #define dev_to_port(D) (dev_to_hdlc(D)->priv)
  329. static void hdlcdev_tx_done(MGSLPC_INFO *info);
  330. static void hdlcdev_rx(MGSLPC_INFO *info, char *buf, int size);
  331. static int hdlcdev_init(MGSLPC_INFO *info);
  332. static void hdlcdev_exit(MGSLPC_INFO *info);
  333. #endif
  334. static void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit);
  335. static BOOLEAN register_test(MGSLPC_INFO *info);
  336. static BOOLEAN irq_test(MGSLPC_INFO *info);
  337. static int adapter_test(MGSLPC_INFO *info);
  338. static int claim_resources(MGSLPC_INFO *info);
  339. static void release_resources(MGSLPC_INFO *info);
  340. static void mgslpc_add_device(MGSLPC_INFO *info);
  341. static void mgslpc_remove_device(MGSLPC_INFO *info);
  342. static int rx_get_frame(MGSLPC_INFO *info);
  343. static void rx_reset_buffers(MGSLPC_INFO *info);
  344. static int rx_alloc_buffers(MGSLPC_INFO *info);
  345. static void rx_free_buffers(MGSLPC_INFO *info);
  346. static irqreturn_t mgslpc_isr(int irq, void *dev_id, struct pt_regs * regs);
  347. /*
  348. * Bottom half interrupt handlers
  349. */
  350. static void bh_handler(void* Context);
  351. static void bh_transmit(MGSLPC_INFO *info);
  352. static void bh_status(MGSLPC_INFO *info);
  353. /*
  354. * ioctl handlers
  355. */
  356. static int tiocmget(struct tty_struct *tty, struct file *file);
  357. static int tiocmset(struct tty_struct *tty, struct file *file,
  358. unsigned int set, unsigned int clear);
  359. static int get_stats(MGSLPC_INFO *info, struct mgsl_icount __user *user_icount);
  360. static int get_params(MGSLPC_INFO *info, MGSL_PARAMS __user *user_params);
  361. static int set_params(MGSLPC_INFO *info, MGSL_PARAMS __user *new_params);
  362. static int get_txidle(MGSLPC_INFO *info, int __user *idle_mode);
  363. static int set_txidle(MGSLPC_INFO *info, int idle_mode);
  364. static int set_txenable(MGSLPC_INFO *info, int enable);
  365. static int tx_abort(MGSLPC_INFO *info);
  366. static int set_rxenable(MGSLPC_INFO *info, int enable);
  367. static int wait_events(MGSLPC_INFO *info, int __user *mask);
  368. static MGSLPC_INFO *mgslpc_device_list = NULL;
  369. static int mgslpc_device_count = 0;
  370. /*
  371. * Set this param to non-zero to load eax with the
  372. * .text section address and breakpoint on module load.
  373. * This is useful for use with gdb and add-symbol-file command.
  374. */
  375. static int break_on_load=0;
  376. /*
  377. * Driver major number, defaults to zero to get auto
  378. * assigned major number. May be forced as module parameter.
  379. */
  380. static int ttymajor=0;
  381. static int debug_level = 0;
  382. static int maxframe[MAX_DEVICE_COUNT] = {0,};
  383. static int dosyncppp[MAX_DEVICE_COUNT] = {1,1,1,1};
  384. module_param(break_on_load, bool, 0);
  385. module_param(ttymajor, int, 0);
  386. module_param(debug_level, int, 0);
  387. module_param_array(maxframe, int, NULL, 0);
  388. module_param_array(dosyncppp, int, NULL, 0);
  389. MODULE_LICENSE("GPL");
  390. static char *driver_name = "SyncLink PC Card driver";
  391. static char *driver_version = "$Revision: 4.26 $";
  392. static struct tty_driver *serial_driver;
  393. /* number of characters left in xmit buffer before we ask for more */
  394. #define WAKEUP_CHARS 256
  395. static void mgslpc_change_params(MGSLPC_INFO *info);
  396. static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout);
  397. /* PCMCIA prototypes */
  398. static void mgslpc_config(dev_link_t *link);
  399. static void mgslpc_release(u_long arg);
  400. static int mgslpc_event(event_t event, int priority,
  401. event_callback_args_t *args);
  402. static dev_link_t *mgslpc_attach(void);
  403. static void mgslpc_detach(dev_link_t *);
  404. static dev_info_t dev_info = "synclink_cs";
  405. static dev_link_t *dev_list = NULL;
  406. /*
  407. * 1st function defined in .text section. Calling this function in
  408. * init_module() followed by a breakpoint allows a remote debugger
  409. * (gdb) to get the .text address for the add-symbol-file command.
  410. * This allows remote debugging of dynamically loadable modules.
  411. */
  412. static void* mgslpc_get_text_ptr(void)
  413. {
  414. return mgslpc_get_text_ptr;
  415. }
  416. /**
  417. * line discipline callback wrappers
  418. *
  419. * The wrappers maintain line discipline references
  420. * while calling into the line discipline.
  421. *
  422. * ldisc_flush_buffer - flush line discipline receive buffers
  423. * ldisc_receive_buf - pass receive data to line discipline
  424. */
  425. static void ldisc_flush_buffer(struct tty_struct *tty)
  426. {
  427. struct tty_ldisc *ld = tty_ldisc_ref(tty);
  428. if (ld) {
  429. if (ld->flush_buffer)
  430. ld->flush_buffer(tty);
  431. tty_ldisc_deref(ld);
  432. }
  433. }
  434. static void ldisc_receive_buf(struct tty_struct *tty,
  435. const __u8 *data, char *flags, int count)
  436. {
  437. struct tty_ldisc *ld;
  438. if (!tty)
  439. return;
  440. ld = tty_ldisc_ref(tty);
  441. if (ld) {
  442. if (ld->receive_buf)
  443. ld->receive_buf(tty, data, flags, count);
  444. tty_ldisc_deref(ld);
  445. }
  446. }
  447. static dev_link_t *mgslpc_attach(void)
  448. {
  449. MGSLPC_INFO *info;
  450. dev_link_t *link;
  451. client_reg_t client_reg;
  452. int ret;
  453. if (debug_level >= DEBUG_LEVEL_INFO)
  454. printk("mgslpc_attach\n");
  455. info = (MGSLPC_INFO *)kmalloc(sizeof(MGSLPC_INFO), GFP_KERNEL);
  456. if (!info) {
  457. printk("Error can't allocate device instance data\n");
  458. return NULL;
  459. }
  460. memset(info, 0, sizeof(MGSLPC_INFO));
  461. info->magic = MGSLPC_MAGIC;
  462. INIT_WORK(&info->task, bh_handler, info);
  463. info->max_frame_size = 4096;
  464. info->close_delay = 5*HZ/10;
  465. info->closing_wait = 30*HZ;
  466. init_waitqueue_head(&info->open_wait);
  467. init_waitqueue_head(&info->close_wait);
  468. init_waitqueue_head(&info->status_event_wait_q);
  469. init_waitqueue_head(&info->event_wait_q);
  470. spin_lock_init(&info->lock);
  471. spin_lock_init(&info->netlock);
  472. memcpy(&info->params,&default_params,sizeof(MGSL_PARAMS));
  473. info->idle_mode = HDLC_TXIDLE_FLAGS;
  474. info->imra_value = 0xffff;
  475. info->imrb_value = 0xffff;
  476. info->pim_value = 0xff;
  477. link = &info->link;
  478. link->priv = info;
  479. /* Initialize the dev_link_t structure */
  480. /* Interrupt setup */
  481. link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
  482. link->irq.IRQInfo1 = IRQ_LEVEL_ID;
  483. link->irq.Handler = NULL;
  484. link->conf.Attributes = 0;
  485. link->conf.Vcc = 50;
  486. link->conf.IntType = INT_MEMORY_AND_IO;
  487. /* Register with Card Services */
  488. link->next = dev_list;
  489. dev_list = link;
  490. client_reg.dev_info = &dev_info;
  491. client_reg.EventMask =
  492. CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
  493. CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
  494. CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
  495. client_reg.event_handler = &mgslpc_event;
  496. client_reg.Version = 0x0210;
  497. client_reg.event_callback_args.client_data = link;
  498. ret = pcmcia_register_client(&link->handle, &client_reg);
  499. if (ret != CS_SUCCESS) {
  500. cs_error(link->handle, RegisterClient, ret);
  501. mgslpc_detach(link);
  502. return NULL;
  503. }
  504. mgslpc_add_device(info);
  505. return link;
  506. }
  507. /* Card has been inserted.
  508. */
  509. #define CS_CHECK(fn, ret) \
  510. do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
  511. static void mgslpc_config(dev_link_t *link)
  512. {
  513. client_handle_t handle = link->handle;
  514. MGSLPC_INFO *info = link->priv;
  515. tuple_t tuple;
  516. cisparse_t parse;
  517. int last_fn, last_ret;
  518. u_char buf[64];
  519. config_info_t conf;
  520. cistpl_cftable_entry_t dflt = { 0 };
  521. cistpl_cftable_entry_t *cfg;
  522. if (debug_level >= DEBUG_LEVEL_INFO)
  523. printk("mgslpc_config(0x%p)\n", link);
  524. /* read CONFIG tuple to find its configuration registers */
  525. tuple.DesiredTuple = CISTPL_CONFIG;
  526. tuple.Attributes = 0;
  527. tuple.TupleData = buf;
  528. tuple.TupleDataMax = sizeof(buf);
  529. tuple.TupleOffset = 0;
  530. CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
  531. CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
  532. CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
  533. link->conf.ConfigBase = parse.config.base;
  534. link->conf.Present = parse.config.rmask[0];
  535. /* Configure card */
  536. link->state |= DEV_CONFIG;
  537. /* Look up the current Vcc */
  538. CS_CHECK(GetConfigurationInfo, pcmcia_get_configuration_info(handle, &conf));
  539. link->conf.Vcc = conf.Vcc;
  540. /* get CIS configuration entry */
  541. tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
  542. CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
  543. cfg = &(parse.cftable_entry);
  544. CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
  545. CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
  546. if (cfg->flags & CISTPL_CFTABLE_DEFAULT) dflt = *cfg;
  547. if (cfg->index == 0)
  548. goto cs_failed;
  549. link->conf.ConfigIndex = cfg->index;
  550. link->conf.Attributes |= CONF_ENABLE_IRQ;
  551. /* IO window settings */
  552. link->io.NumPorts1 = 0;
  553. if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
  554. cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
  555. link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
  556. if (!(io->flags & CISTPL_IO_8BIT))
  557. link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
  558. if (!(io->flags & CISTPL_IO_16BIT))
  559. link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
  560. link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
  561. link->io.BasePort1 = io->win[0].base;
  562. link->io.NumPorts1 = io->win[0].len;
  563. CS_CHECK(RequestIO, pcmcia_request_io(link->handle, &link->io));
  564. }
  565. link->conf.Attributes = CONF_ENABLE_IRQ;
  566. link->conf.Vcc = 50;
  567. link->conf.IntType = INT_MEMORY_AND_IO;
  568. link->conf.ConfigIndex = 8;
  569. link->conf.Present = PRESENT_OPTION;
  570. link->irq.Attributes |= IRQ_HANDLE_PRESENT;
  571. link->irq.Handler = mgslpc_isr;
  572. link->irq.Instance = info;
  573. CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
  574. CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link->handle, &link->conf));
  575. info->io_base = link->io.BasePort1;
  576. info->irq_level = link->irq.AssignedIRQ;
  577. /* add to linked list of devices */
  578. sprintf(info->node.dev_name, "mgslpc0");
  579. info->node.major = info->node.minor = 0;
  580. link->dev = &info->node;
  581. printk(KERN_INFO "%s: index 0x%02x:",
  582. info->node.dev_name, link->conf.ConfigIndex);
  583. if (link->conf.Attributes & CONF_ENABLE_IRQ)
  584. printk(", irq %d", link->irq.AssignedIRQ);
  585. if (link->io.NumPorts1)
  586. printk(", io 0x%04x-0x%04x", link->io.BasePort1,
  587. link->io.BasePort1+link->io.NumPorts1-1);
  588. printk("\n");
  589. link->state &= ~DEV_CONFIG_PENDING;
  590. return;
  591. cs_failed:
  592. cs_error(link->handle, last_fn, last_ret);
  593. mgslpc_release((u_long)link);
  594. }
  595. /* Card has been removed.
  596. * Unregister device and release PCMCIA configuration.
  597. * If device is open, postpone until it is closed.
  598. */
  599. static void mgslpc_release(u_long arg)
  600. {
  601. dev_link_t *link = (dev_link_t *)arg;
  602. if (debug_level >= DEBUG_LEVEL_INFO)
  603. printk("mgslpc_release(0x%p)\n", link);
  604. /* Unlink the device chain */
  605. link->dev = NULL;
  606. link->state &= ~DEV_CONFIG;
  607. pcmcia_release_configuration(link->handle);
  608. if (link->io.NumPorts1)
  609. pcmcia_release_io(link->handle, &link->io);
  610. if (link->irq.AssignedIRQ)
  611. pcmcia_release_irq(link->handle, &link->irq);
  612. if (link->state & DEV_STALE_LINK)
  613. mgslpc_detach(link);
  614. }
  615. static void mgslpc_detach(dev_link_t *link)
  616. {
  617. dev_link_t **linkp;
  618. if (debug_level >= DEBUG_LEVEL_INFO)
  619. printk("mgslpc_detach(0x%p)\n", link);
  620. /* find device */
  621. for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
  622. if (*linkp == link) break;
  623. if (*linkp == NULL)
  624. return;
  625. if (link->state & DEV_CONFIG) {
  626. /* device is configured/active, mark it so when
  627. * release() is called a proper detach() occurs.
  628. */
  629. if (debug_level >= DEBUG_LEVEL_INFO)
  630. printk(KERN_DEBUG "synclinkpc: detach postponed, '%s' "
  631. "still locked\n", link->dev->dev_name);
  632. link->state |= DEV_STALE_LINK;
  633. return;
  634. }
  635. /* Break the link with Card Services */
  636. if (link->handle)
  637. pcmcia_deregister_client(link->handle);
  638. /* Unlink device structure, and free it */
  639. *linkp = link->next;
  640. mgslpc_remove_device((MGSLPC_INFO *)link->priv);
  641. }
  642. static int mgslpc_event(event_t event, int priority,
  643. event_callback_args_t *args)
  644. {
  645. dev_link_t *link = args->client_data;
  646. MGSLPC_INFO *info = link->priv;
  647. if (debug_level >= DEBUG_LEVEL_INFO)
  648. printk("mgslpc_event(0x%06x)\n", event);
  649. switch (event) {
  650. case CS_EVENT_CARD_REMOVAL:
  651. link->state &= ~DEV_PRESENT;
  652. if (link->state & DEV_CONFIG) {
  653. ((MGSLPC_INFO *)link->priv)->stop = 1;
  654. mgslpc_release((u_long)link);
  655. }
  656. break;
  657. case CS_EVENT_CARD_INSERTION:
  658. link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
  659. mgslpc_config(link);
  660. break;
  661. case CS_EVENT_PM_SUSPEND:
  662. link->state |= DEV_SUSPEND;
  663. /* Fall through... */
  664. case CS_EVENT_RESET_PHYSICAL:
  665. /* Mark the device as stopped, to block IO until later */
  666. info->stop = 1;
  667. if (link->state & DEV_CONFIG)
  668. pcmcia_release_configuration(link->handle);
  669. break;
  670. case CS_EVENT_PM_RESUME:
  671. link->state &= ~DEV_SUSPEND;
  672. /* Fall through... */
  673. case CS_EVENT_CARD_RESET:
  674. if (link->state & DEV_CONFIG)
  675. pcmcia_request_configuration(link->handle, &link->conf);
  676. info->stop = 0;
  677. break;
  678. }
  679. return 0;
  680. }
  681. static inline int mgslpc_paranoia_check(MGSLPC_INFO *info,
  682. char *name, const char *routine)
  683. {
  684. #ifdef MGSLPC_PARANOIA_CHECK
  685. static const char *badmagic =
  686. "Warning: bad magic number for mgsl struct (%s) in %s\n";
  687. static const char *badinfo =
  688. "Warning: null mgslpc_info for (%s) in %s\n";
  689. if (!info) {
  690. printk(badinfo, name, routine);
  691. return 1;
  692. }
  693. if (info->magic != MGSLPC_MAGIC) {
  694. printk(badmagic, name, routine);
  695. return 1;
  696. }
  697. #else
  698. if (!info)
  699. return 1;
  700. #endif
  701. return 0;
  702. }
  703. #define CMD_RXFIFO BIT7 // release current rx FIFO
  704. #define CMD_RXRESET BIT6 // receiver reset
  705. #define CMD_RXFIFO_READ BIT5
  706. #define CMD_START_TIMER BIT4
  707. #define CMD_TXFIFO BIT3 // release current tx FIFO
  708. #define CMD_TXEOM BIT1 // transmit end message
  709. #define CMD_TXRESET BIT0 // transmit reset
  710. static BOOLEAN wait_command_complete(MGSLPC_INFO *info, unsigned char channel)
  711. {
  712. int i = 0;
  713. /* wait for command completion */
  714. while (read_reg(info, (unsigned char)(channel+STAR)) & BIT2) {
  715. udelay(1);
  716. if (i++ == 1000)
  717. return FALSE;
  718. }
  719. return TRUE;
  720. }
  721. static void issue_command(MGSLPC_INFO *info, unsigned char channel, unsigned char cmd)
  722. {
  723. wait_command_complete(info, channel);
  724. write_reg(info, (unsigned char) (channel + CMDR), cmd);
  725. }
  726. static void tx_pause(struct tty_struct *tty)
  727. {
  728. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  729. unsigned long flags;
  730. if (mgslpc_paranoia_check(info, tty->name, "tx_pause"))
  731. return;
  732. if (debug_level >= DEBUG_LEVEL_INFO)
  733. printk("tx_pause(%s)\n",info->device_name);
  734. spin_lock_irqsave(&info->lock,flags);
  735. if (info->tx_enabled)
  736. tx_stop(info);
  737. spin_unlock_irqrestore(&info->lock,flags);
  738. }
  739. static void tx_release(struct tty_struct *tty)
  740. {
  741. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  742. unsigned long flags;
  743. if (mgslpc_paranoia_check(info, tty->name, "tx_release"))
  744. return;
  745. if (debug_level >= DEBUG_LEVEL_INFO)
  746. printk("tx_release(%s)\n",info->device_name);
  747. spin_lock_irqsave(&info->lock,flags);
  748. if (!info->tx_enabled)
  749. tx_start(info);
  750. spin_unlock_irqrestore(&info->lock,flags);
  751. }
  752. /* Return next bottom half action to perform.
  753. * or 0 if nothing to do.
  754. */
  755. static int bh_action(MGSLPC_INFO *info)
  756. {
  757. unsigned long flags;
  758. int rc = 0;
  759. spin_lock_irqsave(&info->lock,flags);
  760. if (info->pending_bh & BH_RECEIVE) {
  761. info->pending_bh &= ~BH_RECEIVE;
  762. rc = BH_RECEIVE;
  763. } else if (info->pending_bh & BH_TRANSMIT) {
  764. info->pending_bh &= ~BH_TRANSMIT;
  765. rc = BH_TRANSMIT;
  766. } else if (info->pending_bh & BH_STATUS) {
  767. info->pending_bh &= ~BH_STATUS;
  768. rc = BH_STATUS;
  769. }
  770. if (!rc) {
  771. /* Mark BH routine as complete */
  772. info->bh_running = 0;
  773. info->bh_requested = 0;
  774. }
  775. spin_unlock_irqrestore(&info->lock,flags);
  776. return rc;
  777. }
  778. void bh_handler(void* Context)
  779. {
  780. MGSLPC_INFO *info = (MGSLPC_INFO*)Context;
  781. int action;
  782. if (!info)
  783. return;
  784. if (debug_level >= DEBUG_LEVEL_BH)
  785. printk( "%s(%d):bh_handler(%s) entry\n",
  786. __FILE__,__LINE__,info->device_name);
  787. info->bh_running = 1;
  788. while((action = bh_action(info)) != 0) {
  789. /* Process work item */
  790. if ( debug_level >= DEBUG_LEVEL_BH )
  791. printk( "%s(%d):bh_handler() work item action=%d\n",
  792. __FILE__,__LINE__,action);
  793. switch (action) {
  794. case BH_RECEIVE:
  795. while(rx_get_frame(info));
  796. break;
  797. case BH_TRANSMIT:
  798. bh_transmit(info);
  799. break;
  800. case BH_STATUS:
  801. bh_status(info);
  802. break;
  803. default:
  804. /* unknown work item ID */
  805. printk("Unknown work item ID=%08X!\n", action);
  806. break;
  807. }
  808. }
  809. if (debug_level >= DEBUG_LEVEL_BH)
  810. printk( "%s(%d):bh_handler(%s) exit\n",
  811. __FILE__,__LINE__,info->device_name);
  812. }
  813. void bh_transmit(MGSLPC_INFO *info)
  814. {
  815. struct tty_struct *tty = info->tty;
  816. if (debug_level >= DEBUG_LEVEL_BH)
  817. printk("bh_transmit() entry on %s\n", info->device_name);
  818. if (tty) {
  819. tty_wakeup(tty);
  820. wake_up_interruptible(&tty->write_wait);
  821. }
  822. }
  823. void bh_status(MGSLPC_INFO *info)
  824. {
  825. info->ri_chkcount = 0;
  826. info->dsr_chkcount = 0;
  827. info->dcd_chkcount = 0;
  828. info->cts_chkcount = 0;
  829. }
  830. /* eom: non-zero = end of frame */
  831. static void rx_ready_hdlc(MGSLPC_INFO *info, int eom)
  832. {
  833. unsigned char data[2];
  834. unsigned char fifo_count, read_count, i;
  835. RXBUF *buf = (RXBUF*)(info->rx_buf + (info->rx_put * info->rx_buf_size));
  836. if (debug_level >= DEBUG_LEVEL_ISR)
  837. printk("%s(%d):rx_ready_hdlc(eom=%d)\n",__FILE__,__LINE__,eom);
  838. if (!info->rx_enabled)
  839. return;
  840. if (info->rx_frame_count >= info->rx_buf_count) {
  841. /* no more free buffers */
  842. issue_command(info, CHA, CMD_RXRESET);
  843. info->pending_bh |= BH_RECEIVE;
  844. info->rx_overflow = 1;
  845. info->icount.buf_overrun++;
  846. return;
  847. }
  848. if (eom) {
  849. /* end of frame, get FIFO count from RBCL register */
  850. if (!(fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f)))
  851. fifo_count = 32;
  852. } else
  853. fifo_count = 32;
  854. do {
  855. if (fifo_count == 1) {
  856. read_count = 1;
  857. data[0] = read_reg(info, CHA + RXFIFO);
  858. } else {
  859. read_count = 2;
  860. *((unsigned short *) data) = read_reg16(info, CHA + RXFIFO);
  861. }
  862. fifo_count -= read_count;
  863. if (!fifo_count && eom)
  864. buf->status = data[--read_count];
  865. for (i = 0; i < read_count; i++) {
  866. if (buf->count >= info->max_frame_size) {
  867. /* frame too large, reset receiver and reset current buffer */
  868. issue_command(info, CHA, CMD_RXRESET);
  869. buf->count = 0;
  870. return;
  871. }
  872. *(buf->data + buf->count) = data[i];
  873. buf->count++;
  874. }
  875. } while (fifo_count);
  876. if (eom) {
  877. info->pending_bh |= BH_RECEIVE;
  878. info->rx_frame_count++;
  879. info->rx_put++;
  880. if (info->rx_put >= info->rx_buf_count)
  881. info->rx_put = 0;
  882. }
  883. issue_command(info, CHA, CMD_RXFIFO);
  884. }
  885. static void rx_ready_async(MGSLPC_INFO *info, int tcd)
  886. {
  887. unsigned char data, status;
  888. int fifo_count;
  889. struct tty_struct *tty = info->tty;
  890. struct mgsl_icount *icount = &info->icount;
  891. if (tcd) {
  892. /* early termination, get FIFO count from RBCL register */
  893. fifo_count = (unsigned char)(read_reg(info, CHA+RBCL) & 0x1f);
  894. /* Zero fifo count could mean 0 or 32 bytes available.
  895. * If BIT5 of STAR is set then at least 1 byte is available.
  896. */
  897. if (!fifo_count && (read_reg(info,CHA+STAR) & BIT5))
  898. fifo_count = 32;
  899. } else
  900. fifo_count = 32;
  901. /* Flush received async data to receive data buffer. */
  902. while (fifo_count) {
  903. data = read_reg(info, CHA + RXFIFO);
  904. status = read_reg(info, CHA + RXFIFO);
  905. fifo_count -= 2;
  906. if (tty->flip.count >= TTY_FLIPBUF_SIZE)
  907. break;
  908. *tty->flip.char_buf_ptr = data;
  909. icount->rx++;
  910. *tty->flip.flag_buf_ptr = 0;
  911. // if no frameing/crc error then save data
  912. // BIT7:parity error
  913. // BIT6:framing error
  914. if (status & (BIT7 + BIT6)) {
  915. if (status & BIT7)
  916. icount->parity++;
  917. else
  918. icount->frame++;
  919. /* discard char if tty control flags say so */
  920. if (status & info->ignore_status_mask)
  921. continue;
  922. status &= info->read_status_mask;
  923. if (status & BIT7)
  924. *tty->flip.flag_buf_ptr = TTY_PARITY;
  925. else if (status & BIT6)
  926. *tty->flip.flag_buf_ptr = TTY_FRAME;
  927. }
  928. tty->flip.flag_buf_ptr++;
  929. tty->flip.char_buf_ptr++;
  930. tty->flip.count++;
  931. }
  932. issue_command(info, CHA, CMD_RXFIFO);
  933. if (debug_level >= DEBUG_LEVEL_ISR) {
  934. printk("%s(%d):rx_ready_async count=%d\n",
  935. __FILE__,__LINE__,tty->flip.count);
  936. printk("%s(%d):rx=%d brk=%d parity=%d frame=%d overrun=%d\n",
  937. __FILE__,__LINE__,icount->rx,icount->brk,
  938. icount->parity,icount->frame,icount->overrun);
  939. }
  940. if (tty->flip.count)
  941. tty_flip_buffer_push(tty);
  942. }
  943. static void tx_done(MGSLPC_INFO *info)
  944. {
  945. if (!info->tx_active)
  946. return;
  947. info->tx_active = 0;
  948. info->tx_aborting = 0;
  949. if (info->params.mode == MGSL_MODE_ASYNC)
  950. return;
  951. info->tx_count = info->tx_put = info->tx_get = 0;
  952. del_timer(&info->tx_timer);
  953. if (info->drop_rts_on_tx_done) {
  954. get_signals(info);
  955. if (info->serial_signals & SerialSignal_RTS) {
  956. info->serial_signals &= ~SerialSignal_RTS;
  957. set_signals(info);
  958. }
  959. info->drop_rts_on_tx_done = 0;
  960. }
  961. #ifdef CONFIG_HDLC
  962. if (info->netcount)
  963. hdlcdev_tx_done(info);
  964. else
  965. #endif
  966. {
  967. if (info->tty->stopped || info->tty->hw_stopped) {
  968. tx_stop(info);
  969. return;
  970. }
  971. info->pending_bh |= BH_TRANSMIT;
  972. }
  973. }
  974. static void tx_ready(MGSLPC_INFO *info)
  975. {
  976. unsigned char fifo_count = 32;
  977. int c;
  978. if (debug_level >= DEBUG_LEVEL_ISR)
  979. printk("%s(%d):tx_ready(%s)\n", __FILE__,__LINE__,info->device_name);
  980. if (info->params.mode == MGSL_MODE_HDLC) {
  981. if (!info->tx_active)
  982. return;
  983. } else {
  984. if (info->tty->stopped || info->tty->hw_stopped) {
  985. tx_stop(info);
  986. return;
  987. }
  988. if (!info->tx_count)
  989. info->tx_active = 0;
  990. }
  991. if (!info->tx_count)
  992. return;
  993. while (info->tx_count && fifo_count) {
  994. c = min(2, min_t(int, fifo_count, min(info->tx_count, TXBUFSIZE - info->tx_get)));
  995. if (c == 1) {
  996. write_reg(info, CHA + TXFIFO, *(info->tx_buf + info->tx_get));
  997. } else {
  998. write_reg16(info, CHA + TXFIFO,
  999. *((unsigned short*)(info->tx_buf + info->tx_get)));
  1000. }
  1001. info->tx_count -= c;
  1002. info->tx_get = (info->tx_get + c) & (TXBUFSIZE - 1);
  1003. fifo_count -= c;
  1004. }
  1005. if (info->params.mode == MGSL_MODE_ASYNC) {
  1006. if (info->tx_count < WAKEUP_CHARS)
  1007. info->pending_bh |= BH_TRANSMIT;
  1008. issue_command(info, CHA, CMD_TXFIFO);
  1009. } else {
  1010. if (info->tx_count)
  1011. issue_command(info, CHA, CMD_TXFIFO);
  1012. else
  1013. issue_command(info, CHA, CMD_TXFIFO + CMD_TXEOM);
  1014. }
  1015. }
  1016. static void cts_change(MGSLPC_INFO *info)
  1017. {
  1018. get_signals(info);
  1019. if ((info->cts_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
  1020. irq_disable(info, CHB, IRQ_CTS);
  1021. info->icount.cts++;
  1022. if (info->serial_signals & SerialSignal_CTS)
  1023. info->input_signal_events.cts_up++;
  1024. else
  1025. info->input_signal_events.cts_down++;
  1026. wake_up_interruptible(&info->status_event_wait_q);
  1027. wake_up_interruptible(&info->event_wait_q);
  1028. if (info->flags & ASYNC_CTS_FLOW) {
  1029. if (info->tty->hw_stopped) {
  1030. if (info->serial_signals & SerialSignal_CTS) {
  1031. if (debug_level >= DEBUG_LEVEL_ISR)
  1032. printk("CTS tx start...");
  1033. if (info->tty)
  1034. info->tty->hw_stopped = 0;
  1035. tx_start(info);
  1036. info->pending_bh |= BH_TRANSMIT;
  1037. return;
  1038. }
  1039. } else {
  1040. if (!(info->serial_signals & SerialSignal_CTS)) {
  1041. if (debug_level >= DEBUG_LEVEL_ISR)
  1042. printk("CTS tx stop...");
  1043. if (info->tty)
  1044. info->tty->hw_stopped = 1;
  1045. tx_stop(info);
  1046. }
  1047. }
  1048. }
  1049. info->pending_bh |= BH_STATUS;
  1050. }
  1051. static void dcd_change(MGSLPC_INFO *info)
  1052. {
  1053. get_signals(info);
  1054. if ((info->dcd_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
  1055. irq_disable(info, CHB, IRQ_DCD);
  1056. info->icount.dcd++;
  1057. if (info->serial_signals & SerialSignal_DCD) {
  1058. info->input_signal_events.dcd_up++;
  1059. }
  1060. else
  1061. info->input_signal_events.dcd_down++;
  1062. #ifdef CONFIG_HDLC
  1063. if (info->netcount)
  1064. hdlc_set_carrier(info->serial_signals & SerialSignal_DCD, info->netdev);
  1065. #endif
  1066. wake_up_interruptible(&info->status_event_wait_q);
  1067. wake_up_interruptible(&info->event_wait_q);
  1068. if (info->flags & ASYNC_CHECK_CD) {
  1069. if (debug_level >= DEBUG_LEVEL_ISR)
  1070. printk("%s CD now %s...", info->device_name,
  1071. (info->serial_signals & SerialSignal_DCD) ? "on" : "off");
  1072. if (info->serial_signals & SerialSignal_DCD)
  1073. wake_up_interruptible(&info->open_wait);
  1074. else {
  1075. if (debug_level >= DEBUG_LEVEL_ISR)
  1076. printk("doing serial hangup...");
  1077. if (info->tty)
  1078. tty_hangup(info->tty);
  1079. }
  1080. }
  1081. info->pending_bh |= BH_STATUS;
  1082. }
  1083. static void dsr_change(MGSLPC_INFO *info)
  1084. {
  1085. get_signals(info);
  1086. if ((info->dsr_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
  1087. port_irq_disable(info, PVR_DSR);
  1088. info->icount.dsr++;
  1089. if (info->serial_signals & SerialSignal_DSR)
  1090. info->input_signal_events.dsr_up++;
  1091. else
  1092. info->input_signal_events.dsr_down++;
  1093. wake_up_interruptible(&info->status_event_wait_q);
  1094. wake_up_interruptible(&info->event_wait_q);
  1095. info->pending_bh |= BH_STATUS;
  1096. }
  1097. static void ri_change(MGSLPC_INFO *info)
  1098. {
  1099. get_signals(info);
  1100. if ((info->ri_chkcount)++ >= IO_PIN_SHUTDOWN_LIMIT)
  1101. port_irq_disable(info, PVR_RI);
  1102. info->icount.rng++;
  1103. if (info->serial_signals & SerialSignal_RI)
  1104. info->input_signal_events.ri_up++;
  1105. else
  1106. info->input_signal_events.ri_down++;
  1107. wake_up_interruptible(&info->status_event_wait_q);
  1108. wake_up_interruptible(&info->event_wait_q);
  1109. info->pending_bh |= BH_STATUS;
  1110. }
  1111. /* Interrupt service routine entry point.
  1112. *
  1113. * Arguments:
  1114. *
  1115. * irq interrupt number that caused interrupt
  1116. * dev_id device ID supplied during interrupt registration
  1117. * regs interrupted processor context
  1118. */
  1119. static irqreturn_t mgslpc_isr(int irq, void *dev_id, struct pt_regs * regs)
  1120. {
  1121. MGSLPC_INFO * info = (MGSLPC_INFO *)dev_id;
  1122. unsigned short isr;
  1123. unsigned char gis, pis;
  1124. int count=0;
  1125. if (debug_level >= DEBUG_LEVEL_ISR)
  1126. printk("mgslpc_isr(%d) entry.\n", irq);
  1127. if (!info)
  1128. return IRQ_NONE;
  1129. if (!(info->link.state & DEV_CONFIG))
  1130. return IRQ_HANDLED;
  1131. spin_lock(&info->lock);
  1132. while ((gis = read_reg(info, CHA + GIS))) {
  1133. if (debug_level >= DEBUG_LEVEL_ISR)
  1134. printk("mgslpc_isr %s gis=%04X\n", info->device_name,gis);
  1135. if ((gis & 0x70) || count > 1000) {
  1136. printk("synclink_cs:hardware failed or ejected\n");
  1137. break;
  1138. }
  1139. count++;
  1140. if (gis & (BIT1 + BIT0)) {
  1141. isr = read_reg16(info, CHB + ISR);
  1142. if (isr & IRQ_DCD)
  1143. dcd_change(info);
  1144. if (isr & IRQ_CTS)
  1145. cts_change(info);
  1146. }
  1147. if (gis & (BIT3 + BIT2))
  1148. {
  1149. isr = read_reg16(info, CHA + ISR);
  1150. if (isr & IRQ_TIMER) {
  1151. info->irq_occurred = 1;
  1152. irq_disable(info, CHA, IRQ_TIMER);
  1153. }
  1154. /* receive IRQs */
  1155. if (isr & IRQ_EXITHUNT) {
  1156. info->icount.exithunt++;
  1157. wake_up_interruptible(&info->event_wait_q);
  1158. }
  1159. if (isr & IRQ_BREAK_ON) {
  1160. info->icount.brk++;
  1161. if (info->flags & ASYNC_SAK)
  1162. do_SAK(info->tty);
  1163. }
  1164. if (isr & IRQ_RXTIME) {
  1165. issue_command(info, CHA, CMD_RXFIFO_READ);
  1166. }
  1167. if (isr & (IRQ_RXEOM + IRQ_RXFIFO)) {
  1168. if (info->params.mode == MGSL_MODE_HDLC)
  1169. rx_ready_hdlc(info, isr & IRQ_RXEOM);
  1170. else
  1171. rx_ready_async(info, isr & IRQ_RXEOM);
  1172. }
  1173. /* transmit IRQs */
  1174. if (isr & IRQ_UNDERRUN) {
  1175. if (info->tx_aborting)
  1176. info->icount.txabort++;
  1177. else
  1178. info->icount.txunder++;
  1179. tx_done(info);
  1180. }
  1181. else if (isr & IRQ_ALLSENT) {
  1182. info->icount.txok++;
  1183. tx_done(info);
  1184. }
  1185. else if (isr & IRQ_TXFIFO)
  1186. tx_ready(info);
  1187. }
  1188. if (gis & BIT7) {
  1189. pis = read_reg(info, CHA + PIS);
  1190. if (pis & BIT1)
  1191. dsr_change(info);
  1192. if (pis & BIT2)
  1193. ri_change(info);
  1194. }
  1195. }
  1196. /* Request bottom half processing if there's something
  1197. * for it to do and the bh is not already running
  1198. */
  1199. if (info->pending_bh && !info->bh_running && !info->bh_requested) {
  1200. if ( debug_level >= DEBUG_LEVEL_ISR )
  1201. printk("%s(%d):%s queueing bh task.\n",
  1202. __FILE__,__LINE__,info->device_name);
  1203. schedule_work(&info->task);
  1204. info->bh_requested = 1;
  1205. }
  1206. spin_unlock(&info->lock);
  1207. if (debug_level >= DEBUG_LEVEL_ISR)
  1208. printk("%s(%d):mgslpc_isr(%d)exit.\n",
  1209. __FILE__,__LINE__,irq);
  1210. return IRQ_HANDLED;
  1211. }
  1212. /* Initialize and start device.
  1213. */
  1214. static int startup(MGSLPC_INFO * info)
  1215. {
  1216. int retval = 0;
  1217. if (debug_level >= DEBUG_LEVEL_INFO)
  1218. printk("%s(%d):startup(%s)\n",__FILE__,__LINE__,info->device_name);
  1219. if (info->flags & ASYNC_INITIALIZED)
  1220. return 0;
  1221. if (!info->tx_buf) {
  1222. /* allocate a page of memory for a transmit buffer */
  1223. info->tx_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
  1224. if (!info->tx_buf) {
  1225. printk(KERN_ERR"%s(%d):%s can't allocate transmit buffer\n",
  1226. __FILE__,__LINE__,info->device_name);
  1227. return -ENOMEM;
  1228. }
  1229. }
  1230. info->pending_bh = 0;
  1231. init_timer(&info->tx_timer);
  1232. info->tx_timer.data = (unsigned long)info;
  1233. info->tx_timer.function = tx_timeout;
  1234. /* Allocate and claim adapter resources */
  1235. retval = claim_resources(info);
  1236. /* perform existance check and diagnostics */
  1237. if ( !retval )
  1238. retval = adapter_test(info);
  1239. if ( retval ) {
  1240. if (capable(CAP_SYS_ADMIN) && info->tty)
  1241. set_bit(TTY_IO_ERROR, &info->tty->flags);
  1242. release_resources(info);
  1243. return retval;
  1244. }
  1245. /* program hardware for current parameters */
  1246. mgslpc_change_params(info);
  1247. if (info->tty)
  1248. clear_bit(TTY_IO_ERROR, &info->tty->flags);
  1249. info->flags |= ASYNC_INITIALIZED;
  1250. return 0;
  1251. }
  1252. /* Called by mgslpc_close() and mgslpc_hangup() to shutdown hardware
  1253. */
  1254. static void shutdown(MGSLPC_INFO * info)
  1255. {
  1256. unsigned long flags;
  1257. if (!(info->flags & ASYNC_INITIALIZED))
  1258. return;
  1259. if (debug_level >= DEBUG_LEVEL_INFO)
  1260. printk("%s(%d):mgslpc_shutdown(%s)\n",
  1261. __FILE__,__LINE__, info->device_name );
  1262. /* clear status wait queue because status changes */
  1263. /* can't happen after shutting down the hardware */
  1264. wake_up_interruptible(&info->status_event_wait_q);
  1265. wake_up_interruptible(&info->event_wait_q);
  1266. del_timer(&info->tx_timer);
  1267. if (info->tx_buf) {
  1268. free_page((unsigned long) info->tx_buf);
  1269. info->tx_buf = NULL;
  1270. }
  1271. spin_lock_irqsave(&info->lock,flags);
  1272. rx_stop(info);
  1273. tx_stop(info);
  1274. /* TODO:disable interrupts instead of reset to preserve signal states */
  1275. reset_device(info);
  1276. if (!info->tty || info->tty->termios->c_cflag & HUPCL) {
  1277. info->serial_signals &= ~(SerialSignal_DTR + SerialSignal_RTS);
  1278. set_signals(info);
  1279. }
  1280. spin_unlock_irqrestore(&info->lock,flags);
  1281. release_resources(info);
  1282. if (info->tty)
  1283. set_bit(TTY_IO_ERROR, &info->tty->flags);
  1284. info->flags &= ~ASYNC_INITIALIZED;
  1285. }
  1286. static void mgslpc_program_hw(MGSLPC_INFO *info)
  1287. {
  1288. unsigned long flags;
  1289. spin_lock_irqsave(&info->lock,flags);
  1290. rx_stop(info);
  1291. tx_stop(info);
  1292. info->tx_count = info->tx_put = info->tx_get = 0;
  1293. if (info->params.mode == MGSL_MODE_HDLC || info->netcount)
  1294. hdlc_mode(info);
  1295. else
  1296. async_mode(info);
  1297. set_signals(info);
  1298. info->dcd_chkcount = 0;
  1299. info->cts_chkcount = 0;
  1300. info->ri_chkcount = 0;
  1301. info->dsr_chkcount = 0;
  1302. irq_enable(info, CHB, IRQ_DCD | IRQ_CTS);
  1303. port_irq_enable(info, (unsigned char) PVR_DSR | PVR_RI);
  1304. get_signals(info);
  1305. if (info->netcount || info->tty->termios->c_cflag & CREAD)
  1306. rx_start(info);
  1307. spin_unlock_irqrestore(&info->lock,flags);
  1308. }
  1309. /* Reconfigure adapter based on new parameters
  1310. */
  1311. static void mgslpc_change_params(MGSLPC_INFO *info)
  1312. {
  1313. unsigned cflag;
  1314. int bits_per_char;
  1315. if (!info->tty || !info->tty->termios)
  1316. return;
  1317. if (debug_level >= DEBUG_LEVEL_INFO)
  1318. printk("%s(%d):mgslpc_change_params(%s)\n",
  1319. __FILE__,__LINE__, info->device_name );
  1320. cflag = info->tty->termios->c_cflag;
  1321. /* if B0 rate (hangup) specified then negate DTR and RTS */
  1322. /* otherwise assert DTR and RTS */
  1323. if (cflag & CBAUD)
  1324. info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
  1325. else
  1326. info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
  1327. /* byte size and parity */
  1328. switch (cflag & CSIZE) {
  1329. case CS5: info->params.data_bits = 5; break;
  1330. case CS6: info->params.data_bits = 6; break;
  1331. case CS7: info->params.data_bits = 7; break;
  1332. case CS8: info->params.data_bits = 8; break;
  1333. default: info->params.data_bits = 7; break;
  1334. }
  1335. if (cflag & CSTOPB)
  1336. info->params.stop_bits = 2;
  1337. else
  1338. info->params.stop_bits = 1;
  1339. info->params.parity = ASYNC_PARITY_NONE;
  1340. if (cflag & PARENB) {
  1341. if (cflag & PARODD)
  1342. info->params.parity = ASYNC_PARITY_ODD;
  1343. else
  1344. info->params.parity = ASYNC_PARITY_EVEN;
  1345. #ifdef CMSPAR
  1346. if (cflag & CMSPAR)
  1347. info->params.parity = ASYNC_PARITY_SPACE;
  1348. #endif
  1349. }
  1350. /* calculate number of jiffies to transmit a full
  1351. * FIFO (32 bytes) at specified data rate
  1352. */
  1353. bits_per_char = info->params.data_bits +
  1354. info->params.stop_bits + 1;
  1355. /* if port data rate is set to 460800 or less then
  1356. * allow tty settings to override, otherwise keep the
  1357. * current data rate.
  1358. */
  1359. if (info->params.data_rate <= 460800) {
  1360. info->params.data_rate = tty_get_baud_rate(info->tty);
  1361. }
  1362. if ( info->params.data_rate ) {
  1363. info->timeout = (32*HZ*bits_per_char) /
  1364. info->params.data_rate;
  1365. }
  1366. info->timeout += HZ/50; /* Add .02 seconds of slop */
  1367. if (cflag & CRTSCTS)
  1368. info->flags |= ASYNC_CTS_FLOW;
  1369. else
  1370. info->flags &= ~ASYNC_CTS_FLOW;
  1371. if (cflag & CLOCAL)
  1372. info->flags &= ~ASYNC_CHECK_CD;
  1373. else
  1374. info->flags |= ASYNC_CHECK_CD;
  1375. /* process tty input control flags */
  1376. info->read_status_mask = 0;
  1377. if (I_INPCK(info->tty))
  1378. info->read_status_mask |= BIT7 | BIT6;
  1379. if (I_IGNPAR(info->tty))
  1380. info->ignore_status_mask |= BIT7 | BIT6;
  1381. mgslpc_program_hw(info);
  1382. }
  1383. /* Add a character to the transmit buffer
  1384. */
  1385. static void mgslpc_put_char(struct tty_struct *tty, unsigned char ch)
  1386. {
  1387. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1388. unsigned long flags;
  1389. if (debug_level >= DEBUG_LEVEL_INFO) {
  1390. printk( "%s(%d):mgslpc_put_char(%d) on %s\n",
  1391. __FILE__,__LINE__,ch,info->device_name);
  1392. }
  1393. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_put_char"))
  1394. return;
  1395. if (!tty || !info->tx_buf)
  1396. return;
  1397. spin_lock_irqsave(&info->lock,flags);
  1398. if (info->params.mode == MGSL_MODE_ASYNC || !info->tx_active) {
  1399. if (info->tx_count < TXBUFSIZE - 1) {
  1400. info->tx_buf[info->tx_put++] = ch;
  1401. info->tx_put &= TXBUFSIZE-1;
  1402. info->tx_count++;
  1403. }
  1404. }
  1405. spin_unlock_irqrestore(&info->lock,flags);
  1406. }
  1407. /* Enable transmitter so remaining characters in the
  1408. * transmit buffer are sent.
  1409. */
  1410. static void mgslpc_flush_chars(struct tty_struct *tty)
  1411. {
  1412. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1413. unsigned long flags;
  1414. if (debug_level >= DEBUG_LEVEL_INFO)
  1415. printk( "%s(%d):mgslpc_flush_chars() entry on %s tx_count=%d\n",
  1416. __FILE__,__LINE__,info->device_name,info->tx_count);
  1417. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_chars"))
  1418. return;
  1419. if (info->tx_count <= 0 || tty->stopped ||
  1420. tty->hw_stopped || !info->tx_buf)
  1421. return;
  1422. if (debug_level >= DEBUG_LEVEL_INFO)
  1423. printk( "%s(%d):mgslpc_flush_chars() entry on %s starting transmitter\n",
  1424. __FILE__,__LINE__,info->device_name);
  1425. spin_lock_irqsave(&info->lock,flags);
  1426. if (!info->tx_active)
  1427. tx_start(info);
  1428. spin_unlock_irqrestore(&info->lock,flags);
  1429. }
  1430. /* Send a block of data
  1431. *
  1432. * Arguments:
  1433. *
  1434. * tty pointer to tty information structure
  1435. * buf pointer to buffer containing send data
  1436. * count size of send data in bytes
  1437. *
  1438. * Returns: number of characters written
  1439. */
  1440. static int mgslpc_write(struct tty_struct * tty,
  1441. const unsigned char *buf, int count)
  1442. {
  1443. int c, ret = 0;
  1444. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1445. unsigned long flags;
  1446. if (debug_level >= DEBUG_LEVEL_INFO)
  1447. printk( "%s(%d):mgslpc_write(%s) count=%d\n",
  1448. __FILE__,__LINE__,info->device_name,count);
  1449. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write") ||
  1450. !tty || !info->tx_buf)
  1451. goto cleanup;
  1452. if (info->params.mode == MGSL_MODE_HDLC) {
  1453. if (count > TXBUFSIZE) {
  1454. ret = -EIO;
  1455. goto cleanup;
  1456. }
  1457. if (info->tx_active)
  1458. goto cleanup;
  1459. else if (info->tx_count)
  1460. goto start;
  1461. }
  1462. for (;;) {
  1463. c = min(count,
  1464. min(TXBUFSIZE - info->tx_count - 1,
  1465. TXBUFSIZE - info->tx_put));
  1466. if (c <= 0)
  1467. break;
  1468. memcpy(info->tx_buf + info->tx_put, buf, c);
  1469. spin_lock_irqsave(&info->lock,flags);
  1470. info->tx_put = (info->tx_put + c) & (TXBUFSIZE-1);
  1471. info->tx_count += c;
  1472. spin_unlock_irqrestore(&info->lock,flags);
  1473. buf += c;
  1474. count -= c;
  1475. ret += c;
  1476. }
  1477. start:
  1478. if (info->tx_count && !tty->stopped && !tty->hw_stopped) {
  1479. spin_lock_irqsave(&info->lock,flags);
  1480. if (!info->tx_active)
  1481. tx_start(info);
  1482. spin_unlock_irqrestore(&info->lock,flags);
  1483. }
  1484. cleanup:
  1485. if (debug_level >= DEBUG_LEVEL_INFO)
  1486. printk( "%s(%d):mgslpc_write(%s) returning=%d\n",
  1487. __FILE__,__LINE__,info->device_name,ret);
  1488. return ret;
  1489. }
  1490. /* Return the count of free bytes in transmit buffer
  1491. */
  1492. static int mgslpc_write_room(struct tty_struct *tty)
  1493. {
  1494. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1495. int ret;
  1496. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write_room"))
  1497. return 0;
  1498. if (info->params.mode == MGSL_MODE_HDLC) {
  1499. /* HDLC (frame oriented) mode */
  1500. if (info->tx_active)
  1501. return 0;
  1502. else
  1503. return HDLC_MAX_FRAME_SIZE;
  1504. } else {
  1505. ret = TXBUFSIZE - info->tx_count - 1;
  1506. if (ret < 0)
  1507. ret = 0;
  1508. }
  1509. if (debug_level >= DEBUG_LEVEL_INFO)
  1510. printk("%s(%d):mgslpc_write_room(%s)=%d\n",
  1511. __FILE__,__LINE__, info->device_name, ret);
  1512. return ret;
  1513. }
  1514. /* Return the count of bytes in transmit buffer
  1515. */
  1516. static int mgslpc_chars_in_buffer(struct tty_struct *tty)
  1517. {
  1518. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1519. int rc;
  1520. if (debug_level >= DEBUG_LEVEL_INFO)
  1521. printk("%s(%d):mgslpc_chars_in_buffer(%s)\n",
  1522. __FILE__,__LINE__, info->device_name );
  1523. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_chars_in_buffer"))
  1524. return 0;
  1525. if (info->params.mode == MGSL_MODE_HDLC)
  1526. rc = info->tx_active ? info->max_frame_size : 0;
  1527. else
  1528. rc = info->tx_count;
  1529. if (debug_level >= DEBUG_LEVEL_INFO)
  1530. printk("%s(%d):mgslpc_chars_in_buffer(%s)=%d\n",
  1531. __FILE__,__LINE__, info->device_name, rc);
  1532. return rc;
  1533. }
  1534. /* Discard all data in the send buffer
  1535. */
  1536. static void mgslpc_flush_buffer(struct tty_struct *tty)
  1537. {
  1538. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1539. unsigned long flags;
  1540. if (debug_level >= DEBUG_LEVEL_INFO)
  1541. printk("%s(%d):mgslpc_flush_buffer(%s) entry\n",
  1542. __FILE__,__LINE__, info->device_name );
  1543. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_buffer"))
  1544. return;
  1545. spin_lock_irqsave(&info->lock,flags);
  1546. info->tx_count = info->tx_put = info->tx_get = 0;
  1547. del_timer(&info->tx_timer);
  1548. spin_unlock_irqrestore(&info->lock,flags);
  1549. wake_up_interruptible(&tty->write_wait);
  1550. tty_wakeup(tty);
  1551. }
  1552. /* Send a high-priority XON/XOFF character
  1553. */
  1554. static void mgslpc_send_xchar(struct tty_struct *tty, char ch)
  1555. {
  1556. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1557. unsigned long flags;
  1558. if (debug_level >= DEBUG_LEVEL_INFO)
  1559. printk("%s(%d):mgslpc_send_xchar(%s,%d)\n",
  1560. __FILE__,__LINE__, info->device_name, ch );
  1561. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_send_xchar"))
  1562. return;
  1563. info->x_char = ch;
  1564. if (ch) {
  1565. spin_lock_irqsave(&info->lock,flags);
  1566. if (!info->tx_enabled)
  1567. tx_start(info);
  1568. spin_unlock_irqrestore(&info->lock,flags);
  1569. }
  1570. }
  1571. /* Signal remote device to throttle send data (our receive data)
  1572. */
  1573. static void mgslpc_throttle(struct tty_struct * tty)
  1574. {
  1575. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1576. unsigned long flags;
  1577. if (debug_level >= DEBUG_LEVEL_INFO)
  1578. printk("%s(%d):mgslpc_throttle(%s) entry\n",
  1579. __FILE__,__LINE__, info->device_name );
  1580. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_throttle"))
  1581. return;
  1582. if (I_IXOFF(tty))
  1583. mgslpc_send_xchar(tty, STOP_CHAR(tty));
  1584. if (tty->termios->c_cflag & CRTSCTS) {
  1585. spin_lock_irqsave(&info->lock,flags);
  1586. info->serial_signals &= ~SerialSignal_RTS;
  1587. set_signals(info);
  1588. spin_unlock_irqrestore(&info->lock,flags);
  1589. }
  1590. }
  1591. /* Signal remote device to stop throttling send data (our receive data)
  1592. */
  1593. static void mgslpc_unthrottle(struct tty_struct * tty)
  1594. {
  1595. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1596. unsigned long flags;
  1597. if (debug_level >= DEBUG_LEVEL_INFO)
  1598. printk("%s(%d):mgslpc_unthrottle(%s) entry\n",
  1599. __FILE__,__LINE__, info->device_name );
  1600. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_unthrottle"))
  1601. return;
  1602. if (I_IXOFF(tty)) {
  1603. if (info->x_char)
  1604. info->x_char = 0;
  1605. else
  1606. mgslpc_send_xchar(tty, START_CHAR(tty));
  1607. }
  1608. if (tty->termios->c_cflag & CRTSCTS) {
  1609. spin_lock_irqsave(&info->lock,flags);
  1610. info->serial_signals |= SerialSignal_RTS;
  1611. set_signals(info);
  1612. spin_unlock_irqrestore(&info->lock,flags);
  1613. }
  1614. }
  1615. /* get the current serial statistics
  1616. */
  1617. static int get_stats(MGSLPC_INFO * info, struct mgsl_icount __user *user_icount)
  1618. {
  1619. int err;
  1620. if (debug_level >= DEBUG_LEVEL_INFO)
  1621. printk("get_params(%s)\n", info->device_name);
  1622. COPY_TO_USER(err,user_icount, &info->icount, sizeof(struct mgsl_icount));
  1623. if (err)
  1624. return -EFAULT;
  1625. return 0;
  1626. }
  1627. /* get the current serial parameters
  1628. */
  1629. static int get_params(MGSLPC_INFO * info, MGSL_PARAMS __user *user_params)
  1630. {
  1631. int err;
  1632. if (debug_level >= DEBUG_LEVEL_INFO)
  1633. printk("get_params(%s)\n", info->device_name);
  1634. COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS));
  1635. if (err)
  1636. return -EFAULT;
  1637. return 0;
  1638. }
  1639. /* set the serial parameters
  1640. *
  1641. * Arguments:
  1642. *
  1643. * info pointer to device instance data
  1644. * new_params user buffer containing new serial params
  1645. *
  1646. * Returns: 0 if success, otherwise error code
  1647. */
  1648. static int set_params(MGSLPC_INFO * info, MGSL_PARAMS __user *new_params)
  1649. {
  1650. unsigned long flags;
  1651. MGSL_PARAMS tmp_params;
  1652. int err;
  1653. if (debug_level >= DEBUG_LEVEL_INFO)
  1654. printk("%s(%d):set_params %s\n", __FILE__,__LINE__,
  1655. info->device_name );
  1656. COPY_FROM_USER(err,&tmp_params, new_params, sizeof(MGSL_PARAMS));
  1657. if (err) {
  1658. if ( debug_level >= DEBUG_LEVEL_INFO )
  1659. printk( "%s(%d):set_params(%s) user buffer copy failed\n",
  1660. __FILE__,__LINE__,info->device_name);
  1661. return -EFAULT;
  1662. }
  1663. spin_lock_irqsave(&info->lock,flags);
  1664. memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
  1665. spin_unlock_irqrestore(&info->lock,flags);
  1666. mgslpc_change_params(info);
  1667. return 0;
  1668. }
  1669. static int get_txidle(MGSLPC_INFO * info, int __user *idle_mode)
  1670. {
  1671. int err;
  1672. if (debug_level >= DEBUG_LEVEL_INFO)
  1673. printk("get_txidle(%s)=%d\n", info->device_name, info->idle_mode);
  1674. COPY_TO_USER(err,idle_mode, &info->idle_mode, sizeof(int));
  1675. if (err)
  1676. return -EFAULT;
  1677. return 0;
  1678. }
  1679. static int set_txidle(MGSLPC_INFO * info, int idle_mode)
  1680. {
  1681. unsigned long flags;
  1682. if (debug_level >= DEBUG_LEVEL_INFO)
  1683. printk("set_txidle(%s,%d)\n", info->device_name, idle_mode);
  1684. spin_lock_irqsave(&info->lock,flags);
  1685. info->idle_mode = idle_mode;
  1686. tx_set_idle(info);
  1687. spin_unlock_irqrestore(&info->lock,flags);
  1688. return 0;
  1689. }
  1690. static int get_interface(MGSLPC_INFO * info, int __user *if_mode)
  1691. {
  1692. int err;
  1693. if (debug_level >= DEBUG_LEVEL_INFO)
  1694. printk("get_interface(%s)=%d\n", info->device_name, info->if_mode);
  1695. COPY_TO_USER(err,if_mode, &info->if_mode, sizeof(int));
  1696. if (err)
  1697. return -EFAULT;
  1698. return 0;
  1699. }
  1700. static int set_interface(MGSLPC_INFO * info, int if_mode)
  1701. {
  1702. unsigned long flags;
  1703. unsigned char val;
  1704. if (debug_level >= DEBUG_LEVEL_INFO)
  1705. printk("set_interface(%s,%d)\n", info->device_name, if_mode);
  1706. spin_lock_irqsave(&info->lock,flags);
  1707. info->if_mode = if_mode;
  1708. val = read_reg(info, PVR) & 0x0f;
  1709. switch (info->if_mode)
  1710. {
  1711. case MGSL_INTERFACE_RS232: val |= PVR_RS232; break;
  1712. case MGSL_INTERFACE_V35: val |= PVR_V35; break;
  1713. case MGSL_INTERFACE_RS422: val |= PVR_RS422; break;
  1714. }
  1715. write_reg(info, PVR, val);
  1716. spin_unlock_irqrestore(&info->lock,flags);
  1717. return 0;
  1718. }
  1719. static int set_txenable(MGSLPC_INFO * info, int enable)
  1720. {
  1721. unsigned long flags;
  1722. if (debug_level >= DEBUG_LEVEL_INFO)
  1723. printk("set_txenable(%s,%d)\n", info->device_name, enable);
  1724. spin_lock_irqsave(&info->lock,flags);
  1725. if (enable) {
  1726. if (!info->tx_enabled)
  1727. tx_start(info);
  1728. } else {
  1729. if (info->tx_enabled)
  1730. tx_stop(info);
  1731. }
  1732. spin_unlock_irqrestore(&info->lock,flags);
  1733. return 0;
  1734. }
  1735. static int tx_abort(MGSLPC_INFO * info)
  1736. {
  1737. unsigned long flags;
  1738. if (debug_level >= DEBUG_LEVEL_INFO)
  1739. printk("tx_abort(%s)\n", info->device_name);
  1740. spin_lock_irqsave(&info->lock,flags);
  1741. if (info->tx_active && info->tx_count &&
  1742. info->params.mode == MGSL_MODE_HDLC) {
  1743. /* clear data count so FIFO is not filled on next IRQ.
  1744. * This results in underrun and abort transmission.
  1745. */
  1746. info->tx_count = info->tx_put = info->tx_get = 0;
  1747. info->tx_aborting = TRUE;
  1748. }
  1749. spin_unlock_irqrestore(&info->lock,flags);
  1750. return 0;
  1751. }
  1752. static int set_rxenable(MGSLPC_INFO * info, int enable)
  1753. {
  1754. unsigned long flags;
  1755. if (debug_level >= DEBUG_LEVEL_INFO)
  1756. printk("set_rxenable(%s,%d)\n", info->device_name, enable);
  1757. spin_lock_irqsave(&info->lock,flags);
  1758. if (enable) {
  1759. if (!info->rx_enabled)
  1760. rx_start(info);
  1761. } else {
  1762. if (info->rx_enabled)
  1763. rx_stop(info);
  1764. }
  1765. spin_unlock_irqrestore(&info->lock,flags);
  1766. return 0;
  1767. }
  1768. /* wait for specified event to occur
  1769. *
  1770. * Arguments: info pointer to device instance data
  1771. * mask pointer to bitmask of events to wait for
  1772. * Return Value: 0 if successful and bit mask updated with
  1773. * of events triggerred,
  1774. * otherwise error code
  1775. */
  1776. static int wait_events(MGSLPC_INFO * info, int __user *mask_ptr)
  1777. {
  1778. unsigned long flags;
  1779. int s;
  1780. int rc=0;
  1781. struct mgsl_icount cprev, cnow;
  1782. int events;
  1783. int mask;
  1784. struct _input_signal_events oldsigs, newsigs;
  1785. DECLARE_WAITQUEUE(wait, current);
  1786. COPY_FROM_USER(rc,&mask, mask_ptr, sizeof(int));
  1787. if (rc)
  1788. return -EFAULT;
  1789. if (debug_level >= DEBUG_LEVEL_INFO)
  1790. printk("wait_events(%s,%d)\n", info->device_name, mask);
  1791. spin_lock_irqsave(&info->lock,flags);
  1792. /* return immediately if state matches requested events */
  1793. get_signals(info);
  1794. s = info->serial_signals;
  1795. events = mask &
  1796. ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
  1797. ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
  1798. ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
  1799. ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
  1800. if (events) {
  1801. spin_unlock_irqrestore(&info->lock,flags);
  1802. goto exit;
  1803. }
  1804. /* save current irq counts */
  1805. cprev = info->icount;
  1806. oldsigs = info->input_signal_events;
  1807. if ((info->params.mode == MGSL_MODE_HDLC) &&
  1808. (mask & MgslEvent_ExitHuntMode))
  1809. irq_enable(info, CHA, IRQ_EXITHUNT);
  1810. set_current_state(TASK_INTERRUPTIBLE);
  1811. add_wait_queue(&info->event_wait_q, &wait);
  1812. spin_unlock_irqrestore(&info->lock,flags);
  1813. for(;;) {
  1814. schedule();
  1815. if (signal_pending(current)) {
  1816. rc = -ERESTARTSYS;
  1817. break;
  1818. }
  1819. /* get current irq counts */
  1820. spin_lock_irqsave(&info->lock,flags);
  1821. cnow = info->icount;
  1822. newsigs = info->input_signal_events;
  1823. set_current_state(TASK_INTERRUPTIBLE);
  1824. spin_unlock_irqrestore(&info->lock,flags);
  1825. /* if no change, wait aborted for some reason */
  1826. if (newsigs.dsr_up == oldsigs.dsr_up &&
  1827. newsigs.dsr_down == oldsigs.dsr_down &&
  1828. newsigs.dcd_up == oldsigs.dcd_up &&
  1829. newsigs.dcd_down == oldsigs.dcd_down &&
  1830. newsigs.cts_up == oldsigs.cts_up &&
  1831. newsigs.cts_down == oldsigs.cts_down &&
  1832. newsigs.ri_up == oldsigs.ri_up &&
  1833. newsigs.ri_down == oldsigs.ri_down &&
  1834. cnow.exithunt == cprev.exithunt &&
  1835. cnow.rxidle == cprev.rxidle) {
  1836. rc = -EIO;
  1837. break;
  1838. }
  1839. events = mask &
  1840. ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
  1841. (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
  1842. (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
  1843. (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
  1844. (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
  1845. (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
  1846. (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
  1847. (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
  1848. (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
  1849. (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
  1850. if (events)
  1851. break;
  1852. cprev = cnow;
  1853. oldsigs = newsigs;
  1854. }
  1855. remove_wait_queue(&info->event_wait_q, &wait);
  1856. set_current_state(TASK_RUNNING);
  1857. if (mask & MgslEvent_ExitHuntMode) {
  1858. spin_lock_irqsave(&info->lock,flags);
  1859. if (!waitqueue_active(&info->event_wait_q))
  1860. irq_disable(info, CHA, IRQ_EXITHUNT);
  1861. spin_unlock_irqrestore(&info->lock,flags);
  1862. }
  1863. exit:
  1864. if (rc == 0)
  1865. PUT_USER(rc, events, mask_ptr);
  1866. return rc;
  1867. }
  1868. static int modem_input_wait(MGSLPC_INFO *info,int arg)
  1869. {
  1870. unsigned long flags;
  1871. int rc;
  1872. struct mgsl_icount cprev, cnow;
  1873. DECLARE_WAITQUEUE(wait, current);
  1874. /* save current irq counts */
  1875. spin_lock_irqsave(&info->lock,flags);
  1876. cprev = info->icount;
  1877. add_wait_queue(&info->status_event_wait_q, &wait);
  1878. set_current_state(TASK_INTERRUPTIBLE);
  1879. spin_unlock_irqrestore(&info->lock,flags);
  1880. for(;;) {
  1881. schedule();
  1882. if (signal_pending(current)) {
  1883. rc = -ERESTARTSYS;
  1884. break;
  1885. }
  1886. /* get new irq counts */
  1887. spin_lock_irqsave(&info->lock,flags);
  1888. cnow = info->icount;
  1889. set_current_state(TASK_INTERRUPTIBLE);
  1890. spin_unlock_irqrestore(&info->lock,flags);
  1891. /* if no change, wait aborted for some reason */
  1892. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  1893. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
  1894. rc = -EIO;
  1895. break;
  1896. }
  1897. /* check for change in caller specified modem input */
  1898. if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
  1899. (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
  1900. (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
  1901. (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
  1902. rc = 0;
  1903. break;
  1904. }
  1905. cprev = cnow;
  1906. }
  1907. remove_wait_queue(&info->status_event_wait_q, &wait);
  1908. set_current_state(TASK_RUNNING);
  1909. return rc;
  1910. }
  1911. /* return the state of the serial control and status signals
  1912. */
  1913. static int tiocmget(struct tty_struct *tty, struct file *file)
  1914. {
  1915. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1916. unsigned int result;
  1917. unsigned long flags;
  1918. spin_lock_irqsave(&info->lock,flags);
  1919. get_signals(info);
  1920. spin_unlock_irqrestore(&info->lock,flags);
  1921. result = ((info->serial_signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
  1922. ((info->serial_signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
  1923. ((info->serial_signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
  1924. ((info->serial_signals & SerialSignal_RI) ? TIOCM_RNG:0) +
  1925. ((info->serial_signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
  1926. ((info->serial_signals & SerialSignal_CTS) ? TIOCM_CTS:0);
  1927. if (debug_level >= DEBUG_LEVEL_INFO)
  1928. printk("%s(%d):%s tiocmget() value=%08X\n",
  1929. __FILE__,__LINE__, info->device_name, result );
  1930. return result;
  1931. }
  1932. /* set modem control signals (DTR/RTS)
  1933. */
  1934. static int tiocmset(struct tty_struct *tty, struct file *file,
  1935. unsigned int set, unsigned int clear)
  1936. {
  1937. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1938. unsigned long flags;
  1939. if (debug_level >= DEBUG_LEVEL_INFO)
  1940. printk("%s(%d):%s tiocmset(%x,%x)\n",
  1941. __FILE__,__LINE__,info->device_name, set, clear);
  1942. if (set & TIOCM_RTS)
  1943. info->serial_signals |= SerialSignal_RTS;
  1944. if (set & TIOCM_DTR)
  1945. info->serial_signals |= SerialSignal_DTR;
  1946. if (clear & TIOCM_RTS)
  1947. info->serial_signals &= ~SerialSignal_RTS;
  1948. if (clear & TIOCM_DTR)
  1949. info->serial_signals &= ~SerialSignal_DTR;
  1950. spin_lock_irqsave(&info->lock,flags);
  1951. set_signals(info);
  1952. spin_unlock_irqrestore(&info->lock,flags);
  1953. return 0;
  1954. }
  1955. /* Set or clear transmit break condition
  1956. *
  1957. * Arguments: tty pointer to tty instance data
  1958. * break_state -1=set break condition, 0=clear
  1959. */
  1960. static void mgslpc_break(struct tty_struct *tty, int break_state)
  1961. {
  1962. MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
  1963. unsigned long flags;
  1964. if (debug_level >= DEBUG_LEVEL_INFO)
  1965. printk("%s(%d):mgslpc_break(%s,%d)\n",
  1966. __FILE__,__LINE__, info->device_name, break_state);
  1967. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_break"))
  1968. return;
  1969. spin_lock_irqsave(&info->lock,flags);
  1970. if (break_state == -1)
  1971. set_reg_bits(info, CHA+DAFO, BIT6);
  1972. else
  1973. clear_reg_bits(info, CHA+DAFO, BIT6);
  1974. spin_unlock_irqrestore(&info->lock,flags);
  1975. }
  1976. /* Service an IOCTL request
  1977. *
  1978. * Arguments:
  1979. *
  1980. * tty pointer to tty instance data
  1981. * file pointer to associated file object for device
  1982. * cmd IOCTL command code
  1983. * arg command argument/context
  1984. *
  1985. * Return Value: 0 if success, otherwise error code
  1986. */
  1987. static int mgslpc_ioctl(struct tty_struct *tty, struct file * file,
  1988. unsigned int cmd, unsigned long arg)
  1989. {
  1990. MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
  1991. if (debug_level >= DEBUG_LEVEL_INFO)
  1992. printk("%s(%d):mgslpc_ioctl %s cmd=%08X\n", __FILE__,__LINE__,
  1993. info->device_name, cmd );
  1994. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_ioctl"))
  1995. return -ENODEV;
  1996. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  1997. (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
  1998. if (tty->flags & (1 << TTY_IO_ERROR))
  1999. return -EIO;
  2000. }
  2001. return ioctl_common(info, cmd, arg);
  2002. }
  2003. int ioctl_common(MGSLPC_INFO *info, unsigned int cmd, unsigned long arg)
  2004. {
  2005. int error;
  2006. struct mgsl_icount cnow; /* kernel counter temps */
  2007. struct serial_icounter_struct __user *p_cuser; /* user space */
  2008. void __user *argp = (void __user *)arg;
  2009. unsigned long flags;
  2010. switch (cmd) {
  2011. case MGSL_IOCGPARAMS:
  2012. return get_params(info, argp);
  2013. case MGSL_IOCSPARAMS:
  2014. return set_params(info, argp);
  2015. case MGSL_IOCGTXIDLE:
  2016. return get_txidle(info, argp);
  2017. case MGSL_IOCSTXIDLE:
  2018. return set_txidle(info, (int)arg);
  2019. case MGSL_IOCGIF:
  2020. return get_interface(info, argp);
  2021. case MGSL_IOCSIF:
  2022. return set_interface(info,(int)arg);
  2023. case MGSL_IOCTXENABLE:
  2024. return set_txenable(info,(int)arg);
  2025. case MGSL_IOCRXENABLE:
  2026. return set_rxenable(info,(int)arg);
  2027. case MGSL_IOCTXABORT:
  2028. return tx_abort(info);
  2029. case MGSL_IOCGSTATS:
  2030. return get_stats(info, argp);
  2031. case MGSL_IOCWAITEVENT:
  2032. return wait_events(info, argp);
  2033. case TIOCMIWAIT:
  2034. return modem_input_wait(info,(int)arg);
  2035. case TIOCGICOUNT:
  2036. spin_lock_irqsave(&info->lock,flags);
  2037. cnow = info->icount;
  2038. spin_unlock_irqrestore(&info->lock,flags);
  2039. p_cuser = argp;
  2040. PUT_USER(error,cnow.cts, &p_cuser->cts);
  2041. if (error) return error;
  2042. PUT_USER(error,cnow.dsr, &p_cuser->dsr);
  2043. if (error) return error;
  2044. PUT_USER(error,cnow.rng, &p_cuser->rng);
  2045. if (error) return error;
  2046. PUT_USER(error,cnow.dcd, &p_cuser->dcd);
  2047. if (error) return error;
  2048. PUT_USER(error,cnow.rx, &p_cuser->rx);
  2049. if (error) return error;
  2050. PUT_USER(error,cnow.tx, &p_cuser->tx);
  2051. if (error) return error;
  2052. PUT_USER(error,cnow.frame, &p_cuser->frame);
  2053. if (error) return error;
  2054. PUT_USER(error,cnow.overrun, &p_cuser->overrun);
  2055. if (error) return error;
  2056. PUT_USER(error,cnow.parity, &p_cuser->parity);
  2057. if (error) return error;
  2058. PUT_USER(error,cnow.brk, &p_cuser->brk);
  2059. if (error) return error;
  2060. PUT_USER(error,cnow.buf_overrun, &p_cuser->buf_overrun);
  2061. if (error) return error;
  2062. return 0;
  2063. default:
  2064. return -ENOIOCTLCMD;
  2065. }
  2066. return 0;
  2067. }
  2068. /* Set new termios settings
  2069. *
  2070. * Arguments:
  2071. *
  2072. * tty pointer to tty structure
  2073. * termios pointer to buffer to hold returned old termios
  2074. */
  2075. static void mgslpc_set_termios(struct tty_struct *tty, struct termios *old_termios)
  2076. {
  2077. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  2078. unsigned long flags;
  2079. if (debug_level >= DEBUG_LEVEL_INFO)
  2080. printk("%s(%d):mgslpc_set_termios %s\n", __FILE__,__LINE__,
  2081. tty->driver->name );
  2082. /* just return if nothing has changed */
  2083. if ((tty->termios->c_cflag == old_termios->c_cflag)
  2084. && (RELEVANT_IFLAG(tty->termios->c_iflag)
  2085. == RELEVANT_IFLAG(old_termios->c_iflag)))
  2086. return;
  2087. mgslpc_change_params(info);
  2088. /* Handle transition to B0 status */
  2089. if (old_termios->c_cflag & CBAUD &&
  2090. !(tty->termios->c_cflag & CBAUD)) {
  2091. info->serial_signals &= ~(SerialSignal_RTS + SerialSignal_DTR);
  2092. spin_lock_irqsave(&info->lock,flags);
  2093. set_signals(info);
  2094. spin_unlock_irqrestore(&info->lock,flags);
  2095. }
  2096. /* Handle transition away from B0 status */
  2097. if (!(old_termios->c_cflag & CBAUD) &&
  2098. tty->termios->c_cflag & CBAUD) {
  2099. info->serial_signals |= SerialSignal_DTR;
  2100. if (!(tty->termios->c_cflag & CRTSCTS) ||
  2101. !test_bit(TTY_THROTTLED, &tty->flags)) {
  2102. info->serial_signals |= SerialSignal_RTS;
  2103. }
  2104. spin_lock_irqsave(&info->lock,flags);
  2105. set_signals(info);
  2106. spin_unlock_irqrestore(&info->lock,flags);
  2107. }
  2108. /* Handle turning off CRTSCTS */
  2109. if (old_termios->c_cflag & CRTSCTS &&
  2110. !(tty->termios->c_cflag & CRTSCTS)) {
  2111. tty->hw_stopped = 0;
  2112. tx_release(tty);
  2113. }
  2114. }
  2115. static void mgslpc_close(struct tty_struct *tty, struct file * filp)
  2116. {
  2117. MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
  2118. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_close"))
  2119. return;
  2120. if (debug_level >= DEBUG_LEVEL_INFO)
  2121. printk("%s(%d):mgslpc_close(%s) entry, count=%d\n",
  2122. __FILE__,__LINE__, info->device_name, info->count);
  2123. if (!info->count)
  2124. return;
  2125. if (tty_hung_up_p(filp))
  2126. goto cleanup;
  2127. if ((tty->count == 1) && (info->count != 1)) {
  2128. /*
  2129. * tty->count is 1 and the tty structure will be freed.
  2130. * info->count should be one in this case.
  2131. * if it's not, correct it so that the port is shutdown.
  2132. */
  2133. printk("mgslpc_close: bad refcount; tty->count is 1, "
  2134. "info->count is %d\n", info->count);
  2135. info->count = 1;
  2136. }
  2137. info->count--;
  2138. /* if at least one open remaining, leave hardware active */
  2139. if (info->count)
  2140. goto cleanup;
  2141. info->flags |= ASYNC_CLOSING;
  2142. /* set tty->closing to notify line discipline to
  2143. * only process XON/XOFF characters. Only the N_TTY
  2144. * discipline appears to use this (ppp does not).
  2145. */
  2146. tty->closing = 1;
  2147. /* wait for transmit data to clear all layers */
  2148. if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE) {
  2149. if (debug_level >= DEBUG_LEVEL_INFO)
  2150. printk("%s(%d):mgslpc_close(%s) calling tty_wait_until_sent\n",
  2151. __FILE__,__LINE__, info->device_name );
  2152. tty_wait_until_sent(tty, info->closing_wait);
  2153. }
  2154. if (info->flags & ASYNC_INITIALIZED)
  2155. mgslpc_wait_until_sent(tty, info->timeout);
  2156. if (tty->driver->flush_buffer)
  2157. tty->driver->flush_buffer(tty);
  2158. ldisc_flush_buffer(tty);
  2159. shutdown(info);
  2160. tty->closing = 0;
  2161. info->tty = NULL;
  2162. if (info->blocked_open) {
  2163. if (info->close_delay) {
  2164. msleep_interruptible(jiffies_to_msecs(info->close_delay));
  2165. }
  2166. wake_up_interruptible(&info->open_wait);
  2167. }
  2168. info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
  2169. wake_up_interruptible(&info->close_wait);
  2170. cleanup:
  2171. if (debug_level >= DEBUG_LEVEL_INFO)
  2172. printk("%s(%d):mgslpc_close(%s) exit, count=%d\n", __FILE__,__LINE__,
  2173. tty->driver->name, info->count);
  2174. }
  2175. /* Wait until the transmitter is empty.
  2176. */
  2177. static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout)
  2178. {
  2179. MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
  2180. unsigned long orig_jiffies, char_time;
  2181. if (!info )
  2182. return;
  2183. if (debug_level >= DEBUG_LEVEL_INFO)
  2184. printk("%s(%d):mgslpc_wait_until_sent(%s) entry\n",
  2185. __FILE__,__LINE__, info->device_name );
  2186. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_wait_until_sent"))
  2187. return;
  2188. if (!(info->flags & ASYNC_INITIALIZED))
  2189. goto exit;
  2190. orig_jiffies = jiffies;
  2191. /* Set check interval to 1/5 of estimated time to
  2192. * send a character, and make it at least 1. The check
  2193. * interval should also be less than the timeout.
  2194. * Note: use tight timings here to satisfy the NIST-PCTS.
  2195. */
  2196. if ( info->params.data_rate ) {
  2197. char_time = info->timeout/(32 * 5);
  2198. if (!char_time)
  2199. char_time++;
  2200. } else
  2201. char_time = 1;
  2202. if (timeout)
  2203. char_time = min_t(unsigned long, char_time, timeout);
  2204. if (info->params.mode == MGSL_MODE_HDLC) {
  2205. while (info->tx_active) {
  2206. msleep_interruptible(jiffies_to_msecs(char_time));
  2207. if (signal_pending(current))
  2208. break;
  2209. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  2210. break;
  2211. }
  2212. } else {
  2213. while ((info->tx_count || info->tx_active) &&
  2214. info->tx_enabled) {
  2215. msleep_interruptible(jiffies_to_msecs(char_time));
  2216. if (signal_pending(current))
  2217. break;
  2218. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  2219. break;
  2220. }
  2221. }
  2222. exit:
  2223. if (debug_level >= DEBUG_LEVEL_INFO)
  2224. printk("%s(%d):mgslpc_wait_until_sent(%s) exit\n",
  2225. __FILE__,__LINE__, info->device_name );
  2226. }
  2227. /* Called by tty_hangup() when a hangup is signaled.
  2228. * This is the same as closing all open files for the port.
  2229. */
  2230. static void mgslpc_hangup(struct tty_struct *tty)
  2231. {
  2232. MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
  2233. if (debug_level >= DEBUG_LEVEL_INFO)
  2234. printk("%s(%d):mgslpc_hangup(%s)\n",
  2235. __FILE__,__LINE__, info->device_name );
  2236. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_hangup"))
  2237. return;
  2238. mgslpc_flush_buffer(tty);
  2239. shutdown(info);
  2240. info->count = 0;
  2241. info->flags &= ~ASYNC_NORMAL_ACTIVE;
  2242. info->tty = NULL;
  2243. wake_up_interruptible(&info->open_wait);
  2244. }
  2245. /* Block the current process until the specified port
  2246. * is ready to be opened.
  2247. */
  2248. static int block_til_ready(struct tty_struct *tty, struct file *filp,
  2249. MGSLPC_INFO *info)
  2250. {
  2251. DECLARE_WAITQUEUE(wait, current);
  2252. int retval;
  2253. int do_clocal = 0, extra_count = 0;
  2254. unsigned long flags;
  2255. if (debug_level >= DEBUG_LEVEL_INFO)
  2256. printk("%s(%d):block_til_ready on %s\n",
  2257. __FILE__,__LINE__, tty->driver->name );
  2258. if (filp->f_flags & O_NONBLOCK || tty->flags & (1 << TTY_IO_ERROR)){
  2259. /* nonblock mode is set or port is not enabled */
  2260. /* just verify that callout device is not active */
  2261. info->flags |= ASYNC_NORMAL_ACTIVE;
  2262. return 0;
  2263. }
  2264. if (tty->termios->c_cflag & CLOCAL)
  2265. do_clocal = 1;
  2266. /* Wait for carrier detect and the line to become
  2267. * free (i.e., not in use by the callout). While we are in
  2268. * this loop, info->count is dropped by one, so that
  2269. * mgslpc_close() knows when to free things. We restore it upon
  2270. * exit, either normal or abnormal.
  2271. */
  2272. retval = 0;
  2273. add_wait_queue(&info->open_wait, &wait);
  2274. if (debug_level >= DEBUG_LEVEL_INFO)
  2275. printk("%s(%d):block_til_ready before block on %s count=%d\n",
  2276. __FILE__,__LINE__, tty->driver->name, info->count );
  2277. spin_lock_irqsave(&info->lock, flags);
  2278. if (!tty_hung_up_p(filp)) {
  2279. extra_count = 1;
  2280. info->count--;
  2281. }
  2282. spin_unlock_irqrestore(&info->lock, flags);
  2283. info->blocked_open++;
  2284. while (1) {
  2285. if ((tty->termios->c_cflag & CBAUD)) {
  2286. spin_lock_irqsave(&info->lock,flags);
  2287. info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
  2288. set_signals(info);
  2289. spin_unlock_irqrestore(&info->lock,flags);
  2290. }
  2291. set_current_state(TASK_INTERRUPTIBLE);
  2292. if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)){
  2293. retval = (info->flags & ASYNC_HUP_NOTIFY) ?
  2294. -EAGAIN : -ERESTARTSYS;
  2295. break;
  2296. }
  2297. spin_lock_irqsave(&info->lock,flags);
  2298. get_signals(info);
  2299. spin_unlock_irqrestore(&info->lock,flags);
  2300. if (!(info->flags & ASYNC_CLOSING) &&
  2301. (do_clocal || (info->serial_signals & SerialSignal_DCD)) ) {
  2302. break;
  2303. }
  2304. if (signal_pending(current)) {
  2305. retval = -ERESTARTSYS;
  2306. break;
  2307. }
  2308. if (debug_level >= DEBUG_LEVEL_INFO)
  2309. printk("%s(%d):block_til_ready blocking on %s count=%d\n",
  2310. __FILE__,__LINE__, tty->driver->name, info->count );
  2311. schedule();
  2312. }
  2313. set_current_state(TASK_RUNNING);
  2314. remove_wait_queue(&info->open_wait, &wait);
  2315. if (extra_count)
  2316. info->count++;
  2317. info->blocked_open--;
  2318. if (debug_level >= DEBUG_LEVEL_INFO)
  2319. printk("%s(%d):block_til_ready after blocking on %s count=%d\n",
  2320. __FILE__,__LINE__, tty->driver->name, info->count );
  2321. if (!retval)
  2322. info->flags |= ASYNC_NORMAL_ACTIVE;
  2323. return retval;
  2324. }
  2325. static int mgslpc_open(struct tty_struct *tty, struct file * filp)
  2326. {
  2327. MGSLPC_INFO *info;
  2328. int retval, line;
  2329. unsigned long flags;
  2330. /* verify range of specified line number */
  2331. line = tty->index;
  2332. if ((line < 0) || (line >= mgslpc_device_count)) {
  2333. printk("%s(%d):mgslpc_open with invalid line #%d.\n",
  2334. __FILE__,__LINE__,line);
  2335. return -ENODEV;
  2336. }
  2337. /* find the info structure for the specified line */
  2338. info = mgslpc_device_list;
  2339. while(info && info->line != line)
  2340. info = info->next_device;
  2341. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_open"))
  2342. return -ENODEV;
  2343. tty->driver_data = info;
  2344. info->tty = tty;
  2345. if (debug_level >= DEBUG_LEVEL_INFO)
  2346. printk("%s(%d):mgslpc_open(%s), old ref count = %d\n",
  2347. __FILE__,__LINE__,tty->driver->name, info->count);
  2348. /* If port is closing, signal caller to try again */
  2349. if (tty_hung_up_p(filp) || info->flags & ASYNC_CLOSING){
  2350. if (info->flags & ASYNC_CLOSING)
  2351. interruptible_sleep_on(&info->close_wait);
  2352. retval = ((info->flags & ASYNC_HUP_NOTIFY) ?
  2353. -EAGAIN : -ERESTARTSYS);
  2354. goto cleanup;
  2355. }
  2356. info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  2357. spin_lock_irqsave(&info->netlock, flags);
  2358. if (info->netcount) {
  2359. retval = -EBUSY;
  2360. spin_unlock_irqrestore(&info->netlock, flags);
  2361. goto cleanup;
  2362. }
  2363. info->count++;
  2364. spin_unlock_irqrestore(&info->netlock, flags);
  2365. if (info->count == 1) {
  2366. /* 1st open on this device, init hardware */
  2367. retval = startup(info);
  2368. if (retval < 0)
  2369. goto cleanup;
  2370. }
  2371. retval = block_til_ready(tty, filp, info);
  2372. if (retval) {
  2373. if (debug_level >= DEBUG_LEVEL_INFO)
  2374. printk("%s(%d):block_til_ready(%s) returned %d\n",
  2375. __FILE__,__LINE__, info->device_name, retval);
  2376. goto cleanup;
  2377. }
  2378. if (debug_level >= DEBUG_LEVEL_INFO)
  2379. printk("%s(%d):mgslpc_open(%s) success\n",
  2380. __FILE__,__LINE__, info->device_name);
  2381. retval = 0;
  2382. cleanup:
  2383. if (retval) {
  2384. if (tty->count == 1)
  2385. info->tty = NULL; /* tty layer will release tty struct */
  2386. if(info->count)
  2387. info->count--;
  2388. }
  2389. return retval;
  2390. }
  2391. /*
  2392. * /proc fs routines....
  2393. */
  2394. static inline int line_info(char *buf, MGSLPC_INFO *info)
  2395. {
  2396. char stat_buf[30];
  2397. int ret;
  2398. unsigned long flags;
  2399. ret = sprintf(buf, "%s:io:%04X irq:%d",
  2400. info->device_name, info->io_base, info->irq_level);
  2401. /* output current serial signal states */
  2402. spin_lock_irqsave(&info->lock,flags);
  2403. get_signals(info);
  2404. spin_unlock_irqrestore(&info->lock,flags);
  2405. stat_buf[0] = 0;
  2406. stat_buf[1] = 0;
  2407. if (info->serial_signals & SerialSignal_RTS)
  2408. strcat(stat_buf, "|RTS");
  2409. if (info->serial_signals & SerialSignal_CTS)
  2410. strcat(stat_buf, "|CTS");
  2411. if (info->serial_signals & SerialSignal_DTR)
  2412. strcat(stat_buf, "|DTR");
  2413. if (info->serial_signals & SerialSignal_DSR)
  2414. strcat(stat_buf, "|DSR");
  2415. if (info->serial_signals & SerialSignal_DCD)
  2416. strcat(stat_buf, "|CD");
  2417. if (info->serial_signals & SerialSignal_RI)
  2418. strcat(stat_buf, "|RI");
  2419. if (info->params.mode == MGSL_MODE_HDLC) {
  2420. ret += sprintf(buf+ret, " HDLC txok:%d rxok:%d",
  2421. info->icount.txok, info->icount.rxok);
  2422. if (info->icount.txunder)
  2423. ret += sprintf(buf+ret, " txunder:%d", info->icount.txunder);
  2424. if (info->icount.txabort)
  2425. ret += sprintf(buf+ret, " txabort:%d", info->icount.txabort);
  2426. if (info->icount.rxshort)
  2427. ret += sprintf(buf+ret, " rxshort:%d", info->icount.rxshort);
  2428. if (info->icount.rxlong)
  2429. ret += sprintf(buf+ret, " rxlong:%d", info->icount.rxlong);
  2430. if (info->icount.rxover)
  2431. ret += sprintf(buf+ret, " rxover:%d", info->icount.rxover);
  2432. if (info->icount.rxcrc)
  2433. ret += sprintf(buf+ret, " rxcrc:%d", info->icount.rxcrc);
  2434. } else {
  2435. ret += sprintf(buf+ret, " ASYNC tx:%d rx:%d",
  2436. info->icount.tx, info->icount.rx);
  2437. if (info->icount.frame)
  2438. ret += sprintf(buf+ret, " fe:%d", info->icount.frame);
  2439. if (info->icount.parity)
  2440. ret += sprintf(buf+ret, " pe:%d", info->icount.parity);
  2441. if (info->icount.brk)
  2442. ret += sprintf(buf+ret, " brk:%d", info->icount.brk);
  2443. if (info->icount.overrun)
  2444. ret += sprintf(buf+ret, " oe:%d", info->icount.overrun);
  2445. }
  2446. /* Append serial signal status to end */
  2447. ret += sprintf(buf+ret, " %s\n", stat_buf+1);
  2448. ret += sprintf(buf+ret, "txactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
  2449. info->tx_active,info->bh_requested,info->bh_running,
  2450. info->pending_bh);
  2451. return ret;
  2452. }
  2453. /* Called to print information about devices
  2454. */
  2455. static int mgslpc_read_proc(char *page, char **start, off_t off, int count,
  2456. int *eof, void *data)
  2457. {
  2458. int len = 0, l;
  2459. off_t begin = 0;
  2460. MGSLPC_INFO *info;
  2461. len += sprintf(page, "synclink driver:%s\n", driver_version);
  2462. info = mgslpc_device_list;
  2463. while( info ) {
  2464. l = line_info(page + len, info);
  2465. len += l;
  2466. if (len+begin > off+count)
  2467. goto done;
  2468. if (len+begin < off) {
  2469. begin += len;
  2470. len = 0;
  2471. }
  2472. info = info->next_device;
  2473. }
  2474. *eof = 1;
  2475. done:
  2476. if (off >= len+begin)
  2477. return 0;
  2478. *start = page + (off-begin);
  2479. return ((count < begin+len-off) ? count : begin+len-off);
  2480. }
  2481. int rx_alloc_buffers(MGSLPC_INFO *info)
  2482. {
  2483. /* each buffer has header and data */
  2484. info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size;
  2485. /* calculate total allocation size for 8 buffers */
  2486. info->rx_buf_total_size = info->rx_buf_size * 8;
  2487. /* limit total allocated memory */
  2488. if (info->rx_buf_total_size > 0x10000)
  2489. info->rx_buf_total_size = 0x10000;
  2490. /* calculate number of buffers */
  2491. info->rx_buf_count = info->rx_buf_total_size / info->rx_buf_size;
  2492. info->rx_buf = kmalloc(info->rx_buf_total_size, GFP_KERNEL);
  2493. if (info->rx_buf == NULL)
  2494. return -ENOMEM;
  2495. rx_reset_buffers(info);
  2496. return 0;
  2497. }
  2498. void rx_free_buffers(MGSLPC_INFO *info)
  2499. {
  2500. if (info->rx_buf)
  2501. kfree(info->rx_buf);
  2502. info->rx_buf = NULL;
  2503. }
  2504. int claim_resources(MGSLPC_INFO *info)
  2505. {
  2506. if (rx_alloc_buffers(info) < 0 ) {
  2507. printk( "Cant allocate rx buffer %s\n", info->device_name);
  2508. release_resources(info);
  2509. return -ENODEV;
  2510. }
  2511. return 0;
  2512. }
  2513. void release_resources(MGSLPC_INFO *info)
  2514. {
  2515. if (debug_level >= DEBUG_LEVEL_INFO)
  2516. printk("release_resources(%s)\n", info->device_name);
  2517. rx_free_buffers(info);
  2518. }
  2519. /* Add the specified device instance data structure to the
  2520. * global linked list of devices and increment the device count.
  2521. *
  2522. * Arguments: info pointer to device instance data
  2523. */
  2524. void mgslpc_add_device(MGSLPC_INFO *info)
  2525. {
  2526. info->next_device = NULL;
  2527. info->line = mgslpc_device_count;
  2528. sprintf(info->device_name,"ttySLP%d",info->line);
  2529. if (info->line < MAX_DEVICE_COUNT) {
  2530. if (maxframe[info->line])
  2531. info->max_frame_size = maxframe[info->line];
  2532. info->dosyncppp = dosyncppp[info->line];
  2533. }
  2534. mgslpc_device_count++;
  2535. if (!mgslpc_device_list)
  2536. mgslpc_device_list = info;
  2537. else {
  2538. MGSLPC_INFO *current_dev = mgslpc_device_list;
  2539. while( current_dev->next_device )
  2540. current_dev = current_dev->next_device;
  2541. current_dev->next_device = info;
  2542. }
  2543. if (info->max_frame_size < 4096)
  2544. info->max_frame_size = 4096;
  2545. else if (info->max_frame_size > 65535)
  2546. info->max_frame_size = 65535;
  2547. printk( "SyncLink PC Card %s:IO=%04X IRQ=%d\n",
  2548. info->device_name, info->io_base, info->irq_level);
  2549. #ifdef CONFIG_HDLC
  2550. hdlcdev_init(info);
  2551. #endif
  2552. }
  2553. void mgslpc_remove_device(MGSLPC_INFO *remove_info)
  2554. {
  2555. MGSLPC_INFO *info = mgslpc_device_list;
  2556. MGSLPC_INFO *last = NULL;
  2557. while(info) {
  2558. if (info == remove_info) {
  2559. if (last)
  2560. last->next_device = info->next_device;
  2561. else
  2562. mgslpc_device_list = info->next_device;
  2563. #ifdef CONFIG_HDLC
  2564. hdlcdev_exit(info);
  2565. #endif
  2566. release_resources(info);
  2567. kfree(info);
  2568. mgslpc_device_count--;
  2569. return;
  2570. }
  2571. last = info;
  2572. info = info->next_device;
  2573. }
  2574. }
  2575. static struct pcmcia_device_id mgslpc_ids[] = {
  2576. PCMCIA_DEVICE_MANF_CARD(0x02c5, 0x0050),
  2577. PCMCIA_DEVICE_NULL
  2578. };
  2579. MODULE_DEVICE_TABLE(pcmcia, mgslpc_ids);
  2580. static struct pcmcia_driver mgslpc_driver = {
  2581. .owner = THIS_MODULE,
  2582. .drv = {
  2583. .name = "synclink_cs",
  2584. },
  2585. .attach = mgslpc_attach,
  2586. .detach = mgslpc_detach,
  2587. .id_table = mgslpc_ids,
  2588. };
  2589. static struct tty_operations mgslpc_ops = {
  2590. .open = mgslpc_open,
  2591. .close = mgslpc_close,
  2592. .write = mgslpc_write,
  2593. .put_char = mgslpc_put_char,
  2594. .flush_chars = mgslpc_flush_chars,
  2595. .write_room = mgslpc_write_room,
  2596. .chars_in_buffer = mgslpc_chars_in_buffer,
  2597. .flush_buffer = mgslpc_flush_buffer,
  2598. .ioctl = mgslpc_ioctl,
  2599. .throttle = mgslpc_throttle,
  2600. .unthrottle = mgslpc_unthrottle,
  2601. .send_xchar = mgslpc_send_xchar,
  2602. .break_ctl = mgslpc_break,
  2603. .wait_until_sent = mgslpc_wait_until_sent,
  2604. .read_proc = mgslpc_read_proc,
  2605. .set_termios = mgslpc_set_termios,
  2606. .stop = tx_pause,
  2607. .start = tx_release,
  2608. .hangup = mgslpc_hangup,
  2609. .tiocmget = tiocmget,
  2610. .tiocmset = tiocmset,
  2611. };
  2612. static void synclink_cs_cleanup(void)
  2613. {
  2614. int rc;
  2615. printk("Unloading %s: version %s\n", driver_name, driver_version);
  2616. while(mgslpc_device_list)
  2617. mgslpc_remove_device(mgslpc_device_list);
  2618. if (serial_driver) {
  2619. if ((rc = tty_unregister_driver(serial_driver)))
  2620. printk("%s(%d) failed to unregister tty driver err=%d\n",
  2621. __FILE__,__LINE__,rc);
  2622. put_tty_driver(serial_driver);
  2623. }
  2624. pcmcia_unregister_driver(&mgslpc_driver);
  2625. BUG_ON(dev_list != NULL);
  2626. }
  2627. static int __init synclink_cs_init(void)
  2628. {
  2629. int rc;
  2630. if (break_on_load) {
  2631. mgslpc_get_text_ptr();
  2632. BREAKPOINT();
  2633. }
  2634. printk("%s %s\n", driver_name, driver_version);
  2635. if ((rc = pcmcia_register_driver(&mgslpc_driver)) < 0)
  2636. return rc;
  2637. serial_driver = alloc_tty_driver(MAX_DEVICE_COUNT);
  2638. if (!serial_driver) {
  2639. rc = -ENOMEM;
  2640. goto error;
  2641. }
  2642. /* Initialize the tty_driver structure */
  2643. serial_driver->owner = THIS_MODULE;
  2644. serial_driver->driver_name = "synclink_cs";
  2645. serial_driver->name = "ttySLP";
  2646. serial_driver->major = ttymajor;
  2647. serial_driver->minor_start = 64;
  2648. serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  2649. serial_driver->subtype = SERIAL_TYPE_NORMAL;
  2650. serial_driver->init_termios = tty_std_termios;
  2651. serial_driver->init_termios.c_cflag =
  2652. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  2653. serial_driver->flags = TTY_DRIVER_REAL_RAW;
  2654. tty_set_operations(serial_driver, &mgslpc_ops);
  2655. if ((rc = tty_register_driver(serial_driver)) < 0) {
  2656. printk("%s(%d):Couldn't register serial driver\n",
  2657. __FILE__,__LINE__);
  2658. put_tty_driver(serial_driver);
  2659. serial_driver = NULL;
  2660. goto error;
  2661. }
  2662. printk("%s %s, tty major#%d\n",
  2663. driver_name, driver_version,
  2664. serial_driver->major);
  2665. return 0;
  2666. error:
  2667. synclink_cs_cleanup();
  2668. return rc;
  2669. }
  2670. static void __exit synclink_cs_exit(void)
  2671. {
  2672. synclink_cs_cleanup();
  2673. }
  2674. module_init(synclink_cs_init);
  2675. module_exit(synclink_cs_exit);
  2676. static void mgslpc_set_rate(MGSLPC_INFO *info, unsigned char channel, unsigned int rate)
  2677. {
  2678. unsigned int M, N;
  2679. unsigned char val;
  2680. /* note:standard BRG mode is broken in V3.2 chip
  2681. * so enhanced mode is always used
  2682. */
  2683. if (rate) {
  2684. N = 3686400 / rate;
  2685. if (!N)
  2686. N = 1;
  2687. N >>= 1;
  2688. for (M = 1; N > 64 && M < 16; M++)
  2689. N >>= 1;
  2690. N--;
  2691. /* BGR[5..0] = N
  2692. * BGR[9..6] = M
  2693. * BGR[7..0] contained in BGR register
  2694. * BGR[9..8] contained in CCR2[7..6]
  2695. * divisor = (N+1)*2^M
  2696. *
  2697. * Note: M *must* not be zero (causes asymetric duty cycle)
  2698. */
  2699. write_reg(info, (unsigned char) (channel + BGR),
  2700. (unsigned char) ((M << 6) + N));
  2701. val = read_reg(info, (unsigned char) (channel + CCR2)) & 0x3f;
  2702. val |= ((M << 4) & 0xc0);
  2703. write_reg(info, (unsigned char) (channel + CCR2), val);
  2704. }
  2705. }
  2706. /* Enabled the AUX clock output at the specified frequency.
  2707. */
  2708. static void enable_auxclk(MGSLPC_INFO *info)
  2709. {
  2710. unsigned char val;
  2711. /* MODE
  2712. *
  2713. * 07..06 MDS[1..0] 10 = transparent HDLC mode
  2714. * 05 ADM Address Mode, 0 = no addr recognition
  2715. * 04 TMD Timer Mode, 0 = external
  2716. * 03 RAC Receiver Active, 0 = inactive
  2717. * 02 RTS 0=RTS active during xmit, 1=RTS always active
  2718. * 01 TRS Timer Resolution, 1=512
  2719. * 00 TLP Test Loop, 0 = no loop
  2720. *
  2721. * 1000 0010
  2722. */
  2723. val = 0x82;
  2724. /* channel B RTS is used to enable AUXCLK driver on SP505 */
  2725. if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
  2726. val |= BIT2;
  2727. write_reg(info, CHB + MODE, val);
  2728. /* CCR0
  2729. *
  2730. * 07 PU Power Up, 1=active, 0=power down
  2731. * 06 MCE Master Clock Enable, 1=enabled
  2732. * 05 Reserved, 0
  2733. * 04..02 SC[2..0] Encoding
  2734. * 01..00 SM[1..0] Serial Mode, 00=HDLC
  2735. *
  2736. * 11000000
  2737. */
  2738. write_reg(info, CHB + CCR0, 0xc0);
  2739. /* CCR1
  2740. *
  2741. * 07 SFLG Shared Flag, 0 = disable shared flags
  2742. * 06 GALP Go Active On Loop, 0 = not used
  2743. * 05 GLP Go On Loop, 0 = not used
  2744. * 04 ODS Output Driver Select, 1=TxD is push-pull output
  2745. * 03 ITF Interframe Time Fill, 0=mark, 1=flag
  2746. * 02..00 CM[2..0] Clock Mode
  2747. *
  2748. * 0001 0111
  2749. */
  2750. write_reg(info, CHB + CCR1, 0x17);
  2751. /* CCR2 (Channel B)
  2752. *
  2753. * 07..06 BGR[9..8] Baud rate bits 9..8
  2754. * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
  2755. * 04 SSEL Clock source select, 1=submode b
  2756. * 03 TOE 0=TxCLK is input, 1=TxCLK is output
  2757. * 02 RWX Read/Write Exchange 0=disabled
  2758. * 01 C32, CRC select, 0=CRC-16, 1=CRC-32
  2759. * 00 DIV, data inversion 0=disabled, 1=enabled
  2760. *
  2761. * 0011 1000
  2762. */
  2763. if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
  2764. write_reg(info, CHB + CCR2, 0x38);
  2765. else
  2766. write_reg(info, CHB + CCR2, 0x30);
  2767. /* CCR4
  2768. *
  2769. * 07 MCK4 Master Clock Divide by 4, 1=enabled
  2770. * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
  2771. * 05 TST1 Test Pin, 0=normal operation
  2772. * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
  2773. * 03..02 Reserved, must be 0
  2774. * 01..00 RFT[1..0] RxFIFO Threshold 00=32 bytes
  2775. *
  2776. * 0101 0000
  2777. */
  2778. write_reg(info, CHB + CCR4, 0x50);
  2779. /* if auxclk not enabled, set internal BRG so
  2780. * CTS transitions can be detected (requires TxC)
  2781. */
  2782. if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
  2783. mgslpc_set_rate(info, CHB, info->params.clock_speed);
  2784. else
  2785. mgslpc_set_rate(info, CHB, 921600);
  2786. }
  2787. static void loopback_enable(MGSLPC_INFO *info)
  2788. {
  2789. unsigned char val;
  2790. /* CCR1:02..00 CM[2..0] Clock Mode = 111 (clock mode 7) */
  2791. val = read_reg(info, CHA + CCR1) | (BIT2 + BIT1 + BIT0);
  2792. write_reg(info, CHA + CCR1, val);
  2793. /* CCR2:04 SSEL Clock source select, 1=submode b */
  2794. val = read_reg(info, CHA + CCR2) | (BIT4 + BIT5);
  2795. write_reg(info, CHA + CCR2, val);
  2796. /* set LinkSpeed if available, otherwise default to 2Mbps */
  2797. if (info->params.clock_speed)
  2798. mgslpc_set_rate(info, CHA, info->params.clock_speed);
  2799. else
  2800. mgslpc_set_rate(info, CHA, 1843200);
  2801. /* MODE:00 TLP Test Loop, 1=loopback enabled */
  2802. val = read_reg(info, CHA + MODE) | BIT0;
  2803. write_reg(info, CHA + MODE, val);
  2804. }
  2805. void hdlc_mode(MGSLPC_INFO *info)
  2806. {
  2807. unsigned char val;
  2808. unsigned char clkmode, clksubmode;
  2809. /* disable all interrupts */
  2810. irq_disable(info, CHA, 0xffff);
  2811. irq_disable(info, CHB, 0xffff);
  2812. port_irq_disable(info, 0xff);
  2813. /* assume clock mode 0a, rcv=RxC xmt=TxC */
  2814. clkmode = clksubmode = 0;
  2815. if (info->params.flags & HDLC_FLAG_RXC_DPLL
  2816. && info->params.flags & HDLC_FLAG_TXC_DPLL) {
  2817. /* clock mode 7a, rcv = DPLL, xmt = DPLL */
  2818. clkmode = 7;
  2819. } else if (info->params.flags & HDLC_FLAG_RXC_BRG
  2820. && info->params.flags & HDLC_FLAG_TXC_BRG) {
  2821. /* clock mode 7b, rcv = BRG, xmt = BRG */
  2822. clkmode = 7;
  2823. clksubmode = 1;
  2824. } else if (info->params.flags & HDLC_FLAG_RXC_DPLL) {
  2825. if (info->params.flags & HDLC_FLAG_TXC_BRG) {
  2826. /* clock mode 6b, rcv = DPLL, xmt = BRG/16 */
  2827. clkmode = 6;
  2828. clksubmode = 1;
  2829. } else {
  2830. /* clock mode 6a, rcv = DPLL, xmt = TxC */
  2831. clkmode = 6;
  2832. }
  2833. } else if (info->params.flags & HDLC_FLAG_TXC_BRG) {
  2834. /* clock mode 0b, rcv = RxC, xmt = BRG */
  2835. clksubmode = 1;
  2836. }
  2837. /* MODE
  2838. *
  2839. * 07..06 MDS[1..0] 10 = transparent HDLC mode
  2840. * 05 ADM Address Mode, 0 = no addr recognition
  2841. * 04 TMD Timer Mode, 0 = external
  2842. * 03 RAC Receiver Active, 0 = inactive
  2843. * 02 RTS 0=RTS active during xmit, 1=RTS always active
  2844. * 01 TRS Timer Resolution, 1=512
  2845. * 00 TLP Test Loop, 0 = no loop
  2846. *
  2847. * 1000 0010
  2848. */
  2849. val = 0x82;
  2850. if (info->params.loopback)
  2851. val |= BIT0;
  2852. /* preserve RTS state */
  2853. if (info->serial_signals & SerialSignal_RTS)
  2854. val |= BIT2;
  2855. write_reg(info, CHA + MODE, val);
  2856. /* CCR0
  2857. *
  2858. * 07 PU Power Up, 1=active, 0=power down
  2859. * 06 MCE Master Clock Enable, 1=enabled
  2860. * 05 Reserved, 0
  2861. * 04..02 SC[2..0] Encoding
  2862. * 01..00 SM[1..0] Serial Mode, 00=HDLC
  2863. *
  2864. * 11000000
  2865. */
  2866. val = 0xc0;
  2867. switch (info->params.encoding)
  2868. {
  2869. case HDLC_ENCODING_NRZI:
  2870. val |= BIT3;
  2871. break;
  2872. case HDLC_ENCODING_BIPHASE_SPACE:
  2873. val |= BIT4;
  2874. break; // FM0
  2875. case HDLC_ENCODING_BIPHASE_MARK:
  2876. val |= BIT4 + BIT2;
  2877. break; // FM1
  2878. case HDLC_ENCODING_BIPHASE_LEVEL:
  2879. val |= BIT4 + BIT3;
  2880. break; // Manchester
  2881. }
  2882. write_reg(info, CHA + CCR0, val);
  2883. /* CCR1
  2884. *
  2885. * 07 SFLG Shared Flag, 0 = disable shared flags
  2886. * 06 GALP Go Active On Loop, 0 = not used
  2887. * 05 GLP Go On Loop, 0 = not used
  2888. * 04 ODS Output Driver Select, 1=TxD is push-pull output
  2889. * 03 ITF Interframe Time Fill, 0=mark, 1=flag
  2890. * 02..00 CM[2..0] Clock Mode
  2891. *
  2892. * 0001 0000
  2893. */
  2894. val = 0x10 + clkmode;
  2895. write_reg(info, CHA + CCR1, val);
  2896. /* CCR2
  2897. *
  2898. * 07..06 BGR[9..8] Baud rate bits 9..8
  2899. * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
  2900. * 04 SSEL Clock source select, 1=submode b
  2901. * 03 TOE 0=TxCLK is input, 0=TxCLK is input
  2902. * 02 RWX Read/Write Exchange 0=disabled
  2903. * 01 C32, CRC select, 0=CRC-16, 1=CRC-32
  2904. * 00 DIV, data inversion 0=disabled, 1=enabled
  2905. *
  2906. * 0000 0000
  2907. */
  2908. val = 0x00;
  2909. if (clkmode == 2 || clkmode == 3 || clkmode == 6
  2910. || clkmode == 7 || (clkmode == 0 && clksubmode == 1))
  2911. val |= BIT5;
  2912. if (clksubmode)
  2913. val |= BIT4;
  2914. if (info->params.crc_type == HDLC_CRC_32_CCITT)
  2915. val |= BIT1;
  2916. if (info->params.encoding == HDLC_ENCODING_NRZB)
  2917. val |= BIT0;
  2918. write_reg(info, CHA + CCR2, val);
  2919. /* CCR3
  2920. *
  2921. * 07..06 PRE[1..0] Preamble count 00=1, 01=2, 10=4, 11=8
  2922. * 05 EPT Enable preamble transmission, 1=enabled
  2923. * 04 RADD Receive address pushed to FIFO, 0=disabled
  2924. * 03 CRL CRC Reset Level, 0=FFFF
  2925. * 02 RCRC Rx CRC 0=On 1=Off
  2926. * 01 TCRC Tx CRC 0=On 1=Off
  2927. * 00 PSD DPLL Phase Shift Disable
  2928. *
  2929. * 0000 0000
  2930. */
  2931. val = 0x00;
  2932. if (info->params.crc_type == HDLC_CRC_NONE)
  2933. val |= BIT2 + BIT1;
  2934. if (info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE)
  2935. val |= BIT5;
  2936. switch (info->params.preamble_length)
  2937. {
  2938. case HDLC_PREAMBLE_LENGTH_16BITS:
  2939. val |= BIT6;
  2940. break;
  2941. case HDLC_PREAMBLE_LENGTH_32BITS:
  2942. val |= BIT6;
  2943. break;
  2944. case HDLC_PREAMBLE_LENGTH_64BITS:
  2945. val |= BIT7 + BIT6;
  2946. break;
  2947. }
  2948. write_reg(info, CHA + CCR3, val);
  2949. /* PRE - Preamble pattern */
  2950. val = 0;
  2951. switch (info->params.preamble)
  2952. {
  2953. case HDLC_PREAMBLE_PATTERN_FLAGS: val = 0x7e; break;
  2954. case HDLC_PREAMBLE_PATTERN_10: val = 0xaa; break;
  2955. case HDLC_PREAMBLE_PATTERN_01: val = 0x55; break;
  2956. case HDLC_PREAMBLE_PATTERN_ONES: val = 0xff; break;
  2957. }
  2958. write_reg(info, CHA + PRE, val);
  2959. /* CCR4
  2960. *
  2961. * 07 MCK4 Master Clock Divide by 4, 1=enabled
  2962. * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
  2963. * 05 TST1 Test Pin, 0=normal operation
  2964. * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
  2965. * 03..02 Reserved, must be 0
  2966. * 01..00 RFT[1..0] RxFIFO Threshold 00=32 bytes
  2967. *
  2968. * 0101 0000
  2969. */
  2970. val = 0x50;
  2971. write_reg(info, CHA + CCR4, val);
  2972. if (info->params.flags & HDLC_FLAG_RXC_DPLL)
  2973. mgslpc_set_rate(info, CHA, info->params.clock_speed * 16);
  2974. else
  2975. mgslpc_set_rate(info, CHA, info->params.clock_speed);
  2976. /* RLCR Receive length check register
  2977. *
  2978. * 7 1=enable receive length check
  2979. * 6..0 Max frame length = (RL + 1) * 32
  2980. */
  2981. write_reg(info, CHA + RLCR, 0);
  2982. /* XBCH Transmit Byte Count High
  2983. *
  2984. * 07 DMA mode, 0 = interrupt driven
  2985. * 06 NRM, 0=ABM (ignored)
  2986. * 05 CAS Carrier Auto Start
  2987. * 04 XC Transmit Continuously (ignored)
  2988. * 03..00 XBC[10..8] Transmit byte count bits 10..8
  2989. *
  2990. * 0000 0000
  2991. */
  2992. val = 0x00;
  2993. if (info->params.flags & HDLC_FLAG_AUTO_DCD)
  2994. val |= BIT5;
  2995. write_reg(info, CHA + XBCH, val);
  2996. enable_auxclk(info);
  2997. if (info->params.loopback || info->testing_irq)
  2998. loopback_enable(info);
  2999. if (info->params.flags & HDLC_FLAG_AUTO_CTS)
  3000. {
  3001. irq_enable(info, CHB, IRQ_CTS);
  3002. /* PVR[3] 1=AUTO CTS active */
  3003. set_reg_bits(info, CHA + PVR, BIT3);
  3004. } else
  3005. clear_reg_bits(info, CHA + PVR, BIT3);
  3006. irq_enable(info, CHA,
  3007. IRQ_RXEOM + IRQ_RXFIFO + IRQ_ALLSENT +
  3008. IRQ_UNDERRUN + IRQ_TXFIFO);
  3009. issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
  3010. wait_command_complete(info, CHA);
  3011. read_reg16(info, CHA + ISR); /* clear pending IRQs */
  3012. /* Master clock mode enabled above to allow reset commands
  3013. * to complete even if no data clocks are present.
  3014. *
  3015. * Disable master clock mode for normal communications because
  3016. * V3.2 of the ESCC2 has a bug that prevents the transmit all sent
  3017. * IRQ when in master clock mode.
  3018. *
  3019. * Leave master clock mode enabled for IRQ test because the
  3020. * timer IRQ used by the test can only happen in master clock mode.
  3021. */
  3022. if (!info->testing_irq)
  3023. clear_reg_bits(info, CHA + CCR0, BIT6);
  3024. tx_set_idle(info);
  3025. tx_stop(info);
  3026. rx_stop(info);
  3027. }
  3028. void rx_stop(MGSLPC_INFO *info)
  3029. {
  3030. if (debug_level >= DEBUG_LEVEL_ISR)
  3031. printk("%s(%d):rx_stop(%s)\n",
  3032. __FILE__,__LINE__, info->device_name );
  3033. /* MODE:03 RAC Receiver Active, 0=inactive */
  3034. clear_reg_bits(info, CHA + MODE, BIT3);
  3035. info->rx_enabled = 0;
  3036. info->rx_overflow = 0;
  3037. }
  3038. void rx_start(MGSLPC_INFO *info)
  3039. {
  3040. if (debug_level >= DEBUG_LEVEL_ISR)
  3041. printk("%s(%d):rx_start(%s)\n",
  3042. __FILE__,__LINE__, info->device_name );
  3043. rx_reset_buffers(info);
  3044. info->rx_enabled = 0;
  3045. info->rx_overflow = 0;
  3046. /* MODE:03 RAC Receiver Active, 1=active */
  3047. set_reg_bits(info, CHA + MODE, BIT3);
  3048. info->rx_enabled = 1;
  3049. }
  3050. void tx_start(MGSLPC_INFO *info)
  3051. {
  3052. if (debug_level >= DEBUG_LEVEL_ISR)
  3053. printk("%s(%d):tx_start(%s)\n",
  3054. __FILE__,__LINE__, info->device_name );
  3055. if (info->tx_count) {
  3056. /* If auto RTS enabled and RTS is inactive, then assert */
  3057. /* RTS and set a flag indicating that the driver should */
  3058. /* negate RTS when the transmission completes. */
  3059. info->drop_rts_on_tx_done = 0;
  3060. if (info->params.flags & HDLC_FLAG_AUTO_RTS) {
  3061. get_signals(info);
  3062. if (!(info->serial_signals & SerialSignal_RTS)) {
  3063. info->serial_signals |= SerialSignal_RTS;
  3064. set_signals(info);
  3065. info->drop_rts_on_tx_done = 1;
  3066. }
  3067. }
  3068. if (info->params.mode == MGSL_MODE_ASYNC) {
  3069. if (!info->tx_active) {
  3070. info->tx_active = 1;
  3071. tx_ready(info);
  3072. }
  3073. } else {
  3074. info->tx_active = 1;
  3075. tx_ready(info);
  3076. info->tx_timer.expires = jiffies + msecs_to_jiffies(5000);
  3077. add_timer(&info->tx_timer);
  3078. }
  3079. }
  3080. if (!info->tx_enabled)
  3081. info->tx_enabled = 1;
  3082. }
  3083. void tx_stop(MGSLPC_INFO *info)
  3084. {
  3085. if (debug_level >= DEBUG_LEVEL_ISR)
  3086. printk("%s(%d):tx_stop(%s)\n",
  3087. __FILE__,__LINE__, info->device_name );
  3088. del_timer(&info->tx_timer);
  3089. info->tx_enabled = 0;
  3090. info->tx_active = 0;
  3091. }
  3092. /* Reset the adapter to a known state and prepare it for further use.
  3093. */
  3094. void reset_device(MGSLPC_INFO *info)
  3095. {
  3096. /* power up both channels (set BIT7) */
  3097. write_reg(info, CHA + CCR0, 0x80);
  3098. write_reg(info, CHB + CCR0, 0x80);
  3099. write_reg(info, CHA + MODE, 0);
  3100. write_reg(info, CHB + MODE, 0);
  3101. /* disable all interrupts */
  3102. irq_disable(info, CHA, 0xffff);
  3103. irq_disable(info, CHB, 0xffff);
  3104. port_irq_disable(info, 0xff);
  3105. /* PCR Port Configuration Register
  3106. *
  3107. * 07..04 DEC[3..0] Serial I/F select outputs
  3108. * 03 output, 1=AUTO CTS control enabled
  3109. * 02 RI Ring Indicator input 0=active
  3110. * 01 DSR input 0=active
  3111. * 00 DTR output 0=active
  3112. *
  3113. * 0000 0110
  3114. */
  3115. write_reg(info, PCR, 0x06);
  3116. /* PVR Port Value Register
  3117. *
  3118. * 07..04 DEC[3..0] Serial I/F select (0000=disabled)
  3119. * 03 AUTO CTS output 1=enabled
  3120. * 02 RI Ring Indicator input
  3121. * 01 DSR input
  3122. * 00 DTR output (1=inactive)
  3123. *
  3124. * 0000 0001
  3125. */
  3126. // write_reg(info, PVR, PVR_DTR);
  3127. /* IPC Interrupt Port Configuration
  3128. *
  3129. * 07 VIS 1=Masked interrupts visible
  3130. * 06..05 Reserved, 0
  3131. * 04..03 SLA Slave address, 00 ignored
  3132. * 02 CASM Cascading Mode, 1=daisy chain
  3133. * 01..00 IC[1..0] Interrupt Config, 01=push-pull output, active low
  3134. *
  3135. * 0000 0101
  3136. */
  3137. write_reg(info, IPC, 0x05);
  3138. }
  3139. void async_mode(MGSLPC_INFO *info)
  3140. {
  3141. unsigned char val;
  3142. /* disable all interrupts */
  3143. irq_disable(info, CHA, 0xffff);
  3144. irq_disable(info, CHB, 0xffff);
  3145. port_irq_disable(info, 0xff);
  3146. /* MODE
  3147. *
  3148. * 07 Reserved, 0
  3149. * 06 FRTS RTS State, 0=active
  3150. * 05 FCTS Flow Control on CTS
  3151. * 04 FLON Flow Control Enable
  3152. * 03 RAC Receiver Active, 0 = inactive
  3153. * 02 RTS 0=Auto RTS, 1=manual RTS
  3154. * 01 TRS Timer Resolution, 1=512
  3155. * 00 TLP Test Loop, 0 = no loop
  3156. *
  3157. * 0000 0110
  3158. */
  3159. val = 0x06;
  3160. if (info->params.loopback)
  3161. val |= BIT0;
  3162. /* preserve RTS state */
  3163. if (!(info->serial_signals & SerialSignal_RTS))
  3164. val |= BIT6;
  3165. write_reg(info, CHA + MODE, val);
  3166. /* CCR0
  3167. *
  3168. * 07 PU Power Up, 1=active, 0=power down
  3169. * 06 MCE Master Clock Enable, 1=enabled
  3170. * 05 Reserved, 0
  3171. * 04..02 SC[2..0] Encoding, 000=NRZ
  3172. * 01..00 SM[1..0] Serial Mode, 11=Async
  3173. *
  3174. * 1000 0011
  3175. */
  3176. write_reg(info, CHA + CCR0, 0x83);
  3177. /* CCR1
  3178. *
  3179. * 07..05 Reserved, 0
  3180. * 04 ODS Output Driver Select, 1=TxD is push-pull output
  3181. * 03 BCR Bit Clock Rate, 1=16x
  3182. * 02..00 CM[2..0] Clock Mode, 111=BRG
  3183. *
  3184. * 0001 1111
  3185. */
  3186. write_reg(info, CHA + CCR1, 0x1f);
  3187. /* CCR2 (channel A)
  3188. *
  3189. * 07..06 BGR[9..8] Baud rate bits 9..8
  3190. * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
  3191. * 04 SSEL Clock source select, 1=submode b
  3192. * 03 TOE 0=TxCLK is input, 0=TxCLK is input
  3193. * 02 RWX Read/Write Exchange 0=disabled
  3194. * 01 Reserved, 0
  3195. * 00 DIV, data inversion 0=disabled, 1=enabled
  3196. *
  3197. * 0001 0000
  3198. */
  3199. write_reg(info, CHA + CCR2, 0x10);
  3200. /* CCR3
  3201. *
  3202. * 07..01 Reserved, 0
  3203. * 00 PSD DPLL Phase Shift Disable
  3204. *
  3205. * 0000 0000
  3206. */
  3207. write_reg(info, CHA + CCR3, 0);
  3208. /* CCR4
  3209. *
  3210. * 07 MCK4 Master Clock Divide by 4, 1=enabled
  3211. * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
  3212. * 05 TST1 Test Pin, 0=normal operation
  3213. * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
  3214. * 03..00 Reserved, must be 0
  3215. *
  3216. * 0101 0000
  3217. */
  3218. write_reg(info, CHA + CCR4, 0x50);
  3219. mgslpc_set_rate(info, CHA, info->params.data_rate * 16);
  3220. /* DAFO Data Format
  3221. *
  3222. * 07 Reserved, 0
  3223. * 06 XBRK transmit break, 0=normal operation
  3224. * 05 Stop bits (0=1, 1=2)
  3225. * 04..03 PAR[1..0] Parity (01=odd, 10=even)
  3226. * 02 PAREN Parity Enable
  3227. * 01..00 CHL[1..0] Character Length (00=8, 01=7)
  3228. *
  3229. */
  3230. val = 0x00;
  3231. if (info->params.data_bits != 8)
  3232. val |= BIT0; /* 7 bits */
  3233. if (info->params.stop_bits != 1)
  3234. val |= BIT5;
  3235. if (info->params.parity != ASYNC_PARITY_NONE)
  3236. {
  3237. val |= BIT2; /* Parity enable */
  3238. if (info->params.parity == ASYNC_PARITY_ODD)
  3239. val |= BIT3;
  3240. else
  3241. val |= BIT4;
  3242. }
  3243. write_reg(info, CHA + DAFO, val);
  3244. /* RFC Rx FIFO Control
  3245. *
  3246. * 07 Reserved, 0
  3247. * 06 DPS, 1=parity bit not stored in data byte
  3248. * 05 DXS, 0=all data stored in FIFO (including XON/XOFF)
  3249. * 04 RFDF Rx FIFO Data Format, 1=status byte stored in FIFO
  3250. * 03..02 RFTH[1..0], rx threshold, 11=16 status + 16 data byte
  3251. * 01 Reserved, 0
  3252. * 00 TCDE Terminate Char Detect Enable, 0=disabled
  3253. *
  3254. * 0101 1100
  3255. */
  3256. write_reg(info, CHA + RFC, 0x5c);
  3257. /* RLCR Receive length check register
  3258. *
  3259. * Max frame length = (RL + 1) * 32
  3260. */
  3261. write_reg(info, CHA + RLCR, 0);
  3262. /* XBCH Transmit Byte Count High
  3263. *
  3264. * 07 DMA mode, 0 = interrupt driven
  3265. * 06 NRM, 0=ABM (ignored)
  3266. * 05 CAS Carrier Auto Start
  3267. * 04 XC Transmit Continuously (ignored)
  3268. * 03..00 XBC[10..8] Transmit byte count bits 10..8
  3269. *
  3270. * 0000 0000
  3271. */
  3272. val = 0x00;
  3273. if (info->params.flags & HDLC_FLAG_AUTO_DCD)
  3274. val |= BIT5;
  3275. write_reg(info, CHA + XBCH, val);
  3276. if (info->params.flags & HDLC_FLAG_AUTO_CTS)
  3277. irq_enable(info, CHA, IRQ_CTS);
  3278. /* MODE:03 RAC Receiver Active, 1=active */
  3279. set_reg_bits(info, CHA + MODE, BIT3);
  3280. enable_auxclk(info);
  3281. if (info->params.flags & HDLC_FLAG_AUTO_CTS) {
  3282. irq_enable(info, CHB, IRQ_CTS);
  3283. /* PVR[3] 1=AUTO CTS active */
  3284. set_reg_bits(info, CHA + PVR, BIT3);
  3285. } else
  3286. clear_reg_bits(info, CHA + PVR, BIT3);
  3287. irq_enable(info, CHA,
  3288. IRQ_RXEOM + IRQ_RXFIFO + IRQ_BREAK_ON + IRQ_RXTIME +
  3289. IRQ_ALLSENT + IRQ_TXFIFO);
  3290. issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
  3291. wait_command_complete(info, CHA);
  3292. read_reg16(info, CHA + ISR); /* clear pending IRQs */
  3293. }
  3294. /* Set the HDLC idle mode for the transmitter.
  3295. */
  3296. void tx_set_idle(MGSLPC_INFO *info)
  3297. {
  3298. /* Note: ESCC2 only supports flags and one idle modes */
  3299. if (info->idle_mode == HDLC_TXIDLE_FLAGS)
  3300. set_reg_bits(info, CHA + CCR1, BIT3);
  3301. else
  3302. clear_reg_bits(info, CHA + CCR1, BIT3);
  3303. }
  3304. /* get state of the V24 status (input) signals.
  3305. */
  3306. void get_signals(MGSLPC_INFO *info)
  3307. {
  3308. unsigned char status = 0;
  3309. /* preserve DTR and RTS */
  3310. info->serial_signals &= SerialSignal_DTR + SerialSignal_RTS;
  3311. if (read_reg(info, CHB + VSTR) & BIT7)
  3312. info->serial_signals |= SerialSignal_DCD;
  3313. if (read_reg(info, CHB + STAR) & BIT1)
  3314. info->serial_signals |= SerialSignal_CTS;
  3315. status = read_reg(info, CHA + PVR);
  3316. if (!(status & PVR_RI))
  3317. info->serial_signals |= SerialSignal_RI;
  3318. if (!(status & PVR_DSR))
  3319. info->serial_signals |= SerialSignal_DSR;
  3320. }
  3321. /* Set the state of DTR and RTS based on contents of
  3322. * serial_signals member of device extension.
  3323. */
  3324. void set_signals(MGSLPC_INFO *info)
  3325. {
  3326. unsigned char val;
  3327. val = read_reg(info, CHA + MODE);
  3328. if (info->params.mode == MGSL_MODE_ASYNC) {
  3329. if (info->serial_signals & SerialSignal_RTS)
  3330. val &= ~BIT6;
  3331. else
  3332. val |= BIT6;
  3333. } else {
  3334. if (info->serial_signals & SerialSignal_RTS)
  3335. val |= BIT2;
  3336. else
  3337. val &= ~BIT2;
  3338. }
  3339. write_reg(info, CHA + MODE, val);
  3340. if (info->serial_signals & SerialSignal_DTR)
  3341. clear_reg_bits(info, CHA + PVR, PVR_DTR);
  3342. else
  3343. set_reg_bits(info, CHA + PVR, PVR_DTR);
  3344. }
  3345. void rx_reset_buffers(MGSLPC_INFO *info)
  3346. {
  3347. RXBUF *buf;
  3348. int i;
  3349. info->rx_put = 0;
  3350. info->rx_get = 0;
  3351. info->rx_frame_count = 0;
  3352. for (i=0 ; i < info->rx_buf_count ; i++) {
  3353. buf = (RXBUF*)(info->rx_buf + (i * info->rx_buf_size));
  3354. buf->status = buf->count = 0;
  3355. }
  3356. }
  3357. /* Attempt to return a received HDLC frame
  3358. * Only frames received without errors are returned.
  3359. *
  3360. * Returns 1 if frame returned, otherwise 0
  3361. */
  3362. int rx_get_frame(MGSLPC_INFO *info)
  3363. {
  3364. unsigned short status;
  3365. RXBUF *buf;
  3366. unsigned int framesize = 0;
  3367. unsigned long flags;
  3368. struct tty_struct *tty = info->tty;
  3369. int return_frame = 0;
  3370. if (info->rx_frame_count == 0)
  3371. return 0;
  3372. buf = (RXBUF*)(info->rx_buf + (info->rx_get * info->rx_buf_size));
  3373. status = buf->status;
  3374. /* 07 VFR 1=valid frame
  3375. * 06 RDO 1=data overrun
  3376. * 05 CRC 1=OK, 0=error
  3377. * 04 RAB 1=frame aborted
  3378. */
  3379. if ((status & 0xf0) != 0xA0) {
  3380. if (!(status & BIT7) || (status & BIT4))
  3381. info->icount.rxabort++;
  3382. else if (status & BIT6)
  3383. info->icount.rxover++;
  3384. else if (!(status & BIT5)) {
  3385. info->icount.rxcrc++;
  3386. if (info->params.crc_type & HDLC_CRC_RETURN_EX)
  3387. return_frame = 1;
  3388. }
  3389. framesize = 0;
  3390. #ifdef CONFIG_HDLC
  3391. {
  3392. struct net_device_stats *stats = hdlc_stats(info->netdev);
  3393. stats->rx_errors++;
  3394. stats->rx_frame_errors++;
  3395. }
  3396. #endif
  3397. } else
  3398. return_frame = 1;
  3399. if (return_frame)
  3400. framesize = buf->count;
  3401. if (debug_level >= DEBUG_LEVEL_BH)
  3402. printk("%s(%d):rx_get_frame(%s) status=%04X size=%d\n",
  3403. __FILE__,__LINE__,info->device_name,status,framesize);
  3404. if (debug_level >= DEBUG_LEVEL_DATA)
  3405. trace_block(info, buf->data, framesize, 0);
  3406. if (framesize) {
  3407. if ((info->params.crc_type & HDLC_CRC_RETURN_EX &&
  3408. framesize+1 > info->max_frame_size) ||
  3409. framesize > info->max_frame_size)
  3410. info->icount.rxlong++;
  3411. else {
  3412. if (status & BIT5)
  3413. info->icount.rxok++;
  3414. if (info->params.crc_type & HDLC_CRC_RETURN_EX) {
  3415. *(buf->data + framesize) = status & BIT5 ? RX_OK:RX_CRC_ERROR;
  3416. ++framesize;
  3417. }
  3418. #ifdef CONFIG_HDLC
  3419. if (info->netcount)
  3420. hdlcdev_rx(info, buf->data, framesize);
  3421. else
  3422. #endif
  3423. ldisc_receive_buf(tty, buf->data, info->flag_buf, framesize);
  3424. }
  3425. }
  3426. spin_lock_irqsave(&info->lock,flags);
  3427. buf->status = buf->count = 0;
  3428. info->rx_frame_count--;
  3429. info->rx_get++;
  3430. if (info->rx_get >= info->rx_buf_count)
  3431. info->rx_get = 0;
  3432. spin_unlock_irqrestore(&info->lock,flags);
  3433. return 1;
  3434. }
  3435. BOOLEAN register_test(MGSLPC_INFO *info)
  3436. {
  3437. static unsigned char patterns[] =
  3438. { 0x00, 0xff, 0xaa, 0x55, 0x69, 0x96, 0x0f };
  3439. static unsigned int count = sizeof(patterns) / sizeof(patterns[0]);
  3440. unsigned int i;
  3441. BOOLEAN rc = TRUE;
  3442. unsigned long flags;
  3443. spin_lock_irqsave(&info->lock,flags);
  3444. reset_device(info);
  3445. for (i = 0; i < count; i++) {
  3446. write_reg(info, XAD1, patterns[i]);
  3447. write_reg(info, XAD2, patterns[(i + 1) % count]);
  3448. if ((read_reg(info, XAD1) != patterns[i]) ||
  3449. (read_reg(info, XAD2) != patterns[(i + 1) % count])) {
  3450. rc = FALSE;
  3451. break;
  3452. }
  3453. }
  3454. spin_unlock_irqrestore(&info->lock,flags);
  3455. return rc;
  3456. }
  3457. BOOLEAN irq_test(MGSLPC_INFO *info)
  3458. {
  3459. unsigned long end_time;
  3460. unsigned long flags;
  3461. spin_lock_irqsave(&info->lock,flags);
  3462. reset_device(info);
  3463. info->testing_irq = TRUE;
  3464. hdlc_mode(info);
  3465. info->irq_occurred = FALSE;
  3466. /* init hdlc mode */
  3467. irq_enable(info, CHA, IRQ_TIMER);
  3468. write_reg(info, CHA + TIMR, 0); /* 512 cycles */
  3469. issue_command(info, CHA, CMD_START_TIMER);
  3470. spin_unlock_irqrestore(&info->lock,flags);
  3471. end_time=100;
  3472. while(end_time-- && !info->irq_occurred) {
  3473. msleep_interruptible(10);
  3474. }
  3475. info->testing_irq = FALSE;
  3476. spin_lock_irqsave(&info->lock,flags);
  3477. reset_device(info);
  3478. spin_unlock_irqrestore(&info->lock,flags);
  3479. return info->irq_occurred ? TRUE : FALSE;
  3480. }
  3481. int adapter_test(MGSLPC_INFO *info)
  3482. {
  3483. if (!register_test(info)) {
  3484. info->init_error = DiagStatus_AddressFailure;
  3485. printk( "%s(%d):Register test failure for device %s Addr=%04X\n",
  3486. __FILE__,__LINE__,info->device_name, (unsigned short)(info->io_base) );
  3487. return -ENODEV;
  3488. }
  3489. if (!irq_test(info)) {
  3490. info->init_error = DiagStatus_IrqFailure;
  3491. printk( "%s(%d):Interrupt test failure for device %s IRQ=%d\n",
  3492. __FILE__,__LINE__,info->device_name, (unsigned short)(info->irq_level) );
  3493. return -ENODEV;
  3494. }
  3495. if (debug_level >= DEBUG_LEVEL_INFO)
  3496. printk("%s(%d):device %s passed diagnostics\n",
  3497. __FILE__,__LINE__,info->device_name);
  3498. return 0;
  3499. }
  3500. void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit)
  3501. {
  3502. int i;
  3503. int linecount;
  3504. if (xmit)
  3505. printk("%s tx data:\n",info->device_name);
  3506. else
  3507. printk("%s rx data:\n",info->device_name);
  3508. while(count) {
  3509. if (count > 16)
  3510. linecount = 16;
  3511. else
  3512. linecount = count;
  3513. for(i=0;i<linecount;i++)
  3514. printk("%02X ",(unsigned char)data[i]);
  3515. for(;i<17;i++)
  3516. printk(" ");
  3517. for(i=0;i<linecount;i++) {
  3518. if (data[i]>=040 && data[i]<=0176)
  3519. printk("%c",data[i]);
  3520. else
  3521. printk(".");
  3522. }
  3523. printk("\n");
  3524. data += linecount;
  3525. count -= linecount;
  3526. }
  3527. }
  3528. /* HDLC frame time out
  3529. * update stats and do tx completion processing
  3530. */
  3531. void tx_timeout(unsigned long context)
  3532. {
  3533. MGSLPC_INFO *info = (MGSLPC_INFO*)context;
  3534. unsigned long flags;
  3535. if ( debug_level >= DEBUG_LEVEL_INFO )
  3536. printk( "%s(%d):tx_timeout(%s)\n",
  3537. __FILE__,__LINE__,info->device_name);
  3538. if(info->tx_active &&
  3539. info->params.mode == MGSL_MODE_HDLC) {
  3540. info->icount.txtimeout++;
  3541. }
  3542. spin_lock_irqsave(&info->lock,flags);
  3543. info->tx_active = 0;
  3544. info->tx_count = info->tx_put = info->tx_get = 0;
  3545. spin_unlock_irqrestore(&info->lock,flags);
  3546. #ifdef CONFIG_HDLC
  3547. if (info->netcount)
  3548. hdlcdev_tx_done(info);
  3549. else
  3550. #endif
  3551. bh_transmit(info);
  3552. }
  3553. #ifdef CONFIG_HDLC
  3554. /**
  3555. * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
  3556. * set encoding and frame check sequence (FCS) options
  3557. *
  3558. * dev pointer to network device structure
  3559. * encoding serial encoding setting
  3560. * parity FCS setting
  3561. *
  3562. * returns 0 if success, otherwise error code
  3563. */
  3564. static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
  3565. unsigned short parity)
  3566. {
  3567. MGSLPC_INFO *info = dev_to_port(dev);
  3568. unsigned char new_encoding;
  3569. unsigned short new_crctype;
  3570. /* return error if TTY interface open */
  3571. if (info->count)
  3572. return -EBUSY;
  3573. switch (encoding)
  3574. {
  3575. case ENCODING_NRZ: new_encoding = HDLC_ENCODING_NRZ; break;
  3576. case ENCODING_NRZI: new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
  3577. case ENCODING_FM_MARK: new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
  3578. case ENCODING_FM_SPACE: new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
  3579. case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
  3580. default: return -EINVAL;
  3581. }
  3582. switch (parity)
  3583. {
  3584. case PARITY_NONE: new_crctype = HDLC_CRC_NONE; break;
  3585. case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
  3586. case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
  3587. default: return -EINVAL;
  3588. }
  3589. info->params.encoding = new_encoding;
  3590. info->params.crc_type = new_crctype;;
  3591. /* if network interface up, reprogram hardware */
  3592. if (info->netcount)
  3593. mgslpc_program_hw(info);
  3594. return 0;
  3595. }
  3596. /**
  3597. * called by generic HDLC layer to send frame
  3598. *
  3599. * skb socket buffer containing HDLC frame
  3600. * dev pointer to network device structure
  3601. *
  3602. * returns 0 if success, otherwise error code
  3603. */
  3604. static int hdlcdev_xmit(struct sk_buff *skb, struct net_device *dev)
  3605. {
  3606. MGSLPC_INFO *info = dev_to_port(dev);
  3607. struct net_device_stats *stats = hdlc_stats(dev);
  3608. unsigned long flags;
  3609. if (debug_level >= DEBUG_LEVEL_INFO)
  3610. printk(KERN_INFO "%s:hdlc_xmit(%s)\n",__FILE__,dev->name);
  3611. /* stop sending until this frame completes */
  3612. netif_stop_queue(dev);
  3613. /* copy data to device buffers */
  3614. memcpy(info->tx_buf, skb->data, skb->len);
  3615. info->tx_get = 0;
  3616. info->tx_put = info->tx_count = skb->len;
  3617. /* update network statistics */
  3618. stats->tx_packets++;
  3619. stats->tx_bytes += skb->len;
  3620. /* done with socket buffer, so free it */
  3621. dev_kfree_skb(skb);
  3622. /* save start time for transmit timeout detection */
  3623. dev->trans_start = jiffies;
  3624. /* start hardware transmitter if necessary */
  3625. spin_lock_irqsave(&info->lock,flags);
  3626. if (!info->tx_active)
  3627. tx_start(info);
  3628. spin_unlock_irqrestore(&info->lock,flags);
  3629. return 0;
  3630. }
  3631. /**
  3632. * called by network layer when interface enabled
  3633. * claim resources and initialize hardware
  3634. *
  3635. * dev pointer to network device structure
  3636. *
  3637. * returns 0 if success, otherwise error code
  3638. */
  3639. static int hdlcdev_open(struct net_device *dev)
  3640. {
  3641. MGSLPC_INFO *info = dev_to_port(dev);
  3642. int rc;
  3643. unsigned long flags;
  3644. if (debug_level >= DEBUG_LEVEL_INFO)
  3645. printk("%s:hdlcdev_open(%s)\n",__FILE__,dev->name);
  3646. /* generic HDLC layer open processing */
  3647. if ((rc = hdlc_open(dev)))
  3648. return rc;
  3649. /* arbitrate between network and tty opens */
  3650. spin_lock_irqsave(&info->netlock, flags);
  3651. if (info->count != 0 || info->netcount != 0) {
  3652. printk(KERN_WARNING "%s: hdlc_open returning busy\n", dev->name);
  3653. spin_unlock_irqrestore(&info->netlock, flags);
  3654. return -EBUSY;
  3655. }
  3656. info->netcount=1;
  3657. spin_unlock_irqrestore(&info->netlock, flags);
  3658. /* claim resources and init adapter */
  3659. if ((rc = startup(info)) != 0) {
  3660. spin_lock_irqsave(&info->netlock, flags);
  3661. info->netcount=0;
  3662. spin_unlock_irqrestore(&info->netlock, flags);
  3663. return rc;
  3664. }
  3665. /* assert DTR and RTS, apply hardware settings */
  3666. info->serial_signals |= SerialSignal_RTS + SerialSignal_DTR;
  3667. mgslpc_program_hw(info);
  3668. /* enable network layer transmit */
  3669. dev->trans_start = jiffies;
  3670. netif_start_queue(dev);
  3671. /* inform generic HDLC layer of current DCD status */
  3672. spin_lock_irqsave(&info->lock, flags);
  3673. get_signals(info);
  3674. spin_unlock_irqrestore(&info->lock, flags);
  3675. hdlc_set_carrier(info->serial_signals & SerialSignal_DCD, dev);
  3676. return 0;
  3677. }
  3678. /**
  3679. * called by network layer when interface is disabled
  3680. * shutdown hardware and release resources
  3681. *
  3682. * dev pointer to network device structure
  3683. *
  3684. * returns 0 if success, otherwise error code
  3685. */
  3686. static int hdlcdev_close(struct net_device *dev)
  3687. {
  3688. MGSLPC_INFO *info = dev_to_port(dev);
  3689. unsigned long flags;
  3690. if (debug_level >= DEBUG_LEVEL_INFO)
  3691. printk("%s:hdlcdev_close(%s)\n",__FILE__,dev->name);
  3692. netif_stop_queue(dev);
  3693. /* shutdown adapter and release resources */
  3694. shutdown(info);
  3695. hdlc_close(dev);
  3696. spin_lock_irqsave(&info->netlock, flags);
  3697. info->netcount=0;
  3698. spin_unlock_irqrestore(&info->netlock, flags);
  3699. return 0;
  3700. }
  3701. /**
  3702. * called by network layer to process IOCTL call to network device
  3703. *
  3704. * dev pointer to network device structure
  3705. * ifr pointer to network interface request structure
  3706. * cmd IOCTL command code
  3707. *
  3708. * returns 0 if success, otherwise error code
  3709. */
  3710. static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  3711. {
  3712. const size_t size = sizeof(sync_serial_settings);
  3713. sync_serial_settings new_line;
  3714. sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
  3715. MGSLPC_INFO *info = dev_to_port(dev);
  3716. unsigned int flags;
  3717. if (debug_level >= DEBUG_LEVEL_INFO)
  3718. printk("%s:hdlcdev_ioctl(%s)\n",__FILE__,dev->name);
  3719. /* return error if TTY interface open */
  3720. if (info->count)
  3721. return -EBUSY;
  3722. if (cmd != SIOCWANDEV)
  3723. return hdlc_ioctl(dev, ifr, cmd);
  3724. switch(ifr->ifr_settings.type) {
  3725. case IF_GET_IFACE: /* return current sync_serial_settings */
  3726. ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
  3727. if (ifr->ifr_settings.size < size) {
  3728. ifr->ifr_settings.size = size; /* data size wanted */
  3729. return -ENOBUFS;
  3730. }
  3731. flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
  3732. HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
  3733. HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
  3734. HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
  3735. switch (flags){
  3736. case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
  3737. case (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_INT; break;
  3738. case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_TXINT; break;
  3739. case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
  3740. default: new_line.clock_type = CLOCK_DEFAULT;
  3741. }
  3742. new_line.clock_rate = info->params.clock_speed;
  3743. new_line.loopback = info->params.loopback ? 1:0;
  3744. if (copy_to_user(line, &new_line, size))
  3745. return -EFAULT;
  3746. return 0;
  3747. case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
  3748. if(!capable(CAP_NET_ADMIN))
  3749. return -EPERM;
  3750. if (copy_from_user(&new_line, line, size))
  3751. return -EFAULT;
  3752. switch (new_line.clock_type)
  3753. {
  3754. case CLOCK_EXT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
  3755. case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
  3756. case CLOCK_INT: flags = HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG; break;
  3757. case CLOCK_TXINT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG; break;
  3758. case CLOCK_DEFAULT: flags = info->params.flags &
  3759. (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
  3760. HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
  3761. HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
  3762. HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN); break;
  3763. default: return -EINVAL;
  3764. }
  3765. if (new_line.loopback != 0 && new_line.loopback != 1)
  3766. return -EINVAL;
  3767. info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
  3768. HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
  3769. HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
  3770. HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
  3771. info->params.flags |= flags;
  3772. info->params.loopback = new_line.loopback;
  3773. if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
  3774. info->params.clock_speed = new_line.clock_rate;
  3775. else
  3776. info->params.clock_speed = 0;
  3777. /* if network interface up, reprogram hardware */
  3778. if (info->netcount)
  3779. mgslpc_program_hw(info);
  3780. return 0;
  3781. default:
  3782. return hdlc_ioctl(dev, ifr, cmd);
  3783. }
  3784. }
  3785. /**
  3786. * called by network layer when transmit timeout is detected
  3787. *
  3788. * dev pointer to network device structure
  3789. */
  3790. static void hdlcdev_tx_timeout(struct net_device *dev)
  3791. {
  3792. MGSLPC_INFO *info = dev_to_port(dev);
  3793. struct net_device_stats *stats = hdlc_stats(dev);
  3794. unsigned long flags;
  3795. if (debug_level >= DEBUG_LEVEL_INFO)
  3796. printk("hdlcdev_tx_timeout(%s)\n",dev->name);
  3797. stats->tx_errors++;
  3798. stats->tx_aborted_errors++;
  3799. spin_lock_irqsave(&info->lock,flags);
  3800. tx_stop(info);
  3801. spin_unlock_irqrestore(&info->lock,flags);
  3802. netif_wake_queue(dev);
  3803. }
  3804. /**
  3805. * called by device driver when transmit completes
  3806. * reenable network layer transmit if stopped
  3807. *
  3808. * info pointer to device instance information
  3809. */
  3810. static void hdlcdev_tx_done(MGSLPC_INFO *info)
  3811. {
  3812. if (netif_queue_stopped(info->netdev))
  3813. netif_wake_queue(info->netdev);
  3814. }
  3815. /**
  3816. * called by device driver when frame received
  3817. * pass frame to network layer
  3818. *
  3819. * info pointer to device instance information
  3820. * buf pointer to buffer contianing frame data
  3821. * size count of data bytes in buf
  3822. */
  3823. static void hdlcdev_rx(MGSLPC_INFO *info, char *buf, int size)
  3824. {
  3825. struct sk_buff *skb = dev_alloc_skb(size);
  3826. struct net_device *dev = info->netdev;
  3827. struct net_device_stats *stats = hdlc_stats(dev);
  3828. if (debug_level >= DEBUG_LEVEL_INFO)
  3829. printk("hdlcdev_rx(%s)\n",dev->name);
  3830. if (skb == NULL) {
  3831. printk(KERN_NOTICE "%s: can't alloc skb, dropping packet\n", dev->name);
  3832. stats->rx_dropped++;
  3833. return;
  3834. }
  3835. memcpy(skb_put(skb, size),buf,size);
  3836. skb->protocol = hdlc_type_trans(skb, info->netdev);
  3837. stats->rx_packets++;
  3838. stats->rx_bytes += size;
  3839. netif_rx(skb);
  3840. info->netdev->last_rx = jiffies;
  3841. }
  3842. /**
  3843. * called by device driver when adding device instance
  3844. * do generic HDLC initialization
  3845. *
  3846. * info pointer to device instance information
  3847. *
  3848. * returns 0 if success, otherwise error code
  3849. */
  3850. static int hdlcdev_init(MGSLPC_INFO *info)
  3851. {
  3852. int rc;
  3853. struct net_device *dev;
  3854. hdlc_device *hdlc;
  3855. /* allocate and initialize network and HDLC layer objects */
  3856. if (!(dev = alloc_hdlcdev(info))) {
  3857. printk(KERN_ERR "%s:hdlc device allocation failure\n",__FILE__);
  3858. return -ENOMEM;
  3859. }
  3860. /* for network layer reporting purposes only */
  3861. dev->base_addr = info->io_base;
  3862. dev->irq = info->irq_level;
  3863. /* network layer callbacks and settings */
  3864. dev->do_ioctl = hdlcdev_ioctl;
  3865. dev->open = hdlcdev_open;
  3866. dev->stop = hdlcdev_close;
  3867. dev->tx_timeout = hdlcdev_tx_timeout;
  3868. dev->watchdog_timeo = 10*HZ;
  3869. dev->tx_queue_len = 50;
  3870. /* generic HDLC layer callbacks and settings */
  3871. hdlc = dev_to_hdlc(dev);
  3872. hdlc->attach = hdlcdev_attach;
  3873. hdlc->xmit = hdlcdev_xmit;
  3874. /* register objects with HDLC layer */
  3875. if ((rc = register_hdlc_device(dev))) {
  3876. printk(KERN_WARNING "%s:unable to register hdlc device\n",__FILE__);
  3877. free_netdev(dev);
  3878. return rc;
  3879. }
  3880. info->netdev = dev;
  3881. return 0;
  3882. }
  3883. /**
  3884. * called by device driver when removing device instance
  3885. * do generic HDLC cleanup
  3886. *
  3887. * info pointer to device instance information
  3888. */
  3889. static void hdlcdev_exit(MGSLPC_INFO *info)
  3890. {
  3891. unregister_hdlc_device(info->netdev);
  3892. free_netdev(info->netdev);
  3893. info->netdev = NULL;
  3894. }
  3895. #endif /* CONFIG_HDLC */