synclink_cs.c 109 KB

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