PageRenderTime 70ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/net/irda/irlap_event.c

http://github.com/mirrors/linux
C | 2316 lines | 1309 code | 307 blank | 700 comment | 241 complexity | c113c221fc163573062b46af5303a35b MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.0
  1. /*********************************************************************
  2. *
  3. * Filename: irlap_event.c
  4. * Version: 0.9
  5. * Description: IrLAP state machine implementation
  6. * Status: Experimental.
  7. * Author: Dag Brattli <dag@brattli.net>
  8. * Created at: Sat Aug 16 00:59:29 1997
  9. * Modified at: Sat Dec 25 21:07:57 1999
  10. * Modified by: Dag Brattli <dag@brattli.net>
  11. *
  12. * Copyright (c) 1998-2000 Dag Brattli <dag@brattli.net>,
  13. * Copyright (c) 1998 Thomas Davis <ratbert@radiks.net>
  14. * All Rights Reserved.
  15. * Copyright (c) 2000-2003 Jean Tourrilhes <jt@hpl.hp.com>
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License as
  19. * published by the Free Software Foundation; either version 2 of
  20. * the License, or (at your option) any later version.
  21. *
  22. * Neither Dag Brattli nor University of Tromsø admit liability nor
  23. * provide warranty for any of this software. This material is
  24. * provided "AS-IS" and at no charge.
  25. *
  26. ********************************************************************/
  27. #include <linux/string.h>
  28. #include <linux/kernel.h>
  29. #include <linux/delay.h>
  30. #include <linux/skbuff.h>
  31. #include <linux/slab.h>
  32. #include <net/irda/irda.h>
  33. #include <net/irda/irlap_event.h>
  34. #include <net/irda/timer.h>
  35. #include <net/irda/irlap.h>
  36. #include <net/irda/irlap_frame.h>
  37. #include <net/irda/qos.h>
  38. #include <net/irda/parameters.h>
  39. #include <net/irda/irlmp.h> /* irlmp_flow_indication(), ... */
  40. #include <net/irda/irda_device.h>
  41. #ifdef CONFIG_IRDA_FAST_RR
  42. int sysctl_fast_poll_increase = 50;
  43. #endif
  44. static int irlap_state_ndm (struct irlap_cb *self, IRLAP_EVENT event,
  45. struct sk_buff *skb, struct irlap_info *info);
  46. static int irlap_state_query (struct irlap_cb *self, IRLAP_EVENT event,
  47. struct sk_buff *skb, struct irlap_info *info);
  48. static int irlap_state_reply (struct irlap_cb *self, IRLAP_EVENT event,
  49. struct sk_buff *skb, struct irlap_info *info);
  50. static int irlap_state_conn (struct irlap_cb *self, IRLAP_EVENT event,
  51. struct sk_buff *skb, struct irlap_info *info);
  52. static int irlap_state_setup (struct irlap_cb *self, IRLAP_EVENT event,
  53. struct sk_buff *skb, struct irlap_info *info);
  54. static int irlap_state_offline(struct irlap_cb *self, IRLAP_EVENT event,
  55. struct sk_buff *skb, struct irlap_info *info);
  56. static int irlap_state_xmit_p (struct irlap_cb *self, IRLAP_EVENT event,
  57. struct sk_buff *skb, struct irlap_info *info);
  58. static int irlap_state_pclose (struct irlap_cb *self, IRLAP_EVENT event,
  59. struct sk_buff *skb, struct irlap_info *info);
  60. static int irlap_state_nrm_p (struct irlap_cb *self, IRLAP_EVENT event,
  61. struct sk_buff *skb, struct irlap_info *info);
  62. static int irlap_state_reset_wait(struct irlap_cb *self, IRLAP_EVENT event,
  63. struct sk_buff *skb, struct irlap_info *info);
  64. static int irlap_state_reset (struct irlap_cb *self, IRLAP_EVENT event,
  65. struct sk_buff *skb, struct irlap_info *info);
  66. static int irlap_state_nrm_s (struct irlap_cb *self, IRLAP_EVENT event,
  67. struct sk_buff *skb, struct irlap_info *info);
  68. static int irlap_state_xmit_s (struct irlap_cb *self, IRLAP_EVENT event,
  69. struct sk_buff *skb, struct irlap_info *info);
  70. static int irlap_state_sclose (struct irlap_cb *self, IRLAP_EVENT event,
  71. struct sk_buff *skb, struct irlap_info *info);
  72. static int irlap_state_reset_check(struct irlap_cb *, IRLAP_EVENT event,
  73. struct sk_buff *, struct irlap_info *);
  74. static const char *const irlap_event[] __maybe_unused = {
  75. "DISCOVERY_REQUEST",
  76. "CONNECT_REQUEST",
  77. "CONNECT_RESPONSE",
  78. "DISCONNECT_REQUEST",
  79. "DATA_REQUEST",
  80. "RESET_REQUEST",
  81. "RESET_RESPONSE",
  82. "SEND_I_CMD",
  83. "SEND_UI_FRAME",
  84. "RECV_DISCOVERY_XID_CMD",
  85. "RECV_DISCOVERY_XID_RSP",
  86. "RECV_SNRM_CMD",
  87. "RECV_TEST_CMD",
  88. "RECV_TEST_RSP",
  89. "RECV_UA_RSP",
  90. "RECV_DM_RSP",
  91. "RECV_RD_RSP",
  92. "RECV_I_CMD",
  93. "RECV_I_RSP",
  94. "RECV_UI_FRAME",
  95. "RECV_FRMR_RSP",
  96. "RECV_RR_CMD",
  97. "RECV_RR_RSP",
  98. "RECV_RNR_CMD",
  99. "RECV_RNR_RSP",
  100. "RECV_REJ_CMD",
  101. "RECV_REJ_RSP",
  102. "RECV_SREJ_CMD",
  103. "RECV_SREJ_RSP",
  104. "RECV_DISC_CMD",
  105. "SLOT_TIMER_EXPIRED",
  106. "QUERY_TIMER_EXPIRED",
  107. "FINAL_TIMER_EXPIRED",
  108. "POLL_TIMER_EXPIRED",
  109. "DISCOVERY_TIMER_EXPIRED",
  110. "WD_TIMER_EXPIRED",
  111. "BACKOFF_TIMER_EXPIRED",
  112. "MEDIA_BUSY_TIMER_EXPIRED",
  113. };
  114. const char *const irlap_state[] = {
  115. "LAP_NDM",
  116. "LAP_QUERY",
  117. "LAP_REPLY",
  118. "LAP_CONN",
  119. "LAP_SETUP",
  120. "LAP_OFFLINE",
  121. "LAP_XMIT_P",
  122. "LAP_PCLOSE",
  123. "LAP_NRM_P",
  124. "LAP_RESET_WAIT",
  125. "LAP_RESET",
  126. "LAP_NRM_S",
  127. "LAP_XMIT_S",
  128. "LAP_SCLOSE",
  129. "LAP_RESET_CHECK",
  130. };
  131. static int (*state[])(struct irlap_cb *self, IRLAP_EVENT event,
  132. struct sk_buff *skb, struct irlap_info *info) =
  133. {
  134. irlap_state_ndm,
  135. irlap_state_query,
  136. irlap_state_reply,
  137. irlap_state_conn,
  138. irlap_state_setup,
  139. irlap_state_offline,
  140. irlap_state_xmit_p,
  141. irlap_state_pclose,
  142. irlap_state_nrm_p,
  143. irlap_state_reset_wait,
  144. irlap_state_reset,
  145. irlap_state_nrm_s,
  146. irlap_state_xmit_s,
  147. irlap_state_sclose,
  148. irlap_state_reset_check,
  149. };
  150. /*
  151. * Function irda_poll_timer_expired (data)
  152. *
  153. * Poll timer has expired. Normally we must now send a RR frame to the
  154. * remote device
  155. */
  156. static void irlap_poll_timer_expired(void *data)
  157. {
  158. struct irlap_cb *self = (struct irlap_cb *) data;
  159. IRDA_ASSERT(self != NULL, return;);
  160. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  161. irlap_do_event(self, POLL_TIMER_EXPIRED, NULL, NULL);
  162. }
  163. /*
  164. * Calculate and set time before we will have to send back the pf bit
  165. * to the peer. Use in primary.
  166. * Make sure that state is XMIT_P/XMIT_S when calling this function
  167. * (and that nobody messed up with the state). - Jean II
  168. */
  169. static void irlap_start_poll_timer(struct irlap_cb *self, int timeout)
  170. {
  171. IRDA_ASSERT(self != NULL, return;);
  172. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  173. #ifdef CONFIG_IRDA_FAST_RR
  174. /*
  175. * Send out the RR frames faster if our own transmit queue is empty, or
  176. * if the peer is busy. The effect is a much faster conversation
  177. */
  178. if (skb_queue_empty(&self->txq) || self->remote_busy) {
  179. if (self->fast_RR == TRUE) {
  180. /*
  181. * Assert that the fast poll timer has not reached the
  182. * normal poll timer yet
  183. */
  184. if (self->fast_RR_timeout < timeout) {
  185. /*
  186. * FIXME: this should be a more configurable
  187. * function
  188. */
  189. self->fast_RR_timeout +=
  190. (sysctl_fast_poll_increase * HZ/1000);
  191. /* Use this fast(er) timeout instead */
  192. timeout = self->fast_RR_timeout;
  193. }
  194. } else {
  195. self->fast_RR = TRUE;
  196. /* Start with just 0 ms */
  197. self->fast_RR_timeout = 0;
  198. timeout = 0;
  199. }
  200. } else
  201. self->fast_RR = FALSE;
  202. pr_debug("%s(), timeout=%d (%ld)\n", __func__, timeout, jiffies);
  203. #endif /* CONFIG_IRDA_FAST_RR */
  204. if (timeout == 0)
  205. irlap_do_event(self, POLL_TIMER_EXPIRED, NULL, NULL);
  206. else
  207. irda_start_timer(&self->poll_timer, timeout, self,
  208. irlap_poll_timer_expired);
  209. }
  210. /*
  211. * Function irlap_do_event (event, skb, info)
  212. *
  213. * Rushes through the state machine without any delay. If state == XMIT
  214. * then send queued data frames.
  215. */
  216. void irlap_do_event(struct irlap_cb *self, IRLAP_EVENT event,
  217. struct sk_buff *skb, struct irlap_info *info)
  218. {
  219. int ret;
  220. if (!self || self->magic != LAP_MAGIC)
  221. return;
  222. pr_debug("%s(), event = %s, state = %s\n", __func__,
  223. irlap_event[event], irlap_state[self->state]);
  224. ret = (*state[self->state])(self, event, skb, info);
  225. /*
  226. * Check if there are any pending events that needs to be executed
  227. */
  228. switch (self->state) {
  229. case LAP_XMIT_P: /* FALLTHROUGH */
  230. case LAP_XMIT_S:
  231. /*
  232. * We just received the pf bit and are at the beginning
  233. * of a new LAP transmit window.
  234. * Check if there are any queued data frames, and do not
  235. * try to disconnect link if we send any data frames, since
  236. * that will change the state away form XMIT
  237. */
  238. pr_debug("%s() : queue len = %d\n", __func__,
  239. skb_queue_len(&self->txq));
  240. if (!skb_queue_empty(&self->txq)) {
  241. /* Prevent race conditions with irlap_data_request() */
  242. self->local_busy = TRUE;
  243. /* Theory of operation.
  244. * We send frames up to when we fill the window or
  245. * reach line capacity. Those frames will queue up
  246. * in the device queue, and the driver will slowly
  247. * send them.
  248. * After each frame that we send, we poll the higher
  249. * layer for more data. It's the right time to do
  250. * that because the link layer need to perform the mtt
  251. * and then send the first frame, so we can afford
  252. * to send a bit of time in kernel space.
  253. * The explicit flow indication allow to minimise
  254. * buffers (== lower latency), to avoid higher layer
  255. * polling via timers (== less context switches) and
  256. * to implement a crude scheduler - Jean II */
  257. /* Try to send away all queued data frames */
  258. while ((skb = skb_dequeue(&self->txq)) != NULL) {
  259. /* Send one frame */
  260. ret = (*state[self->state])(self, SEND_I_CMD,
  261. skb, NULL);
  262. /* Drop reference count.
  263. * It will be increase as needed in
  264. * irlap_send_data_xxx() */
  265. kfree_skb(skb);
  266. /* Poll the higher layers for one more frame */
  267. irlmp_flow_indication(self->notify.instance,
  268. FLOW_START);
  269. if (ret == -EPROTO)
  270. break; /* Try again later! */
  271. }
  272. /* Finished transmitting */
  273. self->local_busy = FALSE;
  274. } else if (self->disconnect_pending) {
  275. self->disconnect_pending = FALSE;
  276. ret = (*state[self->state])(self, DISCONNECT_REQUEST,
  277. NULL, NULL);
  278. }
  279. break;
  280. /* case LAP_NDM: */
  281. /* case LAP_CONN: */
  282. /* case LAP_RESET_WAIT: */
  283. /* case LAP_RESET_CHECK: */
  284. default:
  285. break;
  286. }
  287. }
  288. /*
  289. * Function irlap_state_ndm (event, skb, frame)
  290. *
  291. * NDM (Normal Disconnected Mode) state
  292. *
  293. */
  294. static int irlap_state_ndm(struct irlap_cb *self, IRLAP_EVENT event,
  295. struct sk_buff *skb, struct irlap_info *info)
  296. {
  297. discovery_t *discovery_rsp;
  298. int ret = 0;
  299. IRDA_ASSERT(self != NULL, return -1;);
  300. IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
  301. switch (event) {
  302. case CONNECT_REQUEST:
  303. IRDA_ASSERT(self->netdev != NULL, return -1;);
  304. if (self->media_busy) {
  305. /* Note : this will never happen, because we test
  306. * media busy in irlap_connect_request() and
  307. * postpone the event... - Jean II */
  308. pr_debug("%s(), CONNECT_REQUEST: media busy!\n",
  309. __func__);
  310. /* Always switch state before calling upper layers */
  311. irlap_next_state(self, LAP_NDM);
  312. irlap_disconnect_indication(self, LAP_MEDIA_BUSY);
  313. } else {
  314. irlap_send_snrm_frame(self, &self->qos_rx);
  315. /* Start Final-bit timer */
  316. irlap_start_final_timer(self, self->final_timeout);
  317. self->retry_count = 0;
  318. irlap_next_state(self, LAP_SETUP);
  319. }
  320. break;
  321. case RECV_SNRM_CMD:
  322. /* Check if the frame contains and I field */
  323. if (info) {
  324. self->daddr = info->daddr;
  325. self->caddr = info->caddr;
  326. irlap_next_state(self, LAP_CONN);
  327. irlap_connect_indication(self, skb);
  328. } else {
  329. pr_debug("%s(), SNRM frame does not contain an I field!\n",
  330. __func__);
  331. }
  332. break;
  333. case DISCOVERY_REQUEST:
  334. IRDA_ASSERT(info != NULL, return -1;);
  335. if (self->media_busy) {
  336. pr_debug("%s(), DISCOVERY_REQUEST: media busy!\n",
  337. __func__);
  338. /* irlap->log.condition = MEDIA_BUSY; */
  339. /* This will make IrLMP try again */
  340. irlap_discovery_confirm(self, NULL);
  341. /* Note : the discovery log is not cleaned up here,
  342. * it will be done in irlap_discovery_request()
  343. * Jean II */
  344. return 0;
  345. }
  346. self->S = info->S;
  347. self->s = info->s;
  348. irlap_send_discovery_xid_frame(self, info->S, info->s, TRUE,
  349. info->discovery);
  350. self->frame_sent = FALSE;
  351. self->s++;
  352. irlap_start_slot_timer(self, self->slot_timeout);
  353. irlap_next_state(self, LAP_QUERY);
  354. break;
  355. case RECV_DISCOVERY_XID_CMD:
  356. IRDA_ASSERT(info != NULL, return -1;);
  357. /* Assert that this is not the final slot */
  358. if (info->s <= info->S) {
  359. self->slot = irlap_generate_rand_time_slot(info->S,
  360. info->s);
  361. if (self->slot == info->s) {
  362. discovery_rsp = irlmp_get_discovery_response();
  363. discovery_rsp->data.daddr = info->daddr;
  364. irlap_send_discovery_xid_frame(self, info->S,
  365. self->slot,
  366. FALSE,
  367. discovery_rsp);
  368. self->frame_sent = TRUE;
  369. } else
  370. self->frame_sent = FALSE;
  371. /*
  372. * Go to reply state until end of discovery to
  373. * inhibit our own transmissions. Set the timer
  374. * to not stay forever there... Jean II
  375. */
  376. irlap_start_query_timer(self, info->S, info->s);
  377. irlap_next_state(self, LAP_REPLY);
  378. } else {
  379. /* This is the final slot. How is it possible ?
  380. * This would happen is both discoveries are just slightly
  381. * offset (if they are in sync, all packets are lost).
  382. * Most often, all the discovery requests will be received
  383. * in QUERY state (see my comment there), except for the
  384. * last frame that will come here.
  385. * The big trouble when it happen is that active discovery
  386. * doesn't happen, because nobody answer the discoveries
  387. * frame of the other guy, so the log shows up empty.
  388. * What should we do ?
  389. * Not much. It's too late to answer those discovery frames,
  390. * so we just pass the info to IrLMP who will put it in the
  391. * log (and post an event).
  392. * Another cause would be devices that do discovery much
  393. * slower than us, however the latest fixes should minimise
  394. * those cases...
  395. * Jean II
  396. */
  397. pr_debug("%s(), Receiving final discovery request, missed the discovery slots :-(\n",
  398. __func__);
  399. /* Last discovery request -> in the log */
  400. irlap_discovery_indication(self, info->discovery);
  401. }
  402. break;
  403. case MEDIA_BUSY_TIMER_EXPIRED:
  404. /* A bunch of events may be postponed because the media is
  405. * busy (usually immediately after we close a connection),
  406. * or while we are doing discovery (state query/reply).
  407. * In all those cases, the media busy flag will be cleared
  408. * when it's OK for us to process those postponed events.
  409. * This event is not mentioned in the state machines in the
  410. * IrLAP spec. It's because they didn't consider Ultra and
  411. * postponing connection request is optional.
  412. * Jean II */
  413. #ifdef CONFIG_IRDA_ULTRA
  414. /* Send any pending Ultra frames if any */
  415. if (!skb_queue_empty(&self->txq_ultra)) {
  416. /* We don't send the frame, just post an event.
  417. * Also, previously this code was in timer.c...
  418. * Jean II */
  419. ret = (*state[self->state])(self, SEND_UI_FRAME,
  420. NULL, NULL);
  421. }
  422. #endif /* CONFIG_IRDA_ULTRA */
  423. /* Check if we should try to connect.
  424. * This code was previously in irlap_do_event() */
  425. if (self->connect_pending) {
  426. self->connect_pending = FALSE;
  427. /* This one *should* not pend in this state, except
  428. * if a socket try to connect and immediately
  429. * disconnect. - clear - Jean II */
  430. if (self->disconnect_pending)
  431. irlap_disconnect_indication(self, LAP_DISC_INDICATION);
  432. else
  433. ret = (*state[self->state])(self,
  434. CONNECT_REQUEST,
  435. NULL, NULL);
  436. self->disconnect_pending = FALSE;
  437. }
  438. /* Note : one way to test if this code works well (including
  439. * media busy and small busy) is to create a user space
  440. * application generating an Ultra packet every 3.05 sec (or
  441. * 2.95 sec) and to see how it interact with discovery.
  442. * It's fairly easy to check that no packet is lost, that the
  443. * packets are postponed during discovery and that after
  444. * discovery indication you have a 100ms "gap".
  445. * As connection request and Ultra are now processed the same
  446. * way, this avoid the tedious job of trying IrLAP connection
  447. * in all those cases...
  448. * Jean II */
  449. break;
  450. #ifdef CONFIG_IRDA_ULTRA
  451. case SEND_UI_FRAME:
  452. {
  453. int i;
  454. /* Only allowed to repeat an operation twice */
  455. for (i=0; ((i<2) && (self->media_busy == FALSE)); i++) {
  456. skb = skb_dequeue(&self->txq_ultra);
  457. if (skb)
  458. irlap_send_ui_frame(self, skb, CBROADCAST,
  459. CMD_FRAME);
  460. else
  461. break;
  462. /* irlap_send_ui_frame() won't increase skb reference
  463. * count, so no dev_kfree_skb() - Jean II */
  464. }
  465. if (i == 2) {
  466. /* Force us to listen 500 ms again */
  467. irda_device_set_media_busy(self->netdev, TRUE);
  468. }
  469. break;
  470. }
  471. case RECV_UI_FRAME:
  472. /* Only accept broadcast frames in NDM mode */
  473. if (info->caddr != CBROADCAST) {
  474. pr_debug("%s(), not a broadcast frame!\n",
  475. __func__);
  476. } else
  477. irlap_unitdata_indication(self, skb);
  478. break;
  479. #endif /* CONFIG_IRDA_ULTRA */
  480. case RECV_TEST_CMD:
  481. /* Remove test frame header */
  482. skb_pull(skb, sizeof(struct test_frame));
  483. /*
  484. * Send response. This skb will not be sent out again, and
  485. * will only be used to send out the same info as the cmd
  486. */
  487. irlap_send_test_frame(self, CBROADCAST, info->daddr, skb);
  488. break;
  489. case RECV_TEST_RSP:
  490. pr_debug("%s() not implemented!\n", __func__);
  491. break;
  492. default:
  493. pr_debug("%s(), Unknown event %s\n", __func__,
  494. irlap_event[event]);
  495. ret = -1;
  496. break;
  497. }
  498. return ret;
  499. }
  500. /*
  501. * Function irlap_state_query (event, skb, info)
  502. *
  503. * QUERY state
  504. *
  505. */
  506. static int irlap_state_query(struct irlap_cb *self, IRLAP_EVENT event,
  507. struct sk_buff *skb, struct irlap_info *info)
  508. {
  509. int ret = 0;
  510. IRDA_ASSERT(self != NULL, return -1;);
  511. IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
  512. switch (event) {
  513. case RECV_DISCOVERY_XID_RSP:
  514. IRDA_ASSERT(info != NULL, return -1;);
  515. IRDA_ASSERT(info->discovery != NULL, return -1;);
  516. pr_debug("%s(), daddr=%08x\n", __func__,
  517. info->discovery->data.daddr);
  518. if (!self->discovery_log) {
  519. net_warn_ratelimited("%s: discovery log is gone! maybe the discovery timeout has been set too short?\n",
  520. __func__);
  521. break;
  522. }
  523. hashbin_insert(self->discovery_log,
  524. (irda_queue_t *) info->discovery,
  525. info->discovery->data.daddr, NULL);
  526. /* Keep state */
  527. /* irlap_next_state(self, LAP_QUERY); */
  528. break;
  529. case RECV_DISCOVERY_XID_CMD:
  530. /* Yes, it is possible to receive those frames in this mode.
  531. * Note that most often the last discovery request won't
  532. * occur here but in NDM state (see my comment there).
  533. * What should we do ?
  534. * Not much. We are currently performing our own discovery,
  535. * therefore we can't answer those frames. We don't want
  536. * to change state either. We just pass the info to
  537. * IrLMP who will put it in the log (and post an event).
  538. * Jean II
  539. */
  540. IRDA_ASSERT(info != NULL, return -1;);
  541. pr_debug("%s(), Receiving discovery request (s = %d) while performing discovery :-(\n",
  542. __func__, info->s);
  543. /* Last discovery request ? */
  544. if (info->s == 0xff)
  545. irlap_discovery_indication(self, info->discovery);
  546. break;
  547. case SLOT_TIMER_EXPIRED:
  548. /*
  549. * Wait a little longer if we detect an incoming frame. This
  550. * is not mentioned in the spec, but is a good thing to do,
  551. * since we want to work even with devices that violate the
  552. * timing requirements.
  553. */
  554. if (irda_device_is_receiving(self->netdev) && !self->add_wait) {
  555. pr_debug("%s(), device is slow to answer, waiting some more!\n",
  556. __func__);
  557. irlap_start_slot_timer(self, msecs_to_jiffies(10));
  558. self->add_wait = TRUE;
  559. return ret;
  560. }
  561. self->add_wait = FALSE;
  562. if (self->s < self->S) {
  563. irlap_send_discovery_xid_frame(self, self->S,
  564. self->s, TRUE,
  565. self->discovery_cmd);
  566. self->s++;
  567. irlap_start_slot_timer(self, self->slot_timeout);
  568. /* Keep state */
  569. irlap_next_state(self, LAP_QUERY);
  570. } else {
  571. /* This is the final slot! */
  572. irlap_send_discovery_xid_frame(self, self->S, 0xff,
  573. TRUE,
  574. self->discovery_cmd);
  575. /* Always switch state before calling upper layers */
  576. irlap_next_state(self, LAP_NDM);
  577. /*
  578. * We are now finished with the discovery procedure,
  579. * so now we must return the results
  580. */
  581. irlap_discovery_confirm(self, self->discovery_log);
  582. /* IrLMP should now have taken care of the log */
  583. self->discovery_log = NULL;
  584. }
  585. break;
  586. default:
  587. pr_debug("%s(), Unknown event %s\n", __func__,
  588. irlap_event[event]);
  589. ret = -1;
  590. break;
  591. }
  592. return ret;
  593. }
  594. /*
  595. * Function irlap_state_reply (self, event, skb, info)
  596. *
  597. * REPLY, we have received a XID discovery frame from a device and we
  598. * are waiting for the right time slot to send a response XID frame
  599. *
  600. */
  601. static int irlap_state_reply(struct irlap_cb *self, IRLAP_EVENT event,
  602. struct sk_buff *skb, struct irlap_info *info)
  603. {
  604. discovery_t *discovery_rsp;
  605. int ret=0;
  606. IRDA_ASSERT(self != NULL, return -1;);
  607. IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
  608. switch (event) {
  609. case QUERY_TIMER_EXPIRED:
  610. pr_debug("%s(), QUERY_TIMER_EXPIRED <%ld>\n",
  611. __func__, jiffies);
  612. irlap_next_state(self, LAP_NDM);
  613. break;
  614. case RECV_DISCOVERY_XID_CMD:
  615. IRDA_ASSERT(info != NULL, return -1;);
  616. /* Last frame? */
  617. if (info->s == 0xff) {
  618. del_timer(&self->query_timer);
  619. /* info->log.condition = REMOTE; */
  620. /* Always switch state before calling upper layers */
  621. irlap_next_state(self, LAP_NDM);
  622. irlap_discovery_indication(self, info->discovery);
  623. } else {
  624. /* If it's our slot, send our reply */
  625. if ((info->s >= self->slot) && (!self->frame_sent)) {
  626. discovery_rsp = irlmp_get_discovery_response();
  627. discovery_rsp->data.daddr = info->daddr;
  628. irlap_send_discovery_xid_frame(self, info->S,
  629. self->slot,
  630. FALSE,
  631. discovery_rsp);
  632. self->frame_sent = TRUE;
  633. }
  634. /* Readjust our timer to accommodate devices
  635. * doing faster or slower discovery than us...
  636. * Jean II */
  637. irlap_start_query_timer(self, info->S, info->s);
  638. /* Keep state */
  639. //irlap_next_state(self, LAP_REPLY);
  640. }
  641. break;
  642. default:
  643. pr_debug("%s(), Unknown event %d, %s\n", __func__,
  644. event, irlap_event[event]);
  645. ret = -1;
  646. break;
  647. }
  648. return ret;
  649. }
  650. /*
  651. * Function irlap_state_conn (event, skb, info)
  652. *
  653. * CONN, we have received a SNRM command and is waiting for the upper
  654. * layer to accept or refuse connection
  655. *
  656. */
  657. static int irlap_state_conn(struct irlap_cb *self, IRLAP_EVENT event,
  658. struct sk_buff *skb, struct irlap_info *info)
  659. {
  660. int ret = 0;
  661. pr_debug("%s(), event=%s\n", __func__, irlap_event[event]);
  662. IRDA_ASSERT(self != NULL, return -1;);
  663. IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
  664. switch (event) {
  665. case CONNECT_RESPONSE:
  666. skb_pull(skb, sizeof(struct snrm_frame));
  667. IRDA_ASSERT(self->netdev != NULL, return -1;);
  668. irlap_qos_negotiate(self, skb);
  669. irlap_initiate_connection_state(self);
  670. /*
  671. * Applying the parameters now will make sure we change speed
  672. * *after* we have sent the next frame
  673. */
  674. irlap_apply_connection_parameters(self, FALSE);
  675. /*
  676. * Sending this frame will force a speed change after it has
  677. * been sent (i.e. the frame will be sent at 9600).
  678. */
  679. irlap_send_ua_response_frame(self, &self->qos_rx);
  680. #if 0
  681. /*
  682. * We are allowed to send two frames, but this may increase
  683. * the connect latency, so lets not do it for now.
  684. */
  685. /* This is full of good intentions, but doesn't work in
  686. * practice.
  687. * After sending the first UA response, we switch the
  688. * dongle to the negotiated speed, which is usually
  689. * different than 9600 kb/s.
  690. * From there, there is two solutions :
  691. * 1) The other end has received the first UA response :
  692. * it will set up the connection, move to state LAP_NRM_P,
  693. * and will ignore and drop the second UA response.
  694. * Actually, it's even worse : the other side will almost
  695. * immediately send a RR that will likely collide with the
  696. * UA response (depending on negotiated turnaround).
  697. * 2) The other end has not received the first UA response,
  698. * will stay at 9600 and will never see the second UA response.
  699. * Jean II */
  700. irlap_send_ua_response_frame(self, &self->qos_rx);
  701. #endif
  702. /*
  703. * The WD-timer could be set to the duration of the P-timer
  704. * for this case, but it is recommended to use twice the
  705. * value (note 3 IrLAP p. 60).
  706. */
  707. irlap_start_wd_timer(self, self->wd_timeout);
  708. irlap_next_state(self, LAP_NRM_S);
  709. break;
  710. case RECV_DISCOVERY_XID_CMD:
  711. pr_debug("%s(), event RECV_DISCOVER_XID_CMD!\n",
  712. __func__);
  713. irlap_next_state(self, LAP_NDM);
  714. break;
  715. case DISCONNECT_REQUEST:
  716. pr_debug("%s(), Disconnect request!\n", __func__);
  717. irlap_send_dm_frame(self);
  718. irlap_next_state( self, LAP_NDM);
  719. irlap_disconnect_indication(self, LAP_DISC_INDICATION);
  720. break;
  721. default:
  722. pr_debug("%s(), Unknown event %d, %s\n", __func__,
  723. event, irlap_event[event]);
  724. ret = -1;
  725. break;
  726. }
  727. return ret;
  728. }
  729. /*
  730. * Function irlap_state_setup (event, skb, frame)
  731. *
  732. * SETUP state, The local layer has transmitted a SNRM command frame to
  733. * a remote peer layer and is awaiting a reply .
  734. *
  735. */
  736. static int irlap_state_setup(struct irlap_cb *self, IRLAP_EVENT event,
  737. struct sk_buff *skb, struct irlap_info *info)
  738. {
  739. int ret = 0;
  740. IRDA_ASSERT(self != NULL, return -1;);
  741. IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
  742. switch (event) {
  743. case FINAL_TIMER_EXPIRED:
  744. if (self->retry_count < self->N3) {
  745. /*
  746. * Perform random backoff, Wait a random number of time units, minimum
  747. * duration half the time taken to transmitt a SNRM frame, maximum duration
  748. * 1.5 times the time taken to transmit a SNRM frame. So this time should
  749. * between 15 msecs and 45 msecs.
  750. */
  751. irlap_start_backoff_timer(self, msecs_to_jiffies(20 +
  752. (jiffies % 30)));
  753. } else {
  754. /* Always switch state before calling upper layers */
  755. irlap_next_state(self, LAP_NDM);
  756. irlap_disconnect_indication(self, LAP_FOUND_NONE);
  757. }
  758. break;
  759. case BACKOFF_TIMER_EXPIRED:
  760. irlap_send_snrm_frame(self, &self->qos_rx);
  761. irlap_start_final_timer(self, self->final_timeout);
  762. self->retry_count++;
  763. break;
  764. case RECV_SNRM_CMD:
  765. pr_debug("%s(), SNRM battle!\n", __func__);
  766. IRDA_ASSERT(skb != NULL, return 0;);
  767. IRDA_ASSERT(info != NULL, return 0;);
  768. /*
  769. * The device with the largest device address wins the battle
  770. * (both have sent a SNRM command!)
  771. */
  772. if (info &&(info->daddr > self->saddr)) {
  773. del_timer(&self->final_timer);
  774. irlap_initiate_connection_state(self);
  775. IRDA_ASSERT(self->netdev != NULL, return -1;);
  776. skb_pull(skb, sizeof(struct snrm_frame));
  777. irlap_qos_negotiate(self, skb);
  778. /* Send UA frame and then change link settings */
  779. irlap_apply_connection_parameters(self, FALSE);
  780. irlap_send_ua_response_frame(self, &self->qos_rx);
  781. irlap_next_state(self, LAP_NRM_S);
  782. irlap_connect_confirm(self, skb);
  783. /*
  784. * The WD-timer could be set to the duration of the
  785. * P-timer for this case, but it is recommended
  786. * to use twice the value (note 3 IrLAP p. 60).
  787. */
  788. irlap_start_wd_timer(self, self->wd_timeout);
  789. } else {
  790. /* We just ignore the other device! */
  791. irlap_next_state(self, LAP_SETUP);
  792. }
  793. break;
  794. case RECV_UA_RSP:
  795. /* Stop F-timer */
  796. del_timer(&self->final_timer);
  797. /* Initiate connection state */
  798. irlap_initiate_connection_state(self);
  799. /* Negotiate connection parameters */
  800. IRDA_ASSERT(skb->len > 10, return -1;);
  801. skb_pull(skb, sizeof(struct ua_frame));
  802. IRDA_ASSERT(self->netdev != NULL, return -1;);
  803. irlap_qos_negotiate(self, skb);
  804. /* Set the new link setting *now* (before the rr frame) */
  805. irlap_apply_connection_parameters(self, TRUE);
  806. self->retry_count = 0;
  807. /* Wait for turnaround time to give a chance to the other
  808. * device to be ready to receive us.
  809. * Note : the time to switch speed is typically larger
  810. * than the turnaround time, but as we don't have the other
  811. * side speed switch time, that's our best guess...
  812. * Jean II */
  813. irlap_wait_min_turn_around(self, &self->qos_tx);
  814. /* This frame will actually be sent at the new speed */
  815. irlap_send_rr_frame(self, CMD_FRAME);
  816. /* The timer is set to half the normal timer to quickly
  817. * detect a failure to negotiate the new connection
  818. * parameters. IrLAP 6.11.3.2, note 3.
  819. * Note that currently we don't process this failure
  820. * properly, as we should do a quick disconnect.
  821. * Jean II */
  822. irlap_start_final_timer(self, self->final_timeout/2);
  823. irlap_next_state(self, LAP_NRM_P);
  824. irlap_connect_confirm(self, skb);
  825. break;
  826. case RECV_DM_RSP: /* FALLTHROUGH */
  827. case RECV_DISC_CMD:
  828. del_timer(&self->final_timer);
  829. irlap_next_state(self, LAP_NDM);
  830. irlap_disconnect_indication(self, LAP_DISC_INDICATION);
  831. break;
  832. default:
  833. pr_debug("%s(), Unknown event %d, %s\n", __func__,
  834. event, irlap_event[event]);
  835. ret = -1;
  836. break;
  837. }
  838. return ret;
  839. }
  840. /*
  841. * Function irlap_state_offline (self, event, skb, info)
  842. *
  843. * OFFLINE state, not used for now!
  844. *
  845. */
  846. static int irlap_state_offline(struct irlap_cb *self, IRLAP_EVENT event,
  847. struct sk_buff *skb, struct irlap_info *info)
  848. {
  849. pr_debug("%s(), Unknown event\n", __func__);
  850. return -1;
  851. }
  852. /*
  853. * Function irlap_state_xmit_p (self, event, skb, info)
  854. *
  855. * XMIT, Only the primary station has right to transmit, and we
  856. * therefore do not expect to receive any transmissions from other
  857. * stations.
  858. *
  859. */
  860. static int irlap_state_xmit_p(struct irlap_cb *self, IRLAP_EVENT event,
  861. struct sk_buff *skb, struct irlap_info *info)
  862. {
  863. int ret = 0;
  864. switch (event) {
  865. case SEND_I_CMD:
  866. /*
  867. * Only send frame if send-window > 0.
  868. */
  869. if ((self->window > 0) && (!self->remote_busy)) {
  870. int nextfit;
  871. #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
  872. struct sk_buff *skb_next;
  873. /* With DYNAMIC_WINDOW, we keep the window size
  874. * maximum, and adapt on the packets we are sending.
  875. * At 115k, we can send only 2 packets of 2048 bytes
  876. * in a 500 ms turnaround. Without this option, we
  877. * would always limit the window to 2. With this
  878. * option, if we send smaller packets, we can send
  879. * up to 7 of them (always depending on QoS).
  880. * Jean II */
  881. /* Look at the next skb. This is safe, as we are
  882. * the only consumer of the Tx queue (if we are not,
  883. * we have other problems) - Jean II */
  884. skb_next = skb_peek(&self->txq);
  885. /* Check if a subsequent skb exist and would fit in
  886. * the current window (with respect to turnaround
  887. * time).
  888. * This allow us to properly mark the current packet
  889. * with the pf bit, to avoid falling back on the
  890. * second test below, and avoid waiting the
  891. * end of the window and sending a extra RR.
  892. * Note : (skb_next != NULL) <=> (skb_queue_len() > 0)
  893. * Jean II */
  894. nextfit = ((skb_next != NULL) &&
  895. ((skb_next->len + skb->len) <=
  896. self->bytes_left));
  897. /*
  898. * The current packet may not fit ! Because of test
  899. * above, this should not happen any more !!!
  900. * Test if we have transmitted more bytes over the
  901. * link than its possible to do with the current
  902. * speed and turn-around-time.
  903. */
  904. if((!nextfit) && (skb->len > self->bytes_left)) {
  905. pr_debug("%s(), Not allowed to transmit more bytes!\n",
  906. __func__);
  907. /* Requeue the skb */
  908. skb_queue_head(&self->txq, skb_get(skb));
  909. /*
  910. * We should switch state to LAP_NRM_P, but
  911. * that is not possible since we must be sure
  912. * that we poll the other side. Since we have
  913. * used up our time, the poll timer should
  914. * trigger anyway now, so we just wait for it
  915. * DB
  916. */
  917. /*
  918. * Sorry, but that's not totally true. If
  919. * we send 2000B packets, we may wait another
  920. * 1000B until our turnaround expire. That's
  921. * why we need to be proactive in avoiding
  922. * coming here. - Jean II
  923. */
  924. return -EPROTO;
  925. }
  926. /* Subtract space used by this skb */
  927. self->bytes_left -= skb->len;
  928. #else /* CONFIG_IRDA_DYNAMIC_WINDOW */
  929. /* Window has been adjusted for the max packet
  930. * size, so much simpler... - Jean II */
  931. nextfit = !skb_queue_empty(&self->txq);
  932. #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
  933. /*
  934. * Send data with poll bit cleared only if window > 1
  935. * and there is more frames after this one to be sent
  936. */
  937. if ((self->window > 1) && (nextfit)) {
  938. /* More packet to send in current window */
  939. irlap_send_data_primary(self, skb);
  940. irlap_next_state(self, LAP_XMIT_P);
  941. } else {
  942. /* Final packet of window */
  943. irlap_send_data_primary_poll(self, skb);
  944. /*
  945. * Make sure state machine does not try to send
  946. * any more frames
  947. */
  948. ret = -EPROTO;
  949. }
  950. #ifdef CONFIG_IRDA_FAST_RR
  951. /* Peer may want to reply immediately */
  952. self->fast_RR = FALSE;
  953. #endif /* CONFIG_IRDA_FAST_RR */
  954. } else {
  955. pr_debug("%s(), Unable to send! remote busy?\n",
  956. __func__);
  957. skb_queue_head(&self->txq, skb_get(skb));
  958. /*
  959. * The next ret is important, because it tells
  960. * irlap_next_state _not_ to deliver more frames
  961. */
  962. ret = -EPROTO;
  963. }
  964. break;
  965. case POLL_TIMER_EXPIRED:
  966. pr_debug("%s(), POLL_TIMER_EXPIRED <%ld>\n",
  967. __func__, jiffies);
  968. irlap_send_rr_frame(self, CMD_FRAME);
  969. /* Return to NRM properly - Jean II */
  970. self->window = self->window_size;
  971. #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
  972. /* Allowed to transmit a maximum number of bytes again. */
  973. self->bytes_left = self->line_capacity;
  974. #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
  975. irlap_start_final_timer(self, self->final_timeout);
  976. irlap_next_state(self, LAP_NRM_P);
  977. break;
  978. case DISCONNECT_REQUEST:
  979. del_timer(&self->poll_timer);
  980. irlap_wait_min_turn_around(self, &self->qos_tx);
  981. irlap_send_disc_frame(self);
  982. irlap_flush_all_queues(self);
  983. irlap_start_final_timer(self, self->final_timeout);
  984. self->retry_count = 0;
  985. irlap_next_state(self, LAP_PCLOSE);
  986. break;
  987. case DATA_REQUEST:
  988. /* Nothing to do, irlap_do_event() will send the packet
  989. * when we return... - Jean II */
  990. break;
  991. default:
  992. pr_debug("%s(), Unknown event %s\n",
  993. __func__, irlap_event[event]);
  994. ret = -EINVAL;
  995. break;
  996. }
  997. return ret;
  998. }
  999. /*
  1000. * Function irlap_state_pclose (event, skb, info)
  1001. *
  1002. * PCLOSE state
  1003. */
  1004. static int irlap_state_pclose(struct irlap_cb *self, IRLAP_EVENT event,
  1005. struct sk_buff *skb, struct irlap_info *info)
  1006. {
  1007. int ret = 0;
  1008. IRDA_ASSERT(self != NULL, return -1;);
  1009. IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
  1010. switch (event) {
  1011. case RECV_UA_RSP: /* FALLTHROUGH */
  1012. case RECV_DM_RSP:
  1013. del_timer(&self->final_timer);
  1014. /* Set new link parameters */
  1015. irlap_apply_default_connection_parameters(self);
  1016. /* Always switch state before calling upper layers */
  1017. irlap_next_state(self, LAP_NDM);
  1018. irlap_disconnect_indication(self, LAP_DISC_INDICATION);
  1019. break;
  1020. case FINAL_TIMER_EXPIRED:
  1021. if (self->retry_count < self->N3) {
  1022. irlap_wait_min_turn_around(self, &self->qos_tx);
  1023. irlap_send_disc_frame(self);
  1024. irlap_start_final_timer(self, self->final_timeout);
  1025. self->retry_count++;
  1026. /* Keep state */
  1027. } else {
  1028. irlap_apply_default_connection_parameters(self);
  1029. /* Always switch state before calling upper layers */
  1030. irlap_next_state(self, LAP_NDM);
  1031. irlap_disconnect_indication(self, LAP_NO_RESPONSE);
  1032. }
  1033. break;
  1034. default:
  1035. pr_debug("%s(), Unknown event %d\n", __func__, event);
  1036. ret = -1;
  1037. break;
  1038. }
  1039. return ret;
  1040. }
  1041. /*
  1042. * Function irlap_state_nrm_p (self, event, skb, info)
  1043. *
  1044. * NRM_P (Normal Response Mode as Primary), The primary station has given
  1045. * permissions to a secondary station to transmit IrLAP resonse frames
  1046. * (by sending a frame with the P bit set). The primary station will not
  1047. * transmit any frames and is expecting to receive frames only from the
  1048. * secondary to which transmission permissions has been given.
  1049. */
  1050. static int irlap_state_nrm_p(struct irlap_cb *self, IRLAP_EVENT event,
  1051. struct sk_buff *skb, struct irlap_info *info)
  1052. {
  1053. int ret = 0;
  1054. int ns_status;
  1055. int nr_status;
  1056. switch (event) {
  1057. case RECV_I_RSP: /* Optimize for the common case */
  1058. if (unlikely(skb->len <= LAP_ADDR_HEADER + LAP_CTRL_HEADER)) {
  1059. /*
  1060. * Input validation check: a stir4200/mcp2150
  1061. * combination sometimes results in an empty i:rsp.
  1062. * This makes no sense; we can just ignore the frame
  1063. * and send an rr:cmd immediately. This happens before
  1064. * changing nr or ns so triggers a retransmit
  1065. */
  1066. irlap_wait_min_turn_around(self, &self->qos_tx);
  1067. irlap_send_rr_frame(self, CMD_FRAME);
  1068. /* Keep state */
  1069. break;
  1070. }
  1071. /* FIXME: must check for remote_busy below */
  1072. #ifdef CONFIG_IRDA_FAST_RR
  1073. /*
  1074. * Reset the fast_RR so we can use the fast RR code with
  1075. * full speed the next time since peer may have more frames
  1076. * to transmitt
  1077. */
  1078. self->fast_RR = FALSE;
  1079. #endif /* CONFIG_IRDA_FAST_RR */
  1080. IRDA_ASSERT( info != NULL, return -1;);
  1081. ns_status = irlap_validate_ns_received(self, info->ns);
  1082. nr_status = irlap_validate_nr_received(self, info->nr);
  1083. /*
  1084. * Check for expected I(nformation) frame
  1085. */
  1086. if ((ns_status == NS_EXPECTED) && (nr_status == NR_EXPECTED)) {
  1087. /* Update Vr (next frame for us to receive) */
  1088. self->vr = (self->vr + 1) % 8;
  1089. /* Update Nr received, cleanup our retry queue */
  1090. irlap_update_nr_received(self, info->nr);
  1091. /*
  1092. * Got expected NR, so reset the
  1093. * retry_count. This is not done by IrLAP spec,
  1094. * which is strange!
  1095. */
  1096. self->retry_count = 0;
  1097. self->ack_required = TRUE;
  1098. /* poll bit cleared? */
  1099. if (!info->pf) {
  1100. /* Keep state, do not move this line */
  1101. irlap_next_state(self, LAP_NRM_P);
  1102. irlap_data_indication(self, skb, FALSE);
  1103. } else {
  1104. /* No longer waiting for pf */
  1105. del_timer(&self->final_timer);
  1106. irlap_wait_min_turn_around(self, &self->qos_tx);
  1107. /* Call higher layer *before* changing state
  1108. * to give them a chance to send data in the
  1109. * next LAP frame.
  1110. * Jean II */
  1111. irlap_data_indication(self, skb, FALSE);
  1112. /* XMIT states are the most dangerous state
  1113. * to be in, because user requests are
  1114. * processed directly and may change state.
  1115. * On the other hand, in NDM_P, those
  1116. * requests are queued and we will process
  1117. * them when we return to irlap_do_event().
  1118. * Jean II
  1119. */
  1120. irlap_next_state(self, LAP_XMIT_P);
  1121. /* This is the last frame.
  1122. * Make sure it's always called in XMIT state.
  1123. * - Jean II */
  1124. irlap_start_poll_timer(self, self->poll_timeout);
  1125. }
  1126. break;
  1127. }
  1128. /* Unexpected next to send (Ns) */
  1129. if ((ns_status == NS_UNEXPECTED) && (nr_status == NR_EXPECTED))
  1130. {
  1131. if (!info->pf) {
  1132. irlap_update_nr_received(self, info->nr);
  1133. /*
  1134. * Wait until the last frame before doing
  1135. * anything
  1136. */
  1137. /* Keep state */
  1138. irlap_next_state(self, LAP_NRM_P);
  1139. } else {
  1140. pr_debug("%s(), missing or duplicate frame!\n",
  1141. __func__);
  1142. /* Update Nr received */
  1143. irlap_update_nr_received(self, info->nr);
  1144. irlap_wait_min_turn_around(self, &self->qos_tx);
  1145. irlap_send_rr_frame(self, CMD_FRAME);
  1146. self->ack_required = FALSE;
  1147. irlap_start_final_timer(self, self->final_timeout);
  1148. irlap_next_state(self, LAP_NRM_P);
  1149. }
  1150. break;
  1151. }
  1152. /*
  1153. * Unexpected next to receive (Nr)
  1154. */
  1155. if ((ns_status == NS_EXPECTED) && (nr_status == NR_UNEXPECTED))
  1156. {
  1157. if (info->pf) {
  1158. self->vr = (self->vr + 1) % 8;
  1159. /* Update Nr received */
  1160. irlap_update_nr_received(self, info->nr);
  1161. /* Resend rejected frames */
  1162. irlap_resend_rejected_frames(self, CMD_FRAME);
  1163. self->ack_required = FALSE;
  1164. /* Make sure we account for the time
  1165. * to transmit our frames. See comemnts
  1166. * in irlap_send_data_primary_poll().
  1167. * Jean II */
  1168. irlap_start_final_timer(self, 2 * self->final_timeout);
  1169. /* Keep state, do not move this line */
  1170. irlap_next_state(self, LAP_NRM_P);
  1171. irlap_data_indication(self, skb, FALSE);
  1172. } else {
  1173. /*
  1174. * Do not resend frames until the last
  1175. * frame has arrived from the other
  1176. * device. This is not documented in
  1177. * IrLAP!!
  1178. */
  1179. self->vr = (self->vr + 1) % 8;
  1180. /* Update Nr received */
  1181. irlap_update_nr_received(self, info->nr);
  1182. self->ack_required = FALSE;
  1183. /* Keep state, do not move this line!*/
  1184. irlap_next_state(self, LAP_NRM_P);
  1185. irlap_data_indication(self, skb, FALSE);
  1186. }
  1187. break;
  1188. }
  1189. /*
  1190. * Unexpected next to send (Ns) and next to receive (Nr)
  1191. * Not documented by IrLAP!
  1192. */
  1193. if ((ns_status == NS_UNEXPECTED) &&
  1194. (nr_status == NR_UNEXPECTED))
  1195. {
  1196. pr_debug("%s(), unexpected nr and ns!\n",
  1197. __func__);
  1198. if (info->pf) {
  1199. /* Resend rejected frames */
  1200. irlap_resend_rejected_frames(self, CMD_FRAME);
  1201. /* Give peer some time to retransmit!
  1202. * But account for our own Tx. */
  1203. irlap_start_final_timer(self, 2 * self->final_timeout);
  1204. /* Keep state, do not move this line */
  1205. irlap_next_state(self, LAP_NRM_P);
  1206. } else {
  1207. /* Update Nr received */
  1208. /* irlap_update_nr_received( info->nr); */
  1209. self->ack_required = FALSE;
  1210. }
  1211. break;
  1212. }
  1213. /*
  1214. * Invalid NR or NS
  1215. */
  1216. if ((nr_status == NR_INVALID) || (ns_status == NS_INVALID)) {
  1217. if (info->pf) {
  1218. del_timer(&self->final_timer);
  1219. irlap_next_state(self, LAP_RESET_WAIT);
  1220. irlap_disconnect_indication(self, LAP_RESET_INDICATION);
  1221. self->xmitflag = TRUE;
  1222. } else {
  1223. del_timer(&self->final_timer);
  1224. irlap_disconnect_indication(self, LAP_RESET_INDICATION);
  1225. self->xmitflag = FALSE;
  1226. }
  1227. break;
  1228. }
  1229. pr_debug("%s(), Not implemented!\n", __func__);
  1230. pr_debug("%s(), event=%s, ns_status=%d, nr_status=%d\n",
  1231. __func__, irlap_event[event], ns_status, nr_status);
  1232. break;
  1233. case RECV_UI_FRAME:
  1234. /* Poll bit cleared? */
  1235. if (!info->pf) {
  1236. irlap_data_indication(self, skb, TRUE);
  1237. irlap_next_state(self, LAP_NRM_P);
  1238. } else {
  1239. del_timer(&self->final_timer);
  1240. irlap_data_indication(self, skb, TRUE);
  1241. irlap_next_state(self, LAP_XMIT_P);
  1242. pr_debug("%s: RECV_UI_FRAME: next state %s\n",
  1243. __func__, irlap_state[self->state]);
  1244. irlap_start_poll_timer(self, self->poll_timeout);
  1245. }
  1246. break;
  1247. case RECV_RR_RSP:
  1248. /*
  1249. * If you get a RR, the remote isn't busy anymore,
  1250. * no matter what the NR
  1251. */
  1252. self->remote_busy = FALSE;
  1253. /* Stop final timer */
  1254. del_timer(&self->final_timer);
  1255. /*
  1256. * Nr as expected?
  1257. */
  1258. ret = irlap_validate_nr_received(self, info->nr);
  1259. if (ret == NR_EXPECTED) {
  1260. /* Update Nr received */
  1261. irlap_update_nr_received(self, info->nr);
  1262. /*
  1263. * Got expected NR, so reset the retry_count. This
  1264. * is not done by the IrLAP standard , which is
  1265. * strange! DB.
  1266. */
  1267. self->retry_count = 0;
  1268. irlap_wait_min_turn_around(self, &self->qos_tx);
  1269. irlap_next_state(self, LAP_XMIT_P);
  1270. /* Start poll timer */
  1271. irlap_start_poll_timer(self, self->poll_timeout);
  1272. } else if (ret == NR_UNEXPECTED) {
  1273. IRDA_ASSERT(info != NULL, return -1;);
  1274. /*
  1275. * Unexpected nr!
  1276. */
  1277. /* Update Nr received */
  1278. irlap_update_nr_received(self, info->nr);
  1279. pr_debug("RECV_RR_FRAME: Retrans:%d, nr=%d, va=%d, vs=%d, vr=%d\n",
  1280. self->retry_count, info->nr, self->va,
  1281. self->vs, self->vr);
  1282. /* Resend rejected frames */
  1283. irlap_resend_rejected_frames(self, CMD_FRAME);
  1284. irlap_start_final_timer(self, self->final_timeout * 2);
  1285. irlap_next_state(self, LAP_NRM_P);
  1286. } else if (ret == NR_INVALID) {
  1287. pr_debug("%s(), Received RR with invalid nr !\n",
  1288. __func__);
  1289. irlap_next_state(self, LAP_RESET_WAIT);
  1290. irlap_disconnect_indication(self, LAP_RESET_INDICATION);
  1291. self->xmitflag = TRUE;
  1292. }
  1293. break;
  1294. case RECV_RNR_RSP:
  1295. IRDA_ASSERT(info != NULL, return -1;);
  1296. /* Stop final timer */
  1297. del_timer(&self->final_timer);
  1298. self->remote_busy = TRUE;
  1299. /* Update Nr received */
  1300. irlap_update_nr_received(self, info->nr);
  1301. irlap_next_state(self, LAP_XMIT_P);
  1302. /* Start poll timer */
  1303. irlap_start_poll_timer(self, self->poll_timeout);
  1304. break;
  1305. case RECV_FRMR_RSP:
  1306. del_timer(&self->final_timer);
  1307. self->xmitflag = TRUE;
  1308. irlap_next_state(self, LAP_RESET_WAIT);
  1309. irlap_reset_indication(self);
  1310. break;
  1311. case FINAL_TIMER_EXPIRED:
  1312. /*
  1313. * We are allowed to wait for additional 300 ms if
  1314. * final timer expires when we are in the middle
  1315. * of receiving a frame (page 45, IrLAP). Check that
  1316. * we only do this once for each frame.
  1317. */
  1318. if (irda_device_is_receiving(self->netdev) && !self->add_wait) {
  1319. pr_debug("FINAL_TIMER_EXPIRED when receiving a frame! Waiting a little bit more!\n");
  1320. irlap_start_final_timer(self, msecs_to_jiffies(300));
  1321. /*
  1322. * Don't allow this to happen one more time in a row,
  1323. * or else we can get a pretty tight loop here if
  1324. * if we only receive half a frame. DB.
  1325. */
  1326. self->add_wait = TRUE;
  1327. break;
  1328. }
  1329. self->add_wait = FALSE;
  1330. /* N2 is the disconnect timer. Until we reach it, we retry */
  1331. if (self->retry_count < self->N2) {
  1332. if (skb_peek(&self->wx_list) == NULL) {
  1333. /* Retry sending the pf bit to the secondary */
  1334. pr_debug("nrm_p: resending rr");
  1335. irlap_wait_min_turn_around(self, &self->qos_tx);
  1336. irlap_send_rr_frame(self, CMD_FRAME);
  1337. } else {
  1338. pr_debug("nrm_p: resend frames");
  1339. irlap_resend_rejected_frames(self, CMD_FRAME);
  1340. }
  1341. irlap_start_final_timer(self, self->final_timeout);
  1342. self->retry_count++;
  1343. pr_debug("irlap_state_nrm_p: FINAL_TIMER_EXPIRED: retry_count=%d\n",
  1344. self->retry_count);
  1345. /* Early warning event. I'm using a pretty liberal
  1346. * interpretation of the spec and generate an event
  1347. * every time the timer is multiple of N1 (and not
  1348. * only the first time). This allow application
  1349. * to know precisely if connectivity restart...
  1350. * Jean II */
  1351. if((self->retry_count % self->N1) == 0)
  1352. irlap_status_indication(self,
  1353. STATUS_NO_ACTIVITY);
  1354. /* Keep state */
  1355. } else {
  1356. irlap_apply_default_connection_parameters(self);
  1357. /* Always switch state before calling upper layers */
  1358. irlap_next_state(self, LAP_NDM);
  1359. irlap_disconnect_indication(self, LAP_NO_RESPONSE);
  1360. }
  1361. break;
  1362. case RECV_REJ_RSP:
  1363. irlap_update_nr_received(self, info->nr);
  1364. if (self->remote_busy) {
  1365. irlap_wait_min_turn_around(self, &self->qos_tx);
  1366. irlap_send_rr_frame(self, CMD_FRAME);
  1367. } else
  1368. irlap_resend_rejected_frames(self, CMD_FRAME);
  1369. irlap_start_final_timer(self, 2 * self->final_timeout);
  1370. break;
  1371. case RECV_SREJ_RSP:
  1372. irlap_update_nr_received(self, info->nr);
  1373. if (self->remote_busy) {
  1374. irlap_wait_min_turn_around(self, &self->qos_tx);
  1375. irlap_send_rr_frame(self, CMD_FRAME);
  1376. } else
  1377. irlap_resend_rejected_frame(self, CMD_FRAME);
  1378. irlap_start_final_timer(self, 2 * self->final_timeout);
  1379. break;
  1380. case RECV_RD_RSP:
  1381. pr_debug("%s(), RECV_RD_RSP\n", __func__);
  1382. irlap_flush_all_queues(self);
  1383. irlap_next_state(self, LAP_XMIT_P);
  1384. /* Call back the LAP state machine to do a proper disconnect */
  1385. irlap_disconnect_request(self);
  1386. break;
  1387. default:
  1388. pr_debug("%s(), Unknown event %s\n",
  1389. __func__, irlap_event[event]);
  1390. ret = -1;
  1391. break;
  1392. }
  1393. return ret;
  1394. }
  1395. /*
  1396. * Function irlap_state_reset_wait (event, skb, info)
  1397. *
  1398. * We have informed the service user of a reset condition, and is
  1399. * awaiting reset of disconnect request.
  1400. *
  1401. */
  1402. static int irlap_state_reset_wait(struct irlap_cb *self, IRLAP_EVENT event,
  1403. struct sk_buff *skb, struct irlap_info *info)
  1404. {
  1405. int ret = 0;
  1406. pr_debug("%s(), event = %s\n", __func__, irlap_event[event]);
  1407. IRDA_ASSERT(self != NULL, return -1;);
  1408. IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
  1409. switch (event) {
  1410. case RESET_REQUEST:
  1411. if (self->xmitflag) {
  1412. irlap_wait_min_turn_around(self, &self->qos_tx);
  1413. irlap_send_snrm_frame(self, NULL);
  1414. irlap_start_final_timer(self, self->final_timeout);
  1415. irlap_next_state(self, LAP_RESET);
  1416. } else {
  1417. irlap_start_final_timer(self, self->final_timeout);
  1418. irlap_next_state(self, LAP_RESET);
  1419. }
  1420. break;
  1421. case DISCONNECT_REQUEST:
  1422. irlap_wait_min_turn_around( self, &self->qos_tx);
  1423. irlap_send_disc_frame( self);
  1424. irlap_flush_all_queues( self);
  1425. irlap_start_final_timer( self, self->final_timeout);
  1426. self->retry_count = 0;
  1427. irlap_next_state( self, LAP_PCLOSE);
  1428. break;
  1429. default:
  1430. pr_debug("%s(), Unknown event %s\n", __func__,
  1431. irlap_event[event]);
  1432. ret = -1;
  1433. break;
  1434. }
  1435. return ret;
  1436. }
  1437. /*
  1438. * Function irlap_state_reset (self, event, skb, info)
  1439. *
  1440. * We have sent a SNRM reset command to the peer layer, and is awaiting
  1441. * reply.
  1442. *
  1443. */
  1444. static int irlap_state_reset(struct irlap_cb *self, IRLAP_EVENT event,
  1445. struct sk_buff *skb, struct irlap_info *info)
  1446. {
  1447. int ret = 0;
  1448. pr_debug("%s(), event = %s\n", __func__, irlap_event[event]);
  1449. IRDA_ASSERT(self != NULL, return -1;);
  1450. IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
  1451. switch (event) {
  1452. case RECV_DISC_CMD:
  1453. del_timer(&self->final_timer);
  1454. irlap_apply_default_connection_parameters(self);
  1455. /* Always switch state before calling upper layers */
  1456. irlap_next_state(self, LAP_NDM);
  1457. irlap_disconnect_indication(self, LAP_NO_RESPONSE);
  1458. break;
  1459. case RECV_UA_RSP:
  1460. del_timer(&self->final_timer);
  1461. /* Initiate connection state */
  1462. irlap_initiate_connection_state(self);
  1463. irlap_reset_confirm();
  1464. self->remote_busy = FALSE;
  1465. irlap_next_state(self, LAP_XMIT_P);
  1466. irlap_start_poll_timer(self, self->poll_timeout);
  1467. break;
  1468. case FINAL_TIMER_EXPIRED:
  1469. if (self->retry_count < 3) {
  1470. irlap_wait_min_turn_around(self, &self->qos_tx);
  1471. IRDA_ASSERT(self->netdev != NULL, return -1;);
  1472. irlap_send_snrm_frame(self, self->qos_dev);
  1473. self->retry_count++; /* Experimental!! */
  1474. irlap_start_final_timer(self, self->final_timeout);
  1475. irlap_next_state(self, LAP_RESET);
  1476. } else if (self->retry_count >= self->N3) {
  1477. irlap_apply_default_connection_parameters(self);
  1478. /* Always switch state before calling upper layers */
  1479. irlap_next_state(self, LAP_NDM);
  1480. irlap_disconnect_indication(self, LAP_NO_RESPONSE);
  1481. }
  1482. break;
  1483. case RECV_SNRM_CMD:
  1484. /*
  1485. * SNRM frame is not allowed to contain an I-field in this
  1486. * state
  1487. */
  1488. if (!info) {
  1489. pr_debug("%s(), RECV_SNRM_CMD\n", __func__);
  1490. irlap_initiate_connection_state(self);
  1491. irlap_wait_min_turn_around(self, &self->qos_tx);
  1492. irlap_send_ua_response_frame(self, &self->qos_rx);
  1493. irlap_reset_confirm();
  1494. irlap_start_wd_timer(self, self->wd_timeout);
  1495. irlap_next_state(self, LAP_NDM);
  1496. } else {
  1497. pr_debug("%s(), SNRM frame contained an I field!\n",
  1498. __func__);
  1499. }
  1500. break;
  1501. default:
  1502. pr_debug("%s(), Unknown event %s\n",
  1503. __func__, irlap_event[event]);
  1504. ret = -1;
  1505. break;
  1506. }
  1507. return ret;
  1508. }
  1509. /*
  1510. * Function irlap_state_xmit_s (event, skb, info)
  1511. *
  1512. * XMIT_S, The secondary station has been given the right to transmit,
  1513. * and we therefore do not expect to receive any transmissions from other
  1514. * stations.
  1515. */
  1516. static int irlap_state_xmit_s(struct irlap_cb *self, IRLAP_EVENT event,
  1517. struct sk_buff *skb, struct irlap_info *info)
  1518. {
  1519. int ret = 0;
  1520. pr_debug("%s(), event=%s\n", __func__, irlap_event[event]);
  1521. IRDA_ASSERT(self != NULL, return -ENODEV;);
  1522. IRDA_ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
  1523. switch (event) {
  1524. case SEND_I_CMD:
  1525. /*
  1526. * Send frame only if send window > 0
  1527. */
  1528. if ((self->window > 0) && (!self->remote_busy)) {
  1529. int nextfit;
  1530. #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
  1531. struct sk_buff *skb_next;
  1532. /*
  1533. * Same deal as in irlap_state_xmit_p(), so see
  1534. * the comments at that point.
  1535. * We are the secondary, so there are only subtle
  1536. * differences. - Jean II
  1537. */
  1538. /* Check if a subsequent skb exist and would fit in
  1539. * the current window (with respect to turnaround
  1540. * time). - Jean II */
  1541. skb_next = skb_peek(&self->txq);
  1542. nextfit = ((skb_next != NULL) &&
  1543. ((skb_next->len + skb->len) <=
  1544. self->bytes_left));
  1545. /*
  1546. * Test if we have transmitted more bytes over the
  1547. * link than its possible to do with the current
  1548. * speed and turn-around-time.
  1549. */
  1550. if((!nextfit) && (skb->len > self->bytes_left)) {
  1551. pr_debug("%s(), Not allowed to transmit more bytes!\n",
  1552. __func__);
  1553. /* Requeue the skb */
  1554. skb_queue_head(&self->txq, skb_get(skb));
  1555. /*
  1556. * Switch to NRM_S, this is only possible
  1557. * when we are in secondary mode, since we
  1558. * must be sure that we don't miss any RR
  1559. * frames
  1560. */
  1561. self->window = self->window_size;
  1562. self->bytes_left = self->line_capacity;
  1563. irlap_start_wd_timer(self, self->wd_timeout);
  1564. irlap_next_state(self, LAP_NRM_S);
  1565. /* Slight difference with primary :
  1566. * here we would wait for the other side to
  1567. * expire the turnaround. - Jean II */
  1568. return -EPROTO; /* Try again later */
  1569. }
  1570. /* Subtract space used by this skb */
  1571. self->bytes_left -= skb->len;
  1572. #else /* CONFIG_IRDA_DYNAMIC_WINDOW */
  1573. /* Window has been adjusted for the max packet
  1574. * size, so much simpler... - Jean II */
  1575. nextfit = !skb_queue_empty(&self->txq);
  1576. #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
  1577. /*
  1578. * Send data with final bit cleared only if window > 1
  1579. * and there is more frames to be sent
  1580. */
  1581. if ((self->window > 1) && (nextfit)) {
  1582. irlap_send_data_secondary(self, skb);
  1583. irlap_next_state(self, LAP_XMIT_S);
  1584. } else {
  1585. irlap_send_data_secondary_final(self, skb);
  1586. irlap_next_state(self, LAP_NRM_S);
  1587. /*
  1588. * Make sure state machine does not try to send
  1589. * any more frames
  1590. */
  1591. ret = -EPROTO;
  1592. }
  1593. } else {
  1594. pr_debug("%s(), Unable to send!\n", __func__);
  1595. skb_queue_head(&self->txq, skb_get(skb));
  1596. ret = -EPROTO;
  1597. }
  1598. break;
  1599. case DISCONNECT_REQUEST:
  1600. irlap_send_rd_frame(self);
  1601. irlap_flush_all_queues(self);
  1602. irlap_start_wd_timer(self, self->wd_timeout);
  1603. irlap_next_state(self, LAP_SCLOSE);
  1604. break;
  1605. case DATA_REQUEST:
  1606. /* Nothing to do, irlap_do_event() will send the packet
  1607. * when we return... - Jean II */
  1608. break;
  1609. default:
  1610. pr_debug("%s(), Unknown event %s\n", __func__,
  1611. irlap_event[event]);
  1612. ret = -EINVAL;
  1613. break;
  1614. }
  1615. return ret;
  1616. }
  1617. /*
  1618. * Function irlap_state_nrm_s (event, skb, info)
  1619. *
  1620. * NRM_S (Normal Response Mode as Secondary) state, in this state we are
  1621. * expecting to receive frames from the primary station
  1622. *
  1623. */
  1624. static int irlap_state_nrm_s(struct irlap_cb *self, IRLAP_EVENT event,
  1625. struct sk_buff *skb, struct irlap_info *info)
  1626. {
  1627. int ns_status;
  1628. int nr_status;
  1629. int ret = 0;
  1630. pr_debug("%s(), event=%s\n", __func__, irlap_event[event]);
  1631. IRDA_ASSERT(self != NULL, return -1;);
  1632. IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
  1633. switch (event) {
  1634. case RECV_I_CMD: /* Optimize for the common case */
  1635. /* FIXME: must check for remote_busy below */
  1636. pr_debug("%s(), event=%s nr=%d, vs=%d, ns=%d, vr=%d, pf=%d\n",
  1637. __func__, irlap_event[event], info->nr,
  1638. self->vs, info->ns, self->vr, info->pf);
  1639. self->retry_count = 0;
  1640. ns_status = irlap_validate_ns_received(self, info->ns);
  1641. nr_status = irlap_validate_nr_received(self, info->nr);
  1642. /*
  1643. * Check for expected I(nformation) frame
  1644. */
  1645. if ((ns_status == NS_EXPECTED) && (nr_status == NR_EXPECTED)) {
  1646. /* Update Vr (next frame for us to receive) */
  1647. self->vr = (self->vr + 1) % 8;
  1648. /* Update Nr received */
  1649. irlap_update_nr_received(self, info->nr);
  1650. /*
  1651. * poll bit cleared?
  1652. */
  1653. if (!info->pf) {
  1654. self->ack_required = TRUE;
  1655. /*
  1656. * Starting WD-timer here is optional, but
  1657. * not recommended. Note 6 IrLAP p. 83
  1658. */
  1659. #if 0
  1660. irda_start_timer(WD_TIMER, self->wd_timeout);
  1661. #endif
  1662. /* Keep state, do not move this line */
  1663. irlap_next_state(self, LAP_NRM_S);
  1664. irlap_data_indication(self, skb, FALSE);
  1665. break;
  1666. } else {
  1667. /*
  1668. * We should wait before sending RR, and
  1669. * also before changing to XMIT_S
  1670. * state. (note 1, IrLAP p. 82)
  1671. */
  1672. irlap_wait_min_turn_around(self, &self->qos_tx);
  1673. /*
  1674. * Give higher layers a chance to
  1675. * immediately reply with some data before
  1676. * we decide if we should send a RR frame
  1677. * or not
  1678. */
  1679. irlap_data_indication(self, skb, FALSE);
  1680. /* Any pending data requests? */
  1681. if (!skb_queue_empty(&self->txq) &&
  1682. (self->window > 0))
  1683. {
  1684. self->ack_required = TRUE;
  1685. del_timer(&self->wd_timer);
  1686. irlap_next_state(self, LAP_XMIT_S);
  1687. } else {
  1688. irlap_send_rr_frame(self, RSP_FRAME);
  1689. irlap_start_wd_timer(self,
  1690. self->wd_timeout);
  1691. /* Keep the state */
  1692. irlap_next_state(self, LAP_NRM_S);
  1693. }
  1694. break;
  1695. }
  1696. }
  1697. /*
  1698. * Check for Unexpected next to send (Ns)
  1699. */
  1700. if ((ns_status == NS_UNEXPECTED) && (nr_status == NR_EXPECTED))
  1701. {
  1702. /* Unexpected next to send, with final bit cleared */
  1703. if (!info->pf) {
  1704. irlap_update_nr_received(self, info->nr);
  1705. irlap_start_wd_timer(self, self->wd_timeout);
  1706. } else {
  1707. /* Update Nr received */
  1708. irlap_update_nr_received(self, info->nr);
  1709. irlap_wait_min_turn_around(self, &self->qos_tx);
  1710. irlap_send_rr_frame(self, RSP_FRAME);
  1711. irlap_start_wd_timer(self, self->wd_timeout);
  1712. }
  1713. break;
  1714. }
  1715. /*
  1716. * Unexpected Next to Receive(NR) ?
  1717. */
  1718. if ((ns_status == NS_EXPECTED) && (nr_status == NR_UNEXPECTED))
  1719. {
  1720. if (info->pf) {
  1721. pr_debug("RECV_I_RSP: frame(s) lost\n");
  1722. self->vr = (self->vr + 1) % 8;
  1723. /* Update Nr received */
  1724. irlap_update_nr_received(self, info->nr);
  1725. /* Resend rejected frames */
  1726. irlap_resend_rejected_frames(self, RSP_FRAME);
  1727. /* Keep state, do not move this line */
  1728. irlap_next_state(self, LAP_NRM_S);
  1729. irlap_data_indication(self, skb, FALSE);
  1730. irlap_start_wd_timer(self, self->wd_timeout);
  1731. break;
  1732. }
  1733. /*
  1734. * This is not documented in IrLAP!! Unexpected NR
  1735. * with poll bit cleared
  1736. */
  1737. if (!info->pf) {
  1738. self->vr = (self->vr + 1) % 8;
  1739. /* Update Nr received */
  1740. irlap_update_nr_received(self, info->nr);
  1741. /* Keep state, do not move this line */
  1742. irlap_next_state(self, LAP_NRM_S);
  1743. irlap_data_indication(self, skb, FALSE);
  1744. irlap_start_wd_timer(self, self->wd_timeout);
  1745. }
  1746. break;
  1747. }
  1748. if (ret == NR_INVALID) {
  1749. pr_debug("NRM_S, NR_INVALID not implemented!\n");
  1750. }
  1751. if (ret == NS_INVALID) {
  1752. pr_debug("NRM_S, NS_INVALID not implemented!\n");
  1753. }
  1754. break;
  1755. case RECV_UI_FRAME:
  1756. /*
  1757. * poll bit cleared?
  1758. */
  1759. if (!info->pf) {
  1760. irlap_data_indication(self, skb, TRUE);
  1761. irlap_next_state(self, LAP_NRM_S); /* Keep state */
  1762. } else {
  1763. /*
  1764. * Any pending data requests?
  1765. */
  1766. if (!skb_queue_empty(&self->txq) &&
  1767. (self->window > 0) && !self->remote_busy)
  1768. {
  1769. irlap_data_indication(self, skb, TRUE);
  1770. del_timer(&self->wd_timer);
  1771. irlap_next_state(self, LAP_XMIT_S);
  1772. } else {
  1773. irlap_data_indication(self, skb, TRUE);
  1774. irlap_wait_min_turn_around(self, &self->qos_tx);
  1775. irlap_send_rr_frame(self, RSP_FRAME);
  1776. self->ack_required = FALSE;
  1777. irlap_start_wd_timer(self, self->wd_timeout);
  1778. /* Keep the state */
  1779. irlap_next_state(self, LAP_NRM_S);
  1780. }
  1781. }
  1782. break;
  1783. case RECV_RR_CMD:
  1784. self->retry_count = 0;
  1785. /*
  1786. * Nr as expected?
  1787. */
  1788. nr_status = irlap_validate_nr_received(self, info->nr);
  1789. if (nr_status == NR_EXPECTED) {
  1790. if (!skb_queue_empty(&self->txq) &&
  1791. (self->window > 0)) {
  1792. self->remote_busy = FALSE;
  1793. /* Update Nr received */
  1794. irlap_update_nr_received(self, info->nr);
  1795. del_timer(&self->wd_timer);
  1796. irlap_wait_min_turn_around(self, &self->qos_tx);
  1797. irlap_next_state(self, LAP_XMIT_S);
  1798. } else {
  1799. self->remote_busy = FALSE;
  1800. /* Update Nr received */
  1801. irlap_update_nr_received(self, info->nr);
  1802. irlap_wait_min_turn_around(self, &self->qos_tx);
  1803. irlap_start_wd_timer(self, self->wd_timeout);
  1804. /* Note : if the link is idle (this case),
  1805. * we never go in XMIT_S, so we never get a
  1806. * chance to process any DISCONNECT_REQUEST.
  1807. * Do it now ! - Jean II */
  1808. if (self->disconnect_pending) {
  1809. /* Disconnect */
  1810. irlap_send_rd_frame(self);
  1811. irlap_flush_all_queues(self);
  1812. irlap_next_state(self, LAP_SCLOSE);
  1813. } else {
  1814. /* Just send back pf bit */
  1815. irlap_send_rr_frame(self, RSP_FRAME);
  1816. irlap_next_state(self, LAP_NRM_S);
  1817. }
  1818. }
  1819. } else if (nr_status == NR_UNEXPECTED) {
  1820. self->remote_busy = FALSE;
  1821. irlap_update_nr_received(self, info->nr);
  1822. irlap_resend_rejected_frames(self, RSP_FRAME);
  1823. irlap_start_wd_timer(self, self->wd_timeout);
  1824. /* Keep state */
  1825. irlap_next_state(self, LAP_NRM_S);
  1826. } else {
  1827. pr_debug("%s(), invalid nr not implemented!\n",
  1828. __func__);
  1829. }
  1830. break;
  1831. case RECV_SNRM_CMD:
  1832. /* SNRM frame is not allowed to contain an I-field */
  1833. if (!info) {
  1834. del_timer(&self->wd_timer);
  1835. pr_debug("%s(), received SNRM cmd\n", __func__);
  1836. irlap_next_state(self, LAP_RESET_CHECK);
  1837. irlap_reset_indication(self);
  1838. } else {
  1839. pr_debug("%s(), SNRM frame contained an I-field!\n",
  1840. __func__);
  1841. }
  1842. break;
  1843. case RECV_REJ_CMD:
  1844. irlap_update_nr_received(self, info->nr);
  1845. if (self->remote_busy) {
  1846. irlap_wait_min_turn_around(self, &self->qos_tx);
  1847. irlap_send_rr_frame(self, RSP_FRAME);
  1848. } else
  1849. irlap_resend_rejected_frames(self, RSP_FRAME);
  1850. irlap_start_wd_timer(self, self->wd_timeout);
  1851. break;
  1852. case RECV_SREJ_CMD:
  1853. irlap_update_nr_received(self, info->nr);
  1854. if (self->remote_busy) {
  1855. irlap_wait_min_turn_around(self, &self->qos_tx);
  1856. irlap_send_rr_frame(self, RSP_FRAME);
  1857. } else
  1858. irlap_resend_rejected_frame(self, RSP_FRAME);
  1859. irlap_start_wd_timer(self, self->wd_timeout);
  1860. break;
  1861. case WD_TIMER_EXPIRED:
  1862. /*
  1863. * Wait until retry_count * n matches negotiated threshold/
  1864. * disconnect time (note 2 in IrLAP p. 82)
  1865. *
  1866. * Similar to irlap_state_nrm_p() -> FINAL_TIMER_EXPIRED
  1867. * Note : self->wd_timeout = (self->final_timeout * 2),
  1868. * which explain why we use (self->N2 / 2) here !!!
  1869. * Jean II
  1870. */
  1871. pr_debug("%s(), retry_count = %d\n", __func__,
  1872. self->retry_count);
  1873. if (self->retry_count < (self->N2 / 2)) {
  1874. /* No retry, just wait for primary */
  1875. irlap_start_wd_timer(self, self->wd_timeout);
  1876. self->retry_count++;
  1877. if((self->retry_count % (self->N1 / 2)) == 0)
  1878. irlap_status_indication(self,
  1879. STATUS_NO_ACTIVITY);
  1880. } else {
  1881. irlap_apply_default_connection_parameters(self);
  1882. /* Always switch state before calling upper layers */
  1883. irlap_next_state(self, LAP_NDM);
  1884. irlap_disconnect_indication(self, LAP_NO_RESPONSE);
  1885. }
  1886. break;
  1887. case RECV_DISC_CMD:
  1888. /* Always switch state before calling upper layers */
  1889. irlap_next_state(self, LAP_NDM);
  1890. /* Send disconnect response */
  1891. irlap_wait_min_turn_around(self, &self->qos_tx);
  1892. irlap_send_ua_response_frame(self, NULL);
  1893. del_timer(&self->wd_timer);
  1894. irlap_flush_all_queues(self);
  1895. /* Set default link parameters */
  1896. irlap_apply_default_connection_parameters(self);
  1897. irlap_disconnect_indication(self, LAP_DISC_INDICATION);
  1898. break;
  1899. case RECV_DISCOVERY_XID_CMD:
  1900. irlap_wait_min_turn_around(self, &self->qos_tx);
  1901. irlap_send_rr_frame(self, RSP_FRAME);
  1902. self->ack_required = TRUE;
  1903. irlap_start_wd_timer(self, self->wd_timeout);
  1904. irlap_next_state(self, LAP_NRM_S);
  1905. break;
  1906. case RECV_TEST_CMD:
  1907. /* Remove test frame header (only LAP header in NRM) */
  1908. skb_pull(skb, LAP_ADDR_HEADER + LAP_CTRL_HEADER);
  1909. irlap_wait_min_turn_around(self, &self->qos_tx);
  1910. irlap_start_wd_timer(self, self->wd_timeout);
  1911. /* Send response (info will be copied) */
  1912. irlap_send_test_frame(self, self->caddr, info->daddr, skb);
  1913. break;
  1914. default:
  1915. pr_debug("%s(), Unknown event %d, (%s)\n", __func__,
  1916. event, irlap_event[event]);
  1917. ret = -EINVAL;
  1918. break;
  1919. }
  1920. return ret;
  1921. }
  1922. /*
  1923. * Function irlap_state_sclose (self, event, skb, info)
  1924. */
  1925. static int irlap_state_sclose(struct irlap_cb *self, IRLAP_EVENT event,
  1926. struct sk_buff *skb, struct irlap_info *info)
  1927. {
  1928. IRDA_ASSERT(self != NULL, return -ENODEV;);
  1929. IRDA_ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
  1930. switch (event) {
  1931. case RECV_DISC_CMD:
  1932. /* Always switch state before calling upper layers */
  1933. irlap_next_state(self, LAP_NDM);
  1934. /* Send disconnect response */
  1935. irlap_wait_min_turn_around(self, &self->qos_tx);
  1936. irlap_send_ua_response_frame(self, NULL);
  1937. del_timer(&self->wd_timer);
  1938. /* Set default link parameters */
  1939. irlap_apply_default_connection_parameters(self);
  1940. irlap_disconnect_indication(self, LAP_DISC_INDICATION);
  1941. break;
  1942. case RECV_DM_RSP:
  1943. /* IrLAP-1.1 p.82: in SCLOSE, S and I type RSP frames
  1944. * shall take us down into default NDM state, like DM_RSP
  1945. */
  1946. case RECV_RR_RSP:
  1947. case RECV_RNR_RSP:
  1948. case RECV_REJ_RSP:
  1949. case RECV_SREJ_RSP:
  1950. case RECV_I_RSP:
  1951. /* Always switch state before calling upper layers */
  1952. irlap_next_state(self, LAP_NDM);
  1953. del_timer(&self->wd_timer);
  1954. irlap_apply_default_connection_parameters(self);
  1955. irlap_disconnect_indication(self, LAP_DISC_INDICATION);
  1956. break;
  1957. case WD_TIMER_EXPIRED:
  1958. /* Always switch state before calling upper layers */
  1959. irlap_next_state(self, LAP_NDM);
  1960. irlap_apply_default_connection_parameters(self);
  1961. irlap_disconnect_indication(self, LAP_DISC_INDICATION);
  1962. break;
  1963. default:
  1964. /* IrLAP-1.1 p.82: in SCLOSE, basically any received frame
  1965. * with pf=1 shall restart the wd-timer and resend the rd:rsp
  1966. */
  1967. if (info != NULL && info->pf) {
  1968. del_timer(&self->wd_timer);
  1969. irlap_wait_min_turn_around(self, &self->qos_tx);
  1970. irlap_send_rd_frame(self);
  1971. irlap_start_wd_timer(self, self->wd_timeout);
  1972. break; /* stay in SCLOSE */
  1973. }
  1974. pr_debug("%s(), Unknown event %d, (%s)\n", __func__,
  1975. event, irlap_event[event]);
  1976. break;
  1977. }
  1978. return -1;
  1979. }
  1980. static int irlap_state_reset_check( struct irlap_cb *self, IRLAP_EVENT event,
  1981. struct sk_buff *skb,
  1982. struct irlap_info *info)
  1983. {
  1984. int ret = 0;
  1985. pr_debug("%s(), event=%s\n", __func__, irlap_event[event]);
  1986. IRDA_ASSERT(self != NULL, return -ENODEV;);
  1987. IRDA_ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
  1988. switch (event) {
  1989. case RESET_RESPONSE:
  1990. irlap_send_ua_response_frame(self, &self->qos_rx);
  1991. irlap_initiate_connection_state(self);
  1992. irlap_start_wd_timer(self, WD_TIMEOUT);
  1993. irlap_flush_all_queues(self);
  1994. irlap_next_state(self, LAP_NRM_S);
  1995. break;
  1996. case DISCONNECT_REQUEST:
  1997. irlap_wait_min_turn_around(self, &self->qos_tx);
  1998. irlap_send_rd_frame(self);
  1999. irlap_start_wd_timer(self, WD_TIMEOUT);
  2000. irlap_next_state(self, LAP_SCLOSE);
  2001. break;
  2002. default:
  2003. pr_debug("%s(), Unknown event %d, (%s)\n", __func__,
  2004. event, irlap_event[event]);
  2005. ret = -EINVAL;
  2006. break;
  2007. }
  2008. return ret;
  2009. }