.. | .. |
---|
72 | 72 | */ |
---|
73 | 73 | #define MAX_MRU 1500 |
---|
74 | 74 | #define MAX_MTU 1500 |
---|
| 75 | +/* SOF, ADDR, CTRL, LEN1, LEN2, ..., FCS, EOF */ |
---|
| 76 | +#define PROT_OVERHEAD 7 |
---|
75 | 77 | #define GSM_NET_TX_TIMEOUT (HZ*10) |
---|
76 | 78 | |
---|
77 | 79 | /** |
---|
78 | 80 | * struct gsm_mux_net - network interface |
---|
79 | | - * @struct gsm_dlci* dlci |
---|
80 | 81 | * |
---|
81 | 82 | * Created when net interface is initialized. |
---|
82 | | - **/ |
---|
| 83 | + */ |
---|
83 | 84 | struct gsm_mux_net { |
---|
84 | 85 | struct kref ref; |
---|
85 | 86 | struct gsm_dlci *dlci; |
---|
.. | .. |
---|
97 | 98 | u8 ctrl; /* Control byte + flags */ |
---|
98 | 99 | unsigned int len; /* Length of data block (can be zero) */ |
---|
99 | 100 | 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 */ |
---|
101 | 114 | }; |
---|
102 | 115 | |
---|
103 | 116 | /* |
---|
.. | .. |
---|
113 | 126 | struct gsm_dlci { |
---|
114 | 127 | struct gsm_mux *gsm; |
---|
115 | 128 | 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; |
---|
121 | 130 | struct mutex mutex; |
---|
122 | 131 | |
---|
123 | 132 | /* 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; |
---|
127 | 134 | spinlock_t lock; /* Protects the internal state */ |
---|
128 | 135 | struct timer_list t1; /* Retransmit timer for SABM and UA */ |
---|
129 | 136 | int retries; |
---|
130 | 137 | /* Uplink tty if active */ |
---|
131 | 138 | 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 */ |
---|
134 | 140 | int adaption; /* Adaption layer in use */ |
---|
135 | 141 | int prev_adaption; |
---|
136 | 142 | u32 modem_rx; /* Our incoming virtual modem lines */ |
---|
137 | 143 | u32 modem_tx; /* Our outgoing modem lines */ |
---|
138 | | - int dead; /* Refuse re-open */ |
---|
| 144 | + bool dead; /* Refuse re-open */ |
---|
139 | 145 | /* 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 */ |
---|
142 | 148 | /* Packetised I/O */ |
---|
143 | 149 | struct sk_buff *skb; /* Frame being sent */ |
---|
144 | 150 | struct sk_buff_head skb_list; /* Queued frames */ |
---|
145 | 151 | /* 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); |
---|
148 | 154 | struct net_device *net; /* network interface, if created */ |
---|
149 | 155 | }; |
---|
150 | 156 | |
---|
.. | .. |
---|
166 | 172 | int len; /* Length of block for retransmission */ |
---|
167 | 173 | int done; /* Done flag */ |
---|
168 | 174 | 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, |
---|
169 | 189 | }; |
---|
170 | 190 | |
---|
171 | 191 | /* |
---|
.. | .. |
---|
192 | 212 | |
---|
193 | 213 | /* Framing Layer */ |
---|
194 | 214 | 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; |
---|
207 | 216 | unsigned int len; |
---|
208 | 217 | unsigned int address; |
---|
209 | 218 | unsigned int count; |
---|
210 | | - int escape; |
---|
| 219 | + bool escape; |
---|
211 | 220 | int encoding; |
---|
212 | 221 | u8 control; |
---|
213 | 222 | u8 fcs; |
---|
214 | 223 | u8 received_fcs; |
---|
215 | 224 | u8 *txframe; /* TX framing buffer */ |
---|
216 | 225 | |
---|
217 | | - /* Methods for the receiver side */ |
---|
| 226 | + /* Method for the receiver side */ |
---|
218 | 227 | 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); |
---|
222 | 228 | |
---|
223 | 229 | /* Link Layer */ |
---|
224 | 230 | unsigned int mru; |
---|
225 | 231 | unsigned int mtu; |
---|
226 | 232 | int initiator; /* Did we initiate connection */ |
---|
227 | | - int dead; /* Has the mux been shut down */ |
---|
| 233 | + bool dead; /* Has the mux been shut down */ |
---|
228 | 234 | 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 */ |
---|
230 | 237 | |
---|
231 | 238 | spinlock_t tx_lock; |
---|
232 | 239 | unsigned int tx_bytes; /* TX data outstanding */ |
---|
.. | .. |
---|
359 | 366 | #define INIT_FCS 0xFF |
---|
360 | 367 | #define GOOD_FCS 0xCF |
---|
361 | 368 | |
---|
| 369 | +static int gsmld_output(struct gsm_mux *gsm, u8 *data, int len); |
---|
| 370 | + |
---|
362 | 371 | /** |
---|
363 | 372 | * gsm_fcs_add - update FCS |
---|
364 | 373 | * @fcs: Current FCS |
---|
.. | .. |
---|
393 | 402 | /** |
---|
394 | 403 | * gsm_read_ea - read a byte into an EA |
---|
395 | 404 | * @val: variable holding value |
---|
396 | | - * c: byte going into the EA |
---|
| 405 | + * @c: byte going into the EA |
---|
397 | 406 | * |
---|
398 | 407 | * Processes one byte of an EA. Updates the passed variable |
---|
399 | 408 | * and returns 1 if the EA is now completely read |
---|
.. | .. |
---|
406 | 415 | *val |= c >> 1; |
---|
407 | 416 | /* Was this the last byte of the EA 1 = yes*/ |
---|
408 | 417 | 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; |
---|
409 | 439 | } |
---|
410 | 440 | |
---|
411 | 441 | /** |
---|
.. | .. |
---|
497 | 527 | else |
---|
498 | 528 | pr_cont("(F)"); |
---|
499 | 529 | |
---|
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); |
---|
512 | 531 | } |
---|
513 | 532 | |
---|
514 | 533 | |
---|
.. | .. |
---|
518 | 537 | |
---|
519 | 538 | /** |
---|
520 | 539 | * gsm_stuff_packet - bytestuff a packet |
---|
521 | | - * @ibuf: input |
---|
522 | | - * @obuf: output |
---|
| 540 | + * @input: input buffer |
---|
| 541 | + * @output: output buffer |
---|
523 | 542 | * @len: length of input |
---|
524 | 543 | * |
---|
525 | 544 | * Expand a buffer by bytestuffing it. The worst case size change |
---|
.. | .. |
---|
592 | 611 | WARN_ON(1); |
---|
593 | 612 | return; |
---|
594 | 613 | } |
---|
595 | | - gsm->output(gsm, cbuf, len); |
---|
| 614 | + gsmld_output(gsm, cbuf, len); |
---|
596 | 615 | gsm_print_packet("-->", addr, cr, control, NULL, 0); |
---|
597 | 616 | } |
---|
598 | 617 | |
---|
.. | .. |
---|
656 | 675 | } |
---|
657 | 676 | |
---|
658 | 677 | /** |
---|
| 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 | +/** |
---|
659 | 709 | * gsm_data_kick - poke the queue |
---|
660 | 710 | * @gsm: GSM Mux |
---|
661 | 711 | * |
---|
.. | .. |
---|
673 | 723 | int len; |
---|
674 | 724 | |
---|
675 | 725 | 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)) |
---|
677 | 727 | continue; |
---|
678 | 728 | if (gsm->encoding != 0) { |
---|
679 | 729 | gsm->txframe[0] = GSM1_SOF; |
---|
.. | .. |
---|
692 | 742 | print_hex_dump_bytes("gsm_data_kick: ", |
---|
693 | 743 | DUMP_PREFIX_OFFSET, |
---|
694 | 744 | gsm->txframe, len); |
---|
695 | | - if (gsm->output(gsm, gsm->txframe, len) < 0) |
---|
| 745 | + if (gsmld_output(gsm, gsm->txframe, len) < 0) |
---|
696 | 746 | break; |
---|
697 | 747 | /* FIXME: Can eliminate one SOF in many more cases */ |
---|
698 | 748 | gsm->tx_bytes -= msg->len; |
---|
.. | .. |
---|
797 | 847 | { |
---|
798 | 848 | struct gsm_msg *msg; |
---|
799 | 849 | u8 *dp; |
---|
800 | | - int len, total_size, size; |
---|
801 | | - int h = dlci->adaption - 1; |
---|
| 850 | + int h, len, size; |
---|
802 | 851 | |
---|
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); |
---|
808 | 854 | |
---|
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; |
---|
812 | 858 | |
---|
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; |
---|
814 | 862 | |
---|
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; |
---|
832 | 884 | } |
---|
| 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); |
---|
833 | 893 | /* Bytes of data we used up */ |
---|
834 | | - return total_size; |
---|
| 894 | + return size; |
---|
835 | 895 | } |
---|
836 | 896 | |
---|
837 | 897 | /** |
---|
.. | .. |
---|
994 | 1054 | * Encode up and queue a UI/UIH frame containing our response. |
---|
995 | 1055 | */ |
---|
996 | 1056 | |
---|
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, |
---|
998 | 1058 | int dlen) |
---|
999 | 1059 | { |
---|
1000 | 1060 | struct gsm_msg *msg; |
---|
.. | .. |
---|
1040 | 1100 | fc = (modem & MDM_FC) || !(modem & MDM_RTR); |
---|
1041 | 1101 | if (fc && !dlci->constipated) { |
---|
1042 | 1102 | /* Need to throttle our output on this device */ |
---|
1043 | | - dlci->constipated = 1; |
---|
| 1103 | + dlci->constipated = true; |
---|
1044 | 1104 | } else if (!fc && dlci->constipated) { |
---|
1045 | | - dlci->constipated = 0; |
---|
| 1105 | + dlci->constipated = false; |
---|
1046 | 1106 | gsm_dlci_data_kick(dlci); |
---|
1047 | 1107 | } |
---|
1048 | 1108 | |
---|
.. | .. |
---|
1079 | 1139 | * and if need be stuff a break message down the tty. |
---|
1080 | 1140 | */ |
---|
1081 | 1141 | |
---|
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) |
---|
1083 | 1143 | { |
---|
1084 | 1144 | unsigned int addr = 0; |
---|
1085 | 1145 | unsigned int modem = 0; |
---|
1086 | 1146 | unsigned int brk = 0; |
---|
1087 | 1147 | struct gsm_dlci *dlci; |
---|
1088 | 1148 | int len = clen; |
---|
1089 | | - u8 *dp = data; |
---|
| 1149 | + const u8 *dp = data; |
---|
1090 | 1150 | struct tty_struct *tty; |
---|
1091 | 1151 | |
---|
1092 | 1152 | while (gsm_read_ea(&addr, *dp++) == 0) { |
---|
.. | .. |
---|
1140 | 1200 | * this into the uplink tty if present |
---|
1141 | 1201 | */ |
---|
1142 | 1202 | |
---|
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) |
---|
1144 | 1204 | { |
---|
1145 | 1205 | struct tty_port *port; |
---|
1146 | 1206 | unsigned int addr = 0; |
---|
1147 | 1207 | u8 bits; |
---|
1148 | 1208 | int len = clen; |
---|
1149 | | - u8 *dp = data; |
---|
| 1209 | + const u8 *dp = data; |
---|
1150 | 1210 | |
---|
1151 | 1211 | while (gsm_read_ea(&addr, *dp++) == 0) { |
---|
1152 | 1212 | len--; |
---|
.. | .. |
---|
1195 | 1255 | */ |
---|
1196 | 1256 | |
---|
1197 | 1257 | static void gsm_control_message(struct gsm_mux *gsm, unsigned int command, |
---|
1198 | | - u8 *data, int clen) |
---|
| 1258 | + const u8 *data, int clen) |
---|
1199 | 1259 | { |
---|
1200 | 1260 | u8 buf[1]; |
---|
1201 | 1261 | unsigned long flags; |
---|
.. | .. |
---|
1205 | 1265 | struct gsm_dlci *dlci = gsm->dlci[0]; |
---|
1206 | 1266 | /* Modem wishes to close down */ |
---|
1207 | 1267 | if (dlci) { |
---|
1208 | | - dlci->dead = 1; |
---|
1209 | | - gsm->dead = 1; |
---|
| 1268 | + dlci->dead = true; |
---|
| 1269 | + gsm->dead = true; |
---|
1210 | 1270 | gsm_dlci_begin_close(dlci); |
---|
1211 | 1271 | } |
---|
1212 | 1272 | } |
---|
.. | .. |
---|
1217 | 1277 | break; |
---|
1218 | 1278 | case CMD_FCON: |
---|
1219 | 1279 | /* Modem can accept data again */ |
---|
1220 | | - gsm->constipated = 0; |
---|
| 1280 | + gsm->constipated = false; |
---|
1221 | 1281 | gsm_control_reply(gsm, CMD_FCON, NULL, 0); |
---|
1222 | 1282 | /* Kick the link in case it is idling */ |
---|
1223 | 1283 | spin_lock_irqsave(&gsm->tx_lock, flags); |
---|
.. | .. |
---|
1226 | 1286 | break; |
---|
1227 | 1287 | case CMD_FCOFF: |
---|
1228 | 1288 | /* Modem wants us to STFU */ |
---|
1229 | | - gsm->constipated = 1; |
---|
| 1289 | + gsm->constipated = true; |
---|
1230 | 1290 | gsm_control_reply(gsm, CMD_FCOFF, NULL, 0); |
---|
1231 | 1291 | break; |
---|
1232 | 1292 | case CMD_MSC: |
---|
.. | .. |
---|
1267 | 1327 | */ |
---|
1268 | 1328 | |
---|
1269 | 1329 | static void gsm_control_response(struct gsm_mux *gsm, unsigned int command, |
---|
1270 | | - u8 *data, int clen) |
---|
| 1330 | + const u8 *data, int clen) |
---|
1271 | 1331 | { |
---|
1272 | 1332 | struct gsm_control *ctrl; |
---|
1273 | 1333 | unsigned long flags; |
---|
.. | .. |
---|
1300 | 1360 | |
---|
1301 | 1361 | static void gsm_control_transmit(struct gsm_mux *gsm, struct gsm_control *ctrl) |
---|
1302 | 1362 | { |
---|
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); |
---|
1304 | 1364 | if (msg == NULL) |
---|
1305 | 1365 | 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); |
---|
1308 | 1369 | gsm_data_queue(gsm->dlci[0], msg); |
---|
1309 | 1370 | } |
---|
1310 | 1371 | |
---|
1311 | 1372 | /** |
---|
1312 | 1373 | * gsm_control_retransmit - retransmit a control frame |
---|
1313 | | - * @data: pointer to our gsm object |
---|
| 1374 | + * @t: timer contained in our gsm object |
---|
1314 | 1375 | * |
---|
1315 | 1376 | * Called off the T2 timer expiry in order to retransmit control frames |
---|
1316 | 1377 | * that have been lost in the system somewhere. The control_lock protects |
---|
.. | .. |
---|
1327 | 1388 | spin_lock_irqsave(&gsm->control_lock, flags); |
---|
1328 | 1389 | ctrl = gsm->pending_cmd; |
---|
1329 | 1390 | if (ctrl) { |
---|
1330 | | - gsm->cretries--; |
---|
1331 | | - if (gsm->cretries == 0) { |
---|
| 1391 | + if (gsm->cretries == 0 || !gsm->dlci[0] || gsm->dlci[0]->dead) { |
---|
1332 | 1392 | gsm->pending_cmd = NULL; |
---|
1333 | 1393 | ctrl->error = -ETIMEDOUT; |
---|
1334 | 1394 | ctrl->done = 1; |
---|
.. | .. |
---|
1336 | 1396 | wake_up(&gsm->event); |
---|
1337 | 1397 | return; |
---|
1338 | 1398 | } |
---|
| 1399 | + gsm->cretries--; |
---|
1339 | 1400 | gsm_control_transmit(gsm, ctrl); |
---|
1340 | 1401 | mod_timer(&gsm->t2_timer, jiffies + gsm->t2 * HZ / 100); |
---|
1341 | 1402 | } |
---|
.. | .. |
---|
1347 | 1408 | * @gsm: the GSM channel |
---|
1348 | 1409 | * @command: command to send including CR bit |
---|
1349 | 1410 | * @data: bytes of data (must be kmalloced) |
---|
1350 | | - * @len: length of the block to send |
---|
| 1411 | + * @clen: length of the block to send |
---|
1351 | 1412 | * |
---|
1352 | 1413 | * Queue and dispatch a control command. Only one command can be |
---|
1353 | 1414 | * active at a time. In theory more can be outstanding but the matching |
---|
.. | .. |
---|
1358 | 1419 | unsigned int command, u8 *data, int clen) |
---|
1359 | 1420 | { |
---|
1360 | 1421 | struct gsm_control *ctrl = kzalloc(sizeof(struct gsm_control), |
---|
1361 | | - GFP_KERNEL); |
---|
| 1422 | + GFP_ATOMIC); |
---|
1362 | 1423 | unsigned long flags; |
---|
1363 | 1424 | if (ctrl == NULL) |
---|
1364 | 1425 | return NULL; |
---|
.. | .. |
---|
1376 | 1437 | |
---|
1377 | 1438 | /* If DLCI0 is in ADM mode skip retries, it won't respond */ |
---|
1378 | 1439 | if (gsm->dlci[0]->mode == DLCI_MODE_ADM) |
---|
1379 | | - gsm->cretries = 1; |
---|
| 1440 | + gsm->cretries = 0; |
---|
1380 | 1441 | else |
---|
1381 | 1442 | gsm->cretries = gsm->n2; |
---|
1382 | 1443 | |
---|
.. | .. |
---|
1424 | 1485 | |
---|
1425 | 1486 | static void gsm_dlci_close(struct gsm_dlci *dlci) |
---|
1426 | 1487 | { |
---|
| 1488 | + unsigned long flags; |
---|
| 1489 | + |
---|
1427 | 1490 | del_timer(&dlci->t1); |
---|
1428 | 1491 | if (debug & 8) |
---|
1429 | 1492 | pr_debug("DLCI %d goes closed.\n", dlci->addr); |
---|
1430 | 1493 | dlci->state = DLCI_CLOSED; |
---|
| 1494 | + /* Prevent us from sending data before the link is up again */ |
---|
| 1495 | + dlci->constipated = true; |
---|
1431 | 1496 | if (dlci->addr != 0) { |
---|
1432 | 1497 | 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); |
---|
1434 | 1504 | } else |
---|
1435 | | - dlci->gsm->dead = 1; |
---|
| 1505 | + dlci->gsm->dead = true; |
---|
1436 | 1506 | wake_up(&dlci->gsm->event); |
---|
1437 | 1507 | /* A DLCI 0 close is a MUX termination so we need to kick that |
---|
1438 | 1508 | back to userspace somehow */ |
---|
.. | .. |
---|
1452 | 1522 | del_timer(&dlci->t1); |
---|
1453 | 1523 | /* This will let a tty open continue */ |
---|
1454 | 1524 | dlci->state = DLCI_OPEN; |
---|
| 1525 | + dlci->constipated = false; |
---|
1455 | 1526 | if (debug & 8) |
---|
1456 | 1527 | pr_debug("DLCI %d goes open.\n", dlci->addr); |
---|
1457 | 1528 | wake_up(&dlci->gsm->event); |
---|
.. | .. |
---|
1459 | 1530 | |
---|
1460 | 1531 | /** |
---|
1461 | 1532 | * gsm_dlci_t1 - T1 timer expiry |
---|
1462 | | - * @dlci: DLCI that opened |
---|
| 1533 | + * @t: timer contained in the DLCI that opened |
---|
1463 | 1534 | * |
---|
1464 | 1535 | * The T1 timer handles retransmits of control frames (essentially of |
---|
1465 | 1536 | * SABM and DISC). We resend the command until the retry count runs out |
---|
.. | .. |
---|
1479 | 1550 | |
---|
1480 | 1551 | switch (dlci->state) { |
---|
1481 | 1552 | case DLCI_OPENING: |
---|
1482 | | - dlci->retries--; |
---|
1483 | 1553 | if (dlci->retries) { |
---|
| 1554 | + dlci->retries--; |
---|
1484 | 1555 | gsm_command(dlci->gsm, dlci->addr, SABM|PF); |
---|
1485 | 1556 | mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100); |
---|
1486 | 1557 | } else if (!dlci->addr && gsm->control == (DM | PF)) { |
---|
.. | .. |
---|
1495 | 1566 | |
---|
1496 | 1567 | break; |
---|
1497 | 1568 | case DLCI_CLOSING: |
---|
1498 | | - dlci->retries--; |
---|
1499 | 1569 | if (dlci->retries) { |
---|
| 1570 | + dlci->retries--; |
---|
1500 | 1571 | gsm_command(dlci->gsm, dlci->addr, DISC|PF); |
---|
1501 | 1572 | mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100); |
---|
1502 | 1573 | } else |
---|
1503 | 1574 | gsm_dlci_close(dlci); |
---|
| 1575 | + break; |
---|
| 1576 | + default: |
---|
| 1577 | + pr_debug("%s: unhandled state: %d\n", __func__, dlci->state); |
---|
1504 | 1578 | break; |
---|
1505 | 1579 | } |
---|
1506 | 1580 | } |
---|
.. | .. |
---|
1524 | 1598 | dlci->state = DLCI_OPENING; |
---|
1525 | 1599 | gsm_command(dlci->gsm, dlci->addr, SABM|PF); |
---|
1526 | 1600 | 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 | + } |
---|
1527 | 1620 | } |
---|
1528 | 1621 | |
---|
1529 | 1622 | /** |
---|
.. | .. |
---|
1552 | 1645 | * gsm_dlci_data - data arrived |
---|
1553 | 1646 | * @dlci: channel |
---|
1554 | 1647 | * @data: block of bytes received |
---|
1555 | | - * @len: length of received block |
---|
| 1648 | + * @clen: length of received block |
---|
1556 | 1649 | * |
---|
1557 | 1650 | * A UI or UIH frame has arrived which contains data for a channel |
---|
1558 | 1651 | * other than the control channel. If the relevant virtual tty is |
---|
1559 | 1652 | * open we shovel the bits down it, if not we drop them. |
---|
1560 | 1653 | */ |
---|
1561 | 1654 | |
---|
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) |
---|
1563 | 1656 | { |
---|
1564 | 1657 | /* krefs .. */ |
---|
1565 | 1658 | struct tty_port *port = &dlci->port; |
---|
.. | .. |
---|
1571 | 1664 | pr_debug("%d bytes for tty\n", len); |
---|
1572 | 1665 | switch (dlci->adaption) { |
---|
1573 | 1666 | /* Unsupported types */ |
---|
1574 | | - /* Packetised interruptible data */ |
---|
1575 | | - case 4: |
---|
| 1667 | + case 4: /* Packetised interruptible data */ |
---|
1576 | 1668 | break; |
---|
1577 | | - /* Packetised uininterruptible voice/data */ |
---|
1578 | | - case 3: |
---|
| 1669 | + case 3: /* Packetised uininterruptible voice/data */ |
---|
1579 | 1670 | break; |
---|
1580 | | - /* Asynchronous serial with line state in each frame */ |
---|
1581 | | - case 2: |
---|
| 1671 | + case 2: /* Asynchronous serial with line state in each frame */ |
---|
1582 | 1672 | while (gsm_read_ea(&modem, *data++) == 0) { |
---|
1583 | 1673 | len--; |
---|
1584 | 1674 | if (len == 0) |
---|
.. | .. |
---|
1589 | 1679 | gsm_process_modem(tty, dlci, modem, clen); |
---|
1590 | 1680 | tty_kref_put(tty); |
---|
1591 | 1681 | } |
---|
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 */ |
---|
1594 | 1684 | default: |
---|
1595 | 1685 | tty_insert_flip_string(port, data, len); |
---|
1596 | 1686 | tty_flip_buffer_push(port); |
---|
.. | .. |
---|
1609 | 1699 | * and we divide up the work accordingly. |
---|
1610 | 1700 | */ |
---|
1611 | 1701 | |
---|
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) |
---|
1613 | 1703 | { |
---|
1614 | 1704 | /* See what command is involved */ |
---|
1615 | 1705 | unsigned int command = 0; |
---|
.. | .. |
---|
1654 | 1744 | return NULL; |
---|
1655 | 1745 | spin_lock_init(&dlci->lock); |
---|
1656 | 1746 | 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) { |
---|
1659 | 1748 | kfree(dlci); |
---|
1660 | 1749 | return NULL; |
---|
1661 | 1750 | } |
---|
.. | .. |
---|
1668 | 1757 | dlci->addr = addr; |
---|
1669 | 1758 | dlci->adaption = gsm->adaption; |
---|
1670 | 1759 | dlci->state = DLCI_CLOSED; |
---|
1671 | | - if (addr) |
---|
| 1760 | + if (addr) { |
---|
1672 | 1761 | dlci->data = gsm_dlci_data; |
---|
1673 | | - else |
---|
| 1762 | + /* Prevent us from sending data before the link is up */ |
---|
| 1763 | + dlci->constipated = true; |
---|
| 1764 | + } else { |
---|
1674 | 1765 | dlci->data = gsm_dlci_command; |
---|
| 1766 | + } |
---|
1675 | 1767 | gsm->dlci[addr] = dlci; |
---|
1676 | 1768 | return dlci; |
---|
1677 | 1769 | } |
---|
1678 | 1770 | |
---|
1679 | 1771 | /** |
---|
1680 | 1772 | * gsm_dlci_free - free DLCI |
---|
1681 | | - * @dlci: DLCI to free |
---|
| 1773 | + * @port: tty port for DLCI to free |
---|
1682 | 1774 | * |
---|
1683 | 1775 | * Free up a DLCI. |
---|
1684 | 1776 | * |
---|
.. | .. |
---|
1690 | 1782 | |
---|
1691 | 1783 | del_timer_sync(&dlci->t1); |
---|
1692 | 1784 | dlci->gsm->dlci[dlci->addr] = NULL; |
---|
1693 | | - kfifo_free(dlci->fifo); |
---|
| 1785 | + kfifo_free(&dlci->fifo); |
---|
1694 | 1786 | while ((dlci->skb = skb_dequeue(&dlci->skb_list))) |
---|
1695 | 1787 | dev_kfree_skb(dlci->skb); |
---|
1696 | 1788 | kfree(dlci); |
---|
.. | .. |
---|
1725 | 1817 | gsm_destroy_network(dlci); |
---|
1726 | 1818 | mutex_unlock(&dlci->mutex); |
---|
1727 | 1819 | |
---|
| 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 | + */ |
---|
1728 | 1825 | tty_vhangup(tty); |
---|
1729 | 1826 | |
---|
1730 | 1827 | tty_port_tty_set(&dlci->port, NULL); |
---|
.. | .. |
---|
1808 | 1905 | gsm_response(gsm, address, UA); |
---|
1809 | 1906 | gsm_dlci_close(dlci); |
---|
1810 | 1907 | break; |
---|
1811 | | - case UA: |
---|
1812 | 1908 | case UA|PF: |
---|
1813 | 1909 | if (cr == 0 || dlci == NULL) |
---|
1814 | 1910 | break; |
---|
.. | .. |
---|
1818 | 1914 | break; |
---|
1819 | 1915 | case DLCI_OPENING: |
---|
1820 | 1916 | gsm_dlci_open(dlci); |
---|
| 1917 | + break; |
---|
| 1918 | + default: |
---|
| 1919 | + pr_debug("%s: unhandled state: %d\n", __func__, |
---|
| 1920 | + dlci->state); |
---|
1821 | 1921 | break; |
---|
1822 | 1922 | } |
---|
1823 | 1923 | break; |
---|
.. | .. |
---|
1838 | 1938 | goto invalid; |
---|
1839 | 1939 | #endif |
---|
1840 | 1940 | if (dlci == NULL || dlci->state != DLCI_OPEN) { |
---|
1841 | | - gsm_command(gsm, address, DM|PF); |
---|
| 1941 | + gsm_response(gsm, address, DM|PF); |
---|
1842 | 1942 | return; |
---|
1843 | 1943 | } |
---|
1844 | 1944 | dlci->data(dlci, gsm->buf, gsm->len); |
---|
.. | .. |
---|
1932 | 2032 | break; |
---|
1933 | 2033 | } |
---|
1934 | 2034 | break; |
---|
| 2035 | + default: |
---|
| 2036 | + pr_debug("%s: unhandled state: %d\n", __func__, gsm->state); |
---|
| 2037 | + break; |
---|
1935 | 2038 | } |
---|
1936 | 2039 | } |
---|
1937 | 2040 | |
---|
.. | .. |
---|
1945 | 2048 | |
---|
1946 | 2049 | static void gsm1_receive(struct gsm_mux *gsm, unsigned char c) |
---|
1947 | 2050 | { |
---|
| 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 | + } |
---|
1948 | 2061 | if (c == GSM1_SOF) { |
---|
1949 | 2062 | /* EOF is only valid in frame if we have got to the data state |
---|
1950 | 2063 | and received at least one byte (the FCS) */ |
---|
.. | .. |
---|
1959 | 2072 | } |
---|
1960 | 2073 | /* Any partial frame was a runt so go back to start */ |
---|
1961 | 2074 | if (gsm->state != GSM_START) { |
---|
1962 | | - gsm->malformed++; |
---|
| 2075 | + if (gsm->state != GSM_SEARCH) |
---|
| 2076 | + gsm->malformed++; |
---|
1963 | 2077 | gsm->state = GSM_START; |
---|
1964 | 2078 | } |
---|
1965 | 2079 | /* A SOF in GSM_START means we are still reading idling or |
---|
.. | .. |
---|
1968 | 2082 | } |
---|
1969 | 2083 | |
---|
1970 | 2084 | if (c == GSM1_ESCAPE) { |
---|
1971 | | - gsm->escape = 1; |
---|
| 2085 | + gsm->escape = true; |
---|
1972 | 2086 | return; |
---|
1973 | 2087 | } |
---|
1974 | 2088 | |
---|
.. | .. |
---|
1978 | 2092 | |
---|
1979 | 2093 | if (gsm->escape) { |
---|
1980 | 2094 | c ^= GSM1_ESCAPE_BITS; |
---|
1981 | | - gsm->escape = 0; |
---|
| 2095 | + gsm->escape = false; |
---|
1982 | 2096 | } |
---|
1983 | 2097 | switch (gsm->state) { |
---|
1984 | 2098 | case GSM_START: /* First byte after SOF */ |
---|
1985 | 2099 | gsm->address = 0; |
---|
1986 | 2100 | gsm->state = GSM_ADDRESS; |
---|
1987 | 2101 | gsm->fcs = INIT_FCS; |
---|
1988 | | - /* Drop through */ |
---|
| 2102 | + fallthrough; |
---|
1989 | 2103 | case GSM_ADDRESS: /* Address continuation */ |
---|
1990 | 2104 | gsm->fcs = gsm_fcs_add(gsm->fcs, c); |
---|
1991 | 2105 | if (gsm_read_ea(&gsm->address, c)) |
---|
.. | .. |
---|
2005 | 2119 | gsm->buf[gsm->count++] = c; |
---|
2006 | 2120 | break; |
---|
2007 | 2121 | case GSM_OVERRUN: /* Over-long - eg a dropped SOF */ |
---|
| 2122 | + break; |
---|
| 2123 | + default: |
---|
| 2124 | + pr_debug("%s: unhandled state: %d\n", __func__, gsm->state); |
---|
2008 | 2125 | break; |
---|
2009 | 2126 | } |
---|
2010 | 2127 | } |
---|
.. | .. |
---|
2028 | 2145 | gsm->io_error++; |
---|
2029 | 2146 | } |
---|
2030 | 2147 | |
---|
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 | | - |
---|
2058 | 2148 | /** |
---|
2059 | 2149 | * gsm_cleanup_mux - generic GSM protocol cleanup |
---|
2060 | 2150 | * @gsm: our mux |
---|
| 2151 | + * @disc: disconnect link? |
---|
2061 | 2152 | * |
---|
2062 | 2153 | * Clean up the bits of the mux which are the same for all framing |
---|
2063 | 2154 | * protocols. Remove the mux from the mux table, stop all the timers |
---|
2064 | 2155 | * and then shut down each device hanging up the channels as we go. |
---|
2065 | 2156 | */ |
---|
2066 | 2157 | |
---|
2067 | | -static void gsm_cleanup_mux(struct gsm_mux *gsm) |
---|
| 2158 | +static void gsm_cleanup_mux(struct gsm_mux *gsm, bool disc) |
---|
2068 | 2159 | { |
---|
2069 | 2160 | int i; |
---|
2070 | 2161 | struct gsm_dlci *dlci = gsm->dlci[0]; |
---|
2071 | 2162 | struct gsm_msg *txq, *ntxq; |
---|
2072 | 2163 | |
---|
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; |
---|
2093 | 2165 | 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--) |
---|
2095 | 2180 | if (gsm->dlci[i]) |
---|
2096 | 2181 | gsm_dlci_release(gsm->dlci[i]); |
---|
2097 | 2182 | mutex_unlock(&gsm->mutex); |
---|
2098 | 2183 | /* Now wipe the queues */ |
---|
| 2184 | + tty_ldisc_flush(gsm->tty); |
---|
2099 | 2185 | list_for_each_entry_safe(txq, ntxq, &gsm->tx_list, list) |
---|
2100 | 2186 | kfree(txq); |
---|
2101 | 2187 | INIT_LIST_HEAD(&gsm->tx_list); |
---|
.. | .. |
---|
2113 | 2199 | static int gsm_activate_mux(struct gsm_mux *gsm) |
---|
2114 | 2200 | { |
---|
2115 | 2201 | 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); |
---|
2122 | 2202 | |
---|
2123 | 2203 | if (gsm->encoding == 0) |
---|
2124 | 2204 | gsm->receive = gsm0_receive; |
---|
2125 | 2205 | else |
---|
2126 | 2206 | 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; |
---|
2140 | 2207 | |
---|
2141 | 2208 | dlci = gsm_dlci_alloc(gsm, 0); |
---|
2142 | 2209 | if (dlci == NULL) |
---|
2143 | 2210 | return -ENOMEM; |
---|
2144 | | - gsm->dead = 0; /* Tty opens are now permissible */ |
---|
| 2211 | + gsm->dead = false; /* Tty opens are now permissible */ |
---|
2145 | 2212 | return 0; |
---|
2146 | 2213 | } |
---|
2147 | 2214 | |
---|
2148 | 2215 | /** |
---|
2149 | 2216 | * gsm_free_mux - free up a mux |
---|
2150 | | - * @mux: mux to free |
---|
| 2217 | + * @gsm: mux to free |
---|
2151 | 2218 | * |
---|
2152 | 2219 | * Dispose of allocated resources for a dead mux |
---|
2153 | 2220 | */ |
---|
2154 | 2221 | static void gsm_free_mux(struct gsm_mux *gsm) |
---|
2155 | 2222 | { |
---|
| 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); |
---|
2156 | 2232 | kfree(gsm->txframe); |
---|
2157 | 2233 | kfree(gsm->buf); |
---|
2158 | 2234 | kfree(gsm); |
---|
.. | .. |
---|
2160 | 2236 | |
---|
2161 | 2237 | /** |
---|
2162 | 2238 | * gsm_free_muxr - free up a mux |
---|
2163 | | - * @mux: mux to free |
---|
| 2239 | + * @ref: kreference to the mux to free |
---|
2164 | 2240 | * |
---|
2165 | 2241 | * Dispose of allocated resources for a dead mux |
---|
2166 | 2242 | */ |
---|
.. | .. |
---|
2172 | 2248 | |
---|
2173 | 2249 | static inline void mux_get(struct gsm_mux *gsm) |
---|
2174 | 2250 | { |
---|
| 2251 | + unsigned long flags; |
---|
| 2252 | + |
---|
| 2253 | + spin_lock_irqsave(&gsm_mux_lock, flags); |
---|
2175 | 2254 | kref_get(&gsm->ref); |
---|
| 2255 | + spin_unlock_irqrestore(&gsm_mux_lock, flags); |
---|
2176 | 2256 | } |
---|
2177 | 2257 | |
---|
2178 | 2258 | static inline void mux_put(struct gsm_mux *gsm) |
---|
2179 | 2259 | { |
---|
| 2260 | + unsigned long flags; |
---|
| 2261 | + |
---|
| 2262 | + spin_lock_irqsave(&gsm_mux_lock, flags); |
---|
2180 | 2263 | 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; |
---|
2181 | 2275 | } |
---|
2182 | 2276 | |
---|
2183 | 2277 | /** |
---|
.. | .. |
---|
2188 | 2282 | |
---|
2189 | 2283 | static struct gsm_mux *gsm_alloc_mux(void) |
---|
2190 | 2284 | { |
---|
| 2285 | + int i; |
---|
2191 | 2286 | struct gsm_mux *gsm = kzalloc(sizeof(struct gsm_mux), GFP_KERNEL); |
---|
2192 | 2287 | if (gsm == NULL) |
---|
2193 | 2288 | return NULL; |
---|
.. | .. |
---|
2196 | 2291 | kfree(gsm); |
---|
2197 | 2292 | return NULL; |
---|
2198 | 2293 | } |
---|
2199 | | - gsm->txframe = kmalloc(2 * MAX_MRU + 2, GFP_KERNEL); |
---|
| 2294 | + gsm->txframe = kmalloc(2 * (MAX_MTU + PROT_OVERHEAD - 1), GFP_KERNEL); |
---|
2200 | 2295 | if (gsm->txframe == NULL) { |
---|
2201 | 2296 | kfree(gsm->buf); |
---|
2202 | 2297 | kfree(gsm); |
---|
.. | .. |
---|
2206 | 2301 | mutex_init(&gsm->mutex); |
---|
2207 | 2302 | kref_init(&gsm->ref); |
---|
2208 | 2303 | 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); |
---|
2209 | 2308 | |
---|
2210 | 2309 | gsm->t1 = T1; |
---|
2211 | 2310 | gsm->t2 = T2; |
---|
.. | .. |
---|
2215 | 2314 | gsm->encoding = 1; |
---|
2216 | 2315 | gsm->mru = 64; /* Default to encoding 1 so these should be 64 */ |
---|
2217 | 2316 | 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 | + } |
---|
2219 | 2338 | |
---|
2220 | 2339 | 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; |
---|
2221 | 2441 | } |
---|
2222 | 2442 | |
---|
2223 | 2443 | /** |
---|
.. | .. |
---|
2255 | 2475 | |
---|
2256 | 2476 | static int gsmld_attach_gsm(struct tty_struct *tty, struct gsm_mux *gsm) |
---|
2257 | 2477 | { |
---|
2258 | | - int ret, i, base; |
---|
| 2478 | + unsigned int base; |
---|
| 2479 | + int ret, i; |
---|
2259 | 2480 | |
---|
2260 | 2481 | 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); |
---|
2262 | 2485 | ret = gsm_activate_mux(gsm); |
---|
2263 | 2486 | if (ret != 0) |
---|
2264 | 2487 | tty_kref_put(gsm->tty); |
---|
2265 | 2488 | else { |
---|
2266 | 2489 | /* Don't register device 0 - this is the control channel and not |
---|
2267 | 2490 | 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 | + } |
---|
2271 | 2504 | } |
---|
2272 | 2505 | return ret; |
---|
2273 | 2506 | } |
---|
.. | .. |
---|
2283 | 2516 | |
---|
2284 | 2517 | static void gsmld_detach_gsm(struct tty_struct *tty, struct gsm_mux *gsm) |
---|
2285 | 2518 | { |
---|
| 2519 | + unsigned int base = mux_num_to_base(gsm); /* Base for this MUX */ |
---|
2286 | 2520 | int i; |
---|
2287 | | - int base = gsm->num << 6; /* Base for this MUX */ |
---|
2288 | 2521 | |
---|
2289 | 2522 | WARN_ON(tty != gsm->tty); |
---|
2290 | 2523 | for (i = 1; i < NUM_DLCI; i++) |
---|
2291 | 2524 | 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; |
---|
2293 | 2527 | tty_kref_put(gsm->tty); |
---|
2294 | 2528 | gsm->tty = NULL; |
---|
2295 | 2529 | } |
---|
.. | .. |
---|
2318 | 2552 | case TTY_BREAK: |
---|
2319 | 2553 | case TTY_PARITY: |
---|
2320 | 2554 | case TTY_FRAME: |
---|
2321 | | - gsm->error(gsm, *dp, flags); |
---|
| 2555 | + gsm_error(gsm, *dp, flags); |
---|
2322 | 2556 | break; |
---|
2323 | 2557 | default: |
---|
2324 | 2558 | WARN_ONCE(1, "%s: unknown flag %d\n", |
---|
.. | .. |
---|
2356 | 2590 | static void gsmld_close(struct tty_struct *tty) |
---|
2357 | 2591 | { |
---|
2358 | 2592 | 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); |
---|
2359 | 2599 | |
---|
2360 | 2600 | gsmld_detach_gsm(tty, gsm); |
---|
2361 | 2601 | |
---|
.. | .. |
---|
2395 | 2635 | |
---|
2396 | 2636 | ret = gsmld_attach_gsm(tty, gsm); |
---|
2397 | 2637 | if (ret != 0) { |
---|
2398 | | - gsm_cleanup_mux(gsm); |
---|
| 2638 | + gsm_cleanup_mux(gsm, false); |
---|
2399 | 2639 | mux_put(gsm); |
---|
2400 | 2640 | } |
---|
2401 | 2641 | return ret; |
---|
.. | .. |
---|
2441 | 2681 | */ |
---|
2442 | 2682 | |
---|
2443 | 2683 | 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) |
---|
2445 | 2686 | { |
---|
2446 | 2687 | return -EOPNOTSUPP; |
---|
2447 | 2688 | } |
---|
.. | .. |
---|
2463 | 2704 | static ssize_t gsmld_write(struct tty_struct *tty, struct file *file, |
---|
2464 | 2705 | const unsigned char *buf, size_t nr) |
---|
2465 | 2706 | { |
---|
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); |
---|
2467 | 2718 | 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; |
---|
2471 | 2725 | } |
---|
2472 | 2726 | |
---|
2473 | 2727 | /** |
---|
.. | .. |
---|
2492 | 2746 | |
---|
2493 | 2747 | poll_wait(file, &tty->read_wait, wait); |
---|
2494 | 2748 | poll_wait(file, &tty->write_wait, wait); |
---|
| 2749 | + |
---|
| 2750 | + if (gsm->dead) |
---|
| 2751 | + mask |= EPOLLHUP; |
---|
2495 | 2752 | if (tty_hung_up_p(file)) |
---|
| 2753 | + mask |= EPOLLHUP; |
---|
| 2754 | + if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) |
---|
2496 | 2755 | mask |= EPOLLHUP; |
---|
2497 | 2756 | if (!tty_is_writelocked(tty) && tty_write_room(tty) > 0) |
---|
2498 | 2757 | mask |= EPOLLOUT | EPOLLWRNORM; |
---|
2499 | | - if (gsm->dead) |
---|
2500 | | - mask |= EPOLLHUP; |
---|
2501 | 2758 | 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; |
---|
2585 | 2759 | } |
---|
2586 | 2760 | |
---|
2587 | 2761 | static int gsmld_ioctl(struct tty_struct *tty, struct file *file, |
---|
.. | .. |
---|
2589 | 2763 | { |
---|
2590 | 2764 | struct gsm_config c; |
---|
2591 | 2765 | struct gsm_mux *gsm = tty->disc_data; |
---|
| 2766 | + unsigned int base; |
---|
2592 | 2767 | |
---|
2593 | 2768 | switch (cmd) { |
---|
2594 | 2769 | 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))) |
---|
2612 | 2772 | return -EFAULT; |
---|
2613 | 2773 | return 0; |
---|
2614 | 2774 | case GSMIOC_SETCONF: |
---|
2615 | | - if (copy_from_user(&c, (void *)arg, sizeof(c))) |
---|
| 2775 | + if (copy_from_user(&c, (void __user *)arg, sizeof(c))) |
---|
2616 | 2776 | 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); |
---|
2618 | 2781 | default: |
---|
2619 | 2782 | return n_tty_ioctl_helper(tty, file, cmd, arg); |
---|
2620 | 2783 | } |
---|
2621 | 2784 | } |
---|
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 |
---|
2630 | 2785 | |
---|
2631 | 2786 | /* |
---|
2632 | 2787 | * Network interface |
---|
.. | .. |
---|
2699 | 2854 | } |
---|
2700 | 2855 | |
---|
2701 | 2856 | /* 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) |
---|
2703 | 2858 | { |
---|
2704 | 2859 | /* Tell syslog we are hosed. */ |
---|
2705 | 2860 | dev_dbg(&net->dev, "Tx timed out.\n"); |
---|
.. | .. |
---|
2709 | 2864 | } |
---|
2710 | 2865 | |
---|
2711 | 2866 | 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) |
---|
2713 | 2868 | { |
---|
2714 | 2869 | struct net_device *net = dlci->net; |
---|
2715 | 2870 | struct sk_buff *skb; |
---|
.. | .. |
---|
2764 | 2919 | { |
---|
2765 | 2920 | struct gsm_mux_net *mux_net; |
---|
2766 | 2921 | |
---|
2767 | | - pr_debug("destroy network interface"); |
---|
| 2922 | + pr_debug("destroy network interface\n"); |
---|
2768 | 2923 | if (!dlci->net) |
---|
2769 | 2924 | return; |
---|
2770 | 2925 | mux_net = netdev_priv(dlci->net); |
---|
.. | .. |
---|
2793 | 2948 | if (nc->adaption != 3 && nc->adaption != 4) |
---|
2794 | 2949 | return -EPROTONOSUPPORT; |
---|
2795 | 2950 | |
---|
2796 | | - pr_debug("create network interface"); |
---|
| 2951 | + pr_debug("create network interface\n"); |
---|
2797 | 2952 | |
---|
2798 | 2953 | netname = "gsm%d"; |
---|
2799 | 2954 | if (nc->if_name[0] != '\0') |
---|
.. | .. |
---|
2801 | 2956 | net = alloc_netdev(sizeof(struct gsm_mux_net), netname, |
---|
2802 | 2957 | NET_NAME_UNKNOWN, gsm_mux_net_init); |
---|
2803 | 2958 | if (!net) { |
---|
2804 | | - pr_err("alloc_netdev failed"); |
---|
| 2959 | + pr_err("alloc_netdev failed\n"); |
---|
2805 | 2960 | return -ENOMEM; |
---|
2806 | 2961 | } |
---|
2807 | 2962 | net->mtu = dlci->gsm->mtu; |
---|
.. | .. |
---|
2819 | 2974 | dlci->data = gsm_mux_rx_netchar; |
---|
2820 | 2975 | dlci->net = net; |
---|
2821 | 2976 | |
---|
2822 | | - pr_debug("register netdev"); |
---|
| 2977 | + pr_debug("register netdev\n"); |
---|
2823 | 2978 | retval = register_netdev(net); |
---|
2824 | 2979 | if (retval) { |
---|
2825 | 2980 | pr_err("network register fail %d\n", retval); |
---|
.. | .. |
---|
2839 | 2994 | .flush_buffer = gsmld_flush_buffer, |
---|
2840 | 2995 | .read = gsmld_read, |
---|
2841 | 2996 | .write = gsmld_write, |
---|
2842 | | -#ifdef CONFIG_COMPAT |
---|
2843 | | - .compat_ioctl = gsmld_compat_ioctl, |
---|
2844 | | -#endif |
---|
2845 | 2997 | .ioctl = gsmld_ioctl, |
---|
2846 | 2998 | .poll = gsmld_poll, |
---|
2847 | 2999 | .receive_buf = gsmld_receive_buf, |
---|
.. | .. |
---|
2856 | 3008 | |
---|
2857 | 3009 | static int gsmtty_modem_update(struct gsm_dlci *dlci, u8 brk) |
---|
2858 | 3010 | { |
---|
2859 | | - u8 modembits[5]; |
---|
| 3011 | + u8 modembits[3]; |
---|
2860 | 3012 | struct gsm_control *ctrl; |
---|
2861 | 3013 | int len = 2; |
---|
2862 | 3014 | |
---|
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 */ |
---|
2864 | 3019 | 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); |
---|
2872 | 3022 | if (ctrl == NULL) |
---|
2873 | 3023 | return -ENOMEM; |
---|
2874 | 3024 | return gsm_control_wait(dlci->gsm, ctrl); |
---|
.. | .. |
---|
2921 | 3071 | struct gsm_mux *gsm; |
---|
2922 | 3072 | struct gsm_dlci *dlci; |
---|
2923 | 3073 | unsigned int line = tty->index; |
---|
2924 | | - unsigned int mux = line >> 6; |
---|
| 3074 | + unsigned int mux = mux_line_to_num(line); |
---|
2925 | 3075 | bool alloc = false; |
---|
2926 | 3076 | int ret; |
---|
2927 | 3077 | |
---|
.. | .. |
---|
2976 | 3126 | { |
---|
2977 | 3127 | struct gsm_dlci *dlci = tty->driver_data; |
---|
2978 | 3128 | struct tty_port *port = &dlci->port; |
---|
| 3129 | + struct gsm_mux *gsm = dlci->gsm; |
---|
2979 | 3130 | |
---|
2980 | 3131 | port->count++; |
---|
2981 | 3132 | tty_port_tty_set(port, tty); |
---|
.. | .. |
---|
2985 | 3136 | a DM straight back. This is ok as that will have caused a hangup */ |
---|
2986 | 3137 | tty_port_set_initialized(port, 1); |
---|
2987 | 3138 | /* 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); |
---|
2989 | 3143 | /* And wait for virtual carrier */ |
---|
2990 | 3144 | return tty_port_block_til_ready(port, tty, filp); |
---|
2991 | 3145 | } |
---|
.. | .. |
---|
3028 | 3182 | if (dlci->state == DLCI_CLOSED) |
---|
3029 | 3183 | return -EINVAL; |
---|
3030 | 3184 | /* 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); |
---|
3032 | 3186 | /* Need to kick the channel */ |
---|
3033 | 3187 | gsm_dlci_data_kick(dlci); |
---|
3034 | 3188 | return sent; |
---|
.. | .. |
---|
3039 | 3193 | struct gsm_dlci *dlci = tty->driver_data; |
---|
3040 | 3194 | if (dlci->state == DLCI_CLOSED) |
---|
3041 | 3195 | return -EINVAL; |
---|
3042 | | - return TX_SIZE - kfifo_len(dlci->fifo); |
---|
| 3196 | + return TX_SIZE - kfifo_len(&dlci->fifo); |
---|
3043 | 3197 | } |
---|
3044 | 3198 | |
---|
3045 | 3199 | static int gsmtty_chars_in_buffer(struct tty_struct *tty) |
---|
.. | .. |
---|
3047 | 3201 | struct gsm_dlci *dlci = tty->driver_data; |
---|
3048 | 3202 | if (dlci->state == DLCI_CLOSED) |
---|
3049 | 3203 | return -EINVAL; |
---|
3050 | | - return kfifo_len(dlci->fifo); |
---|
| 3204 | + return kfifo_len(&dlci->fifo); |
---|
3051 | 3205 | } |
---|
3052 | 3206 | |
---|
3053 | 3207 | static void gsmtty_flush_buffer(struct tty_struct *tty) |
---|
3054 | 3208 | { |
---|
3055 | 3209 | struct gsm_dlci *dlci = tty->driver_data; |
---|
| 3210 | + unsigned long flags; |
---|
| 3211 | + |
---|
3056 | 3212 | if (dlci->state == DLCI_CLOSED) |
---|
3057 | 3213 | return; |
---|
3058 | 3214 | /* Caution needed: If we implement reliable transport classes |
---|
3059 | 3215 | then the data being transmitted can't simply be junked once |
---|
3060 | 3216 | it has first hit the stack. Until then we can just blow it |
---|
3061 | 3217 | 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); |
---|
3063 | 3221 | /* Need to unhook this DLCI from the transmit queue logic */ |
---|
3064 | 3222 | } |
---|
3065 | 3223 | |
---|
.. | .. |
---|
3149 | 3307 | if (dlci->state == DLCI_CLOSED) |
---|
3150 | 3308 | return; |
---|
3151 | 3309 | 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 */ |
---|
3155 | 3313 | gsmtty_modem_update(dlci, 0); |
---|
3156 | 3314 | } |
---|
3157 | 3315 | |
---|
.. | .. |
---|
3161 | 3319 | if (dlci->state == DLCI_CLOSED) |
---|
3162 | 3320 | return; |
---|
3163 | 3321 | 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 */ |
---|
3167 | 3325 | gsmtty_modem_update(dlci, 0); |
---|
3168 | 3326 | } |
---|
3169 | 3327 | |
---|