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