synclink_gt.c 112 KB

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