synclink_cs.c 112 KB

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