synclink_cs.c 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341
  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 (info->port.flags & ASYNC_CHECK_CD) {
  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 (info->port.flags & ASYNC_INITIALIZED)
  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. info->port.flags |= ASYNC_INITIALIZED;
  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 (!(info->port.flags & ASYNC_INITIALIZED))
  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 || tty->termios.c_cflag & HUPCL) {
  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. info->port.flags &= ~ASYNC_INITIALIZED;
  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 && (tty->termios.c_cflag & CREAD)))
  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. if (cflag & CRTSCTS)
  1211. info->port.flags |= ASYNC_CTS_FLOW;
  1212. else
  1213. info->port.flags &= ~ASYNC_CTS_FLOW;
  1214. if (cflag & CLOCAL)
  1215. info->port.flags &= ~ASYNC_CHECK_CD;
  1216. else
  1217. info->port.flags |= ASYNC_CHECK_CD;
  1218. /* process tty input control flags */
  1219. info->read_status_mask = 0;
  1220. if (I_INPCK(tty))
  1221. info->read_status_mask |= BIT7 | BIT6;
  1222. if (I_IGNPAR(tty))
  1223. info->ignore_status_mask |= BIT7 | BIT6;
  1224. mgslpc_program_hw(info, tty);
  1225. }
  1226. /* Add a character to the transmit buffer
  1227. */
  1228. static int mgslpc_put_char(struct tty_struct *tty, unsigned char ch)
  1229. {
  1230. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1231. unsigned long flags;
  1232. if (debug_level >= DEBUG_LEVEL_INFO) {
  1233. printk("%s(%d):mgslpc_put_char(%d) on %s\n",
  1234. __FILE__, __LINE__, ch, info->device_name);
  1235. }
  1236. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_put_char"))
  1237. return 0;
  1238. if (!info->tx_buf)
  1239. return 0;
  1240. spin_lock_irqsave(&info->lock, flags);
  1241. if (info->params.mode == MGSL_MODE_ASYNC || !info->tx_active) {
  1242. if (info->tx_count < TXBUFSIZE - 1) {
  1243. info->tx_buf[info->tx_put++] = ch;
  1244. info->tx_put &= TXBUFSIZE-1;
  1245. info->tx_count++;
  1246. }
  1247. }
  1248. spin_unlock_irqrestore(&info->lock, flags);
  1249. return 1;
  1250. }
  1251. /* Enable transmitter so remaining characters in the
  1252. * transmit buffer are sent.
  1253. */
  1254. static void mgslpc_flush_chars(struct tty_struct *tty)
  1255. {
  1256. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1257. unsigned long flags;
  1258. if (debug_level >= DEBUG_LEVEL_INFO)
  1259. printk("%s(%d):mgslpc_flush_chars() entry on %s tx_count=%d\n",
  1260. __FILE__, __LINE__, info->device_name, info->tx_count);
  1261. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_chars"))
  1262. return;
  1263. if (info->tx_count <= 0 || tty->stopped ||
  1264. tty->hw_stopped || !info->tx_buf)
  1265. return;
  1266. if (debug_level >= DEBUG_LEVEL_INFO)
  1267. printk("%s(%d):mgslpc_flush_chars() entry on %s starting transmitter\n",
  1268. __FILE__, __LINE__, info->device_name);
  1269. spin_lock_irqsave(&info->lock, flags);
  1270. if (!info->tx_active)
  1271. tx_start(info, tty);
  1272. spin_unlock_irqrestore(&info->lock, flags);
  1273. }
  1274. /* Send a block of data
  1275. *
  1276. * Arguments:
  1277. *
  1278. * tty pointer to tty information structure
  1279. * buf pointer to buffer containing send data
  1280. * count size of send data in bytes
  1281. *
  1282. * Returns: number of characters written
  1283. */
  1284. static int mgslpc_write(struct tty_struct * tty,
  1285. const unsigned char *buf, int count)
  1286. {
  1287. int c, ret = 0;
  1288. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1289. unsigned long flags;
  1290. if (debug_level >= DEBUG_LEVEL_INFO)
  1291. printk("%s(%d):mgslpc_write(%s) count=%d\n",
  1292. __FILE__, __LINE__, info->device_name, count);
  1293. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write") ||
  1294. !info->tx_buf)
  1295. goto cleanup;
  1296. if (info->params.mode == MGSL_MODE_HDLC) {
  1297. if (count > TXBUFSIZE) {
  1298. ret = -EIO;
  1299. goto cleanup;
  1300. }
  1301. if (info->tx_active)
  1302. goto cleanup;
  1303. else if (info->tx_count)
  1304. goto start;
  1305. }
  1306. for (;;) {
  1307. c = min(count,
  1308. min(TXBUFSIZE - info->tx_count - 1,
  1309. TXBUFSIZE - info->tx_put));
  1310. if (c <= 0)
  1311. break;
  1312. memcpy(info->tx_buf + info->tx_put, buf, c);
  1313. spin_lock_irqsave(&info->lock, flags);
  1314. info->tx_put = (info->tx_put + c) & (TXBUFSIZE-1);
  1315. info->tx_count += c;
  1316. spin_unlock_irqrestore(&info->lock, flags);
  1317. buf += c;
  1318. count -= c;
  1319. ret += c;
  1320. }
  1321. start:
  1322. if (info->tx_count && !tty->stopped && !tty->hw_stopped) {
  1323. spin_lock_irqsave(&info->lock, flags);
  1324. if (!info->tx_active)
  1325. tx_start(info, tty);
  1326. spin_unlock_irqrestore(&info->lock, flags);
  1327. }
  1328. cleanup:
  1329. if (debug_level >= DEBUG_LEVEL_INFO)
  1330. printk("%s(%d):mgslpc_write(%s) returning=%d\n",
  1331. __FILE__, __LINE__, info->device_name, ret);
  1332. return ret;
  1333. }
  1334. /* Return the count of free bytes in transmit buffer
  1335. */
  1336. static int mgslpc_write_room(struct tty_struct *tty)
  1337. {
  1338. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1339. int ret;
  1340. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_write_room"))
  1341. return 0;
  1342. if (info->params.mode == MGSL_MODE_HDLC) {
  1343. /* HDLC (frame oriented) mode */
  1344. if (info->tx_active)
  1345. return 0;
  1346. else
  1347. return HDLC_MAX_FRAME_SIZE;
  1348. } else {
  1349. ret = TXBUFSIZE - info->tx_count - 1;
  1350. if (ret < 0)
  1351. ret = 0;
  1352. }
  1353. if (debug_level >= DEBUG_LEVEL_INFO)
  1354. printk("%s(%d):mgslpc_write_room(%s)=%d\n",
  1355. __FILE__, __LINE__, info->device_name, ret);
  1356. return ret;
  1357. }
  1358. /* Return the count of bytes in transmit buffer
  1359. */
  1360. static int mgslpc_chars_in_buffer(struct tty_struct *tty)
  1361. {
  1362. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1363. int rc;
  1364. if (debug_level >= DEBUG_LEVEL_INFO)
  1365. printk("%s(%d):mgslpc_chars_in_buffer(%s)\n",
  1366. __FILE__, __LINE__, info->device_name);
  1367. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_chars_in_buffer"))
  1368. return 0;
  1369. if (info->params.mode == MGSL_MODE_HDLC)
  1370. rc = info->tx_active ? info->max_frame_size : 0;
  1371. else
  1372. rc = info->tx_count;
  1373. if (debug_level >= DEBUG_LEVEL_INFO)
  1374. printk("%s(%d):mgslpc_chars_in_buffer(%s)=%d\n",
  1375. __FILE__, __LINE__, info->device_name, rc);
  1376. return rc;
  1377. }
  1378. /* Discard all data in the send buffer
  1379. */
  1380. static void mgslpc_flush_buffer(struct tty_struct *tty)
  1381. {
  1382. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1383. unsigned long flags;
  1384. if (debug_level >= DEBUG_LEVEL_INFO)
  1385. printk("%s(%d):mgslpc_flush_buffer(%s) entry\n",
  1386. __FILE__, __LINE__, info->device_name);
  1387. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_flush_buffer"))
  1388. return;
  1389. spin_lock_irqsave(&info->lock, flags);
  1390. info->tx_count = info->tx_put = info->tx_get = 0;
  1391. del_timer(&info->tx_timer);
  1392. spin_unlock_irqrestore(&info->lock, flags);
  1393. wake_up_interruptible(&tty->write_wait);
  1394. tty_wakeup(tty);
  1395. }
  1396. /* Send a high-priority XON/XOFF character
  1397. */
  1398. static void mgslpc_send_xchar(struct tty_struct *tty, char ch)
  1399. {
  1400. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1401. unsigned long flags;
  1402. if (debug_level >= DEBUG_LEVEL_INFO)
  1403. printk("%s(%d):mgslpc_send_xchar(%s,%d)\n",
  1404. __FILE__, __LINE__, info->device_name, ch);
  1405. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_send_xchar"))
  1406. return;
  1407. info->x_char = ch;
  1408. if (ch) {
  1409. spin_lock_irqsave(&info->lock, flags);
  1410. if (!info->tx_enabled)
  1411. tx_start(info, tty);
  1412. spin_unlock_irqrestore(&info->lock, flags);
  1413. }
  1414. }
  1415. /* Signal remote device to throttle send data (our receive data)
  1416. */
  1417. static void mgslpc_throttle(struct tty_struct * tty)
  1418. {
  1419. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1420. unsigned long flags;
  1421. if (debug_level >= DEBUG_LEVEL_INFO)
  1422. printk("%s(%d):mgslpc_throttle(%s) entry\n",
  1423. __FILE__, __LINE__, info->device_name);
  1424. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_throttle"))
  1425. return;
  1426. if (I_IXOFF(tty))
  1427. mgslpc_send_xchar(tty, STOP_CHAR(tty));
  1428. if (tty->termios.c_cflag & CRTSCTS) {
  1429. spin_lock_irqsave(&info->lock, flags);
  1430. info->serial_signals &= ~SerialSignal_RTS;
  1431. set_signals(info);
  1432. spin_unlock_irqrestore(&info->lock, flags);
  1433. }
  1434. }
  1435. /* Signal remote device to stop throttling send data (our receive data)
  1436. */
  1437. static void mgslpc_unthrottle(struct tty_struct * tty)
  1438. {
  1439. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1440. unsigned long flags;
  1441. if (debug_level >= DEBUG_LEVEL_INFO)
  1442. printk("%s(%d):mgslpc_unthrottle(%s) entry\n",
  1443. __FILE__, __LINE__, info->device_name);
  1444. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_unthrottle"))
  1445. return;
  1446. if (I_IXOFF(tty)) {
  1447. if (info->x_char)
  1448. info->x_char = 0;
  1449. else
  1450. mgslpc_send_xchar(tty, START_CHAR(tty));
  1451. }
  1452. if (tty->termios.c_cflag & CRTSCTS) {
  1453. spin_lock_irqsave(&info->lock, flags);
  1454. info->serial_signals |= SerialSignal_RTS;
  1455. set_signals(info);
  1456. spin_unlock_irqrestore(&info->lock, flags);
  1457. }
  1458. }
  1459. /* get the current serial statistics
  1460. */
  1461. static int get_stats(MGSLPC_INFO * info, struct mgsl_icount __user *user_icount)
  1462. {
  1463. int err;
  1464. if (debug_level >= DEBUG_LEVEL_INFO)
  1465. printk("get_params(%s)\n", info->device_name);
  1466. if (!user_icount) {
  1467. memset(&info->icount, 0, sizeof(info->icount));
  1468. } else {
  1469. COPY_TO_USER(err, user_icount, &info->icount, sizeof(struct mgsl_icount));
  1470. if (err)
  1471. return -EFAULT;
  1472. }
  1473. return 0;
  1474. }
  1475. /* get the current serial parameters
  1476. */
  1477. static int get_params(MGSLPC_INFO * info, MGSL_PARAMS __user *user_params)
  1478. {
  1479. int err;
  1480. if (debug_level >= DEBUG_LEVEL_INFO)
  1481. printk("get_params(%s)\n", info->device_name);
  1482. COPY_TO_USER(err,user_params, &info->params, sizeof(MGSL_PARAMS));
  1483. if (err)
  1484. return -EFAULT;
  1485. return 0;
  1486. }
  1487. /* set the serial parameters
  1488. *
  1489. * Arguments:
  1490. *
  1491. * info pointer to device instance data
  1492. * new_params user buffer containing new serial params
  1493. *
  1494. * Returns: 0 if success, otherwise error code
  1495. */
  1496. static int set_params(MGSLPC_INFO * info, MGSL_PARAMS __user *new_params, struct tty_struct *tty)
  1497. {
  1498. unsigned long flags;
  1499. MGSL_PARAMS tmp_params;
  1500. int err;
  1501. if (debug_level >= DEBUG_LEVEL_INFO)
  1502. printk("%s(%d):set_params %s\n", __FILE__,__LINE__,
  1503. info->device_name);
  1504. COPY_FROM_USER(err,&tmp_params, new_params, sizeof(MGSL_PARAMS));
  1505. if (err) {
  1506. if (debug_level >= DEBUG_LEVEL_INFO)
  1507. printk("%s(%d):set_params(%s) user buffer copy failed\n",
  1508. __FILE__, __LINE__, info->device_name);
  1509. return -EFAULT;
  1510. }
  1511. spin_lock_irqsave(&info->lock, flags);
  1512. memcpy(&info->params,&tmp_params,sizeof(MGSL_PARAMS));
  1513. spin_unlock_irqrestore(&info->lock, flags);
  1514. mgslpc_change_params(info, tty);
  1515. return 0;
  1516. }
  1517. static int get_txidle(MGSLPC_INFO * info, int __user *idle_mode)
  1518. {
  1519. int err;
  1520. if (debug_level >= DEBUG_LEVEL_INFO)
  1521. printk("get_txidle(%s)=%d\n", info->device_name, info->idle_mode);
  1522. COPY_TO_USER(err,idle_mode, &info->idle_mode, sizeof(int));
  1523. if (err)
  1524. return -EFAULT;
  1525. return 0;
  1526. }
  1527. static int set_txidle(MGSLPC_INFO * info, int idle_mode)
  1528. {
  1529. unsigned long flags;
  1530. if (debug_level >= DEBUG_LEVEL_INFO)
  1531. printk("set_txidle(%s,%d)\n", info->device_name, idle_mode);
  1532. spin_lock_irqsave(&info->lock, flags);
  1533. info->idle_mode = idle_mode;
  1534. tx_set_idle(info);
  1535. spin_unlock_irqrestore(&info->lock, flags);
  1536. return 0;
  1537. }
  1538. static int get_interface(MGSLPC_INFO * info, int __user *if_mode)
  1539. {
  1540. int err;
  1541. if (debug_level >= DEBUG_LEVEL_INFO)
  1542. printk("get_interface(%s)=%d\n", info->device_name, info->if_mode);
  1543. COPY_TO_USER(err,if_mode, &info->if_mode, sizeof(int));
  1544. if (err)
  1545. return -EFAULT;
  1546. return 0;
  1547. }
  1548. static int set_interface(MGSLPC_INFO * info, int if_mode)
  1549. {
  1550. unsigned long flags;
  1551. unsigned char val;
  1552. if (debug_level >= DEBUG_LEVEL_INFO)
  1553. printk("set_interface(%s,%d)\n", info->device_name, if_mode);
  1554. spin_lock_irqsave(&info->lock, flags);
  1555. info->if_mode = if_mode;
  1556. val = read_reg(info, PVR) & 0x0f;
  1557. switch (info->if_mode)
  1558. {
  1559. case MGSL_INTERFACE_RS232: val |= PVR_RS232; break;
  1560. case MGSL_INTERFACE_V35: val |= PVR_V35; break;
  1561. case MGSL_INTERFACE_RS422: val |= PVR_RS422; break;
  1562. }
  1563. write_reg(info, PVR, val);
  1564. spin_unlock_irqrestore(&info->lock, flags);
  1565. return 0;
  1566. }
  1567. static int set_txenable(MGSLPC_INFO * info, int enable, struct tty_struct *tty)
  1568. {
  1569. unsigned long flags;
  1570. if (debug_level >= DEBUG_LEVEL_INFO)
  1571. printk("set_txenable(%s,%d)\n", info->device_name, enable);
  1572. spin_lock_irqsave(&info->lock, flags);
  1573. if (enable) {
  1574. if (!info->tx_enabled)
  1575. tx_start(info, tty);
  1576. } else {
  1577. if (info->tx_enabled)
  1578. tx_stop(info);
  1579. }
  1580. spin_unlock_irqrestore(&info->lock, flags);
  1581. return 0;
  1582. }
  1583. static int tx_abort(MGSLPC_INFO * info)
  1584. {
  1585. unsigned long flags;
  1586. if (debug_level >= DEBUG_LEVEL_INFO)
  1587. printk("tx_abort(%s)\n", info->device_name);
  1588. spin_lock_irqsave(&info->lock, flags);
  1589. if (info->tx_active && info->tx_count &&
  1590. info->params.mode == MGSL_MODE_HDLC) {
  1591. /* clear data count so FIFO is not filled on next IRQ.
  1592. * This results in underrun and abort transmission.
  1593. */
  1594. info->tx_count = info->tx_put = info->tx_get = 0;
  1595. info->tx_aborting = true;
  1596. }
  1597. spin_unlock_irqrestore(&info->lock, flags);
  1598. return 0;
  1599. }
  1600. static int set_rxenable(MGSLPC_INFO * info, int enable)
  1601. {
  1602. unsigned long flags;
  1603. if (debug_level >= DEBUG_LEVEL_INFO)
  1604. printk("set_rxenable(%s,%d)\n", info->device_name, enable);
  1605. spin_lock_irqsave(&info->lock, flags);
  1606. if (enable) {
  1607. if (!info->rx_enabled)
  1608. rx_start(info);
  1609. } else {
  1610. if (info->rx_enabled)
  1611. rx_stop(info);
  1612. }
  1613. spin_unlock_irqrestore(&info->lock, flags);
  1614. return 0;
  1615. }
  1616. /* wait for specified event to occur
  1617. *
  1618. * Arguments: info pointer to device instance data
  1619. * mask pointer to bitmask of events to wait for
  1620. * Return Value: 0 if successful and bit mask updated with
  1621. * of events triggerred,
  1622. * otherwise error code
  1623. */
  1624. static int wait_events(MGSLPC_INFO * info, int __user *mask_ptr)
  1625. {
  1626. unsigned long flags;
  1627. int s;
  1628. int rc=0;
  1629. struct mgsl_icount cprev, cnow;
  1630. int events;
  1631. int mask;
  1632. struct _input_signal_events oldsigs, newsigs;
  1633. DECLARE_WAITQUEUE(wait, current);
  1634. COPY_FROM_USER(rc,&mask, mask_ptr, sizeof(int));
  1635. if (rc)
  1636. return -EFAULT;
  1637. if (debug_level >= DEBUG_LEVEL_INFO)
  1638. printk("wait_events(%s,%d)\n", info->device_name, mask);
  1639. spin_lock_irqsave(&info->lock, flags);
  1640. /* return immediately if state matches requested events */
  1641. get_signals(info);
  1642. s = info->serial_signals;
  1643. events = mask &
  1644. ( ((s & SerialSignal_DSR) ? MgslEvent_DsrActive:MgslEvent_DsrInactive) +
  1645. ((s & SerialSignal_DCD) ? MgslEvent_DcdActive:MgslEvent_DcdInactive) +
  1646. ((s & SerialSignal_CTS) ? MgslEvent_CtsActive:MgslEvent_CtsInactive) +
  1647. ((s & SerialSignal_RI) ? MgslEvent_RiActive :MgslEvent_RiInactive) );
  1648. if (events) {
  1649. spin_unlock_irqrestore(&info->lock, flags);
  1650. goto exit;
  1651. }
  1652. /* save current irq counts */
  1653. cprev = info->icount;
  1654. oldsigs = info->input_signal_events;
  1655. if ((info->params.mode == MGSL_MODE_HDLC) &&
  1656. (mask & MgslEvent_ExitHuntMode))
  1657. irq_enable(info, CHA, IRQ_EXITHUNT);
  1658. set_current_state(TASK_INTERRUPTIBLE);
  1659. add_wait_queue(&info->event_wait_q, &wait);
  1660. spin_unlock_irqrestore(&info->lock, flags);
  1661. for(;;) {
  1662. schedule();
  1663. if (signal_pending(current)) {
  1664. rc = -ERESTARTSYS;
  1665. break;
  1666. }
  1667. /* get current irq counts */
  1668. spin_lock_irqsave(&info->lock, flags);
  1669. cnow = info->icount;
  1670. newsigs = info->input_signal_events;
  1671. set_current_state(TASK_INTERRUPTIBLE);
  1672. spin_unlock_irqrestore(&info->lock, flags);
  1673. /* if no change, wait aborted for some reason */
  1674. if (newsigs.dsr_up == oldsigs.dsr_up &&
  1675. newsigs.dsr_down == oldsigs.dsr_down &&
  1676. newsigs.dcd_up == oldsigs.dcd_up &&
  1677. newsigs.dcd_down == oldsigs.dcd_down &&
  1678. newsigs.cts_up == oldsigs.cts_up &&
  1679. newsigs.cts_down == oldsigs.cts_down &&
  1680. newsigs.ri_up == oldsigs.ri_up &&
  1681. newsigs.ri_down == oldsigs.ri_down &&
  1682. cnow.exithunt == cprev.exithunt &&
  1683. cnow.rxidle == cprev.rxidle) {
  1684. rc = -EIO;
  1685. break;
  1686. }
  1687. events = mask &
  1688. ( (newsigs.dsr_up != oldsigs.dsr_up ? MgslEvent_DsrActive:0) +
  1689. (newsigs.dsr_down != oldsigs.dsr_down ? MgslEvent_DsrInactive:0) +
  1690. (newsigs.dcd_up != oldsigs.dcd_up ? MgslEvent_DcdActive:0) +
  1691. (newsigs.dcd_down != oldsigs.dcd_down ? MgslEvent_DcdInactive:0) +
  1692. (newsigs.cts_up != oldsigs.cts_up ? MgslEvent_CtsActive:0) +
  1693. (newsigs.cts_down != oldsigs.cts_down ? MgslEvent_CtsInactive:0) +
  1694. (newsigs.ri_up != oldsigs.ri_up ? MgslEvent_RiActive:0) +
  1695. (newsigs.ri_down != oldsigs.ri_down ? MgslEvent_RiInactive:0) +
  1696. (cnow.exithunt != cprev.exithunt ? MgslEvent_ExitHuntMode:0) +
  1697. (cnow.rxidle != cprev.rxidle ? MgslEvent_IdleReceived:0) );
  1698. if (events)
  1699. break;
  1700. cprev = cnow;
  1701. oldsigs = newsigs;
  1702. }
  1703. remove_wait_queue(&info->event_wait_q, &wait);
  1704. set_current_state(TASK_RUNNING);
  1705. if (mask & MgslEvent_ExitHuntMode) {
  1706. spin_lock_irqsave(&info->lock, flags);
  1707. if (!waitqueue_active(&info->event_wait_q))
  1708. irq_disable(info, CHA, IRQ_EXITHUNT);
  1709. spin_unlock_irqrestore(&info->lock, flags);
  1710. }
  1711. exit:
  1712. if (rc == 0)
  1713. PUT_USER(rc, events, mask_ptr);
  1714. return rc;
  1715. }
  1716. static int modem_input_wait(MGSLPC_INFO *info,int arg)
  1717. {
  1718. unsigned long flags;
  1719. int rc;
  1720. struct mgsl_icount cprev, cnow;
  1721. DECLARE_WAITQUEUE(wait, current);
  1722. /* save current irq counts */
  1723. spin_lock_irqsave(&info->lock, flags);
  1724. cprev = info->icount;
  1725. add_wait_queue(&info->status_event_wait_q, &wait);
  1726. set_current_state(TASK_INTERRUPTIBLE);
  1727. spin_unlock_irqrestore(&info->lock, flags);
  1728. for(;;) {
  1729. schedule();
  1730. if (signal_pending(current)) {
  1731. rc = -ERESTARTSYS;
  1732. break;
  1733. }
  1734. /* get new irq counts */
  1735. spin_lock_irqsave(&info->lock, flags);
  1736. cnow = info->icount;
  1737. set_current_state(TASK_INTERRUPTIBLE);
  1738. spin_unlock_irqrestore(&info->lock, flags);
  1739. /* if no change, wait aborted for some reason */
  1740. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  1741. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts) {
  1742. rc = -EIO;
  1743. break;
  1744. }
  1745. /* check for change in caller specified modem input */
  1746. if ((arg & TIOCM_RNG && cnow.rng != cprev.rng) ||
  1747. (arg & TIOCM_DSR && cnow.dsr != cprev.dsr) ||
  1748. (arg & TIOCM_CD && cnow.dcd != cprev.dcd) ||
  1749. (arg & TIOCM_CTS && cnow.cts != cprev.cts)) {
  1750. rc = 0;
  1751. break;
  1752. }
  1753. cprev = cnow;
  1754. }
  1755. remove_wait_queue(&info->status_event_wait_q, &wait);
  1756. set_current_state(TASK_RUNNING);
  1757. return rc;
  1758. }
  1759. /* return the state of the serial control and status signals
  1760. */
  1761. static int tiocmget(struct tty_struct *tty)
  1762. {
  1763. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1764. unsigned int result;
  1765. unsigned long flags;
  1766. spin_lock_irqsave(&info->lock, flags);
  1767. get_signals(info);
  1768. spin_unlock_irqrestore(&info->lock, flags);
  1769. result = ((info->serial_signals & SerialSignal_RTS) ? TIOCM_RTS:0) +
  1770. ((info->serial_signals & SerialSignal_DTR) ? TIOCM_DTR:0) +
  1771. ((info->serial_signals & SerialSignal_DCD) ? TIOCM_CAR:0) +
  1772. ((info->serial_signals & SerialSignal_RI) ? TIOCM_RNG:0) +
  1773. ((info->serial_signals & SerialSignal_DSR) ? TIOCM_DSR:0) +
  1774. ((info->serial_signals & SerialSignal_CTS) ? TIOCM_CTS:0);
  1775. if (debug_level >= DEBUG_LEVEL_INFO)
  1776. printk("%s(%d):%s tiocmget() value=%08X\n",
  1777. __FILE__, __LINE__, info->device_name, result);
  1778. return result;
  1779. }
  1780. /* set modem control signals (DTR/RTS)
  1781. */
  1782. static int tiocmset(struct tty_struct *tty,
  1783. unsigned int set, unsigned int clear)
  1784. {
  1785. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1786. unsigned long flags;
  1787. if (debug_level >= DEBUG_LEVEL_INFO)
  1788. printk("%s(%d):%s tiocmset(%x,%x)\n",
  1789. __FILE__, __LINE__, info->device_name, set, clear);
  1790. if (set & TIOCM_RTS)
  1791. info->serial_signals |= SerialSignal_RTS;
  1792. if (set & TIOCM_DTR)
  1793. info->serial_signals |= SerialSignal_DTR;
  1794. if (clear & TIOCM_RTS)
  1795. info->serial_signals &= ~SerialSignal_RTS;
  1796. if (clear & TIOCM_DTR)
  1797. info->serial_signals &= ~SerialSignal_DTR;
  1798. spin_lock_irqsave(&info->lock, flags);
  1799. set_signals(info);
  1800. spin_unlock_irqrestore(&info->lock, flags);
  1801. return 0;
  1802. }
  1803. /* Set or clear transmit break condition
  1804. *
  1805. * Arguments: tty pointer to tty instance data
  1806. * break_state -1=set break condition, 0=clear
  1807. */
  1808. static int mgslpc_break(struct tty_struct *tty, int break_state)
  1809. {
  1810. MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
  1811. unsigned long flags;
  1812. if (debug_level >= DEBUG_LEVEL_INFO)
  1813. printk("%s(%d):mgslpc_break(%s,%d)\n",
  1814. __FILE__, __LINE__, info->device_name, break_state);
  1815. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_break"))
  1816. return -EINVAL;
  1817. spin_lock_irqsave(&info->lock, flags);
  1818. if (break_state == -1)
  1819. set_reg_bits(info, CHA+DAFO, BIT6);
  1820. else
  1821. clear_reg_bits(info, CHA+DAFO, BIT6);
  1822. spin_unlock_irqrestore(&info->lock, flags);
  1823. return 0;
  1824. }
  1825. static int mgslpc_get_icount(struct tty_struct *tty,
  1826. struct serial_icounter_struct *icount)
  1827. {
  1828. MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
  1829. struct mgsl_icount cnow; /* kernel counter temps */
  1830. unsigned long flags;
  1831. spin_lock_irqsave(&info->lock, flags);
  1832. cnow = info->icount;
  1833. spin_unlock_irqrestore(&info->lock, flags);
  1834. icount->cts = cnow.cts;
  1835. icount->dsr = cnow.dsr;
  1836. icount->rng = cnow.rng;
  1837. icount->dcd = cnow.dcd;
  1838. icount->rx = cnow.rx;
  1839. icount->tx = cnow.tx;
  1840. icount->frame = cnow.frame;
  1841. icount->overrun = cnow.overrun;
  1842. icount->parity = cnow.parity;
  1843. icount->brk = cnow.brk;
  1844. icount->buf_overrun = cnow.buf_overrun;
  1845. return 0;
  1846. }
  1847. /* Service an IOCTL request
  1848. *
  1849. * Arguments:
  1850. *
  1851. * tty pointer to tty instance data
  1852. * cmd IOCTL command code
  1853. * arg command argument/context
  1854. *
  1855. * Return Value: 0 if success, otherwise error code
  1856. */
  1857. static int mgslpc_ioctl(struct tty_struct *tty,
  1858. unsigned int cmd, unsigned long arg)
  1859. {
  1860. MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
  1861. void __user *argp = (void __user *)arg;
  1862. if (debug_level >= DEBUG_LEVEL_INFO)
  1863. printk("%s(%d):mgslpc_ioctl %s cmd=%08X\n", __FILE__, __LINE__,
  1864. info->device_name, cmd);
  1865. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_ioctl"))
  1866. return -ENODEV;
  1867. if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
  1868. (cmd != TIOCMIWAIT)) {
  1869. if (tty->flags & (1 << TTY_IO_ERROR))
  1870. return -EIO;
  1871. }
  1872. switch (cmd) {
  1873. case MGSL_IOCGPARAMS:
  1874. return get_params(info, argp);
  1875. case MGSL_IOCSPARAMS:
  1876. return set_params(info, argp, tty);
  1877. case MGSL_IOCGTXIDLE:
  1878. return get_txidle(info, argp);
  1879. case MGSL_IOCSTXIDLE:
  1880. return set_txidle(info, (int)arg);
  1881. case MGSL_IOCGIF:
  1882. return get_interface(info, argp);
  1883. case MGSL_IOCSIF:
  1884. return set_interface(info,(int)arg);
  1885. case MGSL_IOCTXENABLE:
  1886. return set_txenable(info,(int)arg, tty);
  1887. case MGSL_IOCRXENABLE:
  1888. return set_rxenable(info,(int)arg);
  1889. case MGSL_IOCTXABORT:
  1890. return tx_abort(info);
  1891. case MGSL_IOCGSTATS:
  1892. return get_stats(info, argp);
  1893. case MGSL_IOCWAITEVENT:
  1894. return wait_events(info, argp);
  1895. case TIOCMIWAIT:
  1896. return modem_input_wait(info,(int)arg);
  1897. default:
  1898. return -ENOIOCTLCMD;
  1899. }
  1900. return 0;
  1901. }
  1902. /* Set new termios settings
  1903. *
  1904. * Arguments:
  1905. *
  1906. * tty pointer to tty structure
  1907. * termios pointer to buffer to hold returned old termios
  1908. */
  1909. static void mgslpc_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
  1910. {
  1911. MGSLPC_INFO *info = (MGSLPC_INFO *)tty->driver_data;
  1912. unsigned long flags;
  1913. if (debug_level >= DEBUG_LEVEL_INFO)
  1914. printk("%s(%d):mgslpc_set_termios %s\n", __FILE__, __LINE__,
  1915. tty->driver->name);
  1916. /* just return if nothing has changed */
  1917. if ((tty->termios.c_cflag == old_termios->c_cflag)
  1918. && (RELEVANT_IFLAG(tty->termios.c_iflag)
  1919. == RELEVANT_IFLAG(old_termios->c_iflag)))
  1920. return;
  1921. mgslpc_change_params(info, tty);
  1922. /* Handle transition to B0 status */
  1923. if (old_termios->c_cflag & CBAUD &&
  1924. !(tty->termios.c_cflag & CBAUD)) {
  1925. info->serial_signals &= ~(SerialSignal_RTS | SerialSignal_DTR);
  1926. spin_lock_irqsave(&info->lock, flags);
  1927. set_signals(info);
  1928. spin_unlock_irqrestore(&info->lock, flags);
  1929. }
  1930. /* Handle transition away from B0 status */
  1931. if (!(old_termios->c_cflag & CBAUD) &&
  1932. tty->termios.c_cflag & CBAUD) {
  1933. info->serial_signals |= SerialSignal_DTR;
  1934. if (!(tty->termios.c_cflag & CRTSCTS) ||
  1935. !test_bit(TTY_THROTTLED, &tty->flags)) {
  1936. info->serial_signals |= SerialSignal_RTS;
  1937. }
  1938. spin_lock_irqsave(&info->lock, flags);
  1939. set_signals(info);
  1940. spin_unlock_irqrestore(&info->lock, flags);
  1941. }
  1942. /* Handle turning off CRTSCTS */
  1943. if (old_termios->c_cflag & CRTSCTS &&
  1944. !(tty->termios.c_cflag & CRTSCTS)) {
  1945. tty->hw_stopped = 0;
  1946. tx_release(tty);
  1947. }
  1948. }
  1949. static void mgslpc_close(struct tty_struct *tty, struct file * filp)
  1950. {
  1951. MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
  1952. struct tty_port *port = &info->port;
  1953. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_close"))
  1954. return;
  1955. if (debug_level >= DEBUG_LEVEL_INFO)
  1956. printk("%s(%d):mgslpc_close(%s) entry, count=%d\n",
  1957. __FILE__, __LINE__, info->device_name, port->count);
  1958. if (tty_port_close_start(port, tty, filp) == 0)
  1959. goto cleanup;
  1960. if (port->flags & ASYNC_INITIALIZED)
  1961. mgslpc_wait_until_sent(tty, info->timeout);
  1962. mgslpc_flush_buffer(tty);
  1963. tty_ldisc_flush(tty);
  1964. shutdown(info, tty);
  1965. tty_port_close_end(port, tty);
  1966. tty_port_tty_set(port, NULL);
  1967. cleanup:
  1968. if (debug_level >= DEBUG_LEVEL_INFO)
  1969. printk("%s(%d):mgslpc_close(%s) exit, count=%d\n", __FILE__, __LINE__,
  1970. tty->driver->name, port->count);
  1971. }
  1972. /* Wait until the transmitter is empty.
  1973. */
  1974. static void mgslpc_wait_until_sent(struct tty_struct *tty, int timeout)
  1975. {
  1976. MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
  1977. unsigned long orig_jiffies, char_time;
  1978. if (!info)
  1979. return;
  1980. if (debug_level >= DEBUG_LEVEL_INFO)
  1981. printk("%s(%d):mgslpc_wait_until_sent(%s) entry\n",
  1982. __FILE__, __LINE__, info->device_name);
  1983. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_wait_until_sent"))
  1984. return;
  1985. if (!(info->port.flags & ASYNC_INITIALIZED))
  1986. goto exit;
  1987. orig_jiffies = jiffies;
  1988. /* Set check interval to 1/5 of estimated time to
  1989. * send a character, and make it at least 1. The check
  1990. * interval should also be less than the timeout.
  1991. * Note: use tight timings here to satisfy the NIST-PCTS.
  1992. */
  1993. if (info->params.data_rate) {
  1994. char_time = info->timeout/(32 * 5);
  1995. if (!char_time)
  1996. char_time++;
  1997. } else
  1998. char_time = 1;
  1999. if (timeout)
  2000. char_time = min_t(unsigned long, char_time, timeout);
  2001. if (info->params.mode == MGSL_MODE_HDLC) {
  2002. while (info->tx_active) {
  2003. msleep_interruptible(jiffies_to_msecs(char_time));
  2004. if (signal_pending(current))
  2005. break;
  2006. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  2007. break;
  2008. }
  2009. } else {
  2010. while ((info->tx_count || info->tx_active) &&
  2011. info->tx_enabled) {
  2012. msleep_interruptible(jiffies_to_msecs(char_time));
  2013. if (signal_pending(current))
  2014. break;
  2015. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  2016. break;
  2017. }
  2018. }
  2019. exit:
  2020. if (debug_level >= DEBUG_LEVEL_INFO)
  2021. printk("%s(%d):mgslpc_wait_until_sent(%s) exit\n",
  2022. __FILE__, __LINE__, info->device_name);
  2023. }
  2024. /* Called by tty_hangup() when a hangup is signaled.
  2025. * This is the same as closing all open files for the port.
  2026. */
  2027. static void mgslpc_hangup(struct tty_struct *tty)
  2028. {
  2029. MGSLPC_INFO * info = (MGSLPC_INFO *)tty->driver_data;
  2030. if (debug_level >= DEBUG_LEVEL_INFO)
  2031. printk("%s(%d):mgslpc_hangup(%s)\n",
  2032. __FILE__, __LINE__, info->device_name);
  2033. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_hangup"))
  2034. return;
  2035. mgslpc_flush_buffer(tty);
  2036. shutdown(info, tty);
  2037. tty_port_hangup(&info->port);
  2038. }
  2039. static int carrier_raised(struct tty_port *port)
  2040. {
  2041. MGSLPC_INFO *info = container_of(port, MGSLPC_INFO, port);
  2042. unsigned long flags;
  2043. spin_lock_irqsave(&info->lock, flags);
  2044. get_signals(info);
  2045. spin_unlock_irqrestore(&info->lock, flags);
  2046. if (info->serial_signals & SerialSignal_DCD)
  2047. return 1;
  2048. return 0;
  2049. }
  2050. static void dtr_rts(struct tty_port *port, int onoff)
  2051. {
  2052. MGSLPC_INFO *info = container_of(port, MGSLPC_INFO, port);
  2053. unsigned long flags;
  2054. spin_lock_irqsave(&info->lock, flags);
  2055. if (onoff)
  2056. info->serial_signals |= SerialSignal_RTS | SerialSignal_DTR;
  2057. else
  2058. info->serial_signals &= ~(SerialSignal_RTS | SerialSignal_DTR);
  2059. set_signals(info);
  2060. spin_unlock_irqrestore(&info->lock, flags);
  2061. }
  2062. static int mgslpc_open(struct tty_struct *tty, struct file * filp)
  2063. {
  2064. MGSLPC_INFO *info;
  2065. struct tty_port *port;
  2066. int retval, line;
  2067. unsigned long flags;
  2068. /* verify range of specified line number */
  2069. line = tty->index;
  2070. if (line >= mgslpc_device_count) {
  2071. printk("%s(%d):mgslpc_open with invalid line #%d.\n",
  2072. __FILE__, __LINE__, line);
  2073. return -ENODEV;
  2074. }
  2075. /* find the info structure for the specified line */
  2076. info = mgslpc_device_list;
  2077. while(info && info->line != line)
  2078. info = info->next_device;
  2079. if (mgslpc_paranoia_check(info, tty->name, "mgslpc_open"))
  2080. return -ENODEV;
  2081. port = &info->port;
  2082. tty->driver_data = info;
  2083. tty_port_tty_set(port, tty);
  2084. if (debug_level >= DEBUG_LEVEL_INFO)
  2085. printk("%s(%d):mgslpc_open(%s), old ref count = %d\n",
  2086. __FILE__, __LINE__, tty->driver->name, port->count);
  2087. /* If port is closing, signal caller to try again */
  2088. if (port->flags & ASYNC_CLOSING){
  2089. wait_event_interruptible_tty(tty, port->close_wait,
  2090. !(port->flags & ASYNC_CLOSING));
  2091. retval = ((port->flags & ASYNC_HUP_NOTIFY) ?
  2092. -EAGAIN : -ERESTARTSYS);
  2093. goto cleanup;
  2094. }
  2095. port->low_latency = (port->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  2096. spin_lock_irqsave(&info->netlock, flags);
  2097. if (info->netcount) {
  2098. retval = -EBUSY;
  2099. spin_unlock_irqrestore(&info->netlock, flags);
  2100. goto cleanup;
  2101. }
  2102. spin_lock(&port->lock);
  2103. port->count++;
  2104. spin_unlock(&port->lock);
  2105. spin_unlock_irqrestore(&info->netlock, flags);
  2106. if (port->count == 1) {
  2107. /* 1st open on this device, init hardware */
  2108. retval = startup(info, tty);
  2109. if (retval < 0)
  2110. goto cleanup;
  2111. }
  2112. retval = tty_port_block_til_ready(&info->port, tty, filp);
  2113. if (retval) {
  2114. if (debug_level >= DEBUG_LEVEL_INFO)
  2115. printk("%s(%d):block_til_ready(%s) returned %d\n",
  2116. __FILE__, __LINE__, info->device_name, retval);
  2117. goto cleanup;
  2118. }
  2119. if (debug_level >= DEBUG_LEVEL_INFO)
  2120. printk("%s(%d):mgslpc_open(%s) success\n",
  2121. __FILE__, __LINE__, info->device_name);
  2122. retval = 0;
  2123. cleanup:
  2124. return retval;
  2125. }
  2126. /*
  2127. * /proc fs routines....
  2128. */
  2129. static inline void line_info(struct seq_file *m, MGSLPC_INFO *info)
  2130. {
  2131. char stat_buf[30];
  2132. unsigned long flags;
  2133. seq_printf(m, "%s:io:%04X irq:%d",
  2134. info->device_name, info->io_base, info->irq_level);
  2135. /* output current serial signal states */
  2136. spin_lock_irqsave(&info->lock, flags);
  2137. get_signals(info);
  2138. spin_unlock_irqrestore(&info->lock, flags);
  2139. stat_buf[0] = 0;
  2140. stat_buf[1] = 0;
  2141. if (info->serial_signals & SerialSignal_RTS)
  2142. strcat(stat_buf, "|RTS");
  2143. if (info->serial_signals & SerialSignal_CTS)
  2144. strcat(stat_buf, "|CTS");
  2145. if (info->serial_signals & SerialSignal_DTR)
  2146. strcat(stat_buf, "|DTR");
  2147. if (info->serial_signals & SerialSignal_DSR)
  2148. strcat(stat_buf, "|DSR");
  2149. if (info->serial_signals & SerialSignal_DCD)
  2150. strcat(stat_buf, "|CD");
  2151. if (info->serial_signals & SerialSignal_RI)
  2152. strcat(stat_buf, "|RI");
  2153. if (info->params.mode == MGSL_MODE_HDLC) {
  2154. seq_printf(m, " HDLC txok:%d rxok:%d",
  2155. info->icount.txok, info->icount.rxok);
  2156. if (info->icount.txunder)
  2157. seq_printf(m, " txunder:%d", info->icount.txunder);
  2158. if (info->icount.txabort)
  2159. seq_printf(m, " txabort:%d", info->icount.txabort);
  2160. if (info->icount.rxshort)
  2161. seq_printf(m, " rxshort:%d", info->icount.rxshort);
  2162. if (info->icount.rxlong)
  2163. seq_printf(m, " rxlong:%d", info->icount.rxlong);
  2164. if (info->icount.rxover)
  2165. seq_printf(m, " rxover:%d", info->icount.rxover);
  2166. if (info->icount.rxcrc)
  2167. seq_printf(m, " rxcrc:%d", info->icount.rxcrc);
  2168. } else {
  2169. seq_printf(m, " ASYNC tx:%d rx:%d",
  2170. info->icount.tx, info->icount.rx);
  2171. if (info->icount.frame)
  2172. seq_printf(m, " fe:%d", info->icount.frame);
  2173. if (info->icount.parity)
  2174. seq_printf(m, " pe:%d", info->icount.parity);
  2175. if (info->icount.brk)
  2176. seq_printf(m, " brk:%d", info->icount.brk);
  2177. if (info->icount.overrun)
  2178. seq_printf(m, " oe:%d", info->icount.overrun);
  2179. }
  2180. /* Append serial signal status to end */
  2181. seq_printf(m, " %s\n", stat_buf+1);
  2182. seq_printf(m, "txactive=%d bh_req=%d bh_run=%d pending_bh=%x\n",
  2183. info->tx_active,info->bh_requested,info->bh_running,
  2184. info->pending_bh);
  2185. }
  2186. /* Called to print information about devices
  2187. */
  2188. static int mgslpc_proc_show(struct seq_file *m, void *v)
  2189. {
  2190. MGSLPC_INFO *info;
  2191. seq_printf(m, "synclink driver:%s\n", driver_version);
  2192. info = mgslpc_device_list;
  2193. while (info) {
  2194. line_info(m, info);
  2195. info = info->next_device;
  2196. }
  2197. return 0;
  2198. }
  2199. static int mgslpc_proc_open(struct inode *inode, struct file *file)
  2200. {
  2201. return single_open(file, mgslpc_proc_show, NULL);
  2202. }
  2203. static const struct file_operations mgslpc_proc_fops = {
  2204. .owner = THIS_MODULE,
  2205. .open = mgslpc_proc_open,
  2206. .read = seq_read,
  2207. .llseek = seq_lseek,
  2208. .release = single_release,
  2209. };
  2210. static int rx_alloc_buffers(MGSLPC_INFO *info)
  2211. {
  2212. /* each buffer has header and data */
  2213. info->rx_buf_size = sizeof(RXBUF) + info->max_frame_size;
  2214. /* calculate total allocation size for 8 buffers */
  2215. info->rx_buf_total_size = info->rx_buf_size * 8;
  2216. /* limit total allocated memory */
  2217. if (info->rx_buf_total_size > 0x10000)
  2218. info->rx_buf_total_size = 0x10000;
  2219. /* calculate number of buffers */
  2220. info->rx_buf_count = info->rx_buf_total_size / info->rx_buf_size;
  2221. info->rx_buf = kmalloc(info->rx_buf_total_size, GFP_KERNEL);
  2222. if (info->rx_buf == NULL)
  2223. return -ENOMEM;
  2224. /* unused flag buffer to satisfy receive_buf calling interface */
  2225. info->flag_buf = kzalloc(info->max_frame_size, GFP_KERNEL);
  2226. if (!info->flag_buf) {
  2227. kfree(info->rx_buf);
  2228. info->rx_buf = NULL;
  2229. return -ENOMEM;
  2230. }
  2231. rx_reset_buffers(info);
  2232. return 0;
  2233. }
  2234. static void rx_free_buffers(MGSLPC_INFO *info)
  2235. {
  2236. kfree(info->rx_buf);
  2237. info->rx_buf = NULL;
  2238. kfree(info->flag_buf);
  2239. info->flag_buf = NULL;
  2240. }
  2241. static int claim_resources(MGSLPC_INFO *info)
  2242. {
  2243. if (rx_alloc_buffers(info) < 0) {
  2244. printk("Can't allocate rx buffer %s\n", info->device_name);
  2245. release_resources(info);
  2246. return -ENODEV;
  2247. }
  2248. return 0;
  2249. }
  2250. static void release_resources(MGSLPC_INFO *info)
  2251. {
  2252. if (debug_level >= DEBUG_LEVEL_INFO)
  2253. printk("release_resources(%s)\n", info->device_name);
  2254. rx_free_buffers(info);
  2255. }
  2256. /* Add the specified device instance data structure to the
  2257. * global linked list of devices and increment the device count.
  2258. *
  2259. * Arguments: info pointer to device instance data
  2260. */
  2261. static int mgslpc_add_device(MGSLPC_INFO *info)
  2262. {
  2263. MGSLPC_INFO *current_dev = NULL;
  2264. struct device *tty_dev;
  2265. int ret;
  2266. info->next_device = NULL;
  2267. info->line = mgslpc_device_count;
  2268. sprintf(info->device_name,"ttySLP%d",info->line);
  2269. if (info->line < MAX_DEVICE_COUNT) {
  2270. if (maxframe[info->line])
  2271. info->max_frame_size = maxframe[info->line];
  2272. }
  2273. mgslpc_device_count++;
  2274. if (!mgslpc_device_list)
  2275. mgslpc_device_list = info;
  2276. else {
  2277. current_dev = mgslpc_device_list;
  2278. while (current_dev->next_device)
  2279. current_dev = current_dev->next_device;
  2280. current_dev->next_device = info;
  2281. }
  2282. if (info->max_frame_size < 4096)
  2283. info->max_frame_size = 4096;
  2284. else if (info->max_frame_size > 65535)
  2285. info->max_frame_size = 65535;
  2286. printk("SyncLink PC Card %s:IO=%04X IRQ=%d\n",
  2287. info->device_name, info->io_base, info->irq_level);
  2288. #if SYNCLINK_GENERIC_HDLC
  2289. ret = hdlcdev_init(info);
  2290. if (ret != 0)
  2291. goto failed;
  2292. #endif
  2293. tty_dev = tty_port_register_device(&info->port, serial_driver, info->line,
  2294. &info->p_dev->dev);
  2295. if (IS_ERR(tty_dev)) {
  2296. ret = PTR_ERR(tty_dev);
  2297. #if SYNCLINK_GENERIC_HDLC
  2298. hdlcdev_exit(info);
  2299. #endif
  2300. goto failed;
  2301. }
  2302. return 0;
  2303. failed:
  2304. if (current_dev)
  2305. current_dev->next_device = NULL;
  2306. else
  2307. mgslpc_device_list = NULL;
  2308. mgslpc_device_count--;
  2309. return ret;
  2310. }
  2311. static void mgslpc_remove_device(MGSLPC_INFO *remove_info)
  2312. {
  2313. MGSLPC_INFO *info = mgslpc_device_list;
  2314. MGSLPC_INFO *last = NULL;
  2315. while(info) {
  2316. if (info == remove_info) {
  2317. if (last)
  2318. last->next_device = info->next_device;
  2319. else
  2320. mgslpc_device_list = info->next_device;
  2321. tty_unregister_device(serial_driver, info->line);
  2322. #if SYNCLINK_GENERIC_HDLC
  2323. hdlcdev_exit(info);
  2324. #endif
  2325. release_resources(info);
  2326. tty_port_destroy(&info->port);
  2327. kfree(info);
  2328. mgslpc_device_count--;
  2329. return;
  2330. }
  2331. last = info;
  2332. info = info->next_device;
  2333. }
  2334. }
  2335. static const struct pcmcia_device_id mgslpc_ids[] = {
  2336. PCMCIA_DEVICE_MANF_CARD(0x02c5, 0x0050),
  2337. PCMCIA_DEVICE_NULL
  2338. };
  2339. MODULE_DEVICE_TABLE(pcmcia, mgslpc_ids);
  2340. static struct pcmcia_driver mgslpc_driver = {
  2341. .owner = THIS_MODULE,
  2342. .name = "synclink_cs",
  2343. .probe = mgslpc_probe,
  2344. .remove = mgslpc_detach,
  2345. .id_table = mgslpc_ids,
  2346. .suspend = mgslpc_suspend,
  2347. .resume = mgslpc_resume,
  2348. };
  2349. static const struct tty_operations mgslpc_ops = {
  2350. .open = mgslpc_open,
  2351. .close = mgslpc_close,
  2352. .write = mgslpc_write,
  2353. .put_char = mgslpc_put_char,
  2354. .flush_chars = mgslpc_flush_chars,
  2355. .write_room = mgslpc_write_room,
  2356. .chars_in_buffer = mgslpc_chars_in_buffer,
  2357. .flush_buffer = mgslpc_flush_buffer,
  2358. .ioctl = mgslpc_ioctl,
  2359. .throttle = mgslpc_throttle,
  2360. .unthrottle = mgslpc_unthrottle,
  2361. .send_xchar = mgslpc_send_xchar,
  2362. .break_ctl = mgslpc_break,
  2363. .wait_until_sent = mgslpc_wait_until_sent,
  2364. .set_termios = mgslpc_set_termios,
  2365. .stop = tx_pause,
  2366. .start = tx_release,
  2367. .hangup = mgslpc_hangup,
  2368. .tiocmget = tiocmget,
  2369. .tiocmset = tiocmset,
  2370. .get_icount = mgslpc_get_icount,
  2371. .proc_fops = &mgslpc_proc_fops,
  2372. };
  2373. static int __init synclink_cs_init(void)
  2374. {
  2375. int rc;
  2376. if (break_on_load) {
  2377. mgslpc_get_text_ptr();
  2378. BREAKPOINT();
  2379. }
  2380. serial_driver = tty_alloc_driver(MAX_DEVICE_COUNT,
  2381. TTY_DRIVER_REAL_RAW |
  2382. TTY_DRIVER_DYNAMIC_DEV);
  2383. if (IS_ERR(serial_driver)) {
  2384. rc = PTR_ERR(serial_driver);
  2385. goto err;
  2386. }
  2387. /* Initialize the tty_driver structure */
  2388. serial_driver->driver_name = "synclink_cs";
  2389. serial_driver->name = "ttySLP";
  2390. serial_driver->major = ttymajor;
  2391. serial_driver->minor_start = 64;
  2392. serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  2393. serial_driver->subtype = SERIAL_TYPE_NORMAL;
  2394. serial_driver->init_termios = tty_std_termios;
  2395. serial_driver->init_termios.c_cflag =
  2396. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  2397. tty_set_operations(serial_driver, &mgslpc_ops);
  2398. rc = tty_register_driver(serial_driver);
  2399. if (rc < 0) {
  2400. printk(KERN_ERR "%s(%d):Couldn't register serial driver\n",
  2401. __FILE__, __LINE__);
  2402. goto err_put_tty;
  2403. }
  2404. rc = pcmcia_register_driver(&mgslpc_driver);
  2405. if (rc < 0)
  2406. goto err_unreg_tty;
  2407. printk(KERN_INFO "%s %s, tty major#%d\n", driver_name, driver_version,
  2408. serial_driver->major);
  2409. return 0;
  2410. err_unreg_tty:
  2411. tty_unregister_driver(serial_driver);
  2412. err_put_tty:
  2413. put_tty_driver(serial_driver);
  2414. err:
  2415. return rc;
  2416. }
  2417. static void __exit synclink_cs_exit(void)
  2418. {
  2419. pcmcia_unregister_driver(&mgslpc_driver);
  2420. tty_unregister_driver(serial_driver);
  2421. put_tty_driver(serial_driver);
  2422. }
  2423. module_init(synclink_cs_init);
  2424. module_exit(synclink_cs_exit);
  2425. static void mgslpc_set_rate(MGSLPC_INFO *info, unsigned char channel, unsigned int rate)
  2426. {
  2427. unsigned int M, N;
  2428. unsigned char val;
  2429. /* note:standard BRG mode is broken in V3.2 chip
  2430. * so enhanced mode is always used
  2431. */
  2432. if (rate) {
  2433. N = 3686400 / rate;
  2434. if (!N)
  2435. N = 1;
  2436. N >>= 1;
  2437. for (M = 1; N > 64 && M < 16; M++)
  2438. N >>= 1;
  2439. N--;
  2440. /* BGR[5..0] = N
  2441. * BGR[9..6] = M
  2442. * BGR[7..0] contained in BGR register
  2443. * BGR[9..8] contained in CCR2[7..6]
  2444. * divisor = (N+1)*2^M
  2445. *
  2446. * Note: M *must* not be zero (causes asymetric duty cycle)
  2447. */
  2448. write_reg(info, (unsigned char) (channel + BGR),
  2449. (unsigned char) ((M << 6) + N));
  2450. val = read_reg(info, (unsigned char) (channel + CCR2)) & 0x3f;
  2451. val |= ((M << 4) & 0xc0);
  2452. write_reg(info, (unsigned char) (channel + CCR2), val);
  2453. }
  2454. }
  2455. /* Enabled the AUX clock output at the specified frequency.
  2456. */
  2457. static void enable_auxclk(MGSLPC_INFO *info)
  2458. {
  2459. unsigned char val;
  2460. /* MODE
  2461. *
  2462. * 07..06 MDS[1..0] 10 = transparent HDLC mode
  2463. * 05 ADM Address Mode, 0 = no addr recognition
  2464. * 04 TMD Timer Mode, 0 = external
  2465. * 03 RAC Receiver Active, 0 = inactive
  2466. * 02 RTS 0=RTS active during xmit, 1=RTS always active
  2467. * 01 TRS Timer Resolution, 1=512
  2468. * 00 TLP Test Loop, 0 = no loop
  2469. *
  2470. * 1000 0010
  2471. */
  2472. val = 0x82;
  2473. /* channel B RTS is used to enable AUXCLK driver on SP505 */
  2474. if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
  2475. val |= BIT2;
  2476. write_reg(info, CHB + MODE, val);
  2477. /* CCR0
  2478. *
  2479. * 07 PU Power Up, 1=active, 0=power down
  2480. * 06 MCE Master Clock Enable, 1=enabled
  2481. * 05 Reserved, 0
  2482. * 04..02 SC[2..0] Encoding
  2483. * 01..00 SM[1..0] Serial Mode, 00=HDLC
  2484. *
  2485. * 11000000
  2486. */
  2487. write_reg(info, CHB + CCR0, 0xc0);
  2488. /* CCR1
  2489. *
  2490. * 07 SFLG Shared Flag, 0 = disable shared flags
  2491. * 06 GALP Go Active On Loop, 0 = not used
  2492. * 05 GLP Go On Loop, 0 = not used
  2493. * 04 ODS Output Driver Select, 1=TxD is push-pull output
  2494. * 03 ITF Interframe Time Fill, 0=mark, 1=flag
  2495. * 02..00 CM[2..0] Clock Mode
  2496. *
  2497. * 0001 0111
  2498. */
  2499. write_reg(info, CHB + CCR1, 0x17);
  2500. /* CCR2 (Channel B)
  2501. *
  2502. * 07..06 BGR[9..8] Baud rate bits 9..8
  2503. * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
  2504. * 04 SSEL Clock source select, 1=submode b
  2505. * 03 TOE 0=TxCLK is input, 1=TxCLK is output
  2506. * 02 RWX Read/Write Exchange 0=disabled
  2507. * 01 C32, CRC select, 0=CRC-16, 1=CRC-32
  2508. * 00 DIV, data inversion 0=disabled, 1=enabled
  2509. *
  2510. * 0011 1000
  2511. */
  2512. if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
  2513. write_reg(info, CHB + CCR2, 0x38);
  2514. else
  2515. write_reg(info, CHB + CCR2, 0x30);
  2516. /* CCR4
  2517. *
  2518. * 07 MCK4 Master Clock Divide by 4, 1=enabled
  2519. * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
  2520. * 05 TST1 Test Pin, 0=normal operation
  2521. * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
  2522. * 03..02 Reserved, must be 0
  2523. * 01..00 RFT[1..0] RxFIFO Threshold 00=32 bytes
  2524. *
  2525. * 0101 0000
  2526. */
  2527. write_reg(info, CHB + CCR4, 0x50);
  2528. /* if auxclk not enabled, set internal BRG so
  2529. * CTS transitions can be detected (requires TxC)
  2530. */
  2531. if (info->params.mode == MGSL_MODE_HDLC && info->params.clock_speed)
  2532. mgslpc_set_rate(info, CHB, info->params.clock_speed);
  2533. else
  2534. mgslpc_set_rate(info, CHB, 921600);
  2535. }
  2536. static void loopback_enable(MGSLPC_INFO *info)
  2537. {
  2538. unsigned char val;
  2539. /* CCR1:02..00 CM[2..0] Clock Mode = 111 (clock mode 7) */
  2540. val = read_reg(info, CHA + CCR1) | (BIT2 | BIT1 | BIT0);
  2541. write_reg(info, CHA + CCR1, val);
  2542. /* CCR2:04 SSEL Clock source select, 1=submode b */
  2543. val = read_reg(info, CHA + CCR2) | (BIT4 | BIT5);
  2544. write_reg(info, CHA + CCR2, val);
  2545. /* set LinkSpeed if available, otherwise default to 2Mbps */
  2546. if (info->params.clock_speed)
  2547. mgslpc_set_rate(info, CHA, info->params.clock_speed);
  2548. else
  2549. mgslpc_set_rate(info, CHA, 1843200);
  2550. /* MODE:00 TLP Test Loop, 1=loopback enabled */
  2551. val = read_reg(info, CHA + MODE) | BIT0;
  2552. write_reg(info, CHA + MODE, val);
  2553. }
  2554. static void hdlc_mode(MGSLPC_INFO *info)
  2555. {
  2556. unsigned char val;
  2557. unsigned char clkmode, clksubmode;
  2558. /* disable all interrupts */
  2559. irq_disable(info, CHA, 0xffff);
  2560. irq_disable(info, CHB, 0xffff);
  2561. port_irq_disable(info, 0xff);
  2562. /* assume clock mode 0a, rcv=RxC xmt=TxC */
  2563. clkmode = clksubmode = 0;
  2564. if (info->params.flags & HDLC_FLAG_RXC_DPLL
  2565. && info->params.flags & HDLC_FLAG_TXC_DPLL) {
  2566. /* clock mode 7a, rcv = DPLL, xmt = DPLL */
  2567. clkmode = 7;
  2568. } else if (info->params.flags & HDLC_FLAG_RXC_BRG
  2569. && info->params.flags & HDLC_FLAG_TXC_BRG) {
  2570. /* clock mode 7b, rcv = BRG, xmt = BRG */
  2571. clkmode = 7;
  2572. clksubmode = 1;
  2573. } else if (info->params.flags & HDLC_FLAG_RXC_DPLL) {
  2574. if (info->params.flags & HDLC_FLAG_TXC_BRG) {
  2575. /* clock mode 6b, rcv = DPLL, xmt = BRG/16 */
  2576. clkmode = 6;
  2577. clksubmode = 1;
  2578. } else {
  2579. /* clock mode 6a, rcv = DPLL, xmt = TxC */
  2580. clkmode = 6;
  2581. }
  2582. } else if (info->params.flags & HDLC_FLAG_TXC_BRG) {
  2583. /* clock mode 0b, rcv = RxC, xmt = BRG */
  2584. clksubmode = 1;
  2585. }
  2586. /* MODE
  2587. *
  2588. * 07..06 MDS[1..0] 10 = transparent HDLC mode
  2589. * 05 ADM Address Mode, 0 = no addr recognition
  2590. * 04 TMD Timer Mode, 0 = external
  2591. * 03 RAC Receiver Active, 0 = inactive
  2592. * 02 RTS 0=RTS active during xmit, 1=RTS always active
  2593. * 01 TRS Timer Resolution, 1=512
  2594. * 00 TLP Test Loop, 0 = no loop
  2595. *
  2596. * 1000 0010
  2597. */
  2598. val = 0x82;
  2599. if (info->params.loopback)
  2600. val |= BIT0;
  2601. /* preserve RTS state */
  2602. if (info->serial_signals & SerialSignal_RTS)
  2603. val |= BIT2;
  2604. write_reg(info, CHA + MODE, val);
  2605. /* CCR0
  2606. *
  2607. * 07 PU Power Up, 1=active, 0=power down
  2608. * 06 MCE Master Clock Enable, 1=enabled
  2609. * 05 Reserved, 0
  2610. * 04..02 SC[2..0] Encoding
  2611. * 01..00 SM[1..0] Serial Mode, 00=HDLC
  2612. *
  2613. * 11000000
  2614. */
  2615. val = 0xc0;
  2616. switch (info->params.encoding)
  2617. {
  2618. case HDLC_ENCODING_NRZI:
  2619. val |= BIT3;
  2620. break;
  2621. case HDLC_ENCODING_BIPHASE_SPACE:
  2622. val |= BIT4;
  2623. break; // FM0
  2624. case HDLC_ENCODING_BIPHASE_MARK:
  2625. val |= BIT4 | BIT2;
  2626. break; // FM1
  2627. case HDLC_ENCODING_BIPHASE_LEVEL:
  2628. val |= BIT4 | BIT3;
  2629. break; // Manchester
  2630. }
  2631. write_reg(info, CHA + CCR0, val);
  2632. /* CCR1
  2633. *
  2634. * 07 SFLG Shared Flag, 0 = disable shared flags
  2635. * 06 GALP Go Active On Loop, 0 = not used
  2636. * 05 GLP Go On Loop, 0 = not used
  2637. * 04 ODS Output Driver Select, 1=TxD is push-pull output
  2638. * 03 ITF Interframe Time Fill, 0=mark, 1=flag
  2639. * 02..00 CM[2..0] Clock Mode
  2640. *
  2641. * 0001 0000
  2642. */
  2643. val = 0x10 + clkmode;
  2644. write_reg(info, CHA + CCR1, val);
  2645. /* CCR2
  2646. *
  2647. * 07..06 BGR[9..8] Baud rate bits 9..8
  2648. * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
  2649. * 04 SSEL Clock source select, 1=submode b
  2650. * 03 TOE 0=TxCLK is input, 0=TxCLK is input
  2651. * 02 RWX Read/Write Exchange 0=disabled
  2652. * 01 C32, CRC select, 0=CRC-16, 1=CRC-32
  2653. * 00 DIV, data inversion 0=disabled, 1=enabled
  2654. *
  2655. * 0000 0000
  2656. */
  2657. val = 0x00;
  2658. if (clkmode == 2 || clkmode == 3 || clkmode == 6
  2659. || clkmode == 7 || (clkmode == 0 && clksubmode == 1))
  2660. val |= BIT5;
  2661. if (clksubmode)
  2662. val |= BIT4;
  2663. if (info->params.crc_type == HDLC_CRC_32_CCITT)
  2664. val |= BIT1;
  2665. if (info->params.encoding == HDLC_ENCODING_NRZB)
  2666. val |= BIT0;
  2667. write_reg(info, CHA + CCR2, val);
  2668. /* CCR3
  2669. *
  2670. * 07..06 PRE[1..0] Preamble count 00=1, 01=2, 10=4, 11=8
  2671. * 05 EPT Enable preamble transmission, 1=enabled
  2672. * 04 RADD Receive address pushed to FIFO, 0=disabled
  2673. * 03 CRL CRC Reset Level, 0=FFFF
  2674. * 02 RCRC Rx CRC 0=On 1=Off
  2675. * 01 TCRC Tx CRC 0=On 1=Off
  2676. * 00 PSD DPLL Phase Shift Disable
  2677. *
  2678. * 0000 0000
  2679. */
  2680. val = 0x00;
  2681. if (info->params.crc_type == HDLC_CRC_NONE)
  2682. val |= BIT2 | BIT1;
  2683. if (info->params.preamble != HDLC_PREAMBLE_PATTERN_NONE)
  2684. val |= BIT5;
  2685. switch (info->params.preamble_length)
  2686. {
  2687. case HDLC_PREAMBLE_LENGTH_16BITS:
  2688. val |= BIT6;
  2689. break;
  2690. case HDLC_PREAMBLE_LENGTH_32BITS:
  2691. val |= BIT6;
  2692. break;
  2693. case HDLC_PREAMBLE_LENGTH_64BITS:
  2694. val |= BIT7 | BIT6;
  2695. break;
  2696. }
  2697. write_reg(info, CHA + CCR3, val);
  2698. /* PRE - Preamble pattern */
  2699. val = 0;
  2700. switch (info->params.preamble)
  2701. {
  2702. case HDLC_PREAMBLE_PATTERN_FLAGS: val = 0x7e; break;
  2703. case HDLC_PREAMBLE_PATTERN_10: val = 0xaa; break;
  2704. case HDLC_PREAMBLE_PATTERN_01: val = 0x55; break;
  2705. case HDLC_PREAMBLE_PATTERN_ONES: val = 0xff; break;
  2706. }
  2707. write_reg(info, CHA + PRE, val);
  2708. /* CCR4
  2709. *
  2710. * 07 MCK4 Master Clock Divide by 4, 1=enabled
  2711. * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
  2712. * 05 TST1 Test Pin, 0=normal operation
  2713. * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
  2714. * 03..02 Reserved, must be 0
  2715. * 01..00 RFT[1..0] RxFIFO Threshold 00=32 bytes
  2716. *
  2717. * 0101 0000
  2718. */
  2719. val = 0x50;
  2720. write_reg(info, CHA + CCR4, val);
  2721. if (info->params.flags & HDLC_FLAG_RXC_DPLL)
  2722. mgslpc_set_rate(info, CHA, info->params.clock_speed * 16);
  2723. else
  2724. mgslpc_set_rate(info, CHA, info->params.clock_speed);
  2725. /* RLCR Receive length check register
  2726. *
  2727. * 7 1=enable receive length check
  2728. * 6..0 Max frame length = (RL + 1) * 32
  2729. */
  2730. write_reg(info, CHA + RLCR, 0);
  2731. /* XBCH Transmit Byte Count High
  2732. *
  2733. * 07 DMA mode, 0 = interrupt driven
  2734. * 06 NRM, 0=ABM (ignored)
  2735. * 05 CAS Carrier Auto Start
  2736. * 04 XC Transmit Continuously (ignored)
  2737. * 03..00 XBC[10..8] Transmit byte count bits 10..8
  2738. *
  2739. * 0000 0000
  2740. */
  2741. val = 0x00;
  2742. if (info->params.flags & HDLC_FLAG_AUTO_DCD)
  2743. val |= BIT5;
  2744. write_reg(info, CHA + XBCH, val);
  2745. enable_auxclk(info);
  2746. if (info->params.loopback || info->testing_irq)
  2747. loopback_enable(info);
  2748. if (info->params.flags & HDLC_FLAG_AUTO_CTS)
  2749. {
  2750. irq_enable(info, CHB, IRQ_CTS);
  2751. /* PVR[3] 1=AUTO CTS active */
  2752. set_reg_bits(info, CHA + PVR, BIT3);
  2753. } else
  2754. clear_reg_bits(info, CHA + PVR, BIT3);
  2755. irq_enable(info, CHA,
  2756. IRQ_RXEOM | IRQ_RXFIFO | IRQ_ALLSENT |
  2757. IRQ_UNDERRUN | IRQ_TXFIFO);
  2758. issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
  2759. wait_command_complete(info, CHA);
  2760. read_reg16(info, CHA + ISR); /* clear pending IRQs */
  2761. /* Master clock mode enabled above to allow reset commands
  2762. * to complete even if no data clocks are present.
  2763. *
  2764. * Disable master clock mode for normal communications because
  2765. * V3.2 of the ESCC2 has a bug that prevents the transmit all sent
  2766. * IRQ when in master clock mode.
  2767. *
  2768. * Leave master clock mode enabled for IRQ test because the
  2769. * timer IRQ used by the test can only happen in master clock mode.
  2770. */
  2771. if (!info->testing_irq)
  2772. clear_reg_bits(info, CHA + CCR0, BIT6);
  2773. tx_set_idle(info);
  2774. tx_stop(info);
  2775. rx_stop(info);
  2776. }
  2777. static void rx_stop(MGSLPC_INFO *info)
  2778. {
  2779. if (debug_level >= DEBUG_LEVEL_ISR)
  2780. printk("%s(%d):rx_stop(%s)\n",
  2781. __FILE__, __LINE__, info->device_name);
  2782. /* MODE:03 RAC Receiver Active, 0=inactive */
  2783. clear_reg_bits(info, CHA + MODE, BIT3);
  2784. info->rx_enabled = false;
  2785. info->rx_overflow = false;
  2786. }
  2787. static void rx_start(MGSLPC_INFO *info)
  2788. {
  2789. if (debug_level >= DEBUG_LEVEL_ISR)
  2790. printk("%s(%d):rx_start(%s)\n",
  2791. __FILE__, __LINE__, info->device_name);
  2792. rx_reset_buffers(info);
  2793. info->rx_enabled = false;
  2794. info->rx_overflow = false;
  2795. /* MODE:03 RAC Receiver Active, 1=active */
  2796. set_reg_bits(info, CHA + MODE, BIT3);
  2797. info->rx_enabled = true;
  2798. }
  2799. static void tx_start(MGSLPC_INFO *info, struct tty_struct *tty)
  2800. {
  2801. if (debug_level >= DEBUG_LEVEL_ISR)
  2802. printk("%s(%d):tx_start(%s)\n",
  2803. __FILE__, __LINE__, info->device_name);
  2804. if (info->tx_count) {
  2805. /* If auto RTS enabled and RTS is inactive, then assert */
  2806. /* RTS and set a flag indicating that the driver should */
  2807. /* negate RTS when the transmission completes. */
  2808. info->drop_rts_on_tx_done = false;
  2809. if (info->params.flags & HDLC_FLAG_AUTO_RTS) {
  2810. get_signals(info);
  2811. if (!(info->serial_signals & SerialSignal_RTS)) {
  2812. info->serial_signals |= SerialSignal_RTS;
  2813. set_signals(info);
  2814. info->drop_rts_on_tx_done = true;
  2815. }
  2816. }
  2817. if (info->params.mode == MGSL_MODE_ASYNC) {
  2818. if (!info->tx_active) {
  2819. info->tx_active = true;
  2820. tx_ready(info, tty);
  2821. }
  2822. } else {
  2823. info->tx_active = true;
  2824. tx_ready(info, tty);
  2825. mod_timer(&info->tx_timer, jiffies +
  2826. msecs_to_jiffies(5000));
  2827. }
  2828. }
  2829. if (!info->tx_enabled)
  2830. info->tx_enabled = true;
  2831. }
  2832. static void tx_stop(MGSLPC_INFO *info)
  2833. {
  2834. if (debug_level >= DEBUG_LEVEL_ISR)
  2835. printk("%s(%d):tx_stop(%s)\n",
  2836. __FILE__, __LINE__, info->device_name);
  2837. del_timer(&info->tx_timer);
  2838. info->tx_enabled = false;
  2839. info->tx_active = false;
  2840. }
  2841. /* Reset the adapter to a known state and prepare it for further use.
  2842. */
  2843. static void reset_device(MGSLPC_INFO *info)
  2844. {
  2845. /* power up both channels (set BIT7) */
  2846. write_reg(info, CHA + CCR0, 0x80);
  2847. write_reg(info, CHB + CCR0, 0x80);
  2848. write_reg(info, CHA + MODE, 0);
  2849. write_reg(info, CHB + MODE, 0);
  2850. /* disable all interrupts */
  2851. irq_disable(info, CHA, 0xffff);
  2852. irq_disable(info, CHB, 0xffff);
  2853. port_irq_disable(info, 0xff);
  2854. /* PCR Port Configuration Register
  2855. *
  2856. * 07..04 DEC[3..0] Serial I/F select outputs
  2857. * 03 output, 1=AUTO CTS control enabled
  2858. * 02 RI Ring Indicator input 0=active
  2859. * 01 DSR input 0=active
  2860. * 00 DTR output 0=active
  2861. *
  2862. * 0000 0110
  2863. */
  2864. write_reg(info, PCR, 0x06);
  2865. /* PVR Port Value Register
  2866. *
  2867. * 07..04 DEC[3..0] Serial I/F select (0000=disabled)
  2868. * 03 AUTO CTS output 1=enabled
  2869. * 02 RI Ring Indicator input
  2870. * 01 DSR input
  2871. * 00 DTR output (1=inactive)
  2872. *
  2873. * 0000 0001
  2874. */
  2875. // write_reg(info, PVR, PVR_DTR);
  2876. /* IPC Interrupt Port Configuration
  2877. *
  2878. * 07 VIS 1=Masked interrupts visible
  2879. * 06..05 Reserved, 0
  2880. * 04..03 SLA Slave address, 00 ignored
  2881. * 02 CASM Cascading Mode, 1=daisy chain
  2882. * 01..00 IC[1..0] Interrupt Config, 01=push-pull output, active low
  2883. *
  2884. * 0000 0101
  2885. */
  2886. write_reg(info, IPC, 0x05);
  2887. }
  2888. static void async_mode(MGSLPC_INFO *info)
  2889. {
  2890. unsigned char val;
  2891. /* disable all interrupts */
  2892. irq_disable(info, CHA, 0xffff);
  2893. irq_disable(info, CHB, 0xffff);
  2894. port_irq_disable(info, 0xff);
  2895. /* MODE
  2896. *
  2897. * 07 Reserved, 0
  2898. * 06 FRTS RTS State, 0=active
  2899. * 05 FCTS Flow Control on CTS
  2900. * 04 FLON Flow Control Enable
  2901. * 03 RAC Receiver Active, 0 = inactive
  2902. * 02 RTS 0=Auto RTS, 1=manual RTS
  2903. * 01 TRS Timer Resolution, 1=512
  2904. * 00 TLP Test Loop, 0 = no loop
  2905. *
  2906. * 0000 0110
  2907. */
  2908. val = 0x06;
  2909. if (info->params.loopback)
  2910. val |= BIT0;
  2911. /* preserve RTS state */
  2912. if (!(info->serial_signals & SerialSignal_RTS))
  2913. val |= BIT6;
  2914. write_reg(info, CHA + MODE, val);
  2915. /* CCR0
  2916. *
  2917. * 07 PU Power Up, 1=active, 0=power down
  2918. * 06 MCE Master Clock Enable, 1=enabled
  2919. * 05 Reserved, 0
  2920. * 04..02 SC[2..0] Encoding, 000=NRZ
  2921. * 01..00 SM[1..0] Serial Mode, 11=Async
  2922. *
  2923. * 1000 0011
  2924. */
  2925. write_reg(info, CHA + CCR0, 0x83);
  2926. /* CCR1
  2927. *
  2928. * 07..05 Reserved, 0
  2929. * 04 ODS Output Driver Select, 1=TxD is push-pull output
  2930. * 03 BCR Bit Clock Rate, 1=16x
  2931. * 02..00 CM[2..0] Clock Mode, 111=BRG
  2932. *
  2933. * 0001 1111
  2934. */
  2935. write_reg(info, CHA + CCR1, 0x1f);
  2936. /* CCR2 (channel A)
  2937. *
  2938. * 07..06 BGR[9..8] Baud rate bits 9..8
  2939. * 05 BDF Baud rate divisor factor, 0=1, 1=BGR value
  2940. * 04 SSEL Clock source select, 1=submode b
  2941. * 03 TOE 0=TxCLK is input, 0=TxCLK is input
  2942. * 02 RWX Read/Write Exchange 0=disabled
  2943. * 01 Reserved, 0
  2944. * 00 DIV, data inversion 0=disabled, 1=enabled
  2945. *
  2946. * 0001 0000
  2947. */
  2948. write_reg(info, CHA + CCR2, 0x10);
  2949. /* CCR3
  2950. *
  2951. * 07..01 Reserved, 0
  2952. * 00 PSD DPLL Phase Shift Disable
  2953. *
  2954. * 0000 0000
  2955. */
  2956. write_reg(info, CHA + CCR3, 0);
  2957. /* CCR4
  2958. *
  2959. * 07 MCK4 Master Clock Divide by 4, 1=enabled
  2960. * 06 EBRG Enhanced Baud Rate Generator Mode, 1=enabled
  2961. * 05 TST1 Test Pin, 0=normal operation
  2962. * 04 ICD Ivert Carrier Detect, 1=enabled (active low)
  2963. * 03..00 Reserved, must be 0
  2964. *
  2965. * 0101 0000
  2966. */
  2967. write_reg(info, CHA + CCR4, 0x50);
  2968. mgslpc_set_rate(info, CHA, info->params.data_rate * 16);
  2969. /* DAFO Data Format
  2970. *
  2971. * 07 Reserved, 0
  2972. * 06 XBRK transmit break, 0=normal operation
  2973. * 05 Stop bits (0=1, 1=2)
  2974. * 04..03 PAR[1..0] Parity (01=odd, 10=even)
  2975. * 02 PAREN Parity Enable
  2976. * 01..00 CHL[1..0] Character Length (00=8, 01=7)
  2977. *
  2978. */
  2979. val = 0x00;
  2980. if (info->params.data_bits != 8)
  2981. val |= BIT0; /* 7 bits */
  2982. if (info->params.stop_bits != 1)
  2983. val |= BIT5;
  2984. if (info->params.parity != ASYNC_PARITY_NONE)
  2985. {
  2986. val |= BIT2; /* Parity enable */
  2987. if (info->params.parity == ASYNC_PARITY_ODD)
  2988. val |= BIT3;
  2989. else
  2990. val |= BIT4;
  2991. }
  2992. write_reg(info, CHA + DAFO, val);
  2993. /* RFC Rx FIFO Control
  2994. *
  2995. * 07 Reserved, 0
  2996. * 06 DPS, 1=parity bit not stored in data byte
  2997. * 05 DXS, 0=all data stored in FIFO (including XON/XOFF)
  2998. * 04 RFDF Rx FIFO Data Format, 1=status byte stored in FIFO
  2999. * 03..02 RFTH[1..0], rx threshold, 11=16 status + 16 data byte
  3000. * 01 Reserved, 0
  3001. * 00 TCDE Terminate Char Detect Enable, 0=disabled
  3002. *
  3003. * 0101 1100
  3004. */
  3005. write_reg(info, CHA + RFC, 0x5c);
  3006. /* RLCR Receive length check register
  3007. *
  3008. * Max frame length = (RL + 1) * 32
  3009. */
  3010. write_reg(info, CHA + RLCR, 0);
  3011. /* XBCH Transmit Byte Count High
  3012. *
  3013. * 07 DMA mode, 0 = interrupt driven
  3014. * 06 NRM, 0=ABM (ignored)
  3015. * 05 CAS Carrier Auto Start
  3016. * 04 XC Transmit Continuously (ignored)
  3017. * 03..00 XBC[10..8] Transmit byte count bits 10..8
  3018. *
  3019. * 0000 0000
  3020. */
  3021. val = 0x00;
  3022. if (info->params.flags & HDLC_FLAG_AUTO_DCD)
  3023. val |= BIT5;
  3024. write_reg(info, CHA + XBCH, val);
  3025. if (info->params.flags & HDLC_FLAG_AUTO_CTS)
  3026. irq_enable(info, CHA, IRQ_CTS);
  3027. /* MODE:03 RAC Receiver Active, 1=active */
  3028. set_reg_bits(info, CHA + MODE, BIT3);
  3029. enable_auxclk(info);
  3030. if (info->params.flags & HDLC_FLAG_AUTO_CTS) {
  3031. irq_enable(info, CHB, IRQ_CTS);
  3032. /* PVR[3] 1=AUTO CTS active */
  3033. set_reg_bits(info, CHA + PVR, BIT3);
  3034. } else
  3035. clear_reg_bits(info, CHA + PVR, BIT3);
  3036. irq_enable(info, CHA,
  3037. IRQ_RXEOM | IRQ_RXFIFO | IRQ_BREAK_ON | IRQ_RXTIME |
  3038. IRQ_ALLSENT | IRQ_TXFIFO);
  3039. issue_command(info, CHA, CMD_TXRESET + CMD_RXRESET);
  3040. wait_command_complete(info, CHA);
  3041. read_reg16(info, CHA + ISR); /* clear pending IRQs */
  3042. }
  3043. /* Set the HDLC idle mode for the transmitter.
  3044. */
  3045. static void tx_set_idle(MGSLPC_INFO *info)
  3046. {
  3047. /* Note: ESCC2 only supports flags and one idle modes */
  3048. if (info->idle_mode == HDLC_TXIDLE_FLAGS)
  3049. set_reg_bits(info, CHA + CCR1, BIT3);
  3050. else
  3051. clear_reg_bits(info, CHA + CCR1, BIT3);
  3052. }
  3053. /* get state of the V24 status (input) signals.
  3054. */
  3055. static void get_signals(MGSLPC_INFO *info)
  3056. {
  3057. unsigned char status = 0;
  3058. /* preserve RTS and DTR */
  3059. info->serial_signals &= SerialSignal_RTS | SerialSignal_DTR;
  3060. if (read_reg(info, CHB + VSTR) & BIT7)
  3061. info->serial_signals |= SerialSignal_DCD;
  3062. if (read_reg(info, CHB + STAR) & BIT1)
  3063. info->serial_signals |= SerialSignal_CTS;
  3064. status = read_reg(info, CHA + PVR);
  3065. if (!(status & PVR_RI))
  3066. info->serial_signals |= SerialSignal_RI;
  3067. if (!(status & PVR_DSR))
  3068. info->serial_signals |= SerialSignal_DSR;
  3069. }
  3070. /* Set the state of RTS and DTR based on contents of
  3071. * serial_signals member of device extension.
  3072. */
  3073. static void set_signals(MGSLPC_INFO *info)
  3074. {
  3075. unsigned char val;
  3076. val = read_reg(info, CHA + MODE);
  3077. if (info->params.mode == MGSL_MODE_ASYNC) {
  3078. if (info->serial_signals & SerialSignal_RTS)
  3079. val &= ~BIT6;
  3080. else
  3081. val |= BIT6;
  3082. } else {
  3083. if (info->serial_signals & SerialSignal_RTS)
  3084. val |= BIT2;
  3085. else
  3086. val &= ~BIT2;
  3087. }
  3088. write_reg(info, CHA + MODE, val);
  3089. if (info->serial_signals & SerialSignal_DTR)
  3090. clear_reg_bits(info, CHA + PVR, PVR_DTR);
  3091. else
  3092. set_reg_bits(info, CHA + PVR, PVR_DTR);
  3093. }
  3094. static void rx_reset_buffers(MGSLPC_INFO *info)
  3095. {
  3096. RXBUF *buf;
  3097. int i;
  3098. info->rx_put = 0;
  3099. info->rx_get = 0;
  3100. info->rx_frame_count = 0;
  3101. for (i=0 ; i < info->rx_buf_count ; i++) {
  3102. buf = (RXBUF*)(info->rx_buf + (i * info->rx_buf_size));
  3103. buf->status = buf->count = 0;
  3104. }
  3105. }
  3106. /* Attempt to return a received HDLC frame
  3107. * Only frames received without errors are returned.
  3108. *
  3109. * Returns true if frame returned, otherwise false
  3110. */
  3111. static bool rx_get_frame(MGSLPC_INFO *info, struct tty_struct *tty)
  3112. {
  3113. unsigned short status;
  3114. RXBUF *buf;
  3115. unsigned int framesize = 0;
  3116. unsigned long flags;
  3117. bool return_frame = false;
  3118. if (info->rx_frame_count == 0)
  3119. return false;
  3120. buf = (RXBUF*)(info->rx_buf + (info->rx_get * info->rx_buf_size));
  3121. status = buf->status;
  3122. /* 07 VFR 1=valid frame
  3123. * 06 RDO 1=data overrun
  3124. * 05 CRC 1=OK, 0=error
  3125. * 04 RAB 1=frame aborted
  3126. */
  3127. if ((status & 0xf0) != 0xA0) {
  3128. if (!(status & BIT7) || (status & BIT4))
  3129. info->icount.rxabort++;
  3130. else if (status & BIT6)
  3131. info->icount.rxover++;
  3132. else if (!(status & BIT5)) {
  3133. info->icount.rxcrc++;
  3134. if (info->params.crc_type & HDLC_CRC_RETURN_EX)
  3135. return_frame = true;
  3136. }
  3137. framesize = 0;
  3138. #if SYNCLINK_GENERIC_HDLC
  3139. {
  3140. info->netdev->stats.rx_errors++;
  3141. info->netdev->stats.rx_frame_errors++;
  3142. }
  3143. #endif
  3144. } else
  3145. return_frame = true;
  3146. if (return_frame)
  3147. framesize = buf->count;
  3148. if (debug_level >= DEBUG_LEVEL_BH)
  3149. printk("%s(%d):rx_get_frame(%s) status=%04X size=%d\n",
  3150. __FILE__, __LINE__, info->device_name, status, framesize);
  3151. if (debug_level >= DEBUG_LEVEL_DATA)
  3152. trace_block(info, buf->data, framesize, 0);
  3153. if (framesize) {
  3154. if ((info->params.crc_type & HDLC_CRC_RETURN_EX &&
  3155. framesize+1 > info->max_frame_size) ||
  3156. framesize > info->max_frame_size)
  3157. info->icount.rxlong++;
  3158. else {
  3159. if (status & BIT5)
  3160. info->icount.rxok++;
  3161. if (info->params.crc_type & HDLC_CRC_RETURN_EX) {
  3162. *(buf->data + framesize) = status & BIT5 ? RX_OK:RX_CRC_ERROR;
  3163. ++framesize;
  3164. }
  3165. #if SYNCLINK_GENERIC_HDLC
  3166. if (info->netcount)
  3167. hdlcdev_rx(info, buf->data, framesize);
  3168. else
  3169. #endif
  3170. ldisc_receive_buf(tty, buf->data, info->flag_buf, framesize);
  3171. }
  3172. }
  3173. spin_lock_irqsave(&info->lock, flags);
  3174. buf->status = buf->count = 0;
  3175. info->rx_frame_count--;
  3176. info->rx_get++;
  3177. if (info->rx_get >= info->rx_buf_count)
  3178. info->rx_get = 0;
  3179. spin_unlock_irqrestore(&info->lock, flags);
  3180. return true;
  3181. }
  3182. static bool register_test(MGSLPC_INFO *info)
  3183. {
  3184. static unsigned char patterns[] =
  3185. { 0x00, 0xff, 0xaa, 0x55, 0x69, 0x96, 0x0f };
  3186. static unsigned int count = ARRAY_SIZE(patterns);
  3187. unsigned int i;
  3188. bool rc = true;
  3189. unsigned long flags;
  3190. spin_lock_irqsave(&info->lock, flags);
  3191. reset_device(info);
  3192. for (i = 0; i < count; i++) {
  3193. write_reg(info, XAD1, patterns[i]);
  3194. write_reg(info, XAD2, patterns[(i + 1) % count]);
  3195. if ((read_reg(info, XAD1) != patterns[i]) ||
  3196. (read_reg(info, XAD2) != patterns[(i + 1) % count])) {
  3197. rc = false;
  3198. break;
  3199. }
  3200. }
  3201. spin_unlock_irqrestore(&info->lock, flags);
  3202. return rc;
  3203. }
  3204. static bool irq_test(MGSLPC_INFO *info)
  3205. {
  3206. unsigned long end_time;
  3207. unsigned long flags;
  3208. spin_lock_irqsave(&info->lock, flags);
  3209. reset_device(info);
  3210. info->testing_irq = true;
  3211. hdlc_mode(info);
  3212. info->irq_occurred = false;
  3213. /* init hdlc mode */
  3214. irq_enable(info, CHA, IRQ_TIMER);
  3215. write_reg(info, CHA + TIMR, 0); /* 512 cycles */
  3216. issue_command(info, CHA, CMD_START_TIMER);
  3217. spin_unlock_irqrestore(&info->lock, flags);
  3218. end_time=100;
  3219. while(end_time-- && !info->irq_occurred) {
  3220. msleep_interruptible(10);
  3221. }
  3222. info->testing_irq = false;
  3223. spin_lock_irqsave(&info->lock, flags);
  3224. reset_device(info);
  3225. spin_unlock_irqrestore(&info->lock, flags);
  3226. return info->irq_occurred;
  3227. }
  3228. static int adapter_test(MGSLPC_INFO *info)
  3229. {
  3230. if (!register_test(info)) {
  3231. info->init_error = DiagStatus_AddressFailure;
  3232. printk("%s(%d):Register test failure for device %s Addr=%04X\n",
  3233. __FILE__, __LINE__, info->device_name, (unsigned short)(info->io_base));
  3234. return -ENODEV;
  3235. }
  3236. if (!irq_test(info)) {
  3237. info->init_error = DiagStatus_IrqFailure;
  3238. printk("%s(%d):Interrupt test failure for device %s IRQ=%d\n",
  3239. __FILE__, __LINE__, info->device_name, (unsigned short)(info->irq_level));
  3240. return -ENODEV;
  3241. }
  3242. if (debug_level >= DEBUG_LEVEL_INFO)
  3243. printk("%s(%d):device %s passed diagnostics\n",
  3244. __FILE__, __LINE__, info->device_name);
  3245. return 0;
  3246. }
  3247. static void trace_block(MGSLPC_INFO *info,const char* data, int count, int xmit)
  3248. {
  3249. int i;
  3250. int linecount;
  3251. if (xmit)
  3252. printk("%s tx data:\n", info->device_name);
  3253. else
  3254. printk("%s rx data:\n", info->device_name);
  3255. while(count) {
  3256. if (count > 16)
  3257. linecount = 16;
  3258. else
  3259. linecount = count;
  3260. for(i=0;i<linecount;i++)
  3261. printk("%02X ", (unsigned char)data[i]);
  3262. for(;i<17;i++)
  3263. printk(" ");
  3264. for(i=0;i<linecount;i++) {
  3265. if (data[i]>=040 && data[i]<=0176)
  3266. printk("%c", data[i]);
  3267. else
  3268. printk(".");
  3269. }
  3270. printk("\n");
  3271. data += linecount;
  3272. count -= linecount;
  3273. }
  3274. }
  3275. /* HDLC frame time out
  3276. * update stats and do tx completion processing
  3277. */
  3278. static void tx_timeout(unsigned long context)
  3279. {
  3280. MGSLPC_INFO *info = (MGSLPC_INFO*)context;
  3281. unsigned long flags;
  3282. if (debug_level >= DEBUG_LEVEL_INFO)
  3283. printk("%s(%d):tx_timeout(%s)\n",
  3284. __FILE__, __LINE__, info->device_name);
  3285. if (info->tx_active &&
  3286. info->params.mode == MGSL_MODE_HDLC) {
  3287. info->icount.txtimeout++;
  3288. }
  3289. spin_lock_irqsave(&info->lock, flags);
  3290. info->tx_active = false;
  3291. info->tx_count = info->tx_put = info->tx_get = 0;
  3292. spin_unlock_irqrestore(&info->lock, flags);
  3293. #if SYNCLINK_GENERIC_HDLC
  3294. if (info->netcount)
  3295. hdlcdev_tx_done(info);
  3296. else
  3297. #endif
  3298. {
  3299. struct tty_struct *tty = tty_port_tty_get(&info->port);
  3300. bh_transmit(info, tty);
  3301. tty_kref_put(tty);
  3302. }
  3303. }
  3304. #if SYNCLINK_GENERIC_HDLC
  3305. /**
  3306. * called by generic HDLC layer when protocol selected (PPP, frame relay, etc.)
  3307. * set encoding and frame check sequence (FCS) options
  3308. *
  3309. * dev pointer to network device structure
  3310. * encoding serial encoding setting
  3311. * parity FCS setting
  3312. *
  3313. * returns 0 if success, otherwise error code
  3314. */
  3315. static int hdlcdev_attach(struct net_device *dev, unsigned short encoding,
  3316. unsigned short parity)
  3317. {
  3318. MGSLPC_INFO *info = dev_to_port(dev);
  3319. struct tty_struct *tty;
  3320. unsigned char new_encoding;
  3321. unsigned short new_crctype;
  3322. /* return error if TTY interface open */
  3323. if (info->port.count)
  3324. return -EBUSY;
  3325. switch (encoding)
  3326. {
  3327. case ENCODING_NRZ: new_encoding = HDLC_ENCODING_NRZ; break;
  3328. case ENCODING_NRZI: new_encoding = HDLC_ENCODING_NRZI_SPACE; break;
  3329. case ENCODING_FM_MARK: new_encoding = HDLC_ENCODING_BIPHASE_MARK; break;
  3330. case ENCODING_FM_SPACE: new_encoding = HDLC_ENCODING_BIPHASE_SPACE; break;
  3331. case ENCODING_MANCHESTER: new_encoding = HDLC_ENCODING_BIPHASE_LEVEL; break;
  3332. default: return -EINVAL;
  3333. }
  3334. switch (parity)
  3335. {
  3336. case PARITY_NONE: new_crctype = HDLC_CRC_NONE; break;
  3337. case PARITY_CRC16_PR1_CCITT: new_crctype = HDLC_CRC_16_CCITT; break;
  3338. case PARITY_CRC32_PR1_CCITT: new_crctype = HDLC_CRC_32_CCITT; break;
  3339. default: return -EINVAL;
  3340. }
  3341. info->params.encoding = new_encoding;
  3342. info->params.crc_type = new_crctype;
  3343. /* if network interface up, reprogram hardware */
  3344. if (info->netcount) {
  3345. tty = tty_port_tty_get(&info->port);
  3346. mgslpc_program_hw(info, tty);
  3347. tty_kref_put(tty);
  3348. }
  3349. return 0;
  3350. }
  3351. /**
  3352. * called by generic HDLC layer to send frame
  3353. *
  3354. * skb socket buffer containing HDLC frame
  3355. * dev pointer to network device structure
  3356. */
  3357. static netdev_tx_t hdlcdev_xmit(struct sk_buff *skb,
  3358. struct net_device *dev)
  3359. {
  3360. MGSLPC_INFO *info = dev_to_port(dev);
  3361. unsigned long flags;
  3362. if (debug_level >= DEBUG_LEVEL_INFO)
  3363. printk(KERN_INFO "%s:hdlc_xmit(%s)\n", __FILE__, dev->name);
  3364. /* stop sending until this frame completes */
  3365. netif_stop_queue(dev);
  3366. /* copy data to device buffers */
  3367. skb_copy_from_linear_data(skb, info->tx_buf, skb->len);
  3368. info->tx_get = 0;
  3369. info->tx_put = info->tx_count = skb->len;
  3370. /* update network statistics */
  3371. dev->stats.tx_packets++;
  3372. dev->stats.tx_bytes += skb->len;
  3373. /* done with socket buffer, so free it */
  3374. dev_kfree_skb(skb);
  3375. /* save start time for transmit timeout detection */
  3376. dev->trans_start = jiffies;
  3377. /* start hardware transmitter if necessary */
  3378. spin_lock_irqsave(&info->lock, flags);
  3379. if (!info->tx_active) {
  3380. struct tty_struct *tty = tty_port_tty_get(&info->port);
  3381. tx_start(info, tty);
  3382. tty_kref_put(tty);
  3383. }
  3384. spin_unlock_irqrestore(&info->lock, flags);
  3385. return NETDEV_TX_OK;
  3386. }
  3387. /**
  3388. * called by network layer when interface enabled
  3389. * claim resources and initialize hardware
  3390. *
  3391. * dev pointer to network device structure
  3392. *
  3393. * returns 0 if success, otherwise error code
  3394. */
  3395. static int hdlcdev_open(struct net_device *dev)
  3396. {
  3397. MGSLPC_INFO *info = dev_to_port(dev);
  3398. struct tty_struct *tty;
  3399. int rc;
  3400. unsigned long flags;
  3401. if (debug_level >= DEBUG_LEVEL_INFO)
  3402. printk("%s:hdlcdev_open(%s)\n", __FILE__, dev->name);
  3403. /* generic HDLC layer open processing */
  3404. rc = hdlc_open(dev);
  3405. if (rc != 0)
  3406. return rc;
  3407. /* arbitrate between network and tty opens */
  3408. spin_lock_irqsave(&info->netlock, flags);
  3409. if (info->port.count != 0 || info->netcount != 0) {
  3410. printk(KERN_WARNING "%s: hdlc_open returning busy\n", dev->name);
  3411. spin_unlock_irqrestore(&info->netlock, flags);
  3412. return -EBUSY;
  3413. }
  3414. info->netcount=1;
  3415. spin_unlock_irqrestore(&info->netlock, flags);
  3416. tty = tty_port_tty_get(&info->port);
  3417. /* claim resources and init adapter */
  3418. rc = startup(info, tty);
  3419. if (rc != 0) {
  3420. tty_kref_put(tty);
  3421. spin_lock_irqsave(&info->netlock, flags);
  3422. info->netcount=0;
  3423. spin_unlock_irqrestore(&info->netlock, flags);
  3424. return rc;
  3425. }
  3426. /* assert RTS and DTR, apply hardware settings */
  3427. info->serial_signals |= SerialSignal_RTS | SerialSignal_DTR;
  3428. mgslpc_program_hw(info, tty);
  3429. tty_kref_put(tty);
  3430. /* enable network layer transmit */
  3431. dev->trans_start = jiffies;
  3432. netif_start_queue(dev);
  3433. /* inform generic HDLC layer of current DCD status */
  3434. spin_lock_irqsave(&info->lock, flags);
  3435. get_signals(info);
  3436. spin_unlock_irqrestore(&info->lock, flags);
  3437. if (info->serial_signals & SerialSignal_DCD)
  3438. netif_carrier_on(dev);
  3439. else
  3440. netif_carrier_off(dev);
  3441. return 0;
  3442. }
  3443. /**
  3444. * called by network layer when interface is disabled
  3445. * shutdown hardware and release resources
  3446. *
  3447. * dev pointer to network device structure
  3448. *
  3449. * returns 0 if success, otherwise error code
  3450. */
  3451. static int hdlcdev_close(struct net_device *dev)
  3452. {
  3453. MGSLPC_INFO *info = dev_to_port(dev);
  3454. struct tty_struct *tty = tty_port_tty_get(&info->port);
  3455. unsigned long flags;
  3456. if (debug_level >= DEBUG_LEVEL_INFO)
  3457. printk("%s:hdlcdev_close(%s)\n", __FILE__, dev->name);
  3458. netif_stop_queue(dev);
  3459. /* shutdown adapter and release resources */
  3460. shutdown(info, tty);
  3461. tty_kref_put(tty);
  3462. hdlc_close(dev);
  3463. spin_lock_irqsave(&info->netlock, flags);
  3464. info->netcount=0;
  3465. spin_unlock_irqrestore(&info->netlock, flags);
  3466. return 0;
  3467. }
  3468. /**
  3469. * called by network layer to process IOCTL call to network device
  3470. *
  3471. * dev pointer to network device structure
  3472. * ifr pointer to network interface request structure
  3473. * cmd IOCTL command code
  3474. *
  3475. * returns 0 if success, otherwise error code
  3476. */
  3477. static int hdlcdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  3478. {
  3479. const size_t size = sizeof(sync_serial_settings);
  3480. sync_serial_settings new_line;
  3481. sync_serial_settings __user *line = ifr->ifr_settings.ifs_ifsu.sync;
  3482. MGSLPC_INFO *info = dev_to_port(dev);
  3483. unsigned int flags;
  3484. if (debug_level >= DEBUG_LEVEL_INFO)
  3485. printk("%s:hdlcdev_ioctl(%s)\n", __FILE__, dev->name);
  3486. /* return error if TTY interface open */
  3487. if (info->port.count)
  3488. return -EBUSY;
  3489. if (cmd != SIOCWANDEV)
  3490. return hdlc_ioctl(dev, ifr, cmd);
  3491. memset(&new_line, 0, size);
  3492. switch(ifr->ifr_settings.type) {
  3493. case IF_GET_IFACE: /* return current sync_serial_settings */
  3494. ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
  3495. if (ifr->ifr_settings.size < size) {
  3496. ifr->ifr_settings.size = size; /* data size wanted */
  3497. return -ENOBUFS;
  3498. }
  3499. flags = info->params.flags & (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
  3500. HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
  3501. HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
  3502. HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
  3503. switch (flags){
  3504. case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN): new_line.clock_type = CLOCK_EXT; break;
  3505. case (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_INT; break;
  3506. case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG): new_line.clock_type = CLOCK_TXINT; break;
  3507. case (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN): new_line.clock_type = CLOCK_TXFROMRX; break;
  3508. default: new_line.clock_type = CLOCK_DEFAULT;
  3509. }
  3510. new_line.clock_rate = info->params.clock_speed;
  3511. new_line.loopback = info->params.loopback ? 1:0;
  3512. if (copy_to_user(line, &new_line, size))
  3513. return -EFAULT;
  3514. return 0;
  3515. case IF_IFACE_SYNC_SERIAL: /* set sync_serial_settings */
  3516. if(!capable(CAP_NET_ADMIN))
  3517. return -EPERM;
  3518. if (copy_from_user(&new_line, line, size))
  3519. return -EFAULT;
  3520. switch (new_line.clock_type)
  3521. {
  3522. case CLOCK_EXT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_TXCPIN; break;
  3523. case CLOCK_TXFROMRX: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_RXCPIN; break;
  3524. case CLOCK_INT: flags = HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG; break;
  3525. case CLOCK_TXINT: flags = HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_TXC_BRG; break;
  3526. case CLOCK_DEFAULT: flags = info->params.flags &
  3527. (HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
  3528. HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
  3529. HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
  3530. HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN); break;
  3531. default: return -EINVAL;
  3532. }
  3533. if (new_line.loopback != 0 && new_line.loopback != 1)
  3534. return -EINVAL;
  3535. info->params.flags &= ~(HDLC_FLAG_RXC_RXCPIN | HDLC_FLAG_RXC_DPLL |
  3536. HDLC_FLAG_RXC_BRG | HDLC_FLAG_RXC_TXCPIN |
  3537. HDLC_FLAG_TXC_TXCPIN | HDLC_FLAG_TXC_DPLL |
  3538. HDLC_FLAG_TXC_BRG | HDLC_FLAG_TXC_RXCPIN);
  3539. info->params.flags |= flags;
  3540. info->params.loopback = new_line.loopback;
  3541. if (flags & (HDLC_FLAG_RXC_BRG | HDLC_FLAG_TXC_BRG))
  3542. info->params.clock_speed = new_line.clock_rate;
  3543. else
  3544. info->params.clock_speed = 0;
  3545. /* if network interface up, reprogram hardware */
  3546. if (info->netcount) {
  3547. struct tty_struct *tty = tty_port_tty_get(&info->port);
  3548. mgslpc_program_hw(info, tty);
  3549. tty_kref_put(tty);
  3550. }
  3551. return 0;
  3552. default:
  3553. return hdlc_ioctl(dev, ifr, cmd);
  3554. }
  3555. }
  3556. /**
  3557. * called by network layer when transmit timeout is detected
  3558. *
  3559. * dev pointer to network device structure
  3560. */
  3561. static void hdlcdev_tx_timeout(struct net_device *dev)
  3562. {
  3563. MGSLPC_INFO *info = dev_to_port(dev);
  3564. unsigned long flags;
  3565. if (debug_level >= DEBUG_LEVEL_INFO)
  3566. printk("hdlcdev_tx_timeout(%s)\n", dev->name);
  3567. dev->stats.tx_errors++;
  3568. dev->stats.tx_aborted_errors++;
  3569. spin_lock_irqsave(&info->lock, flags);
  3570. tx_stop(info);
  3571. spin_unlock_irqrestore(&info->lock, flags);
  3572. netif_wake_queue(dev);
  3573. }
  3574. /**
  3575. * called by device driver when transmit completes
  3576. * reenable network layer transmit if stopped
  3577. *
  3578. * info pointer to device instance information
  3579. */
  3580. static void hdlcdev_tx_done(MGSLPC_INFO *info)
  3581. {
  3582. if (netif_queue_stopped(info->netdev))
  3583. netif_wake_queue(info->netdev);
  3584. }
  3585. /**
  3586. * called by device driver when frame received
  3587. * pass frame to network layer
  3588. *
  3589. * info pointer to device instance information
  3590. * buf pointer to buffer contianing frame data
  3591. * size count of data bytes in buf
  3592. */
  3593. static void hdlcdev_rx(MGSLPC_INFO *info, char *buf, int size)
  3594. {
  3595. struct sk_buff *skb = dev_alloc_skb(size);
  3596. struct net_device *dev = info->netdev;
  3597. if (debug_level >= DEBUG_LEVEL_INFO)
  3598. printk("hdlcdev_rx(%s)\n", dev->name);
  3599. if (skb == NULL) {
  3600. printk(KERN_NOTICE "%s: can't alloc skb, dropping packet\n", dev->name);
  3601. dev->stats.rx_dropped++;
  3602. return;
  3603. }
  3604. memcpy(skb_put(skb, size), buf, size);
  3605. skb->protocol = hdlc_type_trans(skb, dev);
  3606. dev->stats.rx_packets++;
  3607. dev->stats.rx_bytes += size;
  3608. netif_rx(skb);
  3609. }
  3610. static const struct net_device_ops hdlcdev_ops = {
  3611. .ndo_open = hdlcdev_open,
  3612. .ndo_stop = hdlcdev_close,
  3613. .ndo_change_mtu = hdlc_change_mtu,
  3614. .ndo_start_xmit = hdlc_start_xmit,
  3615. .ndo_do_ioctl = hdlcdev_ioctl,
  3616. .ndo_tx_timeout = hdlcdev_tx_timeout,
  3617. };
  3618. /**
  3619. * called by device driver when adding device instance
  3620. * do generic HDLC initialization
  3621. *
  3622. * info pointer to device instance information
  3623. *
  3624. * returns 0 if success, otherwise error code
  3625. */
  3626. static int hdlcdev_init(MGSLPC_INFO *info)
  3627. {
  3628. int rc;
  3629. struct net_device *dev;
  3630. hdlc_device *hdlc;
  3631. /* allocate and initialize network and HDLC layer objects */
  3632. dev = alloc_hdlcdev(info);
  3633. if (dev == NULL) {
  3634. printk(KERN_ERR "%s:hdlc device allocation failure\n", __FILE__);
  3635. return -ENOMEM;
  3636. }
  3637. /* for network layer reporting purposes only */
  3638. dev->base_addr = info->io_base;
  3639. dev->irq = info->irq_level;
  3640. /* network layer callbacks and settings */
  3641. dev->netdev_ops = &hdlcdev_ops;
  3642. dev->watchdog_timeo = 10 * HZ;
  3643. dev->tx_queue_len = 50;
  3644. /* generic HDLC layer callbacks and settings */
  3645. hdlc = dev_to_hdlc(dev);
  3646. hdlc->attach = hdlcdev_attach;
  3647. hdlc->xmit = hdlcdev_xmit;
  3648. /* register objects with HDLC layer */
  3649. rc = register_hdlc_device(dev);
  3650. if (rc) {
  3651. printk(KERN_WARNING "%s:unable to register hdlc device\n", __FILE__);
  3652. free_netdev(dev);
  3653. return rc;
  3654. }
  3655. info->netdev = dev;
  3656. return 0;
  3657. }
  3658. /**
  3659. * called by device driver when removing device instance
  3660. * do generic HDLC cleanup
  3661. *
  3662. * info pointer to device instance information
  3663. */
  3664. static void hdlcdev_exit(MGSLPC_INFO *info)
  3665. {
  3666. unregister_hdlc_device(info->netdev);
  3667. free_netdev(info->netdev);
  3668. info->netdev = NULL;
  3669. }
  3670. #endif /* CONFIG_HDLC */