synclink_cs.c 108 KB

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