synclink_gt.c 112 KB

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