blob: 5f68f2a70c5c81d85796d6725de970c010a39ff5 [file] [log] [blame]
Alan Coxe1eaea42010-03-26 11:32:54 +00001/*
2 * n_gsm.c GSM 0710 tty multiplexor
3 * Copyright (c) 2009/10 Intel Corporation
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 *
18 * * THIS IS A DEVELOPMENT SNAPSHOT IT IS NOT A FINAL RELEASE *
19 *
20 * TO DO:
21 * Mostly done: ioctls for setting modes/timing
Alan Cox5f9a31d2010-11-04 15:17:27 +000022 * Partly done: hooks so you can pull off frames to non tty devs
Alan Coxe1eaea42010-03-26 11:32:54 +000023 * Restart DLCI 0 when it closes ?
Alan Coxe1eaea42010-03-26 11:32:54 +000024 * Improve the tx engine
25 * Resolve tx side locking by adding a queue_head and routing
26 * all control traffic via it
27 * General tidy/document
28 * Review the locking/move to refcounts more (mux now moved to an
29 * alloc/free model ready)
30 * Use newest tty open/close port helpers and install hooks
31 * What to do about power functions ?
32 * Termios setting and negotiation
33 * Do we need a 'which mux are you' ioctl to correlate mux and tty sets
34 *
35 */
36
37#include <linux/types.h>
38#include <linux/major.h>
39#include <linux/errno.h>
40#include <linux/signal.h>
41#include <linux/fcntl.h>
42#include <linux/sched.h>
43#include <linux/interrupt.h>
44#include <linux/tty.h>
Alan Coxe1eaea42010-03-26 11:32:54 +000045#include <linux/ctype.h>
46#include <linux/mm.h>
47#include <linux/string.h>
48#include <linux/slab.h>
49#include <linux/poll.h>
50#include <linux/bitops.h>
51#include <linux/file.h>
52#include <linux/uaccess.h>
53#include <linux/module.h>
54#include <linux/timer.h>
55#include <linux/tty_flip.h>
56#include <linux/tty_driver.h>
57#include <linux/serial.h>
58#include <linux/kfifo.h>
59#include <linux/skbuff.h>
Russ Gorbybcd5abe2011-06-16 14:20:12 -070060#include <net/arp.h>
61#include <linux/ip.h>
62#include <linux/netdevice.h>
63#include <linux/etherdevice.h>
Alan Coxe1eaea42010-03-26 11:32:54 +000064#include <linux/gsmmux.h>
65
66static int debug;
67module_param(debug, int, 0600);
68
Alan Coxa8d12002011-11-08 18:02:10 +000069/* Defaults: these are from the specification */
70
71#define T1 10 /* 100mS */
72#define T2 34 /* 333mS */
73#define N2 3 /* Retry 3 times */
Alan Coxe1eaea42010-03-26 11:32:54 +000074
75/* Use long timers for testing at low speed with debug on */
76#ifdef DEBUG_TIMING
Alan Coxa8d12002011-11-08 18:02:10 +000077#define T1 100
78#define T2 200
Alan Coxe1eaea42010-03-26 11:32:54 +000079#endif
80
Alan Cox5f9a31d2010-11-04 15:17:27 +000081/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -030082 * Semi-arbitrary buffer size limits. 0710 is normally run with 32-64 byte
Alan Cox5f9a31d2010-11-04 15:17:27 +000083 * limits so this is plenty
84 */
Russ Gorbybcd5abe2011-06-16 14:20:12 -070085#define MAX_MRU 1500
86#define MAX_MTU 1500
87#define GSM_NET_TX_TIMEOUT (HZ*10)
88
89/**
90 * struct gsm_mux_net - network interface
91 * @struct gsm_dlci* dlci
92 * @struct net_device_stats stats;
93 *
94 * Created when net interface is initialized.
95 **/
96struct gsm_mux_net {
97 struct kref ref;
98 struct gsm_dlci *dlci;
99 struct net_device_stats stats;
100};
101
102#define STATS(net) (((struct gsm_mux_net *)netdev_priv(net))->stats)
Alan Coxe1eaea42010-03-26 11:32:54 +0000103
104/*
105 * Each block of data we have queued to go out is in the form of
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300106 * a gsm_msg which holds everything we need in a link layer independent
Alan Coxe1eaea42010-03-26 11:32:54 +0000107 * format
108 */
109
110struct gsm_msg {
111 struct gsm_msg *next;
112 u8 addr; /* DLCI address + flags */
113 u8 ctrl; /* Control byte + flags */
114 unsigned int len; /* Length of data block (can be zero) */
115 unsigned char *data; /* Points into buffer but not at the start */
116 unsigned char buffer[0];
117};
118
119/*
120 * Each active data link has a gsm_dlci structure associated which ties
121 * the link layer to an optional tty (if the tty side is open). To avoid
122 * complexity right now these are only ever freed up when the mux is
123 * shut down.
124 *
125 * At the moment we don't free DLCI objects until the mux is torn down
126 * this avoid object life time issues but might be worth review later.
127 */
128
129struct gsm_dlci {
130 struct gsm_mux *gsm;
131 int addr;
132 int state;
133#define DLCI_CLOSED 0
134#define DLCI_OPENING 1 /* Sending SABM not seen UA */
135#define DLCI_OPEN 2 /* SABM/UA complete */
136#define DLCI_CLOSING 3 /* Sending DISC not seen UA/DM */
Russ Gorby6ab8fba2011-06-16 14:20:13 -0700137 struct kref ref; /* freed from port or mux close */
Russ Gorbybcd5abe2011-06-16 14:20:12 -0700138 struct mutex mutex;
Alan Coxe1eaea42010-03-26 11:32:54 +0000139
140 /* Link layer */
141 spinlock_t lock; /* Protects the internal state */
142 struct timer_list t1; /* Retransmit timer for SABM and UA */
143 int retries;
144 /* Uplink tty if active */
145 struct tty_port port; /* The tty bound to this DLCI if there is one */
146 struct kfifo *fifo; /* Queue fifo for the DLCI */
147 struct kfifo _fifo; /* For new fifo API porting only */
148 int adaption; /* Adaption layer in use */
Russ Gorbybcd5abe2011-06-16 14:20:12 -0700149 int prev_adaption;
Alan Coxe1eaea42010-03-26 11:32:54 +0000150 u32 modem_rx; /* Our incoming virtual modem lines */
151 u32 modem_tx; /* Our outgoing modem lines */
152 int dead; /* Refuse re-open */
153 /* Flow control */
154 int throttled; /* Private copy of throttle state */
155 int constipated; /* Throttle status for outgoing */
156 /* Packetised I/O */
157 struct sk_buff *skb; /* Frame being sent */
158 struct sk_buff_head skb_list; /* Queued frames */
159 /* Data handling callback */
160 void (*data)(struct gsm_dlci *dlci, u8 *data, int len);
Russ Gorbybcd5abe2011-06-16 14:20:12 -0700161 void (*prev_data)(struct gsm_dlci *dlci, u8 *data, int len);
162 struct net_device *net; /* network interface, if created */
Alan Coxe1eaea42010-03-26 11:32:54 +0000163};
164
165/* DLCI 0, 62/63 are special or reseved see gsmtty_open */
166
167#define NUM_DLCI 64
168
169/*
170 * DLCI 0 is used to pass control blocks out of band of the data
171 * flow (and with a higher link priority). One command can be outstanding
172 * at a time and we use this structure to manage them. They are created
173 * and destroyed by the user context, and updated by the receive paths
174 * and timers
175 */
176
177struct gsm_control {
178 u8 cmd; /* Command we are issuing */
179 u8 *data; /* Data for the command in case we retransmit */
180 int len; /* Length of block for retransmission */
181 int done; /* Done flag */
182 int error; /* Error if any */
183};
184
185/*
186 * Each GSM mux we have is represented by this structure. If we are
187 * operating as an ldisc then we use this structure as our ldisc
188 * state. We need to sort out lifetimes and locking with respect
189 * to the gsm mux array. For now we don't free DLCI objects that
190 * have been instantiated until the mux itself is terminated.
191 *
192 * To consider further: tty open versus mux shutdown.
193 */
194
195struct gsm_mux {
196 struct tty_struct *tty; /* The tty our ldisc is bound to */
197 spinlock_t lock;
Russ Gorbyd50f6dc2011-06-14 13:35:32 -0700198 unsigned int num;
Russ Gorby6ab8fba2011-06-16 14:20:13 -0700199 struct kref ref;
Alan Coxe1eaea42010-03-26 11:32:54 +0000200
201 /* Events on the GSM channel */
202 wait_queue_head_t event;
203
204 /* Bits for GSM mode decoding */
205
206 /* Framing Layer */
207 unsigned char *buf;
208 int state;
209#define GSM_SEARCH 0
210#define GSM_START 1
211#define GSM_ADDRESS 2
212#define GSM_CONTROL 3
213#define GSM_LEN 4
214#define GSM_DATA 5
215#define GSM_FCS 6
216#define GSM_OVERRUN 7
Alan Coxc2f2f002010-11-04 15:17:03 +0000217#define GSM_LEN0 8
218#define GSM_LEN1 9
219#define GSM_SSOF 10
Alan Coxe1eaea42010-03-26 11:32:54 +0000220 unsigned int len;
221 unsigned int address;
222 unsigned int count;
223 int escape;
224 int encoding;
225 u8 control;
226 u8 fcs;
Alan Coxc2f2f002010-11-04 15:17:03 +0000227 u8 received_fcs;
Alan Coxe1eaea42010-03-26 11:32:54 +0000228 u8 *txframe; /* TX framing buffer */
229
230 /* Methods for the receiver side */
231 void (*receive)(struct gsm_mux *gsm, u8 ch);
232 void (*error)(struct gsm_mux *gsm, u8 ch, u8 flag);
233 /* And transmit side */
234 int (*output)(struct gsm_mux *mux, u8 *data, int len);
235
236 /* Link Layer */
237 unsigned int mru;
238 unsigned int mtu;
239 int initiator; /* Did we initiate connection */
240 int dead; /* Has the mux been shut down */
241 struct gsm_dlci *dlci[NUM_DLCI];
242 int constipated; /* Asked by remote to shut up */
243
244 spinlock_t tx_lock;
245 unsigned int tx_bytes; /* TX data outstanding */
246#define TX_THRESH_HI 8192
247#define TX_THRESH_LO 2048
248 struct gsm_msg *tx_head; /* Pending data packets */
249 struct gsm_msg *tx_tail;
250
251 /* Control messages */
252 struct timer_list t2_timer; /* Retransmit timer for commands */
253 int cretries; /* Command retry counter */
254 struct gsm_control *pending_cmd;/* Our current pending command */
255 spinlock_t control_lock; /* Protects the pending command */
256
257 /* Configuration */
258 int adaption; /* 1 or 2 supported */
259 u8 ftype; /* UI or UIH */
260 int t1, t2; /* Timers in 1/100th of a sec */
261 int n2; /* Retry count */
262
263 /* Statistics (not currently exposed) */
264 unsigned long bad_fcs;
265 unsigned long malformed;
266 unsigned long io_error;
267 unsigned long bad_size;
268 unsigned long unsupported;
269};
270
271
272/*
273 * Mux objects - needed so that we can translate a tty index into the
274 * relevant mux and DLCI.
275 */
276
277#define MAX_MUX 4 /* 256 minors */
278static struct gsm_mux *gsm_mux[MAX_MUX]; /* GSM muxes */
279static spinlock_t gsm_mux_lock;
280
Russ Gorbyd50f6dc2011-06-14 13:35:32 -0700281static struct tty_driver *gsm_tty_driver;
282
Alan Coxe1eaea42010-03-26 11:32:54 +0000283/*
284 * This section of the driver logic implements the GSM encodings
285 * both the basic and the 'advanced'. Reliable transport is not
286 * supported.
287 */
288
289#define CR 0x02
290#define EA 0x01
291#define PF 0x10
292
293/* I is special: the rest are ..*/
294#define RR 0x01
295#define UI 0x03
296#define RNR 0x05
297#define REJ 0x09
298#define DM 0x0F
299#define SABM 0x2F
300#define DISC 0x43
301#define UA 0x63
302#define UIH 0xEF
303
304/* Channel commands */
305#define CMD_NSC 0x09
306#define CMD_TEST 0x11
307#define CMD_PSC 0x21
308#define CMD_RLS 0x29
309#define CMD_FCOFF 0x31
310#define CMD_PN 0x41
311#define CMD_RPN 0x49
312#define CMD_FCON 0x51
313#define CMD_CLD 0x61
314#define CMD_SNC 0x69
315#define CMD_MSC 0x71
316
317/* Virtual modem bits */
318#define MDM_FC 0x01
319#define MDM_RTC 0x02
320#define MDM_RTR 0x04
321#define MDM_IC 0x20
322#define MDM_DV 0x40
323
324#define GSM0_SOF 0xF9
Alan Cox5f9a31d2010-11-04 15:17:27 +0000325#define GSM1_SOF 0x7E
Alan Coxe1eaea42010-03-26 11:32:54 +0000326#define GSM1_ESCAPE 0x7D
327#define GSM1_ESCAPE_BITS 0x20
328#define XON 0x11
329#define XOFF 0x13
330
331static const struct tty_port_operations gsm_port_ops;
332
333/*
334 * CRC table for GSM 0710
335 */
336
337static const u8 gsm_fcs8[256] = {
338 0x00, 0x91, 0xE3, 0x72, 0x07, 0x96, 0xE4, 0x75,
339 0x0E, 0x9F, 0xED, 0x7C, 0x09, 0x98, 0xEA, 0x7B,
340 0x1C, 0x8D, 0xFF, 0x6E, 0x1B, 0x8A, 0xF8, 0x69,
341 0x12, 0x83, 0xF1, 0x60, 0x15, 0x84, 0xF6, 0x67,
342 0x38, 0xA9, 0xDB, 0x4A, 0x3F, 0xAE, 0xDC, 0x4D,
343 0x36, 0xA7, 0xD5, 0x44, 0x31, 0xA0, 0xD2, 0x43,
344 0x24, 0xB5, 0xC7, 0x56, 0x23, 0xB2, 0xC0, 0x51,
345 0x2A, 0xBB, 0xC9, 0x58, 0x2D, 0xBC, 0xCE, 0x5F,
346 0x70, 0xE1, 0x93, 0x02, 0x77, 0xE6, 0x94, 0x05,
347 0x7E, 0xEF, 0x9D, 0x0C, 0x79, 0xE8, 0x9A, 0x0B,
348 0x6C, 0xFD, 0x8F, 0x1E, 0x6B, 0xFA, 0x88, 0x19,
349 0x62, 0xF3, 0x81, 0x10, 0x65, 0xF4, 0x86, 0x17,
350 0x48, 0xD9, 0xAB, 0x3A, 0x4F, 0xDE, 0xAC, 0x3D,
351 0x46, 0xD7, 0xA5, 0x34, 0x41, 0xD0, 0xA2, 0x33,
352 0x54, 0xC5, 0xB7, 0x26, 0x53, 0xC2, 0xB0, 0x21,
353 0x5A, 0xCB, 0xB9, 0x28, 0x5D, 0xCC, 0xBE, 0x2F,
354 0xE0, 0x71, 0x03, 0x92, 0xE7, 0x76, 0x04, 0x95,
355 0xEE, 0x7F, 0x0D, 0x9C, 0xE9, 0x78, 0x0A, 0x9B,
356 0xFC, 0x6D, 0x1F, 0x8E, 0xFB, 0x6A, 0x18, 0x89,
357 0xF2, 0x63, 0x11, 0x80, 0xF5, 0x64, 0x16, 0x87,
358 0xD8, 0x49, 0x3B, 0xAA, 0xDF, 0x4E, 0x3C, 0xAD,
359 0xD6, 0x47, 0x35, 0xA4, 0xD1, 0x40, 0x32, 0xA3,
360 0xC4, 0x55, 0x27, 0xB6, 0xC3, 0x52, 0x20, 0xB1,
361 0xCA, 0x5B, 0x29, 0xB8, 0xCD, 0x5C, 0x2E, 0xBF,
362 0x90, 0x01, 0x73, 0xE2, 0x97, 0x06, 0x74, 0xE5,
363 0x9E, 0x0F, 0x7D, 0xEC, 0x99, 0x08, 0x7A, 0xEB,
364 0x8C, 0x1D, 0x6F, 0xFE, 0x8B, 0x1A, 0x68, 0xF9,
365 0x82, 0x13, 0x61, 0xF0, 0x85, 0x14, 0x66, 0xF7,
366 0xA8, 0x39, 0x4B, 0xDA, 0xAF, 0x3E, 0x4C, 0xDD,
367 0xA6, 0x37, 0x45, 0xD4, 0xA1, 0x30, 0x42, 0xD3,
368 0xB4, 0x25, 0x57, 0xC6, 0xB3, 0x22, 0x50, 0xC1,
369 0xBA, 0x2B, 0x59, 0xC8, 0xBD, 0x2C, 0x5E, 0xCF
370};
371
372#define INIT_FCS 0xFF
373#define GOOD_FCS 0xCF
374
375/**
376 * gsm_fcs_add - update FCS
377 * @fcs: Current FCS
378 * @c: Next data
379 *
380 * Update the FCS to include c. Uses the algorithm in the specification
381 * notes.
382 */
383
384static inline u8 gsm_fcs_add(u8 fcs, u8 c)
385{
386 return gsm_fcs8[fcs ^ c];
387}
388
389/**
390 * gsm_fcs_add_block - update FCS for a block
391 * @fcs: Current FCS
392 * @c: buffer of data
393 * @len: length of buffer
394 *
395 * Update the FCS to include c. Uses the algorithm in the specification
396 * notes.
397 */
398
399static inline u8 gsm_fcs_add_block(u8 fcs, u8 *c, int len)
400{
401 while (len--)
402 fcs = gsm_fcs8[fcs ^ *c++];
403 return fcs;
404}
405
406/**
407 * gsm_read_ea - read a byte into an EA
408 * @val: variable holding value
409 * c: byte going into the EA
410 *
411 * Processes one byte of an EA. Updates the passed variable
412 * and returns 1 if the EA is now completely read
413 */
414
415static int gsm_read_ea(unsigned int *val, u8 c)
416{
417 /* Add the next 7 bits into the value */
418 *val <<= 7;
419 *val |= c >> 1;
420 /* Was this the last byte of the EA 1 = yes*/
421 return c & EA;
422}
423
424/**
425 * gsm_encode_modem - encode modem data bits
426 * @dlci: DLCI to encode from
427 *
428 * Returns the correct GSM encoded modem status bits (6 bit field) for
429 * the current status of the DLCI and attached tty object
430 */
431
432static u8 gsm_encode_modem(const struct gsm_dlci *dlci)
433{
434 u8 modembits = 0;
435 /* FC is true flow control not modem bits */
436 if (dlci->throttled)
437 modembits |= MDM_FC;
438 if (dlci->modem_tx & TIOCM_DTR)
439 modembits |= MDM_RTC;
440 if (dlci->modem_tx & TIOCM_RTS)
441 modembits |= MDM_RTR;
442 if (dlci->modem_tx & TIOCM_RI)
443 modembits |= MDM_IC;
444 if (dlci->modem_tx & TIOCM_CD)
445 modembits |= MDM_DV;
446 return modembits;
447}
448
449/**
450 * gsm_print_packet - display a frame for debug
451 * @hdr: header to print before decode
452 * @addr: address EA from the frame
453 * @cr: C/R bit from the frame
454 * @control: control including PF bit
455 * @data: following data bytes
456 * @dlen: length of data
457 *
458 * Displays a packet in human readable format for debugging purposes. The
459 * style is based on amateur radio LAP-B dump display.
460 */
461
462static void gsm_print_packet(const char *hdr, int addr, int cr,
463 u8 control, const u8 *data, int dlen)
464{
465 if (!(debug & 1))
466 return;
467
Alan Cox5f9a31d2010-11-04 15:17:27 +0000468 pr_info("%s %d) %c: ", hdr, addr, "RC"[cr]);
Alan Coxe1eaea42010-03-26 11:32:54 +0000469
470 switch (control & ~PF) {
471 case SABM:
Alan Cox5f9a31d2010-11-04 15:17:27 +0000472 pr_cont("SABM");
Alan Coxe1eaea42010-03-26 11:32:54 +0000473 break;
474 case UA:
Alan Cox5f9a31d2010-11-04 15:17:27 +0000475 pr_cont("UA");
Alan Coxe1eaea42010-03-26 11:32:54 +0000476 break;
477 case DISC:
Alan Cox5f9a31d2010-11-04 15:17:27 +0000478 pr_cont("DISC");
Alan Coxe1eaea42010-03-26 11:32:54 +0000479 break;
480 case DM:
Alan Cox5f9a31d2010-11-04 15:17:27 +0000481 pr_cont("DM");
Alan Coxe1eaea42010-03-26 11:32:54 +0000482 break;
483 case UI:
Alan Cox5f9a31d2010-11-04 15:17:27 +0000484 pr_cont("UI");
Alan Coxe1eaea42010-03-26 11:32:54 +0000485 break;
486 case UIH:
Alan Cox5f9a31d2010-11-04 15:17:27 +0000487 pr_cont("UIH");
Alan Coxe1eaea42010-03-26 11:32:54 +0000488 break;
489 default:
490 if (!(control & 0x01)) {
Alan Cox5f9a31d2010-11-04 15:17:27 +0000491 pr_cont("I N(S)%d N(R)%d",
492 (control & 0x0E) >> 1, (control & 0xE) >> 5);
Alan Coxe1eaea42010-03-26 11:32:54 +0000493 } else switch (control & 0x0F) {
Alan Cox5f9a31d2010-11-04 15:17:27 +0000494 case RR:
495 pr_cont("RR(%d)", (control & 0xE0) >> 5);
496 break;
497 case RNR:
498 pr_cont("RNR(%d)", (control & 0xE0) >> 5);
499 break;
500 case REJ:
501 pr_cont("REJ(%d)", (control & 0xE0) >> 5);
502 break;
503 default:
504 pr_cont("[%02X]", control);
Alan Coxe1eaea42010-03-26 11:32:54 +0000505 }
506 }
507
508 if (control & PF)
Alan Cox5f9a31d2010-11-04 15:17:27 +0000509 pr_cont("(P)");
Alan Coxe1eaea42010-03-26 11:32:54 +0000510 else
Alan Cox5f9a31d2010-11-04 15:17:27 +0000511 pr_cont("(F)");
Alan Coxe1eaea42010-03-26 11:32:54 +0000512
513 if (dlen) {
514 int ct = 0;
515 while (dlen--) {
Alan Cox5f9a31d2010-11-04 15:17:27 +0000516 if (ct % 8 == 0) {
517 pr_cont("\n");
518 pr_debug(" ");
519 }
520 pr_cont("%02X ", *data++);
Alan Coxe1eaea42010-03-26 11:32:54 +0000521 ct++;
522 }
523 }
Alan Cox5f9a31d2010-11-04 15:17:27 +0000524 pr_cont("\n");
Alan Coxe1eaea42010-03-26 11:32:54 +0000525}
526
527
528/*
529 * Link level transmission side
530 */
531
532/**
533 * gsm_stuff_packet - bytestuff a packet
534 * @ibuf: input
535 * @obuf: output
536 * @len: length of input
537 *
538 * Expand a buffer by bytestuffing it. The worst case size change
539 * is doubling and the caller is responsible for handing out
540 * suitable sized buffers.
541 */
542
543static int gsm_stuff_frame(const u8 *input, u8 *output, int len)
544{
545 int olen = 0;
546 while (len--) {
547 if (*input == GSM1_SOF || *input == GSM1_ESCAPE
548 || *input == XON || *input == XOFF) {
549 *output++ = GSM1_ESCAPE;
550 *output++ = *input++ ^ GSM1_ESCAPE_BITS;
551 olen++;
552 } else
553 *output++ = *input++;
554 olen++;
555 }
556 return olen;
557}
558
Alan Coxe1eaea42010-03-26 11:32:54 +0000559/**
560 * gsm_send - send a control frame
561 * @gsm: our GSM mux
562 * @addr: address for control frame
563 * @cr: command/response bit
564 * @control: control byte including PF bit
565 *
566 * Format up and transmit a control frame. These do not go via the
567 * queueing logic as they should be transmitted ahead of data when
568 * they are needed.
569 *
570 * FIXME: Lock versus data TX path
571 */
572
573static void gsm_send(struct gsm_mux *gsm, int addr, int cr, int control)
574{
575 int len;
576 u8 cbuf[10];
577 u8 ibuf[3];
578
579 switch (gsm->encoding) {
580 case 0:
581 cbuf[0] = GSM0_SOF;
582 cbuf[1] = (addr << 2) | (cr << 1) | EA;
583 cbuf[2] = control;
584 cbuf[3] = EA; /* Length of data = 0 */
585 cbuf[4] = 0xFF - gsm_fcs_add_block(INIT_FCS, cbuf + 1, 3);
586 cbuf[5] = GSM0_SOF;
587 len = 6;
588 break;
589 case 1:
590 case 2:
591 /* Control frame + packing (but not frame stuffing) in mode 1 */
592 ibuf[0] = (addr << 2) | (cr << 1) | EA;
593 ibuf[1] = control;
594 ibuf[2] = 0xFF - gsm_fcs_add_block(INIT_FCS, ibuf, 2);
595 /* Stuffing may double the size worst case */
596 len = gsm_stuff_frame(ibuf, cbuf + 1, 3);
597 /* Now add the SOF markers */
598 cbuf[0] = GSM1_SOF;
599 cbuf[len + 1] = GSM1_SOF;
600 /* FIXME: we can omit the lead one in many cases */
601 len += 2;
602 break;
603 default:
604 WARN_ON(1);
605 return;
606 }
607 gsm->output(gsm, cbuf, len);
608 gsm_print_packet("-->", addr, cr, control, NULL, 0);
609}
610
611/**
612 * gsm_response - send a control response
613 * @gsm: our GSM mux
614 * @addr: address for control frame
615 * @control: control byte including PF bit
616 *
617 * Format up and transmit a link level response frame.
618 */
619
620static inline void gsm_response(struct gsm_mux *gsm, int addr, int control)
621{
622 gsm_send(gsm, addr, 0, control);
623}
624
625/**
626 * gsm_command - send a control command
627 * @gsm: our GSM mux
628 * @addr: address for control frame
629 * @control: control byte including PF bit
630 *
631 * Format up and transmit a link level command frame.
632 */
633
634static inline void gsm_command(struct gsm_mux *gsm, int addr, int control)
635{
636 gsm_send(gsm, addr, 1, control);
637}
638
639/* Data transmission */
640
641#define HDR_LEN 6 /* ADDR CTRL [LEN.2] DATA FCS */
642
643/**
644 * gsm_data_alloc - allocate data frame
645 * @gsm: GSM mux
646 * @addr: DLCI address
647 * @len: length excluding header and FCS
648 * @ctrl: control byte
649 *
650 * Allocate a new data buffer for sending frames with data. Space is left
651 * at the front for header bytes but that is treated as an implementation
652 * detail and not for the high level code to use
653 */
654
655static struct gsm_msg *gsm_data_alloc(struct gsm_mux *gsm, u8 addr, int len,
656 u8 ctrl)
657{
658 struct gsm_msg *m = kmalloc(sizeof(struct gsm_msg) + len + HDR_LEN,
659 GFP_ATOMIC);
660 if (m == NULL)
661 return NULL;
662 m->data = m->buffer + HDR_LEN - 1; /* Allow for FCS */
663 m->len = len;
664 m->addr = addr;
665 m->ctrl = ctrl;
666 m->next = NULL;
667 return m;
668}
669
670/**
671 * gsm_data_kick - poke the queue
672 * @gsm: GSM Mux
673 *
674 * The tty device has called us to indicate that room has appeared in
675 * the transmit queue. Ram more data into the pipe if we have any
Frederic Beratc01af4f2012-08-13 13:43:58 +0100676 * If we have been flow-stopped by a CMD_FCOFF, then we can only
677 * send messages on DLCI0 until CMD_FCON
Alan Coxe1eaea42010-03-26 11:32:54 +0000678 *
679 * FIXME: lock against link layer control transmissions
680 */
681
682static void gsm_data_kick(struct gsm_mux *gsm)
683{
684 struct gsm_msg *msg = gsm->tx_head;
Frederic Beratc01af4f2012-08-13 13:43:58 +0100685 struct gsm_msg *free_msg;
Alan Coxe1eaea42010-03-26 11:32:54 +0000686 int len;
687 int skip_sof = 0;
688
Frederic Beratc01af4f2012-08-13 13:43:58 +0100689 while (msg) {
690 if (gsm->constipated && msg->addr) {
691 msg = msg->next;
692 continue;
693 }
Alan Coxe1eaea42010-03-26 11:32:54 +0000694 if (gsm->encoding != 0) {
695 gsm->txframe[0] = GSM1_SOF;
696 len = gsm_stuff_frame(msg->data,
697 gsm->txframe + 1, msg->len);
698 gsm->txframe[len + 1] = GSM1_SOF;
699 len += 2;
700 } else {
701 gsm->txframe[0] = GSM0_SOF;
702 memcpy(gsm->txframe + 1 , msg->data, msg->len);
703 gsm->txframe[msg->len + 1] = GSM0_SOF;
704 len = msg->len + 2;
705 }
706
Joe Perches0a77c4f2011-04-25 16:46:49 -0700707 if (debug & 4)
708 print_hex_dump_bytes("gsm_data_kick: ",
709 DUMP_PREFIX_OFFSET,
710 gsm->txframe, len);
Alan Coxe1eaea42010-03-26 11:32:54 +0000711
712 if (gsm->output(gsm, gsm->txframe + skip_sof,
713 len - skip_sof) < 0)
714 break;
715 /* FIXME: Can eliminate one SOF in many more cases */
Alan Coxe1eaea42010-03-26 11:32:54 +0000716 gsm->tx_bytes -= msg->len;
Alan Coxe1eaea42010-03-26 11:32:54 +0000717 /* For a burst of frames skip the extra SOF within the
718 burst */
719 skip_sof = 1;
Frederic Beratc01af4f2012-08-13 13:43:58 +0100720
721 if (gsm->tx_head == msg)
722 gsm->tx_head = msg->next;
723 free_msg = msg;
724 msg = msg->next;
725 kfree(free_msg);
Alan Coxe1eaea42010-03-26 11:32:54 +0000726 }
Frederic Beratc01af4f2012-08-13 13:43:58 +0100727 if (!gsm->tx_head)
728 gsm->tx_tail = NULL;
Alan Coxe1eaea42010-03-26 11:32:54 +0000729}
730
731/**
732 * __gsm_data_queue - queue a UI or UIH frame
733 * @dlci: DLCI sending the data
734 * @msg: message queued
735 *
736 * Add data to the transmit queue and try and get stuff moving
737 * out of the mux tty if not already doing so. The Caller must hold
738 * the gsm tx lock.
739 */
740
741static void __gsm_data_queue(struct gsm_dlci *dlci, struct gsm_msg *msg)
742{
743 struct gsm_mux *gsm = dlci->gsm;
744 u8 *dp = msg->data;
745 u8 *fcs = dp + msg->len;
746
747 /* Fill in the header */
748 if (gsm->encoding == 0) {
749 if (msg->len < 128)
750 *--dp = (msg->len << 1) | EA;
751 else {
Ken Millsbe7a7412010-12-13 15:27:27 +0000752 *--dp = (msg->len >> 7); /* bits 7 - 15 */
753 *--dp = (msg->len & 127) << 1; /* bits 0 - 6 */
Alan Coxe1eaea42010-03-26 11:32:54 +0000754 }
755 }
756
757 *--dp = msg->ctrl;
758 if (gsm->initiator)
759 *--dp = (msg->addr << 2) | 2 | EA;
760 else
761 *--dp = (msg->addr << 2) | EA;
762 *fcs = gsm_fcs_add_block(INIT_FCS, dp , msg->data - dp);
763 /* Ugly protocol layering violation */
764 if (msg->ctrl == UI || msg->ctrl == (UI|PF))
765 *fcs = gsm_fcs_add_block(*fcs, msg->data, msg->len);
766 *fcs = 0xFF - *fcs;
767
768 gsm_print_packet("Q> ", msg->addr, gsm->initiator, msg->ctrl,
769 msg->data, msg->len);
770
771 /* Move the header back and adjust the length, also allow for the FCS
772 now tacked on the end */
773 msg->len += (msg->data - dp) + 1;
774 msg->data = dp;
775
776 /* Add to the actual output queue */
777 if (gsm->tx_tail)
778 gsm->tx_tail->next = msg;
779 else
780 gsm->tx_head = msg;
781 gsm->tx_tail = msg;
782 gsm->tx_bytes += msg->len;
783 gsm_data_kick(gsm);
784}
785
786/**
787 * gsm_data_queue - queue a UI or UIH frame
788 * @dlci: DLCI sending the data
789 * @msg: message queued
790 *
791 * Add data to the transmit queue and try and get stuff moving
792 * out of the mux tty if not already doing so. Take the
793 * the gsm tx lock and dlci lock.
794 */
795
796static void gsm_data_queue(struct gsm_dlci *dlci, struct gsm_msg *msg)
797{
798 unsigned long flags;
799 spin_lock_irqsave(&dlci->gsm->tx_lock, flags);
800 __gsm_data_queue(dlci, msg);
801 spin_unlock_irqrestore(&dlci->gsm->tx_lock, flags);
802}
803
804/**
805 * gsm_dlci_data_output - try and push data out of a DLCI
806 * @gsm: mux
807 * @dlci: the DLCI to pull data from
808 *
809 * Pull data from a DLCI and send it into the transmit queue if there
810 * is data. Keep to the MRU of the mux. This path handles the usual tty
811 * interface which is a byte stream with optional modem data.
812 *
813 * Caller must hold the tx_lock of the mux.
814 */
815
816static int gsm_dlci_data_output(struct gsm_mux *gsm, struct gsm_dlci *dlci)
817{
818 struct gsm_msg *msg;
819 u8 *dp;
Mikhail Kshevetskiy268e5262011-09-23 19:22:56 +0400820 int len, total_size, size;
Alan Coxe1eaea42010-03-26 11:32:54 +0000821 int h = dlci->adaption - 1;
822
Mikhail Kshevetskiy268e5262011-09-23 19:22:56 +0400823 total_size = 0;
824 while(1) {
825 len = kfifo_len(dlci->fifo);
826 if (len == 0)
827 return total_size;
Alan Coxe1eaea42010-03-26 11:32:54 +0000828
Mikhail Kshevetskiy268e5262011-09-23 19:22:56 +0400829 /* MTU/MRU count only the data bits */
830 if (len > gsm->mtu)
831 len = gsm->mtu;
Alan Coxe1eaea42010-03-26 11:32:54 +0000832
Mikhail Kshevetskiy268e5262011-09-23 19:22:56 +0400833 size = len + h;
Alan Coxe1eaea42010-03-26 11:32:54 +0000834
Mikhail Kshevetskiy268e5262011-09-23 19:22:56 +0400835 msg = gsm_data_alloc(gsm, dlci->addr, size, gsm->ftype);
836 /* FIXME: need a timer or something to kick this so it can't
837 get stuck with no work outstanding and no buffer free */
838 if (msg == NULL)
839 return -ENOMEM;
840 dp = msg->data;
841 switch (dlci->adaption) {
842 case 1: /* Unstructured */
843 break;
844 case 2: /* Unstructed with modem bits. Always one byte as we never
845 send inline break data */
846 *dp++ = gsm_encode_modem(dlci);
847 break;
848 }
849 WARN_ON(kfifo_out_locked(dlci->fifo, dp , len, &dlci->lock) != len);
850 __gsm_data_queue(dlci, msg);
851 total_size += size;
Alan Coxe1eaea42010-03-26 11:32:54 +0000852 }
Alan Coxe1eaea42010-03-26 11:32:54 +0000853 /* Bytes of data we used up */
Mikhail Kshevetskiy268e5262011-09-23 19:22:56 +0400854 return total_size;
Alan Coxe1eaea42010-03-26 11:32:54 +0000855}
856
857/**
858 * gsm_dlci_data_output_framed - try and push data out of a DLCI
859 * @gsm: mux
860 * @dlci: the DLCI to pull data from
861 *
862 * Pull data from a DLCI and send it into the transmit queue if there
863 * is data. Keep to the MRU of the mux. This path handles framed data
864 * queued as skbuffs to the DLCI.
865 *
866 * Caller must hold the tx_lock of the mux.
867 */
868
869static int gsm_dlci_data_output_framed(struct gsm_mux *gsm,
870 struct gsm_dlci *dlci)
871{
872 struct gsm_msg *msg;
873 u8 *dp;
874 int len, size;
875 int last = 0, first = 0;
876 int overhead = 0;
877
878 /* One byte per frame is used for B/F flags */
879 if (dlci->adaption == 4)
880 overhead = 1;
881
882 /* dlci->skb is locked by tx_lock */
883 if (dlci->skb == NULL) {
884 dlci->skb = skb_dequeue(&dlci->skb_list);
885 if (dlci->skb == NULL)
886 return 0;
887 first = 1;
888 }
889 len = dlci->skb->len + overhead;
890
891 /* MTU/MRU count only the data bits */
892 if (len > gsm->mtu) {
893 if (dlci->adaption == 3) {
894 /* Over long frame, bin it */
895 kfree_skb(dlci->skb);
896 dlci->skb = NULL;
897 return 0;
898 }
899 len = gsm->mtu;
900 } else
901 last = 1;
902
903 size = len + overhead;
904 msg = gsm_data_alloc(gsm, dlci->addr, size, gsm->ftype);
905
906 /* FIXME: need a timer or something to kick this so it can't
907 get stuck with no work outstanding and no buffer free */
908 if (msg == NULL)
909 return -ENOMEM;
910 dp = msg->data;
911
912 if (dlci->adaption == 4) { /* Interruptible framed (Packetised Data) */
913 /* Flag byte to carry the start/end info */
914 *dp++ = last << 7 | first << 6 | 1; /* EA */
915 len--;
916 }
Russ Gorby57f21042011-06-14 13:23:29 -0700917 memcpy(dp, dlci->skb->data, len);
918 skb_pull(dlci->skb, len);
Alan Coxe1eaea42010-03-26 11:32:54 +0000919 __gsm_data_queue(dlci, msg);
Russ Gorbybcd5abe2011-06-16 14:20:12 -0700920 if (last) {
921 kfree_skb(dlci->skb);
Alan Coxe1eaea42010-03-26 11:32:54 +0000922 dlci->skb = NULL;
Russ Gorbybcd5abe2011-06-16 14:20:12 -0700923 }
Alan Coxe1eaea42010-03-26 11:32:54 +0000924 return size;
925}
926
927/**
928 * gsm_dlci_data_sweep - look for data to send
929 * @gsm: the GSM mux
930 *
931 * Sweep the GSM mux channels in priority order looking for ones with
932 * data to send. We could do with optimising this scan a bit. We aim
933 * to fill the queue totally or up to TX_THRESH_HI bytes. Once we hit
934 * TX_THRESH_LO we get called again
935 *
936 * FIXME: We should round robin between groups and in theory you can
937 * renegotiate DLCI priorities with optional stuff. Needs optimising.
938 */
939
940static void gsm_dlci_data_sweep(struct gsm_mux *gsm)
941{
942 int len;
943 /* Priority ordering: We should do priority with RR of the groups */
944 int i = 1;
Alan Coxe1eaea42010-03-26 11:32:54 +0000945
Alan Coxe1eaea42010-03-26 11:32:54 +0000946 while (i < NUM_DLCI) {
947 struct gsm_dlci *dlci;
948
949 if (gsm->tx_bytes > TX_THRESH_HI)
950 break;
951 dlci = gsm->dlci[i];
952 if (dlci == NULL || dlci->constipated) {
953 i++;
954 continue;
955 }
Russ Gorbybcd5abe2011-06-16 14:20:12 -0700956 if (dlci->adaption < 3 && !dlci->net)
Alan Coxe1eaea42010-03-26 11:32:54 +0000957 len = gsm_dlci_data_output(gsm, dlci);
958 else
959 len = gsm_dlci_data_output_framed(gsm, dlci);
960 if (len < 0)
Julia Lawalle73790a2010-08-10 18:03:12 -0700961 break;
Alan Coxe1eaea42010-03-26 11:32:54 +0000962 /* DLCI empty - try the next */
963 if (len == 0)
964 i++;
965 }
Alan Coxe1eaea42010-03-26 11:32:54 +0000966}
967
968/**
969 * gsm_dlci_data_kick - transmit if possible
970 * @dlci: DLCI to kick
971 *
972 * Transmit data from this DLCI if the queue is empty. We can't rely on
973 * a tty wakeup except when we filled the pipe so we need to fire off
974 * new data ourselves in other cases.
975 */
976
977static void gsm_dlci_data_kick(struct gsm_dlci *dlci)
978{
979 unsigned long flags;
Russ Gorby192b6042012-08-13 13:43:36 +0100980 int sweep;
Alan Coxe1eaea42010-03-26 11:32:54 +0000981
samix.lebsir10c6c382012-08-13 13:44:22 +0100982 if (dlci->constipated)
Frederic Beratc01af4f2012-08-13 13:43:58 +0100983 return;
Frederic Beratc01af4f2012-08-13 13:43:58 +0100984
Alan Coxe1eaea42010-03-26 11:32:54 +0000985 spin_lock_irqsave(&dlci->gsm->tx_lock, flags);
986 /* If we have nothing running then we need to fire up */
Russ Gorby192b6042012-08-13 13:43:36 +0100987 sweep = (dlci->gsm->tx_bytes < TX_THRESH_LO);
Russ Gorbybcd5abe2011-06-16 14:20:12 -0700988 if (dlci->gsm->tx_bytes == 0) {
989 if (dlci->net)
990 gsm_dlci_data_output_framed(dlci->gsm, dlci);
991 else
992 gsm_dlci_data_output(dlci->gsm, dlci);
Russ Gorby192b6042012-08-13 13:43:36 +0100993 }
994 if (sweep)
995 gsm_dlci_data_sweep(dlci->gsm);
Alan Coxe1eaea42010-03-26 11:32:54 +0000996 spin_unlock_irqrestore(&dlci->gsm->tx_lock, flags);
997}
998
999/*
1000 * Control message processing
1001 */
1002
1003
1004/**
1005 * gsm_control_reply - send a response frame to a control
1006 * @gsm: gsm channel
1007 * @cmd: the command to use
1008 * @data: data to follow encoded info
1009 * @dlen: length of data
1010 *
1011 * Encode up and queue a UI/UIH frame containing our response.
1012 */
1013
1014static void gsm_control_reply(struct gsm_mux *gsm, int cmd, u8 *data,
1015 int dlen)
1016{
1017 struct gsm_msg *msg;
1018 msg = gsm_data_alloc(gsm, 0, dlen + 2, gsm->ftype);
Ken Mills093d8042010-12-13 15:28:03 +00001019 if (msg == NULL)
1020 return;
Alan Coxe1eaea42010-03-26 11:32:54 +00001021 msg->data[0] = (cmd & 0xFE) << 1 | EA; /* Clear C/R */
1022 msg->data[1] = (dlen << 1) | EA;
1023 memcpy(msg->data + 2, data, dlen);
1024 gsm_data_queue(gsm->dlci[0], msg);
1025}
1026
1027/**
1028 * gsm_process_modem - process received modem status
1029 * @tty: virtual tty bound to the DLCI
1030 * @dlci: DLCI to affect
1031 * @modem: modem bits (full EA)
1032 *
1033 * Used when a modem control message or line state inline in adaption
1034 * layer 2 is processed. Sort out the local modem state and throttles
1035 */
1036
1037static void gsm_process_modem(struct tty_struct *tty, struct gsm_dlci *dlci,
Russ Gorby72632872011-06-14 13:23:28 -07001038 u32 modem, int clen)
Alan Coxe1eaea42010-03-26 11:32:54 +00001039{
1040 int mlines = 0;
Russ Gorby72632872011-06-14 13:23:28 -07001041 u8 brk = 0;
Frederic Beratc01af4f2012-08-13 13:43:58 +01001042 int fc;
Russ Gorby72632872011-06-14 13:23:28 -07001043
1044 /* The modem status command can either contain one octet (v.24 signals)
1045 or two octets (v.24 signals + break signals). The length field will
1046 either be 2 or 3 respectively. This is specified in section
1047 5.4.6.3.7 of the 27.010 mux spec. */
1048
1049 if (clen == 2)
1050 modem = modem & 0x7f;
1051 else {
1052 brk = modem & 0x7f;
1053 modem = (modem >> 7) & 0x7f;
Frederic Beratc01af4f2012-08-13 13:43:58 +01001054 }
Alan Coxe1eaea42010-03-26 11:32:54 +00001055
1056 /* Flow control/ready to communicate */
Frederic Beratc01af4f2012-08-13 13:43:58 +01001057 fc = (modem & MDM_FC) || !(modem & MDM_RTR);
1058 if (fc && !dlci->constipated) {
Alan Coxe1eaea42010-03-26 11:32:54 +00001059 /* Need to throttle our output on this device */
1060 dlci->constipated = 1;
Frederic Beratc01af4f2012-08-13 13:43:58 +01001061 } else if (!fc && dlci->constipated) {
Alan Coxe1eaea42010-03-26 11:32:54 +00001062 dlci->constipated = 0;
1063 gsm_dlci_data_kick(dlci);
1064 }
Frederic Beratc01af4f2012-08-13 13:43:58 +01001065
Alan Coxe1eaea42010-03-26 11:32:54 +00001066 /* Map modem bits */
Frederic Beratc01af4f2012-08-13 13:43:58 +01001067 if (modem & MDM_RTC)
1068 mlines |= TIOCM_DSR | TIOCM_DTR;
Alan Coxe1eaea42010-03-26 11:32:54 +00001069 if (modem & MDM_RTR)
1070 mlines |= TIOCM_RTS | TIOCM_CTS;
1071 if (modem & MDM_IC)
1072 mlines |= TIOCM_RI;
1073 if (modem & MDM_DV)
1074 mlines |= TIOCM_CD;
1075
1076 /* Carrier drop -> hangup */
1077 if (tty) {
1078 if ((mlines & TIOCM_CD) == 0 && (dlci->modem_rx & TIOCM_CD))
Alan Coxadc8d742012-07-14 15:31:47 +01001079 if (!(tty->termios.c_cflag & CLOCAL))
Alan Coxe1eaea42010-03-26 11:32:54 +00001080 tty_hangup(tty);
1081 if (brk & 0x01)
1082 tty_insert_flip_char(tty, 0, TTY_BREAK);
1083 }
1084 dlci->modem_rx = mlines;
1085}
1086
1087/**
1088 * gsm_control_modem - modem status received
1089 * @gsm: GSM channel
1090 * @data: data following command
1091 * @clen: command length
1092 *
1093 * We have received a modem status control message. This is used by
1094 * the GSM mux protocol to pass virtual modem line status and optionally
1095 * to indicate break signals. Unpack it, convert to Linux representation
1096 * and if need be stuff a break message down the tty.
1097 */
1098
1099static void gsm_control_modem(struct gsm_mux *gsm, u8 *data, int clen)
1100{
1101 unsigned int addr = 0;
1102 unsigned int modem = 0;
1103 struct gsm_dlci *dlci;
1104 int len = clen;
1105 u8 *dp = data;
1106 struct tty_struct *tty;
1107
1108 while (gsm_read_ea(&addr, *dp++) == 0) {
1109 len--;
1110 if (len == 0)
1111 return;
1112 }
1113 /* Must be at least one byte following the EA */
1114 len--;
1115 if (len <= 0)
1116 return;
1117
1118 addr >>= 1;
1119 /* Closed port, or invalid ? */
1120 if (addr == 0 || addr >= NUM_DLCI || gsm->dlci[addr] == NULL)
1121 return;
1122 dlci = gsm->dlci[addr];
1123
1124 while (gsm_read_ea(&modem, *dp++) == 0) {
1125 len--;
1126 if (len == 0)
1127 return;
1128 }
1129 tty = tty_port_tty_get(&dlci->port);
Russ Gorby72632872011-06-14 13:23:28 -07001130 gsm_process_modem(tty, dlci, modem, clen);
Alan Coxe1eaea42010-03-26 11:32:54 +00001131 if (tty) {
1132 tty_wakeup(tty);
1133 tty_kref_put(tty);
1134 }
1135 gsm_control_reply(gsm, CMD_MSC, data, clen);
1136}
1137
1138/**
1139 * gsm_control_rls - remote line status
1140 * @gsm: GSM channel
1141 * @data: data bytes
1142 * @clen: data length
1143 *
1144 * The modem sends us a two byte message on the control channel whenever
1145 * it wishes to send us an error state from the virtual link. Stuff
1146 * this into the uplink tty if present
1147 */
1148
1149static void gsm_control_rls(struct gsm_mux *gsm, u8 *data, int clen)
1150{
1151 struct tty_struct *tty;
1152 unsigned int addr = 0 ;
1153 u8 bits;
1154 int len = clen;
1155 u8 *dp = data;
1156
1157 while (gsm_read_ea(&addr, *dp++) == 0) {
1158 len--;
1159 if (len == 0)
1160 return;
1161 }
1162 /* Must be at least one byte following ea */
1163 len--;
1164 if (len <= 0)
1165 return;
1166 addr >>= 1;
1167 /* Closed port, or invalid ? */
1168 if (addr == 0 || addr >= NUM_DLCI || gsm->dlci[addr] == NULL)
1169 return;
1170 /* No error ? */
1171 bits = *dp;
1172 if ((bits & 1) == 0)
1173 return;
1174 /* See if we have an uplink tty */
1175 tty = tty_port_tty_get(&gsm->dlci[addr]->port);
1176
1177 if (tty) {
1178 if (bits & 2)
1179 tty_insert_flip_char(tty, 0, TTY_OVERRUN);
1180 if (bits & 4)
1181 tty_insert_flip_char(tty, 0, TTY_PARITY);
1182 if (bits & 8)
1183 tty_insert_flip_char(tty, 0, TTY_FRAME);
1184 tty_flip_buffer_push(tty);
1185 tty_kref_put(tty);
1186 }
1187 gsm_control_reply(gsm, CMD_RLS, data, clen);
1188}
1189
1190static void gsm_dlci_begin_close(struct gsm_dlci *dlci);
1191
1192/**
1193 * gsm_control_message - DLCI 0 control processing
1194 * @gsm: our GSM mux
1195 * @command: the command EA
1196 * @data: data beyond the command/length EAs
1197 * @clen: length
1198 *
1199 * Input processor for control messages from the other end of the link.
1200 * Processes the incoming request and queues a response frame or an
1201 * NSC response if not supported
1202 */
1203
1204static void gsm_control_message(struct gsm_mux *gsm, unsigned int command,
1205 u8 *data, int clen)
1206{
1207 u8 buf[1];
1208 switch (command) {
1209 case CMD_CLD: {
1210 struct gsm_dlci *dlci = gsm->dlci[0];
1211 /* Modem wishes to close down */
1212 if (dlci) {
1213 dlci->dead = 1;
1214 gsm->dead = 1;
1215 gsm_dlci_begin_close(dlci);
1216 }
1217 }
1218 break;
1219 case CMD_TEST:
1220 /* Modem wishes to test, reply with the data */
1221 gsm_control_reply(gsm, CMD_TEST, data, clen);
1222 break;
1223 case CMD_FCON:
Alan Coxe1eaea42010-03-26 11:32:54 +00001224 /* Modem can accept data again */
1225 gsm->constipated = 0;
Frederic Beratc01af4f2012-08-13 13:43:58 +01001226 gsm_control_reply(gsm, CMD_FCON, NULL, 0);
Alan Coxe1eaea42010-03-26 11:32:54 +00001227 /* Kick the link in case it is idling */
1228 gsm_data_kick(gsm);
1229 break;
Frederic Beratc01af4f2012-08-13 13:43:58 +01001230 case CMD_FCOFF:
1231 /* Modem wants us to STFU */
Frederic Beratc01af4f2012-08-13 13:43:58 +01001232 gsm->constipated = 1;
1233 gsm_control_reply(gsm, CMD_FCOFF, NULL, 0);
1234 break;
Alan Coxe1eaea42010-03-26 11:32:54 +00001235 case CMD_MSC:
1236 /* Out of band modem line change indicator for a DLCI */
1237 gsm_control_modem(gsm, data, clen);
1238 break;
1239 case CMD_RLS:
1240 /* Out of band error reception for a DLCI */
1241 gsm_control_rls(gsm, data, clen);
1242 break;
1243 case CMD_PSC:
1244 /* Modem wishes to enter power saving state */
1245 gsm_control_reply(gsm, CMD_PSC, NULL, 0);
1246 break;
1247 /* Optional unsupported commands */
1248 case CMD_PN: /* Parameter negotiation */
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001249 case CMD_RPN: /* Remote port negotiation */
1250 case CMD_SNC: /* Service negotiation command */
Alan Coxe1eaea42010-03-26 11:32:54 +00001251 default:
1252 /* Reply to bad commands with an NSC */
1253 buf[0] = command;
1254 gsm_control_reply(gsm, CMD_NSC, buf, 1);
1255 break;
1256 }
1257}
1258
1259/**
1260 * gsm_control_response - process a response to our control
1261 * @gsm: our GSM mux
1262 * @command: the command (response) EA
1263 * @data: data beyond the command/length EA
1264 * @clen: length
1265 *
1266 * Process a response to an outstanding command. We only allow a single
1267 * control message in flight so this is fairly easy. All the clean up
1268 * is done by the caller, we just update the fields, flag it as done
1269 * and return
1270 */
1271
1272static void gsm_control_response(struct gsm_mux *gsm, unsigned int command,
1273 u8 *data, int clen)
1274{
1275 struct gsm_control *ctrl;
1276 unsigned long flags;
1277
1278 spin_lock_irqsave(&gsm->control_lock, flags);
1279
1280 ctrl = gsm->pending_cmd;
1281 /* Does the reply match our command */
1282 command |= 1;
1283 if (ctrl != NULL && (command == ctrl->cmd || command == CMD_NSC)) {
1284 /* Our command was replied to, kill the retry timer */
1285 del_timer(&gsm->t2_timer);
1286 gsm->pending_cmd = NULL;
1287 /* Rejected by the other end */
1288 if (command == CMD_NSC)
1289 ctrl->error = -EOPNOTSUPP;
1290 ctrl->done = 1;
1291 wake_up(&gsm->event);
1292 }
1293 spin_unlock_irqrestore(&gsm->control_lock, flags);
1294}
1295
1296/**
Alan Cox5f9a31d2010-11-04 15:17:27 +00001297 * gsm_control_transmit - send control packet
Alan Coxe1eaea42010-03-26 11:32:54 +00001298 * @gsm: gsm mux
1299 * @ctrl: frame to send
1300 *
1301 * Send out a pending control command (called under control lock)
1302 */
1303
1304static void gsm_control_transmit(struct gsm_mux *gsm, struct gsm_control *ctrl)
1305{
Eric Bénarded43b472011-03-09 19:24:49 +01001306 struct gsm_msg *msg = gsm_data_alloc(gsm, 0, ctrl->len + 1, gsm->ftype);
Alan Coxe1eaea42010-03-26 11:32:54 +00001307 if (msg == NULL)
1308 return;
1309 msg->data[0] = (ctrl->cmd << 1) | 2 | EA; /* command */
1310 memcpy(msg->data + 1, ctrl->data, ctrl->len);
1311 gsm_data_queue(gsm->dlci[0], msg);
1312}
1313
1314/**
1315 * gsm_control_retransmit - retransmit a control frame
1316 * @data: pointer to our gsm object
1317 *
1318 * Called off the T2 timer expiry in order to retransmit control frames
1319 * that have been lost in the system somewhere. The control_lock protects
1320 * us from colliding with another sender or a receive completion event.
1321 * In that situation the timer may still occur in a small window but
1322 * gsm->pending_cmd will be NULL and we just let the timer expire.
1323 */
1324
1325static void gsm_control_retransmit(unsigned long data)
1326{
1327 struct gsm_mux *gsm = (struct gsm_mux *)data;
1328 struct gsm_control *ctrl;
1329 unsigned long flags;
1330 spin_lock_irqsave(&gsm->control_lock, flags);
1331 ctrl = gsm->pending_cmd;
1332 if (ctrl) {
1333 gsm->cretries--;
1334 if (gsm->cretries == 0) {
1335 gsm->pending_cmd = NULL;
1336 ctrl->error = -ETIMEDOUT;
1337 ctrl->done = 1;
1338 spin_unlock_irqrestore(&gsm->control_lock, flags);
1339 wake_up(&gsm->event);
1340 return;
1341 }
1342 gsm_control_transmit(gsm, ctrl);
1343 mod_timer(&gsm->t2_timer, jiffies + gsm->t2 * HZ / 100);
1344 }
1345 spin_unlock_irqrestore(&gsm->control_lock, flags);
1346}
1347
1348/**
1349 * gsm_control_send - send a control frame on DLCI 0
1350 * @gsm: the GSM channel
1351 * @command: command to send including CR bit
1352 * @data: bytes of data (must be kmalloced)
1353 * @len: length of the block to send
1354 *
1355 * Queue and dispatch a control command. Only one command can be
1356 * active at a time. In theory more can be outstanding but the matching
1357 * gets really complicated so for now stick to one outstanding.
1358 */
1359
1360static struct gsm_control *gsm_control_send(struct gsm_mux *gsm,
1361 unsigned int command, u8 *data, int clen)
1362{
1363 struct gsm_control *ctrl = kzalloc(sizeof(struct gsm_control),
1364 GFP_KERNEL);
1365 unsigned long flags;
1366 if (ctrl == NULL)
1367 return NULL;
1368retry:
1369 wait_event(gsm->event, gsm->pending_cmd == NULL);
1370 spin_lock_irqsave(&gsm->control_lock, flags);
1371 if (gsm->pending_cmd != NULL) {
1372 spin_unlock_irqrestore(&gsm->control_lock, flags);
1373 goto retry;
1374 }
1375 ctrl->cmd = command;
1376 ctrl->data = data;
1377 ctrl->len = clen;
1378 gsm->pending_cmd = ctrl;
1379 gsm->cretries = gsm->n2;
1380 mod_timer(&gsm->t2_timer, jiffies + gsm->t2 * HZ / 100);
1381 gsm_control_transmit(gsm, ctrl);
1382 spin_unlock_irqrestore(&gsm->control_lock, flags);
1383 return ctrl;
1384}
1385
1386/**
1387 * gsm_control_wait - wait for a control to finish
1388 * @gsm: GSM mux
1389 * @control: control we are waiting on
1390 *
1391 * Waits for the control to complete or time out. Frees any used
1392 * resources and returns 0 for success, or an error if the remote
1393 * rejected or ignored the request.
1394 */
1395
1396static int gsm_control_wait(struct gsm_mux *gsm, struct gsm_control *control)
1397{
1398 int err;
1399 wait_event(gsm->event, control->done == 1);
1400 err = control->error;
1401 kfree(control);
1402 return err;
1403}
1404
1405
1406/*
1407 * DLCI level handling: Needs krefs
1408 */
1409
1410/*
1411 * State transitions and timers
1412 */
1413
1414/**
1415 * gsm_dlci_close - a DLCI has closed
1416 * @dlci: DLCI that closed
1417 *
1418 * Perform processing when moving a DLCI into closed state. If there
1419 * is an attached tty this is hung up
1420 */
1421
1422static void gsm_dlci_close(struct gsm_dlci *dlci)
1423{
1424 del_timer(&dlci->t1);
1425 if (debug & 8)
Alan Cox5f9a31d2010-11-04 15:17:27 +00001426 pr_debug("DLCI %d goes closed.\n", dlci->addr);
Alan Coxe1eaea42010-03-26 11:32:54 +00001427 dlci->state = DLCI_CLOSED;
1428 if (dlci->addr != 0) {
1429 struct tty_struct *tty = tty_port_tty_get(&dlci->port);
1430 if (tty) {
1431 tty_hangup(tty);
1432 tty_kref_put(tty);
1433 }
1434 kfifo_reset(dlci->fifo);
1435 } else
1436 dlci->gsm->dead = 1;
1437 wake_up(&dlci->gsm->event);
1438 /* A DLCI 0 close is a MUX termination so we need to kick that
1439 back to userspace somehow */
1440}
1441
1442/**
1443 * gsm_dlci_open - a DLCI has opened
1444 * @dlci: DLCI that opened
1445 *
1446 * Perform processing when moving a DLCI into open state.
1447 */
1448
1449static void gsm_dlci_open(struct gsm_dlci *dlci)
1450{
1451 /* Note that SABM UA .. SABM UA first UA lost can mean that we go
1452 open -> open */
1453 del_timer(&dlci->t1);
1454 /* This will let a tty open continue */
1455 dlci->state = DLCI_OPEN;
1456 if (debug & 8)
Alan Cox5f9a31d2010-11-04 15:17:27 +00001457 pr_debug("DLCI %d goes open.\n", dlci->addr);
Alan Coxe1eaea42010-03-26 11:32:54 +00001458 wake_up(&dlci->gsm->event);
1459}
1460
1461/**
1462 * gsm_dlci_t1 - T1 timer expiry
1463 * @dlci: DLCI that opened
1464 *
1465 * The T1 timer handles retransmits of control frames (essentially of
1466 * SABM and DISC). We resend the command until the retry count runs out
1467 * in which case an opening port goes back to closed and a closing port
1468 * is simply put into closed state (any further frames from the other
1469 * end will get a DM response)
1470 */
1471
1472static void gsm_dlci_t1(unsigned long data)
1473{
1474 struct gsm_dlci *dlci = (struct gsm_dlci *)data;
1475 struct gsm_mux *gsm = dlci->gsm;
1476
1477 switch (dlci->state) {
1478 case DLCI_OPENING:
1479 dlci->retries--;
1480 if (dlci->retries) {
1481 gsm_command(dlci->gsm, dlci->addr, SABM|PF);
1482 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
1483 } else
1484 gsm_dlci_close(dlci);
1485 break;
1486 case DLCI_CLOSING:
1487 dlci->retries--;
1488 if (dlci->retries) {
1489 gsm_command(dlci->gsm, dlci->addr, DISC|PF);
1490 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
1491 } else
1492 gsm_dlci_close(dlci);
1493 break;
1494 }
1495}
1496
1497/**
1498 * gsm_dlci_begin_open - start channel open procedure
1499 * @dlci: DLCI to open
1500 *
1501 * Commence opening a DLCI from the Linux side. We issue SABM messages
1502 * to the modem which should then reply with a UA, at which point we
1503 * will move into open state. Opening is done asynchronously with retry
1504 * running off timers and the responses.
1505 */
1506
1507static void gsm_dlci_begin_open(struct gsm_dlci *dlci)
1508{
1509 struct gsm_mux *gsm = dlci->gsm;
1510 if (dlci->state == DLCI_OPEN || dlci->state == DLCI_OPENING)
1511 return;
1512 dlci->retries = gsm->n2;
1513 dlci->state = DLCI_OPENING;
1514 gsm_command(dlci->gsm, dlci->addr, SABM|PF);
1515 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
1516}
1517
1518/**
1519 * gsm_dlci_begin_close - start channel open procedure
1520 * @dlci: DLCI to open
1521 *
1522 * Commence closing a DLCI from the Linux side. We issue DISC messages
1523 * to the modem which should then reply with a UA, at which point we
1524 * will move into closed state. Closing is done asynchronously with retry
1525 * off timers. We may also receive a DM reply from the other end which
1526 * indicates the channel was already closed.
1527 */
1528
1529static void gsm_dlci_begin_close(struct gsm_dlci *dlci)
1530{
1531 struct gsm_mux *gsm = dlci->gsm;
1532 if (dlci->state == DLCI_CLOSED || dlci->state == DLCI_CLOSING)
1533 return;
1534 dlci->retries = gsm->n2;
1535 dlci->state = DLCI_CLOSING;
1536 gsm_command(dlci->gsm, dlci->addr, DISC|PF);
1537 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
1538}
1539
1540/**
1541 * gsm_dlci_data - data arrived
1542 * @dlci: channel
1543 * @data: block of bytes received
1544 * @len: length of received block
1545 *
1546 * A UI or UIH frame has arrived which contains data for a channel
1547 * other than the control channel. If the relevant virtual tty is
1548 * open we shovel the bits down it, if not we drop them.
1549 */
1550
Russ Gorby72632872011-06-14 13:23:28 -07001551static void gsm_dlci_data(struct gsm_dlci *dlci, u8 *data, int clen)
Alan Coxe1eaea42010-03-26 11:32:54 +00001552{
1553 /* krefs .. */
1554 struct tty_port *port = &dlci->port;
1555 struct tty_struct *tty = tty_port_tty_get(port);
1556 unsigned int modem = 0;
Russ Gorby72632872011-06-14 13:23:28 -07001557 int len = clen;
Alan Coxe1eaea42010-03-26 11:32:54 +00001558
1559 if (debug & 16)
Alan Cox5f9a31d2010-11-04 15:17:27 +00001560 pr_debug("%d bytes for tty %p\n", len, tty);
Alan Coxe1eaea42010-03-26 11:32:54 +00001561 if (tty) {
1562 switch (dlci->adaption) {
Alan Cox5f9a31d2010-11-04 15:17:27 +00001563 /* Unsupported types */
1564 /* Packetised interruptible data */
1565 case 4:
1566 break;
1567 /* Packetised uininterruptible voice/data */
1568 case 3:
1569 break;
1570 /* Asynchronous serial with line state in each frame */
1571 case 2:
1572 while (gsm_read_ea(&modem, *data++) == 0) {
1573 len--;
1574 if (len == 0)
1575 return;
1576 }
Russ Gorby72632872011-06-14 13:23:28 -07001577 gsm_process_modem(tty, dlci, modem, clen);
Alan Cox5f9a31d2010-11-04 15:17:27 +00001578 /* Line state will go via DLCI 0 controls only */
1579 case 1:
1580 default:
1581 tty_insert_flip_string(tty, data, len);
1582 tty_flip_buffer_push(tty);
Alan Coxe1eaea42010-03-26 11:32:54 +00001583 }
1584 tty_kref_put(tty);
1585 }
1586}
1587
1588/**
1589 * gsm_dlci_control - data arrived on control channel
1590 * @dlci: channel
1591 * @data: block of bytes received
1592 * @len: length of received block
1593 *
1594 * A UI or UIH frame has arrived which contains data for DLCI 0 the
1595 * control channel. This should contain a command EA followed by
1596 * control data bytes. The command EA contains a command/response bit
1597 * and we divide up the work accordingly.
1598 */
1599
1600static void gsm_dlci_command(struct gsm_dlci *dlci, u8 *data, int len)
1601{
1602 /* See what command is involved */
1603 unsigned int command = 0;
1604 while (len-- > 0) {
1605 if (gsm_read_ea(&command, *data++) == 1) {
1606 int clen = *data++;
1607 len--;
1608 /* FIXME: this is properly an EA */
1609 clen >>= 1;
1610 /* Malformed command ? */
1611 if (clen > len)
1612 return;
1613 if (command & 1)
1614 gsm_control_message(dlci->gsm, command,
1615 data, clen);
1616 else
1617 gsm_control_response(dlci->gsm, command,
1618 data, clen);
1619 return;
1620 }
1621 }
1622}
1623
1624/*
1625 * Allocate/Free DLCI channels
1626 */
1627
1628/**
1629 * gsm_dlci_alloc - allocate a DLCI
1630 * @gsm: GSM mux
1631 * @addr: address of the DLCI
1632 *
1633 * Allocate and install a new DLCI object into the GSM mux.
1634 *
1635 * FIXME: review locking races
1636 */
1637
1638static struct gsm_dlci *gsm_dlci_alloc(struct gsm_mux *gsm, int addr)
1639{
1640 struct gsm_dlci *dlci = kzalloc(sizeof(struct gsm_dlci), GFP_ATOMIC);
1641 if (dlci == NULL)
1642 return NULL;
1643 spin_lock_init(&dlci->lock);
Russ Gorby6ab8fba2011-06-16 14:20:13 -07001644 kref_init(&dlci->ref);
Russ Gorbybcd5abe2011-06-16 14:20:12 -07001645 mutex_init(&dlci->mutex);
Alan Coxe1eaea42010-03-26 11:32:54 +00001646 dlci->fifo = &dlci->_fifo;
1647 if (kfifo_alloc(&dlci->_fifo, 4096, GFP_KERNEL) < 0) {
1648 kfree(dlci);
1649 return NULL;
1650 }
1651
1652 skb_queue_head_init(&dlci->skb_list);
1653 init_timer(&dlci->t1);
1654 dlci->t1.function = gsm_dlci_t1;
1655 dlci->t1.data = (unsigned long)dlci;
1656 tty_port_init(&dlci->port);
1657 dlci->port.ops = &gsm_port_ops;
1658 dlci->gsm = gsm;
1659 dlci->addr = addr;
1660 dlci->adaption = gsm->adaption;
1661 dlci->state = DLCI_CLOSED;
1662 if (addr)
1663 dlci->data = gsm_dlci_data;
1664 else
1665 dlci->data = gsm_dlci_command;
1666 gsm->dlci[addr] = dlci;
1667 return dlci;
1668}
1669
1670/**
Russ Gorby6ab8fba2011-06-16 14:20:13 -07001671 * gsm_dlci_free - free DLCI
1672 * @dlci: DLCI to free
Alan Coxe1eaea42010-03-26 11:32:54 +00001673 *
Russ Gorby6ab8fba2011-06-16 14:20:13 -07001674 * Free up a DLCI.
Alan Coxe1eaea42010-03-26 11:32:54 +00001675 *
1676 * Can sleep.
1677 */
Russ Gorby6ab8fba2011-06-16 14:20:13 -07001678static void gsm_dlci_free(struct kref *ref)
1679{
1680 struct gsm_dlci *dlci = container_of(ref, struct gsm_dlci, ref);
1681
1682 del_timer_sync(&dlci->t1);
1683 dlci->gsm->dlci[dlci->addr] = NULL;
1684 kfifo_free(dlci->fifo);
1685 while ((dlci->skb = skb_dequeue(&dlci->skb_list)))
1686 kfree_skb(dlci->skb);
1687 kfree(dlci);
1688}
1689
1690static inline void dlci_get(struct gsm_dlci *dlci)
1691{
1692 kref_get(&dlci->ref);
1693}
1694
1695static inline void dlci_put(struct gsm_dlci *dlci)
1696{
1697 kref_put(&dlci->ref, gsm_dlci_free);
1698}
1699
1700/**
1701 * gsm_dlci_release - release DLCI
1702 * @dlci: DLCI to destroy
1703 *
1704 * Release a DLCI. Actual free is deferred until either
1705 * mux is closed or tty is closed - whichever is last.
1706 *
1707 * Can sleep.
1708 */
1709static void gsm_dlci_release(struct gsm_dlci *dlci)
Alan Coxe1eaea42010-03-26 11:32:54 +00001710{
1711 struct tty_struct *tty = tty_port_tty_get(&dlci->port);
1712 if (tty) {
1713 tty_vhangup(tty);
1714 tty_kref_put(tty);
1715 }
Russ Gorby6ab8fba2011-06-16 14:20:13 -07001716 dlci_put(dlci);
Alan Coxe1eaea42010-03-26 11:32:54 +00001717}
1718
Alan Coxe1eaea42010-03-26 11:32:54 +00001719/*
1720 * LAPBish link layer logic
1721 */
1722
1723/**
1724 * gsm_queue - a GSM frame is ready to process
1725 * @gsm: pointer to our gsm mux
1726 *
1727 * At this point in time a frame has arrived and been demangled from
1728 * the line encoding. All the differences between the encodings have
1729 * been handled below us and the frame is unpacked into the structures.
1730 * The fcs holds the header FCS but any data FCS must be added here.
1731 */
1732
1733static void gsm_queue(struct gsm_mux *gsm)
1734{
1735 struct gsm_dlci *dlci;
1736 u8 cr;
1737 int address;
1738 /* We have to sneak a look at the packet body to do the FCS.
1739 A somewhat layering violation in the spec */
1740
1741 if ((gsm->control & ~PF) == UI)
1742 gsm->fcs = gsm_fcs_add_block(gsm->fcs, gsm->buf, gsm->len);
Mikhail Kshevetskiy9db4e432011-03-27 04:05:00 +04001743 if (gsm->encoding == 0){
1744 /* WARNING: gsm->received_fcs is used for gsm->encoding = 0 only.
1745 In this case it contain the last piece of data
1746 required to generate final CRC */
1747 gsm->fcs = gsm_fcs_add(gsm->fcs, gsm->received_fcs);
1748 }
Alan Coxe1eaea42010-03-26 11:32:54 +00001749 if (gsm->fcs != GOOD_FCS) {
1750 gsm->bad_fcs++;
1751 if (debug & 4)
Alan Cox5f9a31d2010-11-04 15:17:27 +00001752 pr_debug("BAD FCS %02x\n", gsm->fcs);
Alan Coxe1eaea42010-03-26 11:32:54 +00001753 return;
1754 }
1755 address = gsm->address >> 1;
1756 if (address >= NUM_DLCI)
1757 goto invalid;
1758
1759 cr = gsm->address & 1; /* C/R bit */
1760
1761 gsm_print_packet("<--", address, cr, gsm->control, gsm->buf, gsm->len);
1762
1763 cr ^= 1 - gsm->initiator; /* Flip so 1 always means command */
1764 dlci = gsm->dlci[address];
1765
1766 switch (gsm->control) {
1767 case SABM|PF:
1768 if (cr == 0)
1769 goto invalid;
1770 if (dlci == NULL)
1771 dlci = gsm_dlci_alloc(gsm, address);
1772 if (dlci == NULL)
1773 return;
1774 if (dlci->dead)
1775 gsm_response(gsm, address, DM);
1776 else {
1777 gsm_response(gsm, address, UA);
1778 gsm_dlci_open(dlci);
1779 }
1780 break;
1781 case DISC|PF:
1782 if (cr == 0)
1783 goto invalid;
1784 if (dlci == NULL || dlci->state == DLCI_CLOSED) {
1785 gsm_response(gsm, address, DM);
1786 return;
1787 }
1788 /* Real close complete */
1789 gsm_response(gsm, address, UA);
1790 gsm_dlci_close(dlci);
1791 break;
1792 case UA:
1793 case UA|PF:
1794 if (cr == 0 || dlci == NULL)
1795 break;
1796 switch (dlci->state) {
1797 case DLCI_CLOSING:
1798 gsm_dlci_close(dlci);
1799 break;
1800 case DLCI_OPENING:
1801 gsm_dlci_open(dlci);
1802 break;
1803 }
1804 break;
1805 case DM: /* DM can be valid unsolicited */
1806 case DM|PF:
1807 if (cr)
1808 goto invalid;
1809 if (dlci == NULL)
1810 return;
1811 gsm_dlci_close(dlci);
1812 break;
1813 case UI:
1814 case UI|PF:
1815 case UIH:
1816 case UIH|PF:
1817#if 0
1818 if (cr)
1819 goto invalid;
1820#endif
1821 if (dlci == NULL || dlci->state != DLCI_OPEN) {
1822 gsm_command(gsm, address, DM|PF);
1823 return;
1824 }
1825 dlci->data(dlci, gsm->buf, gsm->len);
1826 break;
1827 default:
1828 goto invalid;
1829 }
1830 return;
1831invalid:
1832 gsm->malformed++;
1833 return;
1834}
1835
1836
1837/**
1838 * gsm0_receive - perform processing for non-transparency
1839 * @gsm: gsm data for this ldisc instance
1840 * @c: character
1841 *
1842 * Receive bytes in gsm mode 0
1843 */
1844
1845static void gsm0_receive(struct gsm_mux *gsm, unsigned char c)
1846{
Alan Coxc2f2f002010-11-04 15:17:03 +00001847 unsigned int len;
1848
Alan Coxe1eaea42010-03-26 11:32:54 +00001849 switch (gsm->state) {
1850 case GSM_SEARCH: /* SOF marker */
1851 if (c == GSM0_SOF) {
1852 gsm->state = GSM_ADDRESS;
1853 gsm->address = 0;
1854 gsm->len = 0;
1855 gsm->fcs = INIT_FCS;
1856 }
Alan Coxc2f2f002010-11-04 15:17:03 +00001857 break;
1858 case GSM_ADDRESS: /* Address EA */
Alan Coxe1eaea42010-03-26 11:32:54 +00001859 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
1860 if (gsm_read_ea(&gsm->address, c))
1861 gsm->state = GSM_CONTROL;
1862 break;
1863 case GSM_CONTROL: /* Control Byte */
1864 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
1865 gsm->control = c;
Alan Coxc2f2f002010-11-04 15:17:03 +00001866 gsm->state = GSM_LEN0;
Alan Coxe1eaea42010-03-26 11:32:54 +00001867 break;
Alan Coxc2f2f002010-11-04 15:17:03 +00001868 case GSM_LEN0: /* Length EA */
Alan Coxe1eaea42010-03-26 11:32:54 +00001869 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
1870 if (gsm_read_ea(&gsm->len, c)) {
1871 if (gsm->len > gsm->mru) {
1872 gsm->bad_size++;
1873 gsm->state = GSM_SEARCH;
1874 break;
1875 }
1876 gsm->count = 0;
Alan Coxc2f2f002010-11-04 15:17:03 +00001877 if (!gsm->len)
1878 gsm->state = GSM_FCS;
1879 else
1880 gsm->state = GSM_DATA;
1881 break;
Alan Coxe1eaea42010-03-26 11:32:54 +00001882 }
Alan Coxc2f2f002010-11-04 15:17:03 +00001883 gsm->state = GSM_LEN1;
1884 break;
1885 case GSM_LEN1:
1886 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
1887 len = c;
1888 gsm->len |= len << 7;
1889 if (gsm->len > gsm->mru) {
1890 gsm->bad_size++;
1891 gsm->state = GSM_SEARCH;
1892 break;
1893 }
1894 gsm->count = 0;
1895 if (!gsm->len)
1896 gsm->state = GSM_FCS;
1897 else
1898 gsm->state = GSM_DATA;
Alan Coxe1eaea42010-03-26 11:32:54 +00001899 break;
1900 case GSM_DATA: /* Data */
1901 gsm->buf[gsm->count++] = c;
1902 if (gsm->count == gsm->len)
1903 gsm->state = GSM_FCS;
1904 break;
1905 case GSM_FCS: /* FCS follows the packet */
Alan Coxc2f2f002010-11-04 15:17:03 +00001906 gsm->received_fcs = c;
Alan Coxe1eaea42010-03-26 11:32:54 +00001907 gsm_queue(gsm);
Alan Coxc2f2f002010-11-04 15:17:03 +00001908 gsm->state = GSM_SSOF;
1909 break;
1910 case GSM_SSOF:
1911 if (c == GSM0_SOF) {
1912 gsm->state = GSM_SEARCH;
1913 break;
1914 }
Alan Coxe1eaea42010-03-26 11:32:54 +00001915 break;
1916 }
1917}
1918
1919/**
Alan Coxc2f2f002010-11-04 15:17:03 +00001920 * gsm1_receive - perform processing for non-transparency
Alan Coxe1eaea42010-03-26 11:32:54 +00001921 * @gsm: gsm data for this ldisc instance
1922 * @c: character
1923 *
1924 * Receive bytes in mode 1 (Advanced option)
1925 */
1926
1927static void gsm1_receive(struct gsm_mux *gsm, unsigned char c)
1928{
1929 if (c == GSM1_SOF) {
1930 /* EOF is only valid in frame if we have got to the data state
1931 and received at least one byte (the FCS) */
1932 if (gsm->state == GSM_DATA && gsm->count) {
1933 /* Extract the FCS */
1934 gsm->count--;
1935 gsm->fcs = gsm_fcs_add(gsm->fcs, gsm->buf[gsm->count]);
1936 gsm->len = gsm->count;
1937 gsm_queue(gsm);
1938 gsm->state = GSM_START;
1939 return;
1940 }
1941 /* Any partial frame was a runt so go back to start */
1942 if (gsm->state != GSM_START) {
1943 gsm->malformed++;
1944 gsm->state = GSM_START;
1945 }
1946 /* A SOF in GSM_START means we are still reading idling or
1947 framing bytes */
1948 return;
1949 }
1950
1951 if (c == GSM1_ESCAPE) {
1952 gsm->escape = 1;
1953 return;
1954 }
1955
1956 /* Only an unescaped SOF gets us out of GSM search */
1957 if (gsm->state == GSM_SEARCH)
1958 return;
1959
1960 if (gsm->escape) {
1961 c ^= GSM1_ESCAPE_BITS;
1962 gsm->escape = 0;
1963 }
1964 switch (gsm->state) {
1965 case GSM_START: /* First byte after SOF */
1966 gsm->address = 0;
1967 gsm->state = GSM_ADDRESS;
1968 gsm->fcs = INIT_FCS;
1969 /* Drop through */
1970 case GSM_ADDRESS: /* Address continuation */
1971 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
1972 if (gsm_read_ea(&gsm->address, c))
1973 gsm->state = GSM_CONTROL;
1974 break;
1975 case GSM_CONTROL: /* Control Byte */
1976 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
1977 gsm->control = c;
1978 gsm->count = 0;
1979 gsm->state = GSM_DATA;
1980 break;
1981 case GSM_DATA: /* Data */
Alan Cox5f9a31d2010-11-04 15:17:27 +00001982 if (gsm->count > gsm->mru) { /* Allow one for the FCS */
Alan Coxe1eaea42010-03-26 11:32:54 +00001983 gsm->state = GSM_OVERRUN;
1984 gsm->bad_size++;
1985 } else
1986 gsm->buf[gsm->count++] = c;
1987 break;
1988 case GSM_OVERRUN: /* Over-long - eg a dropped SOF */
1989 break;
1990 }
1991}
1992
1993/**
1994 * gsm_error - handle tty error
1995 * @gsm: ldisc data
1996 * @data: byte received (may be invalid)
1997 * @flag: error received
1998 *
1999 * Handle an error in the receipt of data for a frame. Currently we just
2000 * go back to hunting for a SOF.
2001 *
2002 * FIXME: better diagnostics ?
2003 */
2004
2005static void gsm_error(struct gsm_mux *gsm,
2006 unsigned char data, unsigned char flag)
2007{
2008 gsm->state = GSM_SEARCH;
2009 gsm->io_error++;
2010}
2011
2012/**
2013 * gsm_cleanup_mux - generic GSM protocol cleanup
2014 * @gsm: our mux
2015 *
2016 * Clean up the bits of the mux which are the same for all framing
2017 * protocols. Remove the mux from the mux table, stop all the timers
2018 * and then shut down each device hanging up the channels as we go.
2019 */
2020
2021void gsm_cleanup_mux(struct gsm_mux *gsm)
2022{
2023 int i;
2024 struct gsm_dlci *dlci = gsm->dlci[0];
2025 struct gsm_msg *txq;
Alan Coxf17141f2011-08-26 11:28:11 +01002026 struct gsm_control *gc;
Alan Coxe1eaea42010-03-26 11:32:54 +00002027
2028 gsm->dead = 1;
2029
2030 spin_lock(&gsm_mux_lock);
2031 for (i = 0; i < MAX_MUX; i++) {
2032 if (gsm_mux[i] == gsm) {
2033 gsm_mux[i] = NULL;
2034 break;
2035 }
2036 }
2037 spin_unlock(&gsm_mux_lock);
2038 WARN_ON(i == MAX_MUX);
2039
Alan Coxf17141f2011-08-26 11:28:11 +01002040 /* In theory disconnecting DLCI 0 is sufficient but for some
2041 modems this is apparently not the case. */
2042 if (dlci) {
2043 gc = gsm_control_send(gsm, CMD_CLD, NULL, 0);
2044 if (gc)
2045 gsm_control_wait(gsm, gc);
2046 }
Alan Coxe1eaea42010-03-26 11:32:54 +00002047 del_timer_sync(&gsm->t2_timer);
2048 /* Now we are sure T2 has stopped */
2049 if (dlci) {
2050 dlci->dead = 1;
2051 gsm_dlci_begin_close(dlci);
2052 wait_event_interruptible(gsm->event,
2053 dlci->state == DLCI_CLOSED);
2054 }
2055 /* Free up any link layer users */
2056 for (i = 0; i < NUM_DLCI; i++)
2057 if (gsm->dlci[i])
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002058 gsm_dlci_release(gsm->dlci[i]);
Alan Coxe1eaea42010-03-26 11:32:54 +00002059 /* Now wipe the queues */
2060 for (txq = gsm->tx_head; txq != NULL; txq = gsm->tx_head) {
2061 gsm->tx_head = txq->next;
2062 kfree(txq);
2063 }
2064 gsm->tx_tail = NULL;
2065}
2066EXPORT_SYMBOL_GPL(gsm_cleanup_mux);
2067
2068/**
2069 * gsm_activate_mux - generic GSM setup
2070 * @gsm: our mux
2071 *
2072 * Set up the bits of the mux which are the same for all framing
2073 * protocols. Add the mux to the mux table so it can be opened and
2074 * finally kick off connecting to DLCI 0 on the modem.
2075 */
2076
2077int gsm_activate_mux(struct gsm_mux *gsm)
2078{
2079 struct gsm_dlci *dlci;
2080 int i = 0;
2081
2082 init_timer(&gsm->t2_timer);
2083 gsm->t2_timer.function = gsm_control_retransmit;
2084 gsm->t2_timer.data = (unsigned long)gsm;
2085 init_waitqueue_head(&gsm->event);
2086 spin_lock_init(&gsm->control_lock);
2087 spin_lock_init(&gsm->tx_lock);
2088
2089 if (gsm->encoding == 0)
2090 gsm->receive = gsm0_receive;
2091 else
2092 gsm->receive = gsm1_receive;
2093 gsm->error = gsm_error;
2094
2095 spin_lock(&gsm_mux_lock);
2096 for (i = 0; i < MAX_MUX; i++) {
2097 if (gsm_mux[i] == NULL) {
Russ Gorbyd50f6dc2011-06-14 13:35:32 -07002098 gsm->num = i;
Alan Coxe1eaea42010-03-26 11:32:54 +00002099 gsm_mux[i] = gsm;
2100 break;
2101 }
2102 }
2103 spin_unlock(&gsm_mux_lock);
2104 if (i == MAX_MUX)
2105 return -EBUSY;
2106
2107 dlci = gsm_dlci_alloc(gsm, 0);
2108 if (dlci == NULL)
2109 return -ENOMEM;
2110 gsm->dead = 0; /* Tty opens are now permissible */
2111 return 0;
2112}
2113EXPORT_SYMBOL_GPL(gsm_activate_mux);
2114
2115/**
2116 * gsm_free_mux - free up a mux
2117 * @mux: mux to free
2118 *
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002119 * Dispose of allocated resources for a dead mux
Alan Coxe1eaea42010-03-26 11:32:54 +00002120 */
2121void gsm_free_mux(struct gsm_mux *gsm)
2122{
2123 kfree(gsm->txframe);
2124 kfree(gsm->buf);
2125 kfree(gsm);
2126}
2127EXPORT_SYMBOL_GPL(gsm_free_mux);
2128
2129/**
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002130 * gsm_free_muxr - free up a mux
2131 * @mux: mux to free
2132 *
2133 * Dispose of allocated resources for a dead mux
2134 */
2135static void gsm_free_muxr(struct kref *ref)
2136{
2137 struct gsm_mux *gsm = container_of(ref, struct gsm_mux, ref);
2138 gsm_free_mux(gsm);
2139}
2140
2141static inline void mux_get(struct gsm_mux *gsm)
2142{
2143 kref_get(&gsm->ref);
2144}
2145
2146static inline void mux_put(struct gsm_mux *gsm)
2147{
2148 kref_put(&gsm->ref, gsm_free_muxr);
2149}
2150
2151/**
Alan Coxe1eaea42010-03-26 11:32:54 +00002152 * gsm_alloc_mux - allocate a mux
2153 *
2154 * Creates a new mux ready for activation.
2155 */
2156
2157struct gsm_mux *gsm_alloc_mux(void)
2158{
2159 struct gsm_mux *gsm = kzalloc(sizeof(struct gsm_mux), GFP_KERNEL);
2160 if (gsm == NULL)
2161 return NULL;
2162 gsm->buf = kmalloc(MAX_MRU + 1, GFP_KERNEL);
2163 if (gsm->buf == NULL) {
2164 kfree(gsm);
2165 return NULL;
2166 }
2167 gsm->txframe = kmalloc(2 * MAX_MRU + 2, GFP_KERNEL);
2168 if (gsm->txframe == NULL) {
2169 kfree(gsm->buf);
2170 kfree(gsm);
2171 return NULL;
2172 }
2173 spin_lock_init(&gsm->lock);
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002174 kref_init(&gsm->ref);
Alan Coxe1eaea42010-03-26 11:32:54 +00002175
2176 gsm->t1 = T1;
2177 gsm->t2 = T2;
2178 gsm->n2 = N2;
2179 gsm->ftype = UIH;
Alan Coxe1eaea42010-03-26 11:32:54 +00002180 gsm->adaption = 1;
2181 gsm->encoding = 1;
2182 gsm->mru = 64; /* Default to encoding 1 so these should be 64 */
2183 gsm->mtu = 64;
2184 gsm->dead = 1; /* Avoid early tty opens */
2185
2186 return gsm;
2187}
2188EXPORT_SYMBOL_GPL(gsm_alloc_mux);
2189
Alan Coxe1eaea42010-03-26 11:32:54 +00002190/**
2191 * gsmld_output - write to link
2192 * @gsm: our mux
2193 * @data: bytes to output
2194 * @len: size
2195 *
2196 * Write a block of data from the GSM mux to the data channel. This
2197 * will eventually be serialized from above but at the moment isn't.
2198 */
2199
2200static int gsmld_output(struct gsm_mux *gsm, u8 *data, int len)
2201{
2202 if (tty_write_room(gsm->tty) < len) {
2203 set_bit(TTY_DO_WRITE_WAKEUP, &gsm->tty->flags);
2204 return -ENOSPC;
2205 }
Joe Perches0a77c4f2011-04-25 16:46:49 -07002206 if (debug & 4)
2207 print_hex_dump_bytes("gsmld_output: ", DUMP_PREFIX_OFFSET,
2208 data, len);
Alan Coxe1eaea42010-03-26 11:32:54 +00002209 gsm->tty->ops->write(gsm->tty, data, len);
2210 return len;
2211}
2212
2213/**
2214 * gsmld_attach_gsm - mode set up
2215 * @tty: our tty structure
2216 * @gsm: our mux
2217 *
2218 * Set up the MUX for basic mode and commence connecting to the
2219 * modem. Currently called from the line discipline set up but
2220 * will need moving to an ioctl path.
2221 */
2222
2223static int gsmld_attach_gsm(struct tty_struct *tty, struct gsm_mux *gsm)
2224{
Russ Gorbyd50f6dc2011-06-14 13:35:32 -07002225 int ret, i;
2226 int base = gsm->num << 6; /* Base for this MUX */
Alan Coxe1eaea42010-03-26 11:32:54 +00002227
2228 gsm->tty = tty_kref_get(tty);
2229 gsm->output = gsmld_output;
2230 ret = gsm_activate_mux(gsm);
2231 if (ret != 0)
2232 tty_kref_put(gsm->tty);
Russ Gorbyd50f6dc2011-06-14 13:35:32 -07002233 else {
2234 /* Don't register device 0 - this is the control channel and not
2235 a usable tty interface */
2236 for (i = 1; i < NUM_DLCI; i++)
2237 tty_register_device(gsm_tty_driver, base + i, NULL);
2238 }
Alan Coxe1eaea42010-03-26 11:32:54 +00002239 return ret;
2240}
2241
2242
2243/**
2244 * gsmld_detach_gsm - stop doing 0710 mux
Justin P. Mattock70f23fd2011-05-10 10:16:21 +02002245 * @tty: tty attached to the mux
Alan Coxe1eaea42010-03-26 11:32:54 +00002246 * @gsm: mux
2247 *
2248 * Shutdown and then clean up the resources used by the line discipline
2249 */
2250
2251static void gsmld_detach_gsm(struct tty_struct *tty, struct gsm_mux *gsm)
2252{
Russ Gorbyd50f6dc2011-06-14 13:35:32 -07002253 int i;
2254 int base = gsm->num << 6; /* Base for this MUX */
2255
Alan Coxe1eaea42010-03-26 11:32:54 +00002256 WARN_ON(tty != gsm->tty);
Russ Gorbyd50f6dc2011-06-14 13:35:32 -07002257 for (i = 1; i < NUM_DLCI; i++)
2258 tty_unregister_device(gsm_tty_driver, base + i);
Alan Coxe1eaea42010-03-26 11:32:54 +00002259 gsm_cleanup_mux(gsm);
2260 tty_kref_put(gsm->tty);
2261 gsm->tty = NULL;
2262}
2263
Linus Torvalds55db4c62011-06-04 06:33:24 +09002264static void gsmld_receive_buf(struct tty_struct *tty, const unsigned char *cp,
2265 char *fp, int count)
Alan Coxe1eaea42010-03-26 11:32:54 +00002266{
2267 struct gsm_mux *gsm = tty->disc_data;
2268 const unsigned char *dp;
2269 char *f;
2270 int i;
2271 char buf[64];
2272 char flags;
2273
Joe Perches0a77c4f2011-04-25 16:46:49 -07002274 if (debug & 4)
2275 print_hex_dump_bytes("gsmld_receive: ", DUMP_PREFIX_OFFSET,
2276 cp, count);
Alan Coxe1eaea42010-03-26 11:32:54 +00002277
2278 for (i = count, dp = cp, f = fp; i; i--, dp++) {
2279 flags = *f++;
2280 switch (flags) {
2281 case TTY_NORMAL:
2282 gsm->receive(gsm, *dp);
2283 break;
2284 case TTY_OVERRUN:
2285 case TTY_BREAK:
2286 case TTY_PARITY:
2287 case TTY_FRAME:
2288 gsm->error(gsm, *dp, flags);
2289 break;
2290 default:
Frederic Beratc01af4f2012-08-13 13:43:58 +01002291 WARN_ONCE(1, "%s: unknown flag %d\n",
Alan Coxe1eaea42010-03-26 11:32:54 +00002292 tty_name(tty, buf), flags);
2293 break;
2294 }
2295 }
2296 /* FASYNC if needed ? */
2297 /* If clogged call tty_throttle(tty); */
2298}
2299
2300/**
2301 * gsmld_chars_in_buffer - report available bytes
2302 * @tty: tty device
2303 *
2304 * Report the number of characters buffered to be delivered to user
2305 * at this instant in time.
2306 *
2307 * Locking: gsm lock
2308 */
2309
2310static ssize_t gsmld_chars_in_buffer(struct tty_struct *tty)
2311{
2312 return 0;
2313}
2314
2315/**
2316 * gsmld_flush_buffer - clean input queue
2317 * @tty: terminal device
2318 *
2319 * Flush the input buffer. Called when the line discipline is
2320 * being closed, when the tty layer wants the buffer flushed (eg
2321 * at hangup).
2322 */
2323
2324static void gsmld_flush_buffer(struct tty_struct *tty)
2325{
2326}
2327
2328/**
2329 * gsmld_close - close the ldisc for this tty
2330 * @tty: device
2331 *
2332 * Called from the terminal layer when this line discipline is
2333 * being shut down, either because of a close or becsuse of a
2334 * discipline change. The function will not be called while other
2335 * ldisc methods are in progress.
2336 */
2337
2338static void gsmld_close(struct tty_struct *tty)
2339{
2340 struct gsm_mux *gsm = tty->disc_data;
2341
2342 gsmld_detach_gsm(tty, gsm);
2343
2344 gsmld_flush_buffer(tty);
2345 /* Do other clean up here */
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002346 mux_put(gsm);
Alan Coxe1eaea42010-03-26 11:32:54 +00002347}
2348
2349/**
2350 * gsmld_open - open an ldisc
2351 * @tty: terminal to open
2352 *
2353 * Called when this line discipline is being attached to the
2354 * terminal device. Can sleep. Called serialized so that no
2355 * other events will occur in parallel. No further open will occur
2356 * until a close.
2357 */
2358
2359static int gsmld_open(struct tty_struct *tty)
2360{
2361 struct gsm_mux *gsm;
2362
2363 if (tty->ops->write == NULL)
2364 return -EINVAL;
2365
2366 /* Attach our ldisc data */
2367 gsm = gsm_alloc_mux();
2368 if (gsm == NULL)
2369 return -ENOMEM;
2370
2371 tty->disc_data = gsm;
2372 tty->receive_room = 65536;
2373
2374 /* Attach the initial passive connection */
2375 gsm->encoding = 1;
2376 return gsmld_attach_gsm(tty, gsm);
2377}
2378
2379/**
2380 * gsmld_write_wakeup - asynchronous I/O notifier
2381 * @tty: tty device
2382 *
2383 * Required for the ptys, serial driver etc. since processes
2384 * that attach themselves to the master and rely on ASYNC
2385 * IO must be woken up
2386 */
2387
2388static void gsmld_write_wakeup(struct tty_struct *tty)
2389{
2390 struct gsm_mux *gsm = tty->disc_data;
Dan Carpenter328be392010-05-25 11:37:17 +02002391 unsigned long flags;
Alan Coxe1eaea42010-03-26 11:32:54 +00002392
2393 /* Queue poll */
2394 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
2395 gsm_data_kick(gsm);
Dan Carpenter328be392010-05-25 11:37:17 +02002396 if (gsm->tx_bytes < TX_THRESH_LO) {
2397 spin_lock_irqsave(&gsm->tx_lock, flags);
Alan Coxe1eaea42010-03-26 11:32:54 +00002398 gsm_dlci_data_sweep(gsm);
Dan Carpenter328be392010-05-25 11:37:17 +02002399 spin_unlock_irqrestore(&gsm->tx_lock, flags);
2400 }
Alan Coxe1eaea42010-03-26 11:32:54 +00002401}
2402
2403/**
2404 * gsmld_read - read function for tty
2405 * @tty: tty device
2406 * @file: file object
2407 * @buf: userspace buffer pointer
2408 * @nr: size of I/O
2409 *
2410 * Perform reads for the line discipline. We are guaranteed that the
2411 * line discipline will not be closed under us but we may get multiple
2412 * parallel readers and must handle this ourselves. We may also get
2413 * a hangup. Always called in user context, may sleep.
2414 *
2415 * This code must be sure never to sleep through a hangup.
2416 */
2417
2418static ssize_t gsmld_read(struct tty_struct *tty, struct file *file,
2419 unsigned char __user *buf, size_t nr)
2420{
2421 return -EOPNOTSUPP;
2422}
2423
2424/**
2425 * gsmld_write - write function for tty
2426 * @tty: tty device
2427 * @file: file object
2428 * @buf: userspace buffer pointer
2429 * @nr: size of I/O
2430 *
2431 * Called when the owner of the device wants to send a frame
2432 * itself (or some other control data). The data is transferred
2433 * as-is and must be properly framed and checksummed as appropriate
2434 * by userspace. Frames are either sent whole or not at all as this
2435 * avoids pain user side.
2436 */
2437
2438static ssize_t gsmld_write(struct tty_struct *tty, struct file *file,
2439 const unsigned char *buf, size_t nr)
2440{
2441 int space = tty_write_room(tty);
2442 if (space >= nr)
2443 return tty->ops->write(tty, buf, nr);
2444 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
2445 return -ENOBUFS;
2446}
2447
2448/**
2449 * gsmld_poll - poll method for N_GSM0710
2450 * @tty: terminal device
2451 * @file: file accessing it
2452 * @wait: poll table
2453 *
2454 * Called when the line discipline is asked to poll() for data or
2455 * for special events. This code is not serialized with respect to
2456 * other events save open/close.
2457 *
2458 * This code must be sure never to sleep through a hangup.
2459 * Called without the kernel lock held - fine
2460 */
2461
2462static unsigned int gsmld_poll(struct tty_struct *tty, struct file *file,
2463 poll_table *wait)
2464{
2465 unsigned int mask = 0;
2466 struct gsm_mux *gsm = tty->disc_data;
2467
2468 poll_wait(file, &tty->read_wait, wait);
2469 poll_wait(file, &tty->write_wait, wait);
2470 if (tty_hung_up_p(file))
2471 mask |= POLLHUP;
2472 if (!tty_is_writelocked(tty) && tty_write_room(tty) > 0)
2473 mask |= POLLOUT | POLLWRNORM;
2474 if (gsm->dead)
2475 mask |= POLLHUP;
2476 return mask;
2477}
2478
2479static int gsmld_config(struct tty_struct *tty, struct gsm_mux *gsm,
2480 struct gsm_config *c)
2481{
2482 int need_close = 0;
2483 int need_restart = 0;
2484
2485 /* Stuff we don't support yet - UI or I frame transport, windowing */
Alan Cox5f9a31d2010-11-04 15:17:27 +00002486 if ((c->adaption != 1 && c->adaption != 2) || c->k)
Alan Coxe1eaea42010-03-26 11:32:54 +00002487 return -EOPNOTSUPP;
2488 /* Check the MRU/MTU range looks sane */
2489 if (c->mru > MAX_MRU || c->mtu > MAX_MTU || c->mru < 8 || c->mtu < 8)
2490 return -EINVAL;
2491 if (c->n2 < 3)
2492 return -EINVAL;
2493 if (c->encapsulation > 1) /* Basic, advanced, no I */
2494 return -EINVAL;
2495 if (c->initiator > 1)
2496 return -EINVAL;
2497 if (c->i == 0 || c->i > 2) /* UIH and UI only */
2498 return -EINVAL;
2499 /*
2500 * See what is needed for reconfiguration
2501 */
2502
2503 /* Timing fields */
2504 if (c->t1 != 0 && c->t1 != gsm->t1)
2505 need_restart = 1;
2506 if (c->t2 != 0 && c->t2 != gsm->t2)
2507 need_restart = 1;
2508 if (c->encapsulation != gsm->encoding)
2509 need_restart = 1;
2510 if (c->adaption != gsm->adaption)
2511 need_restart = 1;
2512 /* Requires care */
2513 if (c->initiator != gsm->initiator)
2514 need_close = 1;
2515 if (c->mru != gsm->mru)
2516 need_restart = 1;
2517 if (c->mtu != gsm->mtu)
2518 need_restart = 1;
2519
2520 /*
2521 * Close down what is needed, restart and initiate the new
2522 * configuration
2523 */
2524
2525 if (need_close || need_restart) {
2526 gsm_dlci_begin_close(gsm->dlci[0]);
2527 /* This will timeout if the link is down due to N2 expiring */
2528 wait_event_interruptible(gsm->event,
2529 gsm->dlci[0]->state == DLCI_CLOSED);
2530 if (signal_pending(current))
2531 return -EINTR;
2532 }
2533 if (need_restart)
2534 gsm_cleanup_mux(gsm);
2535
2536 gsm->initiator = c->initiator;
2537 gsm->mru = c->mru;
Ken Mills91f78f32011-01-25 14:17:45 +00002538 gsm->mtu = c->mtu;
Alan Coxe1eaea42010-03-26 11:32:54 +00002539 gsm->encoding = c->encapsulation;
2540 gsm->adaption = c->adaption;
Ken Mills820e62e2010-11-04 15:16:24 +00002541 gsm->n2 = c->n2;
Alan Coxe1eaea42010-03-26 11:32:54 +00002542
2543 if (c->i == 1)
2544 gsm->ftype = UIH;
2545 else if (c->i == 2)
2546 gsm->ftype = UI;
2547
2548 if (c->t1)
2549 gsm->t1 = c->t1;
2550 if (c->t2)
2551 gsm->t2 = c->t2;
2552
2553 /* FIXME: We need to separate activation/deactivation from adding
2554 and removing from the mux array */
2555 if (need_restart)
2556 gsm_activate_mux(gsm);
2557 if (gsm->initiator && need_close)
2558 gsm_dlci_begin_open(gsm->dlci[0]);
2559 return 0;
2560}
2561
2562static int gsmld_ioctl(struct tty_struct *tty, struct file *file,
2563 unsigned int cmd, unsigned long arg)
2564{
2565 struct gsm_config c;
2566 struct gsm_mux *gsm = tty->disc_data;
2567
2568 switch (cmd) {
2569 case GSMIOC_GETCONF:
2570 memset(&c, 0, sizeof(c));
2571 c.adaption = gsm->adaption;
2572 c.encapsulation = gsm->encoding;
2573 c.initiator = gsm->initiator;
2574 c.t1 = gsm->t1;
2575 c.t2 = gsm->t2;
2576 c.t3 = 0; /* Not supported */
2577 c.n2 = gsm->n2;
2578 if (gsm->ftype == UIH)
2579 c.i = 1;
2580 else
2581 c.i = 2;
Alan Cox5f9a31d2010-11-04 15:17:27 +00002582 pr_debug("Ftype %d i %d\n", gsm->ftype, c.i);
Alan Coxe1eaea42010-03-26 11:32:54 +00002583 c.mru = gsm->mru;
2584 c.mtu = gsm->mtu;
2585 c.k = 0;
2586 if (copy_to_user((void *)arg, &c, sizeof(c)))
2587 return -EFAULT;
2588 return 0;
2589 case GSMIOC_SETCONF:
2590 if (copy_from_user(&c, (void *)arg, sizeof(c)))
2591 return -EFAULT;
2592 return gsmld_config(tty, gsm, &c);
2593 default:
2594 return n_tty_ioctl_helper(tty, file, cmd, arg);
2595 }
2596}
2597
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002598/*
2599 * Network interface
2600 *
2601 */
2602
2603static int gsm_mux_net_open(struct net_device *net)
2604{
2605 pr_debug("%s called\n", __func__);
2606 netif_start_queue(net);
2607 return 0;
2608}
2609
2610static int gsm_mux_net_close(struct net_device *net)
2611{
2612 netif_stop_queue(net);
2613 return 0;
2614}
2615
2616static struct net_device_stats *gsm_mux_net_get_stats(struct net_device *net)
2617{
2618 return &((struct gsm_mux_net *)netdev_priv(net))->stats;
2619}
2620static void dlci_net_free(struct gsm_dlci *dlci)
2621{
2622 if (!dlci->net) {
2623 WARN_ON(1);
2624 return;
2625 }
2626 dlci->adaption = dlci->prev_adaption;
2627 dlci->data = dlci->prev_data;
2628 free_netdev(dlci->net);
2629 dlci->net = NULL;
2630}
2631static void net_free(struct kref *ref)
2632{
2633 struct gsm_mux_net *mux_net;
2634 struct gsm_dlci *dlci;
2635
2636 mux_net = container_of(ref, struct gsm_mux_net, ref);
2637 dlci = mux_net->dlci;
2638
2639 if (dlci->net) {
2640 unregister_netdev(dlci->net);
2641 dlci_net_free(dlci);
2642 }
2643}
2644
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002645static inline void muxnet_get(struct gsm_mux_net *mux_net)
2646{
2647 kref_get(&mux_net->ref);
2648}
2649
2650static inline void muxnet_put(struct gsm_mux_net *mux_net)
2651{
2652 kref_put(&mux_net->ref, net_free);
2653}
2654
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002655static int gsm_mux_net_start_xmit(struct sk_buff *skb,
2656 struct net_device *net)
2657{
2658 struct gsm_mux_net *mux_net = (struct gsm_mux_net *)netdev_priv(net);
2659 struct gsm_dlci *dlci = mux_net->dlci;
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002660 muxnet_get(mux_net);
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002661
2662 skb_queue_head(&dlci->skb_list, skb);
2663 STATS(net).tx_packets++;
2664 STATS(net).tx_bytes += skb->len;
2665 gsm_dlci_data_kick(dlci);
2666 /* And tell the kernel when the last transmit started. */
2667 net->trans_start = jiffies;
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002668 muxnet_put(mux_net);
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002669 return NETDEV_TX_OK;
2670}
2671
2672/* called when a packet did not ack after watchdogtimeout */
2673static void gsm_mux_net_tx_timeout(struct net_device *net)
2674{
2675 /* Tell syslog we are hosed. */
2676 dev_dbg(&net->dev, "Tx timed out.\n");
2677
2678 /* Update statistics */
2679 STATS(net).tx_errors++;
2680}
2681
2682static void gsm_mux_rx_netchar(struct gsm_dlci *dlci,
2683 unsigned char *in_buf, int size)
2684{
2685 struct net_device *net = dlci->net;
2686 struct sk_buff *skb;
2687 struct gsm_mux_net *mux_net = (struct gsm_mux_net *)netdev_priv(net);
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002688 muxnet_get(mux_net);
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002689
2690 /* Allocate an sk_buff */
2691 skb = dev_alloc_skb(size + NET_IP_ALIGN);
2692 if (!skb) {
2693 /* We got no receive buffer. */
2694 STATS(net).rx_dropped++;
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002695 muxnet_put(mux_net);
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002696 return;
2697 }
2698 skb_reserve(skb, NET_IP_ALIGN);
2699 memcpy(skb_put(skb, size), in_buf, size);
2700
2701 skb->dev = net;
2702 skb->protocol = __constant_htons(ETH_P_IP);
2703
2704 /* Ship it off to the kernel */
2705 netif_rx(skb);
2706
2707 /* update out statistics */
2708 STATS(net).rx_packets++;
2709 STATS(net).rx_bytes += size;
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002710 muxnet_put(mux_net);
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002711 return;
2712}
2713
2714int gsm_change_mtu(struct net_device *net, int new_mtu)
2715{
2716 struct gsm_mux_net *mux_net = (struct gsm_mux_net *)netdev_priv(net);
2717 if ((new_mtu < 8) || (new_mtu > mux_net->dlci->gsm->mtu))
2718 return -EINVAL;
2719 net->mtu = new_mtu;
2720 return 0;
2721}
2722
2723static void gsm_mux_net_init(struct net_device *net)
2724{
2725 static const struct net_device_ops gsm_netdev_ops = {
2726 .ndo_open = gsm_mux_net_open,
2727 .ndo_stop = gsm_mux_net_close,
2728 .ndo_start_xmit = gsm_mux_net_start_xmit,
2729 .ndo_tx_timeout = gsm_mux_net_tx_timeout,
2730 .ndo_get_stats = gsm_mux_net_get_stats,
2731 .ndo_change_mtu = gsm_change_mtu,
2732 };
2733
2734 net->netdev_ops = &gsm_netdev_ops;
2735
2736 /* fill in the other fields */
2737 net->watchdog_timeo = GSM_NET_TX_TIMEOUT;
2738 net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2739 net->type = ARPHRD_NONE;
2740 net->tx_queue_len = 10;
2741}
2742
2743
2744/* caller holds the dlci mutex */
2745static void gsm_destroy_network(struct gsm_dlci *dlci)
2746{
2747 struct gsm_mux_net *mux_net;
2748
2749 pr_debug("destroy network interface");
2750 if (!dlci->net)
2751 return;
2752 mux_net = (struct gsm_mux_net *)netdev_priv(dlci->net);
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002753 muxnet_put(mux_net);
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002754}
2755
2756
2757/* caller holds the dlci mutex */
2758static int gsm_create_network(struct gsm_dlci *dlci, struct gsm_netconfig *nc)
2759{
2760 char *netname;
2761 int retval = 0;
2762 struct net_device *net;
2763 struct gsm_mux_net *mux_net;
2764
2765 if (!capable(CAP_NET_ADMIN))
2766 return -EPERM;
2767
2768 /* Already in a non tty mode */
2769 if (dlci->adaption > 2)
2770 return -EBUSY;
2771
2772 if (nc->protocol != htons(ETH_P_IP))
2773 return -EPROTONOSUPPORT;
2774
2775 if (nc->adaption != 3 && nc->adaption != 4)
2776 return -EPROTONOSUPPORT;
2777
2778 pr_debug("create network interface");
2779
2780 netname = "gsm%d";
2781 if (nc->if_name[0] != '\0')
2782 netname = nc->if_name;
2783 net = alloc_netdev(sizeof(struct gsm_mux_net),
2784 netname,
2785 gsm_mux_net_init);
2786 if (!net) {
2787 pr_err("alloc_netdev failed");
2788 return -ENOMEM;
2789 }
2790 net->mtu = dlci->gsm->mtu;
2791 mux_net = (struct gsm_mux_net *)netdev_priv(net);
2792 mux_net->dlci = dlci;
2793 kref_init(&mux_net->ref);
2794 strncpy(nc->if_name, net->name, IFNAMSIZ); /* return net name */
2795
2796 /* reconfigure dlci for network */
2797 dlci->prev_adaption = dlci->adaption;
2798 dlci->prev_data = dlci->data;
2799 dlci->adaption = nc->adaption;
2800 dlci->data = gsm_mux_rx_netchar;
2801 dlci->net = net;
2802
2803 pr_debug("register netdev");
2804 retval = register_netdev(net);
2805 if (retval) {
2806 pr_err("network register fail %d\n", retval);
2807 dlci_net_free(dlci);
2808 return retval;
2809 }
2810 return net->ifindex; /* return network index */
2811}
Alan Coxe1eaea42010-03-26 11:32:54 +00002812
2813/* Line discipline for real tty */
2814struct tty_ldisc_ops tty_ldisc_packet = {
2815 .owner = THIS_MODULE,
2816 .magic = TTY_LDISC_MAGIC,
2817 .name = "n_gsm",
2818 .open = gsmld_open,
2819 .close = gsmld_close,
2820 .flush_buffer = gsmld_flush_buffer,
2821 .chars_in_buffer = gsmld_chars_in_buffer,
2822 .read = gsmld_read,
2823 .write = gsmld_write,
2824 .ioctl = gsmld_ioctl,
2825 .poll = gsmld_poll,
2826 .receive_buf = gsmld_receive_buf,
2827 .write_wakeup = gsmld_write_wakeup
2828};
2829
2830/*
2831 * Virtual tty side
2832 */
2833
2834#define TX_SIZE 512
2835
2836static int gsmtty_modem_update(struct gsm_dlci *dlci, u8 brk)
2837{
2838 u8 modembits[5];
2839 struct gsm_control *ctrl;
2840 int len = 2;
2841
2842 if (brk)
2843 len++;
2844
2845 modembits[0] = len << 1 | EA; /* Data bytes */
2846 modembits[1] = dlci->addr << 2 | 3; /* DLCI, EA, 1 */
2847 modembits[2] = gsm_encode_modem(dlci) << 1 | EA;
2848 if (brk)
2849 modembits[3] = brk << 4 | 2 | EA; /* Valid, EA */
2850 ctrl = gsm_control_send(dlci->gsm, CMD_MSC, modembits, len + 1);
2851 if (ctrl == NULL)
2852 return -ENOMEM;
2853 return gsm_control_wait(dlci->gsm, ctrl);
2854}
2855
2856static int gsm_carrier_raised(struct tty_port *port)
2857{
2858 struct gsm_dlci *dlci = container_of(port, struct gsm_dlci, port);
2859 /* Not yet open so no carrier info */
2860 if (dlci->state != DLCI_OPEN)
2861 return 0;
2862 if (debug & 2)
2863 return 1;
2864 return dlci->modem_rx & TIOCM_CD;
2865}
2866
2867static void gsm_dtr_rts(struct tty_port *port, int onoff)
2868{
2869 struct gsm_dlci *dlci = container_of(port, struct gsm_dlci, port);
2870 unsigned int modem_tx = dlci->modem_tx;
2871 if (onoff)
2872 modem_tx |= TIOCM_DTR | TIOCM_RTS;
2873 else
2874 modem_tx &= ~(TIOCM_DTR | TIOCM_RTS);
2875 if (modem_tx != dlci->modem_tx) {
2876 dlci->modem_tx = modem_tx;
2877 gsmtty_modem_update(dlci, 0);
2878 }
2879}
2880
2881static const struct tty_port_operations gsm_port_ops = {
2882 .carrier_raised = gsm_carrier_raised,
2883 .dtr_rts = gsm_dtr_rts,
2884};
2885
Jiri Slaby86176ed2012-08-07 21:47:28 +02002886static int gsmtty_install(struct tty_driver *driver, struct tty_struct *tty)
Alan Coxe1eaea42010-03-26 11:32:54 +00002887{
2888 struct gsm_mux *gsm;
2889 struct gsm_dlci *dlci;
Alan Coxe1eaea42010-03-26 11:32:54 +00002890 unsigned int line = tty->index;
2891 unsigned int mux = line >> 6;
Jiri Slaby86176ed2012-08-07 21:47:28 +02002892 bool alloc = false;
2893 int ret;
Alan Coxe1eaea42010-03-26 11:32:54 +00002894
2895 line = line & 0x3F;
2896
2897 if (mux >= MAX_MUX)
2898 return -ENXIO;
2899 /* FIXME: we need to lock gsm_mux for lifetimes of ttys eventually */
2900 if (gsm_mux[mux] == NULL)
2901 return -EUNATCH;
2902 if (line == 0 || line > 61) /* 62/63 reserved */
2903 return -ECHRNG;
2904 gsm = gsm_mux[mux];
2905 if (gsm->dead)
2906 return -EL2HLT;
xiaojin7e8ac7b2012-08-13 13:43:15 +01002907 /* If DLCI 0 is not yet fully open return an error. This is ok from a locking
2908 perspective as we don't have to worry about this if DLCI0 is lost */
2909 if (gsm->dlci[0] && gsm->dlci[0]->state != DLCI_OPEN)
2910 return -EL2NSYNC;
Alan Coxe1eaea42010-03-26 11:32:54 +00002911 dlci = gsm->dlci[line];
Jiri Slaby86176ed2012-08-07 21:47:28 +02002912 if (dlci == NULL) {
2913 alloc = true;
Alan Coxe1eaea42010-03-26 11:32:54 +00002914 dlci = gsm_dlci_alloc(gsm, line);
Jiri Slaby86176ed2012-08-07 21:47:28 +02002915 }
Alan Coxe1eaea42010-03-26 11:32:54 +00002916 if (dlci == NULL)
2917 return -ENOMEM;
Jiri Slaby86176ed2012-08-07 21:47:28 +02002918 ret = tty_port_install(&dlci->port, driver, tty);
2919 if (ret) {
2920 if (alloc)
2921 dlci_put(dlci);
2922 return ret;
2923 }
2924
Alan Coxe1eaea42010-03-26 11:32:54 +00002925 tty->driver_data = dlci;
Jiri Slaby86176ed2012-08-07 21:47:28 +02002926
2927 return 0;
2928}
2929
2930static int gsmtty_open(struct tty_struct *tty, struct file *filp)
2931{
2932 struct gsm_dlci *dlci = tty->driver_data;
2933 struct tty_port *port = &dlci->port;
2934
2935 port->count++;
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002936 dlci_get(dlci);
2937 dlci_get(dlci->gsm->dlci[0]);
2938 mux_get(dlci->gsm);
Alan Coxe1eaea42010-03-26 11:32:54 +00002939 tty_port_tty_set(port, tty);
2940
2941 dlci->modem_rx = 0;
2942 /* We could in theory open and close before we wait - eg if we get
2943 a DM straight back. This is ok as that will have caused a hangup */
2944 set_bit(ASYNCB_INITIALIZED, &port->flags);
2945 /* Start sending off SABM messages */
2946 gsm_dlci_begin_open(dlci);
2947 /* And wait for virtual carrier */
2948 return tty_port_block_til_ready(port, tty, filp);
2949}
2950
2951static void gsmtty_close(struct tty_struct *tty, struct file *filp)
2952{
2953 struct gsm_dlci *dlci = tty->driver_data;
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002954 struct gsm_mux *gsm;
2955
Alan Coxe1eaea42010-03-26 11:32:54 +00002956 if (dlci == NULL)
2957 return;
Russ Gorbybcd5abe2011-06-16 14:20:12 -07002958 mutex_lock(&dlci->mutex);
2959 gsm_destroy_network(dlci);
2960 mutex_unlock(&dlci->mutex);
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002961 gsm = dlci->gsm;
Alan Coxe1eaea42010-03-26 11:32:54 +00002962 if (tty_port_close_start(&dlci->port, tty, filp) == 0)
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002963 goto out;
Alan Coxe1eaea42010-03-26 11:32:54 +00002964 gsm_dlci_begin_close(dlci);
2965 tty_port_close_end(&dlci->port, tty);
2966 tty_port_tty_set(&dlci->port, NULL);
Russ Gorby6ab8fba2011-06-16 14:20:13 -07002967out:
2968 dlci_put(dlci);
2969 dlci_put(gsm->dlci[0]);
2970 mux_put(gsm);
Alan Coxe1eaea42010-03-26 11:32:54 +00002971}
2972
2973static void gsmtty_hangup(struct tty_struct *tty)
2974{
2975 struct gsm_dlci *dlci = tty->driver_data;
2976 tty_port_hangup(&dlci->port);
2977 gsm_dlci_begin_close(dlci);
2978}
2979
2980static int gsmtty_write(struct tty_struct *tty, const unsigned char *buf,
2981 int len)
2982{
2983 struct gsm_dlci *dlci = tty->driver_data;
2984 /* Stuff the bytes into the fifo queue */
2985 int sent = kfifo_in_locked(dlci->fifo, buf, len, &dlci->lock);
2986 /* Need to kick the channel */
2987 gsm_dlci_data_kick(dlci);
2988 return sent;
2989}
2990
2991static int gsmtty_write_room(struct tty_struct *tty)
2992{
2993 struct gsm_dlci *dlci = tty->driver_data;
2994 return TX_SIZE - kfifo_len(dlci->fifo);
2995}
2996
2997static int gsmtty_chars_in_buffer(struct tty_struct *tty)
2998{
2999 struct gsm_dlci *dlci = tty->driver_data;
3000 return kfifo_len(dlci->fifo);
3001}
3002
3003static void gsmtty_flush_buffer(struct tty_struct *tty)
3004{
3005 struct gsm_dlci *dlci = tty->driver_data;
3006 /* Caution needed: If we implement reliable transport classes
3007 then the data being transmitted can't simply be junked once
3008 it has first hit the stack. Until then we can just blow it
3009 away */
3010 kfifo_reset(dlci->fifo);
3011 /* Need to unhook this DLCI from the transmit queue logic */
3012}
3013
3014static void gsmtty_wait_until_sent(struct tty_struct *tty, int timeout)
3015{
3016 /* The FIFO handles the queue so the kernel will do the right
3017 thing waiting on chars_in_buffer before calling us. No work
3018 to do here */
3019}
3020
Alan Cox60b33c12011-02-14 16:26:14 +00003021static int gsmtty_tiocmget(struct tty_struct *tty)
Alan Coxe1eaea42010-03-26 11:32:54 +00003022{
3023 struct gsm_dlci *dlci = tty->driver_data;
3024 return dlci->modem_rx;
3025}
3026
Alan Cox20b9d172011-02-14 16:26:50 +00003027static int gsmtty_tiocmset(struct tty_struct *tty,
Alan Coxe1eaea42010-03-26 11:32:54 +00003028 unsigned int set, unsigned int clear)
3029{
3030 struct gsm_dlci *dlci = tty->driver_data;
3031 unsigned int modem_tx = dlci->modem_tx;
3032
Nikola Diklic-Perincf168072011-09-23 10:59:43 +02003033 modem_tx &= ~clear;
Alan Coxe1eaea42010-03-26 11:32:54 +00003034 modem_tx |= set;
3035
3036 if (modem_tx != dlci->modem_tx) {
3037 dlci->modem_tx = modem_tx;
3038 return gsmtty_modem_update(dlci, 0);
3039 }
3040 return 0;
3041}
3042
3043
Alan Cox6caa76b2011-02-14 16:27:22 +00003044static int gsmtty_ioctl(struct tty_struct *tty,
Alan Coxe1eaea42010-03-26 11:32:54 +00003045 unsigned int cmd, unsigned long arg)
3046{
Russ Gorbybcd5abe2011-06-16 14:20:12 -07003047 struct gsm_dlci *dlci = tty->driver_data;
3048 struct gsm_netconfig nc;
3049 int index;
3050
3051 switch (cmd) {
3052 case GSMIOC_ENABLE_NET:
3053 if (copy_from_user(&nc, (void __user *)arg, sizeof(nc)))
3054 return -EFAULT;
3055 nc.if_name[IFNAMSIZ-1] = '\0';
3056 /* return net interface index or error code */
3057 mutex_lock(&dlci->mutex);
3058 index = gsm_create_network(dlci, &nc);
3059 mutex_unlock(&dlci->mutex);
3060 if (copy_to_user((void __user *)arg, &nc, sizeof(nc)))
3061 return -EFAULT;
3062 return index;
3063 case GSMIOC_DISABLE_NET:
3064 if (!capable(CAP_NET_ADMIN))
3065 return -EPERM;
3066 mutex_lock(&dlci->mutex);
3067 gsm_destroy_network(dlci);
3068 mutex_unlock(&dlci->mutex);
3069 return 0;
3070 default:
3071 return -ENOIOCTLCMD;
3072 }
Alan Coxe1eaea42010-03-26 11:32:54 +00003073}
3074
3075static void gsmtty_set_termios(struct tty_struct *tty, struct ktermios *old)
3076{
3077 /* For the moment its fixed. In actual fact the speed information
3078 for the virtual channel can be propogated in both directions by
3079 the RPN control message. This however rapidly gets nasty as we
3080 then have to remap modem signals each way according to whether
3081 our virtual cable is null modem etc .. */
Alan Coxadc8d742012-07-14 15:31:47 +01003082 tty_termios_copy_hw(&tty->termios, old);
Alan Coxe1eaea42010-03-26 11:32:54 +00003083}
3084
3085static void gsmtty_throttle(struct tty_struct *tty)
3086{
3087 struct gsm_dlci *dlci = tty->driver_data;
Alan Coxadc8d742012-07-14 15:31:47 +01003088 if (tty->termios.c_cflag & CRTSCTS)
Alan Coxe1eaea42010-03-26 11:32:54 +00003089 dlci->modem_tx &= ~TIOCM_DTR;
3090 dlci->throttled = 1;
3091 /* Send an MSC with DTR cleared */
3092 gsmtty_modem_update(dlci, 0);
3093}
3094
3095static void gsmtty_unthrottle(struct tty_struct *tty)
3096{
3097 struct gsm_dlci *dlci = tty->driver_data;
Alan Coxadc8d742012-07-14 15:31:47 +01003098 if (tty->termios.c_cflag & CRTSCTS)
Alan Coxe1eaea42010-03-26 11:32:54 +00003099 dlci->modem_tx |= TIOCM_DTR;
3100 dlci->throttled = 0;
3101 /* Send an MSC with DTR set */
3102 gsmtty_modem_update(dlci, 0);
3103}
3104
3105static int gsmtty_break_ctl(struct tty_struct *tty, int state)
3106{
3107 struct gsm_dlci *dlci = tty->driver_data;
3108 int encode = 0; /* Off */
3109
3110 if (state == -1) /* "On indefinitely" - we can't encode this
3111 properly */
3112 encode = 0x0F;
3113 else if (state > 0) {
3114 encode = state / 200; /* mS to encoding */
3115 if (encode > 0x0F)
3116 encode = 0x0F; /* Best effort */
3117 }
3118 return gsmtty_modem_update(dlci, encode);
3119}
3120
Alan Coxe1eaea42010-03-26 11:32:54 +00003121
3122/* Virtual ttys for the demux */
3123static const struct tty_operations gsmtty_ops = {
Jiri Slaby86176ed2012-08-07 21:47:28 +02003124 .install = gsmtty_install,
Alan Coxe1eaea42010-03-26 11:32:54 +00003125 .open = gsmtty_open,
3126 .close = gsmtty_close,
3127 .write = gsmtty_write,
3128 .write_room = gsmtty_write_room,
3129 .chars_in_buffer = gsmtty_chars_in_buffer,
3130 .flush_buffer = gsmtty_flush_buffer,
3131 .ioctl = gsmtty_ioctl,
3132 .throttle = gsmtty_throttle,
3133 .unthrottle = gsmtty_unthrottle,
3134 .set_termios = gsmtty_set_termios,
3135 .hangup = gsmtty_hangup,
3136 .wait_until_sent = gsmtty_wait_until_sent,
3137 .tiocmget = gsmtty_tiocmget,
3138 .tiocmset = gsmtty_tiocmset,
3139 .break_ctl = gsmtty_break_ctl,
3140};
3141
3142
3143
3144static int __init gsm_init(void)
3145{
3146 /* Fill in our line protocol discipline, and register it */
3147 int status = tty_register_ldisc(N_GSM0710, &tty_ldisc_packet);
3148 if (status != 0) {
Alan Cox5f9a31d2010-11-04 15:17:27 +00003149 pr_err("n_gsm: can't register line discipline (err = %d)\n",
3150 status);
Alan Coxe1eaea42010-03-26 11:32:54 +00003151 return status;
3152 }
3153
3154 gsm_tty_driver = alloc_tty_driver(256);
3155 if (!gsm_tty_driver) {
3156 tty_unregister_ldisc(N_GSM0710);
Alan Cox5f9a31d2010-11-04 15:17:27 +00003157 pr_err("gsm_init: tty allocation failed.\n");
Alan Coxe1eaea42010-03-26 11:32:54 +00003158 return -EINVAL;
3159 }
Alan Coxe1eaea42010-03-26 11:32:54 +00003160 gsm_tty_driver->driver_name = "gsmtty";
3161 gsm_tty_driver->name = "gsmtty";
3162 gsm_tty_driver->major = 0; /* Dynamic */
3163 gsm_tty_driver->minor_start = 0;
3164 gsm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
3165 gsm_tty_driver->subtype = SERIAL_TYPE_NORMAL;
3166 gsm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV
Alan Cox5f9a31d2010-11-04 15:17:27 +00003167 | TTY_DRIVER_HARDWARE_BREAK;
Alan Coxe1eaea42010-03-26 11:32:54 +00003168 gsm_tty_driver->init_termios = tty_std_termios;
3169 /* Fixme */
3170 gsm_tty_driver->init_termios.c_lflag &= ~ECHO;
3171 tty_set_operations(gsm_tty_driver, &gsmtty_ops);
3172
3173 spin_lock_init(&gsm_mux_lock);
3174
3175 if (tty_register_driver(gsm_tty_driver)) {
3176 put_tty_driver(gsm_tty_driver);
3177 tty_unregister_ldisc(N_GSM0710);
Alan Cox5f9a31d2010-11-04 15:17:27 +00003178 pr_err("gsm_init: tty registration failed.\n");
Alan Coxe1eaea42010-03-26 11:32:54 +00003179 return -EBUSY;
3180 }
Alan Cox5f9a31d2010-11-04 15:17:27 +00003181 pr_debug("gsm_init: loaded as %d,%d.\n",
3182 gsm_tty_driver->major, gsm_tty_driver->minor_start);
Alan Coxe1eaea42010-03-26 11:32:54 +00003183 return 0;
3184}
3185
3186static void __exit gsm_exit(void)
3187{
3188 int status = tty_unregister_ldisc(N_GSM0710);
3189 if (status != 0)
Alan Cox5f9a31d2010-11-04 15:17:27 +00003190 pr_err("n_gsm: can't unregister line discipline (err = %d)\n",
3191 status);
Alan Coxe1eaea42010-03-26 11:32:54 +00003192 tty_unregister_driver(gsm_tty_driver);
3193 put_tty_driver(gsm_tty_driver);
Alan Coxe1eaea42010-03-26 11:32:54 +00003194}
3195
3196module_init(gsm_init);
3197module_exit(gsm_exit);
3198
3199
3200MODULE_LICENSE("GPL");
3201MODULE_ALIAS_LDISC(N_GSM0710);