1 /*********************************************************************
3 * Filename: irlap_frame.c
5 * Description: Build and transmit IrLAP frames
6 * Status: Experimental.
7 * Author: Dag Brattli <dagb@cs.uit.no>
8 * Created at: Tue Aug 19 10:27:26 1997
9 * Modified at: Mon May 31 09:29:13 1999
10 * Modified by: Dag Brattli <dagb@cs.uit.no>
12 * Copyright (c) 1998-1999 Dag Brattli <dagb@cs.uit.no>, All Rights Resrved.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
19 * Neither Dag Brattli nor University of Tromsø admit liability nor
20 * provide warranty for any of this software. This material is
21 * provided "AS-IS" and at no charge.
23 ********************************************************************/
25 #include <linux/skbuff.h>
27 #include <linux/if_ether.h>
28 #include <linux/netdevice.h>
29 #include <linux/irda.h>
31 #include <net/pkt_sched.h>
34 #include <asm/byteorder.h>
36 #include <net/irda/irda.h>
37 #include <net/irda/irda_device.h>
38 #include <net/irda/irlap.h>
39 #include <net/irda/wrapper.h>
40 #include <net/irda/timer.h>
41 #include <net/irda/irlap_frame.h>
42 #include <net/irda/qos.h>
45 * Function irlap_insert_mtt (self, skb)
47 * Insert minimum turnaround time relevant information into the skb. We
48 * need to do this since it's per packet relevant information.
51 static inline void irlap_insert_mtt(struct irlap_cb
*self
, struct sk_buff
*skb
)
53 struct irlap_skb_cb
*cb
;
55 cb
= (struct irlap_skb_cb
*) skb
->cb
;
57 cb
->magic
= LAP_MAGIC
;
58 cb
->mtt
= self
->mtt_required
;
61 self
->mtt_required
= 0;
64 * Delay equals negotiated BOFs count plus the number of BOFs to
65 * force the negotiated minimum turnaround time
67 cb
->xbofs
= self
->bofs_count
+self
->xbofs_delay
;
69 /* Reset XBOF's delay (used only for getting min turn time) */
70 self
->xbofs_delay
= 0;
74 * Function irlap_queue_xmit (self, skb)
76 * A little wrapper for dev_queue_xmit, so we can insert some common
79 void irlap_queue_xmit(struct irlap_cb
*self
, struct sk_buff
*skb
)
81 /* Some common init stuff */
82 skb
->dev
= self
->netdev
;
83 skb
->h
.raw
= skb
->nh
.raw
= skb
->mac
.raw
= skb
->data
;
84 skb
->protocol
= htons(ETH_P_IRDA
);
85 skb
->priority
= TC_PRIO_BESTEFFORT
;
88 * Insert MTT (min. turn time) into skb, so that the device driver
89 * knows which MTT to use
91 irlap_insert_mtt(self
, skb
);
94 self
->stats
.tx_packets
++;
98 * Function irlap_send_snrm_cmd (void)
100 * Transmits a connect SNRM command frame
102 void irlap_send_snrm_frame(struct irlap_cb
*self
, struct qos_info
*qos
)
105 struct snrm_frame
*frame
;
108 ASSERT(self
!= NULL
, return;);
109 ASSERT(self
->magic
== LAP_MAGIC
, return;);
112 skb
= dev_alloc_skb(64);
117 frame
= (struct snrm_frame
*) skb
->data
;
119 /* Insert connection address field */
121 frame
->caddr
= CMD_FRAME
| CBROADCAST
;
123 frame
->caddr
= CMD_FRAME
| self
->caddr
;
125 /* Insert control field */
126 frame
->control
= SNRM_CMD
| PF_BIT
;
129 * If we are establishing a connection then insert QoS paramerters
132 skb_put(skb
, 9); /* 21 left */
133 frame
->saddr
= cpu_to_le32(self
->saddr
);
134 frame
->daddr
= cpu_to_le32(self
->daddr
);
136 frame
->ncaddr
= self
->caddr
;
138 len
= irda_insert_qos_negotiation_params(qos
, frame
->params
);
139 /* Should not be dangerous to do this afterwards */
142 irlap_queue_xmit(self
, skb
);
146 * Function irlap_recv_snrm_cmd (skb, info)
148 * Received SNRM (Set Normal Response Mode) command frame
151 static void irlap_recv_snrm_cmd(struct irlap_cb
*self
, struct sk_buff
*skb
,
152 struct irlap_info
*info
)
154 struct snrm_frame
*frame
;
156 DEBUG(3, __FUNCTION__
"()\n");
158 ASSERT(skb
!= NULL
, return;);
159 ASSERT(info
!= NULL
, return;);
161 frame
= (struct snrm_frame
*) skb
->data
;
163 /* Copy peer device address */
164 info
->daddr
= le32_to_cpu(frame
->saddr
);
166 /* Copy connection address */
167 info
->caddr
= frame
->ncaddr
;
169 /* Check if connection address has got a valid value */
170 if ((info
->caddr
== 0x00) || (info
->caddr
== 0xfe)) {
171 DEBUG(3, __FUNCTION__
"(), invalid connection address!\n");
176 irlap_do_event(self
, RECV_SNRM_CMD
, skb
, info
);
180 * Function irlap_send_ua_response_frame (qos)
182 * Send UA (Unnumbered Acknowledgement) frame
185 void irlap_send_ua_response_frame(struct irlap_cb
*self
, struct qos_info
*qos
)
188 struct ua_frame
*frame
;
191 DEBUG(2, __FUNCTION__
"() <%ld>\n", jiffies
);
193 ASSERT(self
!= NULL
, return;);
194 ASSERT(self
->magic
== LAP_MAGIC
, return;);
199 skb
= dev_alloc_skb(64);
204 frame
= (struct ua_frame
*) skb
->data
;
206 /* Build UA response */
207 frame
->caddr
= self
->caddr
;
208 frame
->control
= UA_RSP
| PF_BIT
;
210 frame
->saddr
= cpu_to_le32(self
->saddr
);
211 frame
->daddr
= cpu_to_le32(self
->daddr
);
213 /* Should we send QoS negotiation parameters? */
215 len
= irda_insert_qos_negotiation_params(qos
, frame
->params
);
219 irlap_queue_xmit(self
, skb
);
224 * Function irlap_send_dm_frame (void)
226 * Send disconnected mode (DM) frame
229 void irlap_send_dm_frame( struct irlap_cb
*self
)
231 struct sk_buff
*skb
= NULL
;
234 ASSERT(self
!= NULL
, return;);
235 ASSERT(self
->magic
== LAP_MAGIC
, return;);
237 skb
= dev_alloc_skb(32);
244 if (self
->state
== LAP_NDM
)
245 frame
[0] = CBROADCAST
;
247 frame
[0] = self
->caddr
;
249 frame
[1] = DM_RSP
| PF_BIT
;
251 irlap_queue_xmit(self
, skb
);
255 * Function irlap_send_disc_frame (void)
257 * Send disconnect (DISC) frame
260 void irlap_send_disc_frame(struct irlap_cb
*self
)
262 struct sk_buff
*skb
= NULL
;
265 DEBUG(3, __FUNCTION__
"()\n");
267 ASSERT(self
!= NULL
, return;);
268 ASSERT(self
->magic
== LAP_MAGIC
, return;);
270 skb
= dev_alloc_skb(32);
277 frame
[0] = self
->caddr
| CMD_FRAME
;
278 frame
[1] = DISC_CMD
| PF_BIT
;
280 irlap_queue_xmit(self
, skb
);
284 * Function irlap_send_discovery_xid_frame (S, s, command)
286 * Build and transmit a XID (eXchange station IDentifier) discovery
289 void irlap_send_discovery_xid_frame(struct irlap_cb
*self
, int S
, __u8 s
,
290 __u8 command
, discovery_t
*discovery
)
292 struct sk_buff
*skb
= NULL
;
293 struct xid_frame
*frame
;
294 __u32 bcast
= BROADCAST
;
296 DEBUG( 4, __FUNCTION__
"(), s=%d, S=%d, command=%d\n", s
, S
, command
);
298 ASSERT( self
!= NULL
, return;);
299 ASSERT( self
->magic
== LAP_MAGIC
, return;);
300 ASSERT( discovery
!= NULL
, return;);
302 skb
= dev_alloc_skb(64);
307 frame
= (struct xid_frame
*) skb
->data
;
310 frame
->caddr
= CBROADCAST
| CMD_FRAME
;
311 frame
->control
= XID_CMD
| PF_BIT
;
313 frame
->caddr
= CBROADCAST
;
314 frame
->control
= XID_RSP
| PF_BIT
;
316 frame
->ident
= XID_FORMAT
;
318 frame
->saddr
= cpu_to_le32(self
->saddr
);
321 frame
->daddr
= cpu_to_le32(bcast
);
323 frame
->daddr
= cpu_to_le32(discovery
->daddr
);
344 frame
->version
= 0x00;
347 * Provide info for final slot only in commands, and for all
348 * responses. Send the second byte of the hint only if the
349 * EXTENSION bit is set in the first byte.
351 if ( !command
|| ( frame
->slotnr
== 0xff)) {
354 if (discovery
->hints
.byte
[0] & HINT_EXTENSION
)
355 skb_put( skb
, 3+discovery
->info_len
);
357 skb_put( skb
, 2+discovery
->info_len
);
360 frame
->discovery_info
[i
++] = discovery
->hints
.byte
[0];
361 if(discovery
->hints
.byte
[0] & HINT_EXTENSION
)
362 frame
->discovery_info
[i
++] = discovery
->hints
.byte
[1];
364 frame
->discovery_info
[i
++] = discovery
->charset
;
366 ASSERT( discovery
->info_len
< 30, return;);
368 memcpy( &frame
->discovery_info
[i
++], discovery
->info
,
369 discovery
->info_len
);
372 ASSERT( self
->netdev
!= NULL
, return;);
374 irlap_queue_xmit(self
, skb
);
378 * Function irlap_recv_discovery_xid_rsp (skb, info)
380 * Received a XID discovery response
383 static void irlap_recv_discovery_xid_rsp(struct irlap_cb
*self
,
385 struct irlap_info
*info
)
387 struct xid_frame
*xid
;
388 discovery_t
*discovery
= NULL
;
391 DEBUG(4, __FUNCTION__
"()\n");
393 ASSERT(self
!= NULL
, return;);
394 ASSERT(self
->magic
== LAP_MAGIC
, return;);
395 ASSERT(skb
!= NULL
, return;);
396 ASSERT(info
!= NULL
, return;);
398 if ((discovery
= kmalloc(sizeof(discovery_t
), GFP_ATOMIC
)) == NULL
) {
399 DEBUG(0, __FUNCTION__
"(), kmalloc failed!\n");
402 memset(discovery
, 0, sizeof(discovery_t
));
404 xid
= (struct xid_frame
*) skb
->data
;
407 * Copy peer device address and set the source address
409 info
->daddr
= le32_to_cpu(xid
->saddr
);
410 discovery
->daddr
= info
->daddr
;
411 discovery
->saddr
= self
->saddr
;
412 discovery
->timestamp
= jiffies
;
414 DEBUG(4, __FUNCTION__
"(), daddr=%08x\n", discovery
->daddr
);
416 /* Get info returned from peer */
417 discovery
->hints
.byte
[0] = xid
->discovery_info
[0];
418 if (xid
->discovery_info
[0] & HINT_EXTENSION
) {
419 DEBUG(4, "EXTENSION\n");
420 discovery
->hints
.byte
[1] = xid
->discovery_info
[1];
421 discovery
->charset
= xid
->discovery_info
[2];
422 text
= (char *) &xid
->discovery_info
[3];
424 discovery
->hints
.byte
[1] = 0;
425 discovery
->charset
= xid
->discovery_info
[1];
426 text
= (char *) &xid
->discovery_info
[2];
429 * Terminate string, should be safe since this is where the
432 skb
->data
[skb
->len
] = '\0';
433 strcpy(discovery
->info
, text
);
435 info
->discovery
= discovery
;
437 irlap_do_event(self
, RECV_DISCOVERY_XID_RSP
, skb
, info
);
441 * Function irlap_recv_discovery_xid_cmd (skb, info)
443 * Received a XID discovery command
446 static void irlap_recv_discovery_xid_cmd( struct irlap_cb
*self
,
448 struct irlap_info
*info
)
450 struct xid_frame
*xid
;
451 discovery_t
*discovery
= NULL
;
454 DEBUG( 4, __FUNCTION__
"()\n");
456 ASSERT( self
!= NULL
, return;);
457 ASSERT( self
->magic
== LAP_MAGIC
, return;);
458 ASSERT( skb
!= NULL
, return;);
459 ASSERT( info
!= NULL
, return;);
461 xid
= (struct xid_frame
*) skb
->data
;
463 /* Copy peer device address */
464 info
->daddr
= le32_to_cpu(xid
->saddr
);
466 switch ( xid
->flags
& 0x03) {
483 info
->s
= xid
->slotnr
;
486 * Check if last frame
488 if ( info
->s
== 0xff) {
490 * We now have some discovery info to deliver!
492 discovery
= kmalloc( sizeof(discovery_t
), GFP_ATOMIC
);
496 discovery
->daddr
= info
->daddr
;
497 discovery
->saddr
= self
->saddr
;
498 discovery
->timestamp
= jiffies
;
500 DEBUG( 4, __FUNCTION__
"(), daddr=%08x\n",
503 discovery
->hints
.byte
[0] = xid
->discovery_info
[0];
504 if ( xid
->discovery_info
[0] & HINT_EXTENSION
) {
505 discovery
->hints
.byte
[1] = xid
->discovery_info
[1];
506 discovery
->charset
= xid
->discovery_info
[2];
507 text
= (char *) &xid
->discovery_info
[3];
509 discovery
->hints
.byte
[1] = 0;
510 discovery
->charset
= xid
->discovery_info
[1];
511 text
= (char *) &xid
->discovery_info
[2];
514 * Terminate string, should be safe since this is where the
517 skb
->data
[skb
->len
] = '\0';
518 strcpy( discovery
->info
, text
);
520 info
->discovery
= discovery
;
522 info
->discovery
= NULL
;
524 irlap_do_event( self
, RECV_DISCOVERY_XID_CMD
, skb
, info
);
528 * Function irlap_send_rr_frame (self, command)
530 * Build and transmit RR (Receive Ready) frame. Notice that it is currently
531 * only possible to send RR frames with the poll bit set.
533 void irlap_send_rr_frame(struct irlap_cb
*self
, int command
)
538 skb
= dev_alloc_skb(32);
542 frame
= skb_put(skb
, 2);
544 frame
[0] = self
->caddr
;
545 frame
[0] |= (command
) ? CMD_FRAME
: 0;
547 frame
[1] = RR
| PF_BIT
| (self
->vr
<< 5);
549 irlap_queue_xmit(self
, skb
);
553 * Function irlap_recv_rr_frame (skb, info)
555 * Received RR (Receive Ready) frame from peer station, no harm in
556 * making it inline since its called only from one single place
559 static inline void irlap_recv_rr_frame(struct irlap_cb
*self
,
561 struct irlap_info
*info
, int command
)
563 info
->nr
= skb
->data
[1] >> 5;
565 /* Check if this is a command or a response frame */
567 irlap_do_event(self
, RECV_RR_CMD
, skb
, info
);
569 irlap_do_event(self
, RECV_RR_RSP
, skb
, info
);
572 void irlap_send_frmr_frame( struct irlap_cb
*self
, int command
)
574 struct sk_buff
*skb
= NULL
;
577 ASSERT( self
!= NULL
, return;);
578 ASSERT( self
->magic
== LAP_MAGIC
, return;);
580 skb
= dev_alloc_skb( 32);
587 frame
[0] = self
->caddr
;
588 frame
[0] |= (command
) ? CMD_FRAME
: 0;
590 frame
[1] = (self
->vs
<< 1);
592 frame
[1] |= (self
->vr
<< 5);
596 DEBUG( 4, __FUNCTION__
"(), vr=%d, %ld\n",self
->vr
, jiffies
);
598 irlap_queue_xmit(self
, skb
);
602 * Function irlap_recv_rnr_frame (self, skb, info)
604 * Received RNR (Receive Not Ready) frame from peer station
607 static void irlap_recv_rnr_frame( struct irlap_cb
*self
, struct sk_buff
*skb
,
608 struct irlap_info
*info
)
612 ASSERT( skb
!= NULL
, return;);
613 ASSERT( info
!= NULL
, return;);
616 info
->nr
= frame
[1] >> 5;
618 DEBUG( 4, __FUNCTION__
"(), nr=%d, %ld\n", info
->nr
, jiffies
);
620 irlap_do_event( self
, RECV_RNR_FRAME
, skb
, info
);
624 * Function irlap_recv_ua_frame (skb, frame)
626 * Received UA (Unnumbered Acknowledgement) frame
629 static void irlap_recv_ua_frame(struct irlap_cb
*self
, struct sk_buff
*skb
,
630 struct irlap_info
*info
)
632 DEBUG(4, __FUNCTION__
"(), <%ld>\n", jiffies
);
634 ASSERT(skb
!= NULL
, return;);
635 ASSERT(info
!= NULL
, return;);
637 irlap_do_event(self
, RECV_UA_RSP
, skb
, info
);
641 * Function irlap_send_data_primary(self, skb)
646 void irlap_send_data_primary( struct irlap_cb
*self
, struct sk_buff
*skb
)
648 struct sk_buff
*tx_skb
;
650 if (skb
->data
[1] == I_FRAME
) {
653 * Insert frame sequence number (Vs) in control field before
654 * inserting into transmit window queue.
656 skb
->data
[1] = I_FRAME
| (self
->vs
<< 1);
659 tx_skb
= skb_clone(skb
, GFP_ATOMIC
);
660 if (tx_skb
== NULL
) {
666 * make sure the skb->sk accounting of memory usage is sane
669 skb_set_owner_w(tx_skb
, skb
->sk
);
672 * Insert frame in store, in case of retransmissions
674 skb_queue_tail(&self
->wx_list
, skb
);
676 self
->vs
= (self
->vs
+ 1) % 8;
677 self
->ack_required
= FALSE
;
680 irlap_send_i_frame( self
, tx_skb
, CMD_FRAME
);
682 DEBUG( 4, __FUNCTION__
"(), sending unreliable frame\n");
683 irlap_send_ui_frame(self
, skb
, CMD_FRAME
);
688 * Function irlap_send_data_primary_poll ( self, skb)
690 * Send I(nformation) frame as primary with poll bit set
692 void irlap_send_data_primary_poll( struct irlap_cb
*self
, struct sk_buff
*skb
)
694 struct sk_buff
*tx_skb
;
696 /* Is this reliable or unreliable data? */
697 if (skb
->data
[1] == I_FRAME
) {
700 * Insert frame sequence number (Vs) in control field before
701 * inserting into transmit window queue.
703 skb
->data
[1] = I_FRAME
| (self
->vs
<< 1);
706 tx_skb
= skb_clone(skb
, GFP_ATOMIC
);
707 if (tx_skb
== NULL
) {
713 * make sure the skb->sk accounting of memory usage is sane
716 skb_set_owner_w(tx_skb
, skb
->sk
);
719 * Insert frame in store, in case of retransmissions
721 skb_queue_tail(&self
->wx_list
, skb
);
724 * Set poll bit if necessary. We do this to the copied
725 * skb, since retransmitted need to set or clear the poll
726 * bit depending on when they are sent.
729 del_timer(&self
->poll_timer
);
731 tx_skb
->data
[1] |= PF_BIT
;
733 self
->vs
= (self
->vs
+ 1) % 8;
734 self
->ack_required
= FALSE
;
735 self
->window
= self
->window_size
;
737 irlap_start_final_timer(self
, self
->final_timeout
);
739 irlap_send_i_frame(self
, tx_skb
, CMD_FRAME
);
741 DEBUG(4, __FUNCTION__
"(), sending unreliable frame\n");
743 del_timer(&self
->poll_timer
);
745 if (self
->ack_required
) {
746 irlap_send_ui_frame(self
, skb
, CMD_FRAME
);
747 irlap_send_rr_frame(self
, CMD_FRAME
);
748 self
->ack_required
= FALSE
;
750 skb
->data
[1] |= PF_BIT
;
751 irlap_send_ui_frame(self
, skb
, CMD_FRAME
);
753 self
->window
= self
->window_size
;
754 irlap_start_final_timer(self
, self
->final_timeout
);
759 * Function irlap_send_data_secondary_final (self, skb)
761 * Send I(nformation) frame as secondary with final bit set
764 void irlap_send_data_secondary_final(struct irlap_cb
*self
,
767 struct sk_buff
*tx_skb
= NULL
;
769 ASSERT( self
!= NULL
, return;);
770 ASSERT( self
->magic
== LAP_MAGIC
, return;);
771 ASSERT( skb
!= NULL
, return;);
773 /* Is this reliable or unreliable data? */
774 if ( skb
->data
[1] == I_FRAME
) {
777 * Insert frame sequence number (Vs) in control field before
778 * inserting into transmit window queue.
780 skb
->data
[1] = I_FRAME
| (self
->vs
<< 1);
782 tx_skb
= skb_clone( skb
, GFP_ATOMIC
);
783 if ( tx_skb
== NULL
) {
789 skb_set_owner_w( tx_skb
, skb
->sk
);
791 /* Insert frame in store */
792 skb_queue_tail( &self
->wx_list
, skb
);
794 tx_skb
->data
[1] |= PF_BIT
;
796 self
->vs
= (self
->vs
+ 1) % 8;
797 self
->window
= self
->window_size
;
798 self
->ack_required
= FALSE
;
800 irlap_start_wd_timer( self
, self
->wd_timeout
);
802 irlap_send_i_frame( self
, tx_skb
, RSP_FRAME
);
804 if ( self
->ack_required
) {
805 irlap_send_ui_frame( self
, skb
, RSP_FRAME
);
806 irlap_send_rr_frame( self
, RSP_FRAME
);
807 self
->ack_required
= FALSE
;
809 skb
->data
[1] |= PF_BIT
;
810 irlap_send_ui_frame( self
, skb
, RSP_FRAME
);
812 self
->window
= self
->window_size
;
814 irlap_start_wd_timer( self
, self
->wd_timeout
);
819 * Function irlap_send_data_secondary (self, skb)
821 * Send I(nformation) frame as secondary without final bit set
824 void irlap_send_data_secondary( struct irlap_cb
*self
, struct sk_buff
*skb
)
826 struct sk_buff
*tx_skb
= NULL
;
828 ASSERT( self
!= NULL
, return;);
829 ASSERT( self
->magic
== LAP_MAGIC
, return;);
830 ASSERT( skb
!= NULL
, return;);
832 /* Is this reliable or unreliable data? */
833 if ( skb
->data
[1] == I_FRAME
) {
836 * Insert frame sequence number (Vs) in control field before
837 * inserting into transmit window queue.
839 skb
->data
[1] = I_FRAME
| (self
->vs
<< 1);
841 tx_skb
= skb_clone( skb
, GFP_ATOMIC
);
842 if ( tx_skb
== NULL
) {
848 skb_set_owner_w( tx_skb
, skb
->sk
);
850 /* Insert frame in store */
851 skb_queue_tail( &self
->wx_list
, skb
);
853 self
->vs
= (self
->vs
+ 1) % 8;
854 self
->ack_required
= FALSE
;
857 irlap_send_i_frame( self
, tx_skb
, RSP_FRAME
);
859 irlap_send_ui_frame( self
, skb
, RSP_FRAME
);
865 * Function irlap_resend_rejected_frames (nr)
867 * Resend frames which has not been acknowledged. TODO: check that the
868 * traversal of the list is atomic, i.e that no-one tries to insert or
869 * remove frames from the list while we travers it!
871 * FIXME: It is not safe to traverse a this list without locking it!
873 void irlap_resend_rejected_frames( struct irlap_cb
*self
, int command
)
875 struct sk_buff
*tx_skb
;
879 ASSERT( self
!= NULL
, return;);
880 ASSERT( self
->magic
== LAP_MAGIC
, return;);
882 DEBUG(2, __FUNCTION__
"(), retry_count=%d\n", self
->retry_count
);
884 /* Initialize variables */
888 * Resend all unacknowledged frames
890 count
= skb_queue_len( &self
->wx_list
);
891 skb
= skb_peek( &self
->wx_list
);
892 while ( skb
!= NULL
) {
893 irlap_wait_min_turn_around( self
, &self
->qos_tx
);
895 /* We copy the skb to be retransmitted since we will have to
896 * modify it. Cloning will confuse packet sniffers
898 /* tx_skb = skb_clone( skb, GFP_ATOMIC); */
899 tx_skb
= skb_copy(skb
, GFP_ATOMIC
);
900 if ( tx_skb
== NULL
) {
901 /* Unlink tx_skb from list */
902 tx_skb
->next
= tx_skb
->prev
= NULL
;
908 /* Unlink tx_skb from list */
909 tx_skb
->next
= tx_skb
->prev
= NULL
;
913 * make sure the skb->sk accounting of memory usage is sane
915 if ( skb
->sk
!= NULL
)
916 skb_set_owner_w( tx_skb
, skb
->sk
);
918 /* Clear old Nr field + poll bit */
919 tx_skb
->data
[1] &= 0x0f;
922 * Set poll bit on the last frame retransmitted
925 tx_skb
->data
[1] |= PF_BIT
; /* Set p/f bit */
927 tx_skb
->data
[1] &= ~PF_BIT
; /* Clear p/f bit */
929 irlap_send_i_frame( self
, tx_skb
, command
);
932 * If our skb is the last buffer in the list, then
933 * we are finished, if not, move to the next sk-buffer
935 if ( skb
== skb_peek_tail( &self
->wx_list
))
941 * We can now fill the window with additinal data frames
943 return; /* Skip this for now, DB */
945 while ( skb_queue_len( &self
->tx_list
) > 0) {
947 DEBUG( 0, __FUNCTION__
"(), sending additional frames!\n");
948 if (( skb_queue_len( &self
->tx_list
) > 0) &&
949 ( self
->window
> 0)) {
950 skb
= skb_dequeue( &self
->tx_list
);
951 ASSERT( skb
!= NULL
, return;);
954 * If send window > 1 then send frame with pf
957 if ((self
->window
> 1) &&
958 skb_queue_len(&self
->tx_list
) > 0)
960 irlap_send_data_primary(self
, skb
);
962 irlap_send_data_primary_poll(self
, skb
);
969 * Function irlap_send_ui_frame (self, skb, command)
971 * Contruct and transmit an Unnumbered Information (UI) frame
974 void irlap_send_ui_frame(struct irlap_cb
*self
, struct sk_buff
*skb
,
979 DEBUG( 4, __FUNCTION__
"()\n");
981 ASSERT( self
!= NULL
, return;);
982 ASSERT( self
->magic
== LAP_MAGIC
, return;);
983 ASSERT( skb
!= NULL
, return;);
987 /* Insert connection address */
988 frame
[0] = self
->caddr
;
989 frame
[0] |= (command
) ? CMD_FRAME
: 0;
991 irlap_queue_xmit(self
, skb
);
995 * Function irlap_send_i_frame (skb)
997 * Contruct and transmit Information (I) frame
999 void irlap_send_i_frame(struct irlap_cb
*self
, struct sk_buff
*skb
,
1006 /* Insert connection address */
1007 frame
[0] = self
->caddr
;
1008 frame
[0] |= (command
) ? CMD_FRAME
: 0;
1010 /* Insert next to receive (Vr) */
1011 frame
[1] |= (self
->vr
<< 5); /* insert nr */
1013 irlap_queue_xmit(self
, skb
);
1017 * Function irlap_recv_i_frame (skb, frame)
1019 * Receive and parse an I (Information) frame, no harm in making it inline
1020 * since it's called only from one single place (irlap_input).
1022 static inline void irlap_recv_i_frame(struct irlap_cb
*self
,
1023 struct sk_buff
*skb
,
1024 struct irlap_info
*info
, int command
)
1026 info
->nr
= skb
->data
[1] >> 5; /* Next to receive */
1027 info
->pf
= skb
->data
[1] & PF_BIT
; /* Final bit */
1028 info
->ns
= (skb
->data
[1] >> 1) & 0x07; /* Next to send */
1030 DEBUG(4, __FUNCTION__
"(), ns=%d, nr=%d, pf=%d, %ld\n",
1031 info
->ns
, info
->nr
, info
->pf
>>4, jiffies
);
1033 /* Check if this is a command or a response frame */
1035 irlap_do_event(self
, RECV_I_CMD
, skb
, info
);
1037 irlap_do_event(self
, RECV_I_RSP
, skb
, info
);
1041 * Function irlap_recv_ui_frame (self, skb, info)
1043 * Receive and parse an Unnumbered Information (UI) frame
1046 static void irlap_recv_ui_frame(struct irlap_cb
*self
, struct sk_buff
*skb
,
1047 struct irlap_info
*info
)
1051 DEBUG( 4, __FUNCTION__
"()\n");
1055 info
->pf
= frame
[1] & PF_BIT
; /* Final bit */
1057 irlap_do_event( self
, RECV_UI_FRAME
, skb
, info
);
1061 * Function irlap_recv_frmr_frame (skb, frame)
1063 * Received Frame Reject response.
1066 static void irlap_recv_frmr_frame( struct irlap_cb
*self
, struct sk_buff
*skb
,
1067 struct irlap_info
*info
)
1072 DEBUG( 0, __FUNCTION__
"()\n");
1074 ASSERT( self
!= NULL
, return;);
1075 ASSERT( self
->magic
== LAP_MAGIC
, return;);
1076 ASSERT( skb
!= NULL
, return;);
1077 ASSERT( info
!= NULL
, return;);
1081 info
->nr
= frame
[2] >> 5; /* Next to receive */
1082 info
->pf
= frame
[2] & PF_BIT
; /* Final bit */
1083 info
->ns
= (frame
[2] >> 1) & 0x07; /* Next to send */
1085 w
= frame
[3] & 0x01;
1086 x
= frame
[3] & 0x02;
1087 y
= frame
[3] & 0x04;
1088 z
= frame
[3] & 0x08;
1091 DEBUG( 0, "Rejected control field is undefined or not "
1095 DEBUG( 0, "Rejected control field was invalid because it "
1096 "contained a non permitted I field.\n");
1099 DEBUG( 0, "Received I field exceeded the maximum negotiated "
1100 "for the existing connection or exceeded the maximum "
1101 "this station supports if no connection exists.\n");
1104 DEBUG( 0, "Rejected control field control field contained an "
1105 "invalid Nr count.\n");
1107 irlap_do_event( self
, RECV_FRMR_RSP
, skb
, info
);
1111 * Function irlap_send_test_frame (self, daddr)
1113 * Send a test frame response
1116 void irlap_send_test_frame(struct irlap_cb
*self
, __u32 daddr
,
1117 struct sk_buff
*cmd
)
1119 struct sk_buff
*skb
;
1120 struct test_frame
*frame
;
1122 skb
= dev_alloc_skb(32);
1126 skb_put(skb
, sizeof(struct test_frame
));
1128 frame
= (struct test_frame
*) skb
->data
;
1131 if (self
->state
== LAP_NDM
)
1132 frame
->caddr
= CBROADCAST
; /* Send response */
1134 frame
->caddr
= self
->caddr
;
1136 frame
->control
= TEST_RSP
;
1138 /* Insert the swapped addresses */
1139 frame
->saddr
= cpu_to_le32(self
->saddr
);
1140 frame
->daddr
= cpu_to_le32(daddr
);
1143 skb_put(skb
, cmd
->len
);
1144 memcpy(frame
->info
, cmd
->data
, cmd
->len
);
1146 /* Return to sender */
1147 irlap_wait_min_turn_around(self
, &self
->qos_tx
);
1148 irlap_queue_xmit(self
, skb
);
1152 * Function irlap_recv_test_frame (self, skb)
1154 * Receive a test frame
1157 void irlap_recv_test_frame(struct irlap_cb
*self
, struct sk_buff
*skb
,
1158 struct irlap_info
*info
, int command
)
1160 struct test_frame
*frame
;
1162 DEBUG(0, __FUNCTION__
"()\n");
1164 if (skb
->len
< sizeof(struct test_frame
)) {
1165 DEBUG(0, __FUNCTION__
"() test frame to short!\n");
1169 frame
= (struct test_frame
*) skb
->data
;
1171 /* Read and swap addresses */
1172 info
->daddr
= le32_to_cpu(frame
->saddr
);
1173 info
->saddr
= le32_to_cpu(frame
->daddr
);
1176 irlap_do_event(self
, RECV_TEST_CMD
, skb
, info
);
1178 irlap_do_event(self
, RECV_TEST_RSP
, skb
, info
);
1182 * Function irlap_driver_rcv (skb, netdev, ptype)
1184 * Called when a frame is received. Dispatches the right receive function
1185 * for processing of the frame.
1188 int irlap_driver_rcv(struct sk_buff
*skb
, struct device
*dev
,
1189 struct packet_type
*ptype
)
1191 struct irlap_info info
;
1192 struct irlap_cb
*self
;
1193 struct irda_device
*idev
;
1198 idev
= (struct irda_device
*) dev
->priv
;
1200 ASSERT( idev
!= NULL
, return -1;);
1203 ASSERT( self
!= NULL
, return -1;);
1204 ASSERT( self
->magic
== LAP_MAGIC
, return -1;);
1205 ASSERT( skb
->len
> 1, return -1;);
1209 command
= frame
[0] & CMD_FRAME
;
1210 info
.caddr
= frame
[0] & CBROADCAST
;
1212 info
.pf
= frame
[1] & PF_BIT
;
1213 info
.control
= frame
[1] & ~PF_BIT
; /* Mask away poll/final bit */
1215 control
= info
.control
;
1218 * First check if this frame addressed to us
1220 if ((info
.caddr
!= self
->caddr
) && (info
.caddr
!= CBROADCAST
)) {
1221 DEBUG(2, __FUNCTION__
"(), Received frame is not for us!\n");
1227 * Optimize for the common case and check if the frame is an
1228 * I(nformation) frame. Only I-frames have bit 0 set to 0
1230 if (~control
& 0x01) {
1231 irlap_recv_i_frame(self
, skb
, &info
, command
);
1232 self
->stats
.rx_packets
++;
1236 * We now check is the frame is an S(upervisory) frame. Only
1237 * S-frames have bit 0 set to 1 and bit 1 set to 0
1239 if (~control
& 0x02) {
1241 * Received S(upervisory) frame, check which frame type it is
1242 * only the first nibble is of interest
1244 switch (control
& 0x0f) {
1246 irlap_recv_rr_frame( self
, skb
, &info
, command
);
1247 self
->stats
.rx_packets
++;
1250 irlap_recv_rnr_frame( self
, skb
, &info
);
1251 self
->stats
.rx_packets
++;
1254 DEBUG( 0, "*** REJ frame received! ***\n");
1257 DEBUG( 0, "*** SREJ frame received! ***\n");
1260 DEBUG( 0, "Unknown S frame %02x received!\n",
1267 * This must be a C(ontrol) frame
1271 irlap_recv_discovery_xid_rsp(self
, skb
, &info
);
1274 irlap_recv_discovery_xid_cmd(self
, skb
, &info
);
1277 irlap_recv_snrm_cmd(self
, skb
, &info
);
1280 DEBUG( 0, "DM rsp frame received!\n");
1281 irlap_next_state(self
, LAP_NDM
);
1284 irlap_do_event(self
, RECV_DISC_FRAME
, skb
, &info
);
1287 DEBUG(0,__FUNCTION__
"(), TEST_FRAME\n");
1288 irlap_recv_test_frame(self
, skb
, &info
, command
);
1291 DEBUG( 4, "UA rsp frame received!\n");
1292 irlap_recv_ua_frame( self
, skb
, &info
);
1295 irlap_recv_frmr_frame( self
, skb
, &info
);
1298 DEBUG( 4, "UI-frame received!\n");
1299 irlap_recv_ui_frame( self
, skb
, &info
);
1302 DEBUG( 0, "Unknown frame %02x received!\n", info
.control
);
1303 dev_kfree_skb( skb
);
1306 self
->stats
.rx_packets
++;