2.2.0-final
[davej-history.git] / net / irda / irlap_frame.c
blobd00fd185242caa7e8737760cbd0dc69e42034168
1 /*********************************************************************
2 *
3 * Filename: irlap_frame.c
4 * Version: 0.3
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>
27 #include <net/sock.h>
29 #include <linux/skbuff.h>
30 #include <linux/if.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;
63 /* Reset */
64 self->mtt_required = 0;
66 /*
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)
85 struct sk_buff *skb;
86 __u8 *frame;
87 int len;
88 int n;
90 DEBUG( 4, "irlap_send_snrm_cmd()\n");
92 ASSERT( self != NULL, return;);
93 ASSERT( self->magic == LAP_MAGIC, return;);
95 n = 0;
97 /* Allocate frame */
98 skb = dev_alloc_skb( 64);
99 if (skb == NULL) {
100 DEBUG( 0,"irlap_send_snrm_cmd: "
101 "Could not allocate an sk_buff of length %d\n", 64);
102 return;
105 skb_put( skb, 2);
106 frame = skb->data;
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
118 if (qos) {
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 */
128 skb_put( skb, len);
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)
174 struct sk_buff *skb;
175 __u8 *frame;
176 int n;
177 int len;
179 DEBUG( 4, __FUNCTION__ "() <%ld>\n", jiffies);
181 ASSERT( self != NULL, return;);
182 ASSERT( self->magic == LAP_MAGIC, return;);
184 skb = NULL;
185 n = 0;
187 /* Allocate frame */
188 skb = dev_alloc_skb( 64);
189 if (skb == NULL) {
190 DEBUG( 0, __FUNCTION__
191 "Could not allocate an sk_buff of length %d\n", 64);
192 return;
195 skb_put( skb, 10);
196 frame = skb->data;
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? */
205 if ( qos) {
206 len = irda_insert_qos_negotiation_params( qos, frame+n);
207 skb_put( skb, len);
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;
228 __u8 *frame;
230 ASSERT( self != NULL, return;);
231 ASSERT( self->magic == LAP_MAGIC, return;);
233 skb = dev_alloc_skb( 32);
234 if (skb == NULL) {
235 DEBUG( 0,"irlap_send_disc_frame: "
236 "Could not allocate an sk_buff of length %d\n", 64);
237 return;
240 skb_put( skb, 2);
241 frame = skb->data;
243 if ( self->state == LAP_NDM)
244 frame[0] = CBROADCAST;
245 else
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;
267 __u8 *frame;
269 ASSERT( self != NULL, return;);
270 ASSERT( self->magic == LAP_MAGIC, return;);
272 skb = dev_alloc_skb( 32);
273 if (skb == NULL) {
274 DEBUG( 0,"irlap_send_disc_frame: "
275 "Could not allocate an sk_buff of length %d\n", 64);
276 return;
279 skb_put( skb, 2);
280 frame = skb->data;
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
297 * frame.
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);
313 if (skb == NULL) {
314 DEBUG( 0,"irlap_send_discovery_xid_frame: "
315 "Could not allocate an sk_buff of length %d\n", 64);
316 return;
319 skb_put( skb, 14);
320 frame = ( struct xid_frame *) skb->data;
322 if ( command) {
323 frame->caddr = CBROADCAST | CMD_FRAME;
324 frame->control = XID_CMD | PF_BIT;
325 } else {
326 frame->caddr = CBROADCAST;
327 frame->control = XID_RSP | PF_BIT;
329 frame->ident = XID_FORMAT;
331 memcpy( &frame->saddr, &self->saddr, 4);
333 if ( command)
334 memcpy( &frame->daddr, &bcast, 4);
335 else
336 memcpy( &frame->daddr, &self->daddr, 4);
338 switch( S) {
339 case 1:
340 frame->flags = 0x00;
341 break;
342 case 6:
343 frame->flags = 0x01;
344 break;
345 case 8:
346 frame->flags = 0x02;
347 break;
348 case 16:
349 frame->flags = 0x03;
350 break;
351 default:
352 frame->flags = 0x02;
353 break;
356 frame->slotnr = s;
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)) {
365 int i;
367 if( discovery->hint[0] & HINT_EXTENSION)
368 skb_put( skb, 3+discovery->info_len);
369 else
370 skb_put( skb, 2+discovery->info_len);
372 i = 0;
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,
403 struct sk_buff *skb,
404 struct irlap_info *info)
406 struct xid_frame *xid;
407 DISCOVERY *discovery = NULL;
408 char *text;
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");
419 return;
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];
441 } else {
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
448 * FCS bytes resides.
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,
465 struct sk_buff *skb,
466 struct irlap_info *info)
468 struct xid_frame *xid;
469 DISCOVERY *discovery = NULL;
470 char *text;
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) {
485 case 0x00:
486 info->S = 1;
487 break;
488 case 0x01:
489 info->S = 6;
490 break;
491 case 0x02:
492 info->S = 8;
493 break;
494 case 0x03:
495 info->S = 16;
496 break;
497 default:
498 /* Error!! */
499 return;
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);
511 if ( !discovery) {
512 DEBUG( 0, __FUNCTION__ "(), kmalloc failed!\n");
513 return;
515 discovery->daddr = info->daddr;
516 discovery->saddr = self->saddr;
518 DEBUG( 4, __FUNCTION__ "(), daddr=%08x\n",
519 discovery->daddr);
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];
527 } else {
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
534 * FCS bytes resides.
536 skb->data[skb->len] = '\0';
537 strcpy( discovery->info, text);
539 info->discovery = discovery;
540 } else
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;
558 __u8 *frame;
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;
569 #endif
570 if ( !skb) {
571 skb = dev_alloc_skb( 32);
572 if (skb == NULL) {
573 printk( KERN_WARNING "irlap_send_rr_frame: "
574 "Could not allocate an skb of length %d\n", 32);
575 return;
577 skb_put( skb, 2);
579 ASSERT( skb->len == 2, return;);
581 frame = skb->data;
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)
607 __u8 *frame;
609 ASSERT( self != NULL, return;);
610 ASSERT( self->magic == LAP_MAGIC, return;);
611 ASSERT( skb != NULL, return;);
613 frame = skb->data;
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.
625 #if 0
626 if (( self->state != LAP_NRM_P) && ( self->state != LAP_NRM_S)) {
627 DEBUG( 0, __FUNCTION__ "(), Wrong state, dropping frame!\n");
628 dev_kfree_skb( skb);
629 return;
631 #endif
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.
644 skb = NULL;
646 #endif
647 /* Check if this is a command or a response frame */
648 if ( command)
649 irlap_do_event( self, RECV_RR_CMD, skb, info);
650 else
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;
657 __u8 *frame;
659 ASSERT( self != NULL, return;);
660 ASSERT( self->magic == LAP_MAGIC, return;);
662 skb = dev_alloc_skb( 32);
663 if (skb == NULL) {
664 printk( KERN_WARNING "irlap_send_frmr_frame: "
665 "Could not allocate an sk_buff of length %d\n", 32);
666 return;
669 skb_put( skb, 2);
670 frame = skb->data;
672 frame[0] = self->caddr;
673 frame[0] |= (command) ? CMD_FRAME : 0;
675 frame[1] = (self->vs << 1);
676 frame[1] |= PF_BIT;
677 frame[1] |= (self->vr << 5);
679 frame[2] = 0;
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)
700 __u8 *frame;
702 ASSERT( skb != NULL, return;);
703 ASSERT( info != NULL, return;);
705 frame = skb->data;
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 */
747 tx_skb = NULL;
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);
757 /* * Copy buffer */
758 tx_skb = skb_clone( skb, GFP_ATOMIC);
759 /* tx_skb = skb_copy( skb, GFP_ATOMIC); */
760 if ( tx_skb == NULL) {
761 dev_kfree_skb( skb);
762 return;
766 * make sure the skb->sk accounting of memory usage is sane
768 if (skb->sk != NULL)
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;
778 self->window -= 1;
780 irlap_send_i_frame( self, tx_skb, CMD_FRAME);
781 } else {
782 DEBUG( 4, __FUNCTION__ "(), sending unreliable frame\n");
783 irlap_send_ui_frame( self, skb, CMD_FRAME);
784 self->window -= 1;
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 */
803 tx_skb = NULL;
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);
814 /* Copy buffer */
815 tx_skb = skb_clone( skb, GFP_ATOMIC);
816 /* tx_skb = skb_copy( skb, GFP_ATOMIC); */
817 if ( tx_skb == NULL) {
818 dev_kfree_skb( skb);
819 return;
823 * make sure the skb->sk accounting of memory usage is sane
825 if (skb->sk != NULL)
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.
838 /* Stop P timer */
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);
850 } else {
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;
859 } else {
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,
875 struct sk_buff *skb)
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) {
895 dev_kfree_skb( skb);
896 return;
899 if (skb->sk != 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);
914 } else {
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;
919 } else {
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) {
955 dev_kfree_skb( skb);
956 return;
959 if (skb->sk != 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;
967 self->window -= 1;
969 irlap_send_i_frame( self, tx_skb, RSP_FRAME);
970 } else {
971 irlap_send_ui_frame( self, skb, RSP_FRAME);
972 self->window -= 1;
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;
988 struct sk_buff *skb;
989 int count;
991 ASSERT( self != NULL, return;);
992 ASSERT( self->magic == LAP_MAGIC, return;);
994 DEBUG( 4, __FUNCTION__ "(), retry_count=%d\n",
995 self->retry_count);
997 /* Initialize variables */
998 skb = tx_skb = NULL;
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);
1016 return;
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
1034 if ( count-- == 1)
1035 tx_skb->data[1] |= PF_BIT; /* Set p/f bit */
1036 else
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))
1046 skb = NULL;
1047 else
1048 skb = skb->next;
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
1065 * bit cleared
1067 if (( self->window > 1) &&
1068 skb_queue_len( &self->tx_list) > 0)
1070 irlap_send_data_primary( self, skb);
1071 } else {
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,
1085 int command)
1087 __u8 *frame;
1089 DEBUG( 4, __FUNCTION__ "()\n");
1091 ASSERT( self != NULL, return;);
1092 ASSERT( self->magic == LAP_MAGIC, return;);
1093 ASSERT( skb != NULL, return;);
1095 frame = skb->data;
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,
1115 int command)
1117 __u8 *frame;
1119 ASSERT( self != NULL, return;);
1120 ASSERT( self->magic == LAP_MAGIC, return;);
1121 ASSERT( skb != NULL, return;);
1123 frame = skb->data;
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 */
1131 #if 0
1133 int vr, vs, pf;
1135 /* Chech contents of various fields */
1136 vr = frame[1] >> 5;
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);
1143 #endif
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)
1161 __u8 *frame;
1163 ASSERT( self != NULL, return;);
1164 ASSERT( self->magic == LAP_MAGIC, return;);
1165 ASSERT( skb != NULL, return;);
1167 frame = skb->data;
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);
1186 return;
1189 /* Check if this is a command or a response frame */
1190 if ( command)
1191 irlap_do_event( self, RECV_I_CMD, skb, info);
1192 else
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)
1205 __u8 *frame;
1207 DEBUG( 4, __FUNCTION__ "()\n");
1209 frame = skb->data;
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)
1225 __u8 *frame;
1226 int w, x, y, z;
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;);
1235 frame = skb->data;
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;
1246 if ( w) {
1247 DEBUG( 0, "Rejected control field is undefined or not "
1248 "implemented.\n");
1250 if ( x) {
1251 DEBUG( 0, "Rejected control field was invalid because it "
1252 "contained a non permitted I field.\n");
1254 if ( y) {
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");
1259 if ( z) {
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;
1278 __u8 *frame;
1279 int i, command;
1280 __u8 control;
1282 idev = ( struct irda_device *) netdev->priv;
1284 ASSERT( idev != NULL, return -1;);
1285 self = idev->irlap;
1287 ASSERT( self != NULL, return -1;);
1288 ASSERT( self->magic == LAP_MAGIC, return -1;);
1289 ASSERT(( skb != NULL) && (skb->len > 1), return -1;);
1291 frame = skb->data;
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]);
1310 printk("\n");
1312 dev_kfree_skb( skb);
1314 return 0;
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++;
1323 return 0;
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) {
1335 case RR:
1336 irlap_recv_rr_frame( self, skb, &info, command);
1337 self->stats.rx_packets++;
1338 break;
1339 case RNR:
1340 DEBUG( 4, "*** RNR frame received! pf = %d ***\n",
1341 info.pf >> 4);
1342 irlap_recv_rnr_frame( self, skb, &info);
1343 self->stats.rx_packets++;
1344 break;
1345 case REJ:
1346 DEBUG( 0, "*** REJ frame received! ***\n");
1347 break;
1348 case SREJ:
1349 DEBUG( 0, "*** SREJ frame received! ***\n");
1350 break;
1351 default:
1352 DEBUG( 0, "Unknown S frame %02x received!\n",
1353 info.control);
1354 break;
1356 return 0;
1359 * This must be a C(ontrol) frame
1361 switch( control) {
1362 case XID_RSP:
1363 DEBUG( 4, "XID rsp frame received!\n");
1364 irlap_recv_discovery_xid_rsp( self, skb, &info);
1365 break;
1366 case XID_CMD:
1367 DEBUG( 4, "XID cmd frame received!\n");
1368 irlap_recv_discovery_xid_cmd( self, skb, &info);
1369 break;
1370 case SNRM_CMD:
1371 DEBUG( 4, "SNRM frame received!\n");
1372 irlap_recv_snrm_cmd( self, skb, &info);
1373 break;
1374 case DM_RSP:
1375 DEBUG( 0, "DM rsp frame received!\n");
1376 irlap_next_state( self, LAP_NDM);
1377 break;
1378 case DISC_CMD:
1379 DEBUG( 0, "DISC cmd frame received!\n");
1380 irlap_do_event( self, RECV_DISC_FRAME, skb, &info);
1381 break;
1382 case TEST_CMD:
1383 DEBUG( 0, "Test frame received!\n");
1384 dev_kfree_skb( skb);
1385 break;
1386 case UA_RSP:
1387 DEBUG( 4, "UA rsp frame received!\n");
1388 irlap_recv_ua_frame( self, skb, &info);
1389 break;
1390 case FRMR_RSP:
1391 DEBUG( 4, "FRMR_RSP recevied!\n");
1392 irlap_recv_frmr_frame( self, skb, &info);
1393 break;
1394 case UI_FRAME:
1395 DEBUG( 4, "UI-frame received!\n");
1396 irlap_recv_ui_frame( self, skb, &info);
1397 break;
1398 default:
1399 DEBUG( 0, "Unknown frame %02x received!\n", info.control);
1400 dev_kfree_skb( skb);
1401 break;
1403 self->stats.rx_packets++;
1405 return 0;