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 */
166 info->caddr = frame->ncaddr;
168 /* Check if the new connection address is valid */
169 if ((info->caddr == 0x00) || (info->caddr == 0xfe)) {
170 IRDA_DEBUG(3, __FUNCTION__
171 "(), invalid connection address!\n");
175 /* Copy peer device address */
176 info->daddr = le32_to_cpu(frame->saddr);
177 info->saddr = le32_to_cpu(frame->daddr);
179 /* Only accept if addressed directly to us */
180 if (info->saddr != self->saddr) {
181 IRDA_DEBUG(2, __FUNCTION__ "(), not addressed to us!\n");
184 irlap_do_event(self, RECV_SNRM_CMD, skb, info);
186 /* Signal that this SNRM frame does not contain and I-field */
187 irlap_do_event(self, RECV_SNRM_CMD, skb, NULL);
192 * Function irlap_send_ua_response_frame (qos)
194 * Send UA (Unnumbered Acknowledgement) frame
197 void irlap_send_ua_response_frame(struct irlap_cb *self, struct qos_info *qos)
200 struct ua_frame *frame;
203 IRDA_DEBUG(2, __FUNCTION__ "() <%ld>\n", jiffies);
205 ASSERT(self != NULL, return;);
206 ASSERT(self->magic == LAP_MAGIC, return;);
211 skb = dev_alloc_skb(64);
215 frame = (struct ua_frame *) skb_put(skb, 10);
217 /* Build UA response */
218 frame->caddr = self->caddr;
219 frame->control = UA_RSP | PF_BIT;
221 frame->saddr = cpu_to_le32(self->saddr);
222 frame->daddr = cpu_to_le32(self->daddr);
224 /* Should we send QoS negotiation parameters? */
226 ret = irlap_insert_qos_negotiation_params(self, skb);
233 irlap_queue_xmit(self, skb);
238 * Function irlap_send_dm_frame (void)
240 * Send disconnected mode (DM) frame
243 void irlap_send_dm_frame( struct irlap_cb *self)
245 struct sk_buff *skb = NULL;
248 ASSERT(self != NULL, return;);
249 ASSERT(self->magic == LAP_MAGIC, return;);
251 skb = dev_alloc_skb(32);
255 frame = skb_put( skb, 2);
257 if (self->state == LAP_NDM)
258 frame[0] = CBROADCAST;
260 frame[0] = self->caddr;
262 frame[1] = DM_RSP | PF_BIT;
264 irlap_queue_xmit(self, skb);
268 * Function irlap_send_disc_frame (void)
270 * Send disconnect (DISC) frame
273 void irlap_send_disc_frame(struct irlap_cb *self)
275 struct sk_buff *skb = NULL;
278 IRDA_DEBUG(3, __FUNCTION__ "()\n");
280 ASSERT(self != NULL, return;);
281 ASSERT(self->magic == LAP_MAGIC, return;);
283 skb = dev_alloc_skb(16);
287 frame = skb_put(skb, 2);
289 frame[0] = self->caddr | CMD_FRAME;
290 frame[1] = DISC_CMD | PF_BIT;
292 irlap_queue_xmit(self, skb);
296 * Function irlap_send_discovery_xid_frame (S, s, command)
298 * Build and transmit a XID (eXchange station IDentifier) discovery
301 void irlap_send_discovery_xid_frame(struct irlap_cb *self, int S, __u8 s,
302 __u8 command, discovery_t *discovery)
304 struct sk_buff *skb = NULL;
305 struct xid_frame *frame;
306 __u32 bcast = BROADCAST;
309 IRDA_DEBUG(4, __FUNCTION__ "(), s=%d, S=%d, command=%d\n", s, S,
312 ASSERT(self != NULL, return;);
313 ASSERT(self->magic == LAP_MAGIC, return;);
314 ASSERT(discovery != NULL, return;);
316 skb = dev_alloc_skb(64);
321 frame = (struct xid_frame *) skb->data;
324 frame->caddr = CBROADCAST | CMD_FRAME;
325 frame->control = XID_CMD | PF_BIT;
327 frame->caddr = CBROADCAST;
328 frame->control = XID_RSP | PF_BIT;
330 frame->ident = XID_FORMAT;
332 frame->saddr = cpu_to_le32(self->saddr);
335 frame->daddr = cpu_to_le32(bcast);
337 frame->daddr = cpu_to_le32(discovery->daddr);
358 frame->version = 0x00;
361 * Provide info for final slot only in commands, and for all
362 * responses. Send the second byte of the hint only if the
363 * EXTENSION bit is set in the first byte.
365 if (!command || (frame->slotnr == 0xff)) {
368 if (discovery->hints.byte[0] & HINT_EXTENSION) {
369 info = skb_put(skb, 2);
370 info[0] = discovery->hints.byte[0];
371 info[1] = discovery->hints.byte[1];
373 info = skb_put(skb, 1);
374 info[0] = discovery->hints.byte[0];
376 info = skb_put(skb, 1);
377 info[0] = discovery->charset;
379 len = IRDA_MIN(discovery->name_len, skb_tailroom(skb));
380 info = skb_put(skb, len);
381 memcpy(info, discovery->nickname, len);
383 irlap_queue_xmit(self, skb);
387 * Function irlap_recv_discovery_xid_rsp (skb, info)
389 * Received a XID discovery response
392 static void irlap_recv_discovery_xid_rsp(struct irlap_cb *self,
394 struct irlap_info *info)
396 struct xid_frame *xid;
397 discovery_t *discovery = NULL;
398 __u8 *discovery_info;
401 IRDA_DEBUG(4, __FUNCTION__ "()\n");
403 ASSERT(self != NULL, return;);
404 ASSERT(self->magic == LAP_MAGIC, return;);
406 xid = (struct xid_frame *) skb->data;
408 info->daddr = le32_to_cpu(xid->saddr);
409 info->saddr = le32_to_cpu(xid->daddr);
411 /* Make sure frame is addressed to us */
412 if ((info->saddr != self->saddr) && (info->saddr != BROADCAST)) {
413 IRDA_DEBUG(0, __FUNCTION__
414 "(), frame is not addressed to us!\n");
418 if ((discovery = kmalloc(sizeof(discovery_t), GFP_ATOMIC)) == NULL) {
419 WARNING(__FUNCTION__ "(), kmalloc failed!\n");
422 memset(discovery, 0, sizeof(discovery_t));
424 discovery->daddr = info->daddr;
425 discovery->saddr = self->saddr;
426 discovery->timestamp = jiffies;
428 IRDA_DEBUG(4, __FUNCTION__ "(), daddr=%08x\n", discovery->daddr);
430 discovery_info = skb_pull(skb, sizeof(struct xid_frame));
432 /* Get info returned from peer */
433 discovery->hints.byte[0] = discovery_info[0];
434 if (discovery_info[0] & HINT_EXTENSION) {
435 IRDA_DEBUG(4, "EXTENSION\n");
436 discovery->hints.byte[1] = discovery_info[1];
437 discovery->charset = discovery_info[2];
438 text = (char *) &discovery_info[3];
440 discovery->hints.byte[1] = 0;
441 discovery->charset = discovery_info[1];
442 text = (char *) &discovery_info[2];
445 * Terminate info string, should be safe since this is where the
448 skb->data[skb->len] = '\0';
449 strncpy(discovery->nickname, text, NICKNAME_MAX_LEN);
450 discovery->name_len = strlen(discovery->nickname);
452 info->discovery = discovery;
454 irlap_do_event(self, RECV_DISCOVERY_XID_RSP, skb, info);
458 * Function irlap_recv_discovery_xid_cmd (skb, info)
460 * Received a XID discovery command
463 static void irlap_recv_discovery_xid_cmd(struct irlap_cb *self,
465 struct irlap_info *info)
467 struct xid_frame *xid;
468 discovery_t *discovery = NULL;
469 __u8 *discovery_info;
472 xid = (struct xid_frame *) skb->data;
474 info->daddr = le32_to_cpu(xid->saddr);
475 info->saddr = le32_to_cpu(xid->daddr);
477 /* Make sure frame is addressed to us */
478 if ((info->saddr != self->saddr) && (info->saddr != BROADCAST)) {
479 IRDA_DEBUG(0, __FUNCTION__
480 "(), frame is not addressed to us!\n");
484 switch (xid->flags & 0x03) {
502 info->s = xid->slotnr;
504 discovery_info = skb_pull(skb, sizeof(struct xid_frame));
507 * Check if last frame
509 if (info->s == 0xff) {
510 /* Check if things are sane at this point... */
511 if((discovery_info == NULL) || (skb->len < 3)) {
512 ERROR(__FUNCTION__ "(), discovery frame to short!\n");
517 * We now have some discovery info to deliver!
519 discovery = kmalloc(sizeof(discovery_t), GFP_ATOMIC);
521 WARNING(__FUNCTION__ "(), unable to malloc!\n");
525 discovery->daddr = info->daddr;
526 discovery->saddr = self->saddr;
527 discovery->timestamp = jiffies;
529 discovery->hints.byte[0] = discovery_info[0];
530 if (discovery_info[0] & HINT_EXTENSION) {
531 discovery->hints.byte[1] = discovery_info[1];
532 discovery->charset = discovery_info[2];
533 text = (char *) &discovery_info[3];
535 discovery->hints.byte[1] = 0;
536 discovery->charset = discovery_info[1];
537 text = (char *) &discovery_info[2];
540 * Terminate string, should be safe since this is where the
543 skb->data[skb->len] = '\0';
544 strncpy(discovery->nickname, text, NICKNAME_MAX_LEN);
545 discovery->name_len = strlen(discovery->nickname);
547 info->discovery = discovery;
549 info->discovery = NULL;
551 irlap_do_event(self, RECV_DISCOVERY_XID_CMD, skb, info);
555 * Function irlap_send_rr_frame (self, command)
557 * Build and transmit RR (Receive Ready) frame. Notice that it is currently
558 * only possible to send RR frames with the poll bit set.
560 void irlap_send_rr_frame(struct irlap_cb *self, int command)
565 skb = dev_alloc_skb(16);
569 frame = skb_put(skb, 2);
571 frame[0] = self->caddr;
572 frame[0] |= (command) ? CMD_FRAME : 0;
574 frame[1] = RR | PF_BIT | (self->vr << 5);
576 irlap_queue_xmit(self, skb);
580 * Function irlap_send_rd_frame (self)
582 * Request disconnect. Used by a secondary station to request the
583 * disconnection of the link.
585 void irlap_send_rd_frame(struct irlap_cb *self)
590 skb = dev_alloc_skb(16);
594 frame = skb_put(skb, 2);
596 frame[0] = self->caddr;
597 frame[1] = RD_RSP | PF_BIT;
599 irlap_queue_xmit(self, skb);
603 * Function irlap_recv_rr_frame (skb, info)
605 * Received RR (Receive Ready) frame from peer station, no harm in
606 * making it inline since its called only from one single place
607 * (irlap_driver_rcv).
609 static inline void irlap_recv_rr_frame(struct irlap_cb *self,
611 struct irlap_info *info, int command)
613 info->nr = skb->data[1] >> 5;
615 /* Check if this is a command or a response frame */
617 irlap_do_event(self, RECV_RR_CMD, skb, info);
619 irlap_do_event(self, RECV_RR_RSP, skb, info);
622 void irlap_send_frmr_frame( struct irlap_cb *self, int command)
624 struct sk_buff *skb = NULL;
627 ASSERT( self != NULL, return;);
628 ASSERT( self->magic == LAP_MAGIC, return;);
630 skb = dev_alloc_skb( 32);
634 frame = skb_put( skb, 2);
636 frame[0] = self->caddr;
637 frame[0] |= (command) ? CMD_FRAME : 0;
639 frame[1] = (self->vs << 1);
641 frame[1] |= (self->vr << 5);
645 IRDA_DEBUG(4, __FUNCTION__ "(), vr=%d, %ld\n",self->vr, jiffies);
647 irlap_queue_xmit(self, skb);
651 * Function irlap_recv_rnr_frame (self, skb, info)
653 * Received RNR (Receive Not Ready) frame from peer station
656 static void irlap_recv_rnr_frame(struct irlap_cb *self, struct sk_buff *skb,
657 struct irlap_info *info, int command)
659 info->nr = skb->data[1] >> 5;
661 IRDA_DEBUG(4, __FUNCTION__ "(), nr=%d, %ld\n", info->nr, jiffies);
664 irlap_do_event(self, RECV_RNR_CMD, skb, info);
666 irlap_do_event(self, RECV_RNR_RSP, skb, info);
669 static void irlap_recv_rej_frame(struct irlap_cb *self, struct sk_buff *skb,
670 struct irlap_info *info, int command)
672 IRDA_DEBUG(0, __FUNCTION__ "()\n");
674 info->nr = skb->data[1] >> 5;
676 /* Check if this is a command or a response frame */
678 irlap_do_event(self, RECV_REJ_CMD, skb, info);
680 irlap_do_event(self, RECV_REJ_RSP, skb, info);
683 static void irlap_recv_srej_frame(struct irlap_cb *self, struct sk_buff *skb,
684 struct irlap_info *info, int command)
686 IRDA_DEBUG(0, __FUNCTION__ "()\n");
688 info->nr = skb->data[1] >> 5;
690 /* Check if this is a command or a response frame */
692 irlap_do_event(self, RECV_SREJ_CMD, skb, info);
694 irlap_do_event(self, RECV_SREJ_RSP, skb, info);
697 static void irlap_recv_disc_frame(struct irlap_cb *self, struct sk_buff *skb,
698 struct irlap_info *info, int command)
700 IRDA_DEBUG(2, __FUNCTION__ "()\n");
702 /* Check if this is a command or a response frame */
704 irlap_do_event(self, RECV_DISC_CMD, skb, info);
706 irlap_do_event(self, RECV_RD_RSP, skb, info);
710 * Function irlap_recv_ua_frame (skb, frame)
712 * Received UA (Unnumbered Acknowledgement) frame
715 static inline void irlap_recv_ua_frame(struct irlap_cb *self,
717 struct irlap_info *info)
719 irlap_do_event(self, RECV_UA_RSP, skb, info);
723 * Function irlap_send_data_primary(self, skb)
725 * Send I-frames as the primary station but without the poll bit set
728 void irlap_send_data_primary(struct irlap_cb *self, struct sk_buff *skb)
730 struct sk_buff *tx_skb;
732 if (skb->data[1] == I_FRAME) {
735 * Insert frame sequence number (Vs) in control field before
736 * inserting into transmit window queue.
738 skb->data[1] = I_FRAME | (self->vs << 1);
741 tx_skb = skb_clone(skb, GFP_ATOMIC);
742 if (tx_skb == NULL) {
747 * Insert frame in store, in case of retransmissions
749 skb_queue_tail(&self->wx_list, skb_get(skb));
751 self->vs = (self->vs + 1) % 8;
752 self->ack_required = FALSE;
755 irlap_send_i_frame( self, tx_skb, CMD_FRAME);
757 IRDA_DEBUG(4, __FUNCTION__ "(), sending unreliable frame\n");
758 irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
763 * Function irlap_send_data_primary_poll (self, skb)
765 * Send I(nformation) frame as primary with poll bit set
767 void irlap_send_data_primary_poll(struct irlap_cb *self, struct sk_buff *skb)
769 struct sk_buff *tx_skb;
772 del_timer(&self->poll_timer);
774 /* Is this reliable or unreliable data? */
775 if (skb->data[1] == I_FRAME) {
778 * Insert frame sequence number (Vs) in control field before
779 * inserting into transmit window queue.
781 skb->data[1] = I_FRAME | (self->vs << 1);
784 tx_skb = skb_clone(skb, GFP_ATOMIC);
785 if (tx_skb == NULL) {
790 * Insert frame in store, in case of retransmissions
792 skb_queue_tail(&self->wx_list, skb_get(skb));
795 * Set poll bit if necessary. We do this to the copied
796 * skb, since retransmitted need to set or clear the poll
797 * bit depending on when they are sent.
799 tx_skb->data[1] |= PF_BIT;
801 self->vs = (self->vs + 1) % 8;
802 self->ack_required = FALSE;
804 irlap_send_i_frame(self, tx_skb, CMD_FRAME);
806 IRDA_DEBUG(4, __FUNCTION__ "(), sending unreliable frame\n");
808 if (self->ack_required) {
809 irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
810 irlap_send_rr_frame(self, CMD_FRAME);
811 self->ack_required = FALSE;
813 skb->data[1] |= PF_BIT;
814 irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
818 self->window = self->window_size;
819 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
820 /* We are allowed to transmit a maximum number of bytes again. */
821 self->bytes_left = self->line_capacity;
822 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
824 irlap_start_final_timer(self, self->final_timeout);
828 * Function irlap_send_data_secondary_final (self, skb)
830 * Send I(nformation) frame as secondary with final bit set
833 void irlap_send_data_secondary_final(struct irlap_cb *self,
836 struct sk_buff *tx_skb = NULL;
838 ASSERT(self != NULL, return;);
839 ASSERT(self->magic == LAP_MAGIC, return;);
840 ASSERT(skb != NULL, return;);
842 /* Is this reliable or unreliable data? */
843 if (skb->data[1] == I_FRAME) {
846 * Insert frame sequence number (Vs) in control field before
847 * inserting into transmit window queue.
849 skb->data[1] = I_FRAME | (self->vs << 1);
851 tx_skb = skb_clone(skb, GFP_ATOMIC);
852 if (tx_skb == NULL) {
856 /* Insert frame in store */
857 skb_queue_tail(&self->wx_list, skb_get(skb));
859 tx_skb->data[1] |= PF_BIT;
861 self->vs = (self->vs + 1) % 8;
862 self->ack_required = FALSE;
864 irlap_send_i_frame(self, tx_skb, RSP_FRAME);
866 if (self->ack_required) {
867 irlap_send_ui_frame(self, skb_get(skb), self->caddr, RSP_FRAME);
868 irlap_send_rr_frame(self, RSP_FRAME);
869 self->ack_required = FALSE;
871 skb->data[1] |= PF_BIT;
872 irlap_send_ui_frame(self, skb_get(skb), self->caddr, RSP_FRAME);
876 self->window = self->window_size;
877 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
878 /* We are allowed to transmit a maximum number of bytes again. */
879 self->bytes_left = self->line_capacity;
880 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
882 irlap_start_wd_timer(self, self->wd_timeout);
886 * Function irlap_send_data_secondary (self, skb)
888 * Send I(nformation) frame as secondary without final bit set
891 void irlap_send_data_secondary(struct irlap_cb *self, struct sk_buff *skb)
893 struct sk_buff *tx_skb = NULL;
895 /* Is this reliable or unreliable data? */
896 if (skb->data[1] == I_FRAME) {
899 * Insert frame sequence number (Vs) in control field before
900 * inserting into transmit window queue.
902 skb->data[1] = I_FRAME | (self->vs << 1);
904 tx_skb = skb_clone(skb, GFP_ATOMIC);
905 if (tx_skb == NULL) {
909 /* Insert frame in store */
910 skb_queue_tail(&self->wx_list, skb_get(skb));
912 self->vs = (self->vs + 1) % 8;
913 self->ack_required = FALSE;
916 irlap_send_i_frame(self, tx_skb, RSP_FRAME);
918 irlap_send_ui_frame(self, skb_get(skb), self->caddr, RSP_FRAME);
924 * Function irlap_resend_rejected_frames (nr)
926 * Resend frames which has not been acknowledged. Should be safe to
927 * traverse the list without locking it since this function will only be
928 * called from interrupt context (BH)
930 void irlap_resend_rejected_frames(struct irlap_cb *self, int command)
932 struct sk_buff *tx_skb;
936 ASSERT(self != NULL, return;);
937 ASSERT(self->magic == LAP_MAGIC, return;);
939 /* Initialize variables */
942 count = skb_queue_len(&self->wx_list);
944 /* Resend unacknowledged frame(s) */
945 skb = skb_peek(&self->wx_list);
946 while (skb != NULL) {
947 irlap_wait_min_turn_around(self, &self->qos_tx);
949 /* We copy the skb to be retransmitted since we will have to
950 * modify it. Cloning will confuse packet sniffers
952 /* tx_skb = skb_clone( skb, GFP_ATOMIC); */
953 tx_skb = skb_copy(skb, GFP_ATOMIC);
955 IRDA_DEBUG(0, __FUNCTION__ "(), unable to copy\n");
958 /* Unlink tx_skb from list */
959 tx_skb->next = tx_skb->prev = NULL;
962 /* Clear old Nr field + poll bit */
963 tx_skb->data[1] &= 0x0f;
966 * Set poll bit on the last frame retransmitted
969 tx_skb->data[1] |= PF_BIT; /* Set p/f bit */
971 tx_skb->data[1] &= ~PF_BIT; /* Clear p/f bit */
973 irlap_send_i_frame(self, tx_skb, command);
976 * If our skb is the last buffer in the list, then
977 * we are finished, if not, move to the next sk-buffer
979 if (skb == skb_peek_tail(&self->wx_list))
986 * We can now fill the window with additinal data frames
988 while (skb_queue_len( &self->txq) > 0) {
990 IRDA_DEBUG(0, __FUNCTION__ "(), sending additional frames!\n");
991 if ((skb_queue_len( &self->txq) > 0) &&
992 (self->window > 0)) {
993 skb = skb_dequeue( &self->txq);
994 ASSERT(skb != NULL, return;);
997 * If send window > 1 then send frame with pf
1000 if ((self->window > 1) &&
1001 skb_queue_len(&self->txq) > 0)
1003 irlap_send_data_primary(self, skb);
1005 irlap_send_data_primary_poll(self, skb);
1013 void irlap_resend_rejected_frame(struct irlap_cb *self, int command)
1015 struct sk_buff *tx_skb;
1016 struct sk_buff *skb;
1018 ASSERT(self != NULL, return;);
1019 ASSERT(self->magic == LAP_MAGIC, return;);
1021 /* Initialize variables */
1022 skb = tx_skb = NULL;
1024 /* Resend unacknowledged frame(s) */
1025 skb = skb_peek(&self->wx_list);
1027 irlap_wait_min_turn_around(self, &self->qos_tx);
1029 /* We copy the skb to be retransmitted since we will have to
1030 * modify it. Cloning will confuse packet sniffers
1032 /* tx_skb = skb_clone( skb, GFP_ATOMIC); */
1033 tx_skb = skb_copy(skb, GFP_ATOMIC);
1035 IRDA_DEBUG(0, __FUNCTION__ "(), unable to copy\n");
1038 /* Unlink tx_skb from list */
1039 tx_skb->next = tx_skb->prev = NULL;
1040 tx_skb->list = NULL;
1042 /* Clear old Nr field + poll bit */
1043 tx_skb->data[1] &= 0x0f;
1045 /* Set poll/final bit */
1046 tx_skb->data[1] |= PF_BIT; /* Set p/f bit */
1048 irlap_send_i_frame(self, tx_skb, command);
1053 * Function irlap_send_ui_frame (self, skb, command)
1055 * Contruct and transmit an Unnumbered Information (UI) frame
1058 void irlap_send_ui_frame(struct irlap_cb *self, struct sk_buff *skb,
1059 __u8 caddr, int command)
1061 IRDA_DEBUG(4, __FUNCTION__ "()\n");
1063 ASSERT(self != NULL, return;);
1064 ASSERT(self->magic == LAP_MAGIC, return;);
1065 ASSERT(skb != NULL, return;);
1067 /* Insert connection address */
1068 skb->data[0] = caddr | ((command) ? CMD_FRAME : 0);
1070 irlap_queue_xmit(self, skb);
1074 * Function irlap_send_i_frame (skb)
1076 * Contruct and transmit Information (I) frame
1078 void irlap_send_i_frame(struct irlap_cb *self, struct sk_buff *skb,
1081 /* Insert connection address */
1082 skb->data[0] = self->caddr;
1083 skb->data[0] |= (command) ? CMD_FRAME : 0;
1085 /* Insert next to receive (Vr) */
1086 skb->data[1] |= (self->vr << 5); /* insert nr */
1088 irlap_queue_xmit(self, skb);
1092 * Function irlap_recv_i_frame (skb, frame)
1094 * Receive and parse an I (Information) frame, no harm in making it inline
1095 * since it's called only from one single place (irlap_driver_rcv).
1097 static inline void irlap_recv_i_frame(struct irlap_cb *self,
1098 struct sk_buff *skb,
1099 struct irlap_info *info, int command)
1101 info->nr = skb->data[1] >> 5; /* Next to receive */
1102 info->pf = skb->data[1] & PF_BIT; /* Final bit */
1103 info->ns = (skb->data[1] >> 1) & 0x07; /* Next to send */
1105 /* Check if this is a command or a response frame */
1107 irlap_do_event(self, RECV_I_CMD, skb, info);
1109 irlap_do_event(self, RECV_I_RSP, skb, info);
1113 * Function irlap_recv_ui_frame (self, skb, info)
1115 * Receive and parse an Unnumbered Information (UI) frame
1118 static void irlap_recv_ui_frame(struct irlap_cb *self, struct sk_buff *skb,
1119 struct irlap_info *info)
1121 IRDA_DEBUG( 4, __FUNCTION__ "()\n");
1123 info->pf = skb->data[1] & PF_BIT; /* Final bit */
1125 irlap_do_event(self, RECV_UI_FRAME, skb, info);
1129 * Function irlap_recv_frmr_frame (skb, frame)
1131 * Received Frame Reject response.
1134 static void irlap_recv_frmr_frame(struct irlap_cb *self, struct sk_buff *skb,
1135 struct irlap_info *info)
1140 IRDA_DEBUG(0, __FUNCTION__ "()\n");
1142 ASSERT(self != NULL, return;);
1143 ASSERT(self->magic == LAP_MAGIC, return;);
1144 ASSERT(skb != NULL, return;);
1145 ASSERT(info != NULL, return;);
1149 info->nr = frame[2] >> 5; /* Next to receive */
1150 info->pf = frame[2] & PF_BIT; /* Final bit */
1151 info->ns = (frame[2] >> 1) & 0x07; /* Next to send */
1153 w = frame[3] & 0x01;
1154 x = frame[3] & 0x02;
1155 y = frame[3] & 0x04;
1156 z = frame[3] & 0x08;
1159 IRDA_DEBUG(0, "Rejected control field is undefined or not "
1163 IRDA_DEBUG(0, "Rejected control field was invalid because it "
1164 "contained a non permitted I field.\n");
1167 IRDA_DEBUG(0, "Received I field exceeded the maximum negotiated "
1168 "for the existing connection or exceeded the maximum "
1169 "this station supports if no connection exists.\n");
1172 IRDA_DEBUG(0, "Rejected control field control field contained an "
1173 "invalid Nr count.\n");
1175 irlap_do_event(self, RECV_FRMR_RSP, skb, info);
1179 * Function irlap_send_test_frame (self, daddr)
1181 * Send a test frame response
1184 void irlap_send_test_frame(struct irlap_cb *self, __u8 caddr, __u32 daddr,
1185 struct sk_buff *cmd)
1187 struct sk_buff *skb;
1188 struct test_frame *frame;
1191 skb = dev_alloc_skb(cmd->len+sizeof(struct test_frame));
1195 /* Broadcast frames must include saddr and daddr fields */
1196 if (caddr == CBROADCAST) {
1197 frame = (struct test_frame *)
1198 skb_put(skb, sizeof(struct test_frame));
1200 /* Insert the swapped addresses */
1201 frame->saddr = cpu_to_le32(self->saddr);
1202 frame->daddr = cpu_to_le32(daddr);
1204 frame = (struct test_frame *) skb_put(skb, LAP_ADDR_HEADER + LAP_CTRL_HEADER);
1206 frame->caddr = caddr;
1207 frame->control = TEST_RSP | PF_BIT;
1210 info = skb_put(skb, cmd->len);
1211 memcpy(info, cmd->data, cmd->len);
1213 /* Return to sender */
1214 irlap_wait_min_turn_around(self, &self->qos_tx);
1215 irlap_queue_xmit(self, skb);
1219 * Function irlap_recv_test_frame (self, skb)
1221 * Receive a test frame
1224 static void irlap_recv_test_frame(struct irlap_cb *self, struct sk_buff *skb,
1225 struct irlap_info *info, int command)
1227 struct test_frame *frame;
1229 IRDA_DEBUG(2, __FUNCTION__ "()\n");
1231 frame = (struct test_frame *) skb->data;
1233 /* Broadcast frames must carry saddr and daddr fields */
1234 if (info->caddr == CBROADCAST) {
1235 if (skb->len < sizeof(struct test_frame)) {
1236 IRDA_DEBUG(0, __FUNCTION__
1237 "() test frame to short!\n");
1241 /* Read and swap addresses */
1242 info->daddr = le32_to_cpu(frame->saddr);
1243 info->saddr = le32_to_cpu(frame->daddr);
1245 /* Make sure frame is addressed to us */
1246 if ((info->saddr != self->saddr) &&
1247 (info->saddr != BROADCAST)) {
1253 irlap_do_event(self, RECV_TEST_CMD, skb, info);
1255 irlap_do_event(self, RECV_TEST_RSP, skb, info);
1259 * Function irlap_driver_rcv (skb, netdev, ptype)
1261 * Called when a frame is received. Dispatches the right receive function
1262 * for processing of the frame.
1265 int irlap_driver_rcv(struct sk_buff *skb, struct net_device *dev,
1266 struct packet_type *ptype)
1268 struct irlap_info info;
1269 struct irlap_cb *self;
1273 /* FIXME: should we get our own field? */
1274 self = (struct irlap_cb *) dev->atalk_ptr;
1276 /* If the net device is down, then IrLAP is gone! */
1277 if (!self || self->magic != LAP_MAGIC) {
1282 /* Check if frame is large enough for parsing */
1284 ERROR(__FUNCTION__ "(), frame to short!\n");
1289 command = skb->data[0] & CMD_FRAME;
1290 info.caddr = skb->data[0] & CBROADCAST;
1292 info.pf = skb->data[1] & PF_BIT;
1293 info.control = skb->data[1] & ~PF_BIT; /* Mask away poll/final bit */
1295 control = info.control;
1297 /* First we check if this frame has a valid connection address */
1298 if ((info.caddr != self->caddr) && (info.caddr != CBROADCAST)) {
1299 IRDA_DEBUG(0, __FUNCTION__ "(), wrong connection address!\n");
1303 * Optimize for the common case and check if the frame is an
1304 * I(nformation) frame. Only I-frames have bit 0 set to 0
1306 if (~control & 0x01) {
1307 irlap_recv_i_frame(self, skb, &info, command);
1311 * We now check is the frame is an S(upervisory) frame. Only
1312 * S-frames have bit 0 set to 1 and bit 1 set to 0
1314 if (~control & 0x02) {
1316 * Received S(upervisory) frame, check which frame type it is
1317 * only the first nibble is of interest
1319 switch (control & 0x0f) {
1321 irlap_recv_rr_frame(self, skb, &info, command);
1324 irlap_recv_rnr_frame(self, skb, &info, command);
1327 irlap_recv_rej_frame(self, skb, &info, command);
1330 irlap_recv_srej_frame(self, skb, &info, command);
1333 WARNING(__FUNCTION__
1334 "() Unknown S-frame %02x received!\n",
1341 * This must be a C(ontrol) frame
1345 irlap_recv_discovery_xid_rsp(self, skb, &info);
1348 irlap_recv_discovery_xid_cmd(self, skb, &info);
1351 irlap_recv_snrm_cmd(self, skb, &info);
1354 irlap_do_event(self, RECV_DM_RSP, skb, &info);
1356 case DISC_CMD: /* And RD_RSP since they have the same value */
1357 irlap_recv_disc_frame(self, skb, &info, command);
1360 irlap_recv_test_frame(self, skb, &info, command);
1363 irlap_recv_ua_frame(self, skb, &info);
1366 irlap_recv_frmr_frame(self, skb, &info);
1369 irlap_recv_ui_frame(self, skb, &info);
1372 WARNING(__FUNCTION__ "(), Unknown frame %02x received!\n",