Import 2.3.4pre3
[davej-history.git] / net / irda / irlap_frame.c
blob8ffd26a6e051807f10ad8d906ceda709cf0edafe
1 /*********************************************************************
2 *
3 * Filename: irlap_frame.c
4 * Version: 0.9
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: Sun May 9 22:55:11 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>
26 #include <linux/if.h>
27 #include <linux/if_ether.h>
28 #include <linux/netdevice.h>
29 #include <linux/irda.h>
31 #include <net/pkt_sched.h>
32 #include <net/sock.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;
60 /* Reset */
61 self->mtt_required = 0;
63 /*
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
77 * code into it.
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;
87 /*
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);
93 dev_queue_xmit(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)
104 struct sk_buff *skb;
105 struct snrm_frame *frame;
106 int len;
108 ASSERT(self != NULL, return;);
109 ASSERT(self->magic == LAP_MAGIC, return;);
111 /* Allocate frame */
112 skb = dev_alloc_skb(64);
113 if (!skb)
114 return;
116 skb_put(skb, 2);
117 frame = (struct snrm_frame *) skb->data;
119 /* Insert connection address field */
120 if (qos)
121 frame->caddr = CMD_FRAME | CBROADCAST;
122 else
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
131 if (qos) {
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 */
140 skb_put(skb, len);
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");
172 dev_kfree_skb(skb);
173 return;
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)
187 struct sk_buff *skb;
188 struct ua_frame *frame;
189 int len;
191 DEBUG(2, __FUNCTION__ "() <%ld>\n", jiffies);
193 ASSERT(self != NULL, return;);
194 ASSERT(self->magic == LAP_MAGIC, return;);
196 skb = NULL;
198 /* Allocate frame */
199 skb = dev_alloc_skb(64);
200 if (!skb)
201 return;
203 skb_put( skb, 10);
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? */
214 if (qos) {
215 len = irda_insert_qos_negotiation_params(qos, frame->params);
216 skb_put(skb, len);
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;
232 __u8 *frame;
234 ASSERT(self != NULL, return;);
235 ASSERT(self->magic == LAP_MAGIC, return;);
237 skb = dev_alloc_skb(32);
238 if (!skb)
239 return;
241 skb_put( skb, 2);
242 frame = skb->data;
244 if (self->state == LAP_NDM)
245 frame[0] = CBROADCAST;
246 else
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;
263 __u8 *frame;
265 DEBUG(3, __FUNCTION__ "()\n");
267 ASSERT(self != NULL, return;);
268 ASSERT(self->magic == LAP_MAGIC, return;);
270 skb = dev_alloc_skb(32);
271 if (!skb)
272 return;
274 skb_put(skb, 2);
275 frame = skb->data;
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
287 * frame.
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);
303 if (!skb)
304 return;
306 skb_put(skb, 14);
307 frame = (struct xid_frame *) skb->data;
309 if ( command) {
310 frame->caddr = CBROADCAST | CMD_FRAME;
311 frame->control = XID_CMD | PF_BIT;
312 } else {
313 frame->caddr = CBROADCAST;
314 frame->control = XID_RSP | PF_BIT;
316 frame->ident = XID_FORMAT;
318 frame->saddr = cpu_to_le32(self->saddr);
320 if (command)
321 frame->daddr = cpu_to_le32(bcast);
322 else
323 frame->daddr = cpu_to_le32(discovery->daddr);
325 switch(S) {
326 case 1:
327 frame->flags = 0x00;
328 break;
329 case 6:
330 frame->flags = 0x01;
331 break;
332 case 8:
333 frame->flags = 0x02;
334 break;
335 case 16:
336 frame->flags = 0x03;
337 break;
338 default:
339 frame->flags = 0x02;
340 break;
343 frame->slotnr = s;
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)) {
352 int i;
354 if (discovery->hints.byte[0] & HINT_EXTENSION)
355 skb_put( skb, 3+discovery->info_len);
356 else
357 skb_put( skb, 2+discovery->info_len);
359 i = 0;
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,
384 struct sk_buff *skb,
385 struct irlap_info *info)
387 struct xid_frame *xid;
388 discovery_t *discovery = NULL;
389 char *text;
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");
400 return;
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];
423 } else {
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
430 * FCS bytes resides.
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,
447 struct sk_buff *skb,
448 struct irlap_info *info)
450 struct xid_frame *xid;
451 discovery_t *discovery = NULL;
452 char *text;
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) {
467 case 0x00:
468 info->S = 1;
469 break;
470 case 0x01:
471 info->S = 6;
472 break;
473 case 0x02:
474 info->S = 8;
475 break;
476 case 0x03:
477 info->S = 16;
478 break;
479 default:
480 /* Error!! */
481 return;
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);
493 if (!discovery)
494 return;
496 discovery->daddr = info->daddr;
497 discovery->saddr = self->saddr;
498 discovery->timestamp = jiffies;
500 DEBUG( 4, __FUNCTION__ "(), daddr=%08x\n",
501 discovery->daddr);
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];
508 } else {
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
515 * FCS bytes resides.
517 skb->data[skb->len] = '\0';
518 strcpy( discovery->info, text);
520 info->discovery = discovery;
521 } else
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)
535 struct sk_buff *skb;
536 __u8 *frame;
538 skb = dev_alloc_skb(32);
539 if (!skb)
540 return;
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
557 * (irlap_input).
559 static inline void irlap_recv_rr_frame(struct irlap_cb *self,
560 struct sk_buff *skb,
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 */
566 if (command)
567 irlap_do_event(self, RECV_RR_CMD, skb, info);
568 else
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;
575 __u8 *frame;
577 ASSERT( self != NULL, return;);
578 ASSERT( self->magic == LAP_MAGIC, return;);
580 skb = dev_alloc_skb( 32);
581 if (!skb)
582 return;
584 skb_put( skb, 2);
585 frame = skb->data;
587 frame[0] = self->caddr;
588 frame[0] |= (command) ? CMD_FRAME : 0;
590 frame[1] = (self->vs << 1);
591 frame[1] |= PF_BIT;
592 frame[1] |= (self->vr << 5);
594 frame[2] = 0;
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)
610 __u8 *frame;
612 ASSERT( skb != NULL, return;);
613 ASSERT( info != NULL, return;);
615 frame = skb->data;
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);
658 /* Copy buffer */
659 tx_skb = skb_clone(skb, GFP_ATOMIC);
660 if (tx_skb == NULL) {
661 dev_kfree_skb(skb);
662 return;
666 * make sure the skb->sk accounting of memory usage is sane
668 if (skb->sk != NULL)
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;
678 self->window -= 1;
680 irlap_send_i_frame( self, tx_skb, CMD_FRAME);
681 } else {
682 DEBUG( 4, __FUNCTION__ "(), sending unreliable frame\n");
683 irlap_send_ui_frame(self, skb, CMD_FRAME);
684 self->window -= 1;
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);
705 /* Copy buffer */
706 tx_skb = skb_clone(skb, GFP_ATOMIC);
707 if (tx_skb == NULL) {
708 dev_kfree_skb(skb);
709 return;
713 * make sure the skb->sk accounting of memory usage is sane
715 if (skb->sk != NULL)
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.
728 /* Stop P timer */
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);
740 } else {
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;
749 } else {
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,
765 struct sk_buff *skb)
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) {
784 dev_kfree_skb( skb);
785 return;
788 if (skb->sk != 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);
803 } else {
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;
808 } else {
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) {
843 dev_kfree_skb( skb);
844 return;
847 if (skb->sk != 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;
855 self->window -= 1;
857 irlap_send_i_frame( self, tx_skb, RSP_FRAME);
858 } else {
859 irlap_send_ui_frame( self, skb, RSP_FRAME);
860 self->window -= 1;
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;
876 struct sk_buff *skb;
877 int count;
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 */
885 skb = tx_skb = NULL;
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;
903 tx_skb->list = NULL;
905 dev_kfree_skb( skb);
906 return;
908 /* Unlink tx_skb from list */
909 tx_skb->next = tx_skb->prev = NULL;
910 tx_skb->list = 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
924 if ( count-- == 1)
925 tx_skb->data[1] |= PF_BIT; /* Set p/f bit */
926 else
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))
936 skb = NULL;
937 else
938 skb = skb->next;
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
955 * bit cleared
957 if ((self->window > 1) &&
958 skb_queue_len(&self->tx_list) > 0)
960 irlap_send_data_primary(self, skb);
961 } else {
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,
975 int command)
977 __u8 *frame;
979 DEBUG( 4, __FUNCTION__ "()\n");
981 ASSERT( self != NULL, return;);
982 ASSERT( self->magic == LAP_MAGIC, return;);
983 ASSERT( skb != NULL, return;);
985 frame = skb->data;
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,
1000 int command)
1002 __u8 *frame;
1004 ASSERT( self != NULL, return;);
1005 ASSERT( self->magic == LAP_MAGIC, return;);
1006 ASSERT( skb != NULL, return;);
1008 frame = skb->data;
1010 /* Insert connection address */
1011 frame[0] = self->caddr;
1012 frame[0] |= (command) ? CMD_FRAME : 0;
1014 /* Insert next to receive (Vr) */
1015 frame[1] |= (self->vr << 5); /* insert nr */
1017 #if 0
1019 int ns;
1020 ns = (frame[1] >> 1) & 0x07; /* Next to send */
1022 DEBUG(0, __FUNCTION__ "(), ns=%d\n", ns);
1024 #endif
1026 irlap_queue_xmit(self, skb);
1030 * Function irlap_recv_i_frame (skb, frame)
1032 * Receive and parse an I (Information) frame, no harm in making it inline
1033 * since it's called only from one single place (irlap_input).
1035 static inline void irlap_recv_i_frame(struct irlap_cb *self,
1036 struct sk_buff *skb,
1037 struct irlap_info *info, int command)
1039 info->nr = skb->data[1] >> 5; /* Next to receive */
1040 info->pf = skb->data[1] & PF_BIT; /* Final bit */
1041 info->ns = (skb->data[1] >> 1) & 0x07; /* Next to send */
1043 DEBUG(4, __FUNCTION__"(), ns=%d, nr=%d, pf=%d, %ld\n",
1044 info->ns, info->nr, info->pf>>4, jiffies);
1046 /* Check if this is a command or a response frame */
1047 if (command)
1048 irlap_do_event(self, RECV_I_CMD, skb, info);
1049 else
1050 irlap_do_event(self, RECV_I_RSP, skb, info);
1054 * Function irlap_recv_ui_frame (self, skb, info)
1056 * Receive and parse an Unnumbered Information (UI) frame
1059 static void irlap_recv_ui_frame(struct irlap_cb *self, struct sk_buff *skb,
1060 struct irlap_info *info)
1062 __u8 *frame;
1064 DEBUG( 4, __FUNCTION__ "()\n");
1066 frame = skb->data;
1068 info->pf = frame[1] & PF_BIT; /* Final bit */
1070 irlap_do_event( self, RECV_UI_FRAME, skb, info);
1074 * Function irlap_recv_frmr_frame (skb, frame)
1076 * Received Frame Reject response.
1079 static void irlap_recv_frmr_frame( struct irlap_cb *self, struct sk_buff *skb,
1080 struct irlap_info *info)
1082 __u8 *frame;
1083 int w, x, y, z;
1085 DEBUG( 0, __FUNCTION__ "()\n");
1087 ASSERT( self != NULL, return;);
1088 ASSERT( self->magic == LAP_MAGIC, return;);
1089 ASSERT( skb != NULL, return;);
1090 ASSERT( info != NULL, return;);
1092 frame = skb->data;
1094 info->nr = frame[2] >> 5; /* Next to receive */
1095 info->pf = frame[2] & PF_BIT; /* Final bit */
1096 info->ns = (frame[2] >> 1) & 0x07; /* Next to send */
1098 w = frame[3] & 0x01;
1099 x = frame[3] & 0x02;
1100 y = frame[3] & 0x04;
1101 z = frame[3] & 0x08;
1103 if ( w) {
1104 DEBUG( 0, "Rejected control field is undefined or not "
1105 "implemented.\n");
1107 if ( x) {
1108 DEBUG( 0, "Rejected control field was invalid because it "
1109 "contained a non permitted I field.\n");
1111 if ( y) {
1112 DEBUG( 0, "Received I field exceeded the maximum negotiated "
1113 "for the existing connection or exceeded the maximum "
1114 "this station supports if no connection exists.\n");
1116 if ( z) {
1117 DEBUG( 0, "Rejected control field control field contained an "
1118 "invalid Nr count.\n");
1120 irlap_do_event( self, RECV_FRMR_RSP, skb, info);
1124 * Function irlap_send_test_frame (self, daddr)
1126 * Send a test frame response
1129 void irlap_send_test_frame(struct irlap_cb *self, __u32 daddr,
1130 struct sk_buff *cmd)
1132 struct sk_buff *skb;
1133 struct test_frame *frame;
1135 skb = dev_alloc_skb(32);
1136 if (!skb)
1137 return;
1139 skb_put(skb, sizeof(struct test_frame));
1141 frame = (struct test_frame *) skb->data;
1143 /* Build header */
1144 if (self->state == LAP_NDM)
1145 frame->caddr = CBROADCAST; /* Send response */
1146 else
1147 frame->caddr = self->caddr;
1149 frame->control = TEST_RSP;
1151 /* Insert the swapped addresses */
1152 frame->saddr = cpu_to_le32(self->saddr);
1153 frame->daddr = cpu_to_le32(daddr);
1155 /* Copy info */
1156 skb_put(skb, cmd->len);
1157 memcpy(frame->info, cmd->data, cmd->len);
1159 /* Return to sender */
1160 irlap_wait_min_turn_around(self, &self->qos_tx);
1161 irlap_queue_xmit(self, skb);
1165 * Function irlap_recv_test_frame (self, skb)
1167 * Receive a test frame
1170 void irlap_recv_test_frame(struct irlap_cb *self, struct sk_buff *skb,
1171 struct irlap_info *info, int command)
1173 struct test_frame *frame;
1175 DEBUG(0, __FUNCTION__ "()\n");
1177 if (skb->len < sizeof(struct test_frame)) {
1178 DEBUG(0, __FUNCTION__ "() test frame to short!\n");
1179 return;
1182 frame = (struct test_frame *) skb->data;
1184 /* Read and swap addresses */
1185 info->daddr = le32_to_cpu(frame->saddr);
1186 info->saddr = le32_to_cpu(frame->daddr);
1188 if (command)
1189 irlap_do_event(self, RECV_TEST_CMD, skb, info);
1190 else
1191 irlap_do_event(self, RECV_TEST_RSP, skb, info);
1195 * Function irlap_driver_rcv (skb, netdev, ptype)
1197 * Called when a frame is received. Dispatches the right receive function
1198 * for processing of the frame.
1201 int irlap_driver_rcv(struct sk_buff *skb, struct device *dev,
1202 struct packet_type *ptype)
1204 struct irlap_info info;
1205 struct irlap_cb *self;
1206 struct irda_device *idev;
1207 __u8 *frame;
1208 int command;
1209 __u8 control;
1211 idev = (struct irda_device *) dev->priv;
1213 ASSERT( idev != NULL, return -1;);
1214 self = idev->irlap;
1216 ASSERT( self != NULL, return -1;);
1217 ASSERT( self->magic == LAP_MAGIC, return -1;);
1218 ASSERT( skb->len > 1, return -1;);
1220 frame = skb->data;
1222 command = frame[0] & CMD_FRAME;
1223 info.caddr = frame[0] & CBROADCAST;
1225 info.pf = frame[1] & PF_BIT;
1226 info.control = frame[1] & ~PF_BIT; /* Mask away poll/final bit */
1228 control = info.control;
1231 * First check if this frame addressed to us
1233 if ((info.caddr != self->caddr) && (info.caddr != CBROADCAST)) {
1234 DEBUG(2, __FUNCTION__ "(), Received frame is not for us!\n");
1236 dev_kfree_skb(skb);
1237 return 0;
1240 * Optimize for the common case and check if the frame is an
1241 * I(nformation) frame. Only I-frames have bit 0 set to 0
1243 if(~control & 0x01) {
1244 irlap_recv_i_frame(self, skb, &info, command);
1245 self->stats.rx_packets++;
1246 return 0;
1249 * We now check is the frame is an S(upervisory) frame. Only
1250 * S-frames have bit 0 set to 1 and bit 1 set to 0
1252 if (~control & 0x02) {
1254 * Received S(upervisory) frame, check which frame type it is
1255 * only the first nibble is of interest
1257 switch (control & 0x0f) {
1258 case RR:
1259 irlap_recv_rr_frame( self, skb, &info, command);
1260 self->stats.rx_packets++;
1261 break;
1262 case RNR:
1263 irlap_recv_rnr_frame( self, skb, &info);
1264 self->stats.rx_packets++;
1265 break;
1266 case REJ:
1267 DEBUG( 0, "*** REJ frame received! ***\n");
1268 break;
1269 case SREJ:
1270 DEBUG( 0, "*** SREJ frame received! ***\n");
1271 break;
1272 default:
1273 DEBUG( 0, "Unknown S frame %02x received!\n",
1274 info.control);
1275 break;
1277 return 0;
1280 * This must be a C(ontrol) frame
1282 switch (control) {
1283 case XID_RSP:
1284 irlap_recv_discovery_xid_rsp(self, skb, &info);
1285 break;
1286 case XID_CMD:
1287 irlap_recv_discovery_xid_cmd(self, skb, &info);
1288 break;
1289 case SNRM_CMD:
1290 irlap_recv_snrm_cmd(self, skb, &info);
1291 break;
1292 case DM_RSP:
1293 DEBUG( 0, "DM rsp frame received!\n");
1294 irlap_next_state(self, LAP_NDM);
1295 break;
1296 case DISC_CMD:
1297 irlap_do_event(self, RECV_DISC_FRAME, skb, &info);
1298 break;
1299 case TEST_CMD:
1300 DEBUG(0,__FUNCTION__ "(), TEST_FRAME\n");
1301 irlap_recv_test_frame(self, skb, &info, command);
1302 break;
1303 case UA_RSP:
1304 DEBUG( 4, "UA rsp frame received!\n");
1305 irlap_recv_ua_frame( self, skb, &info);
1306 break;
1307 case FRMR_RSP:
1308 irlap_recv_frmr_frame( self, skb, &info);
1309 break;
1310 case UI_FRAME:
1311 DEBUG( 4, "UI-frame received!\n");
1312 irlap_recv_ui_frame( self, skb, &info);
1313 break;
1314 default:
1315 DEBUG( 0, "Unknown frame %02x received!\n", info.control);
1316 dev_kfree_skb( skb);
1317 break;
1319 self->stats.rx_packets++;
1321 return 0;