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: Tue Jan 19 22:58:13 1999
10 * Modified by: Dag Brattli <dagb@cs.uit.no>
12 * Copyright (c) 1998 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/config.h>
26 #include <linux/skbuff.h>
29 #include <linux/skbuff.h>
31 #include <linux/if_ether.h>
32 #include <linux/netdevice.h>
33 #include <net/pkt_sched.h>
35 #include <net/irda/irda.h>
36 #include <net/irda/irda_device.h>
37 #include <net/irda/irlap.h>
38 #include <net/irda/wrapper.h>
39 #include <net/irda/timer.h>
40 #include <net/irda/irlap_frame.h>
41 #include <net/irda/qos.h>
43 extern __u8
*irlmp_hint_to_service( __u8
*hint
);
46 * Function irlap_insert_mtt (self, skb)
48 * Insert minimum turnaround time relevant information into the skb. We
49 * need to do this since it's per packet relevant information.
52 __inline__
void irlap_insert_mtt( struct irlap_cb
*self
, struct sk_buff
*skb
)
54 struct irlap_skb_cb
*cb
;
56 ASSERT( self
!= NULL
, return;);
57 ASSERT( self
->magic
== LAP_MAGIC
, return;);
59 cb
= (struct irlap_skb_cb
*) skb
->cb
;
61 cb
->mtt
= self
->mtt_required
;
64 self
->mtt_required
= 0;
67 * Delay equals negotiated BOFs count plus the number of BOFs to
68 * force the negotiated minimum turnaround time
70 cb
->xbofs
= self
->bofs_count
+self
->xbofs_delay
;
72 /* Reset XBOF's delay (used only for getting min turn time) */
73 self
->xbofs_delay
= 0;
75 DEBUG( 4, __FUNCTION__
"(), using %d xbofs\n", cb
->xbofs
);
79 * Function irlap_send_snrm_cmd (void)
81 * Transmits a connect SNRM command frame
83 void irlap_send_snrm_frame( struct irlap_cb
*self
, struct qos_info
*qos
)
90 DEBUG( 4, "irlap_send_snrm_cmd()\n");
92 ASSERT( self
!= NULL
, return;);
93 ASSERT( self
->magic
== LAP_MAGIC
, return;);
98 skb
= dev_alloc_skb( 64);
100 DEBUG( 0,"irlap_send_snrm_cmd: "
101 "Could not allocate an sk_buff of length %d\n", 64);
108 /* Insert address field */
109 frame
[n
] = CMD_FRAME
;
110 frame
[n
++] |= (qos
) ? CBROADCAST
: self
->caddr
;
112 /* Insert control field */
113 frame
[n
++] = SNRM_CMD
| PF_BIT
;
116 * If we are establishing a connection then insert QoS paramerters
119 skb_put( skb
, 9); /* 21 left */
120 memcpy( frame
+n
, &self
->saddr
, 4); n
+= 4;
121 memcpy( frame
+n
, &self
->daddr
, 4); n
+= 4;
123 frame
[n
++] = self
->caddr
;
125 /* skb_put( skb, 21); */
126 len
= irda_insert_qos_negotiation_params( qos
, frame
+n
);
127 /* Should not be dangerous to do this afterwards */
130 skb
->dev
= self
->netdev
;
131 skb
->h
.raw
= skb
->data
;
132 irlap_insert_mtt( self
, skb
);
134 dev_queue_xmit( skb
);
135 self
->stats
.tx_packets
++;
139 * Function irlap_recv_snrm_cmd (skb, info)
141 * Received SNRM (Set Normal Response Mode) command frame
144 static void irlap_recv_snrm_cmd( struct irlap_cb
*self
, struct sk_buff
*skb
,
145 struct irlap_info
*info
)
147 struct snrm_frame
*frame
;
149 DEBUG( 4, __FUNCTION__
"() <%ld>\n", jiffies
);
151 ASSERT( skb
!= NULL
, return;);
152 ASSERT( info
!= NULL
, return;);
154 frame
= ( struct snrm_frame
*) skb
->data
;
156 /* Copy peer device address */
157 memcpy( &info
->daddr
, &frame
->saddr
, 4);
159 /* Copy connection address */
160 info
->caddr
= frame
->ncaddr
;
162 irlap_do_event( self
, RECV_SNRM_CMD
, skb
, info
);
166 * Function irlap_send_ua_response_frame (qos)
168 * Send UA (Unnumbered Acknowledgement) frame
171 void irlap_send_ua_response_frame( struct irlap_cb
*self
,
172 struct qos_info
*qos
)
179 DEBUG( 4, __FUNCTION__
"() <%ld>\n", jiffies
);
181 ASSERT( self
!= NULL
, return;);
182 ASSERT( self
->magic
== LAP_MAGIC
, return;);
188 skb
= dev_alloc_skb( 64);
190 DEBUG( 0, __FUNCTION__
191 "Could not allocate an sk_buff of length %d\n", 64);
198 /* Build UA response */
199 frame
[n
++] = self
->caddr
;
200 frame
[n
++] = UA_RSP
| PF_BIT
;
201 memcpy( frame
+n
, &self
->saddr
, 4); n
+= 4;
202 memcpy( frame
+n
, &self
->daddr
, 4); n
+= 4;
204 /* Should we send QoS negotiation parameters? */
206 len
= irda_insert_qos_negotiation_params( qos
, frame
+n
);
210 skb
->dev
= self
->netdev
;
211 skb
->h
.raw
= skb
->data
;
212 irlap_insert_mtt( self
, skb
);
214 dev_queue_xmit( skb
);
215 self
->stats
.tx_packets
++;
220 * Function irlap_send_dm_frame (void)
222 * Send disconnected mode (DM) frame
225 void irlap_send_dm_frame( struct irlap_cb
*self
)
227 struct sk_buff
*skb
= NULL
;
230 ASSERT( self
!= NULL
, return;);
231 ASSERT( self
->magic
== LAP_MAGIC
, return;);
233 skb
= dev_alloc_skb( 32);
235 DEBUG( 0,"irlap_send_disc_frame: "
236 "Could not allocate an sk_buff of length %d\n", 64);
243 if ( self
->state
== LAP_NDM
)
244 frame
[0] = CBROADCAST
;
246 frame
[0] = self
->caddr
;
248 frame
[1] = DM_RSP
| PF_BIT
;
250 skb
->dev
= self
->netdev
;
251 skb
->h
.raw
= skb
->data
;
252 irlap_insert_mtt( self
, skb
);
254 dev_queue_xmit( skb
);
255 self
->stats
.tx_packets
++;
259 * Function irlap_send_disc_frame (void)
261 * Send disconnect (DISC) frame
264 void irlap_send_disc_frame( struct irlap_cb
*self
)
266 struct sk_buff
*skb
= NULL
;
269 ASSERT( self
!= NULL
, return;);
270 ASSERT( self
->magic
== LAP_MAGIC
, return;);
272 skb
= dev_alloc_skb( 32);
274 DEBUG( 0,"irlap_send_disc_frame: "
275 "Could not allocate an sk_buff of length %d\n", 64);
282 frame
[0] = self
->caddr
| CMD_FRAME
;
283 frame
[1] = DISC_CMD
| PF_BIT
;
285 skb
->dev
= self
->netdev
;
286 skb
->h
.raw
= skb
->data
;
287 irlap_insert_mtt( self
, skb
);
289 dev_queue_xmit( skb
);
290 self
->stats
.tx_packets
++;
294 * Function irlap_send_discovery_xid_frame (S, s, command)
296 * Build and transmit a XID (eXchange station IDentifier) discovery
299 void irlap_send_discovery_xid_frame( struct irlap_cb
*self
, int S
, __u8 s
,
300 __u8 command
, DISCOVERY
*discovery
)
302 struct sk_buff
*skb
= NULL
;
303 struct xid_frame
*frame
;
304 __u32 bcast
= BROADCAST
;
306 DEBUG( 4, __FUNCTION__
"(), s=%d, S=%d, command=%d\n", s
, S
, command
);
308 ASSERT( self
!= NULL
, return;);
309 ASSERT( self
->magic
== LAP_MAGIC
, return;);
310 ASSERT( discovery
!= NULL
, return;);
312 skb
= dev_alloc_skb( 64);
314 DEBUG( 0,"irlap_send_discovery_xid_frame: "
315 "Could not allocate an sk_buff of length %d\n", 64);
320 frame
= ( struct xid_frame
*) skb
->data
;
323 frame
->caddr
= CBROADCAST
| CMD_FRAME
;
324 frame
->control
= XID_CMD
| PF_BIT
;
326 frame
->caddr
= CBROADCAST
;
327 frame
->control
= XID_RSP
| PF_BIT
;
329 frame
->ident
= XID_FORMAT
;
331 memcpy( &frame
->saddr
, &self
->saddr
, 4);
334 memcpy( &frame
->daddr
, &bcast
, 4);
336 memcpy( &frame
->daddr
, &self
->daddr
, 4);
357 frame
->version
= 0x00;
360 * Provide info for final slot only in commands, and for all
361 * responses. Send the second byte of the hint only if the
362 * EXTENSION bit is set in the first byte.
364 if ( !command
|| ( frame
->slotnr
== 0xff)) {
367 if( discovery
->hint
[0] & HINT_EXTENSION
)
368 skb_put( skb
, 3+discovery
->info_len
);
370 skb_put( skb
, 2+discovery
->info_len
);
373 frame
->discovery_info
[i
++] = discovery
->hint
[0];
374 if( discovery
->hint
[0] & HINT_EXTENSION
)
375 frame
->discovery_info
[i
++] = discovery
->hint
[1];
377 frame
->discovery_info
[i
++] = discovery
->charset
;
379 ASSERT( discovery
->info_len
< 30, return;);
381 memcpy( &frame
->discovery_info
[i
++], discovery
->info
,
382 discovery
->info_len
);
386 ASSERT( self
->netdev
!= NULL
, return;);
388 skb
->dev
= self
->netdev
;
389 skb
->h
.raw
= skb
->data
;
390 irlap_insert_mtt( self
, skb
);
392 dev_queue_xmit( skb
);
393 self
->stats
.tx_packets
++;
397 * Function irlap_recv_discovery_xid_rsp (skb, info)
399 * Received a XID discovery response
402 static void irlap_recv_discovery_xid_rsp( struct irlap_cb
*self
,
404 struct irlap_info
*info
)
406 struct xid_frame
*xid
;
407 DISCOVERY
*discovery
= NULL
;
410 DEBUG( 4, __FUNCTION__
"()\n");
412 ASSERT( self
!= NULL
, return;);
413 ASSERT( self
->magic
== LAP_MAGIC
, return;);
414 ASSERT( skb
!= NULL
, return;);
415 ASSERT( info
!= NULL
, return;);
417 if (( discovery
= kmalloc( sizeof( DISCOVERY
), GFP_ATOMIC
)) == NULL
) {
418 DEBUG( 0, __FUNCTION__
"(), kmalloc failed!\n");
421 memset( discovery
, 0, sizeof( DISCOVERY
));
423 xid
= (struct xid_frame
*) skb
->data
;
426 * Copy peer device address and set the source address
428 memcpy( &info
->daddr
, &xid
->saddr
, 4);
429 discovery
->daddr
= info
->daddr
;
430 discovery
->saddr
= self
->saddr
;
432 DEBUG( 4, __FUNCTION__
"(), daddr=%08x\n", discovery
->daddr
);
434 /* Get info returned from peer */
435 discovery
->hint
[0] = xid
->discovery_info
[0];
436 if ( xid
->discovery_info
[0] & HINT_EXTENSION
) {
437 DEBUG( 4, "EXTENSION\n");
438 discovery
->hint
[1] = xid
->discovery_info
[1];
439 discovery
->charset
= xid
->discovery_info
[2];
440 text
= (char *) &xid
->discovery_info
[3];
442 discovery
->hint
[1] = 0;
443 discovery
->charset
= xid
->discovery_info
[1];
444 text
= (char *) &xid
->discovery_info
[2];
447 * Terminate string, should be safe since this is where the
450 skb
->data
[skb
->len
] = '\0';
451 strcpy( discovery
->info
, text
);
453 info
->discovery
= discovery
;
455 irlap_do_event( self
, RECV_DISCOVERY_XID_RSP
, skb
, info
);
459 * Function irlap_recv_discovery_xid_cmd (skb, info)
461 * Received a XID discovery command
464 static void irlap_recv_discovery_xid_cmd( struct irlap_cb
*self
,
466 struct irlap_info
*info
)
468 struct xid_frame
*xid
;
469 DISCOVERY
*discovery
= NULL
;
472 DEBUG( 4, __FUNCTION__
"()\n");
474 ASSERT( self
!= NULL
, return;);
475 ASSERT( self
->magic
== LAP_MAGIC
, return;);
476 ASSERT( skb
!= NULL
, return;);
477 ASSERT( info
!= NULL
, return;);
479 xid
= (struct xid_frame
*) skb
->data
;
481 /* Copy peer device address */
482 memcpy( &info
->daddr
, &xid
->saddr
, 4);
484 switch ( xid
->flags
& 0x03) {
501 info
->s
= xid
->slotnr
;
504 * Check if last frame
506 if ( info
->s
== 0xff) {
508 * We now have some discovery info to deliver!
510 discovery
= kmalloc( sizeof( DISCOVERY
), GFP_ATOMIC
);
512 DEBUG( 0, __FUNCTION__
"(), kmalloc failed!\n");
515 discovery
->daddr
= info
->daddr
;
516 discovery
->saddr
= self
->saddr
;
518 DEBUG( 4, __FUNCTION__
"(), daddr=%08x\n",
521 discovery
->hint
[0] = xid
->discovery_info
[0];
522 if ( xid
->discovery_info
[0] & HINT_EXTENSION
) {
523 DEBUG( 4, "EXTENSION\n");
524 discovery
->hint
[1] = xid
->discovery_info
[1];
525 discovery
->charset
= xid
->discovery_info
[2];
526 text
= (char *) &xid
->discovery_info
[3];
528 discovery
->hint
[1] = 0;
529 discovery
->charset
= xid
->discovery_info
[1];
530 text
= (char *) &xid
->discovery_info
[2];
533 * Terminate string, should be safe since this is where the
536 skb
->data
[skb
->len
] = '\0';
537 strcpy( discovery
->info
, text
);
539 info
->discovery
= discovery
;
541 info
->discovery
= NULL
;
543 DEBUG( 4, __FUNCTION__
"(), s=%d, S=%d <%ld>\n",
544 info
->s
, info
->S
, jiffies
);
546 irlap_do_event( self
, RECV_DISCOVERY_XID_CMD
, skb
, info
);
550 * Function irlap_send_rr_frame (self, command)
552 * Build and transmit RR (Receive Ready) frame. Notice that it is currently
553 * only possible to send RR frames with the poll bit set.
555 void irlap_send_rr_frame( struct irlap_cb
*self
, int command
)
557 struct sk_buff
*skb
= NULL
;
560 ASSERT( self
!= NULL
, return;);
561 ASSERT( self
->magic
== LAP_MAGIC
, return;);
563 #ifdef CONFIG_IRDA_RECYCLE_RR
564 if ( self
->recycle_rr_skb
) {
565 DEBUG( 4, __FUNCTION__
"(), recycling skb!\n");
566 skb
= self
->recycle_rr_skb
;
567 self
->recycle_rr_skb
= NULL
;
571 skb
= dev_alloc_skb( 32);
573 printk( KERN_WARNING
"irlap_send_rr_frame: "
574 "Could not allocate an skb of length %d\n", 32);
579 ASSERT( skb
->len
== 2, return;);
583 frame
[0] = self
->caddr
;
584 frame
[0] |= (command
) ? CMD_FRAME
: 0;
586 frame
[1] = RR
| PF_BIT
| (self
->vr
<< 5);
588 DEBUG( 4, __FUNCTION__
"(), vr=%d, %ld\n", self
->vr
, jiffies
);
590 skb
->dev
= self
->netdev
;
591 skb
->h
.raw
= skb
->data
;
592 irlap_insert_mtt( self
, skb
);
594 dev_queue_xmit( skb
);
595 self
->stats
.tx_packets
++;
599 * Function irlap_recv_rr_frame (skb, info)
601 * Received RR (Receive Ready) frame from peer station
604 static void irlap_recv_rr_frame( struct irlap_cb
*self
, struct sk_buff
*skb
,
605 struct irlap_info
*info
, int command
)
609 ASSERT( self
!= NULL
, return;);
610 ASSERT( self
->magic
== LAP_MAGIC
, return;);
611 ASSERT( skb
!= NULL
, return;);
614 info
->nr
= frame
[1] >> 5;
616 DEBUG( 4, __FUNCTION__
"(), nr=%d, %ld\n", info
->nr
, jiffies
);
619 * Make sure the state-machine is in the right state for receiving,
620 * if not, then we just discard the received frame for now!
621 * TODO: check if we should queue this frame, or make tty tell that
622 * it is receiving frames until the frame is delivered instead of
623 * until it is outside a frame.
626 if (( self
->state
!= LAP_NRM_P
) && ( self
->state
!= LAP_NRM_S
)) {
627 DEBUG( 0, __FUNCTION__
"(), Wrong state, dropping frame!\n");
633 #ifdef CONFIG_IRDA_RECYCLE_RR
634 /* Only recycle one RR frame */
635 if ( self
->recycle_rr_skb
== NULL
) {
637 /* Keep this skb, so it can be reused */
638 self
->recycle_rr_skb
= skb
;
641 * Set skb to NULL, so that the state machine will not
642 * try to deallocate it.
647 /* Check if this is a command or a response frame */
649 irlap_do_event( self
, RECV_RR_CMD
, skb
, info
);
651 irlap_do_event( self
, RECV_RR_RSP
, skb
, info
);
654 void irlap_send_frmr_frame( struct irlap_cb
*self
, int command
)
656 struct sk_buff
*skb
= NULL
;
659 ASSERT( self
!= NULL
, return;);
660 ASSERT( self
->magic
== LAP_MAGIC
, return;);
662 skb
= dev_alloc_skb( 32);
664 printk( KERN_WARNING
"irlap_send_frmr_frame: "
665 "Could not allocate an sk_buff of length %d\n", 32);
672 frame
[0] = self
->caddr
;
673 frame
[0] |= (command
) ? CMD_FRAME
: 0;
675 frame
[1] = (self
->vs
<< 1);
677 frame
[1] |= (self
->vr
<< 5);
681 DEBUG( 4, __FUNCTION__
"(), vr=%d, %ld\n",self
->vr
, jiffies
);
683 skb
->dev
= self
->netdev
;
684 skb
->h
.raw
= skb
->data
;
685 irlap_insert_mtt( self
, skb
);
687 dev_queue_xmit( skb
);
688 self
->stats
.tx_packets
++;
692 * Function irlap_recv_rnr_frame (self, skb, info)
694 * Received RNR (Receive Not Ready) frame from peer station
697 static void irlap_recv_rnr_frame( struct irlap_cb
*self
, struct sk_buff
*skb
,
698 struct irlap_info
*info
)
702 ASSERT( skb
!= NULL
, return;);
703 ASSERT( info
!= NULL
, return;);
706 info
->nr
= frame
[1] >> 5;
708 DEBUG( 4, __FUNCTION__
"(), nr=%d, %ld\n", info
->nr
, jiffies
);
710 irlap_do_event( self
, RECV_RNR_FRAME
, skb
, info
);
714 * Function irlap_recv_ua_frame (skb, frame)
716 * Received UA (Unnumbered Acknowledgement) frame
719 static void irlap_recv_ua_frame( struct irlap_cb
*self
, struct sk_buff
*skb
,
720 struct irlap_info
*info
)
722 DEBUG( 4, __FUNCTION__
"()\n");
724 ASSERT( skb
!= NULL
, return;);
725 ASSERT( info
!= NULL
, return;);
727 irlap_do_event( self
, RECV_UA_RSP
, skb
, info
);
731 * Function irlap_send_data_primary(self, skb)
736 void irlap_send_data_primary( struct irlap_cb
*self
, struct sk_buff
*skb
)
738 struct sk_buff
*tx_skb
;
740 DEBUG( 4, __FUNCTION__
"()\n");
742 ASSERT( self
!= NULL
, return;);
743 ASSERT( self
->magic
== LAP_MAGIC
, return;);
744 ASSERT( skb
!= NULL
, return;);
746 /* Initialize variables */
749 if ( skb
->data
[1] == I_FRAME
) {
752 * Insert frame sequence number (Vs) in control field before
753 * inserting into transmit window queue.
755 skb
->data
[1] = I_FRAME
| (self
->vs
<< 1);
758 tx_skb
= skb_clone( skb
, GFP_ATOMIC
);
759 /* tx_skb = skb_copy( skb, GFP_ATOMIC); */
760 if ( tx_skb
== NULL
) {
766 * make sure the skb->sk accounting of memory usage is sane
769 skb_set_owner_w( tx_skb
, skb
->sk
);
772 * Insert frame in store, in case of retransmissions
774 skb_queue_tail( &self
->wx_list
, skb
);
776 self
->vs
= (self
->vs
+ 1) % 8;
777 self
->ack_required
= FALSE
;
780 irlap_send_i_frame( self
, tx_skb
, CMD_FRAME
);
782 DEBUG( 4, __FUNCTION__
"(), sending unreliable frame\n");
783 irlap_send_ui_frame( self
, skb
, CMD_FRAME
);
788 * Function irlap_send_data_primary_poll ( self, skb)
790 * Send I(nformation) frame as primary with poll bit set
792 void irlap_send_data_primary_poll( struct irlap_cb
*self
, struct sk_buff
*skb
)
794 struct sk_buff
*tx_skb
;
796 DEBUG( 4, __FUNCTION__
"()\n");
798 ASSERT( self
!= NULL
, return;);
799 ASSERT( self
->magic
== LAP_MAGIC
, return;);
800 ASSERT( skb
!= NULL
, return;);
802 /* Initialize variables */
805 /* Is this reliable or unreliable data? */
806 if ( skb
->data
[1] == I_FRAME
) {
809 * Insert frame sequence number (Vs) in control field before
810 * inserting into transmit window queue.
812 skb
->data
[1] = I_FRAME
| (self
->vs
<< 1);
815 tx_skb
= skb_clone( skb
, GFP_ATOMIC
);
816 /* tx_skb = skb_copy( skb, GFP_ATOMIC); */
817 if ( tx_skb
== NULL
) {
823 * make sure the skb->sk accounting of memory usage is sane
826 skb_set_owner_w( tx_skb
, skb
->sk
);
829 * Insert frame in store, in case of retransmissions
831 skb_queue_tail( &self
->wx_list
, skb
);
834 * Set poll bit if necessary. We do this to the copied
835 * skb, since retransmitted need to set or clear the poll
836 * bit depending on when * they are sent.
839 del_timer( &self
->poll_timer
);
841 tx_skb
->data
[1] |= PF_BIT
;
843 self
->vs
= (self
->vs
+ 1) % 8;
844 self
->ack_required
= FALSE
;
845 self
->window
= self
->window_size
;
847 irlap_start_final_timer( self
, self
->final_timeout
);
849 irlap_send_i_frame( self
, tx_skb
, CMD_FRAME
);
851 DEBUG( 4, __FUNCTION__
"(), sending unreliable frame\n");
853 del_timer( &self
->poll_timer
);
855 if ( self
->ack_required
) {
856 irlap_send_ui_frame( self
, skb
, CMD_FRAME
);
857 irlap_send_rr_frame( self
, CMD_FRAME
);
858 self
->ack_required
= FALSE
;
860 skb
->data
[1] |= PF_BIT
;
861 irlap_send_ui_frame( self
, skb
, CMD_FRAME
);
863 self
->window
= self
->window_size
;
864 irlap_start_final_timer( self
, self
->final_timeout
);
869 * Function irlap_send_data_secondary_final (self, skb)
871 * Send I(nformation) frame as secondary with final bit set
874 void irlap_send_data_secondary_final( struct irlap_cb
*self
,
877 struct sk_buff
*tx_skb
= NULL
;
879 ASSERT( self
!= NULL
, return;);
880 ASSERT( self
->magic
== LAP_MAGIC
, return;);
881 ASSERT( skb
!= NULL
, return;);
883 /* Is this reliable or unreliable data? */
884 if ( skb
->data
[1] == I_FRAME
) {
887 * Insert frame sequence number (Vs) in control field before
888 * inserting into transmit window queue.
890 skb
->data
[1] = I_FRAME
| (self
->vs
<< 1);
892 tx_skb
= skb_clone( skb
, GFP_ATOMIC
);
893 /* tx_skb = skb_copy( skb, GFP_ATOMIC); */
894 if ( tx_skb
== NULL
) {
900 skb_set_owner_w( tx_skb
, skb
->sk
);
902 /* Insert frame in store */
903 skb_queue_tail( &self
->wx_list
, skb
);
905 tx_skb
->data
[1] |= PF_BIT
;
907 self
->vs
= (self
->vs
+ 1) % 8;
908 self
->window
= self
->window_size
;
909 self
->ack_required
= FALSE
;
911 irlap_start_wd_timer( self
, self
->wd_timeout
);
913 irlap_send_i_frame( self
, tx_skb
, RSP_FRAME
);
915 if ( self
->ack_required
) {
916 irlap_send_ui_frame( self
, skb
, RSP_FRAME
);
917 irlap_send_rr_frame( self
, RSP_FRAME
);
918 self
->ack_required
= FALSE
;
920 skb
->data
[1] |= PF_BIT
;
921 irlap_send_ui_frame( self
, skb
, RSP_FRAME
);
923 self
->window
= self
->window_size
;
925 irlap_start_wd_timer( self
, self
->wd_timeout
);
930 * Function irlap_send_data_secondary (self, skb)
932 * Send I(nformation) frame as secondary without final bit set
935 void irlap_send_data_secondary( struct irlap_cb
*self
, struct sk_buff
*skb
)
937 struct sk_buff
*tx_skb
= NULL
;
939 ASSERT( self
!= NULL
, return;);
940 ASSERT( self
->magic
== LAP_MAGIC
, return;);
941 ASSERT( skb
!= NULL
, return;);
943 /* Is this reliable or unreliable data? */
944 if ( skb
->data
[1] == I_FRAME
) {
947 * Insert frame sequence number (Vs) in control field before
948 * inserting into transmit window queue.
950 skb
->data
[1] = I_FRAME
| (self
->vs
<< 1);
952 tx_skb
= skb_clone( skb
, GFP_ATOMIC
);
953 /* tx_skb = skb_copy( skb, GFP_ATOMIC); */
954 if ( tx_skb
== NULL
) {
960 skb_set_owner_w( tx_skb
, skb
->sk
);
962 /* Insert frame in store */
963 skb_queue_tail( &self
->wx_list
, skb
);
965 self
->vs
= (self
->vs
+ 1) % 8;
966 self
->ack_required
= FALSE
;
969 irlap_send_i_frame( self
, tx_skb
, RSP_FRAME
);
971 irlap_send_ui_frame( self
, skb
, RSP_FRAME
);
977 * Function irlap_resend_rejected_frames (nr)
979 * Resend frames which has not been acknowledged. TODO: check that the
980 * traversal of the list is atomic, i.e that no-one tries to insert or
981 * remove frames from the list while we travers it!
983 * FIXME: It is not safe to traverse a this list without locking it!
985 void irlap_resend_rejected_frames( struct irlap_cb
*self
, int command
)
987 struct sk_buff
*tx_skb
;
991 ASSERT( self
!= NULL
, return;);
992 ASSERT( self
->magic
== LAP_MAGIC
, return;);
994 DEBUG( 4, __FUNCTION__
"(), retry_count=%d\n",
997 /* Initialize variables */
1001 * Resend all unacknowledged frames
1003 count
= skb_queue_len( &self
->wx_list
);
1004 skb
= skb_peek( &self
->wx_list
);
1005 while ( skb
!= NULL
) {
1006 irlap_wait_min_turn_around( self
, &self
->qos_tx
);
1008 tx_skb
= skb_clone( skb
, GFP_ATOMIC
);
1009 /* tx_skb = skb_copy( skb, GFP_ATOMIC); */
1010 if ( tx_skb
== NULL
) {
1011 /* Unlink tx_skb from list */
1012 tx_skb
->next
= tx_skb
->prev
= NULL
;
1013 tx_skb
->list
= NULL
;
1015 dev_kfree_skb( skb
);
1018 /* Unlink tx_skb from list */
1019 tx_skb
->next
= tx_skb
->prev
= NULL
;
1020 tx_skb
->list
= NULL
;
1023 * make sure the skb->sk accounting of memory usage is sane
1025 if ( skb
->sk
!= NULL
)
1026 skb_set_owner_w( tx_skb
, skb
->sk
);
1028 /* Clear old Nr field + poll bit */
1029 tx_skb
->data
[1] &= 0x0f;
1032 * Set poll bit on the last frame retransmitted
1035 tx_skb
->data
[1] |= PF_BIT
; /* Set p/f bit */
1037 tx_skb
->data
[1] &= ~PF_BIT
; /* Clear p/f bit */
1039 irlap_send_i_frame( self
, tx_skb
, command
);
1042 * If our skb is the last buffer in the list, then
1043 * we are finished, if not, move to the next sk-buffer
1045 if ( skb
== skb_peek_tail( &self
->wx_list
))
1051 * We can now fill the window with additinal data frames
1053 return; /* Skip this for now, DB */
1055 while ( skb_queue_len( &self
->tx_list
) > 0) {
1057 DEBUG( 0, __FUNCTION__
"(), sending additional frames!\n");
1058 if (( skb_queue_len( &self
->tx_list
) > 0) &&
1059 ( self
->window
> 0)) {
1060 skb
= skb_dequeue( &self
->tx_list
);
1061 ASSERT( skb
!= NULL
, return;);
1064 * If send window > 1 then send frame with pf
1067 if (( self
->window
> 1) &&
1068 skb_queue_len( &self
->tx_list
) > 0)
1070 irlap_send_data_primary( self
, skb
);
1072 irlap_send_data_primary_poll( self
, skb
);
1079 * Function irlap_send_ui_frame (self, skb, command)
1081 * Contruct and transmit an Unnumbered Information (UI) frame
1084 void irlap_send_ui_frame( struct irlap_cb
*self
, struct sk_buff
*skb
,
1089 DEBUG( 4, __FUNCTION__
"()\n");
1091 ASSERT( self
!= NULL
, return;);
1092 ASSERT( self
->magic
== LAP_MAGIC
, return;);
1093 ASSERT( skb
!= NULL
, return;);
1097 /* Insert connection address */
1098 frame
[0] = self
->caddr
;
1099 frame
[0] |= (command
) ? CMD_FRAME
: 0;
1101 skb
->dev
= self
->netdev
;
1102 skb
->h
.raw
= skb
->data
;
1103 irlap_insert_mtt( self
, skb
);
1105 dev_queue_xmit( skb
);
1106 self
->stats
.tx_packets
++;
1110 * Function irlap_send_i_frame (skb)
1112 * Contruct and transmit Information (I) frame
1114 void irlap_send_i_frame( struct irlap_cb
*self
, struct sk_buff
*skb
,
1119 ASSERT( self
!= NULL
, return;);
1120 ASSERT( self
->magic
== LAP_MAGIC
, return;);
1121 ASSERT( skb
!= NULL
, return;);
1125 /* Insert connection address */
1126 frame
[0] = self
->caddr
;
1127 frame
[0] |= (command
) ? CMD_FRAME
: 0;
1129 /* Insert next to receive (Vr) */
1130 frame
[1] |= (self
->vr
<< 5); /* insert nr */
1135 /* Chech contents of various fields */
1137 vs
= (frame
[1] >> 1) & 0x07;
1138 pf
= (frame
[1] >> 4) & 0x01;
1140 DEBUG( 0, __FUNCTION__
"(), vs=%d, vr=%d, p=%d, %ld\n",
1141 vs
, vr
, pf
, jiffies
);
1144 skb
->dev
= self
->netdev
;
1145 skb
->h
.raw
= skb
->data
;
1146 irlap_insert_mtt( self
, skb
);
1148 dev_queue_xmit( skb
);
1149 self
->stats
.tx_packets
++;
1153 * Function irlap_recv_i_frame (skb, frame)
1155 * Receive and parse an I (Information) frame
1158 static void irlap_recv_i_frame( struct irlap_cb
*self
, struct sk_buff
*skb
,
1159 struct irlap_info
*info
, int command
)
1163 ASSERT( self
!= NULL
, return;);
1164 ASSERT( self
->magic
== LAP_MAGIC
, return;);
1165 ASSERT( skb
!= NULL
, return;);
1169 info
->nr
= frame
[1] >> 5; /* Next to receive */
1170 info
->pf
= frame
[1] & PF_BIT
; /* Final bit */
1171 info
->ns
= (frame
[1] >> 1) & 0x07; /* Next to send */
1173 DEBUG( 4, __FUNCTION__
"(), ns=%d, nr=%d, pf=%d, %ld\n",
1174 info
->ns
, info
->nr
, info
->pf
>>4, jiffies
);
1177 * Make sure the state-machine is in the right state for receiving,
1178 * if not, then we just discard the received frame for now!
1179 * TODO: check if we should queue this frame, or make tty tell that
1180 * it is receiving frames until the frame is delivered instead of
1181 * until it is outside a frame.
1183 if (( self
->state
!= LAP_NRM_P
) && ( self
->state
!= LAP_NRM_S
)) {
1184 DEBUG( 0, __FUNCTION__
"(), Wrong state, dropping frame!\n");
1185 dev_kfree_skb( skb
);
1189 /* Check if this is a command or a response frame */
1191 irlap_do_event( self
, RECV_I_CMD
, skb
, info
);
1193 irlap_do_event( self
, RECV_I_RSP
, skb
, info
);
1197 * Function irlap_recv_ui_frame (self, skb, info)
1199 * Receive and parse an Unnumbered Information (UI) frame
1202 static void irlap_recv_ui_frame( struct irlap_cb
*self
, struct sk_buff
*skb
,
1203 struct irlap_info
*info
)
1207 DEBUG( 4, __FUNCTION__
"()\n");
1211 info
->pf
= frame
[1] & PF_BIT
; /* Final bit */
1213 irlap_do_event( self
, RECV_UI_FRAME
, skb
, info
);
1217 * Function irlap_recv_frmr_frame (skb, frame)
1219 * Received Frame Reject response.
1222 static void irlap_recv_frmr_frame( struct irlap_cb
*self
, struct sk_buff
*skb
,
1223 struct irlap_info
*info
)
1228 DEBUG( 0, __FUNCTION__
"()\n");
1230 ASSERT( self
!= NULL
, return;);
1231 ASSERT( self
->magic
== LAP_MAGIC
, return;);
1232 ASSERT( skb
!= NULL
, return;);
1233 ASSERT( info
!= NULL
, return;);
1237 info
->nr
= frame
[2] >> 5; /* Next to receive */
1238 info
->pf
= frame
[2] & PF_BIT
; /* Final bit */
1239 info
->ns
= (frame
[2] >> 1) & 0x07; /* Next to send */
1241 w
= frame
[3] & 0x01;
1242 x
= frame
[3] & 0x02;
1243 y
= frame
[3] & 0x04;
1244 z
= frame
[3] & 0x08;
1247 DEBUG( 0, "Rejected control field is undefined or not "
1251 DEBUG( 0, "Rejected control field was invalid because it "
1252 "contained a non permitted I field.\n");
1255 DEBUG( 0, "Received I field exceeded the maximum negotiated "
1256 "for the existing connection or exceeded the maximum "
1257 "this station supports if no connection exists.\n");
1260 DEBUG( 0, "Rejected control field control field contained an "
1261 "invalid Nr count.\n");
1263 irlap_do_event( self
, RECV_FRMR_RSP
, skb
, info
);
1267 * Function irlap_input (skb)
1269 * Called when a frame is received. Dispatches the right receive function
1270 * for processing of the frame.
1272 int irlap_input( struct sk_buff
*skb
, struct device
*netdev
,
1273 struct packet_type
*ptype
)
1275 struct irlap_info info
;
1276 struct irlap_cb
*self
;
1277 struct irda_device
*idev
;
1282 idev
= ( struct irda_device
*) netdev
->priv
;
1284 ASSERT( idev
!= NULL
, return -1;);
1287 ASSERT( self
!= NULL
, return -1;);
1288 ASSERT( self
->magic
== LAP_MAGIC
, return -1;);
1289 ASSERT(( skb
!= NULL
) && (skb
->len
> 1), return -1;);
1293 command
= frame
[0] & CMD_FRAME
;
1294 info
.caddr
= frame
[0] & CBROADCAST
;
1296 info
.pf
= frame
[1] & PF_BIT
;
1297 info
.control
= frame
[1] & ~PF_BIT
; /* Mask away poll/final bit */
1299 control
= info
.control
;
1302 * First check if this frame addressed to us
1304 if (( info
.caddr
!= self
->caddr
) && ( info
.caddr
!= CBROADCAST
)) {
1306 DEBUG( 0, __FUNCTION__
"(), Received frame is not for us!\n");
1307 for(i
=0; i
<(skb
->len
< 15?skb
->len
:15);i
++) {
1308 printk( "%02x ", frame
[i
]);
1312 dev_kfree_skb( skb
);
1317 * Optimize for the common case and check if the frame is an
1318 * I(nformation) frame. Only I-frames have bit 0 set to 0
1320 if( ~control
& 0x01) {
1321 irlap_recv_i_frame( self
, skb
, &info
, command
);
1322 self
->stats
.rx_packets
++;
1326 * We now check is the frame is an S(upervisory) frame. Only
1327 * S-frames have bit 0 set to 1 and bit 1 set to 0
1329 if ( ~control
& 0x02) {
1331 * Received S(upervisory) frame, check which frame type it is
1332 * only the first nibble is of interest
1334 switch( control
& 0x0f) {
1336 irlap_recv_rr_frame( self
, skb
, &info
, command
);
1337 self
->stats
.rx_packets
++;
1340 DEBUG( 4, "*** RNR frame received! pf = %d ***\n",
1342 irlap_recv_rnr_frame( self
, skb
, &info
);
1343 self
->stats
.rx_packets
++;
1346 DEBUG( 0, "*** REJ frame received! ***\n");
1349 DEBUG( 0, "*** SREJ frame received! ***\n");
1352 DEBUG( 0, "Unknown S frame %02x received!\n",
1359 * This must be a C(ontrol) frame
1363 DEBUG( 4, "XID rsp frame received!\n");
1364 irlap_recv_discovery_xid_rsp( self
, skb
, &info
);
1367 DEBUG( 4, "XID cmd frame received!\n");
1368 irlap_recv_discovery_xid_cmd( self
, skb
, &info
);
1371 DEBUG( 4, "SNRM frame received!\n");
1372 irlap_recv_snrm_cmd( self
, skb
, &info
);
1375 DEBUG( 0, "DM rsp frame received!\n");
1376 irlap_next_state( self
, LAP_NDM
);
1379 DEBUG( 0, "DISC cmd frame received!\n");
1380 irlap_do_event( self
, RECV_DISC_FRAME
, skb
, &info
);
1383 DEBUG( 0, "Test frame received!\n");
1384 dev_kfree_skb( skb
);
1387 DEBUG( 4, "UA rsp frame received!\n");
1388 irlap_recv_ua_frame( self
, skb
, &info
);
1391 DEBUG( 4, "FRMR_RSP recevied!\n");
1392 irlap_recv_frmr_frame( self
, skb
, &info
);
1395 DEBUG( 4, "UI-frame received!\n");
1396 irlap_recv_ui_frame( self
, skb
, &info
);
1399 DEBUG( 0, "Unknown frame %02x received!\n", info
.control
);
1400 dev_kfree_skb( skb
);
1403 self
->stats
.rx_packets
++;