synclink_cs.c 109 KB

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