blob: 1d0a140027fffdc014ddc7516c68bae8fe903528 [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
75#define GSM_NET_TX_TIMEOUT (HZ*10)
76
77/**
78 * struct gsm_mux_net - network interface
79 * @struct gsm_dlci* dlci
Russ Gorbybcd5abe2011-06-16 14:20:12 -070080 *
81 * Created when net interface is initialized.
82 **/
83struct gsm_mux_net {
84 struct kref ref;
85 struct gsm_dlci *dlci;
Russ Gorbybcd5abe2011-06-16 14:20:12 -070086};
87
Alan Coxe1eaea42010-03-26 11:32:54 +000088/*
89 * Each block of data we have queued to go out is in the form of
Lucas De Marchi25985ed2011-03-30 22:57:33 -030090 * a gsm_msg which holds everything we need in a link layer independent
Alan Coxe1eaea42010-03-26 11:32:54 +000091 * format
92 */
93
94struct gsm_msg {
Russ Gorbyb4338e12012-08-13 13:44:59 +010095 struct list_head list;
Alan Coxe1eaea42010-03-26 11:32:54 +000096 u8 addr; /* DLCI address + flags */
97 u8 ctrl; /* Control byte + flags */
98 unsigned int len; /* Length of data block (can be zero) */
99 unsigned char *data; /* Points into buffer but not at the start */
Gustavo A. R. Silva2f202d02020-02-12 13:35:23 -0600100 unsigned char buffer[];
Alan Coxe1eaea42010-03-26 11:32:54 +0000101};
102
Jiri Slaby72ae8cc2020-02-19 09:49:41 +0100103enum gsm_dlci_state {
104 DLCI_CLOSED,
105 DLCI_OPENING, /* Sending SABM not seen UA */
106 DLCI_OPEN, /* SABM/UA complete */
107 DLCI_CLOSING, /* Sending DISC not seen UA/DM */
108};
109
Alan Coxe1eaea42010-03-26 11:32:54 +0000110/*
111 * Each active data link has a gsm_dlci structure associated which ties
112 * the link layer to an optional tty (if the tty side is open). To avoid
113 * complexity right now these are only ever freed up when the mux is
114 * shut down.
115 *
116 * At the moment we don't free DLCI objects until the mux is torn down
117 * this avoid object life time issues but might be worth review later.
118 */
119
120struct gsm_dlci {
121 struct gsm_mux *gsm;
122 int addr;
Jiri Slaby72ae8cc2020-02-19 09:49:41 +0100123 enum gsm_dlci_state state;
Russ Gorbybcd5abe2011-06-16 14:20:12 -0700124 struct mutex mutex;
Alan Coxe1eaea42010-03-26 11:32:54 +0000125
126 /* Link layer */
Tony Lindgrene9ec2252018-04-07 10:19:50 -0700127 int mode;
128#define DLCI_MODE_ABM 0 /* Normal Asynchronous Balanced Mode */
129#define DLCI_MODE_ADM 1 /* Asynchronous Disconnected Mode */
Alan Coxe1eaea42010-03-26 11:32:54 +0000130 spinlock_t lock; /* Protects the internal state */
131 struct timer_list t1; /* Retransmit timer for SABM and UA */
132 int retries;
133 /* Uplink tty if active */
134 struct tty_port port; /* The tty bound to this DLCI if there is one */
Jiri Slaby036bca1fc2020-02-19 09:49:40 +0100135 struct kfifo fifo; /* Queue fifo for the DLCI */
Alan Coxe1eaea42010-03-26 11:32:54 +0000136 int adaption; /* Adaption layer in use */
Russ Gorbybcd5abe2011-06-16 14:20:12 -0700137 int prev_adaption;
Alan Coxe1eaea42010-03-26 11:32:54 +0000138 u32 modem_rx; /* Our incoming virtual modem lines */
139 u32 modem_tx; /* Our outgoing modem lines */
140 int dead; /* Refuse re-open */
141 /* Flow control */
142 int throttled; /* Private copy of throttle state */
143 int constipated; /* Throttle status for outgoing */
144 /* Packetised I/O */
145 struct sk_buff *skb; /* Frame being sent */
146 struct sk_buff_head skb_list; /* Queued frames */
147 /* Data handling callback */
Tony Lindgren4feb7a42019-01-13 17:25:27 -0800148 void (*data)(struct gsm_dlci *dlci, const u8 *data, int len);
149 void (*prev_data)(struct gsm_dlci *dlci, const u8 *data, int len);
Russ Gorbybcd5abe2011-06-16 14:20:12 -0700150 struct net_device *net; /* network interface, if created */
Alan Coxe1eaea42010-03-26 11:32:54 +0000151};
152
Geert Uytterhoevenc33eecc72015-05-21 14:06:11 +0200153/* DLCI 0, 62/63 are special or reserved see gsmtty_open */
Alan Coxe1eaea42010-03-26 11:32:54 +0000154
155#define NUM_DLCI 64
156
157/*
158 * DLCI 0 is used to pass control blocks out of band of the data
159 * flow (and with a higher link priority). One command can be outstanding
160 * at a time and we use this structure to manage them. They are created
161 * and destroyed by the user context, and updated by the receive paths
162 * and timers
163 */
164
165struct gsm_control {
166 u8 cmd; /* Command we are issuing */
167 u8 *data; /* Data for the command in case we retransmit */
168 int len; /* Length of block for retransmission */
169 int done; /* Done flag */
170 int error; /* Error if any */
171};
172
173/*
174 * Each GSM mux we have is represented by this structure. If we are
175 * operating as an ldisc then we use this structure as our ldisc
176 * state. We need to sort out lifetimes and locking with respect
177 * to the gsm mux array. For now we don't free DLCI objects that
178 * have been instantiated until the mux itself is terminated.
179 *
180 * To consider further: tty open versus mux shutdown.
181 */
182
183struct gsm_mux {
184 struct tty_struct *tty; /* The tty our ldisc is bound to */
185 spinlock_t lock;
Chao Bidfabf7f2013-11-26 12:09:39 +0800186 struct mutex mutex;
Russ Gorbyd50f6dc2011-06-14 13:35:32 -0700187 unsigned int num;
Russ Gorby6ab8fba2011-06-16 14:20:13 -0700188 struct kref ref;
Alan Coxe1eaea42010-03-26 11:32:54 +0000189
190 /* Events on the GSM channel */
191 wait_queue_head_t event;
192
193 /* Bits for GSM mode decoding */
194
195 /* Framing Layer */
196 unsigned char *buf;
197 int state;
198#define GSM_SEARCH 0
199#define GSM_START 1
200#define GSM_ADDRESS 2
201#define GSM_CONTROL 3
202#define GSM_LEN 4
203#define GSM_DATA 5
204#define GSM_FCS 6
205#define GSM_OVERRUN 7
Alan Coxc2f2f002010-11-04 15:17:03 +0000206#define GSM_LEN0 8
207#define GSM_LEN1 9
208#define GSM_SSOF 10
Alan Coxe1eaea42010-03-26 11:32:54 +0000209 unsigned int len;
210 unsigned int address;
211 unsigned int count;
212 int escape;
213 int encoding;
214 u8 control;
215 u8 fcs;
Alan Coxc2f2f002010-11-04 15:17:03 +0000216 u8 received_fcs;
Alan Coxe1eaea42010-03-26 11:32:54 +0000217 u8 *txframe; /* TX framing buffer */
218
219 /* Methods for the receiver side */
220 void (*receive)(struct gsm_mux *gsm, u8 ch);
221 void (*error)(struct gsm_mux *gsm, u8 ch, u8 flag);
222 /* And transmit side */
223 int (*output)(struct gsm_mux *mux, u8 *data, int len);
224
225 /* Link Layer */
226 unsigned int mru;
227 unsigned int mtu;
228 int initiator; /* Did we initiate connection */
229 int dead; /* Has the mux been shut down */
230 struct gsm_dlci *dlci[NUM_DLCI];
231 int constipated; /* Asked by remote to shut up */
232
233 spinlock_t tx_lock;
234 unsigned int tx_bytes; /* TX data outstanding */
235#define TX_THRESH_HI 8192
236#define TX_THRESH_LO 2048
Russ Gorbyb4338e12012-08-13 13:44:59 +0100237 struct list_head tx_list; /* Pending data packets */
Alan Coxe1eaea42010-03-26 11:32:54 +0000238
239 /* Control messages */
240 struct timer_list t2_timer; /* Retransmit timer for commands */
241 int cretries; /* Command retry counter */
242 struct gsm_control *pending_cmd;/* Our current pending command */
243 spinlock_t control_lock; /* Protects the pending command */
244
245 /* Configuration */
246 int adaption; /* 1 or 2 supported */
247 u8 ftype; /* UI or UIH */
248 int t1, t2; /* Timers in 1/100th of a sec */
249 int n2; /* Retry count */
250
251 /* Statistics (not currently exposed) */
252 unsigned long bad_fcs;
253 unsigned long malformed;
254 unsigned long io_error;
255 unsigned long bad_size;
256 unsigned long unsupported;
257};
258
259
260/*
261 * Mux objects - needed so that we can translate a tty index into the
262 * relevant mux and DLCI.
263 */
264
265#define MAX_MUX 4 /* 256 minors */
266static struct gsm_mux *gsm_mux[MAX_MUX]; /* GSM muxes */
267static spinlock_t gsm_mux_lock;
268
Russ Gorbyd50f6dc2011-06-14 13:35:32 -0700269static struct tty_driver *gsm_tty_driver;
270
Alan Coxe1eaea42010-03-26 11:32:54 +0000271/*
272 * This section of the driver logic implements the GSM encodings
273 * both the basic and the 'advanced'. Reliable transport is not
274 * supported.
275 */
276
277#define CR 0x02
278#define EA 0x01
279#define PF 0x10
280
281/* I is special: the rest are ..*/
282#define RR 0x01
283#define UI 0x03
284#define RNR 0x05
285#define REJ 0x09
286#define DM 0x0F
287#define SABM 0x2F
288#define DISC 0x43
289#define UA 0x63
290#define UIH 0xEF
291
292/* Channel commands */
293#define CMD_NSC 0x09
294#define CMD_TEST 0x11
295#define CMD_PSC 0x21
296#define CMD_RLS 0x29
297#define CMD_FCOFF 0x31
298#define CMD_PN 0x41
299#define CMD_RPN 0x49
300#define CMD_FCON 0x51
301#define CMD_CLD 0x61
302#define CMD_SNC 0x69
303#define CMD_MSC 0x71
304
305/* Virtual modem bits */
306#define MDM_FC 0x01
307#define MDM_RTC 0x02
308#define MDM_RTR 0x04
309#define MDM_IC 0x20
310#define MDM_DV 0x40
311
312#define GSM0_SOF 0xF9
Alan Cox5f9a31d2010-11-04 15:17:27 +0000313#define GSM1_SOF 0x7E
Alan Coxe1eaea42010-03-26 11:32:54 +0000314#define GSM1_ESCAPE 0x7D
315#define GSM1_ESCAPE_BITS 0x20
316#define XON 0x11
317#define XOFF 0x13
318
319static const struct tty_port_operations gsm_port_ops;
320
321/*
322 * CRC table for GSM 0710
323 */
324
325static const u8 gsm_fcs8[256] = {
326 0x00, 0x91, 0xE3, 0x72, 0x07, 0x96, 0xE4, 0x75,
327 0x0E, 0x9F, 0xED, 0x7C, 0x09, 0x98, 0xEA, 0x7B,
328 0x1C, 0x8D, 0xFF, 0x6E, 0x1B, 0x8A, 0xF8, 0x69,
329 0x12, 0x83, 0xF1, 0x60, 0x15, 0x84, 0xF6, 0x67,
330 0x38, 0xA9, 0xDB, 0x4A, 0x3F, 0xAE, 0xDC, 0x4D,
331 0x36, 0xA7, 0xD5, 0x44, 0x31, 0xA0, 0xD2, 0x43,
332 0x24, 0xB5, 0xC7, 0x56, 0x23, 0xB2, 0xC0, 0x51,
333 0x2A, 0xBB, 0xC9, 0x58, 0x2D, 0xBC, 0xCE, 0x5F,
334 0x70, 0xE1, 0x93, 0x02, 0x77, 0xE6, 0x94, 0x05,
335 0x7E, 0xEF, 0x9D, 0x0C, 0x79, 0xE8, 0x9A, 0x0B,
336 0x6C, 0xFD, 0x8F, 0x1E, 0x6B, 0xFA, 0x88, 0x19,
337 0x62, 0xF3, 0x81, 0x10, 0x65, 0xF4, 0x86, 0x17,
338 0x48, 0xD9, 0xAB, 0x3A, 0x4F, 0xDE, 0xAC, 0x3D,
339 0x46, 0xD7, 0xA5, 0x34, 0x41, 0xD0, 0xA2, 0x33,
340 0x54, 0xC5, 0xB7, 0x26, 0x53, 0xC2, 0xB0, 0x21,
341 0x5A, 0xCB, 0xB9, 0x28, 0x5D, 0xCC, 0xBE, 0x2F,
342 0xE0, 0x71, 0x03, 0x92, 0xE7, 0x76, 0x04, 0x95,
343 0xEE, 0x7F, 0x0D, 0x9C, 0xE9, 0x78, 0x0A, 0x9B,
344 0xFC, 0x6D, 0x1F, 0x8E, 0xFB, 0x6A, 0x18, 0x89,
345 0xF2, 0x63, 0x11, 0x80, 0xF5, 0x64, 0x16, 0x87,
346 0xD8, 0x49, 0x3B, 0xAA, 0xDF, 0x4E, 0x3C, 0xAD,
347 0xD6, 0x47, 0x35, 0xA4, 0xD1, 0x40, 0x32, 0xA3,
348 0xC4, 0x55, 0x27, 0xB6, 0xC3, 0x52, 0x20, 0xB1,
349 0xCA, 0x5B, 0x29, 0xB8, 0xCD, 0x5C, 0x2E, 0xBF,
350 0x90, 0x01, 0x73, 0xE2, 0x97, 0x06, 0x74, 0xE5,
351 0x9E, 0x0F, 0x7D, 0xEC, 0x99, 0x08, 0x7A, 0xEB,
352 0x8C, 0x1D, 0x6F, 0xFE, 0x8B, 0x1A, 0x68, 0xF9,
353 0x82, 0x13, 0x61, 0xF0, 0x85, 0x14, 0x66, 0xF7,
354 0xA8, 0x39, 0x4B, 0xDA, 0xAF, 0x3E, 0x4C, 0xDD,
355 0xA6, 0x37, 0x45, 0xD4, 0xA1, 0x30, 0x42, 0xD3,
356 0xB4, 0x25, 0x57, 0xC6, 0xB3, 0x22, 0x50, 0xC1,
357 0xBA, 0x2B, 0x59, 0xC8, 0xBD, 0x2C, 0x5E, 0xCF
358};
359
360#define INIT_FCS 0xFF
361#define GOOD_FCS 0xCF
362
363/**
364 * gsm_fcs_add - update FCS
365 * @fcs: Current FCS
366 * @c: Next data
367 *
368 * Update the FCS to include c. Uses the algorithm in the specification
369 * notes.
370 */
371
372static inline u8 gsm_fcs_add(u8 fcs, u8 c)
373{
374 return gsm_fcs8[fcs ^ c];
375}
376
377/**
378 * gsm_fcs_add_block - update FCS for a block
379 * @fcs: Current FCS
380 * @c: buffer of data
381 * @len: length of buffer
382 *
383 * Update the FCS to include c. Uses the algorithm in the specification
384 * notes.
385 */
386
387static inline u8 gsm_fcs_add_block(u8 fcs, u8 *c, int len)
388{
389 while (len--)
390 fcs = gsm_fcs8[fcs ^ *c++];
391 return fcs;
392}
393
394/**
395 * gsm_read_ea - read a byte into an EA
396 * @val: variable holding value
397 * c: byte going into the EA
398 *
399 * Processes one byte of an EA. Updates the passed variable
400 * and returns 1 if the EA is now completely read
401 */
402
403static int gsm_read_ea(unsigned int *val, u8 c)
404{
405 /* Add the next 7 bits into the value */
406 *val <<= 7;
407 *val |= c >> 1;
408 /* Was this the last byte of the EA 1 = yes*/
409 return c & EA;
410}
411
412/**
413 * gsm_encode_modem - encode modem data bits
414 * @dlci: DLCI to encode from
415 *
416 * Returns the correct GSM encoded modem status bits (6 bit field) for
417 * the current status of the DLCI and attached tty object
418 */
419
420static u8 gsm_encode_modem(const struct gsm_dlci *dlci)
421{
422 u8 modembits = 0;
423 /* FC is true flow control not modem bits */
424 if (dlci->throttled)
425 modembits |= MDM_FC;
426 if (dlci->modem_tx & TIOCM_DTR)
427 modembits |= MDM_RTC;
428 if (dlci->modem_tx & TIOCM_RTS)
429 modembits |= MDM_RTR;
430 if (dlci->modem_tx & TIOCM_RI)
431 modembits |= MDM_IC;
432 if (dlci->modem_tx & TIOCM_CD)
433 modembits |= MDM_DV;
434 return modembits;
435}
436
437/**
438 * gsm_print_packet - display a frame for debug
439 * @hdr: header to print before decode
440 * @addr: address EA from the frame
441 * @cr: C/R bit from the frame
442 * @control: control including PF bit
443 * @data: following data bytes
444 * @dlen: length of data
445 *
446 * Displays a packet in human readable format for debugging purposes. The
447 * style is based on amateur radio LAP-B dump display.
448 */
449
450static void gsm_print_packet(const char *hdr, int addr, int cr,
451 u8 control, const u8 *data, int dlen)
452{
453 if (!(debug & 1))
454 return;
455
Alan Cox5f9a31d2010-11-04 15:17:27 +0000456 pr_info("%s %d) %c: ", hdr, addr, "RC"[cr]);
Alan Coxe1eaea42010-03-26 11:32:54 +0000457
458 switch (control & ~PF) {
459 case SABM:
Alan Cox5f9a31d2010-11-04 15:17:27 +0000460 pr_cont("SABM");
Alan Coxe1eaea42010-03-26 11:32:54 +0000461 break;
462 case UA:
Alan Cox5f9a31d2010-11-04 15:17:27 +0000463 pr_cont("UA");
Alan Coxe1eaea42010-03-26 11:32:54 +0000464 break;
465 case DISC:
Alan Cox5f9a31d2010-11-04 15:17:27 +0000466 pr_cont("DISC");
Alan Coxe1eaea42010-03-26 11:32:54 +0000467 break;
468 case DM:
Alan Cox5f9a31d2010-11-04 15:17:27 +0000469 pr_cont("DM");
Alan Coxe1eaea42010-03-26 11:32:54 +0000470 break;
471 case UI:
Alan Cox5f9a31d2010-11-04 15:17:27 +0000472 pr_cont("UI");
Alan Coxe1eaea42010-03-26 11:32:54 +0000473 break;
474 case UIH:
Alan Cox5f9a31d2010-11-04 15:17:27 +0000475 pr_cont("UIH");
Alan Coxe1eaea42010-03-26 11:32:54 +0000476 break;
477 default:
478 if (!(control & 0x01)) {
Alan Cox5f9a31d2010-11-04 15:17:27 +0000479 pr_cont("I N(S)%d N(R)%d",
Alan Cox47fdd642012-09-17 12:02:35 +0100480 (control & 0x0E) >> 1, (control & 0xE0) >> 5);
Alan Coxe1eaea42010-03-26 11:32:54 +0000481 } else switch (control & 0x0F) {
Alan Cox5f9a31d2010-11-04 15:17:27 +0000482 case RR:
483 pr_cont("RR(%d)", (control & 0xE0) >> 5);
484 break;
485 case RNR:
486 pr_cont("RNR(%d)", (control & 0xE0) >> 5);
487 break;
488 case REJ:
489 pr_cont("REJ(%d)", (control & 0xE0) >> 5);
490 break;
491 default:
492 pr_cont("[%02X]", control);
Alan Coxe1eaea42010-03-26 11:32:54 +0000493 }
494 }
495
496 if (control & PF)
Alan Cox5f9a31d2010-11-04 15:17:27 +0000497 pr_cont("(P)");
Alan Coxe1eaea42010-03-26 11:32:54 +0000498 else
Alan Cox5f9a31d2010-11-04 15:17:27 +0000499 pr_cont("(F)");
Alan Coxe1eaea42010-03-26 11:32:54 +0000500
501 if (dlen) {
502 int ct = 0;
503 while (dlen--) {
Alan Cox5f9a31d2010-11-04 15:17:27 +0000504 if (ct % 8 == 0) {
505 pr_cont("\n");
506 pr_debug(" ");
507 }
508 pr_cont("%02X ", *data++);
Alan Coxe1eaea42010-03-26 11:32:54 +0000509 ct++;
510 }
511 }
Alan Cox5f9a31d2010-11-04 15:17:27 +0000512 pr_cont("\n");
Alan Coxe1eaea42010-03-26 11:32:54 +0000513}
514
515
516/*
517 * Link level transmission side
518 */
519
520/**
521 * gsm_stuff_packet - bytestuff a packet
522 * @ibuf: input
523 * @obuf: output
524 * @len: length of input
525 *
526 * Expand a buffer by bytestuffing it. The worst case size change
527 * is doubling and the caller is responsible for handing out
528 * suitable sized buffers.
529 */
530
531static int gsm_stuff_frame(const u8 *input, u8 *output, int len)
532{
533 int olen = 0;
534 while (len--) {
535 if (*input == GSM1_SOF || *input == GSM1_ESCAPE
536 || *input == XON || *input == XOFF) {
537 *output++ = GSM1_ESCAPE;
538 *output++ = *input++ ^ GSM1_ESCAPE_BITS;
539 olen++;
540 } else
541 *output++ = *input++;
542 olen++;
543 }
544 return olen;
545}
546
Alan Coxe1eaea42010-03-26 11:32:54 +0000547/**
548 * gsm_send - send a control frame
549 * @gsm: our GSM mux
550 * @addr: address for control frame
551 * @cr: command/response bit
552 * @control: control byte including PF bit
553 *
554 * Format up and transmit a control frame. These do not go via the
555 * queueing logic as they should be transmitted ahead of data when
556 * they are needed.
557 *
558 * FIXME: Lock versus data TX path
559 */
560
561static void gsm_send(struct gsm_mux *gsm, int addr, int cr, int control)
562{
563 int len;
564 u8 cbuf[10];
565 u8 ibuf[3];
566
567 switch (gsm->encoding) {
568 case 0:
569 cbuf[0] = GSM0_SOF;
570 cbuf[1] = (addr << 2) | (cr << 1) | EA;
571 cbuf[2] = control;
572 cbuf[3] = EA; /* Length of data = 0 */
573 cbuf[4] = 0xFF - gsm_fcs_add_block(INIT_FCS, cbuf + 1, 3);
574 cbuf[5] = GSM0_SOF;
575 len = 6;
576 break;
577 case 1:
578 case 2:
579 /* Control frame + packing (but not frame stuffing) in mode 1 */
580 ibuf[0] = (addr << 2) | (cr << 1) | EA;
581 ibuf[1] = control;
582 ibuf[2] = 0xFF - gsm_fcs_add_block(INIT_FCS, ibuf, 2);
583 /* Stuffing may double the size worst case */
584 len = gsm_stuff_frame(ibuf, cbuf + 1, 3);
585 /* Now add the SOF markers */
586 cbuf[0] = GSM1_SOF;
587 cbuf[len + 1] = GSM1_SOF;
588 /* FIXME: we can omit the lead one in many cases */
589 len += 2;
590 break;
591 default:
592 WARN_ON(1);
593 return;
594 }
595 gsm->output(gsm, cbuf, len);
596 gsm_print_packet("-->", addr, cr, control, NULL, 0);
597}
598
599/**
600 * gsm_response - send a control response
601 * @gsm: our GSM mux
602 * @addr: address for control frame
603 * @control: control byte including PF bit
604 *
605 * Format up and transmit a link level response frame.
606 */
607
608static inline void gsm_response(struct gsm_mux *gsm, int addr, int control)
609{
610 gsm_send(gsm, addr, 0, control);
611}
612
613/**
614 * gsm_command - send a control command
615 * @gsm: our GSM mux
616 * @addr: address for control frame
617 * @control: control byte including PF bit
618 *
619 * Format up and transmit a link level command frame.
620 */
621
622static inline void gsm_command(struct gsm_mux *gsm, int addr, int control)
623{
624 gsm_send(gsm, addr, 1, control);
625}
626
627/* Data transmission */
628
629#define HDR_LEN 6 /* ADDR CTRL [LEN.2] DATA FCS */
630
631/**
632 * gsm_data_alloc - allocate data frame
633 * @gsm: GSM mux
634 * @addr: DLCI address
635 * @len: length excluding header and FCS
636 * @ctrl: control byte
637 *
638 * Allocate a new data buffer for sending frames with data. Space is left
639 * at the front for header bytes but that is treated as an implementation
640 * detail and not for the high level code to use
641 */
642
643static struct gsm_msg *gsm_data_alloc(struct gsm_mux *gsm, u8 addr, int len,
644 u8 ctrl)
645{
646 struct gsm_msg *m = kmalloc(sizeof(struct gsm_msg) + len + HDR_LEN,
647 GFP_ATOMIC);
648 if (m == NULL)
649 return NULL;
650 m->data = m->buffer + HDR_LEN - 1; /* Allow for FCS */
651 m->len = len;
652 m->addr = addr;
653 m->ctrl = ctrl;
Russ Gorbyb4338e12012-08-13 13:44:59 +0100654 INIT_LIST_HEAD(&m->list);
Alan Coxe1eaea42010-03-26 11:32:54 +0000655 return m;
656}
657
658/**
659 * gsm_data_kick - poke the queue
660 * @gsm: GSM Mux
661 *
662 * The tty device has called us to indicate that room has appeared in
663 * the transmit queue. Ram more data into the pipe if we have any
Frederic Beratc01af4f2012-08-13 13:43:58 +0100664 * If we have been flow-stopped by a CMD_FCOFF, then we can only
665 * send messages on DLCI0 until CMD_FCON
Alan Coxe1eaea42010-03-26 11:32:54 +0000666 *
667 * FIXME: lock against link layer control transmissions
668 */
669
670static void gsm_data_kick(struct gsm_mux *gsm)
671{
Russ Gorbyb4338e12012-08-13 13:44:59 +0100672 struct gsm_msg *msg, *nmsg;
Alan Coxe1eaea42010-03-26 11:32:54 +0000673 int len;
674 int skip_sof = 0;
675
Russ Gorbyb4338e12012-08-13 13:44:59 +0100676 list_for_each_entry_safe(msg, nmsg, &gsm->tx_list, list) {
677 if (gsm->constipated && msg->addr)
Frederic Beratc01af4f2012-08-13 13:43:58 +0100678 continue;
Alan Coxe1eaea42010-03-26 11:32:54 +0000679 if (gsm->encoding != 0) {
680 gsm->txframe[0] = GSM1_SOF;
681 len = gsm_stuff_frame(msg->data,
682 gsm->txframe + 1, msg->len);
683 gsm->txframe[len + 1] = GSM1_SOF;
684 len += 2;
685 } else {
686 gsm->txframe[0] = GSM0_SOF;
687 memcpy(gsm->txframe + 1 , msg->data, msg->len);
688 gsm->txframe[msg->len + 1] = GSM0_SOF;
689 len = msg->len + 2;
690 }
691
Joe Perches0a77c4f2011-04-25 16:46:49 -0700692 if (debug & 4)
693 print_hex_dump_bytes("gsm_data_kick: ",
694 DUMP_PREFIX_OFFSET,
695 gsm->txframe, len);
Alan Coxe1eaea42010-03-26 11:32:54 +0000696
697 if (gsm->output(gsm, gsm->txframe + skip_sof,
698 len - skip_sof) < 0)
699 break;
700 /* FIXME: Can eliminate one SOF in many more cases */
Alan Coxe1eaea42010-03-26 11:32:54 +0000701 gsm->tx_bytes -= msg->len;
Alan Coxe1eaea42010-03-26 11:32:54 +0000702 /* For a burst of frames skip the extra SOF within the
703 burst */
704 skip_sof = 1;
Frederic Beratc01af4f2012-08-13 13:43:58 +0100705
Russ Gorbyb4338e12012-08-13 13:44:59 +0100706 list_del(&msg->list);
707 kfree(msg);
Alan Coxe1eaea42010-03-26 11:32:54 +0000708 }
709}
710
711/**
712 * __gsm_data_queue - queue a UI or UIH frame
713 * @dlci: DLCI sending the data
714 * @msg: message queued
715 *
716 * Add data to the transmit queue and try and get stuff moving
717 * out of the mux tty if not already doing so. The Caller must hold
718 * the gsm tx lock.
719 */
720
721static void __gsm_data_queue(struct gsm_dlci *dlci, struct gsm_msg *msg)
722{
723 struct gsm_mux *gsm = dlci->gsm;
724 u8 *dp = msg->data;
725 u8 *fcs = dp + msg->len;
726
727 /* Fill in the header */
728 if (gsm->encoding == 0) {
729 if (msg->len < 128)
730 *--dp = (msg->len << 1) | EA;
731 else {
Ken Millsbe7a7412010-12-13 15:27:27 +0000732 *--dp = (msg->len >> 7); /* bits 7 - 15 */
733 *--dp = (msg->len & 127) << 1; /* bits 0 - 6 */
Alan Coxe1eaea42010-03-26 11:32:54 +0000734 }
735 }
736
737 *--dp = msg->ctrl;
738 if (gsm->initiator)
739 *--dp = (msg->addr << 2) | 2 | EA;
740 else
741 *--dp = (msg->addr << 2) | EA;
742 *fcs = gsm_fcs_add_block(INIT_FCS, dp , msg->data - dp);
743 /* Ugly protocol layering violation */
744 if (msg->ctrl == UI || msg->ctrl == (UI|PF))
745 *fcs = gsm_fcs_add_block(*fcs, msg->data, msg->len);
746 *fcs = 0xFF - *fcs;
747
748 gsm_print_packet("Q> ", msg->addr, gsm->initiator, msg->ctrl,
749 msg->data, msg->len);
750
751 /* Move the header back and adjust the length, also allow for the FCS
752 now tacked on the end */
753 msg->len += (msg->data - dp) + 1;
754 msg->data = dp;
755
756 /* Add to the actual output queue */
Russ Gorbyb4338e12012-08-13 13:44:59 +0100757 list_add_tail(&msg->list, &gsm->tx_list);
Alan Coxe1eaea42010-03-26 11:32:54 +0000758 gsm->tx_bytes += msg->len;
759 gsm_data_kick(gsm);
760}
761
762/**
763 * gsm_data_queue - queue a UI or UIH frame
764 * @dlci: DLCI sending the data
765 * @msg: message queued
766 *
767 * Add data to the transmit queue and try and get stuff moving
768 * out of the mux tty if not already doing so. Take the
769 * the gsm tx lock and dlci lock.
770 */
771
772static void gsm_data_queue(struct gsm_dlci *dlci, struct gsm_msg *msg)
773{
774 unsigned long flags;
775 spin_lock_irqsave(&dlci->gsm->tx_lock, flags);
776 __gsm_data_queue(dlci, msg);
777 spin_unlock_irqrestore(&dlci->gsm->tx_lock, flags);
778}
779
780/**
781 * gsm_dlci_data_output - try and push data out of a DLCI
782 * @gsm: mux
783 * @dlci: the DLCI to pull data from
784 *
785 * Pull data from a DLCI and send it into the transmit queue if there
786 * is data. Keep to the MRU of the mux. This path handles the usual tty
787 * interface which is a byte stream with optional modem data.
788 *
789 * Caller must hold the tx_lock of the mux.
790 */
791
792static int gsm_dlci_data_output(struct gsm_mux *gsm, struct gsm_dlci *dlci)
793{
794 struct gsm_msg *msg;
795 u8 *dp;
Mikhail Kshevetskiy268e5262011-09-23 19:22:56 +0400796 int len, total_size, size;
Alan Coxe1eaea42010-03-26 11:32:54 +0000797 int h = dlci->adaption - 1;
798
Mikhail Kshevetskiy268e5262011-09-23 19:22:56 +0400799 total_size = 0;
Aldo Iljazif3c909b2013-07-08 22:28:00 +0300800 while (1) {
Jiri Slaby036bca1fc2020-02-19 09:49:40 +0100801 len = kfifo_len(&dlci->fifo);
Mikhail Kshevetskiy268e5262011-09-23 19:22:56 +0400802 if (len == 0)
803 return total_size;
Alan Coxe1eaea42010-03-26 11:32:54 +0000804
Mikhail Kshevetskiy268e5262011-09-23 19:22:56 +0400805 /* MTU/MRU count only the data bits */
806 if (len > gsm->mtu)
807 len = gsm->mtu;
Alan Coxe1eaea42010-03-26 11:32:54 +0000808
Mikhail Kshevetskiy268e5262011-09-23 19:22:56 +0400809 size = len + h;
Alan Coxe1eaea42010-03-26 11:32:54 +0000810
Mikhail Kshevetskiy268e5262011-09-23 19:22:56 +0400811 msg = gsm_data_alloc(gsm, dlci->addr, size, gsm->ftype);
812 /* FIXME: need a timer or something to kick this so it can't
813 get stuck with no work outstanding and no buffer free */
814 if (msg == NULL)
815 return -ENOMEM;
816 dp = msg->data;
817 switch (dlci->adaption) {
818 case 1: /* Unstructured */
819 break;
Aldo Iljazif3c909b2013-07-08 22:28:00 +0300820 case 2: /* Unstructed with modem bits.
821 Always one byte as we never send inline break data */
Mikhail Kshevetskiy268e5262011-09-23 19:22:56 +0400822 *dp++ = gsm_encode_modem(dlci);
823 break;
824 }
Jiri Slaby036bca1fc2020-02-19 09:49:40 +0100825 WARN_ON(kfifo_out_locked(&dlci->fifo, dp , len, &dlci->lock) != len);
Mikhail Kshevetskiy268e5262011-09-23 19:22:56 +0400826 __gsm_data_queue(dlci, msg);
827 total_size += size;
Alan Coxe1eaea42010-03-26 11:32:54 +0000828 }
Alan Coxe1eaea42010-03-26 11:32:54 +0000829 /* Bytes of data we used up */
Mikhail Kshevetskiy268e5262011-09-23 19:22:56 +0400830 return total_size;
Alan Coxe1eaea42010-03-26 11:32:54 +0000831}
832
833/**
834 * gsm_dlci_data_output_framed - try and push data out of a DLCI
835 * @gsm: mux
836 * @dlci: the DLCI to pull data from
837 *
838 * Pull data from a DLCI and send it into the transmit queue if there
839 * is data. Keep to the MRU of the mux. This path handles framed data
840 * queued as skbuffs to the DLCI.
841 *
842 * Caller must hold the tx_lock of the mux.
843 */
844
845static int gsm_dlci_data_output_framed(struct gsm_mux *gsm,
846 struct gsm_dlci *dlci)
847{
848 struct gsm_msg *msg;
849 u8 *dp;
850 int len, size;
851 int last = 0, first = 0;
852 int overhead = 0;
853
854 /* One byte per frame is used for B/F flags */
855 if (dlci->adaption == 4)
856 overhead = 1;
857
858 /* dlci->skb is locked by tx_lock */
859 if (dlci->skb == NULL) {
Russ Gorby88ed2a62012-08-13 13:45:30 +0100860 dlci->skb = skb_dequeue_tail(&dlci->skb_list);
Alan Coxe1eaea42010-03-26 11:32:54 +0000861 if (dlci->skb == NULL)
862 return 0;
863 first = 1;
864 }
865 len = dlci->skb->len + overhead;
866
867 /* MTU/MRU count only the data bits */
868 if (len > gsm->mtu) {
869 if (dlci->adaption == 3) {
870 /* Over long frame, bin it */
Russ Gorby329e5672012-08-13 13:45:15 +0100871 dev_kfree_skb_any(dlci->skb);
Alan Coxe1eaea42010-03-26 11:32:54 +0000872 dlci->skb = NULL;
873 return 0;
874 }
875 len = gsm->mtu;
876 } else
877 last = 1;
878
879 size = len + overhead;
880 msg = gsm_data_alloc(gsm, dlci->addr, size, gsm->ftype);
881
882 /* FIXME: need a timer or something to kick this so it can't
883 get stuck with no work outstanding and no buffer free */
Russ Gorby88ed2a62012-08-13 13:45:30 +0100884 if (msg == NULL) {
885 skb_queue_tail(&dlci->skb_list, dlci->skb);
886 dlci->skb = NULL;
Alan Coxe1eaea42010-03-26 11:32:54 +0000887 return -ENOMEM;
Russ Gorby88ed2a62012-08-13 13:45:30 +0100888 }
Alan Coxe1eaea42010-03-26 11:32:54 +0000889 dp = msg->data;
890
891 if (dlci->adaption == 4) { /* Interruptible framed (Packetised Data) */
892 /* Flag byte to carry the start/end info */
893 *dp++ = last << 7 | first << 6 | 1; /* EA */
894 len--;
895 }
Russ Gorby57f21042011-06-14 13:23:29 -0700896 memcpy(dp, dlci->skb->data, len);
897 skb_pull(dlci->skb, len);
Alan Coxe1eaea42010-03-26 11:32:54 +0000898 __gsm_data_queue(dlci, msg);
Russ Gorbybcd5abe2011-06-16 14:20:12 -0700899 if (last) {
Russ Gorby329e5672012-08-13 13:45:15 +0100900 dev_kfree_skb_any(dlci->skb);
Alan Coxe1eaea42010-03-26 11:32:54 +0000901 dlci->skb = NULL;
Russ Gorbybcd5abe2011-06-16 14:20:12 -0700902 }
Alan Coxe1eaea42010-03-26 11:32:54 +0000903 return size;
904}
905
906/**
907 * gsm_dlci_data_sweep - look for data to send
908 * @gsm: the GSM mux
909 *
910 * Sweep the GSM mux channels in priority order looking for ones with
911 * data to send. We could do with optimising this scan a bit. We aim
912 * to fill the queue totally or up to TX_THRESH_HI bytes. Once we hit
913 * TX_THRESH_LO we get called again
914 *
915 * FIXME: We should round robin between groups and in theory you can
916 * renegotiate DLCI priorities with optional stuff. Needs optimising.
917 */
918
919static void gsm_dlci_data_sweep(struct gsm_mux *gsm)
920{
921 int len;
922 /* Priority ordering: We should do priority with RR of the groups */
923 int i = 1;
Alan Coxe1eaea42010-03-26 11:32:54 +0000924
Alan Coxe1eaea42010-03-26 11:32:54 +0000925 while (i < NUM_DLCI) {
926 struct gsm_dlci *dlci;
927
928 if (gsm->tx_bytes > TX_THRESH_HI)
929 break;
930 dlci = gsm->dlci[i];
931 if (dlci == NULL || dlci->constipated) {
932 i++;
933 continue;
934 }
Russ Gorbybcd5abe2011-06-16 14:20:12 -0700935 if (dlci->adaption < 3 && !dlci->net)
Alan Coxe1eaea42010-03-26 11:32:54 +0000936 len = gsm_dlci_data_output(gsm, dlci);
937 else
938 len = gsm_dlci_data_output_framed(gsm, dlci);
939 if (len < 0)
Julia Lawalle73790a2010-08-10 18:03:12 -0700940 break;
Alan Coxe1eaea42010-03-26 11:32:54 +0000941 /* DLCI empty - try the next */
942 if (len == 0)
943 i++;
944 }
Alan Coxe1eaea42010-03-26 11:32:54 +0000945}
946
947/**
948 * gsm_dlci_data_kick - transmit if possible
949 * @dlci: DLCI to kick
950 *
951 * Transmit data from this DLCI if the queue is empty. We can't rely on
952 * a tty wakeup except when we filled the pipe so we need to fire off
953 * new data ourselves in other cases.
954 */
955
956static void gsm_dlci_data_kick(struct gsm_dlci *dlci)
957{
958 unsigned long flags;
Russ Gorby192b6042012-08-13 13:43:36 +0100959 int sweep;
Alan Coxe1eaea42010-03-26 11:32:54 +0000960
Aldo Iljazif3c909b2013-07-08 22:28:00 +0300961 if (dlci->constipated)
Frederic Beratc01af4f2012-08-13 13:43:58 +0100962 return;
Frederic Beratc01af4f2012-08-13 13:43:58 +0100963
Alan Coxe1eaea42010-03-26 11:32:54 +0000964 spin_lock_irqsave(&dlci->gsm->tx_lock, flags);
965 /* If we have nothing running then we need to fire up */
Russ Gorby192b6042012-08-13 13:43:36 +0100966 sweep = (dlci->gsm->tx_bytes < TX_THRESH_LO);
Russ Gorbybcd5abe2011-06-16 14:20:12 -0700967 if (dlci->gsm->tx_bytes == 0) {
968 if (dlci->net)
969 gsm_dlci_data_output_framed(dlci->gsm, dlci);
970 else
971 gsm_dlci_data_output(dlci->gsm, dlci);
Russ Gorby192b6042012-08-13 13:43:36 +0100972 }
973 if (sweep)
Aldo Iljazif3c909b2013-07-08 22:28:00 +0300974 gsm_dlci_data_sweep(dlci->gsm);
Alan Coxe1eaea42010-03-26 11:32:54 +0000975 spin_unlock_irqrestore(&dlci->gsm->tx_lock, flags);
976}
977
978/*
979 * Control message processing
980 */
981
982
983/**
984 * gsm_control_reply - send a response frame to a control
985 * @gsm: gsm channel
986 * @cmd: the command to use
987 * @data: data to follow encoded info
988 * @dlen: length of data
989 *
990 * Encode up and queue a UI/UIH frame containing our response.
991 */
992
Tony Lindgren4feb7a42019-01-13 17:25:27 -0800993static void gsm_control_reply(struct gsm_mux *gsm, int cmd, const u8 *data,
Alan Coxe1eaea42010-03-26 11:32:54 +0000994 int dlen)
995{
996 struct gsm_msg *msg;
997 msg = gsm_data_alloc(gsm, 0, dlen + 2, gsm->ftype);
Ken Mills093d8042010-12-13 15:28:03 +0000998 if (msg == NULL)
999 return;
Alan Coxe1eaea42010-03-26 11:32:54 +00001000 msg->data[0] = (cmd & 0xFE) << 1 | EA; /* Clear C/R */
1001 msg->data[1] = (dlen << 1) | EA;
1002 memcpy(msg->data + 2, data, dlen);
1003 gsm_data_queue(gsm->dlci[0], msg);
1004}
1005
1006/**
1007 * gsm_process_modem - process received modem status
1008 * @tty: virtual tty bound to the DLCI
1009 * @dlci: DLCI to affect
1010 * @modem: modem bits (full EA)
1011 *
1012 * Used when a modem control message or line state inline in adaption
1013 * layer 2 is processed. Sort out the local modem state and throttles
1014 */
1015
1016static void gsm_process_modem(struct tty_struct *tty, struct gsm_dlci *dlci,
Russ Gorby72632872011-06-14 13:23:28 -07001017 u32 modem, int clen)
Alan Coxe1eaea42010-03-26 11:32:54 +00001018{
1019 int mlines = 0;
Russ Gorby72632872011-06-14 13:23:28 -07001020 u8 brk = 0;
Frederic Beratc01af4f2012-08-13 13:43:58 +01001021 int fc;
Russ Gorby72632872011-06-14 13:23:28 -07001022
1023 /* The modem status command can either contain one octet (v.24 signals)
1024 or two octets (v.24 signals + break signals). The length field will
1025 either be 2 or 3 respectively. This is specified in section
1026 5.4.6.3.7 of the 27.010 mux spec. */
1027
1028 if (clen == 2)
1029 modem = modem & 0x7f;
1030 else {
1031 brk = modem & 0x7f;
1032 modem = (modem >> 7) & 0x7f;
Frederic Beratc01af4f2012-08-13 13:43:58 +01001033 }
Alan Coxe1eaea42010-03-26 11:32:54 +00001034
1035 /* Flow control/ready to communicate */
Frederic Beratc01af4f2012-08-13 13:43:58 +01001036 fc = (modem & MDM_FC) || !(modem & MDM_RTR);
1037 if (fc && !dlci->constipated) {
Alan Coxe1eaea42010-03-26 11:32:54 +00001038 /* Need to throttle our output on this device */
1039 dlci->constipated = 1;
Frederic Beratc01af4f2012-08-13 13:43:58 +01001040 } else if (!fc && dlci->constipated) {
Alan Coxe1eaea42010-03-26 11:32:54 +00001041 dlci->constipated = 0;
1042 gsm_dlci_data_kick(dlci);
1043 }
Frederic Beratc01af4f2012-08-13 13:43:58 +01001044
Alan Coxe1eaea42010-03-26 11:32:54 +00001045 /* Map modem bits */
Frederic Beratc01af4f2012-08-13 13:43:58 +01001046 if (modem & MDM_RTC)
1047 mlines |= TIOCM_DSR | TIOCM_DTR;
Alan Coxe1eaea42010-03-26 11:32:54 +00001048 if (modem & MDM_RTR)
1049 mlines |= TIOCM_RTS | TIOCM_CTS;
1050 if (modem & MDM_IC)
1051 mlines |= TIOCM_RI;
1052 if (modem & MDM_DV)
1053 mlines |= TIOCM_CD;
1054
1055 /* Carrier drop -> hangup */
1056 if (tty) {
1057 if ((mlines & TIOCM_CD) == 0 && (dlci->modem_rx & TIOCM_CD))
Peter Hurley9db276f2016-01-10 20:36:15 -08001058 if (!C_CLOCAL(tty))
Alan Coxe1eaea42010-03-26 11:32:54 +00001059 tty_hangup(tty);
Alan Coxe1eaea42010-03-26 11:32:54 +00001060 }
Jiri Slaby92a19f92013-01-03 15:53:03 +01001061 if (brk & 0x01)
1062 tty_insert_flip_char(&dlci->port, 0, TTY_BREAK);
Alan Coxe1eaea42010-03-26 11:32:54 +00001063 dlci->modem_rx = mlines;
1064}
1065
1066/**
1067 * gsm_control_modem - modem status received
1068 * @gsm: GSM channel
1069 * @data: data following command
1070 * @clen: command length
1071 *
1072 * We have received a modem status control message. This is used by
1073 * the GSM mux protocol to pass virtual modem line status and optionally
1074 * to indicate break signals. Unpack it, convert to Linux representation
1075 * and if need be stuff a break message down the tty.
1076 */
1077
Tony Lindgren4feb7a42019-01-13 17:25:27 -08001078static void gsm_control_modem(struct gsm_mux *gsm, const u8 *data, int clen)
Alan Coxe1eaea42010-03-26 11:32:54 +00001079{
1080 unsigned int addr = 0;
1081 unsigned int modem = 0;
Lars Poeschel3ac06b92014-01-07 13:34:37 +01001082 unsigned int brk = 0;
Alan Coxe1eaea42010-03-26 11:32:54 +00001083 struct gsm_dlci *dlci;
1084 int len = clen;
Tony Lindgren4feb7a42019-01-13 17:25:27 -08001085 const u8 *dp = data;
Alan Coxe1eaea42010-03-26 11:32:54 +00001086 struct tty_struct *tty;
1087
1088 while (gsm_read_ea(&addr, *dp++) == 0) {
1089 len--;
1090 if (len == 0)
1091 return;
1092 }
1093 /* Must be at least one byte following the EA */
1094 len--;
1095 if (len <= 0)
1096 return;
1097
1098 addr >>= 1;
1099 /* Closed port, or invalid ? */
1100 if (addr == 0 || addr >= NUM_DLCI || gsm->dlci[addr] == NULL)
1101 return;
1102 dlci = gsm->dlci[addr];
1103
1104 while (gsm_read_ea(&modem, *dp++) == 0) {
1105 len--;
1106 if (len == 0)
1107 return;
1108 }
Lars Poeschel3ac06b92014-01-07 13:34:37 +01001109 len--;
1110 if (len > 0) {
1111 while (gsm_read_ea(&brk, *dp++) == 0) {
1112 len--;
1113 if (len == 0)
1114 return;
1115 }
1116 modem <<= 7;
1117 modem |= (brk & 0x7f);
1118 }
Alan Coxe1eaea42010-03-26 11:32:54 +00001119 tty = tty_port_tty_get(&dlci->port);
Russ Gorby72632872011-06-14 13:23:28 -07001120 gsm_process_modem(tty, dlci, modem, clen);
Alan Coxe1eaea42010-03-26 11:32:54 +00001121 if (tty) {
1122 tty_wakeup(tty);
1123 tty_kref_put(tty);
1124 }
1125 gsm_control_reply(gsm, CMD_MSC, data, clen);
1126}
1127
1128/**
1129 * gsm_control_rls - remote line status
1130 * @gsm: GSM channel
1131 * @data: data bytes
1132 * @clen: data length
1133 *
1134 * The modem sends us a two byte message on the control channel whenever
1135 * it wishes to send us an error state from the virtual link. Stuff
1136 * this into the uplink tty if present
1137 */
1138
Tony Lindgren4feb7a42019-01-13 17:25:27 -08001139static void gsm_control_rls(struct gsm_mux *gsm, const u8 *data, int clen)
Alan Coxe1eaea42010-03-26 11:32:54 +00001140{
Jiri Slaby92a19f92013-01-03 15:53:03 +01001141 struct tty_port *port;
Aldo Iljazif3c909b2013-07-08 22:28:00 +03001142 unsigned int addr = 0;
Alan Coxe1eaea42010-03-26 11:32:54 +00001143 u8 bits;
1144 int len = clen;
Tony Lindgren4feb7a42019-01-13 17:25:27 -08001145 const u8 *dp = data;
Alan Coxe1eaea42010-03-26 11:32:54 +00001146
1147 while (gsm_read_ea(&addr, *dp++) == 0) {
1148 len--;
1149 if (len == 0)
1150 return;
1151 }
1152 /* Must be at least one byte following ea */
1153 len--;
1154 if (len <= 0)
1155 return;
1156 addr >>= 1;
1157 /* Closed port, or invalid ? */
1158 if (addr == 0 || addr >= NUM_DLCI || gsm->dlci[addr] == NULL)
1159 return;
1160 /* No error ? */
1161 bits = *dp;
1162 if ((bits & 1) == 0)
1163 return;
Alan Coxe1eaea42010-03-26 11:32:54 +00001164
Jiri Slaby92a19f92013-01-03 15:53:03 +01001165 port = &gsm->dlci[addr]->port;
1166
1167 if (bits & 2)
1168 tty_insert_flip_char(port, 0, TTY_OVERRUN);
1169 if (bits & 4)
1170 tty_insert_flip_char(port, 0, TTY_PARITY);
1171 if (bits & 8)
1172 tty_insert_flip_char(port, 0, TTY_FRAME);
1173
Jiri Slaby2e124b42013-01-03 15:53:06 +01001174 tty_flip_buffer_push(port);
1175
Alan Coxe1eaea42010-03-26 11:32:54 +00001176 gsm_control_reply(gsm, CMD_RLS, data, clen);
1177}
1178
1179static void gsm_dlci_begin_close(struct gsm_dlci *dlci);
1180
1181/**
1182 * gsm_control_message - DLCI 0 control processing
1183 * @gsm: our GSM mux
1184 * @command: the command EA
1185 * @data: data beyond the command/length EAs
1186 * @clen: length
1187 *
1188 * Input processor for control messages from the other end of the link.
1189 * Processes the incoming request and queues a response frame or an
1190 * NSC response if not supported
1191 */
1192
1193static void gsm_control_message(struct gsm_mux *gsm, unsigned int command,
Tony Lindgren4feb7a42019-01-13 17:25:27 -08001194 const u8 *data, int clen)
Alan Coxe1eaea42010-03-26 11:32:54 +00001195{
1196 u8 buf[1];
Russ Gorby5e447082012-08-13 13:44:40 +01001197 unsigned long flags;
1198
Alan Coxe1eaea42010-03-26 11:32:54 +00001199 switch (command) {
1200 case CMD_CLD: {
1201 struct gsm_dlci *dlci = gsm->dlci[0];
1202 /* Modem wishes to close down */
1203 if (dlci) {
1204 dlci->dead = 1;
1205 gsm->dead = 1;
1206 gsm_dlci_begin_close(dlci);
1207 }
1208 }
1209 break;
1210 case CMD_TEST:
1211 /* Modem wishes to test, reply with the data */
1212 gsm_control_reply(gsm, CMD_TEST, data, clen);
1213 break;
1214 case CMD_FCON:
Alan Coxe1eaea42010-03-26 11:32:54 +00001215 /* Modem can accept data again */
1216 gsm->constipated = 0;
Frederic Beratc01af4f2012-08-13 13:43:58 +01001217 gsm_control_reply(gsm, CMD_FCON, NULL, 0);
Alan Coxe1eaea42010-03-26 11:32:54 +00001218 /* Kick the link in case it is idling */
Russ Gorby5e447082012-08-13 13:44:40 +01001219 spin_lock_irqsave(&gsm->tx_lock, flags);
Alan Coxe1eaea42010-03-26 11:32:54 +00001220 gsm_data_kick(gsm);
Russ Gorby5e447082012-08-13 13:44:40 +01001221 spin_unlock_irqrestore(&gsm->tx_lock, flags);
Alan Coxe1eaea42010-03-26 11:32:54 +00001222 break;
Frederic Beratc01af4f2012-08-13 13:43:58 +01001223 case CMD_FCOFF:
1224 /* Modem wants us to STFU */
Frederic Beratc01af4f2012-08-13 13:43:58 +01001225 gsm->constipated = 1;
1226 gsm_control_reply(gsm, CMD_FCOFF, NULL, 0);
1227 break;
Alan Coxe1eaea42010-03-26 11:32:54 +00001228 case CMD_MSC:
1229 /* Out of band modem line change indicator for a DLCI */
1230 gsm_control_modem(gsm, data, clen);
1231 break;
1232 case CMD_RLS:
1233 /* Out of band error reception for a DLCI */
1234 gsm_control_rls(gsm, data, clen);
1235 break;
1236 case CMD_PSC:
1237 /* Modem wishes to enter power saving state */
1238 gsm_control_reply(gsm, CMD_PSC, NULL, 0);
1239 break;
1240 /* Optional unsupported commands */
1241 case CMD_PN: /* Parameter negotiation */
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001242 case CMD_RPN: /* Remote port negotiation */
1243 case CMD_SNC: /* Service negotiation command */
Alan Coxe1eaea42010-03-26 11:32:54 +00001244 default:
1245 /* Reply to bad commands with an NSC */
1246 buf[0] = command;
1247 gsm_control_reply(gsm, CMD_NSC, buf, 1);
1248 break;
1249 }
1250}
1251
1252/**
1253 * gsm_control_response - process a response to our control
1254 * @gsm: our GSM mux
1255 * @command: the command (response) EA
1256 * @data: data beyond the command/length EA
1257 * @clen: length
1258 *
1259 * Process a response to an outstanding command. We only allow a single
1260 * control message in flight so this is fairly easy. All the clean up
1261 * is done by the caller, we just update the fields, flag it as done
1262 * and return
1263 */
1264
1265static void gsm_control_response(struct gsm_mux *gsm, unsigned int command,
Tony Lindgren4feb7a42019-01-13 17:25:27 -08001266 const u8 *data, int clen)
Alan Coxe1eaea42010-03-26 11:32:54 +00001267{
1268 struct gsm_control *ctrl;
1269 unsigned long flags;
1270
1271 spin_lock_irqsave(&gsm->control_lock, flags);
1272
1273 ctrl = gsm->pending_cmd;
1274 /* Does the reply match our command */
1275 command |= 1;
1276 if (ctrl != NULL && (command == ctrl->cmd || command == CMD_NSC)) {
1277 /* Our command was replied to, kill the retry timer */
1278 del_timer(&gsm->t2_timer);
1279 gsm->pending_cmd = NULL;
1280 /* Rejected by the other end */
1281 if (command == CMD_NSC)
1282 ctrl->error = -EOPNOTSUPP;
1283 ctrl->done = 1;
1284 wake_up(&gsm->event);
1285 }
1286 spin_unlock_irqrestore(&gsm->control_lock, flags);
1287}
1288
1289/**
Alan Cox5f9a31d2010-11-04 15:17:27 +00001290 * gsm_control_transmit - send control packet
Alan Coxe1eaea42010-03-26 11:32:54 +00001291 * @gsm: gsm mux
1292 * @ctrl: frame to send
1293 *
1294 * Send out a pending control command (called under control lock)
1295 */
1296
1297static void gsm_control_transmit(struct gsm_mux *gsm, struct gsm_control *ctrl)
1298{
Eric Bénarded43b472011-03-09 19:24:49 +01001299 struct gsm_msg *msg = gsm_data_alloc(gsm, 0, ctrl->len + 1, gsm->ftype);
Alan Coxe1eaea42010-03-26 11:32:54 +00001300 if (msg == NULL)
1301 return;
1302 msg->data[0] = (ctrl->cmd << 1) | 2 | EA; /* command */
1303 memcpy(msg->data + 1, ctrl->data, ctrl->len);
1304 gsm_data_queue(gsm->dlci[0], msg);
1305}
1306
1307/**
1308 * gsm_control_retransmit - retransmit a control frame
1309 * @data: pointer to our gsm object
1310 *
1311 * Called off the T2 timer expiry in order to retransmit control frames
1312 * that have been lost in the system somewhere. The control_lock protects
1313 * us from colliding with another sender or a receive completion event.
1314 * In that situation the timer may still occur in a small window but
1315 * gsm->pending_cmd will be NULL and we just let the timer expire.
1316 */
1317
Kees Cooke99e88a2017-10-16 14:43:17 -07001318static void gsm_control_retransmit(struct timer_list *t)
Alan Coxe1eaea42010-03-26 11:32:54 +00001319{
Kees Cooke99e88a2017-10-16 14:43:17 -07001320 struct gsm_mux *gsm = from_timer(gsm, t, t2_timer);
Alan Coxe1eaea42010-03-26 11:32:54 +00001321 struct gsm_control *ctrl;
1322 unsigned long flags;
1323 spin_lock_irqsave(&gsm->control_lock, flags);
1324 ctrl = gsm->pending_cmd;
1325 if (ctrl) {
1326 gsm->cretries--;
1327 if (gsm->cretries == 0) {
1328 gsm->pending_cmd = NULL;
1329 ctrl->error = -ETIMEDOUT;
1330 ctrl->done = 1;
1331 spin_unlock_irqrestore(&gsm->control_lock, flags);
1332 wake_up(&gsm->event);
1333 return;
1334 }
1335 gsm_control_transmit(gsm, ctrl);
1336 mod_timer(&gsm->t2_timer, jiffies + gsm->t2 * HZ / 100);
1337 }
1338 spin_unlock_irqrestore(&gsm->control_lock, flags);
1339}
1340
1341/**
1342 * gsm_control_send - send a control frame on DLCI 0
1343 * @gsm: the GSM channel
1344 * @command: command to send including CR bit
1345 * @data: bytes of data (must be kmalloced)
1346 * @len: length of the block to send
1347 *
1348 * Queue and dispatch a control command. Only one command can be
1349 * active at a time. In theory more can be outstanding but the matching
1350 * gets really complicated so for now stick to one outstanding.
1351 */
1352
1353static struct gsm_control *gsm_control_send(struct gsm_mux *gsm,
1354 unsigned int command, u8 *data, int clen)
1355{
1356 struct gsm_control *ctrl = kzalloc(sizeof(struct gsm_control),
1357 GFP_KERNEL);
1358 unsigned long flags;
1359 if (ctrl == NULL)
1360 return NULL;
1361retry:
1362 wait_event(gsm->event, gsm->pending_cmd == NULL);
1363 spin_lock_irqsave(&gsm->control_lock, flags);
1364 if (gsm->pending_cmd != NULL) {
1365 spin_unlock_irqrestore(&gsm->control_lock, flags);
1366 goto retry;
1367 }
1368 ctrl->cmd = command;
1369 ctrl->data = data;
1370 ctrl->len = clen;
1371 gsm->pending_cmd = ctrl;
Tony Lindgrene9ec2252018-04-07 10:19:50 -07001372
1373 /* If DLCI0 is in ADM mode skip retries, it won't respond */
1374 if (gsm->dlci[0]->mode == DLCI_MODE_ADM)
1375 gsm->cretries = 1;
1376 else
1377 gsm->cretries = gsm->n2;
1378
Alan Coxe1eaea42010-03-26 11:32:54 +00001379 mod_timer(&gsm->t2_timer, jiffies + gsm->t2 * HZ / 100);
1380 gsm_control_transmit(gsm, ctrl);
1381 spin_unlock_irqrestore(&gsm->control_lock, flags);
1382 return ctrl;
1383}
1384
1385/**
1386 * gsm_control_wait - wait for a control to finish
1387 * @gsm: GSM mux
1388 * @control: control we are waiting on
1389 *
1390 * Waits for the control to complete or time out. Frees any used
1391 * resources and returns 0 for success, or an error if the remote
1392 * rejected or ignored the request.
1393 */
1394
1395static int gsm_control_wait(struct gsm_mux *gsm, struct gsm_control *control)
1396{
1397 int err;
1398 wait_event(gsm->event, control->done == 1);
1399 err = control->error;
1400 kfree(control);
1401 return err;
1402}
1403
1404
1405/*
1406 * DLCI level handling: Needs krefs
1407 */
1408
1409/*
1410 * State transitions and timers
1411 */
1412
1413/**
1414 * gsm_dlci_close - a DLCI has closed
1415 * @dlci: DLCI that closed
1416 *
1417 * Perform processing when moving a DLCI into closed state. If there
1418 * is an attached tty this is hung up
1419 */
1420
1421static void gsm_dlci_close(struct gsm_dlci *dlci)
1422{
1423 del_timer(&dlci->t1);
1424 if (debug & 8)
Alan Cox5f9a31d2010-11-04 15:17:27 +00001425 pr_debug("DLCI %d goes closed.\n", dlci->addr);
Alan Coxe1eaea42010-03-26 11:32:54 +00001426 dlci->state = DLCI_CLOSED;
1427 if (dlci->addr != 0) {
Jiri Slabyaa27a092013-03-07 13:12:30 +01001428 tty_port_tty_hangup(&dlci->port, false);
Jiri Slaby036bca1fc2020-02-19 09:49:40 +01001429 kfifo_reset(&dlci->fifo);
Alan Coxe1eaea42010-03-26 11:32:54 +00001430 } else
1431 dlci->gsm->dead = 1;
1432 wake_up(&dlci->gsm->event);
1433 /* A DLCI 0 close is a MUX termination so we need to kick that
1434 back to userspace somehow */
1435}
1436
1437/**
1438 * gsm_dlci_open - a DLCI has opened
1439 * @dlci: DLCI that opened
1440 *
1441 * Perform processing when moving a DLCI into open state.
1442 */
1443
1444static void gsm_dlci_open(struct gsm_dlci *dlci)
1445{
1446 /* Note that SABM UA .. SABM UA first UA lost can mean that we go
1447 open -> open */
1448 del_timer(&dlci->t1);
1449 /* This will let a tty open continue */
1450 dlci->state = DLCI_OPEN;
1451 if (debug & 8)
Alan Cox5f9a31d2010-11-04 15:17:27 +00001452 pr_debug("DLCI %d goes open.\n", dlci->addr);
Alan Coxe1eaea42010-03-26 11:32:54 +00001453 wake_up(&dlci->gsm->event);
1454}
1455
1456/**
1457 * gsm_dlci_t1 - T1 timer expiry
1458 * @dlci: DLCI that opened
1459 *
1460 * The T1 timer handles retransmits of control frames (essentially of
1461 * SABM and DISC). We resend the command until the retry count runs out
1462 * in which case an opening port goes back to closed and a closing port
1463 * is simply put into closed state (any further frames from the other
1464 * end will get a DM response)
Tony Lindgrenea3d8462018-01-03 10:18:03 -08001465 *
1466 * Some control dlci can stay in ADM mode with other dlci working just
1467 * fine. In that case we can just keep the control dlci open after the
1468 * DLCI_OPENING retries time out.
Alan Coxe1eaea42010-03-26 11:32:54 +00001469 */
1470
Kees Cooke99e88a2017-10-16 14:43:17 -07001471static void gsm_dlci_t1(struct timer_list *t)
Alan Coxe1eaea42010-03-26 11:32:54 +00001472{
Kees Cooke99e88a2017-10-16 14:43:17 -07001473 struct gsm_dlci *dlci = from_timer(dlci, t, t1);
Alan Coxe1eaea42010-03-26 11:32:54 +00001474 struct gsm_mux *gsm = dlci->gsm;
1475
1476 switch (dlci->state) {
1477 case DLCI_OPENING:
1478 dlci->retries--;
1479 if (dlci->retries) {
1480 gsm_command(dlci->gsm, dlci->addr, SABM|PF);
1481 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
Tony Lindgrenea3d8462018-01-03 10:18:03 -08001482 } else if (!dlci->addr && gsm->control == (DM | PF)) {
1483 if (debug & 8)
1484 pr_info("DLCI %d opening in ADM mode.\n",
1485 dlci->addr);
Tony Lindgrene9ec2252018-04-07 10:19:50 -07001486 dlci->mode = DLCI_MODE_ADM;
Tony Lindgrenea3d8462018-01-03 10:18:03 -08001487 gsm_dlci_open(dlci);
1488 } else {
Alan Coxe1eaea42010-03-26 11:32:54 +00001489 gsm_dlci_close(dlci);
Tony Lindgrenea3d8462018-01-03 10:18:03 -08001490 }
1491
Alan Coxe1eaea42010-03-26 11:32:54 +00001492 break;
1493 case DLCI_CLOSING:
1494 dlci->retries--;
1495 if (dlci->retries) {
1496 gsm_command(dlci->gsm, dlci->addr, DISC|PF);
1497 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
1498 } else
1499 gsm_dlci_close(dlci);
1500 break;
Jiri Slaby72ae8cc2020-02-19 09:49:41 +01001501 default:
1502 pr_debug("%s: unhandled state: %d\n", __func__, dlci->state);
1503 break;
Alan Coxe1eaea42010-03-26 11:32:54 +00001504 }
1505}
1506
1507/**
1508 * gsm_dlci_begin_open - start channel open procedure
1509 * @dlci: DLCI to open
1510 *
1511 * Commence opening a DLCI from the Linux side. We issue SABM messages
Tony Lindgrenea3d8462018-01-03 10:18:03 -08001512 * to the modem which should then reply with a UA or ADM, at which point
1513 * we will move into open state. Opening is done asynchronously with retry
Alan Coxe1eaea42010-03-26 11:32:54 +00001514 * running off timers and the responses.
1515 */
1516
1517static void gsm_dlci_begin_open(struct gsm_dlci *dlci)
1518{
1519 struct gsm_mux *gsm = dlci->gsm;
1520 if (dlci->state == DLCI_OPEN || dlci->state == DLCI_OPENING)
1521 return;
1522 dlci->retries = gsm->n2;
1523 dlci->state = DLCI_OPENING;
1524 gsm_command(dlci->gsm, dlci->addr, SABM|PF);
1525 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
1526}
1527
1528/**
1529 * gsm_dlci_begin_close - start channel open procedure
1530 * @dlci: DLCI to open
1531 *
1532 * Commence closing a DLCI from the Linux side. We issue DISC messages
1533 * to the modem which should then reply with a UA, at which point we
1534 * will move into closed state. Closing is done asynchronously with retry
1535 * off timers. We may also receive a DM reply from the other end which
1536 * indicates the channel was already closed.
1537 */
1538
1539static void gsm_dlci_begin_close(struct gsm_dlci *dlci)
1540{
1541 struct gsm_mux *gsm = dlci->gsm;
1542 if (dlci->state == DLCI_CLOSED || dlci->state == DLCI_CLOSING)
1543 return;
1544 dlci->retries = gsm->n2;
1545 dlci->state = DLCI_CLOSING;
1546 gsm_command(dlci->gsm, dlci->addr, DISC|PF);
1547 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
1548}
1549
1550/**
1551 * gsm_dlci_data - data arrived
1552 * @dlci: channel
1553 * @data: block of bytes received
1554 * @len: length of received block
1555 *
1556 * A UI or UIH frame has arrived which contains data for a channel
1557 * other than the control channel. If the relevant virtual tty is
1558 * open we shovel the bits down it, if not we drop them.
1559 */
1560
Tony Lindgren4feb7a42019-01-13 17:25:27 -08001561static void gsm_dlci_data(struct gsm_dlci *dlci, const u8 *data, int clen)
Alan Coxe1eaea42010-03-26 11:32:54 +00001562{
1563 /* krefs .. */
1564 struct tty_port *port = &dlci->port;
Jiri Slaby2e124b42013-01-03 15:53:06 +01001565 struct tty_struct *tty;
Alan Coxe1eaea42010-03-26 11:32:54 +00001566 unsigned int modem = 0;
Russ Gorby72632872011-06-14 13:23:28 -07001567 int len = clen;
Alan Coxe1eaea42010-03-26 11:32:54 +00001568
1569 if (debug & 16)
Jiri Slaby2e124b42013-01-03 15:53:06 +01001570 pr_debug("%d bytes for tty\n", len);
1571 switch (dlci->adaption) {
1572 /* Unsupported types */
Gustavo A. R. Silva3e913ee2019-02-25 11:28:14 -06001573 case 4: /* Packetised interruptible data */
Jiri Slaby2e124b42013-01-03 15:53:06 +01001574 break;
Gustavo A. R. Silva3e913ee2019-02-25 11:28:14 -06001575 case 3: /* Packetised uininterruptible voice/data */
Jiri Slaby2e124b42013-01-03 15:53:06 +01001576 break;
Gustavo A. R. Silva3e913ee2019-02-25 11:28:14 -06001577 case 2: /* Asynchronous serial with line state in each frame */
Jiri Slaby2e124b42013-01-03 15:53:06 +01001578 while (gsm_read_ea(&modem, *data++) == 0) {
1579 len--;
1580 if (len == 0)
1581 return;
Alan Coxe1eaea42010-03-26 11:32:54 +00001582 }
Jiri Slaby2e124b42013-01-03 15:53:06 +01001583 tty = tty_port_tty_get(port);
1584 if (tty) {
1585 gsm_process_modem(tty, dlci, modem, clen);
1586 tty_kref_put(tty);
1587 }
Gustavo A. R. Silva3e913ee2019-02-25 11:28:14 -06001588 /* Fall through */
1589 case 1: /* Line state will go via DLCI 0 controls only */
Jiri Slaby2e124b42013-01-03 15:53:06 +01001590 default:
1591 tty_insert_flip_string(port, data, len);
1592 tty_flip_buffer_push(port);
Alan Coxe1eaea42010-03-26 11:32:54 +00001593 }
1594}
1595
1596/**
1597 * gsm_dlci_control - data arrived on control channel
1598 * @dlci: channel
1599 * @data: block of bytes received
1600 * @len: length of received block
1601 *
1602 * A UI or UIH frame has arrived which contains data for DLCI 0 the
1603 * control channel. This should contain a command EA followed by
1604 * control data bytes. The command EA contains a command/response bit
1605 * and we divide up the work accordingly.
1606 */
1607
Tony Lindgren4feb7a42019-01-13 17:25:27 -08001608static void gsm_dlci_command(struct gsm_dlci *dlci, const u8 *data, int len)
Alan Coxe1eaea42010-03-26 11:32:54 +00001609{
1610 /* See what command is involved */
1611 unsigned int command = 0;
1612 while (len-- > 0) {
1613 if (gsm_read_ea(&command, *data++) == 1) {
1614 int clen = *data++;
1615 len--;
1616 /* FIXME: this is properly an EA */
1617 clen >>= 1;
1618 /* Malformed command ? */
1619 if (clen > len)
1620 return;
1621 if (command & 1)
1622 gsm_control_message(dlci->gsm, command,
1623 data, clen);
1624 else
1625 gsm_control_response(dlci->gsm, command,
1626 data, clen);
1627 return;
1628 }
1629 }
1630}
1631
1632/*
1633 * Allocate/Free DLCI channels
1634 */
1635
1636/**
1637 * gsm_dlci_alloc - allocate a DLCI
1638 * @gsm: GSM mux
1639 * @addr: address of the DLCI
1640 *
1641 * Allocate and install a new DLCI object into the GSM mux.
1642 *
1643 * FIXME: review locking races
1644 */
1645
1646static struct gsm_dlci *gsm_dlci_alloc(struct gsm_mux *gsm, int addr)
1647{
1648 struct gsm_dlci *dlci = kzalloc(sizeof(struct gsm_dlci), GFP_ATOMIC);
1649 if (dlci == NULL)
1650 return NULL;
1651 spin_lock_init(&dlci->lock);
Russ Gorbybcd5abe2011-06-16 14:20:12 -07001652 mutex_init(&dlci->mutex);
Jiri Slaby036bca1fc2020-02-19 09:49:40 +01001653 if (kfifo_alloc(&dlci->fifo, 4096, GFP_KERNEL) < 0) {
Alan Coxe1eaea42010-03-26 11:32:54 +00001654 kfree(dlci);
1655 return NULL;
1656 }
1657
1658 skb_queue_head_init(&dlci->skb_list);
Kees Cooke99e88a2017-10-16 14:43:17 -07001659 timer_setup(&dlci->t1, gsm_dlci_t1, 0);
Alan Coxe1eaea42010-03-26 11:32:54 +00001660 tty_port_init(&dlci->port);
1661 dlci->port.ops = &gsm_port_ops;
1662 dlci->gsm = gsm;
1663 dlci->addr = addr;
1664 dlci->adaption = gsm->adaption;
1665 dlci->state = DLCI_CLOSED;
1666 if (addr)
1667 dlci->data = gsm_dlci_data;
1668 else
1669 dlci->data = gsm_dlci_command;
1670 gsm->dlci[addr] = dlci;
1671 return dlci;
1672}
1673
1674/**
Russ Gorby6ab8fba2011-06-16 14:20:13 -07001675 * gsm_dlci_free - free DLCI
1676 * @dlci: DLCI to free
Alan Coxe1eaea42010-03-26 11:32:54 +00001677 *
Russ Gorby6ab8fba2011-06-16 14:20:13 -07001678 * Free up a DLCI.
Alan Coxe1eaea42010-03-26 11:32:54 +00001679 *
1680 * Can sleep.
1681 */
Jiri Slaby9a8e62b2012-11-15 09:49:53 +01001682static void gsm_dlci_free(struct tty_port *port)
Russ Gorby6ab8fba2011-06-16 14:20:13 -07001683{
Jiri Slaby9a8e62b2012-11-15 09:49:53 +01001684 struct gsm_dlci *dlci = container_of(port, struct gsm_dlci, port);
Russ Gorby6ab8fba2011-06-16 14:20:13 -07001685
1686 del_timer_sync(&dlci->t1);
1687 dlci->gsm->dlci[dlci->addr] = NULL;
Jiri Slaby036bca1fc2020-02-19 09:49:40 +01001688 kfifo_free(&dlci->fifo);
Russ Gorby6ab8fba2011-06-16 14:20:13 -07001689 while ((dlci->skb = skb_dequeue(&dlci->skb_list)))
Russ Gorby329e5672012-08-13 13:45:15 +01001690 dev_kfree_skb(dlci->skb);
Russ Gorby6ab8fba2011-06-16 14:20:13 -07001691 kfree(dlci);
1692}
1693
1694static inline void dlci_get(struct gsm_dlci *dlci)
1695{
Jiri Slaby9a8e62b2012-11-15 09:49:53 +01001696 tty_port_get(&dlci->port);
Russ Gorby6ab8fba2011-06-16 14:20:13 -07001697}
1698
1699static inline void dlci_put(struct gsm_dlci *dlci)
1700{
Jiri Slaby9a8e62b2012-11-15 09:49:53 +01001701 tty_port_put(&dlci->port);
Russ Gorby6ab8fba2011-06-16 14:20:13 -07001702}
1703
Dirkjan Bussink4d9b1092013-01-30 11:44:50 +01001704static void gsm_destroy_network(struct gsm_dlci *dlci);
1705
Russ Gorby6ab8fba2011-06-16 14:20:13 -07001706/**
1707 * gsm_dlci_release - release DLCI
1708 * @dlci: DLCI to destroy
1709 *
1710 * Release a DLCI. Actual free is deferred until either
1711 * mux is closed or tty is closed - whichever is last.
1712 *
1713 * Can sleep.
1714 */
1715static void gsm_dlci_release(struct gsm_dlci *dlci)
Alan Coxe1eaea42010-03-26 11:32:54 +00001716{
1717 struct tty_struct *tty = tty_port_tty_get(&dlci->port);
1718 if (tty) {
Dirkjan Bussink4d9b1092013-01-30 11:44:50 +01001719 mutex_lock(&dlci->mutex);
1720 gsm_destroy_network(dlci);
1721 mutex_unlock(&dlci->mutex);
1722
Martin Hundebøll70300822019-08-22 23:56:01 +02001723 tty_hangup(tty);
Chuansheng Liube706572013-12-18 13:30:11 +08001724
Dirkjan Bussink4d9b1092013-01-30 11:44:50 +01001725 tty_port_tty_set(&dlci->port, NULL);
Alan Coxe1eaea42010-03-26 11:32:54 +00001726 tty_kref_put(tty);
1727 }
Dirkjan Bussink4d9b1092013-01-30 11:44:50 +01001728 dlci->state = DLCI_CLOSED;
Russ Gorby6ab8fba2011-06-16 14:20:13 -07001729 dlci_put(dlci);
Alan Coxe1eaea42010-03-26 11:32:54 +00001730}
1731
Alan Coxe1eaea42010-03-26 11:32:54 +00001732/*
1733 * LAPBish link layer logic
1734 */
1735
1736/**
1737 * gsm_queue - a GSM frame is ready to process
1738 * @gsm: pointer to our gsm mux
1739 *
1740 * At this point in time a frame has arrived and been demangled from
1741 * the line encoding. All the differences between the encodings have
1742 * been handled below us and the frame is unpacked into the structures.
1743 * The fcs holds the header FCS but any data FCS must be added here.
1744 */
1745
1746static void gsm_queue(struct gsm_mux *gsm)
1747{
1748 struct gsm_dlci *dlci;
1749 u8 cr;
1750 int address;
1751 /* We have to sneak a look at the packet body to do the FCS.
1752 A somewhat layering violation in the spec */
1753
1754 if ((gsm->control & ~PF) == UI)
1755 gsm->fcs = gsm_fcs_add_block(gsm->fcs, gsm->buf, gsm->len);
Aldo Iljazif3c909b2013-07-08 22:28:00 +03001756 if (gsm->encoding == 0) {
1757 /* WARNING: gsm->received_fcs is used for
1758 gsm->encoding = 0 only.
1759 In this case it contain the last piece of data
1760 required to generate final CRC */
Mikhail Kshevetskiy9db4e432011-03-27 04:05:00 +04001761 gsm->fcs = gsm_fcs_add(gsm->fcs, gsm->received_fcs);
1762 }
Alan Coxe1eaea42010-03-26 11:32:54 +00001763 if (gsm->fcs != GOOD_FCS) {
1764 gsm->bad_fcs++;
1765 if (debug & 4)
Alan Cox5f9a31d2010-11-04 15:17:27 +00001766 pr_debug("BAD FCS %02x\n", gsm->fcs);
Alan Coxe1eaea42010-03-26 11:32:54 +00001767 return;
1768 }
1769 address = gsm->address >> 1;
1770 if (address >= NUM_DLCI)
1771 goto invalid;
1772
1773 cr = gsm->address & 1; /* C/R bit */
1774
1775 gsm_print_packet("<--", address, cr, gsm->control, gsm->buf, gsm->len);
1776
1777 cr ^= 1 - gsm->initiator; /* Flip so 1 always means command */
1778 dlci = gsm->dlci[address];
1779
1780 switch (gsm->control) {
1781 case SABM|PF:
1782 if (cr == 0)
1783 goto invalid;
1784 if (dlci == NULL)
1785 dlci = gsm_dlci_alloc(gsm, address);
1786 if (dlci == NULL)
1787 return;
1788 if (dlci->dead)
1789 gsm_response(gsm, address, DM);
1790 else {
1791 gsm_response(gsm, address, UA);
1792 gsm_dlci_open(dlci);
1793 }
1794 break;
1795 case DISC|PF:
1796 if (cr == 0)
1797 goto invalid;
1798 if (dlci == NULL || dlci->state == DLCI_CLOSED) {
1799 gsm_response(gsm, address, DM);
1800 return;
1801 }
1802 /* Real close complete */
1803 gsm_response(gsm, address, UA);
1804 gsm_dlci_close(dlci);
1805 break;
1806 case UA:
1807 case UA|PF:
1808 if (cr == 0 || dlci == NULL)
1809 break;
1810 switch (dlci->state) {
1811 case DLCI_CLOSING:
1812 gsm_dlci_close(dlci);
1813 break;
1814 case DLCI_OPENING:
1815 gsm_dlci_open(dlci);
1816 break;
Jiri Slaby72ae8cc2020-02-19 09:49:41 +01001817 default:
1818 pr_debug("%s: unhandled state: %d\n", __func__,
1819 dlci->state);
1820 break;
Alan Coxe1eaea42010-03-26 11:32:54 +00001821 }
1822 break;
1823 case DM: /* DM can be valid unsolicited */
1824 case DM|PF:
1825 if (cr)
1826 goto invalid;
1827 if (dlci == NULL)
1828 return;
1829 gsm_dlci_close(dlci);
1830 break;
1831 case UI:
1832 case UI|PF:
1833 case UIH:
1834 case UIH|PF:
1835#if 0
1836 if (cr)
1837 goto invalid;
1838#endif
1839 if (dlci == NULL || dlci->state != DLCI_OPEN) {
1840 gsm_command(gsm, address, DM|PF);
1841 return;
1842 }
1843 dlci->data(dlci, gsm->buf, gsm->len);
1844 break;
1845 default:
1846 goto invalid;
1847 }
1848 return;
1849invalid:
1850 gsm->malformed++;
1851 return;
1852}
1853
1854
1855/**
1856 * gsm0_receive - perform processing for non-transparency
1857 * @gsm: gsm data for this ldisc instance
1858 * @c: character
1859 *
1860 * Receive bytes in gsm mode 0
1861 */
1862
1863static void gsm0_receive(struct gsm_mux *gsm, unsigned char c)
1864{
Alan Coxc2f2f002010-11-04 15:17:03 +00001865 unsigned int len;
1866
Alan Coxe1eaea42010-03-26 11:32:54 +00001867 switch (gsm->state) {
1868 case GSM_SEARCH: /* SOF marker */
1869 if (c == GSM0_SOF) {
1870 gsm->state = GSM_ADDRESS;
1871 gsm->address = 0;
1872 gsm->len = 0;
1873 gsm->fcs = INIT_FCS;
1874 }
Alan Coxc2f2f002010-11-04 15:17:03 +00001875 break;
1876 case GSM_ADDRESS: /* Address EA */
Alan Coxe1eaea42010-03-26 11:32:54 +00001877 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
1878 if (gsm_read_ea(&gsm->address, c))
1879 gsm->state = GSM_CONTROL;
1880 break;
1881 case GSM_CONTROL: /* Control Byte */
1882 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
1883 gsm->control = c;
Alan Coxc2f2f002010-11-04 15:17:03 +00001884 gsm->state = GSM_LEN0;
Alan Coxe1eaea42010-03-26 11:32:54 +00001885 break;
Alan Coxc2f2f002010-11-04 15:17:03 +00001886 case GSM_LEN0: /* Length EA */
Alan Coxe1eaea42010-03-26 11:32:54 +00001887 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
1888 if (gsm_read_ea(&gsm->len, c)) {
1889 if (gsm->len > gsm->mru) {
1890 gsm->bad_size++;
1891 gsm->state = GSM_SEARCH;
1892 break;
1893 }
1894 gsm->count = 0;
Alan Coxc2f2f002010-11-04 15:17:03 +00001895 if (!gsm->len)
1896 gsm->state = GSM_FCS;
1897 else
1898 gsm->state = GSM_DATA;
1899 break;
Alan Coxe1eaea42010-03-26 11:32:54 +00001900 }
Alan Coxc2f2f002010-11-04 15:17:03 +00001901 gsm->state = GSM_LEN1;
1902 break;
1903 case GSM_LEN1:
1904 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
1905 len = c;
1906 gsm->len |= len << 7;
1907 if (gsm->len > gsm->mru) {
1908 gsm->bad_size++;
1909 gsm->state = GSM_SEARCH;
1910 break;
1911 }
1912 gsm->count = 0;
1913 if (!gsm->len)
1914 gsm->state = GSM_FCS;
1915 else
1916 gsm->state = GSM_DATA;
Alan Coxe1eaea42010-03-26 11:32:54 +00001917 break;
1918 case GSM_DATA: /* Data */
1919 gsm->buf[gsm->count++] = c;
1920 if (gsm->count == gsm->len)
1921 gsm->state = GSM_FCS;
1922 break;
1923 case GSM_FCS: /* FCS follows the packet */
Alan Coxc2f2f002010-11-04 15:17:03 +00001924 gsm->received_fcs = c;
Alan Coxe1eaea42010-03-26 11:32:54 +00001925 gsm_queue(gsm);
Alan Coxc2f2f002010-11-04 15:17:03 +00001926 gsm->state = GSM_SSOF;
1927 break;
1928 case GSM_SSOF:
1929 if (c == GSM0_SOF) {
1930 gsm->state = GSM_SEARCH;
1931 break;
1932 }
Alan Coxe1eaea42010-03-26 11:32:54 +00001933 break;
1934 }
1935}
1936
1937/**
Alan Coxc2f2f002010-11-04 15:17:03 +00001938 * gsm1_receive - perform processing for non-transparency
Alan Coxe1eaea42010-03-26 11:32:54 +00001939 * @gsm: gsm data for this ldisc instance
1940 * @c: character
1941 *
1942 * Receive bytes in mode 1 (Advanced option)
1943 */
1944
1945static void gsm1_receive(struct gsm_mux *gsm, unsigned char c)
1946{
1947 if (c == GSM1_SOF) {
1948 /* EOF is only valid in frame if we have got to the data state
1949 and received at least one byte (the FCS) */
1950 if (gsm->state == GSM_DATA && gsm->count) {
1951 /* Extract the FCS */
1952 gsm->count--;
1953 gsm->fcs = gsm_fcs_add(gsm->fcs, gsm->buf[gsm->count]);
1954 gsm->len = gsm->count;
1955 gsm_queue(gsm);
1956 gsm->state = GSM_START;
1957 return;
1958 }
1959 /* Any partial frame was a runt so go back to start */
1960 if (gsm->state != GSM_START) {
1961 gsm->malformed++;
1962 gsm->state = GSM_START;
1963 }
1964 /* A SOF in GSM_START means we are still reading idling or
1965 framing bytes */
1966 return;
1967 }
1968
1969 if (c == GSM1_ESCAPE) {
1970 gsm->escape = 1;
1971 return;
1972 }
1973
1974 /* Only an unescaped SOF gets us out of GSM search */
1975 if (gsm->state == GSM_SEARCH)
1976 return;
1977
1978 if (gsm->escape) {
1979 c ^= GSM1_ESCAPE_BITS;
1980 gsm->escape = 0;
1981 }
1982 switch (gsm->state) {
1983 case GSM_START: /* First byte after SOF */
1984 gsm->address = 0;
1985 gsm->state = GSM_ADDRESS;
1986 gsm->fcs = INIT_FCS;
Gustavo A. R. Silva3e913ee2019-02-25 11:28:14 -06001987 /* Fall through */
Alan Coxe1eaea42010-03-26 11:32:54 +00001988 case GSM_ADDRESS: /* Address continuation */
1989 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
1990 if (gsm_read_ea(&gsm->address, c))
1991 gsm->state = GSM_CONTROL;
1992 break;
1993 case GSM_CONTROL: /* Control Byte */
1994 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
1995 gsm->control = c;
1996 gsm->count = 0;
1997 gsm->state = GSM_DATA;
1998 break;
1999 case GSM_DATA: /* Data */
Alan Cox5f9a31d2010-11-04 15:17:27 +00002000 if (gsm->count > gsm->mru) { /* Allow one for the FCS */
Alan Coxe1eaea42010-03-26 11:32:54 +00002001 gsm->state = GSM_OVERRUN;
2002 gsm->bad_size++;
2003 } else
2004 gsm->buf[gsm->count++] = c;
2005 break;
2006 case GSM_OVERRUN: /* Over-long - eg a dropped SOF */
2007 break;
2008 }
2009}
2010
2011/**
2012 * gsm_error - handle tty error
2013 * @gsm: ldisc data
2014 * @data: byte received (may be invalid)
2015 * @flag: error received
2016 *
2017 * Handle an error in the receipt of data for a frame. Currently we just
2018 * go back to hunting for a SOF.
2019 *
2020 * FIXME: better diagnostics ?
2021 */
2022
2023static void gsm_error(struct gsm_mux *gsm,
2024 unsigned char data, unsigned char flag)
2025{
2026 gsm->state = GSM_SEARCH;
2027 gsm->io_error++;
2028}
2029
Sascha Hauer71e07792017-05-31 08:19:05 +02002030static int gsm_disconnect(struct gsm_mux *gsm)
2031{
2032 struct gsm_dlci *dlci = gsm->dlci[0];
2033 struct gsm_control *gc;
2034
2035 if (!dlci)
2036 return 0;
2037
2038 /* In theory disconnecting DLCI 0 is sufficient but for some
2039 modems this is apparently not the case. */
2040 gc = gsm_control_send(gsm, CMD_CLD, NULL, 0);
2041 if (gc)
2042 gsm_control_wait(gsm, gc);
2043
2044 del_timer_sync(&gsm->t2_timer);
2045 /* Now we are sure T2 has stopped */
2046
2047 gsm_dlci_begin_close(dlci);
2048 wait_event_interruptible(gsm->event,
2049 dlci->state == DLCI_CLOSED);
2050
2051 if (signal_pending(current))
2052 return -EINTR;
2053
2054 return 0;
2055}
2056
Alan Coxe1eaea42010-03-26 11:32:54 +00002057/**
2058 * gsm_cleanup_mux - generic GSM protocol cleanup
2059 * @gsm: our mux
2060 *
2061 * Clean up the bits of the mux which are the same for all framing
2062 * protocols. Remove the mux from the mux table, stop all the timers
2063 * and then shut down each device hanging up the channels as we go.
2064 */
2065
Rashika Kheria54af5832013-12-16 16:28:24 +05302066static void gsm_cleanup_mux(struct gsm_mux *gsm)
Alan Coxe1eaea42010-03-26 11:32:54 +00002067{
2068 int i;
2069 struct gsm_dlci *dlci = gsm->dlci[0];
Russ Gorby329e5672012-08-13 13:45:15 +01002070 struct gsm_msg *txq, *ntxq;
Alan Coxe1eaea42010-03-26 11:32:54 +00002071
2072 gsm->dead = 1;
2073
2074 spin_lock(&gsm_mux_lock);
2075 for (i = 0; i < MAX_MUX; i++) {
2076 if (gsm_mux[i] == gsm) {
2077 gsm_mux[i] = NULL;
2078 break;
2079 }
2080 }
2081 spin_unlock(&gsm_mux_lock);
Jiri Slabyd175fec2016-03-22 18:09:51 +01002082 /* open failed before registering => nothing to do */
2083 if (i == MAX_MUX)
2084 return;
Alan Coxe1eaea42010-03-26 11:32:54 +00002085
2086 del_timer_sync(&gsm->t2_timer);
2087 /* Now we are sure T2 has stopped */
Sascha Hauer71e07792017-05-31 08:19:05 +02002088 if (dlci)
Alan Coxe1eaea42010-03-26 11:32:54 +00002089 dlci->dead = 1;
Sascha Hauer71e07792017-05-31 08:19:05 +02002090
Alan Coxe1eaea42010-03-26 11:32:54 +00002091 /* Free up any link layer users */
Chao Bidfabf7f2013-11-26 12:09:39 +08002092 mutex_lock(&gsm->mutex);
Alan Coxe1eaea42010-03-26 11:32:54 +00002093 for (i = 0; i < NUM_DLCI; i++)
2094 if (gsm->dlci[i])
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002095 gsm_dlci_release(gsm->dlci[i]);
Chao Bidfabf7f2013-11-26 12:09:39 +08002096 mutex_unlock(&gsm->mutex);
Alan Coxe1eaea42010-03-26 11:32:54 +00002097 /* Now wipe the queues */
Russ Gorbyb4338e12012-08-13 13:44:59 +01002098 list_for_each_entry_safe(txq, ntxq, &gsm->tx_list, list)
Alan Coxe1eaea42010-03-26 11:32:54 +00002099 kfree(txq);
Russ Gorbyb4338e12012-08-13 13:44:59 +01002100 INIT_LIST_HEAD(&gsm->tx_list);
Alan Coxe1eaea42010-03-26 11:32:54 +00002101}
Alan Coxe1eaea42010-03-26 11:32:54 +00002102
2103/**
2104 * gsm_activate_mux - generic GSM setup
2105 * @gsm: our mux
2106 *
2107 * Set up the bits of the mux which are the same for all framing
2108 * protocols. Add the mux to the mux table so it can be opened and
2109 * finally kick off connecting to DLCI 0 on the modem.
2110 */
2111
Rashika Kheria54af5832013-12-16 16:28:24 +05302112static int gsm_activate_mux(struct gsm_mux *gsm)
Alan Coxe1eaea42010-03-26 11:32:54 +00002113{
2114 struct gsm_dlci *dlci;
2115 int i = 0;
2116
Kees Cooke99e88a2017-10-16 14:43:17 -07002117 timer_setup(&gsm->t2_timer, gsm_control_retransmit, 0);
Alan Coxe1eaea42010-03-26 11:32:54 +00002118 init_waitqueue_head(&gsm->event);
2119 spin_lock_init(&gsm->control_lock);
2120 spin_lock_init(&gsm->tx_lock);
2121
2122 if (gsm->encoding == 0)
2123 gsm->receive = gsm0_receive;
2124 else
2125 gsm->receive = gsm1_receive;
2126 gsm->error = gsm_error;
2127
2128 spin_lock(&gsm_mux_lock);
2129 for (i = 0; i < MAX_MUX; i++) {
2130 if (gsm_mux[i] == NULL) {
Russ Gorbyd50f6dc2011-06-14 13:35:32 -07002131 gsm->num = i;
Alan Coxe1eaea42010-03-26 11:32:54 +00002132 gsm_mux[i] = gsm;
2133 break;
2134 }
2135 }
2136 spin_unlock(&gsm_mux_lock);
2137 if (i == MAX_MUX)
2138 return -EBUSY;
2139
2140 dlci = gsm_dlci_alloc(gsm, 0);
2141 if (dlci == NULL)
2142 return -ENOMEM;
2143 gsm->dead = 0; /* Tty opens are now permissible */
2144 return 0;
2145}
Alan Coxe1eaea42010-03-26 11:32:54 +00002146
2147/**
2148 * gsm_free_mux - free up a mux
2149 * @mux: mux to free
2150 *
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002151 * Dispose of allocated resources for a dead mux
Alan Coxe1eaea42010-03-26 11:32:54 +00002152 */
Rashika Kheria54af5832013-12-16 16:28:24 +05302153static void gsm_free_mux(struct gsm_mux *gsm)
Alan Coxe1eaea42010-03-26 11:32:54 +00002154{
2155 kfree(gsm->txframe);
2156 kfree(gsm->buf);
2157 kfree(gsm);
2158}
Alan Coxe1eaea42010-03-26 11:32:54 +00002159
2160/**
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002161 * gsm_free_muxr - free up a mux
2162 * @mux: mux to free
2163 *
2164 * Dispose of allocated resources for a dead mux
2165 */
2166static void gsm_free_muxr(struct kref *ref)
2167{
2168 struct gsm_mux *gsm = container_of(ref, struct gsm_mux, ref);
2169 gsm_free_mux(gsm);
2170}
2171
2172static inline void mux_get(struct gsm_mux *gsm)
2173{
2174 kref_get(&gsm->ref);
2175}
2176
2177static inline void mux_put(struct gsm_mux *gsm)
2178{
2179 kref_put(&gsm->ref, gsm_free_muxr);
2180}
2181
Martin Hundebøll43a9e712019-07-10 21:26:55 +02002182static inline unsigned int mux_num_to_base(struct gsm_mux *gsm)
2183{
2184 return gsm->num * NUM_DLCI;
2185}
2186
2187static inline unsigned int mux_line_to_num(unsigned int line)
2188{
2189 return line / NUM_DLCI;
2190}
2191
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002192/**
Alan Coxe1eaea42010-03-26 11:32:54 +00002193 * gsm_alloc_mux - allocate a mux
2194 *
2195 * Creates a new mux ready for activation.
2196 */
2197
Rashika Kheria54af5832013-12-16 16:28:24 +05302198static struct gsm_mux *gsm_alloc_mux(void)
Alan Coxe1eaea42010-03-26 11:32:54 +00002199{
2200 struct gsm_mux *gsm = kzalloc(sizeof(struct gsm_mux), GFP_KERNEL);
2201 if (gsm == NULL)
2202 return NULL;
2203 gsm->buf = kmalloc(MAX_MRU + 1, GFP_KERNEL);
2204 if (gsm->buf == NULL) {
2205 kfree(gsm);
2206 return NULL;
2207 }
2208 gsm->txframe = kmalloc(2 * MAX_MRU + 2, GFP_KERNEL);
2209 if (gsm->txframe == NULL) {
2210 kfree(gsm->buf);
2211 kfree(gsm);
2212 return NULL;
2213 }
2214 spin_lock_init(&gsm->lock);
Chao Bidfabf7f2013-11-26 12:09:39 +08002215 mutex_init(&gsm->mutex);
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002216 kref_init(&gsm->ref);
Russ Gorbyb4338e12012-08-13 13:44:59 +01002217 INIT_LIST_HEAD(&gsm->tx_list);
Alan Coxe1eaea42010-03-26 11:32:54 +00002218
2219 gsm->t1 = T1;
2220 gsm->t2 = T2;
2221 gsm->n2 = N2;
2222 gsm->ftype = UIH;
Alan Coxe1eaea42010-03-26 11:32:54 +00002223 gsm->adaption = 1;
2224 gsm->encoding = 1;
2225 gsm->mru = 64; /* Default to encoding 1 so these should be 64 */
2226 gsm->mtu = 64;
2227 gsm->dead = 1; /* Avoid early tty opens */
2228
2229 return gsm;
2230}
Alan Coxe1eaea42010-03-26 11:32:54 +00002231
Tony Lindgren33841042019-01-13 17:25:26 -08002232static void gsm_copy_config_values(struct gsm_mux *gsm,
2233 struct gsm_config *c)
2234{
2235 memset(c, 0, sizeof(*c));
2236 c->adaption = gsm->adaption;
2237 c->encapsulation = gsm->encoding;
2238 c->initiator = gsm->initiator;
2239 c->t1 = gsm->t1;
2240 c->t2 = gsm->t2;
2241 c->t3 = 0; /* Not supported */
2242 c->n2 = gsm->n2;
2243 if (gsm->ftype == UIH)
2244 c->i = 1;
2245 else
2246 c->i = 2;
2247 pr_debug("Ftype %d i %d\n", gsm->ftype, c->i);
2248 c->mru = gsm->mru;
2249 c->mtu = gsm->mtu;
2250 c->k = 0;
2251}
2252
2253static int gsm_config(struct gsm_mux *gsm, struct gsm_config *c)
2254{
2255 int need_close = 0;
2256 int need_restart = 0;
2257
2258 /* Stuff we don't support yet - UI or I frame transport, windowing */
2259 if ((c->adaption != 1 && c->adaption != 2) || c->k)
2260 return -EOPNOTSUPP;
2261 /* Check the MRU/MTU range looks sane */
2262 if (c->mru > MAX_MRU || c->mtu > MAX_MTU || c->mru < 8 || c->mtu < 8)
2263 return -EINVAL;
2264 if (c->n2 < 3)
2265 return -EINVAL;
2266 if (c->encapsulation > 1) /* Basic, advanced, no I */
2267 return -EINVAL;
2268 if (c->initiator > 1)
2269 return -EINVAL;
2270 if (c->i == 0 || c->i > 2) /* UIH and UI only */
2271 return -EINVAL;
2272 /*
2273 * See what is needed for reconfiguration
2274 */
2275
2276 /* Timing fields */
2277 if (c->t1 != 0 && c->t1 != gsm->t1)
2278 need_restart = 1;
2279 if (c->t2 != 0 && c->t2 != gsm->t2)
2280 need_restart = 1;
2281 if (c->encapsulation != gsm->encoding)
2282 need_restart = 1;
2283 if (c->adaption != gsm->adaption)
2284 need_restart = 1;
2285 /* Requires care */
2286 if (c->initiator != gsm->initiator)
2287 need_close = 1;
2288 if (c->mru != gsm->mru)
2289 need_restart = 1;
2290 if (c->mtu != gsm->mtu)
2291 need_restart = 1;
2292
2293 /*
2294 * Close down what is needed, restart and initiate the new
2295 * configuration
2296 */
2297
2298 if (need_close || need_restart) {
2299 int ret;
2300
2301 ret = gsm_disconnect(gsm);
2302
2303 if (ret)
2304 return ret;
2305 }
2306 if (need_restart)
2307 gsm_cleanup_mux(gsm);
2308
2309 gsm->initiator = c->initiator;
2310 gsm->mru = c->mru;
2311 gsm->mtu = c->mtu;
2312 gsm->encoding = c->encapsulation;
2313 gsm->adaption = c->adaption;
2314 gsm->n2 = c->n2;
2315
2316 if (c->i == 1)
2317 gsm->ftype = UIH;
2318 else if (c->i == 2)
2319 gsm->ftype = UI;
2320
2321 if (c->t1)
2322 gsm->t1 = c->t1;
2323 if (c->t2)
2324 gsm->t2 = c->t2;
2325
2326 /*
2327 * FIXME: We need to separate activation/deactivation from adding
2328 * and removing from the mux array
2329 */
2330 if (need_restart)
2331 gsm_activate_mux(gsm);
2332 if (gsm->initiator && need_close)
2333 gsm_dlci_begin_open(gsm->dlci[0]);
2334 return 0;
2335}
2336
Alan Coxe1eaea42010-03-26 11:32:54 +00002337/**
2338 * gsmld_output - write to link
2339 * @gsm: our mux
2340 * @data: bytes to output
2341 * @len: size
2342 *
2343 * Write a block of data from the GSM mux to the data channel. This
2344 * will eventually be serialized from above but at the moment isn't.
2345 */
2346
2347static int gsmld_output(struct gsm_mux *gsm, u8 *data, int len)
2348{
2349 if (tty_write_room(gsm->tty) < len) {
2350 set_bit(TTY_DO_WRITE_WAKEUP, &gsm->tty->flags);
2351 return -ENOSPC;
2352 }
Joe Perches0a77c4f2011-04-25 16:46:49 -07002353 if (debug & 4)
2354 print_hex_dump_bytes("gsmld_output: ", DUMP_PREFIX_OFFSET,
2355 data, len);
Alan Coxe1eaea42010-03-26 11:32:54 +00002356 gsm->tty->ops->write(gsm->tty, data, len);
2357 return len;
2358}
2359
2360/**
2361 * gsmld_attach_gsm - mode set up
2362 * @tty: our tty structure
2363 * @gsm: our mux
2364 *
2365 * Set up the MUX for basic mode and commence connecting to the
2366 * modem. Currently called from the line discipline set up but
2367 * will need moving to an ioctl path.
2368 */
2369
2370static int gsmld_attach_gsm(struct tty_struct *tty, struct gsm_mux *gsm)
2371{
Martin Hundebøll43a9e712019-07-10 21:26:55 +02002372 unsigned int base;
2373 int ret, i;
Alan Coxe1eaea42010-03-26 11:32:54 +00002374
2375 gsm->tty = tty_kref_get(tty);
2376 gsm->output = gsmld_output;
2377 ret = gsm_activate_mux(gsm);
2378 if (ret != 0)
2379 tty_kref_put(gsm->tty);
Russ Gorbyd50f6dc2011-06-14 13:35:32 -07002380 else {
2381 /* Don't register device 0 - this is the control channel and not
2382 a usable tty interface */
Martin Hundebøll43a9e712019-07-10 21:26:55 +02002383 base = mux_num_to_base(gsm); /* Base for this MUX */
Russ Gorbyd50f6dc2011-06-14 13:35:32 -07002384 for (i = 1; i < NUM_DLCI; i++)
2385 tty_register_device(gsm_tty_driver, base + i, NULL);
2386 }
Alan Coxe1eaea42010-03-26 11:32:54 +00002387 return ret;
2388}
2389
2390
2391/**
2392 * gsmld_detach_gsm - stop doing 0710 mux
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02002393 * @tty: tty attached to the mux
Alan Coxe1eaea42010-03-26 11:32:54 +00002394 * @gsm: mux
2395 *
2396 * Shutdown and then clean up the resources used by the line discipline
2397 */
2398
2399static void gsmld_detach_gsm(struct tty_struct *tty, struct gsm_mux *gsm)
2400{
Martin Hundebøll43a9e712019-07-10 21:26:55 +02002401 unsigned int base = mux_num_to_base(gsm); /* Base for this MUX */
Russ Gorbyd50f6dc2011-06-14 13:35:32 -07002402 int i;
Russ Gorbyd50f6dc2011-06-14 13:35:32 -07002403
Alan Coxe1eaea42010-03-26 11:32:54 +00002404 WARN_ON(tty != gsm->tty);
Russ Gorbyd50f6dc2011-06-14 13:35:32 -07002405 for (i = 1; i < NUM_DLCI; i++)
2406 tty_unregister_device(gsm_tty_driver, base + i);
Alan Coxe1eaea42010-03-26 11:32:54 +00002407 gsm_cleanup_mux(gsm);
2408 tty_kref_put(gsm->tty);
2409 gsm->tty = NULL;
2410}
2411
Linus Torvalds55db4c62011-06-04 06:33:24 +09002412static void gsmld_receive_buf(struct tty_struct *tty, const unsigned char *cp,
2413 char *fp, int count)
Alan Coxe1eaea42010-03-26 11:32:54 +00002414{
2415 struct gsm_mux *gsm = tty->disc_data;
2416 const unsigned char *dp;
2417 char *f;
2418 int i;
Peter Hurley82f91fe2013-12-02 13:56:03 -05002419 char flags = TTY_NORMAL;
Alan Coxe1eaea42010-03-26 11:32:54 +00002420
Joe Perches0a77c4f2011-04-25 16:46:49 -07002421 if (debug & 4)
2422 print_hex_dump_bytes("gsmld_receive: ", DUMP_PREFIX_OFFSET,
2423 cp, count);
Alan Coxe1eaea42010-03-26 11:32:54 +00002424
2425 for (i = count, dp = cp, f = fp; i; i--, dp++) {
Peter Hurley82f91fe2013-12-02 13:56:03 -05002426 if (f)
2427 flags = *f++;
Alan Coxe1eaea42010-03-26 11:32:54 +00002428 switch (flags) {
2429 case TTY_NORMAL:
2430 gsm->receive(gsm, *dp);
2431 break;
2432 case TTY_OVERRUN:
2433 case TTY_BREAK:
2434 case TTY_PARITY:
2435 case TTY_FRAME:
2436 gsm->error(gsm, *dp, flags);
2437 break;
2438 default:
Frederic Beratc01af4f2012-08-13 13:43:58 +01002439 WARN_ONCE(1, "%s: unknown flag %d\n",
Rasmus Villemoes429b4742015-03-31 15:55:59 +02002440 tty_name(tty), flags);
Alan Coxe1eaea42010-03-26 11:32:54 +00002441 break;
2442 }
2443 }
2444 /* FASYNC if needed ? */
2445 /* If clogged call tty_throttle(tty); */
2446}
2447
2448/**
Alan Coxe1eaea42010-03-26 11:32:54 +00002449 * gsmld_flush_buffer - clean input queue
2450 * @tty: terminal device
2451 *
2452 * Flush the input buffer. Called when the line discipline is
2453 * being closed, when the tty layer wants the buffer flushed (eg
2454 * at hangup).
2455 */
2456
2457static void gsmld_flush_buffer(struct tty_struct *tty)
2458{
2459}
2460
2461/**
2462 * gsmld_close - close the ldisc for this tty
2463 * @tty: device
2464 *
2465 * Called from the terminal layer when this line discipline is
2466 * being shut down, either because of a close or becsuse of a
2467 * discipline change. The function will not be called while other
2468 * ldisc methods are in progress.
2469 */
2470
2471static void gsmld_close(struct tty_struct *tty)
2472{
2473 struct gsm_mux *gsm = tty->disc_data;
2474
2475 gsmld_detach_gsm(tty, gsm);
2476
2477 gsmld_flush_buffer(tty);
2478 /* Do other clean up here */
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002479 mux_put(gsm);
Alan Coxe1eaea42010-03-26 11:32:54 +00002480}
2481
2482/**
2483 * gsmld_open - open an ldisc
2484 * @tty: terminal to open
2485 *
2486 * Called when this line discipline is being attached to the
2487 * terminal device. Can sleep. Called serialized so that no
2488 * other events will occur in parallel. No further open will occur
2489 * until a close.
2490 */
2491
2492static int gsmld_open(struct tty_struct *tty)
2493{
2494 struct gsm_mux *gsm;
xinhui.pan5a640962014-07-28 16:14:52 +08002495 int ret;
Alan Coxe1eaea42010-03-26 11:32:54 +00002496
2497 if (tty->ops->write == NULL)
2498 return -EINVAL;
2499
2500 /* Attach our ldisc data */
2501 gsm = gsm_alloc_mux();
2502 if (gsm == NULL)
2503 return -ENOMEM;
2504
2505 tty->disc_data = gsm;
2506 tty->receive_room = 65536;
2507
2508 /* Attach the initial passive connection */
2509 gsm->encoding = 1;
xinhui.pan5a640962014-07-28 16:14:52 +08002510
2511 ret = gsmld_attach_gsm(tty, gsm);
2512 if (ret != 0) {
2513 gsm_cleanup_mux(gsm);
2514 mux_put(gsm);
2515 }
2516 return ret;
Alan Coxe1eaea42010-03-26 11:32:54 +00002517}
2518
2519/**
2520 * gsmld_write_wakeup - asynchronous I/O notifier
2521 * @tty: tty device
2522 *
2523 * Required for the ptys, serial driver etc. since processes
2524 * that attach themselves to the master and rely on ASYNC
2525 * IO must be woken up
2526 */
2527
2528static void gsmld_write_wakeup(struct tty_struct *tty)
2529{
2530 struct gsm_mux *gsm = tty->disc_data;
Dan Carpenter328be392010-05-25 11:37:17 +02002531 unsigned long flags;
Alan Coxe1eaea42010-03-26 11:32:54 +00002532
2533 /* Queue poll */
2534 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Russ Gorby5e447082012-08-13 13:44:40 +01002535 spin_lock_irqsave(&gsm->tx_lock, flags);
Alan Coxe1eaea42010-03-26 11:32:54 +00002536 gsm_data_kick(gsm);
Dan Carpenter328be392010-05-25 11:37:17 +02002537 if (gsm->tx_bytes < TX_THRESH_LO) {
Alan Coxe1eaea42010-03-26 11:32:54 +00002538 gsm_dlci_data_sweep(gsm);
Dan Carpenter328be392010-05-25 11:37:17 +02002539 }
Russ Gorby5e447082012-08-13 13:44:40 +01002540 spin_unlock_irqrestore(&gsm->tx_lock, flags);
Alan Coxe1eaea42010-03-26 11:32:54 +00002541}
2542
2543/**
2544 * gsmld_read - read function for tty
2545 * @tty: tty device
2546 * @file: file object
2547 * @buf: userspace buffer pointer
2548 * @nr: size of I/O
2549 *
2550 * Perform reads for the line discipline. We are guaranteed that the
2551 * line discipline will not be closed under us but we may get multiple
2552 * parallel readers and must handle this ourselves. We may also get
2553 * a hangup. Always called in user context, may sleep.
2554 *
2555 * This code must be sure never to sleep through a hangup.
2556 */
2557
2558static ssize_t gsmld_read(struct tty_struct *tty, struct file *file,
2559 unsigned char __user *buf, size_t nr)
2560{
2561 return -EOPNOTSUPP;
2562}
2563
2564/**
2565 * gsmld_write - write function for tty
2566 * @tty: tty device
2567 * @file: file object
2568 * @buf: userspace buffer pointer
2569 * @nr: size of I/O
2570 *
2571 * Called when the owner of the device wants to send a frame
2572 * itself (or some other control data). The data is transferred
2573 * as-is and must be properly framed and checksummed as appropriate
2574 * by userspace. Frames are either sent whole or not at all as this
2575 * avoids pain user side.
2576 */
2577
2578static ssize_t gsmld_write(struct tty_struct *tty, struct file *file,
2579 const unsigned char *buf, size_t nr)
2580{
2581 int space = tty_write_room(tty);
2582 if (space >= nr)
2583 return tty->ops->write(tty, buf, nr);
2584 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
2585 return -ENOBUFS;
2586}
2587
2588/**
2589 * gsmld_poll - poll method for N_GSM0710
2590 * @tty: terminal device
2591 * @file: file accessing it
2592 * @wait: poll table
2593 *
2594 * Called when the line discipline is asked to poll() for data or
2595 * for special events. This code is not serialized with respect to
2596 * other events save open/close.
2597 *
2598 * This code must be sure never to sleep through a hangup.
2599 * Called without the kernel lock held - fine
2600 */
2601
Al Viroafc9a422017-07-03 06:39:46 -04002602static __poll_t gsmld_poll(struct tty_struct *tty, struct file *file,
Alan Coxe1eaea42010-03-26 11:32:54 +00002603 poll_table *wait)
2604{
Al Viroafc9a422017-07-03 06:39:46 -04002605 __poll_t mask = 0;
Alan Coxe1eaea42010-03-26 11:32:54 +00002606 struct gsm_mux *gsm = tty->disc_data;
2607
2608 poll_wait(file, &tty->read_wait, wait);
2609 poll_wait(file, &tty->write_wait, wait);
2610 if (tty_hung_up_p(file))
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002611 mask |= EPOLLHUP;
Alan Coxe1eaea42010-03-26 11:32:54 +00002612 if (!tty_is_writelocked(tty) && tty_write_room(tty) > 0)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002613 mask |= EPOLLOUT | EPOLLWRNORM;
Alan Coxe1eaea42010-03-26 11:32:54 +00002614 if (gsm->dead)
Linus Torvaldsa9a08842018-02-11 14:34:03 -08002615 mask |= EPOLLHUP;
Alan Coxe1eaea42010-03-26 11:32:54 +00002616 return mask;
2617}
2618
Alan Coxe1eaea42010-03-26 11:32:54 +00002619static int gsmld_ioctl(struct tty_struct *tty, struct file *file,
2620 unsigned int cmd, unsigned long arg)
2621{
2622 struct gsm_config c;
2623 struct gsm_mux *gsm = tty->disc_data;
Martin Hundebølla7b121b2019-08-12 23:12:43 +02002624 unsigned int base;
Alan Coxe1eaea42010-03-26 11:32:54 +00002625
2626 switch (cmd) {
2627 case GSMIOC_GETCONF:
Tony Lindgren33841042019-01-13 17:25:26 -08002628 gsm_copy_config_values(gsm, &c);
Alan Coxe1eaea42010-03-26 11:32:54 +00002629 if (copy_to_user((void *)arg, &c, sizeof(c)))
2630 return -EFAULT;
2631 return 0;
2632 case GSMIOC_SETCONF:
2633 if (copy_from_user(&c, (void *)arg, sizeof(c)))
2634 return -EFAULT;
Tony Lindgren33841042019-01-13 17:25:26 -08002635 return gsm_config(gsm, &c);
Martin Hundebølla7b121b2019-08-12 23:12:43 +02002636 case GSMIOC_GETFIRST:
2637 base = mux_num_to_base(gsm);
2638 return put_user(base + 1, (__u32 __user *)arg);
Alan Coxe1eaea42010-03-26 11:32:54 +00002639 default:
2640 return n_tty_ioctl_helper(tty, file, cmd, arg);
2641 }
2642}
2643
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002644/*
2645 * Network interface
2646 *
2647 */
2648
2649static int gsm_mux_net_open(struct net_device *net)
2650{
2651 pr_debug("%s called\n", __func__);
2652 netif_start_queue(net);
2653 return 0;
2654}
2655
2656static int gsm_mux_net_close(struct net_device *net)
2657{
2658 netif_stop_queue(net);
2659 return 0;
2660}
2661
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002662static void dlci_net_free(struct gsm_dlci *dlci)
2663{
2664 if (!dlci->net) {
2665 WARN_ON(1);
2666 return;
2667 }
2668 dlci->adaption = dlci->prev_adaption;
2669 dlci->data = dlci->prev_data;
2670 free_netdev(dlci->net);
2671 dlci->net = NULL;
2672}
2673static void net_free(struct kref *ref)
2674{
2675 struct gsm_mux_net *mux_net;
2676 struct gsm_dlci *dlci;
2677
2678 mux_net = container_of(ref, struct gsm_mux_net, ref);
2679 dlci = mux_net->dlci;
2680
2681 if (dlci->net) {
2682 unregister_netdev(dlci->net);
2683 dlci_net_free(dlci);
2684 }
2685}
2686
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002687static inline void muxnet_get(struct gsm_mux_net *mux_net)
2688{
2689 kref_get(&mux_net->ref);
2690}
2691
2692static inline void muxnet_put(struct gsm_mux_net *mux_net)
2693{
2694 kref_put(&mux_net->ref, net_free);
2695}
2696
Luc Van Oostenryck2468b3e2018-04-24 15:18:39 +02002697static netdev_tx_t gsm_mux_net_start_xmit(struct sk_buff *skb,
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002698 struct net_device *net)
2699{
Julia Lawall5dbc32a2015-03-29 14:54:13 +02002700 struct gsm_mux_net *mux_net = netdev_priv(net);
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002701 struct gsm_dlci *dlci = mux_net->dlci;
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002702 muxnet_get(mux_net);
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002703
2704 skb_queue_head(&dlci->skb_list, skb);
Tobias Klauser47baf1a2017-03-13 12:00:50 +01002705 net->stats.tx_packets++;
2706 net->stats.tx_bytes += skb->len;
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002707 gsm_dlci_data_kick(dlci);
2708 /* And tell the kernel when the last transmit started. */
Florian Westphal860e9532016-05-03 16:33:13 +02002709 netif_trans_update(net);
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002710 muxnet_put(mux_net);
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002711 return NETDEV_TX_OK;
2712}
2713
2714/* called when a packet did not ack after watchdogtimeout */
Michael S. Tsirkin0290bd22019-12-10 09:23:51 -05002715static void gsm_mux_net_tx_timeout(struct net_device *net, unsigned int txqueue)
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002716{
2717 /* Tell syslog we are hosed. */
2718 dev_dbg(&net->dev, "Tx timed out.\n");
2719
2720 /* Update statistics */
Tobias Klauser47baf1a2017-03-13 12:00:50 +01002721 net->stats.tx_errors++;
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002722}
2723
2724static void gsm_mux_rx_netchar(struct gsm_dlci *dlci,
Tony Lindgren4feb7a42019-01-13 17:25:27 -08002725 const unsigned char *in_buf, int size)
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002726{
2727 struct net_device *net = dlci->net;
2728 struct sk_buff *skb;
Julia Lawall5dbc32a2015-03-29 14:54:13 +02002729 struct gsm_mux_net *mux_net = netdev_priv(net);
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002730 muxnet_get(mux_net);
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002731
2732 /* Allocate an sk_buff */
2733 skb = dev_alloc_skb(size + NET_IP_ALIGN);
2734 if (!skb) {
2735 /* We got no receive buffer. */
Tobias Klauser47baf1a2017-03-13 12:00:50 +01002736 net->stats.rx_dropped++;
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002737 muxnet_put(mux_net);
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002738 return;
2739 }
2740 skb_reserve(skb, NET_IP_ALIGN);
Johannes Berg59ae1d12017-06-16 14:29:20 +02002741 skb_put_data(skb, in_buf, size);
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002742
2743 skb->dev = net;
Vaishali Thakkar75406b32015-06-06 06:05:24 +05302744 skb->protocol = htons(ETH_P_IP);
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002745
2746 /* Ship it off to the kernel */
2747 netif_rx(skb);
2748
2749 /* update out statistics */
Tobias Klauser47baf1a2017-03-13 12:00:50 +01002750 net->stats.rx_packets++;
2751 net->stats.rx_bytes += size;
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002752 muxnet_put(mux_net);
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002753 return;
2754}
2755
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002756static void gsm_mux_net_init(struct net_device *net)
2757{
2758 static const struct net_device_ops gsm_netdev_ops = {
2759 .ndo_open = gsm_mux_net_open,
2760 .ndo_stop = gsm_mux_net_close,
2761 .ndo_start_xmit = gsm_mux_net_start_xmit,
2762 .ndo_tx_timeout = gsm_mux_net_tx_timeout,
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002763 };
2764
2765 net->netdev_ops = &gsm_netdev_ops;
2766
2767 /* fill in the other fields */
2768 net->watchdog_timeo = GSM_NET_TX_TIMEOUT;
2769 net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2770 net->type = ARPHRD_NONE;
2771 net->tx_queue_len = 10;
2772}
2773
2774
2775/* caller holds the dlci mutex */
2776static void gsm_destroy_network(struct gsm_dlci *dlci)
2777{
2778 struct gsm_mux_net *mux_net;
2779
2780 pr_debug("destroy network interface");
2781 if (!dlci->net)
2782 return;
Julia Lawall5dbc32a2015-03-29 14:54:13 +02002783 mux_net = netdev_priv(dlci->net);
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002784 muxnet_put(mux_net);
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002785}
2786
2787
2788/* caller holds the dlci mutex */
2789static int gsm_create_network(struct gsm_dlci *dlci, struct gsm_netconfig *nc)
2790{
2791 char *netname;
2792 int retval = 0;
2793 struct net_device *net;
2794 struct gsm_mux_net *mux_net;
2795
2796 if (!capable(CAP_NET_ADMIN))
2797 return -EPERM;
2798
2799 /* Already in a non tty mode */
2800 if (dlci->adaption > 2)
2801 return -EBUSY;
2802
2803 if (nc->protocol != htons(ETH_P_IP))
2804 return -EPROTONOSUPPORT;
2805
2806 if (nc->adaption != 3 && nc->adaption != 4)
2807 return -EPROTONOSUPPORT;
2808
2809 pr_debug("create network interface");
2810
2811 netname = "gsm%d";
2812 if (nc->if_name[0] != '\0')
2813 netname = nc->if_name;
Tom Gundersenc835a672014-07-14 16:37:24 +02002814 net = alloc_netdev(sizeof(struct gsm_mux_net), netname,
2815 NET_NAME_UNKNOWN, gsm_mux_net_init);
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002816 if (!net) {
2817 pr_err("alloc_netdev failed");
2818 return -ENOMEM;
2819 }
2820 net->mtu = dlci->gsm->mtu;
Jarod Wilson9c22b4a2016-10-20 13:55:18 -04002821 net->min_mtu = 8;
2822 net->max_mtu = dlci->gsm->mtu;
Julia Lawall5dbc32a2015-03-29 14:54:13 +02002823 mux_net = netdev_priv(net);
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002824 mux_net->dlci = dlci;
2825 kref_init(&mux_net->ref);
2826 strncpy(nc->if_name, net->name, IFNAMSIZ); /* return net name */
2827
2828 /* reconfigure dlci for network */
2829 dlci->prev_adaption = dlci->adaption;
2830 dlci->prev_data = dlci->data;
2831 dlci->adaption = nc->adaption;
2832 dlci->data = gsm_mux_rx_netchar;
2833 dlci->net = net;
2834
2835 pr_debug("register netdev");
2836 retval = register_netdev(net);
2837 if (retval) {
2838 pr_err("network register fail %d\n", retval);
2839 dlci_net_free(dlci);
2840 return retval;
2841 }
2842 return net->ifindex; /* return network index */
2843}
Alan Coxe1eaea42010-03-26 11:32:54 +00002844
2845/* Line discipline for real tty */
Lad, Prabhakard3157b22015-02-04 18:23:59 +00002846static struct tty_ldisc_ops tty_ldisc_packet = {
Alan Coxe1eaea42010-03-26 11:32:54 +00002847 .owner = THIS_MODULE,
2848 .magic = TTY_LDISC_MAGIC,
2849 .name = "n_gsm",
2850 .open = gsmld_open,
2851 .close = gsmld_close,
2852 .flush_buffer = gsmld_flush_buffer,
Alan Coxe1eaea42010-03-26 11:32:54 +00002853 .read = gsmld_read,
2854 .write = gsmld_write,
2855 .ioctl = gsmld_ioctl,
2856 .poll = gsmld_poll,
2857 .receive_buf = gsmld_receive_buf,
2858 .write_wakeup = gsmld_write_wakeup
2859};
2860
2861/*
2862 * Virtual tty side
2863 */
2864
2865#define TX_SIZE 512
2866
2867static int gsmtty_modem_update(struct gsm_dlci *dlci, u8 brk)
2868{
2869 u8 modembits[5];
2870 struct gsm_control *ctrl;
2871 int len = 2;
2872
2873 if (brk)
2874 len++;
2875
2876 modembits[0] = len << 1 | EA; /* Data bytes */
2877 modembits[1] = dlci->addr << 2 | 3; /* DLCI, EA, 1 */
2878 modembits[2] = gsm_encode_modem(dlci) << 1 | EA;
2879 if (brk)
2880 modembits[3] = brk << 4 | 2 | EA; /* Valid, EA */
2881 ctrl = gsm_control_send(dlci->gsm, CMD_MSC, modembits, len + 1);
2882 if (ctrl == NULL)
2883 return -ENOMEM;
2884 return gsm_control_wait(dlci->gsm, ctrl);
2885}
2886
2887static int gsm_carrier_raised(struct tty_port *port)
2888{
2889 struct gsm_dlci *dlci = container_of(port, struct gsm_dlci, port);
Tony Lindgrenb2d89ad92018-04-07 10:19:51 -07002890 struct gsm_mux *gsm = dlci->gsm;
2891
Alan Coxe1eaea42010-03-26 11:32:54 +00002892 /* Not yet open so no carrier info */
2893 if (dlci->state != DLCI_OPEN)
2894 return 0;
2895 if (debug & 2)
2896 return 1;
Tony Lindgrenb2d89ad92018-04-07 10:19:51 -07002897
2898 /*
2899 * Basic mode with control channel in ADM mode may not respond
2900 * to CMD_MSC at all and modem_rx is empty.
2901 */
2902 if (gsm->encoding == 0 && gsm->dlci[0]->mode == DLCI_MODE_ADM &&
2903 !dlci->modem_rx)
2904 return 1;
2905
Alan Coxe1eaea42010-03-26 11:32:54 +00002906 return dlci->modem_rx & TIOCM_CD;
2907}
2908
2909static void gsm_dtr_rts(struct tty_port *port, int onoff)
2910{
2911 struct gsm_dlci *dlci = container_of(port, struct gsm_dlci, port);
2912 unsigned int modem_tx = dlci->modem_tx;
2913 if (onoff)
2914 modem_tx |= TIOCM_DTR | TIOCM_RTS;
2915 else
2916 modem_tx &= ~(TIOCM_DTR | TIOCM_RTS);
2917 if (modem_tx != dlci->modem_tx) {
2918 dlci->modem_tx = modem_tx;
2919 gsmtty_modem_update(dlci, 0);
2920 }
2921}
2922
2923static const struct tty_port_operations gsm_port_ops = {
2924 .carrier_raised = gsm_carrier_raised,
2925 .dtr_rts = gsm_dtr_rts,
Jiri Slaby9a8e62b2012-11-15 09:49:53 +01002926 .destruct = gsm_dlci_free,
Alan Coxe1eaea42010-03-26 11:32:54 +00002927};
2928
Jiri Slaby86176ed2012-08-07 21:47:28 +02002929static int gsmtty_install(struct tty_driver *driver, struct tty_struct *tty)
Alan Coxe1eaea42010-03-26 11:32:54 +00002930{
2931 struct gsm_mux *gsm;
2932 struct gsm_dlci *dlci;
Alan Coxe1eaea42010-03-26 11:32:54 +00002933 unsigned int line = tty->index;
Martin Hundebøll43a9e712019-07-10 21:26:55 +02002934 unsigned int mux = mux_line_to_num(line);
Jiri Slaby86176ed2012-08-07 21:47:28 +02002935 bool alloc = false;
2936 int ret;
Alan Coxe1eaea42010-03-26 11:32:54 +00002937
2938 line = line & 0x3F;
2939
2940 if (mux >= MAX_MUX)
2941 return -ENXIO;
2942 /* FIXME: we need to lock gsm_mux for lifetimes of ttys eventually */
2943 if (gsm_mux[mux] == NULL)
2944 return -EUNATCH;
2945 if (line == 0 || line > 61) /* 62/63 reserved */
2946 return -ECHRNG;
2947 gsm = gsm_mux[mux];
2948 if (gsm->dead)
2949 return -EL2HLT;
Aldo Iljazif3c909b2013-07-08 22:28:00 +03002950 /* If DLCI 0 is not yet fully open return an error.
2951 This is ok from a locking
2952 perspective as we don't have to worry about this
2953 if DLCI0 is lost */
Chao Bidfabf7f2013-11-26 12:09:39 +08002954 mutex_lock(&gsm->mutex);
2955 if (gsm->dlci[0] && gsm->dlci[0]->state != DLCI_OPEN) {
2956 mutex_unlock(&gsm->mutex);
xiaojin7e8ac7b2012-08-13 13:43:15 +01002957 return -EL2NSYNC;
Chao Bidfabf7f2013-11-26 12:09:39 +08002958 }
Alan Coxe1eaea42010-03-26 11:32:54 +00002959 dlci = gsm->dlci[line];
Jiri Slaby86176ed2012-08-07 21:47:28 +02002960 if (dlci == NULL) {
2961 alloc = true;
Alan Coxe1eaea42010-03-26 11:32:54 +00002962 dlci = gsm_dlci_alloc(gsm, line);
Jiri Slaby86176ed2012-08-07 21:47:28 +02002963 }
Chao Bidfabf7f2013-11-26 12:09:39 +08002964 if (dlci == NULL) {
2965 mutex_unlock(&gsm->mutex);
Alan Coxe1eaea42010-03-26 11:32:54 +00002966 return -ENOMEM;
Chao Bidfabf7f2013-11-26 12:09:39 +08002967 }
Jiri Slaby86176ed2012-08-07 21:47:28 +02002968 ret = tty_port_install(&dlci->port, driver, tty);
2969 if (ret) {
2970 if (alloc)
2971 dlci_put(dlci);
Chao Bidfabf7f2013-11-26 12:09:39 +08002972 mutex_unlock(&gsm->mutex);
Jiri Slaby86176ed2012-08-07 21:47:28 +02002973 return ret;
2974 }
2975
Chao Bidfabf7f2013-11-26 12:09:39 +08002976 dlci_get(dlci);
2977 dlci_get(gsm->dlci[0]);
2978 mux_get(gsm);
Alan Coxe1eaea42010-03-26 11:32:54 +00002979 tty->driver_data = dlci;
Chao Bidfabf7f2013-11-26 12:09:39 +08002980 mutex_unlock(&gsm->mutex);
Jiri Slaby86176ed2012-08-07 21:47:28 +02002981
2982 return 0;
2983}
2984
2985static int gsmtty_open(struct tty_struct *tty, struct file *filp)
2986{
2987 struct gsm_dlci *dlci = tty->driver_data;
2988 struct tty_port *port = &dlci->port;
2989
2990 port->count++;
Alan Coxe1eaea42010-03-26 11:32:54 +00002991 tty_port_tty_set(port, tty);
2992
2993 dlci->modem_rx = 0;
2994 /* We could in theory open and close before we wait - eg if we get
2995 a DM straight back. This is ok as that will have caused a hangup */
Peter Hurleyd41861c2016-04-09 17:53:25 -07002996 tty_port_set_initialized(port, 1);
Alan Coxe1eaea42010-03-26 11:32:54 +00002997 /* Start sending off SABM messages */
2998 gsm_dlci_begin_open(dlci);
2999 /* And wait for virtual carrier */
3000 return tty_port_block_til_ready(port, tty, filp);
3001}
3002
3003static void gsmtty_close(struct tty_struct *tty, struct file *filp)
3004{
3005 struct gsm_dlci *dlci = tty->driver_data;
Russ Gorby6ab8fba2011-06-16 14:20:13 -07003006
Alan Coxe1eaea42010-03-26 11:32:54 +00003007 if (dlci == NULL)
3008 return;
Dirkjan Bussink4d9b1092013-01-30 11:44:50 +01003009 if (dlci->state == DLCI_CLOSED)
3010 return;
Russ Gorbybcd5abe2011-06-16 14:20:12 -07003011 mutex_lock(&dlci->mutex);
3012 gsm_destroy_network(dlci);
3013 mutex_unlock(&dlci->mutex);
Alan Coxe1eaea42010-03-26 11:32:54 +00003014 if (tty_port_close_start(&dlci->port, tty, filp) == 0)
Chao Bidfabf7f2013-11-26 12:09:39 +08003015 return;
Alan Coxe1eaea42010-03-26 11:32:54 +00003016 gsm_dlci_begin_close(dlci);
Peter Hurleyd41861c2016-04-09 17:53:25 -07003017 if (tty_port_initialized(&dlci->port) && C_HUPCL(tty))
3018 tty_port_lower_dtr_rts(&dlci->port);
Alan Coxe1eaea42010-03-26 11:32:54 +00003019 tty_port_close_end(&dlci->port, tty);
3020 tty_port_tty_set(&dlci->port, NULL);
Chao Bidfabf7f2013-11-26 12:09:39 +08003021 return;
Alan Coxe1eaea42010-03-26 11:32:54 +00003022}
3023
3024static void gsmtty_hangup(struct tty_struct *tty)
3025{
3026 struct gsm_dlci *dlci = tty->driver_data;
Dirkjan Bussink4d9b1092013-01-30 11:44:50 +01003027 if (dlci->state == DLCI_CLOSED)
3028 return;
Alan Coxe1eaea42010-03-26 11:32:54 +00003029 tty_port_hangup(&dlci->port);
3030 gsm_dlci_begin_close(dlci);
3031}
3032
3033static int gsmtty_write(struct tty_struct *tty, const unsigned char *buf,
3034 int len)
3035{
Dirkjan Bussink4d9b1092013-01-30 11:44:50 +01003036 int sent;
Alan Coxe1eaea42010-03-26 11:32:54 +00003037 struct gsm_dlci *dlci = tty->driver_data;
Dirkjan Bussink4d9b1092013-01-30 11:44:50 +01003038 if (dlci->state == DLCI_CLOSED)
3039 return -EINVAL;
Alan Coxe1eaea42010-03-26 11:32:54 +00003040 /* Stuff the bytes into the fifo queue */
Jiri Slaby036bca1fc2020-02-19 09:49:40 +01003041 sent = kfifo_in_locked(&dlci->fifo, buf, len, &dlci->lock);
Alan Coxe1eaea42010-03-26 11:32:54 +00003042 /* Need to kick the channel */
3043 gsm_dlci_data_kick(dlci);
3044 return sent;
3045}
3046
3047static int gsmtty_write_room(struct tty_struct *tty)
3048{
3049 struct gsm_dlci *dlci = tty->driver_data;
Dirkjan Bussink4d9b1092013-01-30 11:44:50 +01003050 if (dlci->state == DLCI_CLOSED)
3051 return -EINVAL;
Jiri Slaby036bca1fc2020-02-19 09:49:40 +01003052 return TX_SIZE - kfifo_len(&dlci->fifo);
Alan Coxe1eaea42010-03-26 11:32:54 +00003053}
3054
3055static int gsmtty_chars_in_buffer(struct tty_struct *tty)
3056{
3057 struct gsm_dlci *dlci = tty->driver_data;
Dirkjan Bussink4d9b1092013-01-30 11:44:50 +01003058 if (dlci->state == DLCI_CLOSED)
3059 return -EINVAL;
Jiri Slaby036bca1fc2020-02-19 09:49:40 +01003060 return kfifo_len(&dlci->fifo);
Alan Coxe1eaea42010-03-26 11:32:54 +00003061}
3062
3063static void gsmtty_flush_buffer(struct tty_struct *tty)
3064{
3065 struct gsm_dlci *dlci = tty->driver_data;
Dirkjan Bussink4d9b1092013-01-30 11:44:50 +01003066 if (dlci->state == DLCI_CLOSED)
3067 return;
Alan Coxe1eaea42010-03-26 11:32:54 +00003068 /* Caution needed: If we implement reliable transport classes
3069 then the data being transmitted can't simply be junked once
3070 it has first hit the stack. Until then we can just blow it
3071 away */
Jiri Slaby036bca1fc2020-02-19 09:49:40 +01003072 kfifo_reset(&dlci->fifo);
Alan Coxe1eaea42010-03-26 11:32:54 +00003073 /* Need to unhook this DLCI from the transmit queue logic */
3074}
3075
3076static void gsmtty_wait_until_sent(struct tty_struct *tty, int timeout)
3077{
3078 /* The FIFO handles the queue so the kernel will do the right
3079 thing waiting on chars_in_buffer before calling us. No work
3080 to do here */
3081}
3082
Alan Cox60b33c12011-02-14 16:26:14 +00003083static int gsmtty_tiocmget(struct tty_struct *tty)
Alan Coxe1eaea42010-03-26 11:32:54 +00003084{
3085 struct gsm_dlci *dlci = tty->driver_data;
Dirkjan Bussink4d9b1092013-01-30 11:44:50 +01003086 if (dlci->state == DLCI_CLOSED)
3087 return -EINVAL;
Alan Coxe1eaea42010-03-26 11:32:54 +00003088 return dlci->modem_rx;
3089}
3090
Alan Cox20b9d172011-02-14 16:26:50 +00003091static int gsmtty_tiocmset(struct tty_struct *tty,
Alan Coxe1eaea42010-03-26 11:32:54 +00003092 unsigned int set, unsigned int clear)
3093{
3094 struct gsm_dlci *dlci = tty->driver_data;
3095 unsigned int modem_tx = dlci->modem_tx;
3096
Dirkjan Bussink4d9b1092013-01-30 11:44:50 +01003097 if (dlci->state == DLCI_CLOSED)
3098 return -EINVAL;
Nikola Diklic-Perincf168072011-09-23 10:59:43 +02003099 modem_tx &= ~clear;
Alan Coxe1eaea42010-03-26 11:32:54 +00003100 modem_tx |= set;
3101
3102 if (modem_tx != dlci->modem_tx) {
3103 dlci->modem_tx = modem_tx;
3104 return gsmtty_modem_update(dlci, 0);
3105 }
3106 return 0;
3107}
3108
3109
Alan Cox6caa76b2011-02-14 16:27:22 +00003110static int gsmtty_ioctl(struct tty_struct *tty,
Alan Coxe1eaea42010-03-26 11:32:54 +00003111 unsigned int cmd, unsigned long arg)
3112{
Russ Gorbybcd5abe2011-06-16 14:20:12 -07003113 struct gsm_dlci *dlci = tty->driver_data;
3114 struct gsm_netconfig nc;
3115 int index;
3116
Dirkjan Bussink4d9b1092013-01-30 11:44:50 +01003117 if (dlci->state == DLCI_CLOSED)
3118 return -EINVAL;
Russ Gorbybcd5abe2011-06-16 14:20:12 -07003119 switch (cmd) {
3120 case GSMIOC_ENABLE_NET:
3121 if (copy_from_user(&nc, (void __user *)arg, sizeof(nc)))
3122 return -EFAULT;
3123 nc.if_name[IFNAMSIZ-1] = '\0';
3124 /* return net interface index or error code */
3125 mutex_lock(&dlci->mutex);
3126 index = gsm_create_network(dlci, &nc);
3127 mutex_unlock(&dlci->mutex);
3128 if (copy_to_user((void __user *)arg, &nc, sizeof(nc)))
3129 return -EFAULT;
3130 return index;
3131 case GSMIOC_DISABLE_NET:
3132 if (!capable(CAP_NET_ADMIN))
3133 return -EPERM;
3134 mutex_lock(&dlci->mutex);
3135 gsm_destroy_network(dlci);
3136 mutex_unlock(&dlci->mutex);
3137 return 0;
3138 default:
3139 return -ENOIOCTLCMD;
3140 }
Alan Coxe1eaea42010-03-26 11:32:54 +00003141}
3142
3143static void gsmtty_set_termios(struct tty_struct *tty, struct ktermios *old)
3144{
Dirkjan Bussink4d9b1092013-01-30 11:44:50 +01003145 struct gsm_dlci *dlci = tty->driver_data;
3146 if (dlci->state == DLCI_CLOSED)
3147 return;
Alan Coxe1eaea42010-03-26 11:32:54 +00003148 /* For the moment its fixed. In actual fact the speed information
3149 for the virtual channel can be propogated in both directions by
3150 the RPN control message. This however rapidly gets nasty as we
3151 then have to remap modem signals each way according to whether
3152 our virtual cable is null modem etc .. */
Alan Coxadc8d742012-07-14 15:31:47 +01003153 tty_termios_copy_hw(&tty->termios, old);
Alan Coxe1eaea42010-03-26 11:32:54 +00003154}
3155
3156static void gsmtty_throttle(struct tty_struct *tty)
3157{
3158 struct gsm_dlci *dlci = tty->driver_data;
Dirkjan Bussink4d9b1092013-01-30 11:44:50 +01003159 if (dlci->state == DLCI_CLOSED)
3160 return;
Peter Hurley9db276f2016-01-10 20:36:15 -08003161 if (C_CRTSCTS(tty))
Alan Coxe1eaea42010-03-26 11:32:54 +00003162 dlci->modem_tx &= ~TIOCM_DTR;
3163 dlci->throttled = 1;
3164 /* Send an MSC with DTR cleared */
3165 gsmtty_modem_update(dlci, 0);
3166}
3167
3168static void gsmtty_unthrottle(struct tty_struct *tty)
3169{
3170 struct gsm_dlci *dlci = tty->driver_data;
Dirkjan Bussink4d9b1092013-01-30 11:44:50 +01003171 if (dlci->state == DLCI_CLOSED)
3172 return;
Peter Hurley9db276f2016-01-10 20:36:15 -08003173 if (C_CRTSCTS(tty))
Alan Coxe1eaea42010-03-26 11:32:54 +00003174 dlci->modem_tx |= TIOCM_DTR;
3175 dlci->throttled = 0;
3176 /* Send an MSC with DTR set */
3177 gsmtty_modem_update(dlci, 0);
3178}
3179
3180static int gsmtty_break_ctl(struct tty_struct *tty, int state)
3181{
3182 struct gsm_dlci *dlci = tty->driver_data;
3183 int encode = 0; /* Off */
Dirkjan Bussink4d9b1092013-01-30 11:44:50 +01003184 if (dlci->state == DLCI_CLOSED)
3185 return -EINVAL;
Alan Coxe1eaea42010-03-26 11:32:54 +00003186
3187 if (state == -1) /* "On indefinitely" - we can't encode this
3188 properly */
3189 encode = 0x0F;
3190 else if (state > 0) {
3191 encode = state / 200; /* mS to encoding */
3192 if (encode > 0x0F)
3193 encode = 0x0F; /* Best effort */
3194 }
3195 return gsmtty_modem_update(dlci, encode);
3196}
3197
Pan Xinhui8f9cfee2015-03-28 10:42:56 +08003198static void gsmtty_cleanup(struct tty_struct *tty)
Chao Bidfabf7f2013-11-26 12:09:39 +08003199{
3200 struct gsm_dlci *dlci = tty->driver_data;
3201 struct gsm_mux *gsm = dlci->gsm;
3202
3203 dlci_put(dlci);
3204 dlci_put(gsm->dlci[0]);
3205 mux_put(gsm);
Chao Bidfabf7f2013-11-26 12:09:39 +08003206}
Alan Coxe1eaea42010-03-26 11:32:54 +00003207
3208/* Virtual ttys for the demux */
3209static const struct tty_operations gsmtty_ops = {
Jiri Slaby86176ed2012-08-07 21:47:28 +02003210 .install = gsmtty_install,
Alan Coxe1eaea42010-03-26 11:32:54 +00003211 .open = gsmtty_open,
3212 .close = gsmtty_close,
3213 .write = gsmtty_write,
3214 .write_room = gsmtty_write_room,
3215 .chars_in_buffer = gsmtty_chars_in_buffer,
3216 .flush_buffer = gsmtty_flush_buffer,
3217 .ioctl = gsmtty_ioctl,
3218 .throttle = gsmtty_throttle,
3219 .unthrottle = gsmtty_unthrottle,
3220 .set_termios = gsmtty_set_termios,
3221 .hangup = gsmtty_hangup,
3222 .wait_until_sent = gsmtty_wait_until_sent,
3223 .tiocmget = gsmtty_tiocmget,
3224 .tiocmset = gsmtty_tiocmset,
3225 .break_ctl = gsmtty_break_ctl,
Pan Xinhui8f9cfee2015-03-28 10:42:56 +08003226 .cleanup = gsmtty_cleanup,
Alan Coxe1eaea42010-03-26 11:32:54 +00003227};
3228
3229
3230
3231static int __init gsm_init(void)
3232{
3233 /* Fill in our line protocol discipline, and register it */
3234 int status = tty_register_ldisc(N_GSM0710, &tty_ldisc_packet);
3235 if (status != 0) {
Alan Cox5f9a31d2010-11-04 15:17:27 +00003236 pr_err("n_gsm: can't register line discipline (err = %d)\n",
3237 status);
Alan Coxe1eaea42010-03-26 11:32:54 +00003238 return status;
3239 }
3240
3241 gsm_tty_driver = alloc_tty_driver(256);
3242 if (!gsm_tty_driver) {
3243 tty_unregister_ldisc(N_GSM0710);
Alan Cox5f9a31d2010-11-04 15:17:27 +00003244 pr_err("gsm_init: tty allocation failed.\n");
Alan Coxe1eaea42010-03-26 11:32:54 +00003245 return -EINVAL;
3246 }
Alan Coxe1eaea42010-03-26 11:32:54 +00003247 gsm_tty_driver->driver_name = "gsmtty";
3248 gsm_tty_driver->name = "gsmtty";
3249 gsm_tty_driver->major = 0; /* Dynamic */
3250 gsm_tty_driver->minor_start = 0;
3251 gsm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
3252 gsm_tty_driver->subtype = SERIAL_TYPE_NORMAL;
3253 gsm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV
Alan Cox5f9a31d2010-11-04 15:17:27 +00003254 | TTY_DRIVER_HARDWARE_BREAK;
Alan Coxe1eaea42010-03-26 11:32:54 +00003255 gsm_tty_driver->init_termios = tty_std_termios;
3256 /* Fixme */
3257 gsm_tty_driver->init_termios.c_lflag &= ~ECHO;
3258 tty_set_operations(gsm_tty_driver, &gsmtty_ops);
3259
3260 spin_lock_init(&gsm_mux_lock);
3261
3262 if (tty_register_driver(gsm_tty_driver)) {
3263 put_tty_driver(gsm_tty_driver);
3264 tty_unregister_ldisc(N_GSM0710);
Alan Cox5f9a31d2010-11-04 15:17:27 +00003265 pr_err("gsm_init: tty registration failed.\n");
Alan Coxe1eaea42010-03-26 11:32:54 +00003266 return -EBUSY;
3267 }
Alan Cox5f9a31d2010-11-04 15:17:27 +00003268 pr_debug("gsm_init: loaded as %d,%d.\n",
3269 gsm_tty_driver->major, gsm_tty_driver->minor_start);
Alan Coxe1eaea42010-03-26 11:32:54 +00003270 return 0;
3271}
3272
3273static void __exit gsm_exit(void)
3274{
3275 int status = tty_unregister_ldisc(N_GSM0710);
3276 if (status != 0)
Alan Cox5f9a31d2010-11-04 15:17:27 +00003277 pr_err("n_gsm: can't unregister line discipline (err = %d)\n",
3278 status);
Alan Coxe1eaea42010-03-26 11:32:54 +00003279 tty_unregister_driver(gsm_tty_driver);
3280 put_tty_driver(gsm_tty_driver);
Alan Coxe1eaea42010-03-26 11:32:54 +00003281}
3282
3283module_init(gsm_init);
3284module_exit(gsm_exit);
3285
3286
3287MODULE_LICENSE("GPL");
3288MODULE_ALIAS_LDISC(N_GSM0710);