n_gsm.c 78 KB

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