irlap.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  1. /*********************************************************************
  2. *
  3. * Filename: irlap.c
  4. * Version: 1.0
  5. * Description: IrLAP implementation for Linux
  6. * Status: Stable
  7. * Author: Dag Brattli <dagb@cs.uit.no>
  8. * Created at: Mon Aug 4 20:40:53 1997
  9. * Modified at: Tue Dec 14 09:26:44 1999
  10. * Modified by: Dag Brattli <dagb@cs.uit.no>
  11. *
  12. * Copyright (c) 1998-1999 Dag Brattli, All Rights Reserved.
  13. * Copyright (c) 2000-2003 Jean Tourrilhes <jt@hpl.hp.com>
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License as
  17. * published by the Free Software Foundation; either version 2 of
  18. * the License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  27. *
  28. ********************************************************************/
  29. #include <linux/slab.h>
  30. #include <linux/string.h>
  31. #include <linux/skbuff.h>
  32. #include <linux/delay.h>
  33. #include <linux/proc_fs.h>
  34. #include <linux/init.h>
  35. #include <linux/random.h>
  36. #include <linux/module.h>
  37. #include <linux/seq_file.h>
  38. #include <net/irda/irda.h>
  39. #include <net/irda/irda_device.h>
  40. #include <net/irda/irqueue.h>
  41. #include <net/irda/irlmp.h>
  42. #include <net/irda/irlmp_frame.h>
  43. #include <net/irda/irlap_frame.h>
  44. #include <net/irda/irlap.h>
  45. #include <net/irda/timer.h>
  46. #include <net/irda/qos.h>
  47. static hashbin_t *irlap = NULL;
  48. int sysctl_slot_timeout = SLOT_TIMEOUT * 1000 / HZ;
  49. /* This is the delay of missed pf period before generating an event
  50. * to the application. The spec mandate 3 seconds, but in some cases
  51. * it's way too long. - Jean II */
  52. int sysctl_warn_noreply_time = 3;
  53. extern void irlap_queue_xmit(struct irlap_cb *self, struct sk_buff *skb);
  54. static void __irlap_close(struct irlap_cb *self);
  55. static void irlap_init_qos_capabilities(struct irlap_cb *self,
  56. struct qos_info *qos_user);
  57. static const char *const lap_reasons[] __maybe_unused = {
  58. "ERROR, NOT USED",
  59. "LAP_DISC_INDICATION",
  60. "LAP_NO_RESPONSE",
  61. "LAP_RESET_INDICATION",
  62. "LAP_FOUND_NONE",
  63. "LAP_MEDIA_BUSY",
  64. "LAP_PRIMARY_CONFLICT",
  65. "ERROR, NOT USED",
  66. };
  67. int __init irlap_init(void)
  68. {
  69. /* Check if the compiler did its job properly.
  70. * May happen on some ARM configuration, check with Russell King. */
  71. IRDA_ASSERT(sizeof(struct xid_frame) == 14, ;);
  72. IRDA_ASSERT(sizeof(struct test_frame) == 10, ;);
  73. IRDA_ASSERT(sizeof(struct ua_frame) == 10, ;);
  74. IRDA_ASSERT(sizeof(struct snrm_frame) == 11, ;);
  75. /* Allocate master array */
  76. irlap = hashbin_new(HB_LOCK);
  77. if (irlap == NULL) {
  78. net_err_ratelimited("%s: can't allocate irlap hashbin!\n",
  79. __func__);
  80. return -ENOMEM;
  81. }
  82. return 0;
  83. }
  84. void irlap_cleanup(void)
  85. {
  86. IRDA_ASSERT(irlap != NULL, return;);
  87. hashbin_delete(irlap, (FREE_FUNC) __irlap_close);
  88. }
  89. /*
  90. * Function irlap_open (driver)
  91. *
  92. * Initialize IrLAP layer
  93. *
  94. */
  95. struct irlap_cb *irlap_open(struct net_device *dev, struct qos_info *qos,
  96. const char *hw_name)
  97. {
  98. struct irlap_cb *self;
  99. /* Initialize the irlap structure. */
  100. self = kzalloc(sizeof(struct irlap_cb), GFP_KERNEL);
  101. if (self == NULL)
  102. return NULL;
  103. self->magic = LAP_MAGIC;
  104. /* Make a binding between the layers */
  105. self->netdev = dev;
  106. self->qos_dev = qos;
  107. /* Copy hardware name */
  108. if(hw_name != NULL) {
  109. strlcpy(self->hw_name, hw_name, sizeof(self->hw_name));
  110. } else {
  111. self->hw_name[0] = '\0';
  112. }
  113. /* FIXME: should we get our own field? */
  114. dev->atalk_ptr = self;
  115. self->state = LAP_OFFLINE;
  116. /* Initialize transmit queue */
  117. skb_queue_head_init(&self->txq);
  118. skb_queue_head_init(&self->txq_ultra);
  119. skb_queue_head_init(&self->wx_list);
  120. /* My unique IrLAP device address! */
  121. /* We don't want the broadcast address, neither the NULL address
  122. * (most often used to signify "invalid"), and we don't want an
  123. * address already in use (otherwise connect won't be able
  124. * to select the proper link). - Jean II */
  125. do {
  126. get_random_bytes(&self->saddr, sizeof(self->saddr));
  127. } while ((self->saddr == 0x0) || (self->saddr == BROADCAST) ||
  128. (hashbin_lock_find(irlap, self->saddr, NULL)) );
  129. /* Copy to the driver */
  130. memcpy(dev->dev_addr, &self->saddr, 4);
  131. init_timer(&self->slot_timer);
  132. init_timer(&self->query_timer);
  133. init_timer(&self->discovery_timer);
  134. init_timer(&self->final_timer);
  135. init_timer(&self->poll_timer);
  136. init_timer(&self->wd_timer);
  137. init_timer(&self->backoff_timer);
  138. init_timer(&self->media_busy_timer);
  139. irlap_apply_default_connection_parameters(self);
  140. self->N3 = 3; /* # connections attempts to try before giving up */
  141. self->state = LAP_NDM;
  142. hashbin_insert(irlap, (irda_queue_t *) self, self->saddr, NULL);
  143. irlmp_register_link(self, self->saddr, &self->notify);
  144. return self;
  145. }
  146. EXPORT_SYMBOL(irlap_open);
  147. /*
  148. * Function __irlap_close (self)
  149. *
  150. * Remove IrLAP and all allocated memory. Stop any pending timers.
  151. *
  152. */
  153. static void __irlap_close(struct irlap_cb *self)
  154. {
  155. IRDA_ASSERT(self != NULL, return;);
  156. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  157. /* Stop timers */
  158. del_timer(&self->slot_timer);
  159. del_timer(&self->query_timer);
  160. del_timer(&self->discovery_timer);
  161. del_timer(&self->final_timer);
  162. del_timer(&self->poll_timer);
  163. del_timer(&self->wd_timer);
  164. del_timer(&self->backoff_timer);
  165. del_timer(&self->media_busy_timer);
  166. irlap_flush_all_queues(self);
  167. self->magic = 0;
  168. kfree(self);
  169. }
  170. /*
  171. * Function irlap_close (self)
  172. *
  173. * Remove IrLAP instance
  174. *
  175. */
  176. void irlap_close(struct irlap_cb *self)
  177. {
  178. struct irlap_cb *lap;
  179. IRDA_ASSERT(self != NULL, return;);
  180. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  181. /* We used to send a LAP_DISC_INDICATION here, but this was
  182. * racy. This has been move within irlmp_unregister_link()
  183. * itself. Jean II */
  184. /* Kill the LAP and all LSAPs on top of it */
  185. irlmp_unregister_link(self->saddr);
  186. self->notify.instance = NULL;
  187. /* Be sure that we manage to remove ourself from the hash */
  188. lap = hashbin_remove(irlap, self->saddr, NULL);
  189. if (!lap) {
  190. pr_debug("%s(), Didn't find myself!\n", __func__);
  191. return;
  192. }
  193. __irlap_close(lap);
  194. }
  195. EXPORT_SYMBOL(irlap_close);
  196. /*
  197. * Function irlap_connect_indication (self, skb)
  198. *
  199. * Another device is attempting to make a connection
  200. *
  201. */
  202. void irlap_connect_indication(struct irlap_cb *self, struct sk_buff *skb)
  203. {
  204. IRDA_ASSERT(self != NULL, return;);
  205. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  206. irlap_init_qos_capabilities(self, NULL); /* No user QoS! */
  207. irlmp_link_connect_indication(self->notify.instance, self->saddr,
  208. self->daddr, &self->qos_tx, skb);
  209. }
  210. /*
  211. * Function irlap_connect_response (self, skb)
  212. *
  213. * Service user has accepted incoming connection
  214. *
  215. */
  216. void irlap_connect_response(struct irlap_cb *self, struct sk_buff *userdata)
  217. {
  218. irlap_do_event(self, CONNECT_RESPONSE, userdata, NULL);
  219. }
  220. /*
  221. * Function irlap_connect_request (self, daddr, qos_user, sniff)
  222. *
  223. * Request connection with another device, sniffing is not implemented
  224. * yet.
  225. *
  226. */
  227. void irlap_connect_request(struct irlap_cb *self, __u32 daddr,
  228. struct qos_info *qos_user, int sniff)
  229. {
  230. pr_debug("%s(), daddr=0x%08x\n", __func__, daddr);
  231. IRDA_ASSERT(self != NULL, return;);
  232. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  233. self->daddr = daddr;
  234. /*
  235. * If the service user specifies QoS values for this connection,
  236. * then use them
  237. */
  238. irlap_init_qos_capabilities(self, qos_user);
  239. if ((self->state == LAP_NDM) && !self->media_busy)
  240. irlap_do_event(self, CONNECT_REQUEST, NULL, NULL);
  241. else
  242. self->connect_pending = TRUE;
  243. }
  244. /*
  245. * Function irlap_connect_confirm (self, skb)
  246. *
  247. * Connection request has been accepted
  248. *
  249. */
  250. void irlap_connect_confirm(struct irlap_cb *self, struct sk_buff *skb)
  251. {
  252. IRDA_ASSERT(self != NULL, return;);
  253. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  254. irlmp_link_connect_confirm(self->notify.instance, &self->qos_tx, skb);
  255. }
  256. /*
  257. * Function irlap_data_indication (self, skb)
  258. *
  259. * Received data frames from IR-port, so we just pass them up to
  260. * IrLMP for further processing
  261. *
  262. */
  263. void irlap_data_indication(struct irlap_cb *self, struct sk_buff *skb,
  264. int unreliable)
  265. {
  266. /* Hide LAP header from IrLMP layer */
  267. skb_pull(skb, LAP_ADDR_HEADER+LAP_CTRL_HEADER);
  268. irlmp_link_data_indication(self->notify.instance, skb, unreliable);
  269. }
  270. /*
  271. * Function irlap_data_request (self, skb)
  272. *
  273. * Queue data for transmission, must wait until XMIT state
  274. *
  275. */
  276. void irlap_data_request(struct irlap_cb *self, struct sk_buff *skb,
  277. int unreliable)
  278. {
  279. IRDA_ASSERT(self != NULL, return;);
  280. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  281. IRDA_ASSERT(skb_headroom(skb) >= (LAP_ADDR_HEADER+LAP_CTRL_HEADER),
  282. return;);
  283. skb_push(skb, LAP_ADDR_HEADER+LAP_CTRL_HEADER);
  284. /*
  285. * Must set frame format now so that the rest of the code knows
  286. * if its dealing with an I or an UI frame
  287. */
  288. if (unreliable)
  289. skb->data[1] = UI_FRAME;
  290. else
  291. skb->data[1] = I_FRAME;
  292. /* Don't forget to refcount it - see irlmp_connect_request(). */
  293. skb_get(skb);
  294. /* Add at the end of the queue (keep ordering) - Jean II */
  295. skb_queue_tail(&self->txq, skb);
  296. /*
  297. * Send event if this frame only if we are in the right state
  298. * FIXME: udata should be sent first! (skb_queue_head?)
  299. */
  300. if ((self->state == LAP_XMIT_P) || (self->state == LAP_XMIT_S)) {
  301. /* If we are not already processing the Tx queue, trigger
  302. * transmission immediately - Jean II */
  303. if((skb_queue_len(&self->txq) <= 1) && (!self->local_busy))
  304. irlap_do_event(self, DATA_REQUEST, skb, NULL);
  305. /* Otherwise, the packets will be sent normally at the
  306. * next pf-poll - Jean II */
  307. }
  308. }
  309. /*
  310. * Function irlap_unitdata_request (self, skb)
  311. *
  312. * Send Ultra data. This is data that must be sent outside any connection
  313. *
  314. */
  315. #ifdef CONFIG_IRDA_ULTRA
  316. void irlap_unitdata_request(struct irlap_cb *self, struct sk_buff *skb)
  317. {
  318. IRDA_ASSERT(self != NULL, return;);
  319. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  320. IRDA_ASSERT(skb_headroom(skb) >= (LAP_ADDR_HEADER+LAP_CTRL_HEADER),
  321. return;);
  322. skb_push(skb, LAP_ADDR_HEADER+LAP_CTRL_HEADER);
  323. skb->data[0] = CBROADCAST;
  324. skb->data[1] = UI_FRAME;
  325. /* Don't need to refcount, see irlmp_connless_data_request() */
  326. skb_queue_tail(&self->txq_ultra, skb);
  327. irlap_do_event(self, SEND_UI_FRAME, NULL, NULL);
  328. }
  329. #endif /*CONFIG_IRDA_ULTRA */
  330. /*
  331. * Function irlap_udata_indication (self, skb)
  332. *
  333. * Receive Ultra data. This is data that is received outside any connection
  334. *
  335. */
  336. #ifdef CONFIG_IRDA_ULTRA
  337. void irlap_unitdata_indication(struct irlap_cb *self, struct sk_buff *skb)
  338. {
  339. IRDA_ASSERT(self != NULL, return;);
  340. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  341. IRDA_ASSERT(skb != NULL, return;);
  342. /* Hide LAP header from IrLMP layer */
  343. skb_pull(skb, LAP_ADDR_HEADER+LAP_CTRL_HEADER);
  344. irlmp_link_unitdata_indication(self->notify.instance, skb);
  345. }
  346. #endif /* CONFIG_IRDA_ULTRA */
  347. /*
  348. * Function irlap_disconnect_request (void)
  349. *
  350. * Request to disconnect connection by service user
  351. */
  352. void irlap_disconnect_request(struct irlap_cb *self)
  353. {
  354. IRDA_ASSERT(self != NULL, return;);
  355. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  356. /* Don't disconnect until all data frames are successfully sent */
  357. if (!skb_queue_empty(&self->txq)) {
  358. self->disconnect_pending = TRUE;
  359. return;
  360. }
  361. /* Check if we are in the right state for disconnecting */
  362. switch (self->state) {
  363. case LAP_XMIT_P: /* FALLTHROUGH */
  364. case LAP_XMIT_S: /* FALLTHROUGH */
  365. case LAP_CONN: /* FALLTHROUGH */
  366. case LAP_RESET_WAIT: /* FALLTHROUGH */
  367. case LAP_RESET_CHECK:
  368. irlap_do_event(self, DISCONNECT_REQUEST, NULL, NULL);
  369. break;
  370. default:
  371. pr_debug("%s(), disconnect pending!\n", __func__);
  372. self->disconnect_pending = TRUE;
  373. break;
  374. }
  375. }
  376. /*
  377. * Function irlap_disconnect_indication (void)
  378. *
  379. * Disconnect request from other device
  380. *
  381. */
  382. void irlap_disconnect_indication(struct irlap_cb *self, LAP_REASON reason)
  383. {
  384. pr_debug("%s(), reason=%s\n", __func__, lap_reasons[reason]);
  385. IRDA_ASSERT(self != NULL, return;);
  386. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  387. /* Flush queues */
  388. irlap_flush_all_queues(self);
  389. switch (reason) {
  390. case LAP_RESET_INDICATION:
  391. pr_debug("%s(), Sending reset request!\n", __func__);
  392. irlap_do_event(self, RESET_REQUEST, NULL, NULL);
  393. break;
  394. case LAP_NO_RESPONSE: /* FALLTHROUGH */
  395. case LAP_DISC_INDICATION: /* FALLTHROUGH */
  396. case LAP_FOUND_NONE: /* FALLTHROUGH */
  397. case LAP_MEDIA_BUSY:
  398. irlmp_link_disconnect_indication(self->notify.instance, self,
  399. reason, NULL);
  400. break;
  401. default:
  402. net_err_ratelimited("%s: Unknown reason %d\n",
  403. __func__, reason);
  404. }
  405. }
  406. /*
  407. * Function irlap_discovery_request (gen_addr_bit)
  408. *
  409. * Start one single discovery operation.
  410. *
  411. */
  412. void irlap_discovery_request(struct irlap_cb *self, discovery_t *discovery)
  413. {
  414. struct irlap_info info;
  415. IRDA_ASSERT(self != NULL, return;);
  416. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  417. IRDA_ASSERT(discovery != NULL, return;);
  418. pr_debug("%s(), nslots = %d\n", __func__, discovery->nslots);
  419. IRDA_ASSERT((discovery->nslots == 1) || (discovery->nslots == 6) ||
  420. (discovery->nslots == 8) || (discovery->nslots == 16),
  421. return;);
  422. /* Discovery is only possible in NDM mode */
  423. if (self->state != LAP_NDM) {
  424. pr_debug("%s(), discovery only possible in NDM mode\n",
  425. __func__);
  426. irlap_discovery_confirm(self, NULL);
  427. /* Note : in theory, if we are not in NDM, we could postpone
  428. * the discovery like we do for connection request.
  429. * In practice, it's not worth it. If the media was busy,
  430. * it's likely next time around it won't be busy. If we are
  431. * in REPLY state, we will get passive discovery info & event.
  432. * Jean II */
  433. return;
  434. }
  435. /* Check if last discovery request finished in time, or if
  436. * it was aborted due to the media busy flag. */
  437. if (self->discovery_log != NULL) {
  438. hashbin_delete(self->discovery_log, (FREE_FUNC) kfree);
  439. self->discovery_log = NULL;
  440. }
  441. /* All operations will occur at predictable time, no need to lock */
  442. self->discovery_log = hashbin_new(HB_NOLOCK);
  443. if (self->discovery_log == NULL) {
  444. net_warn_ratelimited("%s(), Unable to allocate discovery log!\n",
  445. __func__);
  446. return;
  447. }
  448. info.S = discovery->nslots; /* Number of slots */
  449. info.s = 0; /* Current slot */
  450. self->discovery_cmd = discovery;
  451. info.discovery = discovery;
  452. /* sysctl_slot_timeout bounds are checked in irsysctl.c - Jean II */
  453. self->slot_timeout = msecs_to_jiffies(sysctl_slot_timeout);
  454. irlap_do_event(self, DISCOVERY_REQUEST, NULL, &info);
  455. }
  456. /*
  457. * Function irlap_discovery_confirm (log)
  458. *
  459. * A device has been discovered in front of this station, we
  460. * report directly to LMP.
  461. */
  462. void irlap_discovery_confirm(struct irlap_cb *self, hashbin_t *discovery_log)
  463. {
  464. IRDA_ASSERT(self != NULL, return;);
  465. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  466. IRDA_ASSERT(self->notify.instance != NULL, return;);
  467. /*
  468. * Check for successful discovery, since we are then allowed to clear
  469. * the media busy condition (IrLAP 6.13.4 - p.94). This should allow
  470. * us to make connection attempts much faster and easier (i.e. no
  471. * collisions).
  472. * Setting media busy to false will also generate an event allowing
  473. * to process pending events in NDM state machine.
  474. * Note : the spec doesn't define what's a successful discovery is.
  475. * If we want Ultra to work, it's successful even if there is
  476. * nobody discovered - Jean II
  477. */
  478. if (discovery_log)
  479. irda_device_set_media_busy(self->netdev, FALSE);
  480. /* Inform IrLMP */
  481. irlmp_link_discovery_confirm(self->notify.instance, discovery_log);
  482. }
  483. /*
  484. * Function irlap_discovery_indication (log)
  485. *
  486. * Somebody is trying to discover us!
  487. *
  488. */
  489. void irlap_discovery_indication(struct irlap_cb *self, discovery_t *discovery)
  490. {
  491. IRDA_ASSERT(self != NULL, return;);
  492. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  493. IRDA_ASSERT(discovery != NULL, return;);
  494. IRDA_ASSERT(self->notify.instance != NULL, return;);
  495. /* A device is very likely to connect immediately after it performs
  496. * a successful discovery. This means that in our case, we are much
  497. * more likely to receive a connection request over the medium.
  498. * So, we backoff to avoid collisions.
  499. * IrLAP spec 6.13.4 suggest 100ms...
  500. * Note : this little trick actually make a *BIG* difference. If I set
  501. * my Linux box with discovery enabled and one Ultra frame sent every
  502. * second, my Palm has no trouble connecting to it every time !
  503. * Jean II */
  504. irda_device_set_media_busy(self->netdev, SMALL);
  505. irlmp_link_discovery_indication(self->notify.instance, discovery);
  506. }
  507. /*
  508. * Function irlap_status_indication (quality_of_link)
  509. */
  510. void irlap_status_indication(struct irlap_cb *self, int quality_of_link)
  511. {
  512. switch (quality_of_link) {
  513. case STATUS_NO_ACTIVITY:
  514. net_info_ratelimited("IrLAP, no activity on link!\n");
  515. break;
  516. case STATUS_NOISY:
  517. net_info_ratelimited("IrLAP, noisy link!\n");
  518. break;
  519. default:
  520. break;
  521. }
  522. irlmp_status_indication(self->notify.instance,
  523. quality_of_link, LOCK_NO_CHANGE);
  524. }
  525. /*
  526. * Function irlap_reset_indication (void)
  527. */
  528. void irlap_reset_indication(struct irlap_cb *self)
  529. {
  530. IRDA_ASSERT(self != NULL, return;);
  531. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  532. if (self->state == LAP_RESET_WAIT)
  533. irlap_do_event(self, RESET_REQUEST, NULL, NULL);
  534. else
  535. irlap_do_event(self, RESET_RESPONSE, NULL, NULL);
  536. }
  537. /*
  538. * Function irlap_reset_confirm (void)
  539. */
  540. void irlap_reset_confirm(void)
  541. {
  542. }
  543. /*
  544. * Function irlap_generate_rand_time_slot (S, s)
  545. *
  546. * Generate a random time slot between s and S-1 where
  547. * S = Number of slots (0 -> S-1)
  548. * s = Current slot
  549. */
  550. int irlap_generate_rand_time_slot(int S, int s)
  551. {
  552. static int rand;
  553. int slot;
  554. IRDA_ASSERT((S - s) > 0, return 0;);
  555. rand += jiffies;
  556. rand ^= (rand << 12);
  557. rand ^= (rand >> 20);
  558. slot = s + rand % (S-s);
  559. IRDA_ASSERT((slot >= s) || (slot < S), return 0;);
  560. return slot;
  561. }
  562. /*
  563. * Function irlap_update_nr_received (nr)
  564. *
  565. * Remove all acknowledged frames in current window queue. This code is
  566. * not intuitive and you should not try to change it. If you think it
  567. * contains bugs, please mail a patch to the author instead.
  568. */
  569. void irlap_update_nr_received(struct irlap_cb *self, int nr)
  570. {
  571. struct sk_buff *skb = NULL;
  572. int count = 0;
  573. /*
  574. * Remove all the ack-ed frames from the window queue.
  575. */
  576. /*
  577. * Optimize for the common case. It is most likely that the receiver
  578. * will acknowledge all the frames we have sent! So in that case we
  579. * delete all frames stored in window.
  580. */
  581. if (nr == self->vs) {
  582. while ((skb = skb_dequeue(&self->wx_list)) != NULL) {
  583. dev_kfree_skb(skb);
  584. }
  585. /* The last acked frame is the next to send minus one */
  586. self->va = nr - 1;
  587. } else {
  588. /* Remove all acknowledged frames in current window */
  589. while ((skb_peek(&self->wx_list) != NULL) &&
  590. (((self->va+1) % 8) != nr))
  591. {
  592. skb = skb_dequeue(&self->wx_list);
  593. dev_kfree_skb(skb);
  594. self->va = (self->va + 1) % 8;
  595. count++;
  596. }
  597. }
  598. /* Advance window */
  599. self->window = self->window_size - skb_queue_len(&self->wx_list);
  600. }
  601. /*
  602. * Function irlap_validate_ns_received (ns)
  603. *
  604. * Validate the next to send (ns) field from received frame.
  605. */
  606. int irlap_validate_ns_received(struct irlap_cb *self, int ns)
  607. {
  608. /* ns as expected? */
  609. if (ns == self->vr)
  610. return NS_EXPECTED;
  611. /*
  612. * Stations are allowed to treat invalid NS as unexpected NS
  613. * IrLAP, Recv ... with-invalid-Ns. p. 84
  614. */
  615. return NS_UNEXPECTED;
  616. /* return NR_INVALID; */
  617. }
  618. /*
  619. * Function irlap_validate_nr_received (nr)
  620. *
  621. * Validate the next to receive (nr) field from received frame.
  622. *
  623. */
  624. int irlap_validate_nr_received(struct irlap_cb *self, int nr)
  625. {
  626. /* nr as expected? */
  627. if (nr == self->vs) {
  628. pr_debug("%s(), expected!\n", __func__);
  629. return NR_EXPECTED;
  630. }
  631. /*
  632. * unexpected nr? (but within current window), first we check if the
  633. * ns numbers of the frames in the current window wrap.
  634. */
  635. if (self->va < self->vs) {
  636. if ((nr >= self->va) && (nr <= self->vs))
  637. return NR_UNEXPECTED;
  638. } else {
  639. if ((nr >= self->va) || (nr <= self->vs))
  640. return NR_UNEXPECTED;
  641. }
  642. /* Invalid nr! */
  643. return NR_INVALID;
  644. }
  645. /*
  646. * Function irlap_initiate_connection_state ()
  647. *
  648. * Initialize the connection state parameters
  649. *
  650. */
  651. void irlap_initiate_connection_state(struct irlap_cb *self)
  652. {
  653. IRDA_ASSERT(self != NULL, return;);
  654. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  655. /* Next to send and next to receive */
  656. self->vs = self->vr = 0;
  657. /* Last frame which got acked (0 - 1) % 8 */
  658. self->va = 7;
  659. self->window = 1;
  660. self->remote_busy = FALSE;
  661. self->retry_count = 0;
  662. }
  663. /*
  664. * Function irlap_wait_min_turn_around (self, qos)
  665. *
  666. * Wait negotiated minimum turn around time, this function actually sets
  667. * the number of BOS's that must be sent before the next transmitted
  668. * frame in order to delay for the specified amount of time. This is
  669. * done to avoid using timers, and the forbidden udelay!
  670. */
  671. void irlap_wait_min_turn_around(struct irlap_cb *self, struct qos_info *qos)
  672. {
  673. __u32 min_turn_time;
  674. __u32 speed;
  675. /* Get QoS values. */
  676. speed = qos->baud_rate.value;
  677. min_turn_time = qos->min_turn_time.value;
  678. /* No need to calculate XBOFs for speeds over 115200 bps */
  679. if (speed > 115200) {
  680. self->mtt_required = min_turn_time;
  681. return;
  682. }
  683. /*
  684. * Send additional BOF's for the next frame for the requested
  685. * min turn time, so now we must calculate how many chars (XBOF's) we
  686. * must send for the requested time period (min turn time)
  687. */
  688. self->xbofs_delay = irlap_min_turn_time_in_bytes(speed, min_turn_time);
  689. }
  690. /*
  691. * Function irlap_flush_all_queues (void)
  692. *
  693. * Flush all queues
  694. *
  695. */
  696. void irlap_flush_all_queues(struct irlap_cb *self)
  697. {
  698. struct sk_buff* skb;
  699. IRDA_ASSERT(self != NULL, return;);
  700. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  701. /* Free transmission queue */
  702. while ((skb = skb_dequeue(&self->txq)) != NULL)
  703. dev_kfree_skb(skb);
  704. while ((skb = skb_dequeue(&self->txq_ultra)) != NULL)
  705. dev_kfree_skb(skb);
  706. /* Free sliding window buffered packets */
  707. while ((skb = skb_dequeue(&self->wx_list)) != NULL)
  708. dev_kfree_skb(skb);
  709. }
  710. /*
  711. * Function irlap_setspeed (self, speed)
  712. *
  713. * Change the speed of the IrDA port
  714. *
  715. */
  716. static void irlap_change_speed(struct irlap_cb *self, __u32 speed, int now)
  717. {
  718. struct sk_buff *skb;
  719. pr_debug("%s(), setting speed to %d\n", __func__, speed);
  720. IRDA_ASSERT(self != NULL, return;);
  721. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  722. self->speed = speed;
  723. /* Change speed now, or just piggyback speed on frames */
  724. if (now) {
  725. /* Send down empty frame to trigger speed change */
  726. skb = alloc_skb(0, GFP_ATOMIC);
  727. if (skb)
  728. irlap_queue_xmit(self, skb);
  729. }
  730. }
  731. /*
  732. * Function irlap_init_qos_capabilities (self, qos)
  733. *
  734. * Initialize QoS for this IrLAP session, What we do is to compute the
  735. * intersection of the QoS capabilities for the user, driver and for
  736. * IrLAP itself. Normally, IrLAP will not specify any values, but it can
  737. * be used to restrict certain values.
  738. */
  739. static void irlap_init_qos_capabilities(struct irlap_cb *self,
  740. struct qos_info *qos_user)
  741. {
  742. IRDA_ASSERT(self != NULL, return;);
  743. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  744. IRDA_ASSERT(self->netdev != NULL, return;);
  745. /* Start out with the maximum QoS support possible */
  746. irda_init_max_qos_capabilies(&self->qos_rx);
  747. /* Apply drivers QoS capabilities */
  748. irda_qos_compute_intersection(&self->qos_rx, self->qos_dev);
  749. /*
  750. * Check for user supplied QoS parameters. The service user is only
  751. * allowed to supply these values. We check each parameter since the
  752. * user may not have set all of them.
  753. */
  754. if (qos_user) {
  755. pr_debug("%s(), Found user specified QoS!\n", __func__);
  756. if (qos_user->baud_rate.bits)
  757. self->qos_rx.baud_rate.bits &= qos_user->baud_rate.bits;
  758. if (qos_user->max_turn_time.bits)
  759. self->qos_rx.max_turn_time.bits &= qos_user->max_turn_time.bits;
  760. if (qos_user->data_size.bits)
  761. self->qos_rx.data_size.bits &= qos_user->data_size.bits;
  762. if (qos_user->link_disc_time.bits)
  763. self->qos_rx.link_disc_time.bits &= qos_user->link_disc_time.bits;
  764. }
  765. /* Use 500ms in IrLAP for now */
  766. self->qos_rx.max_turn_time.bits &= 0x01;
  767. /* Set data size */
  768. /*self->qos_rx.data_size.bits &= 0x03;*/
  769. irda_qos_bits_to_value(&self->qos_rx);
  770. }
  771. /*
  772. * Function irlap_apply_default_connection_parameters (void, now)
  773. *
  774. * Use the default connection and transmission parameters
  775. */
  776. void irlap_apply_default_connection_parameters(struct irlap_cb *self)
  777. {
  778. IRDA_ASSERT(self != NULL, return;);
  779. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  780. /* xbofs : Default value in NDM */
  781. self->next_bofs = 12;
  782. self->bofs_count = 12;
  783. /* NDM Speed is 9600 */
  784. irlap_change_speed(self, 9600, TRUE);
  785. /* Set mbusy when going to NDM state */
  786. irda_device_set_media_busy(self->netdev, TRUE);
  787. /*
  788. * Generate random connection address for this session, which must
  789. * be 7 bits wide and different from 0x00 and 0xfe
  790. */
  791. while ((self->caddr == 0x00) || (self->caddr == 0xfe)) {
  792. get_random_bytes(&self->caddr, sizeof(self->caddr));
  793. self->caddr &= 0xfe;
  794. }
  795. /* Use default values until connection has been negitiated */
  796. self->slot_timeout = sysctl_slot_timeout;
  797. self->final_timeout = FINAL_TIMEOUT;
  798. self->poll_timeout = POLL_TIMEOUT;
  799. self->wd_timeout = WD_TIMEOUT;
  800. /* Set some default values */
  801. self->qos_tx.baud_rate.value = 9600;
  802. self->qos_rx.baud_rate.value = 9600;
  803. self->qos_tx.max_turn_time.value = 0;
  804. self->qos_rx.max_turn_time.value = 0;
  805. self->qos_tx.min_turn_time.value = 0;
  806. self->qos_rx.min_turn_time.value = 0;
  807. self->qos_tx.data_size.value = 64;
  808. self->qos_rx.data_size.value = 64;
  809. self->qos_tx.window_size.value = 1;
  810. self->qos_rx.window_size.value = 1;
  811. self->qos_tx.additional_bofs.value = 12;
  812. self->qos_rx.additional_bofs.value = 12;
  813. self->qos_tx.link_disc_time.value = 0;
  814. self->qos_rx.link_disc_time.value = 0;
  815. irlap_flush_all_queues(self);
  816. self->disconnect_pending = FALSE;
  817. self->connect_pending = FALSE;
  818. }
  819. /*
  820. * Function irlap_apply_connection_parameters (qos, now)
  821. *
  822. * Initialize IrLAP with the negotiated QoS values
  823. *
  824. * If 'now' is false, the speed and xbofs will be changed after the next
  825. * frame is sent.
  826. * If 'now' is true, the speed and xbofs is changed immediately
  827. */
  828. void irlap_apply_connection_parameters(struct irlap_cb *self, int now)
  829. {
  830. IRDA_ASSERT(self != NULL, return;);
  831. IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
  832. /* Set the negotiated xbofs value */
  833. self->next_bofs = self->qos_tx.additional_bofs.value;
  834. if (now)
  835. self->bofs_count = self->next_bofs;
  836. /* Set the negotiated link speed (may need the new xbofs value) */
  837. irlap_change_speed(self, self->qos_tx.baud_rate.value, now);
  838. self->window_size = self->qos_tx.window_size.value;
  839. self->window = self->qos_tx.window_size.value;
  840. #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
  841. /*
  842. * Calculate how many bytes it is possible to transmit before the
  843. * link must be turned around
  844. */
  845. self->line_capacity =
  846. irlap_max_line_capacity(self->qos_tx.baud_rate.value,
  847. self->qos_tx.max_turn_time.value);
  848. self->bytes_left = self->line_capacity;
  849. #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
  850. /*
  851. * Initialize timeout values, some of the rules are listed on
  852. * page 92 in IrLAP.
  853. */
  854. IRDA_ASSERT(self->qos_tx.max_turn_time.value != 0, return;);
  855. IRDA_ASSERT(self->qos_rx.max_turn_time.value != 0, return;);
  856. /* The poll timeout applies only to the primary station.
  857. * It defines the maximum time the primary stay in XMIT mode
  858. * before timeout and turning the link around (sending a RR).
  859. * Or, this is how much we can keep the pf bit in primary mode.
  860. * Therefore, it must be lower or equal than our *OWN* max turn around.
  861. * Jean II */
  862. self->poll_timeout = msecs_to_jiffies(
  863. self->qos_tx.max_turn_time.value);
  864. /* The Final timeout applies only to the primary station.
  865. * It defines the maximum time the primary wait (mostly in RECV mode)
  866. * for an answer from the secondary station before polling it again.
  867. * Therefore, it must be greater or equal than our *PARTNER*
  868. * max turn around time - Jean II */
  869. self->final_timeout = msecs_to_jiffies(
  870. self->qos_rx.max_turn_time.value);
  871. /* The Watchdog Bit timeout applies only to the secondary station.
  872. * It defines the maximum time the secondary wait (mostly in RECV mode)
  873. * for poll from the primary station before getting annoyed.
  874. * Therefore, it must be greater or equal than our *PARTNER*
  875. * max turn around time - Jean II */
  876. self->wd_timeout = self->final_timeout * 2;
  877. /*
  878. * N1 and N2 are maximum retry count for *both* the final timer
  879. * and the wd timer (with a factor 2) as defined above.
  880. * After N1 retry of a timer, we give a warning to the user.
  881. * After N2 retry, we consider the link dead and disconnect it.
  882. * Jean II
  883. */
  884. /*
  885. * Set N1 to 0 if Link Disconnect/Threshold Time = 3 and set it to
  886. * 3 seconds otherwise. See page 71 in IrLAP for more details.
  887. * Actually, it's not always 3 seconds, as we allow to set
  888. * it via sysctl... Max maxtt is 500ms, and N1 need to be multiple
  889. * of 2, so 1 second is minimum we can allow. - Jean II
  890. */
  891. if (self->qos_tx.link_disc_time.value == sysctl_warn_noreply_time)
  892. /*
  893. * If we set N1 to 0, it will trigger immediately, which is
  894. * not what we want. What we really want is to disable it,
  895. * Jean II
  896. */
  897. self->N1 = -2; /* Disable - Need to be multiple of 2*/
  898. else
  899. self->N1 = sysctl_warn_noreply_time * 1000 /
  900. self->qos_rx.max_turn_time.value;
  901. pr_debug("Setting N1 = %d\n", self->N1);
  902. /* Set N2 to match our own disconnect time */
  903. self->N2 = self->qos_tx.link_disc_time.value * 1000 /
  904. self->qos_rx.max_turn_time.value;
  905. pr_debug("Setting N2 = %d\n", self->N2);
  906. }
  907. #ifdef CONFIG_PROC_FS
  908. struct irlap_iter_state {
  909. int id;
  910. };
  911. static void *irlap_seq_start(struct seq_file *seq, loff_t *pos)
  912. {
  913. struct irlap_iter_state *iter = seq->private;
  914. struct irlap_cb *self;
  915. /* Protect our access to the tsap list */
  916. spin_lock_irq(&irlap->hb_spinlock);
  917. iter->id = 0;
  918. for (self = (struct irlap_cb *) hashbin_get_first(irlap);
  919. self; self = (struct irlap_cb *) hashbin_get_next(irlap)) {
  920. if (iter->id == *pos)
  921. break;
  922. ++iter->id;
  923. }
  924. return self;
  925. }
  926. static void *irlap_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  927. {
  928. struct irlap_iter_state *iter = seq->private;
  929. ++*pos;
  930. ++iter->id;
  931. return (void *) hashbin_get_next(irlap);
  932. }
  933. static void irlap_seq_stop(struct seq_file *seq, void *v)
  934. {
  935. spin_unlock_irq(&irlap->hb_spinlock);
  936. }
  937. static int irlap_seq_show(struct seq_file *seq, void *v)
  938. {
  939. const struct irlap_iter_state *iter = seq->private;
  940. const struct irlap_cb *self = v;
  941. IRDA_ASSERT(self->magic == LAP_MAGIC, return -EINVAL;);
  942. seq_printf(seq, "irlap%d ", iter->id);
  943. seq_printf(seq, "state: %s\n",
  944. irlap_state[self->state]);
  945. seq_printf(seq, " device name: %s, ",
  946. (self->netdev) ? self->netdev->name : "bug");
  947. seq_printf(seq, "hardware name: %s\n", self->hw_name);
  948. seq_printf(seq, " caddr: %#02x, ", self->caddr);
  949. seq_printf(seq, "saddr: %#08x, ", self->saddr);
  950. seq_printf(seq, "daddr: %#08x\n", self->daddr);
  951. seq_printf(seq, " win size: %d, ",
  952. self->window_size);
  953. seq_printf(seq, "win: %d, ", self->window);
  954. #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
  955. seq_printf(seq, "line capacity: %d, ",
  956. self->line_capacity);
  957. seq_printf(seq, "bytes left: %d\n", self->bytes_left);
  958. #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
  959. seq_printf(seq, " tx queue len: %d ",
  960. skb_queue_len(&self->txq));
  961. seq_printf(seq, "win queue len: %d ",
  962. skb_queue_len(&self->wx_list));
  963. seq_printf(seq, "rbusy: %s", self->remote_busy ?
  964. "TRUE" : "FALSE");
  965. seq_printf(seq, " mbusy: %s\n", self->media_busy ?
  966. "TRUE" : "FALSE");
  967. seq_printf(seq, " retrans: %d ", self->retry_count);
  968. seq_printf(seq, "vs: %d ", self->vs);
  969. seq_printf(seq, "vr: %d ", self->vr);
  970. seq_printf(seq, "va: %d\n", self->va);
  971. seq_printf(seq, " qos\tbps\tmaxtt\tdsize\twinsize\taddbofs\tmintt\tldisc\tcomp\n");
  972. seq_printf(seq, " tx\t%d\t",
  973. self->qos_tx.baud_rate.value);
  974. seq_printf(seq, "%d\t",
  975. self->qos_tx.max_turn_time.value);
  976. seq_printf(seq, "%d\t",
  977. self->qos_tx.data_size.value);
  978. seq_printf(seq, "%d\t",
  979. self->qos_tx.window_size.value);
  980. seq_printf(seq, "%d\t",
  981. self->qos_tx.additional_bofs.value);
  982. seq_printf(seq, "%d\t",
  983. self->qos_tx.min_turn_time.value);
  984. seq_printf(seq, "%d\t",
  985. self->qos_tx.link_disc_time.value);
  986. seq_printf(seq, "\n");
  987. seq_printf(seq, " rx\t%d\t",
  988. self->qos_rx.baud_rate.value);
  989. seq_printf(seq, "%d\t",
  990. self->qos_rx.max_turn_time.value);
  991. seq_printf(seq, "%d\t",
  992. self->qos_rx.data_size.value);
  993. seq_printf(seq, "%d\t",
  994. self->qos_rx.window_size.value);
  995. seq_printf(seq, "%d\t",
  996. self->qos_rx.additional_bofs.value);
  997. seq_printf(seq, "%d\t",
  998. self->qos_rx.min_turn_time.value);
  999. seq_printf(seq, "%d\n",
  1000. self->qos_rx.link_disc_time.value);
  1001. return 0;
  1002. }
  1003. static const struct seq_operations irlap_seq_ops = {
  1004. .start = irlap_seq_start,
  1005. .next = irlap_seq_next,
  1006. .stop = irlap_seq_stop,
  1007. .show = irlap_seq_show,
  1008. };
  1009. static int irlap_seq_open(struct inode *inode, struct file *file)
  1010. {
  1011. if (irlap == NULL)
  1012. return -EINVAL;
  1013. return seq_open_private(file, &irlap_seq_ops,
  1014. sizeof(struct irlap_iter_state));
  1015. }
  1016. const struct file_operations irlap_seq_fops = {
  1017. .owner = THIS_MODULE,
  1018. .open = irlap_seq_open,
  1019. .read = seq_read,
  1020. .llseek = seq_lseek,
  1021. .release = seq_release_private,
  1022. };
  1023. #endif /* CONFIG_PROC_FS */