irnet_irda.c 56 KB

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