PageRenderTime 58ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/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
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  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 mux which are the same for all framing
  1798. * protocols. Add the mux to the mux table so it can be opened and
  1799. * finally kick off connecting to DLCI 0 on the modem.
  1800. */
  1801. int gsm_activate_mux(struct gsm_mux *gsm)
  1802. {
  1803. struct gsm_dlci *dlci;
  1804. int i = 0;
  1805. init_timer(&gsm->t2_timer);
  1806. gsm->t2_timer.function = gsm_control_retransmit;
  1807. gsm->t2_timer.data = (unsigned long)gsm;
  1808. init_waitqueue_head(&gsm->event);
  1809. spin_lock_init(&gsm->control_lock);
  1810. spin_lock_init(&gsm->tx_lock);
  1811. if (gsm->encoding == 0)
  1812. gsm->receive = gsm0_receive;
  1813. else
  1814. gsm->receive = gsm1_receive;
  1815. gsm->error = gsm_error;
  1816. spin_lock(&gsm_mux_lock);
  1817. for (i = 0; i < MAX_MUX; i++) {
  1818. if (gsm_mux[i] == NULL) {
  1819. gsm_mux[i] = gsm;
  1820. break;
  1821. }
  1822. }
  1823. spin_unlock(&gsm_mux_lock);
  1824. if (i == MAX_MUX)
  1825. return -EBUSY;
  1826. dlci = gsm_dlci_alloc(gsm, 0);
  1827. if (dlci == NULL)
  1828. return -ENOMEM;
  1829. gsm->dead = 0; /* Tty opens are now permissible */
  1830. return 0;
  1831. }
  1832. EXPORT_SYMBOL_GPL(gsm_activate_mux);
  1833. /**
  1834. * gsm_free_mux - free up a mux
  1835. * @mux: mux to free
  1836. *
  1837. * Dispose of allocated resources for a dead mux. No refcounting
  1838. * at present so the mux must be truly dead.
  1839. */
  1840. void gsm_free_mux(struct gsm_mux *gsm)
  1841. {
  1842. kfree(gsm->txframe);
  1843. kfree(gsm->buf);
  1844. kfree(gsm);
  1845. }
  1846. EXPORT_SYMBOL_GPL(gsm_free_mux);
  1847. /**
  1848. * gsm_alloc_mux - allocate a mux
  1849. *
  1850. * Creates a new mux ready for activation.
  1851. */
  1852. struct gsm_mux *gsm_alloc_mux(void)
  1853. {
  1854. struct gsm_mux *gsm = kzalloc(sizeof(struct gsm_mux), GFP_KERNEL);
  1855. if (gsm == NULL)
  1856. return NULL;
  1857. gsm->buf = kmalloc(MAX_MRU + 1, GFP_KERNEL);
  1858. if (gsm->buf == NULL) {
  1859. kfree(gsm);
  1860. return NULL;
  1861. }
  1862. gsm->txframe = kmalloc(2 * MAX_MRU + 2, GFP_KERNEL);
  1863. if (gsm->txframe == NULL) {
  1864. kfree(gsm->buf);
  1865. kfree(gsm);
  1866. return NULL;
  1867. }
  1868. spin_lock_init(&gsm->lock);
  1869. gsm->t1 = T1;
  1870. gsm->t2 = T2;
  1871. gsm->n2 = N2;
  1872. gsm->ftype = UIH;
  1873. gsm->initiator = 0;
  1874. gsm->adaption = 1;
  1875. gsm->encoding = 1;
  1876. gsm->mru = 64; /* Default to encoding 1 so these should be 64 */
  1877. gsm->mtu = 64;
  1878. gsm->dead = 1; /* Avoid early tty opens */
  1879. return gsm;
  1880. }
  1881. EXPORT_SYMBOL_GPL(gsm_alloc_mux);
  1882. /**
  1883. * gsmld_output - write to link
  1884. * @gsm: our mux
  1885. * @data: bytes to output
  1886. * @len: size
  1887. *
  1888. * Write a block of data from the GSM mux to the data channel. This
  1889. * will eventually be serialized from above but at the moment isn't.
  1890. */
  1891. static int gsmld_output(struct gsm_mux *gsm, u8 *data, int len)
  1892. {
  1893. if (tty_write_room(gsm->tty) < len) {
  1894. set_bit(TTY_DO_WRITE_WAKEUP, &gsm->tty->flags);
  1895. return -ENOSPC;
  1896. }
  1897. if (debug & 4)
  1898. print_hex_dump_bytes("gsmld_output: ", DUMP_PREFIX_OFFSET,
  1899. data, len);
  1900. gsm->tty->ops->write(gsm->tty, data, len);
  1901. return len;
  1902. }
  1903. /**
  1904. * gsmld_attach_gsm - mode set up
  1905. * @tty: our tty structure
  1906. * @gsm: our mux
  1907. *
  1908. * Set up the MUX for basic mode and commence connecting to the
  1909. * modem. Currently called from the line discipline set up but
  1910. * will need moving to an ioctl path.
  1911. */
  1912. static int gsmld_attach_gsm(struct tty_struct *tty, struct gsm_mux *gsm)
  1913. {
  1914. int ret;
  1915. gsm->tty = tty_kref_get(tty);
  1916. gsm->output = gsmld_output;
  1917. ret = gsm_activate_mux(gsm);
  1918. if (ret != 0)
  1919. tty_kref_put(gsm->tty);
  1920. return ret;
  1921. }
  1922. /**
  1923. * gsmld_detach_gsm - stop doing 0710 mux
  1924. * @tty: tty attached to the mux
  1925. * @gsm: mux
  1926. *
  1927. * Shutdown and then clean up the resources used by the line discipline
  1928. */
  1929. static void gsmld_detach_gsm(struct tty_struct *tty, struct gsm_mux *gsm)
  1930. {
  1931. WARN_ON(tty != gsm->tty);
  1932. gsm_cleanup_mux(gsm);
  1933. tty_kref_put(gsm->tty);
  1934. gsm->tty = NULL;
  1935. }
  1936. static void gsmld_receive_buf(struct tty_struct *tty, const unsigned char *cp,
  1937. char *fp, int count)
  1938. {
  1939. struct gsm_mux *gsm = tty->disc_data;
  1940. const unsigned char *dp;
  1941. char *f;
  1942. int i;
  1943. char buf[64];
  1944. char flags;
  1945. if (debug & 4)
  1946. print_hex_dump_bytes("gsmld_receive: ", DUMP_PREFIX_OFFSET,
  1947. cp, count);
  1948. for (i = count, dp = cp, f = fp; i; i--, dp++) {
  1949. flags = *f++;
  1950. switch (flags) {
  1951. case TTY_NORMAL:
  1952. gsm->receive(gsm, *dp);
  1953. break;
  1954. case TTY_OVERRUN:
  1955. case TTY_BREAK:
  1956. case TTY_PARITY:
  1957. case TTY_FRAME:
  1958. gsm->error(gsm, *dp, flags);
  1959. break;
  1960. default:
  1961. WARN_ONCE("%s: unknown flag %d\n",
  1962. tty_name(tty, buf), flags);
  1963. break;
  1964. }
  1965. }
  1966. /* FASYNC if needed ? */
  1967. /* If clogged call tty_throttle(tty); */
  1968. }
  1969. /**
  1970. * gsmld_chars_in_buffer - report available bytes
  1971. * @tty: tty device
  1972. *
  1973. * Report the number of characters buffered to be delivered to user
  1974. * at this instant in time.
  1975. *
  1976. * Locking: gsm lock
  1977. */
  1978. static ssize_t gsmld_chars_in_buffer(struct tty_struct *tty)
  1979. {
  1980. return 0;
  1981. }
  1982. /**
  1983. * gsmld_flush_buffer - clean input queue
  1984. * @tty: terminal device
  1985. *
  1986. * Flush the input buffer. Called when the line discipline is
  1987. * being closed, when the tty layer wants the buffer flushed (eg
  1988. * at hangup).
  1989. */
  1990. static void gsmld_flush_buffer(struct tty_struct *tty)
  1991. {
  1992. }
  1993. /**
  1994. * gsmld_close - close the ldisc for this tty
  1995. * @tty: device
  1996. *
  1997. * Called from the terminal layer when this line discipline is
  1998. * being shut down, either because of a close or becsuse of a
  1999. * discipline change. The function will not be called while other
  2000. * ldisc methods are in progress.
  2001. */
  2002. static void gsmld_close(struct tty_struct *tty)
  2003. {
  2004. struct gsm_mux *gsm = tty->disc_data;
  2005. gsmld_detach_gsm(tty, gsm);
  2006. gsmld_flush_buffer(tty);
  2007. /* Do other clean up here */
  2008. gsm_free_mux(gsm);
  2009. }
  2010. /**
  2011. * gsmld_open - open an ldisc
  2012. * @tty: terminal to open
  2013. *
  2014. * Called when this line discipline is being attached to the
  2015. * terminal device. Can sleep. Called serialized so that no
  2016. * other events will occur in parallel. No further open will occur
  2017. * until a close.
  2018. */
  2019. static int gsmld_open(struct tty_struct *tty)
  2020. {
  2021. struct gsm_mux *gsm;
  2022. if (tty->ops->write == NULL)
  2023. return -EINVAL;
  2024. /* Attach our ldisc data */
  2025. gsm = gsm_alloc_mux();
  2026. if (gsm == NULL)
  2027. return -ENOMEM;
  2028. tty->disc_data = gsm;
  2029. tty->receive_room = 65536;
  2030. /* Attach the initial passive connection */
  2031. gsm->encoding = 1;
  2032. return gsmld_attach_gsm(tty, gsm);
  2033. }
  2034. /**
  2035. * gsmld_write_wakeup - asynchronous I/O notifier
  2036. * @tty: tty device
  2037. *
  2038. * Required for the ptys, serial driver etc. since processes
  2039. * that attach themselves to the master and rely on ASYNC
  2040. * IO must be woken up
  2041. */
  2042. static void gsmld_write_wakeup(struct tty_struct *tty)
  2043. {
  2044. struct gsm_mux *gsm = tty->disc_data;
  2045. unsigned long flags;
  2046. /* Queue poll */
  2047. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  2048. gsm_data_kick(gsm);
  2049. if (gsm->tx_bytes < TX_THRESH_LO) {
  2050. spin_lock_irqsave(&gsm->tx_lock, flags);
  2051. gsm_dlci_data_sweep(gsm);
  2052. spin_unlock_irqrestore(&gsm->tx_lock, flags);
  2053. }
  2054. }
  2055. /**
  2056. * gsmld_read - read function for tty
  2057. * @tty: tty device
  2058. * @file: file object
  2059. * @buf: userspace buffer pointer
  2060. * @nr: size of I/O
  2061. *
  2062. * Perform reads for the line discipline. We are guaranteed that the
  2063. * line discipline will not be closed under us but we may get multiple
  2064. * parallel readers and must handle this ourselves. We may also get
  2065. * a hangup. Always called in user context, may sleep.
  2066. *
  2067. * This code must be sure never to sleep through a hangup.
  2068. */
  2069. static ssize_t gsmld_read(struct tty_struct *tty, struct file *file,
  2070. unsigned char __user *buf, size_t nr)
  2071. {
  2072. return -EOPNOTSUPP;
  2073. }
  2074. /**
  2075. * gsmld_write - write function for tty
  2076. * @tty: tty device
  2077. * @file: file object
  2078. * @buf: userspace buffer pointer
  2079. * @nr: size of I/O
  2080. *
  2081. * Called when the owner of the device wants to send a frame
  2082. * itself (or some other control data). The data is transferred
  2083. * as-is and must be properly framed and checksummed as appropriate
  2084. * by userspace. Frames are either sent whole or not at all as this
  2085. * avoids pain user side.
  2086. */
  2087. static ssize_t gsmld_write(struct tty_struct *tty, struct file *file,
  2088. const unsigned char *buf, size_t nr)
  2089. {
  2090. int space = tty_write_room(tty);
  2091. if (space >= nr)
  2092. return tty->ops->write(tty, buf, nr);
  2093. set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  2094. return -ENOBUFS;
  2095. }
  2096. /**
  2097. * gsmld_poll - poll method for N_GSM0710
  2098. * @tty: terminal device
  2099. * @file: file accessing it
  2100. * @wait: poll table
  2101. *
  2102. * Called when the line discipline is asked to poll() for data or
  2103. * for special events. This code is not serialized with respect to
  2104. * other events save open/close.
  2105. *
  2106. * This code must be sure never to sleep through a hangup.
  2107. * Called without the kernel lock held - fine
  2108. */
  2109. static unsigned int gsmld_poll(struct tty_struct *tty, struct file *file,
  2110. poll_table *wait)
  2111. {
  2112. unsigned int mask = 0;
  2113. struct gsm_mux *gsm = tty->disc_data;
  2114. poll_wait(file, &tty->read_wait, wait);
  2115. poll_wait(file, &tty->write_wait, wait);
  2116. if (tty_hung_up_p(file))
  2117. mask |= POLLHUP;
  2118. if (!tty_is_writelocked(tty) && tty_write_room(tty) > 0)
  2119. mask |= POLLOUT | POLLWRNORM;
  2120. if (gsm->dead)
  2121. mask |= POLLHUP;
  2122. return mask;
  2123. }
  2124. static int gsmld_config(struct tty_struct *tty, struct gsm_mux *gsm,
  2125. struct gsm_config *c)
  2126. {
  2127. int need_close = 0;
  2128. int need_restart = 0;
  2129. /* Stuff we don't support yet - UI or I frame transport, windowing */
  2130. if ((c->adaption != 1 && c->adaption != 2) || c->k)
  2131. return -EOPNOTSUPP;
  2132. /* Check the MRU/MTU range looks sane */
  2133. if (c->mru > MAX_MRU || c->mtu > MAX_MTU || c->mru < 8 || c->mtu < 8)
  2134. return -EINVAL;
  2135. if (c->n2 < 3)
  2136. return -EINVAL;
  2137. if (c->encapsulation > 1) /* Basic, advanced, no I */
  2138. return -EINVAL;
  2139. if (c->initiator > 1)
  2140. return -EINVAL;
  2141. if (c->i == 0 || c->i > 2) /* UIH and UI only */
  2142. return -EINVAL;
  2143. /*
  2144. * See what is needed for reconfiguration
  2145. */
  2146. /* Timing fields */
  2147. if (c->t1 != 0 && c->t1 != gsm->t1)
  2148. need_restart = 1;
  2149. if (c->t2 != 0 && c->t2 != gsm->t2)
  2150. need_restart = 1;
  2151. if (c->encapsulation != gsm->encoding)
  2152. need_restart = 1;
  2153. if (c->adaption != gsm->adaption)
  2154. need_restart = 1;
  2155. /* Requires care */
  2156. if (c->initiator != gsm->initiator)
  2157. need_close = 1;
  2158. if (c->mru != gsm->mru)
  2159. need_restart = 1;
  2160. if (c->mtu != gsm->mtu)
  2161. need_restart = 1;
  2162. /*
  2163. * Close down what is needed, restart and initiate the new
  2164. * configuration
  2165. */
  2166. if (need_close || need_restart) {
  2167. gsm_dlci_begin_close(gsm->dlci[0]);
  2168. /* This will timeout if the link is down due to N2 expiring */
  2169. wait_event_interruptible(gsm->event,
  2170. gsm->dlci[0]->state == DLCI_CLOSED);
  2171. if (signal_pending(current))
  2172. return -EINTR;
  2173. }
  2174. if (need_restart)
  2175. gsm_cleanup_mux(gsm);
  2176. gsm->initiator = c->initiator;
  2177. gsm->mru = c->mru;
  2178. gsm->mtu = c->mtu;
  2179. gsm->encoding = c->encapsulation;
  2180. gsm->adaption = c->adaption;
  2181. gsm->n2 = c->n2;
  2182. if (c->i == 1)
  2183. gsm->ftype = UIH;
  2184. else if (c->i == 2)
  2185. gsm->ftype = UI;
  2186. if (c->t1)
  2187. gsm->t1 = c->t1;
  2188. if (c->t2)
  2189. gsm->t2 = c->t2;
  2190. /* FIXME: We need to separate activation/deactivation from adding
  2191. and removing from the mux array */
  2192. if (need_restart)
  2193. gsm_activate_mux(gsm);
  2194. if (gsm->initiator && need_close)
  2195. gsm_dlci_begin_open(gsm->dlci[0]);
  2196. return 0;
  2197. }
  2198. static int gsmld_ioctl(struct tty_struct *tty, struct file *file,
  2199. unsigned int cmd, unsigned long arg)
  2200. {
  2201. struct gsm_config c;
  2202. struct gsm_mux *gsm = tty->disc_data;
  2203. switch (cmd) {
  2204. case GSMIOC_GETCONF:
  2205. memset(&c, 0, sizeof(c));
  2206. c.adaption = gsm->adaption;
  2207. c.encapsulation = gsm->encoding;
  2208. c.initiator = gsm->initiator;
  2209. c.t1 = gsm->t1;
  2210. c.t2 = gsm->t2;
  2211. c.t3 = 0; /* Not supported */
  2212. c.n2 = gsm->n2;
  2213. if (gsm->ftype == UIH)
  2214. c.i = 1;
  2215. else
  2216. c.i = 2;
  2217. pr_debug("Ftype %d i %d\n", gsm->ftype, c.i);
  2218. c.mru = gsm->mru;
  2219. c.mtu = gsm->mtu;
  2220. c.k = 0;
  2221. if (copy_to_user((void *)arg, &c, sizeof(c)))
  2222. return -EFAULT;
  2223. return 0;
  2224. case GSMIOC_SETCONF:
  2225. if (copy_from_user(&c, (void *)arg, sizeof(c)))
  2226. return -EFAULT;
  2227. return gsmld_config(tty, gsm, &c);
  2228. default:
  2229. return n_tty_ioctl_helper(tty, file, cmd, arg);
  2230. }
  2231. }
  2232. /* Line discipline for real tty */
  2233. struct tty_ldisc_ops tty_ldisc_packet = {
  2234. .owner = THIS_MODULE,
  2235. .magic = TTY_LDISC_MAGIC,
  2236. .name = "n_gsm",
  2237. .open = gsmld_open,
  2238. .close = gsmld_close,
  2239. .flush_buffer = gsmld_flush_buffer,
  2240. .chars_in_buffer = gsmld_chars_in_buffer,
  2241. .read = gsmld_read,
  2242. .write = gsmld_write,
  2243. .ioctl = gsmld_ioctl,
  2244. .poll = gsmld_poll,
  2245. .receive_buf = gsmld_receive_buf,
  2246. .write_wakeup = gsmld_write_wakeup
  2247. };
  2248. /*
  2249. * Virtual tty side
  2250. */
  2251. #define TX_SIZE 512
  2252. static int gsmtty_modem_update(struct gsm_dlci *dlci, u8 brk)
  2253. {
  2254. u8 modembits[5];
  2255. struct gsm_control *ctrl;
  2256. int len = 2;
  2257. if (brk)
  2258. len++;
  2259. modembits[0] = len << 1 | EA; /* Data bytes */
  2260. modembits[1] = dlci->addr << 2 | 3; /* DLCI, EA, 1 */
  2261. modembits[2] = gsm_encode_modem(dlci) << 1 | EA;
  2262. if (brk)
  2263. modembits[3] = brk << 4 | 2 | EA; /* Valid, EA */
  2264. ctrl = gsm_control_send(dlci->gsm, CMD_MSC, modembits, len + 1);
  2265. if (ctrl == NULL)
  2266. return -ENOMEM;
  2267. return gsm_control_wait(dlci->gsm, ctrl);
  2268. }
  2269. static int gsm_carrier_raised(struct tty_port *port)
  2270. {
  2271. struct gsm_dlci *dlci = container_of(port, struct gsm_dlci, port);
  2272. /* Not yet open so no carrier info */
  2273. if (dlci->state != DLCI_OPEN)
  2274. return 0;
  2275. if (debug & 2)
  2276. return 1;
  2277. return dlci->modem_rx & TIOCM_CD;
  2278. }
  2279. static void gsm_dtr_rts(struct tty_port *port, int onoff)
  2280. {
  2281. struct gsm_dlci *dlci = container_of(port, struct gsm_dlci, port);
  2282. unsigned int modem_tx = dlci->modem_tx;
  2283. if (onoff)
  2284. modem_tx |= TIOCM_DTR | TIOCM_RTS;
  2285. else
  2286. modem_tx &= ~(TIOCM_DTR | TIOCM_RTS);
  2287. if (modem_tx != dlci->modem_tx) {
  2288. dlci->modem_tx = modem_tx;
  2289. gsmtty_modem_update(dlci, 0);
  2290. }
  2291. }
  2292. static const struct tty_port_operations gsm_port_ops = {
  2293. .carrier_raised = gsm_carrier_raised,
  2294. .dtr_rts = gsm_dtr_rts,
  2295. };
  2296. static int gsmtty_open(struct tty_struct *tty, struct file *filp)
  2297. {
  2298. struct gsm_mux *gsm;
  2299. struct gsm_dlci *dlci;
  2300. struct tty_port *port;
  2301. unsigned int line = tty->index;
  2302. unsigned int mux = line >> 6;
  2303. line = line & 0x3F;
  2304. if (mux >= MAX_MUX)
  2305. return -ENXIO;
  2306. /* FIXME: we need to lock gsm_mux for lifetimes of ttys eventually */
  2307. if (gsm_mux[mux] == NULL)
  2308. return -EUNATCH;
  2309. if (line == 0 || line > 61) /* 62/63 reserved */
  2310. return -ECHRNG;
  2311. gsm = gsm_mux[mux];
  2312. if (gsm->dead)
  2313. return -EL2HLT;
  2314. dlci = gsm->dlci[line];
  2315. if (dlci == NULL)
  2316. dlci = gsm_dlci_alloc(gsm, line);
  2317. if (dlci == NULL)
  2318. return -ENOMEM;
  2319. port = &dlci->port;
  2320. port->count++;
  2321. tty->driver_data = dlci;
  2322. tty_port_tty_set(port, tty);
  2323. dlci->modem_rx = 0;
  2324. /* We could in theory open and close before we wait - eg if we get
  2325. a DM straight back. This is ok as that will have caused a hangup */
  2326. set_bit(ASYNCB_INITIALIZED, &port->flags);
  2327. /* Start sending off SABM messages */
  2328. gsm_dlci_begin_open(dlci);
  2329. /* And wait for virtual carrier */
  2330. return tty_port_block_til_ready(port, tty, filp);
  2331. }
  2332. static void gsmtty_close(struct tty_struct *tty, struct file *filp)
  2333. {
  2334. struct gsm_dlci *dlci = tty->driver_data;
  2335. if (dlci == NULL)
  2336. return;
  2337. if (tty_port_close_start(&dlci->port, tty, filp) == 0)
  2338. return;
  2339. gsm_dlci_begin_close(dlci);
  2340. tty_port_close_end(&dlci->port, tty);
  2341. tty_port_tty_set(&dlci->port, NULL);
  2342. }
  2343. static void gsmtty_hangup(struct tty_struct *tty)
  2344. {
  2345. struct gsm_dlci *dlci = tty->driver_data;
  2346. tty_port_hangup(&dlci->port);
  2347. gsm_dlci_begin_close(dlci);
  2348. }
  2349. static int gsmtty_write(struct tty_struct *tty, const unsigned char *buf,
  2350. int len)
  2351. {
  2352. struct gsm_dlci *dlci = tty->driver_data;
  2353. /* Stuff the bytes into the fifo queue */
  2354. int sent = kfifo_in_locked(dlci->fifo, buf, len, &dlci->lock);
  2355. /* Need to kick the channel */
  2356. gsm_dlci_data_kick(dlci);
  2357. return sent;
  2358. }
  2359. static int gsmtty_write_room(struct tty_struct *tty)
  2360. {
  2361. struct gsm_dlci *dlci = tty->driver_data;
  2362. return TX_SIZE - kfifo_len(dlci->fifo);
  2363. }
  2364. static int gsmtty_chars_in_buffer(struct tty_struct *tty)
  2365. {
  2366. struct gsm_dlci *dlci = tty->driver_data;
  2367. return kfifo_len(dlci->fifo);
  2368. }
  2369. static void gsmtty_flush_buffer(struct tty_struct *tty)
  2370. {
  2371. struct gsm_dlci *dlci = tty->driver_data;
  2372. /* Caution needed: If we implement reliable transport classes
  2373. then the data being transmitted can't simply be junked once
  2374. it has first hit the stack. Until then we can just blow it
  2375. away */
  2376. kfifo_reset(dlci->fifo);
  2377. /* Need to unhook this DLCI from the transmit queue logic */
  2378. }
  2379. static void gsmtty_wait_until_sent(struct tty_struct *tty, int timeout)
  2380. {
  2381. /* The FIFO handles the queue so the kernel will do the right
  2382. thing waiting on chars_in_buffer before calling us. No work
  2383. to do here */
  2384. }
  2385. static int gsmtty_tiocmget(struct tty_struct *tty)
  2386. {
  2387. struct gsm_dlci *dlci = tty->driver_data;
  2388. return dlci->modem_rx;
  2389. }
  2390. static int gsmtty_tiocmset(struct tty_struct *tty,
  2391. unsigned int set, unsigned int clear)
  2392. {
  2393. struct gsm_dlci *dlci = tty->driver_data;
  2394. unsigned int modem_tx = dlci->modem_tx;
  2395. modem_tx &= clear;
  2396. modem_tx |= set;
  2397. if (modem_tx != dlci->modem_tx) {
  2398. dlci->modem_tx = modem_tx;
  2399. return gsmtty_modem_update(dlci, 0);
  2400. }
  2401. return 0;
  2402. }
  2403. static int gsmtty_ioctl(struct tty_struct *tty,
  2404. unsigned int cmd, unsigned long arg)
  2405. {
  2406. return -ENOIOCTLCMD;
  2407. }
  2408. static void gsmtty_set_termios(struct tty_struct *tty, struct ktermios *old)
  2409. {
  2410. /* For the moment its fixed. In actual fact the speed information
  2411. for the virtual channel can be propogated in both directions by
  2412. the RPN control message. This however rapidly gets nasty as we
  2413. then have to remap modem signals each way according to whether
  2414. our virtual cable is null modem etc .. */
  2415. tty_termios_copy_hw(tty->termios, old);
  2416. }
  2417. static void gsmtty_throttle(struct tty_struct *tty)
  2418. {
  2419. struct gsm_dlci *dlci = tty->driver_data;
  2420. if (tty->termios->c_cflag & CRTSCTS)
  2421. dlci->modem_tx &= ~TIOCM_DTR;
  2422. dlci->throttled = 1;
  2423. /* Send an MSC with DTR cleared */
  2424. gsmtty_modem_update(dlci, 0);
  2425. }
  2426. static void gsmtty_unthrottle(struct tty_struct *tty)
  2427. {
  2428. struct gsm_dlci *dlci = tty->driver_data;
  2429. if (tty->termios->c_cflag & CRTSCTS)
  2430. dlci->modem_tx |= TIOCM_DTR;
  2431. dlci->throttled = 0;
  2432. /* Send an MSC with DTR set */
  2433. gsmtty_modem_update(dlci, 0);
  2434. }
  2435. static int gsmtty_break_ctl(struct tty_struct *tty, int state)
  2436. {
  2437. struct gsm_dlci *dlci = tty->driver_data;
  2438. int encode = 0; /* Off */
  2439. if (state == -1) /* "On indefinitely" - we can't encode this
  2440. properly */
  2441. encode = 0x0F;
  2442. else if (state > 0) {
  2443. encode = state / 200; /* mS to encoding */
  2444. if (encode > 0x0F)
  2445. encode = 0x0F; /* Best effort */
  2446. }
  2447. return gsmtty_modem_update(dlci, encode);
  2448. }
  2449. static struct tty_driver *gsm_tty_driver;
  2450. /* Virtual ttys for the demux */
  2451. static const struct tty_operations gsmtty_ops = {
  2452. .open = gsmtty_open,
  2453. .close = gsmtty_close,
  2454. .write = gsmtty_write,
  2455. .write_room = gsmtty_write_room,
  2456. .chars_in_buffer = gsmtty_chars_in_buffer,
  2457. .flush_buffer = gsmtty_flush_buffer,
  2458. .ioctl = gsmtty_ioctl,
  2459. .throttle = gsmtty_throttle,
  2460. .unthrottle = gsmtty_unthrottle,
  2461. .set_termios = gsmtty_set_termios,
  2462. .hangup = gsmtty_hangup,
  2463. .wait_until_sent = gsmtty_wait_until_sent,
  2464. .tiocmget = gsmtty_tiocmget,
  2465. .tiocmset = gsmtty_tiocmset,
  2466. .break_ctl = gsmtty_break_ctl,
  2467. };
  2468. static int __init gsm_init(void)
  2469. {
  2470. /* Fill in our line protocol discipline, and register it */
  2471. int status = tty_register_ldisc(N_GSM0710, &tty_ldisc_packet);
  2472. if (status != 0) {
  2473. pr_err("n_gsm: can't register line discipline (err = %d)\n",
  2474. status);
  2475. return status;
  2476. }
  2477. gsm_tty_driver = alloc_tty_driver(256);
  2478. if (!gsm_tty_driver) {
  2479. tty_unregister_ldisc(N_GSM0710);
  2480. pr_err("gsm_init: tty allocation failed.\n");
  2481. return -EINVAL;
  2482. }
  2483. gsm_tty_driver->owner = THIS_MODULE;
  2484. gsm_tty_driver->driver_name = "gsmtty";
  2485. gsm_tty_driver->name = "gsmtty";
  2486. gsm_tty_driver->major = 0; /* Dynamic */
  2487. gsm_tty_driver->minor_start = 0;
  2488. gsm_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
  2489. gsm_tty_driver->subtype = SERIAL_TYPE_NORMAL;
  2490. gsm_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV
  2491. | TTY_DRIVER_HARDWARE_BREAK;
  2492. gsm_tty_driver->init_termios = tty_std_termios;
  2493. /* Fixme */
  2494. gsm_tty_driver->init_termios.c_lflag &= ~ECHO;
  2495. tty_set_operations(gsm_tty_driver, &gsmtty_ops);
  2496. spin_lock_init(&gsm_mux_lock);
  2497. if (tty_register_driver(gsm_tty_driver)) {
  2498. put_tty_driver(gsm_tty_driver);
  2499. tty_unregister_ldisc(N_GSM0710);
  2500. pr_err("gsm_init: tty registration failed.\n");
  2501. return -EBUSY;
  2502. }
  2503. pr_debug("gsm_init: loaded as %d,%d.\n",
  2504. gsm_tty_driver->major, gsm_tty_driver->minor_start);
  2505. return 0;
  2506. }
  2507. static void __exit gsm_exit(void)
  2508. {
  2509. int status = tty_unregister_ldisc(N_GSM0710);
  2510. if (status != 0)
  2511. pr_err("n_gsm: can't unregister line discipline (err = %d)\n",
  2512. status);
  2513. tty_unregister_driver(gsm_tty_driver);
  2514. put_tty_driver(gsm_tty_driver);
  2515. }
  2516. module_init(gsm_init);
  2517. module_exit(gsm_exit);
  2518. MODULE_LICENSE("GPL");
  2519. MODULE_ALIAS_LDISC(N_GSM0710);