PageRenderTime 58ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/net/irda/irlap_event.c

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