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