n_gsm.c 68 KB

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