irlap.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  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 */