Make sure omap cpufreq driver initializes after cpufreq framework and governors
[linux-ginger.git] / net / irda / af_irda.c
blobdd35641835f408d161a7d1c70550a15f1a0309ac
1 /*********************************************************************
3 * Filename: af_irda.c
4 * Version: 0.9
5 * Description: IrDA sockets implementation
6 * Status: Stable
7 * Author: Dag Brattli <dagb@cs.uit.no>
8 * Created at: Sun May 31 10:12:43 1998
9 * Modified at: Sat Dec 25 21:10:23 1999
10 * Modified by: Dag Brattli <dag@brattli.net>
11 * Sources: af_netroom.c, af_ax25.c, af_rose.c, af_x25.c etc.
13 * Copyright (c) 1999 Dag Brattli <dagb@cs.uit.no>
14 * Copyright (c) 1999-2003 Jean Tourrilhes <jt@hpl.hp.com>
15 * All Rights Reserved.
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License as
19 * published by the Free Software Foundation; either version 2 of
20 * the License, or (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30 * MA 02111-1307 USA
32 * Linux-IrDA now supports four different types of IrDA sockets:
34 * o SOCK_STREAM: TinyTP connections with SAR disabled. The
35 * max SDU size is 0 for conn. of this type
36 * o SOCK_SEQPACKET: TinyTP connections with SAR enabled. TTP may
37 * fragment the messages, but will preserve
38 * the message boundaries
39 * o SOCK_DGRAM: IRDAPROTO_UNITDATA: TinyTP connections with Unitdata
40 * (unreliable) transfers
41 * IRDAPROTO_ULTRA: Connectionless and unreliable data
43 ********************************************************************/
45 #include <linux/capability.h>
46 #include <linux/module.h>
47 #include <linux/types.h>
48 #include <linux/smp_lock.h>
49 #include <linux/socket.h>
50 #include <linux/sockios.h>
51 #include <linux/init.h>
52 #include <linux/net.h>
53 #include <linux/irda.h>
54 #include <linux/poll.h>
56 #include <asm/ioctls.h> /* TIOCOUTQ, TIOCINQ */
57 #include <asm/uaccess.h>
59 #include <net/sock.h>
60 #include <net/tcp_states.h>
62 #include <net/irda/af_irda.h>
64 static int irda_create(struct net *net, struct socket *sock, int protocol);
66 static const struct proto_ops irda_stream_ops;
67 static const struct proto_ops irda_seqpacket_ops;
68 static const struct proto_ops irda_dgram_ops;
70 #ifdef CONFIG_IRDA_ULTRA
71 static const struct proto_ops irda_ultra_ops;
72 #define ULTRA_MAX_DATA 382
73 #endif /* CONFIG_IRDA_ULTRA */
75 #define IRDA_MAX_HEADER (TTP_MAX_HEADER)
78 * Function irda_data_indication (instance, sap, skb)
80 * Received some data from TinyTP. Just queue it on the receive queue
83 static int irda_data_indication(void *instance, void *sap, struct sk_buff *skb)
85 struct irda_sock *self;
86 struct sock *sk;
87 int err;
89 IRDA_DEBUG(3, "%s()\n", __func__);
91 self = instance;
92 sk = instance;
94 err = sock_queue_rcv_skb(sk, skb);
95 if (err) {
96 IRDA_DEBUG(1, "%s(), error: no more mem!\n", __func__);
97 self->rx_flow = FLOW_STOP;
99 /* When we return error, TTP will need to requeue the skb */
100 return err;
103 return 0;
107 * Function irda_disconnect_indication (instance, sap, reason, skb)
109 * Connection has been closed. Check reason to find out why
112 static void irda_disconnect_indication(void *instance, void *sap,
113 LM_REASON reason, struct sk_buff *skb)
115 struct irda_sock *self;
116 struct sock *sk;
118 self = instance;
120 IRDA_DEBUG(2, "%s(%p)\n", __func__, self);
122 /* Don't care about it, but let's not leak it */
123 if(skb)
124 dev_kfree_skb(skb);
126 sk = instance;
127 if (sk == NULL) {
128 IRDA_DEBUG(0, "%s(%p) : BUG : sk is NULL\n",
129 __func__, self);
130 return;
133 /* Prevent race conditions with irda_release() and irda_shutdown() */
134 bh_lock_sock(sk);
135 if (!sock_flag(sk, SOCK_DEAD) && sk->sk_state != TCP_CLOSE) {
136 sk->sk_state = TCP_CLOSE;
137 sk->sk_shutdown |= SEND_SHUTDOWN;
139 sk->sk_state_change(sk);
141 /* Close our TSAP.
142 * If we leave it open, IrLMP put it back into the list of
143 * unconnected LSAPs. The problem is that any incoming request
144 * can then be matched to this socket (and it will be, because
145 * it is at the head of the list). This would prevent any
146 * listening socket waiting on the same TSAP to get those
147 * requests. Some apps forget to close sockets, or hang to it
148 * a bit too long, so we may stay in this dead state long
149 * enough to be noticed...
150 * Note : all socket function do check sk->sk_state, so we are
151 * safe...
152 * Jean II
154 if (self->tsap) {
155 irttp_close_tsap(self->tsap);
156 self->tsap = NULL;
159 bh_unlock_sock(sk);
161 /* Note : once we are there, there is not much you want to do
162 * with the socket anymore, apart from closing it.
163 * For example, bind() and connect() won't reset sk->sk_err,
164 * sk->sk_shutdown and sk->sk_flags to valid values...
165 * Jean II
170 * Function irda_connect_confirm (instance, sap, qos, max_sdu_size, skb)
172 * Connections has been confirmed by the remote device
175 static void irda_connect_confirm(void *instance, void *sap,
176 struct qos_info *qos,
177 __u32 max_sdu_size, __u8 max_header_size,
178 struct sk_buff *skb)
180 struct irda_sock *self;
181 struct sock *sk;
183 self = instance;
185 IRDA_DEBUG(2, "%s(%p)\n", __func__, self);
187 sk = instance;
188 if (sk == NULL) {
189 dev_kfree_skb(skb);
190 return;
193 dev_kfree_skb(skb);
194 // Should be ??? skb_queue_tail(&sk->sk_receive_queue, skb);
196 /* How much header space do we need to reserve */
197 self->max_header_size = max_header_size;
199 /* IrTTP max SDU size in transmit direction */
200 self->max_sdu_size_tx = max_sdu_size;
202 /* Find out what the largest chunk of data that we can transmit is */
203 switch (sk->sk_type) {
204 case SOCK_STREAM:
205 if (max_sdu_size != 0) {
206 IRDA_ERROR("%s: max_sdu_size must be 0\n",
207 __func__);
208 return;
210 self->max_data_size = irttp_get_max_seg_size(self->tsap);
211 break;
212 case SOCK_SEQPACKET:
213 if (max_sdu_size == 0) {
214 IRDA_ERROR("%s: max_sdu_size cannot be 0\n",
215 __func__);
216 return;
218 self->max_data_size = max_sdu_size;
219 break;
220 default:
221 self->max_data_size = irttp_get_max_seg_size(self->tsap);
224 IRDA_DEBUG(2, "%s(), max_data_size=%d\n", __func__,
225 self->max_data_size);
227 memcpy(&self->qos_tx, qos, sizeof(struct qos_info));
229 /* We are now connected! */
230 sk->sk_state = TCP_ESTABLISHED;
231 sk->sk_state_change(sk);
235 * Function irda_connect_indication(instance, sap, qos, max_sdu_size, userdata)
237 * Incoming connection
240 static void irda_connect_indication(void *instance, void *sap,
241 struct qos_info *qos, __u32 max_sdu_size,
242 __u8 max_header_size, struct sk_buff *skb)
244 struct irda_sock *self;
245 struct sock *sk;
247 self = instance;
249 IRDA_DEBUG(2, "%s(%p)\n", __func__, self);
251 sk = instance;
252 if (sk == NULL) {
253 dev_kfree_skb(skb);
254 return;
257 /* How much header space do we need to reserve */
258 self->max_header_size = max_header_size;
260 /* IrTTP max SDU size in transmit direction */
261 self->max_sdu_size_tx = max_sdu_size;
263 /* Find out what the largest chunk of data that we can transmit is */
264 switch (sk->sk_type) {
265 case SOCK_STREAM:
266 if (max_sdu_size != 0) {
267 IRDA_ERROR("%s: max_sdu_size must be 0\n",
268 __func__);
269 kfree_skb(skb);
270 return;
272 self->max_data_size = irttp_get_max_seg_size(self->tsap);
273 break;
274 case SOCK_SEQPACKET:
275 if (max_sdu_size == 0) {
276 IRDA_ERROR("%s: max_sdu_size cannot be 0\n",
277 __func__);
278 kfree_skb(skb);
279 return;
281 self->max_data_size = max_sdu_size;
282 break;
283 default:
284 self->max_data_size = irttp_get_max_seg_size(self->tsap);
287 IRDA_DEBUG(2, "%s(), max_data_size=%d\n", __func__,
288 self->max_data_size);
290 memcpy(&self->qos_tx, qos, sizeof(struct qos_info));
292 skb_queue_tail(&sk->sk_receive_queue, skb);
293 sk->sk_state_change(sk);
297 * Function irda_connect_response (handle)
299 * Accept incoming connection
302 static void irda_connect_response(struct irda_sock *self)
304 struct sk_buff *skb;
306 IRDA_DEBUG(2, "%s()\n", __func__);
308 skb = alloc_skb(TTP_MAX_HEADER + TTP_SAR_HEADER,
309 GFP_ATOMIC);
310 if (skb == NULL) {
311 IRDA_DEBUG(0, "%s() Unable to allocate sk_buff!\n",
312 __func__);
313 return;
316 /* Reserve space for MUX_CONTROL and LAP header */
317 skb_reserve(skb, IRDA_MAX_HEADER);
319 irttp_connect_response(self->tsap, self->max_sdu_size_rx, skb);
323 * Function irda_flow_indication (instance, sap, flow)
325 * Used by TinyTP to tell us if it can accept more data or not
328 static void irda_flow_indication(void *instance, void *sap, LOCAL_FLOW flow)
330 struct irda_sock *self;
331 struct sock *sk;
333 IRDA_DEBUG(2, "%s()\n", __func__);
335 self = instance;
336 sk = instance;
337 BUG_ON(sk == NULL);
339 switch (flow) {
340 case FLOW_STOP:
341 IRDA_DEBUG(1, "%s(), IrTTP wants us to slow down\n",
342 __func__);
343 self->tx_flow = flow;
344 break;
345 case FLOW_START:
346 self->tx_flow = flow;
347 IRDA_DEBUG(1, "%s(), IrTTP wants us to start again\n",
348 __func__);
349 wake_up_interruptible(sk->sk_sleep);
350 break;
351 default:
352 IRDA_DEBUG(0, "%s(), Unknown flow command!\n", __func__);
353 /* Unknown flow command, better stop */
354 self->tx_flow = flow;
355 break;
360 * Function irda_getvalue_confirm (obj_id, value, priv)
362 * Got answer from remote LM-IAS, just pass object to requester...
364 * Note : duplicate from above, but we need our own version that
365 * doesn't touch the dtsap_sel and save the full value structure...
367 static void irda_getvalue_confirm(int result, __u16 obj_id,
368 struct ias_value *value, void *priv)
370 struct irda_sock *self;
372 self = (struct irda_sock *) priv;
373 if (!self) {
374 IRDA_WARNING("%s: lost myself!\n", __func__);
375 return;
378 IRDA_DEBUG(2, "%s(%p)\n", __func__, self);
380 /* We probably don't need to make any more queries */
381 iriap_close(self->iriap);
382 self->iriap = NULL;
384 /* Check if request succeeded */
385 if (result != IAS_SUCCESS) {
386 IRDA_DEBUG(1, "%s(), IAS query failed! (%d)\n", __func__,
387 result);
389 self->errno = result; /* We really need it later */
391 /* Wake up any processes waiting for result */
392 wake_up_interruptible(&self->query_wait);
394 return;
397 /* Pass the object to the caller (so the caller must delete it) */
398 self->ias_result = value;
399 self->errno = 0;
401 /* Wake up any processes waiting for result */
402 wake_up_interruptible(&self->query_wait);
406 * Function irda_selective_discovery_indication (discovery)
408 * Got a selective discovery indication from IrLMP.
410 * IrLMP is telling us that this node is new and matching our hint bit
411 * filter. Wake up any process waiting for answer...
413 static void irda_selective_discovery_indication(discinfo_t *discovery,
414 DISCOVERY_MODE mode,
415 void *priv)
417 struct irda_sock *self;
419 IRDA_DEBUG(2, "%s()\n", __func__);
421 self = (struct irda_sock *) priv;
422 if (!self) {
423 IRDA_WARNING("%s: lost myself!\n", __func__);
424 return;
427 /* Pass parameter to the caller */
428 self->cachedaddr = discovery->daddr;
430 /* Wake up process if its waiting for device to be discovered */
431 wake_up_interruptible(&self->query_wait);
435 * Function irda_discovery_timeout (priv)
437 * Timeout in the selective discovery process
439 * We were waiting for a node to be discovered, but nothing has come up
440 * so far. Wake up the user and tell him that we failed...
442 static void irda_discovery_timeout(u_long priv)
444 struct irda_sock *self;
446 IRDA_DEBUG(2, "%s()\n", __func__);
448 self = (struct irda_sock *) priv;
449 BUG_ON(self == NULL);
451 /* Nothing for the caller */
452 self->cachelog = NULL;
453 self->cachedaddr = 0;
454 self->errno = -ETIME;
456 /* Wake up process if its still waiting... */
457 wake_up_interruptible(&self->query_wait);
461 * Function irda_open_tsap (self)
463 * Open local Transport Service Access Point (TSAP)
466 static int irda_open_tsap(struct irda_sock *self, __u8 tsap_sel, char *name)
468 notify_t notify;
470 if (self->tsap) {
471 IRDA_WARNING("%s: busy!\n", __func__);
472 return -EBUSY;
475 /* Initialize callbacks to be used by the IrDA stack */
476 irda_notify_init(&notify);
477 notify.connect_confirm = irda_connect_confirm;
478 notify.connect_indication = irda_connect_indication;
479 notify.disconnect_indication = irda_disconnect_indication;
480 notify.data_indication = irda_data_indication;
481 notify.udata_indication = irda_data_indication;
482 notify.flow_indication = irda_flow_indication;
483 notify.instance = self;
484 strncpy(notify.name, name, NOTIFY_MAX_NAME);
486 self->tsap = irttp_open_tsap(tsap_sel, DEFAULT_INITIAL_CREDIT,
487 &notify);
488 if (self->tsap == NULL) {
489 IRDA_DEBUG(0, "%s(), Unable to allocate TSAP!\n",
490 __func__);
491 return -ENOMEM;
493 /* Remember which TSAP selector we actually got */
494 self->stsap_sel = self->tsap->stsap_sel;
496 return 0;
500 * Function irda_open_lsap (self)
502 * Open local Link Service Access Point (LSAP). Used for opening Ultra
503 * sockets
505 #ifdef CONFIG_IRDA_ULTRA
506 static int irda_open_lsap(struct irda_sock *self, int pid)
508 notify_t notify;
510 if (self->lsap) {
511 IRDA_WARNING("%s(), busy!\n", __func__);
512 return -EBUSY;
515 /* Initialize callbacks to be used by the IrDA stack */
516 irda_notify_init(&notify);
517 notify.udata_indication = irda_data_indication;
518 notify.instance = self;
519 strncpy(notify.name, "Ultra", NOTIFY_MAX_NAME);
521 self->lsap = irlmp_open_lsap(LSAP_CONNLESS, &notify, pid);
522 if (self->lsap == NULL) {
523 IRDA_DEBUG( 0, "%s(), Unable to allocate LSAP!\n", __func__);
524 return -ENOMEM;
527 return 0;
529 #endif /* CONFIG_IRDA_ULTRA */
532 * Function irda_find_lsap_sel (self, name)
534 * Try to lookup LSAP selector in remote LM-IAS
536 * Basically, we start a IAP query, and then go to sleep. When the query
537 * return, irda_getvalue_confirm will wake us up, and we can examine the
538 * result of the query...
539 * Note that in some case, the query fail even before we go to sleep,
540 * creating some races...
542 static int irda_find_lsap_sel(struct irda_sock *self, char *name)
544 IRDA_DEBUG(2, "%s(%p, %s)\n", __func__, self, name);
546 if (self->iriap) {
547 IRDA_WARNING("%s(): busy with a previous query\n",
548 __func__);
549 return -EBUSY;
552 self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
553 irda_getvalue_confirm);
554 if(self->iriap == NULL)
555 return -ENOMEM;
557 /* Treat unexpected wakeup as disconnect */
558 self->errno = -EHOSTUNREACH;
560 /* Query remote LM-IAS */
561 iriap_getvaluebyclass_request(self->iriap, self->saddr, self->daddr,
562 name, "IrDA:TinyTP:LsapSel");
564 /* Wait for answer, if not yet finished (or failed) */
565 if (wait_event_interruptible(self->query_wait, (self->iriap==NULL)))
566 /* Treat signals as disconnect */
567 return -EHOSTUNREACH;
569 /* Check what happened */
570 if (self->errno)
572 /* Requested object/attribute doesn't exist */
573 if((self->errno == IAS_CLASS_UNKNOWN) ||
574 (self->errno == IAS_ATTRIB_UNKNOWN))
575 return (-EADDRNOTAVAIL);
576 else
577 return (-EHOSTUNREACH);
580 /* Get the remote TSAP selector */
581 switch (self->ias_result->type) {
582 case IAS_INTEGER:
583 IRDA_DEBUG(4, "%s() int=%d\n",
584 __func__, self->ias_result->t.integer);
586 if (self->ias_result->t.integer != -1)
587 self->dtsap_sel = self->ias_result->t.integer;
588 else
589 self->dtsap_sel = 0;
590 break;
591 default:
592 self->dtsap_sel = 0;
593 IRDA_DEBUG(0, "%s(), bad type!\n", __func__);
594 break;
596 if (self->ias_result)
597 irias_delete_value(self->ias_result);
599 if (self->dtsap_sel)
600 return 0;
602 return -EADDRNOTAVAIL;
606 * Function irda_discover_daddr_and_lsap_sel (self, name)
608 * This try to find a device with the requested service.
610 * It basically look into the discovery log. For each address in the list,
611 * it queries the LM-IAS of the device to find if this device offer
612 * the requested service.
613 * If there is more than one node supporting the service, we complain
614 * to the user (it should move devices around).
615 * The, we set both the destination address and the lsap selector to point
616 * on the service on the unique device we have found.
618 * Note : this function fails if there is more than one device in range,
619 * because IrLMP doesn't disconnect the LAP when the last LSAP is closed.
620 * Moreover, we would need to wait the LAP disconnection...
622 static int irda_discover_daddr_and_lsap_sel(struct irda_sock *self, char *name)
624 discinfo_t *discoveries; /* Copy of the discovery log */
625 int number; /* Number of nodes in the log */
626 int i;
627 int err = -ENETUNREACH;
628 __u32 daddr = DEV_ADDR_ANY; /* Address we found the service on */
629 __u8 dtsap_sel = 0x0; /* TSAP associated with it */
631 IRDA_DEBUG(2, "%s(), name=%s\n", __func__, name);
633 /* Ask lmp for the current discovery log
634 * Note : we have to use irlmp_get_discoveries(), as opposed
635 * to play with the cachelog directly, because while we are
636 * making our ias query, le log might change... */
637 discoveries = irlmp_get_discoveries(&number, self->mask.word,
638 self->nslots);
639 /* Check if the we got some results */
640 if (discoveries == NULL)
641 return -ENETUNREACH; /* No nodes discovered */
644 * Now, check all discovered devices (if any), and connect
645 * client only about the services that the client is
646 * interested in...
648 for(i = 0; i < number; i++) {
649 /* Try the address in the log */
650 self->daddr = discoveries[i].daddr;
651 self->saddr = 0x0;
652 IRDA_DEBUG(1, "%s(), trying daddr = %08x\n",
653 __func__, self->daddr);
655 /* Query remote LM-IAS for this service */
656 err = irda_find_lsap_sel(self, name);
657 switch (err) {
658 case 0:
659 /* We found the requested service */
660 if(daddr != DEV_ADDR_ANY) {
661 IRDA_DEBUG(1, "%s(), discovered service ''%s'' in two different devices !!!\n",
662 __func__, name);
663 self->daddr = DEV_ADDR_ANY;
664 kfree(discoveries);
665 return(-ENOTUNIQ);
667 /* First time we found that one, save it ! */
668 daddr = self->daddr;
669 dtsap_sel = self->dtsap_sel;
670 break;
671 case -EADDRNOTAVAIL:
672 /* Requested service simply doesn't exist on this node */
673 break;
674 default:
675 /* Something bad did happen :-( */
676 IRDA_DEBUG(0, "%s(), unexpected IAS query failure\n", __func__);
677 self->daddr = DEV_ADDR_ANY;
678 kfree(discoveries);
679 return(-EHOSTUNREACH);
680 break;
683 /* Cleanup our copy of the discovery log */
684 kfree(discoveries);
686 /* Check out what we found */
687 if(daddr == DEV_ADDR_ANY) {
688 IRDA_DEBUG(1, "%s(), cannot discover service ''%s'' in any device !!!\n",
689 __func__, name);
690 self->daddr = DEV_ADDR_ANY;
691 return(-EADDRNOTAVAIL);
694 /* Revert back to discovered device & service */
695 self->daddr = daddr;
696 self->saddr = 0x0;
697 self->dtsap_sel = dtsap_sel;
699 IRDA_DEBUG(1, "%s(), discovered requested service ''%s'' at address %08x\n",
700 __func__, name, self->daddr);
702 return 0;
706 * Function irda_getname (sock, uaddr, uaddr_len, peer)
708 * Return the our own, or peers socket address (sockaddr_irda)
711 static int irda_getname(struct socket *sock, struct sockaddr *uaddr,
712 int *uaddr_len, int peer)
714 struct sockaddr_irda saddr;
715 struct sock *sk = sock->sk;
716 struct irda_sock *self = irda_sk(sk);
718 memset(&saddr, 0, sizeof(saddr));
719 if (peer) {
720 if (sk->sk_state != TCP_ESTABLISHED)
721 return -ENOTCONN;
723 saddr.sir_family = AF_IRDA;
724 saddr.sir_lsap_sel = self->dtsap_sel;
725 saddr.sir_addr = self->daddr;
726 } else {
727 saddr.sir_family = AF_IRDA;
728 saddr.sir_lsap_sel = self->stsap_sel;
729 saddr.sir_addr = self->saddr;
732 IRDA_DEBUG(1, "%s(), tsap_sel = %#x\n", __func__, saddr.sir_lsap_sel);
733 IRDA_DEBUG(1, "%s(), addr = %08x\n", __func__, saddr.sir_addr);
735 /* uaddr_len come to us uninitialised */
736 *uaddr_len = sizeof (struct sockaddr_irda);
737 memcpy(uaddr, &saddr, *uaddr_len);
739 return 0;
743 * Function irda_listen (sock, backlog)
745 * Just move to the listen state
748 static int irda_listen(struct socket *sock, int backlog)
750 struct sock *sk = sock->sk;
752 IRDA_DEBUG(2, "%s()\n", __func__);
754 if ((sk->sk_type != SOCK_STREAM) && (sk->sk_type != SOCK_SEQPACKET) &&
755 (sk->sk_type != SOCK_DGRAM))
756 return -EOPNOTSUPP;
758 if (sk->sk_state != TCP_LISTEN) {
759 sk->sk_max_ack_backlog = backlog;
760 sk->sk_state = TCP_LISTEN;
762 return 0;
765 return -EOPNOTSUPP;
769 * Function irda_bind (sock, uaddr, addr_len)
771 * Used by servers to register their well known TSAP
774 static int irda_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
776 struct sock *sk = sock->sk;
777 struct sockaddr_irda *addr = (struct sockaddr_irda *) uaddr;
778 struct irda_sock *self = irda_sk(sk);
779 int err;
781 IRDA_DEBUG(2, "%s(%p)\n", __func__, self);
783 if (addr_len != sizeof(struct sockaddr_irda))
784 return -EINVAL;
786 #ifdef CONFIG_IRDA_ULTRA
787 /* Special care for Ultra sockets */
788 if ((sk->sk_type == SOCK_DGRAM) &&
789 (sk->sk_protocol == IRDAPROTO_ULTRA)) {
790 self->pid = addr->sir_lsap_sel;
791 if (self->pid & 0x80) {
792 IRDA_DEBUG(0, "%s(), extension in PID not supp!\n", __func__);
793 return -EOPNOTSUPP;
795 err = irda_open_lsap(self, self->pid);
796 if (err < 0)
797 return err;
799 /* Pretend we are connected */
800 sock->state = SS_CONNECTED;
801 sk->sk_state = TCP_ESTABLISHED;
803 return 0;
805 #endif /* CONFIG_IRDA_ULTRA */
807 self->ias_obj = irias_new_object(addr->sir_name, jiffies);
808 if (self->ias_obj == NULL)
809 return -ENOMEM;
811 err = irda_open_tsap(self, addr->sir_lsap_sel, addr->sir_name);
812 if (err < 0) {
813 kfree(self->ias_obj->name);
814 kfree(self->ias_obj);
815 return err;
818 /* Register with LM-IAS */
819 irias_add_integer_attrib(self->ias_obj, "IrDA:TinyTP:LsapSel",
820 self->stsap_sel, IAS_KERNEL_ATTR);
821 irias_insert_object(self->ias_obj);
823 return 0;
827 * Function irda_accept (sock, newsock, flags)
829 * Wait for incoming connection
832 static int irda_accept(struct socket *sock, struct socket *newsock, int flags)
834 struct sock *sk = sock->sk;
835 struct irda_sock *new, *self = irda_sk(sk);
836 struct sock *newsk;
837 struct sk_buff *skb;
838 int err;
840 IRDA_DEBUG(2, "%s()\n", __func__);
842 err = irda_create(sock_net(sk), newsock, sk->sk_protocol);
843 if (err)
844 return err;
846 if (sock->state != SS_UNCONNECTED)
847 return -EINVAL;
849 if ((sk = sock->sk) == NULL)
850 return -EINVAL;
852 if ((sk->sk_type != SOCK_STREAM) && (sk->sk_type != SOCK_SEQPACKET) &&
853 (sk->sk_type != SOCK_DGRAM))
854 return -EOPNOTSUPP;
856 if (sk->sk_state != TCP_LISTEN)
857 return -EINVAL;
860 * The read queue this time is holding sockets ready to use
861 * hooked into the SABM we saved
865 * We can perform the accept only if there is incoming data
866 * on the listening socket.
867 * So, we will block the caller until we receive any data.
868 * If the caller was waiting on select() or poll() before
869 * calling us, the data is waiting for us ;-)
870 * Jean II
872 while (1) {
873 skb = skb_dequeue(&sk->sk_receive_queue);
874 if (skb)
875 break;
877 /* Non blocking operation */
878 if (flags & O_NONBLOCK)
879 return -EWOULDBLOCK;
881 err = wait_event_interruptible(*(sk->sk_sleep),
882 skb_peek(&sk->sk_receive_queue));
883 if (err)
884 return err;
887 newsk = newsock->sk;
888 if (newsk == NULL)
889 return -EIO;
891 newsk->sk_state = TCP_ESTABLISHED;
893 new = irda_sk(newsk);
895 /* Now attach up the new socket */
896 new->tsap = irttp_dup(self->tsap, new);
897 if (!new->tsap) {
898 IRDA_DEBUG(0, "%s(), dup failed!\n", __func__);
899 kfree_skb(skb);
900 return -1;
903 new->stsap_sel = new->tsap->stsap_sel;
904 new->dtsap_sel = new->tsap->dtsap_sel;
905 new->saddr = irttp_get_saddr(new->tsap);
906 new->daddr = irttp_get_daddr(new->tsap);
908 new->max_sdu_size_tx = self->max_sdu_size_tx;
909 new->max_sdu_size_rx = self->max_sdu_size_rx;
910 new->max_data_size = self->max_data_size;
911 new->max_header_size = self->max_header_size;
913 memcpy(&new->qos_tx, &self->qos_tx, sizeof(struct qos_info));
915 /* Clean up the original one to keep it in listen state */
916 irttp_listen(self->tsap);
918 kfree_skb(skb);
919 sk->sk_ack_backlog--;
921 newsock->state = SS_CONNECTED;
923 irda_connect_response(new);
925 return 0;
929 * Function irda_connect (sock, uaddr, addr_len, flags)
931 * Connect to a IrDA device
933 * The main difference with a "standard" connect is that with IrDA we need
934 * to resolve the service name into a TSAP selector (in TCP, port number
935 * doesn't have to be resolved).
936 * Because of this service name resoltion, we can offer "auto-connect",
937 * where we connect to a service without specifying a destination address.
939 * Note : by consulting "errno", the user space caller may learn the cause
940 * of the failure. Most of them are visible in the function, others may come
941 * from subroutines called and are listed here :
942 * o EBUSY : already processing a connect
943 * o EHOSTUNREACH : bad addr->sir_addr argument
944 * o EADDRNOTAVAIL : bad addr->sir_name argument
945 * o ENOTUNIQ : more than one node has addr->sir_name (auto-connect)
946 * o ENETUNREACH : no node found on the network (auto-connect)
948 static int irda_connect(struct socket *sock, struct sockaddr *uaddr,
949 int addr_len, int flags)
951 struct sock *sk = sock->sk;
952 struct sockaddr_irda *addr = (struct sockaddr_irda *) uaddr;
953 struct irda_sock *self = irda_sk(sk);
954 int err;
956 IRDA_DEBUG(2, "%s(%p)\n", __func__, self);
958 /* Don't allow connect for Ultra sockets */
959 if ((sk->sk_type == SOCK_DGRAM) && (sk->sk_protocol == IRDAPROTO_ULTRA))
960 return -ESOCKTNOSUPPORT;
962 if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
963 sock->state = SS_CONNECTED;
964 return 0; /* Connect completed during a ERESTARTSYS event */
967 if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
968 sock->state = SS_UNCONNECTED;
969 return -ECONNREFUSED;
972 if (sk->sk_state == TCP_ESTABLISHED)
973 return -EISCONN; /* No reconnect on a seqpacket socket */
975 sk->sk_state = TCP_CLOSE;
976 sock->state = SS_UNCONNECTED;
978 if (addr_len != sizeof(struct sockaddr_irda))
979 return -EINVAL;
981 /* Check if user supplied any destination device address */
982 if ((!addr->sir_addr) || (addr->sir_addr == DEV_ADDR_ANY)) {
983 /* Try to find one suitable */
984 err = irda_discover_daddr_and_lsap_sel(self, addr->sir_name);
985 if (err) {
986 IRDA_DEBUG(0, "%s(), auto-connect failed!\n", __func__);
987 return err;
989 } else {
990 /* Use the one provided by the user */
991 self->daddr = addr->sir_addr;
992 IRDA_DEBUG(1, "%s(), daddr = %08x\n", __func__, self->daddr);
994 /* If we don't have a valid service name, we assume the
995 * user want to connect on a specific LSAP. Prevent
996 * the use of invalid LSAPs (IrLMP 1.1 p10). Jean II */
997 if((addr->sir_name[0] != '\0') ||
998 (addr->sir_lsap_sel >= 0x70)) {
999 /* Query remote LM-IAS using service name */
1000 err = irda_find_lsap_sel(self, addr->sir_name);
1001 if (err) {
1002 IRDA_DEBUG(0, "%s(), connect failed!\n", __func__);
1003 return err;
1005 } else {
1006 /* Directly connect to the remote LSAP
1007 * specified by the sir_lsap field.
1008 * Please use with caution, in IrDA LSAPs are
1009 * dynamic and there is no "well-known" LSAP. */
1010 self->dtsap_sel = addr->sir_lsap_sel;
1014 /* Check if we have opened a local TSAP */
1015 if (!self->tsap)
1016 irda_open_tsap(self, LSAP_ANY, addr->sir_name);
1018 /* Move to connecting socket, start sending Connect Requests */
1019 sock->state = SS_CONNECTING;
1020 sk->sk_state = TCP_SYN_SENT;
1022 /* Connect to remote device */
1023 err = irttp_connect_request(self->tsap, self->dtsap_sel,
1024 self->saddr, self->daddr, NULL,
1025 self->max_sdu_size_rx, NULL);
1026 if (err) {
1027 IRDA_DEBUG(0, "%s(), connect failed!\n", __func__);
1028 return err;
1031 /* Now the loop */
1032 if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
1033 return -EINPROGRESS;
1035 if (wait_event_interruptible(*(sk->sk_sleep),
1036 (sk->sk_state != TCP_SYN_SENT)))
1037 return -ERESTARTSYS;
1039 if (sk->sk_state != TCP_ESTABLISHED) {
1040 sock->state = SS_UNCONNECTED;
1041 err = sock_error(sk);
1042 return err? err : -ECONNRESET;
1045 sock->state = SS_CONNECTED;
1047 /* At this point, IrLMP has assigned our source address */
1048 self->saddr = irttp_get_saddr(self->tsap);
1050 return 0;
1053 static struct proto irda_proto = {
1054 .name = "IRDA",
1055 .owner = THIS_MODULE,
1056 .obj_size = sizeof(struct irda_sock),
1060 * Function irda_create (sock, protocol)
1062 * Create IrDA socket
1065 static int irda_create(struct net *net, struct socket *sock, int protocol)
1067 struct sock *sk;
1068 struct irda_sock *self;
1070 IRDA_DEBUG(2, "%s()\n", __func__);
1072 if (net != &init_net)
1073 return -EAFNOSUPPORT;
1075 /* Check for valid socket type */
1076 switch (sock->type) {
1077 case SOCK_STREAM: /* For TTP connections with SAR disabled */
1078 case SOCK_SEQPACKET: /* For TTP connections with SAR enabled */
1079 case SOCK_DGRAM: /* For TTP Unitdata or LMP Ultra transfers */
1080 break;
1081 default:
1082 return -ESOCKTNOSUPPORT;
1085 /* Allocate networking socket */
1086 sk = sk_alloc(net, PF_IRDA, GFP_ATOMIC, &irda_proto);
1087 if (sk == NULL)
1088 return -ENOMEM;
1090 self = irda_sk(sk);
1091 IRDA_DEBUG(2, "%s() : self is %p\n", __func__, self);
1093 init_waitqueue_head(&self->query_wait);
1095 switch (sock->type) {
1096 case SOCK_STREAM:
1097 sock->ops = &irda_stream_ops;
1098 self->max_sdu_size_rx = TTP_SAR_DISABLE;
1099 break;
1100 case SOCK_SEQPACKET:
1101 sock->ops = &irda_seqpacket_ops;
1102 self->max_sdu_size_rx = TTP_SAR_UNBOUND;
1103 break;
1104 case SOCK_DGRAM:
1105 switch (protocol) {
1106 #ifdef CONFIG_IRDA_ULTRA
1107 case IRDAPROTO_ULTRA:
1108 sock->ops = &irda_ultra_ops;
1109 /* Initialise now, because we may send on unbound
1110 * sockets. Jean II */
1111 self->max_data_size = ULTRA_MAX_DATA - LMP_PID_HEADER;
1112 self->max_header_size = IRDA_MAX_HEADER + LMP_PID_HEADER;
1113 break;
1114 #endif /* CONFIG_IRDA_ULTRA */
1115 case IRDAPROTO_UNITDATA:
1116 sock->ops = &irda_dgram_ops;
1117 /* We let Unitdata conn. be like seqpack conn. */
1118 self->max_sdu_size_rx = TTP_SAR_UNBOUND;
1119 break;
1120 default:
1121 sk_free(sk);
1122 return -ESOCKTNOSUPPORT;
1124 break;
1125 default:
1126 sk_free(sk);
1127 return -ESOCKTNOSUPPORT;
1130 /* Initialise networking socket struct */
1131 sock_init_data(sock, sk); /* Note : set sk->sk_refcnt to 1 */
1132 sk->sk_family = PF_IRDA;
1133 sk->sk_protocol = protocol;
1135 /* Register as a client with IrLMP */
1136 self->ckey = irlmp_register_client(0, NULL, NULL, NULL);
1137 self->mask.word = 0xffff;
1138 self->rx_flow = self->tx_flow = FLOW_START;
1139 self->nslots = DISCOVERY_DEFAULT_SLOTS;
1140 self->daddr = DEV_ADDR_ANY; /* Until we get connected */
1141 self->saddr = 0x0; /* so IrLMP assign us any link */
1142 return 0;
1146 * Function irda_destroy_socket (self)
1148 * Destroy socket
1151 static void irda_destroy_socket(struct irda_sock *self)
1153 IRDA_DEBUG(2, "%s(%p)\n", __func__, self);
1155 /* Unregister with IrLMP */
1156 irlmp_unregister_client(self->ckey);
1157 irlmp_unregister_service(self->skey);
1159 /* Unregister with LM-IAS */
1160 if (self->ias_obj) {
1161 irias_delete_object(self->ias_obj);
1162 self->ias_obj = NULL;
1165 if (self->iriap) {
1166 iriap_close(self->iriap);
1167 self->iriap = NULL;
1170 if (self->tsap) {
1171 irttp_disconnect_request(self->tsap, NULL, P_NORMAL);
1172 irttp_close_tsap(self->tsap);
1173 self->tsap = NULL;
1175 #ifdef CONFIG_IRDA_ULTRA
1176 if (self->lsap) {
1177 irlmp_close_lsap(self->lsap);
1178 self->lsap = NULL;
1180 #endif /* CONFIG_IRDA_ULTRA */
1184 * Function irda_release (sock)
1186 static int irda_release(struct socket *sock)
1188 struct sock *sk = sock->sk;
1190 IRDA_DEBUG(2, "%s()\n", __func__);
1192 if (sk == NULL)
1193 return 0;
1195 lock_sock(sk);
1196 sk->sk_state = TCP_CLOSE;
1197 sk->sk_shutdown |= SEND_SHUTDOWN;
1198 sk->sk_state_change(sk);
1200 /* Destroy IrDA socket */
1201 irda_destroy_socket(irda_sk(sk));
1203 sock_orphan(sk);
1204 sock->sk = NULL;
1205 release_sock(sk);
1207 /* Purge queues (see sock_init_data()) */
1208 skb_queue_purge(&sk->sk_receive_queue);
1210 /* Destroy networking socket if we are the last reference on it,
1211 * i.e. if(sk->sk_refcnt == 0) -> sk_free(sk) */
1212 sock_put(sk);
1214 /* Notes on socket locking and deallocation... - Jean II
1215 * In theory we should put pairs of sock_hold() / sock_put() to
1216 * prevent the socket to be destroyed whenever there is an
1217 * outstanding request or outstanding incoming packet or event.
1219 * 1) This may include IAS request, both in connect and getsockopt.
1220 * Unfortunately, the situation is a bit more messy than it looks,
1221 * because we close iriap and kfree(self) above.
1223 * 2) This may include selective discovery in getsockopt.
1224 * Same stuff as above, irlmp registration and self are gone.
1226 * Probably 1 and 2 may not matter, because it's all triggered
1227 * by a process and the socket layer already prevent the
1228 * socket to go away while a process is holding it, through
1229 * sockfd_put() and fput()...
1231 * 3) This may include deferred TSAP closure. In particular,
1232 * we may receive a late irda_disconnect_indication()
1233 * Fortunately, (tsap_cb *)->close_pend should protect us
1234 * from that.
1236 * I did some testing on SMP, and it looks solid. And the socket
1237 * memory leak is now gone... - Jean II
1240 return 0;
1244 * Function irda_sendmsg (iocb, sock, msg, len)
1246 * Send message down to TinyTP. This function is used for both STREAM and
1247 * SEQPACK services. This is possible since it forces the client to
1248 * fragment the message if necessary
1250 static int irda_sendmsg(struct kiocb *iocb, struct socket *sock,
1251 struct msghdr *msg, size_t len)
1253 struct sock *sk = sock->sk;
1254 struct irda_sock *self;
1255 struct sk_buff *skb;
1256 int err = -EPIPE;
1258 IRDA_DEBUG(4, "%s(), len=%zd\n", __func__, len);
1260 /* Note : socket.c set MSG_EOR on SEQPACKET sockets */
1261 if (msg->msg_flags & ~(MSG_DONTWAIT | MSG_EOR | MSG_CMSG_COMPAT |
1262 MSG_NOSIGNAL))
1263 return -EINVAL;
1265 if (sk->sk_shutdown & SEND_SHUTDOWN)
1266 goto out_err;
1268 if (sk->sk_state != TCP_ESTABLISHED)
1269 return -ENOTCONN;
1271 self = irda_sk(sk);
1273 /* Check if IrTTP is wants us to slow down */
1275 if (wait_event_interruptible(*(sk->sk_sleep),
1276 (self->tx_flow != FLOW_STOP || sk->sk_state != TCP_ESTABLISHED)))
1277 return -ERESTARTSYS;
1279 /* Check if we are still connected */
1280 if (sk->sk_state != TCP_ESTABLISHED)
1281 return -ENOTCONN;
1283 /* Check that we don't send out too big frames */
1284 if (len > self->max_data_size) {
1285 IRDA_DEBUG(2, "%s(), Chopping frame from %zd to %d bytes!\n",
1286 __func__, len, self->max_data_size);
1287 len = self->max_data_size;
1290 skb = sock_alloc_send_skb(sk, len + self->max_header_size + 16,
1291 msg->msg_flags & MSG_DONTWAIT, &err);
1292 if (!skb)
1293 goto out_err;
1295 skb_reserve(skb, self->max_header_size + 16);
1296 skb_reset_transport_header(skb);
1297 skb_put(skb, len);
1298 err = memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len);
1299 if (err) {
1300 kfree_skb(skb);
1301 goto out_err;
1305 * Just send the message to TinyTP, and let it deal with possible
1306 * errors. No need to duplicate all that here
1308 err = irttp_data_request(self->tsap, skb);
1309 if (err) {
1310 IRDA_DEBUG(0, "%s(), err=%d\n", __func__, err);
1311 goto out_err;
1313 /* Tell client how much data we actually sent */
1314 return len;
1316 out_err:
1317 return sk_stream_error(sk, msg->msg_flags, err);
1322 * Function irda_recvmsg_dgram (iocb, sock, msg, size, flags)
1324 * Try to receive message and copy it to user. The frame is discarded
1325 * after being read, regardless of how much the user actually read
1327 static int irda_recvmsg_dgram(struct kiocb *iocb, struct socket *sock,
1328 struct msghdr *msg, size_t size, int flags)
1330 struct sock *sk = sock->sk;
1331 struct irda_sock *self = irda_sk(sk);
1332 struct sk_buff *skb;
1333 size_t copied;
1334 int err;
1336 IRDA_DEBUG(4, "%s()\n", __func__);
1338 if ((err = sock_error(sk)) < 0)
1339 return err;
1341 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1342 flags & MSG_DONTWAIT, &err);
1343 if (!skb)
1344 return err;
1346 skb_reset_transport_header(skb);
1347 copied = skb->len;
1349 if (copied > size) {
1350 IRDA_DEBUG(2, "%s(), Received truncated frame (%zd < %zd)!\n",
1351 __func__, copied, size);
1352 copied = size;
1353 msg->msg_flags |= MSG_TRUNC;
1355 skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1357 skb_free_datagram(sk, skb);
1360 * Check if we have previously stopped IrTTP and we know
1361 * have more free space in our rx_queue. If so tell IrTTP
1362 * to start delivering frames again before our rx_queue gets
1363 * empty
1365 if (self->rx_flow == FLOW_STOP) {
1366 if ((atomic_read(&sk->sk_rmem_alloc) << 2) <= sk->sk_rcvbuf) {
1367 IRDA_DEBUG(2, "%s(), Starting IrTTP\n", __func__);
1368 self->rx_flow = FLOW_START;
1369 irttp_flow_request(self->tsap, FLOW_START);
1373 return copied;
1377 * Function irda_recvmsg_stream (iocb, sock, msg, size, flags)
1379 static int irda_recvmsg_stream(struct kiocb *iocb, struct socket *sock,
1380 struct msghdr *msg, size_t size, int flags)
1382 struct sock *sk = sock->sk;
1383 struct irda_sock *self = irda_sk(sk);
1384 int noblock = flags & MSG_DONTWAIT;
1385 size_t copied = 0;
1386 int target, err;
1387 long timeo;
1389 IRDA_DEBUG(3, "%s()\n", __func__);
1391 if ((err = sock_error(sk)) < 0)
1392 return err;
1394 if (sock->flags & __SO_ACCEPTCON)
1395 return(-EINVAL);
1397 if (flags & MSG_OOB)
1398 return -EOPNOTSUPP;
1400 target = sock_rcvlowat(sk, flags & MSG_WAITALL, size);
1401 timeo = sock_rcvtimeo(sk, noblock);
1403 msg->msg_namelen = 0;
1405 do {
1406 int chunk;
1407 struct sk_buff *skb = skb_dequeue(&sk->sk_receive_queue);
1409 if (skb == NULL) {
1410 DEFINE_WAIT(wait);
1411 int ret = 0;
1413 if (copied >= target)
1414 break;
1416 prepare_to_wait_exclusive(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
1419 * POSIX 1003.1g mandates this order.
1421 ret = sock_error(sk);
1422 if (ret)
1424 else if (sk->sk_shutdown & RCV_SHUTDOWN)
1426 else if (noblock)
1427 ret = -EAGAIN;
1428 else if (signal_pending(current))
1429 ret = sock_intr_errno(timeo);
1430 else if (sk->sk_state != TCP_ESTABLISHED)
1431 ret = -ENOTCONN;
1432 else if (skb_peek(&sk->sk_receive_queue) == NULL)
1433 /* Wait process until data arrives */
1434 schedule();
1436 finish_wait(sk->sk_sleep, &wait);
1438 if (ret)
1439 return ret;
1440 if (sk->sk_shutdown & RCV_SHUTDOWN)
1441 break;
1443 continue;
1446 chunk = min_t(unsigned int, skb->len, size);
1447 if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
1448 skb_queue_head(&sk->sk_receive_queue, skb);
1449 if (copied == 0)
1450 copied = -EFAULT;
1451 break;
1453 copied += chunk;
1454 size -= chunk;
1456 /* Mark read part of skb as used */
1457 if (!(flags & MSG_PEEK)) {
1458 skb_pull(skb, chunk);
1460 /* put the skb back if we didn't use it up.. */
1461 if (skb->len) {
1462 IRDA_DEBUG(1, "%s(), back on q!\n",
1463 __func__);
1464 skb_queue_head(&sk->sk_receive_queue, skb);
1465 break;
1468 kfree_skb(skb);
1469 } else {
1470 IRDA_DEBUG(0, "%s() questionable!?\n", __func__);
1472 /* put message back and return */
1473 skb_queue_head(&sk->sk_receive_queue, skb);
1474 break;
1476 } while (size);
1479 * Check if we have previously stopped IrTTP and we know
1480 * have more free space in our rx_queue. If so tell IrTTP
1481 * to start delivering frames again before our rx_queue gets
1482 * empty
1484 if (self->rx_flow == FLOW_STOP) {
1485 if ((atomic_read(&sk->sk_rmem_alloc) << 2) <= sk->sk_rcvbuf) {
1486 IRDA_DEBUG(2, "%s(), Starting IrTTP\n", __func__);
1487 self->rx_flow = FLOW_START;
1488 irttp_flow_request(self->tsap, FLOW_START);
1492 return copied;
1496 * Function irda_sendmsg_dgram (iocb, sock, msg, len)
1498 * Send message down to TinyTP for the unreliable sequenced
1499 * packet service...
1502 static int irda_sendmsg_dgram(struct kiocb *iocb, struct socket *sock,
1503 struct msghdr *msg, size_t len)
1505 struct sock *sk = sock->sk;
1506 struct irda_sock *self;
1507 struct sk_buff *skb;
1508 int err;
1510 IRDA_DEBUG(4, "%s(), len=%zd\n", __func__, len);
1512 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT))
1513 return -EINVAL;
1515 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1516 send_sig(SIGPIPE, current, 0);
1517 return -EPIPE;
1520 if (sk->sk_state != TCP_ESTABLISHED)
1521 return -ENOTCONN;
1523 self = irda_sk(sk);
1526 * Check that we don't send out too big frames. This is an unreliable
1527 * service, so we have no fragmentation and no coalescence
1529 if (len > self->max_data_size) {
1530 IRDA_DEBUG(0, "%s(), Warning to much data! "
1531 "Chopping frame from %zd to %d bytes!\n",
1532 __func__, len, self->max_data_size);
1533 len = self->max_data_size;
1536 skb = sock_alloc_send_skb(sk, len + self->max_header_size,
1537 msg->msg_flags & MSG_DONTWAIT, &err);
1538 if (!skb)
1539 return -ENOBUFS;
1541 skb_reserve(skb, self->max_header_size);
1542 skb_reset_transport_header(skb);
1544 IRDA_DEBUG(4, "%s(), appending user data\n", __func__);
1545 skb_put(skb, len);
1546 err = memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len);
1547 if (err) {
1548 kfree_skb(skb);
1549 return err;
1553 * Just send the message to TinyTP, and let it deal with possible
1554 * errors. No need to duplicate all that here
1556 err = irttp_udata_request(self->tsap, skb);
1557 if (err) {
1558 IRDA_DEBUG(0, "%s(), err=%d\n", __func__, err);
1559 return err;
1561 return len;
1565 * Function irda_sendmsg_ultra (iocb, sock, msg, len)
1567 * Send message down to IrLMP for the unreliable Ultra
1568 * packet service...
1570 #ifdef CONFIG_IRDA_ULTRA
1571 static int irda_sendmsg_ultra(struct kiocb *iocb, struct socket *sock,
1572 struct msghdr *msg, size_t len)
1574 struct sock *sk = sock->sk;
1575 struct irda_sock *self;
1576 __u8 pid = 0;
1577 int bound = 0;
1578 struct sk_buff *skb;
1579 int err;
1581 IRDA_DEBUG(4, "%s(), len=%zd\n", __func__, len);
1583 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_CMSG_COMPAT))
1584 return -EINVAL;
1586 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1587 send_sig(SIGPIPE, current, 0);
1588 return -EPIPE;
1591 self = irda_sk(sk);
1593 /* Check if an address was specified with sendto. Jean II */
1594 if (msg->msg_name) {
1595 struct sockaddr_irda *addr = (struct sockaddr_irda *) msg->msg_name;
1596 /* Check address, extract pid. Jean II */
1597 if (msg->msg_namelen < sizeof(*addr))
1598 return -EINVAL;
1599 if (addr->sir_family != AF_IRDA)
1600 return -EINVAL;
1602 pid = addr->sir_lsap_sel;
1603 if (pid & 0x80) {
1604 IRDA_DEBUG(0, "%s(), extension in PID not supp!\n", __func__);
1605 return -EOPNOTSUPP;
1607 } else {
1608 /* Check that the socket is properly bound to an Ultra
1609 * port. Jean II */
1610 if ((self->lsap == NULL) ||
1611 (sk->sk_state != TCP_ESTABLISHED)) {
1612 IRDA_DEBUG(0, "%s(), socket not bound to Ultra PID.\n",
1613 __func__);
1614 return -ENOTCONN;
1616 /* Use PID from socket */
1617 bound = 1;
1621 * Check that we don't send out too big frames. This is an unreliable
1622 * service, so we have no fragmentation and no coalescence
1624 if (len > self->max_data_size) {
1625 IRDA_DEBUG(0, "%s(), Warning to much data! "
1626 "Chopping frame from %zd to %d bytes!\n",
1627 __func__, len, self->max_data_size);
1628 len = self->max_data_size;
1631 skb = sock_alloc_send_skb(sk, len + self->max_header_size,
1632 msg->msg_flags & MSG_DONTWAIT, &err);
1633 if (!skb)
1634 return -ENOBUFS;
1636 skb_reserve(skb, self->max_header_size);
1637 skb_reset_transport_header(skb);
1639 IRDA_DEBUG(4, "%s(), appending user data\n", __func__);
1640 skb_put(skb, len);
1641 err = memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len);
1642 if (err) {
1643 kfree_skb(skb);
1644 return err;
1647 err = irlmp_connless_data_request((bound ? self->lsap : NULL),
1648 skb, pid);
1649 if (err) {
1650 IRDA_DEBUG(0, "%s(), err=%d\n", __func__, err);
1651 return err;
1653 return len;
1655 #endif /* CONFIG_IRDA_ULTRA */
1658 * Function irda_shutdown (sk, how)
1660 static int irda_shutdown(struct socket *sock, int how)
1662 struct sock *sk = sock->sk;
1663 struct irda_sock *self = irda_sk(sk);
1665 IRDA_DEBUG(1, "%s(%p)\n", __func__, self);
1667 sk->sk_state = TCP_CLOSE;
1668 sk->sk_shutdown |= SEND_SHUTDOWN;
1669 sk->sk_state_change(sk);
1671 if (self->iriap) {
1672 iriap_close(self->iriap);
1673 self->iriap = NULL;
1676 if (self->tsap) {
1677 irttp_disconnect_request(self->tsap, NULL, P_NORMAL);
1678 irttp_close_tsap(self->tsap);
1679 self->tsap = NULL;
1682 /* A few cleanup so the socket look as good as new... */
1683 self->rx_flow = self->tx_flow = FLOW_START; /* needed ??? */
1684 self->daddr = DEV_ADDR_ANY; /* Until we get re-connected */
1685 self->saddr = 0x0; /* so IrLMP assign us any link */
1687 return 0;
1691 * Function irda_poll (file, sock, wait)
1693 static unsigned int irda_poll(struct file * file, struct socket *sock,
1694 poll_table *wait)
1696 struct sock *sk = sock->sk;
1697 struct irda_sock *self = irda_sk(sk);
1698 unsigned int mask;
1700 IRDA_DEBUG(4, "%s()\n", __func__);
1702 poll_wait(file, sk->sk_sleep, wait);
1703 mask = 0;
1705 /* Exceptional events? */
1706 if (sk->sk_err)
1707 mask |= POLLERR;
1708 if (sk->sk_shutdown & RCV_SHUTDOWN) {
1709 IRDA_DEBUG(0, "%s(), POLLHUP\n", __func__);
1710 mask |= POLLHUP;
1713 /* Readable? */
1714 if (!skb_queue_empty(&sk->sk_receive_queue)) {
1715 IRDA_DEBUG(4, "Socket is readable\n");
1716 mask |= POLLIN | POLLRDNORM;
1719 /* Connection-based need to check for termination and startup */
1720 switch (sk->sk_type) {
1721 case SOCK_STREAM:
1722 if (sk->sk_state == TCP_CLOSE) {
1723 IRDA_DEBUG(0, "%s(), POLLHUP\n", __func__);
1724 mask |= POLLHUP;
1727 if (sk->sk_state == TCP_ESTABLISHED) {
1728 if ((self->tx_flow == FLOW_START) &&
1729 sock_writeable(sk))
1731 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1734 break;
1735 case SOCK_SEQPACKET:
1736 if ((self->tx_flow == FLOW_START) &&
1737 sock_writeable(sk))
1739 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1741 break;
1742 case SOCK_DGRAM:
1743 if (sock_writeable(sk))
1744 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1745 break;
1746 default:
1747 break;
1749 return mask;
1753 * Function irda_ioctl (sock, cmd, arg)
1755 static int irda_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1757 struct sock *sk = sock->sk;
1759 IRDA_DEBUG(4, "%s(), cmd=%#x\n", __func__, cmd);
1761 switch (cmd) {
1762 case TIOCOUTQ: {
1763 long amount;
1765 amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
1766 if (amount < 0)
1767 amount = 0;
1768 if (put_user(amount, (unsigned int __user *)arg))
1769 return -EFAULT;
1770 return 0;
1773 case TIOCINQ: {
1774 struct sk_buff *skb;
1775 long amount = 0L;
1776 /* These two are safe on a single CPU system as only user tasks fiddle here */
1777 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1778 amount = skb->len;
1779 if (put_user(amount, (unsigned int __user *)arg))
1780 return -EFAULT;
1781 return 0;
1784 case SIOCGSTAMP:
1785 if (sk != NULL)
1786 return sock_get_timestamp(sk, (struct timeval __user *)arg);
1787 return -EINVAL;
1789 case SIOCGIFADDR:
1790 case SIOCSIFADDR:
1791 case SIOCGIFDSTADDR:
1792 case SIOCSIFDSTADDR:
1793 case SIOCGIFBRDADDR:
1794 case SIOCSIFBRDADDR:
1795 case SIOCGIFNETMASK:
1796 case SIOCSIFNETMASK:
1797 case SIOCGIFMETRIC:
1798 case SIOCSIFMETRIC:
1799 return -EINVAL;
1800 default:
1801 IRDA_DEBUG(1, "%s(), doing device ioctl!\n", __func__);
1802 return -ENOIOCTLCMD;
1805 /*NOTREACHED*/
1806 return 0;
1809 #ifdef CONFIG_COMPAT
1811 * Function irda_ioctl (sock, cmd, arg)
1813 static int irda_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1816 * All IRDA's ioctl are standard ones.
1818 return -ENOIOCTLCMD;
1820 #endif
1823 * Function irda_setsockopt (sock, level, optname, optval, optlen)
1825 * Set some options for the socket
1828 static int irda_setsockopt(struct socket *sock, int level, int optname,
1829 char __user *optval, unsigned int optlen)
1831 struct sock *sk = sock->sk;
1832 struct irda_sock *self = irda_sk(sk);
1833 struct irda_ias_set *ias_opt;
1834 struct ias_object *ias_obj;
1835 struct ias_attrib * ias_attr; /* Attribute in IAS object */
1836 int opt, free_ias = 0;
1838 IRDA_DEBUG(2, "%s(%p)\n", __func__, self);
1840 if (level != SOL_IRLMP)
1841 return -ENOPROTOOPT;
1843 switch (optname) {
1844 case IRLMP_IAS_SET:
1845 /* The user want to add an attribute to an existing IAS object
1846 * (in the IAS database) or to create a new object with this
1847 * attribute.
1848 * We first query IAS to know if the object exist, and then
1849 * create the right attribute...
1852 if (optlen != sizeof(struct irda_ias_set))
1853 return -EINVAL;
1855 ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
1856 if (ias_opt == NULL)
1857 return -ENOMEM;
1859 /* Copy query to the driver. */
1860 if (copy_from_user(ias_opt, optval, optlen)) {
1861 kfree(ias_opt);
1862 return -EFAULT;
1865 /* Find the object we target.
1866 * If the user gives us an empty string, we use the object
1867 * associated with this socket. This will workaround
1868 * duplicated class name - Jean II */
1869 if(ias_opt->irda_class_name[0] == '\0') {
1870 if(self->ias_obj == NULL) {
1871 kfree(ias_opt);
1872 return -EINVAL;
1874 ias_obj = self->ias_obj;
1875 } else
1876 ias_obj = irias_find_object(ias_opt->irda_class_name);
1878 /* Only ROOT can mess with the global IAS database.
1879 * Users can only add attributes to the object associated
1880 * with the socket they own - Jean II */
1881 if((!capable(CAP_NET_ADMIN)) &&
1882 ((ias_obj == NULL) || (ias_obj != self->ias_obj))) {
1883 kfree(ias_opt);
1884 return -EPERM;
1887 /* If the object doesn't exist, create it */
1888 if(ias_obj == (struct ias_object *) NULL) {
1889 /* Create a new object */
1890 ias_obj = irias_new_object(ias_opt->irda_class_name,
1891 jiffies);
1892 if (ias_obj == NULL) {
1893 kfree(ias_opt);
1894 return -ENOMEM;
1896 free_ias = 1;
1899 /* Do we have the attribute already ? */
1900 if(irias_find_attrib(ias_obj, ias_opt->irda_attrib_name)) {
1901 kfree(ias_opt);
1902 if (free_ias) {
1903 kfree(ias_obj->name);
1904 kfree(ias_obj);
1906 return -EINVAL;
1909 /* Look at the type */
1910 switch(ias_opt->irda_attrib_type) {
1911 case IAS_INTEGER:
1912 /* Add an integer attribute */
1913 irias_add_integer_attrib(
1914 ias_obj,
1915 ias_opt->irda_attrib_name,
1916 ias_opt->attribute.irda_attrib_int,
1917 IAS_USER_ATTR);
1918 break;
1919 case IAS_OCT_SEQ:
1920 /* Check length */
1921 if(ias_opt->attribute.irda_attrib_octet_seq.len >
1922 IAS_MAX_OCTET_STRING) {
1923 kfree(ias_opt);
1924 if (free_ias) {
1925 kfree(ias_obj->name);
1926 kfree(ias_obj);
1929 return -EINVAL;
1931 /* Add an octet sequence attribute */
1932 irias_add_octseq_attrib(
1933 ias_obj,
1934 ias_opt->irda_attrib_name,
1935 ias_opt->attribute.irda_attrib_octet_seq.octet_seq,
1936 ias_opt->attribute.irda_attrib_octet_seq.len,
1937 IAS_USER_ATTR);
1938 break;
1939 case IAS_STRING:
1940 /* Should check charset & co */
1941 /* Check length */
1942 /* The length is encoded in a __u8, and
1943 * IAS_MAX_STRING == 256, so there is no way
1944 * userspace can pass us a string too large.
1945 * Jean II */
1946 /* NULL terminate the string (avoid troubles) */
1947 ias_opt->attribute.irda_attrib_string.string[ias_opt->attribute.irda_attrib_string.len] = '\0';
1948 /* Add a string attribute */
1949 irias_add_string_attrib(
1950 ias_obj,
1951 ias_opt->irda_attrib_name,
1952 ias_opt->attribute.irda_attrib_string.string,
1953 IAS_USER_ATTR);
1954 break;
1955 default :
1956 kfree(ias_opt);
1957 if (free_ias) {
1958 kfree(ias_obj->name);
1959 kfree(ias_obj);
1961 return -EINVAL;
1963 irias_insert_object(ias_obj);
1964 kfree(ias_opt);
1965 break;
1966 case IRLMP_IAS_DEL:
1967 /* The user want to delete an object from our local IAS
1968 * database. We just need to query the IAS, check is the
1969 * object is not owned by the kernel and delete it.
1972 if (optlen != sizeof(struct irda_ias_set))
1973 return -EINVAL;
1975 ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
1976 if (ias_opt == NULL)
1977 return -ENOMEM;
1979 /* Copy query to the driver. */
1980 if (copy_from_user(ias_opt, optval, optlen)) {
1981 kfree(ias_opt);
1982 return -EFAULT;
1985 /* Find the object we target.
1986 * If the user gives us an empty string, we use the object
1987 * associated with this socket. This will workaround
1988 * duplicated class name - Jean II */
1989 if(ias_opt->irda_class_name[0] == '\0')
1990 ias_obj = self->ias_obj;
1991 else
1992 ias_obj = irias_find_object(ias_opt->irda_class_name);
1993 if(ias_obj == (struct ias_object *) NULL) {
1994 kfree(ias_opt);
1995 return -EINVAL;
1998 /* Only ROOT can mess with the global IAS database.
1999 * Users can only del attributes from the object associated
2000 * with the socket they own - Jean II */
2001 if((!capable(CAP_NET_ADMIN)) &&
2002 ((ias_obj == NULL) || (ias_obj != self->ias_obj))) {
2003 kfree(ias_opt);
2004 return -EPERM;
2007 /* Find the attribute (in the object) we target */
2008 ias_attr = irias_find_attrib(ias_obj,
2009 ias_opt->irda_attrib_name);
2010 if(ias_attr == (struct ias_attrib *) NULL) {
2011 kfree(ias_opt);
2012 return -EINVAL;
2015 /* Check is the user space own the object */
2016 if(ias_attr->value->owner != IAS_USER_ATTR) {
2017 IRDA_DEBUG(1, "%s(), attempting to delete a kernel attribute\n", __func__);
2018 kfree(ias_opt);
2019 return -EPERM;
2022 /* Remove the attribute (and maybe the object) */
2023 irias_delete_attrib(ias_obj, ias_attr, 1);
2024 kfree(ias_opt);
2025 break;
2026 case IRLMP_MAX_SDU_SIZE:
2027 if (optlen < sizeof(int))
2028 return -EINVAL;
2030 if (get_user(opt, (int __user *)optval))
2031 return -EFAULT;
2033 /* Only possible for a seqpacket service (TTP with SAR) */
2034 if (sk->sk_type != SOCK_SEQPACKET) {
2035 IRDA_DEBUG(2, "%s(), setting max_sdu_size = %d\n",
2036 __func__, opt);
2037 self->max_sdu_size_rx = opt;
2038 } else {
2039 IRDA_WARNING("%s: not allowed to set MAXSDUSIZE for this socket type!\n",
2040 __func__);
2041 return -ENOPROTOOPT;
2043 break;
2044 case IRLMP_HINTS_SET:
2045 if (optlen < sizeof(int))
2046 return -EINVAL;
2048 /* The input is really a (__u8 hints[2]), easier as an int */
2049 if (get_user(opt, (int __user *)optval))
2050 return -EFAULT;
2052 /* Unregister any old registration */
2053 if (self->skey)
2054 irlmp_unregister_service(self->skey);
2056 self->skey = irlmp_register_service((__u16) opt);
2057 break;
2058 case IRLMP_HINT_MASK_SET:
2059 /* As opposed to the previous case which set the hint bits
2060 * that we advertise, this one set the filter we use when
2061 * making a discovery (nodes which don't match any hint
2062 * bit in the mask are not reported).
2064 if (optlen < sizeof(int))
2065 return -EINVAL;
2067 /* The input is really a (__u8 hints[2]), easier as an int */
2068 if (get_user(opt, (int __user *)optval))
2069 return -EFAULT;
2071 /* Set the new hint mask */
2072 self->mask.word = (__u16) opt;
2073 /* Mask out extension bits */
2074 self->mask.word &= 0x7f7f;
2075 /* Check if no bits */
2076 if(!self->mask.word)
2077 self->mask.word = 0xFFFF;
2079 break;
2080 default:
2081 return -ENOPROTOOPT;
2083 return 0;
2087 * Function irda_extract_ias_value(ias_opt, ias_value)
2089 * Translate internal IAS value structure to the user space representation
2091 * The external representation of IAS values, as we exchange them with
2092 * user space program is quite different from the internal representation,
2093 * as stored in the IAS database (because we need a flat structure for
2094 * crossing kernel boundary).
2095 * This function transform the former in the latter. We also check
2096 * that the value type is valid.
2098 static int irda_extract_ias_value(struct irda_ias_set *ias_opt,
2099 struct ias_value *ias_value)
2101 /* Look at the type */
2102 switch (ias_value->type) {
2103 case IAS_INTEGER:
2104 /* Copy the integer */
2105 ias_opt->attribute.irda_attrib_int = ias_value->t.integer;
2106 break;
2107 case IAS_OCT_SEQ:
2108 /* Set length */
2109 ias_opt->attribute.irda_attrib_octet_seq.len = ias_value->len;
2110 /* Copy over */
2111 memcpy(ias_opt->attribute.irda_attrib_octet_seq.octet_seq,
2112 ias_value->t.oct_seq, ias_value->len);
2113 break;
2114 case IAS_STRING:
2115 /* Set length */
2116 ias_opt->attribute.irda_attrib_string.len = ias_value->len;
2117 ias_opt->attribute.irda_attrib_string.charset = ias_value->charset;
2118 /* Copy over */
2119 memcpy(ias_opt->attribute.irda_attrib_string.string,
2120 ias_value->t.string, ias_value->len);
2121 /* NULL terminate the string (avoid troubles) */
2122 ias_opt->attribute.irda_attrib_string.string[ias_value->len] = '\0';
2123 break;
2124 case IAS_MISSING:
2125 default :
2126 return -EINVAL;
2129 /* Copy type over */
2130 ias_opt->irda_attrib_type = ias_value->type;
2132 return 0;
2136 * Function irda_getsockopt (sock, level, optname, optval, optlen)
2138 static int irda_getsockopt(struct socket *sock, int level, int optname,
2139 char __user *optval, int __user *optlen)
2141 struct sock *sk = sock->sk;
2142 struct irda_sock *self = irda_sk(sk);
2143 struct irda_device_list list;
2144 struct irda_device_info *discoveries;
2145 struct irda_ias_set * ias_opt; /* IAS get/query params */
2146 struct ias_object * ias_obj; /* Object in IAS */
2147 struct ias_attrib * ias_attr; /* Attribute in IAS object */
2148 int daddr = DEV_ADDR_ANY; /* Dest address for IAS queries */
2149 int val = 0;
2150 int len = 0;
2151 int err;
2152 int offset, total;
2154 IRDA_DEBUG(2, "%s(%p)\n", __func__, self);
2156 if (level != SOL_IRLMP)
2157 return -ENOPROTOOPT;
2159 if (get_user(len, optlen))
2160 return -EFAULT;
2162 if(len < 0)
2163 return -EINVAL;
2165 switch (optname) {
2166 case IRLMP_ENUMDEVICES:
2167 /* Ask lmp for the current discovery log */
2168 discoveries = irlmp_get_discoveries(&list.len, self->mask.word,
2169 self->nslots);
2170 /* Check if the we got some results */
2171 if (discoveries == NULL)
2172 return -EAGAIN; /* Didn't find any devices */
2173 err = 0;
2175 /* Write total list length back to client */
2176 if (copy_to_user(optval, &list,
2177 sizeof(struct irda_device_list) -
2178 sizeof(struct irda_device_info)))
2179 err = -EFAULT;
2181 /* Offset to first device entry */
2182 offset = sizeof(struct irda_device_list) -
2183 sizeof(struct irda_device_info);
2185 /* Copy the list itself - watch for overflow */
2186 if(list.len > 2048)
2188 err = -EINVAL;
2189 goto bed;
2191 total = offset + (list.len * sizeof(struct irda_device_info));
2192 if (total > len)
2193 total = len;
2194 if (copy_to_user(optval+offset, discoveries, total - offset))
2195 err = -EFAULT;
2197 /* Write total number of bytes used back to client */
2198 if (put_user(total, optlen))
2199 err = -EFAULT;
2200 bed:
2201 /* Free up our buffer */
2202 kfree(discoveries);
2203 if (err)
2204 return err;
2205 break;
2206 case IRLMP_MAX_SDU_SIZE:
2207 val = self->max_data_size;
2208 len = sizeof(int);
2209 if (put_user(len, optlen))
2210 return -EFAULT;
2212 if (copy_to_user(optval, &val, len))
2213 return -EFAULT;
2214 break;
2215 case IRLMP_IAS_GET:
2216 /* The user want an object from our local IAS database.
2217 * We just need to query the IAS and return the value
2218 * that we found */
2220 /* Check that the user has allocated the right space for us */
2221 if (len != sizeof(struct irda_ias_set))
2222 return -EINVAL;
2224 ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
2225 if (ias_opt == NULL)
2226 return -ENOMEM;
2228 /* Copy query to the driver. */
2229 if (copy_from_user(ias_opt, optval, len)) {
2230 kfree(ias_opt);
2231 return -EFAULT;
2234 /* Find the object we target.
2235 * If the user gives us an empty string, we use the object
2236 * associated with this socket. This will workaround
2237 * duplicated class name - Jean II */
2238 if(ias_opt->irda_class_name[0] == '\0')
2239 ias_obj = self->ias_obj;
2240 else
2241 ias_obj = irias_find_object(ias_opt->irda_class_name);
2242 if(ias_obj == (struct ias_object *) NULL) {
2243 kfree(ias_opt);
2244 return -EINVAL;
2247 /* Find the attribute (in the object) we target */
2248 ias_attr = irias_find_attrib(ias_obj,
2249 ias_opt->irda_attrib_name);
2250 if(ias_attr == (struct ias_attrib *) NULL) {
2251 kfree(ias_opt);
2252 return -EINVAL;
2255 /* Translate from internal to user structure */
2256 err = irda_extract_ias_value(ias_opt, ias_attr->value);
2257 if(err) {
2258 kfree(ias_opt);
2259 return err;
2262 /* Copy reply to the user */
2263 if (copy_to_user(optval, ias_opt,
2264 sizeof(struct irda_ias_set))) {
2265 kfree(ias_opt);
2266 return -EFAULT;
2268 /* Note : don't need to put optlen, we checked it */
2269 kfree(ias_opt);
2270 break;
2271 case IRLMP_IAS_QUERY:
2272 /* The user want an object from a remote IAS database.
2273 * We need to use IAP to query the remote database and
2274 * then wait for the answer to come back. */
2276 /* Check that the user has allocated the right space for us */
2277 if (len != sizeof(struct irda_ias_set))
2278 return -EINVAL;
2280 ias_opt = kmalloc(sizeof(struct irda_ias_set), GFP_ATOMIC);
2281 if (ias_opt == NULL)
2282 return -ENOMEM;
2284 /* Copy query to the driver. */
2285 if (copy_from_user(ias_opt, optval, len)) {
2286 kfree(ias_opt);
2287 return -EFAULT;
2290 /* At this point, there are two cases...
2291 * 1) the socket is connected - that's the easy case, we
2292 * just query the device we are connected to...
2293 * 2) the socket is not connected - the user doesn't want
2294 * to connect and/or may not have a valid service name
2295 * (so can't create a fake connection). In this case,
2296 * we assume that the user pass us a valid destination
2297 * address in the requesting structure...
2299 if(self->daddr != DEV_ADDR_ANY) {
2300 /* We are connected - reuse known daddr */
2301 daddr = self->daddr;
2302 } else {
2303 /* We are not connected, we must specify a valid
2304 * destination address */
2305 daddr = ias_opt->daddr;
2306 if((!daddr) || (daddr == DEV_ADDR_ANY)) {
2307 kfree(ias_opt);
2308 return -EINVAL;
2312 /* Check that we can proceed with IAP */
2313 if (self->iriap) {
2314 IRDA_WARNING("%s: busy with a previous query\n",
2315 __func__);
2316 kfree(ias_opt);
2317 return -EBUSY;
2320 self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
2321 irda_getvalue_confirm);
2323 if (self->iriap == NULL) {
2324 kfree(ias_opt);
2325 return -ENOMEM;
2328 /* Treat unexpected wakeup as disconnect */
2329 self->errno = -EHOSTUNREACH;
2331 /* Query remote LM-IAS */
2332 iriap_getvaluebyclass_request(self->iriap,
2333 self->saddr, daddr,
2334 ias_opt->irda_class_name,
2335 ias_opt->irda_attrib_name);
2337 /* Wait for answer, if not yet finished (or failed) */
2338 if (wait_event_interruptible(self->query_wait,
2339 (self->iriap == NULL))) {
2340 /* pending request uses copy of ias_opt-content
2341 * we can free it regardless! */
2342 kfree(ias_opt);
2343 /* Treat signals as disconnect */
2344 return -EHOSTUNREACH;
2347 /* Check what happened */
2348 if (self->errno)
2350 kfree(ias_opt);
2351 /* Requested object/attribute doesn't exist */
2352 if((self->errno == IAS_CLASS_UNKNOWN) ||
2353 (self->errno == IAS_ATTRIB_UNKNOWN))
2354 return (-EADDRNOTAVAIL);
2355 else
2356 return (-EHOSTUNREACH);
2359 /* Translate from internal to user structure */
2360 err = irda_extract_ias_value(ias_opt, self->ias_result);
2361 if (self->ias_result)
2362 irias_delete_value(self->ias_result);
2363 if (err) {
2364 kfree(ias_opt);
2365 return err;
2368 /* Copy reply to the user */
2369 if (copy_to_user(optval, ias_opt,
2370 sizeof(struct irda_ias_set))) {
2371 kfree(ias_opt);
2372 return -EFAULT;
2374 /* Note : don't need to put optlen, we checked it */
2375 kfree(ias_opt);
2376 break;
2377 case IRLMP_WAITDEVICE:
2378 /* This function is just another way of seeing life ;-)
2379 * IRLMP_ENUMDEVICES assumes that you have a static network,
2380 * and that you just want to pick one of the devices present.
2381 * On the other hand, in here we assume that no device is
2382 * present and that at some point in the future a device will
2383 * come into range. When this device arrive, we just wake
2384 * up the caller, so that he has time to connect to it before
2385 * the device goes away...
2386 * Note : once the node has been discovered for more than a
2387 * few second, it won't trigger this function, unless it
2388 * goes away and come back changes its hint bits (so we
2389 * might call it IRLMP_WAITNEWDEVICE).
2392 /* Check that the user is passing us an int */
2393 if (len != sizeof(int))
2394 return -EINVAL;
2395 /* Get timeout in ms (max time we block the caller) */
2396 if (get_user(val, (int __user *)optval))
2397 return -EFAULT;
2399 /* Tell IrLMP we want to be notified */
2400 irlmp_update_client(self->ckey, self->mask.word,
2401 irda_selective_discovery_indication,
2402 NULL, (void *) self);
2404 /* Do some discovery (and also return cached results) */
2405 irlmp_discovery_request(self->nslots);
2407 /* Wait until a node is discovered */
2408 if (!self->cachedaddr) {
2409 int ret = 0;
2411 IRDA_DEBUG(1, "%s(), nothing discovered yet, going to sleep...\n", __func__);
2413 /* Set watchdog timer to expire in <val> ms. */
2414 self->errno = 0;
2415 setup_timer(&self->watchdog, irda_discovery_timeout,
2416 (unsigned long)self);
2417 self->watchdog.expires = jiffies + (val * HZ/1000);
2418 add_timer(&(self->watchdog));
2420 /* Wait for IR-LMP to call us back */
2421 __wait_event_interruptible(self->query_wait,
2422 (self->cachedaddr != 0 || self->errno == -ETIME),
2423 ret);
2425 /* If watchdog is still activated, kill it! */
2426 if(timer_pending(&(self->watchdog)))
2427 del_timer(&(self->watchdog));
2429 IRDA_DEBUG(1, "%s(), ...waking up !\n", __func__);
2431 if (ret != 0)
2432 return ret;
2434 else
2435 IRDA_DEBUG(1, "%s(), found immediately !\n",
2436 __func__);
2438 /* Tell IrLMP that we have been notified */
2439 irlmp_update_client(self->ckey, self->mask.word,
2440 NULL, NULL, NULL);
2442 /* Check if the we got some results */
2443 if (!self->cachedaddr)
2444 return -EAGAIN; /* Didn't find any devices */
2445 daddr = self->cachedaddr;
2446 /* Cleanup */
2447 self->cachedaddr = 0;
2449 /* We return the daddr of the device that trigger the
2450 * wakeup. As irlmp pass us only the new devices, we
2451 * are sure that it's not an old device.
2452 * If the user want more details, he should query
2453 * the whole discovery log and pick one device...
2455 if (put_user(daddr, (int __user *)optval))
2456 return -EFAULT;
2458 break;
2459 default:
2460 return -ENOPROTOOPT;
2463 return 0;
2466 static struct net_proto_family irda_family_ops = {
2467 .family = PF_IRDA,
2468 .create = irda_create,
2469 .owner = THIS_MODULE,
2472 static const struct proto_ops SOCKOPS_WRAPPED(irda_stream_ops) = {
2473 .family = PF_IRDA,
2474 .owner = THIS_MODULE,
2475 .release = irda_release,
2476 .bind = irda_bind,
2477 .connect = irda_connect,
2478 .socketpair = sock_no_socketpair,
2479 .accept = irda_accept,
2480 .getname = irda_getname,
2481 .poll = irda_poll,
2482 .ioctl = irda_ioctl,
2483 #ifdef CONFIG_COMPAT
2484 .compat_ioctl = irda_compat_ioctl,
2485 #endif
2486 .listen = irda_listen,
2487 .shutdown = irda_shutdown,
2488 .setsockopt = irda_setsockopt,
2489 .getsockopt = irda_getsockopt,
2490 .sendmsg = irda_sendmsg,
2491 .recvmsg = irda_recvmsg_stream,
2492 .mmap = sock_no_mmap,
2493 .sendpage = sock_no_sendpage,
2496 static const struct proto_ops SOCKOPS_WRAPPED(irda_seqpacket_ops) = {
2497 .family = PF_IRDA,
2498 .owner = THIS_MODULE,
2499 .release = irda_release,
2500 .bind = irda_bind,
2501 .connect = irda_connect,
2502 .socketpair = sock_no_socketpair,
2503 .accept = irda_accept,
2504 .getname = irda_getname,
2505 .poll = datagram_poll,
2506 .ioctl = irda_ioctl,
2507 #ifdef CONFIG_COMPAT
2508 .compat_ioctl = irda_compat_ioctl,
2509 #endif
2510 .listen = irda_listen,
2511 .shutdown = irda_shutdown,
2512 .setsockopt = irda_setsockopt,
2513 .getsockopt = irda_getsockopt,
2514 .sendmsg = irda_sendmsg,
2515 .recvmsg = irda_recvmsg_dgram,
2516 .mmap = sock_no_mmap,
2517 .sendpage = sock_no_sendpage,
2520 static const struct proto_ops SOCKOPS_WRAPPED(irda_dgram_ops) = {
2521 .family = PF_IRDA,
2522 .owner = THIS_MODULE,
2523 .release = irda_release,
2524 .bind = irda_bind,
2525 .connect = irda_connect,
2526 .socketpair = sock_no_socketpair,
2527 .accept = irda_accept,
2528 .getname = irda_getname,
2529 .poll = datagram_poll,
2530 .ioctl = irda_ioctl,
2531 #ifdef CONFIG_COMPAT
2532 .compat_ioctl = irda_compat_ioctl,
2533 #endif
2534 .listen = irda_listen,
2535 .shutdown = irda_shutdown,
2536 .setsockopt = irda_setsockopt,
2537 .getsockopt = irda_getsockopt,
2538 .sendmsg = irda_sendmsg_dgram,
2539 .recvmsg = irda_recvmsg_dgram,
2540 .mmap = sock_no_mmap,
2541 .sendpage = sock_no_sendpage,
2544 #ifdef CONFIG_IRDA_ULTRA
2545 static const struct proto_ops SOCKOPS_WRAPPED(irda_ultra_ops) = {
2546 .family = PF_IRDA,
2547 .owner = THIS_MODULE,
2548 .release = irda_release,
2549 .bind = irda_bind,
2550 .connect = sock_no_connect,
2551 .socketpair = sock_no_socketpair,
2552 .accept = sock_no_accept,
2553 .getname = irda_getname,
2554 .poll = datagram_poll,
2555 .ioctl = irda_ioctl,
2556 #ifdef CONFIG_COMPAT
2557 .compat_ioctl = irda_compat_ioctl,
2558 #endif
2559 .listen = sock_no_listen,
2560 .shutdown = irda_shutdown,
2561 .setsockopt = irda_setsockopt,
2562 .getsockopt = irda_getsockopt,
2563 .sendmsg = irda_sendmsg_ultra,
2564 .recvmsg = irda_recvmsg_dgram,
2565 .mmap = sock_no_mmap,
2566 .sendpage = sock_no_sendpage,
2568 #endif /* CONFIG_IRDA_ULTRA */
2570 SOCKOPS_WRAP(irda_stream, PF_IRDA);
2571 SOCKOPS_WRAP(irda_seqpacket, PF_IRDA);
2572 SOCKOPS_WRAP(irda_dgram, PF_IRDA);
2573 #ifdef CONFIG_IRDA_ULTRA
2574 SOCKOPS_WRAP(irda_ultra, PF_IRDA);
2575 #endif /* CONFIG_IRDA_ULTRA */
2578 * Function irsock_init (pro)
2580 * Initialize IrDA protocol
2583 int __init irsock_init(void)
2585 int rc = proto_register(&irda_proto, 0);
2587 if (rc == 0)
2588 rc = sock_register(&irda_family_ops);
2590 return rc;
2594 * Function irsock_cleanup (void)
2596 * Remove IrDA protocol
2599 void irsock_cleanup(void)
2601 sock_unregister(PF_IRDA);
2602 proto_unregister(&irda_proto);