1
2
3
4
5
6
7
8
9
10
11#include "irnet_irda.h"
12#include <linux/sched.h>
13#include <linux/seq_file.h>
14#include <linux/slab.h>
15#include <asm/unaligned.h>
16
17
18
19
20
21static void irnet_ppp_disconnect(struct work_struct *work)
22{
23 irnet_socket * self =
24 container_of(work, irnet_socket, disconnect_work);
25
26 if (self == NULL)
27 return;
28
29
30
31
32 if (self->ppp_open && !self->ttp_open && !self->ttp_connect) {
33 ppp_unregister_channel(&self->chan);
34 self->ppp_open = 0;
35 }
36}
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51static void
52irnet_post_event(irnet_socket * ap,
53 irnet_event event,
54 __u32 saddr,
55 __u32 daddr,
56 char * name,
57 __u16 hints)
58{
59 int index;
60
61 DENTER(CTRL_TRACE, "(ap=0x%p, event=%d, daddr=%08x, name=``%s'')\n",
62 ap, event, daddr, name);
63
64
65
66
67
68 spin_lock_bh(&irnet_events.spinlock);
69
70
71 index = irnet_events.index;
72 irnet_events.log[index].event = event;
73 irnet_events.log[index].daddr = daddr;
74 irnet_events.log[index].saddr = saddr;
75
76 if(name)
77 strcpy(irnet_events.log[index].name, name);
78 else
79 irnet_events.log[index].name[0] = '\0';
80
81 irnet_events.log[index].hints.word = hints;
82
83 if((ap != (irnet_socket *) NULL) && (ap->ppp_open))
84 irnet_events.log[index].unit = ppp_unit_number(&ap->chan);
85 else
86 irnet_events.log[index].unit = -1;
87
88
89
90
91 irnet_events.index = (index + 1) % IRNET_MAX_EVENTS;
92
93 DEBUG(CTRL_INFO, "New event index is %d\n", irnet_events.index);
94
95
96 spin_unlock_bh(&irnet_events.spinlock);
97
98
99 wake_up_interruptible_all(&irnet_events.rwait);
100
101 DEXIT(CTRL_TRACE, "\n");
102}
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123static inline int
124irnet_open_tsap(irnet_socket * self)
125{
126 notify_t notify;
127
128 DENTER(IRDA_SR_TRACE, "(self=0x%p)\n", self);
129
130 DABORT(self->tsap != NULL, -EBUSY, IRDA_SR_ERROR, "Already busy !\n");
131
132
133 irda_notify_init(¬ify);
134 notify.connect_confirm = irnet_connect_confirm;
135 notify.connect_indication = irnet_connect_indication;
136 notify.disconnect_indication = irnet_disconnect_indication;
137 notify.data_indication = irnet_data_indication;
138
139 notify.flow_indication = irnet_flow_indication;
140 notify.status_indication = irnet_status_indication;
141 notify.instance = self;
142 strlcpy(notify.name, IRNET_NOTIFY_NAME, sizeof(notify.name));
143
144
145 self->tsap = irttp_open_tsap(LSAP_ANY, DEFAULT_INITIAL_CREDIT,
146 ¬ify);
147 DABORT(self->tsap == NULL, -ENOMEM,
148 IRDA_SR_ERROR, "Unable to allocate TSAP !\n");
149
150
151 self->stsap_sel = self->tsap->stsap_sel;
152
153 DEXIT(IRDA_SR_TRACE, " - tsap=0x%p, sel=0x%X\n",
154 self->tsap, self->stsap_sel);
155 return 0;
156}
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172static inline __u8
173irnet_ias_to_tsap(irnet_socket * self,
174 int result,
175 struct ias_value * value)
176{
177 __u8 dtsap_sel = 0;
178
179 DENTER(IRDA_SR_TRACE, "(self=0x%p)\n", self);
180
181
182 self->errno = 0;
183
184
185 switch(result)
186 {
187
188 case IAS_CLASS_UNKNOWN:
189 case IAS_ATTRIB_UNKNOWN:
190 DEBUG(IRDA_SR_INFO, "IAS object doesn't exist ! (%d)\n", result);
191 self->errno = -EADDRNOTAVAIL;
192 break;
193
194
195 default :
196 DEBUG(IRDA_SR_INFO, "IAS query failed ! (%d)\n", result);
197 self->errno = -EHOSTUNREACH;
198 break;
199
200
201 case IAS_SUCCESS:
202 break;
203 }
204
205
206 if(value != NULL)
207 {
208
209 switch(value->type)
210 {
211 case IAS_INTEGER:
212 DEBUG(IRDA_SR_INFO, "result=%d\n", value->t.integer);
213 if(value->t.integer != -1)
214
215 dtsap_sel = value->t.integer;
216 else
217 self->errno = -EADDRNOTAVAIL;
218 break;
219 default:
220 self->errno = -EADDRNOTAVAIL;
221 DERROR(IRDA_SR_ERROR, "bad type ! (0x%X)\n", value->type);
222 break;
223 }
224
225
226 irias_delete_value(value);
227 }
228 else
229 {
230
231 if(!(self->errno))
232 {
233 DERROR(IRDA_SR_ERROR,
234 "IrDA bug : result == SUCCESS && value == NULL\n");
235 self->errno = -EHOSTUNREACH;
236 }
237 }
238 DEXIT(IRDA_SR_TRACE, "\n");
239
240
241 return dtsap_sel;
242}
243
244
245
246
247
248
249
250
251
252
253
254
255
256static inline int
257irnet_find_lsap_sel(irnet_socket * self)
258{
259 DENTER(IRDA_SR_TRACE, "(self=0x%p)\n", self);
260
261
262 DABORT(self->iriap, -EBUSY, IRDA_SR_ERROR, "busy with a previous query.\n");
263
264
265 self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
266 irnet_getvalue_confirm);
267
268
269 self->errno = -EHOSTUNREACH;
270
271
272 iriap_getvaluebyclass_request(self->iriap, self->rsaddr, self->daddr,
273 IRNET_SERVICE_NAME, IRNET_IAS_VALUE);
274
275
276
277
278
279
280 DEXIT(IRDA_SR_TRACE, "\n");
281 return 0;
282}
283
284
285
286
287
288
289
290
291static inline int
292irnet_connect_tsap(irnet_socket * self)
293{
294 int err;
295
296 DENTER(IRDA_SR_TRACE, "(self=0x%p)\n", self);
297
298
299 err = irnet_open_tsap(self);
300 if(err != 0)
301 {
302 clear_bit(0, &self->ttp_connect);
303 DERROR(IRDA_SR_ERROR, "connect aborted!\n");
304 return err;
305 }
306
307
308 err = irttp_connect_request(self->tsap, self->dtsap_sel,
309 self->rsaddr, self->daddr, NULL,
310 self->max_sdu_size_rx, NULL);
311 if(err != 0)
312 {
313 clear_bit(0, &self->ttp_connect);
314 DERROR(IRDA_SR_ERROR, "connect aborted!\n");
315 return err;
316 }
317
318
319
320
321
322
323 DEXIT(IRDA_SR_TRACE, "\n");
324 return err;
325}
326
327
328
329
330
331
332
333
334
335static inline int
336irnet_discover_next_daddr(irnet_socket * self)
337{
338
339
340 if(self->iriap)
341 {
342 iriap_close(self->iriap);
343 self->iriap = NULL;
344 }
345
346 self->iriap = iriap_open(LSAP_ANY, IAS_CLIENT, self,
347 irnet_discovervalue_confirm);
348 if(self->iriap == NULL)
349 return -ENOMEM;
350
351
352 self->disco_index++;
353
354
355 if(self->disco_index < self->disco_number)
356 {
357
358 iriap_getvaluebyclass_request(self->iriap,
359 self->discoveries[self->disco_index].saddr,
360 self->discoveries[self->disco_index].daddr,
361 IRNET_SERVICE_NAME, IRNET_IAS_VALUE);
362
363
364
365 return 0;
366 }
367 else
368 return 1;
369}
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394static inline int
395irnet_discover_daddr_and_lsap_sel(irnet_socket * self)
396{
397 int ret;
398
399 DENTER(IRDA_SR_TRACE, "(self=0x%p)\n", self);
400
401
402 self->discoveries = irlmp_get_discoveries(&self->disco_number, self->mask,
403 DISCOVERY_DEFAULT_SLOTS);
404
405
406 if(self->discoveries == NULL)
407 {
408 self->disco_number = -1;
409 clear_bit(0, &self->ttp_connect);
410 DRETURN(-ENETUNREACH, IRDA_SR_INFO, "No Cachelog...\n");
411 }
412 DEBUG(IRDA_SR_INFO, "Got the log (0x%p), size is %d\n",
413 self->discoveries, self->disco_number);
414
415
416 self->disco_index = -1;
417 self->daddr = DEV_ADDR_ANY;
418
419
420 ret = irnet_discover_next_daddr(self);
421 if(ret)
422 {
423
424 if(self->iriap)
425 iriap_close(self->iriap);
426 self->iriap = NULL;
427
428
429 kfree(self->discoveries);
430 self->discoveries = NULL;
431
432 clear_bit(0, &self->ttp_connect);
433 DRETURN(-ENETUNREACH, IRDA_SR_INFO, "Cachelog empty...\n");
434 }
435
436
437
438 DEXIT(IRDA_SR_TRACE, "\n");
439 return 0;
440}
441
442
443
444
445
446
447
448
449
450static inline int
451irnet_dname_to_daddr(irnet_socket * self)
452{
453 struct irda_device_info *discoveries;
454 int number;
455 int i;
456
457 DENTER(IRDA_SR_TRACE, "(self=0x%p)\n", self);
458
459
460 discoveries = irlmp_get_discoveries(&number, 0xffff,
461 DISCOVERY_DEFAULT_SLOTS);
462
463 if(discoveries == NULL)
464 DRETURN(-ENETUNREACH, IRDA_SR_INFO, "Cachelog empty...\n");
465
466
467
468
469
470
471 for(i = 0; i < number; i++)
472 {
473
474 if(!strncmp(discoveries[i].info, self->rname, NICKNAME_MAX_LEN))
475 {
476
477 self->daddr = discoveries[i].daddr;
478 DEBUG(IRDA_SR_INFO, "discovered device ``%s'' at address 0x%08x.\n",
479 self->rname, self->daddr);
480 kfree(discoveries);
481 DEXIT(IRDA_SR_TRACE, "\n");
482 return 0;
483 }
484 }
485
486 DEBUG(IRDA_SR_INFO, "cannot discover device ``%s'' !!!\n", self->rname);
487 kfree(discoveries);
488 return -EADDRNOTAVAIL;
489}
490
491
492
493
494
495
496
497
498
499
500
501
502int
503irda_irnet_create(irnet_socket * self)
504{
505 DENTER(IRDA_SOCK_TRACE, "(self=0x%p)\n", self);
506
507 self->magic = IRNET_MAGIC;
508
509 self->ttp_open = 0;
510 self->ttp_connect = 0;
511 self->rname[0] = '\0';
512 self->rdaddr = DEV_ADDR_ANY;
513 self->rsaddr = DEV_ADDR_ANY;
514 self->daddr = DEV_ADDR_ANY;
515 self->saddr = DEV_ADDR_ANY;
516 self->max_sdu_size_rx = TTP_SAR_UNBOUND;
517
518
519 self->ckey = irlmp_register_client(0, NULL, NULL, NULL);
520#ifdef DISCOVERY_NOMASK
521 self->mask = 0xffff;
522#else
523 self->mask = irlmp_service_to_hint(S_LAN);
524#endif
525 self->tx_flow = FLOW_START;
526
527 INIT_WORK(&self->disconnect_work, irnet_ppp_disconnect);
528
529 DEXIT(IRDA_SOCK_TRACE, "\n");
530 return 0;
531}
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546int
547irda_irnet_connect(irnet_socket * self)
548{
549 int err;
550
551 DENTER(IRDA_SOCK_TRACE, "(self=0x%p)\n", self);
552
553
554
555
556
557 if(test_and_set_bit(0, &self->ttp_connect))
558 DRETURN(-EBUSY, IRDA_SOCK_INFO, "Already connecting...\n");
559 if((self->iriap != NULL) || (self->tsap != NULL))
560 DERROR(IRDA_SOCK_ERROR, "Socket not cleaned up...\n");
561
562
563
564
565
566 if((irnet_server.running) && (self->q.q_next == NULL))
567 {
568 spin_lock_bh(&irnet_server.spinlock);
569 hashbin_insert(irnet_server.list, (irda_queue_t *) self, 0, self->rname);
570 spin_unlock_bh(&irnet_server.spinlock);
571 DEBUG(IRDA_SOCK_INFO, "Inserted ``%s'' in hashbin...\n", self->rname);
572 }
573
574
575 if((self->rdaddr == DEV_ADDR_ANY) && (self->rname[0] == '\0'))
576 {
577
578 if((err = irnet_discover_daddr_and_lsap_sel(self)) != 0)
579 DRETURN(err, IRDA_SOCK_INFO, "auto-connect failed!\n");
580
581 }
582 else
583 {
584
585 if(self->rdaddr == DEV_ADDR_ANY)
586 {
587 if((err = irnet_dname_to_daddr(self)) != 0)
588 DRETURN(err, IRDA_SOCK_INFO, "name connect failed!\n");
589 }
590 else
591
592 self->daddr = self->rdaddr;
593
594
595 irnet_find_lsap_sel(self);
596
597 }
598
599
600
601
602
603 DEXIT(IRDA_SOCK_TRACE, "\n");
604 return 0;
605}
606
607
608
609
610
611
612
613
614
615void
616irda_irnet_destroy(irnet_socket * self)
617{
618 DENTER(IRDA_SOCK_TRACE, "(self=0x%p)\n", self);
619 if(self == NULL)
620 return;
621
622
623
624 if((irnet_server.running) && (self->q.q_next != NULL))
625 {
626 struct irnet_socket * entry;
627 DEBUG(IRDA_SOCK_INFO, "Removing from hash..\n");
628 spin_lock_bh(&irnet_server.spinlock);
629 entry = hashbin_remove_this(irnet_server.list, (irda_queue_t *) self);
630 self->q.q_next = NULL;
631 spin_unlock_bh(&irnet_server.spinlock);
632 DASSERT(entry == self, , IRDA_SOCK_ERROR, "Can't remove from hash.\n");
633 }
634
635
636 if(test_bit(0, &self->ttp_open))
637 {
638
639
640
641 irnet_post_event(NULL, IRNET_DISCONNECT_TO,
642 self->saddr, self->daddr, self->rname, 0);
643 }
644
645
646
647 clear_bit(0, &self->ttp_connect);
648
649
650 clear_bit(0, &self->ttp_open);
651
652
653 irlmp_unregister_client(self->ckey);
654
655
656 if(self->iriap)
657 {
658 iriap_close(self->iriap);
659 self->iriap = NULL;
660 }
661
662
663 if(self->discoveries != NULL)
664 {
665
666 kfree(self->discoveries);
667 self->discoveries = NULL;
668 }
669
670
671 if(self->tsap)
672 {
673 DEBUG(IRDA_SOCK_INFO, "Closing our TTP connection.\n");
674 irttp_disconnect_request(self->tsap, NULL, P_NORMAL);
675 irttp_close_tsap(self->tsap);
676 self->tsap = NULL;
677 }
678 self->stsap_sel = 0;
679
680 DEXIT(IRDA_SOCK_TRACE, "\n");
681}
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701static inline int
702irnet_daddr_to_dname(irnet_socket * self)
703{
704 struct irda_device_info *discoveries;
705 int number;
706 int i;
707
708 DENTER(IRDA_SERV_TRACE, "(self=0x%p)\n", self);
709
710
711 discoveries = irlmp_get_discoveries(&number, 0xffff,
712 DISCOVERY_DEFAULT_SLOTS);
713
714 if (discoveries == NULL)
715 DRETURN(-ENETUNREACH, IRDA_SERV_INFO, "Cachelog empty...\n");
716
717
718 for(i = 0; i < number; i++)
719 {
720
721 if(discoveries[i].daddr == self->daddr)
722 {
723
724 strlcpy(self->rname, discoveries[i].info, sizeof(self->rname));
725 self->rname[sizeof(self->rname) - 1] = '\0';
726 DEBUG(IRDA_SERV_INFO, "Device 0x%08x is in fact ``%s''.\n",
727 self->daddr, self->rname);
728 kfree(discoveries);
729 DEXIT(IRDA_SERV_TRACE, "\n");
730 return 0;
731 }
732 }
733
734 DEXIT(IRDA_SERV_INFO, ": cannot discover device 0x%08x !!!\n", self->daddr);
735 kfree(discoveries);
736 return -EADDRNOTAVAIL;
737}
738
739
740
741
742
743
744
745
746
747
748static inline irnet_socket *
749irnet_find_socket(irnet_socket * self)
750{
751 irnet_socket * new = (irnet_socket *) NULL;
752 int err;
753
754 DENTER(IRDA_SERV_TRACE, "(self=0x%p)\n", self);
755
756
757 self->daddr = irttp_get_daddr(self->tsap);
758 self->saddr = irttp_get_saddr(self->tsap);
759
760
761 err = irnet_daddr_to_dname(self);
762
763
764 spin_lock_bh(&irnet_server.spinlock);
765
766
767
768 if(err == 0)
769 {
770 new = (irnet_socket *) hashbin_find(irnet_server.list,
771 0, self->rname);
772 if(new)
773 DEBUG(IRDA_SERV_INFO, "Socket 0x%p matches rname ``%s''.\n",
774 new, new->rname);
775 }
776
777
778
779
780
781 if(new == (irnet_socket *) NULL)
782 {
783 new = (irnet_socket *) hashbin_get_first(irnet_server.list);
784 while(new !=(irnet_socket *) NULL)
785 {
786
787 if((new->rdaddr == self->daddr) || (new->daddr == self->daddr))
788 {
789
790 DEBUG(IRDA_SERV_INFO, "Socket 0x%p matches daddr %#08x.\n",
791 new, self->daddr);
792 break;
793 }
794 new = (irnet_socket *) hashbin_get_next(irnet_server.list);
795 }
796 }
797
798
799 if(new == (irnet_socket *) NULL)
800 {
801 new = (irnet_socket *) hashbin_get_first(irnet_server.list);
802 while(new !=(irnet_socket *) NULL)
803 {
804
805 if(!(test_bit(0, &new->ttp_open)) && (new->rdaddr == DEV_ADDR_ANY) &&
806 (new->rname[0] == '\0') && (new->ppp_open))
807 {
808
809 DEBUG(IRDA_SERV_INFO, "Socket 0x%p is free.\n",
810 new);
811 break;
812 }
813 new = (irnet_socket *) hashbin_get_next(irnet_server.list);
814 }
815 }
816
817
818 spin_unlock_bh(&irnet_server.spinlock);
819
820 DEXIT(IRDA_SERV_TRACE, " - new = 0x%p\n", new);
821 return new;
822}
823
824
825
826
827
828
829
830
831static inline int
832irnet_connect_socket(irnet_socket * server,
833 irnet_socket * new,
834 struct qos_info * qos,
835 __u32 max_sdu_size,
836 __u8 max_header_size)
837{
838 DENTER(IRDA_SERV_TRACE, "(server=0x%p, new=0x%p)\n",
839 server, new);
840
841
842 new->tsap = irttp_dup(server->tsap, new);
843 DABORT(new->tsap == NULL, -1, IRDA_SERV_ERROR, "dup failed!\n");
844
845
846 new->stsap_sel = new->tsap->stsap_sel;
847 new->dtsap_sel = new->tsap->dtsap_sel;
848 new->saddr = irttp_get_saddr(new->tsap);
849 new->daddr = irttp_get_daddr(new->tsap);
850
851 new->max_header_size = max_header_size;
852 new->max_sdu_size_tx = max_sdu_size;
853 new->max_data_size = max_sdu_size;
854#ifdef STREAM_COMPAT
855
856 if(max_sdu_size == 0)
857 new->max_data_size = irttp_get_max_seg_size(new->tsap);
858#endif
859
860
861 irttp_listen(server->tsap);
862
863
864 irttp_connect_response(new->tsap, new->max_sdu_size_rx, NULL);
865
866
867 set_bit(0, &new->ttp_open);
868
869
870
871 clear_bit(0, &new->ttp_connect);
872 if(new->iriap)
873 {
874 iriap_close(new->iriap);
875 new->iriap = NULL;
876 }
877 if(new->discoveries != NULL)
878 {
879 kfree(new->discoveries);
880 new->discoveries = NULL;
881 }
882
883#ifdef CONNECT_INDIC_KICK
884
885
886
887
888 ppp_output_wakeup(&new->chan);
889#endif
890
891
892 irnet_post_event(new, IRNET_CONNECT_FROM,
893 new->saddr, new->daddr, server->rname, 0);
894
895 DEXIT(IRDA_SERV_TRACE, "\n");
896 return 0;
897}
898
899
900
901
902
903
904
905
906static inline void
907irnet_disconnect_server(irnet_socket * self,
908 struct sk_buff *skb)
909{
910 DENTER(IRDA_SERV_TRACE, "(self=0x%p)\n", self);
911
912
913 kfree_skb(skb);
914
915#ifdef FAIL_SEND_DISCONNECT
916
917
918
919 irttp_disconnect_request(self->tsap, NULL, P_NORMAL);
920#endif
921
922
923 irnet_post_event(NULL, IRNET_REQUEST_FROM,
924 self->saddr, self->daddr, self->rname, 0);
925
926
927 irttp_listen(self->tsap);
928
929 DEXIT(IRDA_SERV_TRACE, "\n");
930}
931
932
933
934
935
936
937
938
939
940
941static inline int
942irnet_setup_server(void)
943{
944 __u16 hints;
945
946 DENTER(IRDA_SERV_TRACE, "()\n");
947
948
949 irda_irnet_create(&irnet_server.s);
950
951
952 irnet_open_tsap(&irnet_server.s);
953
954
955 irnet_server.s.ppp_open = 0;
956 irnet_server.s.chan.private = NULL;
957 irnet_server.s.file = NULL;
958
959
960
961
962
963
964 hints = irlmp_service_to_hint(S_LAN);
965
966#ifdef ADVERTISE_HINT
967
968 irnet_server.skey = irlmp_register_service(hints);
969#endif
970
971
972 irnet_server.ias_obj = irias_new_object(IRNET_SERVICE_NAME, jiffies);
973 irias_add_integer_attrib(irnet_server.ias_obj, IRNET_IAS_VALUE,
974 irnet_server.s.stsap_sel, IAS_KERNEL_ATTR);
975 irias_insert_object(irnet_server.ias_obj);
976
977#ifdef DISCOVERY_EVENTS
978
979 irlmp_update_client(irnet_server.s.ckey, hints,
980 irnet_discovery_indication, irnet_expiry_indication,
981 (void *) &irnet_server.s);
982#endif
983
984 DEXIT(IRDA_SERV_TRACE, " - self=0x%p\n", &irnet_server.s);
985 return 0;
986}
987
988
989
990
991
992
993
994
995
996static inline void
997irnet_destroy_server(void)
998{
999 DENTER(IRDA_SERV_TRACE, "()\n");
1000
1001#ifdef ADVERTISE_HINT
1002
1003 irlmp_unregister_service(irnet_server.skey);
1004#endif
1005
1006
1007 if(irnet_server.ias_obj)
1008 irias_delete_object(irnet_server.ias_obj);
1009
1010
1011 irda_irnet_destroy(&irnet_server.s);
1012
1013 DEXIT(IRDA_SERV_TRACE, "\n");
1014}
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031static int
1032irnet_data_indication(void * instance,
1033 void * sap,
1034 struct sk_buff *skb)
1035{
1036 irnet_socket * ap = (irnet_socket *) instance;
1037 unsigned char * p;
1038 int code = 0;
1039
1040 DENTER(IRDA_TCB_TRACE, "(self/ap=0x%p, skb=0x%p)\n",
1041 ap, skb);
1042 DASSERT(skb != NULL, 0, IRDA_CB_ERROR, "skb is NULL !!!\n");
1043
1044
1045 if(!ap->ppp_open)
1046 {
1047 DERROR(IRDA_CB_ERROR, "PPP not ready, dropping packet...\n");
1048
1049
1050
1051 return -ENOMEM;
1052 }
1053
1054
1055 p = skb->data;
1056 if((p[0] == PPP_ALLSTATIONS) && (p[1] == PPP_UI))
1057 {
1058
1059 if(skb->len < 3)
1060 goto err_exit;
1061 p = skb_pull(skb, 2);
1062 }
1063
1064
1065 if(p[0] & 1)
1066 {
1067
1068 skb_push(skb, 1)[0] = 0;
1069 }
1070 else
1071 if(skb->len < 2)
1072 goto err_exit;
1073
1074
1075
1076
1077 ppp_input(&ap->chan, skb);
1078
1079 DEXIT(IRDA_TCB_TRACE, "\n");
1080 return 0;
1081
1082 err_exit:
1083 DERROR(IRDA_CB_ERROR, "Packet too small, dropping...\n");
1084 kfree_skb(skb);
1085 ppp_input_error(&ap->chan, code);
1086 return 0;
1087}
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101static void
1102irnet_disconnect_indication(void * instance,
1103 void * sap,
1104 LM_REASON reason,
1105 struct sk_buff *skb)
1106{
1107 irnet_socket * self = (irnet_socket *) instance;
1108 int test_open;
1109 int test_connect;
1110
1111 DENTER(IRDA_TCB_TRACE, "(self=0x%p)\n", self);
1112 DASSERT(self != NULL, , IRDA_CB_ERROR, "Self is NULL !!!\n");
1113
1114
1115 if(skb)
1116 dev_kfree_skb(skb);
1117
1118
1119 test_open = test_and_clear_bit(0, &self->ttp_open);
1120
1121
1122 test_connect = test_and_clear_bit(0, &self->ttp_connect);
1123
1124
1125
1126
1127
1128 if(!(test_open || test_connect))
1129 {
1130 DERROR(IRDA_CB_ERROR, "Race condition detected...\n");
1131 return;
1132 }
1133
1134
1135 if(test_open)
1136 irnet_post_event(self, IRNET_DISCONNECT_FROM,
1137 self->saddr, self->daddr, self->rname, 0);
1138 else
1139
1140 if((self->tsap) && (self != &irnet_server.s))
1141 irnet_post_event(self, IRNET_NOANSWER_FROM,
1142 self->saddr, self->daddr, self->rname, 0);
1143
1144
1145 if((self->tsap) && (self != &irnet_server.s))
1146 {
1147 DEBUG(IRDA_CB_INFO, "Closing our TTP connection.\n");
1148 irttp_close_tsap(self->tsap);
1149 self->tsap = NULL;
1150 }
1151
1152 self->stsap_sel = 0;
1153 self->daddr = DEV_ADDR_ANY;
1154 self->tx_flow = FLOW_START;
1155
1156
1157 if(self->ppp_open)
1158 {
1159 if(test_open)
1160 {
1161
1162 schedule_work(&self->disconnect_work);
1163 }
1164 else
1165 {
1166
1167
1168
1169
1170 ppp_output_wakeup(&self->chan);
1171 }
1172 }
1173
1174 DEXIT(IRDA_TCB_TRACE, "\n");
1175}
1176
1177
1178
1179
1180
1181
1182
1183
1184static void
1185irnet_connect_confirm(void * instance,
1186 void * sap,
1187 struct qos_info *qos,
1188 __u32 max_sdu_size,
1189 __u8 max_header_size,
1190 struct sk_buff *skb)
1191{
1192 irnet_socket * self = (irnet_socket *) instance;
1193
1194 DENTER(IRDA_TCB_TRACE, "(self=0x%p)\n", self);
1195
1196
1197 if(! test_bit(0, &self->ttp_connect))
1198 {
1199 DERROR(IRDA_CB_ERROR, "Socket no longer connecting. Ouch !\n");
1200 return;
1201 }
1202
1203
1204 self->max_header_size = max_header_size;
1205
1206
1207 self->max_sdu_size_tx = max_sdu_size;
1208 self->max_data_size = max_sdu_size;
1209#ifdef STREAM_COMPAT
1210 if(max_sdu_size == 0)
1211 self->max_data_size = irttp_get_max_seg_size(self->tsap);
1212#endif
1213
1214
1215 self->saddr = irttp_get_saddr(self->tsap);
1216
1217
1218 set_bit(0, &self->ttp_open);
1219 clear_bit(0, &self->ttp_connect);
1220
1221 ppp_output_wakeup(&self->chan);
1222
1223
1224 if(skb->len > 0)
1225 {
1226#ifdef PASS_CONNECT_PACKETS
1227 DEBUG(IRDA_CB_INFO, "Passing connect packet to PPP.\n");
1228
1229 irnet_data_indication(instance, sap, skb);
1230#else
1231 DERROR(IRDA_CB_ERROR, "Dropping non empty packet.\n");
1232 kfree_skb(skb);
1233#endif
1234 }
1235 else
1236 kfree_skb(skb);
1237
1238
1239 irnet_post_event(self, IRNET_CONNECT_TO,
1240 self->saddr, self->daddr, self->rname, 0);
1241
1242 DEXIT(IRDA_TCB_TRACE, "\n");
1243}
1244
1245
1246
1247
1248
1249
1250
1251
1252static void
1253irnet_flow_indication(void * instance,
1254 void * sap,
1255 LOCAL_FLOW flow)
1256{
1257 irnet_socket * self = (irnet_socket *) instance;
1258 LOCAL_FLOW oldflow = self->tx_flow;
1259
1260 DENTER(IRDA_TCB_TRACE, "(self=0x%p, flow=%d)\n", self, flow);
1261
1262
1263 self->tx_flow = flow;
1264
1265
1266 switch(flow)
1267 {
1268 case FLOW_START:
1269 DEBUG(IRDA_CB_INFO, "IrTTP wants us to start again\n");
1270
1271 if(oldflow == FLOW_STOP)
1272 ppp_output_wakeup(&self->chan);
1273 else
1274 DEBUG(IRDA_CB_INFO, "But we were already transmitting !!!\n");
1275 break;
1276 case FLOW_STOP:
1277 DEBUG(IRDA_CB_INFO, "IrTTP wants us to slow down\n");
1278 break;
1279 default:
1280 DEBUG(IRDA_CB_INFO, "Unknown flow command!\n");
1281 break;
1282 }
1283
1284 DEXIT(IRDA_TCB_TRACE, "\n");
1285}
1286
1287
1288
1289
1290
1291
1292
1293
1294static void
1295irnet_status_indication(void * instance,
1296 LINK_STATUS link,
1297 LOCK_STATUS lock)
1298{
1299 irnet_socket * self = (irnet_socket *) instance;
1300
1301 DENTER(IRDA_TCB_TRACE, "(self=0x%p)\n", self);
1302 DASSERT(self != NULL, , IRDA_CB_ERROR, "Self is NULL !!!\n");
1303
1304
1305 switch(link)
1306 {
1307 case STATUS_NO_ACTIVITY:
1308 irnet_post_event(self, IRNET_BLOCKED_LINK,
1309 self->saddr, self->daddr, self->rname, 0);
1310 break;
1311 default:
1312 DEBUG(IRDA_CB_INFO, "Unknown status...\n");
1313 }
1314
1315 DEXIT(IRDA_TCB_TRACE, "\n");
1316}
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332static void
1333irnet_connect_indication(void * instance,
1334 void * sap,
1335 struct qos_info *qos,
1336 __u32 max_sdu_size,
1337 __u8 max_header_size,
1338 struct sk_buff *skb)
1339{
1340 irnet_socket * server = &irnet_server.s;
1341 irnet_socket * new = (irnet_socket *) NULL;
1342
1343 DENTER(IRDA_TCB_TRACE, "(server=0x%p)\n", server);
1344 DASSERT(instance == &irnet_server, , IRDA_CB_ERROR,
1345 "Invalid instance (0x%p) !!!\n", instance);
1346 DASSERT(sap == irnet_server.s.tsap, , IRDA_CB_ERROR, "Invalid sap !!!\n");
1347
1348
1349 new = irnet_find_socket(server);
1350
1351
1352 if(new == (irnet_socket *) NULL)
1353 {
1354 DEXIT(IRDA_CB_INFO, ": No socket waiting for this connection.\n");
1355 irnet_disconnect_server(server, skb);
1356 return;
1357 }
1358
1359
1360 if(test_bit(0, &new->ttp_open))
1361 {
1362 DEXIT(IRDA_CB_INFO, ": Socket already connected.\n");
1363 irnet_disconnect_server(server, skb);
1364 return;
1365 }
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402 if(0
1403#ifdef ALLOW_SIMULT_CONNECT
1404 || ((irttp_is_primary(server->tsap) == 1) &&
1405 (test_and_clear_bit(0, &new->ttp_connect)))
1406#endif
1407 )
1408 {
1409 DERROR(IRDA_CB_ERROR, "Socket already connecting, but going to reuse it !\n");
1410
1411
1412 if(new->tsap != NULL)
1413 {
1414
1415
1416
1417 irttp_close_tsap(new->tsap);
1418 new->tsap = NULL;
1419 }
1420 }
1421 else
1422 {
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433 if((test_bit(0, &new->ttp_connect)) || (new->tsap != NULL))
1434 {
1435
1436 DERROR(IRDA_CB_ERROR, "Race condition detected, socket in use, abort connect...\n");
1437 irnet_disconnect_server(server, skb);
1438 return;
1439 }
1440 }
1441
1442
1443 irnet_connect_socket(server, new, qos, max_sdu_size, max_header_size);
1444
1445
1446 if(skb->len > 0)
1447 {
1448#ifdef PASS_CONNECT_PACKETS
1449 DEBUG(IRDA_CB_INFO, "Passing connect packet to PPP.\n");
1450
1451 irnet_data_indication(new, new->tsap, skb);
1452#else
1453 DERROR(IRDA_CB_ERROR, "Dropping non empty packet.\n");
1454 kfree_skb(skb);
1455#endif
1456 }
1457 else
1458 kfree_skb(skb);
1459
1460 DEXIT(IRDA_TCB_TRACE, "\n");
1461}
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481static void
1482irnet_getvalue_confirm(int result,
1483 __u16 obj_id,
1484 struct ias_value *value,
1485 void * priv)
1486{
1487 irnet_socket * self = (irnet_socket *) priv;
1488
1489 DENTER(IRDA_OCB_TRACE, "(self=0x%p)\n", self);
1490 DASSERT(self != NULL, , IRDA_OCB_ERROR, "Self is NULL !!!\n");
1491
1492
1493
1494 if(! test_bit(0, &self->ttp_connect))
1495 {
1496 DERROR(IRDA_OCB_ERROR, "Socket no longer connecting. Ouch !\n");
1497 return;
1498 }
1499
1500
1501 iriap_close(self->iriap);
1502 self->iriap = NULL;
1503
1504
1505 self->dtsap_sel = irnet_ias_to_tsap(self, result, value);
1506
1507
1508 if(self->errno)
1509 {
1510 clear_bit(0, &self->ttp_connect);
1511 DERROR(IRDA_OCB_ERROR, "IAS connect failed ! (0x%X)\n", self->errno);
1512 return;
1513 }
1514
1515 DEBUG(IRDA_OCB_INFO, "daddr = %08x, lsap = %d, starting IrTTP connection\n",
1516 self->daddr, self->dtsap_sel);
1517
1518
1519 irnet_connect_tsap(self);
1520
1521 DEXIT(IRDA_OCB_TRACE, "\n");
1522}
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544static void
1545irnet_discovervalue_confirm(int result,
1546 __u16 obj_id,
1547 struct ias_value *value,
1548 void * priv)
1549{
1550 irnet_socket * self = (irnet_socket *) priv;
1551 __u8 dtsap_sel;
1552
1553 DENTER(IRDA_OCB_TRACE, "(self=0x%p)\n", self);
1554 DASSERT(self != NULL, , IRDA_OCB_ERROR, "Self is NULL !!!\n");
1555
1556
1557
1558 if(! test_bit(0, &self->ttp_connect))
1559 {
1560 DERROR(IRDA_OCB_ERROR, "Socket no longer connecting. Ouch !\n");
1561 return;
1562 }
1563
1564
1565 dtsap_sel = irnet_ias_to_tsap(self, result, value);
1566
1567
1568 if(self->errno == 0)
1569 {
1570
1571 if(self->daddr != DEV_ADDR_ANY)
1572 {
1573 DERROR(IRDA_OCB_ERROR, "More than one device in range supports IrNET...\n");
1574 }
1575 else
1576 {
1577
1578 self->daddr = self->discoveries[self->disco_index].daddr;
1579 self->dtsap_sel = dtsap_sel;
1580 }
1581 }
1582
1583
1584 if((self->errno == -EADDRNOTAVAIL) || (self->errno == 0))
1585 {
1586 int ret;
1587
1588
1589 ret = irnet_discover_next_daddr(self);
1590 if(!ret)
1591 {
1592
1593
1594 return;
1595 }
1596
1597 }
1598
1599
1600
1601
1602 iriap_close(self->iriap);
1603 self->iriap = NULL;
1604
1605
1606 DEBUG(IRDA_OCB_INFO, "Cleaning up log (0x%p)\n",
1607 self->discoveries);
1608 if(self->discoveries != NULL)
1609 {
1610
1611 kfree(self->discoveries);
1612 self->discoveries = NULL;
1613 }
1614 self->disco_number = -1;
1615
1616
1617 if(self->daddr == DEV_ADDR_ANY)
1618 {
1619 self->daddr = DEV_ADDR_ANY;
1620 clear_bit(0, &self->ttp_connect);
1621 DEXIT(IRDA_OCB_TRACE, ": cannot discover IrNET in any device !!!\n");
1622 return;
1623 }
1624
1625
1626
1627 DEBUG(IRDA_OCB_INFO, "daddr = %08x, lsap = %d, starting IrTTP connection\n",
1628 self->daddr, self->dtsap_sel);
1629
1630
1631 irnet_connect_tsap(self);
1632
1633 DEXIT(IRDA_OCB_TRACE, "\n");
1634}
1635
1636#ifdef DISCOVERY_EVENTS
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659static void
1660irnet_discovery_indication(discinfo_t * discovery,
1661 DISCOVERY_MODE mode,
1662 void * priv)
1663{
1664 irnet_socket * self = &irnet_server.s;
1665
1666 DENTER(IRDA_OCB_TRACE, "(self=0x%p)\n", self);
1667 DASSERT(priv == &irnet_server, , IRDA_OCB_ERROR,
1668 "Invalid instance (0x%p) !!!\n", priv);
1669
1670 DEBUG(IRDA_OCB_INFO, "Discovered new IrNET/IrLAN node %s...\n",
1671 discovery->info);
1672
1673
1674 irnet_post_event(NULL, IRNET_DISCOVER,
1675 discovery->saddr, discovery->daddr, discovery->info,
1676 get_unaligned((__u16 *)discovery->hints));
1677
1678 DEXIT(IRDA_OCB_TRACE, "\n");
1679}
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690static void
1691irnet_expiry_indication(discinfo_t * expiry,
1692 DISCOVERY_MODE mode,
1693 void * priv)
1694{
1695 irnet_socket * self = &irnet_server.s;
1696
1697 DENTER(IRDA_OCB_TRACE, "(self=0x%p)\n", self);
1698 DASSERT(priv == &irnet_server, , IRDA_OCB_ERROR,
1699 "Invalid instance (0x%p) !!!\n", priv);
1700
1701 DEBUG(IRDA_OCB_INFO, "IrNET/IrLAN node %s expired...\n",
1702 expiry->info);
1703
1704
1705 irnet_post_event(NULL, IRNET_EXPIRE,
1706 expiry->saddr, expiry->daddr, expiry->info,
1707 get_unaligned((__u16 *)expiry->hints));
1708
1709 DEXIT(IRDA_OCB_TRACE, "\n");
1710}
1711#endif
1712
1713
1714
1715
1716
1717
1718
1719
1720#ifdef CONFIG_PROC_FS
1721static int
1722irnet_proc_show(struct seq_file *m, void *v)
1723{
1724 irnet_socket * self;
1725 char * state;
1726 int i = 0;
1727
1728
1729 seq_printf(m, "IrNET server - ");
1730 seq_printf(m, "IrDA state: %s, ",
1731 (irnet_server.running ? "running" : "dead"));
1732 seq_printf(m, "stsap_sel: %02x, ", irnet_server.s.stsap_sel);
1733 seq_printf(m, "dtsap_sel: %02x\n", irnet_server.s.dtsap_sel);
1734
1735
1736 if(!irnet_server.running)
1737 return 0;
1738
1739
1740 spin_lock_bh(&irnet_server.spinlock);
1741
1742
1743 self = (irnet_socket *) hashbin_get_first(irnet_server.list);
1744 while(self != NULL)
1745 {
1746
1747 seq_printf(m, "\nIrNET socket %d - ", i++);
1748
1749
1750 seq_printf(m, "Requested IrDA name: \"%s\", ", self->rname);
1751 seq_printf(m, "daddr: %08x, ", self->rdaddr);
1752 seq_printf(m, "saddr: %08x\n", self->rsaddr);
1753
1754
1755 seq_printf(m, " PPP state: %s",
1756 (self->ppp_open ? "registered" : "unregistered"));
1757 if(self->ppp_open)
1758 {
1759 seq_printf(m, ", unit: ppp%d",
1760 ppp_unit_number(&self->chan));
1761 seq_printf(m, ", channel: %d",
1762 ppp_channel_index(&self->chan));
1763 seq_printf(m, ", mru: %d",
1764 self->mru);
1765
1766 }
1767
1768
1769 if(self->ttp_open)
1770 state = "connected";
1771 else
1772 if(self->tsap != NULL)
1773 state = "connecting";
1774 else
1775 if(self->iriap != NULL)
1776 state = "searching";
1777 else
1778 if(self->ttp_connect)
1779 state = "weird";
1780 else
1781 state = "idle";
1782 seq_printf(m, "\n IrDA state: %s, ", state);
1783 seq_printf(m, "daddr: %08x, ", self->daddr);
1784 seq_printf(m, "stsap_sel: %02x, ", self->stsap_sel);
1785 seq_printf(m, "dtsap_sel: %02x\n", self->dtsap_sel);
1786
1787
1788 self = (irnet_socket *) hashbin_get_next(irnet_server.list);
1789 }
1790
1791
1792 spin_unlock_bh(&irnet_server.spinlock);
1793
1794 return 0;
1795}
1796
1797static int irnet_proc_open(struct inode *inode, struct file *file)
1798{
1799 return single_open(file, irnet_proc_show, NULL);
1800}
1801
1802static const struct file_operations irnet_proc_fops = {
1803 .owner = THIS_MODULE,
1804 .open = irnet_proc_open,
1805 .read = seq_read,
1806 .llseek = seq_lseek,
1807 .release = single_release,
1808};
1809#endif
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822int __init
1823irda_irnet_init(void)
1824{
1825 int err = 0;
1826
1827 DENTER(MODULE_TRACE, "()\n");
1828
1829
1830 memset(&irnet_server, 0, sizeof(struct irnet_root));
1831
1832
1833 irnet_server.list = hashbin_new(HB_NOLOCK);
1834 DABORT(irnet_server.list == NULL, -ENOMEM,
1835 MODULE_ERROR, "Can't allocate hashbin!\n");
1836
1837 spin_lock_init(&irnet_server.spinlock);
1838
1839
1840 init_waitqueue_head(&irnet_events.rwait);
1841 irnet_events.index = 0;
1842
1843 spin_lock_init(&irnet_events.spinlock);
1844
1845#ifdef CONFIG_PROC_FS
1846
1847 proc_create("irnet", 0, proc_irda, &irnet_proc_fops);
1848#endif
1849
1850
1851 err = irnet_setup_server();
1852
1853 if(!err)
1854
1855 irnet_server.running = 1;
1856
1857 DEXIT(MODULE_TRACE, "\n");
1858 return err;
1859}
1860
1861
1862
1863
1864
1865void __exit
1866irda_irnet_cleanup(void)
1867{
1868 DENTER(MODULE_TRACE, "()\n");
1869
1870
1871 irnet_server.running = 0;
1872
1873#ifdef CONFIG_PROC_FS
1874
1875 remove_proc_entry("irnet", proc_irda);
1876#endif
1877
1878
1879 irnet_destroy_server();
1880
1881
1882 hashbin_delete(irnet_server.list, (FREE_FUNC) irda_irnet_destroy);
1883
1884 DEXIT(MODULE_TRACE, "\n");
1885}
1886