n_gsm.c 79 KB

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