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