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