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