n_gsm.c 114 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * n_gsm.c GSM 0710 tty multiplexor
  4. * Copyright (c) 2009/10 Intel Corporation
  5. * Copyright (c) 2022/23 Siemens Mobility GmbH
  6. *
  7. * * THIS IS A DEVELOPMENT SNAPSHOT IT IS NOT A FINAL RELEASE *
  8. *
  9. * Outgoing path:
  10. * tty -> DLCI fifo -> scheduler -> GSM MUX data queue ---o-> ldisc
  11. * control message -> GSM MUX control queue --´
  12. *
  13. * Incoming path:
  14. * ldisc -> gsm_queue() -o--> tty
  15. * `-> gsm_control_response()
  16. *
  17. * TO DO:
  18. * Mostly done: ioctls for setting modes/timing
  19. * Partly done: hooks so you can pull off frames to non tty devs
  20. * Restart DLCI 0 when it closes ?
  21. * Improve the tx engine
  22. * Resolve tx side locking by adding a queue_head and routing
  23. * all control traffic via it
  24. * General tidy/document
  25. * Review the locking/move to refcounts more (mux now moved to an
  26. * alloc/free model ready)
  27. * Use newest tty open/close port helpers and install hooks
  28. * What to do about power functions ?
  29. * Termios setting and negotiation
  30. * Do we need a 'which mux are you' ioctl to correlate mux and tty sets
  31. *
  32. */
  33. #include <linux/types.h>
  34. #include <linux/major.h>
  35. #include <linux/errno.h>
  36. #include <linux/signal.h>
  37. #include <linux/fcntl.h>
  38. #include <linux/sched/signal.h>
  39. #include <linux/interrupt.h>
  40. #include <linux/tty.h>
  41. #include <linux/bitfield.h>
  42. #include <linux/ctype.h>
  43. #include <linux/mm.h>
  44. #include <linux/math.h>
  45. #include <linux/nospec.h>
  46. #include <linux/string.h>
  47. #include <linux/slab.h>
  48. #include <linux/poll.h>
  49. #include <linux/bitops.h>
  50. #include <linux/file.h>
  51. #include <linux/uaccess.h>
  52. #include <linux/module.h>
  53. #include <linux/timer.h>
  54. #include <linux/tty_flip.h>
  55. #include <linux/tty_driver.h>
  56. #include <linux/serial.h>
  57. #include <linux/kfifo.h>
  58. #include <linux/skbuff.h>
  59. #include <net/arp.h>
  60. #include <linux/ip.h>
  61. #include <linux/netdevice.h>
  62. #include <linux/etherdevice.h>
  63. #include <linux/gsmmux.h>
  64. #include "tty.h"
  65. static int debug;
  66. module_param(debug, int, 0600);
  67. /* Module debug bits */
  68. #define DBG_DUMP BIT(0) /* Data transmission dump. */
  69. #define DBG_CD_ON BIT(1) /* Always assume CD line on. */
  70. #define DBG_DATA BIT(2) /* Data transmission details. */
  71. #define DBG_ERRORS BIT(3) /* Details for fail conditions. */
  72. #define DBG_TTY BIT(4) /* Transmission statistics for DLCI TTYs. */
  73. #define DBG_PAYLOAD BIT(5) /* Limits DBG_DUMP to payload frames. */
  74. /* Defaults: these are from the specification */
  75. #define T1 10 /* 100mS */
  76. #define T2 34 /* 333mS */
  77. #define T3 10 /* 10s */
  78. #define N2 3 /* Retry 3 times */
  79. #define K 2 /* outstanding I frames */
  80. #define MAX_T3 255 /* In seconds. */
  81. #define MAX_WINDOW_SIZE 7 /* Limit of K in error recovery mode. */
  82. /* Use long timers for testing at low speed with debug on */
  83. #ifdef DEBUG_TIMING
  84. #define T1 100
  85. #define T2 200
  86. #endif
  87. /*
  88. * Semi-arbitrary buffer size limits. 0710 is normally run with 32-64 byte
  89. * limits so this is plenty
  90. */
  91. #define MAX_MRU 1500
  92. #define MAX_MTU 1500
  93. #define MIN_MTU (PROT_OVERHEAD + 1)
  94. /* SOF, ADDR, CTRL, LEN1, LEN2, ..., FCS, EOF */
  95. #define PROT_OVERHEAD 7
  96. #define GSM_NET_TX_TIMEOUT (HZ*10)
  97. /*
  98. * struct gsm_mux_net - network interface
  99. *
  100. * Created when net interface is initialized.
  101. */
  102. struct gsm_mux_net {
  103. struct kref ref;
  104. struct gsm_dlci *dlci;
  105. };
  106. /*
  107. * Each block of data we have queued to go out is in the form of
  108. * a gsm_msg which holds everything we need in a link layer independent
  109. * format
  110. */
  111. struct gsm_msg {
  112. struct list_head list;
  113. u8 addr; /* DLCI address + flags */
  114. u8 ctrl; /* Control byte + flags */
  115. unsigned int len; /* Length of data block (can be zero) */
  116. u8 *data; /* Points into buffer but not at the start */
  117. u8 buffer[];
  118. };
  119. enum gsm_dlci_state {
  120. DLCI_CLOSED,
  121. DLCI_WAITING_CONFIG, /* Waiting for DLCI configuration from user */
  122. DLCI_CONFIGURE, /* Sending PN (for adaption > 1) */
  123. DLCI_OPENING, /* Sending SABM not seen UA */
  124. DLCI_OPEN, /* SABM/UA complete */
  125. DLCI_CLOSING, /* Sending DISC not seen UA/DM */
  126. };
  127. enum gsm_dlci_mode {
  128. DLCI_MODE_ABM, /* Normal Asynchronous Balanced Mode */
  129. DLCI_MODE_ADM, /* Asynchronous Disconnected Mode */
  130. };
  131. /*
  132. * Each active data link has a gsm_dlci structure associated which ties
  133. * the link layer to an optional tty (if the tty side is open). To avoid
  134. * complexity right now these are only ever freed up when the mux is
  135. * shut down.
  136. *
  137. * At the moment we don't free DLCI objects until the mux is torn down
  138. * this avoid object life time issues but might be worth review later.
  139. */
  140. struct gsm_dlci {
  141. struct gsm_mux *gsm;
  142. int addr;
  143. enum gsm_dlci_state state;
  144. struct mutex mutex;
  145. /* Link layer */
  146. enum gsm_dlci_mode mode;
  147. spinlock_t lock; /* Protects the internal state */
  148. struct timer_list t1; /* Retransmit timer for SABM and UA */
  149. int retries;
  150. /* Uplink tty if active */
  151. struct tty_port port; /* The tty bound to this DLCI if there is one */
  152. #define TX_SIZE 4096 /* Must be power of 2. */
  153. struct kfifo fifo; /* Queue fifo for the DLCI */
  154. int adaption; /* Adaption layer in use */
  155. int prev_adaption;
  156. u32 modem_rx; /* Our incoming virtual modem lines */
  157. u32 modem_tx; /* Our outgoing modem lines */
  158. unsigned int mtu;
  159. bool dead; /* Refuse re-open */
  160. /* Configuration */
  161. u8 prio; /* Priority */
  162. u8 ftype; /* Frame type */
  163. u8 k; /* Window size */
  164. /* Flow control */
  165. bool throttled; /* Private copy of throttle state */
  166. bool constipated; /* Throttle status for outgoing */
  167. /* Packetised I/O */
  168. struct sk_buff *skb; /* Frame being sent */
  169. struct sk_buff_head skb_list; /* Queued frames */
  170. /* Data handling callback */
  171. void (*data)(struct gsm_dlci *dlci, const u8 *data, int len);
  172. void (*prev_data)(struct gsm_dlci *dlci, const u8 *data, int len);
  173. struct net_device *net; /* network interface, if created */
  174. };
  175. /*
  176. * Parameter bits used for parameter negotiation according to 3GPP 27.010
  177. * chapter 5.4.6.3.1.
  178. */
  179. struct gsm_dlci_param_bits {
  180. u8 d_bits;
  181. u8 i_cl_bits;
  182. u8 p_bits;
  183. u8 t_bits;
  184. __le16 n_bits;
  185. u8 na_bits;
  186. u8 k_bits;
  187. };
  188. static_assert(sizeof(struct gsm_dlci_param_bits) == 8);
  189. #define PN_D_FIELD_DLCI GENMASK(5, 0)
  190. #define PN_I_CL_FIELD_FTYPE GENMASK(3, 0)
  191. #define PN_I_CL_FIELD_ADAPTION GENMASK(7, 4)
  192. #define PN_P_FIELD_PRIO GENMASK(5, 0)
  193. #define PN_T_FIELD_T1 GENMASK(7, 0)
  194. #define PN_N_FIELD_N1 GENMASK(15, 0)
  195. #define PN_NA_FIELD_N2 GENMASK(7, 0)
  196. #define PN_K_FIELD_K GENMASK(2, 0)
  197. /* Total number of supported devices */
  198. #define GSM_TTY_MINORS 256
  199. /* DLCI 0, 62/63 are special or reserved see gsmtty_open */
  200. #define NUM_DLCI 64
  201. /*
  202. * DLCI 0 is used to pass control blocks out of band of the data
  203. * flow (and with a higher link priority). One command can be outstanding
  204. * at a time and we use this structure to manage them. They are created
  205. * and destroyed by the user context, and updated by the receive paths
  206. * and timers
  207. */
  208. struct gsm_control {
  209. u8 cmd; /* Command we are issuing */
  210. u8 *data; /* Data for the command in case we retransmit */
  211. int len; /* Length of block for retransmission */
  212. int done; /* Done flag */
  213. int error; /* Error if any */
  214. };
  215. enum gsm_encoding {
  216. GSM_BASIC_OPT,
  217. GSM_ADV_OPT,
  218. };
  219. enum gsm_mux_state {
  220. GSM_SEARCH,
  221. GSM0_ADDRESS,
  222. GSM0_CONTROL,
  223. GSM0_LEN0,
  224. GSM0_LEN1,
  225. GSM0_DATA,
  226. GSM0_FCS,
  227. GSM0_SSOF,
  228. GSM1_START,
  229. GSM1_ADDRESS,
  230. GSM1_CONTROL,
  231. GSM1_DATA,
  232. GSM1_OVERRUN,
  233. };
  234. /*
  235. * Each GSM mux we have is represented by this structure. If we are
  236. * operating as an ldisc then we use this structure as our ldisc
  237. * state. We need to sort out lifetimes and locking with respect
  238. * to the gsm mux array. For now we don't free DLCI objects that
  239. * have been instantiated until the mux itself is terminated.
  240. *
  241. * To consider further: tty open versus mux shutdown.
  242. */
  243. struct gsm_mux {
  244. struct tty_struct *tty; /* The tty our ldisc is bound to */
  245. spinlock_t lock;
  246. struct mutex mutex;
  247. unsigned int num;
  248. struct kref ref;
  249. /* Events on the GSM channel */
  250. wait_queue_head_t event;
  251. /* ldisc send work */
  252. struct work_struct tx_work;
  253. /* Bits for GSM mode decoding */
  254. /* Framing Layer */
  255. u8 *buf;
  256. enum gsm_mux_state state;
  257. unsigned int len;
  258. unsigned int address;
  259. unsigned int count;
  260. bool escape;
  261. enum gsm_encoding encoding;
  262. u8 control;
  263. u8 fcs;
  264. u8 *txframe; /* TX framing buffer */
  265. /* Method for the receiver side */
  266. void (*receive)(struct gsm_mux *gsm, u8 ch);
  267. /* Link Layer */
  268. unsigned int mru;
  269. unsigned int mtu;
  270. int initiator; /* Did we initiate connection */
  271. bool dead; /* Has the mux been shut down */
  272. struct gsm_dlci *dlci[NUM_DLCI];
  273. int old_c_iflag; /* termios c_iflag value before attach */
  274. bool constipated; /* Asked by remote to shut up */
  275. bool has_devices; /* Devices were registered */
  276. spinlock_t tx_lock;
  277. unsigned int tx_bytes; /* TX data outstanding */
  278. #define TX_THRESH_HI 8192
  279. #define TX_THRESH_LO 2048
  280. struct list_head tx_ctrl_list; /* Pending control packets */
  281. struct list_head tx_data_list; /* Pending data packets */
  282. /* Control messages */
  283. struct timer_list kick_timer; /* Kick TX queuing on timeout */
  284. struct timer_list t2_timer; /* Retransmit timer for commands */
  285. int cretries; /* Command retry counter */
  286. struct gsm_control *pending_cmd;/* Our current pending command */
  287. spinlock_t control_lock; /* Protects the pending command */
  288. /* Keep-alive */
  289. struct timer_list ka_timer; /* Keep-alive response timer */
  290. u8 ka_num; /* Keep-alive match pattern */
  291. signed int ka_retries; /* Keep-alive retry counter, -1 if not yet initialized */
  292. /* Configuration */
  293. int adaption; /* 1 or 2 supported */
  294. u8 ftype; /* UI or UIH */
  295. int t1, t2; /* Timers in 1/100th of a sec */
  296. unsigned int t3; /* Power wake-up timer in seconds. */
  297. int n2; /* Retry count */
  298. u8 k; /* Window size */
  299. bool wait_config; /* Wait for configuration by ioctl before DLCI open */
  300. u32 keep_alive; /* Control channel keep-alive in 10ms */
  301. /* Statistics (not currently exposed) */
  302. unsigned long bad_fcs;
  303. unsigned long malformed;
  304. unsigned long io_error;
  305. unsigned long open_error;
  306. unsigned long bad_size;
  307. unsigned long unsupported;
  308. };
  309. /*
  310. * Mux objects - needed so that we can translate a tty index into the
  311. * relevant mux and DLCI.
  312. */
  313. #define MAX_MUX 4 /* 256 minors */
  314. static struct gsm_mux *gsm_mux[MAX_MUX]; /* GSM muxes */
  315. static DEFINE_SPINLOCK(gsm_mux_lock);
  316. static struct tty_driver *gsm_tty_driver;
  317. /*
  318. * This section of the driver logic implements the GSM encodings
  319. * both the basic and the 'advanced'. Reliable transport is not
  320. * supported.
  321. */
  322. #define CR 0x02
  323. #define EA 0x01
  324. #define PF 0x10
  325. /* I is special: the rest are ..*/
  326. #define RR 0x01
  327. #define UI 0x03
  328. #define RNR 0x05
  329. #define REJ 0x09
  330. #define DM 0x0F
  331. #define SABM 0x2F
  332. #define DISC 0x43
  333. #define UA 0x63
  334. #define UIH 0xEF
  335. /* Channel commands */
  336. #define CMD_NSC 0x09
  337. #define CMD_TEST 0x11
  338. #define CMD_PSC 0x21
  339. #define CMD_RLS 0x29
  340. #define CMD_FCOFF 0x31
  341. #define CMD_PN 0x41
  342. #define CMD_RPN 0x49
  343. #define CMD_FCON 0x51
  344. #define CMD_CLD 0x61
  345. #define CMD_SNC 0x69
  346. #define CMD_MSC 0x71
  347. /* Virtual modem bits */
  348. #define MDM_FC 0x01
  349. #define MDM_RTC 0x02
  350. #define MDM_RTR 0x04
  351. #define MDM_IC 0x20
  352. #define MDM_DV 0x40
  353. #define GSM0_SOF 0xF9
  354. #define GSM1_SOF 0x7E
  355. #define GSM1_ESCAPE 0x7D
  356. #define GSM1_ESCAPE_BITS 0x20
  357. #define XON 0x11
  358. #define XOFF 0x13
  359. #define ISO_IEC_646_MASK 0x7F
  360. static const struct tty_port_operations gsm_port_ops;
  361. /*
  362. * CRC table for GSM 0710
  363. */
  364. static const u8 gsm_fcs8[256] = {
  365. 0x00, 0x91, 0xE3, 0x72, 0x07, 0x96, 0xE4, 0x75,
  366. 0x0E, 0x9F, 0xED, 0x7C, 0x09, 0x98, 0xEA, 0x7B,
  367. 0x1C, 0x8D, 0xFF, 0x6E, 0x1B, 0x8A, 0xF8, 0x69,
  368. 0x12, 0x83, 0xF1, 0x60, 0x15, 0x84, 0xF6, 0x67,
  369. 0x38, 0xA9, 0xDB, 0x4A, 0x3F, 0xAE, 0xDC, 0x4D,
  370. 0x36, 0xA7, 0xD5, 0x44, 0x31, 0xA0, 0xD2, 0x43,
  371. 0x24, 0xB5, 0xC7, 0x56, 0x23, 0xB2, 0xC0, 0x51,
  372. 0x2A, 0xBB, 0xC9, 0x58, 0x2D, 0xBC, 0xCE, 0x5F,
  373. 0x70, 0xE1, 0x93, 0x02, 0x77, 0xE6, 0x94, 0x05,
  374. 0x7E, 0xEF, 0x9D, 0x0C, 0x79, 0xE8, 0x9A, 0x0B,
  375. 0x6C, 0xFD, 0x8F, 0x1E, 0x6B, 0xFA, 0x88, 0x19,
  376. 0x62, 0xF3, 0x81, 0x10, 0x65, 0xF4, 0x86, 0x17,
  377. 0x48, 0xD9, 0xAB, 0x3A, 0x4F, 0xDE, 0xAC, 0x3D,
  378. 0x46, 0xD7, 0xA5, 0x34, 0x41, 0xD0, 0xA2, 0x33,
  379. 0x54, 0xC5, 0xB7, 0x26, 0x53, 0xC2, 0xB0, 0x21,
  380. 0x5A, 0xCB, 0xB9, 0x28, 0x5D, 0xCC, 0xBE, 0x2F,
  381. 0xE0, 0x71, 0x03, 0x92, 0xE7, 0x76, 0x04, 0x95,
  382. 0xEE, 0x7F, 0x0D, 0x9C, 0xE9, 0x78, 0x0A, 0x9B,
  383. 0xFC, 0x6D, 0x1F, 0x8E, 0xFB, 0x6A, 0x18, 0x89,
  384. 0xF2, 0x63, 0x11, 0x80, 0xF5, 0x64, 0x16, 0x87,
  385. 0xD8, 0x49, 0x3B, 0xAA, 0xDF, 0x4E, 0x3C, 0xAD,
  386. 0xD6, 0x47, 0x35, 0xA4, 0xD1, 0x40, 0x32, 0xA3,
  387. 0xC4, 0x55, 0x27, 0xB6, 0xC3, 0x52, 0x20, 0xB1,
  388. 0xCA, 0x5B, 0x29, 0xB8, 0xCD, 0x5C, 0x2E, 0xBF,
  389. 0x90, 0x01, 0x73, 0xE2, 0x97, 0x06, 0x74, 0xE5,
  390. 0x9E, 0x0F, 0x7D, 0xEC, 0x99, 0x08, 0x7A, 0xEB,
  391. 0x8C, 0x1D, 0x6F, 0xFE, 0x8B, 0x1A, 0x68, 0xF9,
  392. 0x82, 0x13, 0x61, 0xF0, 0x85, 0x14, 0x66, 0xF7,
  393. 0xA8, 0x39, 0x4B, 0xDA, 0xAF, 0x3E, 0x4C, 0xDD,
  394. 0xA6, 0x37, 0x45, 0xD4, 0xA1, 0x30, 0x42, 0xD3,
  395. 0xB4, 0x25, 0x57, 0xC6, 0xB3, 0x22, 0x50, 0xC1,
  396. 0xBA, 0x2B, 0x59, 0xC8, 0xBD, 0x2C, 0x5E, 0xCF
  397. };
  398. #define INIT_FCS 0xFF
  399. #define GOOD_FCS 0xCF
  400. static void gsm_dlci_close(struct gsm_dlci *dlci);
  401. static int gsmld_output(struct gsm_mux *gsm, u8 *data, int len);
  402. static int gsm_modem_update(struct gsm_dlci *dlci, u8 brk);
  403. static struct gsm_msg *gsm_data_alloc(struct gsm_mux *gsm, u8 addr, int len,
  404. u8 ctrl);
  405. static int gsm_send_packet(struct gsm_mux *gsm, struct gsm_msg *msg);
  406. static struct gsm_dlci *gsm_dlci_alloc(struct gsm_mux *gsm, int addr);
  407. static void gsmld_write_trigger(struct gsm_mux *gsm);
  408. static void gsmld_write_task(struct work_struct *work);
  409. static int gsm_modem_send_initial_msc(struct gsm_dlci *dlci);
  410. /**
  411. * gsm_fcs_add - update FCS
  412. * @fcs: Current FCS
  413. * @c: Next data
  414. *
  415. * Update the FCS to include c. Uses the algorithm in the specification
  416. * notes.
  417. */
  418. static inline u8 gsm_fcs_add(u8 fcs, u8 c)
  419. {
  420. return gsm_fcs8[fcs ^ c];
  421. }
  422. /**
  423. * gsm_fcs_add_block - update FCS for a block
  424. * @fcs: Current FCS
  425. * @c: buffer of data
  426. * @len: length of buffer
  427. *
  428. * Update the FCS to include c. Uses the algorithm in the specification
  429. * notes.
  430. */
  431. static inline u8 gsm_fcs_add_block(u8 fcs, u8 *c, int len)
  432. {
  433. while (len--)
  434. fcs = gsm_fcs8[fcs ^ *c++];
  435. return fcs;
  436. }
  437. /**
  438. * gsm_read_ea - read a byte into an EA
  439. * @val: variable holding value
  440. * @c: byte going into the EA
  441. *
  442. * Processes one byte of an EA. Updates the passed variable
  443. * and returns 1 if the EA is now completely read
  444. */
  445. static int gsm_read_ea(unsigned int *val, u8 c)
  446. {
  447. /* Add the next 7 bits into the value */
  448. *val <<= 7;
  449. *val |= c >> 1;
  450. /* Was this the last byte of the EA 1 = yes*/
  451. return c & EA;
  452. }
  453. /**
  454. * gsm_read_ea_val - read a value until EA
  455. * @val: variable holding value
  456. * @data: buffer of data
  457. * @dlen: length of data
  458. *
  459. * Processes an EA value. Updates the passed variable and
  460. * returns the processed data length.
  461. */
  462. static unsigned int gsm_read_ea_val(unsigned int *val, const u8 *data, int dlen)
  463. {
  464. unsigned int len = 0;
  465. for (; dlen > 0; dlen--) {
  466. len++;
  467. if (gsm_read_ea(val, *data++))
  468. break;
  469. }
  470. return len;
  471. }
  472. /**
  473. * gsm_encode_modem - encode modem data bits
  474. * @dlci: DLCI to encode from
  475. *
  476. * Returns the correct GSM encoded modem status bits (6 bit field) for
  477. * the current status of the DLCI and attached tty object
  478. */
  479. static u8 gsm_encode_modem(const struct gsm_dlci *dlci)
  480. {
  481. u8 modembits = 0;
  482. /* FC is true flow control not modem bits */
  483. if (dlci->throttled)
  484. modembits |= MDM_FC;
  485. if (dlci->modem_tx & TIOCM_DTR)
  486. modembits |= MDM_RTC;
  487. if (dlci->modem_tx & TIOCM_RTS)
  488. modembits |= MDM_RTR;
  489. if (dlci->modem_tx & TIOCM_RI)
  490. modembits |= MDM_IC;
  491. if (dlci->modem_tx & TIOCM_CD || dlci->gsm->initiator)
  492. modembits |= MDM_DV;
  493. /* special mappings for passive side to operate as UE */
  494. if (dlci->modem_tx & TIOCM_OUT1)
  495. modembits |= MDM_IC;
  496. if (dlci->modem_tx & TIOCM_OUT2)
  497. modembits |= MDM_DV;
  498. return modembits;
  499. }
  500. static void gsm_hex_dump_bytes(const char *fname, const u8 *data,
  501. unsigned long len)
  502. {
  503. char *prefix;
  504. if (!fname) {
  505. print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE, 16, 1, data, len,
  506. true);
  507. return;
  508. }
  509. prefix = kasprintf(GFP_ATOMIC, "%s: ", fname);
  510. if (!prefix)
  511. return;
  512. print_hex_dump(KERN_INFO, prefix, DUMP_PREFIX_OFFSET, 16, 1, data, len,
  513. true);
  514. kfree(prefix);
  515. }
  516. /**
  517. * gsm_encode_params - encode DLCI parameters
  518. * @dlci: DLCI to encode from
  519. * @params: buffer to fill with the encoded parameters
  520. *
  521. * Encodes the parameters according to GSM 07.10 section 5.4.6.3.1
  522. * table 3.
  523. */
  524. static int gsm_encode_params(const struct gsm_dlci *dlci,
  525. struct gsm_dlci_param_bits *params)
  526. {
  527. const struct gsm_mux *gsm = dlci->gsm;
  528. unsigned int i, cl;
  529. switch (dlci->ftype) {
  530. case UIH:
  531. i = 0; /* UIH */
  532. break;
  533. case UI:
  534. i = 1; /* UI */
  535. break;
  536. default:
  537. pr_debug("unsupported frame type %d\n", dlci->ftype);
  538. return -EINVAL;
  539. }
  540. switch (dlci->adaption) {
  541. case 1: /* Unstructured */
  542. cl = 0; /* convergence layer type 1 */
  543. break;
  544. case 2: /* Unstructured with modem bits. */
  545. cl = 1; /* convergence layer type 2 */
  546. break;
  547. default:
  548. pr_debug("unsupported adaption %d\n", dlci->adaption);
  549. return -EINVAL;
  550. }
  551. params->d_bits = FIELD_PREP(PN_D_FIELD_DLCI, dlci->addr);
  552. /* UIH, convergence layer type 1 */
  553. params->i_cl_bits = FIELD_PREP(PN_I_CL_FIELD_FTYPE, i) |
  554. FIELD_PREP(PN_I_CL_FIELD_ADAPTION, cl);
  555. params->p_bits = FIELD_PREP(PN_P_FIELD_PRIO, dlci->prio);
  556. params->t_bits = FIELD_PREP(PN_T_FIELD_T1, gsm->t1);
  557. params->n_bits = cpu_to_le16(FIELD_PREP(PN_N_FIELD_N1, dlci->mtu));
  558. params->na_bits = FIELD_PREP(PN_NA_FIELD_N2, gsm->n2);
  559. params->k_bits = FIELD_PREP(PN_K_FIELD_K, dlci->k);
  560. return 0;
  561. }
  562. /**
  563. * gsm_register_devices - register all tty devices for a given mux index
  564. *
  565. * @driver: the tty driver that describes the tty devices
  566. * @index: the mux number is used to calculate the minor numbers of the
  567. * ttys for this mux and may differ from the position in the
  568. * mux array.
  569. */
  570. static int gsm_register_devices(struct tty_driver *driver, unsigned int index)
  571. {
  572. struct device *dev;
  573. int i;
  574. unsigned int base;
  575. if (!driver || index >= MAX_MUX)
  576. return -EINVAL;
  577. base = index * NUM_DLCI; /* first minor for this index */
  578. for (i = 1; i < NUM_DLCI; i++) {
  579. /* Don't register device 0 - this is the control channel
  580. * and not a usable tty interface
  581. */
  582. dev = tty_register_device(gsm_tty_driver, base + i, NULL);
  583. if (IS_ERR(dev)) {
  584. if (debug & DBG_ERRORS)
  585. pr_info("%s failed to register device minor %u",
  586. __func__, base + i);
  587. for (i--; i >= 1; i--)
  588. tty_unregister_device(gsm_tty_driver, base + i);
  589. return PTR_ERR(dev);
  590. }
  591. }
  592. return 0;
  593. }
  594. /**
  595. * gsm_unregister_devices - unregister all tty devices for a given mux index
  596. *
  597. * @driver: the tty driver that describes the tty devices
  598. * @index: the mux number is used to calculate the minor numbers of the
  599. * ttys for this mux and may differ from the position in the
  600. * mux array.
  601. */
  602. static void gsm_unregister_devices(struct tty_driver *driver,
  603. unsigned int index)
  604. {
  605. int i;
  606. unsigned int base;
  607. if (!driver || index >= MAX_MUX)
  608. return;
  609. base = index * NUM_DLCI; /* first minor for this index */
  610. for (i = 1; i < NUM_DLCI; i++) {
  611. /* Don't unregister device 0 - this is the control
  612. * channel and not a usable tty interface
  613. */
  614. tty_unregister_device(gsm_tty_driver, base + i);
  615. }
  616. }
  617. /**
  618. * gsm_print_packet - display a frame for debug
  619. * @hdr: header to print before decode
  620. * @addr: address EA from the frame
  621. * @cr: C/R bit seen as initiator
  622. * @control: control including PF bit
  623. * @data: following data bytes
  624. * @dlen: length of data
  625. *
  626. * Displays a packet in human readable format for debugging purposes. The
  627. * style is based on amateur radio LAP-B dump display.
  628. */
  629. static void gsm_print_packet(const char *hdr, int addr, int cr,
  630. u8 control, const u8 *data, int dlen)
  631. {
  632. if (!(debug & DBG_DUMP))
  633. return;
  634. /* Only show user payload frames if debug & DBG_PAYLOAD */
  635. if (!(debug & DBG_PAYLOAD) && addr != 0)
  636. if ((control & ~PF) == UI || (control & ~PF) == UIH)
  637. return;
  638. pr_info("%s %d) %c: ", hdr, addr, "RC"[cr]);
  639. switch (control & ~PF) {
  640. case SABM:
  641. pr_cont("SABM");
  642. break;
  643. case UA:
  644. pr_cont("UA");
  645. break;
  646. case DISC:
  647. pr_cont("DISC");
  648. break;
  649. case DM:
  650. pr_cont("DM");
  651. break;
  652. case UI:
  653. pr_cont("UI");
  654. break;
  655. case UIH:
  656. pr_cont("UIH");
  657. break;
  658. default:
  659. if (!(control & 0x01)) {
  660. pr_cont("I N(S)%d N(R)%d",
  661. (control & 0x0E) >> 1, (control & 0xE0) >> 5);
  662. } else switch (control & 0x0F) {
  663. case RR:
  664. pr_cont("RR(%d)", (control & 0xE0) >> 5);
  665. break;
  666. case RNR:
  667. pr_cont("RNR(%d)", (control & 0xE0) >> 5);
  668. break;
  669. case REJ:
  670. pr_cont("REJ(%d)", (control & 0xE0) >> 5);
  671. break;
  672. default:
  673. pr_cont("[%02X]", control);
  674. }
  675. }
  676. if (control & PF)
  677. pr_cont("(P)");
  678. else
  679. pr_cont("(F)");
  680. gsm_hex_dump_bytes(NULL, data, dlen);
  681. }
  682. /*
  683. * Link level transmission side
  684. */
  685. /**
  686. * gsm_stuff_frame - bytestuff a packet
  687. * @input: input buffer
  688. * @output: output buffer
  689. * @len: length of input
  690. *
  691. * Expand a buffer by bytestuffing it. The worst case size change
  692. * is doubling and the caller is responsible for handing out
  693. * suitable sized buffers.
  694. */
  695. static int gsm_stuff_frame(const u8 *input, u8 *output, int len)
  696. {
  697. int olen = 0;
  698. while (len--) {
  699. if (*input == GSM1_SOF || *input == GSM1_ESCAPE
  700. || (*input & ISO_IEC_646_MASK) == XON
  701. || (*input & ISO_IEC_646_MASK) == XOFF) {
  702. *output++ = GSM1_ESCAPE;
  703. *output++ = *input++ ^ GSM1_ESCAPE_BITS;
  704. olen++;
  705. } else
  706. *output++ = *input++;
  707. olen++;
  708. }
  709. return olen;
  710. }
  711. /**
  712. * gsm_send - send a control frame
  713. * @gsm: our GSM mux
  714. * @addr: address for control frame
  715. * @cr: command/response bit seen as initiator
  716. * @control: control byte including PF bit
  717. *
  718. * Format up and transmit a control frame. These should be transmitted
  719. * ahead of data when they are needed.
  720. */
  721. static int gsm_send(struct gsm_mux *gsm, int addr, int cr, int control)
  722. {
  723. struct gsm_msg *msg;
  724. u8 *dp;
  725. int ocr;
  726. unsigned long flags;
  727. msg = gsm_data_alloc(gsm, addr, 0, control);
  728. if (!msg)
  729. return -ENOMEM;
  730. /* toggle C/R coding if not initiator */
  731. ocr = cr ^ (gsm->initiator ? 0 : 1);
  732. msg->data -= 3;
  733. dp = msg->data;
  734. *dp++ = (addr << 2) | (ocr << 1) | EA;
  735. *dp++ = control;
  736. if (gsm->encoding == GSM_BASIC_OPT)
  737. *dp++ = EA; /* Length of data = 0 */
  738. *dp = 0xFF - gsm_fcs_add_block(INIT_FCS, msg->data, dp - msg->data);
  739. msg->len = (dp - msg->data) + 1;
  740. gsm_print_packet("Q->", addr, cr, control, NULL, 0);
  741. spin_lock_irqsave(&gsm->tx_lock, flags);
  742. list_add_tail(&msg->list, &gsm->tx_ctrl_list);
  743. gsm->tx_bytes += msg->len;
  744. spin_unlock_irqrestore(&gsm->tx_lock, flags);
  745. gsmld_write_trigger(gsm);
  746. return 0;
  747. }
  748. /**
  749. * gsm_dlci_clear_queues - remove outstanding data for a DLCI
  750. * @gsm: mux
  751. * @dlci: clear for this DLCI
  752. *
  753. * Clears the data queues for a given DLCI.
  754. */
  755. static void gsm_dlci_clear_queues(struct gsm_mux *gsm, struct gsm_dlci *dlci)
  756. {
  757. struct gsm_msg *msg, *nmsg;
  758. int addr = dlci->addr;
  759. unsigned long flags;
  760. /* Clear DLCI write fifo first */
  761. spin_lock_irqsave(&dlci->lock, flags);
  762. kfifo_reset(&dlci->fifo);
  763. spin_unlock_irqrestore(&dlci->lock, flags);
  764. /* Clear data packets in MUX write queue */
  765. spin_lock_irqsave(&gsm->tx_lock, flags);
  766. list_for_each_entry_safe(msg, nmsg, &gsm->tx_data_list, list) {
  767. if (msg->addr != addr)
  768. continue;
  769. gsm->tx_bytes -= msg->len;
  770. list_del(&msg->list);
  771. kfree(msg);
  772. }
  773. spin_unlock_irqrestore(&gsm->tx_lock, flags);
  774. }
  775. /**
  776. * gsm_response - send a control response
  777. * @gsm: our GSM mux
  778. * @addr: address for control frame
  779. * @control: control byte including PF bit
  780. *
  781. * Format up and transmit a link level response frame.
  782. */
  783. static inline void gsm_response(struct gsm_mux *gsm, int addr, int control)
  784. {
  785. gsm_send(gsm, addr, 0, control);
  786. }
  787. /**
  788. * gsm_command - send a control command
  789. * @gsm: our GSM mux
  790. * @addr: address for control frame
  791. * @control: control byte including PF bit
  792. *
  793. * Format up and transmit a link level command frame.
  794. */
  795. static inline void gsm_command(struct gsm_mux *gsm, int addr, int control)
  796. {
  797. gsm_send(gsm, addr, 1, control);
  798. }
  799. /* Data transmission */
  800. #define HDR_LEN 6 /* ADDR CTRL [LEN.2] DATA FCS */
  801. /**
  802. * gsm_data_alloc - allocate data frame
  803. * @gsm: GSM mux
  804. * @addr: DLCI address
  805. * @len: length excluding header and FCS
  806. * @ctrl: control byte
  807. *
  808. * Allocate a new data buffer for sending frames with data. Space is left
  809. * at the front for header bytes but that is treated as an implementation
  810. * detail and not for the high level code to use
  811. */
  812. static struct gsm_msg *gsm_data_alloc(struct gsm_mux *gsm, u8 addr, int len,
  813. u8 ctrl)
  814. {
  815. struct gsm_msg *m = kmalloc(sizeof(struct gsm_msg) + len + HDR_LEN,
  816. GFP_ATOMIC);
  817. if (m == NULL)
  818. return NULL;
  819. m->data = m->buffer + HDR_LEN - 1; /* Allow for FCS */
  820. m->len = len;
  821. m->addr = addr;
  822. m->ctrl = ctrl;
  823. INIT_LIST_HEAD(&m->list);
  824. return m;
  825. }
  826. /**
  827. * gsm_send_packet - sends a single packet
  828. * @gsm: GSM Mux
  829. * @msg: packet to send
  830. *
  831. * The given packet is encoded and sent out. No memory is freed.
  832. * The caller must hold the gsm tx lock.
  833. */
  834. static int gsm_send_packet(struct gsm_mux *gsm, struct gsm_msg *msg)
  835. {
  836. int len, ret;
  837. if (gsm->encoding == GSM_BASIC_OPT) {
  838. gsm->txframe[0] = GSM0_SOF;
  839. memcpy(gsm->txframe + 1, msg->data, msg->len);
  840. gsm->txframe[msg->len + 1] = GSM0_SOF;
  841. len = msg->len + 2;
  842. } else {
  843. gsm->txframe[0] = GSM1_SOF;
  844. len = gsm_stuff_frame(msg->data, gsm->txframe + 1, msg->len);
  845. gsm->txframe[len + 1] = GSM1_SOF;
  846. len += 2;
  847. }
  848. if (debug & DBG_DATA)
  849. gsm_hex_dump_bytes(__func__, gsm->txframe, len);
  850. gsm_print_packet("-->", msg->addr, gsm->initiator, msg->ctrl, msg->data,
  851. msg->len);
  852. ret = gsmld_output(gsm, gsm->txframe, len);
  853. if (ret <= 0)
  854. return ret;
  855. /* FIXME: Can eliminate one SOF in many more cases */
  856. gsm->tx_bytes -= msg->len;
  857. return 0;
  858. }
  859. /**
  860. * gsm_is_flow_ctrl_msg - checks if flow control message
  861. * @msg: message to check
  862. *
  863. * Returns true if the given message is a flow control command of the
  864. * control channel. False is returned in any other case.
  865. */
  866. static bool gsm_is_flow_ctrl_msg(struct gsm_msg *msg)
  867. {
  868. unsigned int cmd;
  869. if (msg->addr > 0)
  870. return false;
  871. switch (msg->ctrl & ~PF) {
  872. case UI:
  873. case UIH:
  874. cmd = 0;
  875. if (gsm_read_ea_val(&cmd, msg->data + 2, msg->len - 2) < 1)
  876. break;
  877. switch (cmd & ~PF) {
  878. case CMD_FCOFF:
  879. case CMD_FCON:
  880. return true;
  881. }
  882. break;
  883. }
  884. return false;
  885. }
  886. /**
  887. * gsm_data_kick - poke the queue
  888. * @gsm: GSM Mux
  889. *
  890. * The tty device has called us to indicate that room has appeared in
  891. * the transmit queue. Ram more data into the pipe if we have any.
  892. * If we have been flow-stopped by a CMD_FCOFF, then we can only
  893. * send messages on DLCI0 until CMD_FCON. The caller must hold
  894. * the gsm tx lock.
  895. */
  896. static int gsm_data_kick(struct gsm_mux *gsm)
  897. {
  898. struct gsm_msg *msg, *nmsg;
  899. struct gsm_dlci *dlci;
  900. int ret;
  901. clear_bit(TTY_DO_WRITE_WAKEUP, &gsm->tty->flags);
  902. /* Serialize control messages and control channel messages first */
  903. list_for_each_entry_safe(msg, nmsg, &gsm->tx_ctrl_list, list) {
  904. if (gsm->constipated && !gsm_is_flow_ctrl_msg(msg))
  905. continue;
  906. ret = gsm_send_packet(gsm, msg);
  907. switch (ret) {
  908. case -ENOSPC:
  909. return -ENOSPC;
  910. case -ENODEV:
  911. /* ldisc not open */
  912. gsm->tx_bytes -= msg->len;
  913. list_del(&msg->list);
  914. kfree(msg);
  915. continue;
  916. default:
  917. if (ret >= 0) {
  918. list_del(&msg->list);
  919. kfree(msg);
  920. }
  921. break;
  922. }
  923. }
  924. if (gsm->constipated)
  925. return -EAGAIN;
  926. /* Serialize other channels */
  927. if (list_empty(&gsm->tx_data_list))
  928. return 0;
  929. list_for_each_entry_safe(msg, nmsg, &gsm->tx_data_list, list) {
  930. dlci = gsm->dlci[msg->addr];
  931. /* Send only messages for DLCIs with valid state */
  932. if (dlci->state != DLCI_OPEN) {
  933. gsm->tx_bytes -= msg->len;
  934. list_del(&msg->list);
  935. kfree(msg);
  936. continue;
  937. }
  938. ret = gsm_send_packet(gsm, msg);
  939. switch (ret) {
  940. case -ENOSPC:
  941. return -ENOSPC;
  942. case -ENODEV:
  943. /* ldisc not open */
  944. gsm->tx_bytes -= msg->len;
  945. list_del(&msg->list);
  946. kfree(msg);
  947. continue;
  948. default:
  949. if (ret >= 0) {
  950. list_del(&msg->list);
  951. kfree(msg);
  952. }
  953. break;
  954. }
  955. }
  956. return 1;
  957. }
  958. /**
  959. * __gsm_data_queue - queue a UI or UIH frame
  960. * @dlci: DLCI sending the data
  961. * @msg: message queued
  962. *
  963. * Add data to the transmit queue and try and get stuff moving
  964. * out of the mux tty if not already doing so. The Caller must hold
  965. * the gsm tx lock.
  966. */
  967. static void __gsm_data_queue(struct gsm_dlci *dlci, struct gsm_msg *msg)
  968. {
  969. struct gsm_mux *gsm = dlci->gsm;
  970. u8 *dp = msg->data;
  971. u8 *fcs = dp + msg->len;
  972. /* Fill in the header */
  973. if (gsm->encoding == GSM_BASIC_OPT) {
  974. if (msg->len < 128)
  975. *--dp = (msg->len << 1) | EA;
  976. else {
  977. *--dp = (msg->len >> 7); /* bits 7 - 15 */
  978. *--dp = (msg->len & 127) << 1; /* bits 0 - 6 */
  979. }
  980. }
  981. *--dp = msg->ctrl;
  982. if (gsm->initiator)
  983. *--dp = (msg->addr << 2) | CR | EA;
  984. else
  985. *--dp = (msg->addr << 2) | EA;
  986. *fcs = gsm_fcs_add_block(INIT_FCS, dp , msg->data - dp);
  987. /* Ugly protocol layering violation */
  988. if (msg->ctrl == UI || msg->ctrl == (UI|PF))
  989. *fcs = gsm_fcs_add_block(*fcs, msg->data, msg->len);
  990. *fcs = 0xFF - *fcs;
  991. gsm_print_packet("Q> ", msg->addr, gsm->initiator, msg->ctrl,
  992. msg->data, msg->len);
  993. /* Move the header back and adjust the length, also allow for the FCS
  994. now tacked on the end */
  995. msg->len += (msg->data - dp) + 1;
  996. msg->data = dp;
  997. /* Add to the actual output queue */
  998. switch (msg->ctrl & ~PF) {
  999. case UI:
  1000. case UIH:
  1001. if (msg->addr > 0) {
  1002. list_add_tail(&msg->list, &gsm->tx_data_list);
  1003. break;
  1004. }
  1005. fallthrough;
  1006. default:
  1007. list_add_tail(&msg->list, &gsm->tx_ctrl_list);
  1008. break;
  1009. }
  1010. gsm->tx_bytes += msg->len;
  1011. gsmld_write_trigger(gsm);
  1012. mod_timer(&gsm->kick_timer, jiffies + 10 * gsm->t1 * HZ / 100);
  1013. }
  1014. /**
  1015. * gsm_data_queue - queue a UI or UIH frame
  1016. * @dlci: DLCI sending the data
  1017. * @msg: message queued
  1018. *
  1019. * Add data to the transmit queue and try and get stuff moving
  1020. * out of the mux tty if not already doing so. Take the
  1021. * the gsm tx lock and dlci lock.
  1022. */
  1023. static void gsm_data_queue(struct gsm_dlci *dlci, struct gsm_msg *msg)
  1024. {
  1025. unsigned long flags;
  1026. spin_lock_irqsave(&dlci->gsm->tx_lock, flags);
  1027. __gsm_data_queue(dlci, msg);
  1028. spin_unlock_irqrestore(&dlci->gsm->tx_lock, flags);
  1029. }
  1030. /**
  1031. * gsm_dlci_data_output - try and push data out of a DLCI
  1032. * @gsm: mux
  1033. * @dlci: the DLCI to pull data from
  1034. *
  1035. * Pull data from a DLCI and send it into the transmit queue if there
  1036. * is data. Keep to the MRU of the mux. This path handles the usual tty
  1037. * interface which is a byte stream with optional modem data.
  1038. *
  1039. * Caller must hold the tx_lock of the mux.
  1040. */
  1041. static int gsm_dlci_data_output(struct gsm_mux *gsm, struct gsm_dlci *dlci)
  1042. {
  1043. struct gsm_msg *msg;
  1044. u8 *dp;
  1045. int h, len, size;
  1046. /* for modem bits without break data */
  1047. h = ((dlci->adaption == 1) ? 0 : 1);
  1048. len = kfifo_len(&dlci->fifo);
  1049. if (len == 0)
  1050. return 0;
  1051. /* MTU/MRU count only the data bits but watch adaption mode */
  1052. if ((len + h) > dlci->mtu)
  1053. len = dlci->mtu - h;
  1054. size = len + h;
  1055. msg = gsm_data_alloc(gsm, dlci->addr, size, dlci->ftype);
  1056. if (!msg)
  1057. return -ENOMEM;
  1058. dp = msg->data;
  1059. switch (dlci->adaption) {
  1060. case 1: /* Unstructured */
  1061. break;
  1062. case 2: /* Unstructured with modem bits.
  1063. * Always one byte as we never send inline break data
  1064. */
  1065. *dp++ = (gsm_encode_modem(dlci) << 1) | EA;
  1066. break;
  1067. default:
  1068. pr_err("%s: unsupported adaption %d\n", __func__,
  1069. dlci->adaption);
  1070. break;
  1071. }
  1072. WARN_ON(len != kfifo_out_locked(&dlci->fifo, dp, len,
  1073. &dlci->lock));
  1074. /* Notify upper layer about available send space. */
  1075. tty_port_tty_wakeup(&dlci->port);
  1076. __gsm_data_queue(dlci, msg);
  1077. /* Bytes of data we used up */
  1078. return size;
  1079. }
  1080. /**
  1081. * gsm_dlci_data_output_framed - try and push data out of a DLCI
  1082. * @gsm: mux
  1083. * @dlci: the DLCI to pull data from
  1084. *
  1085. * Pull data from a DLCI and send it into the transmit queue if there
  1086. * is data. Keep to the MRU of the mux. This path handles framed data
  1087. * queued as skbuffs to the DLCI.
  1088. *
  1089. * Caller must hold the tx_lock of the mux.
  1090. */
  1091. static int gsm_dlci_data_output_framed(struct gsm_mux *gsm,
  1092. struct gsm_dlci *dlci)
  1093. {
  1094. struct gsm_msg *msg;
  1095. u8 *dp;
  1096. int len, size;
  1097. int last = 0, first = 0;
  1098. int overhead = 0;
  1099. /* One byte per frame is used for B/F flags */
  1100. if (dlci->adaption == 4)
  1101. overhead = 1;
  1102. /* dlci->skb is locked by tx_lock */
  1103. if (dlci->skb == NULL) {
  1104. dlci->skb = skb_dequeue_tail(&dlci->skb_list);
  1105. if (dlci->skb == NULL)
  1106. return 0;
  1107. first = 1;
  1108. }
  1109. len = dlci->skb->len + overhead;
  1110. /* MTU/MRU count only the data bits */
  1111. if (len > dlci->mtu) {
  1112. if (dlci->adaption == 3) {
  1113. /* Over long frame, bin it */
  1114. dev_kfree_skb_any(dlci->skb);
  1115. dlci->skb = NULL;
  1116. return 0;
  1117. }
  1118. len = dlci->mtu;
  1119. } else
  1120. last = 1;
  1121. size = len + overhead;
  1122. msg = gsm_data_alloc(gsm, dlci->addr, size, dlci->ftype);
  1123. if (msg == NULL) {
  1124. skb_queue_tail(&dlci->skb_list, dlci->skb);
  1125. dlci->skb = NULL;
  1126. return -ENOMEM;
  1127. }
  1128. dp = msg->data;
  1129. if (dlci->adaption == 4) { /* Interruptible framed (Packetised Data) */
  1130. /* Flag byte to carry the start/end info */
  1131. *dp++ = last << 7 | first << 6 | 1; /* EA */
  1132. len--;
  1133. }
  1134. memcpy(dp, dlci->skb->data, len);
  1135. skb_pull(dlci->skb, len);
  1136. __gsm_data_queue(dlci, msg);
  1137. if (last) {
  1138. dev_kfree_skb_any(dlci->skb);
  1139. dlci->skb = NULL;
  1140. }
  1141. return size;
  1142. }
  1143. /**
  1144. * gsm_dlci_modem_output - try and push modem status out of a DLCI
  1145. * @gsm: mux
  1146. * @dlci: the DLCI to pull modem status from
  1147. * @brk: break signal
  1148. *
  1149. * Push an empty frame in to the transmit queue to update the modem status
  1150. * bits and to transmit an optional break.
  1151. *
  1152. * Caller must hold the tx_lock of the mux.
  1153. */
  1154. static int gsm_dlci_modem_output(struct gsm_mux *gsm, struct gsm_dlci *dlci,
  1155. u8 brk)
  1156. {
  1157. u8 *dp = NULL;
  1158. struct gsm_msg *msg;
  1159. int size = 0;
  1160. /* for modem bits without break data */
  1161. switch (dlci->adaption) {
  1162. case 1: /* Unstructured */
  1163. break;
  1164. case 2: /* Unstructured with modem bits. */
  1165. size++;
  1166. if (brk > 0)
  1167. size++;
  1168. break;
  1169. default:
  1170. pr_err("%s: unsupported adaption %d\n", __func__,
  1171. dlci->adaption);
  1172. return -EINVAL;
  1173. }
  1174. msg = gsm_data_alloc(gsm, dlci->addr, size, dlci->ftype);
  1175. if (!msg) {
  1176. pr_err("%s: gsm_data_alloc error", __func__);
  1177. return -ENOMEM;
  1178. }
  1179. dp = msg->data;
  1180. switch (dlci->adaption) {
  1181. case 1: /* Unstructured */
  1182. break;
  1183. case 2: /* Unstructured with modem bits. */
  1184. if (brk == 0) {
  1185. *dp++ = (gsm_encode_modem(dlci) << 1) | EA;
  1186. } else {
  1187. *dp++ = gsm_encode_modem(dlci) << 1;
  1188. *dp++ = (brk << 4) | 2 | EA; /* Length, Break, EA */
  1189. }
  1190. break;
  1191. default:
  1192. /* Handled above */
  1193. break;
  1194. }
  1195. __gsm_data_queue(dlci, msg);
  1196. return size;
  1197. }
  1198. /**
  1199. * gsm_dlci_data_sweep - look for data to send
  1200. * @gsm: the GSM mux
  1201. *
  1202. * Sweep the GSM mux channels in priority order looking for ones with
  1203. * data to send. We could do with optimising this scan a bit. We aim
  1204. * to fill the queue totally or up to TX_THRESH_HI bytes. Once we hit
  1205. * TX_THRESH_LO we get called again
  1206. *
  1207. * FIXME: We should round robin between groups and in theory you can
  1208. * renegotiate DLCI priorities with optional stuff. Needs optimising.
  1209. */
  1210. static int gsm_dlci_data_sweep(struct gsm_mux *gsm)
  1211. {
  1212. /* Priority ordering: We should do priority with RR of the groups */
  1213. int i, len, ret = 0;
  1214. bool sent;
  1215. struct gsm_dlci *dlci;
  1216. while (gsm->tx_bytes < TX_THRESH_HI) {
  1217. for (sent = false, i = 1; i < NUM_DLCI; i++) {
  1218. dlci = gsm->dlci[i];
  1219. /* skip unused or blocked channel */
  1220. if (!dlci || dlci->constipated)
  1221. continue;
  1222. /* skip channels with invalid state */
  1223. if (dlci->state != DLCI_OPEN)
  1224. continue;
  1225. /* count the sent data per adaption */
  1226. if (dlci->adaption < 3 && !dlci->net)
  1227. len = gsm_dlci_data_output(gsm, dlci);
  1228. else
  1229. len = gsm_dlci_data_output_framed(gsm, dlci);
  1230. /* on error exit */
  1231. if (len < 0)
  1232. return ret;
  1233. if (len > 0) {
  1234. ret++;
  1235. sent = true;
  1236. /* The lower DLCs can starve the higher DLCs! */
  1237. break;
  1238. }
  1239. /* try next */
  1240. }
  1241. if (!sent)
  1242. break;
  1243. }
  1244. return ret;
  1245. }
  1246. /**
  1247. * gsm_dlci_data_kick - transmit if possible
  1248. * @dlci: DLCI to kick
  1249. *
  1250. * Transmit data from this DLCI if the queue is empty. We can't rely on
  1251. * a tty wakeup except when we filled the pipe so we need to fire off
  1252. * new data ourselves in other cases.
  1253. */
  1254. static void gsm_dlci_data_kick(struct gsm_dlci *dlci)
  1255. {
  1256. unsigned long flags;
  1257. int sweep;
  1258. if (dlci->constipated)
  1259. return;
  1260. spin_lock_irqsave(&dlci->gsm->tx_lock, flags);
  1261. /* If we have nothing running then we need to fire up */
  1262. sweep = (dlci->gsm->tx_bytes < TX_THRESH_LO);
  1263. if (dlci->gsm->tx_bytes == 0) {
  1264. if (dlci->net)
  1265. gsm_dlci_data_output_framed(dlci->gsm, dlci);
  1266. else
  1267. gsm_dlci_data_output(dlci->gsm, dlci);
  1268. }
  1269. if (sweep)
  1270. gsm_dlci_data_sweep(dlci->gsm);
  1271. spin_unlock_irqrestore(&dlci->gsm->tx_lock, flags);
  1272. }
  1273. /*
  1274. * Control message processing
  1275. */
  1276. /**
  1277. * gsm_control_command - send a command frame to a control
  1278. * @gsm: gsm channel
  1279. * @cmd: the command to use
  1280. * @data: data to follow encoded info
  1281. * @dlen: length of data
  1282. *
  1283. * Encode up and queue a UI/UIH frame containing our command.
  1284. */
  1285. static int gsm_control_command(struct gsm_mux *gsm, int cmd, const u8 *data,
  1286. int dlen)
  1287. {
  1288. struct gsm_msg *msg;
  1289. struct gsm_dlci *dlci = gsm->dlci[0];
  1290. msg = gsm_data_alloc(gsm, 0, dlen + 2, dlci->ftype);
  1291. if (msg == NULL)
  1292. return -ENOMEM;
  1293. msg->data[0] = (cmd << 1) | CR | EA; /* Set C/R */
  1294. msg->data[1] = (dlen << 1) | EA;
  1295. memcpy(msg->data + 2, data, dlen);
  1296. gsm_data_queue(dlci, msg);
  1297. return 0;
  1298. }
  1299. /**
  1300. * gsm_control_reply - send a response frame to a control
  1301. * @gsm: gsm channel
  1302. * @cmd: the command to use
  1303. * @data: data to follow encoded info
  1304. * @dlen: length of data
  1305. *
  1306. * Encode up and queue a UI/UIH frame containing our response.
  1307. */
  1308. static void gsm_control_reply(struct gsm_mux *gsm, int cmd, const u8 *data,
  1309. int dlen)
  1310. {
  1311. struct gsm_msg *msg;
  1312. struct gsm_dlci *dlci = gsm->dlci[0];
  1313. msg = gsm_data_alloc(gsm, 0, dlen + 2, dlci->ftype);
  1314. if (msg == NULL)
  1315. return;
  1316. msg->data[0] = (cmd & 0xFE) << 1 | EA; /* Clear C/R */
  1317. msg->data[1] = (dlen << 1) | EA;
  1318. memcpy(msg->data + 2, data, dlen);
  1319. gsm_data_queue(dlci, msg);
  1320. }
  1321. /**
  1322. * gsm_process_modem - process received modem status
  1323. * @tty: virtual tty bound to the DLCI
  1324. * @dlci: DLCI to affect
  1325. * @modem: modem bits (full EA)
  1326. * @slen: number of signal octets
  1327. *
  1328. * Used when a modem control message or line state inline in adaption
  1329. * layer 2 is processed. Sort out the local modem state and throttles
  1330. */
  1331. static void gsm_process_modem(struct tty_struct *tty, struct gsm_dlci *dlci,
  1332. u32 modem, int slen)
  1333. {
  1334. int mlines = 0;
  1335. u8 brk = 0;
  1336. int fc;
  1337. /* The modem status command can either contain one octet (V.24 signals)
  1338. * or two octets (V.24 signals + break signals). This is specified in
  1339. * section 5.4.6.3.7 of the 07.10 mux spec.
  1340. */
  1341. if (slen == 1)
  1342. modem = modem & 0x7f;
  1343. else {
  1344. brk = modem & 0x7f;
  1345. modem = (modem >> 7) & 0x7f;
  1346. }
  1347. /* Flow control/ready to communicate */
  1348. fc = (modem & MDM_FC) || !(modem & MDM_RTR);
  1349. if (fc && !dlci->constipated) {
  1350. /* Need to throttle our output on this device */
  1351. dlci->constipated = true;
  1352. } else if (!fc && dlci->constipated) {
  1353. dlci->constipated = false;
  1354. gsm_dlci_data_kick(dlci);
  1355. }
  1356. /* Map modem bits */
  1357. if (modem & MDM_RTC)
  1358. mlines |= TIOCM_DSR | TIOCM_DTR;
  1359. if (modem & MDM_RTR)
  1360. mlines |= TIOCM_RTS | TIOCM_CTS;
  1361. if (modem & MDM_IC)
  1362. mlines |= TIOCM_RI;
  1363. if (modem & MDM_DV)
  1364. mlines |= TIOCM_CD;
  1365. /* Carrier drop -> hangup */
  1366. if (tty) {
  1367. if ((mlines & TIOCM_CD) == 0 && (dlci->modem_rx & TIOCM_CD))
  1368. if (!C_CLOCAL(tty))
  1369. tty_hangup(tty);
  1370. }
  1371. if (brk & 0x01)
  1372. tty_insert_flip_char(&dlci->port, 0, TTY_BREAK);
  1373. dlci->modem_rx = mlines;
  1374. wake_up_interruptible(&dlci->gsm->event);
  1375. }
  1376. /**
  1377. * gsm_process_negotiation - process received parameters
  1378. * @gsm: GSM channel
  1379. * @addr: DLCI address
  1380. * @cr: command/response
  1381. * @params: encoded parameters from the parameter negotiation message
  1382. *
  1383. * Used when the response for our parameter negotiation command was
  1384. * received.
  1385. */
  1386. static int gsm_process_negotiation(struct gsm_mux *gsm, unsigned int addr,
  1387. unsigned int cr,
  1388. const struct gsm_dlci_param_bits *params)
  1389. {
  1390. struct gsm_dlci *dlci = gsm->dlci[addr];
  1391. unsigned int ftype, i, adaption, prio, n1, k;
  1392. i = FIELD_GET(PN_I_CL_FIELD_FTYPE, params->i_cl_bits);
  1393. adaption = FIELD_GET(PN_I_CL_FIELD_ADAPTION, params->i_cl_bits) + 1;
  1394. prio = FIELD_GET(PN_P_FIELD_PRIO, params->p_bits);
  1395. n1 = FIELD_GET(PN_N_FIELD_N1, get_unaligned_le16(&params->n_bits));
  1396. k = FIELD_GET(PN_K_FIELD_K, params->k_bits);
  1397. if (n1 < MIN_MTU) {
  1398. if (debug & DBG_ERRORS)
  1399. pr_info("%s N1 out of range in PN\n", __func__);
  1400. return -EINVAL;
  1401. }
  1402. switch (i) {
  1403. case 0x00:
  1404. ftype = UIH;
  1405. break;
  1406. case 0x01:
  1407. ftype = UI;
  1408. break;
  1409. case 0x02: /* I frames are not supported */
  1410. if (debug & DBG_ERRORS)
  1411. pr_info("%s unsupported I frame request in PN\n",
  1412. __func__);
  1413. gsm->unsupported++;
  1414. return -EINVAL;
  1415. default:
  1416. if (debug & DBG_ERRORS)
  1417. pr_info("%s i out of range in PN\n", __func__);
  1418. return -EINVAL;
  1419. }
  1420. if (!cr && gsm->initiator) {
  1421. if (adaption != dlci->adaption) {
  1422. if (debug & DBG_ERRORS)
  1423. pr_info("%s invalid adaption %d in PN\n",
  1424. __func__, adaption);
  1425. return -EINVAL;
  1426. }
  1427. if (prio != dlci->prio) {
  1428. if (debug & DBG_ERRORS)
  1429. pr_info("%s invalid priority %d in PN",
  1430. __func__, prio);
  1431. return -EINVAL;
  1432. }
  1433. if (n1 > gsm->mru || n1 > dlci->mtu) {
  1434. /* We requested a frame size but the other party wants
  1435. * to send larger frames. The standard allows only a
  1436. * smaller response value than requested (5.4.6.3.1).
  1437. */
  1438. if (debug & DBG_ERRORS)
  1439. pr_info("%s invalid N1 %d in PN\n", __func__,
  1440. n1);
  1441. return -EINVAL;
  1442. }
  1443. dlci->mtu = n1;
  1444. if (ftype != dlci->ftype) {
  1445. if (debug & DBG_ERRORS)
  1446. pr_info("%s invalid i %d in PN\n", __func__, i);
  1447. return -EINVAL;
  1448. }
  1449. if (ftype != UI && ftype != UIH && k > dlci->k) {
  1450. if (debug & DBG_ERRORS)
  1451. pr_info("%s invalid k %d in PN\n", __func__, k);
  1452. return -EINVAL;
  1453. }
  1454. dlci->k = k;
  1455. } else if (cr && !gsm->initiator) {
  1456. /* Only convergence layer type 1 and 2 are supported. */
  1457. if (adaption != 1 && adaption != 2) {
  1458. if (debug & DBG_ERRORS)
  1459. pr_info("%s invalid adaption %d in PN\n",
  1460. __func__, adaption);
  1461. return -EINVAL;
  1462. }
  1463. dlci->adaption = adaption;
  1464. if (n1 > gsm->mru) {
  1465. /* Propose a smaller value */
  1466. dlci->mtu = gsm->mru;
  1467. } else if (n1 > MAX_MTU) {
  1468. /* Propose a smaller value */
  1469. dlci->mtu = MAX_MTU;
  1470. } else {
  1471. dlci->mtu = n1;
  1472. }
  1473. dlci->prio = prio;
  1474. dlci->ftype = ftype;
  1475. dlci->k = k;
  1476. } else {
  1477. return -EINVAL;
  1478. }
  1479. return 0;
  1480. }
  1481. /**
  1482. * gsm_control_modem - modem status received
  1483. * @gsm: GSM channel
  1484. * @data: data following command
  1485. * @clen: command length
  1486. *
  1487. * We have received a modem status control message. This is used by
  1488. * the GSM mux protocol to pass virtual modem line status and optionally
  1489. * to indicate break signals. Unpack it, convert to Linux representation
  1490. * and if need be stuff a break message down the tty.
  1491. */
  1492. static void gsm_control_modem(struct gsm_mux *gsm, const u8 *data, int clen)
  1493. {
  1494. unsigned int addr = 0;
  1495. unsigned int modem = 0;
  1496. struct gsm_dlci *dlci;
  1497. int len = clen;
  1498. int cl = clen;
  1499. const u8 *dp = data;
  1500. struct tty_struct *tty;
  1501. len = gsm_read_ea_val(&addr, data, cl);
  1502. if (len < 1)
  1503. return;
  1504. addr >>= 1;
  1505. /* Closed port, or invalid ? */
  1506. if (addr == 0 || addr >= NUM_DLCI || gsm->dlci[addr] == NULL)
  1507. return;
  1508. dlci = gsm->dlci[addr];
  1509. /* Must be at least one byte following the EA */
  1510. if ((cl - len) < 1)
  1511. return;
  1512. dp += len;
  1513. cl -= len;
  1514. /* get the modem status */
  1515. len = gsm_read_ea_val(&modem, dp, cl);
  1516. if (len < 1)
  1517. return;
  1518. tty = tty_port_tty_get(&dlci->port);
  1519. gsm_process_modem(tty, dlci, modem, cl);
  1520. if (tty) {
  1521. tty_wakeup(tty);
  1522. tty_kref_put(tty);
  1523. }
  1524. gsm_control_reply(gsm, CMD_MSC, data, clen);
  1525. }
  1526. /**
  1527. * gsm_control_negotiation - parameter negotiation received
  1528. * @gsm: GSM channel
  1529. * @cr: command/response flag
  1530. * @data: data following command
  1531. * @dlen: data length
  1532. *
  1533. * We have received a parameter negotiation message. This is used by
  1534. * the GSM mux protocol to configure protocol parameters for a new DLCI.
  1535. */
  1536. static void gsm_control_negotiation(struct gsm_mux *gsm, unsigned int cr,
  1537. const u8 *data, unsigned int dlen)
  1538. {
  1539. unsigned int addr;
  1540. struct gsm_dlci_param_bits pn_reply;
  1541. struct gsm_dlci *dlci;
  1542. struct gsm_dlci_param_bits *params;
  1543. if (dlen < sizeof(struct gsm_dlci_param_bits)) {
  1544. gsm->open_error++;
  1545. return;
  1546. }
  1547. /* Invalid DLCI? */
  1548. params = (struct gsm_dlci_param_bits *)data;
  1549. addr = FIELD_GET(PN_D_FIELD_DLCI, params->d_bits);
  1550. if (addr == 0 || addr >= NUM_DLCI || !gsm->dlci[addr]) {
  1551. gsm->open_error++;
  1552. return;
  1553. }
  1554. dlci = gsm->dlci[addr];
  1555. /* Too late for parameter negotiation? */
  1556. if ((!cr && dlci->state == DLCI_OPENING) || dlci->state == DLCI_OPEN) {
  1557. gsm->open_error++;
  1558. return;
  1559. }
  1560. /* Process the received parameters */
  1561. if (gsm_process_negotiation(gsm, addr, cr, params) != 0) {
  1562. /* Negotiation failed. Close the link. */
  1563. if (debug & DBG_ERRORS)
  1564. pr_info("%s PN failed\n", __func__);
  1565. gsm->open_error++;
  1566. gsm_dlci_close(dlci);
  1567. return;
  1568. }
  1569. if (cr) {
  1570. /* Reply command with accepted parameters. */
  1571. if (gsm_encode_params(dlci, &pn_reply) == 0)
  1572. gsm_control_reply(gsm, CMD_PN, (const u8 *)&pn_reply,
  1573. sizeof(pn_reply));
  1574. else if (debug & DBG_ERRORS)
  1575. pr_info("%s PN invalid\n", __func__);
  1576. } else if (dlci->state == DLCI_CONFIGURE) {
  1577. /* Proceed with link setup by sending SABM before UA */
  1578. dlci->state = DLCI_OPENING;
  1579. gsm_command(gsm, dlci->addr, SABM|PF);
  1580. mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
  1581. } else {
  1582. if (debug & DBG_ERRORS)
  1583. pr_info("%s PN in invalid state\n", __func__);
  1584. gsm->open_error++;
  1585. }
  1586. }
  1587. /**
  1588. * gsm_control_rls - remote line status
  1589. * @gsm: GSM channel
  1590. * @data: data bytes
  1591. * @clen: data length
  1592. *
  1593. * The modem sends us a two byte message on the control channel whenever
  1594. * it wishes to send us an error state from the virtual link. Stuff
  1595. * this into the uplink tty if present
  1596. */
  1597. static void gsm_control_rls(struct gsm_mux *gsm, const u8 *data, int clen)
  1598. {
  1599. struct tty_port *port;
  1600. unsigned int addr = 0;
  1601. u8 bits;
  1602. int len = clen;
  1603. const u8 *dp = data;
  1604. while (gsm_read_ea(&addr, *dp++) == 0) {
  1605. len--;
  1606. if (len == 0)
  1607. return;
  1608. }
  1609. /* Must be at least one byte following ea */
  1610. len--;
  1611. if (len <= 0)
  1612. return;
  1613. addr >>= 1;
  1614. /* Closed port, or invalid ? */
  1615. if (addr == 0 || addr >= NUM_DLCI || gsm->dlci[addr] == NULL)
  1616. return;
  1617. /* No error ? */
  1618. bits = *dp;
  1619. if ((bits & 1) == 0)
  1620. return;
  1621. port = &gsm->dlci[addr]->port;
  1622. if (bits & 2)
  1623. tty_insert_flip_char(port, 0, TTY_OVERRUN);
  1624. if (bits & 4)
  1625. tty_insert_flip_char(port, 0, TTY_PARITY);
  1626. if (bits & 8)
  1627. tty_insert_flip_char(port, 0, TTY_FRAME);
  1628. tty_flip_buffer_push(port);
  1629. gsm_control_reply(gsm, CMD_RLS, data, clen);
  1630. }
  1631. static void gsm_dlci_begin_close(struct gsm_dlci *dlci);
  1632. /**
  1633. * gsm_control_message - DLCI 0 control processing
  1634. * @gsm: our GSM mux
  1635. * @command: the command EA
  1636. * @data: data beyond the command/length EAs
  1637. * @clen: length
  1638. *
  1639. * Input processor for control messages from the other end of the link.
  1640. * Processes the incoming request and queues a response frame or an
  1641. * NSC response if not supported
  1642. */
  1643. static void gsm_control_message(struct gsm_mux *gsm, unsigned int command,
  1644. const u8 *data, int clen)
  1645. {
  1646. u8 buf[1];
  1647. switch (command) {
  1648. case CMD_CLD: {
  1649. struct gsm_dlci *dlci = gsm->dlci[0];
  1650. /* Modem wishes to close down */
  1651. if (dlci) {
  1652. dlci->dead = true;
  1653. gsm->dead = true;
  1654. gsm_dlci_begin_close(dlci);
  1655. }
  1656. }
  1657. break;
  1658. case CMD_TEST:
  1659. /* Modem wishes to test, reply with the data */
  1660. gsm_control_reply(gsm, CMD_TEST, data, clen);
  1661. break;
  1662. case CMD_FCON:
  1663. /* Modem can accept data again */
  1664. gsm->constipated = false;
  1665. gsm_control_reply(gsm, CMD_FCON, NULL, 0);
  1666. /* Kick the link in case it is idling */
  1667. gsmld_write_trigger(gsm);
  1668. break;
  1669. case CMD_FCOFF:
  1670. /* Modem wants us to STFU */
  1671. gsm->constipated = true;
  1672. gsm_control_reply(gsm, CMD_FCOFF, NULL, 0);
  1673. break;
  1674. case CMD_MSC:
  1675. /* Out of band modem line change indicator for a DLCI */
  1676. gsm_control_modem(gsm, data, clen);
  1677. break;
  1678. case CMD_RLS:
  1679. /* Out of band error reception for a DLCI */
  1680. gsm_control_rls(gsm, data, clen);
  1681. break;
  1682. case CMD_PSC:
  1683. /* Modem wishes to enter power saving state */
  1684. gsm_control_reply(gsm, CMD_PSC, NULL, 0);
  1685. break;
  1686. /* Optional commands */
  1687. case CMD_PN:
  1688. /* Modem sends a parameter negotiation command */
  1689. gsm_control_negotiation(gsm, 1, data, clen);
  1690. break;
  1691. /* Optional unsupported commands */
  1692. case CMD_RPN: /* Remote port negotiation */
  1693. case CMD_SNC: /* Service negotiation command */
  1694. gsm->unsupported++;
  1695. fallthrough;
  1696. default:
  1697. /* Reply to bad commands with an NSC */
  1698. buf[0] = command;
  1699. gsm_control_reply(gsm, CMD_NSC, buf, 1);
  1700. break;
  1701. }
  1702. }
  1703. /**
  1704. * gsm_control_response - process a response to our control
  1705. * @gsm: our GSM mux
  1706. * @command: the command (response) EA
  1707. * @data: data beyond the command/length EA
  1708. * @clen: length
  1709. *
  1710. * Process a response to an outstanding command. We only allow a single
  1711. * control message in flight so this is fairly easy. All the clean up
  1712. * is done by the caller, we just update the fields, flag it as done
  1713. * and return
  1714. */
  1715. static void gsm_control_response(struct gsm_mux *gsm, unsigned int command,
  1716. const u8 *data, int clen)
  1717. {
  1718. struct gsm_control *ctrl;
  1719. struct gsm_dlci *dlci;
  1720. unsigned long flags;
  1721. spin_lock_irqsave(&gsm->control_lock, flags);
  1722. ctrl = gsm->pending_cmd;
  1723. dlci = gsm->dlci[0];
  1724. command |= 1;
  1725. /* Does the reply match our command */
  1726. if (ctrl != NULL && (command == ctrl->cmd || command == CMD_NSC)) {
  1727. /* Our command was replied to, kill the retry timer */
  1728. del_timer(&gsm->t2_timer);
  1729. gsm->pending_cmd = NULL;
  1730. /* Rejected by the other end */
  1731. if (command == CMD_NSC)
  1732. ctrl->error = -EOPNOTSUPP;
  1733. ctrl->done = 1;
  1734. wake_up(&gsm->event);
  1735. /* Or did we receive the PN response to our PN command */
  1736. } else if (command == CMD_PN) {
  1737. gsm_control_negotiation(gsm, 0, data, clen);
  1738. /* Or did we receive the TEST response to our TEST command */
  1739. } else if (command == CMD_TEST && clen == 1 && *data == gsm->ka_num) {
  1740. gsm->ka_retries = -1; /* trigger new keep-alive message */
  1741. if (dlci && !dlci->dead)
  1742. mod_timer(&gsm->ka_timer, jiffies + gsm->keep_alive * HZ / 100);
  1743. }
  1744. spin_unlock_irqrestore(&gsm->control_lock, flags);
  1745. }
  1746. /**
  1747. * gsm_control_keep_alive - check timeout or start keep-alive
  1748. * @t: timer contained in our gsm object
  1749. *
  1750. * Called off the keep-alive timer expiry signaling that our link
  1751. * partner is not responding anymore. Link will be closed.
  1752. * This is also called to startup our timer.
  1753. */
  1754. static void gsm_control_keep_alive(struct timer_list *t)
  1755. {
  1756. struct gsm_mux *gsm = from_timer(gsm, t, ka_timer);
  1757. unsigned long flags;
  1758. spin_lock_irqsave(&gsm->control_lock, flags);
  1759. if (gsm->ka_num && gsm->ka_retries == 0) {
  1760. /* Keep-alive expired -> close the link */
  1761. if (debug & DBG_ERRORS)
  1762. pr_debug("%s keep-alive timed out\n", __func__);
  1763. spin_unlock_irqrestore(&gsm->control_lock, flags);
  1764. if (gsm->dlci[0])
  1765. gsm_dlci_begin_close(gsm->dlci[0]);
  1766. return;
  1767. } else if (gsm->keep_alive && gsm->dlci[0] && !gsm->dlci[0]->dead) {
  1768. if (gsm->ka_retries > 0) {
  1769. /* T2 expired for keep-alive -> resend */
  1770. gsm->ka_retries--;
  1771. } else {
  1772. /* Start keep-alive timer */
  1773. gsm->ka_num++;
  1774. if (!gsm->ka_num)
  1775. gsm->ka_num++;
  1776. gsm->ka_retries = (signed int)gsm->n2;
  1777. }
  1778. gsm_control_command(gsm, CMD_TEST, &gsm->ka_num,
  1779. sizeof(gsm->ka_num));
  1780. mod_timer(&gsm->ka_timer,
  1781. jiffies + gsm->t2 * HZ / 100);
  1782. }
  1783. spin_unlock_irqrestore(&gsm->control_lock, flags);
  1784. }
  1785. /**
  1786. * gsm_control_transmit - send control packet
  1787. * @gsm: gsm mux
  1788. * @ctrl: frame to send
  1789. *
  1790. * Send out a pending control command (called under control lock)
  1791. */
  1792. static void gsm_control_transmit(struct gsm_mux *gsm, struct gsm_control *ctrl)
  1793. {
  1794. gsm_control_command(gsm, ctrl->cmd, ctrl->data, ctrl->len);
  1795. }
  1796. /**
  1797. * gsm_control_retransmit - retransmit a control frame
  1798. * @t: timer contained in our gsm object
  1799. *
  1800. * Called off the T2 timer expiry in order to retransmit control frames
  1801. * that have been lost in the system somewhere. The control_lock protects
  1802. * us from colliding with another sender or a receive completion event.
  1803. * In that situation the timer may still occur in a small window but
  1804. * gsm->pending_cmd will be NULL and we just let the timer expire.
  1805. */
  1806. static void gsm_control_retransmit(struct timer_list *t)
  1807. {
  1808. struct gsm_mux *gsm = from_timer(gsm, t, t2_timer);
  1809. struct gsm_control *ctrl;
  1810. unsigned long flags;
  1811. spin_lock_irqsave(&gsm->control_lock, flags);
  1812. ctrl = gsm->pending_cmd;
  1813. if (ctrl) {
  1814. if (gsm->cretries == 0 || !gsm->dlci[0] || gsm->dlci[0]->dead) {
  1815. gsm->pending_cmd = NULL;
  1816. ctrl->error = -ETIMEDOUT;
  1817. ctrl->done = 1;
  1818. spin_unlock_irqrestore(&gsm->control_lock, flags);
  1819. wake_up(&gsm->event);
  1820. return;
  1821. }
  1822. gsm->cretries--;
  1823. gsm_control_transmit(gsm, ctrl);
  1824. mod_timer(&gsm->t2_timer, jiffies + gsm->t2 * HZ / 100);
  1825. }
  1826. spin_unlock_irqrestore(&gsm->control_lock, flags);
  1827. }
  1828. /**
  1829. * gsm_control_send - send a control frame on DLCI 0
  1830. * @gsm: the GSM channel
  1831. * @command: command to send including CR bit
  1832. * @data: bytes of data (must be kmalloced)
  1833. * @clen: length of the block to send
  1834. *
  1835. * Queue and dispatch a control command. Only one command can be
  1836. * active at a time. In theory more can be outstanding but the matching
  1837. * gets really complicated so for now stick to one outstanding.
  1838. */
  1839. static struct gsm_control *gsm_control_send(struct gsm_mux *gsm,
  1840. unsigned int command, u8 *data, int clen)
  1841. {
  1842. struct gsm_control *ctrl = kzalloc(sizeof(struct gsm_control),
  1843. GFP_ATOMIC);
  1844. unsigned long flags;
  1845. if (ctrl == NULL)
  1846. return NULL;
  1847. retry:
  1848. wait_event(gsm->event, gsm->pending_cmd == NULL);
  1849. spin_lock_irqsave(&gsm->control_lock, flags);
  1850. if (gsm->pending_cmd != NULL) {
  1851. spin_unlock_irqrestore(&gsm->control_lock, flags);
  1852. goto retry;
  1853. }
  1854. ctrl->cmd = command;
  1855. ctrl->data = data;
  1856. ctrl->len = clen;
  1857. gsm->pending_cmd = ctrl;
  1858. /* If DLCI0 is in ADM mode skip retries, it won't respond */
  1859. if (gsm->dlci[0]->mode == DLCI_MODE_ADM)
  1860. gsm->cretries = 0;
  1861. else
  1862. gsm->cretries = gsm->n2;
  1863. mod_timer(&gsm->t2_timer, jiffies + gsm->t2 * HZ / 100);
  1864. gsm_control_transmit(gsm, ctrl);
  1865. spin_unlock_irqrestore(&gsm->control_lock, flags);
  1866. return ctrl;
  1867. }
  1868. /**
  1869. * gsm_control_wait - wait for a control to finish
  1870. * @gsm: GSM mux
  1871. * @control: control we are waiting on
  1872. *
  1873. * Waits for the control to complete or time out. Frees any used
  1874. * resources and returns 0 for success, or an error if the remote
  1875. * rejected or ignored the request.
  1876. */
  1877. static int gsm_control_wait(struct gsm_mux *gsm, struct gsm_control *control)
  1878. {
  1879. int err;
  1880. wait_event(gsm->event, control->done == 1);
  1881. err = control->error;
  1882. kfree(control);
  1883. return err;
  1884. }
  1885. /*
  1886. * DLCI level handling: Needs krefs
  1887. */
  1888. /*
  1889. * State transitions and timers
  1890. */
  1891. /**
  1892. * gsm_dlci_close - a DLCI has closed
  1893. * @dlci: DLCI that closed
  1894. *
  1895. * Perform processing when moving a DLCI into closed state. If there
  1896. * is an attached tty this is hung up
  1897. */
  1898. static void gsm_dlci_close(struct gsm_dlci *dlci)
  1899. {
  1900. del_timer(&dlci->t1);
  1901. if (debug & DBG_ERRORS)
  1902. pr_debug("DLCI %d goes closed.\n", dlci->addr);
  1903. dlci->state = DLCI_CLOSED;
  1904. /* Prevent us from sending data before the link is up again */
  1905. dlci->constipated = true;
  1906. if (dlci->addr != 0) {
  1907. tty_port_tty_hangup(&dlci->port, false);
  1908. gsm_dlci_clear_queues(dlci->gsm, dlci);
  1909. /* Ensure that gsmtty_open() can return. */
  1910. tty_port_set_initialized(&dlci->port, false);
  1911. wake_up_interruptible(&dlci->port.open_wait);
  1912. } else {
  1913. del_timer(&dlci->gsm->ka_timer);
  1914. dlci->gsm->dead = true;
  1915. }
  1916. /* A DLCI 0 close is a MUX termination so we need to kick that
  1917. back to userspace somehow */
  1918. gsm_dlci_data_kick(dlci);
  1919. wake_up_all(&dlci->gsm->event);
  1920. }
  1921. /**
  1922. * gsm_dlci_open - a DLCI has opened
  1923. * @dlci: DLCI that opened
  1924. *
  1925. * Perform processing when moving a DLCI into open state.
  1926. */
  1927. static void gsm_dlci_open(struct gsm_dlci *dlci)
  1928. {
  1929. struct gsm_mux *gsm = dlci->gsm;
  1930. /* Note that SABM UA .. SABM UA first UA lost can mean that we go
  1931. open -> open */
  1932. del_timer(&dlci->t1);
  1933. /* This will let a tty open continue */
  1934. dlci->state = DLCI_OPEN;
  1935. dlci->constipated = false;
  1936. if (debug & DBG_ERRORS)
  1937. pr_debug("DLCI %d goes open.\n", dlci->addr);
  1938. /* Send current modem state */
  1939. if (dlci->addr) {
  1940. gsm_modem_send_initial_msc(dlci);
  1941. } else {
  1942. /* Start keep-alive control */
  1943. gsm->ka_num = 0;
  1944. gsm->ka_retries = -1;
  1945. mod_timer(&gsm->ka_timer,
  1946. jiffies + gsm->keep_alive * HZ / 100);
  1947. }
  1948. gsm_dlci_data_kick(dlci);
  1949. wake_up(&dlci->gsm->event);
  1950. }
  1951. /**
  1952. * gsm_dlci_negotiate - start parameter negotiation
  1953. * @dlci: DLCI to open
  1954. *
  1955. * Starts the parameter negotiation for the new DLCI. This needs to be done
  1956. * before the DLCI initialized the channel via SABM.
  1957. */
  1958. static int gsm_dlci_negotiate(struct gsm_dlci *dlci)
  1959. {
  1960. struct gsm_mux *gsm = dlci->gsm;
  1961. struct gsm_dlci_param_bits params;
  1962. int ret;
  1963. ret = gsm_encode_params(dlci, &params);
  1964. if (ret != 0)
  1965. return ret;
  1966. /* We cannot asynchronous wait for the command response with
  1967. * gsm_command() and gsm_control_wait() at this point.
  1968. */
  1969. ret = gsm_control_command(gsm, CMD_PN, (const u8 *)&params,
  1970. sizeof(params));
  1971. return ret;
  1972. }
  1973. /**
  1974. * gsm_dlci_t1 - T1 timer expiry
  1975. * @t: timer contained in the DLCI that opened
  1976. *
  1977. * The T1 timer handles retransmits of control frames (essentially of
  1978. * SABM and DISC). We resend the command until the retry count runs out
  1979. * in which case an opening port goes back to closed and a closing port
  1980. * is simply put into closed state (any further frames from the other
  1981. * end will get a DM response)
  1982. *
  1983. * Some control dlci can stay in ADM mode with other dlci working just
  1984. * fine. In that case we can just keep the control dlci open after the
  1985. * DLCI_OPENING retries time out.
  1986. */
  1987. static void gsm_dlci_t1(struct timer_list *t)
  1988. {
  1989. struct gsm_dlci *dlci = from_timer(dlci, t, t1);
  1990. struct gsm_mux *gsm = dlci->gsm;
  1991. switch (dlci->state) {
  1992. case DLCI_CONFIGURE:
  1993. if (dlci->retries && gsm_dlci_negotiate(dlci) == 0) {
  1994. dlci->retries--;
  1995. mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
  1996. } else {
  1997. gsm->open_error++;
  1998. gsm_dlci_begin_close(dlci); /* prevent half open link */
  1999. }
  2000. break;
  2001. case DLCI_OPENING:
  2002. if (dlci->retries) {
  2003. dlci->retries--;
  2004. gsm_command(dlci->gsm, dlci->addr, SABM|PF);
  2005. mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
  2006. } else if (!dlci->addr && gsm->control == (DM | PF)) {
  2007. if (debug & DBG_ERRORS)
  2008. pr_info("DLCI %d opening in ADM mode.\n",
  2009. dlci->addr);
  2010. dlci->mode = DLCI_MODE_ADM;
  2011. gsm_dlci_open(dlci);
  2012. } else {
  2013. gsm->open_error++;
  2014. gsm_dlci_begin_close(dlci); /* prevent half open link */
  2015. }
  2016. break;
  2017. case DLCI_CLOSING:
  2018. if (dlci->retries) {
  2019. dlci->retries--;
  2020. gsm_command(dlci->gsm, dlci->addr, DISC|PF);
  2021. mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
  2022. } else
  2023. gsm_dlci_close(dlci);
  2024. break;
  2025. default:
  2026. pr_debug("%s: unhandled state: %d\n", __func__, dlci->state);
  2027. break;
  2028. }
  2029. }
  2030. /**
  2031. * gsm_dlci_begin_open - start channel open procedure
  2032. * @dlci: DLCI to open
  2033. *
  2034. * Commence opening a DLCI from the Linux side. We issue SABM messages
  2035. * to the modem which should then reply with a UA or ADM, at which point
  2036. * we will move into open state. Opening is done asynchronously with retry
  2037. * running off timers and the responses.
  2038. * Parameter negotiation is performed before SABM if required.
  2039. */
  2040. static void gsm_dlci_begin_open(struct gsm_dlci *dlci)
  2041. {
  2042. struct gsm_mux *gsm = dlci ? dlci->gsm : NULL;
  2043. bool need_pn = false;
  2044. if (!gsm)
  2045. return;
  2046. if (dlci->addr != 0) {
  2047. if (gsm->adaption != 1 || gsm->adaption != dlci->adaption)
  2048. need_pn = true;
  2049. if (dlci->prio != (roundup(dlci->addr + 1, 8) - 1))
  2050. need_pn = true;
  2051. if (gsm->ftype != dlci->ftype)
  2052. need_pn = true;
  2053. }
  2054. switch (dlci->state) {
  2055. case DLCI_CLOSED:
  2056. case DLCI_WAITING_CONFIG:
  2057. case DLCI_CLOSING:
  2058. dlci->retries = gsm->n2;
  2059. if (!need_pn) {
  2060. dlci->state = DLCI_OPENING;
  2061. gsm_command(gsm, dlci->addr, SABM|PF);
  2062. } else {
  2063. /* Configure DLCI before setup */
  2064. dlci->state = DLCI_CONFIGURE;
  2065. if (gsm_dlci_negotiate(dlci) != 0) {
  2066. gsm_dlci_close(dlci);
  2067. return;
  2068. }
  2069. }
  2070. mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
  2071. break;
  2072. default:
  2073. break;
  2074. }
  2075. }
  2076. /**
  2077. * gsm_dlci_set_opening - change state to opening
  2078. * @dlci: DLCI to open
  2079. *
  2080. * Change internal state to wait for DLCI open from initiator side.
  2081. * We set off timers and responses upon reception of an SABM.
  2082. */
  2083. static void gsm_dlci_set_opening(struct gsm_dlci *dlci)
  2084. {
  2085. switch (dlci->state) {
  2086. case DLCI_CLOSED:
  2087. case DLCI_WAITING_CONFIG:
  2088. case DLCI_CLOSING:
  2089. dlci->state = DLCI_OPENING;
  2090. break;
  2091. default:
  2092. break;
  2093. }
  2094. }
  2095. /**
  2096. * gsm_dlci_set_wait_config - wait for channel configuration
  2097. * @dlci: DLCI to configure
  2098. *
  2099. * Wait for a DLCI configuration from the application.
  2100. */
  2101. static void gsm_dlci_set_wait_config(struct gsm_dlci *dlci)
  2102. {
  2103. switch (dlci->state) {
  2104. case DLCI_CLOSED:
  2105. case DLCI_CLOSING:
  2106. dlci->state = DLCI_WAITING_CONFIG;
  2107. break;
  2108. default:
  2109. break;
  2110. }
  2111. }
  2112. /**
  2113. * gsm_dlci_begin_close - start channel open procedure
  2114. * @dlci: DLCI to open
  2115. *
  2116. * Commence closing a DLCI from the Linux side. We issue DISC messages
  2117. * to the modem which should then reply with a UA, at which point we
  2118. * will move into closed state. Closing is done asynchronously with retry
  2119. * off timers. We may also receive a DM reply from the other end which
  2120. * indicates the channel was already closed.
  2121. */
  2122. static void gsm_dlci_begin_close(struct gsm_dlci *dlci)
  2123. {
  2124. struct gsm_mux *gsm = dlci->gsm;
  2125. if (dlci->state == DLCI_CLOSED || dlci->state == DLCI_CLOSING)
  2126. return;
  2127. dlci->retries = gsm->n2;
  2128. dlci->state = DLCI_CLOSING;
  2129. gsm_command(dlci->gsm, dlci->addr, DISC|PF);
  2130. mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
  2131. wake_up_interruptible(&gsm->event);
  2132. }
  2133. /**
  2134. * gsm_dlci_data - data arrived
  2135. * @dlci: channel
  2136. * @data: block of bytes received
  2137. * @clen: length of received block
  2138. *
  2139. * A UI or UIH frame has arrived which contains data for a channel
  2140. * other than the control channel. If the relevant virtual tty is
  2141. * open we shovel the bits down it, if not we drop them.
  2142. */
  2143. static void gsm_dlci_data(struct gsm_dlci *dlci, const u8 *data, int clen)
  2144. {
  2145. /* krefs .. */
  2146. struct tty_port *port = &dlci->port;
  2147. struct tty_struct *tty;
  2148. unsigned int modem = 0;
  2149. int len;
  2150. if (debug & DBG_TTY)
  2151. pr_debug("%d bytes for tty\n", clen);
  2152. switch (dlci->adaption) {
  2153. /* Unsupported types */
  2154. case 4: /* Packetised interruptible data */
  2155. break;
  2156. case 3: /* Packetised uininterruptible voice/data */
  2157. break;
  2158. case 2: /* Asynchronous serial with line state in each frame */
  2159. len = gsm_read_ea_val(&modem, data, clen);
  2160. if (len < 1)
  2161. return;
  2162. tty = tty_port_tty_get(port);
  2163. if (tty) {
  2164. gsm_process_modem(tty, dlci, modem, len);
  2165. tty_wakeup(tty);
  2166. tty_kref_put(tty);
  2167. }
  2168. /* Skip processed modem data */
  2169. data += len;
  2170. clen -= len;
  2171. fallthrough;
  2172. case 1: /* Line state will go via DLCI 0 controls only */
  2173. default:
  2174. tty_insert_flip_string(port, data, clen);
  2175. tty_flip_buffer_push(port);
  2176. }
  2177. }
  2178. /**
  2179. * gsm_dlci_command - data arrived on control channel
  2180. * @dlci: channel
  2181. * @data: block of bytes received
  2182. * @len: length of received block
  2183. *
  2184. * A UI or UIH frame has arrived which contains data for DLCI 0 the
  2185. * control channel. This should contain a command EA followed by
  2186. * control data bytes. The command EA contains a command/response bit
  2187. * and we divide up the work accordingly.
  2188. */
  2189. static void gsm_dlci_command(struct gsm_dlci *dlci, const u8 *data, int len)
  2190. {
  2191. /* See what command is involved */
  2192. unsigned int command = 0;
  2193. unsigned int clen = 0;
  2194. unsigned int dlen;
  2195. /* read the command */
  2196. dlen = gsm_read_ea_val(&command, data, len);
  2197. len -= dlen;
  2198. data += dlen;
  2199. /* read any control data */
  2200. dlen = gsm_read_ea_val(&clen, data, len);
  2201. len -= dlen;
  2202. data += dlen;
  2203. /* Malformed command? */
  2204. if (clen > len) {
  2205. dlci->gsm->malformed++;
  2206. return;
  2207. }
  2208. if (command & 1)
  2209. gsm_control_message(dlci->gsm, command, data, clen);
  2210. else
  2211. gsm_control_response(dlci->gsm, command, data, clen);
  2212. }
  2213. /**
  2214. * gsm_kick_timer - transmit if possible
  2215. * @t: timer contained in our gsm object
  2216. *
  2217. * Transmit data from DLCIs if the queue is empty. We can't rely on
  2218. * a tty wakeup except when we filled the pipe so we need to fire off
  2219. * new data ourselves in other cases.
  2220. */
  2221. static void gsm_kick_timer(struct timer_list *t)
  2222. {
  2223. struct gsm_mux *gsm = from_timer(gsm, t, kick_timer);
  2224. unsigned long flags;
  2225. int sent = 0;
  2226. spin_lock_irqsave(&gsm->tx_lock, flags);
  2227. /* If we have nothing running then we need to fire up */
  2228. if (gsm->tx_bytes < TX_THRESH_LO)
  2229. sent = gsm_dlci_data_sweep(gsm);
  2230. spin_unlock_irqrestore(&gsm->tx_lock, flags);
  2231. if (sent && debug & DBG_DATA)
  2232. pr_info("%s TX queue stalled\n", __func__);
  2233. }
  2234. /**
  2235. * gsm_dlci_copy_config_values - copy DLCI configuration
  2236. * @dlci: source DLCI
  2237. * @dc: configuration structure to fill
  2238. */
  2239. static void gsm_dlci_copy_config_values(struct gsm_dlci *dlci, struct gsm_dlci_config *dc)
  2240. {
  2241. memset(dc, 0, sizeof(*dc));
  2242. dc->channel = (u32)dlci->addr;
  2243. dc->adaption = (u32)dlci->adaption;
  2244. dc->mtu = (u32)dlci->mtu;
  2245. dc->priority = (u32)dlci->prio;
  2246. if (dlci->ftype == UIH)
  2247. dc->i = 1;
  2248. else
  2249. dc->i = 2;
  2250. dc->k = (u32)dlci->k;
  2251. }
  2252. /**
  2253. * gsm_dlci_config - configure DLCI from configuration
  2254. * @dlci: DLCI to configure
  2255. * @dc: DLCI configuration
  2256. * @open: open DLCI after configuration?
  2257. */
  2258. static int gsm_dlci_config(struct gsm_dlci *dlci, struct gsm_dlci_config *dc, int open)
  2259. {
  2260. struct gsm_mux *gsm;
  2261. bool need_restart = false;
  2262. bool need_open = false;
  2263. unsigned int i;
  2264. /*
  2265. * Check that userspace doesn't put stuff in here to prevent breakages
  2266. * in the future.
  2267. */
  2268. for (i = 0; i < ARRAY_SIZE(dc->reserved); i++)
  2269. if (dc->reserved[i])
  2270. return -EINVAL;
  2271. if (!dlci)
  2272. return -EINVAL;
  2273. gsm = dlci->gsm;
  2274. /* Stuff we don't support yet - I frame transport */
  2275. if (dc->adaption != 1 && dc->adaption != 2)
  2276. return -EOPNOTSUPP;
  2277. if (dc->mtu > MAX_MTU || dc->mtu < MIN_MTU || dc->mtu > gsm->mru)
  2278. return -EINVAL;
  2279. if (dc->priority >= 64)
  2280. return -EINVAL;
  2281. if (dc->i == 0 || dc->i > 2) /* UIH and UI only */
  2282. return -EINVAL;
  2283. if (dc->k > 7)
  2284. return -EINVAL;
  2285. if (dc->flags & ~GSM_FL_RESTART) /* allow future extensions */
  2286. return -EINVAL;
  2287. /*
  2288. * See what is needed for reconfiguration
  2289. */
  2290. /* Framing fields */
  2291. if (dc->adaption != dlci->adaption)
  2292. need_restart = true;
  2293. if (dc->mtu != dlci->mtu)
  2294. need_restart = true;
  2295. if (dc->i != dlci->ftype)
  2296. need_restart = true;
  2297. /* Requires care */
  2298. if (dc->priority != dlci->prio)
  2299. need_restart = true;
  2300. if (dc->flags & GSM_FL_RESTART)
  2301. need_restart = true;
  2302. if ((open && gsm->wait_config) || need_restart)
  2303. need_open = true;
  2304. if (dlci->state == DLCI_WAITING_CONFIG) {
  2305. need_restart = false;
  2306. need_open = true;
  2307. }
  2308. /*
  2309. * Close down what is needed, restart and initiate the new
  2310. * configuration.
  2311. */
  2312. if (need_restart) {
  2313. gsm_dlci_begin_close(dlci);
  2314. wait_event_interruptible(gsm->event, dlci->state == DLCI_CLOSED);
  2315. if (signal_pending(current))
  2316. return -EINTR;
  2317. }
  2318. /*
  2319. * Setup the new configuration values
  2320. */
  2321. dlci->adaption = (int)dc->adaption;
  2322. if (dc->mtu)
  2323. dlci->mtu = (unsigned int)dc->mtu;
  2324. else
  2325. dlci->mtu = gsm->mtu;
  2326. if (dc->priority)
  2327. dlci->prio = (u8)dc->priority;
  2328. else
  2329. dlci->prio = roundup(dlci->addr + 1, 8) - 1;
  2330. if (dc->i == 1)
  2331. dlci->ftype = UIH;
  2332. else if (dc->i == 2)
  2333. dlci->ftype = UI;
  2334. if (dc->k)
  2335. dlci->k = (u8)dc->k;
  2336. else
  2337. dlci->k = gsm->k;
  2338. if (need_open) {
  2339. if (gsm->initiator)
  2340. gsm_dlci_begin_open(dlci);
  2341. else
  2342. gsm_dlci_set_opening(dlci);
  2343. }
  2344. return 0;
  2345. }
  2346. /*
  2347. * Allocate/Free DLCI channels
  2348. */
  2349. /**
  2350. * gsm_dlci_alloc - allocate a DLCI
  2351. * @gsm: GSM mux
  2352. * @addr: address of the DLCI
  2353. *
  2354. * Allocate and install a new DLCI object into the GSM mux.
  2355. *
  2356. * FIXME: review locking races
  2357. */
  2358. static struct gsm_dlci *gsm_dlci_alloc(struct gsm_mux *gsm, int addr)
  2359. {
  2360. struct gsm_dlci *dlci = kzalloc(sizeof(struct gsm_dlci), GFP_ATOMIC);
  2361. if (dlci == NULL)
  2362. return NULL;
  2363. spin_lock_init(&dlci->lock);
  2364. mutex_init(&dlci->mutex);
  2365. if (kfifo_alloc(&dlci->fifo, TX_SIZE, GFP_KERNEL) < 0) {
  2366. kfree(dlci);
  2367. return NULL;
  2368. }
  2369. skb_queue_head_init(&dlci->skb_list);
  2370. timer_setup(&dlci->t1, gsm_dlci_t1, 0);
  2371. tty_port_init(&dlci->port);
  2372. dlci->port.ops = &gsm_port_ops;
  2373. dlci->gsm = gsm;
  2374. dlci->addr = addr;
  2375. dlci->adaption = gsm->adaption;
  2376. dlci->mtu = gsm->mtu;
  2377. if (addr == 0)
  2378. dlci->prio = 0;
  2379. else
  2380. dlci->prio = roundup(addr + 1, 8) - 1;
  2381. dlci->ftype = gsm->ftype;
  2382. dlci->k = gsm->k;
  2383. dlci->state = DLCI_CLOSED;
  2384. if (addr) {
  2385. dlci->data = gsm_dlci_data;
  2386. /* Prevent us from sending data before the link is up */
  2387. dlci->constipated = true;
  2388. } else {
  2389. dlci->data = gsm_dlci_command;
  2390. }
  2391. gsm->dlci[addr] = dlci;
  2392. return dlci;
  2393. }
  2394. /**
  2395. * gsm_dlci_free - free DLCI
  2396. * @port: tty port for DLCI to free
  2397. *
  2398. * Free up a DLCI.
  2399. *
  2400. * Can sleep.
  2401. */
  2402. static void gsm_dlci_free(struct tty_port *port)
  2403. {
  2404. struct gsm_dlci *dlci = container_of(port, struct gsm_dlci, port);
  2405. timer_shutdown_sync(&dlci->t1);
  2406. dlci->gsm->dlci[dlci->addr] = NULL;
  2407. kfifo_free(&dlci->fifo);
  2408. while ((dlci->skb = skb_dequeue(&dlci->skb_list)))
  2409. dev_kfree_skb(dlci->skb);
  2410. kfree(dlci);
  2411. }
  2412. static inline void dlci_get(struct gsm_dlci *dlci)
  2413. {
  2414. tty_port_get(&dlci->port);
  2415. }
  2416. static inline void dlci_put(struct gsm_dlci *dlci)
  2417. {
  2418. tty_port_put(&dlci->port);
  2419. }
  2420. static void gsm_destroy_network(struct gsm_dlci *dlci);
  2421. /**
  2422. * gsm_dlci_release - release DLCI
  2423. * @dlci: DLCI to destroy
  2424. *
  2425. * Release a DLCI. Actual free is deferred until either
  2426. * mux is closed or tty is closed - whichever is last.
  2427. *
  2428. * Can sleep.
  2429. */
  2430. static void gsm_dlci_release(struct gsm_dlci *dlci)
  2431. {
  2432. struct tty_struct *tty = tty_port_tty_get(&dlci->port);
  2433. if (tty) {
  2434. mutex_lock(&dlci->mutex);
  2435. gsm_destroy_network(dlci);
  2436. mutex_unlock(&dlci->mutex);
  2437. /* We cannot use tty_hangup() because in tty_kref_put() the tty
  2438. * driver assumes that the hangup queue is free and reuses it to
  2439. * queue release_one_tty() -> NULL pointer panic in
  2440. * process_one_work().
  2441. */
  2442. tty_vhangup(tty);
  2443. tty_port_tty_set(&dlci->port, NULL);
  2444. tty_kref_put(tty);
  2445. }
  2446. dlci->state = DLCI_CLOSED;
  2447. dlci_put(dlci);
  2448. }
  2449. /*
  2450. * LAPBish link layer logic
  2451. */
  2452. /**
  2453. * gsm_queue - a GSM frame is ready to process
  2454. * @gsm: pointer to our gsm mux
  2455. *
  2456. * At this point in time a frame has arrived and been demangled from
  2457. * the line encoding. All the differences between the encodings have
  2458. * been handled below us and the frame is unpacked into the structures.
  2459. * The fcs holds the header FCS but any data FCS must be added here.
  2460. */
  2461. static void gsm_queue(struct gsm_mux *gsm)
  2462. {
  2463. struct gsm_dlci *dlci;
  2464. u8 cr;
  2465. int address;
  2466. if (gsm->fcs != GOOD_FCS) {
  2467. gsm->bad_fcs++;
  2468. if (debug & DBG_DATA)
  2469. pr_debug("BAD FCS %02x\n", gsm->fcs);
  2470. return;
  2471. }
  2472. address = gsm->address >> 1;
  2473. if (address >= NUM_DLCI)
  2474. goto invalid;
  2475. cr = gsm->address & 1; /* C/R bit */
  2476. cr ^= gsm->initiator ? 0 : 1; /* Flip so 1 always means command */
  2477. gsm_print_packet("<--", address, cr, gsm->control, gsm->buf, gsm->len);
  2478. dlci = gsm->dlci[address];
  2479. switch (gsm->control) {
  2480. case SABM|PF:
  2481. if (cr == 1) {
  2482. gsm->open_error++;
  2483. goto invalid;
  2484. }
  2485. if (dlci == NULL)
  2486. dlci = gsm_dlci_alloc(gsm, address);
  2487. if (dlci == NULL) {
  2488. gsm->open_error++;
  2489. return;
  2490. }
  2491. if (dlci->dead)
  2492. gsm_response(gsm, address, DM|PF);
  2493. else {
  2494. gsm_response(gsm, address, UA|PF);
  2495. gsm_dlci_open(dlci);
  2496. }
  2497. break;
  2498. case DISC|PF:
  2499. if (cr == 1)
  2500. goto invalid;
  2501. if (dlci == NULL || dlci->state == DLCI_CLOSED) {
  2502. gsm_response(gsm, address, DM|PF);
  2503. return;
  2504. }
  2505. /* Real close complete */
  2506. gsm_response(gsm, address, UA|PF);
  2507. gsm_dlci_close(dlci);
  2508. break;
  2509. case UA|PF:
  2510. if (cr == 0 || dlci == NULL)
  2511. break;
  2512. switch (dlci->state) {
  2513. case DLCI_CLOSING:
  2514. gsm_dlci_close(dlci);
  2515. break;
  2516. case DLCI_OPENING:
  2517. gsm_dlci_open(dlci);
  2518. break;
  2519. default:
  2520. pr_debug("%s: unhandled state: %d\n", __func__,
  2521. dlci->state);
  2522. break;
  2523. }
  2524. break;
  2525. case DM: /* DM can be valid unsolicited */
  2526. case DM|PF:
  2527. if (cr)
  2528. goto invalid;
  2529. if (dlci == NULL)
  2530. return;
  2531. gsm_dlci_close(dlci);
  2532. break;
  2533. case UI:
  2534. case UI|PF:
  2535. case UIH:
  2536. case UIH|PF:
  2537. if (dlci == NULL || dlci->state != DLCI_OPEN) {
  2538. gsm_response(gsm, address, DM|PF);
  2539. return;
  2540. }
  2541. dlci->data(dlci, gsm->buf, gsm->len);
  2542. break;
  2543. default:
  2544. goto invalid;
  2545. }
  2546. return;
  2547. invalid:
  2548. gsm->malformed++;
  2549. return;
  2550. }
  2551. /**
  2552. * gsm0_receive_state_check_and_fix - check and correct receive state
  2553. * @gsm: gsm data for this ldisc instance
  2554. *
  2555. * Ensures that the current receive state is valid for basic option mode.
  2556. */
  2557. static void gsm0_receive_state_check_and_fix(struct gsm_mux *gsm)
  2558. {
  2559. switch (gsm->state) {
  2560. case GSM_SEARCH:
  2561. case GSM0_ADDRESS:
  2562. case GSM0_CONTROL:
  2563. case GSM0_LEN0:
  2564. case GSM0_LEN1:
  2565. case GSM0_DATA:
  2566. case GSM0_FCS:
  2567. case GSM0_SSOF:
  2568. break;
  2569. default:
  2570. gsm->state = GSM_SEARCH;
  2571. break;
  2572. }
  2573. }
  2574. /**
  2575. * gsm0_receive - perform processing for non-transparency
  2576. * @gsm: gsm data for this ldisc instance
  2577. * @c: character
  2578. *
  2579. * Receive bytes in gsm mode 0
  2580. */
  2581. static void gsm0_receive(struct gsm_mux *gsm, u8 c)
  2582. {
  2583. unsigned int len;
  2584. gsm0_receive_state_check_and_fix(gsm);
  2585. switch (gsm->state) {
  2586. case GSM_SEARCH: /* SOF marker */
  2587. if (c == GSM0_SOF) {
  2588. gsm->state = GSM0_ADDRESS;
  2589. gsm->address = 0;
  2590. gsm->len = 0;
  2591. gsm->fcs = INIT_FCS;
  2592. }
  2593. break;
  2594. case GSM0_ADDRESS: /* Address EA */
  2595. gsm->fcs = gsm_fcs_add(gsm->fcs, c);
  2596. if (gsm_read_ea(&gsm->address, c))
  2597. gsm->state = GSM0_CONTROL;
  2598. break;
  2599. case GSM0_CONTROL: /* Control Byte */
  2600. gsm->fcs = gsm_fcs_add(gsm->fcs, c);
  2601. gsm->control = c;
  2602. gsm->state = GSM0_LEN0;
  2603. break;
  2604. case GSM0_LEN0: /* Length EA */
  2605. gsm->fcs = gsm_fcs_add(gsm->fcs, c);
  2606. if (gsm_read_ea(&gsm->len, c)) {
  2607. if (gsm->len > gsm->mru) {
  2608. gsm->bad_size++;
  2609. gsm->state = GSM_SEARCH;
  2610. break;
  2611. }
  2612. gsm->count = 0;
  2613. if (!gsm->len)
  2614. gsm->state = GSM0_FCS;
  2615. else
  2616. gsm->state = GSM0_DATA;
  2617. break;
  2618. }
  2619. gsm->state = GSM0_LEN1;
  2620. break;
  2621. case GSM0_LEN1:
  2622. gsm->fcs = gsm_fcs_add(gsm->fcs, c);
  2623. len = c;
  2624. gsm->len |= len << 7;
  2625. if (gsm->len > gsm->mru) {
  2626. gsm->bad_size++;
  2627. gsm->state = GSM_SEARCH;
  2628. break;
  2629. }
  2630. gsm->count = 0;
  2631. if (!gsm->len)
  2632. gsm->state = GSM0_FCS;
  2633. else
  2634. gsm->state = GSM0_DATA;
  2635. break;
  2636. case GSM0_DATA: /* Data */
  2637. gsm->buf[gsm->count++] = c;
  2638. if (gsm->count >= MAX_MRU) {
  2639. gsm->bad_size++;
  2640. gsm->state = GSM_SEARCH;
  2641. } else if (gsm->count >= gsm->len) {
  2642. /* Calculate final FCS for UI frames over all data */
  2643. if ((gsm->control & ~PF) != UIH) {
  2644. gsm->fcs = gsm_fcs_add_block(gsm->fcs, gsm->buf,
  2645. gsm->count);
  2646. }
  2647. gsm->state = GSM0_FCS;
  2648. }
  2649. break;
  2650. case GSM0_FCS: /* FCS follows the packet */
  2651. gsm->fcs = gsm_fcs_add(gsm->fcs, c);
  2652. gsm->state = GSM0_SSOF;
  2653. break;
  2654. case GSM0_SSOF:
  2655. gsm->state = GSM_SEARCH;
  2656. if (c == GSM0_SOF)
  2657. gsm_queue(gsm);
  2658. else
  2659. gsm->bad_size++;
  2660. break;
  2661. default:
  2662. pr_debug("%s: unhandled state: %d\n", __func__, gsm->state);
  2663. break;
  2664. }
  2665. }
  2666. /**
  2667. * gsm1_receive_state_check_and_fix - check and correct receive state
  2668. * @gsm: gsm data for this ldisc instance
  2669. *
  2670. * Ensures that the current receive state is valid for advanced option mode.
  2671. */
  2672. static void gsm1_receive_state_check_and_fix(struct gsm_mux *gsm)
  2673. {
  2674. switch (gsm->state) {
  2675. case GSM_SEARCH:
  2676. case GSM1_START:
  2677. case GSM1_ADDRESS:
  2678. case GSM1_CONTROL:
  2679. case GSM1_DATA:
  2680. case GSM1_OVERRUN:
  2681. break;
  2682. default:
  2683. gsm->state = GSM_SEARCH;
  2684. break;
  2685. }
  2686. }
  2687. /**
  2688. * gsm1_receive - perform processing for non-transparency
  2689. * @gsm: gsm data for this ldisc instance
  2690. * @c: character
  2691. *
  2692. * Receive bytes in mode 1 (Advanced option)
  2693. */
  2694. static void gsm1_receive(struct gsm_mux *gsm, u8 c)
  2695. {
  2696. gsm1_receive_state_check_and_fix(gsm);
  2697. /* handle XON/XOFF */
  2698. if ((c & ISO_IEC_646_MASK) == XON) {
  2699. gsm->constipated = true;
  2700. return;
  2701. } else if ((c & ISO_IEC_646_MASK) == XOFF) {
  2702. gsm->constipated = false;
  2703. /* Kick the link in case it is idling */
  2704. gsmld_write_trigger(gsm);
  2705. return;
  2706. }
  2707. if (c == GSM1_SOF) {
  2708. /* EOF is only valid in frame if we have got to the data state */
  2709. if (gsm->state == GSM1_DATA) {
  2710. if (gsm->count < 1) {
  2711. /* Missing FSC */
  2712. gsm->malformed++;
  2713. gsm->state = GSM1_START;
  2714. return;
  2715. }
  2716. /* Remove the FCS from data */
  2717. gsm->count--;
  2718. if ((gsm->control & ~PF) != UIH) {
  2719. /* Calculate final FCS for UI frames over all
  2720. * data but FCS
  2721. */
  2722. gsm->fcs = gsm_fcs_add_block(gsm->fcs, gsm->buf,
  2723. gsm->count);
  2724. }
  2725. /* Add the FCS itself to test against GOOD_FCS */
  2726. gsm->fcs = gsm_fcs_add(gsm->fcs, gsm->buf[gsm->count]);
  2727. gsm->len = gsm->count;
  2728. gsm_queue(gsm);
  2729. gsm->state = GSM1_START;
  2730. return;
  2731. }
  2732. /* Any partial frame was a runt so go back to start */
  2733. if (gsm->state != GSM1_START) {
  2734. if (gsm->state != GSM_SEARCH)
  2735. gsm->malformed++;
  2736. gsm->state = GSM1_START;
  2737. }
  2738. /* A SOF in GSM_START means we are still reading idling or
  2739. framing bytes */
  2740. return;
  2741. }
  2742. if (c == GSM1_ESCAPE) {
  2743. gsm->escape = true;
  2744. return;
  2745. }
  2746. /* Only an unescaped SOF gets us out of GSM search */
  2747. if (gsm->state == GSM_SEARCH)
  2748. return;
  2749. if (gsm->escape) {
  2750. c ^= GSM1_ESCAPE_BITS;
  2751. gsm->escape = false;
  2752. }
  2753. switch (gsm->state) {
  2754. case GSM1_START: /* First byte after SOF */
  2755. gsm->address = 0;
  2756. gsm->state = GSM1_ADDRESS;
  2757. gsm->fcs = INIT_FCS;
  2758. fallthrough;
  2759. case GSM1_ADDRESS: /* Address continuation */
  2760. gsm->fcs = gsm_fcs_add(gsm->fcs, c);
  2761. if (gsm_read_ea(&gsm->address, c))
  2762. gsm->state = GSM1_CONTROL;
  2763. break;
  2764. case GSM1_CONTROL: /* Control Byte */
  2765. gsm->fcs = gsm_fcs_add(gsm->fcs, c);
  2766. gsm->control = c;
  2767. gsm->count = 0;
  2768. gsm->state = GSM1_DATA;
  2769. break;
  2770. case GSM1_DATA: /* Data */
  2771. if (gsm->count > gsm->mru || gsm->count > MAX_MRU) { /* Allow one for the FCS */
  2772. gsm->state = GSM1_OVERRUN;
  2773. gsm->bad_size++;
  2774. } else
  2775. gsm->buf[gsm->count++] = c;
  2776. break;
  2777. case GSM1_OVERRUN: /* Over-long - eg a dropped SOF */
  2778. break;
  2779. default:
  2780. pr_debug("%s: unhandled state: %d\n", __func__, gsm->state);
  2781. break;
  2782. }
  2783. }
  2784. /**
  2785. * gsm_error - handle tty error
  2786. * @gsm: ldisc data
  2787. *
  2788. * Handle an error in the receipt of data for a frame. Currently we just
  2789. * go back to hunting for a SOF.
  2790. *
  2791. * FIXME: better diagnostics ?
  2792. */
  2793. static void gsm_error(struct gsm_mux *gsm)
  2794. {
  2795. gsm->state = GSM_SEARCH;
  2796. gsm->io_error++;
  2797. }
  2798. /**
  2799. * gsm_cleanup_mux - generic GSM protocol cleanup
  2800. * @gsm: our mux
  2801. * @disc: disconnect link?
  2802. *
  2803. * Clean up the bits of the mux which are the same for all framing
  2804. * protocols. Remove the mux from the mux table, stop all the timers
  2805. * and then shut down each device hanging up the channels as we go.
  2806. */
  2807. static void gsm_cleanup_mux(struct gsm_mux *gsm, bool disc)
  2808. {
  2809. int i;
  2810. struct gsm_dlci *dlci;
  2811. struct gsm_msg *txq, *ntxq;
  2812. gsm->dead = true;
  2813. mutex_lock(&gsm->mutex);
  2814. dlci = gsm->dlci[0];
  2815. if (dlci) {
  2816. if (disc && dlci->state != DLCI_CLOSED) {
  2817. gsm_dlci_begin_close(dlci);
  2818. wait_event(gsm->event, dlci->state == DLCI_CLOSED);
  2819. }
  2820. dlci->dead = true;
  2821. }
  2822. /* Finish outstanding timers, making sure they are done */
  2823. del_timer_sync(&gsm->kick_timer);
  2824. del_timer_sync(&gsm->t2_timer);
  2825. del_timer_sync(&gsm->ka_timer);
  2826. /* Finish writing to ldisc */
  2827. flush_work(&gsm->tx_work);
  2828. /* Free up any link layer users and finally the control channel */
  2829. if (gsm->has_devices) {
  2830. gsm_unregister_devices(gsm_tty_driver, gsm->num);
  2831. gsm->has_devices = false;
  2832. }
  2833. for (i = NUM_DLCI - 1; i >= 0; i--)
  2834. if (gsm->dlci[i])
  2835. gsm_dlci_release(gsm->dlci[i]);
  2836. mutex_unlock(&gsm->mutex);
  2837. /* Now wipe the queues */
  2838. tty_ldisc_flush(gsm->tty);
  2839. guard(spinlock_irqsave)(&gsm->tx_lock);
  2840. list_for_each_entry_safe(txq, ntxq, &gsm->tx_ctrl_list, list)
  2841. kfree(txq);
  2842. INIT_LIST_HEAD(&gsm->tx_ctrl_list);
  2843. list_for_each_entry_safe(txq, ntxq, &gsm->tx_data_list, list)
  2844. kfree(txq);
  2845. INIT_LIST_HEAD(&gsm->tx_data_list);
  2846. }
  2847. /**
  2848. * gsm_activate_mux - generic GSM setup
  2849. * @gsm: our mux
  2850. *
  2851. * Set up the bits of the mux which are the same for all framing
  2852. * protocols. Add the mux to the mux table so it can be opened and
  2853. * finally kick off connecting to DLCI 0 on the modem.
  2854. */
  2855. static int gsm_activate_mux(struct gsm_mux *gsm)
  2856. {
  2857. struct gsm_dlci *dlci;
  2858. int ret;
  2859. dlci = gsm_dlci_alloc(gsm, 0);
  2860. if (dlci == NULL)
  2861. return -ENOMEM;
  2862. if (gsm->encoding == GSM_BASIC_OPT)
  2863. gsm->receive = gsm0_receive;
  2864. else
  2865. gsm->receive = gsm1_receive;
  2866. ret = gsm_register_devices(gsm_tty_driver, gsm->num);
  2867. if (ret)
  2868. return ret;
  2869. gsm->has_devices = true;
  2870. gsm->dead = false; /* Tty opens are now permissible */
  2871. return 0;
  2872. }
  2873. /**
  2874. * gsm_free_mux - free up a mux
  2875. * @gsm: mux to free
  2876. *
  2877. * Dispose of allocated resources for a dead mux
  2878. */
  2879. static void gsm_free_mux(struct gsm_mux *gsm)
  2880. {
  2881. int i;
  2882. for (i = 0; i < MAX_MUX; i++) {
  2883. if (gsm == gsm_mux[i]) {
  2884. gsm_mux[i] = NULL;
  2885. break;
  2886. }
  2887. }
  2888. mutex_destroy(&gsm->mutex);
  2889. kfree(gsm->txframe);
  2890. kfree(gsm->buf);
  2891. kfree(gsm);
  2892. }
  2893. /**
  2894. * gsm_free_muxr - free up a mux
  2895. * @ref: kreference to the mux to free
  2896. *
  2897. * Dispose of allocated resources for a dead mux
  2898. */
  2899. static void gsm_free_muxr(struct kref *ref)
  2900. {
  2901. struct gsm_mux *gsm = container_of(ref, struct gsm_mux, ref);
  2902. gsm_free_mux(gsm);
  2903. }
  2904. static inline void mux_get(struct gsm_mux *gsm)
  2905. {
  2906. unsigned long flags;
  2907. spin_lock_irqsave(&gsm_mux_lock, flags);
  2908. kref_get(&gsm->ref);
  2909. spin_unlock_irqrestore(&gsm_mux_lock, flags);
  2910. }
  2911. static inline void mux_put(struct gsm_mux *gsm)
  2912. {
  2913. unsigned long flags;
  2914. spin_lock_irqsave(&gsm_mux_lock, flags);
  2915. kref_put(&gsm->ref, gsm_free_muxr);
  2916. spin_unlock_irqrestore(&gsm_mux_lock, flags);
  2917. }
  2918. static inline unsigned int mux_num_to_base(struct gsm_mux *gsm)
  2919. {
  2920. return gsm->num * NUM_DLCI;
  2921. }
  2922. static inline unsigned int mux_line_to_num(unsigned int line)
  2923. {
  2924. return line / NUM_DLCI;
  2925. }
  2926. /**
  2927. * gsm_alloc_mux - allocate a mux
  2928. *
  2929. * Creates a new mux ready for activation.
  2930. */
  2931. static struct gsm_mux *gsm_alloc_mux(void)
  2932. {
  2933. int i;
  2934. struct gsm_mux *gsm = kzalloc(sizeof(struct gsm_mux), GFP_KERNEL);
  2935. if (gsm == NULL)
  2936. return NULL;
  2937. gsm->buf = kmalloc(MAX_MRU + 1, GFP_KERNEL);
  2938. if (gsm->buf == NULL) {
  2939. kfree(gsm);
  2940. return NULL;
  2941. }
  2942. gsm->txframe = kmalloc(2 * (MAX_MTU + PROT_OVERHEAD - 1), GFP_KERNEL);
  2943. if (gsm->txframe == NULL) {
  2944. kfree(gsm->buf);
  2945. kfree(gsm);
  2946. return NULL;
  2947. }
  2948. spin_lock_init(&gsm->lock);
  2949. mutex_init(&gsm->mutex);
  2950. kref_init(&gsm->ref);
  2951. INIT_LIST_HEAD(&gsm->tx_ctrl_list);
  2952. INIT_LIST_HEAD(&gsm->tx_data_list);
  2953. timer_setup(&gsm->kick_timer, gsm_kick_timer, 0);
  2954. timer_setup(&gsm->t2_timer, gsm_control_retransmit, 0);
  2955. timer_setup(&gsm->ka_timer, gsm_control_keep_alive, 0);
  2956. INIT_WORK(&gsm->tx_work, gsmld_write_task);
  2957. init_waitqueue_head(&gsm->event);
  2958. spin_lock_init(&gsm->control_lock);
  2959. spin_lock_init(&gsm->tx_lock);
  2960. gsm->t1 = T1;
  2961. gsm->t2 = T2;
  2962. gsm->t3 = T3;
  2963. gsm->n2 = N2;
  2964. gsm->k = K;
  2965. gsm->ftype = UIH;
  2966. gsm->adaption = 1;
  2967. gsm->encoding = GSM_ADV_OPT;
  2968. gsm->mru = 64; /* Default to encoding 1 so these should be 64 */
  2969. gsm->mtu = 64;
  2970. gsm->dead = true; /* Avoid early tty opens */
  2971. gsm->wait_config = false; /* Disabled */
  2972. gsm->keep_alive = 0; /* Disabled */
  2973. /* Store the instance to the mux array or abort if no space is
  2974. * available.
  2975. */
  2976. spin_lock(&gsm_mux_lock);
  2977. for (i = 0; i < MAX_MUX; i++) {
  2978. if (!gsm_mux[i]) {
  2979. gsm_mux[i] = gsm;
  2980. gsm->num = i;
  2981. break;
  2982. }
  2983. }
  2984. spin_unlock(&gsm_mux_lock);
  2985. if (i == MAX_MUX) {
  2986. mutex_destroy(&gsm->mutex);
  2987. kfree(gsm->txframe);
  2988. kfree(gsm->buf);
  2989. kfree(gsm);
  2990. return NULL;
  2991. }
  2992. return gsm;
  2993. }
  2994. static void gsm_copy_config_values(struct gsm_mux *gsm,
  2995. struct gsm_config *c)
  2996. {
  2997. memset(c, 0, sizeof(*c));
  2998. c->adaption = gsm->adaption;
  2999. c->encapsulation = gsm->encoding;
  3000. c->initiator = gsm->initiator;
  3001. c->t1 = gsm->t1;
  3002. c->t2 = gsm->t2;
  3003. c->t3 = gsm->t3;
  3004. c->n2 = gsm->n2;
  3005. if (gsm->ftype == UIH)
  3006. c->i = 1;
  3007. else
  3008. c->i = 2;
  3009. pr_debug("Ftype %d i %d\n", gsm->ftype, c->i);
  3010. c->mru = gsm->mru;
  3011. c->mtu = gsm->mtu;
  3012. c->k = gsm->k;
  3013. }
  3014. static int gsm_config(struct gsm_mux *gsm, struct gsm_config *c)
  3015. {
  3016. int need_close = 0;
  3017. int need_restart = 0;
  3018. /* Stuff we don't support yet - UI or I frame transport */
  3019. if (c->adaption != 1 && c->adaption != 2)
  3020. return -EOPNOTSUPP;
  3021. /* Check the MRU/MTU range looks sane */
  3022. if (c->mru < MIN_MTU || c->mtu < MIN_MTU)
  3023. return -EINVAL;
  3024. if (c->mru > MAX_MRU || c->mtu > MAX_MTU)
  3025. return -EINVAL;
  3026. if (c->t3 > MAX_T3)
  3027. return -EINVAL;
  3028. if (c->n2 > 255)
  3029. return -EINVAL;
  3030. if (c->encapsulation > 1) /* Basic, advanced, no I */
  3031. return -EINVAL;
  3032. if (c->initiator > 1)
  3033. return -EINVAL;
  3034. if (c->k > MAX_WINDOW_SIZE)
  3035. return -EINVAL;
  3036. if (c->i == 0 || c->i > 2) /* UIH and UI only */
  3037. return -EINVAL;
  3038. /*
  3039. * See what is needed for reconfiguration
  3040. */
  3041. /* Timing fields */
  3042. if (c->t1 != 0 && c->t1 != gsm->t1)
  3043. need_restart = 1;
  3044. if (c->t2 != 0 && c->t2 != gsm->t2)
  3045. need_restart = 1;
  3046. if (c->encapsulation != gsm->encoding)
  3047. need_restart = 1;
  3048. if (c->adaption != gsm->adaption)
  3049. need_restart = 1;
  3050. /* Requires care */
  3051. if (c->initiator != gsm->initiator)
  3052. need_close = 1;
  3053. if (c->mru != gsm->mru)
  3054. need_restart = 1;
  3055. if (c->mtu != gsm->mtu)
  3056. need_restart = 1;
  3057. /*
  3058. * Close down what is needed, restart and initiate the new
  3059. * configuration. On the first time there is no DLCI[0]
  3060. * and closing or cleaning up is not necessary.
  3061. */
  3062. if (need_close || need_restart)
  3063. gsm_cleanup_mux(gsm, true);
  3064. gsm->initiator = c->initiator;
  3065. gsm->mru = c->mru;
  3066. gsm->mtu = c->mtu;
  3067. gsm->encoding = c->encapsulation ? GSM_ADV_OPT : GSM_BASIC_OPT;
  3068. gsm->adaption = c->adaption;
  3069. gsm->n2 = c->n2;
  3070. if (c->i == 1)
  3071. gsm->ftype = UIH;
  3072. else if (c->i == 2)
  3073. gsm->ftype = UI;
  3074. if (c->t1)
  3075. gsm->t1 = c->t1;
  3076. if (c->t2)
  3077. gsm->t2 = c->t2;
  3078. if (c->t3)
  3079. gsm->t3 = c->t3;
  3080. if (c->k)
  3081. gsm->k = c->k;
  3082. /*
  3083. * FIXME: We need to separate activation/deactivation from adding
  3084. * and removing from the mux array
  3085. */
  3086. if (gsm->dead) {
  3087. int ret = gsm_activate_mux(gsm);
  3088. if (ret)
  3089. return ret;
  3090. if (gsm->initiator)
  3091. gsm_dlci_begin_open(gsm->dlci[0]);
  3092. }
  3093. return 0;
  3094. }
  3095. static void gsm_copy_config_ext_values(struct gsm_mux *gsm,
  3096. struct gsm_config_ext *ce)
  3097. {
  3098. memset(ce, 0, sizeof(*ce));
  3099. ce->wait_config = gsm->wait_config ? 1 : 0;
  3100. ce->keep_alive = gsm->keep_alive;
  3101. }
  3102. static int gsm_config_ext(struct gsm_mux *gsm, struct gsm_config_ext *ce)
  3103. {
  3104. bool need_restart = false;
  3105. unsigned int i;
  3106. /*
  3107. * Check that userspace doesn't put stuff in here to prevent breakages
  3108. * in the future.
  3109. */
  3110. for (i = 0; i < ARRAY_SIZE(ce->reserved); i++)
  3111. if (ce->reserved[i])
  3112. return -EINVAL;
  3113. if (ce->flags & ~GSM_FL_RESTART)
  3114. return -EINVAL;
  3115. /* Requires care */
  3116. if (ce->flags & GSM_FL_RESTART)
  3117. need_restart = true;
  3118. /*
  3119. * Close down what is needed, restart and initiate the new
  3120. * configuration. On the first time there is no DLCI[0]
  3121. * and closing or cleaning up is not necessary.
  3122. */
  3123. if (need_restart)
  3124. gsm_cleanup_mux(gsm, true);
  3125. /*
  3126. * Setup the new configuration values
  3127. */
  3128. gsm->wait_config = ce->wait_config ? true : false;
  3129. gsm->keep_alive = ce->keep_alive;
  3130. if (gsm->dead) {
  3131. int ret = gsm_activate_mux(gsm);
  3132. if (ret)
  3133. return ret;
  3134. if (gsm->initiator)
  3135. gsm_dlci_begin_open(gsm->dlci[0]);
  3136. }
  3137. return 0;
  3138. }
  3139. /**
  3140. * gsmld_output - write to link
  3141. * @gsm: our mux
  3142. * @data: bytes to output
  3143. * @len: size
  3144. *
  3145. * Write a block of data from the GSM mux to the data channel. This
  3146. * will eventually be serialized from above but at the moment isn't.
  3147. */
  3148. static int gsmld_output(struct gsm_mux *gsm, u8 *data, int len)
  3149. {
  3150. if (tty_write_room(gsm->tty) < len) {
  3151. set_bit(TTY_DO_WRITE_WAKEUP, &gsm->tty->flags);
  3152. return -ENOSPC;
  3153. }
  3154. if (debug & DBG_DATA)
  3155. gsm_hex_dump_bytes(__func__, data, len);
  3156. return gsm->tty->ops->write(gsm->tty, data, len);
  3157. }
  3158. /**
  3159. * gsmld_write_trigger - schedule ldisc write task
  3160. * @gsm: our mux
  3161. */
  3162. static void gsmld_write_trigger(struct gsm_mux *gsm)
  3163. {
  3164. if (!gsm || !gsm->dlci[0] || gsm->dlci[0]->dead)
  3165. return;
  3166. schedule_work(&gsm->tx_work);
  3167. }
  3168. /**
  3169. * gsmld_write_task - ldisc write task
  3170. * @work: our tx write work
  3171. *
  3172. * Writes out data to the ldisc if possible. We are doing this here to
  3173. * avoid dead-locking. This returns if no space or data is left for output.
  3174. */
  3175. static void gsmld_write_task(struct work_struct *work)
  3176. {
  3177. struct gsm_mux *gsm = container_of(work, struct gsm_mux, tx_work);
  3178. unsigned long flags;
  3179. int i, ret;
  3180. /* All outstanding control channel and control messages and one data
  3181. * frame is sent.
  3182. */
  3183. ret = -ENODEV;
  3184. spin_lock_irqsave(&gsm->tx_lock, flags);
  3185. if (gsm->tty)
  3186. ret = gsm_data_kick(gsm);
  3187. spin_unlock_irqrestore(&gsm->tx_lock, flags);
  3188. if (ret >= 0)
  3189. for (i = 0; i < NUM_DLCI; i++)
  3190. if (gsm->dlci[i])
  3191. tty_port_tty_wakeup(&gsm->dlci[i]->port);
  3192. }
  3193. /**
  3194. * gsmld_attach_gsm - mode set up
  3195. * @tty: our tty structure
  3196. * @gsm: our mux
  3197. *
  3198. * Set up the MUX for basic mode and commence connecting to the
  3199. * modem. Currently called from the line discipline set up but
  3200. * will need moving to an ioctl path.
  3201. */
  3202. static void gsmld_attach_gsm(struct tty_struct *tty, struct gsm_mux *gsm)
  3203. {
  3204. gsm->tty = tty_kref_get(tty);
  3205. /* Turn off tty XON/XOFF handling to handle it explicitly. */
  3206. gsm->old_c_iflag = tty->termios.c_iflag;
  3207. tty->termios.c_iflag &= (IXON | IXOFF);
  3208. }
  3209. /**
  3210. * gsmld_detach_gsm - stop doing 0710 mux
  3211. * @tty: tty attached to the mux
  3212. * @gsm: mux
  3213. *
  3214. * Shutdown and then clean up the resources used by the line discipline
  3215. */
  3216. static void gsmld_detach_gsm(struct tty_struct *tty, struct gsm_mux *gsm)
  3217. {
  3218. WARN_ON(tty != gsm->tty);
  3219. /* Restore tty XON/XOFF handling. */
  3220. gsm->tty->termios.c_iflag = gsm->old_c_iflag;
  3221. tty_kref_put(gsm->tty);
  3222. gsm->tty = NULL;
  3223. }
  3224. static void gsmld_receive_buf(struct tty_struct *tty, const u8 *cp,
  3225. const u8 *fp, size_t count)
  3226. {
  3227. struct gsm_mux *gsm = tty->disc_data;
  3228. u8 flags = TTY_NORMAL;
  3229. if (debug & DBG_DATA)
  3230. gsm_hex_dump_bytes(__func__, cp, count);
  3231. for (; count; count--, cp++) {
  3232. if (fp)
  3233. flags = *fp++;
  3234. switch (flags) {
  3235. case TTY_NORMAL:
  3236. if (gsm->receive)
  3237. gsm->receive(gsm, *cp);
  3238. break;
  3239. case TTY_OVERRUN:
  3240. case TTY_BREAK:
  3241. case TTY_PARITY:
  3242. case TTY_FRAME:
  3243. gsm_error(gsm);
  3244. break;
  3245. default:
  3246. WARN_ONCE(1, "%s: unknown flag %d\n",
  3247. tty_name(tty), flags);
  3248. break;
  3249. }
  3250. }
  3251. /* FASYNC if needed ? */
  3252. /* If clogged call tty_throttle(tty); */
  3253. }
  3254. /**
  3255. * gsmld_flush_buffer - clean input queue
  3256. * @tty: terminal device
  3257. *
  3258. * Flush the input buffer. Called when the line discipline is
  3259. * being closed, when the tty layer wants the buffer flushed (eg
  3260. * at hangup).
  3261. */
  3262. static void gsmld_flush_buffer(struct tty_struct *tty)
  3263. {
  3264. }
  3265. /**
  3266. * gsmld_close - close the ldisc for this tty
  3267. * @tty: device
  3268. *
  3269. * Called from the terminal layer when this line discipline is
  3270. * being shut down, either because of a close or becsuse of a
  3271. * discipline change. The function will not be called while other
  3272. * ldisc methods are in progress.
  3273. */
  3274. static void gsmld_close(struct tty_struct *tty)
  3275. {
  3276. struct gsm_mux *gsm = tty->disc_data;
  3277. /* The ldisc locks and closes the port before calling our close. This
  3278. * means we have no way to do a proper disconnect. We will not bother
  3279. * to do one.
  3280. */
  3281. gsm_cleanup_mux(gsm, false);
  3282. gsmld_detach_gsm(tty, gsm);
  3283. gsmld_flush_buffer(tty);
  3284. /* Do other clean up here */
  3285. mux_put(gsm);
  3286. }
  3287. /**
  3288. * gsmld_open - open an ldisc
  3289. * @tty: terminal to open
  3290. *
  3291. * Called when this line discipline is being attached to the
  3292. * terminal device. Can sleep. Called serialized so that no
  3293. * other events will occur in parallel. No further open will occur
  3294. * until a close.
  3295. */
  3296. static int gsmld_open(struct tty_struct *tty)
  3297. {
  3298. struct gsm_mux *gsm;
  3299. if (!capable(CAP_NET_ADMIN))
  3300. return -EPERM;
  3301. if (tty->ops->write == NULL)
  3302. return -EINVAL;
  3303. /* Attach our ldisc data */
  3304. gsm = gsm_alloc_mux();
  3305. if (gsm == NULL)
  3306. return -ENOMEM;
  3307. tty->disc_data = gsm;
  3308. tty->receive_room = 65536;
  3309. /* Attach the initial passive connection */
  3310. gsmld_attach_gsm(tty, gsm);
  3311. /* The mux will not be activated yet, we wait for correct
  3312. * configuration first.
  3313. */
  3314. if (gsm->encoding == GSM_BASIC_OPT)
  3315. gsm->receive = gsm0_receive;
  3316. else
  3317. gsm->receive = gsm1_receive;
  3318. return 0;
  3319. }
  3320. /**
  3321. * gsmld_write_wakeup - asynchronous I/O notifier
  3322. * @tty: tty device
  3323. *
  3324. * Required for the ptys, serial driver etc. since processes
  3325. * that attach themselves to the master and rely on ASYNC
  3326. * IO must be woken up
  3327. */
  3328. static void gsmld_write_wakeup(struct tty_struct *tty)
  3329. {
  3330. struct gsm_mux *gsm = tty->disc_data;
  3331. /* Queue poll */
  3332. gsmld_write_trigger(gsm);
  3333. }
  3334. /**
  3335. * gsmld_read - read function for tty
  3336. * @tty: tty device
  3337. * @file: file object
  3338. * @buf: userspace buffer pointer
  3339. * @nr: size of I/O
  3340. * @cookie: unused
  3341. * @offset: unused
  3342. *
  3343. * Perform reads for the line discipline. We are guaranteed that the
  3344. * line discipline will not be closed under us but we may get multiple
  3345. * parallel readers and must handle this ourselves. We may also get
  3346. * a hangup. Always called in user context, may sleep.
  3347. *
  3348. * This code must be sure never to sleep through a hangup.
  3349. */
  3350. static ssize_t gsmld_read(struct tty_struct *tty, struct file *file, u8 *buf,
  3351. size_t nr, void **cookie, unsigned long offset)
  3352. {
  3353. return -EOPNOTSUPP;
  3354. }
  3355. /**
  3356. * gsmld_write - write function for tty
  3357. * @tty: tty device
  3358. * @file: file object
  3359. * @buf: userspace buffer pointer
  3360. * @nr: size of I/O
  3361. *
  3362. * Called when the owner of the device wants to send a frame
  3363. * itself (or some other control data). The data is transferred
  3364. * as-is and must be properly framed and checksummed as appropriate
  3365. * by userspace. Frames are either sent whole or not at all as this
  3366. * avoids pain user side.
  3367. */
  3368. static ssize_t gsmld_write(struct tty_struct *tty, struct file *file,
  3369. const u8 *buf, size_t nr)
  3370. {
  3371. struct gsm_mux *gsm = tty->disc_data;
  3372. unsigned long flags;
  3373. size_t space;
  3374. int ret;
  3375. if (!gsm)
  3376. return -ENODEV;
  3377. ret = -ENOBUFS;
  3378. spin_lock_irqsave(&gsm->tx_lock, flags);
  3379. space = tty_write_room(tty);
  3380. if (space >= nr)
  3381. ret = tty->ops->write(tty, buf, nr);
  3382. else
  3383. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  3384. spin_unlock_irqrestore(&gsm->tx_lock, flags);
  3385. return ret;
  3386. }
  3387. /**
  3388. * gsmld_poll - poll method for N_GSM0710
  3389. * @tty: terminal device
  3390. * @file: file accessing it
  3391. * @wait: poll table
  3392. *
  3393. * Called when the line discipline is asked to poll() for data or
  3394. * for special events. This code is not serialized with respect to
  3395. * other events save open/close.
  3396. *
  3397. * This code must be sure never to sleep through a hangup.
  3398. * Called without the kernel lock held - fine
  3399. */
  3400. static __poll_t gsmld_poll(struct tty_struct *tty, struct file *file,
  3401. poll_table *wait)
  3402. {
  3403. __poll_t mask = 0;
  3404. struct gsm_mux *gsm = tty->disc_data;
  3405. poll_wait(file, &tty->read_wait, wait);
  3406. poll_wait(file, &tty->write_wait, wait);
  3407. if (gsm->dead)
  3408. mask |= EPOLLHUP;
  3409. if (tty_hung_up_p(file))
  3410. mask |= EPOLLHUP;
  3411. if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
  3412. mask |= EPOLLHUP;
  3413. if (!tty_is_writelocked(tty) && tty_write_room(tty) > 0)
  3414. mask |= EPOLLOUT | EPOLLWRNORM;
  3415. return mask;
  3416. }
  3417. static int gsmld_ioctl(struct tty_struct *tty, unsigned int cmd,
  3418. unsigned long arg)
  3419. {
  3420. struct gsm_config c;
  3421. struct gsm_config_ext ce;
  3422. struct gsm_dlci_config dc;
  3423. struct gsm_mux *gsm = tty->disc_data;
  3424. unsigned int base, addr;
  3425. struct gsm_dlci *dlci;
  3426. switch (cmd) {
  3427. case GSMIOC_GETCONF:
  3428. gsm_copy_config_values(gsm, &c);
  3429. if (copy_to_user((void __user *)arg, &c, sizeof(c)))
  3430. return -EFAULT;
  3431. return 0;
  3432. case GSMIOC_SETCONF:
  3433. if (copy_from_user(&c, (void __user *)arg, sizeof(c)))
  3434. return -EFAULT;
  3435. return gsm_config(gsm, &c);
  3436. case GSMIOC_GETFIRST:
  3437. base = mux_num_to_base(gsm);
  3438. return put_user(base + 1, (__u32 __user *)arg);
  3439. case GSMIOC_GETCONF_EXT:
  3440. gsm_copy_config_ext_values(gsm, &ce);
  3441. if (copy_to_user((void __user *)arg, &ce, sizeof(ce)))
  3442. return -EFAULT;
  3443. return 0;
  3444. case GSMIOC_SETCONF_EXT:
  3445. if (copy_from_user(&ce, (void __user *)arg, sizeof(ce)))
  3446. return -EFAULT;
  3447. return gsm_config_ext(gsm, &ce);
  3448. case GSMIOC_GETCONF_DLCI:
  3449. if (copy_from_user(&dc, (void __user *)arg, sizeof(dc)))
  3450. return -EFAULT;
  3451. if (dc.channel == 0 || dc.channel >= NUM_DLCI)
  3452. return -EINVAL;
  3453. addr = array_index_nospec(dc.channel, NUM_DLCI);
  3454. dlci = gsm->dlci[addr];
  3455. if (!dlci) {
  3456. dlci = gsm_dlci_alloc(gsm, addr);
  3457. if (!dlci)
  3458. return -ENOMEM;
  3459. }
  3460. gsm_dlci_copy_config_values(dlci, &dc);
  3461. if (copy_to_user((void __user *)arg, &dc, sizeof(dc)))
  3462. return -EFAULT;
  3463. return 0;
  3464. case GSMIOC_SETCONF_DLCI:
  3465. if (copy_from_user(&dc, (void __user *)arg, sizeof(dc)))
  3466. return -EFAULT;
  3467. if (dc.channel == 0 || dc.channel >= NUM_DLCI)
  3468. return -EINVAL;
  3469. addr = array_index_nospec(dc.channel, NUM_DLCI);
  3470. dlci = gsm->dlci[addr];
  3471. if (!dlci) {
  3472. dlci = gsm_dlci_alloc(gsm, addr);
  3473. if (!dlci)
  3474. return -ENOMEM;
  3475. }
  3476. return gsm_dlci_config(dlci, &dc, 0);
  3477. default:
  3478. return n_tty_ioctl_helper(tty, cmd, arg);
  3479. }
  3480. }
  3481. /*
  3482. * Network interface
  3483. *
  3484. */
  3485. static int gsm_mux_net_open(struct net_device *net)
  3486. {
  3487. pr_debug("%s called\n", __func__);
  3488. netif_start_queue(net);
  3489. return 0;
  3490. }
  3491. static int gsm_mux_net_close(struct net_device *net)
  3492. {
  3493. netif_stop_queue(net);
  3494. return 0;
  3495. }
  3496. static void dlci_net_free(struct gsm_dlci *dlci)
  3497. {
  3498. if (!dlci->net) {
  3499. WARN_ON(1);
  3500. return;
  3501. }
  3502. dlci->adaption = dlci->prev_adaption;
  3503. dlci->data = dlci->prev_data;
  3504. free_netdev(dlci->net);
  3505. dlci->net = NULL;
  3506. }
  3507. static void net_free(struct kref *ref)
  3508. {
  3509. struct gsm_mux_net *mux_net;
  3510. struct gsm_dlci *dlci;
  3511. mux_net = container_of(ref, struct gsm_mux_net, ref);
  3512. dlci = mux_net->dlci;
  3513. if (dlci->net) {
  3514. unregister_netdev(dlci->net);
  3515. dlci_net_free(dlci);
  3516. }
  3517. }
  3518. static inline void muxnet_get(struct gsm_mux_net *mux_net)
  3519. {
  3520. kref_get(&mux_net->ref);
  3521. }
  3522. static inline void muxnet_put(struct gsm_mux_net *mux_net)
  3523. {
  3524. kref_put(&mux_net->ref, net_free);
  3525. }
  3526. static netdev_tx_t gsm_mux_net_start_xmit(struct sk_buff *skb,
  3527. struct net_device *net)
  3528. {
  3529. struct gsm_mux_net *mux_net = netdev_priv(net);
  3530. struct gsm_dlci *dlci = mux_net->dlci;
  3531. muxnet_get(mux_net);
  3532. skb_queue_head(&dlci->skb_list, skb);
  3533. net->stats.tx_packets++;
  3534. net->stats.tx_bytes += skb->len;
  3535. gsm_dlci_data_kick(dlci);
  3536. /* And tell the kernel when the last transmit started. */
  3537. netif_trans_update(net);
  3538. muxnet_put(mux_net);
  3539. return NETDEV_TX_OK;
  3540. }
  3541. /* called when a packet did not ack after watchdogtimeout */
  3542. static void gsm_mux_net_tx_timeout(struct net_device *net, unsigned int txqueue)
  3543. {
  3544. /* Tell syslog we are hosed. */
  3545. dev_dbg(&net->dev, "Tx timed out.\n");
  3546. /* Update statistics */
  3547. net->stats.tx_errors++;
  3548. }
  3549. static void gsm_mux_rx_netchar(struct gsm_dlci *dlci, const u8 *in_buf, int size)
  3550. {
  3551. struct net_device *net = dlci->net;
  3552. struct sk_buff *skb;
  3553. struct gsm_mux_net *mux_net = netdev_priv(net);
  3554. muxnet_get(mux_net);
  3555. /* Allocate an sk_buff */
  3556. skb = dev_alloc_skb(size + NET_IP_ALIGN);
  3557. if (!skb) {
  3558. /* We got no receive buffer. */
  3559. net->stats.rx_dropped++;
  3560. muxnet_put(mux_net);
  3561. return;
  3562. }
  3563. skb_reserve(skb, NET_IP_ALIGN);
  3564. skb_put_data(skb, in_buf, size);
  3565. skb->dev = net;
  3566. skb->protocol = htons(ETH_P_IP);
  3567. /* Ship it off to the kernel */
  3568. netif_rx(skb);
  3569. /* update out statistics */
  3570. net->stats.rx_packets++;
  3571. net->stats.rx_bytes += size;
  3572. muxnet_put(mux_net);
  3573. return;
  3574. }
  3575. static void gsm_mux_net_init(struct net_device *net)
  3576. {
  3577. static const struct net_device_ops gsm_netdev_ops = {
  3578. .ndo_open = gsm_mux_net_open,
  3579. .ndo_stop = gsm_mux_net_close,
  3580. .ndo_start_xmit = gsm_mux_net_start_xmit,
  3581. .ndo_tx_timeout = gsm_mux_net_tx_timeout,
  3582. };
  3583. net->netdev_ops = &gsm_netdev_ops;
  3584. /* fill in the other fields */
  3585. net->watchdog_timeo = GSM_NET_TX_TIMEOUT;
  3586. net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  3587. net->type = ARPHRD_NONE;
  3588. net->tx_queue_len = 10;
  3589. }
  3590. /* caller holds the dlci mutex */
  3591. static void gsm_destroy_network(struct gsm_dlci *dlci)
  3592. {
  3593. struct gsm_mux_net *mux_net;
  3594. pr_debug("destroy network interface\n");
  3595. if (!dlci->net)
  3596. return;
  3597. mux_net = netdev_priv(dlci->net);
  3598. muxnet_put(mux_net);
  3599. }
  3600. /* caller holds the dlci mutex */
  3601. static int gsm_create_network(struct gsm_dlci *dlci, struct gsm_netconfig *nc)
  3602. {
  3603. char *netname;
  3604. int retval = 0;
  3605. struct net_device *net;
  3606. struct gsm_mux_net *mux_net;
  3607. if (!capable(CAP_NET_ADMIN))
  3608. return -EPERM;
  3609. /* Already in a non tty mode */
  3610. if (dlci->adaption > 2)
  3611. return -EBUSY;
  3612. if (nc->protocol != htons(ETH_P_IP))
  3613. return -EPROTONOSUPPORT;
  3614. if (nc->adaption != 3 && nc->adaption != 4)
  3615. return -EPROTONOSUPPORT;
  3616. pr_debug("create network interface\n");
  3617. netname = "gsm%d";
  3618. if (nc->if_name[0] != '\0')
  3619. netname = nc->if_name;
  3620. net = alloc_netdev(sizeof(struct gsm_mux_net), netname,
  3621. NET_NAME_UNKNOWN, gsm_mux_net_init);
  3622. if (!net) {
  3623. pr_err("alloc_netdev failed\n");
  3624. return -ENOMEM;
  3625. }
  3626. net->mtu = dlci->mtu;
  3627. net->min_mtu = MIN_MTU;
  3628. net->max_mtu = dlci->mtu;
  3629. mux_net = netdev_priv(net);
  3630. mux_net->dlci = dlci;
  3631. kref_init(&mux_net->ref);
  3632. strscpy(nc->if_name, net->name); /* return net name */
  3633. /* reconfigure dlci for network */
  3634. dlci->prev_adaption = dlci->adaption;
  3635. dlci->prev_data = dlci->data;
  3636. dlci->adaption = nc->adaption;
  3637. dlci->data = gsm_mux_rx_netchar;
  3638. dlci->net = net;
  3639. pr_debug("register netdev\n");
  3640. retval = register_netdev(net);
  3641. if (retval) {
  3642. pr_err("network register fail %d\n", retval);
  3643. dlci_net_free(dlci);
  3644. return retval;
  3645. }
  3646. return net->ifindex; /* return network index */
  3647. }
  3648. /* Line discipline for real tty */
  3649. static struct tty_ldisc_ops tty_ldisc_packet = {
  3650. .owner = THIS_MODULE,
  3651. .num = N_GSM0710,
  3652. .name = "n_gsm",
  3653. .open = gsmld_open,
  3654. .close = gsmld_close,
  3655. .flush_buffer = gsmld_flush_buffer,
  3656. .read = gsmld_read,
  3657. .write = gsmld_write,
  3658. .ioctl = gsmld_ioctl,
  3659. .poll = gsmld_poll,
  3660. .receive_buf = gsmld_receive_buf,
  3661. .write_wakeup = gsmld_write_wakeup
  3662. };
  3663. /*
  3664. * Virtual tty side
  3665. */
  3666. /**
  3667. * gsm_modem_upd_via_data - send modem bits via convergence layer
  3668. * @dlci: channel
  3669. * @brk: break signal
  3670. *
  3671. * Send an empty frame to signal mobile state changes and to transmit the
  3672. * break signal for adaption 2.
  3673. */
  3674. static void gsm_modem_upd_via_data(struct gsm_dlci *dlci, u8 brk)
  3675. {
  3676. struct gsm_mux *gsm = dlci->gsm;
  3677. unsigned long flags;
  3678. if (dlci->state != DLCI_OPEN || dlci->adaption != 2)
  3679. return;
  3680. spin_lock_irqsave(&gsm->tx_lock, flags);
  3681. gsm_dlci_modem_output(gsm, dlci, brk);
  3682. spin_unlock_irqrestore(&gsm->tx_lock, flags);
  3683. }
  3684. /**
  3685. * gsm_modem_upd_via_msc - send modem bits via control frame
  3686. * @dlci: channel
  3687. * @brk: break signal
  3688. */
  3689. static int gsm_modem_upd_via_msc(struct gsm_dlci *dlci, u8 brk)
  3690. {
  3691. u8 modembits[3];
  3692. struct gsm_control *ctrl;
  3693. int len = 2;
  3694. if (dlci->gsm->encoding != GSM_BASIC_OPT)
  3695. return 0;
  3696. modembits[0] = (dlci->addr << 2) | 2 | EA; /* DLCI, Valid, EA */
  3697. if (!brk) {
  3698. modembits[1] = (gsm_encode_modem(dlci) << 1) | EA;
  3699. } else {
  3700. modembits[1] = gsm_encode_modem(dlci) << 1;
  3701. modembits[2] = (brk << 4) | 2 | EA; /* Length, Break, EA */
  3702. len++;
  3703. }
  3704. ctrl = gsm_control_send(dlci->gsm, CMD_MSC, modembits, len);
  3705. if (ctrl == NULL)
  3706. return -ENOMEM;
  3707. return gsm_control_wait(dlci->gsm, ctrl);
  3708. }
  3709. /**
  3710. * gsm_modem_send_initial_msc - Send initial modem status message
  3711. *
  3712. * @dlci channel
  3713. *
  3714. * Send an initial MSC message after DLCI open to set the initial
  3715. * modem status lines. This is only done for basic mode.
  3716. * Does not wait for a response as we cannot block the input queue
  3717. * processing.
  3718. */
  3719. static int gsm_modem_send_initial_msc(struct gsm_dlci *dlci)
  3720. {
  3721. u8 modembits[2];
  3722. if (dlci->adaption != 1 || dlci->gsm->encoding != GSM_BASIC_OPT)
  3723. return 0;
  3724. modembits[0] = (dlci->addr << 2) | 2 | EA; /* DLCI, Valid, EA */
  3725. modembits[1] = (gsm_encode_modem(dlci) << 1) | EA;
  3726. return gsm_control_command(dlci->gsm, CMD_MSC, (const u8 *)&modembits, 2);
  3727. }
  3728. /**
  3729. * gsm_modem_update - send modem status line state
  3730. * @dlci: channel
  3731. * @brk: break signal
  3732. */
  3733. static int gsm_modem_update(struct gsm_dlci *dlci, u8 brk)
  3734. {
  3735. if (dlci->gsm->dead)
  3736. return -EL2HLT;
  3737. if (dlci->adaption == 2) {
  3738. /* Send convergence layer type 2 empty data frame. */
  3739. gsm_modem_upd_via_data(dlci, brk);
  3740. return 0;
  3741. } else if (dlci->gsm->encoding == GSM_BASIC_OPT) {
  3742. /* Send as MSC control message. */
  3743. return gsm_modem_upd_via_msc(dlci, brk);
  3744. }
  3745. /* Modem status lines are not supported. */
  3746. return -EPROTONOSUPPORT;
  3747. }
  3748. /**
  3749. * gsm_wait_modem_change - wait for modem status line change
  3750. * @dlci: channel
  3751. * @mask: modem status line bits
  3752. *
  3753. * The function returns if:
  3754. * - any given modem status line bit changed
  3755. * - the wait event function got interrupted (e.g. by a signal)
  3756. * - the underlying DLCI was closed
  3757. * - the underlying ldisc device was removed
  3758. */
  3759. static int gsm_wait_modem_change(struct gsm_dlci *dlci, u32 mask)
  3760. {
  3761. struct gsm_mux *gsm = dlci->gsm;
  3762. u32 old = dlci->modem_rx;
  3763. int ret;
  3764. ret = wait_event_interruptible(gsm->event, gsm->dead ||
  3765. dlci->state != DLCI_OPEN ||
  3766. (old ^ dlci->modem_rx) & mask);
  3767. if (gsm->dead)
  3768. return -ENODEV;
  3769. if (dlci->state != DLCI_OPEN)
  3770. return -EL2NSYNC;
  3771. return ret;
  3772. }
  3773. static bool gsm_carrier_raised(struct tty_port *port)
  3774. {
  3775. struct gsm_dlci *dlci = container_of(port, struct gsm_dlci, port);
  3776. struct gsm_mux *gsm = dlci->gsm;
  3777. /* Not yet open so no carrier info */
  3778. if (dlci->state != DLCI_OPEN)
  3779. return false;
  3780. if (debug & DBG_CD_ON)
  3781. return true;
  3782. /*
  3783. * Basic mode with control channel in ADM mode may not respond
  3784. * to CMD_MSC at all and modem_rx is empty.
  3785. */
  3786. if (gsm->encoding == GSM_BASIC_OPT &&
  3787. gsm->dlci[0]->mode == DLCI_MODE_ADM && !dlci->modem_rx)
  3788. return true;
  3789. return dlci->modem_rx & TIOCM_CD;
  3790. }
  3791. static void gsm_dtr_rts(struct tty_port *port, bool active)
  3792. {
  3793. struct gsm_dlci *dlci = container_of(port, struct gsm_dlci, port);
  3794. unsigned int modem_tx = dlci->modem_tx;
  3795. if (active)
  3796. modem_tx |= TIOCM_DTR | TIOCM_RTS;
  3797. else
  3798. modem_tx &= ~(TIOCM_DTR | TIOCM_RTS);
  3799. if (modem_tx != dlci->modem_tx) {
  3800. dlci->modem_tx = modem_tx;
  3801. gsm_modem_update(dlci, 0);
  3802. }
  3803. }
  3804. static const struct tty_port_operations gsm_port_ops = {
  3805. .carrier_raised = gsm_carrier_raised,
  3806. .dtr_rts = gsm_dtr_rts,
  3807. .destruct = gsm_dlci_free,
  3808. };
  3809. static int gsmtty_install(struct tty_driver *driver, struct tty_struct *tty)
  3810. {
  3811. struct gsm_mux *gsm;
  3812. struct gsm_dlci *dlci;
  3813. unsigned int line = tty->index;
  3814. unsigned int mux = mux_line_to_num(line);
  3815. bool alloc = false;
  3816. int ret;
  3817. line = line & 0x3F;
  3818. if (mux >= MAX_MUX)
  3819. return -ENXIO;
  3820. /* FIXME: we need to lock gsm_mux for lifetimes of ttys eventually */
  3821. if (gsm_mux[mux] == NULL)
  3822. return -EUNATCH;
  3823. if (line == 0 || line > 61) /* 62/63 reserved */
  3824. return -ECHRNG;
  3825. gsm = gsm_mux[mux];
  3826. if (gsm->dead)
  3827. return -EL2HLT;
  3828. /* If DLCI 0 is not yet fully open return an error.
  3829. This is ok from a locking
  3830. perspective as we don't have to worry about this
  3831. if DLCI0 is lost */
  3832. mutex_lock(&gsm->mutex);
  3833. if (gsm->dlci[0] && gsm->dlci[0]->state != DLCI_OPEN) {
  3834. mutex_unlock(&gsm->mutex);
  3835. return -EL2NSYNC;
  3836. }
  3837. dlci = gsm->dlci[line];
  3838. if (dlci == NULL) {
  3839. alloc = true;
  3840. dlci = gsm_dlci_alloc(gsm, line);
  3841. }
  3842. if (dlci == NULL) {
  3843. mutex_unlock(&gsm->mutex);
  3844. return -ENOMEM;
  3845. }
  3846. ret = tty_port_install(&dlci->port, driver, tty);
  3847. if (ret) {
  3848. if (alloc)
  3849. dlci_put(dlci);
  3850. mutex_unlock(&gsm->mutex);
  3851. return ret;
  3852. }
  3853. dlci_get(dlci);
  3854. dlci_get(gsm->dlci[0]);
  3855. mux_get(gsm);
  3856. tty->driver_data = dlci;
  3857. mutex_unlock(&gsm->mutex);
  3858. return 0;
  3859. }
  3860. static int gsmtty_open(struct tty_struct *tty, struct file *filp)
  3861. {
  3862. struct gsm_dlci *dlci = tty->driver_data;
  3863. struct tty_port *port = &dlci->port;
  3864. port->count++;
  3865. tty_port_tty_set(port, tty);
  3866. dlci->modem_rx = 0;
  3867. /* We could in theory open and close before we wait - eg if we get
  3868. a DM straight back. This is ok as that will have caused a hangup */
  3869. tty_port_set_initialized(port, true);
  3870. /* Start sending off SABM messages */
  3871. if (!dlci->gsm->wait_config) {
  3872. /* Start sending off SABM messages */
  3873. if (dlci->gsm->initiator)
  3874. gsm_dlci_begin_open(dlci);
  3875. else
  3876. gsm_dlci_set_opening(dlci);
  3877. } else {
  3878. gsm_dlci_set_wait_config(dlci);
  3879. }
  3880. /* And wait for virtual carrier */
  3881. return tty_port_block_til_ready(port, tty, filp);
  3882. }
  3883. static void gsmtty_close(struct tty_struct *tty, struct file *filp)
  3884. {
  3885. struct gsm_dlci *dlci = tty->driver_data;
  3886. if (dlci == NULL)
  3887. return;
  3888. if (dlci->state == DLCI_CLOSED)
  3889. return;
  3890. mutex_lock(&dlci->mutex);
  3891. gsm_destroy_network(dlci);
  3892. mutex_unlock(&dlci->mutex);
  3893. if (tty_port_close_start(&dlci->port, tty, filp) == 0)
  3894. return;
  3895. gsm_dlci_begin_close(dlci);
  3896. if (tty_port_initialized(&dlci->port) && C_HUPCL(tty))
  3897. tty_port_lower_dtr_rts(&dlci->port);
  3898. tty_port_close_end(&dlci->port, tty);
  3899. tty_port_tty_set(&dlci->port, NULL);
  3900. return;
  3901. }
  3902. static void gsmtty_hangup(struct tty_struct *tty)
  3903. {
  3904. struct gsm_dlci *dlci = tty->driver_data;
  3905. if (dlci->state == DLCI_CLOSED)
  3906. return;
  3907. tty_port_hangup(&dlci->port);
  3908. gsm_dlci_begin_close(dlci);
  3909. }
  3910. static ssize_t gsmtty_write(struct tty_struct *tty, const u8 *buf, size_t len)
  3911. {
  3912. int sent;
  3913. struct gsm_dlci *dlci = tty->driver_data;
  3914. if (dlci->state == DLCI_CLOSED)
  3915. return -EINVAL;
  3916. /* Stuff the bytes into the fifo queue */
  3917. sent = kfifo_in_locked(&dlci->fifo, buf, len, &dlci->lock);
  3918. /* Need to kick the channel */
  3919. gsm_dlci_data_kick(dlci);
  3920. return sent;
  3921. }
  3922. static unsigned int gsmtty_write_room(struct tty_struct *tty)
  3923. {
  3924. struct gsm_dlci *dlci = tty->driver_data;
  3925. if (dlci->state == DLCI_CLOSED)
  3926. return 0;
  3927. return kfifo_avail(&dlci->fifo);
  3928. }
  3929. static unsigned int gsmtty_chars_in_buffer(struct tty_struct *tty)
  3930. {
  3931. struct gsm_dlci *dlci = tty->driver_data;
  3932. if (dlci->state == DLCI_CLOSED)
  3933. return 0;
  3934. return kfifo_len(&dlci->fifo);
  3935. }
  3936. static void gsmtty_flush_buffer(struct tty_struct *tty)
  3937. {
  3938. struct gsm_dlci *dlci = tty->driver_data;
  3939. unsigned long flags;
  3940. if (dlci->state == DLCI_CLOSED)
  3941. return;
  3942. /* Caution needed: If we implement reliable transport classes
  3943. then the data being transmitted can't simply be junked once
  3944. it has first hit the stack. Until then we can just blow it
  3945. away */
  3946. spin_lock_irqsave(&dlci->lock, flags);
  3947. kfifo_reset(&dlci->fifo);
  3948. spin_unlock_irqrestore(&dlci->lock, flags);
  3949. /* Need to unhook this DLCI from the transmit queue logic */
  3950. }
  3951. static void gsmtty_wait_until_sent(struct tty_struct *tty, int timeout)
  3952. {
  3953. /* The FIFO handles the queue so the kernel will do the right
  3954. thing waiting on chars_in_buffer before calling us. No work
  3955. to do here */
  3956. }
  3957. static int gsmtty_tiocmget(struct tty_struct *tty)
  3958. {
  3959. struct gsm_dlci *dlci = tty->driver_data;
  3960. if (dlci->state == DLCI_CLOSED)
  3961. return -EINVAL;
  3962. return dlci->modem_rx;
  3963. }
  3964. static int gsmtty_tiocmset(struct tty_struct *tty,
  3965. unsigned int set, unsigned int clear)
  3966. {
  3967. struct gsm_dlci *dlci = tty->driver_data;
  3968. unsigned int modem_tx = dlci->modem_tx;
  3969. if (dlci->state == DLCI_CLOSED)
  3970. return -EINVAL;
  3971. modem_tx &= ~clear;
  3972. modem_tx |= set;
  3973. if (modem_tx != dlci->modem_tx) {
  3974. dlci->modem_tx = modem_tx;
  3975. return gsm_modem_update(dlci, 0);
  3976. }
  3977. return 0;
  3978. }
  3979. static int gsmtty_ioctl(struct tty_struct *tty,
  3980. unsigned int cmd, unsigned long arg)
  3981. {
  3982. struct gsm_dlci *dlci = tty->driver_data;
  3983. struct gsm_netconfig nc;
  3984. struct gsm_dlci_config dc;
  3985. int index;
  3986. if (dlci->state == DLCI_CLOSED)
  3987. return -EINVAL;
  3988. switch (cmd) {
  3989. case GSMIOC_ENABLE_NET:
  3990. if (copy_from_user(&nc, (void __user *)arg, sizeof(nc)))
  3991. return -EFAULT;
  3992. nc.if_name[IFNAMSIZ-1] = '\0';
  3993. /* return net interface index or error code */
  3994. mutex_lock(&dlci->mutex);
  3995. index = gsm_create_network(dlci, &nc);
  3996. mutex_unlock(&dlci->mutex);
  3997. if (copy_to_user((void __user *)arg, &nc, sizeof(nc)))
  3998. return -EFAULT;
  3999. return index;
  4000. case GSMIOC_DISABLE_NET:
  4001. if (!capable(CAP_NET_ADMIN))
  4002. return -EPERM;
  4003. mutex_lock(&dlci->mutex);
  4004. gsm_destroy_network(dlci);
  4005. mutex_unlock(&dlci->mutex);
  4006. return 0;
  4007. case GSMIOC_GETCONF_DLCI:
  4008. if (copy_from_user(&dc, (void __user *)arg, sizeof(dc)))
  4009. return -EFAULT;
  4010. if (dc.channel != dlci->addr)
  4011. return -EPERM;
  4012. gsm_dlci_copy_config_values(dlci, &dc);
  4013. if (copy_to_user((void __user *)arg, &dc, sizeof(dc)))
  4014. return -EFAULT;
  4015. return 0;
  4016. case GSMIOC_SETCONF_DLCI:
  4017. if (copy_from_user(&dc, (void __user *)arg, sizeof(dc)))
  4018. return -EFAULT;
  4019. if (dc.channel >= NUM_DLCI)
  4020. return -EINVAL;
  4021. if (dc.channel != 0 && dc.channel != dlci->addr)
  4022. return -EPERM;
  4023. return gsm_dlci_config(dlci, &dc, 1);
  4024. case TIOCMIWAIT:
  4025. return gsm_wait_modem_change(dlci, (u32)arg);
  4026. default:
  4027. return -ENOIOCTLCMD;
  4028. }
  4029. }
  4030. static void gsmtty_set_termios(struct tty_struct *tty,
  4031. const struct ktermios *old)
  4032. {
  4033. struct gsm_dlci *dlci = tty->driver_data;
  4034. if (dlci->state == DLCI_CLOSED)
  4035. return;
  4036. /* For the moment its fixed. In actual fact the speed information
  4037. for the virtual channel can be propogated in both directions by
  4038. the RPN control message. This however rapidly gets nasty as we
  4039. then have to remap modem signals each way according to whether
  4040. our virtual cable is null modem etc .. */
  4041. tty_termios_copy_hw(&tty->termios, old);
  4042. }
  4043. static void gsmtty_throttle(struct tty_struct *tty)
  4044. {
  4045. struct gsm_dlci *dlci = tty->driver_data;
  4046. if (dlci->state == DLCI_CLOSED)
  4047. return;
  4048. if (C_CRTSCTS(tty))
  4049. dlci->modem_tx &= ~TIOCM_RTS;
  4050. dlci->throttled = true;
  4051. /* Send an MSC with RTS cleared */
  4052. gsm_modem_update(dlci, 0);
  4053. }
  4054. static void gsmtty_unthrottle(struct tty_struct *tty)
  4055. {
  4056. struct gsm_dlci *dlci = tty->driver_data;
  4057. if (dlci->state == DLCI_CLOSED)
  4058. return;
  4059. if (C_CRTSCTS(tty))
  4060. dlci->modem_tx |= TIOCM_RTS;
  4061. dlci->throttled = false;
  4062. /* Send an MSC with RTS set */
  4063. gsm_modem_update(dlci, 0);
  4064. }
  4065. static int gsmtty_break_ctl(struct tty_struct *tty, int state)
  4066. {
  4067. struct gsm_dlci *dlci = tty->driver_data;
  4068. int encode = 0; /* Off */
  4069. if (dlci->state == DLCI_CLOSED)
  4070. return -EINVAL;
  4071. if (state == -1) /* "On indefinitely" - we can't encode this
  4072. properly */
  4073. encode = 0x0F;
  4074. else if (state > 0) {
  4075. encode = state / 200; /* mS to encoding */
  4076. if (encode > 0x0F)
  4077. encode = 0x0F; /* Best effort */
  4078. }
  4079. return gsm_modem_update(dlci, encode);
  4080. }
  4081. static void gsmtty_cleanup(struct tty_struct *tty)
  4082. {
  4083. struct gsm_dlci *dlci = tty->driver_data;
  4084. struct gsm_mux *gsm = dlci->gsm;
  4085. dlci_put(dlci);
  4086. dlci_put(gsm->dlci[0]);
  4087. mux_put(gsm);
  4088. }
  4089. /* Virtual ttys for the demux */
  4090. static const struct tty_operations gsmtty_ops = {
  4091. .install = gsmtty_install,
  4092. .open = gsmtty_open,
  4093. .close = gsmtty_close,
  4094. .write = gsmtty_write,
  4095. .write_room = gsmtty_write_room,
  4096. .chars_in_buffer = gsmtty_chars_in_buffer,
  4097. .flush_buffer = gsmtty_flush_buffer,
  4098. .ioctl = gsmtty_ioctl,
  4099. .throttle = gsmtty_throttle,
  4100. .unthrottle = gsmtty_unthrottle,
  4101. .set_termios = gsmtty_set_termios,
  4102. .hangup = gsmtty_hangup,
  4103. .wait_until_sent = gsmtty_wait_until_sent,
  4104. .tiocmget = gsmtty_tiocmget,
  4105. .tiocmset = gsmtty_tiocmset,
  4106. .break_ctl = gsmtty_break_ctl,
  4107. .cleanup = gsmtty_cleanup,
  4108. };
  4109. static int __init gsm_init(void)
  4110. {
  4111. /* Fill in our line protocol discipline, and register it */
  4112. int status = tty_register_ldisc(&tty_ldisc_packet);
  4113. if (status != 0) {
  4114. pr_err("n_gsm: can't register line discipline (err = %d)\n",
  4115. status);
  4116. return status;
  4117. }
  4118. gsm_tty_driver = tty_alloc_driver(GSM_TTY_MINORS, TTY_DRIVER_REAL_RAW |
  4119. TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_HARDWARE_BREAK);
  4120. if (IS_ERR(gsm_tty_driver)) {
  4121. pr_err("gsm_init: tty allocation failed.\n");
  4122. status = PTR_ERR(gsm_tty_driver);
  4123. goto err_unreg_ldisc;
  4124. }
  4125. gsm_tty_driver->driver_name = "gsmtty";
  4126. gsm_tty_driver->name = "gsmtty";
  4127. gsm_tty_driver->major = 0; /* Dynamic */
  4128. gsm_tty_driver->minor_start = 0;
  4129. gsm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
  4130. gsm_tty_driver->subtype = SERIAL_TYPE_NORMAL;
  4131. gsm_tty_driver->init_termios = tty_std_termios;
  4132. /* Fixme */
  4133. gsm_tty_driver->init_termios.c_lflag &= ~ECHO;
  4134. tty_set_operations(gsm_tty_driver, &gsmtty_ops);
  4135. if (tty_register_driver(gsm_tty_driver)) {
  4136. pr_err("gsm_init: tty registration failed.\n");
  4137. status = -EBUSY;
  4138. goto err_put_driver;
  4139. }
  4140. pr_debug("gsm_init: loaded as %d,%d.\n",
  4141. gsm_tty_driver->major, gsm_tty_driver->minor_start);
  4142. return 0;
  4143. err_put_driver:
  4144. tty_driver_kref_put(gsm_tty_driver);
  4145. err_unreg_ldisc:
  4146. tty_unregister_ldisc(&tty_ldisc_packet);
  4147. return status;
  4148. }
  4149. static void __exit gsm_exit(void)
  4150. {
  4151. tty_unregister_ldisc(&tty_ldisc_packet);
  4152. tty_unregister_driver(gsm_tty_driver);
  4153. tty_driver_kref_put(gsm_tty_driver);
  4154. }
  4155. module_init(gsm_init);
  4156. module_exit(gsm_exit);
  4157. MODULE_DESCRIPTION("GSM 0710 tty multiplexor");
  4158. MODULE_LICENSE("GPL");
  4159. MODULE_ALIAS_LDISC(N_GSM0710);