1 // SPDX-License-Identifier: GPL-2.0
3 * n_gsm.c GSM 0710 tty multiplexor
4 * Copyright (c) 2009/10 Intel Corporation
6 * * THIS IS A DEVELOPMENT SNAPSHOT IT IS NOT A FINAL RELEASE *
9 * tty -> DLCI fifo -> scheduler -> GSM MUX data queue ---o-> ldisc
10 * control message -> GSM MUX control queue --ยด
13 * ldisc -> gsm_queue() -o--> tty
14 * `-> gsm_control_response()
17 * Mostly done: ioctls for setting modes/timing
18 * Partly done: hooks so you can pull off frames to non tty devs
19 * Restart DLCI 0 when it closes ?
20 * Improve the tx engine
21 * Resolve tx side locking by adding a queue_head and routing
22 * all control traffic via it
23 * General tidy/document
24 * Review the locking/move to refcounts more (mux now moved to an
25 * alloc/free model ready)
26 * Use newest tty open/close port helpers and install hooks
27 * What to do about power functions ?
28 * Termios setting and negotiation
29 * Do we need a 'which mux are you' ioctl to correlate mux and tty sets
33 #include <linux/types.h>
34 #include <linux/major.h>
35 #include <linux/errno.h>
36 #include <linux/signal.h>
37 #include <linux/fcntl.h>
38 #include <linux/sched/signal.h>
39 #include <linux/interrupt.h>
40 #include <linux/tty.h>
41 #include <linux/bitfield.h>
42 #include <linux/ctype.h>
44 #include <linux/math.h>
45 #include <linux/string.h>
46 #include <linux/slab.h>
47 #include <linux/poll.h>
48 #include <linux/bitops.h>
49 #include <linux/file.h>
50 #include <linux/uaccess.h>
51 #include <linux/module.h>
52 #include <linux/timer.h>
53 #include <linux/tty_flip.h>
54 #include <linux/tty_driver.h>
55 #include <linux/serial.h>
56 #include <linux/kfifo.h>
57 #include <linux/skbuff.h>
60 #include <linux/netdevice.h>
61 #include <linux/etherdevice.h>
62 #include <linux/gsmmux.h>
66 module_param(debug, int, 0600);
68 /* Module debug bits */
69 #define DBG_DUMP BIT(0) /* Data transmission dump. */
70 #define DBG_CD_ON BIT(1) /* Always assume CD line on. */
71 #define DBG_DATA BIT(2) /* Data transmission details. */
72 #define DBG_ERRORS BIT(3) /* Details for fail conditions. */
73 #define DBG_TTY BIT(4) /* Transmission statistics for DLCI TTYs. */
74 #define DBG_PAYLOAD BIT(5) /* Limits DBG_DUMP to payload frames. */
76 /* Defaults: these are from the specification */
78 #define T1 10 /* 100mS */
79 #define T2 34 /* 333mS */
80 #define T3 10 /* 10s */
81 #define N2 3 /* Retry 3 times */
82 #define K 2 /* outstanding I frames */
84 #define MAX_T3 255 /* In seconds. */
85 #define MAX_WINDOW_SIZE 7 /* Limit of K in error recovery mode. */
87 /* Use long timers for testing at low speed with debug on */
94 * Semi-arbitrary buffer size limits. 0710 is normally run with 32-64 byte
95 * limits so this is plenty
99 #define MIN_MTU (PROT_OVERHEAD + 1)
100 /* SOF, ADDR, CTRL, LEN1, LEN2, ..., FCS, EOF */
101 #define PROT_OVERHEAD 7
102 #define GSM_NET_TX_TIMEOUT (HZ*10)
105 * struct gsm_mux_net - network interface
107 * Created when net interface is initialized.
111 struct gsm_dlci *dlci;
115 * Each block of data we have queued to go out is in the form of
116 * a gsm_msg which holds everything we need in a link layer independent
121 struct list_head list;
122 u8 addr; /* DLCI address + flags */
123 u8 ctrl; /* Control byte + flags */
124 unsigned int len; /* Length of data block (can be zero) */
125 unsigned char *data; /* Points into buffer but not at the start */
126 unsigned char buffer[];
129 enum gsm_dlci_state {
131 DLCI_CONFIGURE, /* Sending PN (for adaption > 1) */
132 DLCI_OPENING, /* Sending SABM not seen UA */
133 DLCI_OPEN, /* SABM/UA complete */
134 DLCI_CLOSING, /* Sending DISC not seen UA/DM */
138 DLCI_MODE_ABM, /* Normal Asynchronous Balanced Mode */
139 DLCI_MODE_ADM, /* Asynchronous Disconnected Mode */
143 * Each active data link has a gsm_dlci structure associated which ties
144 * the link layer to an optional tty (if the tty side is open). To avoid
145 * complexity right now these are only ever freed up when the mux is
148 * At the moment we don't free DLCI objects until the mux is torn down
149 * this avoid object life time issues but might be worth review later.
155 enum gsm_dlci_state state;
159 enum gsm_dlci_mode mode;
160 spinlock_t lock; /* Protects the internal state */
161 struct timer_list t1; /* Retransmit timer for SABM and UA */
163 /* Uplink tty if active */
164 struct tty_port port; /* The tty bound to this DLCI if there is one */
165 #define TX_SIZE 4096 /* Must be power of 2. */
166 struct kfifo fifo; /* Queue fifo for the DLCI */
167 int adaption; /* Adaption layer in use */
169 u32 modem_rx; /* Our incoming virtual modem lines */
170 u32 modem_tx; /* Our outgoing modem lines */
172 bool dead; /* Refuse re-open */
174 u8 prio; /* Priority */
175 u8 ftype; /* Frame type */
176 u8 k; /* Window size */
178 bool throttled; /* Private copy of throttle state */
179 bool constipated; /* Throttle status for outgoing */
181 struct sk_buff *skb; /* Frame being sent */
182 struct sk_buff_head skb_list; /* Queued frames */
183 /* Data handling callback */
184 void (*data)(struct gsm_dlci *dlci, const u8 *data, int len);
185 void (*prev_data)(struct gsm_dlci *dlci, const u8 *data, int len);
186 struct net_device *net; /* network interface, if created */
190 * Parameter bits used for parameter negotiation according to 3GPP 27.010
194 struct gsm_dlci_param_bits {
204 static_assert(sizeof(struct gsm_dlci_param_bits) == 8);
206 #define PN_D_FIELD_DLCI GENMASK(5, 0)
207 #define PN_I_CL_FIELD_FTYPE GENMASK(3, 0)
208 #define PN_I_CL_FIELD_ADAPTION GENMASK(7, 4)
209 #define PN_P_FIELD_PRIO GENMASK(5, 0)
210 #define PN_T_FIELD_T1 GENMASK(7, 0)
211 #define PN_N_FIELD_N1 GENMASK(15, 0)
212 #define PN_NA_FIELD_N2 GENMASK(7, 0)
213 #define PN_K_FIELD_K GENMASK(2, 0)
215 /* Total number of supported devices */
216 #define GSM_TTY_MINORS 256
218 /* DLCI 0, 62/63 are special or reserved see gsmtty_open */
223 * DLCI 0 is used to pass control blocks out of band of the data
224 * flow (and with a higher link priority). One command can be outstanding
225 * at a time and we use this structure to manage them. They are created
226 * and destroyed by the user context, and updated by the receive paths
231 u8 cmd; /* Command we are issuing */
232 u8 *data; /* Data for the command in case we retransmit */
233 int len; /* Length of block for retransmission */
234 int done; /* Done flag */
235 int error; /* Error if any */
258 * Each GSM mux we have is represented by this structure. If we are
259 * operating as an ldisc then we use this structure as our ldisc
260 * state. We need to sort out lifetimes and locking with respect
261 * to the gsm mux array. For now we don't free DLCI objects that
262 * have been instantiated until the mux itself is terminated.
264 * To consider further: tty open versus mux shutdown.
268 struct tty_struct *tty; /* The tty our ldisc is bound to */
274 /* Events on the GSM channel */
275 wait_queue_head_t event;
277 /* ldisc send work */
278 struct work_struct tx_work;
280 /* Bits for GSM mode decoding */
284 enum gsm_mux_state state;
286 unsigned int address;
289 enum gsm_encoding encoding;
292 u8 *txframe; /* TX framing buffer */
294 /* Method for the receiver side */
295 void (*receive)(struct gsm_mux *gsm, u8 ch);
300 int initiator; /* Did we initiate connection */
301 bool dead; /* Has the mux been shut down */
302 struct gsm_dlci *dlci[NUM_DLCI];
303 int old_c_iflag; /* termios c_iflag value before attach */
304 bool constipated; /* Asked by remote to shut up */
305 bool has_devices; /* Devices were registered */
308 unsigned int tx_bytes; /* TX data outstanding */
309 #define TX_THRESH_HI 8192
310 #define TX_THRESH_LO 2048
311 struct list_head tx_ctrl_list; /* Pending control packets */
312 struct list_head tx_data_list; /* Pending data packets */
314 /* Control messages */
315 struct timer_list kick_timer; /* Kick TX queuing on timeout */
316 struct timer_list t2_timer; /* Retransmit timer for commands */
317 int cretries; /* Command retry counter */
318 struct gsm_control *pending_cmd;/* Our current pending command */
319 spinlock_t control_lock; /* Protects the pending command */
322 struct timer_list ka_timer; /* Keep-alive response timer */
323 u8 ka_num; /* Keep-alive match pattern */
324 signed int ka_retries; /* Keep-alive retry counter, -1 if not yet initialized */
327 int adaption; /* 1 or 2 supported */
328 u8 ftype; /* UI or UIH */
329 int t1, t2; /* Timers in 1/100th of a sec */
330 unsigned int t3; /* Power wake-up timer in seconds. */
331 int n2; /* Retry count */
332 u8 k; /* Window size */
333 u32 keep_alive; /* Control channel keep-alive in 10ms */
335 /* Statistics (not currently exposed) */
336 unsigned long bad_fcs;
337 unsigned long malformed;
338 unsigned long io_error;
339 unsigned long bad_size;
340 unsigned long unsupported;
345 * Mux objects - needed so that we can translate a tty index into the
346 * relevant mux and DLCI.
349 #define MAX_MUX 4 /* 256 minors */
350 static struct gsm_mux *gsm_mux[MAX_MUX]; /* GSM muxes */
351 static DEFINE_SPINLOCK(gsm_mux_lock);
353 static struct tty_driver *gsm_tty_driver;
356 * This section of the driver logic implements the GSM encodings
357 * both the basic and the 'advanced'. Reliable transport is not
365 /* I is special: the rest are ..*/
376 /* Channel commands */
378 #define CMD_TEST 0x11
381 #define CMD_FCOFF 0x31
384 #define CMD_FCON 0x51
389 /* Virtual modem bits */
396 #define GSM0_SOF 0xF9
397 #define GSM1_SOF 0x7E
398 #define GSM1_ESCAPE 0x7D
399 #define GSM1_ESCAPE_BITS 0x20
402 #define ISO_IEC_646_MASK 0x7F
404 static const struct tty_port_operations gsm_port_ops;
407 * CRC table for GSM 0710
410 static const u8 gsm_fcs8[256] = {
411 0x00, 0x91, 0xE3, 0x72, 0x07, 0x96, 0xE4, 0x75,
412 0x0E, 0x9F, 0xED, 0x7C, 0x09, 0x98, 0xEA, 0x7B,
413 0x1C, 0x8D, 0xFF, 0x6E, 0x1B, 0x8A, 0xF8, 0x69,
414 0x12, 0x83, 0xF1, 0x60, 0x15, 0x84, 0xF6, 0x67,
415 0x38, 0xA9, 0xDB, 0x4A, 0x3F, 0xAE, 0xDC, 0x4D,
416 0x36, 0xA7, 0xD5, 0x44, 0x31, 0xA0, 0xD2, 0x43,
417 0x24, 0xB5, 0xC7, 0x56, 0x23, 0xB2, 0xC0, 0x51,
418 0x2A, 0xBB, 0xC9, 0x58, 0x2D, 0xBC, 0xCE, 0x5F,
419 0x70, 0xE1, 0x93, 0x02, 0x77, 0xE6, 0x94, 0x05,
420 0x7E, 0xEF, 0x9D, 0x0C, 0x79, 0xE8, 0x9A, 0x0B,
421 0x6C, 0xFD, 0x8F, 0x1E, 0x6B, 0xFA, 0x88, 0x19,
422 0x62, 0xF3, 0x81, 0x10, 0x65, 0xF4, 0x86, 0x17,
423 0x48, 0xD9, 0xAB, 0x3A, 0x4F, 0xDE, 0xAC, 0x3D,
424 0x46, 0xD7, 0xA5, 0x34, 0x41, 0xD0, 0xA2, 0x33,
425 0x54, 0xC5, 0xB7, 0x26, 0x53, 0xC2, 0xB0, 0x21,
426 0x5A, 0xCB, 0xB9, 0x28, 0x5D, 0xCC, 0xBE, 0x2F,
427 0xE0, 0x71, 0x03, 0x92, 0xE7, 0x76, 0x04, 0x95,
428 0xEE, 0x7F, 0x0D, 0x9C, 0xE9, 0x78, 0x0A, 0x9B,
429 0xFC, 0x6D, 0x1F, 0x8E, 0xFB, 0x6A, 0x18, 0x89,
430 0xF2, 0x63, 0x11, 0x80, 0xF5, 0x64, 0x16, 0x87,
431 0xD8, 0x49, 0x3B, 0xAA, 0xDF, 0x4E, 0x3C, 0xAD,
432 0xD6, 0x47, 0x35, 0xA4, 0xD1, 0x40, 0x32, 0xA3,
433 0xC4, 0x55, 0x27, 0xB6, 0xC3, 0x52, 0x20, 0xB1,
434 0xCA, 0x5B, 0x29, 0xB8, 0xCD, 0x5C, 0x2E, 0xBF,
435 0x90, 0x01, 0x73, 0xE2, 0x97, 0x06, 0x74, 0xE5,
436 0x9E, 0x0F, 0x7D, 0xEC, 0x99, 0x08, 0x7A, 0xEB,
437 0x8C, 0x1D, 0x6F, 0xFE, 0x8B, 0x1A, 0x68, 0xF9,
438 0x82, 0x13, 0x61, 0xF0, 0x85, 0x14, 0x66, 0xF7,
439 0xA8, 0x39, 0x4B, 0xDA, 0xAF, 0x3E, 0x4C, 0xDD,
440 0xA6, 0x37, 0x45, 0xD4, 0xA1, 0x30, 0x42, 0xD3,
441 0xB4, 0x25, 0x57, 0xC6, 0xB3, 0x22, 0x50, 0xC1,
442 0xBA, 0x2B, 0x59, 0xC8, 0xBD, 0x2C, 0x5E, 0xCF
445 #define INIT_FCS 0xFF
446 #define GOOD_FCS 0xCF
448 static void gsm_dlci_close(struct gsm_dlci *dlci);
449 static int gsmld_output(struct gsm_mux *gsm, u8 *data, int len);
450 static int gsm_modem_update(struct gsm_dlci *dlci, u8 brk);
451 static struct gsm_msg *gsm_data_alloc(struct gsm_mux *gsm, u8 addr, int len,
453 static int gsm_send_packet(struct gsm_mux *gsm, struct gsm_msg *msg);
454 static void gsmld_write_trigger(struct gsm_mux *gsm);
455 static void gsmld_write_task(struct work_struct *work);
458 * gsm_fcs_add - update FCS
462 * Update the FCS to include c. Uses the algorithm in the specification
466 static inline u8 gsm_fcs_add(u8 fcs, u8 c)
468 return gsm_fcs8[fcs ^ c];
472 * gsm_fcs_add_block - update FCS for a block
475 * @len: length of buffer
477 * Update the FCS to include c. Uses the algorithm in the specification
481 static inline u8 gsm_fcs_add_block(u8 fcs, u8 *c, int len)
484 fcs = gsm_fcs8[fcs ^ *c++];
489 * gsm_read_ea - read a byte into an EA
490 * @val: variable holding value
491 * @c: byte going into the EA
493 * Processes one byte of an EA. Updates the passed variable
494 * and returns 1 if the EA is now completely read
497 static int gsm_read_ea(unsigned int *val, u8 c)
499 /* Add the next 7 bits into the value */
502 /* Was this the last byte of the EA 1 = yes*/
507 * gsm_read_ea_val - read a value until EA
508 * @val: variable holding value
509 * @data: buffer of data
510 * @dlen: length of data
512 * Processes an EA value. Updates the passed variable and
513 * returns the processed data length.
515 static unsigned int gsm_read_ea_val(unsigned int *val, const u8 *data, int dlen)
517 unsigned int len = 0;
519 for (; dlen > 0; dlen--) {
521 if (gsm_read_ea(val, *data++))
528 * gsm_encode_modem - encode modem data bits
529 * @dlci: DLCI to encode from
531 * Returns the correct GSM encoded modem status bits (6 bit field) for
532 * the current status of the DLCI and attached tty object
535 static u8 gsm_encode_modem(const struct gsm_dlci *dlci)
538 /* FC is true flow control not modem bits */
541 if (dlci->modem_tx & TIOCM_DTR)
542 modembits |= MDM_RTC;
543 if (dlci->modem_tx & TIOCM_RTS)
544 modembits |= MDM_RTR;
545 if (dlci->modem_tx & TIOCM_RI)
547 if (dlci->modem_tx & TIOCM_CD || dlci->gsm->initiator)
549 /* special mappings for passive side to operate as UE */
550 if (dlci->modem_tx & TIOCM_OUT1)
552 if (dlci->modem_tx & TIOCM_OUT2)
557 static void gsm_hex_dump_bytes(const char *fname, const u8 *data,
563 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE, 16, 1, data, len,
568 prefix = kasprintf(GFP_ATOMIC, "%s: ", fname);
571 print_hex_dump(KERN_INFO, prefix, DUMP_PREFIX_OFFSET, 16, 1, data, len,
577 * gsm_encode_params - encode DLCI parameters
578 * @dlci: DLCI to encode from
579 * @params: buffer to fill with the encoded parameters
581 * Encodes the parameters according to GSM 07.10 section 5.4.6.3.1
584 static int gsm_encode_params(const struct gsm_dlci *dlci,
585 struct gsm_dlci_param_bits *params)
587 const struct gsm_mux *gsm = dlci->gsm;
590 switch (dlci->ftype) {
598 pr_debug("unsupported frame type %d\n", dlci->ftype);
602 switch (dlci->adaption) {
603 case 1: /* Unstructured */
604 cl = 0; /* convergence layer type 1 */
606 case 2: /* Unstructured with modem bits. */
607 cl = 1; /* convergence layer type 2 */
610 pr_debug("unsupported adaption %d\n", dlci->adaption);
614 params->d_bits = FIELD_PREP(PN_D_FIELD_DLCI, dlci->addr);
615 /* UIH, convergence layer type 1 */
616 params->i_cl_bits = FIELD_PREP(PN_I_CL_FIELD_FTYPE, i) |
617 FIELD_PREP(PN_I_CL_FIELD_ADAPTION, cl);
618 params->p_bits = FIELD_PREP(PN_P_FIELD_PRIO, dlci->prio);
619 params->t_bits = FIELD_PREP(PN_T_FIELD_T1, gsm->t1);
620 params->n_bits = cpu_to_le16(FIELD_PREP(PN_N_FIELD_N1, dlci->mtu));
621 params->na_bits = FIELD_PREP(PN_NA_FIELD_N2, gsm->n2);
622 params->k_bits = FIELD_PREP(PN_K_FIELD_K, dlci->k);
628 * gsm_register_devices - register all tty devices for a given mux index
630 * @driver: the tty driver that describes the tty devices
631 * @index: the mux number is used to calculate the minor numbers of the
632 * ttys for this mux and may differ from the position in the
635 static int gsm_register_devices(struct tty_driver *driver, unsigned int index)
641 if (!driver || index >= MAX_MUX)
644 base = index * NUM_DLCI; /* first minor for this index */
645 for (i = 1; i < NUM_DLCI; i++) {
646 /* Don't register device 0 - this is the control channel
647 * and not a usable tty interface
649 dev = tty_register_device(gsm_tty_driver, base + i, NULL);
651 if (debug & DBG_ERRORS)
652 pr_info("%s failed to register device minor %u",
654 for (i--; i >= 1; i--)
655 tty_unregister_device(gsm_tty_driver, base + i);
664 * gsm_unregister_devices - unregister all tty devices for a given mux index
666 * @driver: the tty driver that describes the tty devices
667 * @index: the mux number is used to calculate the minor numbers of the
668 * ttys for this mux and may differ from the position in the
671 static void gsm_unregister_devices(struct tty_driver *driver,
677 if (!driver || index >= MAX_MUX)
680 base = index * NUM_DLCI; /* first minor for this index */
681 for (i = 1; i < NUM_DLCI; i++) {
682 /* Don't unregister device 0 - this is the control
683 * channel and not a usable tty interface
685 tty_unregister_device(gsm_tty_driver, base + i);
690 * gsm_print_packet - display a frame for debug
691 * @hdr: header to print before decode
692 * @addr: address EA from the frame
693 * @cr: C/R bit seen as initiator
694 * @control: control including PF bit
695 * @data: following data bytes
696 * @dlen: length of data
698 * Displays a packet in human readable format for debugging purposes. The
699 * style is based on amateur radio LAP-B dump display.
702 static void gsm_print_packet(const char *hdr, int addr, int cr,
703 u8 control, const u8 *data, int dlen)
705 if (!(debug & DBG_DUMP))
707 /* Only show user payload frames if debug & DBG_PAYLOAD */
708 if (!(debug & DBG_PAYLOAD) && addr != 0)
709 if ((control & ~PF) == UI || (control & ~PF) == UIH)
712 pr_info("%s %d) %c: ", hdr, addr, "RC"[cr]);
714 switch (control & ~PF) {
734 if (!(control & 0x01)) {
735 pr_cont("I N(S)%d N(R)%d",
736 (control & 0x0E) >> 1, (control & 0xE0) >> 5);
737 } else switch (control & 0x0F) {
739 pr_cont("RR(%d)", (control & 0xE0) >> 5);
742 pr_cont("RNR(%d)", (control & 0xE0) >> 5);
745 pr_cont("REJ(%d)", (control & 0xE0) >> 5);
748 pr_cont("[%02X]", control);
757 gsm_hex_dump_bytes(NULL, data, dlen);
762 * Link level transmission side
766 * gsm_stuff_frame - bytestuff a packet
767 * @input: input buffer
768 * @output: output buffer
769 * @len: length of input
771 * Expand a buffer by bytestuffing it. The worst case size change
772 * is doubling and the caller is responsible for handing out
773 * suitable sized buffers.
776 static int gsm_stuff_frame(const u8 *input, u8 *output, int len)
780 if (*input == GSM1_SOF || *input == GSM1_ESCAPE
781 || (*input & ISO_IEC_646_MASK) == XON
782 || (*input & ISO_IEC_646_MASK) == XOFF) {
783 *output++ = GSM1_ESCAPE;
784 *output++ = *input++ ^ GSM1_ESCAPE_BITS;
787 *output++ = *input++;
794 * gsm_send - send a control frame
796 * @addr: address for control frame
797 * @cr: command/response bit seen as initiator
798 * @control: control byte including PF bit
800 * Format up and transmit a control frame. These should be transmitted
801 * ahead of data when they are needed.
803 static int gsm_send(struct gsm_mux *gsm, int addr, int cr, int control)
810 msg = gsm_data_alloc(gsm, addr, 0, control);
814 /* toggle C/R coding if not initiator */
815 ocr = cr ^ (gsm->initiator ? 0 : 1);
819 *dp++ = (addr << 2) | (ocr << 1) | EA;
822 if (gsm->encoding == GSM_BASIC_OPT)
823 *dp++ = EA; /* Length of data = 0 */
825 *dp = 0xFF - gsm_fcs_add_block(INIT_FCS, msg->data, dp - msg->data);
826 msg->len = (dp - msg->data) + 1;
828 gsm_print_packet("Q->", addr, cr, control, NULL, 0);
830 spin_lock_irqsave(&gsm->tx_lock, flags);
831 list_add_tail(&msg->list, &gsm->tx_ctrl_list);
832 gsm->tx_bytes += msg->len;
833 spin_unlock_irqrestore(&gsm->tx_lock, flags);
834 gsmld_write_trigger(gsm);
840 * gsm_dlci_clear_queues - remove outstanding data for a DLCI
842 * @dlci: clear for this DLCI
844 * Clears the data queues for a given DLCI.
846 static void gsm_dlci_clear_queues(struct gsm_mux *gsm, struct gsm_dlci *dlci)
848 struct gsm_msg *msg, *nmsg;
849 int addr = dlci->addr;
852 /* Clear DLCI write fifo first */
853 spin_lock_irqsave(&dlci->lock, flags);
854 kfifo_reset(&dlci->fifo);
855 spin_unlock_irqrestore(&dlci->lock, flags);
857 /* Clear data packets in MUX write queue */
858 spin_lock_irqsave(&gsm->tx_lock, flags);
859 list_for_each_entry_safe(msg, nmsg, &gsm->tx_data_list, list) {
860 if (msg->addr != addr)
862 gsm->tx_bytes -= msg->len;
863 list_del(&msg->list);
866 spin_unlock_irqrestore(&gsm->tx_lock, flags);
870 * gsm_response - send a control response
872 * @addr: address for control frame
873 * @control: control byte including PF bit
875 * Format up and transmit a link level response frame.
878 static inline void gsm_response(struct gsm_mux *gsm, int addr, int control)
880 gsm_send(gsm, addr, 0, control);
884 * gsm_command - send a control command
886 * @addr: address for control frame
887 * @control: control byte including PF bit
889 * Format up and transmit a link level command frame.
892 static inline void gsm_command(struct gsm_mux *gsm, int addr, int control)
894 gsm_send(gsm, addr, 1, control);
897 /* Data transmission */
899 #define HDR_LEN 6 /* ADDR CTRL [LEN.2] DATA FCS */
902 * gsm_data_alloc - allocate data frame
904 * @addr: DLCI address
905 * @len: length excluding header and FCS
906 * @ctrl: control byte
908 * Allocate a new data buffer for sending frames with data. Space is left
909 * at the front for header bytes but that is treated as an implementation
910 * detail and not for the high level code to use
913 static struct gsm_msg *gsm_data_alloc(struct gsm_mux *gsm, u8 addr, int len,
916 struct gsm_msg *m = kmalloc(sizeof(struct gsm_msg) + len + HDR_LEN,
920 m->data = m->buffer + HDR_LEN - 1; /* Allow for FCS */
924 INIT_LIST_HEAD(&m->list);
929 * gsm_send_packet - sends a single packet
931 * @msg: packet to send
933 * The given packet is encoded and sent out. No memory is freed.
934 * The caller must hold the gsm tx lock.
936 static int gsm_send_packet(struct gsm_mux *gsm, struct gsm_msg *msg)
941 if (gsm->encoding == GSM_BASIC_OPT) {
942 gsm->txframe[0] = GSM0_SOF;
943 memcpy(gsm->txframe + 1, msg->data, msg->len);
944 gsm->txframe[msg->len + 1] = GSM0_SOF;
947 gsm->txframe[0] = GSM1_SOF;
948 len = gsm_stuff_frame(msg->data, gsm->txframe + 1, msg->len);
949 gsm->txframe[len + 1] = GSM1_SOF;
953 if (debug & DBG_DATA)
954 gsm_hex_dump_bytes(__func__, gsm->txframe, len);
955 gsm_print_packet("-->", msg->addr, gsm->initiator, msg->ctrl, msg->data,
958 ret = gsmld_output(gsm, gsm->txframe, len);
961 /* FIXME: Can eliminate one SOF in many more cases */
962 gsm->tx_bytes -= msg->len;
968 * gsm_is_flow_ctrl_msg - checks if flow control message
969 * @msg: message to check
971 * Returns true if the given message is a flow control command of the
972 * control channel. False is returned in any other case.
974 static bool gsm_is_flow_ctrl_msg(struct gsm_msg *msg)
981 switch (msg->ctrl & ~PF) {
985 if (gsm_read_ea_val(&cmd, msg->data + 2, msg->len - 2) < 1)
999 * gsm_data_kick - poke the queue
1002 * The tty device has called us to indicate that room has appeared in
1003 * the transmit queue. Ram more data into the pipe if we have any.
1004 * If we have been flow-stopped by a CMD_FCOFF, then we can only
1005 * send messages on DLCI0 until CMD_FCON. The caller must hold
1008 static int gsm_data_kick(struct gsm_mux *gsm)
1010 struct gsm_msg *msg, *nmsg;
1011 struct gsm_dlci *dlci;
1014 clear_bit(TTY_DO_WRITE_WAKEUP, &gsm->tty->flags);
1016 /* Serialize control messages and control channel messages first */
1017 list_for_each_entry_safe(msg, nmsg, &gsm->tx_ctrl_list, list) {
1018 if (gsm->constipated && !gsm_is_flow_ctrl_msg(msg))
1020 ret = gsm_send_packet(gsm, msg);
1025 /* ldisc not open */
1026 gsm->tx_bytes -= msg->len;
1027 list_del(&msg->list);
1032 list_del(&msg->list);
1039 if (gsm->constipated)
1042 /* Serialize other channels */
1043 if (list_empty(&gsm->tx_data_list))
1045 list_for_each_entry_safe(msg, nmsg, &gsm->tx_data_list, list) {
1046 dlci = gsm->dlci[msg->addr];
1047 /* Send only messages for DLCIs with valid state */
1048 if (dlci->state != DLCI_OPEN) {
1049 gsm->tx_bytes -= msg->len;
1050 list_del(&msg->list);
1054 ret = gsm_send_packet(gsm, msg);
1059 /* ldisc not open */
1060 gsm->tx_bytes -= msg->len;
1061 list_del(&msg->list);
1066 list_del(&msg->list);
1077 * __gsm_data_queue - queue a UI or UIH frame
1078 * @dlci: DLCI sending the data
1079 * @msg: message queued
1081 * Add data to the transmit queue and try and get stuff moving
1082 * out of the mux tty if not already doing so. The Caller must hold
1086 static void __gsm_data_queue(struct gsm_dlci *dlci, struct gsm_msg *msg)
1088 struct gsm_mux *gsm = dlci->gsm;
1090 u8 *fcs = dp + msg->len;
1092 /* Fill in the header */
1093 if (gsm->encoding == GSM_BASIC_OPT) {
1095 *--dp = (msg->len << 1) | EA;
1097 *--dp = (msg->len >> 7); /* bits 7 - 15 */
1098 *--dp = (msg->len & 127) << 1; /* bits 0 - 6 */
1104 *--dp = (msg->addr << 2) | CR | EA;
1106 *--dp = (msg->addr << 2) | EA;
1107 *fcs = gsm_fcs_add_block(INIT_FCS, dp , msg->data - dp);
1108 /* Ugly protocol layering violation */
1109 if (msg->ctrl == UI || msg->ctrl == (UI|PF))
1110 *fcs = gsm_fcs_add_block(*fcs, msg->data, msg->len);
1113 gsm_print_packet("Q> ", msg->addr, gsm->initiator, msg->ctrl,
1114 msg->data, msg->len);
1116 /* Move the header back and adjust the length, also allow for the FCS
1117 now tacked on the end */
1118 msg->len += (msg->data - dp) + 1;
1121 /* Add to the actual output queue */
1122 switch (msg->ctrl & ~PF) {
1125 if (msg->addr > 0) {
1126 list_add_tail(&msg->list, &gsm->tx_data_list);
1131 list_add_tail(&msg->list, &gsm->tx_ctrl_list);
1134 gsm->tx_bytes += msg->len;
1136 gsmld_write_trigger(gsm);
1137 mod_timer(&gsm->kick_timer, jiffies + 10 * gsm->t1 * HZ / 100);
1141 * gsm_data_queue - queue a UI or UIH frame
1142 * @dlci: DLCI sending the data
1143 * @msg: message queued
1145 * Add data to the transmit queue and try and get stuff moving
1146 * out of the mux tty if not already doing so. Take the
1147 * the gsm tx lock and dlci lock.
1150 static void gsm_data_queue(struct gsm_dlci *dlci, struct gsm_msg *msg)
1152 unsigned long flags;
1153 spin_lock_irqsave(&dlci->gsm->tx_lock, flags);
1154 __gsm_data_queue(dlci, msg);
1155 spin_unlock_irqrestore(&dlci->gsm->tx_lock, flags);
1159 * gsm_dlci_data_output - try and push data out of a DLCI
1161 * @dlci: the DLCI to pull data from
1163 * Pull data from a DLCI and send it into the transmit queue if there
1164 * is data. Keep to the MRU of the mux. This path handles the usual tty
1165 * interface which is a byte stream with optional modem data.
1167 * Caller must hold the tx_lock of the mux.
1170 static int gsm_dlci_data_output(struct gsm_mux *gsm, struct gsm_dlci *dlci)
1172 struct gsm_msg *msg;
1176 /* for modem bits without break data */
1177 h = ((dlci->adaption == 1) ? 0 : 1);
1179 len = kfifo_len(&dlci->fifo);
1183 /* MTU/MRU count only the data bits but watch adaption mode */
1184 if ((len + h) > dlci->mtu)
1185 len = dlci->mtu - h;
1189 msg = gsm_data_alloc(gsm, dlci->addr, size, dlci->ftype);
1193 switch (dlci->adaption) {
1194 case 1: /* Unstructured */
1196 case 2: /* Unstructured with modem bits.
1197 * Always one byte as we never send inline break data
1199 *dp++ = (gsm_encode_modem(dlci) << 1) | EA;
1202 pr_err("%s: unsupported adaption %d\n", __func__,
1207 WARN_ON(len != kfifo_out_locked(&dlci->fifo, dp, len,
1210 /* Notify upper layer about available send space. */
1211 tty_port_tty_wakeup(&dlci->port);
1213 __gsm_data_queue(dlci, msg);
1214 /* Bytes of data we used up */
1219 * gsm_dlci_data_output_framed - try and push data out of a DLCI
1221 * @dlci: the DLCI to pull data from
1223 * Pull data from a DLCI and send it into the transmit queue if there
1224 * is data. Keep to the MRU of the mux. This path handles framed data
1225 * queued as skbuffs to the DLCI.
1227 * Caller must hold the tx_lock of the mux.
1230 static int gsm_dlci_data_output_framed(struct gsm_mux *gsm,
1231 struct gsm_dlci *dlci)
1233 struct gsm_msg *msg;
1236 int last = 0, first = 0;
1239 /* One byte per frame is used for B/F flags */
1240 if (dlci->adaption == 4)
1243 /* dlci->skb is locked by tx_lock */
1244 if (dlci->skb == NULL) {
1245 dlci->skb = skb_dequeue_tail(&dlci->skb_list);
1246 if (dlci->skb == NULL)
1250 len = dlci->skb->len + overhead;
1252 /* MTU/MRU count only the data bits */
1253 if (len > dlci->mtu) {
1254 if (dlci->adaption == 3) {
1255 /* Over long frame, bin it */
1256 dev_kfree_skb_any(dlci->skb);
1264 size = len + overhead;
1265 msg = gsm_data_alloc(gsm, dlci->addr, size, dlci->ftype);
1267 skb_queue_tail(&dlci->skb_list, dlci->skb);
1273 if (dlci->adaption == 4) { /* Interruptible framed (Packetised Data) */
1274 /* Flag byte to carry the start/end info */
1275 *dp++ = last << 7 | first << 6 | 1; /* EA */
1278 memcpy(dp, dlci->skb->data, len);
1279 skb_pull(dlci->skb, len);
1280 __gsm_data_queue(dlci, msg);
1282 dev_kfree_skb_any(dlci->skb);
1289 * gsm_dlci_modem_output - try and push modem status out of a DLCI
1291 * @dlci: the DLCI to pull modem status from
1292 * @brk: break signal
1294 * Push an empty frame in to the transmit queue to update the modem status
1295 * bits and to transmit an optional break.
1297 * Caller must hold the tx_lock of the mux.
1300 static int gsm_dlci_modem_output(struct gsm_mux *gsm, struct gsm_dlci *dlci,
1304 struct gsm_msg *msg;
1307 /* for modem bits without break data */
1308 switch (dlci->adaption) {
1309 case 1: /* Unstructured */
1311 case 2: /* Unstructured with modem bits. */
1317 pr_err("%s: unsupported adaption %d\n", __func__,
1322 msg = gsm_data_alloc(gsm, dlci->addr, size, dlci->ftype);
1324 pr_err("%s: gsm_data_alloc error", __func__);
1328 switch (dlci->adaption) {
1329 case 1: /* Unstructured */
1331 case 2: /* Unstructured with modem bits. */
1333 *dp++ = (gsm_encode_modem(dlci) << 1) | EA;
1335 *dp++ = gsm_encode_modem(dlci) << 1;
1336 *dp++ = (brk << 4) | 2 | EA; /* Length, Break, EA */
1344 __gsm_data_queue(dlci, msg);
1349 * gsm_dlci_data_sweep - look for data to send
1352 * Sweep the GSM mux channels in priority order looking for ones with
1353 * data to send. We could do with optimising this scan a bit. We aim
1354 * to fill the queue totally or up to TX_THRESH_HI bytes. Once we hit
1355 * TX_THRESH_LO we get called again
1357 * FIXME: We should round robin between groups and in theory you can
1358 * renegotiate DLCI priorities with optional stuff. Needs optimising.
1361 static int gsm_dlci_data_sweep(struct gsm_mux *gsm)
1363 /* Priority ordering: We should do priority with RR of the groups */
1364 int i, len, ret = 0;
1366 struct gsm_dlci *dlci;
1368 while (gsm->tx_bytes < TX_THRESH_HI) {
1369 for (sent = false, i = 1; i < NUM_DLCI; i++) {
1370 dlci = gsm->dlci[i];
1371 /* skip unused or blocked channel */
1372 if (!dlci || dlci->constipated)
1374 /* skip channels with invalid state */
1375 if (dlci->state != DLCI_OPEN)
1377 /* count the sent data per adaption */
1378 if (dlci->adaption < 3 && !dlci->net)
1379 len = gsm_dlci_data_output(gsm, dlci);
1381 len = gsm_dlci_data_output_framed(gsm, dlci);
1388 /* The lower DLCs can starve the higher DLCs! */
1401 * gsm_dlci_data_kick - transmit if possible
1402 * @dlci: DLCI to kick
1404 * Transmit data from this DLCI if the queue is empty. We can't rely on
1405 * a tty wakeup except when we filled the pipe so we need to fire off
1406 * new data ourselves in other cases.
1409 static void gsm_dlci_data_kick(struct gsm_dlci *dlci)
1411 unsigned long flags;
1414 if (dlci->constipated)
1417 spin_lock_irqsave(&dlci->gsm->tx_lock, flags);
1418 /* If we have nothing running then we need to fire up */
1419 sweep = (dlci->gsm->tx_bytes < TX_THRESH_LO);
1420 if (dlci->gsm->tx_bytes == 0) {
1422 gsm_dlci_data_output_framed(dlci->gsm, dlci);
1424 gsm_dlci_data_output(dlci->gsm, dlci);
1427 gsm_dlci_data_sweep(dlci->gsm);
1428 spin_unlock_irqrestore(&dlci->gsm->tx_lock, flags);
1432 * Control message processing
1437 * gsm_control_command - send a command frame to a control
1439 * @cmd: the command to use
1440 * @data: data to follow encoded info
1441 * @dlen: length of data
1443 * Encode up and queue a UI/UIH frame containing our command.
1445 static int gsm_control_command(struct gsm_mux *gsm, int cmd, const u8 *data,
1448 struct gsm_msg *msg;
1450 msg = gsm_data_alloc(gsm, 0, dlen + 2, gsm->dlci[0]->ftype);
1454 msg->data[0] = (cmd << 1) | CR | EA; /* Set C/R */
1455 msg->data[1] = (dlen << 1) | EA;
1456 memcpy(msg->data + 2, data, dlen);
1457 gsm_data_queue(gsm->dlci[0], msg);
1463 * gsm_control_reply - send a response frame to a control
1465 * @cmd: the command to use
1466 * @data: data to follow encoded info
1467 * @dlen: length of data
1469 * Encode up and queue a UI/UIH frame containing our response.
1472 static void gsm_control_reply(struct gsm_mux *gsm, int cmd, const u8 *data,
1475 struct gsm_msg *msg;
1477 msg = gsm_data_alloc(gsm, 0, dlen + 2, gsm->dlci[0]->ftype);
1480 msg->data[0] = (cmd & 0xFE) << 1 | EA; /* Clear C/R */
1481 msg->data[1] = (dlen << 1) | EA;
1482 memcpy(msg->data + 2, data, dlen);
1483 gsm_data_queue(gsm->dlci[0], msg);
1487 * gsm_process_modem - process received modem status
1488 * @tty: virtual tty bound to the DLCI
1489 * @dlci: DLCI to affect
1490 * @modem: modem bits (full EA)
1491 * @slen: number of signal octets
1493 * Used when a modem control message or line state inline in adaption
1494 * layer 2 is processed. Sort out the local modem state and throttles
1497 static void gsm_process_modem(struct tty_struct *tty, struct gsm_dlci *dlci,
1498 u32 modem, int slen)
1504 /* The modem status command can either contain one octet (V.24 signals)
1505 * or two octets (V.24 signals + break signals). This is specified in
1506 * section 5.4.6.3.7 of the 07.10 mux spec.
1510 modem = modem & 0x7f;
1513 modem = (modem >> 7) & 0x7f;
1516 /* Flow control/ready to communicate */
1517 fc = (modem & MDM_FC) || !(modem & MDM_RTR);
1518 if (fc && !dlci->constipated) {
1519 /* Need to throttle our output on this device */
1520 dlci->constipated = true;
1521 } else if (!fc && dlci->constipated) {
1522 dlci->constipated = false;
1523 gsm_dlci_data_kick(dlci);
1526 /* Map modem bits */
1527 if (modem & MDM_RTC)
1528 mlines |= TIOCM_DSR | TIOCM_DTR;
1529 if (modem & MDM_RTR)
1530 mlines |= TIOCM_RTS | TIOCM_CTS;
1536 /* Carrier drop -> hangup */
1538 if ((mlines & TIOCM_CD) == 0 && (dlci->modem_rx & TIOCM_CD))
1543 tty_insert_flip_char(&dlci->port, 0, TTY_BREAK);
1544 dlci->modem_rx = mlines;
1545 wake_up_interruptible(&dlci->gsm->event);
1549 * gsm_process_negotiation - process received parameters
1551 * @addr: DLCI address
1552 * @cr: command/response
1553 * @params: encoded parameters from the parameter negotiation message
1555 * Used when the response for our parameter negotiation command was
1558 static int gsm_process_negotiation(struct gsm_mux *gsm, unsigned int addr,
1560 const struct gsm_dlci_param_bits *params)
1562 struct gsm_dlci *dlci = gsm->dlci[addr];
1563 unsigned int ftype, i, adaption, prio, n1, k;
1565 i = FIELD_GET(PN_I_CL_FIELD_FTYPE, params->i_cl_bits);
1566 adaption = FIELD_GET(PN_I_CL_FIELD_ADAPTION, params->i_cl_bits) + 1;
1567 prio = FIELD_GET(PN_P_FIELD_PRIO, params->p_bits);
1568 n1 = FIELD_GET(PN_N_FIELD_N1, get_unaligned_le16(¶ms->n_bits));
1569 k = FIELD_GET(PN_K_FIELD_K, params->k_bits);
1572 if (debug & DBG_ERRORS)
1573 pr_info("%s N1 out of range in PN\n", __func__);
1584 case 0x02: /* I frames are not supported */
1585 if (debug & DBG_ERRORS)
1586 pr_info("%s unsupported I frame request in PN\n",
1590 if (debug & DBG_ERRORS)
1591 pr_info("%s i out of range in PN\n", __func__);
1595 if (!cr && gsm->initiator) {
1596 if (adaption != dlci->adaption) {
1597 if (debug & DBG_ERRORS)
1598 pr_info("%s invalid adaption %d in PN\n",
1599 __func__, adaption);
1602 if (prio != dlci->prio) {
1603 if (debug & DBG_ERRORS)
1604 pr_info("%s invalid priority %d in PN",
1608 if (n1 > gsm->mru || n1 > dlci->mtu) {
1609 /* We requested a frame size but the other party wants
1610 * to send larger frames. The standard allows only a
1611 * smaller response value than requested (5.4.6.3.1).
1613 if (debug & DBG_ERRORS)
1614 pr_info("%s invalid N1 %d in PN\n", __func__,
1619 if (ftype != dlci->ftype) {
1620 if (debug & DBG_ERRORS)
1621 pr_info("%s invalid i %d in PN\n", __func__, i);
1624 if (ftype != UI && ftype != UIH && k > dlci->k) {
1625 if (debug & DBG_ERRORS)
1626 pr_info("%s invalid k %d in PN\n", __func__, k);
1630 } else if (cr && !gsm->initiator) {
1631 /* Only convergence layer type 1 and 2 are supported. */
1632 if (adaption != 1 && adaption != 2) {
1633 if (debug & DBG_ERRORS)
1634 pr_info("%s invalid adaption %d in PN\n",
1635 __func__, adaption);
1638 dlci->adaption = adaption;
1639 if (n1 > gsm->mru) {
1640 /* Propose a smaller value */
1641 dlci->mtu = gsm->mru;
1642 } else if (n1 > MAX_MTU) {
1643 /* Propose a smaller value */
1644 dlci->mtu = MAX_MTU;
1649 dlci->ftype = ftype;
1659 * gsm_control_modem - modem status received
1661 * @data: data following command
1662 * @clen: command length
1664 * We have received a modem status control message. This is used by
1665 * the GSM mux protocol to pass virtual modem line status and optionally
1666 * to indicate break signals. Unpack it, convert to Linux representation
1667 * and if need be stuff a break message down the tty.
1670 static void gsm_control_modem(struct gsm_mux *gsm, const u8 *data, int clen)
1672 unsigned int addr = 0;
1673 unsigned int modem = 0;
1674 struct gsm_dlci *dlci;
1677 const u8 *dp = data;
1678 struct tty_struct *tty;
1680 len = gsm_read_ea_val(&addr, data, cl);
1685 /* Closed port, or invalid ? */
1686 if (addr == 0 || addr >= NUM_DLCI || gsm->dlci[addr] == NULL)
1688 dlci = gsm->dlci[addr];
1690 /* Must be at least one byte following the EA */
1697 /* get the modem status */
1698 len = gsm_read_ea_val(&modem, dp, cl);
1702 tty = tty_port_tty_get(&dlci->port);
1703 gsm_process_modem(tty, dlci, modem, cl);
1708 gsm_control_reply(gsm, CMD_MSC, data, clen);
1712 * gsm_control_negotiation - parameter negotiation received
1714 * @cr: command/response flag
1715 * @data: data following command
1716 * @dlen: data length
1718 * We have received a parameter negotiation message. This is used by
1719 * the GSM mux protocol to configure protocol parameters for a new DLCI.
1721 static void gsm_control_negotiation(struct gsm_mux *gsm, unsigned int cr,
1722 const u8 *data, unsigned int dlen)
1725 struct gsm_dlci_param_bits pn_reply;
1726 struct gsm_dlci *dlci;
1727 struct gsm_dlci_param_bits *params;
1729 if (dlen < sizeof(struct gsm_dlci_param_bits))
1733 params = (struct gsm_dlci_param_bits *)data;
1734 addr = FIELD_GET(PN_D_FIELD_DLCI, params->d_bits);
1735 if (addr == 0 || addr >= NUM_DLCI || !gsm->dlci[addr])
1737 dlci = gsm->dlci[addr];
1739 /* Too late for parameter negotiation? */
1740 if ((!cr && dlci->state == DLCI_OPENING) || dlci->state == DLCI_OPEN)
1743 /* Process the received parameters */
1744 if (gsm_process_negotiation(gsm, addr, cr, params) != 0) {
1745 /* Negotiation failed. Close the link. */
1746 if (debug & DBG_ERRORS)
1747 pr_info("%s PN failed\n", __func__);
1748 gsm_dlci_close(dlci);
1753 /* Reply command with accepted parameters. */
1754 if (gsm_encode_params(dlci, &pn_reply) == 0)
1755 gsm_control_reply(gsm, CMD_PN, (const u8 *)&pn_reply,
1757 else if (debug & DBG_ERRORS)
1758 pr_info("%s PN invalid\n", __func__);
1759 } else if (dlci->state == DLCI_CONFIGURE) {
1760 /* Proceed with link setup by sending SABM before UA */
1761 dlci->state = DLCI_OPENING;
1762 gsm_command(gsm, dlci->addr, SABM|PF);
1763 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
1765 if (debug & DBG_ERRORS)
1766 pr_info("%s PN in invalid state\n", __func__);
1771 * gsm_control_rls - remote line status
1774 * @clen: data length
1776 * The modem sends us a two byte message on the control channel whenever
1777 * it wishes to send us an error state from the virtual link. Stuff
1778 * this into the uplink tty if present
1781 static void gsm_control_rls(struct gsm_mux *gsm, const u8 *data, int clen)
1783 struct tty_port *port;
1784 unsigned int addr = 0;
1787 const u8 *dp = data;
1789 while (gsm_read_ea(&addr, *dp++) == 0) {
1794 /* Must be at least one byte following ea */
1799 /* Closed port, or invalid ? */
1800 if (addr == 0 || addr >= NUM_DLCI || gsm->dlci[addr] == NULL)
1804 if ((bits & 1) == 0)
1807 port = &gsm->dlci[addr]->port;
1810 tty_insert_flip_char(port, 0, TTY_OVERRUN);
1812 tty_insert_flip_char(port, 0, TTY_PARITY);
1814 tty_insert_flip_char(port, 0, TTY_FRAME);
1816 tty_flip_buffer_push(port);
1818 gsm_control_reply(gsm, CMD_RLS, data, clen);
1821 static void gsm_dlci_begin_close(struct gsm_dlci *dlci);
1824 * gsm_control_message - DLCI 0 control processing
1826 * @command: the command EA
1827 * @data: data beyond the command/length EAs
1830 * Input processor for control messages from the other end of the link.
1831 * Processes the incoming request and queues a response frame or an
1832 * NSC response if not supported
1835 static void gsm_control_message(struct gsm_mux *gsm, unsigned int command,
1836 const u8 *data, int clen)
1842 struct gsm_dlci *dlci = gsm->dlci[0];
1843 /* Modem wishes to close down */
1847 gsm_dlci_begin_close(dlci);
1852 /* Modem wishes to test, reply with the data */
1853 gsm_control_reply(gsm, CMD_TEST, data, clen);
1856 /* Modem can accept data again */
1857 gsm->constipated = false;
1858 gsm_control_reply(gsm, CMD_FCON, NULL, 0);
1859 /* Kick the link in case it is idling */
1860 gsmld_write_trigger(gsm);
1863 /* Modem wants us to STFU */
1864 gsm->constipated = true;
1865 gsm_control_reply(gsm, CMD_FCOFF, NULL, 0);
1868 /* Out of band modem line change indicator for a DLCI */
1869 gsm_control_modem(gsm, data, clen);
1872 /* Out of band error reception for a DLCI */
1873 gsm_control_rls(gsm, data, clen);
1876 /* Modem wishes to enter power saving state */
1877 gsm_control_reply(gsm, CMD_PSC, NULL, 0);
1879 /* Optional commands */
1881 /* Modem sends a parameter negotiation command */
1882 gsm_control_negotiation(gsm, 1, data, clen);
1884 /* Optional unsupported commands */
1885 case CMD_RPN: /* Remote port negotiation */
1886 case CMD_SNC: /* Service negotiation command */
1888 /* Reply to bad commands with an NSC */
1890 gsm_control_reply(gsm, CMD_NSC, buf, 1);
1896 * gsm_control_response - process a response to our control
1898 * @command: the command (response) EA
1899 * @data: data beyond the command/length EA
1902 * Process a response to an outstanding command. We only allow a single
1903 * control message in flight so this is fairly easy. All the clean up
1904 * is done by the caller, we just update the fields, flag it as done
1908 static void gsm_control_response(struct gsm_mux *gsm, unsigned int command,
1909 const u8 *data, int clen)
1911 struct gsm_control *ctrl;
1912 struct gsm_dlci *dlci;
1913 unsigned long flags;
1915 spin_lock_irqsave(&gsm->control_lock, flags);
1917 ctrl = gsm->pending_cmd;
1918 dlci = gsm->dlci[0];
1920 /* Does the reply match our command */
1921 if (ctrl != NULL && (command == ctrl->cmd || command == CMD_NSC)) {
1922 /* Our command was replied to, kill the retry timer */
1923 del_timer(&gsm->t2_timer);
1924 gsm->pending_cmd = NULL;
1925 /* Rejected by the other end */
1926 if (command == CMD_NSC)
1927 ctrl->error = -EOPNOTSUPP;
1929 wake_up(&gsm->event);
1930 /* Or did we receive the PN response to our PN command */
1931 } else if (command == CMD_PN) {
1932 gsm_control_negotiation(gsm, 0, data, clen);
1933 /* Or did we receive the TEST response to our TEST command */
1934 } else if (command == CMD_TEST && clen == 1 && *data == gsm->ka_num) {
1935 gsm->ka_retries = -1; /* trigger new keep-alive message */
1936 if (dlci && !dlci->dead)
1937 mod_timer(&gsm->ka_timer, jiffies + gsm->keep_alive * HZ / 100);
1939 spin_unlock_irqrestore(&gsm->control_lock, flags);
1943 * gsm_control_keep_alive - check timeout or start keep-alive
1944 * @t: timer contained in our gsm object
1946 * Called off the keep-alive timer expiry signaling that our link
1947 * partner is not responding anymore. Link will be closed.
1948 * This is also called to startup our timer.
1951 static void gsm_control_keep_alive(struct timer_list *t)
1953 struct gsm_mux *gsm = from_timer(gsm, t, ka_timer);
1954 unsigned long flags;
1956 spin_lock_irqsave(&gsm->control_lock, flags);
1957 if (gsm->ka_num && gsm->ka_retries == 0) {
1958 /* Keep-alive expired -> close the link */
1959 if (debug & DBG_ERRORS)
1960 pr_debug("%s keep-alive timed out\n", __func__);
1961 spin_unlock_irqrestore(&gsm->control_lock, flags);
1963 gsm_dlci_begin_close(gsm->dlci[0]);
1965 } else if (gsm->keep_alive && gsm->dlci[0] && !gsm->dlci[0]->dead) {
1966 if (gsm->ka_retries > 0) {
1967 /* T2 expired for keep-alive -> resend */
1970 /* Start keep-alive timer */
1974 gsm->ka_retries = (signed int)gsm->n2;
1976 gsm_control_command(gsm, CMD_TEST, &gsm->ka_num,
1977 sizeof(gsm->ka_num));
1978 mod_timer(&gsm->ka_timer,
1979 jiffies + gsm->t2 * HZ / 100);
1981 spin_unlock_irqrestore(&gsm->control_lock, flags);
1985 * gsm_control_transmit - send control packet
1987 * @ctrl: frame to send
1989 * Send out a pending control command (called under control lock)
1992 static void gsm_control_transmit(struct gsm_mux *gsm, struct gsm_control *ctrl)
1994 gsm_control_command(gsm, ctrl->cmd, ctrl->data, ctrl->len);
1998 * gsm_control_retransmit - retransmit a control frame
1999 * @t: timer contained in our gsm object
2001 * Called off the T2 timer expiry in order to retransmit control frames
2002 * that have been lost in the system somewhere. The control_lock protects
2003 * us from colliding with another sender or a receive completion event.
2004 * In that situation the timer may still occur in a small window but
2005 * gsm->pending_cmd will be NULL and we just let the timer expire.
2008 static void gsm_control_retransmit(struct timer_list *t)
2010 struct gsm_mux *gsm = from_timer(gsm, t, t2_timer);
2011 struct gsm_control *ctrl;
2012 unsigned long flags;
2013 spin_lock_irqsave(&gsm->control_lock, flags);
2014 ctrl = gsm->pending_cmd;
2016 if (gsm->cretries == 0 || !gsm->dlci[0] || gsm->dlci[0]->dead) {
2017 gsm->pending_cmd = NULL;
2018 ctrl->error = -ETIMEDOUT;
2020 spin_unlock_irqrestore(&gsm->control_lock, flags);
2021 wake_up(&gsm->event);
2025 gsm_control_transmit(gsm, ctrl);
2026 mod_timer(&gsm->t2_timer, jiffies + gsm->t2 * HZ / 100);
2028 spin_unlock_irqrestore(&gsm->control_lock, flags);
2032 * gsm_control_send - send a control frame on DLCI 0
2033 * @gsm: the GSM channel
2034 * @command: command to send including CR bit
2035 * @data: bytes of data (must be kmalloced)
2036 * @clen: length of the block to send
2038 * Queue and dispatch a control command. Only one command can be
2039 * active at a time. In theory more can be outstanding but the matching
2040 * gets really complicated so for now stick to one outstanding.
2043 static struct gsm_control *gsm_control_send(struct gsm_mux *gsm,
2044 unsigned int command, u8 *data, int clen)
2046 struct gsm_control *ctrl = kzalloc(sizeof(struct gsm_control),
2048 unsigned long flags;
2052 wait_event(gsm->event, gsm->pending_cmd == NULL);
2053 spin_lock_irqsave(&gsm->control_lock, flags);
2054 if (gsm->pending_cmd != NULL) {
2055 spin_unlock_irqrestore(&gsm->control_lock, flags);
2058 ctrl->cmd = command;
2061 gsm->pending_cmd = ctrl;
2063 /* If DLCI0 is in ADM mode skip retries, it won't respond */
2064 if (gsm->dlci[0]->mode == DLCI_MODE_ADM)
2067 gsm->cretries = gsm->n2;
2069 mod_timer(&gsm->t2_timer, jiffies + gsm->t2 * HZ / 100);
2070 gsm_control_transmit(gsm, ctrl);
2071 spin_unlock_irqrestore(&gsm->control_lock, flags);
2076 * gsm_control_wait - wait for a control to finish
2078 * @control: control we are waiting on
2080 * Waits for the control to complete or time out. Frees any used
2081 * resources and returns 0 for success, or an error if the remote
2082 * rejected or ignored the request.
2085 static int gsm_control_wait(struct gsm_mux *gsm, struct gsm_control *control)
2088 wait_event(gsm->event, control->done == 1);
2089 err = control->error;
2096 * DLCI level handling: Needs krefs
2100 * State transitions and timers
2104 * gsm_dlci_close - a DLCI has closed
2105 * @dlci: DLCI that closed
2107 * Perform processing when moving a DLCI into closed state. If there
2108 * is an attached tty this is hung up
2111 static void gsm_dlci_close(struct gsm_dlci *dlci)
2113 del_timer(&dlci->t1);
2114 if (debug & DBG_ERRORS)
2115 pr_debug("DLCI %d goes closed.\n", dlci->addr);
2116 dlci->state = DLCI_CLOSED;
2117 /* Prevent us from sending data before the link is up again */
2118 dlci->constipated = true;
2119 if (dlci->addr != 0) {
2120 tty_port_tty_hangup(&dlci->port, false);
2121 gsm_dlci_clear_queues(dlci->gsm, dlci);
2122 /* Ensure that gsmtty_open() can return. */
2123 tty_port_set_initialized(&dlci->port, false);
2124 wake_up_interruptible(&dlci->port.open_wait);
2126 del_timer(&dlci->gsm->ka_timer);
2127 dlci->gsm->dead = true;
2129 /* A DLCI 0 close is a MUX termination so we need to kick that
2130 back to userspace somehow */
2131 gsm_dlci_data_kick(dlci);
2132 wake_up_all(&dlci->gsm->event);
2136 * gsm_dlci_open - a DLCI has opened
2137 * @dlci: DLCI that opened
2139 * Perform processing when moving a DLCI into open state.
2142 static void gsm_dlci_open(struct gsm_dlci *dlci)
2144 struct gsm_mux *gsm = dlci->gsm;
2146 /* Note that SABM UA .. SABM UA first UA lost can mean that we go
2148 del_timer(&dlci->t1);
2149 /* This will let a tty open continue */
2150 dlci->state = DLCI_OPEN;
2151 dlci->constipated = false;
2152 if (debug & DBG_ERRORS)
2153 pr_debug("DLCI %d goes open.\n", dlci->addr);
2154 /* Send current modem state */
2156 gsm_modem_update(dlci, 0);
2158 /* Start keep-alive control */
2160 gsm->ka_retries = -1;
2161 mod_timer(&gsm->ka_timer,
2162 jiffies + gsm->keep_alive * HZ / 100);
2164 gsm_dlci_data_kick(dlci);
2165 wake_up(&dlci->gsm->event);
2169 * gsm_dlci_negotiate - start parameter negotiation
2170 * @dlci: DLCI to open
2172 * Starts the parameter negotiation for the new DLCI. This needs to be done
2173 * before the DLCI initialized the channel via SABM.
2175 static int gsm_dlci_negotiate(struct gsm_dlci *dlci)
2177 struct gsm_mux *gsm = dlci->gsm;
2178 struct gsm_dlci_param_bits params;
2181 ret = gsm_encode_params(dlci, ¶ms);
2185 /* We cannot asynchronous wait for the command response with
2186 * gsm_command() and gsm_control_wait() at this point.
2188 ret = gsm_control_command(gsm, CMD_PN, (const u8 *)¶ms,
2195 * gsm_dlci_t1 - T1 timer expiry
2196 * @t: timer contained in the DLCI that opened
2198 * The T1 timer handles retransmits of control frames (essentially of
2199 * SABM and DISC). We resend the command until the retry count runs out
2200 * in which case an opening port goes back to closed and a closing port
2201 * is simply put into closed state (any further frames from the other
2202 * end will get a DM response)
2204 * Some control dlci can stay in ADM mode with other dlci working just
2205 * fine. In that case we can just keep the control dlci open after the
2206 * DLCI_OPENING retries time out.
2209 static void gsm_dlci_t1(struct timer_list *t)
2211 struct gsm_dlci *dlci = from_timer(dlci, t, t1);
2212 struct gsm_mux *gsm = dlci->gsm;
2214 switch (dlci->state) {
2215 case DLCI_CONFIGURE:
2216 if (dlci->retries && gsm_dlci_negotiate(dlci) == 0) {
2218 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
2220 gsm_dlci_begin_close(dlci); /* prevent half open link */
2224 if (dlci->retries) {
2226 gsm_command(dlci->gsm, dlci->addr, SABM|PF);
2227 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
2228 } else if (!dlci->addr && gsm->control == (DM | PF)) {
2229 if (debug & DBG_ERRORS)
2230 pr_info("DLCI %d opening in ADM mode.\n",
2232 dlci->mode = DLCI_MODE_ADM;
2233 gsm_dlci_open(dlci);
2235 gsm_dlci_begin_close(dlci); /* prevent half open link */
2240 if (dlci->retries) {
2242 gsm_command(dlci->gsm, dlci->addr, DISC|PF);
2243 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
2245 gsm_dlci_close(dlci);
2248 pr_debug("%s: unhandled state: %d\n", __func__, dlci->state);
2254 * gsm_dlci_begin_open - start channel open procedure
2255 * @dlci: DLCI to open
2257 * Commence opening a DLCI from the Linux side. We issue SABM messages
2258 * to the modem which should then reply with a UA or ADM, at which point
2259 * we will move into open state. Opening is done asynchronously with retry
2260 * running off timers and the responses.
2261 * Parameter negotiation is performed before SABM if required.
2264 static void gsm_dlci_begin_open(struct gsm_dlci *dlci)
2266 struct gsm_mux *gsm = dlci ? dlci->gsm : NULL;
2267 bool need_pn = false;
2272 if (dlci->addr != 0) {
2273 if (gsm->adaption != 1 || gsm->adaption != dlci->adaption)
2275 if (dlci->prio != (roundup(dlci->addr + 1, 8) - 1))
2277 if (gsm->ftype != dlci->ftype)
2281 switch (dlci->state) {
2284 dlci->retries = gsm->n2;
2286 dlci->state = DLCI_OPENING;
2287 gsm_command(gsm, dlci->addr, SABM|PF);
2289 /* Configure DLCI before setup */
2290 dlci->state = DLCI_CONFIGURE;
2291 if (gsm_dlci_negotiate(dlci) != 0) {
2292 gsm_dlci_close(dlci);
2296 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
2304 * gsm_dlci_set_opening - change state to opening
2305 * @dlci: DLCI to open
2307 * Change internal state to wait for DLCI open from initiator side.
2308 * We set off timers and responses upon reception of an SABM.
2310 static void gsm_dlci_set_opening(struct gsm_dlci *dlci)
2312 switch (dlci->state) {
2315 dlci->state = DLCI_OPENING;
2323 * gsm_dlci_begin_close - start channel open procedure
2324 * @dlci: DLCI to open
2326 * Commence closing a DLCI from the Linux side. We issue DISC messages
2327 * to the modem which should then reply with a UA, at which point we
2328 * will move into closed state. Closing is done asynchronously with retry
2329 * off timers. We may also receive a DM reply from the other end which
2330 * indicates the channel was already closed.
2333 static void gsm_dlci_begin_close(struct gsm_dlci *dlci)
2335 struct gsm_mux *gsm = dlci->gsm;
2336 if (dlci->state == DLCI_CLOSED || dlci->state == DLCI_CLOSING)
2338 dlci->retries = gsm->n2;
2339 dlci->state = DLCI_CLOSING;
2340 gsm_command(dlci->gsm, dlci->addr, DISC|PF);
2341 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
2342 wake_up_interruptible(&gsm->event);
2346 * gsm_dlci_data - data arrived
2348 * @data: block of bytes received
2349 * @clen: length of received block
2351 * A UI or UIH frame has arrived which contains data for a channel
2352 * other than the control channel. If the relevant virtual tty is
2353 * open we shovel the bits down it, if not we drop them.
2356 static void gsm_dlci_data(struct gsm_dlci *dlci, const u8 *data, int clen)
2359 struct tty_port *port = &dlci->port;
2360 struct tty_struct *tty;
2361 unsigned int modem = 0;
2364 if (debug & DBG_TTY)
2365 pr_debug("%d bytes for tty\n", clen);
2366 switch (dlci->adaption) {
2367 /* Unsupported types */
2368 case 4: /* Packetised interruptible data */
2370 case 3: /* Packetised uininterruptible voice/data */
2372 case 2: /* Asynchronous serial with line state in each frame */
2373 len = gsm_read_ea_val(&modem, data, clen);
2376 tty = tty_port_tty_get(port);
2378 gsm_process_modem(tty, dlci, modem, len);
2382 /* Skip processed modem data */
2386 case 1: /* Line state will go via DLCI 0 controls only */
2388 tty_insert_flip_string(port, data, clen);
2389 tty_flip_buffer_push(port);
2394 * gsm_dlci_command - data arrived on control channel
2396 * @data: block of bytes received
2397 * @len: length of received block
2399 * A UI or UIH frame has arrived which contains data for DLCI 0 the
2400 * control channel. This should contain a command EA followed by
2401 * control data bytes. The command EA contains a command/response bit
2402 * and we divide up the work accordingly.
2405 static void gsm_dlci_command(struct gsm_dlci *dlci, const u8 *data, int len)
2407 /* See what command is involved */
2408 unsigned int command = 0;
2409 unsigned int clen = 0;
2412 /* read the command */
2413 dlen = gsm_read_ea_val(&command, data, len);
2417 /* read any control data */
2418 dlen = gsm_read_ea_val(&clen, data, len);
2422 /* Malformed command? */
2427 gsm_control_message(dlci->gsm, command, data, clen);
2429 gsm_control_response(dlci->gsm, command, data, clen);
2433 * gsm_kick_timer - transmit if possible
2434 * @t: timer contained in our gsm object
2436 * Transmit data from DLCIs if the queue is empty. We can't rely on
2437 * a tty wakeup except when we filled the pipe so we need to fire off
2438 * new data ourselves in other cases.
2440 static void gsm_kick_timer(struct timer_list *t)
2442 struct gsm_mux *gsm = from_timer(gsm, t, kick_timer);
2443 unsigned long flags;
2446 spin_lock_irqsave(&gsm->tx_lock, flags);
2447 /* If we have nothing running then we need to fire up */
2448 if (gsm->tx_bytes < TX_THRESH_LO)
2449 sent = gsm_dlci_data_sweep(gsm);
2450 spin_unlock_irqrestore(&gsm->tx_lock, flags);
2452 if (sent && debug & DBG_DATA)
2453 pr_info("%s TX queue stalled\n", __func__);
2457 * Allocate/Free DLCI channels
2461 * gsm_dlci_alloc - allocate a DLCI
2463 * @addr: address of the DLCI
2465 * Allocate and install a new DLCI object into the GSM mux.
2467 * FIXME: review locking races
2470 static struct gsm_dlci *gsm_dlci_alloc(struct gsm_mux *gsm, int addr)
2472 struct gsm_dlci *dlci = kzalloc(sizeof(struct gsm_dlci), GFP_ATOMIC);
2475 spin_lock_init(&dlci->lock);
2476 mutex_init(&dlci->mutex);
2477 if (kfifo_alloc(&dlci->fifo, TX_SIZE, GFP_KERNEL) < 0) {
2482 skb_queue_head_init(&dlci->skb_list);
2483 timer_setup(&dlci->t1, gsm_dlci_t1, 0);
2484 tty_port_init(&dlci->port);
2485 dlci->port.ops = &gsm_port_ops;
2488 dlci->adaption = gsm->adaption;
2489 dlci->mtu = gsm->mtu;
2493 dlci->prio = roundup(addr + 1, 8) - 1;
2494 dlci->ftype = gsm->ftype;
2496 dlci->state = DLCI_CLOSED;
2498 dlci->data = gsm_dlci_data;
2499 /* Prevent us from sending data before the link is up */
2500 dlci->constipated = true;
2502 dlci->data = gsm_dlci_command;
2504 gsm->dlci[addr] = dlci;
2509 * gsm_dlci_free - free DLCI
2510 * @port: tty port for DLCI to free
2516 static void gsm_dlci_free(struct tty_port *port)
2518 struct gsm_dlci *dlci = container_of(port, struct gsm_dlci, port);
2520 timer_shutdown_sync(&dlci->t1);
2521 dlci->gsm->dlci[dlci->addr] = NULL;
2522 kfifo_free(&dlci->fifo);
2523 while ((dlci->skb = skb_dequeue(&dlci->skb_list)))
2524 dev_kfree_skb(dlci->skb);
2528 static inline void dlci_get(struct gsm_dlci *dlci)
2530 tty_port_get(&dlci->port);
2533 static inline void dlci_put(struct gsm_dlci *dlci)
2535 tty_port_put(&dlci->port);
2538 static void gsm_destroy_network(struct gsm_dlci *dlci);
2541 * gsm_dlci_release - release DLCI
2542 * @dlci: DLCI to destroy
2544 * Release a DLCI. Actual free is deferred until either
2545 * mux is closed or tty is closed - whichever is last.
2549 static void gsm_dlci_release(struct gsm_dlci *dlci)
2551 struct tty_struct *tty = tty_port_tty_get(&dlci->port);
2553 mutex_lock(&dlci->mutex);
2554 gsm_destroy_network(dlci);
2555 mutex_unlock(&dlci->mutex);
2557 /* We cannot use tty_hangup() because in tty_kref_put() the tty
2558 * driver assumes that the hangup queue is free and reuses it to
2559 * queue release_one_tty() -> NULL pointer panic in
2560 * process_one_work().
2564 tty_port_tty_set(&dlci->port, NULL);
2567 dlci->state = DLCI_CLOSED;
2572 * LAPBish link layer logic
2576 * gsm_queue - a GSM frame is ready to process
2577 * @gsm: pointer to our gsm mux
2579 * At this point in time a frame has arrived and been demangled from
2580 * the line encoding. All the differences between the encodings have
2581 * been handled below us and the frame is unpacked into the structures.
2582 * The fcs holds the header FCS but any data FCS must be added here.
2585 static void gsm_queue(struct gsm_mux *gsm)
2587 struct gsm_dlci *dlci;
2591 if (gsm->fcs != GOOD_FCS) {
2593 if (debug & DBG_DATA)
2594 pr_debug("BAD FCS %02x\n", gsm->fcs);
2597 address = gsm->address >> 1;
2598 if (address >= NUM_DLCI)
2601 cr = gsm->address & 1; /* C/R bit */
2602 cr ^= gsm->initiator ? 0 : 1; /* Flip so 1 always means command */
2604 gsm_print_packet("<--", address, cr, gsm->control, gsm->buf, gsm->len);
2606 dlci = gsm->dlci[address];
2608 switch (gsm->control) {
2613 dlci = gsm_dlci_alloc(gsm, address);
2617 gsm_response(gsm, address, DM|PF);
2619 gsm_response(gsm, address, UA|PF);
2620 gsm_dlci_open(dlci);
2626 if (dlci == NULL || dlci->state == DLCI_CLOSED) {
2627 gsm_response(gsm, address, DM|PF);
2630 /* Real close complete */
2631 gsm_response(gsm, address, UA|PF);
2632 gsm_dlci_close(dlci);
2635 if (cr == 0 || dlci == NULL)
2637 switch (dlci->state) {
2639 gsm_dlci_close(dlci);
2642 gsm_dlci_open(dlci);
2645 pr_debug("%s: unhandled state: %d\n", __func__,
2650 case DM: /* DM can be valid unsolicited */
2656 gsm_dlci_close(dlci);
2662 if (dlci == NULL || dlci->state != DLCI_OPEN) {
2663 gsm_response(gsm, address, DM|PF);
2666 dlci->data(dlci, gsm->buf, gsm->len);
2679 * gsm0_receive - perform processing for non-transparency
2680 * @gsm: gsm data for this ldisc instance
2683 * Receive bytes in gsm mode 0
2686 static void gsm0_receive(struct gsm_mux *gsm, unsigned char c)
2690 switch (gsm->state) {
2691 case GSM_SEARCH: /* SOF marker */
2692 if (c == GSM0_SOF) {
2693 gsm->state = GSM_ADDRESS;
2696 gsm->fcs = INIT_FCS;
2699 case GSM_ADDRESS: /* Address EA */
2700 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
2701 if (gsm_read_ea(&gsm->address, c))
2702 gsm->state = GSM_CONTROL;
2704 case GSM_CONTROL: /* Control Byte */
2705 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
2707 gsm->state = GSM_LEN0;
2709 case GSM_LEN0: /* Length EA */
2710 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
2711 if (gsm_read_ea(&gsm->len, c)) {
2712 if (gsm->len > gsm->mru) {
2714 gsm->state = GSM_SEARCH;
2719 gsm->state = GSM_FCS;
2721 gsm->state = GSM_DATA;
2724 gsm->state = GSM_LEN1;
2727 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
2729 gsm->len |= len << 7;
2730 if (gsm->len > gsm->mru) {
2732 gsm->state = GSM_SEARCH;
2737 gsm->state = GSM_FCS;
2739 gsm->state = GSM_DATA;
2741 case GSM_DATA: /* Data */
2742 gsm->buf[gsm->count++] = c;
2743 if (gsm->count == gsm->len) {
2744 /* Calculate final FCS for UI frames over all data */
2745 if ((gsm->control & ~PF) != UIH) {
2746 gsm->fcs = gsm_fcs_add_block(gsm->fcs, gsm->buf,
2749 gsm->state = GSM_FCS;
2752 case GSM_FCS: /* FCS follows the packet */
2753 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
2754 gsm->state = GSM_SSOF;
2757 gsm->state = GSM_SEARCH;
2764 pr_debug("%s: unhandled state: %d\n", __func__, gsm->state);
2770 * gsm1_receive - perform processing for non-transparency
2771 * @gsm: gsm data for this ldisc instance
2774 * Receive bytes in mode 1 (Advanced option)
2777 static void gsm1_receive(struct gsm_mux *gsm, unsigned char c)
2779 /* handle XON/XOFF */
2780 if ((c & ISO_IEC_646_MASK) == XON) {
2781 gsm->constipated = true;
2783 } else if ((c & ISO_IEC_646_MASK) == XOFF) {
2784 gsm->constipated = false;
2785 /* Kick the link in case it is idling */
2786 gsmld_write_trigger(gsm);
2789 if (c == GSM1_SOF) {
2790 /* EOF is only valid in frame if we have got to the data state */
2791 if (gsm->state == GSM_DATA) {
2792 if (gsm->count < 1) {
2795 gsm->state = GSM_START;
2798 /* Remove the FCS from data */
2800 if ((gsm->control & ~PF) != UIH) {
2801 /* Calculate final FCS for UI frames over all
2804 gsm->fcs = gsm_fcs_add_block(gsm->fcs, gsm->buf,
2807 /* Add the FCS itself to test against GOOD_FCS */
2808 gsm->fcs = gsm_fcs_add(gsm->fcs, gsm->buf[gsm->count]);
2809 gsm->len = gsm->count;
2811 gsm->state = GSM_START;
2814 /* Any partial frame was a runt so go back to start */
2815 if (gsm->state != GSM_START) {
2816 if (gsm->state != GSM_SEARCH)
2818 gsm->state = GSM_START;
2820 /* A SOF in GSM_START means we are still reading idling or
2825 if (c == GSM1_ESCAPE) {
2830 /* Only an unescaped SOF gets us out of GSM search */
2831 if (gsm->state == GSM_SEARCH)
2835 c ^= GSM1_ESCAPE_BITS;
2836 gsm->escape = false;
2838 switch (gsm->state) {
2839 case GSM_START: /* First byte after SOF */
2841 gsm->state = GSM_ADDRESS;
2842 gsm->fcs = INIT_FCS;
2844 case GSM_ADDRESS: /* Address continuation */
2845 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
2846 if (gsm_read_ea(&gsm->address, c))
2847 gsm->state = GSM_CONTROL;
2849 case GSM_CONTROL: /* Control Byte */
2850 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
2853 gsm->state = GSM_DATA;
2855 case GSM_DATA: /* Data */
2856 if (gsm->count > gsm->mru) { /* Allow one for the FCS */
2857 gsm->state = GSM_OVERRUN;
2860 gsm->buf[gsm->count++] = c;
2862 case GSM_OVERRUN: /* Over-long - eg a dropped SOF */
2865 pr_debug("%s: unhandled state: %d\n", __func__, gsm->state);
2871 * gsm_error - handle tty error
2874 * Handle an error in the receipt of data for a frame. Currently we just
2875 * go back to hunting for a SOF.
2877 * FIXME: better diagnostics ?
2880 static void gsm_error(struct gsm_mux *gsm)
2882 gsm->state = GSM_SEARCH;
2887 * gsm_cleanup_mux - generic GSM protocol cleanup
2889 * @disc: disconnect link?
2891 * Clean up the bits of the mux which are the same for all framing
2892 * protocols. Remove the mux from the mux table, stop all the timers
2893 * and then shut down each device hanging up the channels as we go.
2896 static void gsm_cleanup_mux(struct gsm_mux *gsm, bool disc)
2899 struct gsm_dlci *dlci = gsm->dlci[0];
2900 struct gsm_msg *txq, *ntxq;
2903 mutex_lock(&gsm->mutex);
2906 if (disc && dlci->state != DLCI_CLOSED) {
2907 gsm_dlci_begin_close(dlci);
2908 wait_event(gsm->event, dlci->state == DLCI_CLOSED);
2913 /* Finish outstanding timers, making sure they are done */
2914 del_timer_sync(&gsm->kick_timer);
2915 del_timer_sync(&gsm->t2_timer);
2916 del_timer_sync(&gsm->ka_timer);
2918 /* Finish writing to ldisc */
2919 flush_work(&gsm->tx_work);
2921 /* Free up any link layer users and finally the control channel */
2922 if (gsm->has_devices) {
2923 gsm_unregister_devices(gsm_tty_driver, gsm->num);
2924 gsm->has_devices = false;
2926 for (i = NUM_DLCI - 1; i >= 0; i--)
2928 gsm_dlci_release(gsm->dlci[i]);
2929 mutex_unlock(&gsm->mutex);
2930 /* Now wipe the queues */
2931 tty_ldisc_flush(gsm->tty);
2932 list_for_each_entry_safe(txq, ntxq, &gsm->tx_ctrl_list, list)
2934 INIT_LIST_HEAD(&gsm->tx_ctrl_list);
2935 list_for_each_entry_safe(txq, ntxq, &gsm->tx_data_list, list)
2937 INIT_LIST_HEAD(&gsm->tx_data_list);
2941 * gsm_activate_mux - generic GSM setup
2944 * Set up the bits of the mux which are the same for all framing
2945 * protocols. Add the mux to the mux table so it can be opened and
2946 * finally kick off connecting to DLCI 0 on the modem.
2949 static int gsm_activate_mux(struct gsm_mux *gsm)
2951 struct gsm_dlci *dlci;
2954 dlci = gsm_dlci_alloc(gsm, 0);
2958 if (gsm->encoding == GSM_BASIC_OPT)
2959 gsm->receive = gsm0_receive;
2961 gsm->receive = gsm1_receive;
2963 ret = gsm_register_devices(gsm_tty_driver, gsm->num);
2967 gsm->has_devices = true;
2968 gsm->dead = false; /* Tty opens are now permissible */
2973 * gsm_free_mux - free up a mux
2976 * Dispose of allocated resources for a dead mux
2978 static void gsm_free_mux(struct gsm_mux *gsm)
2982 for (i = 0; i < MAX_MUX; i++) {
2983 if (gsm == gsm_mux[i]) {
2988 mutex_destroy(&gsm->mutex);
2989 kfree(gsm->txframe);
2995 * gsm_free_muxr - free up a mux
2996 * @ref: kreference to the mux to free
2998 * Dispose of allocated resources for a dead mux
3000 static void gsm_free_muxr(struct kref *ref)
3002 struct gsm_mux *gsm = container_of(ref, struct gsm_mux, ref);
3006 static inline void mux_get(struct gsm_mux *gsm)
3008 unsigned long flags;
3010 spin_lock_irqsave(&gsm_mux_lock, flags);
3011 kref_get(&gsm->ref);
3012 spin_unlock_irqrestore(&gsm_mux_lock, flags);
3015 static inline void mux_put(struct gsm_mux *gsm)
3017 unsigned long flags;
3019 spin_lock_irqsave(&gsm_mux_lock, flags);
3020 kref_put(&gsm->ref, gsm_free_muxr);
3021 spin_unlock_irqrestore(&gsm_mux_lock, flags);
3024 static inline unsigned int mux_num_to_base(struct gsm_mux *gsm)
3026 return gsm->num * NUM_DLCI;
3029 static inline unsigned int mux_line_to_num(unsigned int line)
3031 return line / NUM_DLCI;
3035 * gsm_alloc_mux - allocate a mux
3037 * Creates a new mux ready for activation.
3040 static struct gsm_mux *gsm_alloc_mux(void)
3043 struct gsm_mux *gsm = kzalloc(sizeof(struct gsm_mux), GFP_KERNEL);
3046 gsm->buf = kmalloc(MAX_MRU + 1, GFP_KERNEL);
3047 if (gsm->buf == NULL) {
3051 gsm->txframe = kmalloc(2 * (MAX_MTU + PROT_OVERHEAD - 1), GFP_KERNEL);
3052 if (gsm->txframe == NULL) {
3057 spin_lock_init(&gsm->lock);
3058 mutex_init(&gsm->mutex);
3059 kref_init(&gsm->ref);
3060 INIT_LIST_HEAD(&gsm->tx_ctrl_list);
3061 INIT_LIST_HEAD(&gsm->tx_data_list);
3062 timer_setup(&gsm->kick_timer, gsm_kick_timer, 0);
3063 timer_setup(&gsm->t2_timer, gsm_control_retransmit, 0);
3064 timer_setup(&gsm->ka_timer, gsm_control_keep_alive, 0);
3065 INIT_WORK(&gsm->tx_work, gsmld_write_task);
3066 init_waitqueue_head(&gsm->event);
3067 spin_lock_init(&gsm->control_lock);
3068 spin_lock_init(&gsm->tx_lock);
3077 gsm->encoding = GSM_ADV_OPT;
3078 gsm->mru = 64; /* Default to encoding 1 so these should be 64 */
3080 gsm->dead = true; /* Avoid early tty opens */
3081 gsm->keep_alive = 0; /* Disabled */
3083 /* Store the instance to the mux array or abort if no space is
3086 spin_lock(&gsm_mux_lock);
3087 for (i = 0; i < MAX_MUX; i++) {
3094 spin_unlock(&gsm_mux_lock);
3096 mutex_destroy(&gsm->mutex);
3097 kfree(gsm->txframe);
3106 static void gsm_copy_config_values(struct gsm_mux *gsm,
3107 struct gsm_config *c)
3109 memset(c, 0, sizeof(*c));
3110 c->adaption = gsm->adaption;
3111 c->encapsulation = gsm->encoding;
3112 c->initiator = gsm->initiator;
3117 if (gsm->ftype == UIH)
3121 pr_debug("Ftype %d i %d\n", gsm->ftype, c->i);
3127 static int gsm_config(struct gsm_mux *gsm, struct gsm_config *c)
3131 int need_restart = 0;
3133 /* Stuff we don't support yet - UI or I frame transport, windowing */
3134 if ((c->adaption != 1 && c->adaption != 2) || c->k)
3136 /* Check the MRU/MTU range looks sane */
3137 if (c->mru < MIN_MTU || c->mtu < MIN_MTU)
3139 if (c->mru > MAX_MRU || c->mtu > MAX_MTU)
3145 if (c->encapsulation > 1) /* Basic, advanced, no I */
3147 if (c->initiator > 1)
3149 if (c->k > MAX_WINDOW_SIZE)
3151 if (c->i == 0 || c->i > 2) /* UIH and UI only */
3154 * See what is needed for reconfiguration
3158 if (c->t1 != 0 && c->t1 != gsm->t1)
3160 if (c->t2 != 0 && c->t2 != gsm->t2)
3162 if (c->encapsulation != gsm->encoding)
3164 if (c->adaption != gsm->adaption)
3167 if (c->initiator != gsm->initiator)
3169 if (c->mru != gsm->mru)
3171 if (c->mtu != gsm->mtu)
3175 * Close down what is needed, restart and initiate the new
3176 * configuration. On the first time there is no DLCI[0]
3177 * and closing or cleaning up is not necessary.
3179 if (need_close || need_restart)
3180 gsm_cleanup_mux(gsm, true);
3182 gsm->initiator = c->initiator;
3185 gsm->encoding = c->encapsulation ? GSM_ADV_OPT : GSM_BASIC_OPT;
3186 gsm->adaption = c->adaption;
3204 * FIXME: We need to separate activation/deactivation from adding
3205 * and removing from the mux array
3208 ret = gsm_activate_mux(gsm);
3212 gsm_dlci_begin_open(gsm->dlci[0]);
3217 static void gsm_copy_config_ext_values(struct gsm_mux *gsm,
3218 struct gsm_config_ext *ce)
3220 memset(ce, 0, sizeof(*ce));
3221 ce->keep_alive = gsm->keep_alive;
3224 static int gsm_config_ext(struct gsm_mux *gsm, struct gsm_config_ext *ce)
3229 * Check that userspace doesn't put stuff in here to prevent breakages
3232 for (i = 0; i < ARRAY_SIZE(ce->reserved); i++)
3233 if (ce->reserved[i])
3236 gsm->keep_alive = ce->keep_alive;
3241 * gsmld_output - write to link
3243 * @data: bytes to output
3246 * Write a block of data from the GSM mux to the data channel. This
3247 * will eventually be serialized from above but at the moment isn't.
3250 static int gsmld_output(struct gsm_mux *gsm, u8 *data, int len)
3252 if (tty_write_room(gsm->tty) < len) {
3253 set_bit(TTY_DO_WRITE_WAKEUP, &gsm->tty->flags);
3256 if (debug & DBG_DATA)
3257 gsm_hex_dump_bytes(__func__, data, len);
3258 return gsm->tty->ops->write(gsm->tty, data, len);
3263 * gsmld_write_trigger - schedule ldisc write task
3266 static void gsmld_write_trigger(struct gsm_mux *gsm)
3268 if (!gsm || !gsm->dlci[0] || gsm->dlci[0]->dead)
3270 schedule_work(&gsm->tx_work);
3275 * gsmld_write_task - ldisc write task
3276 * @work: our tx write work
3278 * Writes out data to the ldisc if possible. We are doing this here to
3279 * avoid dead-locking. This returns if no space or data is left for output.
3281 static void gsmld_write_task(struct work_struct *work)
3283 struct gsm_mux *gsm = container_of(work, struct gsm_mux, tx_work);
3284 unsigned long flags;
3287 /* All outstanding control channel and control messages and one data
3291 spin_lock_irqsave(&gsm->tx_lock, flags);
3293 ret = gsm_data_kick(gsm);
3294 spin_unlock_irqrestore(&gsm->tx_lock, flags);
3297 for (i = 0; i < NUM_DLCI; i++)
3299 tty_port_tty_wakeup(&gsm->dlci[i]->port);
3303 * gsmld_attach_gsm - mode set up
3304 * @tty: our tty structure
3307 * Set up the MUX for basic mode and commence connecting to the
3308 * modem. Currently called from the line discipline set up but
3309 * will need moving to an ioctl path.
3312 static void gsmld_attach_gsm(struct tty_struct *tty, struct gsm_mux *gsm)
3314 gsm->tty = tty_kref_get(tty);
3315 /* Turn off tty XON/XOFF handling to handle it explicitly. */
3316 gsm->old_c_iflag = tty->termios.c_iflag;
3317 tty->termios.c_iflag &= (IXON | IXOFF);
3321 * gsmld_detach_gsm - stop doing 0710 mux
3322 * @tty: tty attached to the mux
3325 * Shutdown and then clean up the resources used by the line discipline
3328 static void gsmld_detach_gsm(struct tty_struct *tty, struct gsm_mux *gsm)
3330 WARN_ON(tty != gsm->tty);
3331 /* Restore tty XON/XOFF handling. */
3332 gsm->tty->termios.c_iflag = gsm->old_c_iflag;
3333 tty_kref_put(gsm->tty);
3337 static void gsmld_receive_buf(struct tty_struct *tty, const unsigned char *cp,
3338 const char *fp, int count)
3340 struct gsm_mux *gsm = tty->disc_data;
3341 char flags = TTY_NORMAL;
3343 if (debug & DBG_DATA)
3344 gsm_hex_dump_bytes(__func__, cp, count);
3346 for (; count; count--, cp++) {
3352 gsm->receive(gsm, *cp);
3361 WARN_ONCE(1, "%s: unknown flag %d\n",
3362 tty_name(tty), flags);
3366 /* FASYNC if needed ? */
3367 /* If clogged call tty_throttle(tty); */
3371 * gsmld_flush_buffer - clean input queue
3372 * @tty: terminal device
3374 * Flush the input buffer. Called when the line discipline is
3375 * being closed, when the tty layer wants the buffer flushed (eg
3379 static void gsmld_flush_buffer(struct tty_struct *tty)
3384 * gsmld_close - close the ldisc for this tty
3387 * Called from the terminal layer when this line discipline is
3388 * being shut down, either because of a close or becsuse of a
3389 * discipline change. The function will not be called while other
3390 * ldisc methods are in progress.
3393 static void gsmld_close(struct tty_struct *tty)
3395 struct gsm_mux *gsm = tty->disc_data;
3397 /* The ldisc locks and closes the port before calling our close. This
3398 * means we have no way to do a proper disconnect. We will not bother
3401 gsm_cleanup_mux(gsm, false);
3403 gsmld_detach_gsm(tty, gsm);
3405 gsmld_flush_buffer(tty);
3406 /* Do other clean up here */
3411 * gsmld_open - open an ldisc
3412 * @tty: terminal to open
3414 * Called when this line discipline is being attached to the
3415 * terminal device. Can sleep. Called serialized so that no
3416 * other events will occur in parallel. No further open will occur
3420 static int gsmld_open(struct tty_struct *tty)
3422 struct gsm_mux *gsm;
3424 if (tty->ops->write == NULL)
3427 /* Attach our ldisc data */
3428 gsm = gsm_alloc_mux();
3432 tty->disc_data = gsm;
3433 tty->receive_room = 65536;
3435 /* Attach the initial passive connection */
3436 gsm->encoding = GSM_ADV_OPT;
3437 gsmld_attach_gsm(tty, gsm);
3443 * gsmld_write_wakeup - asynchronous I/O notifier
3446 * Required for the ptys, serial driver etc. since processes
3447 * that attach themselves to the master and rely on ASYNC
3448 * IO must be woken up
3451 static void gsmld_write_wakeup(struct tty_struct *tty)
3453 struct gsm_mux *gsm = tty->disc_data;
3456 gsmld_write_trigger(gsm);
3460 * gsmld_read - read function for tty
3462 * @file: file object
3463 * @buf: userspace buffer pointer
3468 * Perform reads for the line discipline. We are guaranteed that the
3469 * line discipline will not be closed under us but we may get multiple
3470 * parallel readers and must handle this ourselves. We may also get
3471 * a hangup. Always called in user context, may sleep.
3473 * This code must be sure never to sleep through a hangup.
3476 static ssize_t gsmld_read(struct tty_struct *tty, struct file *file,
3477 unsigned char *buf, size_t nr,
3478 void **cookie, unsigned long offset)
3484 * gsmld_write - write function for tty
3486 * @file: file object
3487 * @buf: userspace buffer pointer
3490 * Called when the owner of the device wants to send a frame
3491 * itself (or some other control data). The data is transferred
3492 * as-is and must be properly framed and checksummed as appropriate
3493 * by userspace. Frames are either sent whole or not at all as this
3494 * avoids pain user side.
3497 static ssize_t gsmld_write(struct tty_struct *tty, struct file *file,
3498 const unsigned char *buf, size_t nr)
3500 struct gsm_mux *gsm = tty->disc_data;
3501 unsigned long flags;
3509 spin_lock_irqsave(&gsm->tx_lock, flags);
3510 space = tty_write_room(tty);
3512 ret = tty->ops->write(tty, buf, nr);
3514 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
3515 spin_unlock_irqrestore(&gsm->tx_lock, flags);
3521 * gsmld_poll - poll method for N_GSM0710
3522 * @tty: terminal device
3523 * @file: file accessing it
3526 * Called when the line discipline is asked to poll() for data or
3527 * for special events. This code is not serialized with respect to
3528 * other events save open/close.
3530 * This code must be sure never to sleep through a hangup.
3531 * Called without the kernel lock held - fine
3534 static __poll_t gsmld_poll(struct tty_struct *tty, struct file *file,
3538 struct gsm_mux *gsm = tty->disc_data;
3540 poll_wait(file, &tty->read_wait, wait);
3541 poll_wait(file, &tty->write_wait, wait);
3545 if (tty_hung_up_p(file))
3547 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
3549 if (!tty_is_writelocked(tty) && tty_write_room(tty) > 0)
3550 mask |= EPOLLOUT | EPOLLWRNORM;
3554 static int gsmld_ioctl(struct tty_struct *tty, unsigned int cmd,
3557 struct gsm_config c;
3558 struct gsm_config_ext ce;
3559 struct gsm_mux *gsm = tty->disc_data;
3563 case GSMIOC_GETCONF:
3564 gsm_copy_config_values(gsm, &c);
3565 if (copy_to_user((void __user *)arg, &c, sizeof(c)))
3568 case GSMIOC_SETCONF:
3569 if (copy_from_user(&c, (void __user *)arg, sizeof(c)))
3571 return gsm_config(gsm, &c);
3572 case GSMIOC_GETFIRST:
3573 base = mux_num_to_base(gsm);
3574 return put_user(base + 1, (__u32 __user *)arg);
3575 case GSMIOC_GETCONF_EXT:
3576 gsm_copy_config_ext_values(gsm, &ce);
3577 if (copy_to_user((void __user *)arg, &ce, sizeof(ce)))
3580 case GSMIOC_SETCONF_EXT:
3581 if (copy_from_user(&ce, (void __user *)arg, sizeof(ce)))
3583 return gsm_config_ext(gsm, &ce);
3585 return n_tty_ioctl_helper(tty, cmd, arg);
3594 static int gsm_mux_net_open(struct net_device *net)
3596 pr_debug("%s called\n", __func__);
3597 netif_start_queue(net);
3601 static int gsm_mux_net_close(struct net_device *net)
3603 netif_stop_queue(net);
3607 static void dlci_net_free(struct gsm_dlci *dlci)
3613 dlci->adaption = dlci->prev_adaption;
3614 dlci->data = dlci->prev_data;
3615 free_netdev(dlci->net);
3618 static void net_free(struct kref *ref)
3620 struct gsm_mux_net *mux_net;
3621 struct gsm_dlci *dlci;
3623 mux_net = container_of(ref, struct gsm_mux_net, ref);
3624 dlci = mux_net->dlci;
3627 unregister_netdev(dlci->net);
3628 dlci_net_free(dlci);
3632 static inline void muxnet_get(struct gsm_mux_net *mux_net)
3634 kref_get(&mux_net->ref);
3637 static inline void muxnet_put(struct gsm_mux_net *mux_net)
3639 kref_put(&mux_net->ref, net_free);
3642 static netdev_tx_t gsm_mux_net_start_xmit(struct sk_buff *skb,
3643 struct net_device *net)
3645 struct gsm_mux_net *mux_net = netdev_priv(net);
3646 struct gsm_dlci *dlci = mux_net->dlci;
3647 muxnet_get(mux_net);
3649 skb_queue_head(&dlci->skb_list, skb);
3650 net->stats.tx_packets++;
3651 net->stats.tx_bytes += skb->len;
3652 gsm_dlci_data_kick(dlci);
3653 /* And tell the kernel when the last transmit started. */
3654 netif_trans_update(net);
3655 muxnet_put(mux_net);
3656 return NETDEV_TX_OK;
3659 /* called when a packet did not ack after watchdogtimeout */
3660 static void gsm_mux_net_tx_timeout(struct net_device *net, unsigned int txqueue)
3662 /* Tell syslog we are hosed. */
3663 dev_dbg(&net->dev, "Tx timed out.\n");
3665 /* Update statistics */
3666 net->stats.tx_errors++;
3669 static void gsm_mux_rx_netchar(struct gsm_dlci *dlci,
3670 const unsigned char *in_buf, int size)
3672 struct net_device *net = dlci->net;
3673 struct sk_buff *skb;
3674 struct gsm_mux_net *mux_net = netdev_priv(net);
3675 muxnet_get(mux_net);
3677 /* Allocate an sk_buff */
3678 skb = dev_alloc_skb(size + NET_IP_ALIGN);
3680 /* We got no receive buffer. */
3681 net->stats.rx_dropped++;
3682 muxnet_put(mux_net);
3685 skb_reserve(skb, NET_IP_ALIGN);
3686 skb_put_data(skb, in_buf, size);
3689 skb->protocol = htons(ETH_P_IP);
3691 /* Ship it off to the kernel */
3694 /* update out statistics */
3695 net->stats.rx_packets++;
3696 net->stats.rx_bytes += size;
3697 muxnet_put(mux_net);
3701 static void gsm_mux_net_init(struct net_device *net)
3703 static const struct net_device_ops gsm_netdev_ops = {
3704 .ndo_open = gsm_mux_net_open,
3705 .ndo_stop = gsm_mux_net_close,
3706 .ndo_start_xmit = gsm_mux_net_start_xmit,
3707 .ndo_tx_timeout = gsm_mux_net_tx_timeout,
3710 net->netdev_ops = &gsm_netdev_ops;
3712 /* fill in the other fields */
3713 net->watchdog_timeo = GSM_NET_TX_TIMEOUT;
3714 net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
3715 net->type = ARPHRD_NONE;
3716 net->tx_queue_len = 10;
3720 /* caller holds the dlci mutex */
3721 static void gsm_destroy_network(struct gsm_dlci *dlci)
3723 struct gsm_mux_net *mux_net;
3725 pr_debug("destroy network interface\n");
3728 mux_net = netdev_priv(dlci->net);
3729 muxnet_put(mux_net);
3733 /* caller holds the dlci mutex */
3734 static int gsm_create_network(struct gsm_dlci *dlci, struct gsm_netconfig *nc)
3738 struct net_device *net;
3739 struct gsm_mux_net *mux_net;
3741 if (!capable(CAP_NET_ADMIN))
3744 /* Already in a non tty mode */
3745 if (dlci->adaption > 2)
3748 if (nc->protocol != htons(ETH_P_IP))
3749 return -EPROTONOSUPPORT;
3751 if (nc->adaption != 3 && nc->adaption != 4)
3752 return -EPROTONOSUPPORT;
3754 pr_debug("create network interface\n");
3757 if (nc->if_name[0] != '\0')
3758 netname = nc->if_name;
3759 net = alloc_netdev(sizeof(struct gsm_mux_net), netname,
3760 NET_NAME_UNKNOWN, gsm_mux_net_init);
3762 pr_err("alloc_netdev failed\n");
3765 net->mtu = dlci->mtu;
3766 net->min_mtu = MIN_MTU;
3767 net->max_mtu = dlci->mtu;
3768 mux_net = netdev_priv(net);
3769 mux_net->dlci = dlci;
3770 kref_init(&mux_net->ref);
3771 strncpy(nc->if_name, net->name, IFNAMSIZ); /* return net name */
3773 /* reconfigure dlci for network */
3774 dlci->prev_adaption = dlci->adaption;
3775 dlci->prev_data = dlci->data;
3776 dlci->adaption = nc->adaption;
3777 dlci->data = gsm_mux_rx_netchar;
3780 pr_debug("register netdev\n");
3781 retval = register_netdev(net);
3783 pr_err("network register fail %d\n", retval);
3784 dlci_net_free(dlci);
3787 return net->ifindex; /* return network index */
3790 /* Line discipline for real tty */
3791 static struct tty_ldisc_ops tty_ldisc_packet = {
3792 .owner = THIS_MODULE,
3796 .close = gsmld_close,
3797 .flush_buffer = gsmld_flush_buffer,
3799 .write = gsmld_write,
3800 .ioctl = gsmld_ioctl,
3802 .receive_buf = gsmld_receive_buf,
3803 .write_wakeup = gsmld_write_wakeup
3811 * gsm_modem_upd_via_data - send modem bits via convergence layer
3813 * @brk: break signal
3815 * Send an empty frame to signal mobile state changes and to transmit the
3816 * break signal for adaption 2.
3819 static void gsm_modem_upd_via_data(struct gsm_dlci *dlci, u8 brk)
3821 struct gsm_mux *gsm = dlci->gsm;
3822 unsigned long flags;
3824 if (dlci->state != DLCI_OPEN || dlci->adaption != 2)
3827 spin_lock_irqsave(&gsm->tx_lock, flags);
3828 gsm_dlci_modem_output(gsm, dlci, brk);
3829 spin_unlock_irqrestore(&gsm->tx_lock, flags);
3833 * gsm_modem_upd_via_msc - send modem bits via control frame
3835 * @brk: break signal
3838 static int gsm_modem_upd_via_msc(struct gsm_dlci *dlci, u8 brk)
3841 struct gsm_control *ctrl;
3844 if (dlci->gsm->encoding != GSM_BASIC_OPT)
3847 modembits[0] = (dlci->addr << 2) | 2 | EA; /* DLCI, Valid, EA */
3849 modembits[1] = (gsm_encode_modem(dlci) << 1) | EA;
3851 modembits[1] = gsm_encode_modem(dlci) << 1;
3852 modembits[2] = (brk << 4) | 2 | EA; /* Length, Break, EA */
3855 ctrl = gsm_control_send(dlci->gsm, CMD_MSC, modembits, len);
3858 return gsm_control_wait(dlci->gsm, ctrl);
3862 * gsm_modem_update - send modem status line state
3864 * @brk: break signal
3867 static int gsm_modem_update(struct gsm_dlci *dlci, u8 brk)
3869 if (dlci->adaption == 2) {
3870 /* Send convergence layer type 2 empty data frame. */
3871 gsm_modem_upd_via_data(dlci, brk);
3873 } else if (dlci->gsm->encoding == GSM_BASIC_OPT) {
3874 /* Send as MSC control message. */
3875 return gsm_modem_upd_via_msc(dlci, brk);
3878 /* Modem status lines are not supported. */
3879 return -EPROTONOSUPPORT;
3883 * gsm_wait_modem_change - wait for modem status line change
3885 * @mask: modem status line bits
3887 * The function returns if:
3888 * - any given modem status line bit changed
3889 * - the wait event function got interrupted (e.g. by a signal)
3890 * - the underlying DLCI was closed
3891 * - the underlying ldisc device was removed
3893 static int gsm_wait_modem_change(struct gsm_dlci *dlci, u32 mask)
3895 struct gsm_mux *gsm = dlci->gsm;
3896 u32 old = dlci->modem_rx;
3899 ret = wait_event_interruptible(gsm->event, gsm->dead ||
3900 dlci->state != DLCI_OPEN ||
3901 (old ^ dlci->modem_rx) & mask);
3904 if (dlci->state != DLCI_OPEN)
3909 static bool gsm_carrier_raised(struct tty_port *port)
3911 struct gsm_dlci *dlci = container_of(port, struct gsm_dlci, port);
3912 struct gsm_mux *gsm = dlci->gsm;
3914 /* Not yet open so no carrier info */
3915 if (dlci->state != DLCI_OPEN)
3917 if (debug & DBG_CD_ON)
3921 * Basic mode with control channel in ADM mode may not respond
3922 * to CMD_MSC at all and modem_rx is empty.
3924 if (gsm->encoding == GSM_BASIC_OPT &&
3925 gsm->dlci[0]->mode == DLCI_MODE_ADM && !dlci->modem_rx)
3928 return dlci->modem_rx & TIOCM_CD;
3931 static void gsm_dtr_rts(struct tty_port *port, bool active)
3933 struct gsm_dlci *dlci = container_of(port, struct gsm_dlci, port);
3934 unsigned int modem_tx = dlci->modem_tx;
3936 modem_tx |= TIOCM_DTR | TIOCM_RTS;
3938 modem_tx &= ~(TIOCM_DTR | TIOCM_RTS);
3939 if (modem_tx != dlci->modem_tx) {
3940 dlci->modem_tx = modem_tx;
3941 gsm_modem_update(dlci, 0);
3945 static const struct tty_port_operations gsm_port_ops = {
3946 .carrier_raised = gsm_carrier_raised,
3947 .dtr_rts = gsm_dtr_rts,
3948 .destruct = gsm_dlci_free,
3951 static int gsmtty_install(struct tty_driver *driver, struct tty_struct *tty)
3953 struct gsm_mux *gsm;
3954 struct gsm_dlci *dlci;
3955 unsigned int line = tty->index;
3956 unsigned int mux = mux_line_to_num(line);
3964 /* FIXME: we need to lock gsm_mux for lifetimes of ttys eventually */
3965 if (gsm_mux[mux] == NULL)
3967 if (line == 0 || line > 61) /* 62/63 reserved */
3972 /* If DLCI 0 is not yet fully open return an error.
3973 This is ok from a locking
3974 perspective as we don't have to worry about this
3976 mutex_lock(&gsm->mutex);
3977 if (gsm->dlci[0] && gsm->dlci[0]->state != DLCI_OPEN) {
3978 mutex_unlock(&gsm->mutex);
3981 dlci = gsm->dlci[line];
3984 dlci = gsm_dlci_alloc(gsm, line);
3987 mutex_unlock(&gsm->mutex);
3990 ret = tty_port_install(&dlci->port, driver, tty);
3994 mutex_unlock(&gsm->mutex);
3999 dlci_get(gsm->dlci[0]);
4001 tty->driver_data = dlci;
4002 mutex_unlock(&gsm->mutex);
4007 static int gsmtty_open(struct tty_struct *tty, struct file *filp)
4009 struct gsm_dlci *dlci = tty->driver_data;
4010 struct tty_port *port = &dlci->port;
4011 struct gsm_mux *gsm = dlci->gsm;
4014 tty_port_tty_set(port, tty);
4017 /* We could in theory open and close before we wait - eg if we get
4018 a DM straight back. This is ok as that will have caused a hangup */
4019 tty_port_set_initialized(port, true);
4020 /* Start sending off SABM messages */
4022 gsm_dlci_begin_open(dlci);
4024 gsm_dlci_set_opening(dlci);
4025 /* And wait for virtual carrier */
4026 return tty_port_block_til_ready(port, tty, filp);
4029 static void gsmtty_close(struct tty_struct *tty, struct file *filp)
4031 struct gsm_dlci *dlci = tty->driver_data;
4035 if (dlci->state == DLCI_CLOSED)
4037 mutex_lock(&dlci->mutex);
4038 gsm_destroy_network(dlci);
4039 mutex_unlock(&dlci->mutex);
4040 if (tty_port_close_start(&dlci->port, tty, filp) == 0)
4042 gsm_dlci_begin_close(dlci);
4043 if (tty_port_initialized(&dlci->port) && C_HUPCL(tty))
4044 tty_port_lower_dtr_rts(&dlci->port);
4045 tty_port_close_end(&dlci->port, tty);
4046 tty_port_tty_set(&dlci->port, NULL);
4050 static void gsmtty_hangup(struct tty_struct *tty)
4052 struct gsm_dlci *dlci = tty->driver_data;
4053 if (dlci->state == DLCI_CLOSED)
4055 tty_port_hangup(&dlci->port);
4056 gsm_dlci_begin_close(dlci);
4059 static int gsmtty_write(struct tty_struct *tty, const unsigned char *buf,
4063 struct gsm_dlci *dlci = tty->driver_data;
4064 if (dlci->state == DLCI_CLOSED)
4066 /* Stuff the bytes into the fifo queue */
4067 sent = kfifo_in_locked(&dlci->fifo, buf, len, &dlci->lock);
4068 /* Need to kick the channel */
4069 gsm_dlci_data_kick(dlci);
4073 static unsigned int gsmtty_write_room(struct tty_struct *tty)
4075 struct gsm_dlci *dlci = tty->driver_data;
4076 if (dlci->state == DLCI_CLOSED)
4078 return kfifo_avail(&dlci->fifo);
4081 static unsigned int gsmtty_chars_in_buffer(struct tty_struct *tty)
4083 struct gsm_dlci *dlci = tty->driver_data;
4084 if (dlci->state == DLCI_CLOSED)
4086 return kfifo_len(&dlci->fifo);
4089 static void gsmtty_flush_buffer(struct tty_struct *tty)
4091 struct gsm_dlci *dlci = tty->driver_data;
4092 unsigned long flags;
4094 if (dlci->state == DLCI_CLOSED)
4096 /* Caution needed: If we implement reliable transport classes
4097 then the data being transmitted can't simply be junked once
4098 it has first hit the stack. Until then we can just blow it
4100 spin_lock_irqsave(&dlci->lock, flags);
4101 kfifo_reset(&dlci->fifo);
4102 spin_unlock_irqrestore(&dlci->lock, flags);
4103 /* Need to unhook this DLCI from the transmit queue logic */
4106 static void gsmtty_wait_until_sent(struct tty_struct *tty, int timeout)
4108 /* The FIFO handles the queue so the kernel will do the right
4109 thing waiting on chars_in_buffer before calling us. No work
4113 static int gsmtty_tiocmget(struct tty_struct *tty)
4115 struct gsm_dlci *dlci = tty->driver_data;
4116 if (dlci->state == DLCI_CLOSED)
4118 return dlci->modem_rx;
4121 static int gsmtty_tiocmset(struct tty_struct *tty,
4122 unsigned int set, unsigned int clear)
4124 struct gsm_dlci *dlci = tty->driver_data;
4125 unsigned int modem_tx = dlci->modem_tx;
4127 if (dlci->state == DLCI_CLOSED)
4132 if (modem_tx != dlci->modem_tx) {
4133 dlci->modem_tx = modem_tx;
4134 return gsm_modem_update(dlci, 0);
4140 static int gsmtty_ioctl(struct tty_struct *tty,
4141 unsigned int cmd, unsigned long arg)
4143 struct gsm_dlci *dlci = tty->driver_data;
4144 struct gsm_netconfig nc;
4147 if (dlci->state == DLCI_CLOSED)
4150 case GSMIOC_ENABLE_NET:
4151 if (copy_from_user(&nc, (void __user *)arg, sizeof(nc)))
4153 nc.if_name[IFNAMSIZ-1] = '\0';
4154 /* return net interface index or error code */
4155 mutex_lock(&dlci->mutex);
4156 index = gsm_create_network(dlci, &nc);
4157 mutex_unlock(&dlci->mutex);
4158 if (copy_to_user((void __user *)arg, &nc, sizeof(nc)))
4161 case GSMIOC_DISABLE_NET:
4162 if (!capable(CAP_NET_ADMIN))
4164 mutex_lock(&dlci->mutex);
4165 gsm_destroy_network(dlci);
4166 mutex_unlock(&dlci->mutex);
4169 return gsm_wait_modem_change(dlci, (u32)arg);
4171 return -ENOIOCTLCMD;
4175 static void gsmtty_set_termios(struct tty_struct *tty,
4176 const struct ktermios *old)
4178 struct gsm_dlci *dlci = tty->driver_data;
4179 if (dlci->state == DLCI_CLOSED)
4181 /* For the moment its fixed. In actual fact the speed information
4182 for the virtual channel can be propogated in both directions by
4183 the RPN control message. This however rapidly gets nasty as we
4184 then have to remap modem signals each way according to whether
4185 our virtual cable is null modem etc .. */
4186 tty_termios_copy_hw(&tty->termios, old);
4189 static void gsmtty_throttle(struct tty_struct *tty)
4191 struct gsm_dlci *dlci = tty->driver_data;
4192 if (dlci->state == DLCI_CLOSED)
4195 dlci->modem_tx &= ~TIOCM_RTS;
4196 dlci->throttled = true;
4197 /* Send an MSC with RTS cleared */
4198 gsm_modem_update(dlci, 0);
4201 static void gsmtty_unthrottle(struct tty_struct *tty)
4203 struct gsm_dlci *dlci = tty->driver_data;
4204 if (dlci->state == DLCI_CLOSED)
4207 dlci->modem_tx |= TIOCM_RTS;
4208 dlci->throttled = false;
4209 /* Send an MSC with RTS set */
4210 gsm_modem_update(dlci, 0);
4213 static int gsmtty_break_ctl(struct tty_struct *tty, int state)
4215 struct gsm_dlci *dlci = tty->driver_data;
4216 int encode = 0; /* Off */
4217 if (dlci->state == DLCI_CLOSED)
4220 if (state == -1) /* "On indefinitely" - we can't encode this
4223 else if (state > 0) {
4224 encode = state / 200; /* mS to encoding */
4226 encode = 0x0F; /* Best effort */
4228 return gsm_modem_update(dlci, encode);
4231 static void gsmtty_cleanup(struct tty_struct *tty)
4233 struct gsm_dlci *dlci = tty->driver_data;
4234 struct gsm_mux *gsm = dlci->gsm;
4237 dlci_put(gsm->dlci[0]);
4241 /* Virtual ttys for the demux */
4242 static const struct tty_operations gsmtty_ops = {
4243 .install = gsmtty_install,
4244 .open = gsmtty_open,
4245 .close = gsmtty_close,
4246 .write = gsmtty_write,
4247 .write_room = gsmtty_write_room,
4248 .chars_in_buffer = gsmtty_chars_in_buffer,
4249 .flush_buffer = gsmtty_flush_buffer,
4250 .ioctl = gsmtty_ioctl,
4251 .throttle = gsmtty_throttle,
4252 .unthrottle = gsmtty_unthrottle,
4253 .set_termios = gsmtty_set_termios,
4254 .hangup = gsmtty_hangup,
4255 .wait_until_sent = gsmtty_wait_until_sent,
4256 .tiocmget = gsmtty_tiocmget,
4257 .tiocmset = gsmtty_tiocmset,
4258 .break_ctl = gsmtty_break_ctl,
4259 .cleanup = gsmtty_cleanup,
4264 static int __init gsm_init(void)
4266 /* Fill in our line protocol discipline, and register it */
4267 int status = tty_register_ldisc(&tty_ldisc_packet);
4269 pr_err("n_gsm: can't register line discipline (err = %d)\n",
4274 gsm_tty_driver = tty_alloc_driver(GSM_TTY_MINORS, TTY_DRIVER_REAL_RAW |
4275 TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_HARDWARE_BREAK);
4276 if (IS_ERR(gsm_tty_driver)) {
4277 pr_err("gsm_init: tty allocation failed.\n");
4278 status = PTR_ERR(gsm_tty_driver);
4279 goto err_unreg_ldisc;
4281 gsm_tty_driver->driver_name = "gsmtty";
4282 gsm_tty_driver->name = "gsmtty";
4283 gsm_tty_driver->major = 0; /* Dynamic */
4284 gsm_tty_driver->minor_start = 0;
4285 gsm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
4286 gsm_tty_driver->subtype = SERIAL_TYPE_NORMAL;
4287 gsm_tty_driver->init_termios = tty_std_termios;
4289 gsm_tty_driver->init_termios.c_lflag &= ~ECHO;
4290 tty_set_operations(gsm_tty_driver, &gsmtty_ops);
4292 if (tty_register_driver(gsm_tty_driver)) {
4293 pr_err("gsm_init: tty registration failed.\n");
4295 goto err_put_driver;
4297 pr_debug("gsm_init: loaded as %d,%d.\n",
4298 gsm_tty_driver->major, gsm_tty_driver->minor_start);
4301 tty_driver_kref_put(gsm_tty_driver);
4303 tty_unregister_ldisc(&tty_ldisc_packet);
4307 static void __exit gsm_exit(void)
4309 tty_unregister_ldisc(&tty_ldisc_packet);
4310 tty_unregister_driver(gsm_tty_driver);
4311 tty_driver_kref_put(gsm_tty_driver);
4314 module_init(gsm_init);
4315 module_exit(gsm_exit);
4318 MODULE_LICENSE("GPL");
4319 MODULE_ALIAS_LDISC(N_GSM0710);