synclink_cs.c 109 KB

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