irlap.c 34 KB

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