1 /*********************************************************************
2 *
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.
12 *
13 * Copyright (c) 1999 Dag Brattli <dagb@cs.uit.no>
14 * Copyright (c) 1999 Jean Tourrilhes <jt@hpl.hp.com>
15 * All Rights Reserved.
16 *
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.
21 *
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.
26 *
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
31 *
32 * Linux-IrDA now supports four different types of IrDA sockets:
33 *
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
42 *
43 ********************************************************************/
44
45 #include <linux/config.h>
46 #include <linux/module.h>
47 #include <linux/types.h>
48 #include <linux/socket.h>
49 #include <linux/sockios.h>
50 #include <linux/init.h>
51 #include <linux/if_arp.h>
52 #include <linux/net.h>
53 #include <linux/irda.h>
54 #include <linux/poll.h>
55
56 #include <asm/uaccess.h>
57
58 #include <net/sock.h>
59
60 #include <net/irda/irda.h>
61 #include <net/irda/iriap.h>
62 #include <net/irda/irias_object.h>
63 #include <net/irda/irlmp.h>
64 #include <net/irda/irttp.h>
65 #include <net/irda/discovery.h>
66
67 extern int irda_init(void);
68 extern void irda_cleanup(void);
69 extern int irlap_driver_rcv(struct sk_buff *, struct net_device *,
70 struct packet_type *);
71
72 static int irda_create(struct socket *sock, int protocol);
73
74 static struct proto_ops irda_stream_ops;
75 static struct proto_ops irda_seqpacket_ops;
76 static struct proto_ops irda_dgram_ops;
77
78 #ifdef CONFIG_IRDA_ULTRA
79 static struct proto_ops irda_ultra_ops;
80 #define ULTRA_MAX_DATA 382
81 #endif /* CONFIG_IRDA_ULTRA */
82
83 #define IRDA_MAX_HEADER (TTP_MAX_HEADER)
84
85 #ifdef CONFIG_IRDA_DEBUG
86 __u32 irda_debug = IRDA_DEBUG_LEVEL;
87 #endif
88
89 /*
90 * Function irda_data_indication (instance, sap, skb)
91 *
92 * Received some data from TinyTP. Just queue it on the receive queue
93 *
94 */
95 static int irda_data_indication(void *instance, void *sap, struct sk_buff *skb)
96 {
97 struct irda_sock *self;
98 struct sock *sk;
99 int err;
100
101 self = (struct irda_sock *) instance;
102 ASSERT(self != NULL, return -1;);
103
104 sk = self->sk;
105 ASSERT(sk != NULL, return -1;);
106
107 err = sock_queue_rcv_skb(sk, skb);
108 if (err) {
109 IRDA_DEBUG(1, __FUNCTION__ "(), error: no more mem!\n");
110 self->rx_flow = FLOW_STOP;
111
112 /* When we return error, TTP will need to requeue the skb */
113 return err;
114 }
115
116 return 0;
117 }
118
119 /*
120 * Function irda_disconnect_indication (instance, sap, reason, skb)
121 *
122 * Connection has been closed. Check reason to find out why
123 *
124 */
125 static void irda_disconnect_indication(void *instance, void *sap,
126 LM_REASON reason, struct sk_buff *skb)
127 {
128 struct irda_sock *self;
129 struct sock *sk;
130
131 IRDA_DEBUG(2, __FUNCTION__ "()\n");
132
133 self = (struct irda_sock *) instance;
134
135 sk = self->sk;
136 if (sk == NULL)
137 return;
138
139 sk->state = TCP_CLOSE;
140 sk->err = ECONNRESET;
141 sk->shutdown |= SEND_SHUTDOWN;
142 if (!sk->dead) {
143 sk->state_change(sk);
144 sk->dead = 1;
145 }
146
147 /* Close our TSAP.
148 * If we leave it open, IrLMP put it back into the list of
149 * unconnected LSAPs. The problem is that any incomming request
150 * can then be matched to this socket (and it will be, because
151 * it is at the head of the list). This would prevent any
152 * listening socket waiting on the same TSAP to get those requests.
153 * Some apps forget to close sockets, or hang to it a bit too long,
154 * so we may stay in this dead state long enough to be noticed...
155 * Note : all socket function do check sk->state, so we are safe...
156 * Jean II
157 */
158 irttp_close_tsap(self->tsap);
159 self->tsap = NULL;
160
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->err,
164 * sk->shutdown and sk->dead to valid values...
165 * Jean II
166 */
167 }
168
169 /*
170 * Function irda_connect_confirm (instance, sap, qos, max_sdu_size, skb)
171 *
172 * Connections has been confirmed by the remote device
173 *
174 */
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)
179 {
180 struct irda_sock *self;
181 struct sock *sk;
182
183 IRDA_DEBUG(2, __FUNCTION__ "()\n");
184
185 self = (struct irda_sock *) instance;
186
187 sk = self->sk;
188 if (sk == NULL)
189 return;
190
191 /* How much header space do we need to reserve */
192 self->max_header_size = max_header_size;
193
194 /* IrTTP max SDU size in transmit direction */
195 self->max_sdu_size_tx = max_sdu_size;
196
197 /* Find out what the largest chunk of data that we can transmit is */
198 switch (sk->type) {
199 case SOCK_STREAM:
200 if (max_sdu_size != 0) {
201 ERROR(__FUNCTION__ "(), max_sdu_size must be 0\n");
202 return;
203 }
204 self->max_data_size = irttp_get_max_seg_size(self->tsap);
205 break;
206 case SOCK_SEQPACKET:
207 if (max_sdu_size == 0) {
208 ERROR(__FUNCTION__ "(), max_sdu_size cannot be 0\n");
209 return;
210 }
211 self->max_data_size = max_sdu_size;
212 break;
213 default:
214 self->max_data_size = irttp_get_max_seg_size(self->tsap);
215 };
216
217 IRDA_DEBUG(2, __FUNCTION__ "(), max_data_size=%d\n",
218 self->max_data_size);
219
220 memcpy(&self->qos_tx, qos, sizeof(struct qos_info));
221
222 skb_queue_tail(&sk->receive_queue, skb);
223
224 /* We are now connected! */
225 sk->state = TCP_ESTABLISHED;
226 sk->state_change(sk);
227 }
228
229 /*
230 * Function irda_connect_indication(instance, sap, qos, max_sdu_size, userdata)
231 *
232 * Incomming connection
233 *
234 */
235 static void irda_connect_indication(void *instance, void *sap,
236 struct qos_info *qos, __u32 max_sdu_size,
237 __u8 max_header_size, struct sk_buff *skb)
238 {
239 struct irda_sock *self;
240 struct sock *sk;
241
242 IRDA_DEBUG(2, __FUNCTION__ "()\n");
243
244 self = (struct irda_sock *) instance;
245
246 sk = self->sk;
247 if (sk == NULL)
248 return;
249
250 /* How much header space do we need to reserve */
251 self->max_header_size = max_header_size;
252
253 /* IrTTP max SDU size in transmit direction */
254 self->max_sdu_size_tx = max_sdu_size;
255
256 /* Find out what the largest chunk of data that we can transmit is */
257 switch (sk->type) {
258 case SOCK_STREAM:
259 if (max_sdu_size != 0) {
260 ERROR(__FUNCTION__ "(), max_sdu_size must be 0\n");
261 return;
262 }
263 self->max_data_size = irttp_get_max_seg_size(self->tsap);
264 break;
265 case SOCK_SEQPACKET:
266 if (max_sdu_size == 0) {
267 ERROR(__FUNCTION__ "(), max_sdu_size cannot be 0\n");
268 return;
269 }
270 self->max_data_size = max_sdu_size;
271 break;
272 default:
273 self->max_data_size = irttp_get_max_seg_size(self->tsap);
274 };
275
276 IRDA_DEBUG(2, __FUNCTION__ "(), max_data_size=%d\n",
277 self->max_data_size);
278
279 memcpy(&self->qos_tx, qos, sizeof(struct qos_info));
280
281 skb_queue_tail(&sk->receive_queue, skb);
282 sk->state_change(sk);
283 }
284
285 /*
286 * Function irda_connect_response (handle)
287 *
288 * Accept incomming connection
289 *
290 */
291 void irda_connect_response(struct irda_sock *self)
292 {
293 struct sk_buff *skb;
294
295 IRDA_DEBUG(2, __FUNCTION__ "()\n");
296
297 ASSERT(self != NULL, return;);
298
299 skb = dev_alloc_skb(64);
300 if (skb == NULL) {
301 IRDA_DEBUG(0, __FUNCTION__ "() Unable to allocate sk_buff!\n");
302 return;
303 }
304
305 /* Reserve space for MUX_CONTROL and LAP header */
306 skb_reserve(skb, IRDA_MAX_HEADER);
307
308 irttp_connect_response(self->tsap, self->max_sdu_size_rx, skb);
309 }
310
311 /*
312 * Function irda_flow_indication (instance, sap, flow)
313 *
314 * Used by TinyTP to tell us if it can accept more data or not
315 *
316 */
317 static void irda_flow_indication(void *instance, void *sap, LOCAL_FLOW flow)
318 {
319 struct irda_sock *self;
320 struct sock *sk;
321
322 IRDA_DEBUG(2, __FUNCTION__ "()\n");
323
324 self = (struct irda_sock *) instance;
325 ASSERT(self != NULL, return;);
326
327 sk = self->sk;
328 ASSERT(sk != NULL, return;);
329
330 switch (flow) {
331 case FLOW_STOP:
332 IRDA_DEBUG(1, __FUNCTION__ "(), IrTTP wants us to slow down\n");
333 self->tx_flow = flow;
334 break;
335 case FLOW_START:
336 self->tx_flow = flow;
337 IRDA_DEBUG(1, __FUNCTION__
338 "(), IrTTP wants us to start again\n");
339 wake_up_interruptible(sk->sleep);
340 break;
341 default:
342 IRDA_DEBUG( 0, __FUNCTION__ "(), Unknown flow command!\n");
343 /* Unknown flow command, better stop */
344 self->tx_flow = flow;
345 break;
346 }
347 }
348
349 /*
350 * Function irda_getvalue_confirm (obj_id, value, priv)
351 *
352 * Got answer from remote LM-IAS, just pass object to requester...
353 *
354 * Note : duplicate from above, but we need our own version that
355 * doesn't touch the dtsap_sel and save the full value structure...
356 */
357 static void irda_getvalue_confirm(int result, __u16 obj_id,
358 struct ias_value *value, void *priv)
359 {
360 struct irda_sock *self;
361
362 IRDA_DEBUG(2, __FUNCTION__ "()\n");
363
364 self = (struct irda_sock *) priv;
365 if (!self) {
366 WARNING(__FUNCTION__ "(), lost myself!\n");
367 return;
368 }
369
370 /* We probably don't need to make any more queries */
371 iriap_close(self->iriap);
372 self->iriap = NULL;
373
374 /* Check if request succeeded */
375 if (result != IAS_SUCCESS) {
376 IRDA_DEBUG(1, __FUNCTION__ "(), IAS query failed! (%d)\n",
377 result);
378
379 self->errno = result; /* We really need it later */
380
381 /* Wake up any processes waiting for result */
382 wake_up_interruptible(&self->query_wait);
383
384 return;
385 }
386
387 /* Pass the object to the caller (so the caller must delete it) */
388 self->ias_result = value;
389 self->errno = 0;
390
391 /* Wake up any processes waiting for result */
392 wake_up_interruptible(&self->query_wait);
393 }
394
395 /*
396 * Function irda_selective_discovery_indication (discovery)
397 *
398 * Got a selective discovery indication from IrLMP.
399 *
400 * IrLMP is telling us that this node is matching our hint bit
401 * filter. Check if it's a newly discovered node (or if node changed its
402 * hint bits), and then wake up any process waiting for answer...
403 */
404 static void irda_selective_discovery_indication(discovery_t *discovery,
405 void *priv)
406 {
407 struct irda_sock *self;
408
409 IRDA_DEBUG(2, __FUNCTION__ "()\n");
410
411 self = (struct irda_sock *) priv;
412 if (!self) {
413 WARNING(__FUNCTION__ "(), lost myself!\n");
414 return;
415 }
416
417 /* Check if node is discovered is a new one or an old one.
418 * We check when how long ago this node was discovered, with a
419 * coarse timeout (we may miss some discovery events or be delayed).
420 * Note : by doing this test here, we avoid waking up a process ;-)
421 */
422 if((jiffies - discovery->first_timestamp) >
423 (sysctl_discovery_timeout * HZ)) {
424 return; /* Too old, not interesting -> goodbye */
425 }
426
427 /* Pass parameter to the caller */
428 self->cachediscovery = discovery;
429
430 /* Wake up process if its waiting for device to be discovered */
431 wake_up_interruptible(&self->query_wait);
432 }
433
434 /*
435 * Function irda_discovery_timeout (priv)
436 *
437 * Timeout in the selective discovery process
438 *
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...
441 */
442 static void irda_discovery_timeout(u_long priv)
443 {
444 struct irda_sock *self;
445
446 IRDA_DEBUG(2, __FUNCTION__ "()\n");
447
448 self = (struct irda_sock *) priv;
449 ASSERT(self != NULL, return;);
450
451 /* Nothing for the caller */
452 self->cachelog = NULL;
453 self->cachediscovery = NULL;
454 self->errno = -ETIME;
455
456 /* Wake up process if its still waiting... */
457 wake_up_interruptible(&self->query_wait);
458 }
459
460 /*
461 * Function irda_open_tsap (self)
462 *
463 * Open local Transport Service Access Point (TSAP)
464 *
465 */
466 static int irda_open_tsap(struct irda_sock *self, __u8 tsap_sel, char *name)
467 {
468 notify_t notify;
469
470 if (self->tsap) {
471 WARNING(__FUNCTION__ "(), busy!\n");
472 return -EBUSY;
473 }
474
475 /* Initialize callbacks to be used by the IrDA stack */
476 irda_notify_init(¬ify);
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);
485
486 self->tsap = irttp_open_tsap(tsap_sel, DEFAULT_INITIAL_CREDIT,
487 ¬ify);
488 if (self->tsap == NULL) {
489 IRDA_DEBUG( 0, __FUNCTION__ "(), Unable to allocate TSAP!\n");
490 return -ENOMEM;
491 }
492 /* Remember which TSAP selector we actually got */
493 self->stsap_sel = self->tsap->stsap_sel;
494
495 return 0;
496 }
497
498 /*
499 * Function irda_open_lsap (self)
500 *
501 * Open local Link Service Access Point (LSAP). Used for opening Ultra
502 * sockets
503 */
504 #ifdef CONFIG_IRDA_ULTRA
505 static int irda_open_lsap(struct irda_sock *self, int pid)
506 {
507 notify_t notify;
508
509 if (self->lsap) {
510 WARNING(__FUNCTION__ "(), busy!\n");
511 return -EBUSY;
512 }
513
514 /* Initialize callbacks to be used by the IrDA stack */
515 irda_notify_init(¬ify);
516 notify.udata_indication = irda_data_indication;
517 notify.instance = self;
518 strncpy(notify.name, "Ultra", NOTIFY_MAX_NAME);
519
520 self->lsap = irlmp_open_lsap(LSAP_CONNLESS, ¬ify, pid);
521 if (self->lsap == NULL) {
522 IRDA_DEBUG( 0, __FUNCTION__ "(), Unable to allocate LSAP!\n");
523 return -ENOMEM;
524 }
525
526 return 0;
527 }
528 #endif /* CONFIG_IRDA_ULTRA */
529
530 /*
531 * Function irda_find_lsap_sel (self, name)
532 *
533 * Try to lookup LSAP selector in remote LM-IAS
534 *
535 * Basically, we start a IAP query, and then go to sleep. When the query
536 * return, irda_getvalue_confirm will wake us up, and we can examine the
537 * result of the query...
538 * Note that in some case, the query fail even before we go to sleep,
539 * creating some races...
540 */
541 static int irda_find_lsap_sel(struct irda_sock *self, char *name)
542 {
543 IRDA_DEBUG(2, __FUNCTION__ "(), name=%s\n", name);
544
545 ASSERT(self != NULL, return -1;);
546
547 if (self->iriap) {
548 WARNING(__FUNCTION__ "(), busy with a previous query\n");
549 return -EBUSY;
550 }
551
552 self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
553 irda_getvalue_confirm);
554
555 /* Treat unexpected signals as disconnect */
556 self->errno = -EHOSTUNREACH;
557
558 /* Query remote LM-IAS */
559 iriap_getvaluebyclass_request(self->iriap, self->saddr, self->daddr,
560 name, "IrDA:TinyTP:LsapSel");
561 /* Wait for answer (if not already failed) */
562 if(self->iriap != NULL)
563 interruptible_sleep_on(&self->query_wait);
564
565 /* Check what happened */
566 if (self->errno)
567 {
568 /* Requested object/attribute doesn't exist */
569 if((self->errno == IAS_CLASS_UNKNOWN) ||
570 (self->errno == IAS_ATTRIB_UNKNOWN))
571 return (-EADDRNOTAVAIL);
572 else
573 return (-EHOSTUNREACH);
574 }
575
576 /* Get the remote TSAP selector */
577 switch (self->ias_result->type) {
578 case IAS_INTEGER:
579 IRDA_DEBUG(4, __FUNCTION__ "() int=%d\n",
580 self->ias_result->t.integer);
581
582 if (self->ias_result->t.integer != -1)
583 self->dtsap_sel = self->ias_result->t.integer;
584 else
585 self->dtsap_sel = 0;
586 break;
587 default:
588 self->dtsap_sel = 0;
589 IRDA_DEBUG(0, __FUNCTION__ "(), bad type!\n");
590 break;
591 }
592 if (self->ias_result)
593 irias_delete_value(self->ias_result);
594
595 if (self->dtsap_sel)
596 return 0;
597
598 return -EADDRNOTAVAIL;
599 }
600
601 /*
602 * Function irda_discover_daddr_and_lsap_sel (self, name)
603 *
604 * This try to find a device with the requested service.
605 *
606 * It basically look into the discovery log. For each address in the list,
607 * it queries the LM-IAS of the device to find if this device offer
608 * the requested service.
609 * If there is more than one node supporting the service, we complain
610 * to the user (it should move devices around).
611 * The, we set both the destination address and the lsap selector to point
612 * on the service on the unique device we have found.
613 *
614 * Note : this function fails if there is more than one device in range,
615 * because IrLMP doesn't disconnect the LAP when the last LSAP is closed.
616 * Moreover, we would need to wait the LAP disconnection...
617 */
618 static int irda_discover_daddr_and_lsap_sel(struct irda_sock *self, char *name)
619 {
620 struct irda_device_info *discoveries; /* Copy of the discovery log */
621 int number; /* Number of nodes in the log */
622 int i;
623 int err = -ENETUNREACH;
624 __u32 daddr = DEV_ADDR_ANY; /* Address we found the service on */
625 __u8 dtsap_sel = 0x0; /* TSAP associated with it */
626
627 IRDA_DEBUG(2, __FUNCTION__ "(), name=%s\n", name);
628
629 ASSERT(self != NULL, return -1;);
630
631 /* Ask lmp for the current discovery log
632 * Note : we have to use irlmp_get_discoveries(), as opposed
633 * to play with the cachelog directly, because while we are
634 * making our ias query, le log might change... */
635 discoveries = irlmp_get_discoveries(&number, self->mask);
636 /* Check if the we got some results */
637 if (discoveries == NULL)
638 return -ENETUNREACH; /* No nodes discovered */
639
640 /*
641 * Now, check all discovered devices (if any), and connect
642 * client only about the services that the client is
643 * interested in...
644 */
645 for(i = 0; i < number; i++) {
646 /* Try the address in the log */
647 self->daddr = discoveries[i].daddr;
648 self->saddr = 0x0;
649 IRDA_DEBUG(1, __FUNCTION__ "(), trying daddr = %08x\n",
650 self->daddr);
651
652 /* Query remote LM-IAS for this service */
653 err = irda_find_lsap_sel(self, name);
654 switch (err) {
655 case 0:
656 /* We found the requested service */
657 if(daddr != DEV_ADDR_ANY) {
658 IRDA_DEBUG(1, __FUNCTION__
659 "(), discovered service ''%s'' in two different devices !!!\n",
660 name);
661 self->daddr = DEV_ADDR_ANY;
662 kfree(discoveries);
663 return(-ENOTUNIQ);
664 }
665 /* First time we found that one, save it ! */
666 daddr = self->daddr;
667 dtsap_sel = self->dtsap_sel;
668 break;
669 case -EADDRNOTAVAIL:
670 /* Requested service simply doesn't exist on this node */
671 break;
672 default:
673 /* Something bad did happen :-( */
674 IRDA_DEBUG(0, __FUNCTION__
675 "(), unexpected IAS query failure\n");
676 self->daddr = DEV_ADDR_ANY;
677 kfree(discoveries);
678 return(-EHOSTUNREACH);
679 break;
680 }
681 }
682 /* Cleanup our copy of the discovery log */
683 kfree(discoveries);
684
685 /* Check out what we found */
686 if(daddr == DEV_ADDR_ANY) {
687 IRDA_DEBUG(1, __FUNCTION__
688 "(), cannot discover service ''%s'' in any device !!!\n",
689 name);
690 self->daddr = DEV_ADDR_ANY;
691 return(-EADDRNOTAVAIL);
692 }
693
694 /* Revert back to discovered device & service */
695 self->daddr = daddr;
696 self->saddr = 0x0;
697 self->dtsap_sel = dtsap_sel;
698
699 IRDA_DEBUG(1, __FUNCTION__
700 "(), discovered requested service ''%s'' at address %08x\n",
701 name, self->daddr);
702
703 return 0;
704 }
705
706 /*
707 * Function irda_getname (sock, uaddr, uaddr_len, peer)
708 *
709 * Return the our own, or peers socket address (sockaddr_irda)
710 *
711 */
712 static int irda_getname(struct socket *sock, struct sockaddr *uaddr,
713 int *uaddr_len, int peer)
714 {
715 struct sockaddr_irda saddr;
716 struct sock *sk = sock->sk;
717 struct irda_sock *self = sk->protinfo.irda;
718
719 if (peer) {
720 if (sk->state != TCP_ESTABLISHED)
721 return -ENOTCONN;
722
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;
730 }
731
732 IRDA_DEBUG(1, __FUNCTION__ "(), tsap_sel = %#x\n", saddr.sir_lsap_sel);
733 IRDA_DEBUG(1, __FUNCTION__ "(), addr = %08x\n", saddr.sir_addr);
734
735 /* uaddr_len come to us uninitialised */
736 *uaddr_len = sizeof (struct sockaddr_irda);
737 memcpy(uaddr, &saddr, *uaddr_len);
738
739 return 0;
740 }
741
742 /*
743 * Function irda_listen (sock, backlog)
744 *
745 * Just move to the listen state
746 *
747 */
748 static int irda_listen(struct socket *sock, int backlog)
749 {
750 struct sock *sk = sock->sk;
751
752 IRDA_DEBUG(2, __FUNCTION__ "()\n");
753
754 if ((sk->type != SOCK_STREAM) && (sk->type != SOCK_SEQPACKET) &&
755 (sk->type != SOCK_DGRAM))
756 return -EOPNOTSUPP;
757
758 if (sk->state != TCP_LISTEN) {
759 sk->max_ack_backlog = backlog;
760 sk->state = TCP_LISTEN;
761
762 return 0;
763 }
764
765 return -EOPNOTSUPP;
766 }
767
768 /*
769 * Function irda_bind (sock, uaddr, addr_len)
770 *
771 * Used by servers to register their well known TSAP
772 *
773 */
774 static int irda_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
775 {
776 struct sock *sk = sock->sk;
777 struct sockaddr_irda *addr = (struct sockaddr_irda *) uaddr;
778 struct irda_sock *self;
779 __u16 hints = 0;
780 int err;
781
782 IRDA_DEBUG(2, __FUNCTION__ "()\n");
783
784 self = sk->protinfo.irda;
785 ASSERT(self != NULL, return -1;);
786
787 if (addr_len != sizeof(struct sockaddr_irda))
788 return -EINVAL;
789
790 #ifdef CONFIG_IRDA_ULTRA
791 /* Special care for Ultra sockets */
792 if ((sk->type == SOCK_DGRAM) && (sk->protocol == IRDAPROTO_ULTRA)) {
793 self->pid = addr->sir_lsap_sel;
794 if (self->pid & 0x80) {
795 IRDA_DEBUG(0, __FUNCTION__
796 "(), extension in PID not supp!\n");
797 return -EOPNOTSUPP;
798 }
799 err = irda_open_lsap(self, self->pid);
800 if (err < 0)
801 return err;
802
803 self->max_data_size = ULTRA_MAX_DATA - LMP_PID_HEADER;
804 self->max_header_size = IRDA_MAX_HEADER + LMP_PID_HEADER;
805
806 /* Pretend we are connected */
807 sock->state = SS_CONNECTED;
808 sk->state = TCP_ESTABLISHED;
809
810 return 0;
811 }
812 #endif /* CONFIG_IRDA_ULTRA */
813
814 err = irda_open_tsap(self, addr->sir_lsap_sel, addr->sir_name);
815 if (err < 0)
816 return err;
817
818 /* Register with LM-IAS */
819 self->ias_obj = irias_new_object(addr->sir_name, jiffies);
820 irias_add_integer_attrib(self->ias_obj, "IrDA:TinyTP:LsapSel",
821 self->stsap_sel, IAS_KERNEL_ATTR);
822 irias_insert_object(self->ias_obj);
823
824 #if 1 /* Will be removed in near future */
825
826 /* Fill in some default hint bits values */
827 if (strncmp(addr->sir_name, "OBEX", 4) == 0)
828 hints = irlmp_service_to_hint(S_OBEX);
829
830 if (hints)
831 self->skey = irlmp_register_service(hints);
832 #endif
833 return 0;
834 }
835
836 /*
837 * Function irda_accept (sock, newsock, flags)
838 *
839 * Wait for incomming connection
840 *
841 */
842 static int irda_accept(struct socket *sock, struct socket *newsock, int flags)
843 {
844 struct irda_sock *self, *new;
845 struct sock *sk = sock->sk;
846 struct sock *newsk;
847 struct sk_buff *skb;
848 int err;
849
850 IRDA_DEBUG(2, __FUNCTION__ "()\n");
851
852 self = sk->protinfo.irda;
853 ASSERT(self != NULL, return -1;);
854
855 err = irda_create(newsock, sk->protocol);
856 if (err)
857 return err;
858
859 if (sock->state != SS_UNCONNECTED)
860 return -EINVAL;
861
862 if ((sk = sock->sk) == NULL)
863 return -EINVAL;
864
865 if ((sk->type != SOCK_STREAM) && (sk->type != SOCK_SEQPACKET) &&
866 (sk->type != SOCK_DGRAM))
867 return -EOPNOTSUPP;
868
869 if (sk->state != TCP_LISTEN)
870 return -EINVAL;
871
872 /*
873 * The read queue this time is holding sockets ready to use
874 * hooked into the SABM we saved
875 */
876 do {
877 if ((skb = skb_dequeue(&sk->receive_queue)) == NULL) {
878 if (flags & O_NONBLOCK)
879 return -EWOULDBLOCK;
880
881 interruptible_sleep_on(sk->sleep);
882 if (signal_pending(current))
883 return -ERESTARTSYS;
884 }
885 } while (skb == NULL);
886
887 newsk = newsock->sk;
888 newsk->state = TCP_ESTABLISHED;
889
890 new = newsk->protinfo.irda;
891 ASSERT(new != NULL, return -1;);
892
893 /* Now attach up the new socket */
894 new->tsap = irttp_dup(self->tsap, new);
895 if (!new->tsap) {
896 IRDA_DEBUG(0, __FUNCTION__ "(), dup failed!\n");
897 return -1;
898 }
899
900 new->stsap_sel = new->tsap->stsap_sel;
901 new->dtsap_sel = new->tsap->dtsap_sel;
902 new->saddr = irttp_get_saddr(new->tsap);
903 new->daddr = irttp_get_daddr(new->tsap);
904
905 new->max_sdu_size_tx = self->max_sdu_size_tx;
906 new->max_sdu_size_rx = self->max_sdu_size_rx;
907 new->max_data_size = self->max_data_size;
908 new->max_header_size = self->max_header_size;
909
910 memcpy(&new->qos_tx, &self->qos_tx, sizeof(struct qos_info));
911
912 /* Clean up the original one to keep it in listen state */
913 self->tsap->dtsap_sel = self->tsap->lsap->dlsap_sel = LSAP_ANY;
914 self->tsap->lsap->lsap_state = LSAP_DISCONNECTED;
915
916 skb->sk = NULL;
917 skb->destructor = NULL;
918 kfree_skb(skb);
919 sk->ack_backlog--;
920
921 newsock->state = SS_CONNECTED;
922
923 irda_connect_response(new);
924
925 return 0;
926 }
927
928 /*
929 * Function irda_connect (sock, uaddr, addr_len, flags)
930 *
931 * Connect to a IrDA device
932 *
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.
938 *
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)
947 */
948 static int irda_connect(struct socket *sock, struct sockaddr *uaddr,
949 int addr_len, int flags)
950 {
951 struct sock *sk = sock->sk;
952 struct sockaddr_irda *addr = (struct sockaddr_irda *) uaddr;
953 struct irda_sock *self;
954 int err;
955
956 IRDA_DEBUG(2, __FUNCTION__ "()\n");
957
958 self = sk->protinfo.irda;
959
960 /* Don't allow connect for Ultra sockets */
961 if ((sk->type == SOCK_DGRAM) && (sk->protocol == IRDAPROTO_ULTRA))
962 return -ESOCKTNOSUPPORT;
963
964 if (sk->state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
965 sock->state = SS_CONNECTED;
966 return 0; /* Connect completed during a ERESTARTSYS event */
967 }
968
969 if (sk->state == TCP_CLOSE && sock->state == SS_CONNECTING) {
970 sock->state = SS_UNCONNECTED;
971 return -ECONNREFUSED;
972 }
973
974 if (sk->state == TCP_ESTABLISHED)
975 return -EISCONN; /* No reconnect on a seqpacket socket */
976
977 sk->state = TCP_CLOSE;
978 sock->state = SS_UNCONNECTED;
979
980 if (addr_len != sizeof(struct sockaddr_irda))
981 return -EINVAL;
982
983 /* Check if user supplied any destination device address */
984 if ((!addr->sir_addr) || (addr->sir_addr == DEV_ADDR_ANY)) {
985 /* Try to find one suitable */
986 err = irda_discover_daddr_and_lsap_sel(self, addr->sir_name);
987 if (err) {
988 IRDA_DEBUG(0, __FUNCTION__
989 "(), auto-connect failed!\n");
990 return err;
991 }
992 } else {
993 /* Use the one provided by the user */
994 self->daddr = addr->sir_addr;
995 IRDA_DEBUG(1, __FUNCTION__ "(), daddr = %08x\n", self->daddr);
996
997 /* Query remote LM-IAS */
998 err = irda_find_lsap_sel(self, addr->sir_name);
999 if (err) {
1000 IRDA_DEBUG(0, __FUNCTION__ "(), connect failed!\n");
1001 return err;
1002 }
1003 }
1004
1005 /* Check if we have opened a local TSAP */
1006 if (!self->tsap)
1007 irda_open_tsap(self, LSAP_ANY, addr->sir_name);
1008
1009 /* Move to connecting socket, start sending Connect Requests */
1010 sock->state = SS_CONNECTING;
1011 sk->state = TCP_SYN_SENT;
1012
1013 /* Connect to remote device */
1014 err = irttp_connect_request(self->tsap, self->dtsap_sel,
1015 self->saddr, self->daddr, NULL,
1016 self->max_sdu_size_rx, NULL);
1017 if (err) {
1018 IRDA_DEBUG(0, __FUNCTION__ "(), connect failed!\n");
1019 return err;
1020 }
1021
1022 /* Now the loop */
1023 if (sk->state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
1024 return -EINPROGRESS;
1025
1026 cli(); /* To avoid races on the sleep */
1027
1028 /* A Connect Ack with Choke or timeout or failed routing will go to
1029 * closed. */
1030 while (sk->state == TCP_SYN_SENT) {
1031 interruptible_sleep_on(sk->sleep);
1032 if (signal_pending(current)) {
1033 sti();
1034 return -ERESTARTSYS;
1035 }
1036 }
1037
1038 if (sk->state != TCP_ESTABLISHED) {
1039 sti();
1040 sock->state = SS_UNCONNECTED;
1041 return sock_error(sk); /* Always set at this point */
1042 }
1043
1044 sock->state = SS_CONNECTED;
1045
1046 sti();
1047
1048 /* At this point, IrLMP has assigned our source address */
1049 self->saddr = irttp_get_saddr(self->tsap);
1050
1051 return 0;
1052 }
1053
1054 /*
1055 * Function irda_create (sock, protocol)
1056 *
1057 * Create IrDA socket
1058 *
1059 */
1060 static int irda_create(struct socket *sock, int protocol)
1061 {
1062 struct sock *sk;
1063 struct irda_sock *self;
1064
1065 IRDA_DEBUG(2, __FUNCTION__ "()\n");
1066
1067 /* Check for valid socket type */
1068 switch (sock->type) {
1069 case SOCK_STREAM: /* For TTP connections with SAR disabled */
1070 case SOCK_SEQPACKET: /* For TTP connections with SAR enabled */
1071 case SOCK_DGRAM: /* For TTP Unitdata or LMP Ultra transfers */
1072 break;
1073 default:
1074 return -ESOCKTNOSUPPORT;
1075 }
1076
1077 /* Allocate socket */
1078 if ((sk = sk_alloc(PF_IRDA, GFP_ATOMIC, 1)) == NULL)
1079 return -ENOMEM;
1080
1081 self = kmalloc(sizeof(struct irda_sock), GFP_ATOMIC);
1082 if (self == NULL)
1083 return -ENOMEM;
1084 memset(self, 0, sizeof(struct irda_sock));
1085
1086 init_waitqueue_head(&self->query_wait);
1087
1088 self->sk = sk;
1089 sk->protinfo.irda = self;
1090 sock_init_data(sock, sk);
1091
1092 switch (sock->type) {
1093 case SOCK_STREAM:
1094 sock->ops = &irda_stream_ops;
1095 self->max_sdu_size_rx = TTP_SAR_DISABLE;
1096 break;
1097 case SOCK_SEQPACKET:
1098 sock->ops = &irda_seqpacket_ops;
1099 self->max_sdu_size_rx = TTP_SAR_UNBOUND;
1100 break;
1101 case SOCK_DGRAM:
1102 switch (protocol) {
1103 #ifdef CONFIG_IRDA_ULTRA
1104 case IRDAPROTO_ULTRA:
1105 sock->ops = &irda_ultra_ops;
1106 break;
1107 #endif /* CONFIG_IRDA_ULTRA */
1108 case IRDAPROTO_UNITDATA:
1109 sock->ops = &irda_dgram_ops;
1110 /* We let Unitdata conn. be like seqpack conn. */
1111 self->max_sdu_size_rx = TTP_SAR_UNBOUND;
1112 break;
1113 default:
1114 ERROR(__FUNCTION__ "(), protocol not supported!\n");
1115 return -ESOCKTNOSUPPORT;
1116 }
1117 break;
1118 default:
1119 return -ESOCKTNOSUPPORT;
1120 }
1121
1122 sk->protocol = protocol;
1123
1124 /* Register as a client with IrLMP */
1125 self->ckey = irlmp_register_client(0, NULL, NULL, NULL);
1126 self->mask = 0xffff;
1127 self->rx_flow = self->tx_flow = FLOW_START;
1128 self->nslots = DISCOVERY_DEFAULT_SLOTS;
1129 self->daddr = DEV_ADDR_ANY; /* Until we get connected */
1130 self->saddr = 0x0; /* so IrLMP assign us any link */
1131
1132 MOD_INC_USE_COUNT;
1133
1134 return 0;
1135 }
1136
1137 /*
1138 * Function irda_destroy_socket (self)
1139 *
1140 * Destroy socket
1141 *
1142 */
1143 void irda_destroy_socket(struct irda_sock *self)
1144 {
1145 IRDA_DEBUG(2, __FUNCTION__ "()\n");
1146
1147 ASSERT(self != NULL, return;);
1148
1149 /* Unregister with IrLMP */
1150 irlmp_unregister_client(self->ckey);
1151 irlmp_unregister_service(self->skey);
1152
1153 /* Unregister with LM-IAS */
1154 if (self->ias_obj) {
1155 irias_delete_object(self->ias_obj);
1156 self->ias_obj = NULL;
1157 }
1158
1159 if (self->iriap) {
1160 iriap_close(self->iriap);
1161 self->iriap = NULL;
1162 }
1163
1164 if (self->tsap) {
1165 irttp_disconnect_request(self->tsap, NULL, P_NORMAL);
1166 irttp_close_tsap(self->tsap);
1167 self->tsap = NULL;
1168 }
1169 #ifdef CONFIG_IRDA_ULTRA
1170 if (self->lsap) {
1171 irlmp_close_lsap(self->lsap);
1172 self->lsap = NULL;
1173 }
1174 #endif /* CONFIG_IRDA_ULTRA */
1175 kfree(self);
1176 MOD_DEC_USE_COUNT;
1177
1178 return;
1179 }
1180
1181 /*
1182 * Function irda_release (sock)
1183 *
1184 *
1185 *
1186 */
1187 static int irda_release(struct socket *sock)
1188 {
1189 struct sock *sk = sock->sk;
1190
1191 IRDA_DEBUG(2, __FUNCTION__ "()\n");
1192
1193 if (sk == NULL)
1194 return 0;
1195
1196 sk->state = TCP_CLOSE;
1197 sk->shutdown |= SEND_SHUTDOWN;
1198 sk->state_change(sk);
1199 sk->dead = 1;
1200
1201 irda_destroy_socket(sk->protinfo.irda);
1202
1203 sock->sk = NULL;
1204 sk->socket = NULL; /* Not used, but we should do this. */
1205
1206 return 0;
1207 }
1208
1209 /*
1210 * Function irda_sendmsg (sock, msg, len, scm)
1211 *
1212 * Send message down to TinyTP. This function is used for both STREAM and
1213 * SEQPACK services. This is possible since it forces the client to
1214 * fragment the message if necessary
1215 */
1216 static int irda_sendmsg(struct socket *sock, struct msghdr *msg, int len,
1217 struct scm_cookie *scm)
1218 {
1219 struct sock *sk = sock->sk;
1220 struct irda_sock *self;
1221 struct sk_buff *skb;
1222 unsigned char *asmptr;
1223 int err;
1224
1225 IRDA_DEBUG(4, __FUNCTION__ "(), len=%d\n", len);
1226
1227 /* Note : socket.c set MSG_EOR on SEQPACKET sockets */
1228 if (msg->msg_flags & ~(MSG_DONTWAIT | MSG_EOR))
1229 return -EINVAL;
1230
1231 if (sk->shutdown & SEND_SHUTDOWN) {
1232 send_sig(SIGPIPE, current, 0);
1233 return -EPIPE;
1234 }
1235
1236 if (sk->state != TCP_ESTABLISHED)
1237 return -ENOTCONN;
1238
1239 self = sk->protinfo.irda;
1240 ASSERT(self != NULL, return -1;);
1241
1242 /* Check if IrTTP is wants us to slow down */
1243 while (self->tx_flow == FLOW_STOP) {
1244 IRDA_DEBUG(2, __FUNCTION__ "(), IrTTP is busy, going to sleep!\n");
1245 interruptible_sleep_on(sk->sleep);
1246
1247 /* Check if we are still connected */
1248 if (sk->state != TCP_ESTABLISHED)
1249 return -ENOTCONN;
1250 }
1251
1252 /* Check that we don't send out to big frames */
1253 if (len > self->max_data_size) {
1254 IRDA_DEBUG(2, __FUNCTION__
1255 "(), Chopping frame from %d to %d bytes!\n", len,
1256 self->max_data_size);
1257 len = self->max_data_size;
1258 }
1259
1260 skb = sock_alloc_send_skb(sk, len + self->max_header_size, 0,
1261 msg->msg_flags & MSG_DONTWAIT, &err);
1262 if (!skb)
1263 return -ENOBUFS;
1264
1265 skb_reserve(skb, self->max_header_size);
1266
1267 asmptr = skb->h.raw = skb_put(skb, len);
1268 memcpy_fromiovec(asmptr, msg->msg_iov, len);
1269
1270 /*
1271 * Just send the message to TinyTP, and let it deal with possible
1272 * errors. No need to duplicate all that here
1273 */
1274 err = irttp_data_request(self->tsap, skb);
1275 if (err) {
1276 IRDA_DEBUG(0, __FUNCTION__ "(), err=%d\n", err);
1277 return err;
1278 }
1279 /* Tell client how much data we actually sent */
1280 return len;
1281 }
1282
1283 /*
1284 * Function irda_recvmsg_dgram (sock, msg, size, flags, scm)
1285 *
1286 * Try to receive message and copy it to user. The frame is discarded
1287 * after being read, regardless of how much the user actually read
1288 */
1289 static int irda_recvmsg_dgram(struct socket *sock, struct msghdr *msg,
1290 int size, int flags, struct scm_cookie *scm)
1291 {
1292 struct irda_sock *self;
1293 struct sock *sk = sock->sk;
1294 struct sk_buff *skb;
1295 int copied, err;
1296
1297 IRDA_DEBUG(4, __FUNCTION__ "()\n");
1298
1299 self = sk->protinfo.irda;
1300 ASSERT(self != NULL, return -1;);
1301
1302 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1303 flags & MSG_DONTWAIT, &err);
1304 if (!skb)
1305 return err;
1306
1307 skb->h.raw = skb->data;
1308 copied = skb->len;
1309
1310 if (copied > size) {
1311 IRDA_DEBUG(2, __FUNCTION__
1312 "(), Received truncated frame (%d < %d)!\n",
1313 copied, size);
1314 copied = size;
1315 msg->msg_flags |= MSG_TRUNC;
1316 }
1317 skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1318
1319 skb_free_datagram(sk, skb);
1320
1321 /*
1322 * Check if we have previously stopped IrTTP and we know
1323 * have more free space in our rx_queue. If so tell IrTTP
1324 * to start delivering frames again before our rx_queue gets
1325 * empty
1326 */
1327 if (self->rx_flow == FLOW_STOP) {
1328 if ((atomic_read(&sk->rmem_alloc) << 2) <= sk->rcvbuf) {
1329 IRDA_DEBUG(2, __FUNCTION__ "(), Starting IrTTP\n");
1330 self->rx_flow = FLOW_START;
1331 irttp_flow_request(self->tsap, FLOW_START);
1332 }
1333 }
1334
1335 return copied;
1336 }
1337
1338 /*
1339 * Function irda_data_wait (sk)
1340 *
1341 * Sleep until data has arrive. But check for races..
1342 *
1343 */
1344 static void irda_data_wait(struct sock *sk)
1345 {
1346 if (!skb_peek(&sk->receive_queue)) {
1347 set_bit(SOCK_ASYNC_WAITDATA, &sk->socket->flags);
1348 interruptible_sleep_on(sk->sleep);
1349 clear_bit(SOCK_ASYNC_WAITDATA, &sk->socket->flags);
1350 }
1351 }
1352
1353 /*
1354 * Function irda_recvmsg_stream (sock, msg, size, flags, scm)
1355 *
1356 *
1357 *
1358 */
1359 static int irda_recvmsg_stream(struct socket *sock, struct msghdr *msg,
1360 int size, int flags, struct scm_cookie *scm)
1361 {
1362 struct irda_sock *self;
1363 struct sock *sk = sock->sk;
1364 int noblock = flags & MSG_DONTWAIT;
1365 int copied = 0;
1366 int target = 1;
1367
1368 IRDA_DEBUG(3, __FUNCTION__ "()\n");
1369
1370 self = sk->protinfo.irda;
1371 ASSERT(self != NULL, return -1;);
1372
1373 if (sock->flags & __SO_ACCEPTCON)
1374 return(-EINVAL);
1375
1376 if (flags & MSG_OOB)
1377 return -EOPNOTSUPP;
1378
1379 if (flags & MSG_WAITALL)
1380 target = size;
1381
1382 msg->msg_namelen = 0;
1383
1384 do {
1385 int chunk;
1386 struct sk_buff *skb;
1387
1388 skb=skb_dequeue(&sk->receive_queue);
1389 if (skb==NULL) {
1390 if (copied >= target)
1391 break;
1392
1393 /*
1394 * POSIX 1003.1g mandates this order.
1395 */
1396
1397 if (sk->err) {
1398 return sock_error(sk);
1399 }
1400
1401 if (sk->shutdown & RCV_SHUTDOWN)
1402 break;
1403
1404 if (noblock)
1405 return -EAGAIN;
1406 irda_data_wait(sk);
1407 if (signal_pending(current))
1408 return -ERESTARTSYS;
1409 continue;
1410 }
1411
1412 chunk = min(skb->len, size);
1413 if (memcpy_toiovec(msg->msg_iov, skb->data, chunk)) {
1414 skb_queue_head(&sk->receive_queue, skb);
1415 if (copied == 0)
1416 copied = -EFAULT;
1417 break;
1418 }
1419 copied += chunk;
1420 size -= chunk;
1421
1422 /* Mark read part of skb as used */
1423 if (!(flags & MSG_PEEK)) {
1424 skb_pull(skb, chunk);
1425
1426 /* put the skb back if we didn't use it up.. */
1427 if (skb->len) {
1428 IRDA_DEBUG(1, __FUNCTION__ "(), back on q!\n");
1429 skb_queue_head(&sk->receive_queue, skb);
1430 break;
1431 }
1432
1433 kfree_skb(skb);
1434 } else {
1435 IRDA_DEBUG(0, __FUNCTION__ "() questionable!?\n");
1436
1437 /* put message back and return */
1438 skb_queue_head(&sk->receive_queue, skb);
1439 break;
1440 }
1441 } while (size);
1442
1443 /*
1444 * Check if we have previously stopped IrTTP and we know
1445 * have more free space in our rx_queue. If so tell IrTTP
1446 * to start delivering frames again before our rx_queue gets
1447 * empty
1448 */
1449 if (self->rx_flow == FLOW_STOP) {
1450 if ((atomic_read(&sk->rmem_alloc) << 2) <= sk->rcvbuf) {
1451 IRDA_DEBUG(2, __FUNCTION__ "(), Starting IrTTP\n");
1452 self->rx_flow = FLOW_START;
1453 irttp_flow_request(self->tsap, FLOW_START);
1454 }
1455 }
1456
1457 return copied;
1458 }
1459
1460 /*
1461 * Function irda_sendmsg_dgram (sock, msg, len, scm)
1462 *
1463 * Send message down to TinyTP for the unreliable sequenced
1464 * packet service...
1465 *
1466 */
1467 static int irda_sendmsg_dgram(struct socket *sock, struct msghdr *msg,
1468 int len, struct scm_cookie *scm)
1469 {
1470 struct sock *sk = sock->sk;
1471 struct irda_sock *self;
1472 struct sk_buff *skb;
1473 unsigned char *asmptr;
1474 int err;
1475
1476 IRDA_DEBUG(4, __FUNCTION__ "(), len=%d\n", len);
1477
1478 if (msg->msg_flags & ~MSG_DONTWAIT)
1479 return -EINVAL;
1480
1481 if (sk->shutdown & SEND_SHUTDOWN) {
1482 send_sig(SIGPIPE, current, 0);
1483 return -EPIPE;
1484 }
1485
1486 if (sk->state != TCP_ESTABLISHED)
1487 return -ENOTCONN;
1488
1489 self = sk->protinfo.irda;
1490 ASSERT(self != NULL, return -1;);
1491
1492 /*
1493 * Check that we don't send out to big frames. This is an unreliable
1494 * service, so we have no fragmentation and no coalescence
1495 */
1496 if (len > self->max_data_size) {
1497 IRDA_DEBUG(0, __FUNCTION__ "(), Warning to much data! "
1498 "Chopping frame from %d to %d bytes!\n", len,
1499 self->max_data_size);
1500 len = self->max_data_size;
1501 }
1502
1503 skb = sock_alloc_send_skb(sk, len + self->max_header_size, 0,
1504 msg->msg_flags & MSG_DONTWAIT, &err);
1505 if (!skb)
1506 return -ENOBUFS;
1507
1508 skb_reserve(skb, self->max_header_size);
1509
1510 IRDA_DEBUG(4, __FUNCTION__ "(), appending user data\n");
1511 asmptr = skb->h.raw = skb_put(skb, len);
1512 memcpy_fromiovec(asmptr, msg->msg_iov, len);
1513
1514 /*
1515 * Just send the message to TinyTP, and let it deal with possible
1516 * errors. No need to duplicate all that here
1517 */
1518 err = irttp_udata_request(self->tsap, skb);
1519 if (err) {
1520 IRDA_DEBUG(0, __FUNCTION__ "(), err=%d\n", err);
1521 return err;
1522 }
1523 return len;
1524 }
1525
1526 /*
1527 * Function irda_sendmsg_ultra (sock, msg, len, scm)
1528 *
1529 * Send message down to IrLMP for the unreliable Ultra
1530 * packet service...
1531 */
1532 #ifdef CONFIG_IRDA_ULTRA
1533 static int irda_sendmsg_ultra(struct socket *sock, struct msghdr *msg,
1534 int len, struct scm_cookie *scm)
1535 {
1536 struct sock *sk = sock->sk;
1537 struct irda_sock *self;
1538 struct sk_buff *skb;
1539 unsigned char *asmptr;
1540 int err;
1541
1542 IRDA_DEBUG(4, __FUNCTION__ "(), len=%d\n", len);
1543
1544 if (msg->msg_flags & ~MSG_DONTWAIT)
1545 return -EINVAL;
1546
1547 if (sk->shutdown & SEND_SHUTDOWN) {
1548 send_sig(SIGPIPE, current, 0);
1549 return -EPIPE;
1550 }
1551
1552 self = sk->protinfo.irda;
1553 ASSERT(self != NULL, return -1;);
1554
1555 /*
1556 * Check that we don't send out to big frames. This is an unreliable
1557 * service, so we have no fragmentation and no coalescence
1558 */
1559 if (len > self->max_data_size) {
1560 IRDA_DEBUG(0, __FUNCTION__ "(), Warning to much data! "
1561 "Chopping frame from %d to %d bytes!\n", len,
1562 self->max_data_size);
1563 len = self->max_data_size;
1564 }
1565
1566 skb = sock_alloc_send_skb(sk, len + self->max_header_size, 0,
1567 msg->msg_flags & MSG_DONTWAIT, &err);
1568 if (!skb)
1569 return -ENOBUFS;
1570
1571 skb_reserve(skb, self->max_header_size);
1572
1573 IRDA_DEBUG(4, __FUNCTION__ "(), appending user data\n");
1574 asmptr = skb->h.raw = skb_put(skb, len);
1575 memcpy_fromiovec(asmptr, msg->msg_iov, len);
1576
1577 err = irlmp_connless_data_request(self->lsap, skb);
1578 if (err) {
1579 IRDA_DEBUG(0, __FUNCTION__ "(), err=%d\n", err);
1580 return err;
1581 }
1582 return len;
1583 }
1584 #endif /* CONFIG_IRDA_ULTRA */
1585
1586 /*
1587 * Function irda_shutdown (sk, how)
1588 *
1589 *
1590 *
1591 */
1592 static int irda_shutdown(struct socket *sock, int how)
1593 {
1594 struct irda_sock *self;
1595 struct sock *sk = sock->sk;
1596
1597 IRDA_DEBUG(0, __FUNCTION__ "()\n");
1598
1599 self = sk->protinfo.irda;
1600 ASSERT(self != NULL, return -1;);
1601
1602 sk->state = TCP_CLOSE;
1603 sk->shutdown |= SEND_SHUTDOWN;
1604 sk->state_change(sk);
1605
1606 if (self->iriap) {
1607 iriap_close(self->iriap);
1608 self->iriap = NULL;
1609 }
1610
1611 if (self->tsap) {
1612 irttp_disconnect_request(self->tsap, NULL, P_NORMAL);
1613 irttp_close_tsap(self->tsap);
1614 self->tsap = NULL;
1615 }
1616
1617 /* A few cleanup so the socket look as good as new... */
1618 self->rx_flow = self->tx_flow = FLOW_START; /* needed ??? */
1619 self->daddr = DEV_ADDR_ANY; /* Until we get re-connected */
1620 self->saddr = 0x0; /* so IrLMP assign us any link */
1621
1622 return 0;
1623 }
1624
1625 /*
1626 * Function irda_poll (file, sock, wait)
1627 *
1628 *
1629 *
1630 */
1631 static unsigned int irda_poll(struct file * file, struct socket *sock,
1632 poll_table *wait)
1633 {
1634 struct sock *sk = sock->sk;
1635 unsigned int mask;
1636
1637 IRDA_DEBUG(4, __FUNCTION__ "()\n");
1638
1639 poll_wait(file, sk->sleep, wait);
1640 mask = 0;
1641
1642 /* exceptional events? */
1643 if (sk->err)
1644 mask |= POLLERR;
1645 if (sk->shutdown & RCV_SHUTDOWN)
1646 mask |= POLLHUP;
1647
1648 /* readable? */
1649 if (!skb_queue_empty(&sk->receive_queue)) {
1650 IRDA_DEBUG(4, "Socket is readable\n");
1651 mask |= POLLIN | POLLRDNORM;
1652 }
1653 /* Connection-based need to check for termination and startup */
1654 if (sk->type == SOCK_STREAM && sk->state==TCP_CLOSE)
1655 mask |= POLLHUP;
1656
1657 /*
1658 * we set writable also when the other side has shut down the
1659 * connection. This prevents stuck sockets.
1660 */
1661 if (sk->sndbuf - (int)atomic_read(&sk->wmem_alloc) >= SOCK_MIN_WRITE_SPACE)
1662 mask |= POLLOUT | POLLWRNORM | POLLWRBAND;
1663
1664 return mask;
1665 }
1666
1667 /*
1668 * Function irda_ioctl (sock, cmd, arg)
1669 *
1670 *
1671 *
1672 */
1673 static int irda_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1674 {
1675 struct sock *sk = sock->sk;
1676
1677 IRDA_DEBUG(4, __FUNCTION__ "(), cmd=%#x\n", cmd);
1678
1679 switch (cmd) {
1680 case TIOCOUTQ: {
1681 long amount;
1682 amount = sk->sndbuf - atomic_read(&sk->wmem_alloc);
1683 if (amount < 0)
1684 amount = 0;
1685 if (put_user(amount, (unsigned int *)arg))
1686 return -EFAULT;
1687 return 0;
1688 }
1689
1690 case TIOCINQ: {
1691 struct sk_buff *skb;
1692 long amount = 0L;
1693 /* These two are safe on a single CPU system as only user tasks fiddle here */
1694 if ((skb = skb_peek(&sk->receive_queue)) != NULL)
1695 amount = skb->len;
1696 if (put_user(amount, (unsigned int *)arg))
1697 return -EFAULT;
1698 return 0;
1699 }
1700
1701 case SIOCGSTAMP:
1702 if (sk != NULL) {
1703 if (sk->stamp.tv_sec == 0)
1704 return -ENOENT;
1705 if (copy_to_user((void *)arg, &sk->stamp,
1706 sizeof(struct timeval)))
1707 return -EFAULT;
1708 return 0;
1709 }
1710 return -EINVAL;
1711
1712 case SIOCGIFADDR:
1713 case SIOCSIFADDR:
1714 case SIOCGIFDSTADDR:
1715 case SIOCSIFDSTADDR:
1716 case SIOCGIFBRDADDR:
1717 case SIOCSIFBRDADDR:
1718 case SIOCGIFNETMASK:
1719 case SIOCSIFNETMASK:
1720 case SIOCGIFMETRIC:
1721 case SIOCSIFMETRIC:
1722 return -EINVAL;
1723 default:
1724 IRDA_DEBUG(1, __FUNCTION__ "(), doing device ioctl!\n");
1725 return dev_ioctl(cmd, (void *) arg);
1726 }
1727
1728 /*NOTREACHED*/
1729 return 0;
1730 }
1731
1732 /*
1733 * Function irda_setsockopt (sock, level, optname, optval, optlen)
1734 *
1735 * Set some options for the socket
1736 *
1737 */
1738 static int irda_setsockopt(struct socket *sock, int level, int optname,
1739 char *optval, int optlen)
1740 {
1741 struct sock *sk = sock->sk;
1742 struct irda_sock *self;
1743 struct irda_ias_set ias_opt;
1744 struct ias_object *ias_obj;
1745 struct ias_attrib * ias_attr; /* Attribute in IAS object */
1746 int opt;
1747
1748 self = sk->protinfo.irda;
1749 ASSERT(self != NULL, return -1;);
1750
1751 if (level != SOL_IRLMP)
1752 return -ENOPROTOOPT;
1753
1754 switch (optname) {
1755 case IRLMP_IAS_SET:
1756 /* The user want to add an attribute to an existing IAS object
1757 * (in the IAS database) or to create a new object with this
1758 * attribute.
1759 * We first query IAS to know if the object exist, and then
1760 * create the right attribute...
1761 */
1762
1763 if (optlen != sizeof(struct irda_ias_set))
1764 return -EINVAL;
1765
1766 /* Copy query to the driver. */
1767 if (copy_from_user(&ias_opt, (char *)optval, optlen))
1768 return -EFAULT;
1769
1770 /* Find the object we target */
1771 ias_obj = irias_find_object(ias_opt.irda_class_name);
1772 if(ias_obj == (struct ias_object *) NULL) {
1773 /* Create a new object */
1774 ias_obj = irias_new_object(ias_opt.irda_class_name,
1775 jiffies);
1776 }
1777
1778 /* Do we have it already ? */
1779 if(irias_find_attrib(ias_obj, ias_opt.irda_attrib_name))
1780 return -EINVAL;
1781
1782 /* Look at the type */
1783 switch(ias_opt.irda_attrib_type) {
1784 case IAS_INTEGER:
1785 /* Add an integer attribute */
1786 irias_add_integer_attrib(
1787 ias_obj,
1788 ias_opt.irda_attrib_name,
1789 ias_opt.attribute.irda_attrib_int,
1790 IAS_USER_ATTR);
1791 break;
1792 case IAS_OCT_SEQ:
1793 /* Check length */
1794 if(ias_opt.attribute.irda_attrib_octet_seq.len >
1795 IAS_MAX_OCTET_STRING)
1796 return -EINVAL;
1797 /* Add an octet sequence attribute */
1798 irias_add_octseq_attrib(
1799 ias_obj,
1800 ias_opt.irda_attrib_name,
1801 ias_opt.attribute.irda_attrib_octet_seq.octet_seq,
1802 ias_opt.attribute.irda_attrib_octet_seq.len,
1803 IAS_USER_ATTR);
1804 break;
1805 case IAS_STRING:
1806 /* Should check charset & co */
1807 /* Check length */
1808 if(ias_opt.attribute.irda_attrib_string.len >
1809 IAS_MAX_STRING)
1810 return -EINVAL;
1811 /* NULL terminate the string (avoid troubles) */
1812 ias_opt.attribute.irda_attrib_string.string[ias_opt.attribute.irda_attrib_string.len] = '\0';
1813 /* Add a string attribute */
1814 irias_add_string_attrib(
1815 ias_obj,
1816 ias_opt.irda_attrib_name,
1817 ias_opt.attribute.irda_attrib_string.string,
1818 IAS_USER_ATTR);
1819 break;
1820 default :
1821 return -EINVAL;
1822 }
1823 irias_insert_object(ias_obj);
1824 break;
1825 case IRLMP_IAS_DEL:
1826 /* The user want to delete an object from our local IAS
1827 * database. We just need to query the IAS, check is the
1828 * object is not owned by the kernel and delete it.
1829 */
1830
1831 if (optlen != sizeof(struct irda_ias_set))
1832 return -EINVAL;
1833
1834 /* Copy query to the driver. */
1835 if (copy_from_user(&ias_opt, (char *)optval, optlen))
1836 return -EFAULT;
1837
1838 /* Find the object we target */
1839 ias_obj = irias_find_object(ias_opt.irda_class_name);
1840 if(ias_obj == (struct ias_object *) NULL)
1841 return -EINVAL;
1842
1843 /* Find the attribute (in the object) we target */
1844 ias_attr = irias_find_attrib(ias_obj,
1845 ias_opt.irda_attrib_name);
1846 if(ias_attr == (struct ias_attrib *) NULL)
1847 return -EINVAL;
1848
1849 /* Check is the user space own the object */
1850 if(ias_attr->value->owner != IAS_USER_ATTR) {
1851 IRDA_DEBUG(1, __FUNCTION__
1852 "(), attempting to delete a kernel attribute\n");
1853 return -EPERM;
1854 }
1855
1856 /* Remove the attribute (and maybe the object) */
1857 irias_delete_attrib(ias_obj, ias_attr);
1858
1859 break;
1860 case IRLMP_MAX_SDU_SIZE:
1861 if (optlen < sizeof(int))
1862 return -EINVAL;
1863
1864 if (get_user(opt, (int *)optval))
1865 return -EFAULT;
1866
1867 /* Only possible for a seqpacket service (TTP with SAR) */
1868 if (sk->type != SOCK_SEQPACKET) {
1869 IRDA_DEBUG(2, __FUNCTION__
1870 "(), setting max_sdu_size = %d\n", opt);
1871 self->max_sdu_size_rx = opt;
1872 } else {
1873 WARNING(__FUNCTION__
1874 "(), not allowed to set MAXSDUSIZE for this "
1875 "socket type!\n");
1876 return -ENOPROTOOPT;
1877 }
1878 break;
1879 case IRLMP_HINTS_SET:
1880 if (optlen < sizeof(int))
1881 return -EINVAL;
1882
1883 if (get_user(opt, (int *)optval))
1884 return -EFAULT;
1885
1886 /* Unregister any old registration */
1887 if (self->skey)
1888 irlmp_unregister_service(self->skey);
1889
1890 self->skey = irlmp_register_service((__u16) opt);
1891 break;
1892 case IRLMP_HINT_MASK_SET:
1893 /* As opposed to the previous case which set the hint bits
1894 * that we advertise, this one set the filter we use when
1895 * making a discovery (nodes which don't match any hint
1896 * bit in the mask are not reported).
1897 */
1898 if (optlen < sizeof(int))
1899 return -EINVAL;
1900
1901 if (get_user(opt, (int *)optval))
1902 return -EFAULT;
1903
1904 /* Set the new hint mask */
1905 self->mask = (__u16) opt;
1906 /* Mask out extension bits */
1907 self->mask &= 0x7f7f;
1908 /* Check if no bits */
1909 if(!self->mask)
1910 self->mask = 0xFFFF;
1911
1912 break;
1913 default:
1914 return -ENOPROTOOPT;
1915 }
1916 return 0;
1917 }
1918
1919 /*
1920 * Function irda_extract_ias_value(ias_opt, ias_value)
1921 *
1922 * Translate internal IAS value structure to the user space representation
1923 *
1924 * The external representation of IAS values, as we exchange them with
1925 * user space program is quite different from the internal representation,
1926 * as stored in the IAS database (because we need a flat structure for
1927 * crossing kernel boundary).
1928 * This function transform the former in the latter. We also check
1929 * that the value type is valid.
1930 */
1931 static int irda_extract_ias_value(struct irda_ias_set *ias_opt,
1932 struct ias_value *ias_value)
1933 {
1934 /* Look at the type */
1935 switch (ias_value->type) {
1936 case IAS_INTEGER:
1937 /* Copy the integer */
1938 ias_opt->attribute.irda_attrib_int = ias_value->t.integer;
1939 break;
1940 case IAS_OCT_SEQ:
1941 /* Set length */
1942 ias_opt->attribute.irda_attrib_octet_seq.len = ias_value->len;
1943 /* Copy over */
1944 memcpy(ias_opt->attribute.irda_attrib_octet_seq.octet_seq,
1945 ias_value->t.oct_seq, ias_value->len);
1946 break;
1947 case IAS_STRING:
1948 /* Set length */
1949 ias_opt->attribute.irda_attrib_string.len = ias_value->len;
1950 ias_opt->attribute.irda_attrib_string.charset = ias_value->charset;
1951 /* Copy over */
1952 memcpy(ias_opt->attribute.irda_attrib_string.string,
1953 ias_value->t.string, ias_value->len);
1954 /* NULL terminate the string (avoid troubles) */
1955 ias_opt->attribute.irda_attrib_string.string[ias_value->len] = '\0';
1956 break;
1957 case IAS_MISSING:
1958 default :
1959 return -EINVAL;
1960 }
1961
1962 /* Copy type over */
1963 ias_opt->irda_attrib_type = ias_value->type;
1964
1965 return 0;
1966 }
1967
1968 /*
1969 * Function irda_getsockopt (sock, level, optname, optval, optlen)
1970 *
1971 *
1972 *
1973 */
1974 static int irda_getsockopt(struct socket *sock, int level, int optname,
1975 char *optval, int *optlen)
1976 {
1977 struct sock *sk = sock->sk;
1978 struct irda_sock *self;
1979 struct irda_device_list list;
1980 struct irda_device_info *discoveries;
1981 struct irda_ias_set ias_opt; /* IAS get/query params */
1982 struct ias_object * ias_obj; /* Object in IAS */
1983 struct ias_attrib * ias_attr; /* Attribute in IAS object */
1984 int daddr = DEV_ADDR_ANY; /* Dest address for IAS queries */
1985 int val = 0;
1986 int len = 0;
1987 int err;
1988 int offset, total;
1989
1990 self = sk->protinfo.irda;
1991
1992 if (level != SOL_IRLMP)
1993 return -ENOPROTOOPT;
1994
1995 if (get_user(len, optlen))
1996 return -EFAULT;
1997
1998 switch (optname) {
1999 case IRLMP_ENUMDEVICES:
2000 /* Ask lmp for the current discovery log */
2001 discoveries = irlmp_get_discoveries(&list.len, self->mask);
2002 /* Check if the we got some results */
2003 if (discoveries == NULL)
2004 return -EAGAIN; /* Didn't find any devices */
2005 err = 0;
2006
2007 /* Write total list length back to client */
2008 if (copy_to_user(optval, &list,
2009 sizeof(struct irda_device_list) -
2010 sizeof(struct irda_device_info)))
2011 err = -EFAULT;
2012
2013 /* Offset to first device entry */
2014 offset = sizeof(struct irda_device_list) -
2015 sizeof(struct irda_device_info);
2016
2017 /* Copy the list itself */
2018 total = offset + (list.len * sizeof(struct irda_device_info));
2019 if (total > len)
2020 total = len;
2021 if (copy_to_user(optval+offset, discoveries, total - offset))
2022 err = -EFAULT;
2023
2024 /* Write total number of bytes used back to client */
2025 if (put_user(total, optlen))
2026 err = -EFAULT;
2027
2028 /* Free up our buffer */
2029 kfree(discoveries);
2030 if (err)
2031 return err;
2032 break;
2033 case IRLMP_MAX_SDU_SIZE:
2034 val = self->max_data_size;
2035 len = sizeof(int);
2036 if (put_user(len, optlen))
2037 return -EFAULT;
2038
2039 if (copy_to_user(optval, &val, len))
2040 return -EFAULT;
2041 break;
2042 case IRLMP_IAS_GET:
2043 /* The user want an object from our local IAS database.
2044 * We just need to query the IAS and return the value
2045 * that we found */
2046
2047 /* Check that the user has allocated the right space for us */
2048 if (len != sizeof(ias_opt))
2049 return -EINVAL;
2050
2051 /* Copy query to the driver. */
2052 if (copy_from_user((char *) &ias_opt, (char *)optval, len))
2053 return -EFAULT;
2054
2055 /* Find the object we target */
2056 ias_obj = irias_find_object(ias_opt.irda_class_name);
2057 if(ias_obj == (struct ias_object *) NULL)
2058 return -EINVAL;
2059
2060 /* Find the attribute (in the object) we target */
2061 ias_attr = irias_find_attrib(ias_obj,
2062 ias_opt.irda_attrib_name);
2063 if(ias_attr == (struct ias_attrib *) NULL)
2064 return -EINVAL;
2065
2066 /* Translate from internal to user structure */
2067 err = irda_extract_ias_value(&ias_opt, ias_attr->value);
2068 if(err)
2069 return err;
2070
2071 /* Copy reply to the user */
2072 if (copy_to_user((char *)optval, (char *) &ias_opt,
2073 sizeof(ias_opt)))
2074 return -EFAULT;
2075 /* Note : don't need to put optlen, we checked it */
2076 break;
2077 case IRLMP_IAS_QUERY:
2078 /* The user want an object from a remote IAS database.
2079 * We need to use IAP to query the remote database and
2080 * then wait for the answer to come back. */
2081
2082 /* Check that the user has allocated the right space for us */
2083 if (len != sizeof(ias_opt))
2084 return -EINVAL;
2085
2086 /* Copy query to the driver. */
2087 if (copy_from_user((char *) &ias_opt, (char *)optval, len))
2088 return -EFAULT;
2089
2090 /* At this point, there are two cases...
2091 * 1) the socket is connected - that's the easy case, we
2092 * just query the device we are connected to...
2093 * 2) the socket is not connected - the user doesn't want
2094 * to connect and/or may not have a valid service name
2095 * (so can't create a fake connection). In this case,
2096 * we assume that the user pass us a valid destination
2097 * address in the requesting structure...
2098 */
2099 if(self->daddr != DEV_ADDR_ANY) {
2100 /* We are connected - reuse known daddr */
2101 daddr = self->daddr;
2102 } else {
2103 /* We are not connected, we must specify a valid
2104 * destination address */
2105 daddr = ias_opt.daddr;
2106 if((!daddr) || (daddr == DEV_ADDR_ANY))
2107 return -EINVAL;
2108 }
2109
2110 /* Check that we can proceed with IAP */
2111 if (self->iriap) {
2112 WARNING(__FUNCTION__
2113 "(), busy with a previous query\n");
2114 return -EBUSY;
2115 }
2116
2117 self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
2118 irda_getvalue_confirm);
2119
2120 /* Treat unexpected signals as disconnect */
2121 self->errno = -EHOSTUNREACH;
2122
2123 /* Query remote LM-IAS */
2124 iriap_getvaluebyclass_request(self->iriap,
2125 self->saddr, daddr,
2126 ias_opt.irda_class_name,
2127 ias_opt.irda_attrib_name);
2128 /* Wait for answer (if not already failed) */
2129 if(self->iriap != NULL)
2130 interruptible_sleep_on(&self->query_wait);
2131 /* Check what happened */
2132 if (self->errno)
2133 {
2134 /* Requested object/attribute doesn't exist */
2135 if((self->errno == IAS_CLASS_UNKNOWN) ||
2136 (self->errno == IAS_ATTRIB_UNKNOWN))
2137 return (-EADDRNOTAVAIL);
2138 else
2139 return (-EHOSTUNREACH);
2140 }
2141
2142 /* Translate from internal to user structure */
2143 err = irda_extract_ias_value(&ias_opt, self->ias_result);
2144 if (self->ias_result)
2145 irias_delete_value(self->ias_result);
2146 if (err)
2147 return err;
2148
2149 /* Copy reply to the user */
2150 if (copy_to_user((char *)optval, (char *) &ias_opt,
2151 sizeof(ias_opt)))
2152 return -EFAULT;
2153 /* Note : don't need to put optlen, we checked it */
2154 break;
2155 case IRLMP_WAITDEVICE:
2156 /* This function is just another way of seeing life ;-)
2157 * IRLMP_ENUMDEVICES assumes that you have a static network,
2158 * and that you just want to pick one of the devices present.
2159 * On the other hand, in here we assume that no device is
2160 * present and that at some point in the future a device will
2161 * come into range. When this device arrive, we just wake
2162 * up the caller, so that he has time to connect to it before
2163 * the device goes away...
2164 * Note : once the node has been discovered for more than a
2165 * few second, it won't trigger this function, unless it
2166 * goes away and come back changes its hint bits (so we
2167 * might call it IRLMP_WAITNEWDEVICE).
2168 */
2169
2170 /* Check that the user is passing us an int */
2171 if (len != sizeof(int))
2172 return -EINVAL;
2173 /* Get timeout in ms (max time we block the caller) */
2174 if (get_user(val, (int *)optval))
2175 return -EFAULT;
2176
2177 /* Tell IrLMP we want to be notified */
2178 irlmp_update_client(self->ckey, self->mask,
2179 irda_selective_discovery_indication,
2180 NULL, (void *) self);
2181
2182 /* Do some discovery (and also return cached results) */
2183 irlmp_discovery_request(self->nslots);
2184
2185 /* Wait until a node is discovered */
2186 if (!self->cachediscovery) {
2187 IRDA_DEBUG(1, __FUNCTION__
2188 "(), nothing discovered yet, going to sleep...\n");
2189
2190 /* Set watchdog timer to expire in <val> ms. */
2191 self->watchdog.function = irda_discovery_timeout;
2192 self->watchdog.data = (unsigned long) self;
2193 self->watchdog.expires = jiffies + (val * HZ/1000);
2194 add_timer(&(self->watchdog));
2195
2196 /* Wait for IR-LMP to call us back */
2197 interruptible_sleep_on(&self->query_wait);
2198
2199 /* If watchdog is still activated, kill it! */
2200 if(timer_pending(&(self->watchdog)))
2201 del_timer(&(self->watchdog));
2202
2203 IRDA_DEBUG(1, __FUNCTION__
2204 "(), ...waking up !\n");
2205 }
2206 else
2207 IRDA_DEBUG(1, __FUNCTION__
2208 "(), found immediately !\n");
2209
2210 /* Tell IrLMP that we have been notified */
2211 irlmp_update_client(self->ckey, self->mask, NULL, NULL, NULL);
2212
2213 /* Check if the we got some results */
2214 if (!self->cachediscovery)
2215 return -EAGAIN; /* Didn't find any devices */
2216 /* Cleanup */
2217 self->cachediscovery = NULL;
2218
2219 /* Note : We don't return anything to the user.
2220 * We could return the device that triggered the wake up,
2221 * but it's probably better to force the user to query
2222 * the whole discovery log and let him pick one device...
2223 */
2224 break;
2225 default:
2226 return -ENOPROTOOPT;
2227 }
2228
2229 return 0;
2230 }
2231
2232 static struct net_proto_family irda_family_ops =
2233 {
2234 PF_IRDA,
2235 irda_create
2236 };
2237
2238 static struct proto_ops SOCKOPS_WRAPPED(irda_stream_ops) = {
2239 family: PF_IRDA,
2240
2241 release: irda_release,
2242 bind: irda_bind,
2243 connect: irda_connect,
2244 socketpair: sock_no_socketpair,
2245 accept: irda_accept,
2246 getname: irda_getname,
2247 poll: irda_poll,
2248 ioctl: irda_ioctl,
2249 listen: irda_listen,
2250 shutdown: irda_shutdown,
2251 setsockopt: irda_setsockopt,
2252 getsockopt: irda_getsockopt,
2253 sendmsg: irda_sendmsg,
2254 recvmsg: irda_recvmsg_stream,
2255 mmap: sock_no_mmap,
2256 };
2257
2258 static struct proto_ops SOCKOPS_WRAPPED(irda_seqpacket_ops) = {
2259 family: PF_IRDA,
2260
2261 release: irda_release,
2262 bind: irda_bind,
2263 connect: irda_connect,
2264 socketpair: sock_no_socketpair,
2265 accept: irda_accept,
2266 getname: irda_getname,
2267 poll: datagram_poll,
2268 ioctl: irda_ioctl,
2269 listen: irda_listen,
2270 shutdown: irda_shutdown,
2271 setsockopt: irda_setsockopt,
2272 getsockopt: irda_getsockopt,
2273 sendmsg: irda_sendmsg,
2274 recvmsg: irda_recvmsg_dgram,
2275 mmap: sock_no_mmap,
2276 };
2277
2278 static struct proto_ops SOCKOPS_WRAPPED(irda_dgram_ops) = {
2279 family: PF_IRDA,
2280
2281 release: irda_release,
2282 bind: irda_bind,
2283 connect: irda_connect,
2284 socketpair: sock_no_socketpair,
2285 accept: irda_accept,
2286 getname: irda_getname,
2287 poll: datagram_poll,
2288 ioctl: irda_ioctl,
2289 listen: irda_listen,
2290 shutdown: irda_shutdown,
2291 setsockopt: irda_setsockopt,
2292 getsockopt: irda_getsockopt,
2293 sendmsg: irda_sendmsg_dgram,
2294 recvmsg: irda_recvmsg_dgram,
2295 mmap: sock_no_mmap,
2296 };
2297
2298 #ifdef CONFIG_IRDA_ULTRA
2299 static struct proto_ops SOCKOPS_WRAPPED(irda_ultra_ops) = {
2300 family: PF_IRDA,
2301
2302 release: irda_release,
2303 bind: irda_bind,
2304 connect: sock_no_connect,
2305 socketpair: sock_no_socketpair,
2306 accept: sock_no_accept,
2307 getname: irda_getname,
2308 poll: datagram_poll,
2309 ioctl: irda_ioctl,
2310 listen: sock_no_listen,
2311 shutdown: irda_shutdown,
2312 setsockopt: irda_setsockopt,
2313 getsockopt: irda_getsockopt,
2314 sendmsg: irda_sendmsg_ultra,
2315 recvmsg: irda_recvmsg_dgram,
2316 mmap: sock_no_mmap,
2317 };
2318 #endif /* CONFIG_IRDA_ULTRA */
2319
2320 #include <linux/smp_lock.h>
2321 SOCKOPS_WRAP(irda_stream, PF_IRDA);
2322 SOCKOPS_WRAP(irda_seqpacket, PF_IRDA);
2323 SOCKOPS_WRAP(irda_dgram, PF_IRDA);
2324
2325 /*
2326 * Function irda_device_event (this, event, ptr)
2327 *
2328 * Called when a device is taken up or down
2329 *
2330 */
2331 static int irda_device_event(struct notifier_block *this, unsigned long event,
2332 void *ptr)
2333 {
2334 struct net_device *dev = (struct net_device *) ptr;
2335
2336 /* Reject non IrDA devices */
2337 if (dev->type != ARPHRD_IRDA)
2338 return NOTIFY_DONE;
2339
2340 switch (event) {
2341 case NETDEV_UP:
2342 IRDA_DEBUG(3, __FUNCTION__ "(), NETDEV_UP\n");
2343 /* irda_dev_device_up(dev); */
2344 break;
2345 case NETDEV_DOWN:
2346 IRDA_DEBUG(3, __FUNCTION__ "(), NETDEV_DOWN\n");
2347 /* irda_kill_by_device(dev); */
2348 /* irda_rt_device_down(dev); */
2349 /* irda_dev_device_down(dev); */
2350 break;
2351 default:
2352 break;
2353 }
2354
2355 return NOTIFY_DONE;
2356 }
2357
2358 static struct packet_type irda_packet_type =
2359 {
2360 0, /* MUTTER ntohs(ETH_P_IRDA),*/
2361 NULL,
2362 irlap_driver_rcv,
2363 NULL,
2364 NULL,
2365 };
2366
2367 static struct notifier_block irda_dev_notifier = {
2368 irda_device_event,
2369 NULL,
2370 0
2371 };
2372
2373 /*
2374 * Function irda_proc_modcount (inode, fill)
2375 *
2376 * Use by the proc file system functions to prevent the irda module
2377 * being removed while the use is standing in the net/irda directory
2378 */
2379 void irda_proc_modcount(struct inode *inode, int fill)
2380 {
2381 #ifdef MODULE
2382 #ifdef CONFIG_PROC_FS
2383 if (fill)
2384 MOD_INC_USE_COUNT;
2385 else
2386 MOD_DEC_USE_COUNT;
2387 #endif /* CONFIG_PROC_FS */
2388 #endif /* MODULE */
2389 }
2390
2391 /*
2392 * Function irda_proto_init (pro)
2393 *
2394 * Initialize IrDA protocol layer
2395 *
2396 */
2397 int __init irda_proto_init(void)
2398 {
2399 sock_register(&irda_family_ops);
2400
2401 irda_packet_type.type = htons(ETH_P_IRDA);
2402 dev_add_pack(&irda_packet_type);
2403
2404 register_netdevice_notifier(&irda_dev_notifier);
2405
2406 irda_init();
2407 #ifdef MODULE
2408 irda_device_init(); /* Called by init/main.c when non-modular */
2409 #endif
2410 return 0;
2411 }
2412
2413 /*
2414 * Function irda_proto_cleanup (void)
2415 *
2416 * Remove IrDA protocol layer
2417 *
2418 */
2419 #ifdef MODULE
2420 void irda_proto_cleanup(void)
2421 {
2422 irda_packet_type.type = htons(ETH_P_IRDA);
2423 dev_remove_pack(&irda_packet_type);
2424
2425 unregister_netdevice_notifier(&irda_dev_notifier);
2426
2427 sock_unregister(PF_IRDA);
2428 irda_cleanup();
2429
2430 return;
2431 }
2432 module_init(irda_proto_init);
2433 module_exit(irda_proto_cleanup);
2434
2435 MODULE_AUTHOR("Dag Brattli <dagb@cs.uit.no>");
2436 MODULE_DESCRIPTION("The Linux IrDA Protocol Subsystem");
2437 MODULE_PARM(irda_debug, "1l");
2438 #endif /* MODULE */
2439
2440
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.