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