Import 2.3.25pre1
[davej-history.git] / net / irda / af_irda.c
blob7a01c28eafbfd0fd7440ac99ad52958e6f27eb95
1 /*********************************************************************
2 *
3 * Filename: af_irda.c
4 * Version: 0.7
5 * Description: IrDA sockets implementation
6 * Status: Experimental.
7 * Author: Dag Brattli <dagb@cs.uit.no>
8 * Created at: Sun May 31 10:12:43 1998
9 * Modified at: Fri Oct 1 19:11:04 1999
10 * Modified by: Dag Brattli <dagb@cs.uit.no>
11 * Sources: af_netroom.c, af_ax25.c, af_rose.c, af_x25.c etc.
13 * Copyright (c) 1999 Dag Brattli, All Rights Reserved.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License as
17 * published by the Free Software Foundation; either version 2 of
18 * the License, or (at your option) any later version.
20 * Neither Dag Brattli nor University of Tromsø admit liability nor
21 * provide warranty for any of this software. This material is
22 * provided "AS-IS" and at no charge.
24 ********************************************************************/
26 #include <linux/types.h>
27 #include <linux/socket.h>
28 #include <linux/sockios.h>
29 #include <linux/init.h>
30 #include <linux/if_arp.h>
31 #include <linux/net.h>
32 #include <linux/irda.h>
33 #include <linux/poll.h>
35 #include <asm/uaccess.h>
37 #include <net/sock.h>
39 #include <net/irda/irda.h>
40 #include <net/irda/iriap.h>
41 #include <net/irda/irias_object.h>
42 #include <net/irda/irttp.h>
43 #include <net/irda/discovery.h>
45 extern int irda_init(void);
46 extern void irda_cleanup(void);
47 extern int irlap_driver_rcv(struct sk_buff *, struct net_device *,
48 struct packet_type *);
50 static int irda_create(struct socket *sock, int protocol);
52 static struct proto_ops irda_stream_ops;
53 static struct proto_ops irda_dgram_ops;
54 static hashbin_t *cachelog = NULL;
55 static DECLARE_WAIT_QUEUE_HEAD(discovery_wait); /* Wait for discovery */
57 #define IRDA_MAX_HEADER (TTP_MAX_HEADER)
60 * Function irda_data_indication (instance, sap, skb)
62 * Received some data from TinyTP. Just queue it on the receive queue
65 static int irda_data_indication(void *instance, void *sap, struct sk_buff *skb)
67 struct irda_sock *self;
68 struct sock *sk;
69 int err;
71 self = (struct irda_sock *) instance;
72 ASSERT(self != NULL, return -1;);
74 sk = self->sk;
75 ASSERT(sk != NULL, return -1;);
77 err = sock_queue_rcv_skb(sk, skb);
78 if (err) {
79 IRDA_DEBUG(1, __FUNCTION__ "(), error: no more mem!\n");
80 self->rx_flow = FLOW_STOP;
82 /* When we return error, TTP will need to requeue the skb */
83 return err;
86 return 0;
90 * Function irda_disconnect_indication (instance, sap, reason, skb)
92 * Connection has been closed. Chech reason to find out why
95 static void irda_disconnect_indication(void *instance, void *sap,
96 LM_REASON reason, struct sk_buff *skb)
98 struct irda_sock *self;
99 struct sock *sk;
101 IRDA_DEBUG(2, __FUNCTION__ "()\n");
103 self = (struct irda_sock *) instance;
105 sk = self->sk;
106 if (sk == NULL)
107 return;
109 sk->state = TCP_CLOSE;
110 sk->err = reason;
111 sk->shutdown |= SEND_SHUTDOWN;
112 if (!sk->dead) {
113 sk->state_change(sk);
114 sk->dead = 1;
119 * Function irda_connect_confirm (instance, sap, qos, max_sdu_size, skb)
121 * Connections has been confirmed by the remote device
124 static void irda_connect_confirm(void *instance, void *sap,
125 struct qos_info *qos,
126 __u32 max_sdu_size, __u8 max_header_size,
127 struct sk_buff *skb)
129 struct irda_sock *self;
130 struct sock *sk;
132 IRDA_DEBUG(2, __FUNCTION__ "()\n");
134 self = (struct irda_sock *) instance;
136 /* How much header space do we need to reserve */
137 self->max_header_size = max_header_size;
139 /* IrTTP max SDU size in transmit direction */
140 self->max_sdu_size_tx = max_sdu_size;
142 /* Find out what the largest chunk of data that we can transmit is */
143 if (max_sdu_size == SAR_DISABLE)
144 self->max_data_size = irttp_get_max_seq_size(self->tsap);
145 else
146 self->max_data_size = max_sdu_size;
148 IRDA_DEBUG(1, __FUNCTION__ "(), max_data_size=%d\n", self->max_data_size);
150 memcpy(&self->qos_tx, qos, sizeof(struct qos_info));
152 sk = self->sk;
153 if (sk == NULL)
154 return;
156 skb_queue_tail(&sk->receive_queue, skb);
158 /* We are now connected! */
159 sk->state = TCP_ESTABLISHED;
160 sk->state_change(sk);
164 * Function irda_connect_indication(instance, sap, qos, max_sdu_size, userdata)
166 * Incomming connection
169 static void irda_connect_indication(void *instance, void *sap,
170 struct qos_info *qos, __u32 max_sdu_size,
171 __u8 max_header_size, struct sk_buff *skb)
173 struct irda_sock *self;
174 struct sock *sk;
176 IRDA_DEBUG(2, __FUNCTION__ "()\n");
178 self = (struct irda_sock *) instance;
180 /* How much header space do we need to reserve */
181 self->max_header_size = max_header_size;
183 /* IrTTP max SDU size in transmit direction */
184 self->max_sdu_size_tx = max_sdu_size;
186 /* Find out what the largest chunk of data that we can transmit is */
187 if (max_sdu_size == SAR_DISABLE)
188 self->max_data_size = irttp_get_max_seq_size(self->tsap);
189 else
190 self->max_data_size = max_sdu_size;
192 IRDA_DEBUG(1, __FUNCTION__ "(), max_data_size=%d\n", self->max_data_size);
194 memcpy(&self->qos_tx, qos, sizeof(struct qos_info));
196 sk = self->sk;
197 if (sk == NULL)
198 return;
200 skb_queue_tail(&sk->receive_queue, skb);
202 sk->state_change(sk);
206 * Function irda_connect_response (handle)
208 * Accept incomming connection
211 void irda_connect_response(struct irda_sock *self)
213 struct sk_buff *skb;
215 IRDA_DEBUG(1, __FUNCTION__ "()\n");
217 ASSERT(self != NULL, return;);
219 skb = dev_alloc_skb(64);
220 if (skb == NULL) {
221 IRDA_DEBUG(0, __FUNCTION__ "() Unable to allocate sk_buff!\n");
222 return;
225 /* Reserve space for MUX_CONTROL and LAP header */
226 skb_reserve(skb, IRDA_MAX_HEADER);
228 irttp_connect_response(self->tsap, self->max_sdu_size_rx, skb);
232 * Function irda_flow_indication (instance, sap, flow)
234 * Used by TinyTP to tell us if it can accept more data or not
237 static void irda_flow_indication(void *instance, void *sap, LOCAL_FLOW flow)
239 struct irda_sock *self;
240 struct sock *sk;
242 IRDA_DEBUG(1, __FUNCTION__ "()\n");
244 self = (struct irda_sock *) instance;
245 ASSERT(self != NULL, return;);
247 sk = self->sk;
248 ASSERT(sk != NULL, return;);
250 switch (flow) {
251 case FLOW_STOP:
252 IRDA_DEBUG(1, __FUNCTION__ "(), IrTTP wants us to slow down\n");
253 self->tx_flow = flow;
254 break;
255 case FLOW_START:
256 self->tx_flow = flow;
257 IRDA_DEBUG(1, __FUNCTION__ "(), IrTTP wants us to start again\n");
258 wake_up_interruptible(sk->sleep);
259 break;
260 default:
261 IRDA_DEBUG( 0, __FUNCTION__ "(), Unknown flow command!\n");
266 * Function irda_get_value_confirm (obj_id, value, priv)
268 * Got answer from remote LM-IAS
271 static void irda_get_value_confirm(int result, __u16 obj_id,
272 struct ias_value *value, void *priv)
274 struct irda_sock *self;
276 IRDA_DEBUG(1, __FUNCTION__ "()\n");
278 ASSERT(priv != NULL, return;);
279 self = (struct irda_sock *) priv;
281 if (!self)
282 return;
284 /* Check if request succeeded */
285 if (result != IAS_SUCCESS) {
286 IRDA_DEBUG(0, __FUNCTION__ "(), IAS query failed!\n");
288 self->errno = result;
290 /* Wake up any processes waiting for result */
291 wake_up_interruptible(&self->ias_wait);
293 return;
296 switch (value->type) {
297 case IAS_INTEGER:
298 IRDA_DEBUG(4, __FUNCTION__ "() int=%d\n", value->t.integer);
300 if (value->t.integer != -1) {
301 self->dtsap_sel = value->t.integer;
302 } else
303 self->dtsap_sel = 0;
304 break;
305 default:
306 IRDA_DEBUG(0, __FUNCTION__ "(), bad type!\n");
307 break;
309 /* Wake up any processes waiting for result */
310 wake_up_interruptible(&self->ias_wait);
314 * Function irda_discovery_indication (log)
316 * Got a discovery log from IrLMP, wake ut any process waiting for answer
319 static void irda_discovery_indication(hashbin_t *log)
321 IRDA_DEBUG(1, __FUNCTION__ "()\n");
323 cachelog = log;
325 /* Wake up process if its waiting for device to be discovered */
326 wake_up_interruptible(&discovery_wait);
330 * Function irda_open_tsap (self)
332 * Open local Transport Service Access Point (TSAP)
335 static int irda_open_tsap(struct irda_sock *self, __u8 tsap_sel, char *name)
337 notify_t notify;
339 /* Initialize callbacks to be used by the IrDA stack */
340 irda_notify_init(&notify);
341 notify.connect_confirm = irda_connect_confirm;
342 notify.connect_indication = irda_connect_indication;
343 notify.disconnect_indication = irda_disconnect_indication;
344 notify.data_indication = irda_data_indication;
345 notify.flow_indication = irda_flow_indication;
346 notify.instance = self;
347 strncpy(notify.name, name, NOTIFY_MAX_NAME);
349 self->tsap = irttp_open_tsap(tsap_sel, DEFAULT_INITIAL_CREDIT,
350 &notify);
351 if (self->tsap == NULL) {
352 IRDA_DEBUG( 0, __FUNCTION__ "(), Unable to allocate TSAP!\n");
353 return -1;
355 /* Remember which TSAP selector we actually got */
356 self->stsap_sel = self->tsap->stsap_sel;
358 return 0;
362 * Function irda_find_lsap_sel (self, name)
364 * Try to lookup LSAP selector in remote LM-IAS
367 static int irda_find_lsap_sel(struct irda_sock *self, char *name)
369 IRDA_DEBUG(1, __FUNCTION__ "()\n");
371 ASSERT(self != NULL, return -1;);
373 /* Query remote LM-IAS */
374 iriap_getvaluebyclass_request(name, "IrDA:TinyTP:LsapSel",
375 self->saddr, self->daddr,
376 irda_get_value_confirm, self);
377 /* Wait for answer */
378 interruptible_sleep_on(&self->ias_wait);
380 if (self->dtsap_sel)
381 return 0;
383 return -ENETUNREACH; /* May not be true */
387 * Function irda_getname (sock, uaddr, uaddr_len, peer)
389 * Return the our own, or peers socket address (sockaddr_irda)
392 static int irda_getname(struct socket *sock, struct sockaddr *uaddr,
393 int *uaddr_len, int peer)
395 struct sockaddr_irda saddr;
396 struct sock *sk = sock->sk;
398 if (peer) {
399 if (sk->state != TCP_ESTABLISHED)
400 return -ENOTCONN;
402 saddr.sir_family = AF_IRDA;
403 saddr.sir_lsap_sel = sk->protinfo.irda->dtsap_sel;
404 saddr.sir_addr = sk->protinfo.irda->daddr;
405 } else {
406 saddr.sir_family = AF_IRDA;
407 saddr.sir_lsap_sel = sk->protinfo.irda->stsap_sel;
408 saddr.sir_addr = sk->protinfo.irda->saddr;
411 IRDA_DEBUG(1, __FUNCTION__ "(), tsap_sel = %#x\n", saddr.sir_lsap_sel);
412 IRDA_DEBUG(1, __FUNCTION__ "(), addr = %08x\n", saddr.sir_addr);
414 if (*uaddr_len > sizeof (struct sockaddr_irda))
415 *uaddr_len = sizeof (struct sockaddr_irda);
416 memcpy(uaddr, &saddr, *uaddr_len);
418 return 0;
422 * Function irda_listen (sock, backlog)
424 * Just move to the listen state
427 static int irda_listen( struct socket *sock, int backlog)
429 struct sock *sk = sock->sk;
431 if (sk->type == SOCK_STREAM && sk->state != TCP_LISTEN) {
432 sk->max_ack_backlog = backlog;
433 sk->state = TCP_LISTEN;
435 return 0;
438 return -EOPNOTSUPP;
442 * Function irda_bind (sock, uaddr, addr_len)
444 * Used by servers to register their well known TSAP
447 static int irda_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
449 struct sock *sk = sock->sk;
450 struct sockaddr_irda *addr = (struct sockaddr_irda *) uaddr;
451 struct irda_sock *self;
452 __u16 hints = 0;
453 int err;
455 self = sk->protinfo.irda;
456 ASSERT(self != NULL, return -1;);
458 if ((addr_len < sizeof(struct sockaddr_irda)) ||
459 (addr_len > sizeof(struct sockaddr_irda)))
460 return -EINVAL;
462 err = irda_open_tsap(self, addr->sir_lsap_sel, addr->sir_name);
463 if (err < 0)
464 return -ENOMEM;
466 /* Register with LM-IAS */
467 self->ias_obj = irias_new_object(addr->sir_name, jiffies);
468 irias_add_integer_attrib(self->ias_obj, "IrDA:TinyTP:LsapSel",
469 self->stsap_sel);
470 irias_insert_object(self->ias_obj);
472 /* Fill in some default hint bits values */
473 if (strncmp(addr->sir_name, "OBEX", 4) == 0)
474 hints = irlmp_service_to_hint(S_OBEX);
476 if (hints)
477 self->skey = irlmp_register_service(hints);
479 return 0;
483 * Function irda_accept (sock, newsock, flags)
485 * Wait for incomming connection
488 static int irda_accept(struct socket *sock, struct socket *newsock, int flags)
490 struct irda_sock *self, *new;
491 struct sock *sk = sock->sk;
492 struct sock *newsk;
493 struct sk_buff *skb;
494 int err;
496 IRDA_DEBUG(0, __FUNCTION__ "()\n");
498 self = sk->protinfo.irda;
499 ASSERT(self != NULL, return -1;);
501 err = irda_create(newsock, sk->protocol);
502 if (err)
503 return err;
505 if (sock->state != SS_UNCONNECTED)
506 return -EINVAL;
508 if ((sk = sock->sk) == NULL)
509 return -EINVAL;
511 if (sk->type != SOCK_STREAM)
512 return -EOPNOTSUPP;
514 if (sk->state != TCP_LISTEN)
515 return -EINVAL;
518 * The read queue this time is holding sockets ready to use
519 * hooked into the SABM we saved
521 do {
522 if ((skb = skb_dequeue(&sk->receive_queue)) == NULL) {
523 if (flags & O_NONBLOCK)
524 return -EWOULDBLOCK;
526 interruptible_sleep_on(sk->sleep);
527 if (signal_pending(current))
528 return -ERESTARTSYS;
530 } while (skb == NULL);
532 newsk = newsock->sk;
533 newsk->state = TCP_ESTABLISHED;
535 new = newsk->protinfo.irda;
536 ASSERT(new != NULL, return -1;);
538 /* Now attach up the new socket */
539 new->tsap = irttp_dup(self->tsap, new);
540 if (!new->tsap) {
541 IRDA_DEBUG(0, __FUNCTION__ "(), dup failed!\n");
542 return -1;
545 new->stsap_sel = new->tsap->stsap_sel;
546 new->dtsap_sel = new->tsap->dtsap_sel;
547 new->saddr = irttp_get_saddr(new->tsap);
548 new->daddr = irttp_get_daddr(new->tsap);
550 new->max_sdu_size_tx = self->max_sdu_size_tx;
551 new->max_sdu_size_rx = self->max_sdu_size_rx;
552 new->max_data_size = self->max_data_size;
553 new->max_header_size = self->max_header_size;
555 memcpy(&new->qos_tx, &self->qos_tx, sizeof(struct qos_info));
557 /* Clean up the original one to keep it in listen state */
558 self->tsap->dtsap_sel = self->tsap->lsap->dlsap_sel = LSAP_ANY;
559 self->tsap->lsap->lsap_state = LSAP_DISCONNECTED;
561 skb->sk = NULL;
562 skb->destructor = NULL;
563 kfree_skb(skb);
564 sk->ack_backlog--;
566 newsock->state = SS_CONNECTED;
568 irda_connect_response(new);
570 return 0;
574 * Function irda_connect (sock, uaddr, addr_len, flags)
576 * Connect to a IrDA device
579 static int irda_connect(struct socket *sock, struct sockaddr *uaddr,
580 int addr_len, int flags)
582 struct sock *sk = sock->sk;
583 struct sockaddr_irda *addr = (struct sockaddr_irda *) uaddr;
584 struct irda_sock *self;
585 int err;
587 self = sk->protinfo.irda;
589 if (sk->state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
590 sock->state = SS_CONNECTED;
591 return 0; /* Connect completed during a ERESTARTSYS event */
594 if (sk->state == TCP_CLOSE && sock->state == SS_CONNECTING) {
595 sock->state = SS_UNCONNECTED;
596 return -ECONNREFUSED;
599 if (sk->state == TCP_ESTABLISHED)
600 return -EISCONN; /* No reconnect on a seqpacket socket */
602 sk->state = TCP_CLOSE;
603 sock->state = SS_UNCONNECTED;
605 if (addr_len != sizeof(struct sockaddr_irda))
606 return -EINVAL;
608 /* Check if user supplied the required destination device address */
609 if (!addr->sir_addr)
610 return -EINVAL;
612 self->daddr = addr->sir_addr;
613 IRDA_DEBUG(1, __FUNCTION__ "(), daddr = %08x\n", self->daddr);
615 /* Query remote LM-IAS */
616 err = irda_find_lsap_sel(self, addr->sir_name);
617 if (err) {
618 IRDA_DEBUG(0, __FUNCTION__ "(), connect failed!\n");
619 return err;
622 /* Check if we have opened a local TSAP */
623 if (!self->tsap)
624 irda_open_tsap(self, LSAP_ANY, addr->sir_name);
626 /* Move to connecting socket, start sending Connect Requests */
627 sock->state = SS_CONNECTING;
628 sk->state = TCP_SYN_SENT;
630 /* Connect to remote device */
631 err = irttp_connect_request(self->tsap, self->dtsap_sel,
632 self->saddr, self->daddr, NULL,
633 self->max_sdu_size_rx, NULL);
634 if (err) {
635 IRDA_DEBUG(0, __FUNCTION__ "(), connect failed!\n");
636 return err;
639 /* Now the loop */
640 if (sk->state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
641 return -EINPROGRESS;
643 cli(); /* To avoid races on the sleep */
645 /* A Connect Ack with Choke or timeout or failed routing will go to
646 * closed. */
647 while (sk->state == TCP_SYN_SENT) {
648 interruptible_sleep_on(sk->sleep);
649 if (signal_pending(current)) {
650 sti();
651 return -ERESTARTSYS;
655 if (sk->state != TCP_ESTABLISHED) {
656 sti();
657 sock->state = SS_UNCONNECTED;
658 return sock_error(sk); /* Always set at this point */
661 sock->state = SS_CONNECTED;
663 sti();
665 return 0;
669 * Function irda_create (sock, protocol)
671 * Create IrDA socket
674 static int irda_create(struct socket *sock, int protocol)
676 struct sock *sk;
677 struct irda_sock *self;
679 IRDA_DEBUG(2, __FUNCTION__ "()\n");
681 /* Check for valid socket type */
682 switch (sock->type) {
683 case SOCK_STREAM: /* FALLTHROUGH */
684 case SOCK_SEQPACKET:
685 break;
686 default:
687 return -ESOCKTNOSUPPORT;
690 /* Allocate socket */
691 if ((sk = sk_alloc(PF_IRDA, GFP_ATOMIC, 1)) == NULL)
692 return -ENOMEM;
694 self = kmalloc(sizeof(struct irda_sock), GFP_ATOMIC);
695 if (self == NULL)
696 return -ENOMEM;
697 memset(self, 0, sizeof(struct irda_sock));
699 init_waitqueue_head(&self->ias_wait);
701 self->sk = sk;
702 sk->protinfo.irda = self;
703 sock_init_data(sock, sk);
705 if (sock->type == SOCK_STREAM)
706 sock->ops = &irda_stream_ops;
707 else
708 sock->ops = &irda_dgram_ops;
710 sk->protocol = protocol;
712 /* Register as a client with IrLMP */
713 self->ckey = irlmp_register_client(0, NULL, NULL);
714 self->mask = 0xffff;
715 self->rx_flow = self->tx_flow = FLOW_START;
716 self->max_sdu_size_rx = SAR_DISABLE; /* Default value */
717 self->nslots = DISCOVERY_DEFAULT_SLOTS;
719 /* Notify that we are using the irda module, so nobody removes it */
720 irda_mod_inc_use_count();
722 return 0;
726 * Function irda_destroy_socket (self)
728 * Destroy socket
731 void irda_destroy_socket(struct irda_sock *self)
733 IRDA_DEBUG(2, __FUNCTION__ "()\n");
735 ASSERT(self != NULL, return;);
737 /* Unregister with IrLMP */
738 irlmp_unregister_client(self->ckey);
739 irlmp_unregister_service(self->skey);
741 /* Unregister with LM-IAS */
742 if (self->ias_obj)
743 irias_delete_object(self->ias_obj);
745 if (self->tsap) {
746 irttp_disconnect_request(self->tsap, NULL, P_NORMAL);
747 irttp_close_tsap(self->tsap);
748 self->tsap = NULL;
751 kfree(self);
753 /* Notify that we are not using the irda module anymore */
754 irda_mod_dec_use_count();
756 return;
760 * Function irda_release (sock, peer)
765 static int irda_release(struct socket *sock)
767 struct sock *sk = sock->sk;
769 IRDA_DEBUG(1, __FUNCTION__ "()\n");
771 if (sk == NULL)
772 return 0;
774 sk->state = TCP_CLOSE;
775 sk->shutdown |= SEND_SHUTDOWN;
776 sk->state_change(sk);
777 sk->dead = 1;
779 irda_destroy_socket(sk->protinfo.irda);
781 sock->sk = NULL;
782 sk->socket = NULL; /* Not used, but we should do this. */
784 return 0;
788 * Function irda_sendmsg (sock, msg, len, scm)
790 * Send message down to TinyTP
793 static int irda_sendmsg(struct socket *sock, struct msghdr *msg, int len,
794 struct scm_cookie *scm)
796 struct sock *sk = sock->sk;
797 /* struct sockaddr_irda *addr = (struct sockaddr_irda *) msg->msg_name; */
798 struct irda_sock *self;
799 struct sk_buff *skb;
800 unsigned char *asmptr;
801 int err;
803 IRDA_DEBUG(4, __FUNCTION__ "(), len=%d\n", len);
805 if (msg->msg_flags & ~MSG_DONTWAIT)
806 return -EINVAL;
808 if (sk->shutdown & SEND_SHUTDOWN) {
809 send_sig(SIGPIPE, current, 0);
810 return -EPIPE;
813 self = sk->protinfo.irda;
814 ASSERT(self != NULL, return -1;);
816 /* Check if IrTTP is wants us to slow down */
817 while (self->tx_flow == FLOW_STOP) {
818 IRDA_DEBUG(2, __FUNCTION__ "(), IrTTP is busy, going to sleep!\n");
819 interruptible_sleep_on(sk->sleep);
821 /* Check if we are still connected */
822 if (sk->state != TCP_ESTABLISHED)
823 return -ENOTCONN;
826 /* Check that we don't send out to big frames */
827 if (len > self->max_data_size) {
828 IRDA_DEBUG(0, __FUNCTION__ "(), Warning to much data! "
829 "Chopping frame from %d to %d bytes!\n", len,
830 self->max_data_size);
831 len = self->max_data_size;
834 skb = sock_alloc_send_skb(sk, len + self->max_header_size, 0,
835 msg->msg_flags & MSG_DONTWAIT, &err);
836 if (!skb)
837 return -ENOBUFS;
839 skb_reserve(skb, self->max_header_size);
841 IRDA_DEBUG(4, __FUNCTION__ "(), appending user data\n");
842 asmptr = skb->h.raw = skb_put(skb, len);
843 memcpy_fromiovec(asmptr, msg->msg_iov, len);
846 * Just send the message to TinyTP, and let it deal with possible
847 * errors. No need to duplicate all that here
849 err = irttp_data_request(self->tsap, skb);
850 if (err) {
851 IRDA_DEBUG(0, __FUNCTION__ "(), err=%d\n", err);
852 return err;
854 return len;
858 * Function irda_recvmsg (sock, msg, size, flags, scm)
860 * Try to receive message and copy it to user
863 static int irda_recvmsg_dgram(struct socket *sock, struct msghdr *msg,
864 int size, int flags, struct scm_cookie *scm)
866 struct irda_sock *self;
867 struct sock *sk = sock->sk;
868 struct sk_buff *skb;
869 int copied, err;
871 IRDA_DEBUG(4, __FUNCTION__ "()\n");
873 self = sk->protinfo.irda;
874 ASSERT(self != NULL, return -1;);
876 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
877 flags & MSG_DONTWAIT, &err);
878 if (!skb)
879 return err;
881 skb->h.raw = skb->data;
882 copied = skb->len;
884 if (copied > size) {
885 copied = size;
886 msg->msg_flags |= MSG_TRUNC;
888 skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
890 skb_free_datagram(sk, skb);
893 * Check if we have previously stopped IrTTP and we know
894 * have more free space in our rx_queue. If so tell IrTTP
895 * to start delivering frames again before our rx_queue gets
896 * empty
898 if (self->rx_flow == FLOW_STOP) {
899 if ((atomic_read(&sk->rmem_alloc) << 2) <= sk->rcvbuf) {
900 IRDA_DEBUG(2, __FUNCTION__ "(), Starting IrTTP\n");
901 self->rx_flow = FLOW_START;
902 irttp_flow_request(self->tsap, FLOW_START);
906 return copied;
910 * Function irda_data_wait (sk)
912 * Sleep until data has arrive. But check for races..
915 static void irda_data_wait(struct sock *sk)
917 if (!skb_peek(&sk->receive_queue)) {
918 sk->socket->flags |= SO_WAITDATA;
919 interruptible_sleep_on(sk->sleep);
920 sk->socket->flags &= ~SO_WAITDATA;
925 * Function irda_recvmsg_stream (sock, msg, size, flags, scm)
930 static int irda_recvmsg_stream(struct socket *sock, struct msghdr *msg,
931 int size, int flags, struct scm_cookie *scm)
933 struct irda_sock *self;
934 struct sock *sk = sock->sk;
935 int noblock = flags & MSG_DONTWAIT;
936 int copied = 0;
937 int target = 1;
939 IRDA_DEBUG(3, __FUNCTION__ "()\n");
941 self = sk->protinfo.irda;
942 ASSERT(self != NULL, return -1;);
944 if (sock->flags & SO_ACCEPTCON)
945 return(-EINVAL);
947 if (flags & MSG_OOB)
948 return -EOPNOTSUPP;
950 if (flags & MSG_WAITALL)
951 target = size;
954 msg->msg_namelen = 0;
956 /* Lock the socket to prevent queue disordering
957 * while sleeps in memcpy_tomsg
959 /* down(&self->readsem); */
961 do {
962 int chunk;
963 struct sk_buff *skb;
965 skb=skb_dequeue(&sk->receive_queue);
966 if (skb==NULL) {
967 if (copied >= target)
968 break;
971 * POSIX 1003.1g mandates this order.
974 if (sk->err) {
975 /* up(&self->readsem); */
976 return sock_error(sk);
979 if (sk->shutdown & RCV_SHUTDOWN)
980 break;
982 /* up(&self->readsem); */
984 if (noblock)
985 return -EAGAIN;
986 irda_data_wait(sk);
987 if (signal_pending(current))
988 return -ERESTARTSYS;
989 /* down(&self->readsem); */
990 continue;
993 /* Never glue messages from different writers */
994 /* if (check_creds && */
995 /* memcmp(UNIXCREDS(skb), &scm->creds, sizeof(scm->creds)) != 0) */
996 /* { */
997 /* skb_queue_head(&sk->receive_queue, skb); */
998 /* break; */
999 /* } */
1001 chunk = min(skb->len, size);
1002 if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
1003 skb_queue_head(&sk->receive_queue, skb);
1004 if (copied == 0)
1005 copied = -EFAULT;
1006 break;
1008 copied += chunk;
1009 size -= chunk;
1011 /* Copy credentials */
1012 /* scm->creds = *UNIXCREDS(skb); */
1013 /* check_creds = 1; */
1015 /* Mark read part of skb as used */
1016 if (!(flags & MSG_PEEK)) {
1017 skb_pull(skb, chunk);
1019 /* if (UNIXCB(skb).fp) */
1020 /* unix_detach_fds(scm, skb); */
1022 /* put the skb back if we didn't use it up.. */
1023 if (skb->len) {
1024 IRDA_DEBUG(1, __FUNCTION__ "(), back on q!\n");
1025 skb_queue_head(&sk->receive_queue, skb);
1026 break;
1029 kfree_skb(skb);
1031 /* if (scm->fp) */
1032 /* break; */
1033 } else {
1034 IRDA_DEBUG(0, __FUNCTION__ "() questionable!?\n");
1035 /* It is questionable, see note in unix_dgram_recvmsg. */
1036 /* if (UNIXCB(skb).fp) */
1037 /* scm->fp = scm_fp_dup(UNIXCB(skb).fp); */
1039 /* put message back and return */
1040 skb_queue_head(&sk->receive_queue, skb);
1041 break;
1043 } while (size);
1046 * Check if we have previously stopped IrTTP and we know
1047 * have more free space in our rx_queue. If so tell IrTTP
1048 * to start delivering frames again before our rx_queue gets
1049 * empty
1051 if (self->rx_flow == FLOW_STOP) {
1052 if ((atomic_read(&sk->rmem_alloc) << 2) <= sk->rcvbuf) {
1053 IRDA_DEBUG(2, __FUNCTION__ "(), Starting IrTTP\n");
1054 self->rx_flow = FLOW_START;
1055 irttp_flow_request(self->tsap, FLOW_START);
1059 /* up(&self->readsem); */
1061 return copied;
1065 * Function irda_shutdown (sk, how)
1070 static int irda_shutdown( struct socket *sk, int how)
1072 IRDA_DEBUG( 0, __FUNCTION__ "()\n");
1074 /* FIXME - generate DM and RNR states */
1075 return -EOPNOTSUPP;
1079 * Function irda_poll (file, sock, wait)
1084 static unsigned int irda_poll(struct file * file, struct socket *sock,
1085 poll_table *wait)
1087 struct sock *sk = sock->sk;
1088 unsigned int mask;
1090 IRDA_DEBUG(1, __FUNCTION__ "()\n");
1092 poll_wait(file, sk->sleep, wait);
1093 mask = 0;
1095 /* exceptional events? */
1096 if (sk->err)
1097 mask |= POLLERR;
1098 if (sk->shutdown & RCV_SHUTDOWN)
1099 mask |= POLLHUP;
1101 /* readable? */
1102 if (!skb_queue_empty(&sk->receive_queue))
1103 mask |= POLLIN | POLLRDNORM;
1105 /* Connection-based need to check for termination and startup */
1106 if (sk->type == SOCK_STREAM && sk->state==TCP_CLOSE)
1107 mask |= POLLHUP;
1110 * we set writable also when the other side has shut down the
1111 * connection. This prevents stuck sockets.
1113 if (sk->sndbuf - (int)atomic_read(&sk->wmem_alloc) >= MIN_WRITE_SPACE)
1114 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1116 return mask;
1120 * Function irda_ioctl (sock, cmd, arg)
1125 static int irda_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1127 struct sock *sk = sock->sk;
1129 IRDA_DEBUG(4, __FUNCTION__ "(), cmd=%#x\n", cmd);
1131 switch (cmd) {
1132 case TIOCOUTQ: {
1133 long amount;
1134 amount = sk->sndbuf - atomic_read(&sk->wmem_alloc);
1135 if (amount < 0)
1136 amount = 0;
1137 if (put_user(amount, (unsigned int *)arg))
1138 return -EFAULT;
1139 return 0;
1142 case TIOCINQ: {
1143 struct sk_buff *skb;
1144 long amount = 0L;
1145 /* These two are safe on a single CPU system as only user tasks fiddle here */
1146 if ((skb = skb_peek(&sk->receive_queue)) != NULL)
1147 amount = skb->len;
1148 if (put_user(amount, (unsigned int *)arg))
1149 return -EFAULT;
1150 return 0;
1153 case SIOCGSTAMP:
1154 if (sk != NULL) {
1155 if (sk->stamp.tv_sec == 0)
1156 return -ENOENT;
1157 if (copy_to_user((void *)arg, &sk->stamp,
1158 sizeof(struct timeval)))
1159 return -EFAULT;
1160 return 0;
1162 return -EINVAL;
1164 case SIOCGIFADDR:
1165 case SIOCSIFADDR:
1166 case SIOCGIFDSTADDR:
1167 case SIOCSIFDSTADDR:
1168 case SIOCGIFBRDADDR:
1169 case SIOCSIFBRDADDR:
1170 case SIOCGIFNETMASK:
1171 case SIOCSIFNETMASK:
1172 case SIOCGIFMETRIC:
1173 case SIOCSIFMETRIC:
1174 return -EINVAL;
1175 default:
1176 IRDA_DEBUG(1, __FUNCTION__ "(), doing device ioctl!\n");
1177 return dev_ioctl(cmd, (void *) arg);
1180 /*NOTREACHED*/
1181 return 0;
1185 * Function irda_setsockopt (sock, level, optname, optval, optlen)
1187 * Set some options for the socket
1190 static int irda_setsockopt(struct socket *sock, int level, int optname,
1191 char *optval, int optlen)
1193 struct sock *sk = sock->sk;
1194 struct irda_sock *self;
1195 int opt;
1197 self = sk->protinfo.irda;
1198 ASSERT(self != NULL, return -1;);
1200 if (level != SOL_IRLMP)
1201 return -ENOPROTOOPT;
1203 if (optlen < sizeof(int))
1204 return -EINVAL;
1206 if (get_user(opt, (int *)optval))
1207 return -EFAULT;
1209 switch (optname) {
1210 case IRLMP_IAS_SET:
1211 IRDA_DEBUG(0, __FUNCTION__ "(), sorry not impl. yet!\n");
1212 return 0;
1213 case IRTTP_MAX_SDU_SIZE:
1214 IRDA_DEBUG(2, __FUNCTION__ "(), setting max_sdu_size = %d\n", opt);
1215 self->max_sdu_size_rx = opt;
1216 break;
1217 default:
1218 return -ENOPROTOOPT;
1220 return 0;
1224 * Function irda_getsockopt (sock, level, optname, optval, optlen)
1229 static int irda_getsockopt(struct socket *sock, int level, int optname,
1230 char *optval, int *optlen)
1232 struct sock *sk = sock->sk;
1233 struct irda_sock *self;
1234 struct irda_device_list list;
1235 struct irda_device_info *info;
1236 discovery_t *discovery;
1237 int val = 0;
1238 int len = 0;
1239 int offset, total;
1241 self = sk->protinfo.irda;
1243 if (level != SOL_IRLMP)
1244 return -ENOPROTOOPT;
1246 if (get_user(len, optlen))
1247 return -EFAULT;
1249 switch (optname) {
1250 case IRLMP_ENUMDEVICES:
1251 /* Tell IrLMP we want to be notified */
1252 irlmp_update_client(self->ckey, self->mask, NULL,
1253 irda_discovery_indication);
1255 /* Do some discovery */
1256 irlmp_discovery_request(self->nslots);
1258 /* Check if the we got some results */
1259 if (!cachelog)
1260 return -EAGAIN;
1262 info = &list.dev[0];
1264 /* Offset to first device entry */
1265 offset = sizeof(struct irda_device_list) -
1266 sizeof(struct irda_device_info);
1268 total = offset; /* Initialized to size of the device list */
1269 list.len = 0; /* Initialize lenght of list */
1272 * Now, check all discovered devices (if any), and notify
1273 * client only about the services that the client is
1274 * interested in
1276 discovery = (discovery_t *) hashbin_get_first(cachelog);
1277 while (discovery != NULL) {
1278 /* Mask out the ones we don't want */
1279 if (discovery->hints.word & self->mask) {
1281 /* Check if room for this device entry */
1282 if (len - total < sizeof(struct irda_device_info))
1283 break;
1285 /* Copy discovery information */
1286 info->saddr = discovery->saddr;
1287 info->daddr = discovery->daddr;
1288 info->charset = discovery->charset;
1289 info->hints[0] = discovery->hints.byte[0];
1290 info->hints[1] = discovery->hints.byte[1];
1291 strncpy(info->info, discovery->nickname,
1292 NICKNAME_MAX_LEN);
1294 if (copy_to_user(optval+offset, info,
1295 sizeof(struct irda_device_info)))
1296 return -EFAULT;
1297 list.len++;
1298 total += sizeof(struct irda_device_info);
1300 discovery = (discovery_t *) hashbin_get_next(cachelog);
1302 cachelog = NULL;
1304 /* Write total number of bytes used back to client */
1305 if (put_user(total, optlen))
1306 return -EFAULT;
1308 /* Write total list length back to client */
1309 if (copy_to_user(optval, &list,
1310 sizeof(struct irda_device_list) -
1311 sizeof(struct irda_device_info)))
1312 return -EFAULT;
1313 break;
1314 case IRTTP_MAX_SDU_SIZE:
1315 val = self->max_data_size;
1316 IRDA_DEBUG(2, __FUNCTION__ "(), getting max_sdu_size = %d\n", val);
1317 len = sizeof(int);
1318 if (put_user(len, optlen))
1319 return -EFAULT;
1321 if (copy_to_user(optval, &val, len))
1322 return -EFAULT;
1323 break;
1324 default:
1325 return -ENOPROTOOPT;
1328 return 0;
1331 static struct net_proto_family irda_family_ops =
1333 PF_IRDA,
1334 irda_create
1337 static struct proto_ops SOCKOPS_WRAPPED(irda_stream_ops) = {
1338 PF_IRDA,
1340 irda_release,
1341 irda_bind,
1342 irda_connect,
1343 sock_no_socketpair,
1344 irda_accept,
1345 irda_getname,
1346 irda_poll,
1347 irda_ioctl,
1348 irda_listen,
1349 irda_shutdown,
1350 irda_setsockopt,
1351 irda_getsockopt,
1352 sock_no_fcntl,
1353 irda_sendmsg,
1354 irda_recvmsg_stream,
1355 sock_no_mmap
1358 static struct proto_ops SOCKOPS_WRAPPED(irda_dgram_ops) = {
1359 PF_IRDA,
1361 irda_release,
1362 irda_bind,
1363 irda_connect,
1364 sock_no_socketpair,
1365 irda_accept,
1366 irda_getname,
1367 datagram_poll,
1368 irda_ioctl,
1369 irda_listen,
1370 irda_shutdown,
1371 irda_setsockopt,
1372 irda_getsockopt,
1373 sock_no_fcntl,
1374 irda_sendmsg,
1375 irda_recvmsg_dgram,
1376 sock_no_mmap
1379 #include <linux/smp_lock.h>
1380 SOCKOPS_WRAP(irda_dgram, PF_IRDA);
1381 SOCKOPS_WRAP(irda_stream, PF_IRDA);
1384 * Function irda_device_event (this, event, ptr)
1386 * Called when a device is taken up or down
1389 static int irda_device_event(struct notifier_block *this, unsigned long event,
1390 void *ptr)
1392 struct net_device *dev = (struct net_device *) ptr;
1394 /* Reject non IrDA devices */
1395 if (dev->type != ARPHRD_IRDA)
1396 return NOTIFY_DONE;
1398 switch (event) {
1399 case NETDEV_UP:
1400 IRDA_DEBUG(3, __FUNCTION__ "(), NETDEV_UP\n");
1401 /* irda_dev_device_up(dev); */
1402 break;
1403 case NETDEV_DOWN:
1404 IRDA_DEBUG(3, __FUNCTION__ "(), NETDEV_DOWN\n");
1405 /* irda_kill_by_device(dev); */
1406 /* irda_rt_device_down(dev); */
1407 /* irda_dev_device_down(dev); */
1408 break;
1409 default:
1410 break;
1413 return NOTIFY_DONE;
1416 static struct packet_type irda_packet_type =
1418 0, /* MUTTER ntohs(ETH_P_IRDA),*/
1419 NULL,
1420 irlap_driver_rcv,
1421 NULL,
1422 NULL,
1425 static struct notifier_block irda_dev_notifier = {
1426 irda_device_event,
1427 NULL,
1432 * Function irda_proto_init (pro)
1434 * Initialize IrDA protocol layer
1437 void __init irda_proto_init(struct net_proto *pro)
1439 sock_register(&irda_family_ops);
1441 irda_packet_type.type = htons(ETH_P_IRDA);
1442 dev_add_pack(&irda_packet_type);
1444 register_netdevice_notifier(&irda_dev_notifier);
1446 irda_init();
1450 * Function irda_proto_cleanup (void)
1452 * Remove IrDA protocol layer
1455 #ifdef MODULE
1456 void irda_proto_cleanup(void)
1458 irda_packet_type.type = htons(ETH_P_IRDA);
1459 dev_remove_pack(&irda_packet_type);
1461 unregister_netdevice_notifier(&irda_dev_notifier);
1463 sock_unregister(PF_IRDA);
1464 irda_cleanup();
1466 return;
1468 #endif /* MODULE */