0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033 #include <linux/types.h>
0034 #include <linux/major.h>
0035 #include <linux/errno.h>
0036 #include <linux/signal.h>
0037 #include <linux/fcntl.h>
0038 #include <linux/sched/signal.h>
0039 #include <linux/interrupt.h>
0040 #include <linux/tty.h>
0041 #include <linux/ctype.h>
0042 #include <linux/mm.h>
0043 #include <linux/string.h>
0044 #include <linux/slab.h>
0045 #include <linux/poll.h>
0046 #include <linux/bitops.h>
0047 #include <linux/file.h>
0048 #include <linux/uaccess.h>
0049 #include <linux/module.h>
0050 #include <linux/timer.h>
0051 #include <linux/tty_flip.h>
0052 #include <linux/tty_driver.h>
0053 #include <linux/serial.h>
0054 #include <linux/kfifo.h>
0055 #include <linux/skbuff.h>
0056 #include <net/arp.h>
0057 #include <linux/ip.h>
0058 #include <linux/netdevice.h>
0059 #include <linux/etherdevice.h>
0060 #include <linux/gsmmux.h>
0061 #include "tty.h"
0062
0063 static int debug;
0064 module_param(debug, int, 0600);
0065
0066
0067
0068 #define T1 10
0069 #define T2 34
0070 #define N2 3
0071
0072
0073 #ifdef DEBUG_TIMING
0074 #define T1 100
0075 #define T2 200
0076 #endif
0077
0078
0079
0080
0081
0082 #define MAX_MRU 1500
0083 #define MAX_MTU 1500
0084
0085 #define PROT_OVERHEAD 7
0086 #define GSM_NET_TX_TIMEOUT (HZ*10)
0087
0088
0089
0090
0091
0092
0093 struct gsm_mux_net {
0094 struct kref ref;
0095 struct gsm_dlci *dlci;
0096 };
0097
0098
0099
0100
0101
0102
0103
0104 struct gsm_msg {
0105 struct list_head list;
0106 u8 addr;
0107 u8 ctrl;
0108 unsigned int len;
0109 unsigned char *data;
0110 unsigned char buffer[];
0111 };
0112
0113 enum gsm_dlci_state {
0114 DLCI_CLOSED,
0115 DLCI_OPENING,
0116 DLCI_OPEN,
0117 DLCI_CLOSING,
0118 };
0119
0120 enum gsm_dlci_mode {
0121 DLCI_MODE_ABM,
0122 DLCI_MODE_ADM,
0123 };
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135 struct gsm_dlci {
0136 struct gsm_mux *gsm;
0137 int addr;
0138 enum gsm_dlci_state state;
0139 struct mutex mutex;
0140
0141
0142 enum gsm_dlci_mode mode;
0143 spinlock_t lock;
0144 struct timer_list t1;
0145 int retries;
0146
0147 struct tty_port port;
0148 #define TX_SIZE 4096
0149 struct kfifo fifo;
0150 int adaption;
0151 int prev_adaption;
0152 u32 modem_rx;
0153 u32 modem_tx;
0154 bool dead;
0155
0156 bool throttled;
0157 bool constipated;
0158
0159 struct sk_buff *skb;
0160 struct sk_buff_head skb_list;
0161
0162 void (*data)(struct gsm_dlci *dlci, const u8 *data, int len);
0163 void (*prev_data)(struct gsm_dlci *dlci, const u8 *data, int len);
0164 struct net_device *net;
0165 };
0166
0167
0168
0169 #define NUM_DLCI 64
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179 struct gsm_control {
0180 u8 cmd;
0181 u8 *data;
0182 int len;
0183 int done;
0184 int error;
0185 };
0186
0187 enum gsm_mux_state {
0188 GSM_SEARCH,
0189 GSM_START,
0190 GSM_ADDRESS,
0191 GSM_CONTROL,
0192 GSM_LEN,
0193 GSM_DATA,
0194 GSM_FCS,
0195 GSM_OVERRUN,
0196 GSM_LEN0,
0197 GSM_LEN1,
0198 GSM_SSOF,
0199 };
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211 struct gsm_mux {
0212 struct tty_struct *tty;
0213 spinlock_t lock;
0214 struct mutex mutex;
0215 unsigned int num;
0216 struct kref ref;
0217
0218
0219 wait_queue_head_t event;
0220
0221
0222 struct work_struct tx_work;
0223
0224
0225
0226
0227 unsigned char *buf;
0228 enum gsm_mux_state state;
0229 unsigned int len;
0230 unsigned int address;
0231 unsigned int count;
0232 bool escape;
0233 int encoding;
0234 u8 control;
0235 u8 fcs;
0236 u8 *txframe;
0237
0238
0239 void (*receive)(struct gsm_mux *gsm, u8 ch);
0240
0241
0242 unsigned int mru;
0243 unsigned int mtu;
0244 int initiator;
0245 bool dead;
0246 struct gsm_dlci *dlci[NUM_DLCI];
0247 int old_c_iflag;
0248 bool constipated;
0249 bool has_devices;
0250
0251 struct mutex tx_mutex;
0252 unsigned int tx_bytes;
0253 #define TX_THRESH_HI 8192
0254 #define TX_THRESH_LO 2048
0255 struct list_head tx_ctrl_list;
0256 struct list_head tx_data_list;
0257
0258
0259 struct delayed_work kick_timeout;
0260 struct timer_list t2_timer;
0261 int cretries;
0262 struct gsm_control *pending_cmd;
0263 spinlock_t control_lock;
0264
0265
0266 int adaption;
0267 u8 ftype;
0268 int t1, t2;
0269 int n2;
0270
0271
0272 unsigned long bad_fcs;
0273 unsigned long malformed;
0274 unsigned long io_error;
0275 unsigned long bad_size;
0276 unsigned long unsupported;
0277 };
0278
0279
0280
0281
0282
0283
0284
0285 #define MAX_MUX 4
0286 static struct gsm_mux *gsm_mux[MAX_MUX];
0287 static DEFINE_SPINLOCK(gsm_mux_lock);
0288
0289 static struct tty_driver *gsm_tty_driver;
0290
0291
0292
0293
0294
0295
0296
0297 #define CR 0x02
0298 #define EA 0x01
0299 #define PF 0x10
0300
0301
0302 #define RR 0x01
0303 #define UI 0x03
0304 #define RNR 0x05
0305 #define REJ 0x09
0306 #define DM 0x0F
0307 #define SABM 0x2F
0308 #define DISC 0x43
0309 #define UA 0x63
0310 #define UIH 0xEF
0311
0312
0313 #define CMD_NSC 0x09
0314 #define CMD_TEST 0x11
0315 #define CMD_PSC 0x21
0316 #define CMD_RLS 0x29
0317 #define CMD_FCOFF 0x31
0318 #define CMD_PN 0x41
0319 #define CMD_RPN 0x49
0320 #define CMD_FCON 0x51
0321 #define CMD_CLD 0x61
0322 #define CMD_SNC 0x69
0323 #define CMD_MSC 0x71
0324
0325
0326 #define MDM_FC 0x01
0327 #define MDM_RTC 0x02
0328 #define MDM_RTR 0x04
0329 #define MDM_IC 0x20
0330 #define MDM_DV 0x40
0331
0332 #define GSM0_SOF 0xF9
0333 #define GSM1_SOF 0x7E
0334 #define GSM1_ESCAPE 0x7D
0335 #define GSM1_ESCAPE_BITS 0x20
0336 #define XON 0x11
0337 #define XOFF 0x13
0338 #define ISO_IEC_646_MASK 0x7F
0339
0340 static const struct tty_port_operations gsm_port_ops;
0341
0342
0343
0344
0345
0346 static const u8 gsm_fcs8[256] = {
0347 0x00, 0x91, 0xE3, 0x72, 0x07, 0x96, 0xE4, 0x75,
0348 0x0E, 0x9F, 0xED, 0x7C, 0x09, 0x98, 0xEA, 0x7B,
0349 0x1C, 0x8D, 0xFF, 0x6E, 0x1B, 0x8A, 0xF8, 0x69,
0350 0x12, 0x83, 0xF1, 0x60, 0x15, 0x84, 0xF6, 0x67,
0351 0x38, 0xA9, 0xDB, 0x4A, 0x3F, 0xAE, 0xDC, 0x4D,
0352 0x36, 0xA7, 0xD5, 0x44, 0x31, 0xA0, 0xD2, 0x43,
0353 0x24, 0xB5, 0xC7, 0x56, 0x23, 0xB2, 0xC0, 0x51,
0354 0x2A, 0xBB, 0xC9, 0x58, 0x2D, 0xBC, 0xCE, 0x5F,
0355 0x70, 0xE1, 0x93, 0x02, 0x77, 0xE6, 0x94, 0x05,
0356 0x7E, 0xEF, 0x9D, 0x0C, 0x79, 0xE8, 0x9A, 0x0B,
0357 0x6C, 0xFD, 0x8F, 0x1E, 0x6B, 0xFA, 0x88, 0x19,
0358 0x62, 0xF3, 0x81, 0x10, 0x65, 0xF4, 0x86, 0x17,
0359 0x48, 0xD9, 0xAB, 0x3A, 0x4F, 0xDE, 0xAC, 0x3D,
0360 0x46, 0xD7, 0xA5, 0x34, 0x41, 0xD0, 0xA2, 0x33,
0361 0x54, 0xC5, 0xB7, 0x26, 0x53, 0xC2, 0xB0, 0x21,
0362 0x5A, 0xCB, 0xB9, 0x28, 0x5D, 0xCC, 0xBE, 0x2F,
0363 0xE0, 0x71, 0x03, 0x92, 0xE7, 0x76, 0x04, 0x95,
0364 0xEE, 0x7F, 0x0D, 0x9C, 0xE9, 0x78, 0x0A, 0x9B,
0365 0xFC, 0x6D, 0x1F, 0x8E, 0xFB, 0x6A, 0x18, 0x89,
0366 0xF2, 0x63, 0x11, 0x80, 0xF5, 0x64, 0x16, 0x87,
0367 0xD8, 0x49, 0x3B, 0xAA, 0xDF, 0x4E, 0x3C, 0xAD,
0368 0xD6, 0x47, 0x35, 0xA4, 0xD1, 0x40, 0x32, 0xA3,
0369 0xC4, 0x55, 0x27, 0xB6, 0xC3, 0x52, 0x20, 0xB1,
0370 0xCA, 0x5B, 0x29, 0xB8, 0xCD, 0x5C, 0x2E, 0xBF,
0371 0x90, 0x01, 0x73, 0xE2, 0x97, 0x06, 0x74, 0xE5,
0372 0x9E, 0x0F, 0x7D, 0xEC, 0x99, 0x08, 0x7A, 0xEB,
0373 0x8C, 0x1D, 0x6F, 0xFE, 0x8B, 0x1A, 0x68, 0xF9,
0374 0x82, 0x13, 0x61, 0xF0, 0x85, 0x14, 0x66, 0xF7,
0375 0xA8, 0x39, 0x4B, 0xDA, 0xAF, 0x3E, 0x4C, 0xDD,
0376 0xA6, 0x37, 0x45, 0xD4, 0xA1, 0x30, 0x42, 0xD3,
0377 0xB4, 0x25, 0x57, 0xC6, 0xB3, 0x22, 0x50, 0xC1,
0378 0xBA, 0x2B, 0x59, 0xC8, 0xBD, 0x2C, 0x5E, 0xCF
0379 };
0380
0381 #define INIT_FCS 0xFF
0382 #define GOOD_FCS 0xCF
0383
0384 static int gsmld_output(struct gsm_mux *gsm, u8 *data, int len);
0385 static int gsm_modem_update(struct gsm_dlci *dlci, u8 brk);
0386 static struct gsm_msg *gsm_data_alloc(struct gsm_mux *gsm, u8 addr, int len,
0387 u8 ctrl);
0388 static int gsm_send_packet(struct gsm_mux *gsm, struct gsm_msg *msg);
0389 static void gsmld_write_trigger(struct gsm_mux *gsm);
0390 static void gsmld_write_task(struct work_struct *work);
0391
0392
0393
0394
0395
0396
0397
0398
0399
0400
0401 static inline u8 gsm_fcs_add(u8 fcs, u8 c)
0402 {
0403 return gsm_fcs8[fcs ^ c];
0404 }
0405
0406
0407
0408
0409
0410
0411
0412
0413
0414
0415
0416 static inline u8 gsm_fcs_add_block(u8 fcs, u8 *c, int len)
0417 {
0418 while (len--)
0419 fcs = gsm_fcs8[fcs ^ *c++];
0420 return fcs;
0421 }
0422
0423
0424
0425
0426
0427
0428
0429
0430
0431
0432 static int gsm_read_ea(unsigned int *val, u8 c)
0433 {
0434
0435 *val <<= 7;
0436 *val |= c >> 1;
0437
0438 return c & EA;
0439 }
0440
0441
0442
0443
0444
0445
0446
0447
0448
0449
0450 static unsigned int gsm_read_ea_val(unsigned int *val, const u8 *data, int dlen)
0451 {
0452 unsigned int len = 0;
0453
0454 for (; dlen > 0; dlen--) {
0455 len++;
0456 if (gsm_read_ea(val, *data++))
0457 break;
0458 }
0459 return len;
0460 }
0461
0462
0463
0464
0465
0466
0467
0468
0469
0470 static u8 gsm_encode_modem(const struct gsm_dlci *dlci)
0471 {
0472 u8 modembits = 0;
0473
0474 if (dlci->throttled)
0475 modembits |= MDM_FC;
0476 if (dlci->modem_tx & TIOCM_DTR)
0477 modembits |= MDM_RTC;
0478 if (dlci->modem_tx & TIOCM_RTS)
0479 modembits |= MDM_RTR;
0480 if (dlci->modem_tx & TIOCM_RI)
0481 modembits |= MDM_IC;
0482 if (dlci->modem_tx & TIOCM_CD || dlci->gsm->initiator)
0483 modembits |= MDM_DV;
0484 return modembits;
0485 }
0486
0487 static void gsm_hex_dump_bytes(const char *fname, const u8 *data,
0488 unsigned long len)
0489 {
0490 char *prefix;
0491
0492 if (!fname) {
0493 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE, 16, 1, data, len,
0494 true);
0495 return;
0496 }
0497
0498 prefix = kasprintf(GFP_ATOMIC, "%s: ", fname);
0499 if (!prefix)
0500 return;
0501 print_hex_dump(KERN_INFO, prefix, DUMP_PREFIX_OFFSET, 16, 1, data, len,
0502 true);
0503 kfree(prefix);
0504 }
0505
0506
0507
0508
0509
0510
0511
0512
0513
0514 static int gsm_register_devices(struct tty_driver *driver, unsigned int index)
0515 {
0516 struct device *dev;
0517 int i;
0518 unsigned int base;
0519
0520 if (!driver || index >= MAX_MUX)
0521 return -EINVAL;
0522
0523 base = index * NUM_DLCI;
0524 for (i = 1; i < NUM_DLCI; i++) {
0525
0526
0527
0528 dev = tty_register_device(gsm_tty_driver, base + i, NULL);
0529 if (IS_ERR(dev)) {
0530 if (debug & 8)
0531 pr_info("%s failed to register device minor %u",
0532 __func__, base + i);
0533 for (i--; i >= 1; i--)
0534 tty_unregister_device(gsm_tty_driver, base + i);
0535 return PTR_ERR(dev);
0536 }
0537 }
0538
0539 return 0;
0540 }
0541
0542
0543
0544
0545
0546
0547
0548
0549
0550 static void gsm_unregister_devices(struct tty_driver *driver,
0551 unsigned int index)
0552 {
0553 int i;
0554 unsigned int base;
0555
0556 if (!driver || index >= MAX_MUX)
0557 return;
0558
0559 base = index * NUM_DLCI;
0560 for (i = 1; i < NUM_DLCI; i++) {
0561
0562
0563
0564 tty_unregister_device(gsm_tty_driver, base + i);
0565 }
0566 }
0567
0568
0569
0570
0571
0572
0573
0574
0575
0576
0577
0578
0579
0580
0581 static void gsm_print_packet(const char *hdr, int addr, int cr,
0582 u8 control, const u8 *data, int dlen)
0583 {
0584 if (!(debug & 1))
0585 return;
0586
0587 pr_info("%s %d) %c: ", hdr, addr, "RC"[cr]);
0588
0589 switch (control & ~PF) {
0590 case SABM:
0591 pr_cont("SABM");
0592 break;
0593 case UA:
0594 pr_cont("UA");
0595 break;
0596 case DISC:
0597 pr_cont("DISC");
0598 break;
0599 case DM:
0600 pr_cont("DM");
0601 break;
0602 case UI:
0603 pr_cont("UI");
0604 break;
0605 case UIH:
0606 pr_cont("UIH");
0607 break;
0608 default:
0609 if (!(control & 0x01)) {
0610 pr_cont("I N(S)%d N(R)%d",
0611 (control & 0x0E) >> 1, (control & 0xE0) >> 5);
0612 } else switch (control & 0x0F) {
0613 case RR:
0614 pr_cont("RR(%d)", (control & 0xE0) >> 5);
0615 break;
0616 case RNR:
0617 pr_cont("RNR(%d)", (control & 0xE0) >> 5);
0618 break;
0619 case REJ:
0620 pr_cont("REJ(%d)", (control & 0xE0) >> 5);
0621 break;
0622 default:
0623 pr_cont("[%02X]", control);
0624 }
0625 }
0626
0627 if (control & PF)
0628 pr_cont("(P)");
0629 else
0630 pr_cont("(F)");
0631
0632 gsm_hex_dump_bytes(NULL, data, dlen);
0633 }
0634
0635
0636
0637
0638
0639
0640
0641
0642
0643
0644
0645
0646
0647
0648
0649
0650
0651 static int gsm_stuff_frame(const u8 *input, u8 *output, int len)
0652 {
0653 int olen = 0;
0654 while (len--) {
0655 if (*input == GSM1_SOF || *input == GSM1_ESCAPE
0656 || (*input & ISO_IEC_646_MASK) == XON
0657 || (*input & ISO_IEC_646_MASK) == XOFF) {
0658 *output++ = GSM1_ESCAPE;
0659 *output++ = *input++ ^ GSM1_ESCAPE_BITS;
0660 olen++;
0661 } else
0662 *output++ = *input++;
0663 olen++;
0664 }
0665 return olen;
0666 }
0667
0668
0669
0670
0671
0672
0673
0674
0675
0676
0677
0678 static int gsm_send(struct gsm_mux *gsm, int addr, int cr, int control)
0679 {
0680 struct gsm_msg *msg;
0681 u8 *dp;
0682 int ocr;
0683
0684 msg = gsm_data_alloc(gsm, addr, 0, control);
0685 if (!msg)
0686 return -ENOMEM;
0687
0688
0689 ocr = cr ^ (gsm->initiator ? 0 : 1);
0690
0691 msg->data -= 3;
0692 dp = msg->data;
0693 *dp++ = (addr << 2) | (ocr << 1) | EA;
0694 *dp++ = control;
0695
0696 if (gsm->encoding == 0)
0697 *dp++ = EA;
0698
0699 *dp = 0xFF - gsm_fcs_add_block(INIT_FCS, msg->data, dp - msg->data);
0700 msg->len = (dp - msg->data) + 1;
0701
0702 gsm_print_packet("Q->", addr, cr, control, NULL, 0);
0703
0704 mutex_lock(&gsm->tx_mutex);
0705 list_add_tail(&msg->list, &gsm->tx_ctrl_list);
0706 gsm->tx_bytes += msg->len;
0707 mutex_unlock(&gsm->tx_mutex);
0708 gsmld_write_trigger(gsm);
0709
0710 return 0;
0711 }
0712
0713
0714
0715
0716
0717
0718
0719
0720 static void gsm_dlci_clear_queues(struct gsm_mux *gsm, struct gsm_dlci *dlci)
0721 {
0722 struct gsm_msg *msg, *nmsg;
0723 int addr = dlci->addr;
0724 unsigned long flags;
0725
0726
0727 spin_lock_irqsave(&dlci->lock, flags);
0728 kfifo_reset(&dlci->fifo);
0729 spin_unlock_irqrestore(&dlci->lock, flags);
0730
0731
0732 mutex_lock(&gsm->tx_mutex);
0733 list_for_each_entry_safe(msg, nmsg, &gsm->tx_data_list, list) {
0734 if (msg->addr != addr)
0735 continue;
0736 gsm->tx_bytes -= msg->len;
0737 list_del(&msg->list);
0738 kfree(msg);
0739 }
0740 mutex_unlock(&gsm->tx_mutex);
0741 }
0742
0743
0744
0745
0746
0747
0748
0749
0750
0751
0752 static inline void gsm_response(struct gsm_mux *gsm, int addr, int control)
0753 {
0754 gsm_send(gsm, addr, 0, control);
0755 }
0756
0757
0758
0759
0760
0761
0762
0763
0764
0765
0766 static inline void gsm_command(struct gsm_mux *gsm, int addr, int control)
0767 {
0768 gsm_send(gsm, addr, 1, control);
0769 }
0770
0771
0772
0773 #define HDR_LEN 6
0774
0775
0776
0777
0778
0779
0780
0781
0782
0783
0784
0785
0786
0787 static struct gsm_msg *gsm_data_alloc(struct gsm_mux *gsm, u8 addr, int len,
0788 u8 ctrl)
0789 {
0790 struct gsm_msg *m = kmalloc(sizeof(struct gsm_msg) + len + HDR_LEN,
0791 GFP_ATOMIC);
0792 if (m == NULL)
0793 return NULL;
0794 m->data = m->buffer + HDR_LEN - 1;
0795 m->len = len;
0796 m->addr = addr;
0797 m->ctrl = ctrl;
0798 INIT_LIST_HEAD(&m->list);
0799 return m;
0800 }
0801
0802
0803
0804
0805
0806
0807
0808
0809
0810 static int gsm_send_packet(struct gsm_mux *gsm, struct gsm_msg *msg)
0811 {
0812 int len, ret;
0813
0814
0815 if (gsm->encoding == 0) {
0816 gsm->txframe[0] = GSM0_SOF;
0817 memcpy(gsm->txframe + 1, msg->data, msg->len);
0818 gsm->txframe[msg->len + 1] = GSM0_SOF;
0819 len = msg->len + 2;
0820 } else {
0821 gsm->txframe[0] = GSM1_SOF;
0822 len = gsm_stuff_frame(msg->data, gsm->txframe + 1, msg->len);
0823 gsm->txframe[len + 1] = GSM1_SOF;
0824 len += 2;
0825 }
0826
0827 if (debug & 4)
0828 gsm_hex_dump_bytes(__func__, gsm->txframe, len);
0829 gsm_print_packet("-->", msg->addr, gsm->initiator, msg->ctrl, msg->data,
0830 msg->len);
0831
0832 ret = gsmld_output(gsm, gsm->txframe, len);
0833 if (ret <= 0)
0834 return ret;
0835
0836 gsm->tx_bytes -= msg->len;
0837
0838 return 0;
0839 }
0840
0841
0842
0843
0844
0845
0846
0847
0848 static bool gsm_is_flow_ctrl_msg(struct gsm_msg *msg)
0849 {
0850 unsigned int cmd;
0851
0852 if (msg->addr > 0)
0853 return false;
0854
0855 switch (msg->ctrl & ~PF) {
0856 case UI:
0857 case UIH:
0858 cmd = 0;
0859 if (gsm_read_ea_val(&cmd, msg->data + 2, msg->len - 2) < 1)
0860 break;
0861 switch (cmd & ~PF) {
0862 case CMD_FCOFF:
0863 case CMD_FCON:
0864 return true;
0865 }
0866 break;
0867 }
0868
0869 return false;
0870 }
0871
0872
0873
0874
0875
0876
0877
0878
0879
0880
0881
0882 static int gsm_data_kick(struct gsm_mux *gsm)
0883 {
0884 struct gsm_msg *msg, *nmsg;
0885 struct gsm_dlci *dlci;
0886 int ret;
0887
0888 clear_bit(TTY_DO_WRITE_WAKEUP, &gsm->tty->flags);
0889
0890
0891 list_for_each_entry_safe(msg, nmsg, &gsm->tx_ctrl_list, list) {
0892 if (gsm->constipated && !gsm_is_flow_ctrl_msg(msg))
0893 continue;
0894 ret = gsm_send_packet(gsm, msg);
0895 switch (ret) {
0896 case -ENOSPC:
0897 return -ENOSPC;
0898 case -ENODEV:
0899
0900 gsm->tx_bytes -= msg->len;
0901 list_del(&msg->list);
0902 kfree(msg);
0903 continue;
0904 default:
0905 if (ret >= 0) {
0906 list_del(&msg->list);
0907 kfree(msg);
0908 }
0909 break;
0910 }
0911 }
0912
0913 if (gsm->constipated)
0914 return -EAGAIN;
0915
0916
0917 if (list_empty(&gsm->tx_data_list))
0918 return 0;
0919 list_for_each_entry_safe(msg, nmsg, &gsm->tx_data_list, list) {
0920 dlci = gsm->dlci[msg->addr];
0921
0922 if (dlci->state != DLCI_OPEN) {
0923 gsm->tx_bytes -= msg->len;
0924 list_del(&msg->list);
0925 kfree(msg);
0926 continue;
0927 }
0928 ret = gsm_send_packet(gsm, msg);
0929 switch (ret) {
0930 case -ENOSPC:
0931 return -ENOSPC;
0932 case -ENODEV:
0933
0934 gsm->tx_bytes -= msg->len;
0935 list_del(&msg->list);
0936 kfree(msg);
0937 continue;
0938 default:
0939 if (ret >= 0) {
0940 list_del(&msg->list);
0941 kfree(msg);
0942 }
0943 break;
0944 }
0945 }
0946
0947 return 1;
0948 }
0949
0950
0951
0952
0953
0954
0955
0956
0957
0958
0959
0960 static void __gsm_data_queue(struct gsm_dlci *dlci, struct gsm_msg *msg)
0961 {
0962 struct gsm_mux *gsm = dlci->gsm;
0963 u8 *dp = msg->data;
0964 u8 *fcs = dp + msg->len;
0965
0966
0967 if (gsm->encoding == 0) {
0968 if (msg->len < 128)
0969 *--dp = (msg->len << 1) | EA;
0970 else {
0971 *--dp = (msg->len >> 7);
0972 *--dp = (msg->len & 127) << 1;
0973 }
0974 }
0975
0976 *--dp = msg->ctrl;
0977 if (gsm->initiator)
0978 *--dp = (msg->addr << 2) | CR | EA;
0979 else
0980 *--dp = (msg->addr << 2) | EA;
0981 *fcs = gsm_fcs_add_block(INIT_FCS, dp , msg->data - dp);
0982
0983 if (msg->ctrl == UI || msg->ctrl == (UI|PF))
0984 *fcs = gsm_fcs_add_block(*fcs, msg->data, msg->len);
0985 *fcs = 0xFF - *fcs;
0986
0987 gsm_print_packet("Q> ", msg->addr, gsm->initiator, msg->ctrl,
0988 msg->data, msg->len);
0989
0990
0991
0992 msg->len += (msg->data - dp) + 1;
0993 msg->data = dp;
0994
0995
0996 switch (msg->ctrl & ~PF) {
0997 case UI:
0998 case UIH:
0999 if (msg->addr > 0) {
1000 list_add_tail(&msg->list, &gsm->tx_data_list);
1001 break;
1002 }
1003 fallthrough;
1004 default:
1005 list_add_tail(&msg->list, &gsm->tx_ctrl_list);
1006 break;
1007 }
1008 gsm->tx_bytes += msg->len;
1009
1010 gsmld_write_trigger(gsm);
1011 schedule_delayed_work(&gsm->kick_timeout, 10 * gsm->t1 * HZ / 100);
1012 }
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024 static void gsm_data_queue(struct gsm_dlci *dlci, struct gsm_msg *msg)
1025 {
1026 mutex_lock(&dlci->gsm->tx_mutex);
1027 __gsm_data_queue(dlci, msg);
1028 mutex_unlock(&dlci->gsm->tx_mutex);
1029 }
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043 static int gsm_dlci_data_output(struct gsm_mux *gsm, struct gsm_dlci *dlci)
1044 {
1045 struct gsm_msg *msg;
1046 u8 *dp;
1047 int h, len, size;
1048
1049
1050 h = ((dlci->adaption == 1) ? 0 : 1);
1051
1052 len = kfifo_len(&dlci->fifo);
1053 if (len == 0)
1054 return 0;
1055
1056
1057 if ((len + h) > gsm->mtu)
1058 len = gsm->mtu - h;
1059
1060 size = len + h;
1061
1062 msg = gsm_data_alloc(gsm, dlci->addr, size, gsm->ftype);
1063 if (!msg)
1064 return -ENOMEM;
1065 dp = msg->data;
1066 switch (dlci->adaption) {
1067 case 1:
1068 break;
1069 case 2:
1070
1071
1072 *dp++ = (gsm_encode_modem(dlci) << 1) | EA;
1073 break;
1074 default:
1075 pr_err("%s: unsupported adaption %d\n", __func__,
1076 dlci->adaption);
1077 break;
1078 }
1079
1080 WARN_ON(len != kfifo_out_locked(&dlci->fifo, dp, len,
1081 &dlci->lock));
1082
1083
1084 tty_port_tty_wakeup(&dlci->port);
1085
1086 __gsm_data_queue(dlci, msg);
1087
1088 return size;
1089 }
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103 static int gsm_dlci_data_output_framed(struct gsm_mux *gsm,
1104 struct gsm_dlci *dlci)
1105 {
1106 struct gsm_msg *msg;
1107 u8 *dp;
1108 int len, size;
1109 int last = 0, first = 0;
1110 int overhead = 0;
1111
1112
1113 if (dlci->adaption == 4)
1114 overhead = 1;
1115
1116
1117 if (dlci->skb == NULL) {
1118 dlci->skb = skb_dequeue_tail(&dlci->skb_list);
1119 if (dlci->skb == NULL)
1120 return 0;
1121 first = 1;
1122 }
1123 len = dlci->skb->len + overhead;
1124
1125
1126 if (len > gsm->mtu) {
1127 if (dlci->adaption == 3) {
1128
1129 dev_kfree_skb_any(dlci->skb);
1130 dlci->skb = NULL;
1131 return 0;
1132 }
1133 len = gsm->mtu;
1134 } else
1135 last = 1;
1136
1137 size = len + overhead;
1138 msg = gsm_data_alloc(gsm, dlci->addr, size, gsm->ftype);
1139 if (msg == NULL) {
1140 skb_queue_tail(&dlci->skb_list, dlci->skb);
1141 dlci->skb = NULL;
1142 return -ENOMEM;
1143 }
1144 dp = msg->data;
1145
1146 if (dlci->adaption == 4) {
1147
1148 *dp++ = last << 7 | first << 6 | 1;
1149 len--;
1150 }
1151 memcpy(dp, dlci->skb->data, len);
1152 skb_pull(dlci->skb, len);
1153 __gsm_data_queue(dlci, msg);
1154 if (last) {
1155 dev_kfree_skb_any(dlci->skb);
1156 dlci->skb = NULL;
1157 }
1158 return size;
1159 }
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173 static int gsm_dlci_modem_output(struct gsm_mux *gsm, struct gsm_dlci *dlci,
1174 u8 brk)
1175 {
1176 u8 *dp = NULL;
1177 struct gsm_msg *msg;
1178 int size = 0;
1179
1180
1181 switch (dlci->adaption) {
1182 case 1:
1183 break;
1184 case 2:
1185 size++;
1186 if (brk > 0)
1187 size++;
1188 break;
1189 default:
1190 pr_err("%s: unsupported adaption %d\n", __func__,
1191 dlci->adaption);
1192 return -EINVAL;
1193 }
1194
1195 msg = gsm_data_alloc(gsm, dlci->addr, size, gsm->ftype);
1196 if (!msg) {
1197 pr_err("%s: gsm_data_alloc error", __func__);
1198 return -ENOMEM;
1199 }
1200 dp = msg->data;
1201 switch (dlci->adaption) {
1202 case 1:
1203 break;
1204 case 2:
1205 if (brk == 0) {
1206 *dp++ = (gsm_encode_modem(dlci) << 1) | EA;
1207 } else {
1208 *dp++ = gsm_encode_modem(dlci) << 1;
1209 *dp++ = (brk << 4) | 2 | EA;
1210 }
1211 break;
1212 default:
1213
1214 break;
1215 }
1216
1217 __gsm_data_queue(dlci, msg);
1218 return size;
1219 }
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234 static int gsm_dlci_data_sweep(struct gsm_mux *gsm)
1235 {
1236
1237 int i, len, ret = 0;
1238 bool sent;
1239 struct gsm_dlci *dlci;
1240
1241 while (gsm->tx_bytes < TX_THRESH_HI) {
1242 for (sent = false, i = 1; i < NUM_DLCI; i++) {
1243 dlci = gsm->dlci[i];
1244
1245 if (!dlci || dlci->constipated)
1246 continue;
1247
1248 if (dlci->state != DLCI_OPEN)
1249 continue;
1250
1251 if (dlci->adaption < 3 && !dlci->net)
1252 len = gsm_dlci_data_output(gsm, dlci);
1253 else
1254 len = gsm_dlci_data_output_framed(gsm, dlci);
1255
1256 if (len < 0)
1257 return ret;
1258 if (len > 0) {
1259 ret++;
1260 sent = true;
1261
1262 break;
1263 }
1264
1265 }
1266 if (!sent)
1267 break;
1268 };
1269
1270 return ret;
1271 }
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282 static void gsm_dlci_data_kick(struct gsm_dlci *dlci)
1283 {
1284 int sweep;
1285
1286 if (dlci->constipated)
1287 return;
1288
1289 mutex_lock(&dlci->gsm->tx_mutex);
1290
1291 sweep = (dlci->gsm->tx_bytes < TX_THRESH_LO);
1292 if (dlci->gsm->tx_bytes == 0) {
1293 if (dlci->net)
1294 gsm_dlci_data_output_framed(dlci->gsm, dlci);
1295 else
1296 gsm_dlci_data_output(dlci->gsm, dlci);
1297 }
1298 if (sweep)
1299 gsm_dlci_data_sweep(dlci->gsm);
1300 mutex_unlock(&dlci->gsm->tx_mutex);
1301 }
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318 static void gsm_control_reply(struct gsm_mux *gsm, int cmd, const u8 *data,
1319 int dlen)
1320 {
1321 struct gsm_msg *msg;
1322 msg = gsm_data_alloc(gsm, 0, dlen + 2, gsm->ftype);
1323 if (msg == NULL)
1324 return;
1325 msg->data[0] = (cmd & 0xFE) << 1 | EA;
1326 msg->data[1] = (dlen << 1) | EA;
1327 memcpy(msg->data + 2, data, dlen);
1328 gsm_data_queue(gsm->dlci[0], msg);
1329 }
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342 static void gsm_process_modem(struct tty_struct *tty, struct gsm_dlci *dlci,
1343 u32 modem, int slen)
1344 {
1345 int mlines = 0;
1346 u8 brk = 0;
1347 int fc;
1348
1349
1350
1351
1352
1353
1354 if (slen == 1)
1355 modem = modem & 0x7f;
1356 else {
1357 brk = modem & 0x7f;
1358 modem = (modem >> 7) & 0x7f;
1359 }
1360
1361
1362 fc = (modem & MDM_FC) || !(modem & MDM_RTR);
1363 if (fc && !dlci->constipated) {
1364
1365 dlci->constipated = true;
1366 } else if (!fc && dlci->constipated) {
1367 dlci->constipated = false;
1368 gsm_dlci_data_kick(dlci);
1369 }
1370
1371
1372 if (modem & MDM_RTC)
1373 mlines |= TIOCM_DSR | TIOCM_DTR;
1374 if (modem & MDM_RTR)
1375 mlines |= TIOCM_RTS | TIOCM_CTS;
1376 if (modem & MDM_IC)
1377 mlines |= TIOCM_RI;
1378 if (modem & MDM_DV)
1379 mlines |= TIOCM_CD;
1380
1381
1382 if (tty) {
1383 if ((mlines & TIOCM_CD) == 0 && (dlci->modem_rx & TIOCM_CD))
1384 if (!C_CLOCAL(tty))
1385 tty_hangup(tty);
1386 }
1387 if (brk & 0x01)
1388 tty_insert_flip_char(&dlci->port, 0, TTY_BREAK);
1389 dlci->modem_rx = mlines;
1390 }
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404 static void gsm_control_modem(struct gsm_mux *gsm, const u8 *data, int clen)
1405 {
1406 unsigned int addr = 0;
1407 unsigned int modem = 0;
1408 struct gsm_dlci *dlci;
1409 int len = clen;
1410 int slen;
1411 const u8 *dp = data;
1412 struct tty_struct *tty;
1413
1414 while (gsm_read_ea(&addr, *dp++) == 0) {
1415 len--;
1416 if (len == 0)
1417 return;
1418 }
1419
1420 len--;
1421 if (len <= 0)
1422 return;
1423
1424 addr >>= 1;
1425
1426 if (addr == 0 || addr >= NUM_DLCI || gsm->dlci[addr] == NULL)
1427 return;
1428 dlci = gsm->dlci[addr];
1429
1430 slen = len;
1431 while (gsm_read_ea(&modem, *dp++) == 0) {
1432 len--;
1433 if (len == 0)
1434 return;
1435 }
1436 len--;
1437 tty = tty_port_tty_get(&dlci->port);
1438 gsm_process_modem(tty, dlci, modem, slen - len);
1439 if (tty) {
1440 tty_wakeup(tty);
1441 tty_kref_put(tty);
1442 }
1443 gsm_control_reply(gsm, CMD_MSC, data, clen);
1444 }
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457 static void gsm_control_rls(struct gsm_mux *gsm, const u8 *data, int clen)
1458 {
1459 struct tty_port *port;
1460 unsigned int addr = 0;
1461 u8 bits;
1462 int len = clen;
1463 const u8 *dp = data;
1464
1465 while (gsm_read_ea(&addr, *dp++) == 0) {
1466 len--;
1467 if (len == 0)
1468 return;
1469 }
1470
1471 len--;
1472 if (len <= 0)
1473 return;
1474 addr >>= 1;
1475
1476 if (addr == 0 || addr >= NUM_DLCI || gsm->dlci[addr] == NULL)
1477 return;
1478
1479 bits = *dp;
1480 if ((bits & 1) == 0)
1481 return;
1482
1483 port = &gsm->dlci[addr]->port;
1484
1485 if (bits & 2)
1486 tty_insert_flip_char(port, 0, TTY_OVERRUN);
1487 if (bits & 4)
1488 tty_insert_flip_char(port, 0, TTY_PARITY);
1489 if (bits & 8)
1490 tty_insert_flip_char(port, 0, TTY_FRAME);
1491
1492 tty_flip_buffer_push(port);
1493
1494 gsm_control_reply(gsm, CMD_RLS, data, clen);
1495 }
1496
1497 static void gsm_dlci_begin_close(struct gsm_dlci *dlci);
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511 static void gsm_control_message(struct gsm_mux *gsm, unsigned int command,
1512 const u8 *data, int clen)
1513 {
1514 u8 buf[1];
1515
1516 switch (command) {
1517 case CMD_CLD: {
1518 struct gsm_dlci *dlci = gsm->dlci[0];
1519
1520 if (dlci) {
1521 dlci->dead = true;
1522 gsm->dead = true;
1523 gsm_dlci_begin_close(dlci);
1524 }
1525 }
1526 break;
1527 case CMD_TEST:
1528
1529 gsm_control_reply(gsm, CMD_TEST, data, clen);
1530 break;
1531 case CMD_FCON:
1532
1533 gsm->constipated = false;
1534 gsm_control_reply(gsm, CMD_FCON, NULL, 0);
1535
1536 gsmld_write_trigger(gsm);
1537 break;
1538 case CMD_FCOFF:
1539
1540 gsm->constipated = true;
1541 gsm_control_reply(gsm, CMD_FCOFF, NULL, 0);
1542 break;
1543 case CMD_MSC:
1544
1545 gsm_control_modem(gsm, data, clen);
1546 break;
1547 case CMD_RLS:
1548
1549 gsm_control_rls(gsm, data, clen);
1550 break;
1551 case CMD_PSC:
1552
1553 gsm_control_reply(gsm, CMD_PSC, NULL, 0);
1554 break;
1555
1556 case CMD_PN:
1557 case CMD_RPN:
1558 case CMD_SNC:
1559 default:
1560
1561 buf[0] = command;
1562 gsm_control_reply(gsm, CMD_NSC, buf, 1);
1563 break;
1564 }
1565 }
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580 static void gsm_control_response(struct gsm_mux *gsm, unsigned int command,
1581 const u8 *data, int clen)
1582 {
1583 struct gsm_control *ctrl;
1584 unsigned long flags;
1585
1586 spin_lock_irqsave(&gsm->control_lock, flags);
1587
1588 ctrl = gsm->pending_cmd;
1589
1590 command |= 1;
1591 if (ctrl != NULL && (command == ctrl->cmd || command == CMD_NSC)) {
1592
1593 del_timer(&gsm->t2_timer);
1594 gsm->pending_cmd = NULL;
1595
1596 if (command == CMD_NSC)
1597 ctrl->error = -EOPNOTSUPP;
1598 ctrl->done = 1;
1599 wake_up(&gsm->event);
1600 }
1601 spin_unlock_irqrestore(&gsm->control_lock, flags);
1602 }
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612 static void gsm_control_transmit(struct gsm_mux *gsm, struct gsm_control *ctrl)
1613 {
1614 struct gsm_msg *msg = gsm_data_alloc(gsm, 0, ctrl->len + 2, gsm->ftype);
1615 if (msg == NULL)
1616 return;
1617 msg->data[0] = (ctrl->cmd << 1) | CR | EA;
1618 msg->data[1] = (ctrl->len << 1) | EA;
1619 memcpy(msg->data + 2, ctrl->data, ctrl->len);
1620 gsm_data_queue(gsm->dlci[0], msg);
1621 }
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634 static void gsm_control_retransmit(struct timer_list *t)
1635 {
1636 struct gsm_mux *gsm = from_timer(gsm, t, t2_timer);
1637 struct gsm_control *ctrl;
1638 unsigned long flags;
1639 spin_lock_irqsave(&gsm->control_lock, flags);
1640 ctrl = gsm->pending_cmd;
1641 if (ctrl) {
1642 if (gsm->cretries == 0 || !gsm->dlci[0] || gsm->dlci[0]->dead) {
1643 gsm->pending_cmd = NULL;
1644 ctrl->error = -ETIMEDOUT;
1645 ctrl->done = 1;
1646 spin_unlock_irqrestore(&gsm->control_lock, flags);
1647 wake_up(&gsm->event);
1648 return;
1649 }
1650 gsm->cretries--;
1651 gsm_control_transmit(gsm, ctrl);
1652 mod_timer(&gsm->t2_timer, jiffies + gsm->t2 * HZ / 100);
1653 }
1654 spin_unlock_irqrestore(&gsm->control_lock, flags);
1655 }
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669 static struct gsm_control *gsm_control_send(struct gsm_mux *gsm,
1670 unsigned int command, u8 *data, int clen)
1671 {
1672 struct gsm_control *ctrl = kzalloc(sizeof(struct gsm_control),
1673 GFP_KERNEL);
1674 unsigned long flags;
1675 if (ctrl == NULL)
1676 return NULL;
1677 retry:
1678 wait_event(gsm->event, gsm->pending_cmd == NULL);
1679 spin_lock_irqsave(&gsm->control_lock, flags);
1680 if (gsm->pending_cmd != NULL) {
1681 spin_unlock_irqrestore(&gsm->control_lock, flags);
1682 goto retry;
1683 }
1684 ctrl->cmd = command;
1685 ctrl->data = data;
1686 ctrl->len = clen;
1687 gsm->pending_cmd = ctrl;
1688
1689
1690 if (gsm->dlci[0]->mode == DLCI_MODE_ADM)
1691 gsm->cretries = 0;
1692 else
1693 gsm->cretries = gsm->n2;
1694
1695 mod_timer(&gsm->t2_timer, jiffies + gsm->t2 * HZ / 100);
1696 gsm_control_transmit(gsm, ctrl);
1697 spin_unlock_irqrestore(&gsm->control_lock, flags);
1698 return ctrl;
1699 }
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711 static int gsm_control_wait(struct gsm_mux *gsm, struct gsm_control *control)
1712 {
1713 int err;
1714 wait_event(gsm->event, control->done == 1);
1715 err = control->error;
1716 kfree(control);
1717 return err;
1718 }
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737 static void gsm_dlci_close(struct gsm_dlci *dlci)
1738 {
1739 del_timer(&dlci->t1);
1740 if (debug & 8)
1741 pr_debug("DLCI %d goes closed.\n", dlci->addr);
1742 dlci->state = DLCI_CLOSED;
1743
1744 dlci->constipated = true;
1745 if (dlci->addr != 0) {
1746 tty_port_tty_hangup(&dlci->port, false);
1747 gsm_dlci_clear_queues(dlci->gsm, dlci);
1748
1749 tty_port_set_initialized(&dlci->port, 0);
1750 wake_up_interruptible(&dlci->port.open_wait);
1751 } else
1752 dlci->gsm->dead = true;
1753
1754
1755 gsm_dlci_data_kick(dlci);
1756 wake_up(&dlci->gsm->event);
1757 }
1758
1759
1760
1761
1762
1763
1764
1765
1766 static void gsm_dlci_open(struct gsm_dlci *dlci)
1767 {
1768
1769
1770 del_timer(&dlci->t1);
1771
1772 dlci->state = DLCI_OPEN;
1773 dlci->constipated = false;
1774 if (debug & 8)
1775 pr_debug("DLCI %d goes open.\n", dlci->addr);
1776
1777 if (dlci->addr)
1778 gsm_modem_update(dlci, 0);
1779 gsm_dlci_data_kick(dlci);
1780 wake_up(&dlci->gsm->event);
1781 }
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798 static void gsm_dlci_t1(struct timer_list *t)
1799 {
1800 struct gsm_dlci *dlci = from_timer(dlci, t, t1);
1801 struct gsm_mux *gsm = dlci->gsm;
1802
1803 switch (dlci->state) {
1804 case DLCI_OPENING:
1805 if (dlci->retries) {
1806 dlci->retries--;
1807 gsm_command(dlci->gsm, dlci->addr, SABM|PF);
1808 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
1809 } else if (!dlci->addr && gsm->control == (DM | PF)) {
1810 if (debug & 8)
1811 pr_info("DLCI %d opening in ADM mode.\n",
1812 dlci->addr);
1813 dlci->mode = DLCI_MODE_ADM;
1814 gsm_dlci_open(dlci);
1815 } else {
1816 gsm_dlci_begin_close(dlci);
1817 }
1818
1819 break;
1820 case DLCI_CLOSING:
1821 if (dlci->retries) {
1822 dlci->retries--;
1823 gsm_command(dlci->gsm, dlci->addr, DISC|PF);
1824 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
1825 } else
1826 gsm_dlci_close(dlci);
1827 break;
1828 default:
1829 pr_debug("%s: unhandled state: %d\n", __func__, dlci->state);
1830 break;
1831 }
1832 }
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844 static void gsm_dlci_begin_open(struct gsm_dlci *dlci)
1845 {
1846 struct gsm_mux *gsm = dlci->gsm;
1847 if (dlci->state == DLCI_OPEN || dlci->state == DLCI_OPENING)
1848 return;
1849 dlci->retries = gsm->n2;
1850 dlci->state = DLCI_OPENING;
1851 gsm_command(dlci->gsm, dlci->addr, SABM|PF);
1852 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
1853 }
1854
1855
1856
1857
1858
1859
1860
1861
1862 static void gsm_dlci_set_opening(struct gsm_dlci *dlci)
1863 {
1864 switch (dlci->state) {
1865 case DLCI_CLOSED:
1866 case DLCI_CLOSING:
1867 dlci->state = DLCI_OPENING;
1868 break;
1869 default:
1870 break;
1871 }
1872 }
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885 static void gsm_dlci_begin_close(struct gsm_dlci *dlci)
1886 {
1887 struct gsm_mux *gsm = dlci->gsm;
1888 if (dlci->state == DLCI_CLOSED || dlci->state == DLCI_CLOSING)
1889 return;
1890 dlci->retries = gsm->n2;
1891 dlci->state = DLCI_CLOSING;
1892 gsm_command(dlci->gsm, dlci->addr, DISC|PF);
1893 mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
1894 }
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907 static void gsm_dlci_data(struct gsm_dlci *dlci, const u8 *data, int clen)
1908 {
1909
1910 struct tty_port *port = &dlci->port;
1911 struct tty_struct *tty;
1912 unsigned int modem = 0;
1913 int len = clen;
1914 int slen = 0;
1915
1916 if (debug & 16)
1917 pr_debug("%d bytes for tty\n", len);
1918 switch (dlci->adaption) {
1919
1920 case 4:
1921 break;
1922 case 3:
1923 break;
1924 case 2:
1925 while (gsm_read_ea(&modem, *data++) == 0) {
1926 len--;
1927 slen++;
1928 if (len == 0)
1929 return;
1930 }
1931 len--;
1932 slen++;
1933 tty = tty_port_tty_get(port);
1934 if (tty) {
1935 gsm_process_modem(tty, dlci, modem, slen);
1936 tty_wakeup(tty);
1937 tty_kref_put(tty);
1938 }
1939 fallthrough;
1940 case 1:
1941 default:
1942 tty_insert_flip_string(port, data, len);
1943 tty_flip_buffer_push(port);
1944 }
1945 }
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959 static void gsm_dlci_command(struct gsm_dlci *dlci, const u8 *data, int len)
1960 {
1961
1962 unsigned int command = 0;
1963 while (len-- > 0) {
1964 if (gsm_read_ea(&command, *data++) == 1) {
1965 int clen = *data++;
1966 len--;
1967
1968 clen >>= 1;
1969
1970 if (clen > len)
1971 return;
1972 if (command & 1)
1973 gsm_control_message(dlci->gsm, command,
1974 data, clen);
1975 else
1976 gsm_control_response(dlci->gsm, command,
1977 data, clen);
1978 return;
1979 }
1980 }
1981 }
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991 static void gsm_kick_timeout(struct work_struct *work)
1992 {
1993 struct gsm_mux *gsm = container_of(work, struct gsm_mux, kick_timeout.work);
1994 int sent = 0;
1995
1996 mutex_lock(&gsm->tx_mutex);
1997
1998 if (gsm->tx_bytes < TX_THRESH_LO)
1999 sent = gsm_dlci_data_sweep(gsm);
2000 mutex_unlock(&gsm->tx_mutex);
2001
2002 if (sent && debug & 4)
2003 pr_info("%s TX queue stalled\n", __func__);
2004 }
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020 static struct gsm_dlci *gsm_dlci_alloc(struct gsm_mux *gsm, int addr)
2021 {
2022 struct gsm_dlci *dlci = kzalloc(sizeof(struct gsm_dlci), GFP_ATOMIC);
2023 if (dlci == NULL)
2024 return NULL;
2025 spin_lock_init(&dlci->lock);
2026 mutex_init(&dlci->mutex);
2027 if (kfifo_alloc(&dlci->fifo, TX_SIZE, GFP_KERNEL) < 0) {
2028 kfree(dlci);
2029 return NULL;
2030 }
2031
2032 skb_queue_head_init(&dlci->skb_list);
2033 timer_setup(&dlci->t1, gsm_dlci_t1, 0);
2034 tty_port_init(&dlci->port);
2035 dlci->port.ops = &gsm_port_ops;
2036 dlci->gsm = gsm;
2037 dlci->addr = addr;
2038 dlci->adaption = gsm->adaption;
2039 dlci->state = DLCI_CLOSED;
2040 if (addr) {
2041 dlci->data = gsm_dlci_data;
2042
2043 dlci->constipated = true;
2044 } else {
2045 dlci->data = gsm_dlci_command;
2046 }
2047 gsm->dlci[addr] = dlci;
2048 return dlci;
2049 }
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059 static void gsm_dlci_free(struct tty_port *port)
2060 {
2061 struct gsm_dlci *dlci = container_of(port, struct gsm_dlci, port);
2062
2063 del_timer_sync(&dlci->t1);
2064 dlci->gsm->dlci[dlci->addr] = NULL;
2065 kfifo_free(&dlci->fifo);
2066 while ((dlci->skb = skb_dequeue(&dlci->skb_list)))
2067 dev_kfree_skb(dlci->skb);
2068 kfree(dlci);
2069 }
2070
2071 static inline void dlci_get(struct gsm_dlci *dlci)
2072 {
2073 tty_port_get(&dlci->port);
2074 }
2075
2076 static inline void dlci_put(struct gsm_dlci *dlci)
2077 {
2078 tty_port_put(&dlci->port);
2079 }
2080
2081 static void gsm_destroy_network(struct gsm_dlci *dlci);
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092 static void gsm_dlci_release(struct gsm_dlci *dlci)
2093 {
2094 struct tty_struct *tty = tty_port_tty_get(&dlci->port);
2095 if (tty) {
2096 mutex_lock(&dlci->mutex);
2097 gsm_destroy_network(dlci);
2098 mutex_unlock(&dlci->mutex);
2099
2100
2101
2102
2103
2104
2105 tty_vhangup(tty);
2106
2107 tty_port_tty_set(&dlci->port, NULL);
2108 tty_kref_put(tty);
2109 }
2110 dlci->state = DLCI_CLOSED;
2111 dlci_put(dlci);
2112 }
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128 static void gsm_queue(struct gsm_mux *gsm)
2129 {
2130 struct gsm_dlci *dlci;
2131 u8 cr;
2132 int address;
2133
2134 if (gsm->fcs != GOOD_FCS) {
2135 gsm->bad_fcs++;
2136 if (debug & 4)
2137 pr_debug("BAD FCS %02x\n", gsm->fcs);
2138 return;
2139 }
2140 address = gsm->address >> 1;
2141 if (address >= NUM_DLCI)
2142 goto invalid;
2143
2144 cr = gsm->address & 1;
2145 cr ^= gsm->initiator ? 0 : 1;
2146
2147 gsm_print_packet("<--", address, cr, gsm->control, gsm->buf, gsm->len);
2148
2149 dlci = gsm->dlci[address];
2150
2151 switch (gsm->control) {
2152 case SABM|PF:
2153 if (cr == 1)
2154 goto invalid;
2155 if (dlci == NULL)
2156 dlci = gsm_dlci_alloc(gsm, address);
2157 if (dlci == NULL)
2158 return;
2159 if (dlci->dead)
2160 gsm_response(gsm, address, DM|PF);
2161 else {
2162 gsm_response(gsm, address, UA|PF);
2163 gsm_dlci_open(dlci);
2164 }
2165 break;
2166 case DISC|PF:
2167 if (cr == 1)
2168 goto invalid;
2169 if (dlci == NULL || dlci->state == DLCI_CLOSED) {
2170 gsm_response(gsm, address, DM|PF);
2171 return;
2172 }
2173
2174 gsm_response(gsm, address, UA|PF);
2175 gsm_dlci_close(dlci);
2176 break;
2177 case UA|PF:
2178 if (cr == 0 || dlci == NULL)
2179 break;
2180 switch (dlci->state) {
2181 case DLCI_CLOSING:
2182 gsm_dlci_close(dlci);
2183 break;
2184 case DLCI_OPENING:
2185 gsm_dlci_open(dlci);
2186 break;
2187 default:
2188 pr_debug("%s: unhandled state: %d\n", __func__,
2189 dlci->state);
2190 break;
2191 }
2192 break;
2193 case DM:
2194 case DM|PF:
2195 if (cr)
2196 goto invalid;
2197 if (dlci == NULL)
2198 return;
2199 gsm_dlci_close(dlci);
2200 break;
2201 case UI:
2202 case UI|PF:
2203 case UIH:
2204 case UIH|PF:
2205 if (dlci == NULL || dlci->state != DLCI_OPEN) {
2206 gsm_response(gsm, address, DM|PF);
2207 return;
2208 }
2209 dlci->data(dlci, gsm->buf, gsm->len);
2210 break;
2211 default:
2212 goto invalid;
2213 }
2214 return;
2215 invalid:
2216 gsm->malformed++;
2217 return;
2218 }
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229 static void gsm0_receive(struct gsm_mux *gsm, unsigned char c)
2230 {
2231 unsigned int len;
2232
2233 switch (gsm->state) {
2234 case GSM_SEARCH:
2235 if (c == GSM0_SOF) {
2236 gsm->state = GSM_ADDRESS;
2237 gsm->address = 0;
2238 gsm->len = 0;
2239 gsm->fcs = INIT_FCS;
2240 }
2241 break;
2242 case GSM_ADDRESS:
2243 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
2244 if (gsm_read_ea(&gsm->address, c))
2245 gsm->state = GSM_CONTROL;
2246 break;
2247 case GSM_CONTROL:
2248 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
2249 gsm->control = c;
2250 gsm->state = GSM_LEN0;
2251 break;
2252 case GSM_LEN0:
2253 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
2254 if (gsm_read_ea(&gsm->len, c)) {
2255 if (gsm->len > gsm->mru) {
2256 gsm->bad_size++;
2257 gsm->state = GSM_SEARCH;
2258 break;
2259 }
2260 gsm->count = 0;
2261 if (!gsm->len)
2262 gsm->state = GSM_FCS;
2263 else
2264 gsm->state = GSM_DATA;
2265 break;
2266 }
2267 gsm->state = GSM_LEN1;
2268 break;
2269 case GSM_LEN1:
2270 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
2271 len = c;
2272 gsm->len |= len << 7;
2273 if (gsm->len > gsm->mru) {
2274 gsm->bad_size++;
2275 gsm->state = GSM_SEARCH;
2276 break;
2277 }
2278 gsm->count = 0;
2279 if (!gsm->len)
2280 gsm->state = GSM_FCS;
2281 else
2282 gsm->state = GSM_DATA;
2283 break;
2284 case GSM_DATA:
2285 gsm->buf[gsm->count++] = c;
2286 if (gsm->count == gsm->len) {
2287
2288 if ((gsm->control & ~PF) != UIH) {
2289 gsm->fcs = gsm_fcs_add_block(gsm->fcs, gsm->buf,
2290 gsm->count);
2291 }
2292 gsm->state = GSM_FCS;
2293 }
2294 break;
2295 case GSM_FCS:
2296 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
2297 gsm->state = GSM_SSOF;
2298 break;
2299 case GSM_SSOF:
2300 gsm->state = GSM_SEARCH;
2301 if (c == GSM0_SOF)
2302 gsm_queue(gsm);
2303 else
2304 gsm->bad_size++;
2305 break;
2306 default:
2307 pr_debug("%s: unhandled state: %d\n", __func__, gsm->state);
2308 break;
2309 }
2310 }
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320 static void gsm1_receive(struct gsm_mux *gsm, unsigned char c)
2321 {
2322
2323 if ((c & ISO_IEC_646_MASK) == XON) {
2324 gsm->constipated = true;
2325 return;
2326 } else if ((c & ISO_IEC_646_MASK) == XOFF) {
2327 gsm->constipated = false;
2328
2329 gsmld_write_trigger(gsm);
2330 return;
2331 }
2332 if (c == GSM1_SOF) {
2333
2334 if (gsm->state == GSM_DATA) {
2335 if (gsm->count < 1) {
2336
2337 gsm->malformed++;
2338 gsm->state = GSM_START;
2339 return;
2340 }
2341
2342 gsm->count--;
2343 if ((gsm->control & ~PF) != UIH) {
2344
2345
2346
2347 gsm->fcs = gsm_fcs_add_block(gsm->fcs, gsm->buf,
2348 gsm->count);
2349 }
2350
2351 gsm->fcs = gsm_fcs_add(gsm->fcs, gsm->buf[gsm->count]);
2352 gsm->len = gsm->count;
2353 gsm_queue(gsm);
2354 gsm->state = GSM_START;
2355 return;
2356 }
2357
2358 if (gsm->state != GSM_START) {
2359 if (gsm->state != GSM_SEARCH)
2360 gsm->malformed++;
2361 gsm->state = GSM_START;
2362 }
2363
2364
2365 return;
2366 }
2367
2368 if (c == GSM1_ESCAPE) {
2369 gsm->escape = true;
2370 return;
2371 }
2372
2373
2374 if (gsm->state == GSM_SEARCH)
2375 return;
2376
2377 if (gsm->escape) {
2378 c ^= GSM1_ESCAPE_BITS;
2379 gsm->escape = false;
2380 }
2381 switch (gsm->state) {
2382 case GSM_START:
2383 gsm->address = 0;
2384 gsm->state = GSM_ADDRESS;
2385 gsm->fcs = INIT_FCS;
2386 fallthrough;
2387 case GSM_ADDRESS:
2388 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
2389 if (gsm_read_ea(&gsm->address, c))
2390 gsm->state = GSM_CONTROL;
2391 break;
2392 case GSM_CONTROL:
2393 gsm->fcs = gsm_fcs_add(gsm->fcs, c);
2394 gsm->control = c;
2395 gsm->count = 0;
2396 gsm->state = GSM_DATA;
2397 break;
2398 case GSM_DATA:
2399 if (gsm->count > gsm->mru) {
2400 gsm->state = GSM_OVERRUN;
2401 gsm->bad_size++;
2402 } else
2403 gsm->buf[gsm->count++] = c;
2404 break;
2405 case GSM_OVERRUN:
2406 break;
2407 default:
2408 pr_debug("%s: unhandled state: %d\n", __func__, gsm->state);
2409 break;
2410 }
2411 }
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423 static void gsm_error(struct gsm_mux *gsm)
2424 {
2425 gsm->state = GSM_SEARCH;
2426 gsm->io_error++;
2427 }
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439 static void gsm_cleanup_mux(struct gsm_mux *gsm, bool disc)
2440 {
2441 int i;
2442 struct gsm_dlci *dlci = gsm->dlci[0];
2443 struct gsm_msg *txq, *ntxq;
2444
2445 gsm->dead = true;
2446 mutex_lock(&gsm->mutex);
2447
2448 if (dlci) {
2449 if (disc && dlci->state != DLCI_CLOSED) {
2450 gsm_dlci_begin_close(dlci);
2451 wait_event(gsm->event, dlci->state == DLCI_CLOSED);
2452 }
2453 dlci->dead = true;
2454 }
2455
2456
2457 cancel_delayed_work_sync(&gsm->kick_timeout);
2458 del_timer_sync(&gsm->t2_timer);
2459
2460
2461 flush_work(&gsm->tx_work);
2462
2463
2464 if (gsm->has_devices) {
2465 gsm_unregister_devices(gsm_tty_driver, gsm->num);
2466 gsm->has_devices = false;
2467 }
2468 for (i = NUM_DLCI - 1; i >= 0; i--)
2469 if (gsm->dlci[i])
2470 gsm_dlci_release(gsm->dlci[i]);
2471 mutex_unlock(&gsm->mutex);
2472
2473 tty_ldisc_flush(gsm->tty);
2474 list_for_each_entry_safe(txq, ntxq, &gsm->tx_ctrl_list, list)
2475 kfree(txq);
2476 INIT_LIST_HEAD(&gsm->tx_ctrl_list);
2477 list_for_each_entry_safe(txq, ntxq, &gsm->tx_data_list, list)
2478 kfree(txq);
2479 INIT_LIST_HEAD(&gsm->tx_data_list);
2480 }
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491 static int gsm_activate_mux(struct gsm_mux *gsm)
2492 {
2493 struct gsm_dlci *dlci;
2494 int ret;
2495
2496 dlci = gsm_dlci_alloc(gsm, 0);
2497 if (dlci == NULL)
2498 return -ENOMEM;
2499
2500 if (gsm->encoding == 0)
2501 gsm->receive = gsm0_receive;
2502 else
2503 gsm->receive = gsm1_receive;
2504
2505 ret = gsm_register_devices(gsm_tty_driver, gsm->num);
2506 if (ret)
2507 return ret;
2508
2509 gsm->has_devices = true;
2510 gsm->dead = false;
2511 return 0;
2512 }
2513
2514
2515
2516
2517
2518
2519
2520 static void gsm_free_mux(struct gsm_mux *gsm)
2521 {
2522 int i;
2523
2524 for (i = 0; i < MAX_MUX; i++) {
2525 if (gsm == gsm_mux[i]) {
2526 gsm_mux[i] = NULL;
2527 break;
2528 }
2529 }
2530 mutex_destroy(&gsm->tx_mutex);
2531 mutex_destroy(&gsm->mutex);
2532 kfree(gsm->txframe);
2533 kfree(gsm->buf);
2534 kfree(gsm);
2535 }
2536
2537
2538
2539
2540
2541
2542
2543 static void gsm_free_muxr(struct kref *ref)
2544 {
2545 struct gsm_mux *gsm = container_of(ref, struct gsm_mux, ref);
2546 gsm_free_mux(gsm);
2547 }
2548
2549 static inline void mux_get(struct gsm_mux *gsm)
2550 {
2551 unsigned long flags;
2552
2553 spin_lock_irqsave(&gsm_mux_lock, flags);
2554 kref_get(&gsm->ref);
2555 spin_unlock_irqrestore(&gsm_mux_lock, flags);
2556 }
2557
2558 static inline void mux_put(struct gsm_mux *gsm)
2559 {
2560 unsigned long flags;
2561
2562 spin_lock_irqsave(&gsm_mux_lock, flags);
2563 kref_put(&gsm->ref, gsm_free_muxr);
2564 spin_unlock_irqrestore(&gsm_mux_lock, flags);
2565 }
2566
2567 static inline unsigned int mux_num_to_base(struct gsm_mux *gsm)
2568 {
2569 return gsm->num * NUM_DLCI;
2570 }
2571
2572 static inline unsigned int mux_line_to_num(unsigned int line)
2573 {
2574 return line / NUM_DLCI;
2575 }
2576
2577
2578
2579
2580
2581
2582
2583 static struct gsm_mux *gsm_alloc_mux(void)
2584 {
2585 int i;
2586 struct gsm_mux *gsm = kzalloc(sizeof(struct gsm_mux), GFP_KERNEL);
2587 if (gsm == NULL)
2588 return NULL;
2589 gsm->buf = kmalloc(MAX_MRU + 1, GFP_KERNEL);
2590 if (gsm->buf == NULL) {
2591 kfree(gsm);
2592 return NULL;
2593 }
2594 gsm->txframe = kmalloc(2 * (MAX_MTU + PROT_OVERHEAD - 1), GFP_KERNEL);
2595 if (gsm->txframe == NULL) {
2596 kfree(gsm->buf);
2597 kfree(gsm);
2598 return NULL;
2599 }
2600 spin_lock_init(&gsm->lock);
2601 mutex_init(&gsm->mutex);
2602 mutex_init(&gsm->tx_mutex);
2603 kref_init(&gsm->ref);
2604 INIT_LIST_HEAD(&gsm->tx_ctrl_list);
2605 INIT_LIST_HEAD(&gsm->tx_data_list);
2606 INIT_DELAYED_WORK(&gsm->kick_timeout, gsm_kick_timeout);
2607 timer_setup(&gsm->t2_timer, gsm_control_retransmit, 0);
2608 INIT_WORK(&gsm->tx_work, gsmld_write_task);
2609 init_waitqueue_head(&gsm->event);
2610 spin_lock_init(&gsm->control_lock);
2611
2612 gsm->t1 = T1;
2613 gsm->t2 = T2;
2614 gsm->n2 = N2;
2615 gsm->ftype = UIH;
2616 gsm->adaption = 1;
2617 gsm->encoding = 1;
2618 gsm->mru = 64;
2619 gsm->mtu = 64;
2620 gsm->dead = true;
2621
2622
2623
2624
2625 spin_lock(&gsm_mux_lock);
2626 for (i = 0; i < MAX_MUX; i++) {
2627 if (!gsm_mux[i]) {
2628 gsm_mux[i] = gsm;
2629 gsm->num = i;
2630 break;
2631 }
2632 }
2633 spin_unlock(&gsm_mux_lock);
2634 if (i == MAX_MUX) {
2635 mutex_destroy(&gsm->tx_mutex);
2636 mutex_destroy(&gsm->mutex);
2637 kfree(gsm->txframe);
2638 kfree(gsm->buf);
2639 kfree(gsm);
2640 return NULL;
2641 }
2642
2643 return gsm;
2644 }
2645
2646 static void gsm_copy_config_values(struct gsm_mux *gsm,
2647 struct gsm_config *c)
2648 {
2649 memset(c, 0, sizeof(*c));
2650 c->adaption = gsm->adaption;
2651 c->encapsulation = gsm->encoding;
2652 c->initiator = gsm->initiator;
2653 c->t1 = gsm->t1;
2654 c->t2 = gsm->t2;
2655 c->t3 = 0;
2656 c->n2 = gsm->n2;
2657 if (gsm->ftype == UIH)
2658 c->i = 1;
2659 else
2660 c->i = 2;
2661 pr_debug("Ftype %d i %d\n", gsm->ftype, c->i);
2662 c->mru = gsm->mru;
2663 c->mtu = gsm->mtu;
2664 c->k = 0;
2665 }
2666
2667 static int gsm_config(struct gsm_mux *gsm, struct gsm_config *c)
2668 {
2669 int ret = 0;
2670 int need_close = 0;
2671 int need_restart = 0;
2672
2673
2674 if ((c->adaption != 1 && c->adaption != 2) || c->k)
2675 return -EOPNOTSUPP;
2676
2677 if (c->mru > MAX_MRU || c->mtu > MAX_MTU || c->mru < 8 || c->mtu < 8)
2678 return -EINVAL;
2679 if (c->n2 > 255)
2680 return -EINVAL;
2681 if (c->encapsulation > 1)
2682 return -EINVAL;
2683 if (c->initiator > 1)
2684 return -EINVAL;
2685 if (c->i == 0 || c->i > 2)
2686 return -EINVAL;
2687
2688
2689
2690
2691
2692 if (c->t1 != 0 && c->t1 != gsm->t1)
2693 need_restart = 1;
2694 if (c->t2 != 0 && c->t2 != gsm->t2)
2695 need_restart = 1;
2696 if (c->encapsulation != gsm->encoding)
2697 need_restart = 1;
2698 if (c->adaption != gsm->adaption)
2699 need_restart = 1;
2700
2701 if (c->initiator != gsm->initiator)
2702 need_close = 1;
2703 if (c->mru != gsm->mru)
2704 need_restart = 1;
2705 if (c->mtu != gsm->mtu)
2706 need_restart = 1;
2707
2708
2709
2710
2711
2712
2713 if (need_close || need_restart)
2714 gsm_cleanup_mux(gsm, true);
2715
2716 gsm->initiator = c->initiator;
2717 gsm->mru = c->mru;
2718 gsm->mtu = c->mtu;
2719 gsm->encoding = c->encapsulation;
2720 gsm->adaption = c->adaption;
2721 gsm->n2 = c->n2;
2722
2723 if (c->i == 1)
2724 gsm->ftype = UIH;
2725 else if (c->i == 2)
2726 gsm->ftype = UI;
2727
2728 if (c->t1)
2729 gsm->t1 = c->t1;
2730 if (c->t2)
2731 gsm->t2 = c->t2;
2732
2733
2734
2735
2736
2737 if (gsm->dead) {
2738 ret = gsm_activate_mux(gsm);
2739 if (ret)
2740 return ret;
2741 if (gsm->initiator)
2742 gsm_dlci_begin_open(gsm->dlci[0]);
2743 }
2744 return 0;
2745 }
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757 static int gsmld_output(struct gsm_mux *gsm, u8 *data, int len)
2758 {
2759 if (tty_write_room(gsm->tty) < len) {
2760 set_bit(TTY_DO_WRITE_WAKEUP, &gsm->tty->flags);
2761 return -ENOSPC;
2762 }
2763 if (debug & 4)
2764 gsm_hex_dump_bytes(__func__, data, len);
2765 return gsm->tty->ops->write(gsm->tty, data, len);
2766 }
2767
2768
2769
2770
2771
2772
2773 static void gsmld_write_trigger(struct gsm_mux *gsm)
2774 {
2775 if (!gsm || !gsm->dlci[0] || gsm->dlci[0]->dead)
2776 return;
2777 schedule_work(&gsm->tx_work);
2778 }
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788 static void gsmld_write_task(struct work_struct *work)
2789 {
2790 struct gsm_mux *gsm = container_of(work, struct gsm_mux, tx_work);
2791 int i, ret;
2792
2793
2794
2795
2796 ret = -ENODEV;
2797 mutex_lock(&gsm->tx_mutex);
2798 if (gsm->tty)
2799 ret = gsm_data_kick(gsm);
2800 mutex_unlock(&gsm->tx_mutex);
2801
2802 if (ret >= 0)
2803 for (i = 0; i < NUM_DLCI; i++)
2804 if (gsm->dlci[i])
2805 tty_port_tty_wakeup(&gsm->dlci[i]->port);
2806 }
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818 static void gsmld_attach_gsm(struct tty_struct *tty, struct gsm_mux *gsm)
2819 {
2820 gsm->tty = tty_kref_get(tty);
2821
2822 gsm->old_c_iflag = tty->termios.c_iflag;
2823 tty->termios.c_iflag &= (IXON | IXOFF);
2824 }
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834 static void gsmld_detach_gsm(struct tty_struct *tty, struct gsm_mux *gsm)
2835 {
2836 WARN_ON(tty != gsm->tty);
2837
2838 gsm->tty->termios.c_iflag = gsm->old_c_iflag;
2839 tty_kref_put(gsm->tty);
2840 gsm->tty = NULL;
2841 }
2842
2843 static void gsmld_receive_buf(struct tty_struct *tty, const unsigned char *cp,
2844 const char *fp, int count)
2845 {
2846 struct gsm_mux *gsm = tty->disc_data;
2847 char flags = TTY_NORMAL;
2848
2849 if (debug & 4)
2850 gsm_hex_dump_bytes(__func__, cp, count);
2851
2852 for (; count; count--, cp++) {
2853 if (fp)
2854 flags = *fp++;
2855 switch (flags) {
2856 case TTY_NORMAL:
2857 if (gsm->receive)
2858 gsm->receive(gsm, *cp);
2859 break;
2860 case TTY_OVERRUN:
2861 case TTY_BREAK:
2862 case TTY_PARITY:
2863 case TTY_FRAME:
2864 gsm_error(gsm);
2865 break;
2866 default:
2867 WARN_ONCE(1, "%s: unknown flag %d\n",
2868 tty_name(tty), flags);
2869 break;
2870 }
2871 }
2872
2873
2874 }
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885 static void gsmld_flush_buffer(struct tty_struct *tty)
2886 {
2887 }
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899 static void gsmld_close(struct tty_struct *tty)
2900 {
2901 struct gsm_mux *gsm = tty->disc_data;
2902
2903
2904
2905
2906
2907 gsm_cleanup_mux(gsm, false);
2908
2909 gsmld_detach_gsm(tty, gsm);
2910
2911 gsmld_flush_buffer(tty);
2912
2913 mux_put(gsm);
2914 }
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926 static int gsmld_open(struct tty_struct *tty)
2927 {
2928 struct gsm_mux *gsm;
2929
2930 if (tty->ops->write == NULL)
2931 return -EINVAL;
2932
2933
2934 gsm = gsm_alloc_mux();
2935 if (gsm == NULL)
2936 return -ENOMEM;
2937
2938 tty->disc_data = gsm;
2939 tty->receive_room = 65536;
2940
2941
2942 gsm->encoding = 1;
2943
2944 gsmld_attach_gsm(tty, gsm);
2945
2946 return 0;
2947 }
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958 static void gsmld_write_wakeup(struct tty_struct *tty)
2959 {
2960 struct gsm_mux *gsm = tty->disc_data;
2961
2962
2963 gsmld_write_trigger(gsm);
2964 }
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983 static ssize_t gsmld_read(struct tty_struct *tty, struct file *file,
2984 unsigned char *buf, size_t nr,
2985 void **cookie, unsigned long offset)
2986 {
2987 return -EOPNOTSUPP;
2988 }
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004 static ssize_t gsmld_write(struct tty_struct *tty, struct file *file,
3005 const unsigned char *buf, size_t nr)
3006 {
3007 struct gsm_mux *gsm = tty->disc_data;
3008 int space;
3009 int ret;
3010
3011 if (!gsm)
3012 return -ENODEV;
3013
3014 ret = -ENOBUFS;
3015 mutex_lock(&gsm->tx_mutex);
3016 space = tty_write_room(tty);
3017 if (space >= nr)
3018 ret = tty->ops->write(tty, buf, nr);
3019 else
3020 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
3021 mutex_unlock(&gsm->tx_mutex);
3022
3023 return ret;
3024 }
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040 static __poll_t gsmld_poll(struct tty_struct *tty, struct file *file,
3041 poll_table *wait)
3042 {
3043 __poll_t mask = 0;
3044 struct gsm_mux *gsm = tty->disc_data;
3045
3046 poll_wait(file, &tty->read_wait, wait);
3047 poll_wait(file, &tty->write_wait, wait);
3048
3049 if (gsm->dead)
3050 mask |= EPOLLHUP;
3051 if (tty_hung_up_p(file))
3052 mask |= EPOLLHUP;
3053 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
3054 mask |= EPOLLHUP;
3055 if (!tty_is_writelocked(tty) && tty_write_room(tty) > 0)
3056 mask |= EPOLLOUT | EPOLLWRNORM;
3057 return mask;
3058 }
3059
3060 static int gsmld_ioctl(struct tty_struct *tty, unsigned int cmd,
3061 unsigned long arg)
3062 {
3063 struct gsm_config c;
3064 struct gsm_mux *gsm = tty->disc_data;
3065 unsigned int base;
3066
3067 switch (cmd) {
3068 case GSMIOC_GETCONF:
3069 gsm_copy_config_values(gsm, &c);
3070 if (copy_to_user((void __user *)arg, &c, sizeof(c)))
3071 return -EFAULT;
3072 return 0;
3073 case GSMIOC_SETCONF:
3074 if (copy_from_user(&c, (void __user *)arg, sizeof(c)))
3075 return -EFAULT;
3076 return gsm_config(gsm, &c);
3077 case GSMIOC_GETFIRST:
3078 base = mux_num_to_base(gsm);
3079 return put_user(base + 1, (__u32 __user *)arg);
3080 default:
3081 return n_tty_ioctl_helper(tty, cmd, arg);
3082 }
3083 }
3084
3085
3086
3087
3088
3089
3090 static int gsm_mux_net_open(struct net_device *net)
3091 {
3092 pr_debug("%s called\n", __func__);
3093 netif_start_queue(net);
3094 return 0;
3095 }
3096
3097 static int gsm_mux_net_close(struct net_device *net)
3098 {
3099 netif_stop_queue(net);
3100 return 0;
3101 }
3102
3103 static void dlci_net_free(struct gsm_dlci *dlci)
3104 {
3105 if (!dlci->net) {
3106 WARN_ON(1);
3107 return;
3108 }
3109 dlci->adaption = dlci->prev_adaption;
3110 dlci->data = dlci->prev_data;
3111 free_netdev(dlci->net);
3112 dlci->net = NULL;
3113 }
3114 static void net_free(struct kref *ref)
3115 {
3116 struct gsm_mux_net *mux_net;
3117 struct gsm_dlci *dlci;
3118
3119 mux_net = container_of(ref, struct gsm_mux_net, ref);
3120 dlci = mux_net->dlci;
3121
3122 if (dlci->net) {
3123 unregister_netdev(dlci->net);
3124 dlci_net_free(dlci);
3125 }
3126 }
3127
3128 static inline void muxnet_get(struct gsm_mux_net *mux_net)
3129 {
3130 kref_get(&mux_net->ref);
3131 }
3132
3133 static inline void muxnet_put(struct gsm_mux_net *mux_net)
3134 {
3135 kref_put(&mux_net->ref, net_free);
3136 }
3137
3138 static netdev_tx_t gsm_mux_net_start_xmit(struct sk_buff *skb,
3139 struct net_device *net)
3140 {
3141 struct gsm_mux_net *mux_net = netdev_priv(net);
3142 struct gsm_dlci *dlci = mux_net->dlci;
3143 muxnet_get(mux_net);
3144
3145 skb_queue_head(&dlci->skb_list, skb);
3146 net->stats.tx_packets++;
3147 net->stats.tx_bytes += skb->len;
3148 gsm_dlci_data_kick(dlci);
3149
3150 netif_trans_update(net);
3151 muxnet_put(mux_net);
3152 return NETDEV_TX_OK;
3153 }
3154
3155
3156 static void gsm_mux_net_tx_timeout(struct net_device *net, unsigned int txqueue)
3157 {
3158
3159 dev_dbg(&net->dev, "Tx timed out.\n");
3160
3161
3162 net->stats.tx_errors++;
3163 }
3164
3165 static void gsm_mux_rx_netchar(struct gsm_dlci *dlci,
3166 const unsigned char *in_buf, int size)
3167 {
3168 struct net_device *net = dlci->net;
3169 struct sk_buff *skb;
3170 struct gsm_mux_net *mux_net = netdev_priv(net);
3171 muxnet_get(mux_net);
3172
3173
3174 skb = dev_alloc_skb(size + NET_IP_ALIGN);
3175 if (!skb) {
3176
3177 net->stats.rx_dropped++;
3178 muxnet_put(mux_net);
3179 return;
3180 }
3181 skb_reserve(skb, NET_IP_ALIGN);
3182 skb_put_data(skb, in_buf, size);
3183
3184 skb->dev = net;
3185 skb->protocol = htons(ETH_P_IP);
3186
3187
3188 netif_rx(skb);
3189
3190
3191 net->stats.rx_packets++;
3192 net->stats.rx_bytes += size;
3193 muxnet_put(mux_net);
3194 return;
3195 }
3196
3197 static void gsm_mux_net_init(struct net_device *net)
3198 {
3199 static const struct net_device_ops gsm_netdev_ops = {
3200 .ndo_open = gsm_mux_net_open,
3201 .ndo_stop = gsm_mux_net_close,
3202 .ndo_start_xmit = gsm_mux_net_start_xmit,
3203 .ndo_tx_timeout = gsm_mux_net_tx_timeout,
3204 };
3205
3206 net->netdev_ops = &gsm_netdev_ops;
3207
3208
3209 net->watchdog_timeo = GSM_NET_TX_TIMEOUT;
3210 net->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
3211 net->type = ARPHRD_NONE;
3212 net->tx_queue_len = 10;
3213 }
3214
3215
3216
3217 static void gsm_destroy_network(struct gsm_dlci *dlci)
3218 {
3219 struct gsm_mux_net *mux_net;
3220
3221 pr_debug("destroy network interface\n");
3222 if (!dlci->net)
3223 return;
3224 mux_net = netdev_priv(dlci->net);
3225 muxnet_put(mux_net);
3226 }
3227
3228
3229
3230 static int gsm_create_network(struct gsm_dlci *dlci, struct gsm_netconfig *nc)
3231 {
3232 char *netname;
3233 int retval = 0;
3234 struct net_device *net;
3235 struct gsm_mux_net *mux_net;
3236
3237 if (!capable(CAP_NET_ADMIN))
3238 return -EPERM;
3239
3240
3241 if (dlci->adaption > 2)
3242 return -EBUSY;
3243
3244 if (nc->protocol != htons(ETH_P_IP))
3245 return -EPROTONOSUPPORT;
3246
3247 if (nc->adaption != 3 && nc->adaption != 4)
3248 return -EPROTONOSUPPORT;
3249
3250 pr_debug("create network interface\n");
3251
3252 netname = "gsm%d";
3253 if (nc->if_name[0] != '\0')
3254 netname = nc->if_name;
3255 net = alloc_netdev(sizeof(struct gsm_mux_net), netname,
3256 NET_NAME_UNKNOWN, gsm_mux_net_init);
3257 if (!net) {
3258 pr_err("alloc_netdev failed\n");
3259 return -ENOMEM;
3260 }
3261 net->mtu = dlci->gsm->mtu;
3262 net->min_mtu = 8;
3263 net->max_mtu = dlci->gsm->mtu;
3264 mux_net = netdev_priv(net);
3265 mux_net->dlci = dlci;
3266 kref_init(&mux_net->ref);
3267 strncpy(nc->if_name, net->name, IFNAMSIZ);
3268
3269
3270 dlci->prev_adaption = dlci->adaption;
3271 dlci->prev_data = dlci->data;
3272 dlci->adaption = nc->adaption;
3273 dlci->data = gsm_mux_rx_netchar;
3274 dlci->net = net;
3275
3276 pr_debug("register netdev\n");
3277 retval = register_netdev(net);
3278 if (retval) {
3279 pr_err("network register fail %d\n", retval);
3280 dlci_net_free(dlci);
3281 return retval;
3282 }
3283 return net->ifindex;
3284 }
3285
3286
3287 static struct tty_ldisc_ops tty_ldisc_packet = {
3288 .owner = THIS_MODULE,
3289 .num = N_GSM0710,
3290 .name = "n_gsm",
3291 .open = gsmld_open,
3292 .close = gsmld_close,
3293 .flush_buffer = gsmld_flush_buffer,
3294 .read = gsmld_read,
3295 .write = gsmld_write,
3296 .ioctl = gsmld_ioctl,
3297 .poll = gsmld_poll,
3298 .receive_buf = gsmld_receive_buf,
3299 .write_wakeup = gsmld_write_wakeup
3300 };
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315 static void gsm_modem_upd_via_data(struct gsm_dlci *dlci, u8 brk)
3316 {
3317 struct gsm_mux *gsm = dlci->gsm;
3318
3319 if (dlci->state != DLCI_OPEN || dlci->adaption != 2)
3320 return;
3321
3322 mutex_lock(&gsm->tx_mutex);
3323 gsm_dlci_modem_output(gsm, dlci, brk);
3324 mutex_unlock(&gsm->tx_mutex);
3325 }
3326
3327
3328
3329
3330
3331
3332
3333 static int gsm_modem_upd_via_msc(struct gsm_dlci *dlci, u8 brk)
3334 {
3335 u8 modembits[3];
3336 struct gsm_control *ctrl;
3337 int len = 2;
3338
3339 if (dlci->gsm->encoding != 0)
3340 return 0;
3341
3342 modembits[0] = (dlci->addr << 2) | 2 | EA;
3343 if (!brk) {
3344 modembits[1] = (gsm_encode_modem(dlci) << 1) | EA;
3345 } else {
3346 modembits[1] = gsm_encode_modem(dlci) << 1;
3347 modembits[2] = (brk << 4) | 2 | EA;
3348 len++;
3349 }
3350 ctrl = gsm_control_send(dlci->gsm, CMD_MSC, modembits, len);
3351 if (ctrl == NULL)
3352 return -ENOMEM;
3353 return gsm_control_wait(dlci->gsm, ctrl);
3354 }
3355
3356
3357
3358
3359
3360
3361
3362 static int gsm_modem_update(struct gsm_dlci *dlci, u8 brk)
3363 {
3364 if (dlci->adaption == 2) {
3365
3366 gsm_modem_upd_via_data(dlci, brk);
3367 return 0;
3368 } else if (dlci->gsm->encoding == 0) {
3369
3370 return gsm_modem_upd_via_msc(dlci, brk);
3371 }
3372
3373
3374 return -EPROTONOSUPPORT;
3375 }
3376
3377 static int gsm_carrier_raised(struct tty_port *port)
3378 {
3379 struct gsm_dlci *dlci = container_of(port, struct gsm_dlci, port);
3380 struct gsm_mux *gsm = dlci->gsm;
3381
3382
3383 if (dlci->state != DLCI_OPEN)
3384 return 0;
3385 if (debug & 2)
3386 return 1;
3387
3388
3389
3390
3391
3392 if (gsm->encoding == 0 && gsm->dlci[0]->mode == DLCI_MODE_ADM &&
3393 !dlci->modem_rx)
3394 return 1;
3395
3396 return dlci->modem_rx & TIOCM_CD;
3397 }
3398
3399 static void gsm_dtr_rts(struct tty_port *port, int onoff)
3400 {
3401 struct gsm_dlci *dlci = container_of(port, struct gsm_dlci, port);
3402 unsigned int modem_tx = dlci->modem_tx;
3403 if (onoff)
3404 modem_tx |= TIOCM_DTR | TIOCM_RTS;
3405 else
3406 modem_tx &= ~(TIOCM_DTR | TIOCM_RTS);
3407 if (modem_tx != dlci->modem_tx) {
3408 dlci->modem_tx = modem_tx;
3409 gsm_modem_update(dlci, 0);
3410 }
3411 }
3412
3413 static const struct tty_port_operations gsm_port_ops = {
3414 .carrier_raised = gsm_carrier_raised,
3415 .dtr_rts = gsm_dtr_rts,
3416 .destruct = gsm_dlci_free,
3417 };
3418
3419 static int gsmtty_install(struct tty_driver *driver, struct tty_struct *tty)
3420 {
3421 struct gsm_mux *gsm;
3422 struct gsm_dlci *dlci;
3423 unsigned int line = tty->index;
3424 unsigned int mux = mux_line_to_num(line);
3425 bool alloc = false;
3426 int ret;
3427
3428 line = line & 0x3F;
3429
3430 if (mux >= MAX_MUX)
3431 return -ENXIO;
3432
3433 if (gsm_mux[mux] == NULL)
3434 return -EUNATCH;
3435 if (line == 0 || line > 61)
3436 return -ECHRNG;
3437 gsm = gsm_mux[mux];
3438 if (gsm->dead)
3439 return -EL2HLT;
3440
3441
3442
3443
3444 mutex_lock(&gsm->mutex);
3445 if (gsm->dlci[0] && gsm->dlci[0]->state != DLCI_OPEN) {
3446 mutex_unlock(&gsm->mutex);
3447 return -EL2NSYNC;
3448 }
3449 dlci = gsm->dlci[line];
3450 if (dlci == NULL) {
3451 alloc = true;
3452 dlci = gsm_dlci_alloc(gsm, line);
3453 }
3454 if (dlci == NULL) {
3455 mutex_unlock(&gsm->mutex);
3456 return -ENOMEM;
3457 }
3458 ret = tty_port_install(&dlci->port, driver, tty);
3459 if (ret) {
3460 if (alloc)
3461 dlci_put(dlci);
3462 mutex_unlock(&gsm->mutex);
3463 return ret;
3464 }
3465
3466 dlci_get(dlci);
3467 dlci_get(gsm->dlci[0]);
3468 mux_get(gsm);
3469 tty->driver_data = dlci;
3470 mutex_unlock(&gsm->mutex);
3471
3472 return 0;
3473 }
3474
3475 static int gsmtty_open(struct tty_struct *tty, struct file *filp)
3476 {
3477 struct gsm_dlci *dlci = tty->driver_data;
3478 struct tty_port *port = &dlci->port;
3479 struct gsm_mux *gsm = dlci->gsm;
3480
3481 port->count++;
3482 tty_port_tty_set(port, tty);
3483
3484 dlci->modem_rx = 0;
3485
3486
3487 tty_port_set_initialized(port, 1);
3488
3489 if (gsm->initiator)
3490 gsm_dlci_begin_open(dlci);
3491 else
3492 gsm_dlci_set_opening(dlci);
3493
3494 return tty_port_block_til_ready(port, tty, filp);
3495 }
3496
3497 static void gsmtty_close(struct tty_struct *tty, struct file *filp)
3498 {
3499 struct gsm_dlci *dlci = tty->driver_data;
3500
3501 if (dlci == NULL)
3502 return;
3503 if (dlci->state == DLCI_CLOSED)
3504 return;
3505 mutex_lock(&dlci->mutex);
3506 gsm_destroy_network(dlci);
3507 mutex_unlock(&dlci->mutex);
3508 if (tty_port_close_start(&dlci->port, tty, filp) == 0)
3509 return;
3510 gsm_dlci_begin_close(dlci);
3511 if (tty_port_initialized(&dlci->port) && C_HUPCL(tty))
3512 tty_port_lower_dtr_rts(&dlci->port);
3513 tty_port_close_end(&dlci->port, tty);
3514 tty_port_tty_set(&dlci->port, NULL);
3515 return;
3516 }
3517
3518 static void gsmtty_hangup(struct tty_struct *tty)
3519 {
3520 struct gsm_dlci *dlci = tty->driver_data;
3521 if (dlci->state == DLCI_CLOSED)
3522 return;
3523 tty_port_hangup(&dlci->port);
3524 gsm_dlci_begin_close(dlci);
3525 }
3526
3527 static int gsmtty_write(struct tty_struct *tty, const unsigned char *buf,
3528 int len)
3529 {
3530 int sent;
3531 struct gsm_dlci *dlci = tty->driver_data;
3532 if (dlci->state == DLCI_CLOSED)
3533 return -EINVAL;
3534
3535 sent = kfifo_in_locked(&dlci->fifo, buf, len, &dlci->lock);
3536
3537 gsm_dlci_data_kick(dlci);
3538 return sent;
3539 }
3540
3541 static unsigned int gsmtty_write_room(struct tty_struct *tty)
3542 {
3543 struct gsm_dlci *dlci = tty->driver_data;
3544 if (dlci->state == DLCI_CLOSED)
3545 return 0;
3546 return kfifo_avail(&dlci->fifo);
3547 }
3548
3549 static unsigned int gsmtty_chars_in_buffer(struct tty_struct *tty)
3550 {
3551 struct gsm_dlci *dlci = tty->driver_data;
3552 if (dlci->state == DLCI_CLOSED)
3553 return 0;
3554 return kfifo_len(&dlci->fifo);
3555 }
3556
3557 static void gsmtty_flush_buffer(struct tty_struct *tty)
3558 {
3559 struct gsm_dlci *dlci = tty->driver_data;
3560 unsigned long flags;
3561
3562 if (dlci->state == DLCI_CLOSED)
3563 return;
3564
3565
3566
3567
3568 spin_lock_irqsave(&dlci->lock, flags);
3569 kfifo_reset(&dlci->fifo);
3570 spin_unlock_irqrestore(&dlci->lock, flags);
3571
3572 }
3573
3574 static void gsmtty_wait_until_sent(struct tty_struct *tty, int timeout)
3575 {
3576
3577
3578
3579 }
3580
3581 static int gsmtty_tiocmget(struct tty_struct *tty)
3582 {
3583 struct gsm_dlci *dlci = tty->driver_data;
3584 if (dlci->state == DLCI_CLOSED)
3585 return -EINVAL;
3586 return dlci->modem_rx;
3587 }
3588
3589 static int gsmtty_tiocmset(struct tty_struct *tty,
3590 unsigned int set, unsigned int clear)
3591 {
3592 struct gsm_dlci *dlci = tty->driver_data;
3593 unsigned int modem_tx = dlci->modem_tx;
3594
3595 if (dlci->state == DLCI_CLOSED)
3596 return -EINVAL;
3597 modem_tx &= ~clear;
3598 modem_tx |= set;
3599
3600 if (modem_tx != dlci->modem_tx) {
3601 dlci->modem_tx = modem_tx;
3602 return gsm_modem_update(dlci, 0);
3603 }
3604 return 0;
3605 }
3606
3607
3608 static int gsmtty_ioctl(struct tty_struct *tty,
3609 unsigned int cmd, unsigned long arg)
3610 {
3611 struct gsm_dlci *dlci = tty->driver_data;
3612 struct gsm_netconfig nc;
3613 int index;
3614
3615 if (dlci->state == DLCI_CLOSED)
3616 return -EINVAL;
3617 switch (cmd) {
3618 case GSMIOC_ENABLE_NET:
3619 if (copy_from_user(&nc, (void __user *)arg, sizeof(nc)))
3620 return -EFAULT;
3621 nc.if_name[IFNAMSIZ-1] = '\0';
3622
3623 mutex_lock(&dlci->mutex);
3624 index = gsm_create_network(dlci, &nc);
3625 mutex_unlock(&dlci->mutex);
3626 if (copy_to_user((void __user *)arg, &nc, sizeof(nc)))
3627 return -EFAULT;
3628 return index;
3629 case GSMIOC_DISABLE_NET:
3630 if (!capable(CAP_NET_ADMIN))
3631 return -EPERM;
3632 mutex_lock(&dlci->mutex);
3633 gsm_destroy_network(dlci);
3634 mutex_unlock(&dlci->mutex);
3635 return 0;
3636 default:
3637 return -ENOIOCTLCMD;
3638 }
3639 }
3640
3641 static void gsmtty_set_termios(struct tty_struct *tty, struct ktermios *old)
3642 {
3643 struct gsm_dlci *dlci = tty->driver_data;
3644 if (dlci->state == DLCI_CLOSED)
3645 return;
3646
3647
3648
3649
3650
3651 tty_termios_copy_hw(&tty->termios, old);
3652 }
3653
3654 static void gsmtty_throttle(struct tty_struct *tty)
3655 {
3656 struct gsm_dlci *dlci = tty->driver_data;
3657 if (dlci->state == DLCI_CLOSED)
3658 return;
3659 if (C_CRTSCTS(tty))
3660 dlci->modem_tx &= ~TIOCM_RTS;
3661 dlci->throttled = true;
3662
3663 gsm_modem_update(dlci, 0);
3664 }
3665
3666 static void gsmtty_unthrottle(struct tty_struct *tty)
3667 {
3668 struct gsm_dlci *dlci = tty->driver_data;
3669 if (dlci->state == DLCI_CLOSED)
3670 return;
3671 if (C_CRTSCTS(tty))
3672 dlci->modem_tx |= TIOCM_RTS;
3673 dlci->throttled = false;
3674
3675 gsm_modem_update(dlci, 0);
3676 }
3677
3678 static int gsmtty_break_ctl(struct tty_struct *tty, int state)
3679 {
3680 struct gsm_dlci *dlci = tty->driver_data;
3681 int encode = 0;
3682 if (dlci->state == DLCI_CLOSED)
3683 return -EINVAL;
3684
3685 if (state == -1)
3686
3687 encode = 0x0F;
3688 else if (state > 0) {
3689 encode = state / 200;
3690 if (encode > 0x0F)
3691 encode = 0x0F;
3692 }
3693 return gsm_modem_update(dlci, encode);
3694 }
3695
3696 static void gsmtty_cleanup(struct tty_struct *tty)
3697 {
3698 struct gsm_dlci *dlci = tty->driver_data;
3699 struct gsm_mux *gsm = dlci->gsm;
3700
3701 dlci_put(dlci);
3702 dlci_put(gsm->dlci[0]);
3703 mux_put(gsm);
3704 }
3705
3706
3707 static const struct tty_operations gsmtty_ops = {
3708 .install = gsmtty_install,
3709 .open = gsmtty_open,
3710 .close = gsmtty_close,
3711 .write = gsmtty_write,
3712 .write_room = gsmtty_write_room,
3713 .chars_in_buffer = gsmtty_chars_in_buffer,
3714 .flush_buffer = gsmtty_flush_buffer,
3715 .ioctl = gsmtty_ioctl,
3716 .throttle = gsmtty_throttle,
3717 .unthrottle = gsmtty_unthrottle,
3718 .set_termios = gsmtty_set_termios,
3719 .hangup = gsmtty_hangup,
3720 .wait_until_sent = gsmtty_wait_until_sent,
3721 .tiocmget = gsmtty_tiocmget,
3722 .tiocmset = gsmtty_tiocmset,
3723 .break_ctl = gsmtty_break_ctl,
3724 .cleanup = gsmtty_cleanup,
3725 };
3726
3727
3728
3729 static int __init gsm_init(void)
3730 {
3731
3732 int status = tty_register_ldisc(&tty_ldisc_packet);
3733 if (status != 0) {
3734 pr_err("n_gsm: can't register line discipline (err = %d)\n",
3735 status);
3736 return status;
3737 }
3738
3739 gsm_tty_driver = tty_alloc_driver(256, TTY_DRIVER_REAL_RAW |
3740 TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_HARDWARE_BREAK);
3741 if (IS_ERR(gsm_tty_driver)) {
3742 pr_err("gsm_init: tty allocation failed.\n");
3743 status = PTR_ERR(gsm_tty_driver);
3744 goto err_unreg_ldisc;
3745 }
3746 gsm_tty_driver->driver_name = "gsmtty";
3747 gsm_tty_driver->name = "gsmtty";
3748 gsm_tty_driver->major = 0;
3749 gsm_tty_driver->minor_start = 0;
3750 gsm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
3751 gsm_tty_driver->subtype = SERIAL_TYPE_NORMAL;
3752 gsm_tty_driver->init_termios = tty_std_termios;
3753
3754 gsm_tty_driver->init_termios.c_lflag &= ~ECHO;
3755 tty_set_operations(gsm_tty_driver, &gsmtty_ops);
3756
3757 if (tty_register_driver(gsm_tty_driver)) {
3758 pr_err("gsm_init: tty registration failed.\n");
3759 status = -EBUSY;
3760 goto err_put_driver;
3761 }
3762 pr_debug("gsm_init: loaded as %d,%d.\n",
3763 gsm_tty_driver->major, gsm_tty_driver->minor_start);
3764 return 0;
3765 err_put_driver:
3766 tty_driver_kref_put(gsm_tty_driver);
3767 err_unreg_ldisc:
3768 tty_unregister_ldisc(&tty_ldisc_packet);
3769 return status;
3770 }
3771
3772 static void __exit gsm_exit(void)
3773 {
3774 tty_unregister_ldisc(&tty_ldisc_packet);
3775 tty_unregister_driver(gsm_tty_driver);
3776 tty_driver_kref_put(gsm_tty_driver);
3777 }
3778
3779 module_init(gsm_init);
3780 module_exit(gsm_exit);
3781
3782
3783 MODULE_LICENSE("GPL");
3784 MODULE_ALIAS_LDISC(N_GSM0710);