1 // SPDX-License-Identifier: GPL-2.0
3 * n_gsm.c GSM 0710 tty multiplexor
4 * Copyright (c) 2009/10 Intel Corporation
6 * * THIS IS A DEVELOPMENT SNAPSHOT IT IS NOT A FINAL RELEASE *
9 * tty -> DLCI fifo -> scheduler -> GSM MUX data queue ---o-> ldisc
10 * control message -> GSM MUX control queue --ยด
13 * ldisc -> gsm_queue() -o--> tty
14 * `-> gsm_control_response()
17 * Mostly done: ioctls for setting modes/timing
18 * Partly done: hooks so you can pull off frames to non tty devs
19 * Restart DLCI 0 when it closes ?
20 * Improve the tx engine
21 * Resolve tx side locking by adding a queue_head and routing
22 * all control traffic via it
23 * General tidy/document
24 * Review the locking/move to refcounts more (mux now moved to an
25 * alloc/free model ready)
26 * Use newest tty open/close port helpers and install hooks
27 * What to do about power functions ?
28 * Termios setting and negotiation
29 * Do we need a 'which mux are you' ioctl to correlate mux and tty sets
33 #include <linux/types.h>
34 #include <linux/major.h>
35 #include <linux/errno.h>
36 #include <linux/signal.h>
37 #include <linux/fcntl.h>
38 #include <linux/sched/signal.h>
39 #include <linux/interrupt.h>
40 #include <linux/tty.h>
41 #include <linux/bitfield.h>
42 #include <linux/ctype.h>
44 #include <linux/math.h>
45 #include <linux/nospec.h>
46 #include <linux/string.h>
47 #include <linux/slab.h>
48 #include <linux/poll.h>
49 #include <linux/bitops.h>
50 #include <linux/file.h>
51 #include <linux/uaccess.h>
52 #include <linux/module.h>
53 #include <linux/timer.h>
54 #include <linux/tty_flip.h>
55 #include <linux/tty_driver.h>
56 #include <linux/serial.h>
57 #include <linux/kfifo.h>
58 #include <linux/skbuff.h>
61 #include <linux/netdevice.h>
62 #include <linux/etherdevice.h>
63 #include <linux/gsmmux.h>
67 module_param(debug, int, 0600);
69 /* Module debug bits */
70 #define DBG_DUMP BIT(0) /* Data transmission dump. */
71 #define DBG_CD_ON BIT(1) /* Always assume CD line on. */
72 #define DBG_DATA BIT(2) /* Data transmission details. */
73 #define DBG_ERRORS BIT(3) /* Details for fail conditions. */
74 #define DBG_TTY BIT(4) /* Transmission statistics for DLCI TTYs. */
75 #define DBG_PAYLOAD BIT(5) /* Limits DBG_DUMP to payload frames. */
77 /* Defaults: these are from the specification */
79 #define T1 10 /* 100mS */
80 #define T2 34 /* 333mS */
81 #define T3 10 /* 10s */
82 #define N2 3 /* Retry 3 times */
83 #define K 2 /* outstanding I frames */
85 #define MAX_T3 255 /* In seconds. */
86 #define MAX_WINDOW_SIZE 7 /* Limit of K in error recovery mode. */
88 /* Use long timers for testing at low speed with debug on */
95 * Semi-arbitrary buffer size limits. 0710 is normally run with 32-64 byte
96 * limits so this is plenty
100 #define MIN_MTU (PROT_OVERHEAD + 1)
101 /* SOF, ADDR, CTRL, LEN1, LEN2, ..., FCS, EOF */
102 #define PROT_OVERHEAD 7
103 #define GSM_NET_TX_TIMEOUT (HZ*10)
106 * struct gsm_mux_net - network interface
108 * Created when net interface is initialized.
112 struct gsm_dlci *dlci;
116 * Each block of data we have queued to go out is in the form of
117 * a gsm_msg which holds everything we need in a link layer independent
122 struct list_head list;
123 u8 addr; /* DLCI address + flags */
124 u8 ctrl; /* Control byte + flags */
125 unsigned int len; /* Length of data block (can be zero) */
126 unsigned char *data; /* Points into buffer but not at the start */
127 unsigned char buffer[];
130 enum gsm_dlci_state {
132 DLCI_WAITING_CONFIG, /* Waiting for DLCI configuration from user */
133 DLCI_CONFIGURE, /* Sending PN (for adaption > 1) */
134 DLCI_OPENING, /* Sending SABM not seen UA */
135 DLCI_OPEN, /* SABM/UA complete */
136 DLCI_CLOSING, /* Sending DISC not seen UA/DM */
140 DLCI_MODE_ABM, /* Normal Asynchronous Balanced Mode */
141 DLCI_MODE_ADM, /* Asynchronous Disconnected Mode */
145 * Each active data link has a gsm_dlci structure associated which ties
146 * the link layer to an optional tty (if the tty side is open). To avoid
147 * complexity right now these are only ever freed up when the mux is
150 * At the moment we don't free DLCI objects until the mux is torn down
151 * this avoid object life time issues but might be worth review later.
157 enum gsm_dlci_state state;
161 enum gsm_dlci_mode mode;
162 spinlock_t lock; /* Protects the internal state */
163 struct timer_list t1; /* Retransmit timer for SABM and UA */
165 /* Uplink tty if active */
166 struct tty_port port; /* The tty bound to this DLCI if there is one */
167 #define TX_SIZE 4096 /* Must be power of 2. */
168 struct kfifo fifo; /* Queue fifo for the DLCI */
169 int adaption; /* Adaption layer in use */
171 u32 modem_rx; /* Our incoming virtual modem lines */
172 u32 modem_tx; /* Our outgoing modem lines */
174 bool dead; /* Refuse re-open */
176 u8 prio; /* Priority */
177 u8 ftype; /* Frame type */
178 u8 k; /* Window size */
180 bool throttled; /* Private copy of throttle state */
181 bool constipated; /* Throttle status for outgoing */
183 struct sk_buff *skb; /* Frame being sent */
184 struct sk_buff_head skb_list; /* Queued frames */
185 /* Data handling callback */
186 void (*data)(struct gsm_dlci *dlci, const u8 *data, int len);
187 void (*prev_data)(struct gsm_dlci *dlci, const u8 *data, int len);
188 struct net_device *net; /* network interface, if created */
192 * Parameter bits used for parameter negotiation according to 3GPP 27.010
196 struct gsm_dlci_param_bits {
206 static_assert(sizeof(struct gsm_dlci_param_bits) == 8);
208 #define PN_D_FIELD_DLCI GENMASK(5, 0)
209 #define PN_I_CL_FIELD_FTYPE GENMASK(3, 0)
210 #define PN_I_CL_FIELD_ADAPTION GENMASK(7, 4)
211 #define PN_P_FIELD_PRIO GENMASK(5, 0)
212 #define PN_T_FIELD_T1 GENMASK(7, 0)
213 #define PN_N_FIELD_N1 GENMASK(15, 0)
214 #define PN_NA_FIELD_N2 GENMASK(7, 0)
215 #define PN_K_FIELD_K GENMASK(2, 0)
217 /* Total number of supported devices */
218 #define GSM_TTY_MINORS 256
220 /* DLCI 0, 62/63 are special or reserved see gsmtty_open */
225 * DLCI 0 is used to pass control blocks out of band of the data
226 * flow (and with a higher link priority). One command can be outstanding
227 * at a time and we use this structure to manage them. They are created
228 * and destroyed by the user context, and updated by the receive paths
233 u8 cmd; /* Command we are issuing */
234 u8 *data; /* Data for the command in case we retransmit */
235 int len; /* Length of block for retransmission */
236 int done; /* Done flag */
237 int error; /* Error if any */
260 * Each GSM mux we have is represented by this structure. If we are
261 * operating as an ldisc then we use this structure as our ldisc
262 * state. We need to sort out lifetimes and locking with respect
263 * to the gsm mux array. For now we don't free DLCI objects that
264 * have been instantiated until the mux itself is terminated.
266 * To consider further: tty open versus mux shutdown.
270 struct tty_struct *tty; /* The tty our ldisc is bound to */
276 /* Events on the GSM channel */
277 wait_queue_head_t event;
279 /* ldisc send work */
280 struct work_struct tx_work;
282 /* Bits for GSM mode decoding */
286 enum gsm_mux_state state;
288 unsigned int address;
291 enum gsm_encoding encoding;
294 u8 *txframe; /* TX framing buffer */
296 /* Method for the receiver side */
297 void (*receive)(struct gsm_mux *gsm, u8 ch);
302 int initiator; /* Did we initiate connection */
303 bool dead; /* Has the mux been shut down */
304 struct gsm_dlci *dlci[NUM_DLCI];
305 int old_c_iflag; /* termios c_iflag value before attach */
306 bool constipated; /* Asked by remote to shut up */
307 bool has_devices; /* Devices were registered */
310 unsigned int tx_bytes; /* TX data outstanding */
311 #define TX_THRESH_HI 8192
312 #define TX_THRESH_LO 2048
313 struct list_head tx_ctrl_list; /* Pending control packets */
314 struct list_head tx_data_list; /* Pending data packets */
316 /* Control messages */
317 struct timer_list kick_timer; /* Kick TX queuing on timeout */
318 struct timer_list t2_timer; /* Retransmit timer for commands */
319 int cretries; /* Command retry counter */
320 struct gsm_control *pending_cmd;/* Our current pending command */
321 spinlock_t control_lock; /* Protects the pending command */
324 struct timer_list ka_timer; /* Keep-alive response timer */
325 u8 ka_num; /* Keep-alive match pattern */
326 signed int ka_retries; /* Keep-alive retry counter, -1 if not yet initialized */
329 int adaption; /* 1 or 2 supported */
330 u8 ftype; /* UI or UIH */
331 int t1, t2; /* Timers in 1/100th of a sec */
332 unsigned int t3; /* Power wake-up timer in seconds. */
333 int n2; /* Retry count */
334 u8 k; /* Window size */
335 bool wait_config; /* Wait for configuration by ioctl before DLCI open */
336 u32 keep_alive; /* Control channel keep-alive in 10ms */
338 /* Statistics (not currently exposed) */
339 unsigned long bad_fcs;
340 unsigned long malformed;
341 unsigned long io_error;
342 unsigned long bad_size;
343 unsigned long unsupported;
348 * Mux objects - needed so that we can translate a tty index into the
349 * relevant mux and DLCI.
352 #define MAX_MUX 4 /* 256 minors */
353 static struct gsm_mux *gsm_mux[MAX_MUX]; /* GSM muxes */
354 static DEFINE_SPINLOCK(gsm_mux_lock);
356 static struct tty_driver *gsm_tty_driver;
359 * This section of the driver logic implements the GSM encodings
360 * both the basic and the 'advanced'. Reliable transport is not
368 /* I is special: the rest are ..*/
379 /* Channel commands */
381 #define CMD_TEST 0x11
384 #define CMD_FCOFF 0x31
387 #define CMD_FCON 0x51
392 /* Virtual modem bits */
399 #define GSM0_SOF 0xF9
400 #define GSM1_SOF 0x7E
401 #define GSM1_ESCAPE 0x7D
402 #define GSM1_ESCAPE_BITS 0x20
405 #define ISO_IEC_646_MASK 0x7F
407 static const struct tty_port_operations gsm_port_ops;
410 * CRC table for GSM 0710
413 static const u8 gsm_fcs8[256] = {
414 0x00, 0x91, 0xE3, 0x72, 0x07, 0x96, 0xE4, 0x75,
415 0x0E, 0x9F, 0xED, 0x7C, 0x09, 0x98, 0xEA, 0x7B,
416 0x1C, 0x8D, 0xFF, 0x6E, 0x1B, 0x8A, 0xF8, 0x69,
417 0x12, 0x83, 0xF1, 0x60, 0x15, 0x84, 0xF6, 0x67,
418 0x38, 0xA9, 0xDB, 0x4A, 0x3F, 0xAE, 0xDC, 0x4D,
419 0x36, 0xA7, 0xD5, 0x44, 0x31, 0xA0, 0xD2, 0x43,
420 0x24, 0xB5, 0xC7, 0x56, 0x23, 0xB2, 0xC0, 0x51,
421 0x2A, 0xBB, 0xC9, 0x58, 0x2D, 0xBC, 0xCE, 0x5F,
422 0x70, 0xE1, 0x93, 0x02, 0x77, 0xE6, 0x94, 0x05,
423 0x7E, 0xEF, 0x9D, 0x0C, 0x79, 0xE8, 0x9A, 0x0B,
424 0x6C, 0xFD, 0x8F, 0x1E, 0x6B, 0xFA, 0x88, 0x19,
425 0x62, 0xF3, 0x81, 0x10, 0x65, 0xF4, 0x86, 0x17,
426 0x48, 0xD9, 0xAB, 0x3A, 0x4F, 0xDE, 0xAC, 0x3D,
427 0x46, 0xD7, 0xA5, 0x34, 0x41, 0xD0, 0xA2, 0x33,
428 0x54, 0xC5, 0xB7, 0x26, 0x53, 0xC2, 0xB0, 0x21,
429 0x5A, 0xCB, 0xB9, 0x28, 0x5D, 0xCC, 0xBE, 0x2F,
430 0xE0, 0x71, 0x03, 0x92, 0xE7, 0x76, 0x04, 0x95,
431 0xEE, 0x7F, 0x0D, 0x9C, 0xE9, 0x78, 0x0A, 0x9B,
432 0xFC, 0x6D, 0x1F, 0x8E, 0xFB, 0x6A, 0x18, 0x89,
433 0xF2, 0x63, 0x11, 0x80, 0xF5, 0x64, 0x16, 0x87,
434 0xD8, 0x49, 0x3B, 0xAA, 0xDF, 0x4E, 0x3C, 0xAD,
435 0xD6, 0x47, 0x35, 0xA4, 0xD1, 0x40, 0x32, 0xA3,
436 0xC4, 0x55, 0x27, 0xB6, 0xC3, 0x52, 0x20, 0xB1,
437 0xCA, 0x5B, 0x29, 0xB8, 0xCD, 0x5C, 0x2E, 0xBF,
438 0x90, 0x01, 0x73, 0xE2, 0x97, 0x06, 0x74, 0xE5,
439 0x9E, 0x0F, 0x7D, 0xEC, 0x99, 0x08, 0x7A, 0xEB,
440 0x8C, 0x1D, 0x6F, 0xFE, 0x8B, 0x1A, 0x68, 0xF9,
441 0x82, 0x13, 0x61, 0xF0, 0x85, 0x14, 0x66, 0xF7,
442 0xA8, 0x39, 0x4B, 0xDA, 0xAF, 0x3E, 0x4C, 0xDD,
443 0xA6, 0x37, 0x45, 0xD4, 0xA1, 0x30, 0x42, 0xD3,
444 0xB4, 0x25, 0x57, 0xC6, 0xB3, 0x22, 0x50, 0xC1,
445 0xBA, 0x2B, 0x59, 0xC8, 0xBD, 0x2C, 0x5E, 0xCF
448 #define INIT_FCS 0xFF
449 #define GOOD_FCS 0xCF
451 static void gsm_dlci_close(struct gsm_dlci *dlci);
452 static int gsmld_output(struct gsm_mux *gsm, u8 *data, int len);
453 static int gsm_modem_update(struct gsm_dlci *dlci, u8 brk);
454 static struct gsm_msg *gsm_data_alloc(struct gsm_mux *gsm, u8 addr, int len,
456 static int gsm_send_packet(struct gsm_mux *gsm, struct gsm_msg *msg);
457 static struct gsm_dlci *gsm_dlci_alloc(struct gsm_mux *gsm, int addr);
458 static void gsmld_write_trigger(struct gsm_mux *gsm);
459 static void gsmld_write_task(struct work_struct *work);
462 * gsm_fcs_add - update FCS
466 * Update the FCS to include c. Uses the algorithm in the specification
470 static inline u8 gsm_fcs_add(u8 fcs, u8 c)
472 return gsm_fcs8[fcs ^ c];
476 * gsm_fcs_add_block - update FCS for a block
479 * @len: length of buffer
481 * Update the FCS to include c. Uses the algorithm in the specification
485 static inline u8 gsm_fcs_add_block(u8 fcs, u8 *c, int len)
488 fcs = gsm_fcs8[fcs ^ *c++];
493 * gsm_read_ea - read a byte into an EA
494 * @val: variable holding value
495 * @c: byte going into the EA
497 * Processes one byte of an EA. Updates the passed variable
498 * and returns 1 if the EA is now completely read
501 static int gsm_read_ea(unsigned int *val, u8 c)
503 /* Add the next 7 bits into the value */
506 /* Was this the last byte of the EA 1 = yes*/
511 * gsm_read_ea_val - read a value until EA
512 * @val: variable holding value
513 * @data: buffer of data
514 * @dlen: length of data
516 * Processes an EA value. Updates the passed variable and
517 * returns the processed data length.
519 static unsigned int gsm_read_ea_val(unsigned int *val, const u8 *data, int dlen)
521 unsigned int len = 0;
523 for (; dlen > 0; dlen--) {
525 if (gsm_read_ea(val, *data++))
532 * gsm_encode_modem - encode modem data bits
533 * @dlci: DLCI to encode from
535 * Returns the correct GSM encoded modem status bits (6 bit field) for
536 * the current status of the DLCI and attached tty object
539 static u8 gsm_encode_modem(const struct gsm_dlci *dlci)
542 /* FC is true flow control not modem bits */
545 if (dlci->modem_tx & TIOCM_DTR)
546 modembits |= MDM_RTC;
547 if (dlci->modem_tx & TIOCM_RTS)
548 modembits |= MDM_RTR;
549 if (dlci->modem_tx & TIOCM_RI)
551 if (dlci->modem_tx & TIOCM_CD || dlci->gsm->initiator)
553 /* special mappings for passive side to operate as UE */
554 if (dlci->modem_tx & TIOCM_OUT1)
556 if (dlci->modem_tx & TIOCM_OUT2)
561 static void gsm_hex_dump_bytes(const char *fname, const u8 *data,
567 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE, 16, 1, data, len,
572 prefix = kasprintf(GFP_ATOMIC, "%s: ", fname);
575 print_hex_dump(KERN_INFO, prefix, DUMP_PREFIX_OFFSET, 16, 1, data, len,
581 * gsm_encode_params - encode DLCI parameters
582 * @dlci: DLCI to encode from
583 * @params: buffer to fill with the encoded parameters
585 * Encodes the parameters according to GSM 07.10 section 5.4.6.3.1
588 static int gsm_encode_params(const struct gsm_dlci *dlci,
589 struct gsm_dlci_param_bits *params)
591 const struct gsm_mux *gsm = dlci->gsm;
594 switch (dlci->ftype) {
602 pr_debug("unsupported frame type %d\n", dlci->ftype);
606 switch (dlci->adaption) {
607 case 1: /* Unstructured */
608 cl = 0; /* convergence layer type 1 */
610 case 2: /* Unstructured with modem bits. */
611 cl = 1; /* convergence layer type 2 */
614 pr_debug("unsupported adaption %d\n", dlci->adaption);
618 params->d_bits = FIELD_PREP(PN_D_FIELD_DLCI, dlci->addr);
619 /* UIH, convergence layer type 1 */
620 params->i_cl_bits = FIELD_PREP(PN_I_CL_FIELD_FTYPE, i) |
621 FIELD_PREP(PN_I_CL_FIELD_ADAPTION, cl);
622 params->p_bits = FIELD_PREP(PN_P_FIELD_PRIO, dlci->prio);
623 params->t_bits = FIELD_PREP(PN_T_FIELD_T1, gsm->t1);
624 params->n_bits = cpu_to_le16(FIELD_PREP(PN_N_FIELD_N1, dlci->mtu));
625 params->na_bits = FIELD_PREP(PN_NA_FIELD_N2, gsm->n2);
626 params->k_bits = FIELD_PREP(PN_K_FIELD_K, dlci->k);
632 * gsm_register_devices - register all tty devices for a given mux index
634 * @driver: the tty driver that describes the tty devices
635 * @index: the mux number is used to calculate the minor numbers of the
636 * ttys for this mux and may differ from the position in the
639 static int gsm_register_devices(struct tty_driver *driver, unsigned int index)
645 if (!driver || index >= MAX_MUX)
648 base = index * NUM_DLCI; /* first minor for this index */
649 for (i = 1; i < NUM_DLCI; i++) {
650 /* Don't register device 0 - this is the control channel
651 * and not a usable tty interface
653 dev = tty_register_device(gsm_tty_driver, base + i, NULL);
655 if (debug & DBG_ERRORS)
656 pr_info("%s failed to register device minor %u",
658 for (i--; i >= 1; i--)
659 tty_unregister_device(gsm_tty_driver, base + i);
668 * gsm_unregister_devices - unregister all tty devices for a given mux index
670 * @driver: the tty driver that describes the tty devices
671 * @index: the mux number is used to calculate the minor numbers of the
672 * ttys for this mux and may differ from the position in the
675 static void gsm_unregister_devices(struct tty_driver *driver,
681 if (!driver || index >= MAX_MUX)
684 base = index * NUM_DLCI; /* first minor for this index */
685 for (i = 1; i < NUM_DLCI; i++) {
686 /* Don't unregister device 0 - this is the control
687 * channel and not a usable tty interface
689 tty_unregister_device(gsm_tty_driver, base + i);
694 * gsm_print_packet - display a frame for debug
695 * @hdr: header to print before decode
696 * @addr: address EA from the frame
697 * @cr: C/R bit seen as initiator
698 * @control: control including PF bit
699 * @data: following data bytes
700 * @dlen: length of data
702 * Displays a packet in human readable format for debugging purposes. The
703 * style is based on amateur radio LAP-B dump display.
706 static void gsm_print_packet(const char *hdr, int addr, int cr,
707 u8 control, const u8 *data, int dlen)
709 if (!(debug & DBG_DUMP))
711 /* Only show user payload frames if debug & DBG_PAYLOAD */
712 if (!(debug & DBG_PAYLOAD) && addr != 0)
713 if ((control & ~PF) == UI || (control & ~PF) == UIH)
716 pr_info("%s %d) %c: ", hdr, addr, "RC"[cr]);
718 switch (control & ~PF) {
738 if (!(control & 0x01)) {
739 pr_cont("I N(S)%d N(R)%d",
740 (control & 0x0E) >> 1, (control & 0xE0) >> 5);
741 } else switch (control & 0x0F) {
743 pr_cont("RR(%d)", (control & 0xE0) >> 5);
746 pr_cont("RNR(%d)", (control & 0xE0) >> 5);
749 pr_cont("REJ(%d)", (control & 0xE0) >> 5);
752 pr_cont("[%02X]", control);
761 gsm_hex_dump_bytes(NULL, data, dlen);
766 * Link level transmission side
770 * gsm_stuff_frame - bytestuff a packet
771 * @input: input buffer
772 * @output: output buffer
773 * @len: length of input
775 * Expand a buffer by bytestuffing it. The worst case size change
776 * is doubling and the caller is responsible for handing out
777 * suitable sized buffers.
780 static int gsm_stuff_frame(const u8 *input, u8 *output, int len)
784 if (*input == GSM1_SOF || *input == GSM1_ESCAPE
785 || (*input & ISO_IEC_646_MASK) == XON
786 || (*input & ISO_IEC_646_MASK) == XOFF) {
787 *output++ = GSM1_ESCAPE;
788 *output++ = *input++ ^ GSM1_ESCAPE_BITS;
791 *output++ = *input++;
798 * gsm_send - send a control frame
800 * @addr: address for control frame
801 * @cr: command/response bit seen as initiator
802 * @control: control byte including PF bit
804 * Format up and transmit a control frame. These should be transmitted
805 * ahead of data when they are needed.
807 static int gsm_send(struct gsm_mux *gsm, int addr, int cr, int control)
814 msg = gsm_data_alloc(gsm, addr, 0, control);
818 /* toggle C/R coding if not initiator */
819 ocr = cr ^ (gsm->initiator ? 0 : 1);
823 *dp++ = (addr << 2) | (ocr << 1) | EA;
826 if (gsm->encoding == GSM_BASIC_OPT)
827 *dp++ = EA; /* Length of data = 0 */
829 *dp = 0xFF - gsm_fcs_add_block(INIT_FCS, msg->data, dp - msg->data);
830 msg->len = (dp - msg->data) + 1;
832 gsm_print_packet("Q->", addr, cr, control, NULL, 0);
834 spin_lock_irqsave(&gsm->tx_lock, flags);
835 list_add_tail(&msg->list, &gsm->tx_ctrl_list);
836 gsm->tx_bytes += msg->len;
837 spin_unlock_irqrestore(&gsm->tx_lock, flags);
838 gsmld_write_trigger(gsm);
844 * gsm_dlci_clear_queues - remove outstanding data for a DLCI
846 * @dlci: clear for this DLCI
848 * Clears the data queues for a given DLCI.
850 static void gsm_dlci_clear_queues(struct gsm_mux *gsm, struct gsm_dlci *dlci)
852 struct gsm_msg *msg, *nmsg;
853 int addr = dlci->addr;
856 /* Clear DLCI write fifo first */
857 spin_lock_irqsave(&dlci->lock, flags);
858 kfifo_reset(&dlci->fifo);
859 spin_unlock_irqrestore(&dlci->lock, flags);
861 /* Clear data packets in MUX write queue */
862 spin_lock_irqsave(&gsm->tx_lock, flags);
863 list_for_each_entry_safe(msg, nmsg, &gsm->tx_data_list, list) {
864 if (msg->addr != addr)
866 gsm->tx_bytes -= msg->len;
867 list_del(&msg->list);
870 spin_unlock_irqrestore(&gsm->tx_lock, flags);
874 * gsm_response - send a control response
876 * @addr: address for control frame
877 * @control: control byte including PF bit
879 * Format up and transmit a link level response frame.
882 static inline void gsm_response(struct gsm_mux *gsm, int addr, int control)
884 gsm_send(gsm, addr, 0, control);
888 * gsm_command - send a control command
890 * @addr: address for control frame
891 * @control: control byte including PF bit
893 * Format up and transmit a link level command frame.
896 static inline void gsm_command(struct gsm_mux *gsm, int addr, int control)
898 gsm_send(gsm, addr, 1, control);
901 /* Data transmission */
903 #define HDR_LEN 6 /* ADDR CTRL [LEN.2] DATA FCS */
906 * gsm_data_alloc - allocate data frame
908 * @addr: DLCI address
909 * @len: length excluding header and FCS
910 * @ctrl: control byte
912 * Allocate a new data buffer for sending frames with data. Space is left
913 * at the front for header bytes but that is treated as an implementation
914 * detail and not for the high level code to use
917 static struct gsm_msg *gsm_data_alloc(struct gsm_mux *gsm, u8 addr, int len,
920 struct gsm_msg *m = kmalloc(sizeof(struct gsm_msg) + len + HDR_LEN,
924 m->data = m->buffer + HDR_LEN - 1; /* Allow for FCS */
928 INIT_LIST_HEAD(&m->list);
933 * gsm_send_packet - sends a single packet
935 * @msg: packet to send
937 * The given packet is encoded and sent out. No memory is freed.
938 * The caller must hold the gsm tx lock.
940 static int gsm_send_packet(struct gsm_mux *gsm, struct gsm_msg *msg)
945 if (gsm->encoding == GSM_BASIC_OPT) {
946 gsm->txframe[0] = GSM0_SOF;
947 memcpy(gsm->txframe + 1, msg->data, msg->len);
948 gsm->txframe[msg->len + 1] = GSM0_SOF;
951 gsm->txframe[0] = GSM1_SOF;
952 len = gsm_stuff_frame(msg->data, gsm->txframe + 1, msg->len);
953 gsm->txframe[len + 1] = GSM1_SOF;
957 if (debug & DBG_DATA)
958 gsm_hex_dump_bytes(__func__, gsm->txframe, len);
959 gsm_print_packet("-->", msg->addr, gsm->initiator, msg->ctrl, msg->data,
962 ret = gsmld_output(gsm, gsm->txframe, len);
965 /* FIXME: Can eliminate one SOF in many more cases */
966 gsm->tx_bytes -= msg->len;
972 * gsm_is_flow_ctrl_msg - checks if flow control message
973 * @msg: message to check
975 * Returns true if the given message is a flow control command of the
976 * control channel. False is returned in any other case.
978 static bool gsm_is_flow_ctrl_msg(struct gsm_msg *msg)
985 switch (msg->ctrl & ~PF) {
989 if (gsm_read_ea_val(&cmd, msg->data + 2, msg->len - 2) < 1)
1003 * gsm_data_kick - poke the queue
1006 * The tty device has called us to indicate that room has appeared in
1007 * the transmit queue. Ram more data into the pipe if we have any.
1008 * If we have been flow-stopped by a CMD_FCOFF, then we can only
1009 * send messages on DLCI0 until CMD_FCON. The caller must hold
1012 static int gsm_data_kick(struct gsm_mux *gsm)
1014 struct gsm_msg *msg, *nmsg;
1015 struct gsm_dlci *dlci;
1018 clear_bit(TTY_DO_WRITE_WAKEUP, &gsm->tty->flags);
1020 /* Serialize control messages and control channel messages first */
1021 list_for_each_entry_safe(msg, nmsg, &gsm->tx_ctrl_list, list) {
1022 if (gsm->constipated && !gsm_is_flow_ctrl_msg(msg))
1024 ret = gsm_send_packet(gsm, msg);
1029 /* ldisc not open */
1030 gsm->tx_bytes -= msg->len;
1031 list_del(&msg->list);
1036 list_del(&msg->list);
1043 if (gsm->constipated)
1046 /* Serialize other channels */
1047 if (list_empty(&gsm->tx_data_list))
1049 list_for_each_entry_safe(msg, nmsg, &gsm->tx_data_list, list) {
1050 dlci = gsm->dlci[msg->addr];
1051 /* Send only messages for DLCIs with valid state */
1052 if (dlci->state != DLCI_OPEN) {
1053 gsm->tx_bytes -= msg->len;
1054 list_del(&msg->list);
1058 ret = gsm_send_packet(gsm, msg);
1063 /* ldisc not open */
1064 gsm->tx_bytes -= msg->len;
1065 list_del(&msg->list);
1070 list_del(&msg->list);
1081 * __gsm_data_queue - queue a UI or UIH frame
1082 * @dlci: DLCI sending the data
1083 * @msg: message queued
1085 * Add data to the transmit queue and try and get stuff moving
1086 * out of the mux tty if not already doing so. The Caller must hold
1090 static void __gsm_data_queue(struct gsm_dlci *dlci, struct gsm_msg *msg)
1092 struct gsm_mux *gsm = dlci->gsm;
1094 u8 *fcs = dp + msg->len;
1096 /* Fill in the header */
1097 if (gsm->encoding == GSM_BASIC_OPT) {
1099 *--dp = (msg->len << 1) | EA;
1101 *--dp = (msg->len >> 7); /* bits 7 - 15 */
1102 *--dp = (msg->len & 127) << 1; /* bits 0 - 6 */
1108 *--dp = (msg->addr << 2) | CR | EA;
1110 *--dp = (msg->addr << 2) | EA;
1111 *fcs = gsm_fcs_add_block(INIT_FCS, dp , msg->data - dp);
1112 /* Ugly protocol layering violation */
1113 if (msg->ctrl == UI || msg->ctrl == (UI|PF))
1114 *fcs = gsm_fcs_add_block(*fcs, msg->data, msg->len);
1117 gsm_print_packet("Q> ", msg->addr, gsm->initiator, msg->ctrl,
1118 msg->data, msg->len);
1120 /* Move the header back and adjust the length, also allow for the FCS
1121 now tacked on the end */
1122 msg->len += (msg->data - dp) + 1;
1125 /* Add to the actual output queue */
1126 switch (msg->ctrl & ~PF) {
1129 if (msg->addr > 0) {
1130 list_add_tail(&msg->list, &gsm->tx_data_list);
1135 list_add_tail(&msg->list, &gsm->tx_ctrl_list);
1138 gsm->tx_bytes += msg->len;
1140 gsmld_write_trigger(gsm);
1141 mod_timer(&gsm->kick_timer, jiffies + 10 * gsm->t1 * HZ / 100);
1145 * gsm_data_queue - queue a UI or UIH frame
1146 * @dlci: DLCI sending the data
1147 * @msg: message queued
1149 * Add data to the transmit queue and try and get stuff moving
1150 * out of the mux tty if not already doing so. Take the
1151 * the gsm tx lock and dlci lock.
1154 static void gsm_data_queue(struct gsm_dlci *dlci, struct gsm_msg *msg)
1156 unsigned long flags;
1157 spin_lock_irqsave(&dlci->gsm->tx_lock, flags);
1158 __gsm_data_queue(dlci, msg);
1159 spin_unlock_irqrestore(&dlci->gsm->tx_lock, flags);
1163 * gsm_dlci_data_output - try and push data out of a DLCI
1165 * @dlci: the DLCI to pull data from
1167 * Pull data from a DLCI and send it into the transmit queue if there
1168 * is data. Keep to the MRU of the mux. This path handles the usual tty
1169 * interface which is a byte stream with optional modem data.
1171 * Caller must hold the tx_lock of the mux.
1174 static int gsm_dlci_data_output(struct gsm_mux *gsm, struct gsm_dlci *dlci)
1176 struct gsm_msg *msg;
1180 /* for modem bits without break data */
1181 h = ((dlci->adaption == 1) ? 0 : 1);
1183 len = kfifo_len(&dlci->fifo);
1187 /* MTU/MRU count only the data bits but watch adaption mode */
1188 if ((len + h) > dlci->mtu)
1189 len = dlci->mtu - h;
1193 msg = gsm_data_alloc(gsm, dlci->addr, size, dlci->ftype);
1197 switch (dlci->adaption) {
1198 case 1: /* Unstructured */
1200 case 2: /* Unstructured with modem bits.
1201 * Always one byte as we never send inline break data
1203 *dp++ = (gsm_encode_modem(dlci) << 1) | EA;
1206 pr_err("%s: unsupported adaption %d\n", __func__,
1211 WARN_ON(len != kfifo_out_locked(&dlci->fifo, dp, len,
1214 /* Notify upper layer about available send space. */
1215 tty_port_tty_wakeup(&dlci->port);
1217 __gsm_data_queue(dlci, msg);
1218 /* Bytes of data we used up */
1223 * gsm_dlci_data_output_framed - try and push data out of a DLCI
1225 * @dlci: the DLCI to pull data from
1227 * Pull data from a DLCI and send it into the transmit queue if there
1228 * is data. Keep to the MRU of the mux. This path handles framed data
1229 * queued as skbuffs to the DLCI.
1231 * Caller must hold the tx_lock of the mux.
1234 static int gsm_dlci_data_output_framed(struct gsm_mux *gsm,
1235 struct gsm_dlci *dlci)
1237 struct gsm_msg *msg;
1240 int last = 0, first = 0;
1243 /* One byte per frame is used for B/F flags */
1244 if (dlci->adaption == 4)
1247 /* dlci->skb is locked by tx_lock */
1248 if (dlci->skb == NULL) {
1249 dlci->skb = skb_dequeue_tail(&dlci->skb_list);
1250 if (dlci->skb == NULL)
1254 len = dlci->skb->len + overhead;
1256 /* MTU/MRU count only the data bits */
1257 if (len > dlci->mtu) {
1258 if (dlci->adaption == 3) {
1259 /* Over long frame, bin it */
1260 dev_kfree_skb_any(dlci->skb);
1268 size = len + overhead;
1269 msg = gsm_data_alloc(gsm, dlci->addr, size, dlci->ftype);
1271 skb_queue_tail(&dlci->skb_list, dlci->skb);
1277 if (dlci->adaption == 4) { /* Interruptible framed (Packetised Data) */
1278 /* Flag byte to carry the start/end info */
1279 *dp++ = last << 7 | first << 6 | 1; /* EA */
1282 memcpy(dp, dlci->skb->data, len);
1283 skb_pull(dlci->skb, len);
1284 __gsm_data_queue(dlci, msg);
1286 dev_kfree_skb_any(dlci->skb);
1293 * gsm_dlci_modem_output - try and push modem status out of a DLCI
1295 * @dlci: the DLCI to pull modem status from
1296 * @brk: break signal
1298 * Push an empty frame in to the transmit queue to update the modem status
1299 * bits and to transmit an optional break.
1301 * Caller must hold the tx_lock of the mux.
1304 static int gsm_dlci_modem_output(struct gsm_mux *gsm, struct gsm_dlci *dlci,
1308 struct gsm_msg *msg;
1311 /* for modem bits without break data */
1312 switch (dlci->adaption) {
1313 case 1: /* Unstructured */
1315 case 2: /* Unstructured with modem bits. */
1321 pr_err("%s: unsupported adaption %d\n", __func__,
1326 msg = gsm_data_alloc(gsm, dlci->addr, size, dlci->ftype);
1328 pr_err("%s: gsm_data_alloc error", __func__);
1332 switch (dlci->adaption) {
1333 case 1: /* Unstructured */
1335 case 2: /* Unstructured with modem bits. */
1337 *dp++ = (gsm_encode_modem(dlci) << 1) | EA;
1339 *dp++ = gsm_encode_modem(dlci) << 1;
1340 *dp++ = (brk << 4) | 2 | EA; /* Length, Break, EA */
1348 __gsm_data_queue(dlci, msg);
1353 * gsm_dlci_data_sweep - look for data to send
1356 * Sweep the GSM mux channels in priority order looking for ones with
1357 * data to send. We could do with optimising this scan a bit. We aim
1358 * to fill the queue totally or up to TX_THRESH_HI bytes. Once we hit
1359 * TX_THRESH_LO we get called again
1361 * FIXME: We should round robin between groups and in theory you can
1362 * renegotiate DLCI priorities with optional stuff. Needs optimising.
1365 static int gsm_dlci_data_sweep(struct gsm_mux *gsm)
1367 /* Priority ordering: We should do priority with RR of the groups */
1368 int i, len, ret = 0;
1370 struct gsm_dlci *dlci;
1372 while (gsm->tx_bytes < TX_THRESH_HI) {
1373 for (sent = false, i = 1; i < NUM_DLCI; i++) {
1374 dlci = gsm->dlci[i];
1375 /* skip unused or blocked channel */
1376 if (!dlci || dlci->constipated)
1378 /* skip channels with invalid state */
1379 if (dlci->state != DLCI_OPEN)
1381 /* count the sent data per adaption */
1382 if (dlci->adaption < 3 && !dlci->net)
1383 len = gsm_dlci_data_output(gsm, dlci);
1385 len = gsm_dlci_data_output_framed(gsm, dlci);
1392 /* The lower DLCs can starve the higher DLCs! */
1405 * gsm_dlci_data_kick - transmit if possible
1406 * @dlci: DLCI to kick
1408 * Transmit data from this DLCI if the queue is empty. We can't rely on
1409 * a tty wakeup except when we filled the pipe so we need to fire off
1410 * new data ourselves in other cases.
1413 static void gsm_dlci_data_kick(struct gsm_dlci *dlci)
1415 unsigned long flags;
1418 if (dlci->constipated)
1421 spin_lock_irqsave(&dlci->gsm->tx_lock, flags);
1422 /* If we have nothing running then we need to fire up */
1423 sweep = (dlci->gsm->tx_bytes < TX_THRESH_LO);
1424 if (dlci->gsm->tx_bytes == 0) {
1426 gsm_dlci_data_output_framed(dlci->gsm, dlci);
1428 gsm_dlci_data_output(dlci->gsm, dlci);
1431 gsm_dlci_data_sweep(dlci->gsm);
1432 spin_unlock_irqrestore(&dlci->gsm->tx_lock, flags);
1436 * Control message processing
1441 * gsm_control_command - send a command frame to a control
1443 * @cmd: the command to use
1444 * @data: data to follow encoded info
1445 * @dlen: length of data
1447 * Encode up and queue a UI/UIH frame containing our command.
1449 static int gsm_control_command(struct gsm_mux *gsm, int cmd, const u8 *data,
1452 struct gsm_msg *msg;
1454 msg = gsm_data_alloc(gsm, 0, dlen + 2, gsm->dlci[0]->ftype);
1458 msg->data[0] = (cmd << 1) | CR | EA; /* Set C/R */
1459 msg->data[1] = (dlen << 1) | EA;
1460 memcpy(msg->data + 2, data, dlen);
1461 gsm_data_queue(gsm->dlci[0], msg);
1467 * gsm_control_reply - send a response frame to a control
1469 * @cmd: the command to use
1470 * @data: data to follow encoded info
1471 * @dlen: length of data
1473 * Encode up and queue a UI/UIH frame containing our response.
1476 static void gsm_control_reply(struct gsm_mux *gsm, int cmd, const u8 *data,
1479 struct gsm_msg *msg;
1481 msg = gsm_data_alloc(gsm, 0, dlen + 2, gsm->dlci[0]->ftype);
1484 msg->data[0] = (cmd & 0xFE) << 1 | EA; /* Clear C/R */
1485 msg->data[1] = (dlen << 1) | EA;
1486 memcpy(msg->data + 2, data, dlen);
1487 gsm_data_queue(gsm->dlci[0], msg);
1491 * gsm_process_modem - process received modem status
1492 * @tty: virtual tty bound to the DLCI
1493 * @dlci: DLCI to affect
1494 * @modem: modem bits (full EA)
1495 * @slen: number of signal octets
1497 * Used when a modem control message or line state inline in adaption
1498 * layer 2 is processed. Sort out the local modem state and throttles
1501 static void gsm_process_modem(struct tty_struct *tty, struct gsm_dlci *dlci,
1502 u32 modem, int slen)
1508 /* The modem status command can either contain one octet (V.24 signals)
1509 * or two octets (V.24 signals + break signals). This is specified in
1510 * section 5.4.6.3.7 of the 07.10 mux spec.
1514 modem = modem & 0x7f;
1517 modem = (modem >> 7) & 0x7f;
1520 /* Flow control/ready to communicate */
1521 fc = (modem & MDM_FC) || !(modem & MDM_RTR);
1522 if (fc && !dlci->constipated) {
1523 /* Need to throttle our output on this device */
1524 dlci->constipated = true;
1525 } else if (!fc && dlci->constipated) {
1526 dlci->constipated = false;
1527 gsm_dlci_data_kick(dlci);
1530 /* Map modem bits */
1531 if (modem & MDM_RTC)
1532 mlines |= TIOCM_DSR | TIOCM_DTR;
1533 if (modem & MDM_RTR)
1534 mlines |= TIOCM_RTS | TIOCM_CTS;
1540 /* Carrier drop -> hangup */
1542 if ((mlines & TIOCM_CD) == 0 && (dlci->modem_rx & TIOCM_CD))
1547 tty_insert_flip_char(&dlci->port, 0, TTY_BREAK);
1548 dlci->modem_rx = mlines;
1549 wake_up_interruptible(&dlci->gsm->event);
1553 * gsm_process_negotiation - process received parameters
1555 * @addr: DLCI address
1556 * @cr: command/response
1557 * @params: encoded parameters from the parameter negotiation message
1559 * Used when the response for our parameter negotiation command was
1562 static int gsm_process_negotiation(struct gsm_mux *gsm, unsigned int addr,
1564 const struct gsm_dlci_param_bits *params)
1566 struct gsm_dlci *dlci = gsm->dlci[addr];
1567 unsigned int ftype, i, adaption, prio, n1, k;
1569 i = FIELD_GET(PN_I_CL_FIELD_FTYPE, params->i_cl_bits);
1570 adaption = FIELD_GET(PN_I_CL_FIELD_ADAPTION, params->i_cl_bits) + 1;
1571 prio = FIELD_GET(PN_P_FIELD_PRIO, params->p_bits);
1572 n1 = FIELD_GET(PN_N_FIELD_N1, get_unaligned_le16(¶ms->n_bits));
1573 k = FIELD_GET(PN_K_FIELD_K, params->k_bits);
1576 if (debug & DBG_ERRORS)
1577 pr_info("%s N1 out of range in PN\n", __func__);
1588 case 0x02: /* I frames are not supported */
1589 if (debug & DBG_ERRORS)
1590 pr_info("%s unsupported I frame request in PN\n",
1594 if (debug & DBG_ERRORS)
1595 pr_info("%s i out of range in PN\n", __func__);
1599 if (!cr && gsm->initiator) {
1600 if (adaption != dlci->adaption) {
1601 if (debug & DBG_ERRORS)
1602 pr_info("%s invalid adaption %d in PN\n",
1603 __func__, adaption);
1606 if (prio != dlci->prio) {
1607 if (debug & DBG_ERRORS)
1608 pr_info("%s invalid priority %d in PN",
1612 if (n1 > gsm->mru || n1 > dlci->mtu) {
1613 /* We requested a frame size but the other party wants
1614 * to send larger frames. The standard allows only a
1615 * smaller response value than requested (5.4.6.3.1).
1617 if (debug & DBG_ERRORS)
1618 pr_info("%s invalid N1 %d in PN\n", __func__,
1623 if (ftype != dlci->ftype) {
1624 if (debug & DBG_ERRORS)
1625 pr_info("%s invalid i %d in PN\n", __func__, i);
1628 if (ftype != UI && ftype != UIH && k > dlci->k) {
1629 if (debug & DBG_ERRORS)
1630 pr_info("%s invalid k %d in PN\n", __func__, k);
1634 } else if (cr && !gsm->initiator) {
1635 /* Only convergence layer type 1 and 2 are supported. */
1636 if (adaption != 1 && adaption != 2) {
1637 if (debug & DBG_ERRORS)
1638 pr_info("%s invalid adaption %d in PN\n",
1639 __func__, adaption);
1642 dlci->adaption = adaption;
1643 if (n1 > gsm->mru) {
1644 /* Propose a smaller value */
1645 dlci->mtu = gsm->mru;
1646 } else if (n1 > MAX_MTU) {
1647 /* Propose a smaller value */
1648 dlci->mtu = MAX_MTU;
1653 dlci->ftype = ftype;
1663 * gsm_control_modem - modem status received
1665 * @data: data following command
1666 * @clen: command length
1668 * We have received a modem status control message. This is used by
1669 * the GSM mux protocol to pass virtual modem line status and optionally
1670 * to indicate break signals. Unpack it, convert to Linux representation
1671 * and if need be stuff a break message down the tty.
1674 static void gsm_control_modem(struct gsm_mux *gsm, const u8 *data, int clen)
1676 unsigned int addr = 0;
1677 unsigned int modem = 0;
1678 struct gsm_dlci *dlci;
1681 const u8 *dp = data;
1682 struct tty_struct *tty;
1684 len = gsm_read_ea_val(&addr, data, cl);
1689 /* Closed port, or invalid ? */
1690 if (addr == 0 || addr >= NUM_DLCI || gsm->dlci[addr] == NULL)
1692 dlci = gsm->dlci[addr];
1694 /* Must be at least one byte following the EA */
1701 /* get the modem status */
1702 len = gsm_read_ea_val(&modem, dp, cl);
1706 tty = tty_port_tty_get(&dlci->port);
1707 gsm_process_modem(tty, dlci, modem, cl);
1712 gsm_control_reply(gsm, CMD_MSC, data, clen);
1716 * gsm_control_negotiation - parameter negotiation received
1718 * @cr: command/response flag
1719 * @data: data following command
1720 * @dlen: data length
1722 * We have received a parameter negotiation message. This is used by
1723 * the GSM mux protocol to configure protocol parameters for a new DLCI.
1725 static void gsm_control_negotiation(struct gsm_mux *gsm, unsigned int cr,
1726 const u8 *data, unsigned int dlen)
1729 struct gsm_dlci_param_bits pn_reply;
1730 struct gsm_dlci *dlci;
1731 struct gsm_dlci_param_bits *params;
1733 if (dlen < sizeof(struct gsm_dlci_param_bits))
1737 params = (struct gsm_dlci_param_bits *)data;
1738 addr = FIELD_GET(PN_D_FIELD_DLCI, params->d_bits);
1739 if (addr == 0 || addr >= NUM_DLCI || !gsm->dlci[addr])
1741 dlci = gsm->dlci[addr];
1743 /* Too late for parameter negotiation? */
1744 if ((!cr && dlci->state == DLCI_OPENING) || dlci->state == DLCI_OPEN)
1747 /* Process the received parameters */
1748 if (gsm_process_negotiation(gsm, addr, cr, params) != 0) {
1749 /* Negotiation failed. Close the link. */
1750 if (debug & DBG_ERRORS)
1751 pr_info("%s PN failed\n", __func__);
1752 gsm_dlci_close(dlci);
1757 /* Reply command with accepted parameters. */
1758 if (gsm_encode_params(dlci, &pn_reply) == 0)
1759 gsm_control_reply(gsm, CMD_PN, (const u8 *)&pn_reply,
1761 else if (debug & DBG_ERRORS)
1762 pr_info("%s PN invalid\n", __func__);
1763 } else if (dlci->state == DLCI_CONFIGURE) {
1764 /* Proceed with link setup by sending SABM before UA */
1765 dlci->state = DLCI_OPENING;
1766 gsm_command(gsm, dlci->addr, SABM|PF);
1767 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
1769 if (debug & DBG_ERRORS)
1770 pr_info("%s PN in invalid state\n", __func__);
1775 * gsm_control_rls - remote line status
1778 * @clen: data length
1780 * The modem sends us a two byte message on the control channel whenever
1781 * it wishes to send us an error state from the virtual link. Stuff
1782 * this into the uplink tty if present
1785 static void gsm_control_rls(struct gsm_mux *gsm, const u8 *data, int clen)
1787 struct tty_port *port;
1788 unsigned int addr = 0;
1791 const u8 *dp = data;
1793 while (gsm_read_ea(&addr, *dp++) == 0) {
1798 /* Must be at least one byte following ea */
1803 /* Closed port, or invalid ? */
1804 if (addr == 0 || addr >= NUM_DLCI || gsm->dlci[addr] == NULL)
1808 if ((bits & 1) == 0)
1811 port = &gsm->dlci[addr]->port;
1814 tty_insert_flip_char(port, 0, TTY_OVERRUN);
1816 tty_insert_flip_char(port, 0, TTY_PARITY);
1818 tty_insert_flip_char(port, 0, TTY_FRAME);
1820 tty_flip_buffer_push(port);
1822 gsm_control_reply(gsm, CMD_RLS, data, clen);
1825 static void gsm_dlci_begin_close(struct gsm_dlci *dlci);
1828 * gsm_control_message - DLCI 0 control processing
1830 * @command: the command EA
1831 * @data: data beyond the command/length EAs
1834 * Input processor for control messages from the other end of the link.
1835 * Processes the incoming request and queues a response frame or an
1836 * NSC response if not supported
1839 static void gsm_control_message(struct gsm_mux *gsm, unsigned int command,
1840 const u8 *data, int clen)
1846 struct gsm_dlci *dlci = gsm->dlci[0];
1847 /* Modem wishes to close down */
1851 gsm_dlci_begin_close(dlci);
1856 /* Modem wishes to test, reply with the data */
1857 gsm_control_reply(gsm, CMD_TEST, data, clen);
1860 /* Modem can accept data again */
1861 gsm->constipated = false;
1862 gsm_control_reply(gsm, CMD_FCON, NULL, 0);
1863 /* Kick the link in case it is idling */
1864 gsmld_write_trigger(gsm);
1867 /* Modem wants us to STFU */
1868 gsm->constipated = true;
1869 gsm_control_reply(gsm, CMD_FCOFF, NULL, 0);
1872 /* Out of band modem line change indicator for a DLCI */
1873 gsm_control_modem(gsm, data, clen);
1876 /* Out of band error reception for a DLCI */
1877 gsm_control_rls(gsm, data, clen);
1880 /* Modem wishes to enter power saving state */
1881 gsm_control_reply(gsm, CMD_PSC, NULL, 0);
1883 /* Optional commands */
1885 /* Modem sends a parameter negotiation command */
1886 gsm_control_negotiation(gsm, 1, data, clen);
1888 /* Optional unsupported commands */
1889 case CMD_RPN: /* Remote port negotiation */
1890 case CMD_SNC: /* Service negotiation command */
1892 /* Reply to bad commands with an NSC */
1894 gsm_control_reply(gsm, CMD_NSC, buf, 1);
1900 * gsm_control_response - process a response to our control
1902 * @command: the command (response) EA
1903 * @data: data beyond the command/length EA
1906 * Process a response to an outstanding command. We only allow a single
1907 * control message in flight so this is fairly easy. All the clean up
1908 * is done by the caller, we just update the fields, flag it as done
1912 static void gsm_control_response(struct gsm_mux *gsm, unsigned int command,
1913 const u8 *data, int clen)
1915 struct gsm_control *ctrl;
1916 struct gsm_dlci *dlci;
1917 unsigned long flags;
1919 spin_lock_irqsave(&gsm->control_lock, flags);
1921 ctrl = gsm->pending_cmd;
1922 dlci = gsm->dlci[0];
1924 /* Does the reply match our command */
1925 if (ctrl != NULL && (command == ctrl->cmd || command == CMD_NSC)) {
1926 /* Our command was replied to, kill the retry timer */
1927 del_timer(&gsm->t2_timer);
1928 gsm->pending_cmd = NULL;
1929 /* Rejected by the other end */
1930 if (command == CMD_NSC)
1931 ctrl->error = -EOPNOTSUPP;
1933 wake_up(&gsm->event);
1934 /* Or did we receive the PN response to our PN command */
1935 } else if (command == CMD_PN) {
1936 gsm_control_negotiation(gsm, 0, data, clen);
1937 /* Or did we receive the TEST response to our TEST command */
1938 } else if (command == CMD_TEST && clen == 1 && *data == gsm->ka_num) {
1939 gsm->ka_retries = -1; /* trigger new keep-alive message */
1940 if (dlci && !dlci->dead)
1941 mod_timer(&gsm->ka_timer, jiffies + gsm->keep_alive * HZ / 100);
1943 spin_unlock_irqrestore(&gsm->control_lock, flags);
1947 * gsm_control_keep_alive - check timeout or start keep-alive
1948 * @t: timer contained in our gsm object
1950 * Called off the keep-alive timer expiry signaling that our link
1951 * partner is not responding anymore. Link will be closed.
1952 * This is also called to startup our timer.
1955 static void gsm_control_keep_alive(struct timer_list *t)
1957 struct gsm_mux *gsm = from_timer(gsm, t, ka_timer);
1958 unsigned long flags;
1960 spin_lock_irqsave(&gsm->control_lock, flags);
1961 if (gsm->ka_num && gsm->ka_retries == 0) {
1962 /* Keep-alive expired -> close the link */
1963 if (debug & DBG_ERRORS)
1964 pr_debug("%s keep-alive timed out\n", __func__);
1965 spin_unlock_irqrestore(&gsm->control_lock, flags);
1967 gsm_dlci_begin_close(gsm->dlci[0]);
1969 } else if (gsm->keep_alive && gsm->dlci[0] && !gsm->dlci[0]->dead) {
1970 if (gsm->ka_retries > 0) {
1971 /* T2 expired for keep-alive -> resend */
1974 /* Start keep-alive timer */
1978 gsm->ka_retries = (signed int)gsm->n2;
1980 gsm_control_command(gsm, CMD_TEST, &gsm->ka_num,
1981 sizeof(gsm->ka_num));
1982 mod_timer(&gsm->ka_timer,
1983 jiffies + gsm->t2 * HZ / 100);
1985 spin_unlock_irqrestore(&gsm->control_lock, flags);
1989 * gsm_control_transmit - send control packet
1991 * @ctrl: frame to send
1993 * Send out a pending control command (called under control lock)
1996 static void gsm_control_transmit(struct gsm_mux *gsm, struct gsm_control *ctrl)
1998 gsm_control_command(gsm, ctrl->cmd, ctrl->data, ctrl->len);
2002 * gsm_control_retransmit - retransmit a control frame
2003 * @t: timer contained in our gsm object
2005 * Called off the T2 timer expiry in order to retransmit control frames
2006 * that have been lost in the system somewhere. The control_lock protects
2007 * us from colliding with another sender or a receive completion event.
2008 * In that situation the timer may still occur in a small window but
2009 * gsm->pending_cmd will be NULL and we just let the timer expire.
2012 static void gsm_control_retransmit(struct timer_list *t)
2014 struct gsm_mux *gsm = from_timer(gsm, t, t2_timer);
2015 struct gsm_control *ctrl;
2016 unsigned long flags;
2017 spin_lock_irqsave(&gsm->control_lock, flags);
2018 ctrl = gsm->pending_cmd;
2020 if (gsm->cretries == 0 || !gsm->dlci[0] || gsm->dlci[0]->dead) {
2021 gsm->pending_cmd = NULL;
2022 ctrl->error = -ETIMEDOUT;
2024 spin_unlock_irqrestore(&gsm->control_lock, flags);
2025 wake_up(&gsm->event);
2029 gsm_control_transmit(gsm, ctrl);
2030 mod_timer(&gsm->t2_timer, jiffies + gsm->t2 * HZ / 100);
2032 spin_unlock_irqrestore(&gsm->control_lock, flags);
2036 * gsm_control_send - send a control frame on DLCI 0
2037 * @gsm: the GSM channel
2038 * @command: command to send including CR bit
2039 * @data: bytes of data (must be kmalloced)
2040 * @clen: length of the block to send
2042 * Queue and dispatch a control command. Only one command can be
2043 * active at a time. In theory more can be outstanding but the matching
2044 * gets really complicated so for now stick to one outstanding.
2047 static struct gsm_control *gsm_control_send(struct gsm_mux *gsm,
2048 unsigned int command, u8 *data, int clen)
2050 struct gsm_control *ctrl = kzalloc(sizeof(struct gsm_control),
2052 unsigned long flags;
2056 wait_event(gsm->event, gsm->pending_cmd == NULL);
2057 spin_lock_irqsave(&gsm->control_lock, flags);
2058 if (gsm->pending_cmd != NULL) {
2059 spin_unlock_irqrestore(&gsm->control_lock, flags);
2062 ctrl->cmd = command;
2065 gsm->pending_cmd = ctrl;
2067 /* If DLCI0 is in ADM mode skip retries, it won't respond */
2068 if (gsm->dlci[0]->mode == DLCI_MODE_ADM)
2071 gsm->cretries = gsm->n2;
2073 mod_timer(&gsm->t2_timer, jiffies + gsm->t2 * HZ / 100);
2074 gsm_control_transmit(gsm, ctrl);
2075 spin_unlock_irqrestore(&gsm->control_lock, flags);
2080 * gsm_control_wait - wait for a control to finish
2082 * @control: control we are waiting on
2084 * Waits for the control to complete or time out. Frees any used
2085 * resources and returns 0 for success, or an error if the remote
2086 * rejected or ignored the request.
2089 static int gsm_control_wait(struct gsm_mux *gsm, struct gsm_control *control)
2092 wait_event(gsm->event, control->done == 1);
2093 err = control->error;
2100 * DLCI level handling: Needs krefs
2104 * State transitions and timers
2108 * gsm_dlci_close - a DLCI has closed
2109 * @dlci: DLCI that closed
2111 * Perform processing when moving a DLCI into closed state. If there
2112 * is an attached tty this is hung up
2115 static void gsm_dlci_close(struct gsm_dlci *dlci)
2117 del_timer(&dlci->t1);
2118 if (debug & DBG_ERRORS)
2119 pr_debug("DLCI %d goes closed.\n", dlci->addr);
2120 dlci->state = DLCI_CLOSED;
2121 /* Prevent us from sending data before the link is up again */
2122 dlci->constipated = true;
2123 if (dlci->addr != 0) {
2124 tty_port_tty_hangup(&dlci->port, false);
2125 gsm_dlci_clear_queues(dlci->gsm, dlci);
2126 /* Ensure that gsmtty_open() can return. */
2127 tty_port_set_initialized(&dlci->port, false);
2128 wake_up_interruptible(&dlci->port.open_wait);
2130 del_timer(&dlci->gsm->ka_timer);
2131 dlci->gsm->dead = true;
2133 /* A DLCI 0 close is a MUX termination so we need to kick that
2134 back to userspace somehow */
2135 gsm_dlci_data_kick(dlci);
2136 wake_up_all(&dlci->gsm->event);
2140 * gsm_dlci_open - a DLCI has opened
2141 * @dlci: DLCI that opened
2143 * Perform processing when moving a DLCI into open state.
2146 static void gsm_dlci_open(struct gsm_dlci *dlci)
2148 struct gsm_mux *gsm = dlci->gsm;
2150 /* Note that SABM UA .. SABM UA first UA lost can mean that we go
2152 del_timer(&dlci->t1);
2153 /* This will let a tty open continue */
2154 dlci->state = DLCI_OPEN;
2155 dlci->constipated = false;
2156 if (debug & DBG_ERRORS)
2157 pr_debug("DLCI %d goes open.\n", dlci->addr);
2158 /* Send current modem state */
2160 gsm_modem_update(dlci, 0);
2162 /* Start keep-alive control */
2164 gsm->ka_retries = -1;
2165 mod_timer(&gsm->ka_timer,
2166 jiffies + gsm->keep_alive * HZ / 100);
2168 gsm_dlci_data_kick(dlci);
2169 wake_up(&dlci->gsm->event);
2173 * gsm_dlci_negotiate - start parameter negotiation
2174 * @dlci: DLCI to open
2176 * Starts the parameter negotiation for the new DLCI. This needs to be done
2177 * before the DLCI initialized the channel via SABM.
2179 static int gsm_dlci_negotiate(struct gsm_dlci *dlci)
2181 struct gsm_mux *gsm = dlci->gsm;
2182 struct gsm_dlci_param_bits params;
2185 ret = gsm_encode_params(dlci, ¶ms);
2189 /* We cannot asynchronous wait for the command response with
2190 * gsm_command() and gsm_control_wait() at this point.
2192 ret = gsm_control_command(gsm, CMD_PN, (const u8 *)¶ms,
2199 * gsm_dlci_t1 - T1 timer expiry
2200 * @t: timer contained in the DLCI that opened
2202 * The T1 timer handles retransmits of control frames (essentially of
2203 * SABM and DISC). We resend the command until the retry count runs out
2204 * in which case an opening port goes back to closed and a closing port
2205 * is simply put into closed state (any further frames from the other
2206 * end will get a DM response)
2208 * Some control dlci can stay in ADM mode with other dlci working just
2209 * fine. In that case we can just keep the control dlci open after the
2210 * DLCI_OPENING retries time out.
2213 static void gsm_dlci_t1(struct timer_list *t)
2215 struct gsm_dlci *dlci = from_timer(dlci, t, t1);
2216 struct gsm_mux *gsm = dlci->gsm;
2218 switch (dlci->state) {
2219 case DLCI_CONFIGURE:
2220 if (dlci->retries && gsm_dlci_negotiate(dlci) == 0) {
2222 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
2224 gsm_dlci_begin_close(dlci); /* prevent half open link */
2228 if (dlci->retries) {
2230 gsm_command(dlci->gsm, dlci->addr, SABM|PF);
2231 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
2232 } else if (!dlci->addr && gsm->control == (DM | PF)) {
2233 if (debug & DBG_ERRORS)
2234 pr_info("DLCI %d opening in ADM mode.\n",
2236 dlci->mode = DLCI_MODE_ADM;
2237 gsm_dlci_open(dlci);
2239 gsm_dlci_begin_close(dlci); /* prevent half open link */
2244 if (dlci->retries) {
2246 gsm_command(dlci->gsm, dlci->addr, DISC|PF);
2247 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
2249 gsm_dlci_close(dlci);
2252 pr_debug("%s: unhandled state: %d\n", __func__, dlci->state);
2258 * gsm_dlci_begin_open - start channel open procedure
2259 * @dlci: DLCI to open
2261 * Commence opening a DLCI from the Linux side. We issue SABM messages
2262 * to the modem which should then reply with a UA or ADM, at which point
2263 * we will move into open state. Opening is done asynchronously with retry
2264 * running off timers and the responses.
2265 * Parameter negotiation is performed before SABM if required.
2268 static void gsm_dlci_begin_open(struct gsm_dlci *dlci)
2270 struct gsm_mux *gsm = dlci ? dlci->gsm : NULL;
2271 bool need_pn = false;
2276 if (dlci->addr != 0) {
2277 if (gsm->adaption != 1 || gsm->adaption != dlci->adaption)
2279 if (dlci->prio != (roundup(dlci->addr + 1, 8) - 1))
2281 if (gsm->ftype != dlci->ftype)
2285 switch (dlci->state) {
2287 case DLCI_WAITING_CONFIG:
2289 dlci->retries = gsm->n2;
2291 dlci->state = DLCI_OPENING;
2292 gsm_command(gsm, dlci->addr, SABM|PF);
2294 /* Configure DLCI before setup */
2295 dlci->state = DLCI_CONFIGURE;
2296 if (gsm_dlci_negotiate(dlci) != 0) {
2297 gsm_dlci_close(dlci);
2301 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
2309 * gsm_dlci_set_opening - change state to opening
2310 * @dlci: DLCI to open
2312 * Change internal state to wait for DLCI open from initiator side.
2313 * We set off timers and responses upon reception of an SABM.
2315 static void gsm_dlci_set_opening(struct gsm_dlci *dlci)
2317 switch (dlci->state) {
2319 case DLCI_WAITING_CONFIG:
2321 dlci->state = DLCI_OPENING;
2329 * gsm_dlci_set_wait_config - wait for channel configuration
2330 * @dlci: DLCI to configure
2332 * Wait for a DLCI configuration from the application.
2334 static void gsm_dlci_set_wait_config(struct gsm_dlci *dlci)
2336 switch (dlci->state) {
2339 dlci->state = DLCI_WAITING_CONFIG;
2347 * gsm_dlci_begin_close - start channel open procedure
2348 * @dlci: DLCI to open
2350 * Commence closing a DLCI from the Linux side. We issue DISC messages
2351 * to the modem which should then reply with a UA, at which point we
2352 * will move into closed state. Closing is done asynchronously with retry
2353 * off timers. We may also receive a DM reply from the other end which
2354 * indicates the channel was already closed.
2357 static void gsm_dlci_begin_close(struct gsm_dlci *dlci)
2359 struct gsm_mux *gsm = dlci->gsm;
2360 if (dlci->state == DLCI_CLOSED || dlci->state == DLCI_CLOSING)
2362 dlci->retries = gsm->n2;
2363 dlci->state = DLCI_CLOSING;
2364 gsm_command(dlci->gsm, dlci->addr, DISC|PF);
2365 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
2366 wake_up_interruptible(&gsm->event);
2370 * gsm_dlci_data - data arrived
2372 * @data: block of bytes received
2373 * @clen: length of received block
2375 * A UI or UIH frame has arrived which contains data for a channel
2376 * other than the control channel. If the relevant virtual tty is
2377 * open we shovel the bits down it, if not we drop them.
2380 static void gsm_dlci_data(struct gsm_dlci *dlci, const u8 *data, int clen)
2383 struct tty_port *port = &dlci->port;
2384 struct tty_struct *tty;
2385 unsigned int modem = 0;
2388 if (debug & DBG_TTY)
2389 pr_debug("%d bytes for tty\n", clen);
2390 switch (dlci->adaption) {
2391 /* Unsupported types */
2392 case 4: /* Packetised interruptible data */
2394 case 3: /* Packetised uininterruptible voice/data */
2396 case 2: /* Asynchronous serial with line state in each frame */
2397 len = gsm_read_ea_val(&modem, data, clen);
2400 tty = tty_port_tty_get(port);
2402 gsm_process_modem(tty, dlci, modem, len);
2406 /* Skip processed modem data */
2410 case 1: /* Line state will go via DLCI 0 controls only */
2412 tty_insert_flip_string(port, data, clen);
2413 tty_flip_buffer_push(port);
2418 * gsm_dlci_command - data arrived on control channel
2420 * @data: block of bytes received
2421 * @len: length of received block
2423 * A UI or UIH frame has arrived which contains data for DLCI 0 the
2424 * control channel. This should contain a command EA followed by
2425 * control data bytes. The command EA contains a command/response bit
2426 * and we divide up the work accordingly.
2429 static void gsm_dlci_command(struct gsm_dlci *dlci, const u8 *data, int len)
2431 /* See what command is involved */
2432 unsigned int command = 0;
2433 unsigned int clen = 0;
2436 /* read the command */
2437 dlen = gsm_read_ea_val(&command, data, len);
2441 /* read any control data */
2442 dlen = gsm_read_ea_val(&clen, data, len);
2446 /* Malformed command? */
2451 gsm_control_message(dlci->gsm, command, data, clen);
2453 gsm_control_response(dlci->gsm, command, data, clen);
2457 * gsm_kick_timer - transmit if possible
2458 * @t: timer contained in our gsm object
2460 * Transmit data from DLCIs if the queue is empty. We can't rely on
2461 * a tty wakeup except when we filled the pipe so we need to fire off
2462 * new data ourselves in other cases.
2464 static void gsm_kick_timer(struct timer_list *t)
2466 struct gsm_mux *gsm = from_timer(gsm, t, kick_timer);
2467 unsigned long flags;
2470 spin_lock_irqsave(&gsm->tx_lock, flags);
2471 /* If we have nothing running then we need to fire up */
2472 if (gsm->tx_bytes < TX_THRESH_LO)
2473 sent = gsm_dlci_data_sweep(gsm);
2474 spin_unlock_irqrestore(&gsm->tx_lock, flags);
2476 if (sent && debug & DBG_DATA)
2477 pr_info("%s TX queue stalled\n", __func__);
2481 * gsm_dlci_copy_config_values - copy DLCI configuration
2482 * @dlci: source DLCI
2483 * @dc: configuration structure to fill
2485 static void gsm_dlci_copy_config_values(struct gsm_dlci *dlci, struct gsm_dlci_config *dc)
2487 memset(dc, 0, sizeof(*dc));
2488 dc->channel = (u32)dlci->addr;
2489 dc->adaption = (u32)dlci->adaption;
2490 dc->mtu = (u32)dlci->mtu;
2491 dc->priority = (u32)dlci->prio;
2492 if (dlci->ftype == UIH)
2496 dc->k = (u32)dlci->k;
2500 * gsm_dlci_config - configure DLCI from configuration
2501 * @dlci: DLCI to configure
2502 * @dc: DLCI configuration
2503 * @open: open DLCI after configuration?
2505 static int gsm_dlci_config(struct gsm_dlci *dlci, struct gsm_dlci_config *dc, int open)
2507 struct gsm_mux *gsm;
2508 bool need_restart = false;
2509 bool need_open = false;
2513 * Check that userspace doesn't put stuff in here to prevent breakages
2516 for (i = 0; i < ARRAY_SIZE(dc->reserved); i++)
2517 if (dc->reserved[i])
2524 /* Stuff we don't support yet - I frame transport */
2525 if (dc->adaption != 1 && dc->adaption != 2)
2527 if (dc->mtu > MAX_MTU || dc->mtu < MIN_MTU || dc->mtu > gsm->mru)
2529 if (dc->priority >= 64)
2531 if (dc->i == 0 || dc->i > 2) /* UIH and UI only */
2537 * See what is needed for reconfiguration
2539 /* Framing fields */
2540 if (dc->adaption != dlci->adaption)
2541 need_restart = true;
2542 if (dc->mtu != dlci->mtu)
2543 need_restart = true;
2544 if (dc->i != dlci->ftype)
2545 need_restart = true;
2547 if (dc->priority != dlci->prio)
2548 need_restart = true;
2550 if ((open && gsm->wait_config) || need_restart)
2552 if (dlci->state == DLCI_WAITING_CONFIG) {
2553 need_restart = false;
2558 * Close down what is needed, restart and initiate the new
2562 gsm_dlci_begin_close(dlci);
2563 wait_event_interruptible(gsm->event, dlci->state == DLCI_CLOSED);
2564 if (signal_pending(current))
2568 * Setup the new configuration values
2570 dlci->adaption = (int)dc->adaption;
2573 dlci->mtu = (unsigned int)dc->mtu;
2575 dlci->mtu = gsm->mtu;
2578 dlci->prio = (u8)dc->priority;
2580 dlci->prio = roundup(dlci->addr + 1, 8) - 1;
2584 else if (dc->i == 2)
2588 dlci->k = (u8)dc->k;
2594 gsm_dlci_begin_open(dlci);
2596 gsm_dlci_set_opening(dlci);
2603 * Allocate/Free DLCI channels
2607 * gsm_dlci_alloc - allocate a DLCI
2609 * @addr: address of the DLCI
2611 * Allocate and install a new DLCI object into the GSM mux.
2613 * FIXME: review locking races
2616 static struct gsm_dlci *gsm_dlci_alloc(struct gsm_mux *gsm, int addr)
2618 struct gsm_dlci *dlci = kzalloc(sizeof(struct gsm_dlci), GFP_ATOMIC);
2621 spin_lock_init(&dlci->lock);
2622 mutex_init(&dlci->mutex);
2623 if (kfifo_alloc(&dlci->fifo, TX_SIZE, GFP_KERNEL) < 0) {
2628 skb_queue_head_init(&dlci->skb_list);
2629 timer_setup(&dlci->t1, gsm_dlci_t1, 0);
2630 tty_port_init(&dlci->port);
2631 dlci->port.ops = &gsm_port_ops;
2634 dlci->adaption = gsm->adaption;
2635 dlci->mtu = gsm->mtu;
2639 dlci->prio = roundup(addr + 1, 8) - 1;
2640 dlci->ftype = gsm->ftype;
2642 dlci->state = DLCI_CLOSED;
2644 dlci->data = gsm_dlci_data;
2645 /* Prevent us from sending data before the link is up */
2646 dlci->constipated = true;
2648 dlci->data = gsm_dlci_command;
2650 gsm->dlci[addr] = dlci;
2655 * gsm_dlci_free - free DLCI
2656 * @port: tty port for DLCI to free
2662 static void gsm_dlci_free(struct tty_port *port)
2664 struct gsm_dlci *dlci = container_of(port, struct gsm_dlci, port);
2666 timer_shutdown_sync(&dlci->t1);
2667 dlci->gsm->dlci[dlci->addr] = NULL;
2668 kfifo_free(&dlci->fifo);
2669 while ((dlci->skb = skb_dequeue(&dlci->skb_list)))
2670 dev_kfree_skb(dlci->skb);
2674 static inline void dlci_get(struct gsm_dlci *dlci)
2676 tty_port_get(&dlci->port);
2679 static inline void dlci_put(struct gsm_dlci *dlci)
2681 tty_port_put(&dlci->port);
2684 static void gsm_destroy_network(struct gsm_dlci *dlci);
2687 * gsm_dlci_release - release DLCI
2688 * @dlci: DLCI to destroy
2690 * Release a DLCI. Actual free is deferred until either
2691 * mux is closed or tty is closed - whichever is last.
2695 static void gsm_dlci_release(struct gsm_dlci *dlci)
2697 struct tty_struct *tty = tty_port_tty_get(&dlci->port);
2699 mutex_lock(&dlci->mutex);
2700 gsm_destroy_network(dlci);
2701 mutex_unlock(&dlci->mutex);
2703 /* We cannot use tty_hangup() because in tty_kref_put() the tty
2704 * driver assumes that the hangup queue is free and reuses it to
2705 * queue release_one_tty() -> NULL pointer panic in
2706 * process_one_work().
2710 tty_port_tty_set(&dlci->port, NULL);
2713 dlci->state = DLCI_CLOSED;
2718 * LAPBish link layer logic
2722 * gsm_queue - a GSM frame is ready to process
2723 * @gsm: pointer to our gsm mux
2725 * At this point in time a frame has arrived and been demangled from
2726 * the line encoding. All the differences between the encodings have
2727 * been handled below us and the frame is unpacked into the structures.
2728 * The fcs holds the header FCS but any data FCS must be added here.
2731 static void gsm_queue(struct gsm_mux *gsm)
2733 struct gsm_dlci *dlci;
2737 if (gsm->fcs != GOOD_FCS) {
2739 if (debug & DBG_DATA)
2740 pr_debug("BAD FCS %02x\n", gsm->fcs);
2743 address = gsm->address >> 1;
2744 if (address >= NUM_DLCI)
2747 cr = gsm->address & 1; /* C/R bit */
2748 cr ^= gsm->initiator ? 0 : 1; /* Flip so 1 always means command */
2750 gsm_print_packet("<--", address, cr, gsm->control, gsm->buf, gsm->len);
2752 dlci = gsm->dlci[address];
2754 switch (gsm->control) {
2759 dlci = gsm_dlci_alloc(gsm, address);
2763 gsm_response(gsm, address, DM|PF);
2765 gsm_response(gsm, address, UA|PF);
2766 gsm_dlci_open(dlci);
2772 if (dlci == NULL || dlci->state == DLCI_CLOSED) {
2773 gsm_response(gsm, address, DM|PF);
2776 /* Real close complete */
2777 gsm_response(gsm, address, UA|PF);
2778 gsm_dlci_close(dlci);
2781 if (cr == 0 || dlci == NULL)
2783 switch (dlci->state) {
2785 gsm_dlci_close(dlci);
2788 gsm_dlci_open(dlci);
2791 pr_debug("%s: unhandled state: %d\n", __func__,
2796 case DM: /* DM can be valid unsolicited */
2802 gsm_dlci_close(dlci);
2808 if (dlci == NULL || dlci->state != DLCI_OPEN) {
2809 gsm_response(gsm, address, DM|PF);
2812 dlci->data(dlci, gsm->buf, gsm->len);
2825 * gsm0_receive - perform processing for non-transparency
2826 * @gsm: gsm data for this ldisc instance
2829 * Receive bytes in gsm mode 0
2832 static void gsm0_receive(struct gsm_mux *gsm, unsigned char c)
2836 switch (gsm->state) {
2837 case GSM_SEARCH: /* SOF marker */
2838 if (c == GSM0_SOF) {
2839 gsm->state = GSM_ADDRESS;
2842 gsm->fcs = INIT_FCS;
2845 case GSM_ADDRESS: /* Address EA */
2846 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
2847 if (gsm_read_ea(&gsm->address, c))
2848 gsm->state = GSM_CONTROL;
2850 case GSM_CONTROL: /* Control Byte */
2851 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
2853 gsm->state = GSM_LEN0;
2855 case GSM_LEN0: /* Length EA */
2856 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
2857 if (gsm_read_ea(&gsm->len, c)) {
2858 if (gsm->len > gsm->mru) {
2860 gsm->state = GSM_SEARCH;
2865 gsm->state = GSM_FCS;
2867 gsm->state = GSM_DATA;
2870 gsm->state = GSM_LEN1;
2873 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
2875 gsm->len |= len << 7;
2876 if (gsm->len > gsm->mru) {
2878 gsm->state = GSM_SEARCH;
2883 gsm->state = GSM_FCS;
2885 gsm->state = GSM_DATA;
2887 case GSM_DATA: /* Data */
2888 gsm->buf[gsm->count++] = c;
2889 if (gsm->count == gsm->len) {
2890 /* Calculate final FCS for UI frames over all data */
2891 if ((gsm->control & ~PF) != UIH) {
2892 gsm->fcs = gsm_fcs_add_block(gsm->fcs, gsm->buf,
2895 gsm->state = GSM_FCS;
2898 case GSM_FCS: /* FCS follows the packet */
2899 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
2900 gsm->state = GSM_SSOF;
2903 gsm->state = GSM_SEARCH;
2910 pr_debug("%s: unhandled state: %d\n", __func__, gsm->state);
2916 * gsm1_receive - perform processing for non-transparency
2917 * @gsm: gsm data for this ldisc instance
2920 * Receive bytes in mode 1 (Advanced option)
2923 static void gsm1_receive(struct gsm_mux *gsm, unsigned char c)
2925 /* handle XON/XOFF */
2926 if ((c & ISO_IEC_646_MASK) == XON) {
2927 gsm->constipated = true;
2929 } else if ((c & ISO_IEC_646_MASK) == XOFF) {
2930 gsm->constipated = false;
2931 /* Kick the link in case it is idling */
2932 gsmld_write_trigger(gsm);
2935 if (c == GSM1_SOF) {
2936 /* EOF is only valid in frame if we have got to the data state */
2937 if (gsm->state == GSM_DATA) {
2938 if (gsm->count < 1) {
2941 gsm->state = GSM_START;
2944 /* Remove the FCS from data */
2946 if ((gsm->control & ~PF) != UIH) {
2947 /* Calculate final FCS for UI frames over all
2950 gsm->fcs = gsm_fcs_add_block(gsm->fcs, gsm->buf,
2953 /* Add the FCS itself to test against GOOD_FCS */
2954 gsm->fcs = gsm_fcs_add(gsm->fcs, gsm->buf[gsm->count]);
2955 gsm->len = gsm->count;
2957 gsm->state = GSM_START;
2960 /* Any partial frame was a runt so go back to start */
2961 if (gsm->state != GSM_START) {
2962 if (gsm->state != GSM_SEARCH)
2964 gsm->state = GSM_START;
2966 /* A SOF in GSM_START means we are still reading idling or
2971 if (c == GSM1_ESCAPE) {
2976 /* Only an unescaped SOF gets us out of GSM search */
2977 if (gsm->state == GSM_SEARCH)
2981 c ^= GSM1_ESCAPE_BITS;
2982 gsm->escape = false;
2984 switch (gsm->state) {
2985 case GSM_START: /* First byte after SOF */
2987 gsm->state = GSM_ADDRESS;
2988 gsm->fcs = INIT_FCS;
2990 case GSM_ADDRESS: /* Address continuation */
2991 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
2992 if (gsm_read_ea(&gsm->address, c))
2993 gsm->state = GSM_CONTROL;
2995 case GSM_CONTROL: /* Control Byte */
2996 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
2999 gsm->state = GSM_DATA;
3001 case GSM_DATA: /* Data */
3002 if (gsm->count > gsm->mru) { /* Allow one for the FCS */
3003 gsm->state = GSM_OVERRUN;
3006 gsm->buf[gsm->count++] = c;
3008 case GSM_OVERRUN: /* Over-long - eg a dropped SOF */
3011 pr_debug("%s: unhandled state: %d\n", __func__, gsm->state);
3017 * gsm_error - handle tty error
3020 * Handle an error in the receipt of data for a frame. Currently we just
3021 * go back to hunting for a SOF.
3023 * FIXME: better diagnostics ?
3026 static void gsm_error(struct gsm_mux *gsm)
3028 gsm->state = GSM_SEARCH;
3033 * gsm_cleanup_mux - generic GSM protocol cleanup
3035 * @disc: disconnect link?
3037 * Clean up the bits of the mux which are the same for all framing
3038 * protocols. Remove the mux from the mux table, stop all the timers
3039 * and then shut down each device hanging up the channels as we go.
3042 static void gsm_cleanup_mux(struct gsm_mux *gsm, bool disc)
3045 struct gsm_dlci *dlci = gsm->dlci[0];
3046 struct gsm_msg *txq, *ntxq;
3049 mutex_lock(&gsm->mutex);
3052 if (disc && dlci->state != DLCI_CLOSED) {
3053 gsm_dlci_begin_close(dlci);
3054 wait_event(gsm->event, dlci->state == DLCI_CLOSED);
3059 /* Finish outstanding timers, making sure they are done */
3060 del_timer_sync(&gsm->kick_timer);
3061 del_timer_sync(&gsm->t2_timer);
3062 del_timer_sync(&gsm->ka_timer);
3064 /* Finish writing to ldisc */
3065 flush_work(&gsm->tx_work);
3067 /* Free up any link layer users and finally the control channel */
3068 if (gsm->has_devices) {
3069 gsm_unregister_devices(gsm_tty_driver, gsm->num);
3070 gsm->has_devices = false;
3072 for (i = NUM_DLCI - 1; i >= 0; i--)
3074 gsm_dlci_release(gsm->dlci[i]);
3075 gsm->dlci[i] = NULL;
3077 mutex_unlock(&gsm->mutex);
3078 /* Now wipe the queues */
3079 tty_ldisc_flush(gsm->tty);
3080 list_for_each_entry_safe(txq, ntxq, &gsm->tx_ctrl_list, list)
3082 INIT_LIST_HEAD(&gsm->tx_ctrl_list);
3083 list_for_each_entry_safe(txq, ntxq, &gsm->tx_data_list, list)
3085 INIT_LIST_HEAD(&gsm->tx_data_list);
3089 * gsm_activate_mux - generic GSM setup
3092 * Set up the bits of the mux which are the same for all framing
3093 * protocols. Add the mux to the mux table so it can be opened and
3094 * finally kick off connecting to DLCI 0 on the modem.
3097 static int gsm_activate_mux(struct gsm_mux *gsm)
3099 struct gsm_dlci *dlci;
3102 dlci = gsm_dlci_alloc(gsm, 0);
3106 if (gsm->encoding == GSM_BASIC_OPT)
3107 gsm->receive = gsm0_receive;
3109 gsm->receive = gsm1_receive;
3111 ret = gsm_register_devices(gsm_tty_driver, gsm->num);
3115 gsm->has_devices = true;
3116 gsm->dead = false; /* Tty opens are now permissible */
3121 * gsm_free_mux - free up a mux
3124 * Dispose of allocated resources for a dead mux
3126 static void gsm_free_mux(struct gsm_mux *gsm)
3130 for (i = 0; i < MAX_MUX; i++) {
3131 if (gsm == gsm_mux[i]) {
3136 mutex_destroy(&gsm->mutex);
3137 kfree(gsm->txframe);
3143 * gsm_free_muxr - free up a mux
3144 * @ref: kreference to the mux to free
3146 * Dispose of allocated resources for a dead mux
3148 static void gsm_free_muxr(struct kref *ref)
3150 struct gsm_mux *gsm = container_of(ref, struct gsm_mux, ref);
3154 static inline void mux_get(struct gsm_mux *gsm)
3156 unsigned long flags;
3158 spin_lock_irqsave(&gsm_mux_lock, flags);
3159 kref_get(&gsm->ref);
3160 spin_unlock_irqrestore(&gsm_mux_lock, flags);
3163 static inline void mux_put(struct gsm_mux *gsm)
3165 unsigned long flags;
3167 spin_lock_irqsave(&gsm_mux_lock, flags);
3168 kref_put(&gsm->ref, gsm_free_muxr);
3169 spin_unlock_irqrestore(&gsm_mux_lock, flags);
3172 static inline unsigned int mux_num_to_base(struct gsm_mux *gsm)
3174 return gsm->num * NUM_DLCI;
3177 static inline unsigned int mux_line_to_num(unsigned int line)
3179 return line / NUM_DLCI;
3183 * gsm_alloc_mux - allocate a mux
3185 * Creates a new mux ready for activation.
3188 static struct gsm_mux *gsm_alloc_mux(void)
3191 struct gsm_mux *gsm = kzalloc(sizeof(struct gsm_mux), GFP_KERNEL);
3194 gsm->buf = kmalloc(MAX_MRU + 1, GFP_KERNEL);
3195 if (gsm->buf == NULL) {
3199 gsm->txframe = kmalloc(2 * (MAX_MTU + PROT_OVERHEAD - 1), GFP_KERNEL);
3200 if (gsm->txframe == NULL) {
3205 spin_lock_init(&gsm->lock);
3206 mutex_init(&gsm->mutex);
3207 kref_init(&gsm->ref);
3208 INIT_LIST_HEAD(&gsm->tx_ctrl_list);
3209 INIT_LIST_HEAD(&gsm->tx_data_list);
3210 timer_setup(&gsm->kick_timer, gsm_kick_timer, 0);
3211 timer_setup(&gsm->t2_timer, gsm_control_retransmit, 0);
3212 timer_setup(&gsm->ka_timer, gsm_control_keep_alive, 0);
3213 INIT_WORK(&gsm->tx_work, gsmld_write_task);
3214 init_waitqueue_head(&gsm->event);
3215 spin_lock_init(&gsm->control_lock);
3216 spin_lock_init(&gsm->tx_lock);
3225 gsm->encoding = GSM_ADV_OPT;
3226 gsm->mru = 64; /* Default to encoding 1 so these should be 64 */
3228 gsm->dead = true; /* Avoid early tty opens */
3229 gsm->wait_config = false; /* Disabled */
3230 gsm->keep_alive = 0; /* Disabled */
3232 /* Store the instance to the mux array or abort if no space is
3235 spin_lock(&gsm_mux_lock);
3236 for (i = 0; i < MAX_MUX; i++) {
3243 spin_unlock(&gsm_mux_lock);
3245 mutex_destroy(&gsm->mutex);
3246 kfree(gsm->txframe);
3255 static void gsm_copy_config_values(struct gsm_mux *gsm,
3256 struct gsm_config *c)
3258 memset(c, 0, sizeof(*c));
3259 c->adaption = gsm->adaption;
3260 c->encapsulation = gsm->encoding;
3261 c->initiator = gsm->initiator;
3266 if (gsm->ftype == UIH)
3270 pr_debug("Ftype %d i %d\n", gsm->ftype, c->i);
3276 static int gsm_config(struct gsm_mux *gsm, struct gsm_config *c)
3280 int need_restart = 0;
3282 /* Stuff we don't support yet - UI or I frame transport */
3283 if (c->adaption != 1 && c->adaption != 2)
3285 /* Check the MRU/MTU range looks sane */
3286 if (c->mru < MIN_MTU || c->mtu < MIN_MTU)
3288 if (c->mru > MAX_MRU || c->mtu > MAX_MTU)
3294 if (c->encapsulation > 1) /* Basic, advanced, no I */
3296 if (c->initiator > 1)
3298 if (c->k > MAX_WINDOW_SIZE)
3300 if (c->i == 0 || c->i > 2) /* UIH and UI only */
3303 * See what is needed for reconfiguration
3307 if (c->t1 != 0 && c->t1 != gsm->t1)
3309 if (c->t2 != 0 && c->t2 != gsm->t2)
3311 if (c->encapsulation != gsm->encoding)
3313 if (c->adaption != gsm->adaption)
3316 if (c->initiator != gsm->initiator)
3318 if (c->mru != gsm->mru)
3320 if (c->mtu != gsm->mtu)
3324 * Close down what is needed, restart and initiate the new
3325 * configuration. On the first time there is no DLCI[0]
3326 * and closing or cleaning up is not necessary.
3328 if (need_close || need_restart)
3329 gsm_cleanup_mux(gsm, true);
3331 gsm->initiator = c->initiator;
3334 gsm->encoding = c->encapsulation ? GSM_ADV_OPT : GSM_BASIC_OPT;
3335 gsm->adaption = c->adaption;
3353 * FIXME: We need to separate activation/deactivation from adding
3354 * and removing from the mux array
3357 ret = gsm_activate_mux(gsm);
3361 gsm_dlci_begin_open(gsm->dlci[0]);
3366 static void gsm_copy_config_ext_values(struct gsm_mux *gsm,
3367 struct gsm_config_ext *ce)
3369 memset(ce, 0, sizeof(*ce));
3370 ce->wait_config = gsm->wait_config ? 1 : 0;
3371 ce->keep_alive = gsm->keep_alive;
3374 static int gsm_config_ext(struct gsm_mux *gsm, struct gsm_config_ext *ce)
3379 * Check that userspace doesn't put stuff in here to prevent breakages
3382 for (i = 0; i < ARRAY_SIZE(ce->reserved); i++)
3383 if (ce->reserved[i])
3387 * Setup the new configuration values
3389 gsm->wait_config = ce->wait_config ? true : false;
3390 gsm->keep_alive = ce->keep_alive;
3396 * gsmld_output - write to link
3398 * @data: bytes to output
3401 * Write a block of data from the GSM mux to the data channel. This
3402 * will eventually be serialized from above but at the moment isn't.
3405 static int gsmld_output(struct gsm_mux *gsm, u8 *data, int len)
3407 if (tty_write_room(gsm->tty) < len) {
3408 set_bit(TTY_DO_WRITE_WAKEUP, &gsm->tty->flags);
3411 if (debug & DBG_DATA)
3412 gsm_hex_dump_bytes(__func__, data, len);
3413 return gsm->tty->ops->write(gsm->tty, data, len);
3418 * gsmld_write_trigger - schedule ldisc write task
3421 static void gsmld_write_trigger(struct gsm_mux *gsm)
3423 if (!gsm || !gsm->dlci[0] || gsm->dlci[0]->dead)
3425 schedule_work(&gsm->tx_work);
3430 * gsmld_write_task - ldisc write task
3431 * @work: our tx write work
3433 * Writes out data to the ldisc if possible. We are doing this here to
3434 * avoid dead-locking. This returns if no space or data is left for output.
3436 static void gsmld_write_task(struct work_struct *work)
3438 struct gsm_mux *gsm = container_of(work, struct gsm_mux, tx_work);
3439 unsigned long flags;
3442 /* All outstanding control channel and control messages and one data
3446 spin_lock_irqsave(&gsm->tx_lock, flags);
3448 ret = gsm_data_kick(gsm);
3449 spin_unlock_irqrestore(&gsm->tx_lock, flags);
3452 for (i = 0; i < NUM_DLCI; i++)
3454 tty_port_tty_wakeup(&gsm->dlci[i]->port);
3458 * gsmld_attach_gsm - mode set up
3459 * @tty: our tty structure
3462 * Set up the MUX for basic mode and commence connecting to the
3463 * modem. Currently called from the line discipline set up but
3464 * will need moving to an ioctl path.
3467 static void gsmld_attach_gsm(struct tty_struct *tty, struct gsm_mux *gsm)
3469 gsm->tty = tty_kref_get(tty);
3470 /* Turn off tty XON/XOFF handling to handle it explicitly. */
3471 gsm->old_c_iflag = tty->termios.c_iflag;
3472 tty->termios.c_iflag &= (IXON | IXOFF);
3476 * gsmld_detach_gsm - stop doing 0710 mux
3477 * @tty: tty attached to the mux
3480 * Shutdown and then clean up the resources used by the line discipline
3483 static void gsmld_detach_gsm(struct tty_struct *tty, struct gsm_mux *gsm)
3485 WARN_ON(tty != gsm->tty);
3486 /* Restore tty XON/XOFF handling. */
3487 gsm->tty->termios.c_iflag = gsm->old_c_iflag;
3488 tty_kref_put(gsm->tty);
3492 static void gsmld_receive_buf(struct tty_struct *tty, const u8 *cp,
3493 const u8 *fp, size_t count)
3495 struct gsm_mux *gsm = tty->disc_data;
3496 char flags = TTY_NORMAL;
3498 if (debug & DBG_DATA)
3499 gsm_hex_dump_bytes(__func__, cp, count);
3501 for (; count; count--, cp++) {
3507 gsm->receive(gsm, *cp);
3516 WARN_ONCE(1, "%s: unknown flag %d\n",
3517 tty_name(tty), flags);
3521 /* FASYNC if needed ? */
3522 /* If clogged call tty_throttle(tty); */
3526 * gsmld_flush_buffer - clean input queue
3527 * @tty: terminal device
3529 * Flush the input buffer. Called when the line discipline is
3530 * being closed, when the tty layer wants the buffer flushed (eg
3534 static void gsmld_flush_buffer(struct tty_struct *tty)
3539 * gsmld_close - close the ldisc for this tty
3542 * Called from the terminal layer when this line discipline is
3543 * being shut down, either because of a close or becsuse of a
3544 * discipline change. The function will not be called while other
3545 * ldisc methods are in progress.
3548 static void gsmld_close(struct tty_struct *tty)
3550 struct gsm_mux *gsm = tty->disc_data;
3552 /* The ldisc locks and closes the port before calling our close. This
3553 * means we have no way to do a proper disconnect. We will not bother
3556 gsm_cleanup_mux(gsm, false);
3558 gsmld_detach_gsm(tty, gsm);
3560 gsmld_flush_buffer(tty);
3561 /* Do other clean up here */
3566 * gsmld_open - open an ldisc
3567 * @tty: terminal to open
3569 * Called when this line discipline is being attached to the
3570 * terminal device. Can sleep. Called serialized so that no
3571 * other events will occur in parallel. No further open will occur
3575 static int gsmld_open(struct tty_struct *tty)
3577 struct gsm_mux *gsm;
3579 if (!capable(CAP_NET_ADMIN))
3582 if (tty->ops->write == NULL)
3585 /* Attach our ldisc data */
3586 gsm = gsm_alloc_mux();
3590 tty->disc_data = gsm;
3591 tty->receive_room = 65536;
3593 /* Attach the initial passive connection */
3594 gsmld_attach_gsm(tty, gsm);
3596 /* The mux will not be activated yet, we wait for correct
3597 * configuration first.
3599 if (gsm->encoding == GSM_BASIC_OPT)
3600 gsm->receive = gsm0_receive;
3602 gsm->receive = gsm1_receive;
3608 * gsmld_write_wakeup - asynchronous I/O notifier
3611 * Required for the ptys, serial driver etc. since processes
3612 * that attach themselves to the master and rely on ASYNC
3613 * IO must be woken up
3616 static void gsmld_write_wakeup(struct tty_struct *tty)
3618 struct gsm_mux *gsm = tty->disc_data;
3621 gsmld_write_trigger(gsm);
3625 * gsmld_read - read function for tty
3627 * @file: file object
3628 * @buf: userspace buffer pointer
3633 * Perform reads for the line discipline. We are guaranteed that the
3634 * line discipline will not be closed under us but we may get multiple
3635 * parallel readers and must handle this ourselves. We may also get
3636 * a hangup. Always called in user context, may sleep.
3638 * This code must be sure never to sleep through a hangup.
3641 static ssize_t gsmld_read(struct tty_struct *tty, struct file *file, u8 *buf,
3642 size_t nr, void **cookie, unsigned long offset)
3648 * gsmld_write - write function for tty
3650 * @file: file object
3651 * @buf: userspace buffer pointer
3654 * Called when the owner of the device wants to send a frame
3655 * itself (or some other control data). The data is transferred
3656 * as-is and must be properly framed and checksummed as appropriate
3657 * by userspace. Frames are either sent whole or not at all as this
3658 * avoids pain user side.
3661 static ssize_t gsmld_write(struct tty_struct *tty, struct file *file,
3662 const u8 *buf, size_t nr)
3664 struct gsm_mux *gsm = tty->disc_data;
3665 unsigned long flags;
3673 spin_lock_irqsave(&gsm->tx_lock, flags);
3674 space = tty_write_room(tty);
3676 ret = tty->ops->write(tty, buf, nr);
3678 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
3679 spin_unlock_irqrestore(&gsm->tx_lock, flags);
3685 * gsmld_poll - poll method for N_GSM0710
3686 * @tty: terminal device
3687 * @file: file accessing it
3690 * Called when the line discipline is asked to poll() for data or
3691 * for special events. This code is not serialized with respect to
3692 * other events save open/close.
3694 * This code must be sure never to sleep through a hangup.
3695 * Called without the kernel lock held - fine
3698 static __poll_t gsmld_poll(struct tty_struct *tty, struct file *file,
3702 struct gsm_mux *gsm = tty->disc_data;
3704 poll_wait(file, &tty->read_wait, wait);
3705 poll_wait(file, &tty->write_wait, wait);
3709 if (tty_hung_up_p(file))
3711 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
3713 if (!tty_is_writelocked(tty) && tty_write_room(tty) > 0)
3714 mask |= EPOLLOUT | EPOLLWRNORM;
3718 static int gsmld_ioctl(struct tty_struct *tty, unsigned int cmd,
3721 struct gsm_config c;
3722 struct gsm_config_ext ce;
3723 struct gsm_dlci_config dc;
3724 struct gsm_mux *gsm = tty->disc_data;
3725 unsigned int base, addr;
3726 struct gsm_dlci *dlci;
3729 case GSMIOC_GETCONF:
3730 gsm_copy_config_values(gsm, &c);
3731 if (copy_to_user((void __user *)arg, &c, sizeof(c)))
3734 case GSMIOC_SETCONF:
3735 if (copy_from_user(&c, (void __user *)arg, sizeof(c)))
3737 return gsm_config(gsm, &c);
3738 case GSMIOC_GETFIRST:
3739 base = mux_num_to_base(gsm);
3740 return put_user(base + 1, (__u32 __user *)arg);
3741 case GSMIOC_GETCONF_EXT:
3742 gsm_copy_config_ext_values(gsm, &ce);
3743 if (copy_to_user((void __user *)arg, &ce, sizeof(ce)))
3746 case GSMIOC_SETCONF_EXT:
3747 if (copy_from_user(&ce, (void __user *)arg, sizeof(ce)))
3749 return gsm_config_ext(gsm, &ce);
3750 case GSMIOC_GETCONF_DLCI:
3751 if (copy_from_user(&dc, (void __user *)arg, sizeof(dc)))
3753 if (dc.channel == 0 || dc.channel >= NUM_DLCI)
3755 addr = array_index_nospec(dc.channel, NUM_DLCI);
3756 dlci = gsm->dlci[addr];
3758 dlci = gsm_dlci_alloc(gsm, addr);
3762 gsm_dlci_copy_config_values(dlci, &dc);
3763 if (copy_to_user((void __user *)arg, &dc, sizeof(dc)))
3766 case GSMIOC_SETCONF_DLCI:
3767 if (copy_from_user(&dc, (void __user *)arg, sizeof(dc)))
3769 if (dc.channel == 0 || dc.channel >= NUM_DLCI)
3771 addr = array_index_nospec(dc.channel, NUM_DLCI);
3772 dlci = gsm->dlci[addr];
3774 dlci = gsm_dlci_alloc(gsm, addr);
3778 return gsm_dlci_config(dlci, &dc, 0);
3780 return n_tty_ioctl_helper(tty, cmd, arg);
3789 static int gsm_mux_net_open(struct net_device *net)
3791 pr_debug("%s called\n", __func__);
3792 netif_start_queue(net);
3796 static int gsm_mux_net_close(struct net_device *net)
3798 netif_stop_queue(net);
3802 static void dlci_net_free(struct gsm_dlci *dlci)
3808 dlci->adaption = dlci->prev_adaption;
3809 dlci->data = dlci->prev_data;
3810 free_netdev(dlci->net);
3813 static void net_free(struct kref *ref)
3815 struct gsm_mux_net *mux_net;
3816 struct gsm_dlci *dlci;
3818 mux_net = container_of(ref, struct gsm_mux_net, ref);
3819 dlci = mux_net->dlci;
3822 unregister_netdev(dlci->net);
3823 dlci_net_free(dlci);
3827 static inline void muxnet_get(struct gsm_mux_net *mux_net)
3829 kref_get(&mux_net->ref);
3832 static inline void muxnet_put(struct gsm_mux_net *mux_net)
3834 kref_put(&mux_net->ref, net_free);
3837 static netdev_tx_t gsm_mux_net_start_xmit(struct sk_buff *skb,
3838 struct net_device *net)
3840 struct gsm_mux_net *mux_net = netdev_priv(net);
3841 struct gsm_dlci *dlci = mux_net->dlci;
3842 muxnet_get(mux_net);
3844 skb_queue_head(&dlci->skb_list, skb);
3845 net->stats.tx_packets++;
3846 net->stats.tx_bytes += skb->len;
3847 gsm_dlci_data_kick(dlci);
3848 /* And tell the kernel when the last transmit started. */
3849 netif_trans_update(net);
3850 muxnet_put(mux_net);
3851 return NETDEV_TX_OK;
3854 /* called when a packet did not ack after watchdogtimeout */
3855 static void gsm_mux_net_tx_timeout(struct net_device *net, unsigned int txqueue)
3857 /* Tell syslog we are hosed. */
3858 dev_dbg(&net->dev, "Tx timed out.\n");
3860 /* Update statistics */
3861 net->stats.tx_errors++;
3864 static void gsm_mux_rx_netchar(struct gsm_dlci *dlci,
3865 const unsigned char *in_buf, int size)
3867 struct net_device *net = dlci->net;
3868 struct sk_buff *skb;
3869 struct gsm_mux_net *mux_net = netdev_priv(net);
3870 muxnet_get(mux_net);
3872 /* Allocate an sk_buff */
3873 skb = dev_alloc_skb(size + NET_IP_ALIGN);
3875 /* We got no receive buffer. */
3876 net->stats.rx_dropped++;
3877 muxnet_put(mux_net);
3880 skb_reserve(skb, NET_IP_ALIGN);
3881 skb_put_data(skb, in_buf, size);
3884 skb->protocol = htons(ETH_P_IP);
3886 /* Ship it off to the kernel */
3889 /* update out statistics */
3890 net->stats.rx_packets++;
3891 net->stats.rx_bytes += size;
3892 muxnet_put(mux_net);
3896 static void gsm_mux_net_init(struct net_device *net)
3898 static const struct net_device_ops gsm_netdev_ops = {
3899 .ndo_open = gsm_mux_net_open,
3900 .ndo_stop = gsm_mux_net_close,
3901 .ndo_start_xmit = gsm_mux_net_start_xmit,
3902 .ndo_tx_timeout = gsm_mux_net_tx_timeout,
3905 net->netdev_ops = &gsm_netdev_ops;
3907 /* fill in the other fields */
3908 net->watchdog_timeo = GSM_NET_TX_TIMEOUT;
3909 net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
3910 net->type = ARPHRD_NONE;
3911 net->tx_queue_len = 10;
3915 /* caller holds the dlci mutex */
3916 static void gsm_destroy_network(struct gsm_dlci *dlci)
3918 struct gsm_mux_net *mux_net;
3920 pr_debug("destroy network interface\n");
3923 mux_net = netdev_priv(dlci->net);
3924 muxnet_put(mux_net);
3928 /* caller holds the dlci mutex */
3929 static int gsm_create_network(struct gsm_dlci *dlci, struct gsm_netconfig *nc)
3933 struct net_device *net;
3934 struct gsm_mux_net *mux_net;
3936 if (!capable(CAP_NET_ADMIN))
3939 /* Already in a non tty mode */
3940 if (dlci->adaption > 2)
3943 if (nc->protocol != htons(ETH_P_IP))
3944 return -EPROTONOSUPPORT;
3946 if (nc->adaption != 3 && nc->adaption != 4)
3947 return -EPROTONOSUPPORT;
3949 pr_debug("create network interface\n");
3952 if (nc->if_name[0] != '\0')
3953 netname = nc->if_name;
3954 net = alloc_netdev(sizeof(struct gsm_mux_net), netname,
3955 NET_NAME_UNKNOWN, gsm_mux_net_init);
3957 pr_err("alloc_netdev failed\n");
3960 net->mtu = dlci->mtu;
3961 net->min_mtu = MIN_MTU;
3962 net->max_mtu = dlci->mtu;
3963 mux_net = netdev_priv(net);
3964 mux_net->dlci = dlci;
3965 kref_init(&mux_net->ref);
3966 strncpy(nc->if_name, net->name, IFNAMSIZ); /* return net name */
3968 /* reconfigure dlci for network */
3969 dlci->prev_adaption = dlci->adaption;
3970 dlci->prev_data = dlci->data;
3971 dlci->adaption = nc->adaption;
3972 dlci->data = gsm_mux_rx_netchar;
3975 pr_debug("register netdev\n");
3976 retval = register_netdev(net);
3978 pr_err("network register fail %d\n", retval);
3979 dlci_net_free(dlci);
3982 return net->ifindex; /* return network index */
3985 /* Line discipline for real tty */
3986 static struct tty_ldisc_ops tty_ldisc_packet = {
3987 .owner = THIS_MODULE,
3991 .close = gsmld_close,
3992 .flush_buffer = gsmld_flush_buffer,
3994 .write = gsmld_write,
3995 .ioctl = gsmld_ioctl,
3997 .receive_buf = gsmld_receive_buf,
3998 .write_wakeup = gsmld_write_wakeup
4006 * gsm_modem_upd_via_data - send modem bits via convergence layer
4008 * @brk: break signal
4010 * Send an empty frame to signal mobile state changes and to transmit the
4011 * break signal for adaption 2.
4014 static void gsm_modem_upd_via_data(struct gsm_dlci *dlci, u8 brk)
4016 struct gsm_mux *gsm = dlci->gsm;
4017 unsigned long flags;
4019 if (dlci->state != DLCI_OPEN || dlci->adaption != 2)
4022 spin_lock_irqsave(&gsm->tx_lock, flags);
4023 gsm_dlci_modem_output(gsm, dlci, brk);
4024 spin_unlock_irqrestore(&gsm->tx_lock, flags);
4028 * gsm_modem_upd_via_msc - send modem bits via control frame
4030 * @brk: break signal
4033 static int gsm_modem_upd_via_msc(struct gsm_dlci *dlci, u8 brk)
4036 struct gsm_control *ctrl;
4039 if (dlci->gsm->encoding != GSM_BASIC_OPT)
4042 modembits[0] = (dlci->addr << 2) | 2 | EA; /* DLCI, Valid, EA */
4044 modembits[1] = (gsm_encode_modem(dlci) << 1) | EA;
4046 modembits[1] = gsm_encode_modem(dlci) << 1;
4047 modembits[2] = (brk << 4) | 2 | EA; /* Length, Break, EA */
4050 ctrl = gsm_control_send(dlci->gsm, CMD_MSC, modembits, len);
4053 return gsm_control_wait(dlci->gsm, ctrl);
4057 * gsm_modem_update - send modem status line state
4059 * @brk: break signal
4062 static int gsm_modem_update(struct gsm_dlci *dlci, u8 brk)
4064 if (dlci->adaption == 2) {
4065 /* Send convergence layer type 2 empty data frame. */
4066 gsm_modem_upd_via_data(dlci, brk);
4068 } else if (dlci->gsm->encoding == GSM_BASIC_OPT) {
4069 /* Send as MSC control message. */
4070 return gsm_modem_upd_via_msc(dlci, brk);
4073 /* Modem status lines are not supported. */
4074 return -EPROTONOSUPPORT;
4078 * gsm_wait_modem_change - wait for modem status line change
4080 * @mask: modem status line bits
4082 * The function returns if:
4083 * - any given modem status line bit changed
4084 * - the wait event function got interrupted (e.g. by a signal)
4085 * - the underlying DLCI was closed
4086 * - the underlying ldisc device was removed
4088 static int gsm_wait_modem_change(struct gsm_dlci *dlci, u32 mask)
4090 struct gsm_mux *gsm = dlci->gsm;
4091 u32 old = dlci->modem_rx;
4094 ret = wait_event_interruptible(gsm->event, gsm->dead ||
4095 dlci->state != DLCI_OPEN ||
4096 (old ^ dlci->modem_rx) & mask);
4099 if (dlci->state != DLCI_OPEN)
4104 static bool gsm_carrier_raised(struct tty_port *port)
4106 struct gsm_dlci *dlci = container_of(port, struct gsm_dlci, port);
4107 struct gsm_mux *gsm = dlci->gsm;
4109 /* Not yet open so no carrier info */
4110 if (dlci->state != DLCI_OPEN)
4112 if (debug & DBG_CD_ON)
4116 * Basic mode with control channel in ADM mode may not respond
4117 * to CMD_MSC at all and modem_rx is empty.
4119 if (gsm->encoding == GSM_BASIC_OPT &&
4120 gsm->dlci[0]->mode == DLCI_MODE_ADM && !dlci->modem_rx)
4123 return dlci->modem_rx & TIOCM_CD;
4126 static void gsm_dtr_rts(struct tty_port *port, bool active)
4128 struct gsm_dlci *dlci = container_of(port, struct gsm_dlci, port);
4129 unsigned int modem_tx = dlci->modem_tx;
4131 modem_tx |= TIOCM_DTR | TIOCM_RTS;
4133 modem_tx &= ~(TIOCM_DTR | TIOCM_RTS);
4134 if (modem_tx != dlci->modem_tx) {
4135 dlci->modem_tx = modem_tx;
4136 gsm_modem_update(dlci, 0);
4140 static const struct tty_port_operations gsm_port_ops = {
4141 .carrier_raised = gsm_carrier_raised,
4142 .dtr_rts = gsm_dtr_rts,
4143 .destruct = gsm_dlci_free,
4146 static int gsmtty_install(struct tty_driver *driver, struct tty_struct *tty)
4148 struct gsm_mux *gsm;
4149 struct gsm_dlci *dlci;
4150 unsigned int line = tty->index;
4151 unsigned int mux = mux_line_to_num(line);
4159 /* FIXME: we need to lock gsm_mux for lifetimes of ttys eventually */
4160 if (gsm_mux[mux] == NULL)
4162 if (line == 0 || line > 61) /* 62/63 reserved */
4167 /* If DLCI 0 is not yet fully open return an error.
4168 This is ok from a locking
4169 perspective as we don't have to worry about this
4171 mutex_lock(&gsm->mutex);
4172 if (gsm->dlci[0] && gsm->dlci[0]->state != DLCI_OPEN) {
4173 mutex_unlock(&gsm->mutex);
4176 dlci = gsm->dlci[line];
4179 dlci = gsm_dlci_alloc(gsm, line);
4182 mutex_unlock(&gsm->mutex);
4185 ret = tty_port_install(&dlci->port, driver, tty);
4189 mutex_unlock(&gsm->mutex);
4194 dlci_get(gsm->dlci[0]);
4196 tty->driver_data = dlci;
4197 mutex_unlock(&gsm->mutex);
4202 static int gsmtty_open(struct tty_struct *tty, struct file *filp)
4204 struct gsm_dlci *dlci = tty->driver_data;
4205 struct tty_port *port = &dlci->port;
4208 tty_port_tty_set(port, tty);
4211 /* We could in theory open and close before we wait - eg if we get
4212 a DM straight back. This is ok as that will have caused a hangup */
4213 tty_port_set_initialized(port, true);
4214 /* Start sending off SABM messages */
4215 if (!dlci->gsm->wait_config) {
4216 /* Start sending off SABM messages */
4217 if (dlci->gsm->initiator)
4218 gsm_dlci_begin_open(dlci);
4220 gsm_dlci_set_opening(dlci);
4222 gsm_dlci_set_wait_config(dlci);
4224 /* And wait for virtual carrier */
4225 return tty_port_block_til_ready(port, tty, filp);
4228 static void gsmtty_close(struct tty_struct *tty, struct file *filp)
4230 struct gsm_dlci *dlci = tty->driver_data;
4234 if (dlci->state == DLCI_CLOSED)
4236 mutex_lock(&dlci->mutex);
4237 gsm_destroy_network(dlci);
4238 mutex_unlock(&dlci->mutex);
4239 if (tty_port_close_start(&dlci->port, tty, filp) == 0)
4241 gsm_dlci_begin_close(dlci);
4242 if (tty_port_initialized(&dlci->port) && C_HUPCL(tty))
4243 tty_port_lower_dtr_rts(&dlci->port);
4244 tty_port_close_end(&dlci->port, tty);
4245 tty_port_tty_set(&dlci->port, NULL);
4249 static void gsmtty_hangup(struct tty_struct *tty)
4251 struct gsm_dlci *dlci = tty->driver_data;
4252 if (dlci->state == DLCI_CLOSED)
4254 tty_port_hangup(&dlci->port);
4255 gsm_dlci_begin_close(dlci);
4258 static ssize_t gsmtty_write(struct tty_struct *tty, const u8 *buf, size_t len)
4261 struct gsm_dlci *dlci = tty->driver_data;
4262 if (dlci->state == DLCI_CLOSED)
4264 /* Stuff the bytes into the fifo queue */
4265 sent = kfifo_in_locked(&dlci->fifo, buf, len, &dlci->lock);
4266 /* Need to kick the channel */
4267 gsm_dlci_data_kick(dlci);
4271 static unsigned int gsmtty_write_room(struct tty_struct *tty)
4273 struct gsm_dlci *dlci = tty->driver_data;
4274 if (dlci->state == DLCI_CLOSED)
4276 return kfifo_avail(&dlci->fifo);
4279 static unsigned int gsmtty_chars_in_buffer(struct tty_struct *tty)
4281 struct gsm_dlci *dlci = tty->driver_data;
4282 if (dlci->state == DLCI_CLOSED)
4284 return kfifo_len(&dlci->fifo);
4287 static void gsmtty_flush_buffer(struct tty_struct *tty)
4289 struct gsm_dlci *dlci = tty->driver_data;
4290 unsigned long flags;
4292 if (dlci->state == DLCI_CLOSED)
4294 /* Caution needed: If we implement reliable transport classes
4295 then the data being transmitted can't simply be junked once
4296 it has first hit the stack. Until then we can just blow it
4298 spin_lock_irqsave(&dlci->lock, flags);
4299 kfifo_reset(&dlci->fifo);
4300 spin_unlock_irqrestore(&dlci->lock, flags);
4301 /* Need to unhook this DLCI from the transmit queue logic */
4304 static void gsmtty_wait_until_sent(struct tty_struct *tty, int timeout)
4306 /* The FIFO handles the queue so the kernel will do the right
4307 thing waiting on chars_in_buffer before calling us. No work
4311 static int gsmtty_tiocmget(struct tty_struct *tty)
4313 struct gsm_dlci *dlci = tty->driver_data;
4314 if (dlci->state == DLCI_CLOSED)
4316 return dlci->modem_rx;
4319 static int gsmtty_tiocmset(struct tty_struct *tty,
4320 unsigned int set, unsigned int clear)
4322 struct gsm_dlci *dlci = tty->driver_data;
4323 unsigned int modem_tx = dlci->modem_tx;
4325 if (dlci->state == DLCI_CLOSED)
4330 if (modem_tx != dlci->modem_tx) {
4331 dlci->modem_tx = modem_tx;
4332 return gsm_modem_update(dlci, 0);
4338 static int gsmtty_ioctl(struct tty_struct *tty,
4339 unsigned int cmd, unsigned long arg)
4341 struct gsm_dlci *dlci = tty->driver_data;
4342 struct gsm_netconfig nc;
4343 struct gsm_dlci_config dc;
4346 if (dlci->state == DLCI_CLOSED)
4349 case GSMIOC_ENABLE_NET:
4350 if (copy_from_user(&nc, (void __user *)arg, sizeof(nc)))
4352 nc.if_name[IFNAMSIZ-1] = '\0';
4353 /* return net interface index or error code */
4354 mutex_lock(&dlci->mutex);
4355 index = gsm_create_network(dlci, &nc);
4356 mutex_unlock(&dlci->mutex);
4357 if (copy_to_user((void __user *)arg, &nc, sizeof(nc)))
4360 case GSMIOC_DISABLE_NET:
4361 if (!capable(CAP_NET_ADMIN))
4363 mutex_lock(&dlci->mutex);
4364 gsm_destroy_network(dlci);
4365 mutex_unlock(&dlci->mutex);
4367 case GSMIOC_GETCONF_DLCI:
4368 if (copy_from_user(&dc, (void __user *)arg, sizeof(dc)))
4370 if (dc.channel != dlci->addr)
4372 gsm_dlci_copy_config_values(dlci, &dc);
4373 if (copy_to_user((void __user *)arg, &dc, sizeof(dc)))
4376 case GSMIOC_SETCONF_DLCI:
4377 if (copy_from_user(&dc, (void __user *)arg, sizeof(dc)))
4379 if (dc.channel >= NUM_DLCI)
4381 if (dc.channel != 0 && dc.channel != dlci->addr)
4383 return gsm_dlci_config(dlci, &dc, 1);
4385 return gsm_wait_modem_change(dlci, (u32)arg);
4387 return -ENOIOCTLCMD;
4391 static void gsmtty_set_termios(struct tty_struct *tty,
4392 const struct ktermios *old)
4394 struct gsm_dlci *dlci = tty->driver_data;
4395 if (dlci->state == DLCI_CLOSED)
4397 /* For the moment its fixed. In actual fact the speed information
4398 for the virtual channel can be propogated in both directions by
4399 the RPN control message. This however rapidly gets nasty as we
4400 then have to remap modem signals each way according to whether
4401 our virtual cable is null modem etc .. */
4402 tty_termios_copy_hw(&tty->termios, old);
4405 static void gsmtty_throttle(struct tty_struct *tty)
4407 struct gsm_dlci *dlci = tty->driver_data;
4408 if (dlci->state == DLCI_CLOSED)
4411 dlci->modem_tx &= ~TIOCM_RTS;
4412 dlci->throttled = true;
4413 /* Send an MSC with RTS cleared */
4414 gsm_modem_update(dlci, 0);
4417 static void gsmtty_unthrottle(struct tty_struct *tty)
4419 struct gsm_dlci *dlci = tty->driver_data;
4420 if (dlci->state == DLCI_CLOSED)
4423 dlci->modem_tx |= TIOCM_RTS;
4424 dlci->throttled = false;
4425 /* Send an MSC with RTS set */
4426 gsm_modem_update(dlci, 0);
4429 static int gsmtty_break_ctl(struct tty_struct *tty, int state)
4431 struct gsm_dlci *dlci = tty->driver_data;
4432 int encode = 0; /* Off */
4433 if (dlci->state == DLCI_CLOSED)
4436 if (state == -1) /* "On indefinitely" - we can't encode this
4439 else if (state > 0) {
4440 encode = state / 200; /* mS to encoding */
4442 encode = 0x0F; /* Best effort */
4444 return gsm_modem_update(dlci, encode);
4447 static void gsmtty_cleanup(struct tty_struct *tty)
4449 struct gsm_dlci *dlci = tty->driver_data;
4450 struct gsm_mux *gsm = dlci->gsm;
4453 dlci_put(gsm->dlci[0]);
4457 /* Virtual ttys for the demux */
4458 static const struct tty_operations gsmtty_ops = {
4459 .install = gsmtty_install,
4460 .open = gsmtty_open,
4461 .close = gsmtty_close,
4462 .write = gsmtty_write,
4463 .write_room = gsmtty_write_room,
4464 .chars_in_buffer = gsmtty_chars_in_buffer,
4465 .flush_buffer = gsmtty_flush_buffer,
4466 .ioctl = gsmtty_ioctl,
4467 .throttle = gsmtty_throttle,
4468 .unthrottle = gsmtty_unthrottle,
4469 .set_termios = gsmtty_set_termios,
4470 .hangup = gsmtty_hangup,
4471 .wait_until_sent = gsmtty_wait_until_sent,
4472 .tiocmget = gsmtty_tiocmget,
4473 .tiocmset = gsmtty_tiocmset,
4474 .break_ctl = gsmtty_break_ctl,
4475 .cleanup = gsmtty_cleanup,
4480 static int __init gsm_init(void)
4482 /* Fill in our line protocol discipline, and register it */
4483 int status = tty_register_ldisc(&tty_ldisc_packet);
4485 pr_err("n_gsm: can't register line discipline (err = %d)\n",
4490 gsm_tty_driver = tty_alloc_driver(GSM_TTY_MINORS, TTY_DRIVER_REAL_RAW |
4491 TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_HARDWARE_BREAK);
4492 if (IS_ERR(gsm_tty_driver)) {
4493 pr_err("gsm_init: tty allocation failed.\n");
4494 status = PTR_ERR(gsm_tty_driver);
4495 goto err_unreg_ldisc;
4497 gsm_tty_driver->driver_name = "gsmtty";
4498 gsm_tty_driver->name = "gsmtty";
4499 gsm_tty_driver->major = 0; /* Dynamic */
4500 gsm_tty_driver->minor_start = 0;
4501 gsm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
4502 gsm_tty_driver->subtype = SERIAL_TYPE_NORMAL;
4503 gsm_tty_driver->init_termios = tty_std_termios;
4505 gsm_tty_driver->init_termios.c_lflag &= ~ECHO;
4506 tty_set_operations(gsm_tty_driver, &gsmtty_ops);
4508 if (tty_register_driver(gsm_tty_driver)) {
4509 pr_err("gsm_init: tty registration failed.\n");
4511 goto err_put_driver;
4513 pr_debug("gsm_init: loaded as %d,%d.\n",
4514 gsm_tty_driver->major, gsm_tty_driver->minor_start);
4517 tty_driver_kref_put(gsm_tty_driver);
4519 tty_unregister_ldisc(&tty_ldisc_packet);
4523 static void __exit gsm_exit(void)
4525 tty_unregister_ldisc(&tty_ldisc_packet);
4526 tty_unregister_driver(gsm_tty_driver);
4527 tty_driver_kref_put(gsm_tty_driver);
4530 module_init(gsm_init);
4531 module_exit(gsm_exit);
4534 MODULE_LICENSE("GPL");
4535 MODULE_ALIAS_LDISC(N_GSM0710);