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