1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include <linux/skbuff.h>
28#include <linux/init.h>
29#include <linux/fs.h>
30#include <linux/seq_file.h>
31#include <linux/slab.h>
32#include <linux/export.h>
33
34#include <asm/byteorder.h>
35#include <asm/unaligned.h>
36
37#include <net/irda/irda.h>
38#include <net/irda/irlap.h>
39#include <net/irda/irlmp.h>
40#include <net/irda/parameters.h>
41#include <net/irda/irttp.h>
42
43static struct irttp_cb *irttp;
44
45static void __irttp_close_tsap(struct tsap_cb *self);
46
47static int irttp_data_indication(void *instance, void *sap,
48 struct sk_buff *skb);
49static int irttp_udata_indication(void *instance, void *sap,
50 struct sk_buff *skb);
51static void irttp_disconnect_indication(void *instance, void *sap,
52 LM_REASON reason, struct sk_buff *);
53static void irttp_connect_indication(void *instance, void *sap,
54 struct qos_info *qos, __u32 max_sdu_size,
55 __u8 header_size, struct sk_buff *skb);
56static void irttp_connect_confirm(void *instance, void *sap,
57 struct qos_info *qos, __u32 max_sdu_size,
58 __u8 header_size, struct sk_buff *skb);
59static void irttp_run_tx_queue(struct tsap_cb *self);
60static void irttp_run_rx_queue(struct tsap_cb *self);
61
62static void irttp_flush_queues(struct tsap_cb *self);
63static void irttp_fragment_skb(struct tsap_cb *self, struct sk_buff *skb);
64static struct sk_buff *irttp_reassemble_skb(struct tsap_cb *self);
65static void irttp_todo_expired(unsigned long data);
66static int irttp_param_max_sdu_size(void *instance, irda_param_t *param,
67 int get);
68
69static void irttp_flow_indication(void *instance, void *sap, LOCAL_FLOW flow);
70static void irttp_status_indication(void *instance,
71 LINK_STATUS link, LOCK_STATUS lock);
72
73
74static const pi_minor_info_t pi_minor_call_table[] = {
75 { NULL, 0 },
76 { irttp_param_max_sdu_size, PV_INTEGER | PV_BIG_ENDIAN }
77};
78static const pi_major_info_t pi_major_call_table[] = {
79 { pi_minor_call_table, 2 }
80};
81static pi_param_info_t param_info = { pi_major_call_table, 1, 0x0f, 4 };
82
83
84
85
86
87
88
89
90
91int __init irttp_init(void)
92{
93 irttp = kzalloc(sizeof(struct irttp_cb), GFP_KERNEL);
94 if (irttp == NULL)
95 return -ENOMEM;
96
97 irttp->magic = TTP_MAGIC;
98
99 irttp->tsaps = hashbin_new(HB_LOCK);
100 if (!irttp->tsaps) {
101 net_err_ratelimited("%s: can't allocate IrTTP hashbin!\n",
102 __func__);
103 kfree(irttp);
104 return -ENOMEM;
105 }
106
107 return 0;
108}
109
110
111
112
113
114
115
116void irttp_cleanup(void)
117{
118
119 IRDA_ASSERT(irttp->magic == TTP_MAGIC, return;);
120
121
122
123
124 hashbin_delete(irttp->tsaps, (FREE_FUNC) __irttp_close_tsap);
125
126 irttp->magic = 0;
127
128
129 kfree(irttp);
130
131 irttp = NULL;
132}
133
134
135
136
137
138
139
140
141
142
143static inline void irttp_start_todo_timer(struct tsap_cb *self, int timeout)
144{
145
146 mod_timer(&self->todo_timer, jiffies + timeout);
147}
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163static void irttp_todo_expired(unsigned long data)
164{
165 struct tsap_cb *self = (struct tsap_cb *) data;
166
167
168 if (!self || self->magic != TTP_TSAP_MAGIC)
169 return;
170
171 pr_debug("%s(instance=%p)\n", __func__, self);
172
173
174 irttp_run_rx_queue(self);
175 irttp_run_tx_queue(self);
176
177
178 if (test_bit(0, &self->disconnect_pend)) {
179
180 if (skb_queue_empty(&self->tx_queue)) {
181
182 clear_bit(0, &self->disconnect_pend);
183
184
185 irttp_disconnect_request(self, self->disconnect_skb,
186 P_NORMAL);
187 self->disconnect_skb = NULL;
188 } else {
189
190 irttp_start_todo_timer(self, HZ/10);
191
192
193 return;
194 }
195 }
196
197
198 if (self->close_pend)
199
200 irttp_close_tsap(self);
201}
202
203
204
205
206
207
208static void irttp_flush_queues(struct tsap_cb *self)
209{
210 struct sk_buff *skb;
211
212 IRDA_ASSERT(self != NULL, return;);
213 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
214
215
216 while ((skb = skb_dequeue(&self->tx_queue)) != NULL)
217 dev_kfree_skb(skb);
218
219
220 while ((skb = skb_dequeue(&self->rx_queue)) != NULL)
221 dev_kfree_skb(skb);
222
223
224 while ((skb = skb_dequeue(&self->rx_fragments)) != NULL)
225 dev_kfree_skb(skb);
226}
227
228
229
230
231
232
233
234
235static struct sk_buff *irttp_reassemble_skb(struct tsap_cb *self)
236{
237 struct sk_buff *skb, *frag;
238 int n = 0;
239
240 IRDA_ASSERT(self != NULL, return NULL;);
241 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return NULL;);
242
243 pr_debug("%s(), self->rx_sdu_size=%d\n", __func__,
244 self->rx_sdu_size);
245
246 skb = dev_alloc_skb(TTP_HEADER + self->rx_sdu_size);
247 if (!skb)
248 return NULL;
249
250
251
252
253
254 skb_reserve(skb, TTP_HEADER);
255 skb_put(skb, self->rx_sdu_size);
256
257
258
259
260 while ((frag = skb_dequeue(&self->rx_fragments)) != NULL) {
261 skb_copy_to_linear_data_offset(skb, n, frag->data, frag->len);
262 n += frag->len;
263
264 dev_kfree_skb(frag);
265 }
266
267 pr_debug("%s(), frame len=%d, rx_sdu_size=%d, rx_max_sdu_size=%d\n",
268 __func__, n, self->rx_sdu_size, self->rx_max_sdu_size);
269
270
271
272
273
274
275 IRDA_ASSERT(n <= self->rx_sdu_size, n = self->rx_sdu_size;);
276
277
278 skb_trim(skb, n);
279
280 self->rx_sdu_size = 0;
281
282 return skb;
283}
284
285
286
287
288
289
290
291static inline void irttp_fragment_skb(struct tsap_cb *self,
292 struct sk_buff *skb)
293{
294 struct sk_buff *frag;
295 __u8 *frame;
296
297 IRDA_ASSERT(self != NULL, return;);
298 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
299 IRDA_ASSERT(skb != NULL, return;);
300
301
302
303
304 while (skb->len > self->max_seg_size) {
305 pr_debug("%s(), fragmenting ...\n", __func__);
306
307
308 frag = alloc_skb(self->max_seg_size+self->max_header_size,
309 GFP_ATOMIC);
310 if (!frag)
311 return;
312
313 skb_reserve(frag, self->max_header_size);
314
315
316 skb_copy_from_linear_data(skb, skb_put(frag, self->max_seg_size),
317 self->max_seg_size);
318
319
320 frame = skb_push(frag, TTP_HEADER);
321 frame[0] = TTP_MORE;
322
323
324 skb_pull(skb, self->max_seg_size);
325
326
327 skb_queue_tail(&self->tx_queue, frag);
328 }
329
330 pr_debug("%s(), queuing last segment\n", __func__);
331
332 frame = skb_push(skb, TTP_HEADER);
333 frame[0] = 0x00;
334
335
336 skb_queue_tail(&self->tx_queue, skb);
337}
338
339
340
341
342
343
344
345
346static int irttp_param_max_sdu_size(void *instance, irda_param_t *param,
347 int get)
348{
349 struct tsap_cb *self;
350
351 self = instance;
352
353 IRDA_ASSERT(self != NULL, return -1;);
354 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
355
356 if (get)
357 param->pv.i = self->tx_max_sdu_size;
358 else
359 self->tx_max_sdu_size = param->pv.i;
360
361 pr_debug("%s(), MaxSduSize=%d\n", __func__, param->pv.i);
362
363 return 0;
364}
365
366
367
368
369
370
371
372
373
374static void irttp_init_tsap(struct tsap_cb *tsap)
375{
376 spin_lock_init(&tsap->lock);
377 init_timer(&tsap->todo_timer);
378
379 skb_queue_head_init(&tsap->rx_queue);
380 skb_queue_head_init(&tsap->tx_queue);
381 skb_queue_head_init(&tsap->rx_fragments);
382}
383
384
385
386
387
388
389struct tsap_cb *irttp_open_tsap(__u8 stsap_sel, int credit, notify_t *notify)
390{
391 struct tsap_cb *self;
392 struct lsap_cb *lsap;
393 notify_t ttp_notify;
394
395 IRDA_ASSERT(irttp->magic == TTP_MAGIC, return NULL;);
396
397
398
399
400 if ((stsap_sel != LSAP_ANY) &&
401 ((stsap_sel < 0x01) || (stsap_sel >= 0x70))) {
402 pr_debug("%s(), invalid tsap!\n", __func__);
403 return NULL;
404 }
405
406 self = kzalloc(sizeof(struct tsap_cb), GFP_ATOMIC);
407 if (self == NULL)
408 return NULL;
409
410
411 irttp_init_tsap(self);
412
413
414 self->todo_timer.data = (unsigned long) self;
415 self->todo_timer.function = &irttp_todo_expired;
416
417
418 irda_notify_init(&ttp_notify);
419 ttp_notify.connect_confirm = irttp_connect_confirm;
420 ttp_notify.connect_indication = irttp_connect_indication;
421 ttp_notify.disconnect_indication = irttp_disconnect_indication;
422 ttp_notify.data_indication = irttp_data_indication;
423 ttp_notify.udata_indication = irttp_udata_indication;
424 ttp_notify.flow_indication = irttp_flow_indication;
425 if (notify->status_indication != NULL)
426 ttp_notify.status_indication = irttp_status_indication;
427 ttp_notify.instance = self;
428 strncpy(ttp_notify.name, notify->name, NOTIFY_MAX_NAME);
429
430 self->magic = TTP_TSAP_MAGIC;
431 self->connected = FALSE;
432
433
434
435
436 lsap = irlmp_open_lsap(stsap_sel, &ttp_notify, 0);
437 if (lsap == NULL) {
438 pr_debug("%s: unable to allocate LSAP!!\n", __func__);
439 __irttp_close_tsap(self);
440 return NULL;
441 }
442
443
444
445
446
447
448 self->stsap_sel = lsap->slsap_sel;
449 pr_debug("%s(), stsap_sel=%02x\n", __func__, self->stsap_sel);
450
451 self->notify = *notify;
452 self->lsap = lsap;
453
454 hashbin_insert(irttp->tsaps, (irda_queue_t *) self, (long) self, NULL);
455
456 if (credit > TTP_RX_MAX_CREDIT)
457 self->initial_credit = TTP_RX_MAX_CREDIT;
458 else
459 self->initial_credit = credit;
460
461 return self;
462}
463EXPORT_SYMBOL(irttp_open_tsap);
464
465
466
467
468
469
470
471
472static void __irttp_close_tsap(struct tsap_cb *self)
473{
474
475 IRDA_ASSERT(self != NULL, return;);
476 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
477
478 irttp_flush_queues(self);
479
480 del_timer(&self->todo_timer);
481
482
483
484 if (self->disconnect_skb)
485 dev_kfree_skb(self->disconnect_skb);
486
487 self->connected = FALSE;
488 self->magic = ~TTP_TSAP_MAGIC;
489
490 kfree(self);
491}
492
493
494
495
496
497
498
499
500
501
502
503int irttp_close_tsap(struct tsap_cb *self)
504{
505 struct tsap_cb *tsap;
506
507 IRDA_ASSERT(self != NULL, return -1;);
508 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
509
510
511 if (self->connected) {
512
513 if (!test_bit(0, &self->disconnect_pend)) {
514 net_warn_ratelimited("%s: TSAP still connected!\n",
515 __func__);
516 irttp_disconnect_request(self, NULL, P_NORMAL);
517 }
518 self->close_pend = TRUE;
519 irttp_start_todo_timer(self, HZ/10);
520
521 return 0;
522 }
523
524 tsap = hashbin_remove(irttp->tsaps, (long) self, NULL);
525
526 IRDA_ASSERT(tsap == self, return -1;);
527
528
529 if (self->lsap) {
530 irlmp_close_lsap(self->lsap);
531 self->lsap = NULL;
532 }
533
534 __irttp_close_tsap(self);
535
536 return 0;
537}
538EXPORT_SYMBOL(irttp_close_tsap);
539
540
541
542
543
544
545
546int irttp_udata_request(struct tsap_cb *self, struct sk_buff *skb)
547{
548 int ret;
549
550 IRDA_ASSERT(self != NULL, return -1;);
551 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
552 IRDA_ASSERT(skb != NULL, return -1;);
553
554
555 if (skb->len == 0) {
556 ret = 0;
557 goto err;
558 }
559
560
561 if (!self->connected) {
562 net_warn_ratelimited("%s(), Not connected\n", __func__);
563 ret = -ENOTCONN;
564 goto err;
565 }
566
567 if (skb->len > self->max_seg_size) {
568 net_err_ratelimited("%s(), UData is too large for IrLAP!\n",
569 __func__);
570 ret = -EMSGSIZE;
571 goto err;
572 }
573
574 irlmp_udata_request(self->lsap, skb);
575 self->stats.tx_packets++;
576
577 return 0;
578
579err:
580 dev_kfree_skb(skb);
581 return ret;
582}
583EXPORT_SYMBOL(irttp_udata_request);
584
585
586
587
588
589
590
591
592int irttp_data_request(struct tsap_cb *self, struct sk_buff *skb)
593{
594 __u8 *frame;
595 int ret;
596
597 IRDA_ASSERT(self != NULL, return -1;);
598 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
599 IRDA_ASSERT(skb != NULL, return -1;);
600
601 pr_debug("%s() : queue len = %d\n", __func__,
602 skb_queue_len(&self->tx_queue));
603
604
605 if (skb->len == 0) {
606 ret = 0;
607 goto err;
608 }
609
610
611 if (!self->connected) {
612 net_warn_ratelimited("%s: Not connected\n", __func__);
613 ret = -ENOTCONN;
614 goto err;
615 }
616
617
618
619
620
621 if ((self->tx_max_sdu_size == 0) && (skb->len > self->max_seg_size)) {
622 net_err_ratelimited("%s: SAR disabled, and data is too large for IrLAP!\n",
623 __func__);
624 ret = -EMSGSIZE;
625 goto err;
626 }
627
628
629
630
631
632 if ((self->tx_max_sdu_size != 0) &&
633 (self->tx_max_sdu_size != TTP_SAR_UNBOUND) &&
634 (skb->len > self->tx_max_sdu_size)) {
635 net_err_ratelimited("%s: SAR enabled, but data is larger than TxMaxSduSize!\n",
636 __func__);
637 ret = -EMSGSIZE;
638 goto err;
639 }
640
641
642
643 if (skb_queue_len(&self->tx_queue) >= TTP_TX_MAX_QUEUE) {
644
645
646
647 irttp_run_tx_queue(self);
648
649
650
651 ret = -ENOBUFS;
652 goto err;
653 }
654
655
656 if ((self->tx_max_sdu_size == 0) || (skb->len < self->max_seg_size)) {
657
658 IRDA_ASSERT(skb_headroom(skb) >= TTP_HEADER, return -1;);
659 frame = skb_push(skb, TTP_HEADER);
660 frame[0] = 0x00;
661
662 skb_queue_tail(&self->tx_queue, skb);
663 } else {
664
665
666
667
668
669
670 irttp_fragment_skb(self, skb);
671 }
672
673
674 if ((!self->tx_sdu_busy) &&
675 (skb_queue_len(&self->tx_queue) > TTP_TX_HIGH_THRESHOLD)) {
676
677 if (self->notify.flow_indication) {
678 self->notify.flow_indication(self->notify.instance,
679 self, FLOW_STOP);
680 }
681
682
683
684
685
686
687
688 self->tx_sdu_busy = TRUE;
689 }
690
691
692 irttp_run_tx_queue(self);
693
694 return 0;
695
696err:
697 dev_kfree_skb(skb);
698 return ret;
699}
700EXPORT_SYMBOL(irttp_data_request);
701
702
703
704
705
706
707
708static void irttp_run_tx_queue(struct tsap_cb *self)
709{
710 struct sk_buff *skb;
711 unsigned long flags;
712 int n;
713
714 pr_debug("%s() : send_credit = %d, queue_len = %d\n",
715 __func__,
716 self->send_credit, skb_queue_len(&self->tx_queue));
717
718
719 if (irda_lock(&self->tx_queue_lock) == FALSE)
720 return;
721
722
723
724
725 while ((self->send_credit > 0) &&
726 (!irlmp_lap_tx_queue_full(self->lsap)) &&
727 (skb = skb_dequeue(&self->tx_queue))) {
728
729
730
731
732
733 spin_lock_irqsave(&self->lock, flags);
734
735 n = self->avail_credit;
736 self->avail_credit = 0;
737
738
739 if (n > 127) {
740 self->avail_credit = n-127;
741 n = 127;
742 }
743 self->remote_credit += n;
744 self->send_credit--;
745
746 spin_unlock_irqrestore(&self->lock, flags);
747
748
749
750
751
752 skb->data[0] |= (n & 0x7f);
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772 if (skb->sk != NULL) {
773
774 skb_orphan(skb);
775 }
776
777
778
779 irlmp_data_request(self->lsap, skb);
780 self->stats.tx_packets++;
781 }
782
783
784
785
786
787
788
789 if ((self->tx_sdu_busy) &&
790 (skb_queue_len(&self->tx_queue) < TTP_TX_LOW_THRESHOLD) &&
791 (!self->close_pend)) {
792 if (self->notify.flow_indication)
793 self->notify.flow_indication(self->notify.instance,
794 self, FLOW_START);
795
796
797
798
799 self->tx_sdu_busy = FALSE;
800 }
801
802
803 self->tx_queue_lock = 0;
804}
805
806
807
808
809
810
811
812static inline void irttp_give_credit(struct tsap_cb *self)
813{
814 struct sk_buff *tx_skb = NULL;
815 unsigned long flags;
816 int n;
817
818 IRDA_ASSERT(self != NULL, return;);
819 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
820
821 pr_debug("%s() send=%d,avail=%d,remote=%d\n",
822 __func__,
823 self->send_credit, self->avail_credit, self->remote_credit);
824
825
826 tx_skb = alloc_skb(TTP_MAX_HEADER, GFP_ATOMIC);
827 if (!tx_skb)
828 return;
829
830
831 skb_reserve(tx_skb, LMP_MAX_HEADER);
832
833
834
835
836
837
838 spin_lock_irqsave(&self->lock, flags);
839
840 n = self->avail_credit;
841 self->avail_credit = 0;
842
843
844 if (n > 127) {
845 self->avail_credit = n - 127;
846 n = 127;
847 }
848 self->remote_credit += n;
849
850 spin_unlock_irqrestore(&self->lock, flags);
851
852 skb_put(tx_skb, 1);
853 tx_skb->data[0] = (__u8) (n & 0x7f);
854
855 irlmp_data_request(self->lsap, tx_skb);
856 self->stats.tx_packets++;
857}
858
859
860
861
862
863
864
865static int irttp_udata_indication(void *instance, void *sap,
866 struct sk_buff *skb)
867{
868 struct tsap_cb *self;
869 int err;
870
871 self = instance;
872
873 IRDA_ASSERT(self != NULL, return -1;);
874 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
875 IRDA_ASSERT(skb != NULL, return -1;);
876
877 self->stats.rx_packets++;
878
879
880 if (self->notify.udata_indication) {
881 err = self->notify.udata_indication(self->notify.instance,
882 self, skb);
883
884 if (!err)
885 return 0;
886 }
887
888 dev_kfree_skb(skb);
889
890 return 0;
891}
892
893
894
895
896
897
898
899static int irttp_data_indication(void *instance, void *sap,
900 struct sk_buff *skb)
901{
902 struct tsap_cb *self;
903 unsigned long flags;
904 int n;
905
906 self = instance;
907
908 n = skb->data[0] & 0x7f;
909
910 self->stats.rx_packets++;
911
912
913
914
915
916
917 spin_lock_irqsave(&self->lock, flags);
918 self->send_credit += n;
919 if (skb->len > 1)
920 self->remote_credit--;
921 spin_unlock_irqrestore(&self->lock, flags);
922
923
924
925
926
927 if (skb->len > 1) {
928
929
930
931
932 skb_queue_tail(&self->rx_queue, skb);
933 } else {
934
935 dev_kfree_skb(skb);
936 }
937
938
939
940
941
942
943
944
945
946
947
948
949
950 irttp_run_rx_queue(self);
951
952
953
954
955
956
957
958
959
960
961
962
963
964 if (self->send_credit == n) {
965
966 irttp_run_tx_queue(self);
967
968
969
970 }
971
972 return 0;
973}
974
975
976
977
978
979
980
981static void irttp_status_indication(void *instance,
982 LINK_STATUS link, LOCK_STATUS lock)
983{
984 struct tsap_cb *self;
985
986 self = instance;
987
988 IRDA_ASSERT(self != NULL, return;);
989 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
990
991
992 if (self->close_pend)
993 return;
994
995
996
997
998 if (self->notify.status_indication != NULL)
999 self->notify.status_indication(self->notify.instance,
1000 link, lock);
1001 else
1002 pr_debug("%s(), no handler\n", __func__);
1003}
1004
1005
1006
1007
1008
1009
1010
1011static void irttp_flow_indication(void *instance, void *sap, LOCAL_FLOW flow)
1012{
1013 struct tsap_cb *self;
1014
1015 self = instance;
1016
1017 IRDA_ASSERT(self != NULL, return;);
1018 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1019
1020 pr_debug("%s(instance=%p)\n", __func__, self);
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031 irttp_run_tx_queue(self);
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045 if (self->disconnect_pend)
1046 irttp_start_todo_timer(self, 0);
1047}
1048
1049
1050
1051
1052
1053
1054
1055
1056void irttp_flow_request(struct tsap_cb *self, LOCAL_FLOW flow)
1057{
1058 IRDA_ASSERT(self != NULL, return;);
1059 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1060
1061 switch (flow) {
1062 case FLOW_STOP:
1063 pr_debug("%s(), flow stop\n", __func__);
1064 self->rx_sdu_busy = TRUE;
1065 break;
1066 case FLOW_START:
1067 pr_debug("%s(), flow start\n", __func__);
1068 self->rx_sdu_busy = FALSE;
1069
1070
1071
1072 irttp_run_rx_queue(self);
1073
1074 break;
1075 default:
1076 pr_debug("%s(), Unknown flow command!\n", __func__);
1077 }
1078}
1079EXPORT_SYMBOL(irttp_flow_request);
1080
1081
1082
1083
1084
1085
1086
1087int irttp_connect_request(struct tsap_cb *self, __u8 dtsap_sel,
1088 __u32 saddr, __u32 daddr,
1089 struct qos_info *qos, __u32 max_sdu_size,
1090 struct sk_buff *userdata)
1091{
1092 struct sk_buff *tx_skb;
1093 __u8 *frame;
1094 __u8 n;
1095
1096 pr_debug("%s(), max_sdu_size=%d\n", __func__, max_sdu_size);
1097
1098 IRDA_ASSERT(self != NULL, return -EBADR;);
1099 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -EBADR;);
1100
1101 if (self->connected) {
1102 if (userdata)
1103 dev_kfree_skb(userdata);
1104 return -EISCONN;
1105 }
1106
1107
1108 if (userdata == NULL) {
1109 tx_skb = alloc_skb(TTP_MAX_HEADER + TTP_SAR_HEADER,
1110 GFP_ATOMIC);
1111 if (!tx_skb)
1112 return -ENOMEM;
1113
1114
1115 skb_reserve(tx_skb, TTP_MAX_HEADER + TTP_SAR_HEADER);
1116 } else {
1117 tx_skb = userdata;
1118
1119
1120
1121
1122 IRDA_ASSERT(skb_headroom(userdata) >= TTP_MAX_HEADER,
1123 { dev_kfree_skb(userdata); return -1; });
1124 }
1125
1126
1127 self->connected = FALSE;
1128 self->avail_credit = 0;
1129 self->rx_max_sdu_size = max_sdu_size;
1130 self->rx_sdu_size = 0;
1131 self->rx_sdu_busy = FALSE;
1132 self->dtsap_sel = dtsap_sel;
1133
1134 n = self->initial_credit;
1135
1136 self->remote_credit = 0;
1137 self->send_credit = 0;
1138
1139
1140
1141
1142 if (n > 127) {
1143 self->avail_credit = n - 127;
1144 n = 127;
1145 }
1146
1147 self->remote_credit = n;
1148
1149
1150 if (max_sdu_size > 0) {
1151 IRDA_ASSERT(skb_headroom(tx_skb) >= (TTP_MAX_HEADER + TTP_SAR_HEADER),
1152 { dev_kfree_skb(tx_skb); return -1; });
1153
1154
1155 frame = skb_push(tx_skb, TTP_HEADER + TTP_SAR_HEADER);
1156
1157 frame[0] = TTP_PARAMETERS | n;
1158 frame[1] = 0x04;
1159 frame[2] = 0x01;
1160 frame[3] = 0x02;
1161
1162 put_unaligned(cpu_to_be16((__u16) max_sdu_size),
1163 (__be16 *)(frame+4));
1164 } else {
1165
1166 frame = skb_push(tx_skb, TTP_HEADER);
1167
1168
1169 frame[0] = n & 0x7f;
1170 }
1171
1172
1173 return irlmp_connect_request(self->lsap, dtsap_sel, saddr, daddr, qos,
1174 tx_skb);
1175}
1176EXPORT_SYMBOL(irttp_connect_request);
1177
1178
1179
1180
1181
1182
1183
1184static void irttp_connect_confirm(void *instance, void *sap,
1185 struct qos_info *qos, __u32 max_seg_size,
1186 __u8 max_header_size, struct sk_buff *skb)
1187{
1188 struct tsap_cb *self;
1189 int parameters;
1190 int ret;
1191 __u8 plen;
1192 __u8 n;
1193
1194 self = instance;
1195
1196 IRDA_ASSERT(self != NULL, return;);
1197 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1198 IRDA_ASSERT(skb != NULL, return;);
1199
1200 self->max_seg_size = max_seg_size - TTP_HEADER;
1201 self->max_header_size = max_header_size + TTP_HEADER;
1202
1203
1204
1205
1206
1207 if (qos) {
1208 pr_debug("IrTTP, Negotiated BAUD_RATE: %02x\n",
1209 qos->baud_rate.bits);
1210 pr_debug("IrTTP, Negotiated BAUD_RATE: %d bps.\n",
1211 qos->baud_rate.value);
1212 }
1213
1214 n = skb->data[0] & 0x7f;
1215
1216 pr_debug("%s(), Initial send_credit=%d\n", __func__, n);
1217
1218 self->send_credit = n;
1219 self->tx_max_sdu_size = 0;
1220 self->connected = TRUE;
1221
1222 parameters = skb->data[0] & 0x80;
1223
1224 IRDA_ASSERT(skb->len >= TTP_HEADER, return;);
1225 skb_pull(skb, TTP_HEADER);
1226
1227 if (parameters) {
1228 plen = skb->data[0];
1229
1230 ret = irda_param_extract_all(self, skb->data+1,
1231 IRDA_MIN(skb->len-1, plen),
1232 ¶m_info);
1233
1234
1235 if (ret < 0) {
1236 net_warn_ratelimited("%s: error extracting parameters\n",
1237 __func__);
1238 dev_kfree_skb(skb);
1239
1240
1241 return;
1242 }
1243
1244 skb_pull(skb, IRDA_MIN(skb->len, plen+1));
1245 }
1246
1247 pr_debug("%s() send=%d,avail=%d,remote=%d\n", __func__,
1248 self->send_credit, self->avail_credit, self->remote_credit);
1249
1250 pr_debug("%s(), MaxSduSize=%d\n", __func__,
1251 self->tx_max_sdu_size);
1252
1253 if (self->notify.connect_confirm) {
1254 self->notify.connect_confirm(self->notify.instance, self, qos,
1255 self->tx_max_sdu_size,
1256 self->max_header_size, skb);
1257 } else
1258 dev_kfree_skb(skb);
1259}
1260
1261
1262
1263
1264
1265
1266
1267static void irttp_connect_indication(void *instance, void *sap,
1268 struct qos_info *qos, __u32 max_seg_size, __u8 max_header_size,
1269 struct sk_buff *skb)
1270{
1271 struct tsap_cb *self;
1272 struct lsap_cb *lsap;
1273 int parameters;
1274 int ret;
1275 __u8 plen;
1276 __u8 n;
1277
1278 self = instance;
1279
1280 IRDA_ASSERT(self != NULL, return;);
1281 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1282 IRDA_ASSERT(skb != NULL, return;);
1283
1284 lsap = sap;
1285
1286 self->max_seg_size = max_seg_size - TTP_HEADER;
1287 self->max_header_size = max_header_size+TTP_HEADER;
1288
1289 pr_debug("%s(), TSAP sel=%02x\n", __func__, self->stsap_sel);
1290
1291
1292 self->dtsap_sel = lsap->dlsap_sel;
1293
1294 n = skb->data[0] & 0x7f;
1295
1296 self->send_credit = n;
1297 self->tx_max_sdu_size = 0;
1298
1299 parameters = skb->data[0] & 0x80;
1300
1301 IRDA_ASSERT(skb->len >= TTP_HEADER, return;);
1302 skb_pull(skb, TTP_HEADER);
1303
1304 if (parameters) {
1305 plen = skb->data[0];
1306
1307 ret = irda_param_extract_all(self, skb->data+1,
1308 IRDA_MIN(skb->len-1, plen),
1309 ¶m_info);
1310
1311
1312 if (ret < 0) {
1313 net_warn_ratelimited("%s: error extracting parameters\n",
1314 __func__);
1315 dev_kfree_skb(skb);
1316
1317
1318 return;
1319 }
1320
1321
1322 skb_pull(skb, IRDA_MIN(skb->len, plen+1));
1323 }
1324
1325 if (self->notify.connect_indication) {
1326 self->notify.connect_indication(self->notify.instance, self,
1327 qos, self->tx_max_sdu_size,
1328 self->max_header_size, skb);
1329 } else
1330 dev_kfree_skb(skb);
1331}
1332
1333
1334
1335
1336
1337
1338
1339
1340int irttp_connect_response(struct tsap_cb *self, __u32 max_sdu_size,
1341 struct sk_buff *userdata)
1342{
1343 struct sk_buff *tx_skb;
1344 __u8 *frame;
1345 int ret;
1346 __u8 n;
1347
1348 IRDA_ASSERT(self != NULL, return -1;);
1349 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
1350
1351 pr_debug("%s(), Source TSAP selector=%02x\n", __func__,
1352 self->stsap_sel);
1353
1354
1355 if (userdata == NULL) {
1356 tx_skb = alloc_skb(TTP_MAX_HEADER + TTP_SAR_HEADER,
1357 GFP_ATOMIC);
1358 if (!tx_skb)
1359 return -ENOMEM;
1360
1361
1362 skb_reserve(tx_skb, TTP_MAX_HEADER + TTP_SAR_HEADER);
1363 } else {
1364 tx_skb = userdata;
1365
1366
1367
1368
1369 IRDA_ASSERT(skb_headroom(userdata) >= TTP_MAX_HEADER,
1370 { dev_kfree_skb(userdata); return -1; });
1371 }
1372
1373 self->avail_credit = 0;
1374 self->remote_credit = 0;
1375 self->rx_max_sdu_size = max_sdu_size;
1376 self->rx_sdu_size = 0;
1377 self->rx_sdu_busy = FALSE;
1378
1379 n = self->initial_credit;
1380
1381
1382 if (n > 127) {
1383 self->avail_credit = n - 127;
1384 n = 127;
1385 }
1386
1387 self->remote_credit = n;
1388 self->connected = TRUE;
1389
1390
1391 if (max_sdu_size > 0) {
1392 IRDA_ASSERT(skb_headroom(tx_skb) >= (TTP_MAX_HEADER + TTP_SAR_HEADER),
1393 { dev_kfree_skb(tx_skb); return -1; });
1394
1395
1396 frame = skb_push(tx_skb, TTP_HEADER + TTP_SAR_HEADER);
1397
1398 frame[0] = TTP_PARAMETERS | n;
1399 frame[1] = 0x04;
1400
1401
1402
1403
1404 frame[2] = 0x01;
1405 frame[3] = 0x02;
1406
1407 put_unaligned(cpu_to_be16((__u16) max_sdu_size),
1408 (__be16 *)(frame+4));
1409 } else {
1410
1411 frame = skb_push(tx_skb, TTP_HEADER);
1412
1413 frame[0] = n & 0x7f;
1414 }
1415
1416 ret = irlmp_connect_response(self->lsap, tx_skb);
1417
1418 return ret;
1419}
1420EXPORT_SYMBOL(irttp_connect_response);
1421
1422
1423
1424
1425
1426
1427
1428struct tsap_cb *irttp_dup(struct tsap_cb *orig, void *instance)
1429{
1430 struct tsap_cb *new;
1431 unsigned long flags;
1432
1433
1434 spin_lock_irqsave(&irttp->tsaps->hb_spinlock, flags);
1435
1436
1437 if (!hashbin_find(irttp->tsaps, (long) orig, NULL)) {
1438 pr_debug("%s(), unable to find TSAP\n", __func__);
1439 spin_unlock_irqrestore(&irttp->tsaps->hb_spinlock, flags);
1440 return NULL;
1441 }
1442
1443
1444 new = kmemdup(orig, sizeof(struct tsap_cb), GFP_ATOMIC);
1445 if (!new) {
1446 pr_debug("%s(), unable to kmalloc\n", __func__);
1447 spin_unlock_irqrestore(&irttp->tsaps->hb_spinlock, flags);
1448 return NULL;
1449 }
1450 spin_lock_init(&new->lock);
1451
1452
1453 spin_unlock_irqrestore(&irttp->tsaps->hb_spinlock, flags);
1454
1455
1456 new->lsap = irlmp_dup(orig->lsap, new);
1457 if (!new->lsap) {
1458 pr_debug("%s(), dup failed!\n", __func__);
1459 kfree(new);
1460 return NULL;
1461 }
1462
1463
1464 new->notify.instance = instance;
1465
1466
1467 irttp_init_tsap(new);
1468
1469
1470 hashbin_insert(irttp->tsaps, (irda_queue_t *) new, (long) new, NULL);
1471
1472 return new;
1473}
1474EXPORT_SYMBOL(irttp_dup);
1475
1476
1477
1478
1479
1480
1481
1482
1483int irttp_disconnect_request(struct tsap_cb *self, struct sk_buff *userdata,
1484 int priority)
1485{
1486 int ret;
1487
1488 IRDA_ASSERT(self != NULL, return -1;);
1489 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
1490
1491
1492 if (!self->connected) {
1493 pr_debug("%s(), already disconnected!\n", __func__);
1494 if (userdata)
1495 dev_kfree_skb(userdata);
1496 return -1;
1497 }
1498
1499
1500
1501
1502
1503
1504 if (test_and_set_bit(0, &self->disconnect_pend)) {
1505 pr_debug("%s(), disconnect already pending\n",
1506 __func__);
1507 if (userdata)
1508 dev_kfree_skb(userdata);
1509
1510
1511 irttp_run_tx_queue(self);
1512 return -1;
1513 }
1514
1515
1516
1517
1518 if (!skb_queue_empty(&self->tx_queue)) {
1519 if (priority == P_HIGH) {
1520
1521
1522
1523
1524
1525 pr_debug("%s(): High priority!!()\n", __func__);
1526 irttp_flush_queues(self);
1527 } else if (priority == P_NORMAL) {
1528
1529
1530
1531
1532
1533 self->disconnect_skb = userdata;
1534
1535 irttp_run_tx_queue(self);
1536
1537 irttp_start_todo_timer(self, HZ/10);
1538 return -1;
1539 }
1540 }
1541
1542
1543
1544
1545
1546 pr_debug("%s(), Disconnecting ...\n", __func__);
1547 self->connected = FALSE;
1548
1549 if (!userdata) {
1550 struct sk_buff *tx_skb;
1551 tx_skb = alloc_skb(LMP_MAX_HEADER, GFP_ATOMIC);
1552 if (!tx_skb)
1553 return -ENOMEM;
1554
1555
1556
1557
1558 skb_reserve(tx_skb, LMP_MAX_HEADER);
1559
1560 userdata = tx_skb;
1561 }
1562 ret = irlmp_disconnect_request(self->lsap, userdata);
1563
1564
1565 clear_bit(0, &self->disconnect_pend);
1566
1567 return ret;
1568}
1569EXPORT_SYMBOL(irttp_disconnect_request);
1570
1571
1572
1573
1574
1575
1576
1577static void irttp_disconnect_indication(void *instance, void *sap,
1578 LM_REASON reason, struct sk_buff *skb)
1579{
1580 struct tsap_cb *self;
1581
1582 self = instance;
1583
1584 IRDA_ASSERT(self != NULL, return;);
1585 IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1586
1587
1588 self->connected = FALSE;
1589
1590
1591 if (self->close_pend) {
1592
1593
1594 if (skb)
1595 dev_kfree_skb(skb);
1596 irttp_close_tsap(self);
1597 return;
1598 }
1599
1600
1601
1602
1603
1604
1605
1606
1607 if (self->notify.disconnect_indication)
1608 self->notify.disconnect_indication(self->notify.instance, self,
1609 reason, skb);
1610 else
1611 if (skb)
1612 dev_kfree_skb(skb);
1613}
1614
1615
1616
1617
1618
1619
1620
1621
1622static void irttp_do_data_indication(struct tsap_cb *self, struct sk_buff *skb)
1623{
1624 int err;
1625
1626
1627 if (self->close_pend) {
1628 dev_kfree_skb(skb);
1629 return;
1630 }
1631
1632 err = self->notify.data_indication(self->notify.instance, self, skb);
1633
1634
1635
1636
1637
1638
1639 if (err) {
1640 pr_debug("%s() requeueing skb!\n", __func__);
1641
1642
1643 self->rx_sdu_busy = TRUE;
1644
1645
1646 skb_push(skb, TTP_HEADER);
1647 skb->data[0] = 0x00;
1648
1649
1650 skb_queue_head(&self->rx_queue, skb);
1651 }
1652}
1653
1654
1655
1656
1657
1658
1659
1660static void irttp_run_rx_queue(struct tsap_cb *self)
1661{
1662 struct sk_buff *skb;
1663 int more = 0;
1664
1665 pr_debug("%s() send=%d,avail=%d,remote=%d\n", __func__,
1666 self->send_credit, self->avail_credit, self->remote_credit);
1667
1668
1669 if (irda_lock(&self->rx_queue_lock) == FALSE)
1670 return;
1671
1672
1673
1674
1675 while (!self->rx_sdu_busy && (skb = skb_dequeue(&self->rx_queue))) {
1676
1677 more = skb->data[0] & 0x80;
1678
1679
1680 skb_pull(skb, TTP_HEADER);
1681
1682
1683 self->rx_sdu_size += skb->len;
1684
1685
1686
1687
1688
1689
1690
1691 if (self->rx_max_sdu_size == TTP_SAR_DISABLE) {
1692 irttp_do_data_indication(self, skb);
1693 self->rx_sdu_size = 0;
1694
1695 continue;
1696 }
1697
1698
1699 if (more) {
1700
1701
1702
1703
1704 if (self->rx_sdu_size <= self->rx_max_sdu_size) {
1705 pr_debug("%s(), queueing frag\n",
1706 __func__);
1707 skb_queue_tail(&self->rx_fragments, skb);
1708 } else {
1709
1710 dev_kfree_skb(skb);
1711 }
1712 continue;
1713 }
1714
1715
1716
1717 if ((self->rx_sdu_size <= self->rx_max_sdu_size) ||
1718 (self->rx_max_sdu_size == TTP_SAR_UNBOUND)) {
1719
1720
1721
1722
1723
1724
1725 if (!skb_queue_empty(&self->rx_fragments)) {
1726 skb_queue_tail(&self->rx_fragments,
1727 skb);
1728
1729 skb = irttp_reassemble_skb(self);
1730 }
1731
1732
1733 irttp_do_data_indication(self, skb);
1734 } else {
1735 pr_debug("%s(), Truncated frame\n", __func__);
1736
1737
1738 dev_kfree_skb(skb);
1739
1740
1741 skb = irttp_reassemble_skb(self);
1742
1743 irttp_do_data_indication(self, skb);
1744 }
1745 self->rx_sdu_size = 0;
1746 }
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759 self->avail_credit = (self->initial_credit -
1760 (self->remote_credit +
1761 skb_queue_len(&self->rx_queue) +
1762 skb_queue_len(&self->rx_fragments)));
1763
1764
1765 if ((self->remote_credit <= TTP_RX_MIN_CREDIT) &&
1766 (self->avail_credit > 0)) {
1767
1768 irttp_give_credit(self);
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783 }
1784
1785
1786 self->rx_queue_lock = 0;
1787}
1788
1789#ifdef CONFIG_PROC_FS
1790struct irttp_iter_state {
1791 int id;
1792};
1793
1794static void *irttp_seq_start(struct seq_file *seq, loff_t *pos)
1795{
1796 struct irttp_iter_state *iter = seq->private;
1797 struct tsap_cb *self;
1798
1799
1800 spin_lock_irq(&irttp->tsaps->hb_spinlock);
1801 iter->id = 0;
1802
1803 for (self = (struct tsap_cb *) hashbin_get_first(irttp->tsaps);
1804 self != NULL;
1805 self = (struct tsap_cb *) hashbin_get_next(irttp->tsaps)) {
1806 if (iter->id == *pos)
1807 break;
1808 ++iter->id;
1809 }
1810
1811 return self;
1812}
1813
1814static void *irttp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1815{
1816 struct irttp_iter_state *iter = seq->private;
1817
1818 ++*pos;
1819 ++iter->id;
1820 return (void *) hashbin_get_next(irttp->tsaps);
1821}
1822
1823static void irttp_seq_stop(struct seq_file *seq, void *v)
1824{
1825 spin_unlock_irq(&irttp->tsaps->hb_spinlock);
1826}
1827
1828static int irttp_seq_show(struct seq_file *seq, void *v)
1829{
1830 const struct irttp_iter_state *iter = seq->private;
1831 const struct tsap_cb *self = v;
1832
1833 seq_printf(seq, "TSAP %d, ", iter->id);
1834 seq_printf(seq, "stsap_sel: %02x, ",
1835 self->stsap_sel);
1836 seq_printf(seq, "dtsap_sel: %02x\n",
1837 self->dtsap_sel);
1838 seq_printf(seq, " connected: %s, ",
1839 self->connected ? "TRUE" : "FALSE");
1840 seq_printf(seq, "avail credit: %d, ",
1841 self->avail_credit);
1842 seq_printf(seq, "remote credit: %d, ",
1843 self->remote_credit);
1844 seq_printf(seq, "send credit: %d\n",
1845 self->send_credit);
1846 seq_printf(seq, " tx packets: %lu, ",
1847 self->stats.tx_packets);
1848 seq_printf(seq, "rx packets: %lu, ",
1849 self->stats.rx_packets);
1850 seq_printf(seq, "tx_queue len: %u ",
1851 skb_queue_len(&self->tx_queue));
1852 seq_printf(seq, "rx_queue len: %u\n",
1853 skb_queue_len(&self->rx_queue));
1854 seq_printf(seq, " tx_sdu_busy: %s, ",
1855 self->tx_sdu_busy ? "TRUE" : "FALSE");
1856 seq_printf(seq, "rx_sdu_busy: %s\n",
1857 self->rx_sdu_busy ? "TRUE" : "FALSE");
1858 seq_printf(seq, " max_seg_size: %u, ",
1859 self->max_seg_size);
1860 seq_printf(seq, "tx_max_sdu_size: %u, ",
1861 self->tx_max_sdu_size);
1862 seq_printf(seq, "rx_max_sdu_size: %u\n",
1863 self->rx_max_sdu_size);
1864
1865 seq_printf(seq, " Used by (%s)\n\n",
1866 self->notify.name);
1867 return 0;
1868}
1869
1870static const struct seq_operations irttp_seq_ops = {
1871 .start = irttp_seq_start,
1872 .next = irttp_seq_next,
1873 .stop = irttp_seq_stop,
1874 .show = irttp_seq_show,
1875};
1876
1877static int irttp_seq_open(struct inode *inode, struct file *file)
1878{
1879 return seq_open_private(file, &irttp_seq_ops,
1880 sizeof(struct irttp_iter_state));
1881}
1882
1883const struct file_operations irttp_seq_fops = {
1884 .owner = THIS_MODULE,
1885 .open = irttp_seq_open,
1886 .read = seq_read,
1887 .llseek = seq_lseek,
1888 .release = seq_release_private,
1889};
1890
1891#endif
1892