irlap_event.c 65 KB

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