n_gsm.c 79 KB

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