blob: cce1a8bf43db9d2f4df13738b7694458f08d6c17 [file] [log] [blame]
Greg Kroah-Hartmane3b3d0f2017-11-06 18:11:51 +01001// SPDX-License-Identifier: GPL-2.0
Alan Coxe1eaea42010-03-26 11:32:54 +00002/*
3 * n_gsm.c GSM 0710 tty multiplexor
4 * Copyright (c) 2009/10 Intel Corporation
5 *
Alan Coxe1eaea42010-03-26 11:32:54 +00006 * * THIS IS A DEVELOPMENT SNAPSHOT IT IS NOT A FINAL RELEASE *
7 *
8 * TO DO:
9 * Mostly done: ioctls for setting modes/timing
Alan Cox5f9a31d2010-11-04 15:17:27 +000010 * Partly done: hooks so you can pull off frames to non tty devs
Alan Coxe1eaea42010-03-26 11:32:54 +000011 * Restart DLCI 0 when it closes ?
Alan Coxe1eaea42010-03-26 11:32:54 +000012 * Improve the tx engine
13 * Resolve tx side locking by adding a queue_head and routing
14 * all control traffic via it
15 * General tidy/document
16 * Review the locking/move to refcounts more (mux now moved to an
17 * alloc/free model ready)
18 * Use newest tty open/close port helpers and install hooks
19 * What to do about power functions ?
20 * Termios setting and negotiation
21 * Do we need a 'which mux are you' ioctl to correlate mux and tty sets
22 *
23 */
24
25#include <linux/types.h>
26#include <linux/major.h>
27#include <linux/errno.h>
28#include <linux/signal.h>
29#include <linux/fcntl.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010030#include <linux/sched/signal.h>
Alan Coxe1eaea42010-03-26 11:32:54 +000031#include <linux/interrupt.h>
32#include <linux/tty.h>
Alan Coxe1eaea42010-03-26 11:32:54 +000033#include <linux/ctype.h>
34#include <linux/mm.h>
35#include <linux/string.h>
36#include <linux/slab.h>
37#include <linux/poll.h>
38#include <linux/bitops.h>
39#include <linux/file.h>
40#include <linux/uaccess.h>
41#include <linux/module.h>
42#include <linux/timer.h>
43#include <linux/tty_flip.h>
44#include <linux/tty_driver.h>
45#include <linux/serial.h>
46#include <linux/kfifo.h>
47#include <linux/skbuff.h>
Russ Gorbybcd5abe2011-06-16 14:20:12 -070048#include <net/arp.h>
49#include <linux/ip.h>
50#include <linux/netdevice.h>
51#include <linux/etherdevice.h>
Alan Coxe1eaea42010-03-26 11:32:54 +000052#include <linux/gsmmux.h>
53
54static int debug;
55module_param(debug, int, 0600);
56
Alan Coxa8d12002011-11-08 18:02:10 +000057/* Defaults: these are from the specification */
58
59#define T1 10 /* 100mS */
60#define T2 34 /* 333mS */
61#define N2 3 /* Retry 3 times */
Alan Coxe1eaea42010-03-26 11:32:54 +000062
63/* Use long timers for testing at low speed with debug on */
64#ifdef DEBUG_TIMING
Alan Coxa8d12002011-11-08 18:02:10 +000065#define T1 100
66#define T2 200
Alan Coxe1eaea42010-03-26 11:32:54 +000067#endif
68
Alan Cox5f9a31d2010-11-04 15:17:27 +000069/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -030070 * Semi-arbitrary buffer size limits. 0710 is normally run with 32-64 byte
Alan Cox5f9a31d2010-11-04 15:17:27 +000071 * limits so this is plenty
72 */
Russ Gorbybcd5abe2011-06-16 14:20:12 -070073#define MAX_MRU 1500
74#define MAX_MTU 1500
Daniel Starke705925e2022-04-14 02:42:13 -070075/* SOF, ADDR, CTRL, LEN1, LEN2, ..., FCS, EOF */
76#define PROT_OVERHEAD 7
Russ Gorbybcd5abe2011-06-16 14:20:12 -070077#define GSM_NET_TX_TIMEOUT (HZ*10)
78
79/**
80 * struct gsm_mux_net - network interface
Russ Gorbybcd5abe2011-06-16 14:20:12 -070081 *
82 * Created when net interface is initialized.
Jiri Slaby724ac072020-08-18 10:56:52 +020083 */
Russ Gorbybcd5abe2011-06-16 14:20:12 -070084struct gsm_mux_net {
85 struct kref ref;
86 struct gsm_dlci *dlci;
Russ Gorbybcd5abe2011-06-16 14:20:12 -070087};
88
Alan Coxe1eaea42010-03-26 11:32:54 +000089/*
90 * Each block of data we have queued to go out is in the form of
Lucas De Marchi25985ed2011-03-30 22:57:33 -030091 * a gsm_msg which holds everything we need in a link layer independent
Alan Coxe1eaea42010-03-26 11:32:54 +000092 * format
93 */
94
95struct gsm_msg {
Russ Gorbyb4338e12012-08-13 13:44:59 +010096 struct list_head list;
Alan Coxe1eaea42010-03-26 11:32:54 +000097 u8 addr; /* DLCI address + flags */
98 u8 ctrl; /* Control byte + flags */
99 unsigned int len; /* Length of data block (can be zero) */
100 unsigned char *data; /* Points into buffer but not at the start */
Gustavo A. R. Silva2f202d02020-02-12 13:35:23 -0600101 unsigned char buffer[];
Alan Coxe1eaea42010-03-26 11:32:54 +0000102};
103
Jiri Slaby72ae8cc2020-02-19 09:49:41 +0100104enum gsm_dlci_state {
105 DLCI_CLOSED,
106 DLCI_OPENING, /* Sending SABM not seen UA */
107 DLCI_OPEN, /* SABM/UA complete */
108 DLCI_CLOSING, /* Sending DISC not seen UA/DM */
109};
110
Jiri Slabye1785992020-02-19 09:49:42 +0100111enum gsm_dlci_mode {
112 DLCI_MODE_ABM, /* Normal Asynchronous Balanced Mode */
113 DLCI_MODE_ADM, /* Asynchronous Disconnected Mode */
114};
115
Alan Coxe1eaea42010-03-26 11:32:54 +0000116/*
117 * Each active data link has a gsm_dlci structure associated which ties
118 * the link layer to an optional tty (if the tty side is open). To avoid
119 * complexity right now these are only ever freed up when the mux is
120 * shut down.
121 *
122 * At the moment we don't free DLCI objects until the mux is torn down
123 * this avoid object life time issues but might be worth review later.
124 */
125
126struct gsm_dlci {
127 struct gsm_mux *gsm;
128 int addr;
Jiri Slaby72ae8cc2020-02-19 09:49:41 +0100129 enum gsm_dlci_state state;
Russ Gorbybcd5abe2011-06-16 14:20:12 -0700130 struct mutex mutex;
Alan Coxe1eaea42010-03-26 11:32:54 +0000131
132 /* Link layer */
Jiri Slabye1785992020-02-19 09:49:42 +0100133 enum gsm_dlci_mode mode;
Alan Coxe1eaea42010-03-26 11:32:54 +0000134 spinlock_t lock; /* Protects the internal state */
135 struct timer_list t1; /* Retransmit timer for SABM and UA */
136 int retries;
137 /* Uplink tty if active */
138 struct tty_port port; /* The tty bound to this DLCI if there is one */
Jiri Slaby036bca1fc2020-02-19 09:49:40 +0100139 struct kfifo fifo; /* Queue fifo for the DLCI */
Alan Coxe1eaea42010-03-26 11:32:54 +0000140 int adaption; /* Adaption layer in use */
Russ Gorbybcd5abe2011-06-16 14:20:12 -0700141 int prev_adaption;
Alan Coxe1eaea42010-03-26 11:32:54 +0000142 u32 modem_rx; /* Our incoming virtual modem lines */
143 u32 modem_tx; /* Our outgoing modem lines */
Jiri Slaby5677fcf2020-02-19 09:49:46 +0100144 bool dead; /* Refuse re-open */
Alan Coxe1eaea42010-03-26 11:32:54 +0000145 /* Flow control */
Jiri Slabye9360b92020-02-19 09:49:47 +0100146 bool throttled; /* Private copy of throttle state */
Jiri Slaby7a9ed9c2020-02-19 09:49:48 +0100147 bool constipated; /* Throttle status for outgoing */
Alan Coxe1eaea42010-03-26 11:32:54 +0000148 /* Packetised I/O */
149 struct sk_buff *skb; /* Frame being sent */
150 struct sk_buff_head skb_list; /* Queued frames */
151 /* Data handling callback */
Tony Lindgren4feb7a42019-01-13 17:25:27 -0800152 void (*data)(struct gsm_dlci *dlci, const u8 *data, int len);
153 void (*prev_data)(struct gsm_dlci *dlci, const u8 *data, int len);
Russ Gorbybcd5abe2011-06-16 14:20:12 -0700154 struct net_device *net; /* network interface, if created */
Alan Coxe1eaea42010-03-26 11:32:54 +0000155};
156
Geert Uytterhoevenc33eecc72015-05-21 14:06:11 +0200157/* DLCI 0, 62/63 are special or reserved see gsmtty_open */
Alan Coxe1eaea42010-03-26 11:32:54 +0000158
159#define NUM_DLCI 64
160
161/*
162 * DLCI 0 is used to pass control blocks out of band of the data
163 * flow (and with a higher link priority). One command can be outstanding
164 * at a time and we use this structure to manage them. They are created
165 * and destroyed by the user context, and updated by the receive paths
166 * and timers
167 */
168
169struct gsm_control {
170 u8 cmd; /* Command we are issuing */
171 u8 *data; /* Data for the command in case we retransmit */
172 int len; /* Length of block for retransmission */
173 int done; /* Done flag */
174 int error; /* Error if any */
175};
176
Jiri Slaby329aa6e2020-02-19 09:49:43 +0100177enum gsm_mux_state {
178 GSM_SEARCH,
179 GSM_START,
180 GSM_ADDRESS,
181 GSM_CONTROL,
182 GSM_LEN,
183 GSM_DATA,
184 GSM_FCS,
185 GSM_OVERRUN,
186 GSM_LEN0,
187 GSM_LEN1,
188 GSM_SSOF,
189};
190
Alan Coxe1eaea42010-03-26 11:32:54 +0000191/*
192 * Each GSM mux we have is represented by this structure. If we are
193 * operating as an ldisc then we use this structure as our ldisc
194 * state. We need to sort out lifetimes and locking with respect
195 * to the gsm mux array. For now we don't free DLCI objects that
196 * have been instantiated until the mux itself is terminated.
197 *
198 * To consider further: tty open versus mux shutdown.
199 */
200
201struct gsm_mux {
202 struct tty_struct *tty; /* The tty our ldisc is bound to */
203 spinlock_t lock;
Chao Bidfabf7f2013-11-26 12:09:39 +0800204 struct mutex mutex;
Russ Gorbyd50f6dc2011-06-14 13:35:32 -0700205 unsigned int num;
Russ Gorby6ab8fba2011-06-16 14:20:13 -0700206 struct kref ref;
Alan Coxe1eaea42010-03-26 11:32:54 +0000207
208 /* Events on the GSM channel */
209 wait_queue_head_t event;
210
211 /* Bits for GSM mode decoding */
212
213 /* Framing Layer */
214 unsigned char *buf;
Jiri Slaby329aa6e2020-02-19 09:49:43 +0100215 enum gsm_mux_state state;
Alan Coxe1eaea42010-03-26 11:32:54 +0000216 unsigned int len;
217 unsigned int address;
218 unsigned int count;
Jiri Slabyc50704b2020-02-19 09:49:49 +0100219 bool escape;
Alan Coxe1eaea42010-03-26 11:32:54 +0000220 int encoding;
221 u8 control;
222 u8 fcs;
Alan Coxc2f2f002010-11-04 15:17:03 +0000223 u8 received_fcs;
Alan Coxe1eaea42010-03-26 11:32:54 +0000224 u8 *txframe; /* TX framing buffer */
225
Jiri Slabya5797672020-08-18 10:56:48 +0200226 /* Method for the receiver side */
Alan Coxe1eaea42010-03-26 11:32:54 +0000227 void (*receive)(struct gsm_mux *gsm, u8 ch);
Alan Coxe1eaea42010-03-26 11:32:54 +0000228
229 /* Link Layer */
230 unsigned int mru;
231 unsigned int mtu;
232 int initiator; /* Did we initiate connection */
Jiri Slaby5677fcf2020-02-19 09:49:46 +0100233 bool dead; /* Has the mux been shut down */
Alan Coxe1eaea42010-03-26 11:32:54 +0000234 struct gsm_dlci *dlci[NUM_DLCI];
Jiri Slaby7a9ed9c2020-02-19 09:49:48 +0100235 bool constipated; /* Asked by remote to shut up */
Alan Coxe1eaea42010-03-26 11:32:54 +0000236
237 spinlock_t tx_lock;
238 unsigned int tx_bytes; /* TX data outstanding */
239#define TX_THRESH_HI 8192
240#define TX_THRESH_LO 2048
Russ Gorbyb4338e12012-08-13 13:44:59 +0100241 struct list_head tx_list; /* Pending data packets */
Alan Coxe1eaea42010-03-26 11:32:54 +0000242
243 /* Control messages */
244 struct timer_list t2_timer; /* Retransmit timer for commands */
245 int cretries; /* Command retry counter */
246 struct gsm_control *pending_cmd;/* Our current pending command */
247 spinlock_t control_lock; /* Protects the pending command */
248
249 /* Configuration */
250 int adaption; /* 1 or 2 supported */
251 u8 ftype; /* UI or UIH */
252 int t1, t2; /* Timers in 1/100th of a sec */
253 int n2; /* Retry count */
254
255 /* Statistics (not currently exposed) */
256 unsigned long bad_fcs;
257 unsigned long malformed;
258 unsigned long io_error;
259 unsigned long bad_size;
260 unsigned long unsupported;
261};
262
263
264/*
265 * Mux objects - needed so that we can translate a tty index into the
266 * relevant mux and DLCI.
267 */
268
269#define MAX_MUX 4 /* 256 minors */
270static struct gsm_mux *gsm_mux[MAX_MUX]; /* GSM muxes */
271static spinlock_t gsm_mux_lock;
272
Russ Gorbyd50f6dc2011-06-14 13:35:32 -0700273static struct tty_driver *gsm_tty_driver;
274
Alan Coxe1eaea42010-03-26 11:32:54 +0000275/*
276 * This section of the driver logic implements the GSM encodings
277 * both the basic and the 'advanced'. Reliable transport is not
278 * supported.
279 */
280
281#define CR 0x02
282#define EA 0x01
283#define PF 0x10
284
285/* I is special: the rest are ..*/
286#define RR 0x01
287#define UI 0x03
288#define RNR 0x05
289#define REJ 0x09
290#define DM 0x0F
291#define SABM 0x2F
292#define DISC 0x43
293#define UA 0x63
294#define UIH 0xEF
295
296/* Channel commands */
297#define CMD_NSC 0x09
298#define CMD_TEST 0x11
299#define CMD_PSC 0x21
300#define CMD_RLS 0x29
301#define CMD_FCOFF 0x31
302#define CMD_PN 0x41
303#define CMD_RPN 0x49
304#define CMD_FCON 0x51
305#define CMD_CLD 0x61
306#define CMD_SNC 0x69
307#define CMD_MSC 0x71
308
309/* Virtual modem bits */
310#define MDM_FC 0x01
311#define MDM_RTC 0x02
312#define MDM_RTR 0x04
313#define MDM_IC 0x20
314#define MDM_DV 0x40
315
316#define GSM0_SOF 0xF9
Alan Cox5f9a31d2010-11-04 15:17:27 +0000317#define GSM1_SOF 0x7E
Alan Coxe1eaea42010-03-26 11:32:54 +0000318#define GSM1_ESCAPE 0x7D
319#define GSM1_ESCAPE_BITS 0x20
320#define XON 0x11
321#define XOFF 0x13
daniel.starke@siemens.com70792832022-01-20 02:18:57 -0800322#define ISO_IEC_646_MASK 0x7F
Alan Coxe1eaea42010-03-26 11:32:54 +0000323
324static const struct tty_port_operations gsm_port_ops;
325
326/*
327 * CRC table for GSM 0710
328 */
329
330static const u8 gsm_fcs8[256] = {
331 0x00, 0x91, 0xE3, 0x72, 0x07, 0x96, 0xE4, 0x75,
332 0x0E, 0x9F, 0xED, 0x7C, 0x09, 0x98, 0xEA, 0x7B,
333 0x1C, 0x8D, 0xFF, 0x6E, 0x1B, 0x8A, 0xF8, 0x69,
334 0x12, 0x83, 0xF1, 0x60, 0x15, 0x84, 0xF6, 0x67,
335 0x38, 0xA9, 0xDB, 0x4A, 0x3F, 0xAE, 0xDC, 0x4D,
336 0x36, 0xA7, 0xD5, 0x44, 0x31, 0xA0, 0xD2, 0x43,
337 0x24, 0xB5, 0xC7, 0x56, 0x23, 0xB2, 0xC0, 0x51,
338 0x2A, 0xBB, 0xC9, 0x58, 0x2D, 0xBC, 0xCE, 0x5F,
339 0x70, 0xE1, 0x93, 0x02, 0x77, 0xE6, 0x94, 0x05,
340 0x7E, 0xEF, 0x9D, 0x0C, 0x79, 0xE8, 0x9A, 0x0B,
341 0x6C, 0xFD, 0x8F, 0x1E, 0x6B, 0xFA, 0x88, 0x19,
342 0x62, 0xF3, 0x81, 0x10, 0x65, 0xF4, 0x86, 0x17,
343 0x48, 0xD9, 0xAB, 0x3A, 0x4F, 0xDE, 0xAC, 0x3D,
344 0x46, 0xD7, 0xA5, 0x34, 0x41, 0xD0, 0xA2, 0x33,
345 0x54, 0xC5, 0xB7, 0x26, 0x53, 0xC2, 0xB0, 0x21,
346 0x5A, 0xCB, 0xB9, 0x28, 0x5D, 0xCC, 0xBE, 0x2F,
347 0xE0, 0x71, 0x03, 0x92, 0xE7, 0x76, 0x04, 0x95,
348 0xEE, 0x7F, 0x0D, 0x9C, 0xE9, 0x78, 0x0A, 0x9B,
349 0xFC, 0x6D, 0x1F, 0x8E, 0xFB, 0x6A, 0x18, 0x89,
350 0xF2, 0x63, 0x11, 0x80, 0xF5, 0x64, 0x16, 0x87,
351 0xD8, 0x49, 0x3B, 0xAA, 0xDF, 0x4E, 0x3C, 0xAD,
352 0xD6, 0x47, 0x35, 0xA4, 0xD1, 0x40, 0x32, 0xA3,
353 0xC4, 0x55, 0x27, 0xB6, 0xC3, 0x52, 0x20, 0xB1,
354 0xCA, 0x5B, 0x29, 0xB8, 0xCD, 0x5C, 0x2E, 0xBF,
355 0x90, 0x01, 0x73, 0xE2, 0x97, 0x06, 0x74, 0xE5,
356 0x9E, 0x0F, 0x7D, 0xEC, 0x99, 0x08, 0x7A, 0xEB,
357 0x8C, 0x1D, 0x6F, 0xFE, 0x8B, 0x1A, 0x68, 0xF9,
358 0x82, 0x13, 0x61, 0xF0, 0x85, 0x14, 0x66, 0xF7,
359 0xA8, 0x39, 0x4B, 0xDA, 0xAF, 0x3E, 0x4C, 0xDD,
360 0xA6, 0x37, 0x45, 0xD4, 0xA1, 0x30, 0x42, 0xD3,
361 0xB4, 0x25, 0x57, 0xC6, 0xB3, 0x22, 0x50, 0xC1,
362 0xBA, 0x2B, 0x59, 0xC8, 0xBD, 0x2C, 0x5E, 0xCF
363};
364
365#define INIT_FCS 0xFF
366#define GOOD_FCS 0xCF
367
Jiri Slabya5797672020-08-18 10:56:48 +0200368static int gsmld_output(struct gsm_mux *gsm, u8 *data, int len);
369
Alan Coxe1eaea42010-03-26 11:32:54 +0000370/**
371 * gsm_fcs_add - update FCS
372 * @fcs: Current FCS
373 * @c: Next data
374 *
375 * Update the FCS to include c. Uses the algorithm in the specification
376 * notes.
377 */
378
379static inline u8 gsm_fcs_add(u8 fcs, u8 c)
380{
381 return gsm_fcs8[fcs ^ c];
382}
383
384/**
385 * gsm_fcs_add_block - update FCS for a block
386 * @fcs: Current FCS
387 * @c: buffer of data
388 * @len: length of buffer
389 *
390 * Update the FCS to include c. Uses the algorithm in the specification
391 * notes.
392 */
393
394static inline u8 gsm_fcs_add_block(u8 fcs, u8 *c, int len)
395{
396 while (len--)
397 fcs = gsm_fcs8[fcs ^ *c++];
398 return fcs;
399}
400
401/**
402 * gsm_read_ea - read a byte into an EA
403 * @val: variable holding value
Jiri Slaby724ac072020-08-18 10:56:52 +0200404 * @c: byte going into the EA
Alan Coxe1eaea42010-03-26 11:32:54 +0000405 *
406 * Processes one byte of an EA. Updates the passed variable
407 * and returns 1 if the EA is now completely read
408 */
409
410static int gsm_read_ea(unsigned int *val, u8 c)
411{
412 /* Add the next 7 bits into the value */
413 *val <<= 7;
414 *val |= c >> 1;
415 /* Was this the last byte of the EA 1 = yes*/
416 return c & EA;
417}
418
419/**
420 * gsm_encode_modem - encode modem data bits
421 * @dlci: DLCI to encode from
422 *
423 * Returns the correct GSM encoded modem status bits (6 bit field) for
424 * the current status of the DLCI and attached tty object
425 */
426
427static u8 gsm_encode_modem(const struct gsm_dlci *dlci)
428{
429 u8 modembits = 0;
430 /* FC is true flow control not modem bits */
431 if (dlci->throttled)
432 modembits |= MDM_FC;
433 if (dlci->modem_tx & TIOCM_DTR)
434 modembits |= MDM_RTC;
435 if (dlci->modem_tx & TIOCM_RTS)
436 modembits |= MDM_RTR;
437 if (dlci->modem_tx & TIOCM_RI)
438 modembits |= MDM_IC;
daniel.starke@siemens.com90b47e62022-02-17 23:31:17 -0800439 if (dlci->modem_tx & TIOCM_CD || dlci->gsm->initiator)
Alan Coxe1eaea42010-03-26 11:32:54 +0000440 modembits |= MDM_DV;
441 return modembits;
442}
443
444/**
445 * gsm_print_packet - display a frame for debug
446 * @hdr: header to print before decode
447 * @addr: address EA from the frame
448 * @cr: C/R bit from the frame
449 * @control: control including PF bit
450 * @data: following data bytes
451 * @dlen: length of data
452 *
453 * Displays a packet in human readable format for debugging purposes. The
454 * style is based on amateur radio LAP-B dump display.
455 */
456
457static void gsm_print_packet(const char *hdr, int addr, int cr,
458 u8 control, const u8 *data, int dlen)
459{
460 if (!(debug & 1))
461 return;
462
Alan Cox5f9a31d2010-11-04 15:17:27 +0000463 pr_info("%s %d) %c: ", hdr, addr, "RC"[cr]);
Alan Coxe1eaea42010-03-26 11:32:54 +0000464
465 switch (control & ~PF) {
466 case SABM:
Alan Cox5f9a31d2010-11-04 15:17:27 +0000467 pr_cont("SABM");
Alan Coxe1eaea42010-03-26 11:32:54 +0000468 break;
469 case UA:
Alan Cox5f9a31d2010-11-04 15:17:27 +0000470 pr_cont("UA");
Alan Coxe1eaea42010-03-26 11:32:54 +0000471 break;
472 case DISC:
Alan Cox5f9a31d2010-11-04 15:17:27 +0000473 pr_cont("DISC");
Alan Coxe1eaea42010-03-26 11:32:54 +0000474 break;
475 case DM:
Alan Cox5f9a31d2010-11-04 15:17:27 +0000476 pr_cont("DM");
Alan Coxe1eaea42010-03-26 11:32:54 +0000477 break;
478 case UI:
Alan Cox5f9a31d2010-11-04 15:17:27 +0000479 pr_cont("UI");
Alan Coxe1eaea42010-03-26 11:32:54 +0000480 break;
481 case UIH:
Alan Cox5f9a31d2010-11-04 15:17:27 +0000482 pr_cont("UIH");
Alan Coxe1eaea42010-03-26 11:32:54 +0000483 break;
484 default:
485 if (!(control & 0x01)) {
Alan Cox5f9a31d2010-11-04 15:17:27 +0000486 pr_cont("I N(S)%d N(R)%d",
Alan Cox47fdd642012-09-17 12:02:35 +0100487 (control & 0x0E) >> 1, (control & 0xE0) >> 5);
Alan Coxe1eaea42010-03-26 11:32:54 +0000488 } else switch (control & 0x0F) {
Alan Cox5f9a31d2010-11-04 15:17:27 +0000489 case RR:
490 pr_cont("RR(%d)", (control & 0xE0) >> 5);
491 break;
492 case RNR:
493 pr_cont("RNR(%d)", (control & 0xE0) >> 5);
494 break;
495 case REJ:
496 pr_cont("REJ(%d)", (control & 0xE0) >> 5);
497 break;
498 default:
499 pr_cont("[%02X]", control);
Alan Coxe1eaea42010-03-26 11:32:54 +0000500 }
501 }
502
503 if (control & PF)
Alan Cox5f9a31d2010-11-04 15:17:27 +0000504 pr_cont("(P)");
Alan Coxe1eaea42010-03-26 11:32:54 +0000505 else
Alan Cox5f9a31d2010-11-04 15:17:27 +0000506 pr_cont("(F)");
Alan Coxe1eaea42010-03-26 11:32:54 +0000507
Gregory CLEMENT57626ff2020-05-18 10:45:12 +0200508 print_hex_dump_bytes("", DUMP_PREFIX_NONE, data, dlen);
Alan Coxe1eaea42010-03-26 11:32:54 +0000509}
510
511
512/*
513 * Link level transmission side
514 */
515
516/**
517 * gsm_stuff_packet - bytestuff a packet
Jiri Slaby724ac072020-08-18 10:56:52 +0200518 * @input: input buffer
519 * @output: output buffer
Alan Coxe1eaea42010-03-26 11:32:54 +0000520 * @len: length of input
521 *
522 * Expand a buffer by bytestuffing it. The worst case size change
523 * is doubling and the caller is responsible for handing out
524 * suitable sized buffers.
525 */
526
527static int gsm_stuff_frame(const u8 *input, u8 *output, int len)
528{
529 int olen = 0;
530 while (len--) {
531 if (*input == GSM1_SOF || *input == GSM1_ESCAPE
daniel.starke@siemens.com70792832022-01-20 02:18:57 -0800532 || (*input & ISO_IEC_646_MASK) == XON
533 || (*input & ISO_IEC_646_MASK) == XOFF) {
Alan Coxe1eaea42010-03-26 11:32:54 +0000534 *output++ = GSM1_ESCAPE;
535 *output++ = *input++ ^ GSM1_ESCAPE_BITS;
536 olen++;
537 } else
538 *output++ = *input++;
539 olen++;
540 }
541 return olen;
542}
543
Alan Coxe1eaea42010-03-26 11:32:54 +0000544/**
545 * gsm_send - send a control frame
546 * @gsm: our GSM mux
547 * @addr: address for control frame
548 * @cr: command/response bit
549 * @control: control byte including PF bit
550 *
551 * Format up and transmit a control frame. These do not go via the
552 * queueing logic as they should be transmitted ahead of data when
553 * they are needed.
554 *
555 * FIXME: Lock versus data TX path
556 */
557
558static void gsm_send(struct gsm_mux *gsm, int addr, int cr, int control)
559{
560 int len;
561 u8 cbuf[10];
562 u8 ibuf[3];
563
564 switch (gsm->encoding) {
565 case 0:
566 cbuf[0] = GSM0_SOF;
567 cbuf[1] = (addr << 2) | (cr << 1) | EA;
568 cbuf[2] = control;
569 cbuf[3] = EA; /* Length of data = 0 */
570 cbuf[4] = 0xFF - gsm_fcs_add_block(INIT_FCS, cbuf + 1, 3);
571 cbuf[5] = GSM0_SOF;
572 len = 6;
573 break;
574 case 1:
575 case 2:
576 /* Control frame + packing (but not frame stuffing) in mode 1 */
577 ibuf[0] = (addr << 2) | (cr << 1) | EA;
578 ibuf[1] = control;
579 ibuf[2] = 0xFF - gsm_fcs_add_block(INIT_FCS, ibuf, 2);
580 /* Stuffing may double the size worst case */
581 len = gsm_stuff_frame(ibuf, cbuf + 1, 3);
582 /* Now add the SOF markers */
583 cbuf[0] = GSM1_SOF;
584 cbuf[len + 1] = GSM1_SOF;
585 /* FIXME: we can omit the lead one in many cases */
586 len += 2;
587 break;
588 default:
589 WARN_ON(1);
590 return;
591 }
Jiri Slabya5797672020-08-18 10:56:48 +0200592 gsmld_output(gsm, cbuf, len);
Alan Coxe1eaea42010-03-26 11:32:54 +0000593 gsm_print_packet("-->", addr, cr, control, NULL, 0);
594}
595
596/**
597 * gsm_response - send a control response
598 * @gsm: our GSM mux
599 * @addr: address for control frame
600 * @control: control byte including PF bit
601 *
602 * Format up and transmit a link level response frame.
603 */
604
605static inline void gsm_response(struct gsm_mux *gsm, int addr, int control)
606{
607 gsm_send(gsm, addr, 0, control);
608}
609
610/**
611 * gsm_command - send a control command
612 * @gsm: our GSM mux
613 * @addr: address for control frame
614 * @control: control byte including PF bit
615 *
616 * Format up and transmit a link level command frame.
617 */
618
619static inline void gsm_command(struct gsm_mux *gsm, int addr, int control)
620{
621 gsm_send(gsm, addr, 1, control);
622}
623
624/* Data transmission */
625
626#define HDR_LEN 6 /* ADDR CTRL [LEN.2] DATA FCS */
627
628/**
629 * gsm_data_alloc - allocate data frame
630 * @gsm: GSM mux
631 * @addr: DLCI address
632 * @len: length excluding header and FCS
633 * @ctrl: control byte
634 *
635 * Allocate a new data buffer for sending frames with data. Space is left
636 * at the front for header bytes but that is treated as an implementation
637 * detail and not for the high level code to use
638 */
639
640static struct gsm_msg *gsm_data_alloc(struct gsm_mux *gsm, u8 addr, int len,
641 u8 ctrl)
642{
643 struct gsm_msg *m = kmalloc(sizeof(struct gsm_msg) + len + HDR_LEN,
644 GFP_ATOMIC);
645 if (m == NULL)
646 return NULL;
647 m->data = m->buffer + HDR_LEN - 1; /* Allow for FCS */
648 m->len = len;
649 m->addr = addr;
650 m->ctrl = ctrl;
Russ Gorbyb4338e12012-08-13 13:44:59 +0100651 INIT_LIST_HEAD(&m->list);
Alan Coxe1eaea42010-03-26 11:32:54 +0000652 return m;
653}
654
655/**
656 * gsm_data_kick - poke the queue
657 * @gsm: GSM Mux
658 *
659 * The tty device has called us to indicate that room has appeared in
660 * the transmit queue. Ram more data into the pipe if we have any
Frederic Beratc01af4f2012-08-13 13:43:58 +0100661 * If we have been flow-stopped by a CMD_FCOFF, then we can only
662 * send messages on DLCI0 until CMD_FCON
Alan Coxe1eaea42010-03-26 11:32:54 +0000663 *
664 * FIXME: lock against link layer control transmissions
665 */
666
Gregory CLEMENT01dbb362020-05-12 13:53:23 +0200667static void gsm_data_kick(struct gsm_mux *gsm, struct gsm_dlci *dlci)
Alan Coxe1eaea42010-03-26 11:32:54 +0000668{
Russ Gorbyb4338e12012-08-13 13:44:59 +0100669 struct gsm_msg *msg, *nmsg;
Alan Coxe1eaea42010-03-26 11:32:54 +0000670 int len;
Alan Coxe1eaea42010-03-26 11:32:54 +0000671
Russ Gorbyb4338e12012-08-13 13:44:59 +0100672 list_for_each_entry_safe(msg, nmsg, &gsm->tx_list, list) {
673 if (gsm->constipated && msg->addr)
Frederic Beratc01af4f2012-08-13 13:43:58 +0100674 continue;
Alan Coxe1eaea42010-03-26 11:32:54 +0000675 if (gsm->encoding != 0) {
676 gsm->txframe[0] = GSM1_SOF;
677 len = gsm_stuff_frame(msg->data,
678 gsm->txframe + 1, msg->len);
679 gsm->txframe[len + 1] = GSM1_SOF;
680 len += 2;
681 } else {
682 gsm->txframe[0] = GSM0_SOF;
683 memcpy(gsm->txframe + 1 , msg->data, msg->len);
684 gsm->txframe[msg->len + 1] = GSM0_SOF;
685 len = msg->len + 2;
686 }
687
Joe Perches0a77c4f2011-04-25 16:46:49 -0700688 if (debug & 4)
689 print_hex_dump_bytes("gsm_data_kick: ",
690 DUMP_PREFIX_OFFSET,
691 gsm->txframe, len);
Jiri Slabya5797672020-08-18 10:56:48 +0200692 if (gsmld_output(gsm, gsm->txframe, len) < 0)
Alan Coxe1eaea42010-03-26 11:32:54 +0000693 break;
694 /* FIXME: Can eliminate one SOF in many more cases */
Alan Coxe1eaea42010-03-26 11:32:54 +0000695 gsm->tx_bytes -= msg->len;
Frederic Beratc01af4f2012-08-13 13:43:58 +0100696
Russ Gorbyb4338e12012-08-13 13:44:59 +0100697 list_del(&msg->list);
698 kfree(msg);
Gregory CLEMENT01dbb362020-05-12 13:53:23 +0200699
700 if (dlci) {
701 tty_port_tty_wakeup(&dlci->port);
702 } else {
703 int i = 0;
704
Gregory CLEMENT4dd31f12020-05-18 10:45:13 +0200705 for (i = 0; i < NUM_DLCI; i++)
706 if (gsm->dlci[i])
707 tty_port_tty_wakeup(&gsm->dlci[i]->port);
Gregory CLEMENT01dbb362020-05-12 13:53:23 +0200708 }
Alan Coxe1eaea42010-03-26 11:32:54 +0000709 }
710}
711
712/**
713 * __gsm_data_queue - queue a UI or UIH frame
714 * @dlci: DLCI sending the data
715 * @msg: message queued
716 *
717 * Add data to the transmit queue and try and get stuff moving
718 * out of the mux tty if not already doing so. The Caller must hold
719 * the gsm tx lock.
720 */
721
722static void __gsm_data_queue(struct gsm_dlci *dlci, struct gsm_msg *msg)
723{
724 struct gsm_mux *gsm = dlci->gsm;
725 u8 *dp = msg->data;
726 u8 *fcs = dp + msg->len;
727
728 /* Fill in the header */
729 if (gsm->encoding == 0) {
730 if (msg->len < 128)
731 *--dp = (msg->len << 1) | EA;
732 else {
Ken Millsbe7a7412010-12-13 15:27:27 +0000733 *--dp = (msg->len >> 7); /* bits 7 - 15 */
734 *--dp = (msg->len & 127) << 1; /* bits 0 - 6 */
Alan Coxe1eaea42010-03-26 11:32:54 +0000735 }
736 }
737
738 *--dp = msg->ctrl;
739 if (gsm->initiator)
740 *--dp = (msg->addr << 2) | 2 | EA;
741 else
742 *--dp = (msg->addr << 2) | EA;
743 *fcs = gsm_fcs_add_block(INIT_FCS, dp , msg->data - dp);
744 /* Ugly protocol layering violation */
745 if (msg->ctrl == UI || msg->ctrl == (UI|PF))
746 *fcs = gsm_fcs_add_block(*fcs, msg->data, msg->len);
747 *fcs = 0xFF - *fcs;
748
749 gsm_print_packet("Q> ", msg->addr, gsm->initiator, msg->ctrl,
750 msg->data, msg->len);
751
752 /* Move the header back and adjust the length, also allow for the FCS
753 now tacked on the end */
754 msg->len += (msg->data - dp) + 1;
755 msg->data = dp;
756
757 /* Add to the actual output queue */
Russ Gorbyb4338e12012-08-13 13:44:59 +0100758 list_add_tail(&msg->list, &gsm->tx_list);
Alan Coxe1eaea42010-03-26 11:32:54 +0000759 gsm->tx_bytes += msg->len;
Gregory CLEMENT01dbb362020-05-12 13:53:23 +0200760 gsm_data_kick(gsm, dlci);
Alan Coxe1eaea42010-03-26 11:32:54 +0000761}
762
763/**
764 * gsm_data_queue - queue a UI or UIH frame
765 * @dlci: DLCI sending the data
766 * @msg: message queued
767 *
768 * Add data to the transmit queue and try and get stuff moving
769 * out of the mux tty if not already doing so. Take the
770 * the gsm tx lock and dlci lock.
771 */
772
773static void gsm_data_queue(struct gsm_dlci *dlci, struct gsm_msg *msg)
774{
775 unsigned long flags;
776 spin_lock_irqsave(&dlci->gsm->tx_lock, flags);
777 __gsm_data_queue(dlci, msg);
778 spin_unlock_irqrestore(&dlci->gsm->tx_lock, flags);
779}
780
781/**
782 * gsm_dlci_data_output - try and push data out of a DLCI
783 * @gsm: mux
784 * @dlci: the DLCI to pull data from
785 *
786 * Pull data from a DLCI and send it into the transmit queue if there
787 * is data. Keep to the MRU of the mux. This path handles the usual tty
788 * interface which is a byte stream with optional modem data.
789 *
790 * Caller must hold the tx_lock of the mux.
791 */
792
793static int gsm_dlci_data_output(struct gsm_mux *gsm, struct gsm_dlci *dlci)
794{
795 struct gsm_msg *msg;
796 u8 *dp;
Mikhail Kshevetskiy268e5262011-09-23 19:22:56 +0400797 int len, total_size, size;
Alan Coxe1eaea42010-03-26 11:32:54 +0000798 int h = dlci->adaption - 1;
799
Mikhail Kshevetskiy268e5262011-09-23 19:22:56 +0400800 total_size = 0;
Aldo Iljazif3c909b2013-07-08 22:28:00 +0300801 while (1) {
Jiri Slaby036bca1fc2020-02-19 09:49:40 +0100802 len = kfifo_len(&dlci->fifo);
Mikhail Kshevetskiy268e5262011-09-23 19:22:56 +0400803 if (len == 0)
804 return total_size;
Alan Coxe1eaea42010-03-26 11:32:54 +0000805
Mikhail Kshevetskiy268e5262011-09-23 19:22:56 +0400806 /* MTU/MRU count only the data bits */
807 if (len > gsm->mtu)
808 len = gsm->mtu;
Alan Coxe1eaea42010-03-26 11:32:54 +0000809
Mikhail Kshevetskiy268e5262011-09-23 19:22:56 +0400810 size = len + h;
Alan Coxe1eaea42010-03-26 11:32:54 +0000811
Mikhail Kshevetskiy268e5262011-09-23 19:22:56 +0400812 msg = gsm_data_alloc(gsm, dlci->addr, size, gsm->ftype);
813 /* FIXME: need a timer or something to kick this so it can't
814 get stuck with no work outstanding and no buffer free */
815 if (msg == NULL)
816 return -ENOMEM;
817 dp = msg->data;
818 switch (dlci->adaption) {
819 case 1: /* Unstructured */
820 break;
Aldo Iljazif3c909b2013-07-08 22:28:00 +0300821 case 2: /* Unstructed with modem bits.
822 Always one byte as we never send inline break data */
Daniel Starked1961382022-04-14 02:42:10 -0700823 *dp++ = (gsm_encode_modem(dlci) << 1) | EA;
Mikhail Kshevetskiy268e5262011-09-23 19:22:56 +0400824 break;
825 }
Jiri Slaby036bca1fc2020-02-19 09:49:40 +0100826 WARN_ON(kfifo_out_locked(&dlci->fifo, dp , len, &dlci->lock) != len);
Mikhail Kshevetskiy268e5262011-09-23 19:22:56 +0400827 __gsm_data_queue(dlci, msg);
828 total_size += size;
Alan Coxe1eaea42010-03-26 11:32:54 +0000829 }
Alan Coxe1eaea42010-03-26 11:32:54 +0000830 /* Bytes of data we used up */
Mikhail Kshevetskiy268e5262011-09-23 19:22:56 +0400831 return total_size;
Alan Coxe1eaea42010-03-26 11:32:54 +0000832}
833
834/**
835 * gsm_dlci_data_output_framed - try and push data out of a DLCI
836 * @gsm: mux
837 * @dlci: the DLCI to pull data from
838 *
839 * Pull data from a DLCI and send it into the transmit queue if there
840 * is data. Keep to the MRU of the mux. This path handles framed data
841 * queued as skbuffs to the DLCI.
842 *
843 * Caller must hold the tx_lock of the mux.
844 */
845
846static int gsm_dlci_data_output_framed(struct gsm_mux *gsm,
847 struct gsm_dlci *dlci)
848{
849 struct gsm_msg *msg;
850 u8 *dp;
851 int len, size;
852 int last = 0, first = 0;
853 int overhead = 0;
854
855 /* One byte per frame is used for B/F flags */
856 if (dlci->adaption == 4)
857 overhead = 1;
858
859 /* dlci->skb is locked by tx_lock */
860 if (dlci->skb == NULL) {
Russ Gorby88ed2a62012-08-13 13:45:30 +0100861 dlci->skb = skb_dequeue_tail(&dlci->skb_list);
Alan Coxe1eaea42010-03-26 11:32:54 +0000862 if (dlci->skb == NULL)
863 return 0;
864 first = 1;
865 }
866 len = dlci->skb->len + overhead;
867
868 /* MTU/MRU count only the data bits */
869 if (len > gsm->mtu) {
870 if (dlci->adaption == 3) {
871 /* Over long frame, bin it */
Russ Gorby329e5672012-08-13 13:45:15 +0100872 dev_kfree_skb_any(dlci->skb);
Alan Coxe1eaea42010-03-26 11:32:54 +0000873 dlci->skb = NULL;
874 return 0;
875 }
876 len = gsm->mtu;
877 } else
878 last = 1;
879
880 size = len + overhead;
881 msg = gsm_data_alloc(gsm, dlci->addr, size, gsm->ftype);
882
883 /* FIXME: need a timer or something to kick this so it can't
884 get stuck with no work outstanding and no buffer free */
Russ Gorby88ed2a62012-08-13 13:45:30 +0100885 if (msg == NULL) {
886 skb_queue_tail(&dlci->skb_list, dlci->skb);
887 dlci->skb = NULL;
Alan Coxe1eaea42010-03-26 11:32:54 +0000888 return -ENOMEM;
Russ Gorby88ed2a62012-08-13 13:45:30 +0100889 }
Alan Coxe1eaea42010-03-26 11:32:54 +0000890 dp = msg->data;
891
892 if (dlci->adaption == 4) { /* Interruptible framed (Packetised Data) */
893 /* Flag byte to carry the start/end info */
894 *dp++ = last << 7 | first << 6 | 1; /* EA */
895 len--;
896 }
Russ Gorby57f21042011-06-14 13:23:29 -0700897 memcpy(dp, dlci->skb->data, len);
898 skb_pull(dlci->skb, len);
Alan Coxe1eaea42010-03-26 11:32:54 +0000899 __gsm_data_queue(dlci, msg);
Russ Gorbybcd5abe2011-06-16 14:20:12 -0700900 if (last) {
Russ Gorby329e5672012-08-13 13:45:15 +0100901 dev_kfree_skb_any(dlci->skb);
Alan Coxe1eaea42010-03-26 11:32:54 +0000902 dlci->skb = NULL;
Russ Gorbybcd5abe2011-06-16 14:20:12 -0700903 }
Alan Coxe1eaea42010-03-26 11:32:54 +0000904 return size;
905}
906
907/**
908 * gsm_dlci_data_sweep - look for data to send
909 * @gsm: the GSM mux
910 *
911 * Sweep the GSM mux channels in priority order looking for ones with
912 * data to send. We could do with optimising this scan a bit. We aim
913 * to fill the queue totally or up to TX_THRESH_HI bytes. Once we hit
914 * TX_THRESH_LO we get called again
915 *
916 * FIXME: We should round robin between groups and in theory you can
917 * renegotiate DLCI priorities with optional stuff. Needs optimising.
918 */
919
920static void gsm_dlci_data_sweep(struct gsm_mux *gsm)
921{
922 int len;
923 /* Priority ordering: We should do priority with RR of the groups */
924 int i = 1;
Alan Coxe1eaea42010-03-26 11:32:54 +0000925
Alan Coxe1eaea42010-03-26 11:32:54 +0000926 while (i < NUM_DLCI) {
927 struct gsm_dlci *dlci;
928
929 if (gsm->tx_bytes > TX_THRESH_HI)
930 break;
931 dlci = gsm->dlci[i];
932 if (dlci == NULL || dlci->constipated) {
933 i++;
934 continue;
935 }
Russ Gorbybcd5abe2011-06-16 14:20:12 -0700936 if (dlci->adaption < 3 && !dlci->net)
Alan Coxe1eaea42010-03-26 11:32:54 +0000937 len = gsm_dlci_data_output(gsm, dlci);
938 else
939 len = gsm_dlci_data_output_framed(gsm, dlci);
940 if (len < 0)
Julia Lawalle73790a2010-08-10 18:03:12 -0700941 break;
Alan Coxe1eaea42010-03-26 11:32:54 +0000942 /* DLCI empty - try the next */
943 if (len == 0)
944 i++;
945 }
Alan Coxe1eaea42010-03-26 11:32:54 +0000946}
947
948/**
949 * gsm_dlci_data_kick - transmit if possible
950 * @dlci: DLCI to kick
951 *
952 * Transmit data from this DLCI if the queue is empty. We can't rely on
953 * a tty wakeup except when we filled the pipe so we need to fire off
954 * new data ourselves in other cases.
955 */
956
957static void gsm_dlci_data_kick(struct gsm_dlci *dlci)
958{
959 unsigned long flags;
Russ Gorby192b6042012-08-13 13:43:36 +0100960 int sweep;
Alan Coxe1eaea42010-03-26 11:32:54 +0000961
Aldo Iljazif3c909b2013-07-08 22:28:00 +0300962 if (dlci->constipated)
Frederic Beratc01af4f2012-08-13 13:43:58 +0100963 return;
Frederic Beratc01af4f2012-08-13 13:43:58 +0100964
Alan Coxe1eaea42010-03-26 11:32:54 +0000965 spin_lock_irqsave(&dlci->gsm->tx_lock, flags);
966 /* If we have nothing running then we need to fire up */
Russ Gorby192b6042012-08-13 13:43:36 +0100967 sweep = (dlci->gsm->tx_bytes < TX_THRESH_LO);
Russ Gorbybcd5abe2011-06-16 14:20:12 -0700968 if (dlci->gsm->tx_bytes == 0) {
969 if (dlci->net)
970 gsm_dlci_data_output_framed(dlci->gsm, dlci);
971 else
972 gsm_dlci_data_output(dlci->gsm, dlci);
Russ Gorby192b6042012-08-13 13:43:36 +0100973 }
974 if (sweep)
Aldo Iljazif3c909b2013-07-08 22:28:00 +0300975 gsm_dlci_data_sweep(dlci->gsm);
Alan Coxe1eaea42010-03-26 11:32:54 +0000976 spin_unlock_irqrestore(&dlci->gsm->tx_lock, flags);
977}
978
979/*
980 * Control message processing
981 */
982
983
984/**
985 * gsm_control_reply - send a response frame to a control
986 * @gsm: gsm channel
987 * @cmd: the command to use
988 * @data: data to follow encoded info
989 * @dlen: length of data
990 *
991 * Encode up and queue a UI/UIH frame containing our response.
992 */
993
Tony Lindgren4feb7a42019-01-13 17:25:27 -0800994static void gsm_control_reply(struct gsm_mux *gsm, int cmd, const u8 *data,
Alan Coxe1eaea42010-03-26 11:32:54 +0000995 int dlen)
996{
997 struct gsm_msg *msg;
998 msg = gsm_data_alloc(gsm, 0, dlen + 2, gsm->ftype);
Ken Mills093d8042010-12-13 15:28:03 +0000999 if (msg == NULL)
1000 return;
Alan Coxe1eaea42010-03-26 11:32:54 +00001001 msg->data[0] = (cmd & 0xFE) << 1 | EA; /* Clear C/R */
1002 msg->data[1] = (dlen << 1) | EA;
1003 memcpy(msg->data + 2, data, dlen);
1004 gsm_data_queue(gsm->dlci[0], msg);
1005}
1006
1007/**
1008 * gsm_process_modem - process received modem status
1009 * @tty: virtual tty bound to the DLCI
1010 * @dlci: DLCI to affect
1011 * @modem: modem bits (full EA)
1012 *
1013 * Used when a modem control message or line state inline in adaption
1014 * layer 2 is processed. Sort out the local modem state and throttles
1015 */
1016
1017static void gsm_process_modem(struct tty_struct *tty, struct gsm_dlci *dlci,
Russ Gorby72632872011-06-14 13:23:28 -07001018 u32 modem, int clen)
Alan Coxe1eaea42010-03-26 11:32:54 +00001019{
1020 int mlines = 0;
Russ Gorby72632872011-06-14 13:23:28 -07001021 u8 brk = 0;
Frederic Beratc01af4f2012-08-13 13:43:58 +01001022 int fc;
Russ Gorby72632872011-06-14 13:23:28 -07001023
1024 /* The modem status command can either contain one octet (v.24 signals)
1025 or two octets (v.24 signals + break signals). The length field will
1026 either be 2 or 3 respectively. This is specified in section
1027 5.4.6.3.7 of the 27.010 mux spec. */
1028
1029 if (clen == 2)
1030 modem = modem & 0x7f;
1031 else {
1032 brk = modem & 0x7f;
1033 modem = (modem >> 7) & 0x7f;
Frederic Beratc01af4f2012-08-13 13:43:58 +01001034 }
Alan Coxe1eaea42010-03-26 11:32:54 +00001035
1036 /* Flow control/ready to communicate */
Frederic Beratc01af4f2012-08-13 13:43:58 +01001037 fc = (modem & MDM_FC) || !(modem & MDM_RTR);
1038 if (fc && !dlci->constipated) {
Alan Coxe1eaea42010-03-26 11:32:54 +00001039 /* Need to throttle our output on this device */
Jiri Slaby7a9ed9c2020-02-19 09:49:48 +01001040 dlci->constipated = true;
Frederic Beratc01af4f2012-08-13 13:43:58 +01001041 } else if (!fc && dlci->constipated) {
Jiri Slaby7a9ed9c2020-02-19 09:49:48 +01001042 dlci->constipated = false;
Alan Coxe1eaea42010-03-26 11:32:54 +00001043 gsm_dlci_data_kick(dlci);
1044 }
Frederic Beratc01af4f2012-08-13 13:43:58 +01001045
Alan Coxe1eaea42010-03-26 11:32:54 +00001046 /* Map modem bits */
Frederic Beratc01af4f2012-08-13 13:43:58 +01001047 if (modem & MDM_RTC)
1048 mlines |= TIOCM_DSR | TIOCM_DTR;
Alan Coxe1eaea42010-03-26 11:32:54 +00001049 if (modem & MDM_RTR)
1050 mlines |= TIOCM_RTS | TIOCM_CTS;
1051 if (modem & MDM_IC)
1052 mlines |= TIOCM_RI;
1053 if (modem & MDM_DV)
1054 mlines |= TIOCM_CD;
1055
1056 /* Carrier drop -> hangup */
1057 if (tty) {
1058 if ((mlines & TIOCM_CD) == 0 && (dlci->modem_rx & TIOCM_CD))
Peter Hurley9db276f2016-01-10 20:36:15 -08001059 if (!C_CLOCAL(tty))
Alan Coxe1eaea42010-03-26 11:32:54 +00001060 tty_hangup(tty);
Alan Coxe1eaea42010-03-26 11:32:54 +00001061 }
Jiri Slaby92a19f92013-01-03 15:53:03 +01001062 if (brk & 0x01)
1063 tty_insert_flip_char(&dlci->port, 0, TTY_BREAK);
Alan Coxe1eaea42010-03-26 11:32:54 +00001064 dlci->modem_rx = mlines;
1065}
1066
1067/**
1068 * gsm_control_modem - modem status received
1069 * @gsm: GSM channel
1070 * @data: data following command
1071 * @clen: command length
1072 *
1073 * We have received a modem status control message. This is used by
1074 * the GSM mux protocol to pass virtual modem line status and optionally
1075 * to indicate break signals. Unpack it, convert to Linux representation
1076 * and if need be stuff a break message down the tty.
1077 */
1078
Tony Lindgren4feb7a42019-01-13 17:25:27 -08001079static void gsm_control_modem(struct gsm_mux *gsm, const u8 *data, int clen)
Alan Coxe1eaea42010-03-26 11:32:54 +00001080{
1081 unsigned int addr = 0;
1082 unsigned int modem = 0;
Lars Poeschel3ac06b92014-01-07 13:34:37 +01001083 unsigned int brk = 0;
Alan Coxe1eaea42010-03-26 11:32:54 +00001084 struct gsm_dlci *dlci;
1085 int len = clen;
Tony Lindgren4feb7a42019-01-13 17:25:27 -08001086 const u8 *dp = data;
Alan Coxe1eaea42010-03-26 11:32:54 +00001087 struct tty_struct *tty;
1088
1089 while (gsm_read_ea(&addr, *dp++) == 0) {
1090 len--;
1091 if (len == 0)
1092 return;
1093 }
1094 /* Must be at least one byte following the EA */
1095 len--;
1096 if (len <= 0)
1097 return;
1098
1099 addr >>= 1;
1100 /* Closed port, or invalid ? */
1101 if (addr == 0 || addr >= NUM_DLCI || gsm->dlci[addr] == NULL)
1102 return;
1103 dlci = gsm->dlci[addr];
1104
1105 while (gsm_read_ea(&modem, *dp++) == 0) {
1106 len--;
1107 if (len == 0)
1108 return;
1109 }
Lars Poeschel3ac06b92014-01-07 13:34:37 +01001110 len--;
1111 if (len > 0) {
1112 while (gsm_read_ea(&brk, *dp++) == 0) {
1113 len--;
1114 if (len == 0)
1115 return;
1116 }
1117 modem <<= 7;
1118 modem |= (brk & 0x7f);
1119 }
Alan Coxe1eaea42010-03-26 11:32:54 +00001120 tty = tty_port_tty_get(&dlci->port);
Russ Gorby72632872011-06-14 13:23:28 -07001121 gsm_process_modem(tty, dlci, modem, clen);
Alan Coxe1eaea42010-03-26 11:32:54 +00001122 if (tty) {
1123 tty_wakeup(tty);
1124 tty_kref_put(tty);
1125 }
1126 gsm_control_reply(gsm, CMD_MSC, data, clen);
1127}
1128
1129/**
1130 * gsm_control_rls - remote line status
1131 * @gsm: GSM channel
1132 * @data: data bytes
1133 * @clen: data length
1134 *
1135 * The modem sends us a two byte message on the control channel whenever
1136 * it wishes to send us an error state from the virtual link. Stuff
1137 * this into the uplink tty if present
1138 */
1139
Tony Lindgren4feb7a42019-01-13 17:25:27 -08001140static void gsm_control_rls(struct gsm_mux *gsm, const u8 *data, int clen)
Alan Coxe1eaea42010-03-26 11:32:54 +00001141{
Jiri Slaby92a19f92013-01-03 15:53:03 +01001142 struct tty_port *port;
Aldo Iljazif3c909b2013-07-08 22:28:00 +03001143 unsigned int addr = 0;
Alan Coxe1eaea42010-03-26 11:32:54 +00001144 u8 bits;
1145 int len = clen;
Tony Lindgren4feb7a42019-01-13 17:25:27 -08001146 const u8 *dp = data;
Alan Coxe1eaea42010-03-26 11:32:54 +00001147
1148 while (gsm_read_ea(&addr, *dp++) == 0) {
1149 len--;
1150 if (len == 0)
1151 return;
1152 }
1153 /* Must be at least one byte following ea */
1154 len--;
1155 if (len <= 0)
1156 return;
1157 addr >>= 1;
1158 /* Closed port, or invalid ? */
1159 if (addr == 0 || addr >= NUM_DLCI || gsm->dlci[addr] == NULL)
1160 return;
1161 /* No error ? */
1162 bits = *dp;
1163 if ((bits & 1) == 0)
1164 return;
Alan Coxe1eaea42010-03-26 11:32:54 +00001165
Jiri Slaby92a19f92013-01-03 15:53:03 +01001166 port = &gsm->dlci[addr]->port;
1167
1168 if (bits & 2)
1169 tty_insert_flip_char(port, 0, TTY_OVERRUN);
1170 if (bits & 4)
1171 tty_insert_flip_char(port, 0, TTY_PARITY);
1172 if (bits & 8)
1173 tty_insert_flip_char(port, 0, TTY_FRAME);
1174
Jiri Slaby2e124b42013-01-03 15:53:06 +01001175 tty_flip_buffer_push(port);
1176
Alan Coxe1eaea42010-03-26 11:32:54 +00001177 gsm_control_reply(gsm, CMD_RLS, data, clen);
1178}
1179
1180static void gsm_dlci_begin_close(struct gsm_dlci *dlci);
1181
1182/**
1183 * gsm_control_message - DLCI 0 control processing
1184 * @gsm: our GSM mux
1185 * @command: the command EA
1186 * @data: data beyond the command/length EAs
1187 * @clen: length
1188 *
1189 * Input processor for control messages from the other end of the link.
1190 * Processes the incoming request and queues a response frame or an
1191 * NSC response if not supported
1192 */
1193
1194static void gsm_control_message(struct gsm_mux *gsm, unsigned int command,
Tony Lindgren4feb7a42019-01-13 17:25:27 -08001195 const u8 *data, int clen)
Alan Coxe1eaea42010-03-26 11:32:54 +00001196{
1197 u8 buf[1];
Russ Gorby5e447082012-08-13 13:44:40 +01001198 unsigned long flags;
1199
Alan Coxe1eaea42010-03-26 11:32:54 +00001200 switch (command) {
1201 case CMD_CLD: {
1202 struct gsm_dlci *dlci = gsm->dlci[0];
1203 /* Modem wishes to close down */
1204 if (dlci) {
Jiri Slaby5677fcf2020-02-19 09:49:46 +01001205 dlci->dead = true;
1206 gsm->dead = true;
Alan Coxe1eaea42010-03-26 11:32:54 +00001207 gsm_dlci_begin_close(dlci);
1208 }
1209 }
1210 break;
1211 case CMD_TEST:
1212 /* Modem wishes to test, reply with the data */
1213 gsm_control_reply(gsm, CMD_TEST, data, clen);
1214 break;
1215 case CMD_FCON:
Alan Coxe1eaea42010-03-26 11:32:54 +00001216 /* Modem can accept data again */
Jiri Slaby7a9ed9c2020-02-19 09:49:48 +01001217 gsm->constipated = false;
Frederic Beratc01af4f2012-08-13 13:43:58 +01001218 gsm_control_reply(gsm, CMD_FCON, NULL, 0);
Alan Coxe1eaea42010-03-26 11:32:54 +00001219 /* Kick the link in case it is idling */
Russ Gorby5e447082012-08-13 13:44:40 +01001220 spin_lock_irqsave(&gsm->tx_lock, flags);
Gregory CLEMENT01dbb362020-05-12 13:53:23 +02001221 gsm_data_kick(gsm, NULL);
Russ Gorby5e447082012-08-13 13:44:40 +01001222 spin_unlock_irqrestore(&gsm->tx_lock, flags);
Alan Coxe1eaea42010-03-26 11:32:54 +00001223 break;
Frederic Beratc01af4f2012-08-13 13:43:58 +01001224 case CMD_FCOFF:
1225 /* Modem wants us to STFU */
Jiri Slaby7a9ed9c2020-02-19 09:49:48 +01001226 gsm->constipated = true;
Frederic Beratc01af4f2012-08-13 13:43:58 +01001227 gsm_control_reply(gsm, CMD_FCOFF, NULL, 0);
1228 break;
Alan Coxe1eaea42010-03-26 11:32:54 +00001229 case CMD_MSC:
1230 /* Out of band modem line change indicator for a DLCI */
1231 gsm_control_modem(gsm, data, clen);
1232 break;
1233 case CMD_RLS:
1234 /* Out of band error reception for a DLCI */
1235 gsm_control_rls(gsm, data, clen);
1236 break;
1237 case CMD_PSC:
1238 /* Modem wishes to enter power saving state */
1239 gsm_control_reply(gsm, CMD_PSC, NULL, 0);
1240 break;
1241 /* Optional unsupported commands */
1242 case CMD_PN: /* Parameter negotiation */
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001243 case CMD_RPN: /* Remote port negotiation */
1244 case CMD_SNC: /* Service negotiation command */
Alan Coxe1eaea42010-03-26 11:32:54 +00001245 default:
1246 /* Reply to bad commands with an NSC */
1247 buf[0] = command;
1248 gsm_control_reply(gsm, CMD_NSC, buf, 1);
1249 break;
1250 }
1251}
1252
1253/**
1254 * gsm_control_response - process a response to our control
1255 * @gsm: our GSM mux
1256 * @command: the command (response) EA
1257 * @data: data beyond the command/length EA
1258 * @clen: length
1259 *
1260 * Process a response to an outstanding command. We only allow a single
1261 * control message in flight so this is fairly easy. All the clean up
1262 * is done by the caller, we just update the fields, flag it as done
1263 * and return
1264 */
1265
1266static void gsm_control_response(struct gsm_mux *gsm, unsigned int command,
Tony Lindgren4feb7a42019-01-13 17:25:27 -08001267 const u8 *data, int clen)
Alan Coxe1eaea42010-03-26 11:32:54 +00001268{
1269 struct gsm_control *ctrl;
1270 unsigned long flags;
1271
1272 spin_lock_irqsave(&gsm->control_lock, flags);
1273
1274 ctrl = gsm->pending_cmd;
1275 /* Does the reply match our command */
1276 command |= 1;
1277 if (ctrl != NULL && (command == ctrl->cmd || command == CMD_NSC)) {
1278 /* Our command was replied to, kill the retry timer */
1279 del_timer(&gsm->t2_timer);
1280 gsm->pending_cmd = NULL;
1281 /* Rejected by the other end */
1282 if (command == CMD_NSC)
1283 ctrl->error = -EOPNOTSUPP;
1284 ctrl->done = 1;
1285 wake_up(&gsm->event);
1286 }
1287 spin_unlock_irqrestore(&gsm->control_lock, flags);
1288}
1289
1290/**
Alan Cox5f9a31d2010-11-04 15:17:27 +00001291 * gsm_control_transmit - send control packet
Alan Coxe1eaea42010-03-26 11:32:54 +00001292 * @gsm: gsm mux
1293 * @ctrl: frame to send
1294 *
1295 * Send out a pending control command (called under control lock)
1296 */
1297
1298static void gsm_control_transmit(struct gsm_mux *gsm, struct gsm_control *ctrl)
1299{
Daniel Starke320a24c2022-04-14 02:42:17 -07001300 struct gsm_msg *msg = gsm_data_alloc(gsm, 0, ctrl->len + 2, gsm->ftype);
Alan Coxe1eaea42010-03-26 11:32:54 +00001301 if (msg == NULL)
1302 return;
Daniel Starke320a24c2022-04-14 02:42:17 -07001303 msg->data[0] = (ctrl->cmd << 1) | CR | EA; /* command */
1304 msg->data[1] = (ctrl->len << 1) | EA;
1305 memcpy(msg->data + 2, ctrl->data, ctrl->len);
Alan Coxe1eaea42010-03-26 11:32:54 +00001306 gsm_data_queue(gsm->dlci[0], msg);
1307}
1308
1309/**
1310 * gsm_control_retransmit - retransmit a control frame
Jiri Slaby724ac072020-08-18 10:56:52 +02001311 * @t: timer contained in our gsm object
Alan Coxe1eaea42010-03-26 11:32:54 +00001312 *
1313 * Called off the T2 timer expiry in order to retransmit control frames
1314 * that have been lost in the system somewhere. The control_lock protects
1315 * us from colliding with another sender or a receive completion event.
1316 * In that situation the timer may still occur in a small window but
1317 * gsm->pending_cmd will be NULL and we just let the timer expire.
1318 */
1319
Kees Cooke99e88a2017-10-16 14:43:17 -07001320static void gsm_control_retransmit(struct timer_list *t)
Alan Coxe1eaea42010-03-26 11:32:54 +00001321{
Kees Cooke99e88a2017-10-16 14:43:17 -07001322 struct gsm_mux *gsm = from_timer(gsm, t, t2_timer);
Alan Coxe1eaea42010-03-26 11:32:54 +00001323 struct gsm_control *ctrl;
1324 unsigned long flags;
1325 spin_lock_irqsave(&gsm->control_lock, flags);
1326 ctrl = gsm->pending_cmd;
1327 if (ctrl) {
Alan Coxe1eaea42010-03-26 11:32:54 +00001328 if (gsm->cretries == 0) {
1329 gsm->pending_cmd = NULL;
1330 ctrl->error = -ETIMEDOUT;
1331 ctrl->done = 1;
1332 spin_unlock_irqrestore(&gsm->control_lock, flags);
1333 wake_up(&gsm->event);
1334 return;
1335 }
Daniel Starke935f3142022-04-14 02:42:16 -07001336 gsm->cretries--;
Alan Coxe1eaea42010-03-26 11:32:54 +00001337 gsm_control_transmit(gsm, ctrl);
1338 mod_timer(&gsm->t2_timer, jiffies + gsm->t2 * HZ / 100);
1339 }
1340 spin_unlock_irqrestore(&gsm->control_lock, flags);
1341}
1342
1343/**
1344 * gsm_control_send - send a control frame on DLCI 0
1345 * @gsm: the GSM channel
1346 * @command: command to send including CR bit
1347 * @data: bytes of data (must be kmalloced)
Jiri Slaby724ac072020-08-18 10:56:52 +02001348 * @clen: length of the block to send
Alan Coxe1eaea42010-03-26 11:32:54 +00001349 *
1350 * Queue and dispatch a control command. Only one command can be
1351 * active at a time. In theory more can be outstanding but the matching
1352 * gets really complicated so for now stick to one outstanding.
1353 */
1354
1355static struct gsm_control *gsm_control_send(struct gsm_mux *gsm,
1356 unsigned int command, u8 *data, int clen)
1357{
1358 struct gsm_control *ctrl = kzalloc(sizeof(struct gsm_control),
1359 GFP_KERNEL);
1360 unsigned long flags;
1361 if (ctrl == NULL)
1362 return NULL;
1363retry:
1364 wait_event(gsm->event, gsm->pending_cmd == NULL);
1365 spin_lock_irqsave(&gsm->control_lock, flags);
1366 if (gsm->pending_cmd != NULL) {
1367 spin_unlock_irqrestore(&gsm->control_lock, flags);
1368 goto retry;
1369 }
1370 ctrl->cmd = command;
1371 ctrl->data = data;
1372 ctrl->len = clen;
1373 gsm->pending_cmd = ctrl;
Tony Lindgrene9ec2252018-04-07 10:19:50 -07001374
1375 /* If DLCI0 is in ADM mode skip retries, it won't respond */
1376 if (gsm->dlci[0]->mode == DLCI_MODE_ADM)
Daniel Starke935f3142022-04-14 02:42:16 -07001377 gsm->cretries = 0;
Tony Lindgrene9ec2252018-04-07 10:19:50 -07001378 else
1379 gsm->cretries = gsm->n2;
1380
Alan Coxe1eaea42010-03-26 11:32:54 +00001381 mod_timer(&gsm->t2_timer, jiffies + gsm->t2 * HZ / 100);
1382 gsm_control_transmit(gsm, ctrl);
1383 spin_unlock_irqrestore(&gsm->control_lock, flags);
1384 return ctrl;
1385}
1386
1387/**
1388 * gsm_control_wait - wait for a control to finish
1389 * @gsm: GSM mux
1390 * @control: control we are waiting on
1391 *
1392 * Waits for the control to complete or time out. Frees any used
1393 * resources and returns 0 for success, or an error if the remote
1394 * rejected or ignored the request.
1395 */
1396
1397static int gsm_control_wait(struct gsm_mux *gsm, struct gsm_control *control)
1398{
1399 int err;
1400 wait_event(gsm->event, control->done == 1);
1401 err = control->error;
1402 kfree(control);
1403 return err;
1404}
1405
1406
1407/*
1408 * DLCI level handling: Needs krefs
1409 */
1410
1411/*
1412 * State transitions and timers
1413 */
1414
1415/**
1416 * gsm_dlci_close - a DLCI has closed
1417 * @dlci: DLCI that closed
1418 *
1419 * Perform processing when moving a DLCI into closed state. If there
1420 * is an attached tty this is hung up
1421 */
1422
1423static void gsm_dlci_close(struct gsm_dlci *dlci)
1424{
Daniel Starke70b045d2022-04-14 02:42:22 -07001425 unsigned long flags;
1426
Alan Coxe1eaea42010-03-26 11:32:54 +00001427 del_timer(&dlci->t1);
1428 if (debug & 8)
Alan Cox5f9a31d2010-11-04 15:17:27 +00001429 pr_debug("DLCI %d goes closed.\n", dlci->addr);
Alan Coxe1eaea42010-03-26 11:32:54 +00001430 dlci->state = DLCI_CLOSED;
1431 if (dlci->addr != 0) {
Jiri Slabyaa27a092013-03-07 13:12:30 +01001432 tty_port_tty_hangup(&dlci->port, false);
Daniel Starke70b045d2022-04-14 02:42:22 -07001433 spin_lock_irqsave(&dlci->lock, flags);
Jiri Slaby036bca1fc2020-02-19 09:49:40 +01001434 kfifo_reset(&dlci->fifo);
Daniel Starke70b045d2022-04-14 02:42:22 -07001435 spin_unlock_irqrestore(&dlci->lock, flags);
daniel.starke@siemens.combb2e0a72022-02-17 23:31:23 -08001436 /* Ensure that gsmtty_open() can return. */
1437 tty_port_set_initialized(&dlci->port, 0);
1438 wake_up_interruptible(&dlci->port.open_wait);
Alan Coxe1eaea42010-03-26 11:32:54 +00001439 } else
Jiri Slaby5677fcf2020-02-19 09:49:46 +01001440 dlci->gsm->dead = true;
Alan Coxe1eaea42010-03-26 11:32:54 +00001441 wake_up(&dlci->gsm->event);
1442 /* A DLCI 0 close is a MUX termination so we need to kick that
1443 back to userspace somehow */
1444}
1445
1446/**
1447 * gsm_dlci_open - a DLCI has opened
1448 * @dlci: DLCI that opened
1449 *
1450 * Perform processing when moving a DLCI into open state.
1451 */
1452
1453static void gsm_dlci_open(struct gsm_dlci *dlci)
1454{
1455 /* Note that SABM UA .. SABM UA first UA lost can mean that we go
1456 open -> open */
1457 del_timer(&dlci->t1);
1458 /* This will let a tty open continue */
1459 dlci->state = DLCI_OPEN;
1460 if (debug & 8)
Alan Cox5f9a31d2010-11-04 15:17:27 +00001461 pr_debug("DLCI %d goes open.\n", dlci->addr);
Alan Coxe1eaea42010-03-26 11:32:54 +00001462 wake_up(&dlci->gsm->event);
1463}
1464
1465/**
1466 * gsm_dlci_t1 - T1 timer expiry
Jiri Slaby724ac072020-08-18 10:56:52 +02001467 * @t: timer contained in the DLCI that opened
Alan Coxe1eaea42010-03-26 11:32:54 +00001468 *
1469 * The T1 timer handles retransmits of control frames (essentially of
1470 * SABM and DISC). We resend the command until the retry count runs out
1471 * in which case an opening port goes back to closed and a closing port
1472 * is simply put into closed state (any further frames from the other
1473 * end will get a DM response)
Tony Lindgrenea3d8462018-01-03 10:18:03 -08001474 *
1475 * Some control dlci can stay in ADM mode with other dlci working just
1476 * fine. In that case we can just keep the control dlci open after the
1477 * DLCI_OPENING retries time out.
Alan Coxe1eaea42010-03-26 11:32:54 +00001478 */
1479
Kees Cooke99e88a2017-10-16 14:43:17 -07001480static void gsm_dlci_t1(struct timer_list *t)
Alan Coxe1eaea42010-03-26 11:32:54 +00001481{
Kees Cooke99e88a2017-10-16 14:43:17 -07001482 struct gsm_dlci *dlci = from_timer(dlci, t, t1);
Alan Coxe1eaea42010-03-26 11:32:54 +00001483 struct gsm_mux *gsm = dlci->gsm;
1484
1485 switch (dlci->state) {
1486 case DLCI_OPENING:
1487 dlci->retries--;
1488 if (dlci->retries) {
1489 gsm_command(dlci->gsm, dlci->addr, SABM|PF);
1490 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
Tony Lindgrenea3d8462018-01-03 10:18:03 -08001491 } else if (!dlci->addr && gsm->control == (DM | PF)) {
1492 if (debug & 8)
1493 pr_info("DLCI %d opening in ADM mode.\n",
1494 dlci->addr);
Tony Lindgrene9ec2252018-04-07 10:19:50 -07001495 dlci->mode = DLCI_MODE_ADM;
Tony Lindgrenea3d8462018-01-03 10:18:03 -08001496 gsm_dlci_open(dlci);
1497 } else {
daniel.starke@siemens.com1e35cb92022-02-17 23:31:19 -08001498 gsm_dlci_begin_close(dlci); /* prevent half open link */
Tony Lindgrenea3d8462018-01-03 10:18:03 -08001499 }
1500
Alan Coxe1eaea42010-03-26 11:32:54 +00001501 break;
1502 case DLCI_CLOSING:
1503 dlci->retries--;
1504 if (dlci->retries) {
1505 gsm_command(dlci->gsm, dlci->addr, DISC|PF);
1506 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
1507 } else
1508 gsm_dlci_close(dlci);
1509 break;
Jiri Slaby72ae8cc2020-02-19 09:49:41 +01001510 default:
1511 pr_debug("%s: unhandled state: %d\n", __func__, dlci->state);
1512 break;
Alan Coxe1eaea42010-03-26 11:32:54 +00001513 }
1514}
1515
1516/**
1517 * gsm_dlci_begin_open - start channel open procedure
1518 * @dlci: DLCI to open
1519 *
1520 * Commence opening a DLCI from the Linux side. We issue SABM messages
Tony Lindgrenea3d8462018-01-03 10:18:03 -08001521 * to the modem which should then reply with a UA or ADM, at which point
1522 * we will move into open state. Opening is done asynchronously with retry
Alan Coxe1eaea42010-03-26 11:32:54 +00001523 * running off timers and the responses.
1524 */
1525
1526static void gsm_dlci_begin_open(struct gsm_dlci *dlci)
1527{
1528 struct gsm_mux *gsm = dlci->gsm;
1529 if (dlci->state == DLCI_OPEN || dlci->state == DLCI_OPENING)
1530 return;
1531 dlci->retries = gsm->n2;
1532 dlci->state = DLCI_OPENING;
1533 gsm_command(dlci->gsm, dlci->addr, SABM|PF);
1534 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
1535}
1536
1537/**
1538 * gsm_dlci_begin_close - start channel open procedure
1539 * @dlci: DLCI to open
1540 *
1541 * Commence closing a DLCI from the Linux side. We issue DISC messages
1542 * to the modem which should then reply with a UA, at which point we
1543 * will move into closed state. Closing is done asynchronously with retry
1544 * off timers. We may also receive a DM reply from the other end which
1545 * indicates the channel was already closed.
1546 */
1547
1548static void gsm_dlci_begin_close(struct gsm_dlci *dlci)
1549{
1550 struct gsm_mux *gsm = dlci->gsm;
1551 if (dlci->state == DLCI_CLOSED || dlci->state == DLCI_CLOSING)
1552 return;
1553 dlci->retries = gsm->n2;
1554 dlci->state = DLCI_CLOSING;
1555 gsm_command(dlci->gsm, dlci->addr, DISC|PF);
1556 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
1557}
1558
1559/**
1560 * gsm_dlci_data - data arrived
1561 * @dlci: channel
1562 * @data: block of bytes received
Jiri Slaby724ac072020-08-18 10:56:52 +02001563 * @clen: length of received block
Alan Coxe1eaea42010-03-26 11:32:54 +00001564 *
1565 * A UI or UIH frame has arrived which contains data for a channel
1566 * other than the control channel. If the relevant virtual tty is
1567 * open we shovel the bits down it, if not we drop them.
1568 */
1569
Tony Lindgren4feb7a42019-01-13 17:25:27 -08001570static void gsm_dlci_data(struct gsm_dlci *dlci, const u8 *data, int clen)
Alan Coxe1eaea42010-03-26 11:32:54 +00001571{
1572 /* krefs .. */
1573 struct tty_port *port = &dlci->port;
Jiri Slaby2e124b42013-01-03 15:53:06 +01001574 struct tty_struct *tty;
Alan Coxe1eaea42010-03-26 11:32:54 +00001575 unsigned int modem = 0;
Russ Gorby72632872011-06-14 13:23:28 -07001576 int len = clen;
Alan Coxe1eaea42010-03-26 11:32:54 +00001577
1578 if (debug & 16)
Jiri Slaby2e124b42013-01-03 15:53:06 +01001579 pr_debug("%d bytes for tty\n", len);
1580 switch (dlci->adaption) {
1581 /* Unsupported types */
Gustavo A. R. Silva3e913ee2019-02-25 11:28:14 -06001582 case 4: /* Packetised interruptible data */
Jiri Slaby2e124b42013-01-03 15:53:06 +01001583 break;
Gustavo A. R. Silva3e913ee2019-02-25 11:28:14 -06001584 case 3: /* Packetised uininterruptible voice/data */
Jiri Slaby2e124b42013-01-03 15:53:06 +01001585 break;
Gustavo A. R. Silva3e913ee2019-02-25 11:28:14 -06001586 case 2: /* Asynchronous serial with line state in each frame */
Jiri Slaby2e124b42013-01-03 15:53:06 +01001587 while (gsm_read_ea(&modem, *data++) == 0) {
1588 len--;
1589 if (len == 0)
1590 return;
Alan Coxe1eaea42010-03-26 11:32:54 +00001591 }
Jiri Slaby2e124b42013-01-03 15:53:06 +01001592 tty = tty_port_tty_get(port);
1593 if (tty) {
1594 gsm_process_modem(tty, dlci, modem, clen);
1595 tty_kref_put(tty);
1596 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001597 fallthrough;
Gustavo A. R. Silva3e913ee2019-02-25 11:28:14 -06001598 case 1: /* Line state will go via DLCI 0 controls only */
Jiri Slaby2e124b42013-01-03 15:53:06 +01001599 default:
1600 tty_insert_flip_string(port, data, len);
1601 tty_flip_buffer_push(port);
Alan Coxe1eaea42010-03-26 11:32:54 +00001602 }
1603}
1604
1605/**
1606 * gsm_dlci_control - data arrived on control channel
1607 * @dlci: channel
1608 * @data: block of bytes received
1609 * @len: length of received block
1610 *
1611 * A UI or UIH frame has arrived which contains data for DLCI 0 the
1612 * control channel. This should contain a command EA followed by
1613 * control data bytes. The command EA contains a command/response bit
1614 * and we divide up the work accordingly.
1615 */
1616
Tony Lindgren4feb7a42019-01-13 17:25:27 -08001617static void gsm_dlci_command(struct gsm_dlci *dlci, const u8 *data, int len)
Alan Coxe1eaea42010-03-26 11:32:54 +00001618{
1619 /* See what command is involved */
1620 unsigned int command = 0;
1621 while (len-- > 0) {
1622 if (gsm_read_ea(&command, *data++) == 1) {
1623 int clen = *data++;
1624 len--;
1625 /* FIXME: this is properly an EA */
1626 clen >>= 1;
1627 /* Malformed command ? */
1628 if (clen > len)
1629 return;
1630 if (command & 1)
1631 gsm_control_message(dlci->gsm, command,
1632 data, clen);
1633 else
1634 gsm_control_response(dlci->gsm, command,
1635 data, clen);
1636 return;
1637 }
1638 }
1639}
1640
1641/*
1642 * Allocate/Free DLCI channels
1643 */
1644
1645/**
1646 * gsm_dlci_alloc - allocate a DLCI
1647 * @gsm: GSM mux
1648 * @addr: address of the DLCI
1649 *
1650 * Allocate and install a new DLCI object into the GSM mux.
1651 *
1652 * FIXME: review locking races
1653 */
1654
1655static struct gsm_dlci *gsm_dlci_alloc(struct gsm_mux *gsm, int addr)
1656{
1657 struct gsm_dlci *dlci = kzalloc(sizeof(struct gsm_dlci), GFP_ATOMIC);
1658 if (dlci == NULL)
1659 return NULL;
1660 spin_lock_init(&dlci->lock);
Russ Gorbybcd5abe2011-06-16 14:20:12 -07001661 mutex_init(&dlci->mutex);
Jiri Slaby036bca1fc2020-02-19 09:49:40 +01001662 if (kfifo_alloc(&dlci->fifo, 4096, GFP_KERNEL) < 0) {
Alan Coxe1eaea42010-03-26 11:32:54 +00001663 kfree(dlci);
1664 return NULL;
1665 }
1666
1667 skb_queue_head_init(&dlci->skb_list);
Kees Cooke99e88a2017-10-16 14:43:17 -07001668 timer_setup(&dlci->t1, gsm_dlci_t1, 0);
Alan Coxe1eaea42010-03-26 11:32:54 +00001669 tty_port_init(&dlci->port);
1670 dlci->port.ops = &gsm_port_ops;
1671 dlci->gsm = gsm;
1672 dlci->addr = addr;
1673 dlci->adaption = gsm->adaption;
1674 dlci->state = DLCI_CLOSED;
1675 if (addr)
1676 dlci->data = gsm_dlci_data;
1677 else
1678 dlci->data = gsm_dlci_command;
1679 gsm->dlci[addr] = dlci;
1680 return dlci;
1681}
1682
1683/**
Russ Gorby6ab8fba2011-06-16 14:20:13 -07001684 * gsm_dlci_free - free DLCI
Jiri Slaby724ac072020-08-18 10:56:52 +02001685 * @port: tty port for DLCI to free
Alan Coxe1eaea42010-03-26 11:32:54 +00001686 *
Russ Gorby6ab8fba2011-06-16 14:20:13 -07001687 * Free up a DLCI.
Alan Coxe1eaea42010-03-26 11:32:54 +00001688 *
1689 * Can sleep.
1690 */
Jiri Slaby9a8e62b2012-11-15 09:49:53 +01001691static void gsm_dlci_free(struct tty_port *port)
Russ Gorby6ab8fba2011-06-16 14:20:13 -07001692{
Jiri Slaby9a8e62b2012-11-15 09:49:53 +01001693 struct gsm_dlci *dlci = container_of(port, struct gsm_dlci, port);
Russ Gorby6ab8fba2011-06-16 14:20:13 -07001694
1695 del_timer_sync(&dlci->t1);
1696 dlci->gsm->dlci[dlci->addr] = NULL;
Jiri Slaby036bca1fc2020-02-19 09:49:40 +01001697 kfifo_free(&dlci->fifo);
Russ Gorby6ab8fba2011-06-16 14:20:13 -07001698 while ((dlci->skb = skb_dequeue(&dlci->skb_list)))
Russ Gorby329e5672012-08-13 13:45:15 +01001699 dev_kfree_skb(dlci->skb);
Russ Gorby6ab8fba2011-06-16 14:20:13 -07001700 kfree(dlci);
1701}
1702
1703static inline void dlci_get(struct gsm_dlci *dlci)
1704{
Jiri Slaby9a8e62b2012-11-15 09:49:53 +01001705 tty_port_get(&dlci->port);
Russ Gorby6ab8fba2011-06-16 14:20:13 -07001706}
1707
1708static inline void dlci_put(struct gsm_dlci *dlci)
1709{
Jiri Slaby9a8e62b2012-11-15 09:49:53 +01001710 tty_port_put(&dlci->port);
Russ Gorby6ab8fba2011-06-16 14:20:13 -07001711}
1712
Dirkjan Bussink4d9b1092013-01-30 11:44:50 +01001713static void gsm_destroy_network(struct gsm_dlci *dlci);
1714
Russ Gorby6ab8fba2011-06-16 14:20:13 -07001715/**
1716 * gsm_dlci_release - release DLCI
1717 * @dlci: DLCI to destroy
1718 *
1719 * Release a DLCI. Actual free is deferred until either
1720 * mux is closed or tty is closed - whichever is last.
1721 *
1722 * Can sleep.
1723 */
1724static void gsm_dlci_release(struct gsm_dlci *dlci)
Alan Coxe1eaea42010-03-26 11:32:54 +00001725{
1726 struct tty_struct *tty = tty_port_tty_get(&dlci->port);
1727 if (tty) {
Dirkjan Bussink4d9b1092013-01-30 11:44:50 +01001728 mutex_lock(&dlci->mutex);
1729 gsm_destroy_network(dlci);
1730 mutex_unlock(&dlci->mutex);
1731
daniel.starke@siemens.com1f0641d2022-02-17 23:31:20 -08001732 /* We cannot use tty_hangup() because in tty_kref_put() the tty
1733 * driver assumes that the hangup queue is free and reuses it to
1734 * queue release_one_tty() -> NULL pointer panic in
1735 * process_one_work().
1736 */
1737 tty_vhangup(tty);
Chuansheng Liube706572013-12-18 13:30:11 +08001738
Dirkjan Bussink4d9b1092013-01-30 11:44:50 +01001739 tty_port_tty_set(&dlci->port, NULL);
Alan Coxe1eaea42010-03-26 11:32:54 +00001740 tty_kref_put(tty);
1741 }
Dirkjan Bussink4d9b1092013-01-30 11:44:50 +01001742 dlci->state = DLCI_CLOSED;
Russ Gorby6ab8fba2011-06-16 14:20:13 -07001743 dlci_put(dlci);
Alan Coxe1eaea42010-03-26 11:32:54 +00001744}
1745
Alan Coxe1eaea42010-03-26 11:32:54 +00001746/*
1747 * LAPBish link layer logic
1748 */
1749
1750/**
1751 * gsm_queue - a GSM frame is ready to process
1752 * @gsm: pointer to our gsm mux
1753 *
1754 * At this point in time a frame has arrived and been demangled from
1755 * the line encoding. All the differences between the encodings have
1756 * been handled below us and the frame is unpacked into the structures.
1757 * The fcs holds the header FCS but any data FCS must be added here.
1758 */
1759
1760static void gsm_queue(struct gsm_mux *gsm)
1761{
1762 struct gsm_dlci *dlci;
1763 u8 cr;
1764 int address;
1765 /* We have to sneak a look at the packet body to do the FCS.
1766 A somewhat layering violation in the spec */
1767
1768 if ((gsm->control & ~PF) == UI)
1769 gsm->fcs = gsm_fcs_add_block(gsm->fcs, gsm->buf, gsm->len);
Aldo Iljazif3c909b2013-07-08 22:28:00 +03001770 if (gsm->encoding == 0) {
1771 /* WARNING: gsm->received_fcs is used for
1772 gsm->encoding = 0 only.
1773 In this case it contain the last piece of data
1774 required to generate final CRC */
Mikhail Kshevetskiy9db4e432011-03-27 04:05:00 +04001775 gsm->fcs = gsm_fcs_add(gsm->fcs, gsm->received_fcs);
1776 }
Alan Coxe1eaea42010-03-26 11:32:54 +00001777 if (gsm->fcs != GOOD_FCS) {
1778 gsm->bad_fcs++;
1779 if (debug & 4)
Alan Cox5f9a31d2010-11-04 15:17:27 +00001780 pr_debug("BAD FCS %02x\n", gsm->fcs);
Alan Coxe1eaea42010-03-26 11:32:54 +00001781 return;
1782 }
1783 address = gsm->address >> 1;
1784 if (address >= NUM_DLCI)
1785 goto invalid;
1786
1787 cr = gsm->address & 1; /* C/R bit */
1788
1789 gsm_print_packet("<--", address, cr, gsm->control, gsm->buf, gsm->len);
1790
1791 cr ^= 1 - gsm->initiator; /* Flip so 1 always means command */
1792 dlci = gsm->dlci[address];
1793
1794 switch (gsm->control) {
1795 case SABM|PF:
1796 if (cr == 0)
1797 goto invalid;
1798 if (dlci == NULL)
1799 dlci = gsm_dlci_alloc(gsm, address);
1800 if (dlci == NULL)
1801 return;
1802 if (dlci->dead)
1803 gsm_response(gsm, address, DM);
1804 else {
1805 gsm_response(gsm, address, UA);
1806 gsm_dlci_open(dlci);
1807 }
1808 break;
1809 case DISC|PF:
1810 if (cr == 0)
1811 goto invalid;
1812 if (dlci == NULL || dlci->state == DLCI_CLOSED) {
1813 gsm_response(gsm, address, DM);
1814 return;
1815 }
1816 /* Real close complete */
1817 gsm_response(gsm, address, UA);
1818 gsm_dlci_close(dlci);
1819 break;
1820 case UA:
1821 case UA|PF:
1822 if (cr == 0 || dlci == NULL)
1823 break;
1824 switch (dlci->state) {
1825 case DLCI_CLOSING:
1826 gsm_dlci_close(dlci);
1827 break;
1828 case DLCI_OPENING:
1829 gsm_dlci_open(dlci);
1830 break;
Jiri Slaby72ae8cc2020-02-19 09:49:41 +01001831 default:
1832 pr_debug("%s: unhandled state: %d\n", __func__,
1833 dlci->state);
1834 break;
Alan Coxe1eaea42010-03-26 11:32:54 +00001835 }
1836 break;
1837 case DM: /* DM can be valid unsolicited */
1838 case DM|PF:
1839 if (cr)
1840 goto invalid;
1841 if (dlci == NULL)
1842 return;
1843 gsm_dlci_close(dlci);
1844 break;
1845 case UI:
1846 case UI|PF:
1847 case UIH:
1848 case UIH|PF:
1849#if 0
1850 if (cr)
1851 goto invalid;
1852#endif
1853 if (dlci == NULL || dlci->state != DLCI_OPEN) {
1854 gsm_command(gsm, address, DM|PF);
1855 return;
1856 }
1857 dlci->data(dlci, gsm->buf, gsm->len);
1858 break;
1859 default:
1860 goto invalid;
1861 }
1862 return;
1863invalid:
1864 gsm->malformed++;
1865 return;
1866}
1867
1868
1869/**
1870 * gsm0_receive - perform processing for non-transparency
1871 * @gsm: gsm data for this ldisc instance
1872 * @c: character
1873 *
1874 * Receive bytes in gsm mode 0
1875 */
1876
1877static void gsm0_receive(struct gsm_mux *gsm, unsigned char c)
1878{
Alan Coxc2f2f002010-11-04 15:17:03 +00001879 unsigned int len;
1880
Alan Coxe1eaea42010-03-26 11:32:54 +00001881 switch (gsm->state) {
1882 case GSM_SEARCH: /* SOF marker */
1883 if (c == GSM0_SOF) {
1884 gsm->state = GSM_ADDRESS;
1885 gsm->address = 0;
1886 gsm->len = 0;
1887 gsm->fcs = INIT_FCS;
1888 }
Alan Coxc2f2f002010-11-04 15:17:03 +00001889 break;
1890 case GSM_ADDRESS: /* Address EA */
Alan Coxe1eaea42010-03-26 11:32:54 +00001891 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
1892 if (gsm_read_ea(&gsm->address, c))
1893 gsm->state = GSM_CONTROL;
1894 break;
1895 case GSM_CONTROL: /* Control Byte */
1896 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
1897 gsm->control = c;
Alan Coxc2f2f002010-11-04 15:17:03 +00001898 gsm->state = GSM_LEN0;
Alan Coxe1eaea42010-03-26 11:32:54 +00001899 break;
Alan Coxc2f2f002010-11-04 15:17:03 +00001900 case GSM_LEN0: /* Length EA */
Alan Coxe1eaea42010-03-26 11:32:54 +00001901 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
1902 if (gsm_read_ea(&gsm->len, c)) {
1903 if (gsm->len > gsm->mru) {
1904 gsm->bad_size++;
1905 gsm->state = GSM_SEARCH;
1906 break;
1907 }
1908 gsm->count = 0;
Alan Coxc2f2f002010-11-04 15:17:03 +00001909 if (!gsm->len)
1910 gsm->state = GSM_FCS;
1911 else
1912 gsm->state = GSM_DATA;
1913 break;
Alan Coxe1eaea42010-03-26 11:32:54 +00001914 }
Alan Coxc2f2f002010-11-04 15:17:03 +00001915 gsm->state = GSM_LEN1;
1916 break;
1917 case GSM_LEN1:
1918 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
1919 len = c;
1920 gsm->len |= len << 7;
1921 if (gsm->len > gsm->mru) {
1922 gsm->bad_size++;
1923 gsm->state = GSM_SEARCH;
1924 break;
1925 }
1926 gsm->count = 0;
1927 if (!gsm->len)
1928 gsm->state = GSM_FCS;
1929 else
1930 gsm->state = GSM_DATA;
Alan Coxe1eaea42010-03-26 11:32:54 +00001931 break;
1932 case GSM_DATA: /* Data */
1933 gsm->buf[gsm->count++] = c;
1934 if (gsm->count == gsm->len)
1935 gsm->state = GSM_FCS;
1936 break;
1937 case GSM_FCS: /* FCS follows the packet */
Alan Coxc2f2f002010-11-04 15:17:03 +00001938 gsm->received_fcs = c;
Alan Coxe1eaea42010-03-26 11:32:54 +00001939 gsm_queue(gsm);
Alan Coxc2f2f002010-11-04 15:17:03 +00001940 gsm->state = GSM_SSOF;
1941 break;
1942 case GSM_SSOF:
1943 if (c == GSM0_SOF) {
1944 gsm->state = GSM_SEARCH;
1945 break;
1946 }
Alan Coxe1eaea42010-03-26 11:32:54 +00001947 break;
Jiri Slaby329aa6e2020-02-19 09:49:43 +01001948 default:
1949 pr_debug("%s: unhandled state: %d\n", __func__, gsm->state);
1950 break;
Alan Coxe1eaea42010-03-26 11:32:54 +00001951 }
1952}
1953
1954/**
Alan Coxc2f2f002010-11-04 15:17:03 +00001955 * gsm1_receive - perform processing for non-transparency
Alan Coxe1eaea42010-03-26 11:32:54 +00001956 * @gsm: gsm data for this ldisc instance
1957 * @c: character
1958 *
1959 * Receive bytes in mode 1 (Advanced option)
1960 */
1961
1962static void gsm1_receive(struct gsm_mux *gsm, unsigned char c)
1963{
1964 if (c == GSM1_SOF) {
1965 /* EOF is only valid in frame if we have got to the data state
1966 and received at least one byte (the FCS) */
1967 if (gsm->state == GSM_DATA && gsm->count) {
1968 /* Extract the FCS */
1969 gsm->count--;
1970 gsm->fcs = gsm_fcs_add(gsm->fcs, gsm->buf[gsm->count]);
1971 gsm->len = gsm->count;
1972 gsm_queue(gsm);
1973 gsm->state = GSM_START;
1974 return;
1975 }
1976 /* Any partial frame was a runt so go back to start */
1977 if (gsm->state != GSM_START) {
Daniel Starke7346e542022-04-14 02:42:12 -07001978 if (gsm->state != GSM_SEARCH)
1979 gsm->malformed++;
Alan Coxe1eaea42010-03-26 11:32:54 +00001980 gsm->state = GSM_START;
1981 }
1982 /* A SOF in GSM_START means we are still reading idling or
1983 framing bytes */
1984 return;
1985 }
1986
1987 if (c == GSM1_ESCAPE) {
Jiri Slabyc50704b2020-02-19 09:49:49 +01001988 gsm->escape = true;
Alan Coxe1eaea42010-03-26 11:32:54 +00001989 return;
1990 }
1991
1992 /* Only an unescaped SOF gets us out of GSM search */
1993 if (gsm->state == GSM_SEARCH)
1994 return;
1995
1996 if (gsm->escape) {
1997 c ^= GSM1_ESCAPE_BITS;
Jiri Slabyc50704b2020-02-19 09:49:49 +01001998 gsm->escape = false;
Alan Coxe1eaea42010-03-26 11:32:54 +00001999 }
2000 switch (gsm->state) {
2001 case GSM_START: /* First byte after SOF */
2002 gsm->address = 0;
2003 gsm->state = GSM_ADDRESS;
2004 gsm->fcs = INIT_FCS;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002005 fallthrough;
Alan Coxe1eaea42010-03-26 11:32:54 +00002006 case GSM_ADDRESS: /* Address continuation */
2007 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
2008 if (gsm_read_ea(&gsm->address, c))
2009 gsm->state = GSM_CONTROL;
2010 break;
2011 case GSM_CONTROL: /* Control Byte */
2012 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
2013 gsm->control = c;
2014 gsm->count = 0;
2015 gsm->state = GSM_DATA;
2016 break;
2017 case GSM_DATA: /* Data */
Alan Cox5f9a31d2010-11-04 15:17:27 +00002018 if (gsm->count > gsm->mru) { /* Allow one for the FCS */
Alan Coxe1eaea42010-03-26 11:32:54 +00002019 gsm->state = GSM_OVERRUN;
2020 gsm->bad_size++;
2021 } else
2022 gsm->buf[gsm->count++] = c;
2023 break;
2024 case GSM_OVERRUN: /* Over-long - eg a dropped SOF */
2025 break;
Jiri Slaby329aa6e2020-02-19 09:49:43 +01002026 default:
2027 pr_debug("%s: unhandled state: %d\n", __func__, gsm->state);
2028 break;
Alan Coxe1eaea42010-03-26 11:32:54 +00002029 }
2030}
2031
2032/**
2033 * gsm_error - handle tty error
2034 * @gsm: ldisc data
2035 * @data: byte received (may be invalid)
2036 * @flag: error received
2037 *
2038 * Handle an error in the receipt of data for a frame. Currently we just
2039 * go back to hunting for a SOF.
2040 *
2041 * FIXME: better diagnostics ?
2042 */
2043
2044static void gsm_error(struct gsm_mux *gsm,
2045 unsigned char data, unsigned char flag)
2046{
2047 gsm->state = GSM_SEARCH;
2048 gsm->io_error++;
2049}
2050
2051/**
2052 * gsm_cleanup_mux - generic GSM protocol cleanup
2053 * @gsm: our mux
Daniel Starke47132f92022-04-14 02:42:07 -07002054 * @disc: disconnect link?
Alan Coxe1eaea42010-03-26 11:32:54 +00002055 *
2056 * Clean up the bits of the mux which are the same for all framing
2057 * protocols. Remove the mux from the mux table, stop all the timers
2058 * and then shut down each device hanging up the channels as we go.
2059 */
2060
Daniel Starke47132f92022-04-14 02:42:07 -07002061static void gsm_cleanup_mux(struct gsm_mux *gsm, bool disc)
Alan Coxe1eaea42010-03-26 11:32:54 +00002062{
2063 int i;
2064 struct gsm_dlci *dlci = gsm->dlci[0];
Russ Gorby329e5672012-08-13 13:45:15 +01002065 struct gsm_msg *txq, *ntxq;
Alan Coxe1eaea42010-03-26 11:32:54 +00002066
Jiri Slaby5677fcf2020-02-19 09:49:46 +01002067 gsm->dead = true;
Daniel Starke47132f92022-04-14 02:42:07 -07002068 mutex_lock(&gsm->mutex);
2069
2070 if (dlci) {
2071 if (disc && dlci->state != DLCI_CLOSED) {
2072 gsm_dlci_begin_close(dlci);
2073 wait_event(gsm->event, dlci->state == DLCI_CLOSED);
2074 }
2075 dlci->dead = true;
2076 }
2077
2078 /* Finish outstanding timers, making sure they are done */
2079 del_timer_sync(&gsm->t2_timer);
Alan Coxe1eaea42010-03-26 11:32:54 +00002080
Daniel Starkea2baa902022-04-14 02:42:14 -07002081 /* Free up any link layer users and finally the control channel */
2082 for (i = NUM_DLCI - 1; i >= 0; i--)
Alan Coxe1eaea42010-03-26 11:32:54 +00002083 if (gsm->dlci[i])
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002084 gsm_dlci_release(gsm->dlci[i]);
Chao Bidfabf7f2013-11-26 12:09:39 +08002085 mutex_unlock(&gsm->mutex);
Alan Coxe1eaea42010-03-26 11:32:54 +00002086 /* Now wipe the queues */
Daniel Starke17b86db2022-04-14 02:42:15 -07002087 tty_ldisc_flush(gsm->tty);
Russ Gorbyb4338e12012-08-13 13:44:59 +01002088 list_for_each_entry_safe(txq, ntxq, &gsm->tx_list, list)
Alan Coxe1eaea42010-03-26 11:32:54 +00002089 kfree(txq);
Russ Gorbyb4338e12012-08-13 13:44:59 +01002090 INIT_LIST_HEAD(&gsm->tx_list);
Alan Coxe1eaea42010-03-26 11:32:54 +00002091}
Alan Coxe1eaea42010-03-26 11:32:54 +00002092
2093/**
2094 * gsm_activate_mux - generic GSM setup
2095 * @gsm: our mux
2096 *
2097 * Set up the bits of the mux which are the same for all framing
2098 * protocols. Add the mux to the mux table so it can be opened and
2099 * finally kick off connecting to DLCI 0 on the modem.
2100 */
2101
Rashika Kheria54af5832013-12-16 16:28:24 +05302102static int gsm_activate_mux(struct gsm_mux *gsm)
Alan Coxe1eaea42010-03-26 11:32:54 +00002103{
2104 struct gsm_dlci *dlci;
Alan Coxe1eaea42010-03-26 11:32:54 +00002105
Kees Cooke99e88a2017-10-16 14:43:17 -07002106 timer_setup(&gsm->t2_timer, gsm_control_retransmit, 0);
Alan Coxe1eaea42010-03-26 11:32:54 +00002107 init_waitqueue_head(&gsm->event);
2108 spin_lock_init(&gsm->control_lock);
2109 spin_lock_init(&gsm->tx_lock);
2110
2111 if (gsm->encoding == 0)
2112 gsm->receive = gsm0_receive;
2113 else
2114 gsm->receive = gsm1_receive;
Alan Coxe1eaea42010-03-26 11:32:54 +00002115
Alan Coxe1eaea42010-03-26 11:32:54 +00002116 dlci = gsm_dlci_alloc(gsm, 0);
2117 if (dlci == NULL)
2118 return -ENOMEM;
Jiri Slaby5677fcf2020-02-19 09:49:46 +01002119 gsm->dead = false; /* Tty opens are now permissible */
Alan Coxe1eaea42010-03-26 11:32:54 +00002120 return 0;
2121}
Alan Coxe1eaea42010-03-26 11:32:54 +00002122
2123/**
2124 * gsm_free_mux - free up a mux
Jiri Slaby724ac072020-08-18 10:56:52 +02002125 * @gsm: mux to free
Alan Coxe1eaea42010-03-26 11:32:54 +00002126 *
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002127 * Dispose of allocated resources for a dead mux
Alan Coxe1eaea42010-03-26 11:32:54 +00002128 */
Rashika Kheria54af5832013-12-16 16:28:24 +05302129static void gsm_free_mux(struct gsm_mux *gsm)
Alan Coxe1eaea42010-03-26 11:32:54 +00002130{
Daniel Starkef26c2712022-04-14 02:42:08 -07002131 int i;
2132
2133 for (i = 0; i < MAX_MUX; i++) {
2134 if (gsm == gsm_mux[i]) {
2135 gsm_mux[i] = NULL;
2136 break;
2137 }
2138 }
2139 mutex_destroy(&gsm->mutex);
Alan Coxe1eaea42010-03-26 11:32:54 +00002140 kfree(gsm->txframe);
2141 kfree(gsm->buf);
2142 kfree(gsm);
2143}
Alan Coxe1eaea42010-03-26 11:32:54 +00002144
2145/**
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002146 * gsm_free_muxr - free up a mux
Jiri Slaby724ac072020-08-18 10:56:52 +02002147 * @ref: kreference to the mux to free
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002148 *
2149 * Dispose of allocated resources for a dead mux
2150 */
2151static void gsm_free_muxr(struct kref *ref)
2152{
2153 struct gsm_mux *gsm = container_of(ref, struct gsm_mux, ref);
2154 gsm_free_mux(gsm);
2155}
2156
2157static inline void mux_get(struct gsm_mux *gsm)
2158{
Daniel Starkef26c2712022-04-14 02:42:08 -07002159 unsigned long flags;
2160
2161 spin_lock_irqsave(&gsm_mux_lock, flags);
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002162 kref_get(&gsm->ref);
Daniel Starkef26c2712022-04-14 02:42:08 -07002163 spin_unlock_irqrestore(&gsm_mux_lock, flags);
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002164}
2165
2166static inline void mux_put(struct gsm_mux *gsm)
2167{
Daniel Starkef26c2712022-04-14 02:42:08 -07002168 unsigned long flags;
2169
2170 spin_lock_irqsave(&gsm_mux_lock, flags);
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002171 kref_put(&gsm->ref, gsm_free_muxr);
Daniel Starkef26c2712022-04-14 02:42:08 -07002172 spin_unlock_irqrestore(&gsm_mux_lock, flags);
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002173}
2174
Martin Hundebøll43a9e712019-07-10 21:26:55 +02002175static inline unsigned int mux_num_to_base(struct gsm_mux *gsm)
2176{
2177 return gsm->num * NUM_DLCI;
2178}
2179
2180static inline unsigned int mux_line_to_num(unsigned int line)
2181{
2182 return line / NUM_DLCI;
2183}
2184
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002185/**
Alan Coxe1eaea42010-03-26 11:32:54 +00002186 * gsm_alloc_mux - allocate a mux
2187 *
2188 * Creates a new mux ready for activation.
2189 */
2190
Rashika Kheria54af5832013-12-16 16:28:24 +05302191static struct gsm_mux *gsm_alloc_mux(void)
Alan Coxe1eaea42010-03-26 11:32:54 +00002192{
Daniel Starkef26c2712022-04-14 02:42:08 -07002193 int i;
Alan Coxe1eaea42010-03-26 11:32:54 +00002194 struct gsm_mux *gsm = kzalloc(sizeof(struct gsm_mux), GFP_KERNEL);
2195 if (gsm == NULL)
2196 return NULL;
2197 gsm->buf = kmalloc(MAX_MRU + 1, GFP_KERNEL);
2198 if (gsm->buf == NULL) {
2199 kfree(gsm);
2200 return NULL;
2201 }
Daniel Starke705925e2022-04-14 02:42:13 -07002202 gsm->txframe = kmalloc(2 * (MAX_MTU + PROT_OVERHEAD - 1), GFP_KERNEL);
Alan Coxe1eaea42010-03-26 11:32:54 +00002203 if (gsm->txframe == NULL) {
2204 kfree(gsm->buf);
2205 kfree(gsm);
2206 return NULL;
2207 }
2208 spin_lock_init(&gsm->lock);
Chao Bidfabf7f2013-11-26 12:09:39 +08002209 mutex_init(&gsm->mutex);
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002210 kref_init(&gsm->ref);
Russ Gorbyb4338e12012-08-13 13:44:59 +01002211 INIT_LIST_HEAD(&gsm->tx_list);
Alan Coxe1eaea42010-03-26 11:32:54 +00002212
2213 gsm->t1 = T1;
2214 gsm->t2 = T2;
2215 gsm->n2 = N2;
2216 gsm->ftype = UIH;
Alan Coxe1eaea42010-03-26 11:32:54 +00002217 gsm->adaption = 1;
2218 gsm->encoding = 1;
2219 gsm->mru = 64; /* Default to encoding 1 so these should be 64 */
2220 gsm->mtu = 64;
Jiri Slaby5677fcf2020-02-19 09:49:46 +01002221 gsm->dead = true; /* Avoid early tty opens */
Alan Coxe1eaea42010-03-26 11:32:54 +00002222
Daniel Starkef26c2712022-04-14 02:42:08 -07002223 /* Store the instance to the mux array or abort if no space is
2224 * available.
2225 */
2226 spin_lock(&gsm_mux_lock);
2227 for (i = 0; i < MAX_MUX; i++) {
2228 if (!gsm_mux[i]) {
2229 gsm_mux[i] = gsm;
2230 gsm->num = i;
2231 break;
2232 }
2233 }
2234 spin_unlock(&gsm_mux_lock);
2235 if (i == MAX_MUX) {
2236 mutex_destroy(&gsm->mutex);
2237 kfree(gsm->txframe);
2238 kfree(gsm->buf);
2239 kfree(gsm);
2240 return NULL;
2241 }
2242
Alan Coxe1eaea42010-03-26 11:32:54 +00002243 return gsm;
2244}
Alan Coxe1eaea42010-03-26 11:32:54 +00002245
Tony Lindgren33841042019-01-13 17:25:26 -08002246static void gsm_copy_config_values(struct gsm_mux *gsm,
2247 struct gsm_config *c)
2248{
2249 memset(c, 0, sizeof(*c));
2250 c->adaption = gsm->adaption;
2251 c->encapsulation = gsm->encoding;
2252 c->initiator = gsm->initiator;
2253 c->t1 = gsm->t1;
2254 c->t2 = gsm->t2;
2255 c->t3 = 0; /* Not supported */
2256 c->n2 = gsm->n2;
2257 if (gsm->ftype == UIH)
2258 c->i = 1;
2259 else
2260 c->i = 2;
2261 pr_debug("Ftype %d i %d\n", gsm->ftype, c->i);
2262 c->mru = gsm->mru;
2263 c->mtu = gsm->mtu;
2264 c->k = 0;
2265}
2266
2267static int gsm_config(struct gsm_mux *gsm, struct gsm_config *c)
2268{
2269 int need_close = 0;
2270 int need_restart = 0;
2271
2272 /* Stuff we don't support yet - UI or I frame transport, windowing */
2273 if ((c->adaption != 1 && c->adaption != 2) || c->k)
2274 return -EOPNOTSUPP;
2275 /* Check the MRU/MTU range looks sane */
2276 if (c->mru > MAX_MRU || c->mtu > MAX_MTU || c->mru < 8 || c->mtu < 8)
2277 return -EINVAL;
Daniel Starke935f3142022-04-14 02:42:16 -07002278 if (c->n2 > 255)
Tony Lindgren33841042019-01-13 17:25:26 -08002279 return -EINVAL;
2280 if (c->encapsulation > 1) /* Basic, advanced, no I */
2281 return -EINVAL;
2282 if (c->initiator > 1)
2283 return -EINVAL;
2284 if (c->i == 0 || c->i > 2) /* UIH and UI only */
2285 return -EINVAL;
2286 /*
2287 * See what is needed for reconfiguration
2288 */
2289
2290 /* Timing fields */
2291 if (c->t1 != 0 && c->t1 != gsm->t1)
2292 need_restart = 1;
2293 if (c->t2 != 0 && c->t2 != gsm->t2)
2294 need_restart = 1;
2295 if (c->encapsulation != gsm->encoding)
2296 need_restart = 1;
2297 if (c->adaption != gsm->adaption)
2298 need_restart = 1;
2299 /* Requires care */
2300 if (c->initiator != gsm->initiator)
2301 need_close = 1;
2302 if (c->mru != gsm->mru)
2303 need_restart = 1;
2304 if (c->mtu != gsm->mtu)
2305 need_restart = 1;
2306
2307 /*
2308 * Close down what is needed, restart and initiate the new
Daniel Starke47132f92022-04-14 02:42:07 -07002309 * configuration. On the first time there is no DLCI[0]
2310 * and closing or cleaning up is not necessary.
Tony Lindgren33841042019-01-13 17:25:26 -08002311 */
Daniel Starke47132f92022-04-14 02:42:07 -07002312 if (need_close || need_restart)
2313 gsm_cleanup_mux(gsm, true);
Tony Lindgren33841042019-01-13 17:25:26 -08002314
2315 gsm->initiator = c->initiator;
2316 gsm->mru = c->mru;
2317 gsm->mtu = c->mtu;
2318 gsm->encoding = c->encapsulation;
2319 gsm->adaption = c->adaption;
2320 gsm->n2 = c->n2;
2321
2322 if (c->i == 1)
2323 gsm->ftype = UIH;
2324 else if (c->i == 2)
2325 gsm->ftype = UI;
2326
2327 if (c->t1)
2328 gsm->t1 = c->t1;
2329 if (c->t2)
2330 gsm->t2 = c->t2;
2331
2332 /*
2333 * FIXME: We need to separate activation/deactivation from adding
2334 * and removing from the mux array
2335 */
2336 if (need_restart)
2337 gsm_activate_mux(gsm);
2338 if (gsm->initiator && need_close)
2339 gsm_dlci_begin_open(gsm->dlci[0]);
2340 return 0;
2341}
2342
Alan Coxe1eaea42010-03-26 11:32:54 +00002343/**
2344 * gsmld_output - write to link
2345 * @gsm: our mux
2346 * @data: bytes to output
2347 * @len: size
2348 *
2349 * Write a block of data from the GSM mux to the data channel. This
2350 * will eventually be serialized from above but at the moment isn't.
2351 */
2352
2353static int gsmld_output(struct gsm_mux *gsm, u8 *data, int len)
2354{
2355 if (tty_write_room(gsm->tty) < len) {
2356 set_bit(TTY_DO_WRITE_WAKEUP, &gsm->tty->flags);
2357 return -ENOSPC;
2358 }
Joe Perches0a77c4f2011-04-25 16:46:49 -07002359 if (debug & 4)
2360 print_hex_dump_bytes("gsmld_output: ", DUMP_PREFIX_OFFSET,
2361 data, len);
Alan Coxe1eaea42010-03-26 11:32:54 +00002362 gsm->tty->ops->write(gsm->tty, data, len);
2363 return len;
2364}
2365
2366/**
2367 * gsmld_attach_gsm - mode set up
2368 * @tty: our tty structure
2369 * @gsm: our mux
2370 *
2371 * Set up the MUX for basic mode and commence connecting to the
2372 * modem. Currently called from the line discipline set up but
2373 * will need moving to an ioctl path.
2374 */
2375
2376static int gsmld_attach_gsm(struct tty_struct *tty, struct gsm_mux *gsm)
2377{
Martin Hundebøll43a9e712019-07-10 21:26:55 +02002378 unsigned int base;
2379 int ret, i;
Alan Coxe1eaea42010-03-26 11:32:54 +00002380
2381 gsm->tty = tty_kref_get(tty);
Alan Coxe1eaea42010-03-26 11:32:54 +00002382 ret = gsm_activate_mux(gsm);
2383 if (ret != 0)
2384 tty_kref_put(gsm->tty);
Russ Gorbyd50f6dc2011-06-14 13:35:32 -07002385 else {
2386 /* Don't register device 0 - this is the control channel and not
2387 a usable tty interface */
Martin Hundebøll43a9e712019-07-10 21:26:55 +02002388 base = mux_num_to_base(gsm); /* Base for this MUX */
Hillf Dantonb549cc72021-04-12 11:57:58 +08002389 for (i = 1; i < NUM_DLCI; i++) {
2390 struct device *dev;
2391
2392 dev = tty_register_device(gsm_tty_driver,
2393 base + i, NULL);
2394 if (IS_ERR(dev)) {
2395 for (i--; i >= 1; i--)
2396 tty_unregister_device(gsm_tty_driver,
2397 base + i);
2398 return PTR_ERR(dev);
2399 }
2400 }
Russ Gorbyd50f6dc2011-06-14 13:35:32 -07002401 }
Alan Coxe1eaea42010-03-26 11:32:54 +00002402 return ret;
2403}
2404
2405
2406/**
2407 * gsmld_detach_gsm - stop doing 0710 mux
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02002408 * @tty: tty attached to the mux
Alan Coxe1eaea42010-03-26 11:32:54 +00002409 * @gsm: mux
2410 *
2411 * Shutdown and then clean up the resources used by the line discipline
2412 */
2413
2414static void gsmld_detach_gsm(struct tty_struct *tty, struct gsm_mux *gsm)
2415{
Martin Hundebøll43a9e712019-07-10 21:26:55 +02002416 unsigned int base = mux_num_to_base(gsm); /* Base for this MUX */
Russ Gorbyd50f6dc2011-06-14 13:35:32 -07002417 int i;
Russ Gorbyd50f6dc2011-06-14 13:35:32 -07002418
Alan Coxe1eaea42010-03-26 11:32:54 +00002419 WARN_ON(tty != gsm->tty);
Russ Gorbyd50f6dc2011-06-14 13:35:32 -07002420 for (i = 1; i < NUM_DLCI; i++)
2421 tty_unregister_device(gsm_tty_driver, base + i);
Alan Coxe1eaea42010-03-26 11:32:54 +00002422 tty_kref_put(gsm->tty);
2423 gsm->tty = NULL;
2424}
2425
Linus Torvalds55db4c62011-06-04 06:33:24 +09002426static void gsmld_receive_buf(struct tty_struct *tty, const unsigned char *cp,
2427 char *fp, int count)
Alan Coxe1eaea42010-03-26 11:32:54 +00002428{
2429 struct gsm_mux *gsm = tty->disc_data;
2430 const unsigned char *dp;
2431 char *f;
2432 int i;
Peter Hurley82f91fe2013-12-02 13:56:03 -05002433 char flags = TTY_NORMAL;
Alan Coxe1eaea42010-03-26 11:32:54 +00002434
Joe Perches0a77c4f2011-04-25 16:46:49 -07002435 if (debug & 4)
2436 print_hex_dump_bytes("gsmld_receive: ", DUMP_PREFIX_OFFSET,
2437 cp, count);
Alan Coxe1eaea42010-03-26 11:32:54 +00002438
2439 for (i = count, dp = cp, f = fp; i; i--, dp++) {
Peter Hurley82f91fe2013-12-02 13:56:03 -05002440 if (f)
2441 flags = *f++;
Alan Coxe1eaea42010-03-26 11:32:54 +00002442 switch (flags) {
2443 case TTY_NORMAL:
2444 gsm->receive(gsm, *dp);
2445 break;
2446 case TTY_OVERRUN:
2447 case TTY_BREAK:
2448 case TTY_PARITY:
2449 case TTY_FRAME:
Jiri Slabya5797672020-08-18 10:56:48 +02002450 gsm_error(gsm, *dp, flags);
Alan Coxe1eaea42010-03-26 11:32:54 +00002451 break;
2452 default:
Frederic Beratc01af4f2012-08-13 13:43:58 +01002453 WARN_ONCE(1, "%s: unknown flag %d\n",
Rasmus Villemoes429b4742015-03-31 15:55:59 +02002454 tty_name(tty), flags);
Alan Coxe1eaea42010-03-26 11:32:54 +00002455 break;
2456 }
2457 }
2458 /* FASYNC if needed ? */
2459 /* If clogged call tty_throttle(tty); */
2460}
2461
2462/**
Alan Coxe1eaea42010-03-26 11:32:54 +00002463 * gsmld_flush_buffer - clean input queue
2464 * @tty: terminal device
2465 *
2466 * Flush the input buffer. Called when the line discipline is
2467 * being closed, when the tty layer wants the buffer flushed (eg
2468 * at hangup).
2469 */
2470
2471static void gsmld_flush_buffer(struct tty_struct *tty)
2472{
2473}
2474
2475/**
2476 * gsmld_close - close the ldisc for this tty
2477 * @tty: device
2478 *
2479 * Called from the terminal layer when this line discipline is
2480 * being shut down, either because of a close or becsuse of a
2481 * discipline change. The function will not be called while other
2482 * ldisc methods are in progress.
2483 */
2484
2485static void gsmld_close(struct tty_struct *tty)
2486{
2487 struct gsm_mux *gsm = tty->disc_data;
2488
Daniel Starke26f127f2022-04-14 02:42:09 -07002489 /* The ldisc locks and closes the port before calling our close. This
2490 * means we have no way to do a proper disconnect. We will not bother
2491 * to do one.
2492 */
2493 gsm_cleanup_mux(gsm, false);
2494
Alan Coxe1eaea42010-03-26 11:32:54 +00002495 gsmld_detach_gsm(tty, gsm);
2496
2497 gsmld_flush_buffer(tty);
2498 /* Do other clean up here */
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002499 mux_put(gsm);
Alan Coxe1eaea42010-03-26 11:32:54 +00002500}
2501
2502/**
2503 * gsmld_open - open an ldisc
2504 * @tty: terminal to open
2505 *
2506 * Called when this line discipline is being attached to the
2507 * terminal device. Can sleep. Called serialized so that no
2508 * other events will occur in parallel. No further open will occur
2509 * until a close.
2510 */
2511
2512static int gsmld_open(struct tty_struct *tty)
2513{
2514 struct gsm_mux *gsm;
xinhui.pan5a640962014-07-28 16:14:52 +08002515 int ret;
Alan Coxe1eaea42010-03-26 11:32:54 +00002516
2517 if (tty->ops->write == NULL)
2518 return -EINVAL;
2519
2520 /* Attach our ldisc data */
2521 gsm = gsm_alloc_mux();
2522 if (gsm == NULL)
2523 return -ENOMEM;
2524
2525 tty->disc_data = gsm;
2526 tty->receive_room = 65536;
2527
2528 /* Attach the initial passive connection */
2529 gsm->encoding = 1;
xinhui.pan5a640962014-07-28 16:14:52 +08002530
2531 ret = gsmld_attach_gsm(tty, gsm);
2532 if (ret != 0) {
Daniel Starke47132f92022-04-14 02:42:07 -07002533 gsm_cleanup_mux(gsm, false);
xinhui.pan5a640962014-07-28 16:14:52 +08002534 mux_put(gsm);
2535 }
2536 return ret;
Alan Coxe1eaea42010-03-26 11:32:54 +00002537}
2538
2539/**
2540 * gsmld_write_wakeup - asynchronous I/O notifier
2541 * @tty: tty device
2542 *
2543 * Required for the ptys, serial driver etc. since processes
2544 * that attach themselves to the master and rely on ASYNC
2545 * IO must be woken up
2546 */
2547
2548static void gsmld_write_wakeup(struct tty_struct *tty)
2549{
2550 struct gsm_mux *gsm = tty->disc_data;
Dan Carpenter328be392010-05-25 11:37:17 +02002551 unsigned long flags;
Alan Coxe1eaea42010-03-26 11:32:54 +00002552
2553 /* Queue poll */
2554 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Russ Gorby5e447082012-08-13 13:44:40 +01002555 spin_lock_irqsave(&gsm->tx_lock, flags);
Gregory CLEMENT01dbb362020-05-12 13:53:23 +02002556 gsm_data_kick(gsm, NULL);
Dan Carpenter328be392010-05-25 11:37:17 +02002557 if (gsm->tx_bytes < TX_THRESH_LO) {
Alan Coxe1eaea42010-03-26 11:32:54 +00002558 gsm_dlci_data_sweep(gsm);
Dan Carpenter328be392010-05-25 11:37:17 +02002559 }
Russ Gorby5e447082012-08-13 13:44:40 +01002560 spin_unlock_irqrestore(&gsm->tx_lock, flags);
Alan Coxe1eaea42010-03-26 11:32:54 +00002561}
2562
2563/**
2564 * gsmld_read - read function for tty
2565 * @tty: tty device
2566 * @file: file object
2567 * @buf: userspace buffer pointer
2568 * @nr: size of I/O
2569 *
2570 * Perform reads for the line discipline. We are guaranteed that the
2571 * line discipline will not be closed under us but we may get multiple
2572 * parallel readers and must handle this ourselves. We may also get
2573 * a hangup. Always called in user context, may sleep.
2574 *
2575 * This code must be sure never to sleep through a hangup.
2576 */
2577
2578static ssize_t gsmld_read(struct tty_struct *tty, struct file *file,
Linus Torvalds279e5452021-01-18 13:31:30 -08002579 unsigned char *buf, size_t nr,
2580 void **cookie, unsigned long offset)
Alan Coxe1eaea42010-03-26 11:32:54 +00002581{
2582 return -EOPNOTSUPP;
2583}
2584
2585/**
2586 * gsmld_write - write function for tty
2587 * @tty: tty device
2588 * @file: file object
2589 * @buf: userspace buffer pointer
2590 * @nr: size of I/O
2591 *
2592 * Called when the owner of the device wants to send a frame
2593 * itself (or some other control data). The data is transferred
2594 * as-is and must be properly framed and checksummed as appropriate
2595 * by userspace. Frames are either sent whole or not at all as this
2596 * avoids pain user side.
2597 */
2598
2599static ssize_t gsmld_write(struct tty_struct *tty, struct file *file,
2600 const unsigned char *buf, size_t nr)
2601{
2602 int space = tty_write_room(tty);
2603 if (space >= nr)
2604 return tty->ops->write(tty, buf, nr);
2605 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
2606 return -ENOBUFS;
2607}
2608
2609/**
2610 * gsmld_poll - poll method for N_GSM0710
2611 * @tty: terminal device
2612 * @file: file accessing it
2613 * @wait: poll table
2614 *
2615 * Called when the line discipline is asked to poll() for data or
2616 * for special events. This code is not serialized with respect to
2617 * other events save open/close.
2618 *
2619 * This code must be sure never to sleep through a hangup.
2620 * Called without the kernel lock held - fine
2621 */
2622
Al Viroafc9a422017-07-03 06:39:46 -04002623static __poll_t gsmld_poll(struct tty_struct *tty, struct file *file,
Alan Coxe1eaea42010-03-26 11:32:54 +00002624 poll_table *wait)
2625{
Al Viroafc9a422017-07-03 06:39:46 -04002626 __poll_t mask = 0;
Alan Coxe1eaea42010-03-26 11:32:54 +00002627 struct gsm_mux *gsm = tty->disc_data;
2628
2629 poll_wait(file, &tty->read_wait, wait);
2630 poll_wait(file, &tty->write_wait, wait);
2631 if (tty_hung_up_p(file))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002632 mask |= EPOLLHUP;
Alan Coxe1eaea42010-03-26 11:32:54 +00002633 if (!tty_is_writelocked(tty) && tty_write_room(tty) > 0)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002634 mask |= EPOLLOUT | EPOLLWRNORM;
Alan Coxe1eaea42010-03-26 11:32:54 +00002635 if (gsm->dead)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002636 mask |= EPOLLHUP;
Alan Coxe1eaea42010-03-26 11:32:54 +00002637 return mask;
2638}
2639
Alan Coxe1eaea42010-03-26 11:32:54 +00002640static int gsmld_ioctl(struct tty_struct *tty, struct file *file,
2641 unsigned int cmd, unsigned long arg)
2642{
2643 struct gsm_config c;
2644 struct gsm_mux *gsm = tty->disc_data;
Martin Hundebølla7b121b2019-08-12 23:12:43 +02002645 unsigned int base;
Alan Coxe1eaea42010-03-26 11:32:54 +00002646
2647 switch (cmd) {
2648 case GSMIOC_GETCONF:
Tony Lindgren33841042019-01-13 17:25:26 -08002649 gsm_copy_config_values(gsm, &c);
Jiri Slabyedd05a72020-02-19 09:49:44 +01002650 if (copy_to_user((void __user *)arg, &c, sizeof(c)))
Alan Coxe1eaea42010-03-26 11:32:54 +00002651 return -EFAULT;
2652 return 0;
2653 case GSMIOC_SETCONF:
Jiri Slabyedd05a72020-02-19 09:49:44 +01002654 if (copy_from_user(&c, (void __user *)arg, sizeof(c)))
Alan Coxe1eaea42010-03-26 11:32:54 +00002655 return -EFAULT;
Tony Lindgren33841042019-01-13 17:25:26 -08002656 return gsm_config(gsm, &c);
Martin Hundebølla7b121b2019-08-12 23:12:43 +02002657 case GSMIOC_GETFIRST:
2658 base = mux_num_to_base(gsm);
2659 return put_user(base + 1, (__u32 __user *)arg);
Alan Coxe1eaea42010-03-26 11:32:54 +00002660 default:
2661 return n_tty_ioctl_helper(tty, file, cmd, arg);
2662 }
2663}
2664
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002665/*
2666 * Network interface
2667 *
2668 */
2669
2670static int gsm_mux_net_open(struct net_device *net)
2671{
2672 pr_debug("%s called\n", __func__);
2673 netif_start_queue(net);
2674 return 0;
2675}
2676
2677static int gsm_mux_net_close(struct net_device *net)
2678{
2679 netif_stop_queue(net);
2680 return 0;
2681}
2682
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002683static void dlci_net_free(struct gsm_dlci *dlci)
2684{
2685 if (!dlci->net) {
2686 WARN_ON(1);
2687 return;
2688 }
2689 dlci->adaption = dlci->prev_adaption;
2690 dlci->data = dlci->prev_data;
2691 free_netdev(dlci->net);
2692 dlci->net = NULL;
2693}
2694static void net_free(struct kref *ref)
2695{
2696 struct gsm_mux_net *mux_net;
2697 struct gsm_dlci *dlci;
2698
2699 mux_net = container_of(ref, struct gsm_mux_net, ref);
2700 dlci = mux_net->dlci;
2701
2702 if (dlci->net) {
2703 unregister_netdev(dlci->net);
2704 dlci_net_free(dlci);
2705 }
2706}
2707
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002708static inline void muxnet_get(struct gsm_mux_net *mux_net)
2709{
2710 kref_get(&mux_net->ref);
2711}
2712
2713static inline void muxnet_put(struct gsm_mux_net *mux_net)
2714{
2715 kref_put(&mux_net->ref, net_free);
2716}
2717
Luc Van Oostenryck2468b3e2018-04-24 15:18:39 +02002718static netdev_tx_t gsm_mux_net_start_xmit(struct sk_buff *skb,
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002719 struct net_device *net)
2720{
Julia Lawall5dbc32a2015-03-29 14:54:13 +02002721 struct gsm_mux_net *mux_net = netdev_priv(net);
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002722 struct gsm_dlci *dlci = mux_net->dlci;
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002723 muxnet_get(mux_net);
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002724
2725 skb_queue_head(&dlci->skb_list, skb);
Tobias Klauser47baf1a2017-03-13 12:00:50 +01002726 net->stats.tx_packets++;
2727 net->stats.tx_bytes += skb->len;
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002728 gsm_dlci_data_kick(dlci);
2729 /* And tell the kernel when the last transmit started. */
Florian Westphal860e9532016-05-03 16:33:13 +02002730 netif_trans_update(net);
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002731 muxnet_put(mux_net);
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002732 return NETDEV_TX_OK;
2733}
2734
2735/* called when a packet did not ack after watchdogtimeout */
Michael S. Tsirkin0290bd22019-12-10 09:23:51 -05002736static void gsm_mux_net_tx_timeout(struct net_device *net, unsigned int txqueue)
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002737{
2738 /* Tell syslog we are hosed. */
2739 dev_dbg(&net->dev, "Tx timed out.\n");
2740
2741 /* Update statistics */
Tobias Klauser47baf1a2017-03-13 12:00:50 +01002742 net->stats.tx_errors++;
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002743}
2744
2745static void gsm_mux_rx_netchar(struct gsm_dlci *dlci,
Tony Lindgren4feb7a42019-01-13 17:25:27 -08002746 const unsigned char *in_buf, int size)
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002747{
2748 struct net_device *net = dlci->net;
2749 struct sk_buff *skb;
Julia Lawall5dbc32a2015-03-29 14:54:13 +02002750 struct gsm_mux_net *mux_net = netdev_priv(net);
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002751 muxnet_get(mux_net);
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002752
2753 /* Allocate an sk_buff */
2754 skb = dev_alloc_skb(size + NET_IP_ALIGN);
2755 if (!skb) {
2756 /* We got no receive buffer. */
Tobias Klauser47baf1a2017-03-13 12:00:50 +01002757 net->stats.rx_dropped++;
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002758 muxnet_put(mux_net);
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002759 return;
2760 }
2761 skb_reserve(skb, NET_IP_ALIGN);
Johannes Berg59ae1d12017-06-16 14:29:20 +02002762 skb_put_data(skb, in_buf, size);
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002763
2764 skb->dev = net;
Vaishali Thakkar75406b32015-06-06 06:05:24 +05302765 skb->protocol = htons(ETH_P_IP);
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002766
2767 /* Ship it off to the kernel */
2768 netif_rx(skb);
2769
2770 /* update out statistics */
Tobias Klauser47baf1a2017-03-13 12:00:50 +01002771 net->stats.rx_packets++;
2772 net->stats.rx_bytes += size;
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002773 muxnet_put(mux_net);
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002774 return;
2775}
2776
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002777static void gsm_mux_net_init(struct net_device *net)
2778{
2779 static const struct net_device_ops gsm_netdev_ops = {
2780 .ndo_open = gsm_mux_net_open,
2781 .ndo_stop = gsm_mux_net_close,
2782 .ndo_start_xmit = gsm_mux_net_start_xmit,
2783 .ndo_tx_timeout = gsm_mux_net_tx_timeout,
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002784 };
2785
2786 net->netdev_ops = &gsm_netdev_ops;
2787
2788 /* fill in the other fields */
2789 net->watchdog_timeo = GSM_NET_TX_TIMEOUT;
2790 net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2791 net->type = ARPHRD_NONE;
2792 net->tx_queue_len = 10;
2793}
2794
2795
2796/* caller holds the dlci mutex */
2797static void gsm_destroy_network(struct gsm_dlci *dlci)
2798{
2799 struct gsm_mux_net *mux_net;
2800
Jiri Slabyd8ca4ec2020-02-19 09:49:45 +01002801 pr_debug("destroy network interface\n");
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002802 if (!dlci->net)
2803 return;
Julia Lawall5dbc32a2015-03-29 14:54:13 +02002804 mux_net = netdev_priv(dlci->net);
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002805 muxnet_put(mux_net);
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002806}
2807
2808
2809/* caller holds the dlci mutex */
2810static int gsm_create_network(struct gsm_dlci *dlci, struct gsm_netconfig *nc)
2811{
2812 char *netname;
2813 int retval = 0;
2814 struct net_device *net;
2815 struct gsm_mux_net *mux_net;
2816
2817 if (!capable(CAP_NET_ADMIN))
2818 return -EPERM;
2819
2820 /* Already in a non tty mode */
2821 if (dlci->adaption > 2)
2822 return -EBUSY;
2823
2824 if (nc->protocol != htons(ETH_P_IP))
2825 return -EPROTONOSUPPORT;
2826
2827 if (nc->adaption != 3 && nc->adaption != 4)
2828 return -EPROTONOSUPPORT;
2829
Jiri Slabyd8ca4ec2020-02-19 09:49:45 +01002830 pr_debug("create network interface\n");
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002831
2832 netname = "gsm%d";
2833 if (nc->if_name[0] != '\0')
2834 netname = nc->if_name;
Tom Gundersenc835a672014-07-14 16:37:24 +02002835 net = alloc_netdev(sizeof(struct gsm_mux_net), netname,
2836 NET_NAME_UNKNOWN, gsm_mux_net_init);
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002837 if (!net) {
Jiri Slabyd8ca4ec2020-02-19 09:49:45 +01002838 pr_err("alloc_netdev failed\n");
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002839 return -ENOMEM;
2840 }
2841 net->mtu = dlci->gsm->mtu;
Jarod Wilson9c22b4a2016-10-20 13:55:18 -04002842 net->min_mtu = 8;
2843 net->max_mtu = dlci->gsm->mtu;
Julia Lawall5dbc32a2015-03-29 14:54:13 +02002844 mux_net = netdev_priv(net);
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002845 mux_net->dlci = dlci;
2846 kref_init(&mux_net->ref);
2847 strncpy(nc->if_name, net->name, IFNAMSIZ); /* return net name */
2848
2849 /* reconfigure dlci for network */
2850 dlci->prev_adaption = dlci->adaption;
2851 dlci->prev_data = dlci->data;
2852 dlci->adaption = nc->adaption;
2853 dlci->data = gsm_mux_rx_netchar;
2854 dlci->net = net;
2855
Jiri Slabyd8ca4ec2020-02-19 09:49:45 +01002856 pr_debug("register netdev\n");
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002857 retval = register_netdev(net);
2858 if (retval) {
2859 pr_err("network register fail %d\n", retval);
2860 dlci_net_free(dlci);
2861 return retval;
2862 }
2863 return net->ifindex; /* return network index */
2864}
Alan Coxe1eaea42010-03-26 11:32:54 +00002865
2866/* Line discipline for real tty */
Lad, Prabhakard3157b22015-02-04 18:23:59 +00002867static struct tty_ldisc_ops tty_ldisc_packet = {
Alan Coxe1eaea42010-03-26 11:32:54 +00002868 .owner = THIS_MODULE,
2869 .magic = TTY_LDISC_MAGIC,
2870 .name = "n_gsm",
2871 .open = gsmld_open,
2872 .close = gsmld_close,
2873 .flush_buffer = gsmld_flush_buffer,
Alan Coxe1eaea42010-03-26 11:32:54 +00002874 .read = gsmld_read,
2875 .write = gsmld_write,
2876 .ioctl = gsmld_ioctl,
2877 .poll = gsmld_poll,
2878 .receive_buf = gsmld_receive_buf,
2879 .write_wakeup = gsmld_write_wakeup
2880};
2881
2882/*
2883 * Virtual tty side
2884 */
2885
2886#define TX_SIZE 512
2887
2888static int gsmtty_modem_update(struct gsm_dlci *dlci, u8 brk)
2889{
Daniel Starke320a24c2022-04-14 02:42:17 -07002890 u8 modembits[3];
Alan Coxe1eaea42010-03-26 11:32:54 +00002891 struct gsm_control *ctrl;
2892 int len = 2;
2893
Daniel Starke320a24c2022-04-14 02:42:17 -07002894 modembits[0] = (dlci->addr << 2) | 2 | EA; /* DLCI, Valid, EA */
2895 modembits[1] = (gsm_encode_modem(dlci) << 1) | EA;
2896 if (brk) {
2897 modembits[2] = (brk << 4) | 2 | EA; /* Length, Break, EA */
Alan Coxe1eaea42010-03-26 11:32:54 +00002898 len++;
Daniel Starke320a24c2022-04-14 02:42:17 -07002899 }
2900 ctrl = gsm_control_send(dlci->gsm, CMD_MSC, modembits, len);
Alan Coxe1eaea42010-03-26 11:32:54 +00002901 if (ctrl == NULL)
2902 return -ENOMEM;
2903 return gsm_control_wait(dlci->gsm, ctrl);
2904}
2905
2906static int gsm_carrier_raised(struct tty_port *port)
2907{
2908 struct gsm_dlci *dlci = container_of(port, struct gsm_dlci, port);
Tony Lindgrenb2d89ad92018-04-07 10:19:51 -07002909 struct gsm_mux *gsm = dlci->gsm;
2910
Alan Coxe1eaea42010-03-26 11:32:54 +00002911 /* Not yet open so no carrier info */
2912 if (dlci->state != DLCI_OPEN)
2913 return 0;
2914 if (debug & 2)
2915 return 1;
Tony Lindgrenb2d89ad92018-04-07 10:19:51 -07002916
2917 /*
2918 * Basic mode with control channel in ADM mode may not respond
2919 * to CMD_MSC at all and modem_rx is empty.
2920 */
2921 if (gsm->encoding == 0 && gsm->dlci[0]->mode == DLCI_MODE_ADM &&
2922 !dlci->modem_rx)
2923 return 1;
2924
Alan Coxe1eaea42010-03-26 11:32:54 +00002925 return dlci->modem_rx & TIOCM_CD;
2926}
2927
2928static void gsm_dtr_rts(struct tty_port *port, int onoff)
2929{
2930 struct gsm_dlci *dlci = container_of(port, struct gsm_dlci, port);
2931 unsigned int modem_tx = dlci->modem_tx;
2932 if (onoff)
2933 modem_tx |= TIOCM_DTR | TIOCM_RTS;
2934 else
2935 modem_tx &= ~(TIOCM_DTR | TIOCM_RTS);
2936 if (modem_tx != dlci->modem_tx) {
2937 dlci->modem_tx = modem_tx;
2938 gsmtty_modem_update(dlci, 0);
2939 }
2940}
2941
2942static const struct tty_port_operations gsm_port_ops = {
2943 .carrier_raised = gsm_carrier_raised,
2944 .dtr_rts = gsm_dtr_rts,
Jiri Slaby9a8e62b2012-11-15 09:49:53 +01002945 .destruct = gsm_dlci_free,
Alan Coxe1eaea42010-03-26 11:32:54 +00002946};
2947
Jiri Slaby86176ed2012-08-07 21:47:28 +02002948static int gsmtty_install(struct tty_driver *driver, struct tty_struct *tty)
Alan Coxe1eaea42010-03-26 11:32:54 +00002949{
2950 struct gsm_mux *gsm;
2951 struct gsm_dlci *dlci;
Alan Coxe1eaea42010-03-26 11:32:54 +00002952 unsigned int line = tty->index;
Martin Hundebøll43a9e712019-07-10 21:26:55 +02002953 unsigned int mux = mux_line_to_num(line);
Jiri Slaby86176ed2012-08-07 21:47:28 +02002954 bool alloc = false;
2955 int ret;
Alan Coxe1eaea42010-03-26 11:32:54 +00002956
2957 line = line & 0x3F;
2958
2959 if (mux >= MAX_MUX)
2960 return -ENXIO;
2961 /* FIXME: we need to lock gsm_mux for lifetimes of ttys eventually */
2962 if (gsm_mux[mux] == NULL)
2963 return -EUNATCH;
2964 if (line == 0 || line > 61) /* 62/63 reserved */
2965 return -ECHRNG;
2966 gsm = gsm_mux[mux];
2967 if (gsm->dead)
2968 return -EL2HLT;
Aldo Iljazif3c909b2013-07-08 22:28:00 +03002969 /* If DLCI 0 is not yet fully open return an error.
2970 This is ok from a locking
2971 perspective as we don't have to worry about this
2972 if DLCI0 is lost */
Chao Bidfabf7f2013-11-26 12:09:39 +08002973 mutex_lock(&gsm->mutex);
2974 if (gsm->dlci[0] && gsm->dlci[0]->state != DLCI_OPEN) {
2975 mutex_unlock(&gsm->mutex);
xiaojin7e8ac7b2012-08-13 13:43:15 +01002976 return -EL2NSYNC;
Chao Bidfabf7f2013-11-26 12:09:39 +08002977 }
Alan Coxe1eaea42010-03-26 11:32:54 +00002978 dlci = gsm->dlci[line];
Jiri Slaby86176ed2012-08-07 21:47:28 +02002979 if (dlci == NULL) {
2980 alloc = true;
Alan Coxe1eaea42010-03-26 11:32:54 +00002981 dlci = gsm_dlci_alloc(gsm, line);
Jiri Slaby86176ed2012-08-07 21:47:28 +02002982 }
Chao Bidfabf7f2013-11-26 12:09:39 +08002983 if (dlci == NULL) {
2984 mutex_unlock(&gsm->mutex);
Alan Coxe1eaea42010-03-26 11:32:54 +00002985 return -ENOMEM;
Chao Bidfabf7f2013-11-26 12:09:39 +08002986 }
Jiri Slaby86176ed2012-08-07 21:47:28 +02002987 ret = tty_port_install(&dlci->port, driver, tty);
2988 if (ret) {
2989 if (alloc)
2990 dlci_put(dlci);
Chao Bidfabf7f2013-11-26 12:09:39 +08002991 mutex_unlock(&gsm->mutex);
Jiri Slaby86176ed2012-08-07 21:47:28 +02002992 return ret;
2993 }
2994
Chao Bidfabf7f2013-11-26 12:09:39 +08002995 dlci_get(dlci);
2996 dlci_get(gsm->dlci[0]);
2997 mux_get(gsm);
Alan Coxe1eaea42010-03-26 11:32:54 +00002998 tty->driver_data = dlci;
Chao Bidfabf7f2013-11-26 12:09:39 +08002999 mutex_unlock(&gsm->mutex);
Jiri Slaby86176ed2012-08-07 21:47:28 +02003000
3001 return 0;
3002}
3003
3004static int gsmtty_open(struct tty_struct *tty, struct file *filp)
3005{
3006 struct gsm_dlci *dlci = tty->driver_data;
3007 struct tty_port *port = &dlci->port;
3008
3009 port->count++;
Alan Coxe1eaea42010-03-26 11:32:54 +00003010 tty_port_tty_set(port, tty);
3011
3012 dlci->modem_rx = 0;
3013 /* We could in theory open and close before we wait - eg if we get
3014 a DM straight back. This is ok as that will have caused a hangup */
Peter Hurleyd41861c2016-04-09 17:53:25 -07003015 tty_port_set_initialized(port, 1);
Alan Coxe1eaea42010-03-26 11:32:54 +00003016 /* Start sending off SABM messages */
3017 gsm_dlci_begin_open(dlci);
3018 /* And wait for virtual carrier */
3019 return tty_port_block_til_ready(port, tty, filp);
3020}
3021
3022static void gsmtty_close(struct tty_struct *tty, struct file *filp)
3023{
3024 struct gsm_dlci *dlci = tty->driver_data;
Russ Gorby6ab8fba2011-06-16 14:20:13 -07003025
Alan Coxe1eaea42010-03-26 11:32:54 +00003026 if (dlci == NULL)
3027 return;
Dirkjan Bussink4d9b1092013-01-30 11:44:50 +01003028 if (dlci->state == DLCI_CLOSED)
3029 return;
Russ Gorbybcd5abe2011-06-16 14:20:12 -07003030 mutex_lock(&dlci->mutex);
3031 gsm_destroy_network(dlci);
3032 mutex_unlock(&dlci->mutex);
Alan Coxe1eaea42010-03-26 11:32:54 +00003033 if (tty_port_close_start(&dlci->port, tty, filp) == 0)
Chao Bidfabf7f2013-11-26 12:09:39 +08003034 return;
Alan Coxe1eaea42010-03-26 11:32:54 +00003035 gsm_dlci_begin_close(dlci);
Peter Hurleyd41861c2016-04-09 17:53:25 -07003036 if (tty_port_initialized(&dlci->port) && C_HUPCL(tty))
3037 tty_port_lower_dtr_rts(&dlci->port);
Alan Coxe1eaea42010-03-26 11:32:54 +00003038 tty_port_close_end(&dlci->port, tty);
3039 tty_port_tty_set(&dlci->port, NULL);
Chao Bidfabf7f2013-11-26 12:09:39 +08003040 return;
Alan Coxe1eaea42010-03-26 11:32:54 +00003041}
3042
3043static void gsmtty_hangup(struct tty_struct *tty)
3044{
3045 struct gsm_dlci *dlci = tty->driver_data;
Dirkjan Bussink4d9b1092013-01-30 11:44:50 +01003046 if (dlci->state == DLCI_CLOSED)
3047 return;
Alan Coxe1eaea42010-03-26 11:32:54 +00003048 tty_port_hangup(&dlci->port);
3049 gsm_dlci_begin_close(dlci);
3050}
3051
3052static int gsmtty_write(struct tty_struct *tty, const unsigned char *buf,
3053 int len)
3054{
Dirkjan Bussink4d9b1092013-01-30 11:44:50 +01003055 int sent;
Alan Coxe1eaea42010-03-26 11:32:54 +00003056 struct gsm_dlci *dlci = tty->driver_data;
Dirkjan Bussink4d9b1092013-01-30 11:44:50 +01003057 if (dlci->state == DLCI_CLOSED)
3058 return -EINVAL;
Alan Coxe1eaea42010-03-26 11:32:54 +00003059 /* Stuff the bytes into the fifo queue */
Jiri Slaby036bca1fc2020-02-19 09:49:40 +01003060 sent = kfifo_in_locked(&dlci->fifo, buf, len, &dlci->lock);
Alan Coxe1eaea42010-03-26 11:32:54 +00003061 /* Need to kick the channel */
3062 gsm_dlci_data_kick(dlci);
3063 return sent;
3064}
3065
3066static int gsmtty_write_room(struct tty_struct *tty)
3067{
3068 struct gsm_dlci *dlci = tty->driver_data;
Dirkjan Bussink4d9b1092013-01-30 11:44:50 +01003069 if (dlci->state == DLCI_CLOSED)
3070 return -EINVAL;
Jiri Slaby036bca1fc2020-02-19 09:49:40 +01003071 return TX_SIZE - kfifo_len(&dlci->fifo);
Alan Coxe1eaea42010-03-26 11:32:54 +00003072}
3073
3074static int gsmtty_chars_in_buffer(struct tty_struct *tty)
3075{
3076 struct gsm_dlci *dlci = tty->driver_data;
Dirkjan Bussink4d9b1092013-01-30 11:44:50 +01003077 if (dlci->state == DLCI_CLOSED)
3078 return -EINVAL;
Jiri Slaby036bca1fc2020-02-19 09:49:40 +01003079 return kfifo_len(&dlci->fifo);
Alan Coxe1eaea42010-03-26 11:32:54 +00003080}
3081
3082static void gsmtty_flush_buffer(struct tty_struct *tty)
3083{
3084 struct gsm_dlci *dlci = tty->driver_data;
Daniel Starke70b045d2022-04-14 02:42:22 -07003085 unsigned long flags;
3086
Dirkjan Bussink4d9b1092013-01-30 11:44:50 +01003087 if (dlci->state == DLCI_CLOSED)
3088 return;
Alan Coxe1eaea42010-03-26 11:32:54 +00003089 /* Caution needed: If we implement reliable transport classes
3090 then the data being transmitted can't simply be junked once
3091 it has first hit the stack. Until then we can just blow it
3092 away */
Daniel Starke70b045d2022-04-14 02:42:22 -07003093 spin_lock_irqsave(&dlci->lock, flags);
Jiri Slaby036bca1fc2020-02-19 09:49:40 +01003094 kfifo_reset(&dlci->fifo);
Daniel Starke70b045d2022-04-14 02:42:22 -07003095 spin_unlock_irqrestore(&dlci->lock, flags);
Alan Coxe1eaea42010-03-26 11:32:54 +00003096 /* Need to unhook this DLCI from the transmit queue logic */
3097}
3098
3099static void gsmtty_wait_until_sent(struct tty_struct *tty, int timeout)
3100{
3101 /* The FIFO handles the queue so the kernel will do the right
3102 thing waiting on chars_in_buffer before calling us. No work
3103 to do here */
3104}
3105
Alan Cox60b33c12011-02-14 16:26:14 +00003106static int gsmtty_tiocmget(struct tty_struct *tty)
Alan Coxe1eaea42010-03-26 11:32:54 +00003107{
3108 struct gsm_dlci *dlci = tty->driver_data;
Dirkjan Bussink4d9b1092013-01-30 11:44:50 +01003109 if (dlci->state == DLCI_CLOSED)
3110 return -EINVAL;
Alan Coxe1eaea42010-03-26 11:32:54 +00003111 return dlci->modem_rx;
3112}
3113
Alan Cox20b9d172011-02-14 16:26:50 +00003114static int gsmtty_tiocmset(struct tty_struct *tty,
Alan Coxe1eaea42010-03-26 11:32:54 +00003115 unsigned int set, unsigned int clear)
3116{
3117 struct gsm_dlci *dlci = tty->driver_data;
3118 unsigned int modem_tx = dlci->modem_tx;
3119
Dirkjan Bussink4d9b1092013-01-30 11:44:50 +01003120 if (dlci->state == DLCI_CLOSED)
3121 return -EINVAL;
Nikola Diklic-Perincf168072011-09-23 10:59:43 +02003122 modem_tx &= ~clear;
Alan Coxe1eaea42010-03-26 11:32:54 +00003123 modem_tx |= set;
3124
3125 if (modem_tx != dlci->modem_tx) {
3126 dlci->modem_tx = modem_tx;
3127 return gsmtty_modem_update(dlci, 0);
3128 }
3129 return 0;
3130}
3131
3132
Alan Cox6caa76b2011-02-14 16:27:22 +00003133static int gsmtty_ioctl(struct tty_struct *tty,
Alan Coxe1eaea42010-03-26 11:32:54 +00003134 unsigned int cmd, unsigned long arg)
3135{
Russ Gorbybcd5abe2011-06-16 14:20:12 -07003136 struct gsm_dlci *dlci = tty->driver_data;
3137 struct gsm_netconfig nc;
3138 int index;
3139
Dirkjan Bussink4d9b1092013-01-30 11:44:50 +01003140 if (dlci->state == DLCI_CLOSED)
3141 return -EINVAL;
Russ Gorbybcd5abe2011-06-16 14:20:12 -07003142 switch (cmd) {
3143 case GSMIOC_ENABLE_NET:
3144 if (copy_from_user(&nc, (void __user *)arg, sizeof(nc)))
3145 return -EFAULT;
3146 nc.if_name[IFNAMSIZ-1] = '\0';
3147 /* return net interface index or error code */
3148 mutex_lock(&dlci->mutex);
3149 index = gsm_create_network(dlci, &nc);
3150 mutex_unlock(&dlci->mutex);
3151 if (copy_to_user((void __user *)arg, &nc, sizeof(nc)))
3152 return -EFAULT;
3153 return index;
3154 case GSMIOC_DISABLE_NET:
3155 if (!capable(CAP_NET_ADMIN))
3156 return -EPERM;
3157 mutex_lock(&dlci->mutex);
3158 gsm_destroy_network(dlci);
3159 mutex_unlock(&dlci->mutex);
3160 return 0;
3161 default:
3162 return -ENOIOCTLCMD;
3163 }
Alan Coxe1eaea42010-03-26 11:32:54 +00003164}
3165
3166static void gsmtty_set_termios(struct tty_struct *tty, struct ktermios *old)
3167{
Dirkjan Bussink4d9b1092013-01-30 11:44:50 +01003168 struct gsm_dlci *dlci = tty->driver_data;
3169 if (dlci->state == DLCI_CLOSED)
3170 return;
Alan Coxe1eaea42010-03-26 11:32:54 +00003171 /* For the moment its fixed. In actual fact the speed information
3172 for the virtual channel can be propogated in both directions by
3173 the RPN control message. This however rapidly gets nasty as we
3174 then have to remap modem signals each way according to whether
3175 our virtual cable is null modem etc .. */
Alan Coxadc8d742012-07-14 15:31:47 +01003176 tty_termios_copy_hw(&tty->termios, old);
Alan Coxe1eaea42010-03-26 11:32:54 +00003177}
3178
3179static void gsmtty_throttle(struct tty_struct *tty)
3180{
3181 struct gsm_dlci *dlci = tty->driver_data;
Dirkjan Bussink4d9b1092013-01-30 11:44:50 +01003182 if (dlci->state == DLCI_CLOSED)
3183 return;
Peter Hurley9db276f2016-01-10 20:36:15 -08003184 if (C_CRTSCTS(tty))
daniel.starke@siemens.come4c8cb92022-02-17 23:31:21 -08003185 dlci->modem_tx &= ~TIOCM_RTS;
Jiri Slabye9360b92020-02-19 09:49:47 +01003186 dlci->throttled = true;
daniel.starke@siemens.come4c8cb92022-02-17 23:31:21 -08003187 /* Send an MSC with RTS cleared */
Alan Coxe1eaea42010-03-26 11:32:54 +00003188 gsmtty_modem_update(dlci, 0);
3189}
3190
3191static void gsmtty_unthrottle(struct tty_struct *tty)
3192{
3193 struct gsm_dlci *dlci = tty->driver_data;
Dirkjan Bussink4d9b1092013-01-30 11:44:50 +01003194 if (dlci->state == DLCI_CLOSED)
3195 return;
Peter Hurley9db276f2016-01-10 20:36:15 -08003196 if (C_CRTSCTS(tty))
daniel.starke@siemens.come4c8cb92022-02-17 23:31:21 -08003197 dlci->modem_tx |= TIOCM_RTS;
Jiri Slabye9360b92020-02-19 09:49:47 +01003198 dlci->throttled = false;
daniel.starke@siemens.come4c8cb92022-02-17 23:31:21 -08003199 /* Send an MSC with RTS set */
Alan Coxe1eaea42010-03-26 11:32:54 +00003200 gsmtty_modem_update(dlci, 0);
3201}
3202
3203static int gsmtty_break_ctl(struct tty_struct *tty, int state)
3204{
3205 struct gsm_dlci *dlci = tty->driver_data;
3206 int encode = 0; /* Off */
Dirkjan Bussink4d9b1092013-01-30 11:44:50 +01003207 if (dlci->state == DLCI_CLOSED)
3208 return -EINVAL;
Alan Coxe1eaea42010-03-26 11:32:54 +00003209
3210 if (state == -1) /* "On indefinitely" - we can't encode this
3211 properly */
3212 encode = 0x0F;
3213 else if (state > 0) {
3214 encode = state / 200; /* mS to encoding */
3215 if (encode > 0x0F)
3216 encode = 0x0F; /* Best effort */
3217 }
3218 return gsmtty_modem_update(dlci, encode);
3219}
3220
Pan Xinhui8f9cfee2015-03-28 10:42:56 +08003221static void gsmtty_cleanup(struct tty_struct *tty)
Chao Bidfabf7f2013-11-26 12:09:39 +08003222{
3223 struct gsm_dlci *dlci = tty->driver_data;
3224 struct gsm_mux *gsm = dlci->gsm;
3225
3226 dlci_put(dlci);
3227 dlci_put(gsm->dlci[0]);
3228 mux_put(gsm);
Chao Bidfabf7f2013-11-26 12:09:39 +08003229}
Alan Coxe1eaea42010-03-26 11:32:54 +00003230
3231/* Virtual ttys for the demux */
3232static const struct tty_operations gsmtty_ops = {
Jiri Slaby86176ed2012-08-07 21:47:28 +02003233 .install = gsmtty_install,
Alan Coxe1eaea42010-03-26 11:32:54 +00003234 .open = gsmtty_open,
3235 .close = gsmtty_close,
3236 .write = gsmtty_write,
3237 .write_room = gsmtty_write_room,
3238 .chars_in_buffer = gsmtty_chars_in_buffer,
3239 .flush_buffer = gsmtty_flush_buffer,
3240 .ioctl = gsmtty_ioctl,
3241 .throttle = gsmtty_throttle,
3242 .unthrottle = gsmtty_unthrottle,
3243 .set_termios = gsmtty_set_termios,
3244 .hangup = gsmtty_hangup,
3245 .wait_until_sent = gsmtty_wait_until_sent,
3246 .tiocmget = gsmtty_tiocmget,
3247 .tiocmset = gsmtty_tiocmset,
3248 .break_ctl = gsmtty_break_ctl,
Pan Xinhui8f9cfee2015-03-28 10:42:56 +08003249 .cleanup = gsmtty_cleanup,
Alan Coxe1eaea42010-03-26 11:32:54 +00003250};
3251
3252
3253
3254static int __init gsm_init(void)
3255{
3256 /* Fill in our line protocol discipline, and register it */
3257 int status = tty_register_ldisc(N_GSM0710, &tty_ldisc_packet);
3258 if (status != 0) {
Alan Cox5f9a31d2010-11-04 15:17:27 +00003259 pr_err("n_gsm: can't register line discipline (err = %d)\n",
3260 status);
Alan Coxe1eaea42010-03-26 11:32:54 +00003261 return status;
3262 }
3263
3264 gsm_tty_driver = alloc_tty_driver(256);
3265 if (!gsm_tty_driver) {
3266 tty_unregister_ldisc(N_GSM0710);
Alan Cox5f9a31d2010-11-04 15:17:27 +00003267 pr_err("gsm_init: tty allocation failed.\n");
Alan Coxe1eaea42010-03-26 11:32:54 +00003268 return -EINVAL;
3269 }
Alan Coxe1eaea42010-03-26 11:32:54 +00003270 gsm_tty_driver->driver_name = "gsmtty";
3271 gsm_tty_driver->name = "gsmtty";
3272 gsm_tty_driver->major = 0; /* Dynamic */
3273 gsm_tty_driver->minor_start = 0;
3274 gsm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
3275 gsm_tty_driver->subtype = SERIAL_TYPE_NORMAL;
3276 gsm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV
Alan Cox5f9a31d2010-11-04 15:17:27 +00003277 | TTY_DRIVER_HARDWARE_BREAK;
Alan Coxe1eaea42010-03-26 11:32:54 +00003278 gsm_tty_driver->init_termios = tty_std_termios;
3279 /* Fixme */
3280 gsm_tty_driver->init_termios.c_lflag &= ~ECHO;
3281 tty_set_operations(gsm_tty_driver, &gsmtty_ops);
3282
3283 spin_lock_init(&gsm_mux_lock);
3284
3285 if (tty_register_driver(gsm_tty_driver)) {
3286 put_tty_driver(gsm_tty_driver);
3287 tty_unregister_ldisc(N_GSM0710);
Alan Cox5f9a31d2010-11-04 15:17:27 +00003288 pr_err("gsm_init: tty registration failed.\n");
Alan Coxe1eaea42010-03-26 11:32:54 +00003289 return -EBUSY;
3290 }
Alan Cox5f9a31d2010-11-04 15:17:27 +00003291 pr_debug("gsm_init: loaded as %d,%d.\n",
3292 gsm_tty_driver->major, gsm_tty_driver->minor_start);
Alan Coxe1eaea42010-03-26 11:32:54 +00003293 return 0;
3294}
3295
3296static void __exit gsm_exit(void)
3297{
3298 int status = tty_unregister_ldisc(N_GSM0710);
3299 if (status != 0)
Alan Cox5f9a31d2010-11-04 15:17:27 +00003300 pr_err("n_gsm: can't unregister line discipline (err = %d)\n",
3301 status);
Alan Coxe1eaea42010-03-26 11:32:54 +00003302 tty_unregister_driver(gsm_tty_driver);
3303 put_tty_driver(gsm_tty_driver);
Alan Coxe1eaea42010-03-26 11:32:54 +00003304}
3305
3306module_init(gsm_init);
3307module_exit(gsm_exit);
3308
3309
3310MODULE_LICENSE("GPL");
3311MODULE_ALIAS_LDISC(N_GSM0710);