n_gsm.c 93 KB

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