n_gsm.c 80 KB

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