irnet_irda.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885
  1. /*
  2. * IrNET protocol module : Synchronous PPP over an IrDA socket.
  3. *
  4. * Jean II - HPL `00 - <jt@hpl.hp.com>
  5. *
  6. * This file implement the IRDA interface of IrNET.
  7. * Basically, we sit on top of IrTTP. We set up IrTTP, IrIAS properly,
  8. * and exchange frames with IrTTP.
  9. */
  10. #include "irnet_irda.h" /* Private header */
  11. #include <linux/sched.h>
  12. #include <linux/seq_file.h>
  13. #include <linux/slab.h>
  14. #include <asm/unaligned.h>
  15. /*
  16. * PPP disconnect work: we need to make sure we're in
  17. * process context when calling ppp_unregister_channel().
  18. */
  19. static void irnet_ppp_disconnect(struct work_struct *work)
  20. {
  21. irnet_socket * self =
  22. container_of(work, irnet_socket, disconnect_work);
  23. if (self == NULL)
  24. return;
  25. /*
  26. * If we were connected, cleanup & close the PPP
  27. * channel, which will kill pppd (hangup) and the rest.
  28. */
  29. if (self->ppp_open && !self->ttp_open && !self->ttp_connect) {
  30. ppp_unregister_channel(&self->chan);
  31. self->ppp_open = 0;
  32. }
  33. }
  34. /************************* CONTROL CHANNEL *************************/
  35. /*
  36. * When ppp is not active, /dev/irnet act as a control channel.
  37. * Writing allow to set up the IrDA destination of the IrNET channel,
  38. * and any application may be read events happening on IrNET...
  39. */
  40. /*------------------------------------------------------------------*/
  41. /*
  42. * Post an event to the control channel...
  43. * Put the event in the log, and then wait all process blocked on read
  44. * so they can read the log...
  45. */
  46. static void
  47. irnet_post_event(irnet_socket * ap,
  48. irnet_event event,
  49. __u32 saddr,
  50. __u32 daddr,
  51. char * name,
  52. __u16 hints)
  53. {
  54. int index; /* In the log */
  55. DENTER(CTRL_TRACE, "(ap=0x%p, event=%d, daddr=%08x, name=``%s'')\n",
  56. ap, event, daddr, name);
  57. /* Protect this section via spinlock.
  58. * Note : as we are the only event producer, we only need to exclude
  59. * ourself when touching the log, which is nice and easy.
  60. */
  61. spin_lock_bh(&irnet_events.spinlock);
  62. /* Copy the event in the log */
  63. index = irnet_events.index;
  64. irnet_events.log[index].event = event;
  65. irnet_events.log[index].daddr = daddr;
  66. irnet_events.log[index].saddr = saddr;
  67. /* Try to copy IrDA nickname */
  68. if(name)
  69. strcpy(irnet_events.log[index].name, name);
  70. else
  71. irnet_events.log[index].name[0] = '\0';
  72. /* Copy hints */
  73. irnet_events.log[index].hints.word = hints;
  74. /* Try to get ppp unit number */
  75. if((ap != (irnet_socket *) NULL) && (ap->ppp_open))
  76. irnet_events.log[index].unit = ppp_unit_number(&ap->chan);
  77. else
  78. irnet_events.log[index].unit = -1;
  79. /* Increment the index
  80. * Note that we increment the index only after the event is written,
  81. * to make sure that the readers don't get garbage... */
  82. irnet_events.index = (index + 1) % IRNET_MAX_EVENTS;
  83. DEBUG(CTRL_INFO, "New event index is %d\n", irnet_events.index);
  84. /* Spin lock end */
  85. spin_unlock_bh(&irnet_events.spinlock);
  86. /* Now : wake up everybody waiting for events... */
  87. wake_up_interruptible_all(&irnet_events.rwait);
  88. DEXIT(CTRL_TRACE, "\n");
  89. }
  90. /************************* IRDA SUBROUTINES *************************/
  91. /*
  92. * These are a bunch of subroutines called from other functions
  93. * down there, mostly common code or to improve readability...
  94. *
  95. * Note : we duplicate quite heavily some routines of af_irda.c,
  96. * because our input structure (self) is quite different
  97. * (struct irnet instead of struct irda_sock), which make sharing
  98. * the same code impossible (at least, without templates).
  99. */
  100. /*------------------------------------------------------------------*/
  101. /*
  102. * Function irda_open_tsap (self)
  103. *
  104. * Open local Transport Service Access Point (TSAP)
  105. *
  106. * Create a IrTTP instance for us and set all the IrTTP callbacks.
  107. */
  108. static inline int
  109. irnet_open_tsap(irnet_socket * self)
  110. {
  111. notify_t notify; /* Callback structure */
  112. DENTER(IRDA_SR_TRACE, "(self=0x%p)\n", self);
  113. DABORT(self->tsap != NULL, -EBUSY, IRDA_SR_ERROR, "Already busy !\n");
  114. /* Initialize IrTTP callbacks to be used by the IrDA stack */
  115. irda_notify_init(&notify);
  116. notify.connect_confirm = irnet_connect_confirm;
  117. notify.connect_indication = irnet_connect_indication;
  118. notify.disconnect_indication = irnet_disconnect_indication;
  119. notify.data_indication = irnet_data_indication;
  120. /*notify.udata_indication = NULL;*/
  121. notify.flow_indication = irnet_flow_indication;
  122. notify.status_indication = irnet_status_indication;
  123. notify.instance = self;
  124. strlcpy(notify.name, IRNET_NOTIFY_NAME, sizeof(notify.name));
  125. /* Open an IrTTP instance */
  126. self->tsap = irttp_open_tsap(LSAP_ANY, DEFAULT_INITIAL_CREDIT,
  127. &notify);
  128. DABORT(self->tsap == NULL, -ENOMEM,
  129. IRDA_SR_ERROR, "Unable to allocate TSAP !\n");
  130. /* Remember which TSAP selector we actually got */
  131. self->stsap_sel = self->tsap->stsap_sel;
  132. DEXIT(IRDA_SR_TRACE, " - tsap=0x%p, sel=0x%X\n",
  133. self->tsap, self->stsap_sel);
  134. return 0;
  135. }
  136. /*------------------------------------------------------------------*/
  137. /*
  138. * Function irnet_ias_to_tsap (self, result, value)
  139. *
  140. * Examine an IAS object and extract TSAP
  141. *
  142. * We do an IAP query to find the TSAP associated with the IrNET service.
  143. * When IrIAP pass us the result of the query, this function look at
  144. * the return values to check for failures and extract the TSAP if
  145. * possible.
  146. * Also deallocate value
  147. * The failure is in self->errno
  148. * Return TSAP or -1
  149. */
  150. static inline __u8
  151. irnet_ias_to_tsap(irnet_socket * self,
  152. int result,
  153. struct ias_value * value)
  154. {
  155. __u8 dtsap_sel = 0; /* TSAP we are looking for */
  156. DENTER(IRDA_SR_TRACE, "(self=0x%p)\n", self);
  157. /* By default, no error */
  158. self->errno = 0;
  159. /* Check if request succeeded */
  160. switch(result)
  161. {
  162. /* Standard errors : service not available */
  163. case IAS_CLASS_UNKNOWN:
  164. case IAS_ATTRIB_UNKNOWN:
  165. DEBUG(IRDA_SR_INFO, "IAS object doesn't exist ! (%d)\n", result);
  166. self->errno = -EADDRNOTAVAIL;
  167. break;
  168. /* Other errors, most likely IrDA stack failure */
  169. default :
  170. DEBUG(IRDA_SR_INFO, "IAS query failed ! (%d)\n", result);
  171. self->errno = -EHOSTUNREACH;
  172. break;
  173. /* Success : we got what we wanted */
  174. case IAS_SUCCESS:
  175. break;
  176. }
  177. /* Check what was returned to us */
  178. if(value != NULL)
  179. {
  180. /* What type of argument have we got ? */
  181. switch(value->type)
  182. {
  183. case IAS_INTEGER:
  184. DEBUG(IRDA_SR_INFO, "result=%d\n", value->t.integer);
  185. if(value->t.integer != -1)
  186. /* Get the remote TSAP selector */
  187. dtsap_sel = value->t.integer;
  188. else
  189. self->errno = -EADDRNOTAVAIL;
  190. break;
  191. default:
  192. self->errno = -EADDRNOTAVAIL;
  193. DERROR(IRDA_SR_ERROR, "bad type ! (0x%X)\n", value->type);
  194. break;
  195. }
  196. /* Cleanup */
  197. irias_delete_value(value);
  198. }
  199. else /* value == NULL */
  200. {
  201. /* Nothing returned to us - usually result != SUCCESS */
  202. if(!(self->errno))
  203. {
  204. DERROR(IRDA_SR_ERROR,
  205. "IrDA bug : result == SUCCESS && value == NULL\n");
  206. self->errno = -EHOSTUNREACH;
  207. }
  208. }
  209. DEXIT(IRDA_SR_TRACE, "\n");
  210. /* Return the TSAP */
  211. return dtsap_sel;
  212. }
  213. /*------------------------------------------------------------------*/
  214. /*
  215. * Function irnet_find_lsap_sel (self)
  216. *
  217. * Try to lookup LSAP selector in remote LM-IAS
  218. *
  219. * Basically, we start a IAP query, and then go to sleep. When the query
  220. * return, irnet_getvalue_confirm will wake us up, and we can examine the
  221. * result of the query...
  222. * Note that in some case, the query fail even before we go to sleep,
  223. * creating some races...
  224. */
  225. static inline int
  226. irnet_find_lsap_sel(irnet_socket * self)
  227. {
  228. DENTER(IRDA_SR_TRACE, "(self=0x%p)\n", self);
  229. /* This should not happen */
  230. DABORT(self->iriap, -EBUSY, IRDA_SR_ERROR, "busy with a previous query.\n");
  231. /* Create an IAP instance, will be closed in irnet_getvalue_confirm() */
  232. self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
  233. irnet_getvalue_confirm);
  234. /* Treat unexpected signals as disconnect */
  235. self->errno = -EHOSTUNREACH;
  236. /* Query remote LM-IAS */
  237. iriap_getvaluebyclass_request(self->iriap, self->rsaddr, self->daddr,
  238. IRNET_SERVICE_NAME, IRNET_IAS_VALUE);
  239. /* The above request is non-blocking.
  240. * After a while, IrDA will call us back in irnet_getvalue_confirm()
  241. * We will then call irnet_ias_to_tsap() and finish the
  242. * connection procedure */
  243. DEXIT(IRDA_SR_TRACE, "\n");
  244. return 0;
  245. }
  246. /*------------------------------------------------------------------*/
  247. /*
  248. * Function irnet_connect_tsap (self)
  249. *
  250. * Initialise the TTP socket and initiate TTP connection
  251. *
  252. */
  253. static inline int
  254. irnet_connect_tsap(irnet_socket * self)
  255. {
  256. int err;
  257. DENTER(IRDA_SR_TRACE, "(self=0x%p)\n", self);
  258. /* Open a local TSAP (an IrTTP instance) */
  259. err = irnet_open_tsap(self);
  260. if(err != 0)
  261. {
  262. clear_bit(0, &self->ttp_connect);
  263. DERROR(IRDA_SR_ERROR, "connect aborted!\n");
  264. return err;
  265. }
  266. /* Connect to remote device */
  267. err = irttp_connect_request(self->tsap, self->dtsap_sel,
  268. self->rsaddr, self->daddr, NULL,
  269. self->max_sdu_size_rx, NULL);
  270. if(err != 0)
  271. {
  272. clear_bit(0, &self->ttp_connect);
  273. DERROR(IRDA_SR_ERROR, "connect aborted!\n");
  274. return err;
  275. }
  276. /* The above call is non-blocking.
  277. * After a while, the IrDA stack will either call us back in
  278. * irnet_connect_confirm() or irnet_disconnect_indication()
  279. * See you there ;-) */
  280. DEXIT(IRDA_SR_TRACE, "\n");
  281. return err;
  282. }
  283. /*------------------------------------------------------------------*/
  284. /*
  285. * Function irnet_discover_next_daddr (self)
  286. *
  287. * Query the IrNET TSAP of the next device in the log.
  288. *
  289. * Used in the TSAP discovery procedure.
  290. */
  291. static inline int
  292. irnet_discover_next_daddr(irnet_socket * self)
  293. {
  294. /* Close the last instance of IrIAP, and open a new one.
  295. * We can't reuse the IrIAP instance in the IrIAP callback */
  296. if(self->iriap)
  297. {
  298. iriap_close(self->iriap);
  299. self->iriap = NULL;
  300. }
  301. /* Create a new IAP instance */
  302. self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
  303. irnet_discovervalue_confirm);
  304. if(self->iriap == NULL)
  305. return -ENOMEM;
  306. /* Next discovery - before the call to avoid races */
  307. self->disco_index++;
  308. /* Check if we have one more address to try */
  309. if(self->disco_index < self->disco_number)
  310. {
  311. /* Query remote LM-IAS */
  312. iriap_getvaluebyclass_request(self->iriap,
  313. self->discoveries[self->disco_index].saddr,
  314. self->discoveries[self->disco_index].daddr,
  315. IRNET_SERVICE_NAME, IRNET_IAS_VALUE);
  316. /* The above request is non-blocking.
  317. * After a while, IrDA will call us back in irnet_discovervalue_confirm()
  318. * We will then call irnet_ias_to_tsap() and come back here again... */
  319. return 0;
  320. }
  321. else
  322. return 1;
  323. }
  324. /*------------------------------------------------------------------*/
  325. /*
  326. * Function irnet_discover_daddr_and_lsap_sel (self)
  327. *
  328. * This try to find a device with the requested service.
  329. *
  330. * Initiate a TSAP discovery procedure.
  331. * It basically look into the discovery log. For each address in the list,
  332. * it queries the LM-IAS of the device to find if this device offer
  333. * the requested service.
  334. * If there is more than one node supporting the service, we complain
  335. * to the user (it should move devices around).
  336. * If we find one node which have the requested TSAP, we connect to it.
  337. *
  338. * This function just start the whole procedure. It request the discovery
  339. * log and submit the first IAS query.
  340. * The bulk of the job is handled in irnet_discovervalue_confirm()
  341. *
  342. * Note : this procedure fails if there is more than one device in range
  343. * on the same dongle, because IrLMP doesn't disconnect the LAP when the
  344. * last LSAP is closed. Moreover, we would need to wait the LAP
  345. * disconnection...
  346. */
  347. static inline int
  348. irnet_discover_daddr_and_lsap_sel(irnet_socket * self)
  349. {
  350. int ret;
  351. DENTER(IRDA_SR_TRACE, "(self=0x%p)\n", self);
  352. /* Ask lmp for the current discovery log */
  353. self->discoveries = irlmp_get_discoveries(&self->disco_number, self->mask,
  354. DISCOVERY_DEFAULT_SLOTS);
  355. /* Check if the we got some results */
  356. if(self->discoveries == NULL)
  357. {
  358. self->disco_number = -1;
  359. clear_bit(0, &self->ttp_connect);
  360. DRETURN(-ENETUNREACH, IRDA_SR_INFO, "No Cachelog...\n");
  361. }
  362. DEBUG(IRDA_SR_INFO, "Got the log (0x%p), size is %d\n",
  363. self->discoveries, self->disco_number);
  364. /* Start with the first discovery */
  365. self->disco_index = -1;
  366. self->daddr = DEV_ADDR_ANY;
  367. /* This will fail if the log is empty - this is non-blocking */
  368. ret = irnet_discover_next_daddr(self);
  369. if(ret)
  370. {
  371. /* Close IAP */
  372. if(self->iriap)
  373. iriap_close(self->iriap);
  374. self->iriap = NULL;
  375. /* Cleanup our copy of the discovery log */
  376. kfree(self->discoveries);
  377. self->discoveries = NULL;
  378. clear_bit(0, &self->ttp_connect);
  379. DRETURN(-ENETUNREACH, IRDA_SR_INFO, "Cachelog empty...\n");
  380. }
  381. /* Follow me in irnet_discovervalue_confirm() */
  382. DEXIT(IRDA_SR_TRACE, "\n");
  383. return 0;
  384. }
  385. /*------------------------------------------------------------------*/
  386. /*
  387. * Function irnet_dname_to_daddr (self)
  388. *
  389. * Convert an IrDA nickname to a valid IrDA address
  390. *
  391. * It basically look into the discovery log until there is a match.
  392. */
  393. static inline int
  394. irnet_dname_to_daddr(irnet_socket * self)
  395. {
  396. struct irda_device_info *discoveries; /* Copy of the discovery log */
  397. int number; /* Number of nodes in the log */
  398. int i;
  399. DENTER(IRDA_SR_TRACE, "(self=0x%p)\n", self);
  400. /* Ask lmp for the current discovery log */
  401. discoveries = irlmp_get_discoveries(&number, 0xffff,
  402. DISCOVERY_DEFAULT_SLOTS);
  403. /* Check if the we got some results */
  404. if(discoveries == NULL)
  405. DRETURN(-ENETUNREACH, IRDA_SR_INFO, "Cachelog empty...\n");
  406. /*
  407. * Now, check all discovered devices (if any), and connect
  408. * client only about the services that the client is
  409. * interested in...
  410. */
  411. for(i = 0; i < number; i++)
  412. {
  413. /* Does the name match ? */
  414. if(!strncmp(discoveries[i].info, self->rname, NICKNAME_MAX_LEN))
  415. {
  416. /* Yes !!! Get it.. */
  417. self->daddr = discoveries[i].daddr;
  418. DEBUG(IRDA_SR_INFO, "discovered device ``%s'' at address 0x%08x.\n",
  419. self->rname, self->daddr);
  420. kfree(discoveries);
  421. DEXIT(IRDA_SR_TRACE, "\n");
  422. return 0;
  423. }
  424. }
  425. /* No luck ! */
  426. DEBUG(IRDA_SR_INFO, "cannot discover device ``%s'' !!!\n", self->rname);
  427. kfree(discoveries);
  428. return -EADDRNOTAVAIL;
  429. }
  430. /************************* SOCKET ROUTINES *************************/
  431. /*
  432. * This are the main operations on IrNET sockets, basically to create
  433. * and destroy IrNET sockets. These are called from the PPP part...
  434. */
  435. /*------------------------------------------------------------------*/
  436. /*
  437. * Create a IrNET instance : just initialise some parameters...
  438. */
  439. int
  440. irda_irnet_create(irnet_socket * self)
  441. {
  442. DENTER(IRDA_SOCK_TRACE, "(self=0x%p)\n", self);
  443. self->magic = IRNET_MAGIC; /* Paranoia */
  444. self->ttp_open = 0; /* Prevent higher layer from accessing IrTTP */
  445. self->ttp_connect = 0; /* Not connecting yet */
  446. self->rname[0] = '\0'; /* May be set via control channel */
  447. self->rdaddr = DEV_ADDR_ANY; /* May be set via control channel */
  448. self->rsaddr = DEV_ADDR_ANY; /* May be set via control channel */
  449. self->daddr = DEV_ADDR_ANY; /* Until we get connected */
  450. self->saddr = DEV_ADDR_ANY; /* Until we get connected */
  451. self->max_sdu_size_rx = TTP_SAR_UNBOUND;
  452. /* Register as a client with IrLMP */
  453. self->ckey = irlmp_register_client(0, NULL, NULL, NULL);
  454. #ifdef DISCOVERY_NOMASK
  455. self->mask = 0xffff; /* For W2k compatibility */
  456. #else /* DISCOVERY_NOMASK */
  457. self->mask = irlmp_service_to_hint(S_LAN);
  458. #endif /* DISCOVERY_NOMASK */
  459. self->tx_flow = FLOW_START; /* Flow control from IrTTP */
  460. INIT_WORK(&self->disconnect_work, irnet_ppp_disconnect);
  461. DEXIT(IRDA_SOCK_TRACE, "\n");
  462. return 0;
  463. }
  464. /*------------------------------------------------------------------*/
  465. /*
  466. * Connect to the other side :
  467. * o convert device name to an address
  468. * o find the socket number (dlsap)
  469. * o Establish the connection
  470. *
  471. * Note : We no longer mimic af_irda. The IAS query for finding the TSAP
  472. * is done asynchronously, like the TTP connection. This allow us to
  473. * call this function from any context (not only process).
  474. * The downside is that following what's happening in there is tricky
  475. * because it involve various functions all over the place...
  476. */
  477. int
  478. irda_irnet_connect(irnet_socket * self)
  479. {
  480. int err;
  481. DENTER(IRDA_SOCK_TRACE, "(self=0x%p)\n", self);
  482. /* Check if we are already trying to connect.
  483. * Because irda_irnet_connect() can be called directly by pppd plus
  484. * packet retries in ppp_generic and connect may take time, plus we may
  485. * race with irnet_connect_indication(), we need to be careful there... */
  486. if(test_and_set_bit(0, &self->ttp_connect))
  487. DRETURN(-EBUSY, IRDA_SOCK_INFO, "Already connecting...\n");
  488. if((self->iriap != NULL) || (self->tsap != NULL))
  489. DERROR(IRDA_SOCK_ERROR, "Socket not cleaned up...\n");
  490. /* Insert ourselves in the hashbin so that the IrNET server can find us.
  491. * Notes : 4th arg is string of 32 char max and must be null terminated
  492. * When 4th arg is used (string), 3rd arg isn't (int)
  493. * Can't re-insert (MUST remove first) so check for that... */
  494. if((irnet_server.running) && (self->q.q_next == NULL))
  495. {
  496. spin_lock_bh(&irnet_server.spinlock);
  497. hashbin_insert(irnet_server.list, (irda_queue_t *) self, 0, self->rname);
  498. spin_unlock_bh(&irnet_server.spinlock);
  499. DEBUG(IRDA_SOCK_INFO, "Inserted ``%s'' in hashbin...\n", self->rname);
  500. }
  501. /* If we don't have anything (no address, no name) */
  502. if((self->rdaddr == DEV_ADDR_ANY) && (self->rname[0] == '\0'))
  503. {
  504. /* Try to find a suitable address */
  505. if((err = irnet_discover_daddr_and_lsap_sel(self)) != 0)
  506. DRETURN(err, IRDA_SOCK_INFO, "auto-connect failed!\n");
  507. /* In most cases, the call above is non-blocking */
  508. }
  509. else
  510. {
  511. /* If we have only the name (no address), try to get an address */
  512. if(self->rdaddr == DEV_ADDR_ANY)
  513. {
  514. if((err = irnet_dname_to_daddr(self)) != 0)
  515. DRETURN(err, IRDA_SOCK_INFO, "name connect failed!\n");
  516. }
  517. else
  518. /* Use the requested destination address */
  519. self->daddr = self->rdaddr;
  520. /* Query remote LM-IAS to find LSAP selector */
  521. irnet_find_lsap_sel(self);
  522. /* The above call is non blocking */
  523. }
  524. /* At this point, we are waiting for the IrDA stack to call us back,
  525. * or we have already failed.
  526. * We will finish the connection procedure in irnet_connect_tsap().
  527. */
  528. DEXIT(IRDA_SOCK_TRACE, "\n");
  529. return 0;
  530. }
  531. /*------------------------------------------------------------------*/
  532. /*
  533. * Function irda_irnet_destroy(self)
  534. *
  535. * Destroy irnet instance
  536. *
  537. * Note : this need to be called from a process context.
  538. */
  539. void
  540. irda_irnet_destroy(irnet_socket * self)
  541. {
  542. DENTER(IRDA_SOCK_TRACE, "(self=0x%p)\n", self);
  543. if(self == NULL)
  544. return;
  545. /* Remove ourselves from hashbin (if we are queued in hashbin)
  546. * Note : `irnet_server.running' protect us from calls in hashbin_delete() */
  547. if((irnet_server.running) && (self->q.q_next != NULL))
  548. {
  549. struct irnet_socket * entry;
  550. DEBUG(IRDA_SOCK_INFO, "Removing from hash..\n");
  551. spin_lock_bh(&irnet_server.spinlock);
  552. entry = hashbin_remove_this(irnet_server.list, (irda_queue_t *) self);
  553. self->q.q_next = NULL;
  554. spin_unlock_bh(&irnet_server.spinlock);
  555. DASSERT(entry == self, , IRDA_SOCK_ERROR, "Can't remove from hash.\n");
  556. }
  557. /* If we were connected, post a message */
  558. if(test_bit(0, &self->ttp_open))
  559. {
  560. /* Note : as the disconnect comes from ppp_generic, the unit number
  561. * doesn't exist anymore when we post the event, so we need to pass
  562. * NULL as the first arg... */
  563. irnet_post_event(NULL, IRNET_DISCONNECT_TO,
  564. self->saddr, self->daddr, self->rname, 0);
  565. }
  566. /* Prevent various IrDA callbacks from messing up things
  567. * Need to be first */
  568. clear_bit(0, &self->ttp_connect);
  569. /* Prevent higher layer from accessing IrTTP */
  570. clear_bit(0, &self->ttp_open);
  571. /* Unregister with IrLMP */
  572. irlmp_unregister_client(self->ckey);
  573. /* Unregister with LM-IAS */
  574. if(self->iriap)
  575. {
  576. iriap_close(self->iriap);
  577. self->iriap = NULL;
  578. }
  579. /* Cleanup eventual discoveries from connection attempt or control channel */
  580. if(self->discoveries != NULL)
  581. {
  582. /* Cleanup our copy of the discovery log */
  583. kfree(self->discoveries);
  584. self->discoveries = NULL;
  585. }
  586. /* Close our IrTTP connection */
  587. if(self->tsap)
  588. {
  589. DEBUG(IRDA_SOCK_INFO, "Closing our TTP connection.\n");
  590. irttp_disconnect_request(self->tsap, NULL, P_NORMAL);
  591. irttp_close_tsap(self->tsap);
  592. self->tsap = NULL;
  593. }
  594. self->stsap_sel = 0;
  595. DEXIT(IRDA_SOCK_TRACE, "\n");
  596. }
  597. /************************** SERVER SOCKET **************************/
  598. /*
  599. * The IrNET service is composed of one server socket and a variable
  600. * number of regular IrNET sockets. The server socket is supposed to
  601. * handle incoming connections and redirect them to one IrNET sockets.
  602. * It's a superset of the regular IrNET socket, but has a very distinct
  603. * behaviour...
  604. */
  605. /*------------------------------------------------------------------*/
  606. /*
  607. * Function irnet_daddr_to_dname (self)
  608. *
  609. * Convert an IrDA address to a IrDA nickname
  610. *
  611. * It basically look into the discovery log until there is a match.
  612. */
  613. static inline int
  614. irnet_daddr_to_dname(irnet_socket * self)
  615. {
  616. struct irda_device_info *discoveries; /* Copy of the discovery log */
  617. int number; /* Number of nodes in the log */
  618. int i;
  619. DENTER(IRDA_SERV_TRACE, "(self=0x%p)\n", self);
  620. /* Ask lmp for the current discovery log */
  621. discoveries = irlmp_get_discoveries(&number, 0xffff,
  622. DISCOVERY_DEFAULT_SLOTS);
  623. /* Check if the we got some results */
  624. if (discoveries == NULL)
  625. DRETURN(-ENETUNREACH, IRDA_SERV_INFO, "Cachelog empty...\n");
  626. /* Now, check all discovered devices (if any) */
  627. for(i = 0; i < number; i++)
  628. {
  629. /* Does the name match ? */
  630. if(discoveries[i].daddr == self->daddr)
  631. {
  632. /* Yes !!! Get it.. */
  633. strlcpy(self->rname, discoveries[i].info, sizeof(self->rname));
  634. self->rname[sizeof(self->rname) - 1] = '\0';
  635. DEBUG(IRDA_SERV_INFO, "Device 0x%08x is in fact ``%s''.\n",
  636. self->daddr, self->rname);
  637. kfree(discoveries);
  638. DEXIT(IRDA_SERV_TRACE, "\n");
  639. return 0;
  640. }
  641. }
  642. /* No luck ! */
  643. DEXIT(IRDA_SERV_INFO, ": cannot discover device 0x%08x !!!\n", self->daddr);
  644. kfree(discoveries);
  645. return -EADDRNOTAVAIL;
  646. }
  647. /*------------------------------------------------------------------*/
  648. /*
  649. * Function irda_find_socket (self)
  650. *
  651. * Find the correct IrNET socket
  652. *
  653. * Look into the list of IrNET sockets and finds one with the right
  654. * properties...
  655. */
  656. static inline irnet_socket *
  657. irnet_find_socket(irnet_socket * self)
  658. {
  659. irnet_socket * new = (irnet_socket *) NULL;
  660. int err;
  661. DENTER(IRDA_SERV_TRACE, "(self=0x%p)\n", self);
  662. /* Get the addresses of the requester */
  663. self->daddr = irttp_get_daddr(self->tsap);
  664. self->saddr = irttp_get_saddr(self->tsap);
  665. /* Try to get the IrDA nickname of the requester */
  666. err = irnet_daddr_to_dname(self);
  667. /* Protect access to the instance list */
  668. spin_lock_bh(&irnet_server.spinlock);
  669. /* So now, try to get an socket having specifically
  670. * requested that nickname */
  671. if(err == 0)
  672. {
  673. new = (irnet_socket *) hashbin_find(irnet_server.list,
  674. 0, self->rname);
  675. if(new)
  676. DEBUG(IRDA_SERV_INFO, "Socket 0x%p matches rname ``%s''.\n",
  677. new, new->rname);
  678. }
  679. /* If no name matches, try to find an socket by the destination address */
  680. /* It can be either the requested destination address (set via the
  681. * control channel), or the current destination address if the
  682. * socket is in the middle of a connection request */
  683. if(new == (irnet_socket *) NULL)
  684. {
  685. new = (irnet_socket *) hashbin_get_first(irnet_server.list);
  686. while(new !=(irnet_socket *) NULL)
  687. {
  688. /* Does it have the same address ? */
  689. if((new->rdaddr == self->daddr) || (new->daddr == self->daddr))
  690. {
  691. /* Yes !!! Get it.. */
  692. DEBUG(IRDA_SERV_INFO, "Socket 0x%p matches daddr %#08x.\n",
  693. new, self->daddr);
  694. break;
  695. }
  696. new = (irnet_socket *) hashbin_get_next(irnet_server.list);
  697. }
  698. }
  699. /* If we don't have any socket, get the first unconnected socket */
  700. if(new == (irnet_socket *) NULL)
  701. {
  702. new = (irnet_socket *) hashbin_get_first(irnet_server.list);
  703. while(new !=(irnet_socket *) NULL)
  704. {
  705. /* Is it available ? */
  706. if(!(test_bit(0, &new->ttp_open)) && (new->rdaddr == DEV_ADDR_ANY) &&
  707. (new->rname[0] == '\0') && (new->ppp_open))
  708. {
  709. /* Yes !!! Get it.. */
  710. DEBUG(IRDA_SERV_INFO, "Socket 0x%p is free.\n",
  711. new);
  712. break;
  713. }
  714. new = (irnet_socket *) hashbin_get_next(irnet_server.list);
  715. }
  716. }
  717. /* Spin lock end */
  718. spin_unlock_bh(&irnet_server.spinlock);
  719. DEXIT(IRDA_SERV_TRACE, " - new = 0x%p\n", new);
  720. return new;
  721. }
  722. /*------------------------------------------------------------------*/
  723. /*
  724. * Function irda_connect_socket (self)
  725. *
  726. * Connect an incoming connection to the socket
  727. *
  728. */
  729. static inline int
  730. irnet_connect_socket(irnet_socket * server,
  731. irnet_socket * new,
  732. struct qos_info * qos,
  733. __u32 max_sdu_size,
  734. __u8 max_header_size)
  735. {
  736. DENTER(IRDA_SERV_TRACE, "(server=0x%p, new=0x%p)\n",
  737. server, new);
  738. /* Now attach up the new socket */
  739. new->tsap = irttp_dup(server->tsap, new);
  740. DABORT(new->tsap == NULL, -1, IRDA_SERV_ERROR, "dup failed!\n");
  741. /* Set up all the relevant parameters on the new socket */
  742. new->stsap_sel = new->tsap->stsap_sel;
  743. new->dtsap_sel = new->tsap->dtsap_sel;
  744. new->saddr = irttp_get_saddr(new->tsap);
  745. new->daddr = irttp_get_daddr(new->tsap);
  746. new->max_header_size = max_header_size;
  747. new->max_sdu_size_tx = max_sdu_size;
  748. new->max_data_size = max_sdu_size;
  749. #ifdef STREAM_COMPAT
  750. /* If we want to receive "stream sockets" */
  751. if(max_sdu_size == 0)
  752. new->max_data_size = irttp_get_max_seg_size(new->tsap);
  753. #endif /* STREAM_COMPAT */
  754. /* Clean up the original one to keep it in listen state */
  755. irttp_listen(server->tsap);
  756. /* Send a connection response on the new socket */
  757. irttp_connect_response(new->tsap, new->max_sdu_size_rx, NULL);
  758. /* Allow PPP to send its junk over the new socket... */
  759. set_bit(0, &new->ttp_open);
  760. /* Not connecting anymore, and clean up last possible remains
  761. * of connection attempts on the socket */
  762. clear_bit(0, &new->ttp_connect);
  763. if(new->iriap)
  764. {
  765. iriap_close(new->iriap);
  766. new->iriap = NULL;
  767. }
  768. if(new->discoveries != NULL)
  769. {
  770. kfree(new->discoveries);
  771. new->discoveries = NULL;
  772. }
  773. #ifdef CONNECT_INDIC_KICK
  774. /* As currently we don't block packets in ppp_irnet_send() while passive,
  775. * this is not really needed...
  776. * Also, not doing it give IrDA a chance to finish the setup properly
  777. * before being swamped with packets... */
  778. ppp_output_wakeup(&new->chan);
  779. #endif /* CONNECT_INDIC_KICK */
  780. /* Notify the control channel */
  781. irnet_post_event(new, IRNET_CONNECT_FROM,
  782. new->saddr, new->daddr, server->rname, 0);
  783. DEXIT(IRDA_SERV_TRACE, "\n");
  784. return 0;
  785. }
  786. /*------------------------------------------------------------------*/
  787. /*
  788. * Function irda_disconnect_server (self)
  789. *
  790. * Cleanup the server socket when the incoming connection abort
  791. *
  792. */
  793. static inline void
  794. irnet_disconnect_server(irnet_socket * self,
  795. struct sk_buff *skb)
  796. {
  797. DENTER(IRDA_SERV_TRACE, "(self=0x%p)\n", self);
  798. /* Put the received packet in the black hole */
  799. kfree_skb(skb);
  800. #ifdef FAIL_SEND_DISCONNECT
  801. /* Tell the other party we don't want to be connected */
  802. /* Hum... Is it the right thing to do ? And do we need to send
  803. * a connect response before ? It looks ok without this... */
  804. irttp_disconnect_request(self->tsap, NULL, P_NORMAL);
  805. #endif /* FAIL_SEND_DISCONNECT */
  806. /* Notify the control channel (see irnet_find_socket()) */
  807. irnet_post_event(NULL, IRNET_REQUEST_FROM,
  808. self->saddr, self->daddr, self->rname, 0);
  809. /* Clean up the server to keep it in listen state */
  810. irttp_listen(self->tsap);
  811. DEXIT(IRDA_SERV_TRACE, "\n");
  812. }
  813. /*------------------------------------------------------------------*/
  814. /*
  815. * Function irda_setup_server (self)
  816. *
  817. * Create a IrTTP server and set it up...
  818. *
  819. * Register the IrLAN hint bit, create a IrTTP instance for us,
  820. * set all the IrTTP callbacks and create an IrIAS entry...
  821. */
  822. static inline int
  823. irnet_setup_server(void)
  824. {
  825. __u16 hints;
  826. DENTER(IRDA_SERV_TRACE, "()\n");
  827. /* Initialise the regular socket part of the server */
  828. irda_irnet_create(&irnet_server.s);
  829. /* Open a local TSAP (an IrTTP instance) for the server */
  830. irnet_open_tsap(&irnet_server.s);
  831. /* PPP part setup */
  832. irnet_server.s.ppp_open = 0;
  833. irnet_server.s.chan.private = NULL;
  834. irnet_server.s.file = NULL;
  835. /* Get the hint bit corresponding to IrLAN */
  836. /* Note : we overload the IrLAN hint bit. As it is only a "hint", and as
  837. * we provide roughly the same functionality as IrLAN, this is ok.
  838. * In fact, the situation is similar as JetSend overloading the Obex hint
  839. */
  840. hints = irlmp_service_to_hint(S_LAN);
  841. #ifdef ADVERTISE_HINT
  842. /* Register with IrLMP as a service (advertise our hint bit) */
  843. irnet_server.skey = irlmp_register_service(hints);
  844. #endif /* ADVERTISE_HINT */
  845. /* Register with LM-IAS (so that people can connect to us) */
  846. irnet_server.ias_obj = irias_new_object(IRNET_SERVICE_NAME, jiffies);
  847. irias_add_integer_attrib(irnet_server.ias_obj, IRNET_IAS_VALUE,
  848. irnet_server.s.stsap_sel, IAS_KERNEL_ATTR);
  849. irias_insert_object(irnet_server.ias_obj);
  850. #ifdef DISCOVERY_EVENTS
  851. /* Tell IrLMP we want to be notified of newly discovered nodes */
  852. irlmp_update_client(irnet_server.s.ckey, hints,
  853. irnet_discovery_indication, irnet_expiry_indication,
  854. (void *) &irnet_server.s);
  855. #endif
  856. DEXIT(IRDA_SERV_TRACE, " - self=0x%p\n", &irnet_server.s);
  857. return 0;
  858. }
  859. /*------------------------------------------------------------------*/
  860. /*
  861. * Function irda_destroy_server (self)
  862. *
  863. * Destroy the IrTTP server...
  864. *
  865. * Reverse of the previous function...
  866. */
  867. static inline void
  868. irnet_destroy_server(void)
  869. {
  870. DENTER(IRDA_SERV_TRACE, "()\n");
  871. #ifdef ADVERTISE_HINT
  872. /* Unregister with IrLMP */
  873. irlmp_unregister_service(irnet_server.skey);
  874. #endif /* ADVERTISE_HINT */
  875. /* Unregister with LM-IAS */
  876. if(irnet_server.ias_obj)
  877. irias_delete_object(irnet_server.ias_obj);
  878. /* Cleanup the socket part */
  879. irda_irnet_destroy(&irnet_server.s);
  880. DEXIT(IRDA_SERV_TRACE, "\n");
  881. }
  882. /************************ IRDA-TTP CALLBACKS ************************/
  883. /*
  884. * When we create a IrTTP instance, we pass to it a set of callbacks
  885. * that IrTTP will call in case of various events.
  886. * We take care of those events here.
  887. */
  888. /*------------------------------------------------------------------*/
  889. /*
  890. * Function irnet_data_indication (instance, sap, skb)
  891. *
  892. * Received some data from TinyTP. Just queue it on the receive queue
  893. *
  894. */
  895. static int
  896. irnet_data_indication(void * instance,
  897. void * sap,
  898. struct sk_buff *skb)
  899. {
  900. irnet_socket * ap = (irnet_socket *) instance;
  901. unsigned char * p;
  902. int code = 0;
  903. DENTER(IRDA_TCB_TRACE, "(self/ap=0x%p, skb=0x%p)\n",
  904. ap, skb);
  905. DASSERT(skb != NULL, 0, IRDA_CB_ERROR, "skb is NULL !!!\n");
  906. /* Check is ppp is ready to receive our packet */
  907. if(!ap->ppp_open)
  908. {
  909. DERROR(IRDA_CB_ERROR, "PPP not ready, dropping packet...\n");
  910. /* When we return error, TTP will need to requeue the skb and
  911. * will stop the sender. IrTTP will stall until we send it a
  912. * flow control request... */
  913. return -ENOMEM;
  914. }
  915. /* strip address/control field if present */
  916. p = skb->data;
  917. if((p[0] == PPP_ALLSTATIONS) && (p[1] == PPP_UI))
  918. {
  919. /* chop off address/control */
  920. if(skb->len < 3)
  921. goto err_exit;
  922. p = skb_pull(skb, 2);
  923. }
  924. /* decompress protocol field if compressed */
  925. if(p[0] & 1)
  926. {
  927. /* protocol is compressed */
  928. skb_push(skb, 1)[0] = 0;
  929. }
  930. else
  931. if(skb->len < 2)
  932. goto err_exit;
  933. /* pass to generic ppp layer */
  934. /* Note : how do I know if ppp can accept or not the packet ? This is
  935. * essential if I want to manage flow control smoothly... */
  936. ppp_input(&ap->chan, skb);
  937. DEXIT(IRDA_TCB_TRACE, "\n");
  938. return 0;
  939. err_exit:
  940. DERROR(IRDA_CB_ERROR, "Packet too small, dropping...\n");
  941. kfree_skb(skb);
  942. ppp_input_error(&ap->chan, code);
  943. return 0; /* Don't return an error code, only for flow control... */
  944. }
  945. /*------------------------------------------------------------------*/
  946. /*
  947. * Function irnet_disconnect_indication (instance, sap, reason, skb)
  948. *
  949. * Connection has been closed. Chech reason to find out why
  950. *
  951. * Note : there are many cases where we come here :
  952. * o attempted to connect, timeout
  953. * o connected, link is broken, LAP has timeout
  954. * o connected, other side close the link
  955. * o connection request on the server not handled
  956. */
  957. static void
  958. irnet_disconnect_indication(void * instance,
  959. void * sap,
  960. LM_REASON reason,
  961. struct sk_buff *skb)
  962. {
  963. irnet_socket * self = (irnet_socket *) instance;
  964. int test_open;
  965. int test_connect;
  966. DENTER(IRDA_TCB_TRACE, "(self=0x%p)\n", self);
  967. DASSERT(self != NULL, , IRDA_CB_ERROR, "Self is NULL !!!\n");
  968. /* Don't care about it, but let's not leak it */
  969. if(skb)
  970. dev_kfree_skb(skb);
  971. /* Prevent higher layer from accessing IrTTP */
  972. test_open = test_and_clear_bit(0, &self->ttp_open);
  973. /* Not connecting anymore...
  974. * (note : TSAP is open, so IAP callbacks are no longer pending...) */
  975. test_connect = test_and_clear_bit(0, &self->ttp_connect);
  976. /* If both self->ttp_open and self->ttp_connect are NULL, it mean that we
  977. * have a race condition with irda_irnet_destroy() or
  978. * irnet_connect_indication(), so don't mess up tsap...
  979. */
  980. if(!(test_open || test_connect))
  981. {
  982. DERROR(IRDA_CB_ERROR, "Race condition detected...\n");
  983. return;
  984. }
  985. /* If we were active, notify the control channel */
  986. if(test_open)
  987. irnet_post_event(self, IRNET_DISCONNECT_FROM,
  988. self->saddr, self->daddr, self->rname, 0);
  989. else
  990. /* If we were trying to connect, notify the control channel */
  991. if((self->tsap) && (self != &irnet_server.s))
  992. irnet_post_event(self, IRNET_NOANSWER_FROM,
  993. self->saddr, self->daddr, self->rname, 0);
  994. /* Close our IrTTP connection, cleanup tsap */
  995. if((self->tsap) && (self != &irnet_server.s))
  996. {
  997. DEBUG(IRDA_CB_INFO, "Closing our TTP connection.\n");
  998. irttp_close_tsap(self->tsap);
  999. self->tsap = NULL;
  1000. }
  1001. /* Cleanup the socket in case we want to reconnect in ppp_output_wakeup() */
  1002. self->stsap_sel = 0;
  1003. self->daddr = DEV_ADDR_ANY;
  1004. self->tx_flow = FLOW_START;
  1005. /* Deal with the ppp instance if it's still alive */
  1006. if(self->ppp_open)
  1007. {
  1008. if(test_open)
  1009. {
  1010. /* ppp_unregister_channel() wants a user context. */
  1011. schedule_work(&self->disconnect_work);
  1012. }
  1013. else
  1014. {
  1015. /* If we were trying to connect, flush (drain) ppp_generic
  1016. * Tx queue (most often we have blocked it), which will
  1017. * trigger an other attempt to connect. If we are passive,
  1018. * this will empty the Tx queue after last try. */
  1019. ppp_output_wakeup(&self->chan);
  1020. }
  1021. }
  1022. DEXIT(IRDA_TCB_TRACE, "\n");
  1023. }
  1024. /*------------------------------------------------------------------*/
  1025. /*
  1026. * Function irnet_connect_confirm (instance, sap, qos, max_sdu_size, skb)
  1027. *
  1028. * Connections has been confirmed by the remote device
  1029. *
  1030. */
  1031. static void
  1032. irnet_connect_confirm(void * instance,
  1033. void * sap,
  1034. struct qos_info *qos,
  1035. __u32 max_sdu_size,
  1036. __u8 max_header_size,
  1037. struct sk_buff *skb)
  1038. {
  1039. irnet_socket * self = (irnet_socket *) instance;
  1040. DENTER(IRDA_TCB_TRACE, "(self=0x%p)\n", self);
  1041. /* Check if socket is closing down (via irda_irnet_destroy()) */
  1042. if(! test_bit(0, &self->ttp_connect))
  1043. {
  1044. DERROR(IRDA_CB_ERROR, "Socket no longer connecting. Ouch !\n");
  1045. return;
  1046. }
  1047. /* How much header space do we need to reserve */
  1048. self->max_header_size = max_header_size;
  1049. /* IrTTP max SDU size in transmit direction */
  1050. self->max_sdu_size_tx = max_sdu_size;
  1051. self->max_data_size = max_sdu_size;
  1052. #ifdef STREAM_COMPAT
  1053. if(max_sdu_size == 0)
  1054. self->max_data_size = irttp_get_max_seg_size(self->tsap);
  1055. #endif /* STREAM_COMPAT */
  1056. /* At this point, IrLMP has assigned our source address */
  1057. self->saddr = irttp_get_saddr(self->tsap);
  1058. /* Allow higher layer to access IrTTP */
  1059. set_bit(0, &self->ttp_open);
  1060. clear_bit(0, &self->ttp_connect); /* Not racy, IrDA traffic is serial */
  1061. /* Give a kick in the ass of ppp_generic so that he sends us some data */
  1062. ppp_output_wakeup(&self->chan);
  1063. /* Check size of received packet */
  1064. if(skb->len > 0)
  1065. {
  1066. #ifdef PASS_CONNECT_PACKETS
  1067. DEBUG(IRDA_CB_INFO, "Passing connect packet to PPP.\n");
  1068. /* Try to pass it to PPP */
  1069. irnet_data_indication(instance, sap, skb);
  1070. #else /* PASS_CONNECT_PACKETS */
  1071. DERROR(IRDA_CB_ERROR, "Dropping non empty packet.\n");
  1072. kfree_skb(skb); /* Note : will be optimised with other kfree... */
  1073. #endif /* PASS_CONNECT_PACKETS */
  1074. }
  1075. else
  1076. kfree_skb(skb);
  1077. /* Notify the control channel */
  1078. irnet_post_event(self, IRNET_CONNECT_TO,
  1079. self->saddr, self->daddr, self->rname, 0);
  1080. DEXIT(IRDA_TCB_TRACE, "\n");
  1081. }
  1082. /*------------------------------------------------------------------*/
  1083. /*
  1084. * Function irnet_flow_indication (instance, sap, flow)
  1085. *
  1086. * Used by TinyTP to tell us if it can accept more data or not
  1087. *
  1088. */
  1089. static void
  1090. irnet_flow_indication(void * instance,
  1091. void * sap,
  1092. LOCAL_FLOW flow)
  1093. {
  1094. irnet_socket * self = (irnet_socket *) instance;
  1095. LOCAL_FLOW oldflow = self->tx_flow;
  1096. DENTER(IRDA_TCB_TRACE, "(self=0x%p, flow=%d)\n", self, flow);
  1097. /* Update our state */
  1098. self->tx_flow = flow;
  1099. /* Check what IrTTP want us to do... */
  1100. switch(flow)
  1101. {
  1102. case FLOW_START:
  1103. DEBUG(IRDA_CB_INFO, "IrTTP wants us to start again\n");
  1104. /* Check if we really need to wake up PPP */
  1105. if(oldflow == FLOW_STOP)
  1106. ppp_output_wakeup(&self->chan);
  1107. else
  1108. DEBUG(IRDA_CB_INFO, "But we were already transmitting !!!\n");
  1109. break;
  1110. case FLOW_STOP:
  1111. DEBUG(IRDA_CB_INFO, "IrTTP wants us to slow down\n");
  1112. break;
  1113. default:
  1114. DEBUG(IRDA_CB_INFO, "Unknown flow command!\n");
  1115. break;
  1116. }
  1117. DEXIT(IRDA_TCB_TRACE, "\n");
  1118. }
  1119. /*------------------------------------------------------------------*/
  1120. /*
  1121. * Function irnet_status_indication (instance, sap, reason, skb)
  1122. *
  1123. * Link (IrLAP) status report.
  1124. *
  1125. */
  1126. static void
  1127. irnet_status_indication(void * instance,
  1128. LINK_STATUS link,
  1129. LOCK_STATUS lock)
  1130. {
  1131. irnet_socket * self = (irnet_socket *) instance;
  1132. DENTER(IRDA_TCB_TRACE, "(self=0x%p)\n", self);
  1133. DASSERT(self != NULL, , IRDA_CB_ERROR, "Self is NULL !!!\n");
  1134. /* We can only get this event if we are connected */
  1135. switch(link)
  1136. {
  1137. case STATUS_NO_ACTIVITY:
  1138. irnet_post_event(self, IRNET_BLOCKED_LINK,
  1139. self->saddr, self->daddr, self->rname, 0);
  1140. break;
  1141. default:
  1142. DEBUG(IRDA_CB_INFO, "Unknown status...\n");
  1143. }
  1144. DEXIT(IRDA_TCB_TRACE, "\n");
  1145. }
  1146. /*------------------------------------------------------------------*/
  1147. /*
  1148. * Function irnet_connect_indication(instance, sap, qos, max_sdu_size, userdata)
  1149. *
  1150. * Incoming connection
  1151. *
  1152. * In theory, this function is called only on the server socket.
  1153. * Some other node is attempting to connect to the IrNET service, and has
  1154. * sent a connection request on our server socket.
  1155. * We just redirect the connection to the relevant IrNET socket.
  1156. *
  1157. * Note : we also make sure that between 2 irnet nodes, there can
  1158. * exist only one irnet connection.
  1159. */
  1160. static void
  1161. irnet_connect_indication(void * instance,
  1162. void * sap,
  1163. struct qos_info *qos,
  1164. __u32 max_sdu_size,
  1165. __u8 max_header_size,
  1166. struct sk_buff *skb)
  1167. {
  1168. irnet_socket * server = &irnet_server.s;
  1169. irnet_socket * new = (irnet_socket *) NULL;
  1170. DENTER(IRDA_TCB_TRACE, "(server=0x%p)\n", server);
  1171. DASSERT(instance == &irnet_server, , IRDA_CB_ERROR,
  1172. "Invalid instance (0x%p) !!!\n", instance);
  1173. DASSERT(sap == irnet_server.s.tsap, , IRDA_CB_ERROR, "Invalid sap !!!\n");
  1174. /* Try to find the most appropriate IrNET socket */
  1175. new = irnet_find_socket(server);
  1176. /* After all this hard work, do we have an socket ? */
  1177. if(new == (irnet_socket *) NULL)
  1178. {
  1179. DEXIT(IRDA_CB_INFO, ": No socket waiting for this connection.\n");
  1180. irnet_disconnect_server(server, skb);
  1181. return;
  1182. }
  1183. /* Is the socket already busy ? */
  1184. if(test_bit(0, &new->ttp_open))
  1185. {
  1186. DEXIT(IRDA_CB_INFO, ": Socket already connected.\n");
  1187. irnet_disconnect_server(server, skb);
  1188. return;
  1189. }
  1190. /* The following code is a bit tricky, so need comments ;-)
  1191. */
  1192. /* If ttp_connect is set, the socket is trying to connect to the other
  1193. * end and may have sent a IrTTP connection request and is waiting for
  1194. * a connection response (that may never come).
  1195. * Now, the pain is that the socket may have opened a tsap and is
  1196. * waiting on it, while the other end is trying to connect to it on
  1197. * another tsap.
  1198. * Because IrNET can be peer to peer, we need to workaround this.
  1199. * Furthermore, the way the irnetd script is implemented, the
  1200. * target will create a second IrNET connection back to the
  1201. * originator and expect the originator to bind this new connection
  1202. * to the original PPPD instance.
  1203. * And of course, if we don't use irnetd, we can have a race when
  1204. * both side try to connect simultaneously, which could leave both
  1205. * connections half closed (yuck).
  1206. * Conclusions :
  1207. * 1) The "originator" must accept the new connection and get rid
  1208. * of the old one so that irnetd works
  1209. * 2) One side must deny the new connection to avoid races,
  1210. * but both side must agree on which side it is...
  1211. * Most often, the originator is primary at the LAP layer.
  1212. * Jean II
  1213. */
  1214. /* Now, let's look at the way I wrote the test...
  1215. * We need to clear up the ttp_connect flag atomically to prevent
  1216. * irnet_disconnect_indication() to mess up the tsap we are going to close.
  1217. * We want to clear the ttp_connect flag only if we close the tsap,
  1218. * otherwise we will never close it, so we need to check for primary
  1219. * *before* doing the test on the flag.
  1220. * And of course, ALLOW_SIMULT_CONNECT can disable this entirely...
  1221. * Jean II
  1222. */
  1223. /* Socket already connecting ? On primary ? */
  1224. if(0
  1225. #ifdef ALLOW_SIMULT_CONNECT
  1226. || ((irttp_is_primary(server->tsap) == 1) && /* primary */
  1227. (test_and_clear_bit(0, &new->ttp_connect)))
  1228. #endif /* ALLOW_SIMULT_CONNECT */
  1229. )
  1230. {
  1231. DERROR(IRDA_CB_ERROR, "Socket already connecting, but going to reuse it !\n");
  1232. /* Cleanup the old TSAP if necessary - IrIAP will be cleaned up later */
  1233. if(new->tsap != NULL)
  1234. {
  1235. /* Close the old connection the new socket was attempting,
  1236. * so that we can hook it up to the new connection.
  1237. * It's now safe to do it... */
  1238. irttp_close_tsap(new->tsap);
  1239. new->tsap = NULL;
  1240. }
  1241. }
  1242. else
  1243. {
  1244. /* Three options :
  1245. * 1) socket was not connecting or connected : ttp_connect should be 0.
  1246. * 2) we don't want to connect the socket because we are secondary or
  1247. * ALLOW_SIMULT_CONNECT is undefined. ttp_connect should be 1.
  1248. * 3) we are half way in irnet_disconnect_indication(), and it's a
  1249. * nice race condition... Fortunately, we can detect that by checking
  1250. * if tsap is still alive. On the other hand, we can't be in
  1251. * irda_irnet_destroy() otherwise we would not have found this
  1252. * socket in the hashbin.
  1253. * Jean II */
  1254. if((test_bit(0, &new->ttp_connect)) || (new->tsap != NULL))
  1255. {
  1256. /* Don't mess this socket, somebody else in in charge... */
  1257. DERROR(IRDA_CB_ERROR, "Race condition detected, socket in use, abort connect...\n");
  1258. irnet_disconnect_server(server, skb);
  1259. return;
  1260. }
  1261. }
  1262. /* So : at this point, we have a socket, and it is idle. Good ! */
  1263. irnet_connect_socket(server, new, qos, max_sdu_size, max_header_size);
  1264. /* Check size of received packet */
  1265. if(skb->len > 0)
  1266. {
  1267. #ifdef PASS_CONNECT_PACKETS
  1268. DEBUG(IRDA_CB_INFO, "Passing connect packet to PPP.\n");
  1269. /* Try to pass it to PPP */
  1270. irnet_data_indication(new, new->tsap, skb);
  1271. #else /* PASS_CONNECT_PACKETS */
  1272. DERROR(IRDA_CB_ERROR, "Dropping non empty packet.\n");
  1273. kfree_skb(skb); /* Note : will be optimised with other kfree... */
  1274. #endif /* PASS_CONNECT_PACKETS */
  1275. }
  1276. else
  1277. kfree_skb(skb);
  1278. DEXIT(IRDA_TCB_TRACE, "\n");
  1279. }
  1280. /********************** IRDA-IAS/LMP CALLBACKS **********************/
  1281. /*
  1282. * These are the callbacks called by other layers of the IrDA stack,
  1283. * mainly LMP for discovery and IAS for name queries.
  1284. */
  1285. /*------------------------------------------------------------------*/
  1286. /*
  1287. * Function irnet_getvalue_confirm (result, obj_id, value, priv)
  1288. *
  1289. * Got answer from remote LM-IAS, just connect
  1290. *
  1291. * This is the reply to a IAS query we were doing to find the TSAP of
  1292. * the device we want to connect to.
  1293. * If we have found a valid TSAP, just initiate the TTP connection
  1294. * on this TSAP.
  1295. */
  1296. static void
  1297. irnet_getvalue_confirm(int result,
  1298. __u16 obj_id,
  1299. struct ias_value *value,
  1300. void * priv)
  1301. {
  1302. irnet_socket * self = (irnet_socket *) priv;
  1303. DENTER(IRDA_OCB_TRACE, "(self=0x%p)\n", self);
  1304. DASSERT(self != NULL, , IRDA_OCB_ERROR, "Self is NULL !!!\n");
  1305. /* Check if already connected (via irnet_connect_socket())
  1306. * or socket is closing down (via irda_irnet_destroy()) */
  1307. if(! test_bit(0, &self->ttp_connect))
  1308. {
  1309. DERROR(IRDA_OCB_ERROR, "Socket no longer connecting. Ouch !\n");
  1310. return;
  1311. }
  1312. /* We probably don't need to make any more queries */
  1313. iriap_close(self->iriap);
  1314. self->iriap = NULL;
  1315. /* Post process the IAS reply */
  1316. self->dtsap_sel = irnet_ias_to_tsap(self, result, value);
  1317. /* If error, just go out */
  1318. if(self->errno)
  1319. {
  1320. clear_bit(0, &self->ttp_connect);
  1321. DERROR(IRDA_OCB_ERROR, "IAS connect failed ! (0x%X)\n", self->errno);
  1322. return;
  1323. }
  1324. DEBUG(IRDA_OCB_INFO, "daddr = %08x, lsap = %d, starting IrTTP connection\n",
  1325. self->daddr, self->dtsap_sel);
  1326. /* Start up TTP - non blocking */
  1327. irnet_connect_tsap(self);
  1328. DEXIT(IRDA_OCB_TRACE, "\n");
  1329. }
  1330. /*------------------------------------------------------------------*/
  1331. /*
  1332. * Function irnet_discovervalue_confirm (result, obj_id, value, priv)
  1333. *
  1334. * Handle the TSAP discovery procedure state machine.
  1335. * Got answer from remote LM-IAS, try next device
  1336. *
  1337. * We are doing a TSAP discovery procedure, and we got an answer to
  1338. * a IAS query we were doing to find the TSAP on one of the address
  1339. * in the discovery log.
  1340. *
  1341. * If we have found a valid TSAP for the first time, save it. If it's
  1342. * not the first time we found one, complain.
  1343. *
  1344. * If we have more addresses in the log, just initiate a new query.
  1345. * Note that those query may fail (see irnet_discover_daddr_and_lsap_sel())
  1346. *
  1347. * Otherwise, wrap up the procedure (cleanup), check if we have found
  1348. * any device and connect to it.
  1349. */
  1350. static void
  1351. irnet_discovervalue_confirm(int result,
  1352. __u16 obj_id,
  1353. struct ias_value *value,
  1354. void * priv)
  1355. {
  1356. irnet_socket * self = (irnet_socket *) priv;
  1357. __u8 dtsap_sel; /* TSAP we are looking for */
  1358. DENTER(IRDA_OCB_TRACE, "(self=0x%p)\n", self);
  1359. DASSERT(self != NULL, , IRDA_OCB_ERROR, "Self is NULL !!!\n");
  1360. /* Check if already connected (via irnet_connect_socket())
  1361. * or socket is closing down (via irda_irnet_destroy()) */
  1362. if(! test_bit(0, &self->ttp_connect))
  1363. {
  1364. DERROR(IRDA_OCB_ERROR, "Socket no longer connecting. Ouch !\n");
  1365. return;
  1366. }
  1367. /* Post process the IAS reply */
  1368. dtsap_sel = irnet_ias_to_tsap(self, result, value);
  1369. /* Have we got something ? */
  1370. if(self->errno == 0)
  1371. {
  1372. /* We found the requested service */
  1373. if(self->daddr != DEV_ADDR_ANY)
  1374. {
  1375. DERROR(IRDA_OCB_ERROR, "More than one device in range supports IrNET...\n");
  1376. }
  1377. else
  1378. {
  1379. /* First time we found that one, save it ! */
  1380. self->daddr = self->discoveries[self->disco_index].daddr;
  1381. self->dtsap_sel = dtsap_sel;
  1382. }
  1383. }
  1384. /* If no failure */
  1385. if((self->errno == -EADDRNOTAVAIL) || (self->errno == 0))
  1386. {
  1387. int ret;
  1388. /* Search the next node */
  1389. ret = irnet_discover_next_daddr(self);
  1390. if(!ret)
  1391. {
  1392. /* In this case, the above request was non-blocking.
  1393. * We will return here after a while... */
  1394. return;
  1395. }
  1396. /* In this case, we have processed the last discovery item */
  1397. }
  1398. /* No more queries to be done (failure or last one) */
  1399. /* We probably don't need to make any more queries */
  1400. iriap_close(self->iriap);
  1401. self->iriap = NULL;
  1402. /* No more items : remove the log and signal termination */
  1403. DEBUG(IRDA_OCB_INFO, "Cleaning up log (0x%p)\n",
  1404. self->discoveries);
  1405. if(self->discoveries != NULL)
  1406. {
  1407. /* Cleanup our copy of the discovery log */
  1408. kfree(self->discoveries);
  1409. self->discoveries = NULL;
  1410. }
  1411. self->disco_number = -1;
  1412. /* Check out what we found */
  1413. if(self->daddr == DEV_ADDR_ANY)
  1414. {
  1415. self->daddr = DEV_ADDR_ANY;
  1416. clear_bit(0, &self->ttp_connect);
  1417. DEXIT(IRDA_OCB_TRACE, ": cannot discover IrNET in any device !!!\n");
  1418. return;
  1419. }
  1420. /* We have a valid address - just connect */
  1421. DEBUG(IRDA_OCB_INFO, "daddr = %08x, lsap = %d, starting IrTTP connection\n",
  1422. self->daddr, self->dtsap_sel);
  1423. /* Start up TTP - non blocking */
  1424. irnet_connect_tsap(self);
  1425. DEXIT(IRDA_OCB_TRACE, "\n");
  1426. }
  1427. #ifdef DISCOVERY_EVENTS
  1428. /*------------------------------------------------------------------*/
  1429. /*
  1430. * Function irnet_discovery_indication (discovery)
  1431. *
  1432. * Got a discovery indication from IrLMP, post an event
  1433. *
  1434. * Note : IrLMP take care of matching the hint mask for us, and also
  1435. * check if it is a "new" node for us...
  1436. *
  1437. * As IrLMP filter on the IrLAN hint bit, we get both IrLAN and IrNET
  1438. * nodes, so it's only at connection time that we will know if the
  1439. * node support IrNET, IrLAN or both. The other solution is to check
  1440. * in IAS the PNP ids and service name.
  1441. * Note : even if a node support IrNET (or IrLAN), it's no guarantee
  1442. * that we will be able to connect to it, the node might already be
  1443. * busy...
  1444. *
  1445. * One last thing : in some case, this function will trigger duplicate
  1446. * discovery events. On the other hand, we should catch all
  1447. * discoveries properly (i.e. not miss one). Filtering duplicate here
  1448. * is to messy, so we leave that to user space...
  1449. */
  1450. static void
  1451. irnet_discovery_indication(discinfo_t * discovery,
  1452. DISCOVERY_MODE mode,
  1453. void * priv)
  1454. {
  1455. irnet_socket * self = &irnet_server.s;
  1456. DENTER(IRDA_OCB_TRACE, "(self=0x%p)\n", self);
  1457. DASSERT(priv == &irnet_server, , IRDA_OCB_ERROR,
  1458. "Invalid instance (0x%p) !!!\n", priv);
  1459. DEBUG(IRDA_OCB_INFO, "Discovered new IrNET/IrLAN node %s...\n",
  1460. discovery->info);
  1461. /* Notify the control channel */
  1462. irnet_post_event(NULL, IRNET_DISCOVER,
  1463. discovery->saddr, discovery->daddr, discovery->info,
  1464. get_unaligned((__u16 *)discovery->hints));
  1465. DEXIT(IRDA_OCB_TRACE, "\n");
  1466. }
  1467. /*------------------------------------------------------------------*/
  1468. /*
  1469. * Function irnet_expiry_indication (expiry)
  1470. *
  1471. * Got a expiry indication from IrLMP, post an event
  1472. *
  1473. * Note : IrLMP take care of matching the hint mask for us, we only
  1474. * check if it is a "new" node...
  1475. */
  1476. static void
  1477. irnet_expiry_indication(discinfo_t * expiry,
  1478. DISCOVERY_MODE mode,
  1479. void * priv)
  1480. {
  1481. irnet_socket * self = &irnet_server.s;
  1482. DENTER(IRDA_OCB_TRACE, "(self=0x%p)\n", self);
  1483. DASSERT(priv == &irnet_server, , IRDA_OCB_ERROR,
  1484. "Invalid instance (0x%p) !!!\n", priv);
  1485. DEBUG(IRDA_OCB_INFO, "IrNET/IrLAN node %s expired...\n",
  1486. expiry->info);
  1487. /* Notify the control channel */
  1488. irnet_post_event(NULL, IRNET_EXPIRE,
  1489. expiry->saddr, expiry->daddr, expiry->info,
  1490. get_unaligned((__u16 *)expiry->hints));
  1491. DEXIT(IRDA_OCB_TRACE, "\n");
  1492. }
  1493. #endif /* DISCOVERY_EVENTS */
  1494. /*********************** PROC ENTRY CALLBACKS ***********************/
  1495. /*
  1496. * We create a instance in the /proc filesystem, and here we take care
  1497. * of that...
  1498. */
  1499. #ifdef CONFIG_PROC_FS
  1500. static int
  1501. irnet_proc_show(struct seq_file *m, void *v)
  1502. {
  1503. irnet_socket * self;
  1504. char * state;
  1505. int i = 0;
  1506. /* Get the IrNET server information... */
  1507. seq_printf(m, "IrNET server - ");
  1508. seq_printf(m, "IrDA state: %s, ",
  1509. (irnet_server.running ? "running" : "dead"));
  1510. seq_printf(m, "stsap_sel: %02x, ", irnet_server.s.stsap_sel);
  1511. seq_printf(m, "dtsap_sel: %02x\n", irnet_server.s.dtsap_sel);
  1512. /* Do we need to continue ? */
  1513. if(!irnet_server.running)
  1514. return 0;
  1515. /* Protect access to the instance list */
  1516. spin_lock_bh(&irnet_server.spinlock);
  1517. /* Get the sockets one by one... */
  1518. self = (irnet_socket *) hashbin_get_first(irnet_server.list);
  1519. while(self != NULL)
  1520. {
  1521. /* Start printing info about the socket. */
  1522. seq_printf(m, "\nIrNET socket %d - ", i++);
  1523. /* First, get the requested configuration */
  1524. seq_printf(m, "Requested IrDA name: \"%s\", ", self->rname);
  1525. seq_printf(m, "daddr: %08x, ", self->rdaddr);
  1526. seq_printf(m, "saddr: %08x\n", self->rsaddr);
  1527. /* Second, get all the PPP info */
  1528. seq_printf(m, " PPP state: %s",
  1529. (self->ppp_open ? "registered" : "unregistered"));
  1530. if(self->ppp_open)
  1531. {
  1532. seq_printf(m, ", unit: ppp%d",
  1533. ppp_unit_number(&self->chan));
  1534. seq_printf(m, ", channel: %d",
  1535. ppp_channel_index(&self->chan));
  1536. seq_printf(m, ", mru: %d",
  1537. self->mru);
  1538. /* Maybe add self->flags ? Later... */
  1539. }
  1540. /* Then, get all the IrDA specific info... */
  1541. if(self->ttp_open)
  1542. state = "connected";
  1543. else
  1544. if(self->tsap != NULL)
  1545. state = "connecting";
  1546. else
  1547. if(self->iriap != NULL)
  1548. state = "searching";
  1549. else
  1550. if(self->ttp_connect)
  1551. state = "weird";
  1552. else
  1553. state = "idle";
  1554. seq_printf(m, "\n IrDA state: %s, ", state);
  1555. seq_printf(m, "daddr: %08x, ", self->daddr);
  1556. seq_printf(m, "stsap_sel: %02x, ", self->stsap_sel);
  1557. seq_printf(m, "dtsap_sel: %02x\n", self->dtsap_sel);
  1558. /* Next socket, please... */
  1559. self = (irnet_socket *) hashbin_get_next(irnet_server.list);
  1560. }
  1561. /* Spin lock end */
  1562. spin_unlock_bh(&irnet_server.spinlock);
  1563. return 0;
  1564. }
  1565. static int irnet_proc_open(struct inode *inode, struct file *file)
  1566. {
  1567. return single_open(file, irnet_proc_show, NULL);
  1568. }
  1569. static const struct file_operations irnet_proc_fops = {
  1570. .owner = THIS_MODULE,
  1571. .open = irnet_proc_open,
  1572. .read = seq_read,
  1573. .llseek = seq_lseek,
  1574. .release = single_release,
  1575. };
  1576. #endif /* PROC_FS */
  1577. /********************** CONFIGURATION/CLEANUP **********************/
  1578. /*
  1579. * Initialisation and teardown of the IrDA part, called at module
  1580. * insertion and removal...
  1581. */
  1582. /*------------------------------------------------------------------*/
  1583. /*
  1584. * Prepare the IrNET layer for operation...
  1585. */
  1586. int __init
  1587. irda_irnet_init(void)
  1588. {
  1589. int err = 0;
  1590. DENTER(MODULE_TRACE, "()\n");
  1591. /* Pure paranoia - should be redundant */
  1592. memset(&irnet_server, 0, sizeof(struct irnet_root));
  1593. /* Setup start of irnet instance list */
  1594. irnet_server.list = hashbin_new(HB_NOLOCK);
  1595. DABORT(irnet_server.list == NULL, -ENOMEM,
  1596. MODULE_ERROR, "Can't allocate hashbin!\n");
  1597. /* Init spinlock for instance list */
  1598. spin_lock_init(&irnet_server.spinlock);
  1599. /* Initialise control channel */
  1600. init_waitqueue_head(&irnet_events.rwait);
  1601. irnet_events.index = 0;
  1602. /* Init spinlock for event logging */
  1603. spin_lock_init(&irnet_events.spinlock);
  1604. #ifdef CONFIG_PROC_FS
  1605. /* Add a /proc file for irnet infos */
  1606. proc_create("irnet", 0, proc_irda, &irnet_proc_fops);
  1607. #endif /* CONFIG_PROC_FS */
  1608. /* Setup the IrNET server */
  1609. err = irnet_setup_server();
  1610. if(!err)
  1611. /* We are no longer functional... */
  1612. irnet_server.running = 1;
  1613. DEXIT(MODULE_TRACE, "\n");
  1614. return err;
  1615. }
  1616. /*------------------------------------------------------------------*/
  1617. /*
  1618. * Cleanup at exit...
  1619. */
  1620. void __exit
  1621. irda_irnet_cleanup(void)
  1622. {
  1623. DENTER(MODULE_TRACE, "()\n");
  1624. /* We are no longer there... */
  1625. irnet_server.running = 0;
  1626. #ifdef CONFIG_PROC_FS
  1627. /* Remove our /proc file */
  1628. remove_proc_entry("irnet", proc_irda);
  1629. #endif /* CONFIG_PROC_FS */
  1630. /* Remove our IrNET server from existence */
  1631. irnet_destroy_server();
  1632. /* Remove all instances of IrNET socket still present */
  1633. hashbin_delete(irnet_server.list, (FREE_FUNC) irda_irnet_destroy);
  1634. DEXIT(MODULE_TRACE, "\n");
  1635. }