31 #include <linux/slab.h>
32 #include <linux/export.h>
34 #include <asm/byteorder.h>
35 #include <asm/unaligned.h>
45 static void __irttp_close_tsap(
struct tsap_cb *
self);
47 static int irttp_data_indication(
void *instance,
void *sap,
49 static int irttp_udata_indication(
void *instance,
void *sap,
51 static void irttp_disconnect_indication(
void *instance,
void *sap,
53 static void irttp_connect_indication(
void *instance,
void *sap,
56 static void irttp_connect_confirm(
void *instance,
void *sap,
59 static void irttp_run_tx_queue(
struct tsap_cb *
self);
60 static void irttp_run_rx_queue(
struct tsap_cb *
self);
62 static void irttp_flush_queues(
struct tsap_cb *
self);
65 static void irttp_todo_expired(
unsigned long data);
69 static void irttp_flow_indication(
void *instance,
void *sap,
LOCAL_FLOW flow);
70 static void irttp_status_indication(
void *instance,
78 static pi_major_info_t pi_major_call_table[] = {{ pi_minor_call_table, 2 }};
79 static pi_param_info_t param_info = { pi_major_call_table, 1, 0x0f, 4 };
99 IRDA_ERROR(
"%s: can't allocate IrTTP hashbin!\n",
141 static inline void irttp_start_todo_timer(
struct tsap_cb *
self,
int timeout)
144 mod_timer(&self->todo_timer, jiffies + timeout);
161 static void irttp_todo_expired(
unsigned long data)
169 IRDA_DEBUG(4,
"%s(instance=%p)\n", __func__,
self);
172 irttp_run_rx_queue(
self);
173 irttp_run_tx_queue(
self);
176 if (
test_bit(0, &self->disconnect_pend)) {
178 if (skb_queue_empty(&self->tx_queue)) {
185 self->disconnect_skb =
NULL;
188 irttp_start_todo_timer(
self,
HZ/10);
196 if (self->close_pend)
206 static void irttp_flush_queues(
struct tsap_cb *
self)
243 IRDA_DEBUG(2,
"%s(), self->rx_sdu_size=%d\n", __func__,
246 skb = dev_alloc_skb(
TTP_HEADER + self->rx_sdu_size);
255 skb_put(skb, self->rx_sdu_size);
261 skb_copy_to_linear_data_offset(skb, n, frag->
data, frag->
len);
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);
276 IRDA_ASSERT(n <= self->rx_sdu_size, n = self->rx_sdu_size;);
281 self->rx_sdu_size = 0;
292 static inline void irttp_fragment_skb(
struct tsap_cb *
self,
307 while (skb->
len > self->max_seg_size) {
308 IRDA_DEBUG(2,
"%s(), fragmenting ...\n", __func__);
311 frag = alloc_skb(self->max_seg_size+self->max_header_size,
316 skb_reserve(frag, self->max_header_size);
319 skb_copy_from_linear_data(skb,
skb_put(frag, self->max_seg_size),
333 IRDA_DEBUG(2,
"%s(), queuing last segment\n", __func__);
360 param->
pv.
i =
self->tx_max_sdu_size;
362 self->tx_max_sdu_size = param->
pv.
i;
364 IRDA_DEBUG(1,
"%s(), MaxSduSize=%d\n", __func__, param->
pv.
i);
377 static void irttp_init_tsap(
struct tsap_cb *tsap)
382 skb_queue_head_init(&tsap->
rx_queue);
383 skb_queue_head_init(&tsap->
tx_queue);
404 ((stsap_sel < 0x01) || (stsap_sel >= 0x70))) {
405 IRDA_DEBUG(0,
"%s(), invalid tsap!\n", __func__);
411 IRDA_DEBUG(0,
"%s(), unable to kmalloc!\n", __func__);
416 irttp_init_tsap(
self);
419 self->todo_timer.data = (
unsigned long)
self;
420 self->todo_timer.function = &irttp_todo_expired;
436 self->connected =
FALSE;
443 IRDA_DEBUG(0,
"%s: unable to allocate LSAP!!\n", __func__);
444 __irttp_close_tsap(
self);
454 IRDA_DEBUG(4,
"%s(), stsap_sel=%02x\n", __func__, self->stsap_sel);
464 self->initial_credit = credit;
477 static void __irttp_close_tsap(
struct tsap_cb *
self)
483 irttp_flush_queues(
self);
489 if (self->disconnect_skb)
490 dev_kfree_skb(self->disconnect_skb);
492 self->connected =
FALSE;
518 if (self->connected) {
520 if (!
test_bit(0, &self->disconnect_pend)) {
525 self->close_pend =
TRUE;
526 irttp_start_todo_timer(
self,
HZ/10);
541 __irttp_close_tsap(
self);
570 if (!self->connected) {
576 if (skb->
len > self->max_seg_size) {
577 IRDA_ERROR(
"%s(), UData is too large for IrLAP!\n", __func__);
583 self->stats.tx_packets++;
609 IRDA_DEBUG(2,
"%s() : queue len = %d\n", __func__,
610 skb_queue_len(&self->tx_queue));
619 if (!self->connected) {
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",
640 if ((self->tx_max_sdu_size != 0) &&
642 (skb->
len > self->tx_max_sdu_size))
644 IRDA_ERROR(
"%s: SAR enabled, but data is larger than TxMaxSduSize!\n",
656 irttp_run_tx_queue(
self);
665 if ((self->tx_max_sdu_size == 0) || (skb->
len < self->max_seg_size)) {
679 irttp_fragment_skb(
self, skb);
683 if ((!self->tx_sdu_busy) &&
686 if (self->notify.flow_indication) {
687 self->notify.flow_indication(self->notify.instance,
697 self->tx_sdu_busy =
TRUE;
701 irttp_run_tx_queue(
self);
717 static void irttp_run_tx_queue(
struct tsap_cb *
self)
723 IRDA_DEBUG(2,
"%s() : send_credit = %d, queue_len = %d\n",
725 self->send_credit, skb_queue_len(&self->tx_queue));
734 while ((self->send_credit > 0) &&
735 (!irlmp_lap_tx_queue_full(self->lsap)) &&
745 n =
self->avail_credit;
746 self->avail_credit = 0;
750 self->avail_credit = n-127;
753 self->remote_credit +=
n;
756 spin_unlock_irqrestore(&self->lock, flags);
762 skb->
data[0] |= (n & 0x7f);
790 self->stats.tx_packets++;
799 if ((self->tx_sdu_busy) &&
803 if (self->notify.flow_indication)
804 self->notify.flow_indication(self->notify.instance,
810 self->tx_sdu_busy =
FALSE;
814 self->tx_queue_lock = 0;
823 static inline void irttp_give_credit(
struct tsap_cb *
self)
832 IRDA_DEBUG(4,
"%s() send=%d,avail=%d,remote=%d\n",
834 self->send_credit, self->avail_credit, self->remote_credit);
851 n =
self->avail_credit;
852 self->avail_credit = 0;
856 self->avail_credit = n - 127;
859 self->remote_credit +=
n;
861 spin_unlock_irqrestore(&self->lock, flags);
864 tx_skb->
data[0] = (
__u8) (n & 0x7f);
867 self->stats.tx_packets++;
876 static int irttp_udata_indication(
void *instance,
void *sap,
890 self->stats.rx_packets++;
893 if (self->notify.udata_indication) {
894 err =
self->notify.udata_indication(self->notify.instance,
912 static int irttp_data_indication(
void *instance,
void *sap,
921 n = skb->
data[0] & 0x7f;
923 self->stats.rx_packets++;
931 self->send_credit +=
n;
933 self->remote_credit--;
934 spin_unlock_irqrestore(&self->lock, flags);
963 irttp_run_rx_queue(
self);
977 if (self->send_credit == n) {
979 irttp_run_tx_queue(
self);
994 static void irttp_status_indication(
void *instance,
1007 if (self->close_pend)
1013 if (self->notify.status_indication !=
NULL)
1014 self->notify.status_indication(self->notify.instance,
1017 IRDA_DEBUG(2,
"%s(), no handler\n", __func__);
1026 static void irttp_flow_indication(
void *instance,
void *sap,
LOCAL_FLOW flow)
1035 IRDA_DEBUG(4,
"%s(instance=%p)\n", __func__,
self);
1046 irttp_run_tx_queue(
self);
1060 if(self->disconnect_pend)
1061 irttp_start_todo_timer(
self, 0);
1080 IRDA_DEBUG(1,
"%s(), flow stop\n", __func__);
1081 self->rx_sdu_busy =
TRUE;
1084 IRDA_DEBUG(1,
"%s(), flow start\n", __func__);
1085 self->rx_sdu_busy =
FALSE;
1089 irttp_run_rx_queue(
self);
1093 IRDA_DEBUG(1,
"%s(), Unknown flow command!\n", __func__);
1113 IRDA_DEBUG(4,
"%s(), max_sdu_size=%d\n", __func__, max_sdu_size);
1118 if (self->connected) {
1120 dev_kfree_skb(userdata);
1125 if (userdata ==
NULL) {
1140 { dev_kfree_skb(userdata);
return -1; } );
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;
1151 n =
self->initial_credit;
1153 self->remote_credit = 0;
1154 self->send_credit = 0;
1160 self->avail_credit=n-127;
1164 self->remote_credit =
n;
1167 if (max_sdu_size > 0) {
1169 { dev_kfree_skb(tx_skb);
return -1; } );
1186 frame[0] = n & 0x7f;
1201 static void irttp_connect_confirm(
void *instance,
void *sap,
1219 self->max_seg_size = max_seg_size -
TTP_HEADER;
1220 self->max_header_size = max_header_size +
TTP_HEADER;
1227 IRDA_DEBUG(4,
"IrTTP, Negotiated BAUD_RATE: %02x\n",
1229 IRDA_DEBUG(4,
"IrTTP, Negotiated BAUD_RATE: %d bps.\n",
1233 n = skb->
data[0] & 0x7f;
1235 IRDA_DEBUG(4,
"%s(), Initial send_credit=%d\n", __func__, n);
1237 self->send_credit =
n;
1238 self->tx_max_sdu_size = 0;
1239 self->connected =
TRUE;
1241 parameters = skb->
data[0] & 0x80;
1247 plen = skb->
data[0];
1266 IRDA_DEBUG(4,
"%s() send=%d,avail=%d,remote=%d\n", __func__,
1267 self->send_credit, self->avail_credit, self->remote_credit);
1269 IRDA_DEBUG(2,
"%s(), MaxSduSize=%d\n", __func__,
1270 self->tx_max_sdu_size);
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);
1286 static void irttp_connect_indication(
void *instance,
void *sap,
1305 self->max_seg_size = max_seg_size -
TTP_HEADER;
1306 self->max_header_size = max_header_size+
TTP_HEADER;
1308 IRDA_DEBUG(4,
"%s(), TSAP sel=%02x\n", __func__, self->stsap_sel);
1311 self->dtsap_sel = lsap->dlsap_sel;
1313 n = skb->
data[0] & 0x7f;
1315 self->send_credit =
n;
1316 self->tx_max_sdu_size = 0;
1318 parameters = skb->
data[0] & 0x80;
1324 plen = skb->
data[0];
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);
1370 IRDA_DEBUG(4,
"%s(), Source TSAP selector=%02x\n", __func__,
1374 if (userdata ==
NULL) {
1389 { dev_kfree_skb(userdata);
return -1; } );
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;
1398 n =
self->initial_credit;
1402 self->avail_credit = n - 127;
1406 self->remote_credit =
n;
1407 self->connected =
TRUE;
1410 if (max_sdu_size > 0) {
1412 { dev_kfree_skb(tx_skb);
return -1; } );
1430 frame =
skb_push(tx_skb, TTP_HEADER);
1432 frame[0] = n & 0x7f;
1450 unsigned long flags;
1459 IRDA_DEBUG(0,
"%s(), unable to find TSAP\n", __func__);
1467 IRDA_DEBUG(0,
"%s(), unable to kmalloc\n", __func__);
1479 IRDA_DEBUG(0,
"%s(), dup failed!\n", __func__);
1485 new->notify.instance = instance;
1488 irttp_init_tsap(
new);
1513 if (!self->connected) {
1514 IRDA_DEBUG(4,
"%s(), already disconnected!\n", __func__);
1516 dev_kfree_skb(userdata);
1526 IRDA_DEBUG(0,
"%s(), disconnect already pending\n",
1529 dev_kfree_skb(userdata);
1532 irttp_run_tx_queue(
self);
1539 if (!skb_queue_empty(&self->tx_queue)) {
1540 if (priority ==
P_HIGH) {
1546 IRDA_DEBUG(1,
"%s(): High priority!!()\n", __func__);
1547 irttp_flush_queues(
self);
1554 self->disconnect_skb = userdata;
1556 irttp_run_tx_queue(
self);
1558 irttp_start_todo_timer(
self,
HZ/10);
1567 IRDA_DEBUG(1,
"%s(), Disconnecting ...\n", __func__);
1568 self->connected =
FALSE;
1598 static void irttp_disconnect_indication(
void *instance,
void *sap,
1611 self->connected =
FALSE;
1614 if (self->close_pend) {
1630 if(self->notify.disconnect_indication)
1631 self->notify.disconnect_indication(self->notify.instance,
self,
1645 static void irttp_do_data_indication(
struct tsap_cb *
self,
struct sk_buff *skb)
1650 if (self->close_pend) {
1655 err =
self->notify.data_indication(self->notify.instance,
self, skb);
1663 IRDA_DEBUG(0,
"%s() requeueing skb!\n", __func__);
1666 self->rx_sdu_busy =
TRUE;
1670 skb->
data[0] = 0x00;
1683 static void irttp_run_rx_queue(
struct tsap_cb *
self)
1688 IRDA_DEBUG(2,
"%s() send=%d,avail=%d,remote=%d\n", __func__,
1689 self->send_credit, self->avail_credit, self->remote_credit);
1698 while (!self->rx_sdu_busy && (skb =
skb_dequeue(&self->rx_queue))) {
1700 more = skb->
data[0] & 0x80;
1706 self->rx_sdu_size += skb->
len;
1715 irttp_do_data_indication(
self, skb);
1716 self->rx_sdu_size = 0;
1727 if (self->rx_sdu_size <= self->rx_max_sdu_size) {
1740 if ((self->rx_sdu_size <= self->rx_max_sdu_size) ||
1749 if (!skb_queue_empty(&self->rx_fragments)) {
1753 skb = irttp_reassemble_skb(
self);
1757 irttp_do_data_indication(
self, skb);
1759 IRDA_DEBUG(1,
"%s(), Truncated frame\n", __func__);
1765 skb = irttp_reassemble_skb(
self);
1767 irttp_do_data_indication(
self, skb);
1769 self->rx_sdu_size = 0;
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)));
1790 (
self->avail_credit > 0)) {
1792 irttp_give_credit(
self);
1810 self->rx_queue_lock = 0;
1813 #ifdef CONFIG_PROC_FS
1814 struct irttp_iter_state {
1818 static void *irttp_seq_start(
struct seq_file *seq, loff_t *
pos)
1820 struct irttp_iter_state *iter = seq->
private;
1830 if (iter->id == *pos)
1838 static void *irttp_seq_next(
struct seq_file *seq,
void *
v, loff_t *pos)
1840 struct irttp_iter_state *iter = seq->
private;
1847 static void irttp_seq_stop(
struct seq_file *seq,
void *v)
1852 static int irttp_seq_show(
struct seq_file *seq,
void *v)
1854 const struct irttp_iter_state *iter = seq->
private;
1863 self->connected?
"TRUE":
"FALSE");
1865 self->avail_credit);
1867 self->remote_credit);
1871 self->stats.tx_packets);
1873 self->stats.rx_packets);
1875 skb_queue_len(&self->tx_queue));
1877 skb_queue_len(&self->rx_queue));
1879 self->tx_sdu_busy?
"TRUE":
"FALSE");
1881 self->rx_sdu_busy?
"TRUE":
"FALSE");
1883 self->max_seg_size);
1885 self->tx_max_sdu_size);
1887 self->rx_max_sdu_size);
1895 .
start = irttp_seq_start,
1896 .next = irttp_seq_next,
1897 .stop = irttp_seq_stop,
1898 .show = irttp_seq_show,
1904 sizeof(
struct irttp_iter_state));
1909 .open = irttp_seq_open,