irlap_event.c 65 KB

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