synclink_cs.c 115 KB

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