irlap.c 34 KB

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