irlap_event.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313
  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_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. __FUNCTION__);
  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", __FUNCTION__);
  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. __FUNCTION__);
  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", __FUNCTION__);
  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. __FUNCTION__);
  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", __FUNCTION__);
  491. break;
  492. default:
  493. IRDA_DEBUG(2, "%s(), Unknown event %s\n", __FUNCTION__,
  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", __FUNCTION__,
  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", __FUNCTION__);
  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", __FUNCTION__, 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", __FUNCTION__);
  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", __FUNCTION__,
  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", __FUNCTION__);
  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. __FUNCTION__, 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", __FUNCTION__,
  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", __FUNCTION__, 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. __FUNCTION__);
  714. irlap_next_state(self, LAP_NDM);
  715. break;
  716. case DISCONNECT_REQUEST:
  717. IRDA_DEBUG(0, "%s(), Disconnect request!\n", __FUNCTION__);
  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", __FUNCTION__,
  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", __FUNCTION__);
  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", __FUNCTION__);
  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", __FUNCTION__,
  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", __FUNCTION__);
  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", __FUNCTION__);
  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. __FUNCTION__);
  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. __FUNCTION__, 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. __FUNCTION__, 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", __FUNCTION__);
  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", __FUNCTION__, 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. /* FIXME: must check for remote_busy below */
  1062. #ifdef CONFIG_IRDA_FAST_RR
  1063. /*
  1064. * Reset the fast_RR so we can use the fast RR code with
  1065. * full speed the next time since peer may have more frames
  1066. * to transmitt
  1067. */
  1068. self->fast_RR = FALSE;
  1069. #endif /* CONFIG_IRDA_FAST_RR */
  1070. IRDA_ASSERT( info != NULL, return -1;);
  1071. ns_status = irlap_validate_ns_received(self, info->ns);
  1072. nr_status = irlap_validate_nr_received(self, info->nr);
  1073. /*
  1074. * Check for expected I(nformation) frame
  1075. */
  1076. if ((ns_status == NS_EXPECTED) && (nr_status == NR_EXPECTED)) {
  1077. /* Update Vr (next frame for us to receive) */
  1078. self->vr = (self->vr + 1) % 8;
  1079. /* Update Nr received, cleanup our retry queue */
  1080. irlap_update_nr_received(self, info->nr);
  1081. /*
  1082. * Got expected NR, so reset the
  1083. * retry_count. This is not done by IrLAP spec,
  1084. * which is strange!
  1085. */
  1086. self->retry_count = 0;
  1087. self->ack_required = TRUE;
  1088. /* poll bit cleared? */
  1089. if (!info->pf) {
  1090. /* Keep state, do not move this line */
  1091. irlap_next_state(self, LAP_NRM_P);
  1092. irlap_data_indication(self, skb, FALSE);
  1093. } else {
  1094. /* No longer waiting for pf */
  1095. del_timer(&self->final_timer);
  1096. irlap_wait_min_turn_around(self, &self->qos_tx);
  1097. /* Call higher layer *before* changing state
  1098. * to give them a chance to send data in the
  1099. * next LAP frame.
  1100. * Jean II */
  1101. irlap_data_indication(self, skb, FALSE);
  1102. /* XMIT states are the most dangerous state
  1103. * to be in, because user requests are
  1104. * processed directly and may change state.
  1105. * On the other hand, in NDM_P, those
  1106. * requests are queued and we will process
  1107. * them when we return to irlap_do_event().
  1108. * Jean II
  1109. */
  1110. irlap_next_state(self, LAP_XMIT_P);
  1111. /* This is the last frame.
  1112. * Make sure it's always called in XMIT state.
  1113. * - Jean II */
  1114. irlap_start_poll_timer(self, self->poll_timeout);
  1115. }
  1116. break;
  1117. }
  1118. /* Unexpected next to send (Ns) */
  1119. if ((ns_status == NS_UNEXPECTED) && (nr_status == NR_EXPECTED))
  1120. {
  1121. if (!info->pf) {
  1122. irlap_update_nr_received(self, info->nr);
  1123. /*
  1124. * Wait until the last frame before doing
  1125. * anything
  1126. */
  1127. /* Keep state */
  1128. irlap_next_state(self, LAP_NRM_P);
  1129. } else {
  1130. IRDA_DEBUG(4,
  1131. "%s(), missing or duplicate frame!\n",
  1132. __FUNCTION__);
  1133. /* Update Nr received */
  1134. irlap_update_nr_received(self, info->nr);
  1135. irlap_wait_min_turn_around(self, &self->qos_tx);
  1136. irlap_send_rr_frame(self, CMD_FRAME);
  1137. self->ack_required = FALSE;
  1138. irlap_start_final_timer(self, self->final_timeout);
  1139. irlap_next_state(self, LAP_NRM_P);
  1140. }
  1141. break;
  1142. }
  1143. /*
  1144. * Unexpected next to receive (Nr)
  1145. */
  1146. if ((ns_status == NS_EXPECTED) && (nr_status == NR_UNEXPECTED))
  1147. {
  1148. if (info->pf) {
  1149. self->vr = (self->vr + 1) % 8;
  1150. /* Update Nr received */
  1151. irlap_update_nr_received(self, info->nr);
  1152. /* Resend rejected frames */
  1153. irlap_resend_rejected_frames(self, CMD_FRAME);
  1154. self->ack_required = FALSE;
  1155. /* Make sure we account for the time
  1156. * to transmit our frames. See comemnts
  1157. * in irlap_send_data_primary_poll().
  1158. * Jean II */
  1159. irlap_start_final_timer(self, 2 * self->final_timeout);
  1160. /* Keep state, do not move this line */
  1161. irlap_next_state(self, LAP_NRM_P);
  1162. irlap_data_indication(self, skb, FALSE);
  1163. } else {
  1164. /*
  1165. * Do not resend frames until the last
  1166. * frame has arrived from the other
  1167. * device. This is not documented in
  1168. * IrLAP!!
  1169. */
  1170. self->vr = (self->vr + 1) % 8;
  1171. /* Update Nr received */
  1172. irlap_update_nr_received(self, info->nr);
  1173. self->ack_required = FALSE;
  1174. /* Keep state, do not move this line!*/
  1175. irlap_next_state(self, LAP_NRM_P);
  1176. irlap_data_indication(self, skb, FALSE);
  1177. }
  1178. break;
  1179. }
  1180. /*
  1181. * Unexpected next to send (Ns) and next to receive (Nr)
  1182. * Not documented by IrLAP!
  1183. */
  1184. if ((ns_status == NS_UNEXPECTED) &&
  1185. (nr_status == NR_UNEXPECTED))
  1186. {
  1187. IRDA_DEBUG(4, "%s(), unexpected nr and ns!\n",
  1188. __FUNCTION__);
  1189. if (info->pf) {
  1190. /* Resend rejected frames */
  1191. irlap_resend_rejected_frames(self, CMD_FRAME);
  1192. /* Give peer some time to retransmit!
  1193. * But account for our own Tx. */
  1194. irlap_start_final_timer(self, 2 * self->final_timeout);
  1195. /* Keep state, do not move this line */
  1196. irlap_next_state(self, LAP_NRM_P);
  1197. } else {
  1198. /* Update Nr received */
  1199. /* irlap_update_nr_received( info->nr); */
  1200. self->ack_required = FALSE;
  1201. }
  1202. break;
  1203. }
  1204. /*
  1205. * Invalid NR or NS
  1206. */
  1207. if ((nr_status == NR_INVALID) || (ns_status == NS_INVALID)) {
  1208. if (info->pf) {
  1209. del_timer(&self->final_timer);
  1210. irlap_next_state(self, LAP_RESET_WAIT);
  1211. irlap_disconnect_indication(self, LAP_RESET_INDICATION);
  1212. self->xmitflag = TRUE;
  1213. } else {
  1214. del_timer(&self->final_timer);
  1215. irlap_disconnect_indication(self, LAP_RESET_INDICATION);
  1216. self->xmitflag = FALSE;
  1217. }
  1218. break;
  1219. }
  1220. IRDA_DEBUG(1, "%s(), Not implemented!\n", __FUNCTION__);
  1221. IRDA_DEBUG(1, "%s(), event=%s, ns_status=%d, nr_status=%d\n",
  1222. __FUNCTION__, irlap_event[event], ns_status, nr_status);
  1223. break;
  1224. case RECV_UI_FRAME:
  1225. /* Poll bit cleared? */
  1226. if (!info->pf) {
  1227. irlap_data_indication(self, skb, TRUE);
  1228. irlap_next_state(self, LAP_NRM_P);
  1229. } else {
  1230. del_timer(&self->final_timer);
  1231. irlap_data_indication(self, skb, TRUE);
  1232. irlap_next_state(self, LAP_XMIT_P);
  1233. IRDA_DEBUG(1, "%s: RECV_UI_FRAME: next state %s\n", __FUNCTION__, irlap_state[self->state]);
  1234. irlap_start_poll_timer(self, self->poll_timeout);
  1235. }
  1236. break;
  1237. case RECV_RR_RSP:
  1238. /*
  1239. * If you get a RR, the remote isn't busy anymore,
  1240. * no matter what the NR
  1241. */
  1242. self->remote_busy = FALSE;
  1243. /* Stop final timer */
  1244. del_timer(&self->final_timer);
  1245. /*
  1246. * Nr as expected?
  1247. */
  1248. ret = irlap_validate_nr_received(self, info->nr);
  1249. if (ret == NR_EXPECTED) {
  1250. /* Update Nr received */
  1251. irlap_update_nr_received(self, info->nr);
  1252. /*
  1253. * Got expected NR, so reset the retry_count. This
  1254. * is not done by the IrLAP standard , which is
  1255. * strange! DB.
  1256. */
  1257. self->retry_count = 0;
  1258. irlap_wait_min_turn_around(self, &self->qos_tx);
  1259. irlap_next_state(self, LAP_XMIT_P);
  1260. /* Start poll timer */
  1261. irlap_start_poll_timer(self, self->poll_timeout);
  1262. } else if (ret == NR_UNEXPECTED) {
  1263. IRDA_ASSERT(info != NULL, return -1;);
  1264. /*
  1265. * Unexpected nr!
  1266. */
  1267. /* Update Nr received */
  1268. irlap_update_nr_received(self, info->nr);
  1269. IRDA_DEBUG(4, "RECV_RR_FRAME: Retrans:%d, nr=%d, va=%d, "
  1270. "vs=%d, vr=%d\n",
  1271. self->retry_count, info->nr, self->va,
  1272. self->vs, self->vr);
  1273. /* Resend rejected frames */
  1274. irlap_resend_rejected_frames(self, CMD_FRAME);
  1275. irlap_start_final_timer(self, self->final_timeout * 2);
  1276. irlap_next_state(self, LAP_NRM_P);
  1277. } else if (ret == NR_INVALID) {
  1278. IRDA_DEBUG(1, "%s(), Received RR with "
  1279. "invalid nr !\n", __FUNCTION__);
  1280. irlap_next_state(self, LAP_RESET_WAIT);
  1281. irlap_disconnect_indication(self, LAP_RESET_INDICATION);
  1282. self->xmitflag = TRUE;
  1283. }
  1284. break;
  1285. case RECV_RNR_RSP:
  1286. IRDA_ASSERT(info != NULL, return -1;);
  1287. /* Stop final timer */
  1288. del_timer(&self->final_timer);
  1289. self->remote_busy = TRUE;
  1290. /* Update Nr received */
  1291. irlap_update_nr_received(self, info->nr);
  1292. irlap_next_state(self, LAP_XMIT_P);
  1293. /* Start poll timer */
  1294. irlap_start_poll_timer(self, self->poll_timeout);
  1295. break;
  1296. case RECV_FRMR_RSP:
  1297. del_timer(&self->final_timer);
  1298. self->xmitflag = TRUE;
  1299. irlap_next_state(self, LAP_RESET_WAIT);
  1300. irlap_reset_indication(self);
  1301. break;
  1302. case FINAL_TIMER_EXPIRED:
  1303. /*
  1304. * We are allowed to wait for additional 300 ms if
  1305. * final timer expires when we are in the middle
  1306. * of receiving a frame (page 45, IrLAP). Check that
  1307. * we only do this once for each frame.
  1308. */
  1309. if (irda_device_is_receiving(self->netdev) && !self->add_wait) {
  1310. IRDA_DEBUG(1, "FINAL_TIMER_EXPIRED when receiving a "
  1311. "frame! Waiting a little bit more!\n");
  1312. irlap_start_final_timer(self, msecs_to_jiffies(300));
  1313. /*
  1314. * Don't allow this to happen one more time in a row,
  1315. * or else we can get a pretty tight loop here if
  1316. * if we only receive half a frame. DB.
  1317. */
  1318. self->add_wait = TRUE;
  1319. break;
  1320. }
  1321. self->add_wait = FALSE;
  1322. /* N2 is the disconnect timer. Until we reach it, we retry */
  1323. if (self->retry_count < self->N2) {
  1324. /* Retry sending the pf bit to the secondary */
  1325. irlap_wait_min_turn_around(self, &self->qos_tx);
  1326. irlap_send_rr_frame(self, CMD_FRAME);
  1327. irlap_start_final_timer(self, self->final_timeout);
  1328. self->retry_count++;
  1329. IRDA_DEBUG(4, "irlap_state_nrm_p: FINAL_TIMER_EXPIRED:"
  1330. " retry_count=%d\n", self->retry_count);
  1331. /* Early warning event. I'm using a pretty liberal
  1332. * interpretation of the spec and generate an event
  1333. * every time the timer is multiple of N1 (and not
  1334. * only the first time). This allow application
  1335. * to know precisely if connectivity restart...
  1336. * Jean II */
  1337. if((self->retry_count % self->N1) == 0)
  1338. irlap_status_indication(self,
  1339. STATUS_NO_ACTIVITY);
  1340. /* Keep state */
  1341. } else {
  1342. irlap_apply_default_connection_parameters(self);
  1343. /* Always switch state before calling upper layers */
  1344. irlap_next_state(self, LAP_NDM);
  1345. irlap_disconnect_indication(self, LAP_NO_RESPONSE);
  1346. }
  1347. break;
  1348. case RECV_REJ_RSP:
  1349. irlap_update_nr_received(self, info->nr);
  1350. if (self->remote_busy) {
  1351. irlap_wait_min_turn_around(self, &self->qos_tx);
  1352. irlap_send_rr_frame(self, CMD_FRAME);
  1353. } else
  1354. irlap_resend_rejected_frames(self, CMD_FRAME);
  1355. irlap_start_final_timer(self, 2 * self->final_timeout);
  1356. break;
  1357. case RECV_SREJ_RSP:
  1358. irlap_update_nr_received(self, info->nr);
  1359. if (self->remote_busy) {
  1360. irlap_wait_min_turn_around(self, &self->qos_tx);
  1361. irlap_send_rr_frame(self, CMD_FRAME);
  1362. } else
  1363. irlap_resend_rejected_frame(self, CMD_FRAME);
  1364. irlap_start_final_timer(self, 2 * self->final_timeout);
  1365. break;
  1366. case RECV_RD_RSP:
  1367. IRDA_DEBUG(1, "%s(), RECV_RD_RSP\n", __FUNCTION__);
  1368. irlap_flush_all_queues(self);
  1369. irlap_next_state(self, LAP_XMIT_P);
  1370. /* Call back the LAP state machine to do a proper disconnect */
  1371. irlap_disconnect_request(self);
  1372. break;
  1373. default:
  1374. IRDA_DEBUG(1, "%s(), Unknown event %s\n",
  1375. __FUNCTION__, irlap_event[event]);
  1376. ret = -1;
  1377. break;
  1378. }
  1379. return ret;
  1380. }
  1381. /*
  1382. * Function irlap_state_reset_wait (event, skb, info)
  1383. *
  1384. * We have informed the service user of a reset condition, and is
  1385. * awaiting reset of disconnect request.
  1386. *
  1387. */
  1388. static int irlap_state_reset_wait(struct irlap_cb *self, IRLAP_EVENT event,
  1389. struct sk_buff *skb, struct irlap_info *info)
  1390. {
  1391. int ret = 0;
  1392. IRDA_DEBUG(3, "%s(), event = %s\n", __FUNCTION__, irlap_event[event]);
  1393. IRDA_ASSERT(self != NULL, return -1;);
  1394. IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
  1395. switch (event) {
  1396. case RESET_REQUEST:
  1397. if (self->xmitflag) {
  1398. irlap_wait_min_turn_around(self, &self->qos_tx);
  1399. irlap_send_snrm_frame(self, NULL);
  1400. irlap_start_final_timer(self, self->final_timeout);
  1401. irlap_next_state(self, LAP_RESET);
  1402. } else {
  1403. irlap_start_final_timer(self, self->final_timeout);
  1404. irlap_next_state(self, LAP_RESET);
  1405. }
  1406. break;
  1407. case DISCONNECT_REQUEST:
  1408. irlap_wait_min_turn_around( self, &self->qos_tx);
  1409. irlap_send_disc_frame( self);
  1410. irlap_flush_all_queues( self);
  1411. irlap_start_final_timer( self, self->final_timeout);
  1412. self->retry_count = 0;
  1413. irlap_next_state( self, LAP_PCLOSE);
  1414. break;
  1415. default:
  1416. IRDA_DEBUG(2, "%s(), Unknown event %s\n", __FUNCTION__,
  1417. irlap_event[event]);
  1418. ret = -1;
  1419. break;
  1420. }
  1421. return ret;
  1422. }
  1423. /*
  1424. * Function irlap_state_reset (self, event, skb, info)
  1425. *
  1426. * We have sent a SNRM reset command to the peer layer, and is awaiting
  1427. * reply.
  1428. *
  1429. */
  1430. static int irlap_state_reset(struct irlap_cb *self, IRLAP_EVENT event,
  1431. struct sk_buff *skb, struct irlap_info *info)
  1432. {
  1433. int ret = 0;
  1434. IRDA_DEBUG(3, "%s(), event = %s\n", __FUNCTION__, irlap_event[event]);
  1435. IRDA_ASSERT(self != NULL, return -1;);
  1436. IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
  1437. switch (event) {
  1438. case RECV_DISC_CMD:
  1439. del_timer(&self->final_timer);
  1440. irlap_apply_default_connection_parameters(self);
  1441. /* Always switch state before calling upper layers */
  1442. irlap_next_state(self, LAP_NDM);
  1443. irlap_disconnect_indication(self, LAP_NO_RESPONSE);
  1444. break;
  1445. case RECV_UA_RSP:
  1446. del_timer(&self->final_timer);
  1447. /* Initiate connection state */
  1448. irlap_initiate_connection_state(self);
  1449. irlap_reset_confirm();
  1450. self->remote_busy = FALSE;
  1451. irlap_next_state(self, LAP_XMIT_P);
  1452. irlap_start_poll_timer(self, self->poll_timeout);
  1453. break;
  1454. case FINAL_TIMER_EXPIRED:
  1455. if (self->retry_count < 3) {
  1456. irlap_wait_min_turn_around(self, &self->qos_tx);
  1457. IRDA_ASSERT(self->netdev != NULL, return -1;);
  1458. irlap_send_snrm_frame(self, self->qos_dev);
  1459. self->retry_count++; /* Experimental!! */
  1460. irlap_start_final_timer(self, self->final_timeout);
  1461. irlap_next_state(self, LAP_RESET);
  1462. } else if (self->retry_count >= self->N3) {
  1463. irlap_apply_default_connection_parameters(self);
  1464. /* Always switch state before calling upper layers */
  1465. irlap_next_state(self, LAP_NDM);
  1466. irlap_disconnect_indication(self, LAP_NO_RESPONSE);
  1467. }
  1468. break;
  1469. case RECV_SNRM_CMD:
  1470. /*
  1471. * SNRM frame is not allowed to contain an I-field in this
  1472. * state
  1473. */
  1474. if (!info) {
  1475. IRDA_DEBUG(3, "%s(), RECV_SNRM_CMD\n", __FUNCTION__);
  1476. irlap_initiate_connection_state(self);
  1477. irlap_wait_min_turn_around(self, &self->qos_tx);
  1478. irlap_send_ua_response_frame(self, &self->qos_rx);
  1479. irlap_reset_confirm();
  1480. irlap_start_wd_timer(self, self->wd_timeout);
  1481. irlap_next_state(self, LAP_NDM);
  1482. } else {
  1483. IRDA_DEBUG(0,
  1484. "%s(), SNRM frame contained an I field!\n",
  1485. __FUNCTION__);
  1486. }
  1487. break;
  1488. default:
  1489. IRDA_DEBUG(1, "%s(), Unknown event %s\n",
  1490. __FUNCTION__, irlap_event[event]);
  1491. ret = -1;
  1492. break;
  1493. }
  1494. return ret;
  1495. }
  1496. /*
  1497. * Function irlap_state_xmit_s (event, skb, info)
  1498. *
  1499. * XMIT_S, The secondary station has been given the right to transmit,
  1500. * and we therefor do not expect to receive any transmissions from other
  1501. * stations.
  1502. */
  1503. static int irlap_state_xmit_s(struct irlap_cb *self, IRLAP_EVENT event,
  1504. struct sk_buff *skb, struct irlap_info *info)
  1505. {
  1506. int ret = 0;
  1507. IRDA_DEBUG(4, "%s(), event=%s\n", __FUNCTION__, irlap_event[event]);
  1508. IRDA_ASSERT(self != NULL, return -ENODEV;);
  1509. IRDA_ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
  1510. switch (event) {
  1511. case SEND_I_CMD:
  1512. /*
  1513. * Send frame only if send window > 0
  1514. */
  1515. if ((self->window > 0) && (!self->remote_busy)) {
  1516. int nextfit;
  1517. #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
  1518. struct sk_buff *skb_next;
  1519. /*
  1520. * Same deal as in irlap_state_xmit_p(), so see
  1521. * the comments at that point.
  1522. * We are the secondary, so there are only subtle
  1523. * differences. - Jean II
  1524. */
  1525. /* Check if a subsequent skb exist and would fit in
  1526. * the current window (with respect to turnaround
  1527. * time). - Jean II */
  1528. skb_next = skb_peek(&self->txq);
  1529. nextfit = ((skb_next != NULL) &&
  1530. ((skb_next->len + skb->len) <=
  1531. self->bytes_left));
  1532. /*
  1533. * Test if we have transmitted more bytes over the
  1534. * link than its possible to do with the current
  1535. * speed and turn-around-time.
  1536. */
  1537. if((!nextfit) && (skb->len > self->bytes_left)) {
  1538. IRDA_DEBUG(0, "%s(), Not allowed to transmit"
  1539. " more bytes!\n", __FUNCTION__);
  1540. /* Requeue the skb */
  1541. skb_queue_head(&self->txq, skb_get(skb));
  1542. /*
  1543. * Switch to NRM_S, this is only possible
  1544. * when we are in secondary mode, since we
  1545. * must be sure that we don't miss any RR
  1546. * frames
  1547. */
  1548. self->window = self->window_size;
  1549. self->bytes_left = self->line_capacity;
  1550. irlap_start_wd_timer(self, self->wd_timeout);
  1551. irlap_next_state(self, LAP_NRM_S);
  1552. /* Slight difference with primary :
  1553. * here we would wait for the other side to
  1554. * expire the turnaround. - Jean II */
  1555. return -EPROTO; /* Try again later */
  1556. }
  1557. /* Substract space used by this skb */
  1558. self->bytes_left -= skb->len;
  1559. #else /* CONFIG_IRDA_DYNAMIC_WINDOW */
  1560. /* Window has been adjusted for the max packet
  1561. * size, so much simpler... - Jean II */
  1562. nextfit = !skb_queue_empty(&self->txq);
  1563. #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
  1564. /*
  1565. * Send data with final bit cleared only if window > 1
  1566. * and there is more frames to be sent
  1567. */
  1568. if ((self->window > 1) && (nextfit)) {
  1569. irlap_send_data_secondary(self, skb);
  1570. irlap_next_state(self, LAP_XMIT_S);
  1571. } else {
  1572. irlap_send_data_secondary_final(self, skb);
  1573. irlap_next_state(self, LAP_NRM_S);
  1574. /*
  1575. * Make sure state machine does not try to send
  1576. * any more frames
  1577. */
  1578. ret = -EPROTO;
  1579. }
  1580. } else {
  1581. IRDA_DEBUG(2, "%s(), Unable to send!\n", __FUNCTION__);
  1582. skb_queue_head(&self->txq, skb_get(skb));
  1583. ret = -EPROTO;
  1584. }
  1585. break;
  1586. case DISCONNECT_REQUEST:
  1587. irlap_send_rd_frame(self);
  1588. irlap_flush_all_queues(self);
  1589. irlap_start_wd_timer(self, self->wd_timeout);
  1590. irlap_next_state(self, LAP_SCLOSE);
  1591. break;
  1592. case DATA_REQUEST:
  1593. /* Nothing to do, irlap_do_event() will send the packet
  1594. * when we return... - Jean II */
  1595. break;
  1596. default:
  1597. IRDA_DEBUG(2, "%s(), Unknown event %s\n", __FUNCTION__,
  1598. irlap_event[event]);
  1599. ret = -EINVAL;
  1600. break;
  1601. }
  1602. return ret;
  1603. }
  1604. /*
  1605. * Function irlap_state_nrm_s (event, skb, info)
  1606. *
  1607. * NRM_S (Normal Response Mode as Secondary) state, in this state we are
  1608. * expecting to receive frames from the primary station
  1609. *
  1610. */
  1611. static int irlap_state_nrm_s(struct irlap_cb *self, IRLAP_EVENT event,
  1612. struct sk_buff *skb, struct irlap_info *info)
  1613. {
  1614. int ns_status;
  1615. int nr_status;
  1616. int ret = 0;
  1617. IRDA_DEBUG(4, "%s(), event=%s\n", __FUNCTION__, irlap_event[ event]);
  1618. IRDA_ASSERT(self != NULL, return -1;);
  1619. IRDA_ASSERT(self->magic == LAP_MAGIC, return -1;);
  1620. switch (event) {
  1621. case RECV_I_CMD: /* Optimize for the common case */
  1622. /* FIXME: must check for remote_busy below */
  1623. IRDA_DEBUG(4, "%s(), event=%s nr=%d, vs=%d, ns=%d, "
  1624. "vr=%d, pf=%d\n", __FUNCTION__,
  1625. irlap_event[event], info->nr,
  1626. self->vs, info->ns, self->vr, info->pf);
  1627. self->retry_count = 0;
  1628. ns_status = irlap_validate_ns_received(self, info->ns);
  1629. nr_status = irlap_validate_nr_received(self, info->nr);
  1630. /*
  1631. * Check for expected I(nformation) frame
  1632. */
  1633. if ((ns_status == NS_EXPECTED) && (nr_status == NR_EXPECTED)) {
  1634. /* Update Vr (next frame for us to receive) */
  1635. self->vr = (self->vr + 1) % 8;
  1636. /* Update Nr received */
  1637. irlap_update_nr_received(self, info->nr);
  1638. /*
  1639. * poll bit cleared?
  1640. */
  1641. if (!info->pf) {
  1642. self->ack_required = TRUE;
  1643. /*
  1644. * Starting WD-timer here is optional, but
  1645. * not recommended. Note 6 IrLAP p. 83
  1646. */
  1647. #if 0
  1648. irda_start_timer(WD_TIMER, self->wd_timeout);
  1649. #endif
  1650. /* Keep state, do not move this line */
  1651. irlap_next_state(self, LAP_NRM_S);
  1652. irlap_data_indication(self, skb, FALSE);
  1653. break;
  1654. } else {
  1655. /*
  1656. * We should wait before sending RR, and
  1657. * also before changing to XMIT_S
  1658. * state. (note 1, IrLAP p. 82)
  1659. */
  1660. irlap_wait_min_turn_around(self, &self->qos_tx);
  1661. /*
  1662. * Give higher layers a chance to
  1663. * immediately reply with some data before
  1664. * we decide if we should send a RR frame
  1665. * or not
  1666. */
  1667. irlap_data_indication(self, skb, FALSE);
  1668. /* Any pending data requests? */
  1669. if (!skb_queue_empty(&self->txq) &&
  1670. (self->window > 0))
  1671. {
  1672. self->ack_required = TRUE;
  1673. del_timer(&self->wd_timer);
  1674. irlap_next_state(self, LAP_XMIT_S);
  1675. } else {
  1676. irlap_send_rr_frame(self, RSP_FRAME);
  1677. irlap_start_wd_timer(self,
  1678. self->wd_timeout);
  1679. /* Keep the state */
  1680. irlap_next_state(self, LAP_NRM_S);
  1681. }
  1682. break;
  1683. }
  1684. }
  1685. /*
  1686. * Check for Unexpected next to send (Ns)
  1687. */
  1688. if ((ns_status == NS_UNEXPECTED) && (nr_status == NR_EXPECTED))
  1689. {
  1690. /* Unexpected next to send, with final bit cleared */
  1691. if (!info->pf) {
  1692. irlap_update_nr_received(self, info->nr);
  1693. irlap_start_wd_timer(self, self->wd_timeout);
  1694. } else {
  1695. /* Update Nr received */
  1696. irlap_update_nr_received(self, info->nr);
  1697. irlap_wait_min_turn_around(self, &self->qos_tx);
  1698. irlap_send_rr_frame(self, RSP_FRAME);
  1699. irlap_start_wd_timer(self, self->wd_timeout);
  1700. }
  1701. break;
  1702. }
  1703. /*
  1704. * Unexpected Next to Receive(NR) ?
  1705. */
  1706. if ((ns_status == NS_EXPECTED) && (nr_status == NR_UNEXPECTED))
  1707. {
  1708. if (info->pf) {
  1709. IRDA_DEBUG(4, "RECV_I_RSP: frame(s) lost\n");
  1710. self->vr = (self->vr + 1) % 8;
  1711. /* Update Nr received */
  1712. irlap_update_nr_received(self, info->nr);
  1713. /* Resend rejected frames */
  1714. irlap_resend_rejected_frames(self, RSP_FRAME);
  1715. /* Keep state, do not move this line */
  1716. irlap_next_state(self, LAP_NRM_S);
  1717. irlap_data_indication(self, skb, FALSE);
  1718. irlap_start_wd_timer(self, self->wd_timeout);
  1719. break;
  1720. }
  1721. /*
  1722. * This is not documented in IrLAP!! Unexpected NR
  1723. * with poll bit cleared
  1724. */
  1725. if (!info->pf) {
  1726. self->vr = (self->vr + 1) % 8;
  1727. /* Update Nr received */
  1728. irlap_update_nr_received(self, info->nr);
  1729. /* Keep state, do not move this line */
  1730. irlap_next_state(self, LAP_NRM_S);
  1731. irlap_data_indication(self, skb, FALSE);
  1732. irlap_start_wd_timer(self, self->wd_timeout);
  1733. }
  1734. break;
  1735. }
  1736. if (ret == NR_INVALID) {
  1737. IRDA_DEBUG(0, "NRM_S, NR_INVALID not implemented!\n");
  1738. }
  1739. if (ret == NS_INVALID) {
  1740. IRDA_DEBUG(0, "NRM_S, NS_INVALID not implemented!\n");
  1741. }
  1742. break;
  1743. case RECV_UI_FRAME:
  1744. /*
  1745. * poll bit cleared?
  1746. */
  1747. if (!info->pf) {
  1748. irlap_data_indication(self, skb, TRUE);
  1749. irlap_next_state(self, LAP_NRM_S); /* Keep state */
  1750. } else {
  1751. /*
  1752. * Any pending data requests?
  1753. */
  1754. if (!skb_queue_empty(&self->txq) &&
  1755. (self->window > 0) && !self->remote_busy)
  1756. {
  1757. irlap_data_indication(self, skb, TRUE);
  1758. del_timer(&self->wd_timer);
  1759. irlap_next_state(self, LAP_XMIT_S);
  1760. } else {
  1761. irlap_data_indication(self, skb, TRUE);
  1762. irlap_wait_min_turn_around(self, &self->qos_tx);
  1763. irlap_send_rr_frame(self, RSP_FRAME);
  1764. self->ack_required = FALSE;
  1765. irlap_start_wd_timer(self, self->wd_timeout);
  1766. /* Keep the state */
  1767. irlap_next_state(self, LAP_NRM_S);
  1768. }
  1769. }
  1770. break;
  1771. case RECV_RR_CMD:
  1772. self->retry_count = 0;
  1773. /*
  1774. * Nr as expected?
  1775. */
  1776. nr_status = irlap_validate_nr_received(self, info->nr);
  1777. if (nr_status == NR_EXPECTED) {
  1778. if (!skb_queue_empty(&self->txq) &&
  1779. (self->window > 0)) {
  1780. self->remote_busy = FALSE;
  1781. /* Update Nr received */
  1782. irlap_update_nr_received(self, info->nr);
  1783. del_timer(&self->wd_timer);
  1784. irlap_wait_min_turn_around(self, &self->qos_tx);
  1785. irlap_next_state(self, LAP_XMIT_S);
  1786. } else {
  1787. self->remote_busy = FALSE;
  1788. /* Update Nr received */
  1789. irlap_update_nr_received(self, info->nr);
  1790. irlap_wait_min_turn_around(self, &self->qos_tx);
  1791. irlap_start_wd_timer(self, self->wd_timeout);
  1792. /* Note : if the link is idle (this case),
  1793. * we never go in XMIT_S, so we never get a
  1794. * chance to process any DISCONNECT_REQUEST.
  1795. * Do it now ! - Jean II */
  1796. if (self->disconnect_pending) {
  1797. /* Disconnect */
  1798. irlap_send_rd_frame(self);
  1799. irlap_flush_all_queues(self);
  1800. irlap_next_state(self, LAP_SCLOSE);
  1801. } else {
  1802. /* Just send back pf bit */
  1803. irlap_send_rr_frame(self, RSP_FRAME);
  1804. irlap_next_state(self, LAP_NRM_S);
  1805. }
  1806. }
  1807. } else if (nr_status == NR_UNEXPECTED) {
  1808. self->remote_busy = FALSE;
  1809. irlap_update_nr_received(self, info->nr);
  1810. irlap_resend_rejected_frames(self, RSP_FRAME);
  1811. irlap_start_wd_timer(self, self->wd_timeout);
  1812. /* Keep state */
  1813. irlap_next_state(self, LAP_NRM_S);
  1814. } else {
  1815. IRDA_DEBUG(1, "%s(), invalid nr not implemented!\n",
  1816. __FUNCTION__);
  1817. }
  1818. break;
  1819. case RECV_SNRM_CMD:
  1820. /* SNRM frame is not allowed to contain an I-field */
  1821. if (!info) {
  1822. del_timer(&self->wd_timer);
  1823. IRDA_DEBUG(1, "%s(), received SNRM cmd\n", __FUNCTION__);
  1824. irlap_next_state(self, LAP_RESET_CHECK);
  1825. irlap_reset_indication(self);
  1826. } else {
  1827. IRDA_DEBUG(0,
  1828. "%s(), SNRM frame contained an I-field!\n",
  1829. __FUNCTION__);
  1830. }
  1831. break;
  1832. case RECV_REJ_CMD:
  1833. irlap_update_nr_received(self, info->nr);
  1834. if (self->remote_busy) {
  1835. irlap_wait_min_turn_around(self, &self->qos_tx);
  1836. irlap_send_rr_frame(self, RSP_FRAME);
  1837. } else
  1838. irlap_resend_rejected_frames(self, RSP_FRAME);
  1839. irlap_start_wd_timer(self, self->wd_timeout);
  1840. break;
  1841. case RECV_SREJ_CMD:
  1842. irlap_update_nr_received(self, info->nr);
  1843. if (self->remote_busy) {
  1844. irlap_wait_min_turn_around(self, &self->qos_tx);
  1845. irlap_send_rr_frame(self, RSP_FRAME);
  1846. } else
  1847. irlap_resend_rejected_frame(self, RSP_FRAME);
  1848. irlap_start_wd_timer(self, self->wd_timeout);
  1849. break;
  1850. case WD_TIMER_EXPIRED:
  1851. /*
  1852. * Wait until retry_count * n matches negotiated threshold/
  1853. * disconnect time (note 2 in IrLAP p. 82)
  1854. *
  1855. * Similar to irlap_state_nrm_p() -> FINAL_TIMER_EXPIRED
  1856. * Note : self->wd_timeout = (self->final_timeout * 2),
  1857. * which explain why we use (self->N2 / 2) here !!!
  1858. * Jean II
  1859. */
  1860. IRDA_DEBUG(1, "%s(), retry_count = %d\n", __FUNCTION__,
  1861. self->retry_count);
  1862. if (self->retry_count < (self->N2 / 2)) {
  1863. /* No retry, just wait for primary */
  1864. irlap_start_wd_timer(self, self->wd_timeout);
  1865. self->retry_count++;
  1866. if((self->retry_count % (self->N1 / 2)) == 0)
  1867. irlap_status_indication(self,
  1868. STATUS_NO_ACTIVITY);
  1869. } else {
  1870. irlap_apply_default_connection_parameters(self);
  1871. /* Always switch state before calling upper layers */
  1872. irlap_next_state(self, LAP_NDM);
  1873. irlap_disconnect_indication(self, LAP_NO_RESPONSE);
  1874. }
  1875. break;
  1876. case RECV_DISC_CMD:
  1877. /* Always switch state before calling upper layers */
  1878. irlap_next_state(self, LAP_NDM);
  1879. /* Send disconnect response */
  1880. irlap_wait_min_turn_around(self, &self->qos_tx);
  1881. irlap_send_ua_response_frame(self, NULL);
  1882. del_timer(&self->wd_timer);
  1883. irlap_flush_all_queues(self);
  1884. /* Set default link parameters */
  1885. irlap_apply_default_connection_parameters(self);
  1886. irlap_disconnect_indication(self, LAP_DISC_INDICATION);
  1887. break;
  1888. case RECV_DISCOVERY_XID_CMD:
  1889. irlap_wait_min_turn_around(self, &self->qos_tx);
  1890. irlap_send_rr_frame(self, RSP_FRAME);
  1891. self->ack_required = TRUE;
  1892. irlap_start_wd_timer(self, self->wd_timeout);
  1893. irlap_next_state(self, LAP_NRM_S);
  1894. break;
  1895. case RECV_TEST_CMD:
  1896. /* Remove test frame header (only LAP header in NRM) */
  1897. skb_pull(skb, LAP_ADDR_HEADER + LAP_CTRL_HEADER);
  1898. irlap_wait_min_turn_around(self, &self->qos_tx);
  1899. irlap_start_wd_timer(self, self->wd_timeout);
  1900. /* Send response (info will be copied) */
  1901. irlap_send_test_frame(self, self->caddr, info->daddr, skb);
  1902. break;
  1903. default:
  1904. IRDA_DEBUG(1, "%s(), Unknown event %d, (%s)\n", __FUNCTION__,
  1905. event, irlap_event[event]);
  1906. ret = -EINVAL;
  1907. break;
  1908. }
  1909. return ret;
  1910. }
  1911. /*
  1912. * Function irlap_state_sclose (self, event, skb, info)
  1913. */
  1914. static int irlap_state_sclose(struct irlap_cb *self, IRLAP_EVENT event,
  1915. struct sk_buff *skb, struct irlap_info *info)
  1916. {
  1917. int ret = 0;
  1918. IRDA_DEBUG(1, "%s()\n", __FUNCTION__);
  1919. IRDA_ASSERT(self != NULL, return -ENODEV;);
  1920. IRDA_ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
  1921. switch (event) {
  1922. case RECV_DISC_CMD:
  1923. /* Always switch state before calling upper layers */
  1924. irlap_next_state(self, LAP_NDM);
  1925. /* Send disconnect response */
  1926. irlap_wait_min_turn_around(self, &self->qos_tx);
  1927. irlap_send_ua_response_frame(self, NULL);
  1928. del_timer(&self->wd_timer);
  1929. /* Set default link parameters */
  1930. irlap_apply_default_connection_parameters(self);
  1931. irlap_disconnect_indication(self, LAP_DISC_INDICATION);
  1932. break;
  1933. case RECV_DM_RSP:
  1934. /* IrLAP-1.1 p.82: in SCLOSE, S and I type RSP frames
  1935. * shall take us down into default NDM state, like DM_RSP
  1936. */
  1937. case RECV_RR_RSP:
  1938. case RECV_RNR_RSP:
  1939. case RECV_REJ_RSP:
  1940. case RECV_SREJ_RSP:
  1941. case RECV_I_RSP:
  1942. /* Always switch state before calling upper layers */
  1943. irlap_next_state(self, LAP_NDM);
  1944. del_timer(&self->wd_timer);
  1945. irlap_apply_default_connection_parameters(self);
  1946. irlap_disconnect_indication(self, LAP_DISC_INDICATION);
  1947. break;
  1948. case WD_TIMER_EXPIRED:
  1949. /* Always switch state before calling upper layers */
  1950. irlap_next_state(self, LAP_NDM);
  1951. irlap_apply_default_connection_parameters(self);
  1952. irlap_disconnect_indication(self, LAP_DISC_INDICATION);
  1953. break;
  1954. default:
  1955. /* IrLAP-1.1 p.82: in SCLOSE, basically any received frame
  1956. * with pf=1 shall restart the wd-timer and resend the rd:rsp
  1957. */
  1958. if (info != NULL && info->pf) {
  1959. del_timer(&self->wd_timer);
  1960. irlap_wait_min_turn_around(self, &self->qos_tx);
  1961. irlap_send_rd_frame(self);
  1962. irlap_start_wd_timer(self, self->wd_timeout);
  1963. break; /* stay in SCLOSE */
  1964. }
  1965. IRDA_DEBUG(1, "%s(), Unknown event %d, (%s)\n", __FUNCTION__,
  1966. event, irlap_event[event]);
  1967. ret = -EINVAL;
  1968. break;
  1969. }
  1970. return -1;
  1971. }
  1972. static int irlap_state_reset_check( struct irlap_cb *self, IRLAP_EVENT event,
  1973. struct sk_buff *skb,
  1974. struct irlap_info *info)
  1975. {
  1976. int ret = 0;
  1977. IRDA_DEBUG(1, "%s(), event=%s\n", __FUNCTION__, irlap_event[event]);
  1978. IRDA_ASSERT(self != NULL, return -ENODEV;);
  1979. IRDA_ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
  1980. switch (event) {
  1981. case RESET_RESPONSE:
  1982. irlap_send_ua_response_frame(self, &self->qos_rx);
  1983. irlap_initiate_connection_state(self);
  1984. irlap_start_wd_timer(self, WD_TIMEOUT);
  1985. irlap_flush_all_queues(self);
  1986. irlap_next_state(self, LAP_NRM_S);
  1987. break;
  1988. case DISCONNECT_REQUEST:
  1989. irlap_wait_min_turn_around(self, &self->qos_tx);
  1990. irlap_send_rd_frame(self);
  1991. irlap_start_wd_timer(self, WD_TIMEOUT);
  1992. irlap_next_state(self, LAP_SCLOSE);
  1993. break;
  1994. default:
  1995. IRDA_DEBUG(1, "%s(), Unknown event %d, (%s)\n", __FUNCTION__,
  1996. event, irlap_event[event]);
  1997. ret = -EINVAL;
  1998. break;
  1999. }
  2000. return ret;
  2001. }