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