2 * IrNET protocol module : Synchronous PPP over an IrDA socket.
4 * Jean II - HPL `00 - <jt@hpl.hp.com>
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.
11 #include "irnet_irda.h" /* Private header */
12 #include <linux/sched.h>
13 #include <linux/seq_file.h>
14 #include <linux/slab.h>
15 #include <asm/unaligned.h>
18 * PPP disconnect work: we need to make sure we're in
19 * process context when calling ppp_unregister_channel().
21 static void irnet_ppp_disconnect(struct work_struct *work)
24 container_of(work, irnet_socket, disconnect_work);
29 * If we were connected, cleanup & close the PPP
30 * channel, which will kill pppd (hangup) and the rest.
32 if (self->ppp_open && !self->ttp_open && !self->ttp_connect) {
33 ppp_unregister_channel(&self->chan);
38 /************************* CONTROL CHANNEL *************************/
40 * When ppp is not active, /dev/irnet act as a control channel.
41 * Writing allow to set up the IrDA destination of the IrNET channel,
42 * and any application may be read events happening on IrNET...
45 /*------------------------------------------------------------------*/
47 * Post an event to the control channel...
48 * Put the event in the log, and then wait all process blocked on read
49 * so they can read the log...
52 irnet_post_event(irnet_socket * ap,
59 int index; /* In the log */
61 DENTER(CTRL_TRACE, "(ap=0x%p, event=%d, daddr=%08x, name=``%s'')\n",
62 ap, event, daddr, name);
64 /* Protect this section via spinlock.
65 * Note : as we are the only event producer, we only need to exclude
66 * ourself when touching the log, which is nice and easy.
68 spin_lock_bh(&irnet_events.spinlock);
70 /* Copy the event in the log */
71 index = irnet_events.index;
72 irnet_events.log[index].event = event;
73 irnet_events.log[index].daddr = daddr;
74 irnet_events.log[index].saddr = saddr;
75 /* Try to copy IrDA nickname */
77 strcpy(irnet_events.log[index].name, name);
79 irnet_events.log[index].name[0] = '\0';
81 irnet_events.log[index].hints.word = hints;
82 /* Try to get ppp unit number */
83 if((ap != (irnet_socket *) NULL) && (ap->ppp_open))
84 irnet_events.log[index].unit = ppp_unit_number(&ap->chan);
86 irnet_events.log[index].unit = -1;
88 /* Increment the index
89 * Note that we increment the index only after the event is written,
90 * to make sure that the readers don't get garbage... */
91 irnet_events.index = (index + 1) % IRNET_MAX_EVENTS;
93 DEBUG(CTRL_INFO, "New event index is %d\n", irnet_events.index);
96 spin_unlock_bh(&irnet_events.spinlock);
98 /* Now : wake up everybody waiting for events... */
99 wake_up_interruptible_all(&irnet_events.rwait);
101 DEXIT(CTRL_TRACE, "\n");
104 /************************* IRDA SUBROUTINES *************************/
106 * These are a bunch of subroutines called from other functions
107 * down there, mostly common code or to improve readability...
109 * Note : we duplicate quite heavily some routines of af_irda.c,
110 * because our input structure (self) is quite different
111 * (struct irnet instead of struct irda_sock), which make sharing
112 * the same code impossible (at least, without templates).
115 /*------------------------------------------------------------------*/
117 * Function irda_open_tsap (self)
119 * Open local Transport Service Access Point (TSAP)
121 * Create a IrTTP instance for us and set all the IrTTP callbacks.
124 irnet_open_tsap(irnet_socket * self)
126 notify_t notify; /* Callback structure */
128 DENTER(IRDA_SR_TRACE, "(self=0x%p)\n", self);
130 DABORT(self->tsap != NULL, -EBUSY, IRDA_SR_ERROR, "Already busy !\n");
132 /* Initialize IrTTP callbacks to be used by the IrDA stack */
133 irda_notify_init(¬ify);
134 notify.connect_confirm = irnet_connect_confirm;
135 notify.connect_indication = irnet_connect_indication;
136 notify.disconnect_indication = irnet_disconnect_indication;
137 notify.data_indication = irnet_data_indication;
138 /*notify.udata_indication = NULL;*/
139 notify.flow_indication = irnet_flow_indication;
140 notify.status_indication = irnet_status_indication;
141 notify.instance = self;
142 strlcpy(notify.name, IRNET_NOTIFY_NAME, sizeof(notify.name));
144 /* Open an IrTTP instance */
145 self->tsap = irttp_open_tsap(LSAP_ANY, DEFAULT_INITIAL_CREDIT,
147 DABORT(self->tsap == NULL, -ENOMEM,
148 IRDA_SR_ERROR, "Unable to allocate TSAP !\n");
150 /* Remember which TSAP selector we actually got */
151 self->stsap_sel = self->tsap->stsap_sel;
153 DEXIT(IRDA_SR_TRACE, " - tsap=0x%p, sel=0x%X\n",
154 self->tsap, self->stsap_sel);
158 /*------------------------------------------------------------------*/
160 * Function irnet_ias_to_tsap (self, result, value)
162 * Examine an IAS object and extract TSAP
164 * We do an IAP query to find the TSAP associated with the IrNET service.
165 * When IrIAP pass us the result of the query, this function look at
166 * the return values to check for failures and extract the TSAP if
168 * Also deallocate value
169 * The failure is in self->errno
173 irnet_ias_to_tsap(irnet_socket * self,
175 struct ias_value * value)
177 __u8 dtsap_sel = 0; /* TSAP we are looking for */
179 DENTER(IRDA_SR_TRACE, "(self=0x%p)\n", self);
181 /* By default, no error */
184 /* Check if request succeeded */
187 /* Standard errors : service not available */
188 case IAS_CLASS_UNKNOWN:
189 case IAS_ATTRIB_UNKNOWN:
190 DEBUG(IRDA_SR_INFO, "IAS object doesn't exist ! (%d)\n", result);
191 self->errno = -EADDRNOTAVAIL;
194 /* Other errors, most likely IrDA stack failure */
196 DEBUG(IRDA_SR_INFO, "IAS query failed ! (%d)\n", result);
197 self->errno = -EHOSTUNREACH;
200 /* Success : we got what we wanted */
205 /* Check what was returned to us */
208 /* What type of argument have we got ? */
212 DEBUG(IRDA_SR_INFO, "result=%d\n", value->t.integer);
213 if(value->t.integer != -1)
214 /* Get the remote TSAP selector */
215 dtsap_sel = value->t.integer;
217 self->errno = -EADDRNOTAVAIL;
220 self->errno = -EADDRNOTAVAIL;
221 DERROR(IRDA_SR_ERROR, "bad type ! (0x%X)\n", value->type);
226 irias_delete_value(value);
228 else /* value == NULL */
230 /* Nothing returned to us - usually result != SUCCESS */
233 DERROR(IRDA_SR_ERROR,
234 "IrDA bug : result == SUCCESS && value == NULL\n");
235 self->errno = -EHOSTUNREACH;
238 DEXIT(IRDA_SR_TRACE, "\n");
240 /* Return the TSAP */
244 /*------------------------------------------------------------------*/
246 * Function irnet_find_lsap_sel (self)
248 * Try to lookup LSAP selector in remote LM-IAS
250 * Basically, we start a IAP query, and then go to sleep. When the query
251 * return, irnet_getvalue_confirm will wake us up, and we can examine the
252 * result of the query...
253 * Note that in some case, the query fail even before we go to sleep,
254 * creating some races...
257 irnet_find_lsap_sel(irnet_socket * self)
259 DENTER(IRDA_SR_TRACE, "(self=0x%p)\n", self);
261 /* This should not happen */
262 DABORT(self->iriap, -EBUSY, IRDA_SR_ERROR, "busy with a previous query.\n");
264 /* Create an IAP instance, will be closed in irnet_getvalue_confirm() */
265 self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
266 irnet_getvalue_confirm);
268 /* Treat unexpected signals as disconnect */
269 self->errno = -EHOSTUNREACH;
271 /* Query remote LM-IAS */
272 iriap_getvaluebyclass_request(self->iriap, self->rsaddr, self->daddr,
273 IRNET_SERVICE_NAME, IRNET_IAS_VALUE);
275 /* The above request is non-blocking.
276 * After a while, IrDA will call us back in irnet_getvalue_confirm()
277 * We will then call irnet_ias_to_tsap() and finish the
278 * connection procedure */
280 DEXIT(IRDA_SR_TRACE, "\n");
284 /*------------------------------------------------------------------*/
286 * Function irnet_connect_tsap (self)
288 * Initialise the TTP socket and initiate TTP connection
292 irnet_connect_tsap(irnet_socket * self)
296 DENTER(IRDA_SR_TRACE, "(self=0x%p)\n", self);
298 /* Open a local TSAP (an IrTTP instance) */
299 err = irnet_open_tsap(self);
302 clear_bit(0, &self->ttp_connect);
303 DERROR(IRDA_SR_ERROR, "connect aborted!\n");
307 /* Connect to remote device */
308 err = irttp_connect_request(self->tsap, self->dtsap_sel,
309 self->rsaddr, self->daddr, NULL,
310 self->max_sdu_size_rx, NULL);
313 clear_bit(0, &self->ttp_connect);
314 DERROR(IRDA_SR_ERROR, "connect aborted!\n");
318 /* The above call is non-blocking.
319 * After a while, the IrDA stack will either call us back in
320 * irnet_connect_confirm() or irnet_disconnect_indication()
321 * See you there ;-) */
323 DEXIT(IRDA_SR_TRACE, "\n");
327 /*------------------------------------------------------------------*/
329 * Function irnet_discover_next_daddr (self)
331 * Query the IrNET TSAP of the next device in the log.
333 * Used in the TSAP discovery procedure.
336 irnet_discover_next_daddr(irnet_socket * self)
338 /* Close the last instance of IrIAP, and open a new one.
339 * We can't reuse the IrIAP instance in the IrIAP callback */
342 iriap_close(self->iriap);
345 /* Create a new IAP instance */
346 self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
347 irnet_discovervalue_confirm);
348 if(self->iriap == NULL)
351 /* Next discovery - before the call to avoid races */
354 /* Check if we have one more address to try */
355 if(self->disco_index < self->disco_number)
357 /* Query remote LM-IAS */
358 iriap_getvaluebyclass_request(self->iriap,
359 self->discoveries[self->disco_index].saddr,
360 self->discoveries[self->disco_index].daddr,
361 IRNET_SERVICE_NAME, IRNET_IAS_VALUE);
362 /* The above request is non-blocking.
363 * After a while, IrDA will call us back in irnet_discovervalue_confirm()
364 * We will then call irnet_ias_to_tsap() and come back here again... */
371 /*------------------------------------------------------------------*/
373 * Function irnet_discover_daddr_and_lsap_sel (self)
375 * This try to find a device with the requested service.
377 * Initiate a TSAP discovery procedure.
378 * It basically look into the discovery log. For each address in the list,
379 * it queries the LM-IAS of the device to find if this device offer
380 * the requested service.
381 * If there is more than one node supporting the service, we complain
382 * to the user (it should move devices around).
383 * If we find one node which have the requested TSAP, we connect to it.
385 * This function just start the whole procedure. It request the discovery
386 * log and submit the first IAS query.
387 * The bulk of the job is handled in irnet_discovervalue_confirm()
389 * Note : this procedure fails if there is more than one device in range
390 * on the same dongle, because IrLMP doesn't disconnect the LAP when the
391 * last LSAP is closed. Moreover, we would need to wait the LAP
395 irnet_discover_daddr_and_lsap_sel(irnet_socket * self)
399 DENTER(IRDA_SR_TRACE, "(self=0x%p)\n", self);
401 /* Ask lmp for the current discovery log */
402 self->discoveries = irlmp_get_discoveries(&self->disco_number, self->mask,
403 DISCOVERY_DEFAULT_SLOTS);
405 /* Check if the we got some results */
406 if(self->discoveries == NULL)
408 self->disco_number = -1;
409 clear_bit(0, &self->ttp_connect);
410 DRETURN(-ENETUNREACH, IRDA_SR_INFO, "No Cachelog...\n");
412 DEBUG(IRDA_SR_INFO, "Got the log (0x%p), size is %d\n",
413 self->discoveries, self->disco_number);
415 /* Start with the first discovery */
416 self->disco_index = -1;
417 self->daddr = DEV_ADDR_ANY;
419 /* This will fail if the log is empty - this is non-blocking */
420 ret = irnet_discover_next_daddr(self);
425 iriap_close(self->iriap);
428 /* Cleanup our copy of the discovery log */
429 kfree(self->discoveries);
430 self->discoveries = NULL;
432 clear_bit(0, &self->ttp_connect);
433 DRETURN(-ENETUNREACH, IRDA_SR_INFO, "Cachelog empty...\n");
436 /* Follow me in irnet_discovervalue_confirm() */
438 DEXIT(IRDA_SR_TRACE, "\n");
442 /*------------------------------------------------------------------*/
444 * Function irnet_dname_to_daddr (self)
446 * Convert an IrDA nickname to a valid IrDA address
448 * It basically look into the discovery log until there is a match.
451 irnet_dname_to_daddr(irnet_socket * self)
453 struct irda_device_info *discoveries; /* Copy of the discovery log */
454 int number; /* Number of nodes in the log */
457 DENTER(IRDA_SR_TRACE, "(self=0x%p)\n", self);
459 /* Ask lmp for the current discovery log */
460 discoveries = irlmp_get_discoveries(&number, 0xffff,
461 DISCOVERY_DEFAULT_SLOTS);
462 /* Check if the we got some results */
463 if(discoveries == NULL)
464 DRETURN(-ENETUNREACH, IRDA_SR_INFO, "Cachelog empty...\n");
467 * Now, check all discovered devices (if any), and connect
468 * client only about the services that the client is
471 for(i = 0; i < number; i++)
473 /* Does the name match ? */
474 if(!strncmp(discoveries[i].info, self->rname, NICKNAME_MAX_LEN))
476 /* Yes !!! Get it.. */
477 self->daddr = discoveries[i].daddr;
478 DEBUG(IRDA_SR_INFO, "discovered device ``%s'' at address 0x%08x.\n",
479 self->rname, self->daddr);
481 DEXIT(IRDA_SR_TRACE, "\n");
486 DEBUG(IRDA_SR_INFO, "cannot discover device ``%s'' !!!\n", self->rname);
488 return(-EADDRNOTAVAIL);
492 /************************* SOCKET ROUTINES *************************/
494 * This are the main operations on IrNET sockets, basically to create
495 * and destroy IrNET sockets. These are called from the PPP part...
498 /*------------------------------------------------------------------*/
500 * Create a IrNET instance : just initialise some parameters...
503 irda_irnet_create(irnet_socket * self)
505 DENTER(IRDA_SOCK_TRACE, "(self=0x%p)\n", self);
507 self->magic = IRNET_MAGIC; /* Paranoia */
509 self->ttp_open = 0; /* Prevent higher layer from accessing IrTTP */
510 self->ttp_connect = 0; /* Not connecting yet */
511 self->rname[0] = '\0'; /* May be set via control channel */
512 self->rdaddr = DEV_ADDR_ANY; /* May be set via control channel */
513 self->rsaddr = DEV_ADDR_ANY; /* May be set via control channel */
514 self->daddr = DEV_ADDR_ANY; /* Until we get connected */
515 self->saddr = DEV_ADDR_ANY; /* Until we get connected */
516 self->max_sdu_size_rx = TTP_SAR_UNBOUND;
518 /* Register as a client with IrLMP */
519 self->ckey = irlmp_register_client(0, NULL, NULL, NULL);
520 #ifdef DISCOVERY_NOMASK
521 self->mask = 0xffff; /* For W2k compatibility */
522 #else /* DISCOVERY_NOMASK */
523 self->mask = irlmp_service_to_hint(S_LAN);
524 #endif /* DISCOVERY_NOMASK */
525 self->tx_flow = FLOW_START; /* Flow control from IrTTP */
527 INIT_WORK(&self->disconnect_work, irnet_ppp_disconnect);
529 DEXIT(IRDA_SOCK_TRACE, "\n");
533 /*------------------------------------------------------------------*/
535 * Connect to the other side :
536 * o convert device name to an address
537 * o find the socket number (dlsap)
538 * o Establish the connection
540 * Note : We no longer mimic af_irda. The IAS query for finding the TSAP
541 * is done asynchronously, like the TTP connection. This allow us to
542 * call this function from any context (not only process).
543 * The downside is that following what's happening in there is tricky
544 * because it involve various functions all over the place...
547 irda_irnet_connect(irnet_socket * self)
551 DENTER(IRDA_SOCK_TRACE, "(self=0x%p)\n", self);
553 /* Check if we are already trying to connect.
554 * Because irda_irnet_connect() can be called directly by pppd plus
555 * packet retries in ppp_generic and connect may take time, plus we may
556 * race with irnet_connect_indication(), we need to be careful there... */
557 if(test_and_set_bit(0, &self->ttp_connect))
558 DRETURN(-EBUSY, IRDA_SOCK_INFO, "Already connecting...\n");
559 if((self->iriap != NULL) || (self->tsap != NULL))
560 DERROR(IRDA_SOCK_ERROR, "Socket not cleaned up...\n");
562 /* Insert ourselves in the hashbin so that the IrNET server can find us.
563 * Notes : 4th arg is string of 32 char max and must be null terminated
564 * When 4th arg is used (string), 3rd arg isn't (int)
565 * Can't re-insert (MUST remove first) so check for that... */
566 if((irnet_server.running) && (self->q.q_next == NULL))
568 spin_lock_bh(&irnet_server.spinlock);
569 hashbin_insert(irnet_server.list, (irda_queue_t *) self, 0, self->rname);
570 spin_unlock_bh(&irnet_server.spinlock);
571 DEBUG(IRDA_SOCK_INFO, "Inserted ``%s'' in hashbin...\n", self->rname);
574 /* If we don't have anything (no address, no name) */
575 if((self->rdaddr == DEV_ADDR_ANY) && (self->rname[0] == '\0'))
577 /* Try to find a suitable address */
578 if((err = irnet_discover_daddr_and_lsap_sel(self)) != 0)
579 DRETURN(err, IRDA_SOCK_INFO, "auto-connect failed!\n");
580 /* In most cases, the call above is non-blocking */
584 /* If we have only the name (no address), try to get an address */
585 if(self->rdaddr == DEV_ADDR_ANY)
587 if((err = irnet_dname_to_daddr(self)) != 0)
588 DRETURN(err, IRDA_SOCK_INFO, "name connect failed!\n");
591 /* Use the requested destination address */
592 self->daddr = self->rdaddr;
594 /* Query remote LM-IAS to find LSAP selector */
595 irnet_find_lsap_sel(self);
596 /* The above call is non blocking */
599 /* At this point, we are waiting for the IrDA stack to call us back,
600 * or we have already failed.
601 * We will finish the connection procedure in irnet_connect_tsap().
603 DEXIT(IRDA_SOCK_TRACE, "\n");
607 /*------------------------------------------------------------------*/
609 * Function irda_irnet_destroy(self)
611 * Destroy irnet instance
613 * Note : this need to be called from a process context.
616 irda_irnet_destroy(irnet_socket * self)
618 DENTER(IRDA_SOCK_TRACE, "(self=0x%p)\n", self);
622 /* Remove ourselves from hashbin (if we are queued in hashbin)
623 * Note : `irnet_server.running' protect us from calls in hashbin_delete() */
624 if((irnet_server.running) && (self->q.q_next != NULL))
626 struct irnet_socket * entry;
627 DEBUG(IRDA_SOCK_INFO, "Removing from hash..\n");
628 spin_lock_bh(&irnet_server.spinlock);
629 entry = hashbin_remove_this(irnet_server.list, (irda_queue_t *) self);
630 self->q.q_next = NULL;
631 spin_unlock_bh(&irnet_server.spinlock);
632 DASSERT(entry == self, , IRDA_SOCK_ERROR, "Can't remove from hash.\n");
635 /* If we were connected, post a message */
636 if(test_bit(0, &self->ttp_open))
638 /* Note : as the disconnect comes from ppp_generic, the unit number
639 * doesn't exist anymore when we post the event, so we need to pass
640 * NULL as the first arg... */
641 irnet_post_event(NULL, IRNET_DISCONNECT_TO,
642 self->saddr, self->daddr, self->rname, 0);
645 /* Prevent various IrDA callbacks from messing up things
646 * Need to be first */
647 clear_bit(0, &self->ttp_connect);
649 /* Prevent higher layer from accessing IrTTP */
650 clear_bit(0, &self->ttp_open);
652 /* Unregister with IrLMP */
653 irlmp_unregister_client(self->ckey);
655 /* Unregister with LM-IAS */
658 iriap_close(self->iriap);
662 /* Cleanup eventual discoveries from connection attempt or control channel */
663 if(self->discoveries != NULL)
665 /* Cleanup our copy of the discovery log */
666 kfree(self->discoveries);
667 self->discoveries = NULL;
670 /* Close our IrTTP connection */
673 DEBUG(IRDA_SOCK_INFO, "Closing our TTP connection.\n");
674 irttp_disconnect_request(self->tsap, NULL, P_NORMAL);
675 irttp_close_tsap(self->tsap);
680 DEXIT(IRDA_SOCK_TRACE, "\n");
685 /************************** SERVER SOCKET **************************/
687 * The IrNET service is composed of one server socket and a variable
688 * number of regular IrNET sockets. The server socket is supposed to
689 * handle incoming connections and redirect them to one IrNET sockets.
690 * It's a superset of the regular IrNET socket, but has a very distinct
694 /*------------------------------------------------------------------*/
696 * Function irnet_daddr_to_dname (self)
698 * Convert an IrDA address to a IrDA nickname
700 * It basically look into the discovery log until there is a match.
703 irnet_daddr_to_dname(irnet_socket * self)
705 struct irda_device_info *discoveries; /* Copy of the discovery log */
706 int number; /* Number of nodes in the log */
709 DENTER(IRDA_SERV_TRACE, "(self=0x%p)\n", self);
711 /* Ask lmp for the current discovery log */
712 discoveries = irlmp_get_discoveries(&number, 0xffff,
713 DISCOVERY_DEFAULT_SLOTS);
714 /* Check if the we got some results */
715 if (discoveries == NULL)
716 DRETURN(-ENETUNREACH, IRDA_SERV_INFO, "Cachelog empty...\n");
718 /* Now, check all discovered devices (if any) */
719 for(i = 0; i < number; i++)
721 /* Does the name match ? */
722 if(discoveries[i].daddr == self->daddr)
724 /* Yes !!! Get it.. */
725 strlcpy(self->rname, discoveries[i].info, sizeof(self->rname));
726 self->rname[sizeof(self->rname) - 1] = '\0';
727 DEBUG(IRDA_SERV_INFO, "Device 0x%08x is in fact ``%s''.\n",
728 self->daddr, self->rname);
730 DEXIT(IRDA_SERV_TRACE, "\n");
735 DEXIT(IRDA_SERV_INFO, ": cannot discover device 0x%08x !!!\n", self->daddr);
737 return(-EADDRNOTAVAIL);
740 /*------------------------------------------------------------------*/
742 * Function irda_find_socket (self)
744 * Find the correct IrNET socket
746 * Look into the list of IrNET sockets and finds one with the right
749 static inline irnet_socket *
750 irnet_find_socket(irnet_socket * self)
752 irnet_socket * new = (irnet_socket *) NULL;
755 DENTER(IRDA_SERV_TRACE, "(self=0x%p)\n", self);
757 /* Get the addresses of the requester */
758 self->daddr = irttp_get_daddr(self->tsap);
759 self->saddr = irttp_get_saddr(self->tsap);
761 /* Try to get the IrDA nickname of the requester */
762 err = irnet_daddr_to_dname(self);
764 /* Protect access to the instance list */
765 spin_lock_bh(&irnet_server.spinlock);
767 /* So now, try to get an socket having specifically
768 * requested that nickname */
771 new = (irnet_socket *) hashbin_find(irnet_server.list,
774 DEBUG(IRDA_SERV_INFO, "Socket 0x%p matches rname ``%s''.\n",
778 /* If no name matches, try to find an socket by the destination address */
779 /* It can be either the requested destination address (set via the
780 * control channel), or the current destination address if the
781 * socket is in the middle of a connection request */
782 if(new == (irnet_socket *) NULL)
784 new = (irnet_socket *) hashbin_get_first(irnet_server.list);
785 while(new !=(irnet_socket *) NULL)
787 /* Does it have the same address ? */
788 if((new->rdaddr == self->daddr) || (new->daddr == self->daddr))
790 /* Yes !!! Get it.. */
791 DEBUG(IRDA_SERV_INFO, "Socket 0x%p matches daddr %#08x.\n",
795 new = (irnet_socket *) hashbin_get_next(irnet_server.list);
799 /* If we don't have any socket, get the first unconnected socket */
800 if(new == (irnet_socket *) NULL)
802 new = (irnet_socket *) hashbin_get_first(irnet_server.list);
803 while(new !=(irnet_socket *) NULL)
805 /* Is it available ? */
806 if(!(test_bit(0, &new->ttp_open)) && (new->rdaddr == DEV_ADDR_ANY) &&
807 (new->rname[0] == '\0') && (new->ppp_open))
809 /* Yes !!! Get it.. */
810 DEBUG(IRDA_SERV_INFO, "Socket 0x%p is free.\n",
814 new = (irnet_socket *) hashbin_get_next(irnet_server.list);
819 spin_unlock_bh(&irnet_server.spinlock);
821 DEXIT(IRDA_SERV_TRACE, " - new = 0x%p\n", new);
825 /*------------------------------------------------------------------*/
827 * Function irda_connect_socket (self)
829 * Connect an incoming connection to the socket
833 irnet_connect_socket(irnet_socket * server,
835 struct qos_info * qos,
837 __u8 max_header_size)
839 DENTER(IRDA_SERV_TRACE, "(server=0x%p, new=0x%p)\n",
842 /* Now attach up the new socket */
843 new->tsap = irttp_dup(server->tsap, new);
844 DABORT(new->tsap == NULL, -1, IRDA_SERV_ERROR, "dup failed!\n");
846 /* Set up all the relevant parameters on the new socket */
847 new->stsap_sel = new->tsap->stsap_sel;
848 new->dtsap_sel = new->tsap->dtsap_sel;
849 new->saddr = irttp_get_saddr(new->tsap);
850 new->daddr = irttp_get_daddr(new->tsap);
852 new->max_header_size = max_header_size;
853 new->max_sdu_size_tx = max_sdu_size;
854 new->max_data_size = max_sdu_size;
856 /* If we want to receive "stream sockets" */
857 if(max_sdu_size == 0)
858 new->max_data_size = irttp_get_max_seg_size(new->tsap);
859 #endif /* STREAM_COMPAT */
861 /* Clean up the original one to keep it in listen state */
862 irttp_listen(server->tsap);
864 /* Send a connection response on the new socket */
865 irttp_connect_response(new->tsap, new->max_sdu_size_rx, NULL);
867 /* Allow PPP to send its junk over the new socket... */
868 set_bit(0, &new->ttp_open);
870 /* Not connecting anymore, and clean up last possible remains
871 * of connection attempts on the socket */
872 clear_bit(0, &new->ttp_connect);
875 iriap_close(new->iriap);
878 if(new->discoveries != NULL)
880 kfree(new->discoveries);
881 new->discoveries = NULL;
884 #ifdef CONNECT_INDIC_KICK
885 /* As currently we don't block packets in ppp_irnet_send() while passive,
886 * this is not really needed...
887 * Also, not doing it give IrDA a chance to finish the setup properly
888 * before being swamped with packets... */
889 ppp_output_wakeup(&new->chan);
890 #endif /* CONNECT_INDIC_KICK */
892 /* Notify the control channel */
893 irnet_post_event(new, IRNET_CONNECT_FROM,
894 new->saddr, new->daddr, server->rname, 0);
896 DEXIT(IRDA_SERV_TRACE, "\n");
900 /*------------------------------------------------------------------*/
902 * Function irda_disconnect_server (self)
904 * Cleanup the server socket when the incoming connection abort
908 irnet_disconnect_server(irnet_socket * self,
911 DENTER(IRDA_SERV_TRACE, "(self=0x%p)\n", self);
913 /* Put the received packet in the black hole */
916 #ifdef FAIL_SEND_DISCONNECT
917 /* Tell the other party we don't want to be connected */
918 /* Hum... Is it the right thing to do ? And do we need to send
919 * a connect response before ? It looks ok without this... */
920 irttp_disconnect_request(self->tsap, NULL, P_NORMAL);
921 #endif /* FAIL_SEND_DISCONNECT */
923 /* Notify the control channel (see irnet_find_socket()) */
924 irnet_post_event(NULL, IRNET_REQUEST_FROM,
925 self->saddr, self->daddr, self->rname, 0);
927 /* Clean up the server to keep it in listen state */
928 irttp_listen(self->tsap);
930 DEXIT(IRDA_SERV_TRACE, "\n");
934 /*------------------------------------------------------------------*/
936 * Function irda_setup_server (self)
938 * Create a IrTTP server and set it up...
940 * Register the IrLAN hint bit, create a IrTTP instance for us,
941 * set all the IrTTP callbacks and create an IrIAS entry...
944 irnet_setup_server(void)
948 DENTER(IRDA_SERV_TRACE, "()\n");
950 /* Initialise the regular socket part of the server */
951 irda_irnet_create(&irnet_server.s);
953 /* Open a local TSAP (an IrTTP instance) for the server */
954 irnet_open_tsap(&irnet_server.s);
957 irnet_server.s.ppp_open = 0;
958 irnet_server.s.chan.private = NULL;
959 irnet_server.s.file = NULL;
961 /* Get the hint bit corresponding to IrLAN */
962 /* Note : we overload the IrLAN hint bit. As it is only a "hint", and as
963 * we provide roughly the same functionality as IrLAN, this is ok.
964 * In fact, the situation is similar as JetSend overloading the Obex hint
966 hints = irlmp_service_to_hint(S_LAN);
968 #ifdef ADVERTISE_HINT
969 /* Register with IrLMP as a service (advertise our hint bit) */
970 irnet_server.skey = irlmp_register_service(hints);
971 #endif /* ADVERTISE_HINT */
973 /* Register with LM-IAS (so that people can connect to us) */
974 irnet_server.ias_obj = irias_new_object(IRNET_SERVICE_NAME, jiffies);
975 irias_add_integer_attrib(irnet_server.ias_obj, IRNET_IAS_VALUE,
976 irnet_server.s.stsap_sel, IAS_KERNEL_ATTR);
977 irias_insert_object(irnet_server.ias_obj);
979 #ifdef DISCOVERY_EVENTS
980 /* Tell IrLMP we want to be notified of newly discovered nodes */
981 irlmp_update_client(irnet_server.s.ckey, hints,
982 irnet_discovery_indication, irnet_expiry_indication,
983 (void *) &irnet_server.s);
986 DEXIT(IRDA_SERV_TRACE, " - self=0x%p\n", &irnet_server.s);
990 /*------------------------------------------------------------------*/
992 * Function irda_destroy_server (self)
994 * Destroy the IrTTP server...
996 * Reverse of the previous function...
999 irnet_destroy_server(void)
1001 DENTER(IRDA_SERV_TRACE, "()\n");
1003 #ifdef ADVERTISE_HINT
1004 /* Unregister with IrLMP */
1005 irlmp_unregister_service(irnet_server.skey);
1006 #endif /* ADVERTISE_HINT */
1008 /* Unregister with LM-IAS */
1009 if(irnet_server.ias_obj)
1010 irias_delete_object(irnet_server.ias_obj);
1012 /* Cleanup the socket part */
1013 irda_irnet_destroy(&irnet_server.s);
1015 DEXIT(IRDA_SERV_TRACE, "\n");
1020 /************************ IRDA-TTP CALLBACKS ************************/
1022 * When we create a IrTTP instance, we pass to it a set of callbacks
1023 * that IrTTP will call in case of various events.
1024 * We take care of those events here.
1027 /*------------------------------------------------------------------*/
1029 * Function irnet_data_indication (instance, sap, skb)
1031 * Received some data from TinyTP. Just queue it on the receive queue
1035 irnet_data_indication(void * instance,
1037 struct sk_buff *skb)
1039 irnet_socket * ap = (irnet_socket *) instance;
1043 DENTER(IRDA_TCB_TRACE, "(self/ap=0x%p, skb=0x%p)\n",
1045 DASSERT(skb != NULL, 0, IRDA_CB_ERROR, "skb is NULL !!!\n");
1047 /* Check is ppp is ready to receive our packet */
1050 DERROR(IRDA_CB_ERROR, "PPP not ready, dropping packet...\n");
1051 /* When we return error, TTP will need to requeue the skb and
1052 * will stop the sender. IrTTP will stall until we send it a
1053 * flow control request... */
1057 /* strip address/control field if present */
1059 if((p[0] == PPP_ALLSTATIONS) && (p[1] == PPP_UI))
1061 /* chop off address/control */
1064 p = skb_pull(skb, 2);
1067 /* decompress protocol field if compressed */
1070 /* protocol is compressed */
1071 skb_push(skb, 1)[0] = 0;
1077 /* pass to generic ppp layer */
1078 /* Note : how do I know if ppp can accept or not the packet ? This is
1079 * essential if I want to manage flow control smoothly... */
1080 ppp_input(&ap->chan, skb);
1082 DEXIT(IRDA_TCB_TRACE, "\n");
1086 DERROR(IRDA_CB_ERROR, "Packet too small, dropping...\n");
1088 ppp_input_error(&ap->chan, code);
1089 return 0; /* Don't return an error code, only for flow control... */
1092 /*------------------------------------------------------------------*/
1094 * Function irnet_disconnect_indication (instance, sap, reason, skb)
1096 * Connection has been closed. Chech reason to find out why
1098 * Note : there are many cases where we come here :
1099 * o attempted to connect, timeout
1100 * o connected, link is broken, LAP has timeout
1101 * o connected, other side close the link
1102 * o connection request on the server not handled
1105 irnet_disconnect_indication(void * instance,
1108 struct sk_buff *skb)
1110 irnet_socket * self = (irnet_socket *) instance;
1114 DENTER(IRDA_TCB_TRACE, "(self=0x%p)\n", self);
1115 DASSERT(self != NULL, , IRDA_CB_ERROR, "Self is NULL !!!\n");
1117 /* Don't care about it, but let's not leak it */
1121 /* Prevent higher layer from accessing IrTTP */
1122 test_open = test_and_clear_bit(0, &self->ttp_open);
1123 /* Not connecting anymore...
1124 * (note : TSAP is open, so IAP callbacks are no longer pending...) */
1125 test_connect = test_and_clear_bit(0, &self->ttp_connect);
1127 /* If both self->ttp_open and self->ttp_connect are NULL, it mean that we
1128 * have a race condition with irda_irnet_destroy() or
1129 * irnet_connect_indication(), so don't mess up tsap...
1131 if(!(test_open || test_connect))
1133 DERROR(IRDA_CB_ERROR, "Race condition detected...\n");
1137 /* If we were active, notify the control channel */
1139 irnet_post_event(self, IRNET_DISCONNECT_FROM,
1140 self->saddr, self->daddr, self->rname, 0);
1142 /* If we were trying to connect, notify the control channel */
1143 if((self->tsap) && (self != &irnet_server.s))
1144 irnet_post_event(self, IRNET_NOANSWER_FROM,
1145 self->saddr, self->daddr, self->rname, 0);
1147 /* Close our IrTTP connection, cleanup tsap */
1148 if((self->tsap) && (self != &irnet_server.s))
1150 DEBUG(IRDA_CB_INFO, "Closing our TTP connection.\n");
1151 irttp_close_tsap(self->tsap);
1154 /* Cleanup the socket in case we want to reconnect in ppp_output_wakeup() */
1155 self->stsap_sel = 0;
1156 self->daddr = DEV_ADDR_ANY;
1157 self->tx_flow = FLOW_START;
1159 /* Deal with the ppp instance if it's still alive */
1164 /* ppp_unregister_channel() wants a user context. */
1165 schedule_work(&self->disconnect_work);
1169 /* If we were trying to connect, flush (drain) ppp_generic
1170 * Tx queue (most often we have blocked it), which will
1171 * trigger an other attempt to connect. If we are passive,
1172 * this will empty the Tx queue after last try. */
1173 ppp_output_wakeup(&self->chan);
1177 DEXIT(IRDA_TCB_TRACE, "\n");
1180 /*------------------------------------------------------------------*/
1182 * Function irnet_connect_confirm (instance, sap, qos, max_sdu_size, skb)
1184 * Connections has been confirmed by the remote device
1188 irnet_connect_confirm(void * instance,
1190 struct qos_info *qos,
1192 __u8 max_header_size,
1193 struct sk_buff *skb)
1195 irnet_socket * self = (irnet_socket *) instance;
1197 DENTER(IRDA_TCB_TRACE, "(self=0x%p)\n", self);
1199 /* Check if socket is closing down (via irda_irnet_destroy()) */
1200 if(! test_bit(0, &self->ttp_connect))
1202 DERROR(IRDA_CB_ERROR, "Socket no longer connecting. Ouch !\n");
1206 /* How much header space do we need to reserve */
1207 self->max_header_size = max_header_size;
1209 /* IrTTP max SDU size in transmit direction */
1210 self->max_sdu_size_tx = max_sdu_size;
1211 self->max_data_size = max_sdu_size;
1212 #ifdef STREAM_COMPAT
1213 if(max_sdu_size == 0)
1214 self->max_data_size = irttp_get_max_seg_size(self->tsap);
1215 #endif /* STREAM_COMPAT */
1217 /* At this point, IrLMP has assigned our source address */
1218 self->saddr = irttp_get_saddr(self->tsap);
1220 /* Allow higher layer to access IrTTP */
1221 set_bit(0, &self->ttp_open);
1222 clear_bit(0, &self->ttp_connect); /* Not racy, IrDA traffic is serial */
1223 /* Give a kick in the ass of ppp_generic so that he sends us some data */
1224 ppp_output_wakeup(&self->chan);
1226 /* Check size of received packet */
1229 #ifdef PASS_CONNECT_PACKETS
1230 DEBUG(IRDA_CB_INFO, "Passing connect packet to PPP.\n");
1231 /* Try to pass it to PPP */
1232 irnet_data_indication(instance, sap, skb);
1233 #else /* PASS_CONNECT_PACKETS */
1234 DERROR(IRDA_CB_ERROR, "Dropping non empty packet.\n");
1235 kfree_skb(skb); /* Note : will be optimised with other kfree... */
1236 #endif /* PASS_CONNECT_PACKETS */
1241 /* Notify the control channel */
1242 irnet_post_event(self, IRNET_CONNECT_TO,
1243 self->saddr, self->daddr, self->rname, 0);
1245 DEXIT(IRDA_TCB_TRACE, "\n");
1248 /*------------------------------------------------------------------*/
1250 * Function irnet_flow_indication (instance, sap, flow)
1252 * Used by TinyTP to tell us if it can accept more data or not
1256 irnet_flow_indication(void * instance,
1260 irnet_socket * self = (irnet_socket *) instance;
1261 LOCAL_FLOW oldflow = self->tx_flow;
1263 DENTER(IRDA_TCB_TRACE, "(self=0x%p, flow=%d)\n", self, flow);
1265 /* Update our state */
1266 self->tx_flow = flow;
1268 /* Check what IrTTP want us to do... */
1272 DEBUG(IRDA_CB_INFO, "IrTTP wants us to start again\n");
1273 /* Check if we really need to wake up PPP */
1274 if(oldflow == FLOW_STOP)
1275 ppp_output_wakeup(&self->chan);
1277 DEBUG(IRDA_CB_INFO, "But we were already transmitting !!!\n");
1280 DEBUG(IRDA_CB_INFO, "IrTTP wants us to slow down\n");
1283 DEBUG(IRDA_CB_INFO, "Unknown flow command!\n");
1287 DEXIT(IRDA_TCB_TRACE, "\n");
1290 /*------------------------------------------------------------------*/
1292 * Function irnet_status_indication (instance, sap, reason, skb)
1294 * Link (IrLAP) status report.
1298 irnet_status_indication(void * instance,
1302 irnet_socket * self = (irnet_socket *) instance;
1304 DENTER(IRDA_TCB_TRACE, "(self=0x%p)\n", self);
1305 DASSERT(self != NULL, , IRDA_CB_ERROR, "Self is NULL !!!\n");
1307 /* We can only get this event if we are connected */
1310 case STATUS_NO_ACTIVITY:
1311 irnet_post_event(self, IRNET_BLOCKED_LINK,
1312 self->saddr, self->daddr, self->rname, 0);
1315 DEBUG(IRDA_CB_INFO, "Unknown status...\n");
1318 DEXIT(IRDA_TCB_TRACE, "\n");
1321 /*------------------------------------------------------------------*/
1323 * Function irnet_connect_indication(instance, sap, qos, max_sdu_size, userdata)
1325 * Incoming connection
1327 * In theory, this function is called only on the server socket.
1328 * Some other node is attempting to connect to the IrNET service, and has
1329 * sent a connection request on our server socket.
1330 * We just redirect the connection to the relevant IrNET socket.
1332 * Note : we also make sure that between 2 irnet nodes, there can
1333 * exist only one irnet connection.
1336 irnet_connect_indication(void * instance,
1338 struct qos_info *qos,
1340 __u8 max_header_size,
1341 struct sk_buff *skb)
1343 irnet_socket * server = &irnet_server.s;
1344 irnet_socket * new = (irnet_socket *) NULL;
1346 DENTER(IRDA_TCB_TRACE, "(server=0x%p)\n", server);
1347 DASSERT(instance == &irnet_server, , IRDA_CB_ERROR,
1348 "Invalid instance (0x%p) !!!\n", instance);
1349 DASSERT(sap == irnet_server.s.tsap, , IRDA_CB_ERROR, "Invalid sap !!!\n");
1351 /* Try to find the most appropriate IrNET socket */
1352 new = irnet_find_socket(server);
1354 /* After all this hard work, do we have an socket ? */
1355 if(new == (irnet_socket *) NULL)
1357 DEXIT(IRDA_CB_INFO, ": No socket waiting for this connection.\n");
1358 irnet_disconnect_server(server, skb);
1362 /* Is the socket already busy ? */
1363 if(test_bit(0, &new->ttp_open))
1365 DEXIT(IRDA_CB_INFO, ": Socket already connected.\n");
1366 irnet_disconnect_server(server, skb);
1370 /* The following code is a bit tricky, so need comments ;-)
1372 /* If ttp_connect is set, the socket is trying to connect to the other
1373 * end and may have sent a IrTTP connection request and is waiting for
1374 * a connection response (that may never come).
1375 * Now, the pain is that the socket may have opened a tsap and is
1376 * waiting on it, while the other end is trying to connect to it on
1378 * Because IrNET can be peer to peer, we need to workaround this.
1379 * Furthermore, the way the irnetd script is implemented, the
1380 * target will create a second IrNET connection back to the
1381 * originator and expect the originator to bind this new connection
1382 * to the original PPPD instance.
1383 * And of course, if we don't use irnetd, we can have a race when
1384 * both side try to connect simultaneously, which could leave both
1385 * connections half closed (yuck).
1387 * 1) The "originator" must accept the new connection and get rid
1388 * of the old one so that irnetd works
1389 * 2) One side must deny the new connection to avoid races,
1390 * but both side must agree on which side it is...
1391 * Most often, the originator is primary at the LAP layer.
1394 /* Now, let's look at the way I wrote the test...
1395 * We need to clear up the ttp_connect flag atomically to prevent
1396 * irnet_disconnect_indication() to mess up the tsap we are going to close.
1397 * We want to clear the ttp_connect flag only if we close the tsap,
1398 * otherwise we will never close it, so we need to check for primary
1399 * *before* doing the test on the flag.
1400 * And of course, ALLOW_SIMULT_CONNECT can disable this entirely...
1404 /* Socket already connecting ? On primary ? */
1406 #ifdef ALLOW_SIMULT_CONNECT
1407 || ((irttp_is_primary(server->tsap) == 1) && /* primary */
1408 (test_and_clear_bit(0, &new->ttp_connect)))
1409 #endif /* ALLOW_SIMULT_CONNECT */
1412 DERROR(IRDA_CB_ERROR, "Socket already connecting, but going to reuse it !\n");
1414 /* Cleanup the old TSAP if necessary - IrIAP will be cleaned up later */
1415 if(new->tsap != NULL)
1417 /* Close the old connection the new socket was attempting,
1418 * so that we can hook it up to the new connection.
1419 * It's now safe to do it... */
1420 irttp_close_tsap(new->tsap);
1427 * 1) socket was not connecting or connected : ttp_connect should be 0.
1428 * 2) we don't want to connect the socket because we are secondary or
1429 * ALLOW_SIMULT_CONNECT is undefined. ttp_connect should be 1.
1430 * 3) we are half way in irnet_disconnect_indication(), and it's a
1431 * nice race condition... Fortunately, we can detect that by checking
1432 * if tsap is still alive. On the other hand, we can't be in
1433 * irda_irnet_destroy() otherwise we would not have found this
1434 * socket in the hashbin.
1436 if((test_bit(0, &new->ttp_connect)) || (new->tsap != NULL))
1438 /* Don't mess this socket, somebody else in in charge... */
1439 DERROR(IRDA_CB_ERROR, "Race condition detected, socket in use, abort connect...\n");
1440 irnet_disconnect_server(server, skb);
1445 /* So : at this point, we have a socket, and it is idle. Good ! */
1446 irnet_connect_socket(server, new, qos, max_sdu_size, max_header_size);
1448 /* Check size of received packet */
1451 #ifdef PASS_CONNECT_PACKETS
1452 DEBUG(IRDA_CB_INFO, "Passing connect packet to PPP.\n");
1453 /* Try to pass it to PPP */
1454 irnet_data_indication(new, new->tsap, skb);
1455 #else /* PASS_CONNECT_PACKETS */
1456 DERROR(IRDA_CB_ERROR, "Dropping non empty packet.\n");
1457 kfree_skb(skb); /* Note : will be optimised with other kfree... */
1458 #endif /* PASS_CONNECT_PACKETS */
1463 DEXIT(IRDA_TCB_TRACE, "\n");
1467 /********************** IRDA-IAS/LMP CALLBACKS **********************/
1469 * These are the callbacks called by other layers of the IrDA stack,
1470 * mainly LMP for discovery and IAS for name queries.
1473 /*------------------------------------------------------------------*/
1475 * Function irnet_getvalue_confirm (result, obj_id, value, priv)
1477 * Got answer from remote LM-IAS, just connect
1479 * This is the reply to a IAS query we were doing to find the TSAP of
1480 * the device we want to connect to.
1481 * If we have found a valid TSAP, just initiate the TTP connection
1485 irnet_getvalue_confirm(int result,
1487 struct ias_value *value,
1490 irnet_socket * self = (irnet_socket *) priv;
1492 DENTER(IRDA_OCB_TRACE, "(self=0x%p)\n", self);
1493 DASSERT(self != NULL, , IRDA_OCB_ERROR, "Self is NULL !!!\n");
1495 /* Check if already connected (via irnet_connect_socket())
1496 * or socket is closing down (via irda_irnet_destroy()) */
1497 if(! test_bit(0, &self->ttp_connect))
1499 DERROR(IRDA_OCB_ERROR, "Socket no longer connecting. Ouch !\n");
1503 /* We probably don't need to make any more queries */
1504 iriap_close(self->iriap);
1507 /* Post process the IAS reply */
1508 self->dtsap_sel = irnet_ias_to_tsap(self, result, value);
1510 /* If error, just go out */
1513 clear_bit(0, &self->ttp_connect);
1514 DERROR(IRDA_OCB_ERROR, "IAS connect failed ! (0x%X)\n", self->errno);
1518 DEBUG(IRDA_OCB_INFO, "daddr = %08x, lsap = %d, starting IrTTP connection\n",
1519 self->daddr, self->dtsap_sel);
1521 /* Start up TTP - non blocking */
1522 irnet_connect_tsap(self);
1524 DEXIT(IRDA_OCB_TRACE, "\n");
1527 /*------------------------------------------------------------------*/
1529 * Function irnet_discovervalue_confirm (result, obj_id, value, priv)
1531 * Handle the TSAP discovery procedure state machine.
1532 * Got answer from remote LM-IAS, try next device
1534 * We are doing a TSAP discovery procedure, and we got an answer to
1535 * a IAS query we were doing to find the TSAP on one of the address
1536 * in the discovery log.
1538 * If we have found a valid TSAP for the first time, save it. If it's
1539 * not the first time we found one, complain.
1541 * If we have more addresses in the log, just initiate a new query.
1542 * Note that those query may fail (see irnet_discover_daddr_and_lsap_sel())
1544 * Otherwise, wrap up the procedure (cleanup), check if we have found
1545 * any device and connect to it.
1548 irnet_discovervalue_confirm(int result,
1550 struct ias_value *value,
1553 irnet_socket * self = (irnet_socket *) priv;
1554 __u8 dtsap_sel; /* TSAP we are looking for */
1556 DENTER(IRDA_OCB_TRACE, "(self=0x%p)\n", self);
1557 DASSERT(self != NULL, , IRDA_OCB_ERROR, "Self is NULL !!!\n");
1559 /* Check if already connected (via irnet_connect_socket())
1560 * or socket is closing down (via irda_irnet_destroy()) */
1561 if(! test_bit(0, &self->ttp_connect))
1563 DERROR(IRDA_OCB_ERROR, "Socket no longer connecting. Ouch !\n");
1567 /* Post process the IAS reply */
1568 dtsap_sel = irnet_ias_to_tsap(self, result, value);
1570 /* Have we got something ? */
1571 if(self->errno == 0)
1573 /* We found the requested service */
1574 if(self->daddr != DEV_ADDR_ANY)
1576 DERROR(IRDA_OCB_ERROR, "More than one device in range supports IrNET...\n");
1580 /* First time we found that one, save it ! */
1581 self->daddr = self->discoveries[self->disco_index].daddr;
1582 self->dtsap_sel = dtsap_sel;
1587 if((self->errno == -EADDRNOTAVAIL) || (self->errno == 0))
1591 /* Search the next node */
1592 ret = irnet_discover_next_daddr(self);
1595 /* In this case, the above request was non-blocking.
1596 * We will return here after a while... */
1599 /* In this case, we have processed the last discovery item */
1602 /* No more queries to be done (failure or last one) */
1604 /* We probably don't need to make any more queries */
1605 iriap_close(self->iriap);
1608 /* No more items : remove the log and signal termination */
1609 DEBUG(IRDA_OCB_INFO, "Cleaning up log (0x%p)\n",
1611 if(self->discoveries != NULL)
1613 /* Cleanup our copy of the discovery log */
1614 kfree(self->discoveries);
1615 self->discoveries = NULL;
1617 self->disco_number = -1;
1619 /* Check out what we found */
1620 if(self->daddr == DEV_ADDR_ANY)
1622 self->daddr = DEV_ADDR_ANY;
1623 clear_bit(0, &self->ttp_connect);
1624 DEXIT(IRDA_OCB_TRACE, ": cannot discover IrNET in any device !!!\n");
1628 /* We have a valid address - just connect */
1630 DEBUG(IRDA_OCB_INFO, "daddr = %08x, lsap = %d, starting IrTTP connection\n",
1631 self->daddr, self->dtsap_sel);
1633 /* Start up TTP - non blocking */
1634 irnet_connect_tsap(self);
1636 DEXIT(IRDA_OCB_TRACE, "\n");
1639 #ifdef DISCOVERY_EVENTS
1640 /*------------------------------------------------------------------*/
1642 * Function irnet_discovery_indication (discovery)
1644 * Got a discovery indication from IrLMP, post an event
1646 * Note : IrLMP take care of matching the hint mask for us, and also
1647 * check if it is a "new" node for us...
1649 * As IrLMP filter on the IrLAN hint bit, we get both IrLAN and IrNET
1650 * nodes, so it's only at connection time that we will know if the
1651 * node support IrNET, IrLAN or both. The other solution is to check
1652 * in IAS the PNP ids and service name.
1653 * Note : even if a node support IrNET (or IrLAN), it's no guarantee
1654 * that we will be able to connect to it, the node might already be
1657 * One last thing : in some case, this function will trigger duplicate
1658 * discovery events. On the other hand, we should catch all
1659 * discoveries properly (i.e. not miss one). Filtering duplicate here
1660 * is to messy, so we leave that to user space...
1663 irnet_discovery_indication(discinfo_t * discovery,
1664 DISCOVERY_MODE mode,
1667 irnet_socket * self = &irnet_server.s;
1669 DENTER(IRDA_OCB_TRACE, "(self=0x%p)\n", self);
1670 DASSERT(priv == &irnet_server, , IRDA_OCB_ERROR,
1671 "Invalid instance (0x%p) !!!\n", priv);
1673 DEBUG(IRDA_OCB_INFO, "Discovered new IrNET/IrLAN node %s...\n",
1676 /* Notify the control channel */
1677 irnet_post_event(NULL, IRNET_DISCOVER,
1678 discovery->saddr, discovery->daddr, discovery->info,
1679 get_unaligned((__u16 *)discovery->hints));
1681 DEXIT(IRDA_OCB_TRACE, "\n");
1684 /*------------------------------------------------------------------*/
1686 * Function irnet_expiry_indication (expiry)
1688 * Got a expiry indication from IrLMP, post an event
1690 * Note : IrLMP take care of matching the hint mask for us, we only
1691 * check if it is a "new" node...
1694 irnet_expiry_indication(discinfo_t * expiry,
1695 DISCOVERY_MODE mode,
1698 irnet_socket * self = &irnet_server.s;
1700 DENTER(IRDA_OCB_TRACE, "(self=0x%p)\n", self);
1701 DASSERT(priv == &irnet_server, , IRDA_OCB_ERROR,
1702 "Invalid instance (0x%p) !!!\n", priv);
1704 DEBUG(IRDA_OCB_INFO, "IrNET/IrLAN node %s expired...\n",
1707 /* Notify the control channel */
1708 irnet_post_event(NULL, IRNET_EXPIRE,
1709 expiry->saddr, expiry->daddr, expiry->info,
1710 get_unaligned((__u16 *)expiry->hints));
1712 DEXIT(IRDA_OCB_TRACE, "\n");
1714 #endif /* DISCOVERY_EVENTS */
1717 /*********************** PROC ENTRY CALLBACKS ***********************/
1719 * We create a instance in the /proc filesystem, and here we take care
1723 #ifdef CONFIG_PROC_FS
1725 irnet_proc_show(struct seq_file *m, void *v)
1727 irnet_socket * self;
1731 /* Get the IrNET server information... */
1732 seq_printf(m, "IrNET server - ");
1733 seq_printf(m, "IrDA state: %s, ",
1734 (irnet_server.running ? "running" : "dead"));
1735 seq_printf(m, "stsap_sel: %02x, ", irnet_server.s.stsap_sel);
1736 seq_printf(m, "dtsap_sel: %02x\n", irnet_server.s.dtsap_sel);
1738 /* Do we need to continue ? */
1739 if(!irnet_server.running)
1742 /* Protect access to the instance list */
1743 spin_lock_bh(&irnet_server.spinlock);
1745 /* Get the sockets one by one... */
1746 self = (irnet_socket *) hashbin_get_first(irnet_server.list);
1749 /* Start printing info about the socket. */
1750 seq_printf(m, "\nIrNET socket %d - ", i++);
1752 /* First, get the requested configuration */
1753 seq_printf(m, "Requested IrDA name: \"%s\", ", self->rname);
1754 seq_printf(m, "daddr: %08x, ", self->rdaddr);
1755 seq_printf(m, "saddr: %08x\n", self->rsaddr);
1757 /* Second, get all the PPP info */
1758 seq_printf(m, " PPP state: %s",
1759 (self->ppp_open ? "registered" : "unregistered"));
1762 seq_printf(m, ", unit: ppp%d",
1763 ppp_unit_number(&self->chan));
1764 seq_printf(m, ", channel: %d",
1765 ppp_channel_index(&self->chan));
1766 seq_printf(m, ", mru: %d",
1768 /* Maybe add self->flags ? Later... */
1771 /* Then, get all the IrDA specific info... */
1773 state = "connected";
1775 if(self->tsap != NULL)
1776 state = "connecting";
1778 if(self->iriap != NULL)
1779 state = "searching";
1781 if(self->ttp_connect)
1785 seq_printf(m, "\n IrDA state: %s, ", state);
1786 seq_printf(m, "daddr: %08x, ", self->daddr);
1787 seq_printf(m, "stsap_sel: %02x, ", self->stsap_sel);
1788 seq_printf(m, "dtsap_sel: %02x\n", self->dtsap_sel);
1790 /* Next socket, please... */
1791 self = (irnet_socket *) hashbin_get_next(irnet_server.list);
1795 spin_unlock_bh(&irnet_server.spinlock);
1800 static int irnet_proc_open(struct inode *inode, struct file *file)
1802 return single_open(file, irnet_proc_show, NULL);
1805 static const struct file_operations irnet_proc_fops = {
1806 .owner = THIS_MODULE,
1807 .open = irnet_proc_open,
1809 .llseek = seq_lseek,
1810 .release = single_release,
1812 #endif /* PROC_FS */
1815 /********************** CONFIGURATION/CLEANUP **********************/
1817 * Initialisation and teardown of the IrDA part, called at module
1818 * insertion and removal...
1821 /*------------------------------------------------------------------*/
1823 * Prepare the IrNET layer for operation...
1826 irda_irnet_init(void)
1830 DENTER(MODULE_TRACE, "()\n");
1832 /* Pure paranoia - should be redundant */
1833 memset(&irnet_server, 0, sizeof(struct irnet_root));
1835 /* Setup start of irnet instance list */
1836 irnet_server.list = hashbin_new(HB_NOLOCK);
1837 DABORT(irnet_server.list == NULL, -ENOMEM,
1838 MODULE_ERROR, "Can't allocate hashbin!\n");
1839 /* Init spinlock for instance list */
1840 spin_lock_init(&irnet_server.spinlock);
1842 /* Initialise control channel */
1843 init_waitqueue_head(&irnet_events.rwait);
1844 irnet_events.index = 0;
1845 /* Init spinlock for event logging */
1846 spin_lock_init(&irnet_events.spinlock);
1848 #ifdef CONFIG_PROC_FS
1849 /* Add a /proc file for irnet infos */
1850 proc_create("irnet", 0, proc_irda, &irnet_proc_fops);
1851 #endif /* CONFIG_PROC_FS */
1853 /* Setup the IrNET server */
1854 err = irnet_setup_server();
1857 /* We are no longer functional... */
1858 irnet_server.running = 1;
1860 DEXIT(MODULE_TRACE, "\n");
1864 /*------------------------------------------------------------------*/
1866 * Cleanup at exit...
1869 irda_irnet_cleanup(void)
1871 DENTER(MODULE_TRACE, "()\n");
1873 /* We are no longer there... */
1874 irnet_server.running = 0;
1876 #ifdef CONFIG_PROC_FS
1877 /* Remove our /proc file */
1878 remove_proc_entry("irnet", proc_irda);
1879 #endif /* CONFIG_PROC_FS */
1881 /* Remove our IrNET server from existence */
1882 irnet_destroy_server();
1884 /* Remove all instances of IrNET socket still present */
1885 hashbin_delete(irnet_server.list, (FREE_FUNC) irda_irnet_destroy);
1887 DEXIT(MODULE_TRACE, "\n");