synclink_cs.c 115 KB

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