irlap_event.c 65 KB

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