n_gsm.c 80 KB

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