hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/drivers/tty/n_gsm.c
....@@ -72,14 +72,15 @@
7272 */
7373 #define MAX_MRU 1500
7474 #define MAX_MTU 1500
75
+/* SOF, ADDR, CTRL, LEN1, LEN2, ..., FCS, EOF */
76
+#define PROT_OVERHEAD 7
7577 #define GSM_NET_TX_TIMEOUT (HZ*10)
7678
7779 /**
7880 * struct gsm_mux_net - network interface
79
- * @struct gsm_dlci* dlci
8081 *
8182 * Created when net interface is initialized.
82
- **/
83
+ */
8384 struct gsm_mux_net {
8485 struct kref ref;
8586 struct gsm_dlci *dlci;
....@@ -97,7 +98,19 @@
9798 u8 ctrl; /* Control byte + flags */
9899 unsigned int len; /* Length of data block (can be zero) */
99100 unsigned char *data; /* Points into buffer but not at the start */
100
- unsigned char buffer[0];
101
+ unsigned char buffer[];
102
+};
103
+
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
+
111
+enum gsm_dlci_mode {
112
+ DLCI_MODE_ABM, /* Normal Asynchronous Balanced Mode */
113
+ DLCI_MODE_ADM, /* Asynchronous Disconnected Mode */
101114 };
102115
103116 /*
....@@ -113,38 +126,31 @@
113126 struct gsm_dlci {
114127 struct gsm_mux *gsm;
115128 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 */
129
+ enum gsm_dlci_state state;
121130 struct mutex mutex;
122131
123132 /* Link layer */
124
- int mode;
125
-#define DLCI_MODE_ABM 0 /* Normal Asynchronous Balanced Mode */
126
-#define DLCI_MODE_ADM 1 /* Asynchronous Disconnected Mode */
133
+ enum gsm_dlci_mode mode;
127134 spinlock_t lock; /* Protects the internal state */
128135 struct timer_list t1; /* Retransmit timer for SABM and UA */
129136 int retries;
130137 /* Uplink tty if active */
131138 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 */
139
+ struct kfifo fifo; /* Queue fifo for the DLCI */
134140 int adaption; /* Adaption layer in use */
135141 int prev_adaption;
136142 u32 modem_rx; /* Our incoming virtual modem lines */
137143 u32 modem_tx; /* Our outgoing modem lines */
138
- int dead; /* Refuse re-open */
144
+ bool dead; /* Refuse re-open */
139145 /* Flow control */
140
- int throttled; /* Private copy of throttle state */
141
- int constipated; /* Throttle status for outgoing */
146
+ bool throttled; /* Private copy of throttle state */
147
+ bool constipated; /* Throttle status for outgoing */
142148 /* Packetised I/O */
143149 struct sk_buff *skb; /* Frame being sent */
144150 struct sk_buff_head skb_list; /* Queued frames */
145151 /* 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);
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);
148154 struct net_device *net; /* network interface, if created */
149155 };
150156
....@@ -166,6 +172,20 @@
166172 int len; /* Length of block for retransmission */
167173 int done; /* Done flag */
168174 int error; /* Error if any */
175
+};
176
+
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,
169189 };
170190
171191 /*
....@@ -192,41 +212,28 @@
192212
193213 /* Framing Layer */
194214 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
215
+ enum gsm_mux_state state;
207216 unsigned int len;
208217 unsigned int address;
209218 unsigned int count;
210
- int escape;
219
+ bool escape;
211220 int encoding;
212221 u8 control;
213222 u8 fcs;
214223 u8 received_fcs;
215224 u8 *txframe; /* TX framing buffer */
216225
217
- /* Methods for the receiver side */
226
+ /* Method for the receiver side */
218227 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);
222228
223229 /* Link Layer */
224230 unsigned int mru;
225231 unsigned int mtu;
226232 int initiator; /* Did we initiate connection */
227
- int dead; /* Has the mux been shut down */
233
+ bool dead; /* Has the mux been shut down */
228234 struct gsm_dlci *dlci[NUM_DLCI];
229
- int constipated; /* Asked by remote to shut up */
235
+ int old_c_iflag; /* termios c_iflag value before attach */
236
+ bool constipated; /* Asked by remote to shut up */
230237
231238 spinlock_t tx_lock;
232239 unsigned int tx_bytes; /* TX data outstanding */
....@@ -359,6 +366,8 @@
359366 #define INIT_FCS 0xFF
360367 #define GOOD_FCS 0xCF
361368
369
+static int gsmld_output(struct gsm_mux *gsm, u8 *data, int len);
370
+
362371 /**
363372 * gsm_fcs_add - update FCS
364373 * @fcs: Current FCS
....@@ -393,7 +402,7 @@
393402 /**
394403 * gsm_read_ea - read a byte into an EA
395404 * @val: variable holding value
396
- * c: byte going into the EA
405
+ * @c: byte going into the EA
397406 *
398407 * Processes one byte of an EA. Updates the passed variable
399408 * and returns 1 if the EA is now completely read
....@@ -406,6 +415,27 @@
406415 *val |= c >> 1;
407416 /* Was this the last byte of the EA 1 = yes*/
408417 return c & EA;
418
+}
419
+
420
+/**
421
+ * gsm_read_ea_val - read a value until EA
422
+ * @val: variable holding value
423
+ * @data: buffer of data
424
+ * @dlen: length of data
425
+ *
426
+ * Processes an EA value. Updates the passed variable and
427
+ * returns the processed data length.
428
+ */
429
+static unsigned int gsm_read_ea_val(unsigned int *val, const u8 *data, int dlen)
430
+{
431
+ unsigned int len = 0;
432
+
433
+ for (; dlen > 0; dlen--) {
434
+ len++;
435
+ if (gsm_read_ea(val, *data++))
436
+ break;
437
+ }
438
+ return len;
409439 }
410440
411441 /**
....@@ -497,18 +527,7 @@
497527 else
498528 pr_cont("(F)");
499529
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");
530
+ print_hex_dump_bytes("", DUMP_PREFIX_NONE, data, dlen);
512531 }
513532
514533
....@@ -518,8 +537,8 @@
518537
519538 /**
520539 * gsm_stuff_packet - bytestuff a packet
521
- * @ibuf: input
522
- * @obuf: output
540
+ * @input: input buffer
541
+ * @output: output buffer
523542 * @len: length of input
524543 *
525544 * Expand a buffer by bytestuffing it. The worst case size change
....@@ -592,7 +611,7 @@
592611 WARN_ON(1);
593612 return;
594613 }
595
- gsm->output(gsm, cbuf, len);
614
+ gsmld_output(gsm, cbuf, len);
596615 gsm_print_packet("-->", addr, cr, control, NULL, 0);
597616 }
598617
....@@ -656,6 +675,37 @@
656675 }
657676
658677 /**
678
+ * gsm_is_flow_ctrl_msg - checks if flow control message
679
+ * @msg: message to check
680
+ *
681
+ * Returns true if the given message is a flow control command of the
682
+ * control channel. False is returned in any other case.
683
+ */
684
+static bool gsm_is_flow_ctrl_msg(struct gsm_msg *msg)
685
+{
686
+ unsigned int cmd;
687
+
688
+ if (msg->addr > 0)
689
+ return false;
690
+
691
+ switch (msg->ctrl & ~PF) {
692
+ case UI:
693
+ case UIH:
694
+ cmd = 0;
695
+ if (gsm_read_ea_val(&cmd, msg->data + 2, msg->len - 2) < 1)
696
+ break;
697
+ switch (cmd & ~PF) {
698
+ case CMD_FCOFF:
699
+ case CMD_FCON:
700
+ return true;
701
+ }
702
+ break;
703
+ }
704
+
705
+ return false;
706
+}
707
+
708
+/**
659709 * gsm_data_kick - poke the queue
660710 * @gsm: GSM Mux
661711 *
....@@ -673,7 +723,7 @@
673723 int len;
674724
675725 list_for_each_entry_safe(msg, nmsg, &gsm->tx_list, list) {
676
- if (gsm->constipated && msg->addr)
726
+ if (gsm->constipated && !gsm_is_flow_ctrl_msg(msg))
677727 continue;
678728 if (gsm->encoding != 0) {
679729 gsm->txframe[0] = GSM1_SOF;
....@@ -692,7 +742,7 @@
692742 print_hex_dump_bytes("gsm_data_kick: ",
693743 DUMP_PREFIX_OFFSET,
694744 gsm->txframe, len);
695
- if (gsm->output(gsm, gsm->txframe, len) < 0)
745
+ if (gsmld_output(gsm, gsm->txframe, len) < 0)
696746 break;
697747 /* FIXME: Can eliminate one SOF in many more cases */
698748 gsm->tx_bytes -= msg->len;
....@@ -797,41 +847,51 @@
797847 {
798848 struct gsm_msg *msg;
799849 u8 *dp;
800
- int len, total_size, size;
801
- int h = dlci->adaption - 1;
850
+ int h, len, size;
802851
803
- total_size = 0;
804
- while (1) {
805
- len = kfifo_len(dlci->fifo);
806
- if (len == 0)
807
- return total_size;
852
+ /* for modem bits without break data */
853
+ h = ((dlci->adaption == 1) ? 0 : 1);
808854
809
- /* MTU/MRU count only the data bits */
810
- if (len > gsm->mtu)
811
- len = gsm->mtu;
855
+ len = kfifo_len(&dlci->fifo);
856
+ if (len == 0)
857
+ return 0;
812858
813
- size = len + h;
859
+ /* MTU/MRU count only the data bits but watch adaption mode */
860
+ if ((len + h) > gsm->mtu)
861
+ len = gsm->mtu - h;
814862
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;
863
+ size = len + h;
864
+
865
+ msg = gsm_data_alloc(gsm, dlci->addr, size, gsm->ftype);
866
+ /* FIXME: need a timer or something to kick this so it can't
867
+ * get stuck with no work outstanding and no buffer free
868
+ */
869
+ if (!msg)
870
+ return -ENOMEM;
871
+ dp = msg->data;
872
+ switch (dlci->adaption) {
873
+ case 1: /* Unstructured */
874
+ break;
875
+ case 2: /* Unstructured with modem bits.
876
+ * Always one byte as we never send inline break data
877
+ */
878
+ *dp++ = (gsm_encode_modem(dlci) << 1) | EA;
879
+ break;
880
+ default:
881
+ pr_err("%s: unsupported adaption %d\n", __func__,
882
+ dlci->adaption);
883
+ break;
832884 }
885
+
886
+ WARN_ON(len != kfifo_out_locked(&dlci->fifo, dp, len,
887
+ &dlci->lock));
888
+
889
+ /* Notify upper layer about available send space. */
890
+ tty_port_tty_wakeup(&dlci->port);
891
+
892
+ __gsm_data_queue(dlci, msg);
833893 /* Bytes of data we used up */
834
- return total_size;
894
+ return size;
835895 }
836896
837897 /**
....@@ -994,7 +1054,7 @@
9941054 * Encode up and queue a UI/UIH frame containing our response.
9951055 */
9961056
997
-static void gsm_control_reply(struct gsm_mux *gsm, int cmd, u8 *data,
1057
+static void gsm_control_reply(struct gsm_mux *gsm, int cmd, const u8 *data,
9981058 int dlen)
9991059 {
10001060 struct gsm_msg *msg;
....@@ -1040,9 +1100,9 @@
10401100 fc = (modem & MDM_FC) || !(modem & MDM_RTR);
10411101 if (fc && !dlci->constipated) {
10421102 /* Need to throttle our output on this device */
1043
- dlci->constipated = 1;
1103
+ dlci->constipated = true;
10441104 } else if (!fc && dlci->constipated) {
1045
- dlci->constipated = 0;
1105
+ dlci->constipated = false;
10461106 gsm_dlci_data_kick(dlci);
10471107 }
10481108
....@@ -1079,14 +1139,14 @@
10791139 * and if need be stuff a break message down the tty.
10801140 */
10811141
1082
-static void gsm_control_modem(struct gsm_mux *gsm, u8 *data, int clen)
1142
+static void gsm_control_modem(struct gsm_mux *gsm, const u8 *data, int clen)
10831143 {
10841144 unsigned int addr = 0;
10851145 unsigned int modem = 0;
10861146 unsigned int brk = 0;
10871147 struct gsm_dlci *dlci;
10881148 int len = clen;
1089
- u8 *dp = data;
1149
+ const u8 *dp = data;
10901150 struct tty_struct *tty;
10911151
10921152 while (gsm_read_ea(&addr, *dp++) == 0) {
....@@ -1140,13 +1200,13 @@
11401200 * this into the uplink tty if present
11411201 */
11421202
1143
-static void gsm_control_rls(struct gsm_mux *gsm, u8 *data, int clen)
1203
+static void gsm_control_rls(struct gsm_mux *gsm, const u8 *data, int clen)
11441204 {
11451205 struct tty_port *port;
11461206 unsigned int addr = 0;
11471207 u8 bits;
11481208 int len = clen;
1149
- u8 *dp = data;
1209
+ const u8 *dp = data;
11501210
11511211 while (gsm_read_ea(&addr, *dp++) == 0) {
11521212 len--;
....@@ -1195,7 +1255,7 @@
11951255 */
11961256
11971257 static void gsm_control_message(struct gsm_mux *gsm, unsigned int command,
1198
- u8 *data, int clen)
1258
+ const u8 *data, int clen)
11991259 {
12001260 u8 buf[1];
12011261 unsigned long flags;
....@@ -1205,8 +1265,8 @@
12051265 struct gsm_dlci *dlci = gsm->dlci[0];
12061266 /* Modem wishes to close down */
12071267 if (dlci) {
1208
- dlci->dead = 1;
1209
- gsm->dead = 1;
1268
+ dlci->dead = true;
1269
+ gsm->dead = true;
12101270 gsm_dlci_begin_close(dlci);
12111271 }
12121272 }
....@@ -1217,7 +1277,7 @@
12171277 break;
12181278 case CMD_FCON:
12191279 /* Modem can accept data again */
1220
- gsm->constipated = 0;
1280
+ gsm->constipated = false;
12211281 gsm_control_reply(gsm, CMD_FCON, NULL, 0);
12221282 /* Kick the link in case it is idling */
12231283 spin_lock_irqsave(&gsm->tx_lock, flags);
....@@ -1226,7 +1286,7 @@
12261286 break;
12271287 case CMD_FCOFF:
12281288 /* Modem wants us to STFU */
1229
- gsm->constipated = 1;
1289
+ gsm->constipated = true;
12301290 gsm_control_reply(gsm, CMD_FCOFF, NULL, 0);
12311291 break;
12321292 case CMD_MSC:
....@@ -1267,7 +1327,7 @@
12671327 */
12681328
12691329 static void gsm_control_response(struct gsm_mux *gsm, unsigned int command,
1270
- u8 *data, int clen)
1330
+ const u8 *data, int clen)
12711331 {
12721332 struct gsm_control *ctrl;
12731333 unsigned long flags;
....@@ -1300,17 +1360,18 @@
13001360
13011361 static void gsm_control_transmit(struct gsm_mux *gsm, struct gsm_control *ctrl)
13021362 {
1303
- struct gsm_msg *msg = gsm_data_alloc(gsm, 0, ctrl->len + 1, gsm->ftype);
1363
+ struct gsm_msg *msg = gsm_data_alloc(gsm, 0, ctrl->len + 2, gsm->ftype);
13041364 if (msg == NULL)
13051365 return;
1306
- msg->data[0] = (ctrl->cmd << 1) | 2 | EA; /* command */
1307
- memcpy(msg->data + 1, ctrl->data, ctrl->len);
1366
+ msg->data[0] = (ctrl->cmd << 1) | CR | EA; /* command */
1367
+ msg->data[1] = (ctrl->len << 1) | EA;
1368
+ memcpy(msg->data + 2, ctrl->data, ctrl->len);
13081369 gsm_data_queue(gsm->dlci[0], msg);
13091370 }
13101371
13111372 /**
13121373 * gsm_control_retransmit - retransmit a control frame
1313
- * @data: pointer to our gsm object
1374
+ * @t: timer contained in our gsm object
13141375 *
13151376 * Called off the T2 timer expiry in order to retransmit control frames
13161377 * that have been lost in the system somewhere. The control_lock protects
....@@ -1327,8 +1388,7 @@
13271388 spin_lock_irqsave(&gsm->control_lock, flags);
13281389 ctrl = gsm->pending_cmd;
13291390 if (ctrl) {
1330
- gsm->cretries--;
1331
- if (gsm->cretries == 0) {
1391
+ if (gsm->cretries == 0 || !gsm->dlci[0] || gsm->dlci[0]->dead) {
13321392 gsm->pending_cmd = NULL;
13331393 ctrl->error = -ETIMEDOUT;
13341394 ctrl->done = 1;
....@@ -1336,6 +1396,7 @@
13361396 wake_up(&gsm->event);
13371397 return;
13381398 }
1399
+ gsm->cretries--;
13391400 gsm_control_transmit(gsm, ctrl);
13401401 mod_timer(&gsm->t2_timer, jiffies + gsm->t2 * HZ / 100);
13411402 }
....@@ -1347,7 +1408,7 @@
13471408 * @gsm: the GSM channel
13481409 * @command: command to send including CR bit
13491410 * @data: bytes of data (must be kmalloced)
1350
- * @len: length of the block to send
1411
+ * @clen: length of the block to send
13511412 *
13521413 * Queue and dispatch a control command. Only one command can be
13531414 * active at a time. In theory more can be outstanding but the matching
....@@ -1358,7 +1419,7 @@
13581419 unsigned int command, u8 *data, int clen)
13591420 {
13601421 struct gsm_control *ctrl = kzalloc(sizeof(struct gsm_control),
1361
- GFP_KERNEL);
1422
+ GFP_ATOMIC);
13621423 unsigned long flags;
13631424 if (ctrl == NULL)
13641425 return NULL;
....@@ -1376,7 +1437,7 @@
13761437
13771438 /* If DLCI0 is in ADM mode skip retries, it won't respond */
13781439 if (gsm->dlci[0]->mode == DLCI_MODE_ADM)
1379
- gsm->cretries = 1;
1440
+ gsm->cretries = 0;
13801441 else
13811442 gsm->cretries = gsm->n2;
13821443
....@@ -1424,15 +1485,24 @@
14241485
14251486 static void gsm_dlci_close(struct gsm_dlci *dlci)
14261487 {
1488
+ unsigned long flags;
1489
+
14271490 del_timer(&dlci->t1);
14281491 if (debug & 8)
14291492 pr_debug("DLCI %d goes closed.\n", dlci->addr);
14301493 dlci->state = DLCI_CLOSED;
1494
+ /* Prevent us from sending data before the link is up again */
1495
+ dlci->constipated = true;
14311496 if (dlci->addr != 0) {
14321497 tty_port_tty_hangup(&dlci->port, false);
1433
- kfifo_reset(dlci->fifo);
1498
+ spin_lock_irqsave(&dlci->lock, flags);
1499
+ kfifo_reset(&dlci->fifo);
1500
+ spin_unlock_irqrestore(&dlci->lock, flags);
1501
+ /* Ensure that gsmtty_open() can return. */
1502
+ tty_port_set_initialized(&dlci->port, 0);
1503
+ wake_up_interruptible(&dlci->port.open_wait);
14341504 } else
1435
- dlci->gsm->dead = 1;
1505
+ dlci->gsm->dead = true;
14361506 wake_up(&dlci->gsm->event);
14371507 /* A DLCI 0 close is a MUX termination so we need to kick that
14381508 back to userspace somehow */
....@@ -1452,6 +1522,7 @@
14521522 del_timer(&dlci->t1);
14531523 /* This will let a tty open continue */
14541524 dlci->state = DLCI_OPEN;
1525
+ dlci->constipated = false;
14551526 if (debug & 8)
14561527 pr_debug("DLCI %d goes open.\n", dlci->addr);
14571528 wake_up(&dlci->gsm->event);
....@@ -1459,7 +1530,7 @@
14591530
14601531 /**
14611532 * gsm_dlci_t1 - T1 timer expiry
1462
- * @dlci: DLCI that opened
1533
+ * @t: timer contained in the DLCI that opened
14631534 *
14641535 * The T1 timer handles retransmits of control frames (essentially of
14651536 * SABM and DISC). We resend the command until the retry count runs out
....@@ -1479,8 +1550,8 @@
14791550
14801551 switch (dlci->state) {
14811552 case DLCI_OPENING:
1482
- dlci->retries--;
14831553 if (dlci->retries) {
1554
+ dlci->retries--;
14841555 gsm_command(dlci->gsm, dlci->addr, SABM|PF);
14851556 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
14861557 } else if (!dlci->addr && gsm->control == (DM | PF)) {
....@@ -1495,12 +1566,15 @@
14951566
14961567 break;
14971568 case DLCI_CLOSING:
1498
- dlci->retries--;
14991569 if (dlci->retries) {
1570
+ dlci->retries--;
15001571 gsm_command(dlci->gsm, dlci->addr, DISC|PF);
15011572 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
15021573 } else
15031574 gsm_dlci_close(dlci);
1575
+ break;
1576
+ default:
1577
+ pr_debug("%s: unhandled state: %d\n", __func__, dlci->state);
15041578 break;
15051579 }
15061580 }
....@@ -1524,6 +1598,25 @@
15241598 dlci->state = DLCI_OPENING;
15251599 gsm_command(dlci->gsm, dlci->addr, SABM|PF);
15261600 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
1601
+}
1602
+
1603
+/**
1604
+ * gsm_dlci_set_opening - change state to opening
1605
+ * @dlci: DLCI to open
1606
+ *
1607
+ * Change internal state to wait for DLCI open from initiator side.
1608
+ * We set off timers and responses upon reception of an SABM.
1609
+ */
1610
+static void gsm_dlci_set_opening(struct gsm_dlci *dlci)
1611
+{
1612
+ switch (dlci->state) {
1613
+ case DLCI_CLOSED:
1614
+ case DLCI_CLOSING:
1615
+ dlci->state = DLCI_OPENING;
1616
+ break;
1617
+ default:
1618
+ break;
1619
+ }
15271620 }
15281621
15291622 /**
....@@ -1552,14 +1645,14 @@
15521645 * gsm_dlci_data - data arrived
15531646 * @dlci: channel
15541647 * @data: block of bytes received
1555
- * @len: length of received block
1648
+ * @clen: length of received block
15561649 *
15571650 * A UI or UIH frame has arrived which contains data for a channel
15581651 * other than the control channel. If the relevant virtual tty is
15591652 * open we shovel the bits down it, if not we drop them.
15601653 */
15611654
1562
-static void gsm_dlci_data(struct gsm_dlci *dlci, u8 *data, int clen)
1655
+static void gsm_dlci_data(struct gsm_dlci *dlci, const u8 *data, int clen)
15631656 {
15641657 /* krefs .. */
15651658 struct tty_port *port = &dlci->port;
....@@ -1571,14 +1664,11 @@
15711664 pr_debug("%d bytes for tty\n", len);
15721665 switch (dlci->adaption) {
15731666 /* Unsupported types */
1574
- /* Packetised interruptible data */
1575
- case 4:
1667
+ case 4: /* Packetised interruptible data */
15761668 break;
1577
- /* Packetised uininterruptible voice/data */
1578
- case 3:
1669
+ case 3: /* Packetised uininterruptible voice/data */
15791670 break;
1580
- /* Asynchronous serial with line state in each frame */
1581
- case 2:
1671
+ case 2: /* Asynchronous serial with line state in each frame */
15821672 while (gsm_read_ea(&modem, *data++) == 0) {
15831673 len--;
15841674 if (len == 0)
....@@ -1589,8 +1679,8 @@
15891679 gsm_process_modem(tty, dlci, modem, clen);
15901680 tty_kref_put(tty);
15911681 }
1592
- /* Line state will go via DLCI 0 controls only */
1593
- case 1:
1682
+ fallthrough;
1683
+ case 1: /* Line state will go via DLCI 0 controls only */
15941684 default:
15951685 tty_insert_flip_string(port, data, len);
15961686 tty_flip_buffer_push(port);
....@@ -1609,7 +1699,7 @@
16091699 * and we divide up the work accordingly.
16101700 */
16111701
1612
-static void gsm_dlci_command(struct gsm_dlci *dlci, u8 *data, int len)
1702
+static void gsm_dlci_command(struct gsm_dlci *dlci, const u8 *data, int len)
16131703 {
16141704 /* See what command is involved */
16151705 unsigned int command = 0;
....@@ -1654,8 +1744,7 @@
16541744 return NULL;
16551745 spin_lock_init(&dlci->lock);
16561746 mutex_init(&dlci->mutex);
1657
- dlci->fifo = &dlci->_fifo;
1658
- if (kfifo_alloc(&dlci->_fifo, 4096, GFP_KERNEL) < 0) {
1747
+ if (kfifo_alloc(&dlci->fifo, 4096, GFP_KERNEL) < 0) {
16591748 kfree(dlci);
16601749 return NULL;
16611750 }
....@@ -1668,17 +1757,20 @@
16681757 dlci->addr = addr;
16691758 dlci->adaption = gsm->adaption;
16701759 dlci->state = DLCI_CLOSED;
1671
- if (addr)
1760
+ if (addr) {
16721761 dlci->data = gsm_dlci_data;
1673
- else
1762
+ /* Prevent us from sending data before the link is up */
1763
+ dlci->constipated = true;
1764
+ } else {
16741765 dlci->data = gsm_dlci_command;
1766
+ }
16751767 gsm->dlci[addr] = dlci;
16761768 return dlci;
16771769 }
16781770
16791771 /**
16801772 * gsm_dlci_free - free DLCI
1681
- * @dlci: DLCI to free
1773
+ * @port: tty port for DLCI to free
16821774 *
16831775 * Free up a DLCI.
16841776 *
....@@ -1690,7 +1782,7 @@
16901782
16911783 del_timer_sync(&dlci->t1);
16921784 dlci->gsm->dlci[dlci->addr] = NULL;
1693
- kfifo_free(dlci->fifo);
1785
+ kfifo_free(&dlci->fifo);
16941786 while ((dlci->skb = skb_dequeue(&dlci->skb_list)))
16951787 dev_kfree_skb(dlci->skb);
16961788 kfree(dlci);
....@@ -1725,6 +1817,11 @@
17251817 gsm_destroy_network(dlci);
17261818 mutex_unlock(&dlci->mutex);
17271819
1820
+ /* We cannot use tty_hangup() because in tty_kref_put() the tty
1821
+ * driver assumes that the hangup queue is free and reuses it to
1822
+ * queue release_one_tty() -> NULL pointer panic in
1823
+ * process_one_work().
1824
+ */
17281825 tty_vhangup(tty);
17291826
17301827 tty_port_tty_set(&dlci->port, NULL);
....@@ -1808,7 +1905,6 @@
18081905 gsm_response(gsm, address, UA);
18091906 gsm_dlci_close(dlci);
18101907 break;
1811
- case UA:
18121908 case UA|PF:
18131909 if (cr == 0 || dlci == NULL)
18141910 break;
....@@ -1818,6 +1914,10 @@
18181914 break;
18191915 case DLCI_OPENING:
18201916 gsm_dlci_open(dlci);
1917
+ break;
1918
+ default:
1919
+ pr_debug("%s: unhandled state: %d\n", __func__,
1920
+ dlci->state);
18211921 break;
18221922 }
18231923 break;
....@@ -1838,7 +1938,7 @@
18381938 goto invalid;
18391939 #endif
18401940 if (dlci == NULL || dlci->state != DLCI_OPEN) {
1841
- gsm_command(gsm, address, DM|PF);
1941
+ gsm_response(gsm, address, DM|PF);
18421942 return;
18431943 }
18441944 dlci->data(dlci, gsm->buf, gsm->len);
....@@ -1932,6 +2032,9 @@
19322032 break;
19332033 }
19342034 break;
2035
+ default:
2036
+ pr_debug("%s: unhandled state: %d\n", __func__, gsm->state);
2037
+ break;
19352038 }
19362039 }
19372040
....@@ -1945,6 +2048,16 @@
19452048
19462049 static void gsm1_receive(struct gsm_mux *gsm, unsigned char c)
19472050 {
2051
+ /* handle XON/XOFF */
2052
+ if ((c & ISO_IEC_646_MASK) == XON) {
2053
+ gsm->constipated = true;
2054
+ return;
2055
+ } else if ((c & ISO_IEC_646_MASK) == XOFF) {
2056
+ gsm->constipated = false;
2057
+ /* Kick the link in case it is idling */
2058
+ gsm_data_kick(gsm, NULL);
2059
+ return;
2060
+ }
19482061 if (c == GSM1_SOF) {
19492062 /* EOF is only valid in frame if we have got to the data state
19502063 and received at least one byte (the FCS) */
....@@ -1959,7 +2072,8 @@
19592072 }
19602073 /* Any partial frame was a runt so go back to start */
19612074 if (gsm->state != GSM_START) {
1962
- gsm->malformed++;
2075
+ if (gsm->state != GSM_SEARCH)
2076
+ gsm->malformed++;
19632077 gsm->state = GSM_START;
19642078 }
19652079 /* A SOF in GSM_START means we are still reading idling or
....@@ -1968,7 +2082,7 @@
19682082 }
19692083
19702084 if (c == GSM1_ESCAPE) {
1971
- gsm->escape = 1;
2085
+ gsm->escape = true;
19722086 return;
19732087 }
19742088
....@@ -1978,14 +2092,14 @@
19782092
19792093 if (gsm->escape) {
19802094 c ^= GSM1_ESCAPE_BITS;
1981
- gsm->escape = 0;
2095
+ gsm->escape = false;
19822096 }
19832097 switch (gsm->state) {
19842098 case GSM_START: /* First byte after SOF */
19852099 gsm->address = 0;
19862100 gsm->state = GSM_ADDRESS;
19872101 gsm->fcs = INIT_FCS;
1988
- /* Drop through */
2102
+ fallthrough;
19892103 case GSM_ADDRESS: /* Address continuation */
19902104 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
19912105 if (gsm_read_ea(&gsm->address, c))
....@@ -2005,6 +2119,9 @@
20052119 gsm->buf[gsm->count++] = c;
20062120 break;
20072121 case GSM_OVERRUN: /* Over-long - eg a dropped SOF */
2122
+ break;
2123
+ default:
2124
+ pr_debug("%s: unhandled state: %d\n", __func__, gsm->state);
20082125 break;
20092126 }
20102127 }
....@@ -2028,74 +2145,43 @@
20282145 gsm->io_error++;
20292146 }
20302147
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
-
20582148 /**
20592149 * gsm_cleanup_mux - generic GSM protocol cleanup
20602150 * @gsm: our mux
2151
+ * @disc: disconnect link?
20612152 *
20622153 * Clean up the bits of the mux which are the same for all framing
20632154 * protocols. Remove the mux from the mux table, stop all the timers
20642155 * and then shut down each device hanging up the channels as we go.
20652156 */
20662157
2067
-static void gsm_cleanup_mux(struct gsm_mux *gsm)
2158
+static void gsm_cleanup_mux(struct gsm_mux *gsm, bool disc)
20682159 {
20692160 int i;
20702161 struct gsm_dlci *dlci = gsm->dlci[0];
20712162 struct gsm_msg *txq, *ntxq;
20722163
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 */
2164
+ gsm->dead = true;
20932165 mutex_lock(&gsm->mutex);
2094
- for (i = 0; i < NUM_DLCI; i++)
2166
+
2167
+ if (dlci) {
2168
+ if (disc && dlci->state != DLCI_CLOSED) {
2169
+ gsm_dlci_begin_close(dlci);
2170
+ wait_event(gsm->event, dlci->state == DLCI_CLOSED);
2171
+ }
2172
+ dlci->dead = true;
2173
+ }
2174
+
2175
+ /* Finish outstanding timers, making sure they are done */
2176
+ del_timer_sync(&gsm->t2_timer);
2177
+
2178
+ /* Free up any link layer users and finally the control channel */
2179
+ for (i = NUM_DLCI - 1; i >= 0; i--)
20952180 if (gsm->dlci[i])
20962181 gsm_dlci_release(gsm->dlci[i]);
20972182 mutex_unlock(&gsm->mutex);
20982183 /* Now wipe the queues */
2184
+ tty_ldisc_flush(gsm->tty);
20992185 list_for_each_entry_safe(txq, ntxq, &gsm->tx_list, list)
21002186 kfree(txq);
21012187 INIT_LIST_HEAD(&gsm->tx_list);
....@@ -2113,46 +2199,36 @@
21132199 static int gsm_activate_mux(struct gsm_mux *gsm)
21142200 {
21152201 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);
21222202
21232203 if (gsm->encoding == 0)
21242204 gsm->receive = gsm0_receive;
21252205 else
21262206 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;
21402207
21412208 dlci = gsm_dlci_alloc(gsm, 0);
21422209 if (dlci == NULL)
21432210 return -ENOMEM;
2144
- gsm->dead = 0; /* Tty opens are now permissible */
2211
+ gsm->dead = false; /* Tty opens are now permissible */
21452212 return 0;
21462213 }
21472214
21482215 /**
21492216 * gsm_free_mux - free up a mux
2150
- * @mux: mux to free
2217
+ * @gsm: mux to free
21512218 *
21522219 * Dispose of allocated resources for a dead mux
21532220 */
21542221 static void gsm_free_mux(struct gsm_mux *gsm)
21552222 {
2223
+ int i;
2224
+
2225
+ for (i = 0; i < MAX_MUX; i++) {
2226
+ if (gsm == gsm_mux[i]) {
2227
+ gsm_mux[i] = NULL;
2228
+ break;
2229
+ }
2230
+ }
2231
+ mutex_destroy(&gsm->mutex);
21562232 kfree(gsm->txframe);
21572233 kfree(gsm->buf);
21582234 kfree(gsm);
....@@ -2160,7 +2236,7 @@
21602236
21612237 /**
21622238 * gsm_free_muxr - free up a mux
2163
- * @mux: mux to free
2239
+ * @ref: kreference to the mux to free
21642240 *
21652241 * Dispose of allocated resources for a dead mux
21662242 */
....@@ -2172,12 +2248,30 @@
21722248
21732249 static inline void mux_get(struct gsm_mux *gsm)
21742250 {
2251
+ unsigned long flags;
2252
+
2253
+ spin_lock_irqsave(&gsm_mux_lock, flags);
21752254 kref_get(&gsm->ref);
2255
+ spin_unlock_irqrestore(&gsm_mux_lock, flags);
21762256 }
21772257
21782258 static inline void mux_put(struct gsm_mux *gsm)
21792259 {
2260
+ unsigned long flags;
2261
+
2262
+ spin_lock_irqsave(&gsm_mux_lock, flags);
21802263 kref_put(&gsm->ref, gsm_free_muxr);
2264
+ spin_unlock_irqrestore(&gsm_mux_lock, flags);
2265
+}
2266
+
2267
+static inline unsigned int mux_num_to_base(struct gsm_mux *gsm)
2268
+{
2269
+ return gsm->num * NUM_DLCI;
2270
+}
2271
+
2272
+static inline unsigned int mux_line_to_num(unsigned int line)
2273
+{
2274
+ return line / NUM_DLCI;
21812275 }
21822276
21832277 /**
....@@ -2188,6 +2282,7 @@
21882282
21892283 static struct gsm_mux *gsm_alloc_mux(void)
21902284 {
2285
+ int i;
21912286 struct gsm_mux *gsm = kzalloc(sizeof(struct gsm_mux), GFP_KERNEL);
21922287 if (gsm == NULL)
21932288 return NULL;
....@@ -2196,7 +2291,7 @@
21962291 kfree(gsm);
21972292 return NULL;
21982293 }
2199
- gsm->txframe = kmalloc(2 * MAX_MRU + 2, GFP_KERNEL);
2294
+ gsm->txframe = kmalloc(2 * (MAX_MTU + PROT_OVERHEAD - 1), GFP_KERNEL);
22002295 if (gsm->txframe == NULL) {
22012296 kfree(gsm->buf);
22022297 kfree(gsm);
....@@ -2206,6 +2301,10 @@
22062301 mutex_init(&gsm->mutex);
22072302 kref_init(&gsm->ref);
22082303 INIT_LIST_HEAD(&gsm->tx_list);
2304
+ timer_setup(&gsm->t2_timer, gsm_control_retransmit, 0);
2305
+ init_waitqueue_head(&gsm->event);
2306
+ spin_lock_init(&gsm->control_lock);
2307
+ spin_lock_init(&gsm->tx_lock);
22092308
22102309 gsm->t1 = T1;
22112310 gsm->t2 = T2;
....@@ -2215,9 +2314,130 @@
22152314 gsm->encoding = 1;
22162315 gsm->mru = 64; /* Default to encoding 1 so these should be 64 */
22172316 gsm->mtu = 64;
2218
- gsm->dead = 1; /* Avoid early tty opens */
2317
+ gsm->dead = true; /* Avoid early tty opens */
2318
+
2319
+ /* Store the instance to the mux array or abort if no space is
2320
+ * available.
2321
+ */
2322
+ spin_lock(&gsm_mux_lock);
2323
+ for (i = 0; i < MAX_MUX; i++) {
2324
+ if (!gsm_mux[i]) {
2325
+ gsm_mux[i] = gsm;
2326
+ gsm->num = i;
2327
+ break;
2328
+ }
2329
+ }
2330
+ spin_unlock(&gsm_mux_lock);
2331
+ if (i == MAX_MUX) {
2332
+ mutex_destroy(&gsm->mutex);
2333
+ kfree(gsm->txframe);
2334
+ kfree(gsm->buf);
2335
+ kfree(gsm);
2336
+ return NULL;
2337
+ }
22192338
22202339 return gsm;
2340
+}
2341
+
2342
+static void gsm_copy_config_values(struct gsm_mux *gsm,
2343
+ struct gsm_config *c)
2344
+{
2345
+ memset(c, 0, sizeof(*c));
2346
+ c->adaption = gsm->adaption;
2347
+ c->encapsulation = gsm->encoding;
2348
+ c->initiator = gsm->initiator;
2349
+ c->t1 = gsm->t1;
2350
+ c->t2 = gsm->t2;
2351
+ c->t3 = 0; /* Not supported */
2352
+ c->n2 = gsm->n2;
2353
+ if (gsm->ftype == UIH)
2354
+ c->i = 1;
2355
+ else
2356
+ c->i = 2;
2357
+ pr_debug("Ftype %d i %d\n", gsm->ftype, c->i);
2358
+ c->mru = gsm->mru;
2359
+ c->mtu = gsm->mtu;
2360
+ c->k = 0;
2361
+}
2362
+
2363
+static int gsm_config(struct gsm_mux *gsm, struct gsm_config *c)
2364
+{
2365
+ int ret = 0;
2366
+ int need_close = 0;
2367
+ int need_restart = 0;
2368
+
2369
+ /* Stuff we don't support yet - UI or I frame transport, windowing */
2370
+ if ((c->adaption != 1 && c->adaption != 2) || c->k)
2371
+ return -EOPNOTSUPP;
2372
+ /* Check the MRU/MTU range looks sane */
2373
+ if (c->mru > MAX_MRU || c->mtu > MAX_MTU || c->mru < 8 || c->mtu < 8)
2374
+ return -EINVAL;
2375
+ if (c->n2 > 255)
2376
+ return -EINVAL;
2377
+ if (c->encapsulation > 1) /* Basic, advanced, no I */
2378
+ return -EINVAL;
2379
+ if (c->initiator > 1)
2380
+ return -EINVAL;
2381
+ if (c->i == 0 || c->i > 2) /* UIH and UI only */
2382
+ return -EINVAL;
2383
+ /*
2384
+ * See what is needed for reconfiguration
2385
+ */
2386
+
2387
+ /* Timing fields */
2388
+ if (c->t1 != 0 && c->t1 != gsm->t1)
2389
+ need_restart = 1;
2390
+ if (c->t2 != 0 && c->t2 != gsm->t2)
2391
+ need_restart = 1;
2392
+ if (c->encapsulation != gsm->encoding)
2393
+ need_restart = 1;
2394
+ if (c->adaption != gsm->adaption)
2395
+ need_restart = 1;
2396
+ /* Requires care */
2397
+ if (c->initiator != gsm->initiator)
2398
+ need_close = 1;
2399
+ if (c->mru != gsm->mru)
2400
+ need_restart = 1;
2401
+ if (c->mtu != gsm->mtu)
2402
+ need_restart = 1;
2403
+
2404
+ /*
2405
+ * Close down what is needed, restart and initiate the new
2406
+ * configuration. On the first time there is no DLCI[0]
2407
+ * and closing or cleaning up is not necessary.
2408
+ */
2409
+ if (need_close || need_restart)
2410
+ gsm_cleanup_mux(gsm, true);
2411
+
2412
+ gsm->initiator = c->initiator;
2413
+ gsm->mru = c->mru;
2414
+ gsm->mtu = c->mtu;
2415
+ gsm->encoding = c->encapsulation;
2416
+ gsm->adaption = c->adaption;
2417
+ gsm->n2 = c->n2;
2418
+
2419
+ if (c->i == 1)
2420
+ gsm->ftype = UIH;
2421
+ else if (c->i == 2)
2422
+ gsm->ftype = UI;
2423
+
2424
+ if (c->t1)
2425
+ gsm->t1 = c->t1;
2426
+ if (c->t2)
2427
+ gsm->t2 = c->t2;
2428
+
2429
+ /*
2430
+ * FIXME: We need to separate activation/deactivation from adding
2431
+ * and removing from the mux array
2432
+ */
2433
+ if (gsm->dead) {
2434
+ ret = gsm_activate_mux(gsm);
2435
+ if (ret)
2436
+ return ret;
2437
+ if (gsm->initiator)
2438
+ gsm_dlci_begin_open(gsm->dlci[0]);
2439
+ }
2440
+ return 0;
22212441 }
22222442
22232443 /**
....@@ -2255,19 +2475,32 @@
22552475
22562476 static int gsmld_attach_gsm(struct tty_struct *tty, struct gsm_mux *gsm)
22572477 {
2258
- int ret, i, base;
2478
+ unsigned int base;
2479
+ int ret, i;
22592480
22602481 gsm->tty = tty_kref_get(tty);
2261
- gsm->output = gsmld_output;
2482
+ /* Turn off tty XON/XOFF handling to handle it explicitly. */
2483
+ gsm->old_c_iflag = tty->termios.c_iflag;
2484
+ tty->termios.c_iflag &= (IXON | IXOFF);
22622485 ret = gsm_activate_mux(gsm);
22632486 if (ret != 0)
22642487 tty_kref_put(gsm->tty);
22652488 else {
22662489 /* Don't register device 0 - this is the control channel and not
22672490 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);
2491
+ base = mux_num_to_base(gsm); /* Base for this MUX */
2492
+ for (i = 1; i < NUM_DLCI; i++) {
2493
+ struct device *dev;
2494
+
2495
+ dev = tty_register_device(gsm_tty_driver,
2496
+ base + i, NULL);
2497
+ if (IS_ERR(dev)) {
2498
+ for (i--; i >= 1; i--)
2499
+ tty_unregister_device(gsm_tty_driver,
2500
+ base + i);
2501
+ return PTR_ERR(dev);
2502
+ }
2503
+ }
22712504 }
22722505 return ret;
22732506 }
....@@ -2283,13 +2516,14 @@
22832516
22842517 static void gsmld_detach_gsm(struct tty_struct *tty, struct gsm_mux *gsm)
22852518 {
2519
+ unsigned int base = mux_num_to_base(gsm); /* Base for this MUX */
22862520 int i;
2287
- int base = gsm->num << 6; /* Base for this MUX */
22882521
22892522 WARN_ON(tty != gsm->tty);
22902523 for (i = 1; i < NUM_DLCI; i++)
22912524 tty_unregister_device(gsm_tty_driver, base + i);
2292
- gsm_cleanup_mux(gsm);
2525
+ /* Restore tty XON/XOFF handling. */
2526
+ gsm->tty->termios.c_iflag = gsm->old_c_iflag;
22932527 tty_kref_put(gsm->tty);
22942528 gsm->tty = NULL;
22952529 }
....@@ -2318,7 +2552,7 @@
23182552 case TTY_BREAK:
23192553 case TTY_PARITY:
23202554 case TTY_FRAME:
2321
- gsm->error(gsm, *dp, flags);
2555
+ gsm_error(gsm, *dp, flags);
23222556 break;
23232557 default:
23242558 WARN_ONCE(1, "%s: unknown flag %d\n",
....@@ -2356,6 +2590,12 @@
23562590 static void gsmld_close(struct tty_struct *tty)
23572591 {
23582592 struct gsm_mux *gsm = tty->disc_data;
2593
+
2594
+ /* The ldisc locks and closes the port before calling our close. This
2595
+ * means we have no way to do a proper disconnect. We will not bother
2596
+ * to do one.
2597
+ */
2598
+ gsm_cleanup_mux(gsm, false);
23592599
23602600 gsmld_detach_gsm(tty, gsm);
23612601
....@@ -2395,7 +2635,7 @@
23952635
23962636 ret = gsmld_attach_gsm(tty, gsm);
23972637 if (ret != 0) {
2398
- gsm_cleanup_mux(gsm);
2638
+ gsm_cleanup_mux(gsm, false);
23992639 mux_put(gsm);
24002640 }
24012641 return ret;
....@@ -2441,7 +2681,8 @@
24412681 */
24422682
24432683 static ssize_t gsmld_read(struct tty_struct *tty, struct file *file,
2444
- unsigned char __user *buf, size_t nr)
2684
+ unsigned char *buf, size_t nr,
2685
+ void **cookie, unsigned long offset)
24452686 {
24462687 return -EOPNOTSUPP;
24472688 }
....@@ -2463,11 +2704,24 @@
24632704 static ssize_t gsmld_write(struct tty_struct *tty, struct file *file,
24642705 const unsigned char *buf, size_t nr)
24652706 {
2466
- int space = tty_write_room(tty);
2707
+ struct gsm_mux *gsm = tty->disc_data;
2708
+ unsigned long flags;
2709
+ int space;
2710
+ int ret;
2711
+
2712
+ if (!gsm)
2713
+ return -ENODEV;
2714
+
2715
+ ret = -ENOBUFS;
2716
+ spin_lock_irqsave(&gsm->tx_lock, flags);
2717
+ space = tty_write_room(tty);
24672718 if (space >= nr)
2468
- return tty->ops->write(tty, buf, nr);
2469
- set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
2470
- return -ENOBUFS;
2719
+ ret = tty->ops->write(tty, buf, nr);
2720
+ else
2721
+ set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
2722
+ spin_unlock_irqrestore(&gsm->tx_lock, flags);
2723
+
2724
+ return ret;
24712725 }
24722726
24732727 /**
....@@ -2492,96 +2746,16 @@
24922746
24932747 poll_wait(file, &tty->read_wait, wait);
24942748 poll_wait(file, &tty->write_wait, wait);
2749
+
2750
+ if (gsm->dead)
2751
+ mask |= EPOLLHUP;
24952752 if (tty_hung_up_p(file))
2753
+ mask |= EPOLLHUP;
2754
+ if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
24962755 mask |= EPOLLHUP;
24972756 if (!tty_is_writelocked(tty) && tty_write_room(tty) > 0)
24982757 mask |= EPOLLOUT | EPOLLWRNORM;
2499
- if (gsm->dead)
2500
- mask |= EPOLLHUP;
25012758 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;
25852759 }
25862760
25872761 static int gsmld_ioctl(struct tty_struct *tty, struct file *file,
....@@ -2589,44 +2763,25 @@
25892763 {
25902764 struct gsm_config c;
25912765 struct gsm_mux *gsm = tty->disc_data;
2766
+ unsigned int base;
25922767
25932768 switch (cmd) {
25942769 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)))
2770
+ gsm_copy_config_values(gsm, &c);
2771
+ if (copy_to_user((void __user *)arg, &c, sizeof(c)))
26122772 return -EFAULT;
26132773 return 0;
26142774 case GSMIOC_SETCONF:
2615
- if (copy_from_user(&c, (void *)arg, sizeof(c)))
2775
+ if (copy_from_user(&c, (void __user *)arg, sizeof(c)))
26162776 return -EFAULT;
2617
- return gsmld_config(tty, gsm, &c);
2777
+ return gsm_config(gsm, &c);
2778
+ case GSMIOC_GETFIRST:
2779
+ base = mux_num_to_base(gsm);
2780
+ return put_user(base + 1, (__u32 __user *)arg);
26182781 default:
26192782 return n_tty_ioctl_helper(tty, file, cmd, arg);
26202783 }
26212784 }
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
26302785
26312786 /*
26322787 * Network interface
....@@ -2699,7 +2854,7 @@
26992854 }
27002855
27012856 /* called when a packet did not ack after watchdogtimeout */
2702
-static void gsm_mux_net_tx_timeout(struct net_device *net)
2857
+static void gsm_mux_net_tx_timeout(struct net_device *net, unsigned int txqueue)
27032858 {
27042859 /* Tell syslog we are hosed. */
27052860 dev_dbg(&net->dev, "Tx timed out.\n");
....@@ -2709,7 +2864,7 @@
27092864 }
27102865
27112866 static void gsm_mux_rx_netchar(struct gsm_dlci *dlci,
2712
- unsigned char *in_buf, int size)
2867
+ const unsigned char *in_buf, int size)
27132868 {
27142869 struct net_device *net = dlci->net;
27152870 struct sk_buff *skb;
....@@ -2764,7 +2919,7 @@
27642919 {
27652920 struct gsm_mux_net *mux_net;
27662921
2767
- pr_debug("destroy network interface");
2922
+ pr_debug("destroy network interface\n");
27682923 if (!dlci->net)
27692924 return;
27702925 mux_net = netdev_priv(dlci->net);
....@@ -2793,7 +2948,7 @@
27932948 if (nc->adaption != 3 && nc->adaption != 4)
27942949 return -EPROTONOSUPPORT;
27952950
2796
- pr_debug("create network interface");
2951
+ pr_debug("create network interface\n");
27972952
27982953 netname = "gsm%d";
27992954 if (nc->if_name[0] != '\0')
....@@ -2801,7 +2956,7 @@
28012956 net = alloc_netdev(sizeof(struct gsm_mux_net), netname,
28022957 NET_NAME_UNKNOWN, gsm_mux_net_init);
28032958 if (!net) {
2804
- pr_err("alloc_netdev failed");
2959
+ pr_err("alloc_netdev failed\n");
28052960 return -ENOMEM;
28062961 }
28072962 net->mtu = dlci->gsm->mtu;
....@@ -2819,7 +2974,7 @@
28192974 dlci->data = gsm_mux_rx_netchar;
28202975 dlci->net = net;
28212976
2822
- pr_debug("register netdev");
2977
+ pr_debug("register netdev\n");
28232978 retval = register_netdev(net);
28242979 if (retval) {
28252980 pr_err("network register fail %d\n", retval);
....@@ -2839,9 +2994,6 @@
28392994 .flush_buffer = gsmld_flush_buffer,
28402995 .read = gsmld_read,
28412996 .write = gsmld_write,
2842
-#ifdef CONFIG_COMPAT
2843
- .compat_ioctl = gsmld_compat_ioctl,
2844
-#endif
28452997 .ioctl = gsmld_ioctl,
28462998 .poll = gsmld_poll,
28472999 .receive_buf = gsmld_receive_buf,
....@@ -2856,19 +3008,17 @@
28563008
28573009 static int gsmtty_modem_update(struct gsm_dlci *dlci, u8 brk)
28583010 {
2859
- u8 modembits[5];
3011
+ u8 modembits[3];
28603012 struct gsm_control *ctrl;
28613013 int len = 2;
28623014
2863
- if (brk)
3015
+ modembits[0] = (dlci->addr << 2) | 2 | EA; /* DLCI, Valid, EA */
3016
+ modembits[1] = (gsm_encode_modem(dlci) << 1) | EA;
3017
+ if (brk) {
3018
+ modembits[2] = (brk << 4) | 2 | EA; /* Length, Break, EA */
28643019 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);
3020
+ }
3021
+ ctrl = gsm_control_send(dlci->gsm, CMD_MSC, modembits, len);
28723022 if (ctrl == NULL)
28733023 return -ENOMEM;
28743024 return gsm_control_wait(dlci->gsm, ctrl);
....@@ -2921,7 +3071,7 @@
29213071 struct gsm_mux *gsm;
29223072 struct gsm_dlci *dlci;
29233073 unsigned int line = tty->index;
2924
- unsigned int mux = line >> 6;
3074
+ unsigned int mux = mux_line_to_num(line);
29253075 bool alloc = false;
29263076 int ret;
29273077
....@@ -2976,6 +3126,7 @@
29763126 {
29773127 struct gsm_dlci *dlci = tty->driver_data;
29783128 struct tty_port *port = &dlci->port;
3129
+ struct gsm_mux *gsm = dlci->gsm;
29793130
29803131 port->count++;
29813132 tty_port_tty_set(port, tty);
....@@ -2985,7 +3136,10 @@
29853136 a DM straight back. This is ok as that will have caused a hangup */
29863137 tty_port_set_initialized(port, 1);
29873138 /* Start sending off SABM messages */
2988
- gsm_dlci_begin_open(dlci);
3139
+ if (gsm->initiator)
3140
+ gsm_dlci_begin_open(dlci);
3141
+ else
3142
+ gsm_dlci_set_opening(dlci);
29893143 /* And wait for virtual carrier */
29903144 return tty_port_block_til_ready(port, tty, filp);
29913145 }
....@@ -3028,7 +3182,7 @@
30283182 if (dlci->state == DLCI_CLOSED)
30293183 return -EINVAL;
30303184 /* Stuff the bytes into the fifo queue */
3031
- sent = kfifo_in_locked(dlci->fifo, buf, len, &dlci->lock);
3185
+ sent = kfifo_in_locked(&dlci->fifo, buf, len, &dlci->lock);
30323186 /* Need to kick the channel */
30333187 gsm_dlci_data_kick(dlci);
30343188 return sent;
....@@ -3039,7 +3193,7 @@
30393193 struct gsm_dlci *dlci = tty->driver_data;
30403194 if (dlci->state == DLCI_CLOSED)
30413195 return -EINVAL;
3042
- return TX_SIZE - kfifo_len(dlci->fifo);
3196
+ return TX_SIZE - kfifo_len(&dlci->fifo);
30433197 }
30443198
30453199 static int gsmtty_chars_in_buffer(struct tty_struct *tty)
....@@ -3047,19 +3201,23 @@
30473201 struct gsm_dlci *dlci = tty->driver_data;
30483202 if (dlci->state == DLCI_CLOSED)
30493203 return -EINVAL;
3050
- return kfifo_len(dlci->fifo);
3204
+ return kfifo_len(&dlci->fifo);
30513205 }
30523206
30533207 static void gsmtty_flush_buffer(struct tty_struct *tty)
30543208 {
30553209 struct gsm_dlci *dlci = tty->driver_data;
3210
+ unsigned long flags;
3211
+
30563212 if (dlci->state == DLCI_CLOSED)
30573213 return;
30583214 /* Caution needed: If we implement reliable transport classes
30593215 then the data being transmitted can't simply be junked once
30603216 it has first hit the stack. Until then we can just blow it
30613217 away */
3062
- kfifo_reset(dlci->fifo);
3218
+ spin_lock_irqsave(&dlci->lock, flags);
3219
+ kfifo_reset(&dlci->fifo);
3220
+ spin_unlock_irqrestore(&dlci->lock, flags);
30633221 /* Need to unhook this DLCI from the transmit queue logic */
30643222 }
30653223
....@@ -3149,9 +3307,9 @@
31493307 if (dlci->state == DLCI_CLOSED)
31503308 return;
31513309 if (C_CRTSCTS(tty))
3152
- dlci->modem_tx &= ~TIOCM_DTR;
3153
- dlci->throttled = 1;
3154
- /* Send an MSC with DTR cleared */
3310
+ dlci->modem_tx &= ~TIOCM_RTS;
3311
+ dlci->throttled = true;
3312
+ /* Send an MSC with RTS cleared */
31553313 gsmtty_modem_update(dlci, 0);
31563314 }
31573315
....@@ -3161,9 +3319,9 @@
31613319 if (dlci->state == DLCI_CLOSED)
31623320 return;
31633321 if (C_CRTSCTS(tty))
3164
- dlci->modem_tx |= TIOCM_DTR;
3165
- dlci->throttled = 0;
3166
- /* Send an MSC with DTR set */
3322
+ dlci->modem_tx |= TIOCM_RTS;
3323
+ dlci->throttled = false;
3324
+ /* Send an MSC with RTS set */
31673325 gsmtty_modem_update(dlci, 0);
31683326 }
31693327