/drivers/tty/n_gsm.c

https://bitbucket.org/slukk/jb-tsm-kernel-4.2 · C · 2806 lines · 1745 code · 287 blank · 774 comment · 383 complexity · 9ad1cacc539b2242df4e09c61f8a0c53 MD5 · raw file

Large files are truncated click here to view the full file

  1. /*
  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. #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 <linux/gsmmux.h>
  61. static int debug;
  62. module_param(debug, int, 0600);
  63. #define T1 (HZ/10)
  64. #define T2 (HZ/3)
  65. #define N2 3
  66. /* Use long timers for testing at low speed with debug on */
  67. #ifdef DEBUG_TIMING
  68. #define T1 HZ
  69. #define T2 (2 * HZ)
  70. #endif
  71. /*
  72. * Semi-arbitrary buffer size limits. 0710 is normally run with 32-64 byte
  73. * limits so this is plenty
  74. */
  75. #define MAX_MRU 512
  76. #define MAX_MTU 512
  77. /*
  78. * Each block of data we have queued to go out is in the form of
  79. * a gsm_msg which holds everything we need in a link layer independent
  80. * format
  81. */
  82. struct gsm_msg {
  83. struct gsm_msg *next;
  84. u8 addr; /* DLCI address + flags */
  85. u8 ctrl; /* Control byte + flags */
  86. unsigned int len; /* Length of data block (can be zero) */
  87. unsigned char *data; /* Points into buffer but not at the start */
  88. unsigned char buffer[0];
  89. };
  90. /*
  91. * Each active data link has a gsm_dlci structure associated which ties
  92. * the link layer to an optional tty (if the tty side is open). To avoid
  93. * complexity right now these are only ever freed up when the mux is
  94. * shut down.
  95. *
  96. * At the moment we don't free DLCI objects until the mux is torn down
  97. * this avoid object life time issues but might be worth review later.
  98. */
  99. struct gsm_dlci {
  100. struct gsm_mux *gsm;
  101. int addr;
  102. int state;
  103. #define DLCI_CLOSED 0
  104. #define DLCI_OPENING 1 /* Sending SABM not seen UA */
  105. #define DLCI_OPEN 2 /* SABM/UA complete */
  106. #define DLCI_CLOSING 3 /* Sending DISC not seen UA/DM */
  107. /* Link layer */
  108. spinlock_t lock; /* Protects the internal state */
  109. struct timer_list t1; /* Retransmit timer for SABM and UA */
  110. int retries;
  111. /* Uplink tty if active */
  112. struct tty_port port; /* The tty bound to this DLCI if there is one */
  113. struct kfifo *fifo; /* Queue fifo for the DLCI */
  114. struct kfifo _fifo; /* For new fifo API porting only */
  115. int adaption; /* Adaption layer in use */
  116. u32 modem_rx; /* Our incoming virtual modem lines */
  117. u32 modem_tx; /* Our outgoing modem lines */
  118. int dead; /* Refuse re-open */
  119. /* Flow control */
  120. int throttled; /* Private copy of throttle state */
  121. int constipated; /* Throttle status for outgoing */
  122. /* Packetised I/O */
  123. struct sk_buff *skb; /* Frame being sent */
  124. struct sk_buff_head skb_list; /* Queued frames */
  125. /* Data handling callback */
  126. void (*data)(struct gsm_dlci *dlci, u8 *data, int len);
  127. };
  128. /* DLCI 0, 62/63 are special or reseved see gsmtty_open */
  129. #define NUM_DLCI 64
  130. /*
  131. * DLCI 0 is used to pass control blocks out of band of the data
  132. * flow (and with a higher link priority). One command can be outstanding
  133. * at a time and we use this structure to manage them. They are created
  134. * and destroyed by the user context, and updated by the receive paths
  135. * and timers
  136. */
  137. struct gsm_control {
  138. u8 cmd; /* Command we are issuing */
  139. u8 *data; /* Data for the command in case we retransmit */
  140. int len; /* Length of block for retransmission */
  141. int done; /* Done flag */
  142. int error; /* Error if any */
  143. };
  144. /*
  145. * Each GSM mux we have is represented by this structure. If we are
  146. * operating as an ldisc then we use this structure as our ldisc
  147. * state. We need to sort out lifetimes and locking with respect
  148. * to the gsm mux array. For now we don't free DLCI objects that
  149. * have been instantiated until the mux itself is terminated.
  150. *
  151. * To consider further: tty open versus mux shutdown.
  152. */
  153. struct gsm_mux {
  154. struct tty_struct *tty; /* The tty our ldisc is bound to */
  155. spinlock_t lock;
  156. /* Events on the GSM channel */
  157. wait_queue_head_t event;
  158. /* Bits for GSM mode decoding */
  159. /* Framing Layer */
  160. unsigned char *buf;
  161. int state;
  162. #define GSM_SEARCH 0
  163. #define GSM_START 1
  164. #define GSM_ADDRESS 2
  165. #define GSM_CONTROL 3
  166. #define GSM_LEN 4
  167. #define GSM_DATA 5
  168. #define GSM_FCS 6
  169. #define GSM_OVERRUN 7
  170. #define GSM_LEN0 8
  171. #define GSM_LEN1 9
  172. #define GSM_SSOF 10
  173. unsigned int len;
  174. unsigned int address;
  175. unsigned int count;
  176. int escape;
  177. int encoding;
  178. u8 control;
  179. u8 fcs;
  180. u8 received_fcs;
  181. u8 *txframe; /* TX framing buffer */
  182. /* Methods for the receiver side */
  183. void (*receive)(struct gsm_mux *gsm, u8 ch);
  184. void (*error)(struct gsm_mux *gsm, u8 ch, u8 flag);
  185. /* And transmit side */
  186. int (*output)(struct gsm_mux *mux, u8 *data, int len);
  187. /* Link Layer */
  188. unsigned int mru;
  189. unsigned int mtu;
  190. int initiator; /* Did we initiate connection */
  191. int dead; /* Has the mux been shut down */
  192. struct gsm_dlci *dlci[NUM_DLCI];
  193. int constipated; /* Asked by remote to shut up */
  194. spinlock_t tx_lock;
  195. unsigned int tx_bytes; /* TX data outstanding */
  196. #define TX_THRESH_HI 8192
  197. #define TX_THRESH_LO 2048
  198. struct gsm_msg *tx_head; /* Pending data packets */
  199. struct gsm_msg *tx_tail;
  200. /* Control messages */
  201. struct timer_list t2_timer; /* Retransmit timer for commands */
  202. int cretries; /* Command retry counter */
  203. struct gsm_control *pending_cmd;/* Our current pending command */
  204. spinlock_t control_lock; /* Protects the pending command */
  205. /* Configuration */
  206. int adaption; /* 1 or 2 supported */
  207. u8 ftype; /* UI or UIH */
  208. int t1, t2; /* Timers in 1/100th of a sec */
  209. int n2; /* Retry count */
  210. /* Statistics (not currently exposed) */
  211. unsigned long bad_fcs;
  212. unsigned long malformed;
  213. unsigned long io_error;
  214. unsigned long bad_size;
  215. unsigned long unsupported;
  216. };
  217. /*
  218. * Mux objects - needed so that we can translate a tty index into the
  219. * relevant mux and DLCI.
  220. */
  221. #define MAX_MUX 4 /* 256 minors */
  222. static struct gsm_mux *gsm_mux[MAX_MUX]; /* GSM muxes */
  223. static spinlock_t gsm_mux_lock;
  224. /*
  225. * This section of the driver logic implements the GSM encodings
  226. * both the basic and the 'advanced'. Reliable transport is not
  227. * supported.
  228. */
  229. #define CR 0x02
  230. #define EA 0x01
  231. #define PF 0x10
  232. /* I is special: the rest are ..*/
  233. #define RR 0x01
  234. #define UI 0x03
  235. #define RNR 0x05
  236. #define REJ 0x09
  237. #define DM 0x0F
  238. #define SABM 0x2F
  239. #define DISC 0x43
  240. #define UA 0x63
  241. #define UIH 0xEF
  242. /* Channel commands */
  243. #define CMD_NSC 0x09
  244. #define CMD_TEST 0x11
  245. #define CMD_PSC 0x21
  246. #define CMD_RLS 0x29
  247. #define CMD_FCOFF 0x31
  248. #define CMD_PN 0x41
  249. #define CMD_RPN 0x49
  250. #define CMD_FCON 0x51
  251. #define CMD_CLD 0x61
  252. #define CMD_SNC 0x69
  253. #define CMD_MSC 0x71
  254. /* Virtual modem bits */
  255. #define MDM_FC 0x01
  256. #define MDM_RTC 0x02
  257. #define MDM_RTR 0x04
  258. #define MDM_IC 0x20
  259. #define MDM_DV 0x40
  260. #define GSM0_SOF 0xF9
  261. #define GSM1_SOF 0x7E
  262. #define GSM1_ESCAPE 0x7D
  263. #define GSM1_ESCAPE_BITS 0x20
  264. #define XON 0x11
  265. #define XOFF 0x13
  266. static const struct tty_port_operations gsm_port_ops;
  267. /*
  268. * CRC table for GSM 0710
  269. */
  270. static const u8 gsm_fcs8[256] = {
  271. 0x00, 0x91, 0xE3, 0x72, 0x07, 0x96, 0xE4, 0x75,
  272. 0x0E, 0x9F, 0xED, 0x7C, 0x09, 0x98, 0xEA, 0x7B,
  273. 0x1C, 0x8D, 0xFF, 0x6E, 0x1B, 0x8A, 0xF8, 0x69,
  274. 0x12, 0x83, 0xF1, 0x60, 0x15, 0x84, 0xF6, 0x67,
  275. 0x38, 0xA9, 0xDB, 0x4A, 0x3F, 0xAE, 0xDC, 0x4D,
  276. 0x36, 0xA7, 0xD5, 0x44, 0x31, 0xA0, 0xD2, 0x43,
  277. 0x24, 0xB5, 0xC7, 0x56, 0x23, 0xB2, 0xC0, 0x51,
  278. 0x2A, 0xBB, 0xC9, 0x58, 0x2D, 0xBC, 0xCE, 0x5F,
  279. 0x70, 0xE1, 0x93, 0x02, 0x77, 0xE6, 0x94, 0x05,
  280. 0x7E, 0xEF, 0x9D, 0x0C, 0x79, 0xE8, 0x9A, 0x0B,
  281. 0x6C, 0xFD, 0x8F, 0x1E, 0x6B, 0xFA, 0x88, 0x19,
  282. 0x62, 0xF3, 0x81, 0x10, 0x65, 0xF4, 0x86, 0x17,
  283. 0x48, 0xD9, 0xAB, 0x3A, 0x4F, 0xDE, 0xAC, 0x3D,
  284. 0x46, 0xD7, 0xA5, 0x34, 0x41, 0xD0, 0xA2, 0x33,
  285. 0x54, 0xC5, 0xB7, 0x26, 0x53, 0xC2, 0xB0, 0x21,
  286. 0x5A, 0xCB, 0xB9, 0x28, 0x5D, 0xCC, 0xBE, 0x2F,
  287. 0xE0, 0x71, 0x03, 0x92, 0xE7, 0x76, 0x04, 0x95,
  288. 0xEE, 0x7F, 0x0D, 0x9C, 0xE9, 0x78, 0x0A, 0x9B,
  289. 0xFC, 0x6D, 0x1F, 0x8E, 0xFB, 0x6A, 0x18, 0x89,
  290. 0xF2, 0x63, 0x11, 0x80, 0xF5, 0x64, 0x16, 0x87,
  291. 0xD8, 0x49, 0x3B, 0xAA, 0xDF, 0x4E, 0x3C, 0xAD,
  292. 0xD6, 0x47, 0x35, 0xA4, 0xD1, 0x40, 0x32, 0xA3,
  293. 0xC4, 0x55, 0x27, 0xB6, 0xC3, 0x52, 0x20, 0xB1,
  294. 0xCA, 0x5B, 0x29, 0xB8, 0xCD, 0x5C, 0x2E, 0xBF,
  295. 0x90, 0x01, 0x73, 0xE2, 0x97, 0x06, 0x74, 0xE5,
  296. 0x9E, 0x0F, 0x7D, 0xEC, 0x99, 0x08, 0x7A, 0xEB,
  297. 0x8C, 0x1D, 0x6F, 0xFE, 0x8B, 0x1A, 0x68, 0xF9,
  298. 0x82, 0x13, 0x61, 0xF0, 0x85, 0x14, 0x66, 0xF7,
  299. 0xA8, 0x39, 0x4B, 0xDA, 0xAF, 0x3E, 0x4C, 0xDD,
  300. 0xA6, 0x37, 0x45, 0xD4, 0xA1, 0x30, 0x42, 0xD3,
  301. 0xB4, 0x25, 0x57, 0xC6, 0xB3, 0x22, 0x50, 0xC1,
  302. 0xBA, 0x2B, 0x59, 0xC8, 0xBD, 0x2C, 0x5E, 0xCF
  303. };
  304. #define INIT_FCS 0xFF
  305. #define GOOD_FCS 0xCF
  306. /**
  307. * gsm_fcs_add - update FCS
  308. * @fcs: Current FCS
  309. * @c: Next data
  310. *
  311. * Update the FCS to include c. Uses the algorithm in the specification
  312. * notes.
  313. */
  314. static inline u8 gsm_fcs_add(u8 fcs, u8 c)
  315. {
  316. return gsm_fcs8[fcs ^ c];
  317. }
  318. /**
  319. * gsm_fcs_add_block - update FCS for a block
  320. * @fcs: Current FCS
  321. * @c: buffer of data
  322. * @len: length of buffer
  323. *
  324. * Update the FCS to include c. Uses the algorithm in the specification
  325. * notes.
  326. */
  327. static inline u8 gsm_fcs_add_block(u8 fcs, u8 *c, int len)
  328. {
  329. while (len--)
  330. fcs = gsm_fcs8[fcs ^ *c++];
  331. return fcs;
  332. }
  333. /**
  334. * gsm_read_ea - read a byte into an EA
  335. * @val: variable holding value
  336. * c: byte going into the EA
  337. *
  338. * Processes one byte of an EA. Updates the passed variable
  339. * and returns 1 if the EA is now completely read
  340. */
  341. static int gsm_read_ea(unsigned int *val, u8 c)
  342. {
  343. /* Add the next 7 bits into the value */
  344. *val <<= 7;
  345. *val |= c >> 1;
  346. /* Was this the last byte of the EA 1 = yes*/
  347. return c & EA;
  348. }
  349. /**
  350. * gsm_encode_modem - encode modem data bits
  351. * @dlci: DLCI to encode from
  352. *
  353. * Returns the correct GSM encoded modem status bits (6 bit field) for
  354. * the current status of the DLCI and attached tty object
  355. */
  356. static u8 gsm_encode_modem(const struct gsm_dlci *dlci)
  357. {
  358. u8 modembits = 0;
  359. /* FC is true flow control not modem bits */
  360. if (dlci->throttled)
  361. modembits |= MDM_FC;
  362. if (dlci->modem_tx & TIOCM_DTR)
  363. modembits |= MDM_RTC;
  364. if (dlci->modem_tx & TIOCM_RTS)
  365. modembits |= MDM_RTR;
  366. if (dlci->modem_tx & TIOCM_RI)
  367. modembits |= MDM_IC;
  368. if (dlci->modem_tx & TIOCM_CD)
  369. modembits |= MDM_DV;
  370. return modembits;
  371. }
  372. /**
  373. * gsm_print_packet - display a frame for debug
  374. * @hdr: header to print before decode
  375. * @addr: address EA from the frame
  376. * @cr: C/R bit from the frame
  377. * @control: control including PF bit
  378. * @data: following data bytes
  379. * @dlen: length of data
  380. *
  381. * Displays a packet in human readable format for debugging purposes. The
  382. * style is based on amateur radio LAP-B dump display.
  383. */
  384. static void gsm_print_packet(const char *hdr, int addr, int cr,
  385. u8 control, const u8 *data, int dlen)
  386. {
  387. if (!(debug & 1))
  388. return;
  389. pr_info("%s %d) %c: ", hdr, addr, "RC"[cr]);
  390. switch (control & ~PF) {
  391. case SABM:
  392. pr_cont("SABM");
  393. break;
  394. case UA:
  395. pr_cont("UA");
  396. break;
  397. case DISC:
  398. pr_cont("DISC");
  399. break;
  400. case DM:
  401. pr_cont("DM");
  402. break;
  403. case UI:
  404. pr_cont("UI");
  405. break;
  406. case UIH:
  407. pr_cont("UIH");
  408. break;
  409. default:
  410. if (!(control & 0x01)) {
  411. pr_cont("I N(S)%d N(R)%d",
  412. (control & 0x0E) >> 1, (control & 0xE) >> 5);
  413. } else switch (control & 0x0F) {
  414. case RR:
  415. pr_cont("RR(%d)", (control & 0xE0) >> 5);
  416. break;
  417. case RNR:
  418. pr_cont("RNR(%d)", (control & 0xE0) >> 5);
  419. break;
  420. case REJ:
  421. pr_cont("REJ(%d)", (control & 0xE0) >> 5);
  422. break;
  423. default:
  424. pr_cont("[%02X]", control);
  425. }
  426. }
  427. if (control & PF)
  428. pr_cont("(P)");
  429. else
  430. pr_cont("(F)");
  431. if (dlen) {
  432. int ct = 0;
  433. while (dlen--) {
  434. if (ct % 8 == 0) {
  435. pr_cont("\n");
  436. pr_debug(" ");
  437. }
  438. pr_cont("%02X ", *data++);
  439. ct++;
  440. }
  441. }
  442. pr_cont("\n");
  443. }
  444. /*
  445. * Link level transmission side
  446. */
  447. /**
  448. * gsm_stuff_packet - bytestuff a packet
  449. * @ibuf: input
  450. * @obuf: output
  451. * @len: length of input
  452. *
  453. * Expand a buffer by bytestuffing it. The worst case size change
  454. * is doubling and the caller is responsible for handing out
  455. * suitable sized buffers.
  456. */
  457. static int gsm_stuff_frame(const u8 *input, u8 *output, int len)
  458. {
  459. int olen = 0;
  460. while (len--) {
  461. if (*input == GSM1_SOF || *input == GSM1_ESCAPE
  462. || *input == XON || *input == XOFF) {
  463. *output++ = GSM1_ESCAPE;
  464. *output++ = *input++ ^ GSM1_ESCAPE_BITS;
  465. olen++;
  466. } else
  467. *output++ = *input++;
  468. olen++;
  469. }
  470. return olen;
  471. }
  472. /**
  473. * gsm_send - send a control frame
  474. * @gsm: our GSM mux
  475. * @addr: address for control frame
  476. * @cr: command/response bit
  477. * @control: control byte including PF bit
  478. *
  479. * Format up and transmit a control frame. These do not go via the
  480. * queueing logic as they should be transmitted ahead of data when
  481. * they are needed.
  482. *
  483. * FIXME: Lock versus data TX path
  484. */
  485. static void gsm_send(struct gsm_mux *gsm, int addr, int cr, int control)
  486. {
  487. int len;
  488. u8 cbuf[10];
  489. u8 ibuf[3];
  490. switch (gsm->encoding) {
  491. case 0:
  492. cbuf[0] = GSM0_SOF;
  493. cbuf[1] = (addr << 2) | (cr << 1) | EA;
  494. cbuf[2] = control;
  495. cbuf[3] = EA; /* Length of data = 0 */
  496. cbuf[4] = 0xFF - gsm_fcs_add_block(INIT_FCS, cbuf + 1, 3);
  497. cbuf[5] = GSM0_SOF;
  498. len = 6;
  499. break;
  500. case 1:
  501. case 2:
  502. /* Control frame + packing (but not frame stuffing) in mode 1 */
  503. ibuf[0] = (addr << 2) | (cr << 1) | EA;
  504. ibuf[1] = control;
  505. ibuf[2] = 0xFF - gsm_fcs_add_block(INIT_FCS, ibuf, 2);
  506. /* Stuffing may double the size worst case */
  507. len = gsm_stuff_frame(ibuf, cbuf + 1, 3);
  508. /* Now add the SOF markers */
  509. cbuf[0] = GSM1_SOF;
  510. cbuf[len + 1] = GSM1_SOF;
  511. /* FIXME: we can omit the lead one in many cases */
  512. len += 2;
  513. break;
  514. default:
  515. WARN_ON(1);
  516. return;
  517. }
  518. gsm->output(gsm, cbuf, len);
  519. gsm_print_packet("-->", addr, cr, control, NULL, 0);
  520. }
  521. /**
  522. * gsm_response - send a control response
  523. * @gsm: our GSM mux
  524. * @addr: address for control frame
  525. * @control: control byte including PF bit
  526. *
  527. * Format up and transmit a link level response frame.
  528. */
  529. static inline void gsm_response(struct gsm_mux *gsm, int addr, int control)
  530. {
  531. gsm_send(gsm, addr, 0, control);
  532. }
  533. /**
  534. * gsm_command - send a control command
  535. * @gsm: our GSM mux
  536. * @addr: address for control frame
  537. * @control: control byte including PF bit
  538. *
  539. * Format up and transmit a link level command frame.
  540. */
  541. static inline void gsm_command(struct gsm_mux *gsm, int addr, int control)
  542. {
  543. gsm_send(gsm, addr, 1, control);
  544. }
  545. /* Data transmission */
  546. #define HDR_LEN 6 /* ADDR CTRL [LEN.2] DATA FCS */
  547. /**
  548. * gsm_data_alloc - allocate data frame
  549. * @gsm: GSM mux
  550. * @addr: DLCI address
  551. * @len: length excluding header and FCS
  552. * @ctrl: control byte
  553. *
  554. * Allocate a new data buffer for sending frames with data. Space is left
  555. * at the front for header bytes but that is treated as an implementation
  556. * detail and not for the high level code to use
  557. */
  558. static struct gsm_msg *gsm_data_alloc(struct gsm_mux *gsm, u8 addr, int len,
  559. u8 ctrl)
  560. {
  561. struct gsm_msg *m = kmalloc(sizeof(struct gsm_msg) + len + HDR_LEN,
  562. GFP_ATOMIC);
  563. if (m == NULL)
  564. return NULL;
  565. m->data = m->buffer + HDR_LEN - 1; /* Allow for FCS */
  566. m->len = len;
  567. m->addr = addr;
  568. m->ctrl = ctrl;
  569. m->next = NULL;
  570. return m;
  571. }
  572. /**
  573. * gsm_data_kick - poke the queue
  574. * @gsm: GSM Mux
  575. *
  576. * The tty device has called us to indicate that room has appeared in
  577. * the transmit queue. Ram more data into the pipe if we have any
  578. *
  579. * FIXME: lock against link layer control transmissions
  580. */
  581. static void gsm_data_kick(struct gsm_mux *gsm)
  582. {
  583. struct gsm_msg *msg = gsm->tx_head;
  584. int len;
  585. int skip_sof = 0;
  586. /* FIXME: We need to apply this solely to data messages */
  587. if (gsm->constipated)
  588. return;
  589. while (gsm->tx_head != NULL) {
  590. msg = gsm->tx_head;
  591. if (gsm->encoding != 0) {
  592. gsm->txframe[0] = GSM1_SOF;
  593. len = gsm_stuff_frame(msg->data,
  594. gsm->txframe + 1, msg->len);
  595. gsm->txframe[len + 1] = GSM1_SOF;
  596. len += 2;
  597. } else {
  598. gsm->txframe[0] = GSM0_SOF;
  599. memcpy(gsm->txframe + 1 , msg->data, msg->len);
  600. gsm->txframe[msg->len + 1] = GSM0_SOF;
  601. len = msg->len + 2;
  602. }
  603. if (debug & 4)
  604. print_hex_dump_bytes("gsm_data_kick: ",
  605. DUMP_PREFIX_OFFSET,
  606. gsm->txframe, len);
  607. if (gsm->output(gsm, gsm->txframe + skip_sof,
  608. len - skip_sof) < 0)
  609. break;
  610. /* FIXME: Can eliminate one SOF in many more cases */
  611. gsm->tx_head = msg->next;
  612. if (gsm->tx_head == NULL)
  613. gsm->tx_tail = NULL;
  614. gsm->tx_bytes -= msg->len;
  615. kfree(msg);
  616. /* For a burst of frames skip the extra SOF within the
  617. burst */
  618. skip_sof = 1;
  619. }
  620. }
  621. /**
  622. * __gsm_data_queue - queue a UI or UIH frame
  623. * @dlci: DLCI sending the data
  624. * @msg: message queued
  625. *
  626. * Add data to the transmit queue and try and get stuff moving
  627. * out of the mux tty if not already doing so. The Caller must hold
  628. * the gsm tx lock.
  629. */
  630. static void __gsm_data_queue(struct gsm_dlci *dlci, struct gsm_msg *msg)
  631. {
  632. struct gsm_mux *gsm = dlci->gsm;
  633. u8 *dp = msg->data;
  634. u8 *fcs = dp + msg->len;
  635. /* Fill in the header */
  636. if (gsm->encoding == 0) {
  637. if (msg->len < 128)
  638. *--dp = (msg->len << 1) | EA;
  639. else {
  640. *--dp = (msg->len >> 7); /* bits 7 - 15 */
  641. *--dp = (msg->len & 127) << 1; /* bits 0 - 6 */
  642. }
  643. }
  644. *--dp = msg->ctrl;
  645. if (gsm->initiator)
  646. *--dp = (msg->addr << 2) | 2 | EA;
  647. else
  648. *--dp = (msg->addr << 2) | EA;
  649. *fcs = gsm_fcs_add_block(INIT_FCS, dp , msg->data - dp);
  650. /* Ugly protocol layering violation */
  651. if (msg->ctrl == UI || msg->ctrl == (UI|PF))
  652. *fcs = gsm_fcs_add_block(*fcs, msg->data, msg->len);
  653. *fcs = 0xFF - *fcs;
  654. gsm_print_packet("Q> ", msg->addr, gsm->initiator, msg->ctrl,
  655. msg->data, msg->len);
  656. /* Move the header back and adjust the length, also allow for the FCS
  657. now tacked on the end */
  658. msg->len += (msg->data - dp) + 1;
  659. msg->data = dp;
  660. /* Add to the actual output queue */
  661. if (gsm->tx_tail)
  662. gsm->tx_tail->next = msg;
  663. else
  664. gsm->tx_head = msg;
  665. gsm->tx_tail = msg;
  666. gsm->tx_bytes += msg->len;
  667. gsm_data_kick(gsm);
  668. }
  669. /**
  670. * gsm_data_queue - queue a UI or UIH frame
  671. * @dlci: DLCI sending the data
  672. * @msg: message queued
  673. *
  674. * Add data to the transmit queue and try and get stuff moving
  675. * out of the mux tty if not already doing so. Take the
  676. * the gsm tx lock and dlci lock.
  677. */
  678. static void gsm_data_queue(struct gsm_dlci *dlci, struct gsm_msg *msg)
  679. {
  680. unsigned long flags;
  681. spin_lock_irqsave(&dlci->gsm->tx_lock, flags);
  682. __gsm_data_queue(dlci, msg);
  683. spin_unlock_irqrestore(&dlci->gsm->tx_lock, flags);
  684. }
  685. /**
  686. * gsm_dlci_data_output - try and push data out of a DLCI
  687. * @gsm: mux
  688. * @dlci: the DLCI to pull data from
  689. *
  690. * Pull data from a DLCI and send it into the transmit queue if there
  691. * is data. Keep to the MRU of the mux. This path handles the usual tty
  692. * interface which is a byte stream with optional modem data.
  693. *
  694. * Caller must hold the tx_lock of the mux.
  695. */
  696. static int gsm_dlci_data_output(struct gsm_mux *gsm, struct gsm_dlci *dlci)
  697. {
  698. struct gsm_msg *msg;
  699. u8 *dp;
  700. int len, size;
  701. int h = dlci->adaption - 1;
  702. len = kfifo_len(dlci->fifo);
  703. if (len == 0)
  704. return 0;
  705. /* MTU/MRU count only the data bits */
  706. if (len > gsm->mtu)
  707. len = gsm->mtu;
  708. size = len + h;
  709. msg = gsm_data_alloc(gsm, dlci->addr, size, gsm->ftype);
  710. /* FIXME: need a timer or something to kick this so it can't
  711. get stuck with no work outstanding and no buffer free */
  712. if (msg == NULL)
  713. return -ENOMEM;
  714. dp = msg->data;
  715. switch (dlci->adaption) {
  716. case 1: /* Unstructured */
  717. break;
  718. case 2: /* Unstructed with modem bits. Always one byte as we never
  719. send inline break data */
  720. *dp += gsm_encode_modem(dlci);
  721. len--;
  722. break;
  723. }
  724. WARN_ON(kfifo_out_locked(dlci->fifo, dp , len, &dlci->lock) != len);
  725. __gsm_data_queue(dlci, msg);
  726. /* Bytes of data we used up */
  727. return size;
  728. }
  729. /**
  730. * gsm_dlci_data_output_framed - try and push data out of a DLCI
  731. * @gsm: mux
  732. * @dlci: the DLCI to pull data from
  733. *
  734. * Pull data from a DLCI and send it into the transmit queue if there
  735. * is data. Keep to the MRU of the mux. This path handles framed data
  736. * queued as skbuffs to the DLCI.
  737. *
  738. * Caller must hold the tx_lock of the mux.
  739. */
  740. static int gsm_dlci_data_output_framed(struct gsm_mux *gsm,
  741. struct gsm_dlci *dlci)
  742. {
  743. struct gsm_msg *msg;
  744. u8 *dp;
  745. int len, size;
  746. int last = 0, first = 0;
  747. int overhead = 0;
  748. /* One byte per frame is used for B/F flags */
  749. if (dlci->adaption == 4)
  750. overhead = 1;
  751. /* dlci->skb is locked by tx_lock */
  752. if (dlci->skb == NULL) {
  753. dlci->skb = skb_dequeue(&dlci->skb_list);
  754. if (dlci->skb == NULL)
  755. return 0;
  756. first = 1;
  757. }
  758. len = dlci->skb->len + overhead;
  759. /* MTU/MRU count only the data bits */
  760. if (len > gsm->mtu) {
  761. if (dlci->adaption == 3) {
  762. /* Over long frame, bin it */
  763. kfree_skb(dlci->skb);
  764. dlci->skb = NULL;
  765. return 0;
  766. }
  767. len = gsm->mtu;
  768. } else
  769. last = 1;
  770. size = len + overhead;
  771. msg = gsm_data_alloc(gsm, dlci->addr, size, gsm->ftype);
  772. /* FIXME: need a timer or something to kick this so it can't
  773. get stuck with no work outstanding and no buffer free */
  774. if (msg == NULL)
  775. return -ENOMEM;
  776. dp = msg->data;
  777. if (dlci->adaption == 4) { /* Interruptible framed (Packetised Data) */
  778. /* Flag byte to carry the start/end info */
  779. *dp++ = last << 7 | first << 6 | 1; /* EA */
  780. len--;
  781. }
  782. memcpy(dp, dlci->skb->data, len);
  783. skb_pull(dlci->skb, len);
  784. __gsm_data_queue(dlci, msg);
  785. if (last)
  786. dlci->skb = NULL;
  787. return size;
  788. }
  789. /**
  790. * gsm_dlci_data_sweep - look for data to send
  791. * @gsm: the GSM mux
  792. *
  793. * Sweep the GSM mux channels in priority order looking for ones with
  794. * data to send. We could do with optimising this scan a bit. We aim
  795. * to fill the queue totally or up to TX_THRESH_HI bytes. Once we hit
  796. * TX_THRESH_LO we get called again
  797. *
  798. * FIXME: We should round robin between groups and in theory you can
  799. * renegotiate DLCI priorities with optional stuff. Needs optimising.
  800. */
  801. static void gsm_dlci_data_sweep(struct gsm_mux *gsm)
  802. {
  803. int len;
  804. /* Priority ordering: We should do priority with RR of the groups */
  805. int i = 1;
  806. while (i < NUM_DLCI) {
  807. struct gsm_dlci *dlci;
  808. if (gsm->tx_bytes > TX_THRESH_HI)
  809. break;
  810. dlci = gsm->dlci[i];
  811. if (dlci == NULL || dlci->constipated) {
  812. i++;
  813. continue;
  814. }
  815. if (dlci->adaption < 3)
  816. len = gsm_dlci_data_output(gsm, dlci);
  817. else
  818. len = gsm_dlci_data_output_framed(gsm, dlci);
  819. if (len < 0)
  820. break;
  821. /* DLCI empty - try the next */
  822. if (len == 0)
  823. i++;
  824. }
  825. }
  826. /**
  827. * gsm_dlci_data_kick - transmit if possible
  828. * @dlci: DLCI to kick
  829. *
  830. * Transmit data from this DLCI if the queue is empty. We can't rely on
  831. * a tty wakeup except when we filled the pipe so we need to fire off
  832. * new data ourselves in other cases.
  833. */
  834. static void gsm_dlci_data_kick(struct gsm_dlci *dlci)
  835. {
  836. unsigned long flags;
  837. spin_lock_irqsave(&dlci->gsm->tx_lock, flags);
  838. /* If we have nothing running then we need to fire up */
  839. if (dlci->gsm->tx_bytes == 0)
  840. gsm_dlci_data_output(dlci->gsm, dlci);
  841. else if (dlci->gsm->tx_bytes < TX_THRESH_LO)
  842. gsm_dlci_data_sweep(dlci->gsm);
  843. spin_unlock_irqrestore(&dlci->gsm->tx_lock, flags);
  844. }
  845. /*
  846. * Control message processing
  847. */
  848. /**
  849. * gsm_control_reply - send a response frame to a control
  850. * @gsm: gsm channel
  851. * @cmd: the command to use
  852. * @data: data to follow encoded info
  853. * @dlen: length of data
  854. *
  855. * Encode up and queue a UI/UIH frame containing our response.
  856. */
  857. static void gsm_control_reply(struct gsm_mux *gsm, int cmd, u8 *data,
  858. int dlen)
  859. {
  860. struct gsm_msg *msg;
  861. msg = gsm_data_alloc(gsm, 0, dlen + 2, gsm->ftype);
  862. if (msg == NULL)
  863. return;
  864. msg->data[0] = (cmd & 0xFE) << 1 | EA; /* Clear C/R */
  865. msg->data[1] = (dlen << 1) | EA;
  866. memcpy(msg->data + 2, data, dlen);
  867. gsm_data_queue(gsm->dlci[0], msg);
  868. }
  869. /**
  870. * gsm_process_modem - process received modem status
  871. * @tty: virtual tty bound to the DLCI
  872. * @dlci: DLCI to affect
  873. * @modem: modem bits (full EA)
  874. *
  875. * Used when a modem control message or line state inline in adaption
  876. * layer 2 is processed. Sort out the local modem state and throttles
  877. */
  878. static void gsm_process_modem(struct tty_struct *tty, struct gsm_dlci *dlci,
  879. u32 modem, int clen)
  880. {
  881. int mlines = 0;
  882. u8 brk = 0;
  883. /* The modem status command can either contain one octet (v.24 signals)
  884. or two octets (v.24 signals + break signals). The length field will
  885. either be 2 or 3 respectively. This is specified in section
  886. 5.4.6.3.7 of the 27.010 mux spec. */
  887. if (clen == 2)
  888. modem = modem & 0x7f;
  889. else {
  890. brk = modem & 0x7f;
  891. modem = (modem >> 7) & 0x7f;
  892. };
  893. /* Flow control/ready to communicate */
  894. if (modem & MDM_FC) {
  895. /* Need to throttle our output on this device */
  896. dlci->constipated = 1;
  897. }
  898. if (modem & MDM_RTC) {
  899. mlines |= TIOCM_DSR | TIOCM_DTR;
  900. dlci->constipated = 0;
  901. gsm_dlci_data_kick(dlci);
  902. }
  903. /* Map modem bits */
  904. if (modem & MDM_RTR)
  905. mlines |= TIOCM_RTS | TIOCM_CTS;
  906. if (modem & MDM_IC)
  907. mlines |= TIOCM_RI;
  908. if (modem & MDM_DV)
  909. mlines |= TIOCM_CD;
  910. /* Carrier drop -> hangup */
  911. if (tty) {
  912. if ((mlines & TIOCM_CD) == 0 && (dlci->modem_rx & TIOCM_CD))
  913. if (!(tty->termios->c_cflag & CLOCAL))
  914. tty_hangup(tty);
  915. if (brk & 0x01)
  916. tty_insert_flip_char(tty, 0, TTY_BREAK);
  917. }
  918. dlci->modem_rx = mlines;
  919. }
  920. /**
  921. * gsm_control_modem - modem status received
  922. * @gsm: GSM channel
  923. * @data: data following command
  924. * @clen: command length
  925. *
  926. * We have received a modem status control message. This is used by
  927. * the GSM mux protocol to pass virtual modem line status and optionally
  928. * to indicate break signals. Unpack it, convert to Linux representation
  929. * and if need be stuff a break message down the tty.
  930. */
  931. static void gsm_control_modem(struct gsm_mux *gsm, u8 *data, int clen)
  932. {
  933. unsigned int addr = 0;
  934. unsigned int modem = 0;
  935. struct gsm_dlci *dlci;
  936. int len = clen;
  937. u8 *dp = data;
  938. struct tty_struct *tty;
  939. while (gsm_read_ea(&addr, *dp++) == 0) {
  940. len--;
  941. if (len == 0)
  942. return;
  943. }
  944. /* Must be at least one byte following the EA */
  945. len--;
  946. if (len <= 0)
  947. return;
  948. addr >>= 1;
  949. /* Closed port, or invalid ? */
  950. if (addr == 0 || addr >= NUM_DLCI || gsm->dlci[addr] == NULL)
  951. return;
  952. dlci = gsm->dlci[addr];
  953. while (gsm_read_ea(&modem, *dp++) == 0) {
  954. len--;
  955. if (len == 0)
  956. return;
  957. }
  958. tty = tty_port_tty_get(&dlci->port);
  959. gsm_process_modem(tty, dlci, modem, clen);
  960. if (tty) {
  961. tty_wakeup(tty);
  962. tty_kref_put(tty);
  963. }
  964. gsm_control_reply(gsm, CMD_MSC, data, clen);
  965. }
  966. /**
  967. * gsm_control_rls - remote line status
  968. * @gsm: GSM channel
  969. * @data: data bytes
  970. * @clen: data length
  971. *
  972. * The modem sends us a two byte message on the control channel whenever
  973. * it wishes to send us an error state from the virtual link. Stuff
  974. * this into the uplink tty if present
  975. */
  976. static void gsm_control_rls(struct gsm_mux *gsm, u8 *data, int clen)
  977. {
  978. struct tty_struct *tty;
  979. unsigned int addr = 0 ;
  980. u8 bits;
  981. int len = clen;
  982. u8 *dp = data;
  983. while (gsm_read_ea(&addr, *dp++) == 0) {
  984. len--;
  985. if (len == 0)
  986. return;
  987. }
  988. /* Must be at least one byte following ea */
  989. len--;
  990. if (len <= 0)
  991. return;
  992. addr >>= 1;
  993. /* Closed port, or invalid ? */
  994. if (addr == 0 || addr >= NUM_DLCI || gsm->dlci[addr] == NULL)
  995. return;
  996. /* No error ? */
  997. bits = *dp;
  998. if ((bits & 1) == 0)
  999. return;
  1000. /* See if we have an uplink tty */
  1001. tty = tty_port_tty_get(&gsm->dlci[addr]->port);
  1002. if (tty) {
  1003. if (bits & 2)
  1004. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  1005. if (bits & 4)
  1006. tty_insert_flip_char(tty, 0, TTY_PARITY);
  1007. if (bits & 8)
  1008. tty_insert_flip_char(tty, 0, TTY_FRAME);
  1009. tty_flip_buffer_push(tty);
  1010. tty_kref_put(tty);
  1011. }
  1012. gsm_control_reply(gsm, CMD_RLS, data, clen);
  1013. }
  1014. static void gsm_dlci_begin_close(struct gsm_dlci *dlci);
  1015. /**
  1016. * gsm_control_message - DLCI 0 control processing
  1017. * @gsm: our GSM mux
  1018. * @command: the command EA
  1019. * @data: data beyond the command/length EAs
  1020. * @clen: length
  1021. *
  1022. * Input processor for control messages from the other end of the link.
  1023. * Processes the incoming request and queues a response frame or an
  1024. * NSC response if not supported
  1025. */
  1026. static void gsm_control_message(struct gsm_mux *gsm, unsigned int command,
  1027. u8 *data, int clen)
  1028. {
  1029. u8 buf[1];
  1030. switch (command) {
  1031. case CMD_CLD: {
  1032. struct gsm_dlci *dlci = gsm->dlci[0];
  1033. /* Modem wishes to close down */
  1034. if (dlci) {
  1035. dlci->dead = 1;
  1036. gsm->dead = 1;
  1037. gsm_dlci_begin_close(dlci);
  1038. }
  1039. }
  1040. break;
  1041. case CMD_TEST:
  1042. /* Modem wishes to test, reply with the data */
  1043. gsm_control_reply(gsm, CMD_TEST, data, clen);
  1044. break;
  1045. case CMD_FCON:
  1046. /* Modem wants us to STFU */
  1047. gsm->constipated = 1;
  1048. gsm_control_reply(gsm, CMD_FCON, NULL, 0);
  1049. break;
  1050. case CMD_FCOFF:
  1051. /* Modem can accept data again */
  1052. gsm->constipated = 0;
  1053. gsm_control_reply(gsm, CMD_FCOFF, NULL, 0);
  1054. /* Kick the link in case it is idling */
  1055. gsm_data_kick(gsm);
  1056. break;
  1057. case CMD_MSC:
  1058. /* Out of band modem line change indicator for a DLCI */
  1059. gsm_control_modem(gsm, data, clen);
  1060. break;
  1061. case CMD_RLS:
  1062. /* Out of band error reception for a DLCI */
  1063. gsm_control_rls(gsm, data, clen);
  1064. break;
  1065. case CMD_PSC:
  1066. /* Modem wishes to enter power saving state */
  1067. gsm_control_reply(gsm, CMD_PSC, NULL, 0);
  1068. break;
  1069. /* Optional unsupported commands */
  1070. case CMD_PN: /* Parameter negotiation */
  1071. case CMD_RPN: /* Remote port negotiation */
  1072. case CMD_SNC: /* Service negotiation command */
  1073. default:
  1074. /* Reply to bad commands with an NSC */
  1075. buf[0] = command;
  1076. gsm_control_reply(gsm, CMD_NSC, buf, 1);
  1077. break;
  1078. }
  1079. }
  1080. /**
  1081. * gsm_control_response - process a response to our control
  1082. * @gsm: our GSM mux
  1083. * @command: the command (response) EA
  1084. * @data: data beyond the command/length EA
  1085. * @clen: length
  1086. *
  1087. * Process a response to an outstanding command. We only allow a single
  1088. * control message in flight so this is fairly easy. All the clean up
  1089. * is done by the caller, we just update the fields, flag it as done
  1090. * and return
  1091. */
  1092. static void gsm_control_response(struct gsm_mux *gsm, unsigned int command,
  1093. u8 *data, int clen)
  1094. {
  1095. struct gsm_control *ctrl;
  1096. unsigned long flags;
  1097. spin_lock_irqsave(&gsm->control_lock, flags);
  1098. ctrl = gsm->pending_cmd;
  1099. /* Does the reply match our command */
  1100. command |= 1;
  1101. if (ctrl != NULL && (command == ctrl->cmd || command == CMD_NSC)) {
  1102. /* Our command was replied to, kill the retry timer */
  1103. del_timer(&gsm->t2_timer);
  1104. gsm->pending_cmd = NULL;
  1105. /* Rejected by the other end */
  1106. if (command == CMD_NSC)
  1107. ctrl->error = -EOPNOTSUPP;
  1108. ctrl->done = 1;
  1109. wake_up(&gsm->event);
  1110. }
  1111. spin_unlock_irqrestore(&gsm->control_lock, flags);
  1112. }
  1113. /**
  1114. * gsm_control_transmit - send control packet
  1115. * @gsm: gsm mux
  1116. * @ctrl: frame to send
  1117. *
  1118. * Send out a pending control command (called under control lock)
  1119. */
  1120. static void gsm_control_transmit(struct gsm_mux *gsm, struct gsm_control *ctrl)
  1121. {
  1122. struct gsm_msg *msg = gsm_data_alloc(gsm, 0, ctrl->len + 1, gsm->ftype);
  1123. if (msg == NULL)
  1124. return;
  1125. msg->data[0] = (ctrl->cmd << 1) | 2 | EA; /* command */
  1126. memcpy(msg->data + 1, ctrl->data, ctrl->len);
  1127. gsm_data_queue(gsm->dlci[0], msg);
  1128. }
  1129. /**
  1130. * gsm_control_retransmit - retransmit a control frame
  1131. * @data: pointer to our gsm object
  1132. *
  1133. * Called off the T2 timer expiry in order to retransmit control frames
  1134. * that have been lost in the system somewhere. The control_lock protects
  1135. * us from colliding with another sender or a receive completion event.
  1136. * In that situation the timer may still occur in a small window but
  1137. * gsm->pending_cmd will be NULL and we just let the timer expire.
  1138. */
  1139. static void gsm_control_retransmit(unsigned long data)
  1140. {
  1141. struct gsm_mux *gsm = (struct gsm_mux *)data;
  1142. struct gsm_control *ctrl;
  1143. unsigned long flags;
  1144. spin_lock_irqsave(&gsm->control_lock, flags);
  1145. ctrl = gsm->pending_cmd;
  1146. if (ctrl) {
  1147. gsm->cretries--;
  1148. if (gsm->cretries == 0) {
  1149. gsm->pending_cmd = NULL;
  1150. ctrl->error = -ETIMEDOUT;
  1151. ctrl->done = 1;
  1152. spin_unlock_irqrestore(&gsm->control_lock, flags);
  1153. wake_up(&gsm->event);
  1154. return;
  1155. }
  1156. gsm_control_transmit(gsm, ctrl);
  1157. mod_timer(&gsm->t2_timer, jiffies + gsm->t2 * HZ / 100);
  1158. }
  1159. spin_unlock_irqrestore(&gsm->control_lock, flags);
  1160. }
  1161. /**
  1162. * gsm_control_send - send a control frame on DLCI 0
  1163. * @gsm: the GSM channel
  1164. * @command: command to send including CR bit
  1165. * @data: bytes of data (must be kmalloced)
  1166. * @len: length of the block to send
  1167. *
  1168. * Queue and dispatch a control command. Only one command can be
  1169. * active at a time. In theory more can be outstanding but the matching
  1170. * gets really complicated so for now stick to one outstanding.
  1171. */
  1172. static struct gsm_control *gsm_control_send(struct gsm_mux *gsm,
  1173. unsigned int command, u8 *data, int clen)
  1174. {
  1175. struct gsm_control *ctrl = kzalloc(sizeof(struct gsm_control),
  1176. GFP_KERNEL);
  1177. unsigned long flags;
  1178. if (ctrl == NULL)
  1179. return NULL;
  1180. retry:
  1181. wait_event(gsm->event, gsm->pending_cmd == NULL);
  1182. spin_lock_irqsave(&gsm->control_lock, flags);
  1183. if (gsm->pending_cmd != NULL) {
  1184. spin_unlock_irqrestore(&gsm->control_lock, flags);
  1185. goto retry;
  1186. }
  1187. ctrl->cmd = command;
  1188. ctrl->data = data;
  1189. ctrl->len = clen;
  1190. gsm->pending_cmd = ctrl;
  1191. gsm->cretries = gsm->n2;
  1192. mod_timer(&gsm->t2_timer, jiffies + gsm->t2 * HZ / 100);
  1193. gsm_control_transmit(gsm, ctrl);
  1194. spin_unlock_irqrestore(&gsm->control_lock, flags);
  1195. return ctrl;
  1196. }
  1197. /**
  1198. * gsm_control_wait - wait for a control to finish
  1199. * @gsm: GSM mux
  1200. * @control: control we are waiting on
  1201. *
  1202. * Waits for the control to complete or time out. Frees any used
  1203. * resources and returns 0 for success, or an error if the remote
  1204. * rejected or ignored the request.
  1205. */
  1206. static int gsm_control_wait(struct gsm_mux *gsm, struct gsm_control *control)
  1207. {
  1208. int err;
  1209. wait_event(gsm->event, control->done == 1);
  1210. err = control->error;
  1211. kfree(control);
  1212. return err;
  1213. }
  1214. /*
  1215. * DLCI level handling: Needs krefs
  1216. */
  1217. /*
  1218. * State transitions and timers
  1219. */
  1220. /**
  1221. * gsm_dlci_close - a DLCI has closed
  1222. * @dlci: DLCI that closed
  1223. *
  1224. * Perform processing when moving a DLCI into closed state. If there
  1225. * is an attached tty this is hung up
  1226. */
  1227. static void gsm_dlci_close(struct gsm_dlci *dlci)
  1228. {
  1229. del_timer(&dlci->t1);
  1230. if (debug & 8)
  1231. pr_debug("DLCI %d goes closed.\n", dlci->addr);
  1232. dlci->state = DLCI_CLOSED;
  1233. if (dlci->addr != 0) {
  1234. struct tty_struct *tty = tty_port_tty_get(&dlci->port);
  1235. if (tty) {
  1236. tty_hangup(tty);
  1237. tty_kref_put(tty);
  1238. }
  1239. kfifo_reset(dlci->fifo);
  1240. } else
  1241. dlci->gsm->dead = 1;
  1242. wake_up(&dlci->gsm->event);
  1243. /* A DLCI 0 close is a MUX termination so we need to kick that
  1244. back to userspace somehow */
  1245. }
  1246. /**
  1247. * gsm_dlci_open - a DLCI has opened
  1248. * @dlci: DLCI that opened
  1249. *
  1250. * Perform processing when moving a DLCI into open state.
  1251. */
  1252. static void gsm_dlci_open(struct gsm_dlci *dlci)
  1253. {
  1254. /* Note that SABM UA .. SABM UA first UA lost can mean that we go
  1255. open -> open */
  1256. del_timer(&dlci->t1);
  1257. /* This will let a tty open continue */
  1258. dlci->state = DLCI_OPEN;
  1259. if (debug & 8)
  1260. pr_debug("DLCI %d goes open.\n", dlci->addr);
  1261. wake_up(&dlci->gsm->event);
  1262. }
  1263. /**
  1264. * gsm_dlci_t1 - T1 timer expiry
  1265. * @dlci: DLCI that opened
  1266. *
  1267. * The T1 timer handles retransmits of control frames (essentially of
  1268. * SABM and DISC). We resend the command until the retry count runs out
  1269. * in which case an opening port goes back to closed and a closing port
  1270. * is simply put into closed state (any further frames from the other
  1271. * end will get a DM response)
  1272. */
  1273. static void gsm_dlci_t1(unsigned long data)
  1274. {
  1275. struct gsm_dlci *dlci = (struct gsm_dlci *)data;
  1276. struct gsm_mux *gsm = dlci->gsm;
  1277. switch (dlci->state) {
  1278. case DLCI_OPENING:
  1279. dlci->retries--;
  1280. if (dlci->retries) {
  1281. gsm_command(dlci->gsm, dlci->addr, SABM|PF);
  1282. mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
  1283. } else
  1284. gsm_dlci_close(dlci);
  1285. break;
  1286. case DLCI_CLOSING:
  1287. dlci->retries--;
  1288. if (dlci->retries) {
  1289. gsm_command(dlci->gsm, dlci->addr, DISC|PF);
  1290. mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
  1291. } else
  1292. gsm_dlci_close(dlci);
  1293. break;
  1294. }
  1295. }
  1296. /**
  1297. * gsm_dlci_begin_open - start channel open procedure
  1298. * @dlci: DLCI to open
  1299. *
  1300. * Commence opening a DLCI from the Linux side. We issue SABM messages
  1301. * to the modem which should then reply with a UA, at which point we
  1302. * will move into open state. Opening is done asynchronously with retry
  1303. * running off timers and the responses.
  1304. */
  1305. static void gsm_dlci_begin_open(struct gsm_dlci *dlci)
  1306. {
  1307. struct gsm_mux *gsm = dlci->gsm;
  1308. if (dlci->state == DLCI_OPEN || dlci->state == DLCI_OPENING)
  1309. return;
  1310. dlci->retries = gsm->n2;
  1311. dlci->state = DLCI_OPENING;
  1312. gsm_command(dlci->gsm, dlci->addr, SABM|PF);
  1313. mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
  1314. }
  1315. /**
  1316. * gsm_dlci_begin_close - start channel open procedure
  1317. * @dlci: DLCI to open
  1318. *
  1319. * Commence closing a DLCI from the Linux side. We issue DISC messages
  1320. * to the modem which should then reply with a UA, at which point we
  1321. * will move into closed state. Closing is done asynchronously with retry
  1322. * off timers. We may also receive a DM reply from the other end which
  1323. * indicates the channel was already closed.
  1324. */
  1325. static void gsm_dlci_begin_close(struct gsm_dlci *dlci)
  1326. {
  1327. struct gsm_mux *gsm = dlci->gsm;
  1328. if (dlci->state == DLCI_CLOSED || dlci->state == DLCI_CLOSING)
  1329. return;
  1330. dlci->retries = gsm->n2;
  1331. dlci->state = DLCI_CLOSING;
  1332. gsm_command(dlci->gsm, dlci->addr, DISC|PF);
  1333. mod_timer(&dlci->t1, jiffies + gsm->t1 * HZ / 100);
  1334. }
  1335. /**
  1336. * gsm_dlci_data - data arrived
  1337. * @dlci: channel
  1338. * @data: block of bytes received
  1339. * @len: length of received block
  1340. *
  1341. * A UI or UIH frame has arrived which contains data for a channel
  1342. * other than the control channel. If the relevant virtual tty is
  1343. * open we shovel the bits down it, if not we drop them.
  1344. */
  1345. static void gsm_dlci_data(struct gsm_dlci *dlci, u8 *data, int clen)
  1346. {
  1347. /* krefs .. */
  1348. struct tty_port *port = &dlci->port;
  1349. struct tty_struct *tty = tty_port_tty_get(port);
  1350. unsigned int modem = 0;
  1351. int len = clen;
  1352. if (debug & 16)
  1353. pr_debug("%d bytes for tty %p\n", len, tty);
  1354. if (tty) {
  1355. switch (dlci->adaption) {
  1356. /* Unsupported types */
  1357. /* Packetised interruptible data */
  1358. case 4:
  1359. break;
  1360. /* Packetised uininterruptible voice/data */
  1361. case 3:
  1362. break;
  1363. /* Asynchronous serial with line state in each frame */
  1364. case 2:
  1365. while (gsm_read_ea(&modem, *data++) == 0) {
  1366. len--;
  1367. if (len == 0)
  1368. return;
  1369. }
  1370. gsm_process_modem(tty, dlci, modem, clen);
  1371. /* Line state will go via DLCI 0 controls only */
  1372. case 1:
  1373. default:
  1374. tty_insert_flip_string(tty, data, len);
  1375. tty_flip_buffer_push(tty);
  1376. }
  1377. tty_kref_put(tty);
  1378. }
  1379. }
  1380. /**
  1381. * gsm_dlci_control - data arrived on control channel
  1382. * @dlci: channel
  1383. * @data: block of bytes received
  1384. * @len: length of received block
  1385. *
  1386. * A UI or UIH frame has arrived which contains data for DLCI 0 the
  1387. * control channel. This should contain a command EA followed by
  1388. * control data bytes. The command EA contains a command/response bit
  1389. * and we divide up the work accordingly.
  1390. */
  1391. static void gsm_dlci_command(struct gsm_dlci *dlci, u8 *data, int len)
  1392. {
  1393. /* See what command is involved */
  1394. unsigned int command = 0;
  1395. while (len-- > 0) {
  1396. if (gsm_read_ea(&command, *data++) == 1) {
  1397. int clen = *data++;
  1398. len--;
  1399. /* FIXME: this is properly an EA */
  1400. clen >>= 1;
  1401. /* Malformed command ? */
  1402. if (clen > len)
  1403. return;
  1404. if (command & 1)
  1405. gsm_control_message(dlci->gsm, command,
  1406. data, clen);
  1407. else
  1408. gsm_control_response(dlci->gsm, command,
  1409. data, clen);
  1410. return;
  1411. }
  1412. }
  1413. }
  1414. /*
  1415. * Allocate/Free DLCI channels
  1416. */
  1417. /**
  1418. * gsm_dlci_alloc - allocate a DLCI
  1419. * @gsm: GSM mux
  1420. * @addr: address of the DLCI
  1421. *
  1422. * Allocate and install a new DLCI object into the GSM mux.
  1423. *
  1424. * FIXME: review locking races
  1425. */
  1426. static struct gsm_dlci *gsm_dlci_alloc(struct gsm_mux *gsm, int addr)
  1427. {
  1428. struct gsm_dlci *dlci = kzalloc(sizeof(struct gsm_dlci), GFP_ATOMIC);
  1429. if (dlci == NULL)
  1430. return NULL;
  1431. spin_lock_init(&dlci->lock);
  1432. dlci->fifo = &dlci->_fifo;
  1433. if (kfifo_alloc(&dlci->_fifo, 4096, GFP_KERNEL) < 0) {
  1434. kfree(dlci);
  1435. return NULL;
  1436. }
  1437. skb_queue_head_init(&dlci->skb_list);
  1438. init_timer(&dlci->t1);
  1439. dlci->t1.function = gsm_dlci_t1;
  1440. dlci->t1.data = (unsigned long)dlci;
  1441. tty_port_init(&dlci->port);
  1442. dlci->port.ops = &gsm_port_ops;
  1443. dlci->gsm = gsm;
  1444. dlci->addr = addr;
  1445. dlci->adaption = gsm->adaption;
  1446. dlci->state = DLCI_CLOSED;
  1447. if (addr)
  1448. dlci->data = gsm_dlci_data;
  1449. else
  1450. dlci->data = gsm_dlci_command;
  1451. gsm->dlci[addr] = dlci;
  1452. return dlci;
  1453. }
  1454. /**
  1455. * gsm_dlci_free - release DLCI
  1456. * @dlci: DLCI to destroy
  1457. *
  1458. * Free up a DLCI. Currently to keep the lifetime rules sane we only
  1459. * clean up DLCI objects when the MUX closes rather than as the port
  1460. * is closed down on both the tty and mux levels.
  1461. *
  1462. * Can sleep.
  1463. */
  1464. static void gsm_dlci_free(struct gsm_dlci *dlci)
  1465. {
  1466. struct tty_struct *tty = tty_port_tty_get(&dlci->port);
  1467. if (tty) {
  1468. tty_vhangup(tty);
  1469. tty_kref_put(tty);
  1470. }
  1471. del_timer_sync(&dlci->t1);
  1472. dlci->gsm->dlci[dlci->addr] = NULL;
  1473. kfifo_free(dlci->fifo);
  1474. kfree(dlci);
  1475. }
  1476. /*
  1477. * LAPBish link layer logic
  1478. */
  1479. /**
  1480. * gsm_queue - a GSM frame is ready to process
  1481. * @gsm: pointer to our gsm mux
  1482. *
  1483. * At this point in time a frame has arrived and been demangled from
  1484. * the line encoding. All the differences between the encodings have
  1485. * been handled below us and the frame is unpacked into the structures.
  1486. * The fcs holds the header FCS but any data FCS must be added here.
  1487. */
  1488. static void gsm_queue(struct gsm_mux *gsm)
  1489. {
  1490. struct gsm_dlci *dlci;
  1491. u8 cr;
  1492. int address;
  1493. /* We have to sneak a look at the packet body to do the FCS.
  1494. A somewhat layering violation in the spec */
  1495. if ((gsm->control & ~PF) == UI)
  1496. gsm->fcs = gsm_fcs_add_block(gsm->fcs, gsm->buf, gsm->len);
  1497. if (gsm->encoding == 0){
  1498. /* WARNING: gsm->received_fcs is used for gsm->encoding = 0 only.
  1499. In this case it contain the last piece of data
  1500. required to generate final CRC */
  1501. gsm->fcs = gsm_fcs_add(gsm->fcs, gsm->received_fcs);
  1502. }
  1503. if (gsm->fcs != GOOD_FCS) {
  1504. gsm->bad_fcs++;
  1505. if (debug & 4)
  1506. pr_debug("BAD FCS %02x\n", gsm->fcs);
  1507. return;
  1508. }
  1509. address = gsm->address >> 1;
  1510. if (address >= NUM_DLCI)
  1511. goto invalid;
  1512. cr = gsm->address & 1; /* C/R bit */
  1513. gsm_print_packet("<--", address, cr, gsm->control, gsm->buf, gsm->len);
  1514. cr ^= 1 - gsm->initiator; /* Flip so 1 always means command */
  1515. dlci = gsm->dlci[address];
  1516. switch (gsm->control) {
  1517. case SABM|PF:
  1518. if (cr == 0)
  1519. goto invalid;
  1520. if (dlci == NULL)
  1521. dlci = gsm_dlci_alloc(gsm, address);
  1522. if (dlci == NULL)
  1523. return;
  1524. if (dlci->dead)
  1525. gsm_response(gsm, address, DM);
  1526. else {
  1527. gsm_response(gsm, address, UA);
  1528. gsm_dlci_open(dlci);
  1529. }
  1530. break;
  1531. case DISC|PF:
  1532. if (cr == 0)
  1533. goto invalid;
  1534. if (dlci == NULL || dlci->state == DLCI_CLOSED) {
  1535. gsm_response(gsm, address, DM);
  1536. return;
  1537. }
  1538. /* Real close complete */
  1539. gsm_response(gsm, address, UA);
  1540. gsm_dlci_close(dlci);
  1541. break;
  1542. case UA:
  1543. case UA|PF:
  1544. if (cr == 0 || dlci == NULL)
  1545. break;
  1546. switch (dlci->state) {
  1547. case DLCI_CLOSING:
  1548. gsm_dlci_close(dlci);
  1549. break;
  1550. case DLCI_OPENING:
  1551. gsm_dlci_open(dlci);
  1552. break;
  1553. }
  1554. break;
  1555. case DM: /* DM can be valid unsolicited */
  1556. case DM|PF:
  1557. if (cr)
  1558. goto invalid;
  1559. if (dlci == NULL)
  1560. return;
  1561. gsm_dlci_close(dlci);
  1562. break;
  1563. case UI:
  1564. case UI|PF:
  1565. case UIH:
  1566. case UIH|PF:
  1567. #if 0
  1568. if (cr)
  1569. goto invalid;
  1570. #endif
  1571. if (dlci == NULL || dlci->state != DLCI_OPEN) {
  1572. gsm_command(gsm, address, DM|PF);
  1573. return;
  1574. }
  1575. dlci->data(dlci, gsm->buf, gsm->len);
  1576. break;
  1577. default:
  1578. goto invalid;
  1579. }
  1580. return;
  1581. invalid:
  1582. gsm->malformed++;
  1583. return;
  1584. }
  1585. /**
  1586. * gsm0_receive - perform processing for non-transparency
  1587. * @gsm: gsm data for this ldisc instance
  1588. * @c: character
  1589. *
  1590. * Receive bytes in gsm mode 0
  1591. */
  1592. static void gsm0_receive(struct gsm_mux *gsm, unsigned char c)
  1593. {
  1594. unsigned int len;
  1595. switch (gsm->state) {
  1596. case GSM_SEARCH: /* SOF marker */
  1597. if (c == GSM0_SOF) {
  1598. gsm->state = GSM_ADDRESS;
  1599. gsm->address = 0;
  1600. gsm->len = 0;
  1601. gsm->fcs = INIT_FCS;
  1602. }
  1603. break;
  1604. case GSM_ADDRESS: /* Address EA */
  1605. gsm->fcs = gsm_fcs_add(gsm->fcs, c);
  1606. if (gsm_read_ea(&gsm->address, c))
  1607. gsm->state = GSM_CONTROL;
  1608. break;
  1609. case GSM_CONTROL: /* Control Byte */
  1610. gsm->fcs = gsm_fcs_add(gsm->fcs, c);
  1611. gsm->control = c;
  1612. gsm->state = GSM_LEN0;
  1613. break;
  1614. case GSM_LEN0: /* Length EA */
  1615. gsm->fcs = gsm_fcs_add(gsm->fcs, c);
  1616. if (gsm_read_ea(&gsm->len, c)) {
  1617. if (gsm->len > gsm->mru) {
  1618. gsm->bad_size++;
  1619. gsm->state = GSM_SEARCH;
  1620. break;
  1621. }
  1622. gsm->count = 0;
  1623. if (!gsm->len)
  1624. gsm->state = GSM_FCS;
  1625. else
  1626. gsm->state = GSM_DATA;
  1627. break;
  1628. }
  1629. gsm->state = GSM_LEN1;
  1630. break;
  1631. case GSM_LEN1:
  1632. gsm->fcs = gsm_fcs_add(gsm->fcs, c);
  1633. len = c;
  1634. gsm->len |= len << 7;
  1635. if (gsm->len > gsm->mru) {
  1636. gsm->bad_size++;
  1637. gsm->state = GSM_SEARCH;
  1638. break;
  1639. }
  1640. gsm->count = 0;
  1641. if (!gsm->len)
  1642. gsm->state = GSM_FCS;
  1643. else
  1644. gsm->state = GSM_DATA;
  1645. break;
  1646. case GSM_DATA: /* Data */
  1647. gsm->buf[gsm->count++] = c;
  1648. if (gsm->count == gsm->len)
  1649. gsm->state = GSM_FCS;
  1650. break;
  1651. case GSM_FCS: /* FCS follows the packet */
  1652. gsm->received_fcs = c;
  1653. gsm_queue(gsm);
  1654. gsm->state = GSM_SSOF;
  1655. break;
  1656. case GSM_SSOF:
  1657. if (c == GSM0_SOF) {
  1658. gsm->state = GSM_SEARCH;
  1659. break;
  1660. }
  1661. break;
  1662. }
  1663. }
  1664. /**
  1665. * gsm1_receive - perform processing for non-transparency
  1666. * @gsm: gsm data for this ldisc instance
  1667. * @c: character
  1668. *
  1669. * Receive bytes in mode 1 (Advanced option)
  1670. */
  1671. static void gsm1_receive(struct gsm_mux *gsm, unsigned char c)
  1672. {
  1673. if (c == GSM1_SOF) {
  1674. /* EOF is only valid in frame if we have got to the data state
  1675. and received at least one byte (the FCS) */
  1676. if (gsm->state == GSM_DATA && gsm->count) {
  1677. /* Extract the FCS */
  1678. gsm->count--;
  1679. gsm->fcs = gsm_fcs_add(gsm->fcs, gsm->buf[gsm->count]);
  1680. gsm->len = gsm->count;
  1681. gsm_queue(gsm);
  1682. gsm->state = GSM_START;
  1683. return;
  1684. }
  1685. /* Any partial frame was a runt so go back to start */
  1686. if (gsm->state != GSM_START) {
  1687. gsm->malformed++;
  1688. gsm->state = GSM_START;
  1689. }
  1690. /* A SOF in GSM_START means we are still reading idling or
  1691. framing bytes */
  1692. return;
  1693. }
  1694. if (c == GSM1_ESCAPE) {
  1695. gsm->escape = 1;
  1696. return;
  1697. }
  1698. /* Only an unescaped SOF gets us out of GSM search */
  1699. if (gsm->state == GSM_SEARCH)
  1700. return;
  1701. if (gsm->escape) {
  1702. c ^= GSM1_ESCAPE_BITS;
  1703. gsm->escape = 0;
  1704. }
  1705. switch (gsm->state) {
  1706. case GSM_START: /* First byte after SOF */
  1707. gsm->address = 0;
  1708. gsm->state = GSM_ADDRESS;
  1709. gsm->fcs = INIT_FCS;
  1710. /* Drop through */
  1711. case GSM_ADDRESS: /* Address continuation */
  1712. gsm->fcs = gsm_fcs_add(gsm->fcs, c);
  1713. if (gsm_read_ea(&gsm->address, c))
  1714. gsm->state = GSM_CONTROL;
  1715. break;
  1716. case GSM_CONTROL: /* Control Byte */
  1717. gsm->fcs = gsm_fcs_add(gsm->fcs, c);
  1718. gsm->control = c;
  1719. gsm->count = 0;
  1720. gsm->state = GSM_DATA;
  1721. break;
  1722. case GSM_DATA: /* Data */
  1723. if (gsm->count > gsm->mru) { /* Allow one for the FCS */
  1724. gsm->state = GSM_OVERRUN;
  1725. gsm->bad_size++;
  1726. } else
  1727. gsm->buf[gsm->count++] = c;
  1728. break;
  1729. case GSM_OVERRUN: /* Over-long - eg a dropped SOF */
  1730. break;
  1731. }
  1732. }
  1733. /**
  1734. * gsm_error - handle tty error
  1735. * @gsm: ldisc data
  1736. * @data: byte received (may be invalid)
  1737. * @flag: error received
  1738. *
  1739. * Handle an error in the receipt of data for a frame. Currently we just
  1740. * go back to hunting for a SOF.
  1741. *
  1742. * FIXME: better diagnostics ?
  1743. */
  1744. static void gsm_error(struct gsm_mux *gsm,
  1745. unsigned char data, unsigned char flag)
  1746. {
  1747. gsm->state = GSM_SEARCH;
  1748. gsm->io_error++;
  1749. }
  1750. /**
  1751. * gsm_cleanup_mux - generic GSM protocol cleanup
  1752. * @gsm: our mux
  1753. *
  1754. * Clean up the bits of the mux which are the same for all framing
  1755. * protocols. Remove the mux from the mux table, stop all the timers
  1756. * and then shut down each device hanging up the channels as we go.
  1757. */
  1758. void gsm_cleanup_mux(struct gsm_mux *gsm)
  1759. {
  1760. int i;
  1761. struct gsm_dlci *dlci = gsm->dlci[0];
  1762. struct gsm_msg *txq;
  1763. gsm->dead = 1;
  1764. spin_lock(&gsm_mux_lock);
  1765. for (i = 0; i < MAX_MUX; i++) {
  1766. if (gsm_mux[i] == gsm) {
  1767. gsm_mux[i] = NULL;
  1768. break;
  1769. }
  1770. }
  1771. spin_unlock(&gsm_mux_lock);
  1772. WARN_ON(i == MAX_MUX);
  1773. del_timer_sync(&gsm->t2_timer);
  1774. /* Now we are sure T2 has stopped */
  1775. if (dlci) {
  1776. dlci->dead = 1;
  1777. gsm_dlci_begin_close(dlci);
  1778. wait_event_interruptible(gsm->event,
  1779. dlci->state == DLCI_CLOSED);
  1780. }
  1781. /* Free up any link layer users */
  1782. for (i = 0; i < NUM_DLCI; i++)
  1783. if (gsm->dlci[i])
  1784. gsm_dlci_free(gsm->dlci[i]);
  1785. /* Now wipe the queues */
  1786. for (txq = gsm->tx_head; txq != NULL; txq = gsm->tx_head) {
  1787. gsm->tx_head = txq->next;
  1788. kfree(txq);
  1789. }
  1790. gsm->tx_tail = NULL;
  1791. }
  1792. EXPORT_SYMBOL_GPL(gsm_cleanup_mux);
  1793. /**
  1794. * gsm_activate_mux - generic GSM setup
  1795. * @gsm: our mux
  1796. *
  1797. * Set up the bits of the…