1 /*********************************************************************
3 * Filename: irlap_frame.c
5 * Description: Build and transmit IrLAP frames
7 * Author: Dag Brattli <dagb@cs.uit.no>
8 * Created at: Tue Aug 19 10:27:26 1997
9 * Modified at: Wed Jan 5 08:59:04 2000
10 * Modified by: Dag Brattli <dagb@cs.uit.no>
12 * Copyright (c) 1998-2000 Dag Brattli <dagb@cs.uit.no>,
13 * All Rights Reserved.
14 * Copyright (c) 2000-2001 Jean Tourrilhes <jt@hpl.hp.com>
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2 of
19 * the License, or (at your option) any later version.
21 * Neither Dag Brattli nor University of Tromsø admit liability nor
22 * provide warranty for any of this software. This material is
23 * provided "AS-IS" and at no charge.
25 ********************************************************************/
27 #include <linux/skbuff.h>
29 #include <linux/if_ether.h>
30 #include <linux/netdevice.h>
31 #include <linux/irda.h>
33 #include <net/pkt_sched.h>
36 #include <asm/byteorder.h>
38 #include <net/irda/irda.h>
39 #include <net/irda/irda_device.h>
40 #include <net/irda/irlap.h>
41 #include <net/irda/wrapper.h>
42 #include <net/irda/timer.h>
43 #include <net/irda/irlap_frame.h>
44 #include <net/irda/qos.h>
47 * Function irlap_insert_info (self, skb)
49 * Insert minimum turnaround time and speed information into the skb. We
50 * need to do this since it's per packet relevant information. Safe to
51 * have this function inlined since it's only called from one place
53 static inline void irlap_insert_info(struct irlap_cb *self,
56 struct irda_skb_cb *cb = (struct irda_skb_cb *) skb->cb;
59 * Insert MTT (min. turn time) and speed into skb, so that the
60 * device driver knows which settings to use
62 cb->magic = LAP_MAGIC;
63 cb->mtt = self->mtt_required;
64 cb->next_speed = self->speed;
67 self->mtt_required = 0;
70 * Delay equals negotiated BOFs count, plus the number of BOFs to
71 * force the negotiated minimum turnaround time
73 cb->xbofs = self->bofs_count;
74 cb->next_xbofs = self->next_bofs;
75 cb->xbofs_delay = self->xbofs_delay;
77 /* Reset XBOF's delay (used only for getting min turn time) */
78 self->xbofs_delay = 0;
79 /* Put the correct xbofs value for the next packet */
80 self->bofs_count = self->next_bofs;
84 * Function irlap_queue_xmit (self, skb)
86 * A little wrapper for dev_queue_xmit, so we can insert some common
89 void irlap_queue_xmit(struct irlap_cb *self, struct sk_buff *skb)
91 /* Some common init stuff */
92 skb->dev = self->netdev;
93 skb->h.raw = skb->nh.raw = skb->mac.raw = skb->data;
94 skb->protocol = htons(ETH_P_IRDA);
95 skb->priority = TC_PRIO_BESTEFFORT;
97 irlap_insert_info(self, skb);
103 * Function irlap_send_snrm_cmd (void)
105 * Transmits a connect SNRM command frame
107 void irlap_send_snrm_frame(struct irlap_cb *self, struct qos_info *qos)
110 struct snrm_frame *frame;
113 ASSERT(self != NULL, return;);
114 ASSERT(self->magic == LAP_MAGIC, return;);
117 skb = dev_alloc_skb(64);
121 frame = (struct snrm_frame *) skb_put(skb, 2);
123 /* Insert connection address field */
125 frame->caddr = CMD_FRAME | CBROADCAST;
127 frame->caddr = CMD_FRAME | self->caddr;
129 /* Insert control field */
130 frame->control = SNRM_CMD | PF_BIT;
133 * If we are establishing a connection then insert QoS paramerters
136 skb_put(skb, 9); /* 21 left */
137 frame->saddr = cpu_to_le32(self->saddr);
138 frame->daddr = cpu_to_le32(self->daddr);
140 frame->ncaddr = self->caddr;
142 ret = irlap_insert_qos_negotiation_params(self, skb);
148 irlap_queue_xmit(self, skb);
152 * Function irlap_recv_snrm_cmd (skb, info)
154 * Received SNRM (Set Normal Response Mode) command frame
157 static void irlap_recv_snrm_cmd(struct irlap_cb *self, struct sk_buff *skb,
158 struct irlap_info *info)
160 struct snrm_frame *frame;
162 frame = (struct snrm_frame *) skb->data;
164 if (skb->len >= sizeof(struct snrm_frame)) {
165 /* Copy the new connection address ignoring the C/R bit */
166 info->caddr = frame->ncaddr & 0xFE;
168 /* Check if the new connection address is valid */
169 if ((info->caddr == 0x00) || (info->caddr == 0xfe)) {
170 IRDA_DEBUG(3, "%s(), invalid connection address!\n", __FUNCTION__);
174 /* Copy peer device address */
175 info->daddr = le32_to_cpu(frame->saddr);
176 info->saddr = le32_to_cpu(frame->daddr);
178 /* Only accept if addressed directly to us */
179 if (info->saddr != self->saddr) {
180 IRDA_DEBUG(2, "%s(), not addressed to us!\n", __FUNCTION__);
183 irlap_do_event(self, RECV_SNRM_CMD, skb, info);
185 /* Signal that this SNRM frame does not contain and I-field */
186 irlap_do_event(self, RECV_SNRM_CMD, skb, NULL);
191 * Function irlap_send_ua_response_frame (qos)
193 * Send UA (Unnumbered Acknowledgement) frame
196 void irlap_send_ua_response_frame(struct irlap_cb *self, struct qos_info *qos)
199 struct ua_frame *frame;
202 IRDA_DEBUG(2, "%s() <%ld>\n", __FUNCTION__, jiffies);
204 ASSERT(self != NULL, return;);
205 ASSERT(self->magic == LAP_MAGIC, return;);
210 skb = dev_alloc_skb(64);
214 frame = (struct ua_frame *) skb_put(skb, 10);
216 /* Build UA response */
217 frame->caddr = self->caddr;
218 frame->control = UA_RSP | PF_BIT;
220 frame->saddr = cpu_to_le32(self->saddr);
221 frame->daddr = cpu_to_le32(self->daddr);
223 /* Should we send QoS negotiation parameters? */
225 ret = irlap_insert_qos_negotiation_params(self, skb);
232 irlap_queue_xmit(self, skb);
237 * Function irlap_send_dm_frame (void)
239 * Send disconnected mode (DM) frame
242 void irlap_send_dm_frame( struct irlap_cb *self)
244 struct sk_buff *skb = NULL;
247 ASSERT(self != NULL, return;);
248 ASSERT(self->magic == LAP_MAGIC, return;);
250 skb = dev_alloc_skb(32);
254 frame = skb_put( skb, 2);
256 if (self->state == LAP_NDM)
257 frame[0] = CBROADCAST;
259 frame[0] = self->caddr;
261 frame[1] = DM_RSP | PF_BIT;
263 irlap_queue_xmit(self, skb);
267 * Function irlap_send_disc_frame (void)
269 * Send disconnect (DISC) frame
272 void irlap_send_disc_frame(struct irlap_cb *self)
274 struct sk_buff *skb = NULL;
277 IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
279 ASSERT(self != NULL, return;);
280 ASSERT(self->magic == LAP_MAGIC, return;);
282 skb = dev_alloc_skb(16);
286 frame = skb_put(skb, 2);
288 frame[0] = self->caddr | CMD_FRAME;
289 frame[1] = DISC_CMD | PF_BIT;
291 irlap_queue_xmit(self, skb);
295 * Function irlap_send_discovery_xid_frame (S, s, command)
297 * Build and transmit a XID (eXchange station IDentifier) discovery
300 void irlap_send_discovery_xid_frame(struct irlap_cb *self, int S, __u8 s,
301 __u8 command, discovery_t *discovery)
303 struct sk_buff *skb = NULL;
304 struct xid_frame *frame;
305 __u32 bcast = BROADCAST;
308 IRDA_DEBUG(4, "%s(), s=%d, S=%d, command=%d\n", __FUNCTION__, s, S,
311 ASSERT(self != NULL, return;);
312 ASSERT(self->magic == LAP_MAGIC, return;);
313 ASSERT(discovery != NULL, return;);
315 skb = dev_alloc_skb(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 frame->saddr = cpu_to_le32(self->saddr);
334 frame->daddr = cpu_to_le32(bcast);
336 frame->daddr = cpu_to_le32(discovery->daddr);
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->hints.byte[0] & HINT_EXTENSION) {
368 info = skb_put(skb, 2);
369 info[0] = discovery->hints.byte[0];
370 info[1] = discovery->hints.byte[1];
372 info = skb_put(skb, 1);
373 info[0] = discovery->hints.byte[0];
375 info = skb_put(skb, 1);
376 info[0] = discovery->charset;
378 len = IRDA_MIN(discovery->name_len, skb_tailroom(skb));
379 info = skb_put(skb, len);
380 memcpy(info, discovery->nickname, len);
382 irlap_queue_xmit(self, skb);
386 * Function irlap_recv_discovery_xid_rsp (skb, info)
388 * Received a XID discovery response
391 static void irlap_recv_discovery_xid_rsp(struct irlap_cb *self,
393 struct irlap_info *info)
395 struct xid_frame *xid;
396 discovery_t *discovery = NULL;
397 __u8 *discovery_info;
400 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
402 ASSERT(self != NULL, return;);
403 ASSERT(self->magic == LAP_MAGIC, return;);
405 xid = (struct xid_frame *) skb->data;
407 info->daddr = le32_to_cpu(xid->saddr);
408 info->saddr = le32_to_cpu(xid->daddr);
410 /* Make sure frame is addressed to us */
411 if ((info->saddr != self->saddr) && (info->saddr != BROADCAST)) {
412 IRDA_DEBUG(0, "%s(), frame is not addressed to us!\n", __FUNCTION__);
416 if ((discovery = kmalloc(sizeof(discovery_t), GFP_ATOMIC)) == NULL) {
417 WARNING("%s(), kmalloc failed!\n", __FUNCTION__);
420 memset(discovery, 0, sizeof(discovery_t));
422 discovery->daddr = info->daddr;
423 discovery->saddr = self->saddr;
424 discovery->timestamp = jiffies;
426 IRDA_DEBUG(4, "%s(), daddr=%08x\n", __FUNCTION__, discovery->daddr);
428 discovery_info = skb_pull(skb, sizeof(struct xid_frame));
430 /* Get info returned from peer */
431 discovery->hints.byte[0] = discovery_info[0];
432 if (discovery_info[0] & HINT_EXTENSION) {
433 IRDA_DEBUG(4, "EXTENSION\n");
434 discovery->hints.byte[1] = discovery_info[1];
435 discovery->charset = discovery_info[2];
436 text = (char *) &discovery_info[3];
438 discovery->hints.byte[1] = 0;
439 discovery->charset = discovery_info[1];
440 text = (char *) &discovery_info[2];
443 * Terminate info string, should be safe since this is where the
446 skb->data[skb->len] = '\0';
447 strncpy(discovery->nickname, text, NICKNAME_MAX_LEN);
448 discovery->name_len = strlen(discovery->nickname);
450 info->discovery = discovery;
452 irlap_do_event(self, RECV_DISCOVERY_XID_RSP, skb, info);
456 * Function irlap_recv_discovery_xid_cmd (skb, info)
458 * Received a XID discovery command
461 static void irlap_recv_discovery_xid_cmd(struct irlap_cb *self,
463 struct irlap_info *info)
465 struct xid_frame *xid;
466 discovery_t *discovery = NULL;
467 __u8 *discovery_info;
470 xid = (struct xid_frame *) skb->data;
472 info->daddr = le32_to_cpu(xid->saddr);
473 info->saddr = le32_to_cpu(xid->daddr);
475 /* Make sure frame is addressed to us */
476 if ((info->saddr != self->saddr) && (info->saddr != BROADCAST)) {
477 IRDA_DEBUG(0, "%s(), frame is not addressed to us!\n", __FUNCTION__);
481 switch (xid->flags & 0x03) {
499 info->s = xid->slotnr;
501 discovery_info = skb_pull(skb, sizeof(struct xid_frame));
504 * Check if last frame
506 if (info->s == 0xff) {
507 /* Check if things are sane at this point... */
508 if((discovery_info == NULL) || (skb->len < 3)) {
509 ERROR("%s(), discovery frame to short!\n", __FUNCTION__);
514 * We now have some discovery info to deliver!
516 discovery = kmalloc(sizeof(discovery_t), GFP_ATOMIC);
518 WARNING("%s(), unable to malloc!\n", __FUNCTION__);
522 discovery->daddr = info->daddr;
523 discovery->saddr = self->saddr;
524 discovery->timestamp = jiffies;
526 discovery->hints.byte[0] = discovery_info[0];
527 if (discovery_info[0] & HINT_EXTENSION) {
528 discovery->hints.byte[1] = discovery_info[1];
529 discovery->charset = discovery_info[2];
530 text = (char *) &discovery_info[3];
532 discovery->hints.byte[1] = 0;
533 discovery->charset = discovery_info[1];
534 text = (char *) &discovery_info[2];
537 * Terminate string, should be safe since this is where the
540 skb->data[skb->len] = '\0';
541 strncpy(discovery->nickname, text, NICKNAME_MAX_LEN);
542 discovery->name_len = strlen(discovery->nickname);
544 info->discovery = discovery;
546 info->discovery = NULL;
548 irlap_do_event(self, RECV_DISCOVERY_XID_CMD, skb, info);
552 * Function irlap_send_rr_frame (self, command)
554 * Build and transmit RR (Receive Ready) frame. Notice that it is currently
555 * only possible to send RR frames with the poll bit set.
557 void irlap_send_rr_frame(struct irlap_cb *self, int command)
562 skb = dev_alloc_skb(16);
566 frame = skb_put(skb, 2);
568 frame[0] = self->caddr;
569 frame[0] |= (command) ? CMD_FRAME : 0;
571 frame[1] = RR | PF_BIT | (self->vr << 5);
573 irlap_queue_xmit(self, skb);
577 * Function irlap_send_rd_frame (self)
579 * Request disconnect. Used by a secondary station to request the
580 * disconnection of the link.
582 void irlap_send_rd_frame(struct irlap_cb *self)
587 skb = dev_alloc_skb(16);
591 frame = skb_put(skb, 2);
593 frame[0] = self->caddr;
594 frame[1] = RD_RSP | PF_BIT;
596 irlap_queue_xmit(self, skb);
600 * Function irlap_recv_rr_frame (skb, info)
602 * Received RR (Receive Ready) frame from peer station, no harm in
603 * making it inline since its called only from one single place
604 * (irlap_driver_rcv).
606 static inline void irlap_recv_rr_frame(struct irlap_cb *self,
608 struct irlap_info *info, int command)
610 info->nr = skb->data[1] >> 5;
612 /* Check if this is a command or a response frame */
614 irlap_do_event(self, RECV_RR_CMD, skb, info);
616 irlap_do_event(self, RECV_RR_RSP, skb, info);
619 void irlap_send_frmr_frame( struct irlap_cb *self, int command)
621 struct sk_buff *skb = NULL;
624 ASSERT( self != NULL, return;);
625 ASSERT( self->magic == LAP_MAGIC, return;);
627 skb = dev_alloc_skb( 32);
631 frame = skb_put( skb, 2);
633 frame[0] = self->caddr;
634 frame[0] |= (command) ? CMD_FRAME : 0;
636 frame[1] = (self->vs << 1);
638 frame[1] |= (self->vr << 5);
642 IRDA_DEBUG(4, "%s(), vr=%d, %ld\n", __FUNCTION__, self->vr, jiffies);
644 irlap_queue_xmit(self, skb);
648 * Function irlap_recv_rnr_frame (self, skb, info)
650 * Received RNR (Receive Not Ready) frame from peer station
653 static void irlap_recv_rnr_frame(struct irlap_cb *self, struct sk_buff *skb,
654 struct irlap_info *info, int command)
656 info->nr = skb->data[1] >> 5;
658 IRDA_DEBUG(4, "%s(), nr=%d, %ld\n", __FUNCTION__, info->nr, jiffies);
661 irlap_do_event(self, RECV_RNR_CMD, skb, info);
663 irlap_do_event(self, RECV_RNR_RSP, skb, info);
666 static void irlap_recv_rej_frame(struct irlap_cb *self, struct sk_buff *skb,
667 struct irlap_info *info, int command)
669 IRDA_DEBUG(0, "%s()\n", __FUNCTION__);
671 info->nr = skb->data[1] >> 5;
673 /* Check if this is a command or a response frame */
675 irlap_do_event(self, RECV_REJ_CMD, skb, info);
677 irlap_do_event(self, RECV_REJ_RSP, skb, info);
680 static void irlap_recv_srej_frame(struct irlap_cb *self, struct sk_buff *skb,
681 struct irlap_info *info, int command)
683 IRDA_DEBUG(0, "%s()\n", __FUNCTION__);
685 info->nr = skb->data[1] >> 5;
687 /* Check if this is a command or a response frame */
689 irlap_do_event(self, RECV_SREJ_CMD, skb, info);
691 irlap_do_event(self, RECV_SREJ_RSP, skb, info);
694 static void irlap_recv_disc_frame(struct irlap_cb *self, struct sk_buff *skb,
695 struct irlap_info *info, int command)
697 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
699 /* Check if this is a command or a response frame */
701 irlap_do_event(self, RECV_DISC_CMD, skb, info);
703 irlap_do_event(self, RECV_RD_RSP, skb, info);
707 * Function irlap_recv_ua_frame (skb, frame)
709 * Received UA (Unnumbered Acknowledgement) frame
712 static inline void irlap_recv_ua_frame(struct irlap_cb *self,
714 struct irlap_info *info)
716 irlap_do_event(self, RECV_UA_RSP, skb, info);
720 * Function irlap_send_data_primary(self, skb)
722 * Send I-frames as the primary station but without the poll bit set
725 void irlap_send_data_primary(struct irlap_cb *self, struct sk_buff *skb)
727 struct sk_buff *tx_skb;
729 if (skb->data[1] == I_FRAME) {
732 * Insert frame sequence number (Vs) in control field before
733 * inserting into transmit window queue.
735 skb->data[1] = I_FRAME | (self->vs << 1);
738 tx_skb = skb_clone(skb, GFP_ATOMIC);
739 if (tx_skb == NULL) {
744 * Insert frame in store, in case of retransmissions
746 skb_queue_tail(&self->wx_list, skb_get(skb));
748 self->vs = (self->vs + 1) % 8;
749 self->ack_required = FALSE;
752 irlap_send_i_frame( self, tx_skb, CMD_FRAME);
754 IRDA_DEBUG(4, "%s(), sending unreliable frame\n", __FUNCTION__);
755 irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
760 * Function irlap_send_data_primary_poll (self, skb)
762 * Send I(nformation) frame as primary with poll bit set
764 void irlap_send_data_primary_poll(struct irlap_cb *self, struct sk_buff *skb)
766 struct sk_buff *tx_skb;
769 del_timer(&self->poll_timer);
771 /* Is this reliable or unreliable data? */
772 if (skb->data[1] == I_FRAME) {
775 * Insert frame sequence number (Vs) in control field before
776 * inserting into transmit window queue.
778 skb->data[1] = I_FRAME | (self->vs << 1);
781 tx_skb = skb_clone(skb, GFP_ATOMIC);
782 if (tx_skb == NULL) {
787 * Insert frame in store, in case of retransmissions
789 skb_queue_tail(&self->wx_list, skb_get(skb));
792 * Set poll bit if necessary. We do this to the copied
793 * skb, since retransmitted need to set or clear the poll
794 * bit depending on when they are sent.
796 tx_skb->data[1] |= PF_BIT;
798 self->vs = (self->vs + 1) % 8;
799 self->ack_required = FALSE;
801 irlap_send_i_frame(self, tx_skb, CMD_FRAME);
803 IRDA_DEBUG(4, "%s(), sending unreliable frame\n", __FUNCTION__);
805 if (self->ack_required) {
806 irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
807 irlap_send_rr_frame(self, CMD_FRAME);
808 self->ack_required = FALSE;
810 skb->data[1] |= PF_BIT;
811 irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
815 self->window = self->window_size;
816 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
817 /* We are allowed to transmit a maximum number of bytes again. */
818 self->bytes_left = self->line_capacity;
819 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
821 irlap_start_final_timer(self, self->final_timeout);
825 * Function irlap_send_data_secondary_final (self, skb)
827 * Send I(nformation) frame as secondary with final bit set
830 void irlap_send_data_secondary_final(struct irlap_cb *self,
833 struct sk_buff *tx_skb = NULL;
835 ASSERT(self != NULL, return;);
836 ASSERT(self->magic == LAP_MAGIC, return;);
837 ASSERT(skb != NULL, return;);
839 /* Is this reliable or unreliable data? */
840 if (skb->data[1] == I_FRAME) {
843 * Insert frame sequence number (Vs) in control field before
844 * inserting into transmit window queue.
846 skb->data[1] = I_FRAME | (self->vs << 1);
848 tx_skb = skb_clone(skb, GFP_ATOMIC);
849 if (tx_skb == NULL) {
853 /* Insert frame in store */
854 skb_queue_tail(&self->wx_list, skb_get(skb));
856 tx_skb->data[1] |= PF_BIT;
858 self->vs = (self->vs + 1) % 8;
859 self->ack_required = FALSE;
861 irlap_send_i_frame(self, tx_skb, RSP_FRAME);
863 if (self->ack_required) {
864 irlap_send_ui_frame(self, skb_get(skb), self->caddr, RSP_FRAME);
865 irlap_send_rr_frame(self, RSP_FRAME);
866 self->ack_required = FALSE;
868 skb->data[1] |= PF_BIT;
869 irlap_send_ui_frame(self, skb_get(skb), self->caddr, RSP_FRAME);
873 self->window = self->window_size;
874 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
875 /* We are allowed to transmit a maximum number of bytes again. */
876 self->bytes_left = self->line_capacity;
877 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
879 irlap_start_wd_timer(self, self->wd_timeout);
883 * Function irlap_send_data_secondary (self, skb)
885 * Send I(nformation) frame as secondary without final bit set
888 void irlap_send_data_secondary(struct irlap_cb *self, struct sk_buff *skb)
890 struct sk_buff *tx_skb = NULL;
892 /* Is this reliable or unreliable data? */
893 if (skb->data[1] == I_FRAME) {
896 * Insert frame sequence number (Vs) in control field before
897 * inserting into transmit window queue.
899 skb->data[1] = I_FRAME | (self->vs << 1);
901 tx_skb = skb_clone(skb, GFP_ATOMIC);
902 if (tx_skb == NULL) {
906 /* Insert frame in store */
907 skb_queue_tail(&self->wx_list, skb_get(skb));
909 self->vs = (self->vs + 1) % 8;
910 self->ack_required = FALSE;
913 irlap_send_i_frame(self, tx_skb, RSP_FRAME);
915 irlap_send_ui_frame(self, skb_get(skb), self->caddr, RSP_FRAME);
921 * Function irlap_resend_rejected_frames (nr)
923 * Resend frames which has not been acknowledged. Should be safe to
924 * traverse the list without locking it since this function will only be
925 * called from interrupt context (BH)
927 void irlap_resend_rejected_frames(struct irlap_cb *self, int command)
929 struct sk_buff *tx_skb;
933 ASSERT(self != NULL, return;);
934 ASSERT(self->magic == LAP_MAGIC, return;);
936 /* Initialize variables */
939 count = skb_queue_len(&self->wx_list);
941 /* Resend unacknowledged frame(s) */
942 skb = skb_peek(&self->wx_list);
943 while (skb != NULL) {
944 irlap_wait_min_turn_around(self, &self->qos_tx);
946 /* We copy the skb to be retransmitted since we will have to
947 * modify it. Cloning will confuse packet sniffers
949 /* tx_skb = skb_clone( skb, GFP_ATOMIC); */
950 tx_skb = skb_copy(skb, GFP_ATOMIC);
952 IRDA_DEBUG(0, "%s(), unable to copy\n", __FUNCTION__);
955 /* Unlink tx_skb from list */
956 tx_skb->next = tx_skb->prev = NULL;
959 /* Clear old Nr field + poll bit */
960 tx_skb->data[1] &= 0x0f;
963 * Set poll bit on the last frame retransmitted
966 tx_skb->data[1] |= PF_BIT; /* Set p/f bit */
968 tx_skb->data[1] &= ~PF_BIT; /* Clear p/f bit */
970 irlap_send_i_frame(self, tx_skb, command);
973 * If our skb is the last buffer in the list, then
974 * we are finished, if not, move to the next sk-buffer
976 if (skb == skb_peek_tail(&self->wx_list))
983 * We can now fill the window with additinal data frames
985 while (skb_queue_len( &self->txq) > 0) {
987 IRDA_DEBUG(0, "%s(), sending additional frames!\n", __FUNCTION__);
988 if ((skb_queue_len( &self->txq) > 0) &&
989 (self->window > 0)) {
990 skb = skb_dequeue( &self->txq);
991 ASSERT(skb != NULL, return;);
994 * If send window > 1 then send frame with pf
997 if ((self->window > 1) &&
998 skb_queue_len(&self->txq) > 0)
1000 irlap_send_data_primary(self, skb);
1002 irlap_send_data_primary_poll(self, skb);
1010 void irlap_resend_rejected_frame(struct irlap_cb *self, int command)
1012 struct sk_buff *tx_skb;
1013 struct sk_buff *skb;
1015 ASSERT(self != NULL, return;);
1016 ASSERT(self->magic == LAP_MAGIC, return;);
1018 /* Initialize variables */
1019 skb = tx_skb = NULL;
1021 /* Resend unacknowledged frame(s) */
1022 skb = skb_peek(&self->wx_list);
1024 irlap_wait_min_turn_around(self, &self->qos_tx);
1026 /* We copy the skb to be retransmitted since we will have to
1027 * modify it. Cloning will confuse packet sniffers
1029 /* tx_skb = skb_clone( skb, GFP_ATOMIC); */
1030 tx_skb = skb_copy(skb, GFP_ATOMIC);
1032 IRDA_DEBUG(0, "%s(), unable to copy\n", __FUNCTION__);
1035 /* Unlink tx_skb from list */
1036 tx_skb->next = tx_skb->prev = NULL;
1037 tx_skb->list = NULL;
1039 /* Clear old Nr field + poll bit */
1040 tx_skb->data[1] &= 0x0f;
1042 /* Set poll/final bit */
1043 tx_skb->data[1] |= PF_BIT; /* Set p/f bit */
1045 irlap_send_i_frame(self, tx_skb, command);
1050 * Function irlap_send_ui_frame (self, skb, command)
1052 * Contruct and transmit an Unnumbered Information (UI) frame
1055 void irlap_send_ui_frame(struct irlap_cb *self, struct sk_buff *skb,
1056 __u8 caddr, int command)
1058 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1060 ASSERT(self != NULL, return;);
1061 ASSERT(self->magic == LAP_MAGIC, return;);
1062 ASSERT(skb != NULL, return;);
1064 /* Insert connection address */
1065 skb->data[0] = caddr | ((command) ? CMD_FRAME : 0);
1067 irlap_queue_xmit(self, skb);
1071 * Function irlap_send_i_frame (skb)
1073 * Contruct and transmit Information (I) frame
1075 void irlap_send_i_frame(struct irlap_cb *self, struct sk_buff *skb,
1078 /* Insert connection address */
1079 skb->data[0] = self->caddr;
1080 skb->data[0] |= (command) ? CMD_FRAME : 0;
1082 /* Insert next to receive (Vr) */
1083 skb->data[1] |= (self->vr << 5); /* insert nr */
1085 irlap_queue_xmit(self, skb);
1089 * Function irlap_recv_i_frame (skb, frame)
1091 * Receive and parse an I (Information) frame, no harm in making it inline
1092 * since it's called only from one single place (irlap_driver_rcv).
1094 static inline void irlap_recv_i_frame(struct irlap_cb *self,
1095 struct sk_buff *skb,
1096 struct irlap_info *info, int command)
1098 info->nr = skb->data[1] >> 5; /* Next to receive */
1099 info->pf = skb->data[1] & PF_BIT; /* Final bit */
1100 info->ns = (skb->data[1] >> 1) & 0x07; /* Next to send */
1102 /* Check if this is a command or a response frame */
1104 irlap_do_event(self, RECV_I_CMD, skb, info);
1106 irlap_do_event(self, RECV_I_RSP, skb, info);
1110 * Function irlap_recv_ui_frame (self, skb, info)
1112 * Receive and parse an Unnumbered Information (UI) frame
1115 static void irlap_recv_ui_frame(struct irlap_cb *self, struct sk_buff *skb,
1116 struct irlap_info *info)
1118 IRDA_DEBUG( 4, "%s()\n", __FUNCTION__);
1120 info->pf = skb->data[1] & PF_BIT; /* Final bit */
1122 irlap_do_event(self, RECV_UI_FRAME, skb, info);
1126 * Function irlap_recv_frmr_frame (skb, frame)
1128 * Received Frame Reject response.
1131 static void irlap_recv_frmr_frame(struct irlap_cb *self, struct sk_buff *skb,
1132 struct irlap_info *info)
1137 IRDA_DEBUG(0, "%s()\n", __FUNCTION__);
1139 ASSERT(self != NULL, return;);
1140 ASSERT(self->magic == LAP_MAGIC, return;);
1141 ASSERT(skb != NULL, return;);
1142 ASSERT(info != NULL, return;);
1146 info->nr = frame[2] >> 5; /* Next to receive */
1147 info->pf = frame[2] & PF_BIT; /* Final bit */
1148 info->ns = (frame[2] >> 1) & 0x07; /* Next to send */
1150 w = frame[3] & 0x01;
1151 x = frame[3] & 0x02;
1152 y = frame[3] & 0x04;
1153 z = frame[3] & 0x08;
1156 IRDA_DEBUG(0, "Rejected control field is undefined or not "
1160 IRDA_DEBUG(0, "Rejected control field was invalid because it "
1161 "contained a non permitted I field.\n");
1164 IRDA_DEBUG(0, "Received I field exceeded the maximum negotiated "
1165 "for the existing connection or exceeded the maximum "
1166 "this station supports if no connection exists.\n");
1169 IRDA_DEBUG(0, "Rejected control field control field contained an "
1170 "invalid Nr count.\n");
1172 irlap_do_event(self, RECV_FRMR_RSP, skb, info);
1176 * Function irlap_send_test_frame (self, daddr)
1178 * Send a test frame response
1181 void irlap_send_test_frame(struct irlap_cb *self, __u8 caddr, __u32 daddr,
1182 struct sk_buff *cmd)
1184 struct sk_buff *skb;
1185 struct test_frame *frame;
1188 skb = dev_alloc_skb(cmd->len+sizeof(struct test_frame));
1192 /* Broadcast frames must include saddr and daddr fields */
1193 if (caddr == CBROADCAST) {
1194 frame = (struct test_frame *)
1195 skb_put(skb, sizeof(struct test_frame));
1197 /* Insert the swapped addresses */
1198 frame->saddr = cpu_to_le32(self->saddr);
1199 frame->daddr = cpu_to_le32(daddr);
1201 frame = (struct test_frame *) skb_put(skb, LAP_ADDR_HEADER + LAP_CTRL_HEADER);
1203 frame->caddr = caddr;
1204 frame->control = TEST_RSP | PF_BIT;
1207 info = skb_put(skb, cmd->len);
1208 memcpy(info, cmd->data, cmd->len);
1210 /* Return to sender */
1211 irlap_wait_min_turn_around(self, &self->qos_tx);
1212 irlap_queue_xmit(self, skb);
1216 * Function irlap_recv_test_frame (self, skb)
1218 * Receive a test frame
1221 static void irlap_recv_test_frame(struct irlap_cb *self, struct sk_buff *skb,
1222 struct irlap_info *info, int command)
1224 struct test_frame *frame;
1226 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
1228 frame = (struct test_frame *) skb->data;
1230 /* Broadcast frames must carry saddr and daddr fields */
1231 if (info->caddr == CBROADCAST) {
1232 if (skb->len < sizeof(struct test_frame)) {
1233 IRDA_DEBUG(0, "%s() test frame to short!\n", __FUNCTION__);
1237 /* Read and swap addresses */
1238 info->daddr = le32_to_cpu(frame->saddr);
1239 info->saddr = le32_to_cpu(frame->daddr);
1241 /* Make sure frame is addressed to us */
1242 if ((info->saddr != self->saddr) &&
1243 (info->saddr != BROADCAST)) {
1249 irlap_do_event(self, RECV_TEST_CMD, skb, info);
1251 irlap_do_event(self, RECV_TEST_RSP, skb, info);
1255 * Function irlap_driver_rcv (skb, netdev, ptype)
1257 * Called when a frame is received. Dispatches the right receive function
1258 * for processing of the frame.
1261 int irlap_driver_rcv(struct sk_buff *skb, struct net_device *dev,
1262 struct packet_type *ptype)
1264 struct irlap_info info;
1265 struct irlap_cb *self;
1269 /* FIXME: should we get our own field? */
1270 self = (struct irlap_cb *) dev->atalk_ptr;
1272 /* If the net device is down, then IrLAP is gone! */
1273 if (!self || self->magic != LAP_MAGIC) {
1278 /* Check if frame is large enough for parsing */
1280 ERROR("%s(), frame to short!\n", __FUNCTION__);
1285 command = skb->data[0] & CMD_FRAME;
1286 info.caddr = skb->data[0] & CBROADCAST;
1288 info.pf = skb->data[1] & PF_BIT;
1289 info.control = skb->data[1] & ~PF_BIT; /* Mask away poll/final bit */
1291 control = info.control;
1293 /* First we check if this frame has a valid connection address */
1294 if ((info.caddr != self->caddr) && (info.caddr != CBROADCAST)) {
1295 IRDA_DEBUG(0, "%s(), wrong connection address!\n", __FUNCTION__);
1299 * Optimize for the common case and check if the frame is an
1300 * I(nformation) frame. Only I-frames have bit 0 set to 0
1302 if (~control & 0x01) {
1303 irlap_recv_i_frame(self, skb, &info, command);
1307 * We now check is the frame is an S(upervisory) frame. Only
1308 * S-frames have bit 0 set to 1 and bit 1 set to 0
1310 if (~control & 0x02) {
1312 * Received S(upervisory) frame, check which frame type it is
1313 * only the first nibble is of interest
1315 switch (control & 0x0f) {
1317 irlap_recv_rr_frame(self, skb, &info, command);
1320 irlap_recv_rnr_frame(self, skb, &info, command);
1323 irlap_recv_rej_frame(self, skb, &info, command);
1326 irlap_recv_srej_frame(self, skb, &info, command);
1329 WARNING("%s() Unknown S-frame %02x received!\n", __FUNCTION__,
1336 * This must be a C(ontrol) frame
1340 irlap_recv_discovery_xid_rsp(self, skb, &info);
1343 irlap_recv_discovery_xid_cmd(self, skb, &info);
1346 irlap_recv_snrm_cmd(self, skb, &info);
1349 irlap_do_event(self, RECV_DM_RSP, skb, &info);
1351 case DISC_CMD: /* And RD_RSP since they have the same value */
1352 irlap_recv_disc_frame(self, skb, &info, command);
1355 irlap_recv_test_frame(self, skb, &info, command);
1358 irlap_recv_ua_frame(self, skb, &info);
1361 irlap_recv_frmr_frame(self, skb, &info);
1364 irlap_recv_ui_frame(self, skb, &info);
1367 WARNING("%s(), Unknown frame %02x received!\n", __FUNCTION__,