hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/tty/n_gsm.c
....@@ -50,6 +50,7 @@
5050 #include <linux/netdevice.h>
5151 #include <linux/etherdevice.h>
5252 #include <linux/gsmmux.h>
53
+#include "tty.h"
5354
5455 static int debug;
5556 module_param(debug, int, 0600);
....@@ -72,14 +73,15 @@
7273 */
7374 #define MAX_MRU 1500
7475 #define MAX_MTU 1500
76
+/* SOF, ADDR, CTRL, LEN1, LEN2, ..., FCS, EOF */
77
+#define PROT_OVERHEAD 7
7578 #define GSM_NET_TX_TIMEOUT (HZ*10)
7679
7780 /**
7881 * struct gsm_mux_net - network interface
79
- * @struct gsm_dlci* dlci
8082 *
8183 * Created when net interface is initialized.
82
- **/
84
+ */
8385 struct gsm_mux_net {
8486 struct kref ref;
8587 struct gsm_dlci *dlci;
....@@ -97,7 +99,19 @@
9799 u8 ctrl; /* Control byte + flags */
98100 unsigned int len; /* Length of data block (can be zero) */
99101 unsigned char *data; /* Points into buffer but not at the start */
100
- unsigned char buffer[0];
102
+ unsigned char buffer[];
103
+};
104
+
105
+enum gsm_dlci_state {
106
+ DLCI_CLOSED,
107
+ DLCI_OPENING, /* Sending SABM not seen UA */
108
+ DLCI_OPEN, /* SABM/UA complete */
109
+ DLCI_CLOSING, /* Sending DISC not seen UA/DM */
110
+};
111
+
112
+enum gsm_dlci_mode {
113
+ DLCI_MODE_ABM, /* Normal Asynchronous Balanced Mode */
114
+ DLCI_MODE_ADM, /* Asynchronous Disconnected Mode */
101115 };
102116
103117 /*
....@@ -113,38 +127,31 @@
113127 struct gsm_dlci {
114128 struct gsm_mux *gsm;
115129 int addr;
116
- int state;
117
-#define DLCI_CLOSED 0
118
-#define DLCI_OPENING 1 /* Sending SABM not seen UA */
119
-#define DLCI_OPEN 2 /* SABM/UA complete */
120
-#define DLCI_CLOSING 3 /* Sending DISC not seen UA/DM */
130
+ enum gsm_dlci_state state;
121131 struct mutex mutex;
122132
123133 /* Link layer */
124
- int mode;
125
-#define DLCI_MODE_ABM 0 /* Normal Asynchronous Balanced Mode */
126
-#define DLCI_MODE_ADM 1 /* Asynchronous Disconnected Mode */
134
+ enum gsm_dlci_mode mode;
127135 spinlock_t lock; /* Protects the internal state */
128136 struct timer_list t1; /* Retransmit timer for SABM and UA */
129137 int retries;
130138 /* Uplink tty if active */
131139 struct tty_port port; /* The tty bound to this DLCI if there is one */
132
- struct kfifo *fifo; /* Queue fifo for the DLCI */
133
- struct kfifo _fifo; /* For new fifo API porting only */
140
+ struct kfifo fifo; /* Queue fifo for the DLCI */
134141 int adaption; /* Adaption layer in use */
135142 int prev_adaption;
136143 u32 modem_rx; /* Our incoming virtual modem lines */
137144 u32 modem_tx; /* Our outgoing modem lines */
138
- int dead; /* Refuse re-open */
145
+ bool dead; /* Refuse re-open */
139146 /* Flow control */
140
- int throttled; /* Private copy of throttle state */
141
- int constipated; /* Throttle status for outgoing */
147
+ bool throttled; /* Private copy of throttle state */
148
+ bool constipated; /* Throttle status for outgoing */
142149 /* Packetised I/O */
143150 struct sk_buff *skb; /* Frame being sent */
144151 struct sk_buff_head skb_list; /* Queued frames */
145152 /* Data handling callback */
146
- void (*data)(struct gsm_dlci *dlci, u8 *data, int len);
147
- void (*prev_data)(struct gsm_dlci *dlci, u8 *data, int len);
153
+ void (*data)(struct gsm_dlci *dlci, const u8 *data, int len);
154
+ void (*prev_data)(struct gsm_dlci *dlci, const u8 *data, int len);
148155 struct net_device *net; /* network interface, if created */
149156 };
150157
....@@ -166,6 +173,20 @@
166173 int len; /* Length of block for retransmission */
167174 int done; /* Done flag */
168175 int error; /* Error if any */
176
+};
177
+
178
+enum gsm_mux_state {
179
+ GSM_SEARCH,
180
+ GSM_START,
181
+ GSM_ADDRESS,
182
+ GSM_CONTROL,
183
+ GSM_LEN,
184
+ GSM_DATA,
185
+ GSM_FCS,
186
+ GSM_OVERRUN,
187
+ GSM_LEN0,
188
+ GSM_LEN1,
189
+ GSM_SSOF,
169190 };
170191
171192 /*
....@@ -192,41 +213,28 @@
192213
193214 /* Framing Layer */
194215 unsigned char *buf;
195
- int state;
196
-#define GSM_SEARCH 0
197
-#define GSM_START 1
198
-#define GSM_ADDRESS 2
199
-#define GSM_CONTROL 3
200
-#define GSM_LEN 4
201
-#define GSM_DATA 5
202
-#define GSM_FCS 6
203
-#define GSM_OVERRUN 7
204
-#define GSM_LEN0 8
205
-#define GSM_LEN1 9
206
-#define GSM_SSOF 10
216
+ enum gsm_mux_state state;
207217 unsigned int len;
208218 unsigned int address;
209219 unsigned int count;
210
- int escape;
220
+ bool escape;
211221 int encoding;
212222 u8 control;
213223 u8 fcs;
214224 u8 received_fcs;
215225 u8 *txframe; /* TX framing buffer */
216226
217
- /* Methods for the receiver side */
227
+ /* Method for the receiver side */
218228 void (*receive)(struct gsm_mux *gsm, u8 ch);
219
- void (*error)(struct gsm_mux *gsm, u8 ch, u8 flag);
220
- /* And transmit side */
221
- int (*output)(struct gsm_mux *mux, u8 *data, int len);
222229
223230 /* Link Layer */
224231 unsigned int mru;
225232 unsigned int mtu;
226233 int initiator; /* Did we initiate connection */
227
- int dead; /* Has the mux been shut down */
234
+ bool dead; /* Has the mux been shut down */
228235 struct gsm_dlci *dlci[NUM_DLCI];
229
- int constipated; /* Asked by remote to shut up */
236
+ int old_c_iflag; /* termios c_iflag value before attach */
237
+ bool constipated; /* Asked by remote to shut up */
230238
231239 spinlock_t tx_lock;
232240 unsigned int tx_bytes; /* TX data outstanding */
....@@ -359,6 +367,8 @@
359367 #define INIT_FCS 0xFF
360368 #define GOOD_FCS 0xCF
361369
370
+static int gsmld_output(struct gsm_mux *gsm, u8 *data, int len);
371
+
362372 /**
363373 * gsm_fcs_add - update FCS
364374 * @fcs: Current FCS
....@@ -393,7 +403,7 @@
393403 /**
394404 * gsm_read_ea - read a byte into an EA
395405 * @val: variable holding value
396
- * c: byte going into the EA
406
+ * @c: byte going into the EA
397407 *
398408 * Processes one byte of an EA. Updates the passed variable
399409 * and returns 1 if the EA is now completely read
....@@ -406,6 +416,27 @@
406416 *val |= c >> 1;
407417 /* Was this the last byte of the EA 1 = yes*/
408418 return c & EA;
419
+}
420
+
421
+/**
422
+ * gsm_read_ea_val - read a value until EA
423
+ * @val: variable holding value
424
+ * @data: buffer of data
425
+ * @dlen: length of data
426
+ *
427
+ * Processes an EA value. Updates the passed variable and
428
+ * returns the processed data length.
429
+ */
430
+static unsigned int gsm_read_ea_val(unsigned int *val, const u8 *data, int dlen)
431
+{
432
+ unsigned int len = 0;
433
+
434
+ for (; dlen > 0; dlen--) {
435
+ len++;
436
+ if (gsm_read_ea(val, *data++))
437
+ break;
438
+ }
439
+ return len;
409440 }
410441
411442 /**
....@@ -497,18 +528,7 @@
497528 else
498529 pr_cont("(F)");
499530
500
- if (dlen) {
501
- int ct = 0;
502
- while (dlen--) {
503
- if (ct % 8 == 0) {
504
- pr_cont("\n");
505
- pr_debug(" ");
506
- }
507
- pr_cont("%02X ", *data++);
508
- ct++;
509
- }
510
- }
511
- pr_cont("\n");
531
+ print_hex_dump_bytes("", DUMP_PREFIX_NONE, data, dlen);
512532 }
513533
514534
....@@ -518,8 +538,8 @@
518538
519539 /**
520540 * gsm_stuff_packet - bytestuff a packet
521
- * @ibuf: input
522
- * @obuf: output
541
+ * @input: input buffer
542
+ * @output: output buffer
523543 * @len: length of input
524544 *
525545 * Expand a buffer by bytestuffing it. The worst case size change
....@@ -592,7 +612,7 @@
592612 WARN_ON(1);
593613 return;
594614 }
595
- gsm->output(gsm, cbuf, len);
615
+ gsmld_output(gsm, cbuf, len);
596616 gsm_print_packet("-->", addr, cr, control, NULL, 0);
597617 }
598618
....@@ -656,6 +676,37 @@
656676 }
657677
658678 /**
679
+ * gsm_is_flow_ctrl_msg - checks if flow control message
680
+ * @msg: message to check
681
+ *
682
+ * Returns true if the given message is a flow control command of the
683
+ * control channel. False is returned in any other case.
684
+ */
685
+static bool gsm_is_flow_ctrl_msg(struct gsm_msg *msg)
686
+{
687
+ unsigned int cmd;
688
+
689
+ if (msg->addr > 0)
690
+ return false;
691
+
692
+ switch (msg->ctrl & ~PF) {
693
+ case UI:
694
+ case UIH:
695
+ cmd = 0;
696
+ if (gsm_read_ea_val(&cmd, msg->data + 2, msg->len - 2) < 1)
697
+ break;
698
+ switch (cmd & ~PF) {
699
+ case CMD_FCOFF:
700
+ case CMD_FCON:
701
+ return true;
702
+ }
703
+ break;
704
+ }
705
+
706
+ return false;
707
+}
708
+
709
+/**
659710 * gsm_data_kick - poke the queue
660711 * @gsm: GSM Mux
661712 *
....@@ -673,7 +724,7 @@
673724 int len;
674725
675726 list_for_each_entry_safe(msg, nmsg, &gsm->tx_list, list) {
676
- if (gsm->constipated && msg->addr)
727
+ if (gsm->constipated && !gsm_is_flow_ctrl_msg(msg))
677728 continue;
678729 if (gsm->encoding != 0) {
679730 gsm->txframe[0] = GSM1_SOF;
....@@ -692,7 +743,7 @@
692743 print_hex_dump_bytes("gsm_data_kick: ",
693744 DUMP_PREFIX_OFFSET,
694745 gsm->txframe, len);
695
- if (gsm->output(gsm, gsm->txframe, len) < 0)
746
+ if (gsmld_output(gsm, gsm->txframe, len) < 0)
696747 break;
697748 /* FIXME: Can eliminate one SOF in many more cases */
698749 gsm->tx_bytes -= msg->len;
....@@ -797,41 +848,51 @@
797848 {
798849 struct gsm_msg *msg;
799850 u8 *dp;
800
- int len, total_size, size;
801
- int h = dlci->adaption - 1;
851
+ int h, len, size;
802852
803
- total_size = 0;
804
- while (1) {
805
- len = kfifo_len(dlci->fifo);
806
- if (len == 0)
807
- return total_size;
853
+ /* for modem bits without break data */
854
+ h = ((dlci->adaption == 1) ? 0 : 1);
808855
809
- /* MTU/MRU count only the data bits */
810
- if (len > gsm->mtu)
811
- len = gsm->mtu;
856
+ len = kfifo_len(&dlci->fifo);
857
+ if (len == 0)
858
+ return 0;
812859
813
- size = len + h;
860
+ /* MTU/MRU count only the data bits but watch adaption mode */
861
+ if ((len + h) > gsm->mtu)
862
+ len = gsm->mtu - h;
814863
815
- msg = gsm_data_alloc(gsm, dlci->addr, size, gsm->ftype);
816
- /* FIXME: need a timer or something to kick this so it can't
817
- get stuck with no work outstanding and no buffer free */
818
- if (msg == NULL)
819
- return -ENOMEM;
820
- dp = msg->data;
821
- switch (dlci->adaption) {
822
- case 1: /* Unstructured */
823
- break;
824
- case 2: /* Unstructed with modem bits.
825
- Always one byte as we never send inline break data */
826
- *dp++ = gsm_encode_modem(dlci);
827
- break;
828
- }
829
- WARN_ON(kfifo_out_locked(dlci->fifo, dp , len, &dlci->lock) != len);
830
- __gsm_data_queue(dlci, msg);
831
- total_size += size;
864
+ size = len + h;
865
+
866
+ msg = gsm_data_alloc(gsm, dlci->addr, size, gsm->ftype);
867
+ /* FIXME: need a timer or something to kick this so it can't
868
+ * get stuck with no work outstanding and no buffer free
869
+ */
870
+ if (!msg)
871
+ return -ENOMEM;
872
+ dp = msg->data;
873
+ switch (dlci->adaption) {
874
+ case 1: /* Unstructured */
875
+ break;
876
+ case 2: /* Unstructured with modem bits.
877
+ * Always one byte as we never send inline break data
878
+ */
879
+ *dp++ = (gsm_encode_modem(dlci) << 1) | EA;
880
+ break;
881
+ default:
882
+ pr_err("%s: unsupported adaption %d\n", __func__,
883
+ dlci->adaption);
884
+ break;
832885 }
886
+
887
+ WARN_ON(len != kfifo_out_locked(&dlci->fifo, dp, len,
888
+ &dlci->lock));
889
+
890
+ /* Notify upper layer about available send space. */
891
+ tty_port_tty_wakeup(&dlci->port);
892
+
893
+ __gsm_data_queue(dlci, msg);
833894 /* Bytes of data we used up */
834
- return total_size;
895
+ return size;
835896 }
836897
837898 /**
....@@ -994,7 +1055,7 @@
9941055 * Encode up and queue a UI/UIH frame containing our response.
9951056 */
9961057
997
-static void gsm_control_reply(struct gsm_mux *gsm, int cmd, u8 *data,
1058
+static void gsm_control_reply(struct gsm_mux *gsm, int cmd, const u8 *data,
9981059 int dlen)
9991060 {
10001061 struct gsm_msg *msg;
....@@ -1040,9 +1101,9 @@
10401101 fc = (modem & MDM_FC) || !(modem & MDM_RTR);
10411102 if (fc && !dlci->constipated) {
10421103 /* Need to throttle our output on this device */
1043
- dlci->constipated = 1;
1104
+ dlci->constipated = true;
10441105 } else if (!fc && dlci->constipated) {
1045
- dlci->constipated = 0;
1106
+ dlci->constipated = false;
10461107 gsm_dlci_data_kick(dlci);
10471108 }
10481109
....@@ -1079,14 +1140,14 @@
10791140 * and if need be stuff a break message down the tty.
10801141 */
10811142
1082
-static void gsm_control_modem(struct gsm_mux *gsm, u8 *data, int clen)
1143
+static void gsm_control_modem(struct gsm_mux *gsm, const u8 *data, int clen)
10831144 {
10841145 unsigned int addr = 0;
10851146 unsigned int modem = 0;
10861147 unsigned int brk = 0;
10871148 struct gsm_dlci *dlci;
10881149 int len = clen;
1089
- u8 *dp = data;
1150
+ const u8 *dp = data;
10901151 struct tty_struct *tty;
10911152
10921153 while (gsm_read_ea(&addr, *dp++) == 0) {
....@@ -1140,13 +1201,13 @@
11401201 * this into the uplink tty if present
11411202 */
11421203
1143
-static void gsm_control_rls(struct gsm_mux *gsm, u8 *data, int clen)
1204
+static void gsm_control_rls(struct gsm_mux *gsm, const u8 *data, int clen)
11441205 {
11451206 struct tty_port *port;
11461207 unsigned int addr = 0;
11471208 u8 bits;
11481209 int len = clen;
1149
- u8 *dp = data;
1210
+ const u8 *dp = data;
11501211
11511212 while (gsm_read_ea(&addr, *dp++) == 0) {
11521213 len--;
....@@ -1195,7 +1256,7 @@
11951256 */
11961257
11971258 static void gsm_control_message(struct gsm_mux *gsm, unsigned int command,
1198
- u8 *data, int clen)
1259
+ const u8 *data, int clen)
11991260 {
12001261 u8 buf[1];
12011262 unsigned long flags;
....@@ -1205,8 +1266,8 @@
12051266 struct gsm_dlci *dlci = gsm->dlci[0];
12061267 /* Modem wishes to close down */
12071268 if (dlci) {
1208
- dlci->dead = 1;
1209
- gsm->dead = 1;
1269
+ dlci->dead = true;
1270
+ gsm->dead = true;
12101271 gsm_dlci_begin_close(dlci);
12111272 }
12121273 }
....@@ -1217,7 +1278,7 @@
12171278 break;
12181279 case CMD_FCON:
12191280 /* Modem can accept data again */
1220
- gsm->constipated = 0;
1281
+ gsm->constipated = false;
12211282 gsm_control_reply(gsm, CMD_FCON, NULL, 0);
12221283 /* Kick the link in case it is idling */
12231284 spin_lock_irqsave(&gsm->tx_lock, flags);
....@@ -1226,7 +1287,7 @@
12261287 break;
12271288 case CMD_FCOFF:
12281289 /* Modem wants us to STFU */
1229
- gsm->constipated = 1;
1290
+ gsm->constipated = true;
12301291 gsm_control_reply(gsm, CMD_FCOFF, NULL, 0);
12311292 break;
12321293 case CMD_MSC:
....@@ -1267,7 +1328,7 @@
12671328 */
12681329
12691330 static void gsm_control_response(struct gsm_mux *gsm, unsigned int command,
1270
- u8 *data, int clen)
1331
+ const u8 *data, int clen)
12711332 {
12721333 struct gsm_control *ctrl;
12731334 unsigned long flags;
....@@ -1300,17 +1361,18 @@
13001361
13011362 static void gsm_control_transmit(struct gsm_mux *gsm, struct gsm_control *ctrl)
13021363 {
1303
- struct gsm_msg *msg = gsm_data_alloc(gsm, 0, ctrl->len + 1, gsm->ftype);
1364
+ struct gsm_msg *msg = gsm_data_alloc(gsm, 0, ctrl->len + 2, gsm->ftype);
13041365 if (msg == NULL)
13051366 return;
1306
- msg->data[0] = (ctrl->cmd << 1) | 2 | EA; /* command */
1307
- memcpy(msg->data + 1, ctrl->data, ctrl->len);
1367
+ msg->data[0] = (ctrl->cmd << 1) | CR | EA; /* command */
1368
+ msg->data[1] = (ctrl->len << 1) | EA;
1369
+ memcpy(msg->data + 2, ctrl->data, ctrl->len);
13081370 gsm_data_queue(gsm->dlci[0], msg);
13091371 }
13101372
13111373 /**
13121374 * gsm_control_retransmit - retransmit a control frame
1313
- * @data: pointer to our gsm object
1375
+ * @t: timer contained in our gsm object
13141376 *
13151377 * Called off the T2 timer expiry in order to retransmit control frames
13161378 * that have been lost in the system somewhere. The control_lock protects
....@@ -1327,8 +1389,7 @@
13271389 spin_lock_irqsave(&gsm->control_lock, flags);
13281390 ctrl = gsm->pending_cmd;
13291391 if (ctrl) {
1330
- gsm->cretries--;
1331
- if (gsm->cretries == 0) {
1392
+ if (gsm->cretries == 0 || !gsm->dlci[0] || gsm->dlci[0]->dead) {
13321393 gsm->pending_cmd = NULL;
13331394 ctrl->error = -ETIMEDOUT;
13341395 ctrl->done = 1;
....@@ -1336,6 +1397,7 @@
13361397 wake_up(&gsm->event);
13371398 return;
13381399 }
1400
+ gsm->cretries--;
13391401 gsm_control_transmit(gsm, ctrl);
13401402 mod_timer(&gsm->t2_timer, jiffies + gsm->t2 * HZ / 100);
13411403 }
....@@ -1347,7 +1409,7 @@
13471409 * @gsm: the GSM channel
13481410 * @command: command to send including CR bit
13491411 * @data: bytes of data (must be kmalloced)
1350
- * @len: length of the block to send
1412
+ * @clen: length of the block to send
13511413 *
13521414 * Queue and dispatch a control command. Only one command can be
13531415 * active at a time. In theory more can be outstanding but the matching
....@@ -1358,7 +1420,7 @@
13581420 unsigned int command, u8 *data, int clen)
13591421 {
13601422 struct gsm_control *ctrl = kzalloc(sizeof(struct gsm_control),
1361
- GFP_KERNEL);
1423
+ GFP_ATOMIC);
13621424 unsigned long flags;
13631425 if (ctrl == NULL)
13641426 return NULL;
....@@ -1376,7 +1438,7 @@
13761438
13771439 /* If DLCI0 is in ADM mode skip retries, it won't respond */
13781440 if (gsm->dlci[0]->mode == DLCI_MODE_ADM)
1379
- gsm->cretries = 1;
1441
+ gsm->cretries = 0;
13801442 else
13811443 gsm->cretries = gsm->n2;
13821444
....@@ -1424,15 +1486,24 @@
14241486
14251487 static void gsm_dlci_close(struct gsm_dlci *dlci)
14261488 {
1489
+ unsigned long flags;
1490
+
14271491 del_timer(&dlci->t1);
14281492 if (debug & 8)
14291493 pr_debug("DLCI %d goes closed.\n", dlci->addr);
14301494 dlci->state = DLCI_CLOSED;
1495
+ /* Prevent us from sending data before the link is up again */
1496
+ dlci->constipated = true;
14311497 if (dlci->addr != 0) {
14321498 tty_port_tty_hangup(&dlci->port, false);
1433
- kfifo_reset(dlci->fifo);
1499
+ spin_lock_irqsave(&dlci->lock, flags);
1500
+ kfifo_reset(&dlci->fifo);
1501
+ spin_unlock_irqrestore(&dlci->lock, flags);
1502
+ /* Ensure that gsmtty_open() can return. */
1503
+ tty_port_set_initialized(&dlci->port, 0);
1504
+ wake_up_interruptible(&dlci->port.open_wait);
14341505 } else
1435
- dlci->gsm->dead = 1;
1506
+ dlci->gsm->dead = true;
14361507 wake_up(&dlci->gsm->event);
14371508 /* A DLCI 0 close is a MUX termination so we need to kick that
14381509 back to userspace somehow */
....@@ -1452,6 +1523,7 @@
14521523 del_timer(&dlci->t1);
14531524 /* This will let a tty open continue */
14541525 dlci->state = DLCI_OPEN;
1526
+ dlci->constipated = false;
14551527 if (debug & 8)
14561528 pr_debug("DLCI %d goes open.\n", dlci->addr);
14571529 wake_up(&dlci->gsm->event);
....@@ -1459,7 +1531,7 @@
14591531
14601532 /**
14611533 * gsm_dlci_t1 - T1 timer expiry
1462
- * @dlci: DLCI that opened
1534
+ * @t: timer contained in the DLCI that opened
14631535 *
14641536 * The T1 timer handles retransmits of control frames (essentially of
14651537 * SABM and DISC). We resend the command until the retry count runs out
....@@ -1479,8 +1551,8 @@
14791551
14801552 switch (dlci->state) {
14811553 case DLCI_OPENING:
1482
- dlci->retries--;
14831554 if (dlci->retries) {
1555
+ dlci->retries--;
14841556 gsm_command(dlci->gsm, dlci->addr, SABM|PF);
14851557 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
14861558 } else if (!dlci->addr && gsm->control == (DM | PF)) {
....@@ -1495,12 +1567,15 @@
14951567
14961568 break;
14971569 case DLCI_CLOSING:
1498
- dlci->retries--;
14991570 if (dlci->retries) {
1571
+ dlci->retries--;
15001572 gsm_command(dlci->gsm, dlci->addr, DISC|PF);
15011573 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
15021574 } else
15031575 gsm_dlci_close(dlci);
1576
+ break;
1577
+ default:
1578
+ pr_debug("%s: unhandled state: %d\n", __func__, dlci->state);
15041579 break;
15051580 }
15061581 }
....@@ -1524,6 +1599,25 @@
15241599 dlci->state = DLCI_OPENING;
15251600 gsm_command(dlci->gsm, dlci->addr, SABM|PF);
15261601 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
1602
+}
1603
+
1604
+/**
1605
+ * gsm_dlci_set_opening - change state to opening
1606
+ * @dlci: DLCI to open
1607
+ *
1608
+ * Change internal state to wait for DLCI open from initiator side.
1609
+ * We set off timers and responses upon reception of an SABM.
1610
+ */
1611
+static void gsm_dlci_set_opening(struct gsm_dlci *dlci)
1612
+{
1613
+ switch (dlci->state) {
1614
+ case DLCI_CLOSED:
1615
+ case DLCI_CLOSING:
1616
+ dlci->state = DLCI_OPENING;
1617
+ break;
1618
+ default:
1619
+ break;
1620
+ }
15271621 }
15281622
15291623 /**
....@@ -1552,14 +1646,14 @@
15521646 * gsm_dlci_data - data arrived
15531647 * @dlci: channel
15541648 * @data: block of bytes received
1555
- * @len: length of received block
1649
+ * @clen: length of received block
15561650 *
15571651 * A UI or UIH frame has arrived which contains data for a channel
15581652 * other than the control channel. If the relevant virtual tty is
15591653 * open we shovel the bits down it, if not we drop them.
15601654 */
15611655
1562
-static void gsm_dlci_data(struct gsm_dlci *dlci, u8 *data, int clen)
1656
+static void gsm_dlci_data(struct gsm_dlci *dlci, const u8 *data, int clen)
15631657 {
15641658 /* krefs .. */
15651659 struct tty_port *port = &dlci->port;
....@@ -1571,14 +1665,11 @@
15711665 pr_debug("%d bytes for tty\n", len);
15721666 switch (dlci->adaption) {
15731667 /* Unsupported types */
1574
- /* Packetised interruptible data */
1575
- case 4:
1668
+ case 4: /* Packetised interruptible data */
15761669 break;
1577
- /* Packetised uininterruptible voice/data */
1578
- case 3:
1670
+ case 3: /* Packetised uininterruptible voice/data */
15791671 break;
1580
- /* Asynchronous serial with line state in each frame */
1581
- case 2:
1672
+ case 2: /* Asynchronous serial with line state in each frame */
15821673 while (gsm_read_ea(&modem, *data++) == 0) {
15831674 len--;
15841675 if (len == 0)
....@@ -1589,8 +1680,8 @@
15891680 gsm_process_modem(tty, dlci, modem, clen);
15901681 tty_kref_put(tty);
15911682 }
1592
- /* Line state will go via DLCI 0 controls only */
1593
- case 1:
1683
+ fallthrough;
1684
+ case 1: /* Line state will go via DLCI 0 controls only */
15941685 default:
15951686 tty_insert_flip_string(port, data, len);
15961687 tty_flip_buffer_push(port);
....@@ -1609,7 +1700,7 @@
16091700 * and we divide up the work accordingly.
16101701 */
16111702
1612
-static void gsm_dlci_command(struct gsm_dlci *dlci, u8 *data, int len)
1703
+static void gsm_dlci_command(struct gsm_dlci *dlci, const u8 *data, int len)
16131704 {
16141705 /* See what command is involved */
16151706 unsigned int command = 0;
....@@ -1654,8 +1745,7 @@
16541745 return NULL;
16551746 spin_lock_init(&dlci->lock);
16561747 mutex_init(&dlci->mutex);
1657
- dlci->fifo = &dlci->_fifo;
1658
- if (kfifo_alloc(&dlci->_fifo, 4096, GFP_KERNEL) < 0) {
1748
+ if (kfifo_alloc(&dlci->fifo, 4096, GFP_KERNEL) < 0) {
16591749 kfree(dlci);
16601750 return NULL;
16611751 }
....@@ -1668,17 +1758,20 @@
16681758 dlci->addr = addr;
16691759 dlci->adaption = gsm->adaption;
16701760 dlci->state = DLCI_CLOSED;
1671
- if (addr)
1761
+ if (addr) {
16721762 dlci->data = gsm_dlci_data;
1673
- else
1763
+ /* Prevent us from sending data before the link is up */
1764
+ dlci->constipated = true;
1765
+ } else {
16741766 dlci->data = gsm_dlci_command;
1767
+ }
16751768 gsm->dlci[addr] = dlci;
16761769 return dlci;
16771770 }
16781771
16791772 /**
16801773 * gsm_dlci_free - free DLCI
1681
- * @dlci: DLCI to free
1774
+ * @port: tty port for DLCI to free
16821775 *
16831776 * Free up a DLCI.
16841777 *
....@@ -1690,7 +1783,7 @@
16901783
16911784 del_timer_sync(&dlci->t1);
16921785 dlci->gsm->dlci[dlci->addr] = NULL;
1693
- kfifo_free(dlci->fifo);
1786
+ kfifo_free(&dlci->fifo);
16941787 while ((dlci->skb = skb_dequeue(&dlci->skb_list)))
16951788 dev_kfree_skb(dlci->skb);
16961789 kfree(dlci);
....@@ -1725,6 +1818,11 @@
17251818 gsm_destroy_network(dlci);
17261819 mutex_unlock(&dlci->mutex);
17271820
1821
+ /* We cannot use tty_hangup() because in tty_kref_put() the tty
1822
+ * driver assumes that the hangup queue is free and reuses it to
1823
+ * queue release_one_tty() -> NULL pointer panic in
1824
+ * process_one_work().
1825
+ */
17281826 tty_vhangup(tty);
17291827
17301828 tty_port_tty_set(&dlci->port, NULL);
....@@ -1808,7 +1906,6 @@
18081906 gsm_response(gsm, address, UA);
18091907 gsm_dlci_close(dlci);
18101908 break;
1811
- case UA:
18121909 case UA|PF:
18131910 if (cr == 0 || dlci == NULL)
18141911 break;
....@@ -1818,6 +1915,10 @@
18181915 break;
18191916 case DLCI_OPENING:
18201917 gsm_dlci_open(dlci);
1918
+ break;
1919
+ default:
1920
+ pr_debug("%s: unhandled state: %d\n", __func__,
1921
+ dlci->state);
18211922 break;
18221923 }
18231924 break;
....@@ -1838,7 +1939,7 @@
18381939 goto invalid;
18391940 #endif
18401941 if (dlci == NULL || dlci->state != DLCI_OPEN) {
1841
- gsm_command(gsm, address, DM|PF);
1942
+ gsm_response(gsm, address, DM|PF);
18421943 return;
18431944 }
18441945 dlci->data(dlci, gsm->buf, gsm->len);
....@@ -1932,6 +2033,9 @@
19322033 break;
19332034 }
19342035 break;
2036
+ default:
2037
+ pr_debug("%s: unhandled state: %d\n", __func__, gsm->state);
2038
+ break;
19352039 }
19362040 }
19372041
....@@ -1945,6 +2049,16 @@
19452049
19462050 static void gsm1_receive(struct gsm_mux *gsm, unsigned char c)
19472051 {
2052
+ /* handle XON/XOFF */
2053
+ if ((c & ISO_IEC_646_MASK) == XON) {
2054
+ gsm->constipated = true;
2055
+ return;
2056
+ } else if ((c & ISO_IEC_646_MASK) == XOFF) {
2057
+ gsm->constipated = false;
2058
+ /* Kick the link in case it is idling */
2059
+ gsm_data_kick(gsm, NULL);
2060
+ return;
2061
+ }
19482062 if (c == GSM1_SOF) {
19492063 /* EOF is only valid in frame if we have got to the data state
19502064 and received at least one byte (the FCS) */
....@@ -1959,7 +2073,8 @@
19592073 }
19602074 /* Any partial frame was a runt so go back to start */
19612075 if (gsm->state != GSM_START) {
1962
- gsm->malformed++;
2076
+ if (gsm->state != GSM_SEARCH)
2077
+ gsm->malformed++;
19632078 gsm->state = GSM_START;
19642079 }
19652080 /* A SOF in GSM_START means we are still reading idling or
....@@ -1968,7 +2083,7 @@
19682083 }
19692084
19702085 if (c == GSM1_ESCAPE) {
1971
- gsm->escape = 1;
2086
+ gsm->escape = true;
19722087 return;
19732088 }
19742089
....@@ -1978,14 +2093,14 @@
19782093
19792094 if (gsm->escape) {
19802095 c ^= GSM1_ESCAPE_BITS;
1981
- gsm->escape = 0;
2096
+ gsm->escape = false;
19822097 }
19832098 switch (gsm->state) {
19842099 case GSM_START: /* First byte after SOF */
19852100 gsm->address = 0;
19862101 gsm->state = GSM_ADDRESS;
19872102 gsm->fcs = INIT_FCS;
1988
- /* Drop through */
2103
+ fallthrough;
19892104 case GSM_ADDRESS: /* Address continuation */
19902105 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
19912106 if (gsm_read_ea(&gsm->address, c))
....@@ -2005,6 +2120,9 @@
20052120 gsm->buf[gsm->count++] = c;
20062121 break;
20072122 case GSM_OVERRUN: /* Over-long - eg a dropped SOF */
2123
+ break;
2124
+ default:
2125
+ pr_debug("%s: unhandled state: %d\n", __func__, gsm->state);
20082126 break;
20092127 }
20102128 }
....@@ -2028,74 +2146,46 @@
20282146 gsm->io_error++;
20292147 }
20302148
2031
-static int gsm_disconnect(struct gsm_mux *gsm)
2032
-{
2033
- struct gsm_dlci *dlci = gsm->dlci[0];
2034
- struct gsm_control *gc;
2035
-
2036
- if (!dlci)
2037
- return 0;
2038
-
2039
- /* In theory disconnecting DLCI 0 is sufficient but for some
2040
- modems this is apparently not the case. */
2041
- gc = gsm_control_send(gsm, CMD_CLD, NULL, 0);
2042
- if (gc)
2043
- gsm_control_wait(gsm, gc);
2044
-
2045
- del_timer_sync(&gsm->t2_timer);
2046
- /* Now we are sure T2 has stopped */
2047
-
2048
- gsm_dlci_begin_close(dlci);
2049
- wait_event_interruptible(gsm->event,
2050
- dlci->state == DLCI_CLOSED);
2051
-
2052
- if (signal_pending(current))
2053
- return -EINTR;
2054
-
2055
- return 0;
2056
-}
2057
-
20582149 /**
20592150 * gsm_cleanup_mux - generic GSM protocol cleanup
20602151 * @gsm: our mux
2152
+ * @disc: disconnect link?
20612153 *
20622154 * Clean up the bits of the mux which are the same for all framing
20632155 * protocols. Remove the mux from the mux table, stop all the timers
20642156 * and then shut down each device hanging up the channels as we go.
20652157 */
20662158
2067
-static void gsm_cleanup_mux(struct gsm_mux *gsm)
2159
+static void gsm_cleanup_mux(struct gsm_mux *gsm, bool disc)
20682160 {
20692161 int i;
2070
- struct gsm_dlci *dlci = gsm->dlci[0];
2162
+ struct gsm_dlci *dlci;
20712163 struct gsm_msg *txq, *ntxq;
20722164
2073
- gsm->dead = 1;
2074
-
2075
- spin_lock(&gsm_mux_lock);
2076
- for (i = 0; i < MAX_MUX; i++) {
2077
- if (gsm_mux[i] == gsm) {
2078
- gsm_mux[i] = NULL;
2079
- break;
2080
- }
2081
- }
2082
- spin_unlock(&gsm_mux_lock);
2083
- /* open failed before registering => nothing to do */
2084
- if (i == MAX_MUX)
2085
- return;
2086
-
2087
- del_timer_sync(&gsm->t2_timer);
2088
- /* Now we are sure T2 has stopped */
2089
- if (dlci)
2090
- dlci->dead = 1;
2091
-
2092
- /* Free up any link layer users */
2165
+ gsm->dead = true;
20932166 mutex_lock(&gsm->mutex);
2094
- for (i = 0; i < NUM_DLCI; i++)
2095
- if (gsm->dlci[i])
2167
+
2168
+ dlci = gsm->dlci[0];
2169
+ if (dlci) {
2170
+ if (disc && dlci->state != DLCI_CLOSED) {
2171
+ gsm_dlci_begin_close(dlci);
2172
+ wait_event(gsm->event, dlci->state == DLCI_CLOSED);
2173
+ }
2174
+ dlci->dead = true;
2175
+ }
2176
+
2177
+ /* Finish outstanding timers, making sure they are done */
2178
+ del_timer_sync(&gsm->t2_timer);
2179
+
2180
+ /* Free up any link layer users and finally the control channel */
2181
+ for (i = NUM_DLCI - 1; i >= 0; i--)
2182
+ if (gsm->dlci[i]) {
20962183 gsm_dlci_release(gsm->dlci[i]);
2184
+ gsm->dlci[i] = NULL;
2185
+ }
20972186 mutex_unlock(&gsm->mutex);
20982187 /* Now wipe the queues */
2188
+ tty_ldisc_flush(gsm->tty);
20992189 list_for_each_entry_safe(txq, ntxq, &gsm->tx_list, list)
21002190 kfree(txq);
21012191 INIT_LIST_HEAD(&gsm->tx_list);
....@@ -2113,46 +2203,36 @@
21132203 static int gsm_activate_mux(struct gsm_mux *gsm)
21142204 {
21152205 struct gsm_dlci *dlci;
2116
- int i = 0;
2117
-
2118
- timer_setup(&gsm->t2_timer, gsm_control_retransmit, 0);
2119
- init_waitqueue_head(&gsm->event);
2120
- spin_lock_init(&gsm->control_lock);
2121
- spin_lock_init(&gsm->tx_lock);
21222206
21232207 if (gsm->encoding == 0)
21242208 gsm->receive = gsm0_receive;
21252209 else
21262210 gsm->receive = gsm1_receive;
2127
- gsm->error = gsm_error;
2128
-
2129
- spin_lock(&gsm_mux_lock);
2130
- for (i = 0; i < MAX_MUX; i++) {
2131
- if (gsm_mux[i] == NULL) {
2132
- gsm->num = i;
2133
- gsm_mux[i] = gsm;
2134
- break;
2135
- }
2136
- }
2137
- spin_unlock(&gsm_mux_lock);
2138
- if (i == MAX_MUX)
2139
- return -EBUSY;
21402211
21412212 dlci = gsm_dlci_alloc(gsm, 0);
21422213 if (dlci == NULL)
21432214 return -ENOMEM;
2144
- gsm->dead = 0; /* Tty opens are now permissible */
2215
+ gsm->dead = false; /* Tty opens are now permissible */
21452216 return 0;
21462217 }
21472218
21482219 /**
21492220 * gsm_free_mux - free up a mux
2150
- * @mux: mux to free
2221
+ * @gsm: mux to free
21512222 *
21522223 * Dispose of allocated resources for a dead mux
21532224 */
21542225 static void gsm_free_mux(struct gsm_mux *gsm)
21552226 {
2227
+ int i;
2228
+
2229
+ for (i = 0; i < MAX_MUX; i++) {
2230
+ if (gsm == gsm_mux[i]) {
2231
+ gsm_mux[i] = NULL;
2232
+ break;
2233
+ }
2234
+ }
2235
+ mutex_destroy(&gsm->mutex);
21562236 kfree(gsm->txframe);
21572237 kfree(gsm->buf);
21582238 kfree(gsm);
....@@ -2160,7 +2240,7 @@
21602240
21612241 /**
21622242 * gsm_free_muxr - free up a mux
2163
- * @mux: mux to free
2243
+ * @ref: kreference to the mux to free
21642244 *
21652245 * Dispose of allocated resources for a dead mux
21662246 */
....@@ -2172,12 +2252,30 @@
21722252
21732253 static inline void mux_get(struct gsm_mux *gsm)
21742254 {
2255
+ unsigned long flags;
2256
+
2257
+ spin_lock_irqsave(&gsm_mux_lock, flags);
21752258 kref_get(&gsm->ref);
2259
+ spin_unlock_irqrestore(&gsm_mux_lock, flags);
21762260 }
21772261
21782262 static inline void mux_put(struct gsm_mux *gsm)
21792263 {
2264
+ unsigned long flags;
2265
+
2266
+ spin_lock_irqsave(&gsm_mux_lock, flags);
21802267 kref_put(&gsm->ref, gsm_free_muxr);
2268
+ spin_unlock_irqrestore(&gsm_mux_lock, flags);
2269
+}
2270
+
2271
+static inline unsigned int mux_num_to_base(struct gsm_mux *gsm)
2272
+{
2273
+ return gsm->num * NUM_DLCI;
2274
+}
2275
+
2276
+static inline unsigned int mux_line_to_num(unsigned int line)
2277
+{
2278
+ return line / NUM_DLCI;
21812279 }
21822280
21832281 /**
....@@ -2188,6 +2286,7 @@
21882286
21892287 static struct gsm_mux *gsm_alloc_mux(void)
21902288 {
2289
+ int i;
21912290 struct gsm_mux *gsm = kzalloc(sizeof(struct gsm_mux), GFP_KERNEL);
21922291 if (gsm == NULL)
21932292 return NULL;
....@@ -2196,7 +2295,7 @@
21962295 kfree(gsm);
21972296 return NULL;
21982297 }
2199
- gsm->txframe = kmalloc(2 * MAX_MRU + 2, GFP_KERNEL);
2298
+ gsm->txframe = kmalloc(2 * (MAX_MTU + PROT_OVERHEAD - 1), GFP_KERNEL);
22002299 if (gsm->txframe == NULL) {
22012300 kfree(gsm->buf);
22022301 kfree(gsm);
....@@ -2206,6 +2305,10 @@
22062305 mutex_init(&gsm->mutex);
22072306 kref_init(&gsm->ref);
22082307 INIT_LIST_HEAD(&gsm->tx_list);
2308
+ timer_setup(&gsm->t2_timer, gsm_control_retransmit, 0);
2309
+ init_waitqueue_head(&gsm->event);
2310
+ spin_lock_init(&gsm->control_lock);
2311
+ spin_lock_init(&gsm->tx_lock);
22092312
22102313 gsm->t1 = T1;
22112314 gsm->t2 = T2;
....@@ -2215,9 +2318,130 @@
22152318 gsm->encoding = 1;
22162319 gsm->mru = 64; /* Default to encoding 1 so these should be 64 */
22172320 gsm->mtu = 64;
2218
- gsm->dead = 1; /* Avoid early tty opens */
2321
+ gsm->dead = true; /* Avoid early tty opens */
2322
+
2323
+ /* Store the instance to the mux array or abort if no space is
2324
+ * available.
2325
+ */
2326
+ spin_lock(&gsm_mux_lock);
2327
+ for (i = 0; i < MAX_MUX; i++) {
2328
+ if (!gsm_mux[i]) {
2329
+ gsm_mux[i] = gsm;
2330
+ gsm->num = i;
2331
+ break;
2332
+ }
2333
+ }
2334
+ spin_unlock(&gsm_mux_lock);
2335
+ if (i == MAX_MUX) {
2336
+ mutex_destroy(&gsm->mutex);
2337
+ kfree(gsm->txframe);
2338
+ kfree(gsm->buf);
2339
+ kfree(gsm);
2340
+ return NULL;
2341
+ }
22192342
22202343 return gsm;
2344
+}
2345
+
2346
+static void gsm_copy_config_values(struct gsm_mux *gsm,
2347
+ struct gsm_config *c)
2348
+{
2349
+ memset(c, 0, sizeof(*c));
2350
+ c->adaption = gsm->adaption;
2351
+ c->encapsulation = gsm->encoding;
2352
+ c->initiator = gsm->initiator;
2353
+ c->t1 = gsm->t1;
2354
+ c->t2 = gsm->t2;
2355
+ c->t3 = 0; /* Not supported */
2356
+ c->n2 = gsm->n2;
2357
+ if (gsm->ftype == UIH)
2358
+ c->i = 1;
2359
+ else
2360
+ c->i = 2;
2361
+ pr_debug("Ftype %d i %d\n", gsm->ftype, c->i);
2362
+ c->mru = gsm->mru;
2363
+ c->mtu = gsm->mtu;
2364
+ c->k = 0;
2365
+}
2366
+
2367
+static int gsm_config(struct gsm_mux *gsm, struct gsm_config *c)
2368
+{
2369
+ int ret = 0;
2370
+ int need_close = 0;
2371
+ int need_restart = 0;
2372
+
2373
+ /* Stuff we don't support yet - UI or I frame transport, windowing */
2374
+ if ((c->adaption != 1 && c->adaption != 2) || c->k)
2375
+ return -EOPNOTSUPP;
2376
+ /* Check the MRU/MTU range looks sane */
2377
+ if (c->mru > MAX_MRU || c->mtu > MAX_MTU || c->mru < 8 || c->mtu < 8)
2378
+ return -EINVAL;
2379
+ if (c->n2 > 255)
2380
+ return -EINVAL;
2381
+ if (c->encapsulation > 1) /* Basic, advanced, no I */
2382
+ return -EINVAL;
2383
+ if (c->initiator > 1)
2384
+ return -EINVAL;
2385
+ if (c->i == 0 || c->i > 2) /* UIH and UI only */
2386
+ return -EINVAL;
2387
+ /*
2388
+ * See what is needed for reconfiguration
2389
+ */
2390
+
2391
+ /* Timing fields */
2392
+ if (c->t1 != 0 && c->t1 != gsm->t1)
2393
+ need_restart = 1;
2394
+ if (c->t2 != 0 && c->t2 != gsm->t2)
2395
+ need_restart = 1;
2396
+ if (c->encapsulation != gsm->encoding)
2397
+ need_restart = 1;
2398
+ if (c->adaption != gsm->adaption)
2399
+ need_restart = 1;
2400
+ /* Requires care */
2401
+ if (c->initiator != gsm->initiator)
2402
+ need_close = 1;
2403
+ if (c->mru != gsm->mru)
2404
+ need_restart = 1;
2405
+ if (c->mtu != gsm->mtu)
2406
+ need_restart = 1;
2407
+
2408
+ /*
2409
+ * Close down what is needed, restart and initiate the new
2410
+ * configuration. On the first time there is no DLCI[0]
2411
+ * and closing or cleaning up is not necessary.
2412
+ */
2413
+ if (need_close || need_restart)
2414
+ gsm_cleanup_mux(gsm, true);
2415
+
2416
+ gsm->initiator = c->initiator;
2417
+ gsm->mru = c->mru;
2418
+ gsm->mtu = c->mtu;
2419
+ gsm->encoding = c->encapsulation;
2420
+ gsm->adaption = c->adaption;
2421
+ gsm->n2 = c->n2;
2422
+
2423
+ if (c->i == 1)
2424
+ gsm->ftype = UIH;
2425
+ else if (c->i == 2)
2426
+ gsm->ftype = UI;
2427
+
2428
+ if (c->t1)
2429
+ gsm->t1 = c->t1;
2430
+ if (c->t2)
2431
+ gsm->t2 = c->t2;
2432
+
2433
+ /*
2434
+ * FIXME: We need to separate activation/deactivation from adding
2435
+ * and removing from the mux array
2436
+ */
2437
+ if (gsm->dead) {
2438
+ ret = gsm_activate_mux(gsm);
2439
+ if (ret)
2440
+ return ret;
2441
+ if (gsm->initiator)
2442
+ gsm_dlci_begin_open(gsm->dlci[0]);
2443
+ }
2444
+ return 0;
22212445 }
22222446
22232447 /**
....@@ -2255,19 +2479,32 @@
22552479
22562480 static int gsmld_attach_gsm(struct tty_struct *tty, struct gsm_mux *gsm)
22572481 {
2258
- int ret, i, base;
2482
+ unsigned int base;
2483
+ int ret, i;
22592484
22602485 gsm->tty = tty_kref_get(tty);
2261
- gsm->output = gsmld_output;
2486
+ /* Turn off tty XON/XOFF handling to handle it explicitly. */
2487
+ gsm->old_c_iflag = tty->termios.c_iflag;
2488
+ tty->termios.c_iflag &= (IXON | IXOFF);
22622489 ret = gsm_activate_mux(gsm);
22632490 if (ret != 0)
22642491 tty_kref_put(gsm->tty);
22652492 else {
22662493 /* Don't register device 0 - this is the control channel and not
22672494 a usable tty interface */
2268
- base = gsm->num << 6; /* Base for this MUX */
2269
- for (i = 1; i < NUM_DLCI; i++)
2270
- tty_register_device(gsm_tty_driver, base + i, NULL);
2495
+ base = mux_num_to_base(gsm); /* Base for this MUX */
2496
+ for (i = 1; i < NUM_DLCI; i++) {
2497
+ struct device *dev;
2498
+
2499
+ dev = tty_register_device(gsm_tty_driver,
2500
+ base + i, NULL);
2501
+ if (IS_ERR(dev)) {
2502
+ for (i--; i >= 1; i--)
2503
+ tty_unregister_device(gsm_tty_driver,
2504
+ base + i);
2505
+ return PTR_ERR(dev);
2506
+ }
2507
+ }
22712508 }
22722509 return ret;
22732510 }
....@@ -2283,13 +2520,14 @@
22832520
22842521 static void gsmld_detach_gsm(struct tty_struct *tty, struct gsm_mux *gsm)
22852522 {
2523
+ unsigned int base = mux_num_to_base(gsm); /* Base for this MUX */
22862524 int i;
2287
- int base = gsm->num << 6; /* Base for this MUX */
22882525
22892526 WARN_ON(tty != gsm->tty);
22902527 for (i = 1; i < NUM_DLCI; i++)
22912528 tty_unregister_device(gsm_tty_driver, base + i);
2292
- gsm_cleanup_mux(gsm);
2529
+ /* Restore tty XON/XOFF handling. */
2530
+ gsm->tty->termios.c_iflag = gsm->old_c_iflag;
22932531 tty_kref_put(gsm->tty);
22942532 gsm->tty = NULL;
22952533 }
....@@ -2318,7 +2556,7 @@
23182556 case TTY_BREAK:
23192557 case TTY_PARITY:
23202558 case TTY_FRAME:
2321
- gsm->error(gsm, *dp, flags);
2559
+ gsm_error(gsm, *dp, flags);
23222560 break;
23232561 default:
23242562 WARN_ONCE(1, "%s: unknown flag %d\n",
....@@ -2356,6 +2594,12 @@
23562594 static void gsmld_close(struct tty_struct *tty)
23572595 {
23582596 struct gsm_mux *gsm = tty->disc_data;
2597
+
2598
+ /* The ldisc locks and closes the port before calling our close. This
2599
+ * means we have no way to do a proper disconnect. We will not bother
2600
+ * to do one.
2601
+ */
2602
+ gsm_cleanup_mux(gsm, false);
23592603
23602604 gsmld_detach_gsm(tty, gsm);
23612605
....@@ -2395,7 +2639,7 @@
23952639
23962640 ret = gsmld_attach_gsm(tty, gsm);
23972641 if (ret != 0) {
2398
- gsm_cleanup_mux(gsm);
2642
+ gsm_cleanup_mux(gsm, false);
23992643 mux_put(gsm);
24002644 }
24012645 return ret;
....@@ -2441,7 +2685,8 @@
24412685 */
24422686
24432687 static ssize_t gsmld_read(struct tty_struct *tty, struct file *file,
2444
- unsigned char __user *buf, size_t nr)
2688
+ unsigned char *buf, size_t nr,
2689
+ void **cookie, unsigned long offset)
24452690 {
24462691 return -EOPNOTSUPP;
24472692 }
....@@ -2463,11 +2708,24 @@
24632708 static ssize_t gsmld_write(struct tty_struct *tty, struct file *file,
24642709 const unsigned char *buf, size_t nr)
24652710 {
2466
- int space = tty_write_room(tty);
2711
+ struct gsm_mux *gsm = tty->disc_data;
2712
+ unsigned long flags;
2713
+ int space;
2714
+ int ret;
2715
+
2716
+ if (!gsm)
2717
+ return -ENODEV;
2718
+
2719
+ ret = -ENOBUFS;
2720
+ spin_lock_irqsave(&gsm->tx_lock, flags);
2721
+ space = tty_write_room(tty);
24672722 if (space >= nr)
2468
- return tty->ops->write(tty, buf, nr);
2469
- set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
2470
- return -ENOBUFS;
2723
+ ret = tty->ops->write(tty, buf, nr);
2724
+ else
2725
+ set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
2726
+ spin_unlock_irqrestore(&gsm->tx_lock, flags);
2727
+
2728
+ return ret;
24712729 }
24722730
24732731 /**
....@@ -2492,96 +2750,16 @@
24922750
24932751 poll_wait(file, &tty->read_wait, wait);
24942752 poll_wait(file, &tty->write_wait, wait);
2753
+
2754
+ if (gsm->dead)
2755
+ mask |= EPOLLHUP;
24952756 if (tty_hung_up_p(file))
2757
+ mask |= EPOLLHUP;
2758
+ if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
24962759 mask |= EPOLLHUP;
24972760 if (!tty_is_writelocked(tty) && tty_write_room(tty) > 0)
24982761 mask |= EPOLLOUT | EPOLLWRNORM;
2499
- if (gsm->dead)
2500
- mask |= EPOLLHUP;
25012762 return mask;
2502
-}
2503
-
2504
-static int gsmld_config(struct tty_struct *tty, struct gsm_mux *gsm,
2505
- struct gsm_config *c)
2506
-{
2507
- int need_close = 0;
2508
- int need_restart = 0;
2509
-
2510
- /* Stuff we don't support yet - UI or I frame transport, windowing */
2511
- if ((c->adaption != 1 && c->adaption != 2) || c->k)
2512
- return -EOPNOTSUPP;
2513
- /* Check the MRU/MTU range looks sane */
2514
- if (c->mru > MAX_MRU || c->mtu > MAX_MTU || c->mru < 8 || c->mtu < 8)
2515
- return -EINVAL;
2516
- if (c->n2 < 3)
2517
- return -EINVAL;
2518
- if (c->encapsulation > 1) /* Basic, advanced, no I */
2519
- return -EINVAL;
2520
- if (c->initiator > 1)
2521
- return -EINVAL;
2522
- if (c->i == 0 || c->i > 2) /* UIH and UI only */
2523
- return -EINVAL;
2524
- /*
2525
- * See what is needed for reconfiguration
2526
- */
2527
-
2528
- /* Timing fields */
2529
- if (c->t1 != 0 && c->t1 != gsm->t1)
2530
- need_restart = 1;
2531
- if (c->t2 != 0 && c->t2 != gsm->t2)
2532
- need_restart = 1;
2533
- if (c->encapsulation != gsm->encoding)
2534
- need_restart = 1;
2535
- if (c->adaption != gsm->adaption)
2536
- need_restart = 1;
2537
- /* Requires care */
2538
- if (c->initiator != gsm->initiator)
2539
- need_close = 1;
2540
- if (c->mru != gsm->mru)
2541
- need_restart = 1;
2542
- if (c->mtu != gsm->mtu)
2543
- need_restart = 1;
2544
-
2545
- /*
2546
- * Close down what is needed, restart and initiate the new
2547
- * configuration
2548
- */
2549
-
2550
- if (need_close || need_restart) {
2551
- int ret;
2552
-
2553
- ret = gsm_disconnect(gsm);
2554
-
2555
- if (ret)
2556
- return ret;
2557
- }
2558
- if (need_restart)
2559
- gsm_cleanup_mux(gsm);
2560
-
2561
- gsm->initiator = c->initiator;
2562
- gsm->mru = c->mru;
2563
- gsm->mtu = c->mtu;
2564
- gsm->encoding = c->encapsulation;
2565
- gsm->adaption = c->adaption;
2566
- gsm->n2 = c->n2;
2567
-
2568
- if (c->i == 1)
2569
- gsm->ftype = UIH;
2570
- else if (c->i == 2)
2571
- gsm->ftype = UI;
2572
-
2573
- if (c->t1)
2574
- gsm->t1 = c->t1;
2575
- if (c->t2)
2576
- gsm->t2 = c->t2;
2577
-
2578
- /* FIXME: We need to separate activation/deactivation from adding
2579
- and removing from the mux array */
2580
- if (need_restart)
2581
- gsm_activate_mux(gsm);
2582
- if (gsm->initiator && need_close)
2583
- gsm_dlci_begin_open(gsm->dlci[0]);
2584
- return 0;
25852763 }
25862764
25872765 static int gsmld_ioctl(struct tty_struct *tty, struct file *file,
....@@ -2589,44 +2767,25 @@
25892767 {
25902768 struct gsm_config c;
25912769 struct gsm_mux *gsm = tty->disc_data;
2770
+ unsigned int base;
25922771
25932772 switch (cmd) {
25942773 case GSMIOC_GETCONF:
2595
- memset(&c, 0, sizeof(c));
2596
- c.adaption = gsm->adaption;
2597
- c.encapsulation = gsm->encoding;
2598
- c.initiator = gsm->initiator;
2599
- c.t1 = gsm->t1;
2600
- c.t2 = gsm->t2;
2601
- c.t3 = 0; /* Not supported */
2602
- c.n2 = gsm->n2;
2603
- if (gsm->ftype == UIH)
2604
- c.i = 1;
2605
- else
2606
- c.i = 2;
2607
- pr_debug("Ftype %d i %d\n", gsm->ftype, c.i);
2608
- c.mru = gsm->mru;
2609
- c.mtu = gsm->mtu;
2610
- c.k = 0;
2611
- if (copy_to_user((void *)arg, &c, sizeof(c)))
2774
+ gsm_copy_config_values(gsm, &c);
2775
+ if (copy_to_user((void __user *)arg, &c, sizeof(c)))
26122776 return -EFAULT;
26132777 return 0;
26142778 case GSMIOC_SETCONF:
2615
- if (copy_from_user(&c, (void *)arg, sizeof(c)))
2779
+ if (copy_from_user(&c, (void __user *)arg, sizeof(c)))
26162780 return -EFAULT;
2617
- return gsmld_config(tty, gsm, &c);
2781
+ return gsm_config(gsm, &c);
2782
+ case GSMIOC_GETFIRST:
2783
+ base = mux_num_to_base(gsm);
2784
+ return put_user(base + 1, (__u32 __user *)arg);
26182785 default:
26192786 return n_tty_ioctl_helper(tty, file, cmd, arg);
26202787 }
26212788 }
2622
-
2623
-#ifdef CONFIG_COMPAT
2624
-static long gsmld_compat_ioctl(struct tty_struct *tty, struct file *file,
2625
- unsigned int cmd, unsigned long arg)
2626
-{
2627
- return gsmld_ioctl(tty, file, cmd, arg);
2628
-}
2629
-#endif
26302789
26312790 /*
26322791 * Network interface
....@@ -2699,7 +2858,7 @@
26992858 }
27002859
27012860 /* called when a packet did not ack after watchdogtimeout */
2702
-static void gsm_mux_net_tx_timeout(struct net_device *net)
2861
+static void gsm_mux_net_tx_timeout(struct net_device *net, unsigned int txqueue)
27032862 {
27042863 /* Tell syslog we are hosed. */
27052864 dev_dbg(&net->dev, "Tx timed out.\n");
....@@ -2709,7 +2868,7 @@
27092868 }
27102869
27112870 static void gsm_mux_rx_netchar(struct gsm_dlci *dlci,
2712
- unsigned char *in_buf, int size)
2871
+ const unsigned char *in_buf, int size)
27132872 {
27142873 struct net_device *net = dlci->net;
27152874 struct sk_buff *skb;
....@@ -2764,7 +2923,7 @@
27642923 {
27652924 struct gsm_mux_net *mux_net;
27662925
2767
- pr_debug("destroy network interface");
2926
+ pr_debug("destroy network interface\n");
27682927 if (!dlci->net)
27692928 return;
27702929 mux_net = netdev_priv(dlci->net);
....@@ -2793,7 +2952,7 @@
27932952 if (nc->adaption != 3 && nc->adaption != 4)
27942953 return -EPROTONOSUPPORT;
27952954
2796
- pr_debug("create network interface");
2955
+ pr_debug("create network interface\n");
27972956
27982957 netname = "gsm%d";
27992958 if (nc->if_name[0] != '\0')
....@@ -2801,7 +2960,7 @@
28012960 net = alloc_netdev(sizeof(struct gsm_mux_net), netname,
28022961 NET_NAME_UNKNOWN, gsm_mux_net_init);
28032962 if (!net) {
2804
- pr_err("alloc_netdev failed");
2963
+ pr_err("alloc_netdev failed\n");
28052964 return -ENOMEM;
28062965 }
28072966 net->mtu = dlci->gsm->mtu;
....@@ -2819,7 +2978,7 @@
28192978 dlci->data = gsm_mux_rx_netchar;
28202979 dlci->net = net;
28212980
2822
- pr_debug("register netdev");
2981
+ pr_debug("register netdev\n");
28232982 retval = register_netdev(net);
28242983 if (retval) {
28252984 pr_err("network register fail %d\n", retval);
....@@ -2839,9 +2998,6 @@
28392998 .flush_buffer = gsmld_flush_buffer,
28402999 .read = gsmld_read,
28413000 .write = gsmld_write,
2842
-#ifdef CONFIG_COMPAT
2843
- .compat_ioctl = gsmld_compat_ioctl,
2844
-#endif
28453001 .ioctl = gsmld_ioctl,
28463002 .poll = gsmld_poll,
28473003 .receive_buf = gsmld_receive_buf,
....@@ -2856,19 +3012,17 @@
28563012
28573013 static int gsmtty_modem_update(struct gsm_dlci *dlci, u8 brk)
28583014 {
2859
- u8 modembits[5];
3015
+ u8 modembits[3];
28603016 struct gsm_control *ctrl;
28613017 int len = 2;
28623018
2863
- if (brk)
3019
+ modembits[0] = (dlci->addr << 2) | 2 | EA; /* DLCI, Valid, EA */
3020
+ modembits[1] = (gsm_encode_modem(dlci) << 1) | EA;
3021
+ if (brk) {
3022
+ modembits[2] = (brk << 4) | 2 | EA; /* Length, Break, EA */
28643023 len++;
2865
-
2866
- modembits[0] = len << 1 | EA; /* Data bytes */
2867
- modembits[1] = dlci->addr << 2 | 3; /* DLCI, EA, 1 */
2868
- modembits[2] = gsm_encode_modem(dlci) << 1 | EA;
2869
- if (brk)
2870
- modembits[3] = brk << 4 | 2 | EA; /* Valid, EA */
2871
- ctrl = gsm_control_send(dlci->gsm, CMD_MSC, modembits, len + 1);
3024
+ }
3025
+ ctrl = gsm_control_send(dlci->gsm, CMD_MSC, modembits, len);
28723026 if (ctrl == NULL)
28733027 return -ENOMEM;
28743028 return gsm_control_wait(dlci->gsm, ctrl);
....@@ -2921,7 +3075,7 @@
29213075 struct gsm_mux *gsm;
29223076 struct gsm_dlci *dlci;
29233077 unsigned int line = tty->index;
2924
- unsigned int mux = line >> 6;
3078
+ unsigned int mux = mux_line_to_num(line);
29253079 bool alloc = false;
29263080 int ret;
29273081
....@@ -2976,6 +3130,7 @@
29763130 {
29773131 struct gsm_dlci *dlci = tty->driver_data;
29783132 struct tty_port *port = &dlci->port;
3133
+ struct gsm_mux *gsm = dlci->gsm;
29793134
29803135 port->count++;
29813136 tty_port_tty_set(port, tty);
....@@ -2985,7 +3140,10 @@
29853140 a DM straight back. This is ok as that will have caused a hangup */
29863141 tty_port_set_initialized(port, 1);
29873142 /* Start sending off SABM messages */
2988
- gsm_dlci_begin_open(dlci);
3143
+ if (gsm->initiator)
3144
+ gsm_dlci_begin_open(dlci);
3145
+ else
3146
+ gsm_dlci_set_opening(dlci);
29893147 /* And wait for virtual carrier */
29903148 return tty_port_block_til_ready(port, tty, filp);
29913149 }
....@@ -3028,7 +3186,7 @@
30283186 if (dlci->state == DLCI_CLOSED)
30293187 return -EINVAL;
30303188 /* Stuff the bytes into the fifo queue */
3031
- sent = kfifo_in_locked(dlci->fifo, buf, len, &dlci->lock);
3189
+ sent = kfifo_in_locked(&dlci->fifo, buf, len, &dlci->lock);
30323190 /* Need to kick the channel */
30333191 gsm_dlci_data_kick(dlci);
30343192 return sent;
....@@ -3039,7 +3197,7 @@
30393197 struct gsm_dlci *dlci = tty->driver_data;
30403198 if (dlci->state == DLCI_CLOSED)
30413199 return -EINVAL;
3042
- return TX_SIZE - kfifo_len(dlci->fifo);
3200
+ return TX_SIZE - kfifo_len(&dlci->fifo);
30433201 }
30443202
30453203 static int gsmtty_chars_in_buffer(struct tty_struct *tty)
....@@ -3047,19 +3205,23 @@
30473205 struct gsm_dlci *dlci = tty->driver_data;
30483206 if (dlci->state == DLCI_CLOSED)
30493207 return -EINVAL;
3050
- return kfifo_len(dlci->fifo);
3208
+ return kfifo_len(&dlci->fifo);
30513209 }
30523210
30533211 static void gsmtty_flush_buffer(struct tty_struct *tty)
30543212 {
30553213 struct gsm_dlci *dlci = tty->driver_data;
3214
+ unsigned long flags;
3215
+
30563216 if (dlci->state == DLCI_CLOSED)
30573217 return;
30583218 /* Caution needed: If we implement reliable transport classes
30593219 then the data being transmitted can't simply be junked once
30603220 it has first hit the stack. Until then we can just blow it
30613221 away */
3062
- kfifo_reset(dlci->fifo);
3222
+ spin_lock_irqsave(&dlci->lock, flags);
3223
+ kfifo_reset(&dlci->fifo);
3224
+ spin_unlock_irqrestore(&dlci->lock, flags);
30633225 /* Need to unhook this DLCI from the transmit queue logic */
30643226 }
30653227
....@@ -3149,9 +3311,9 @@
31493311 if (dlci->state == DLCI_CLOSED)
31503312 return;
31513313 if (C_CRTSCTS(tty))
3152
- dlci->modem_tx &= ~TIOCM_DTR;
3153
- dlci->throttled = 1;
3154
- /* Send an MSC with DTR cleared */
3314
+ dlci->modem_tx &= ~TIOCM_RTS;
3315
+ dlci->throttled = true;
3316
+ /* Send an MSC with RTS cleared */
31553317 gsmtty_modem_update(dlci, 0);
31563318 }
31573319
....@@ -3161,9 +3323,9 @@
31613323 if (dlci->state == DLCI_CLOSED)
31623324 return;
31633325 if (C_CRTSCTS(tty))
3164
- dlci->modem_tx |= TIOCM_DTR;
3165
- dlci->throttled = 0;
3166
- /* Send an MSC with DTR set */
3326
+ dlci->modem_tx |= TIOCM_RTS;
3327
+ dlci->throttled = false;
3328
+ /* Send an MSC with RTS set */
31673329 gsmtty_modem_update(dlci, 0);
31683330 }
31693331