irnet_irda.c 56 KB

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