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-2003 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>
46 static void irlap_send_i_frame(struct irlap_cb *self, struct sk_buff *skb,
50 * Function irlap_insert_info (self, skb)
52 * Insert minimum turnaround time and speed information into the skb. We
53 * need to do this since it's per packet relevant information. Safe to
54 * have this function inlined since it's only called from one place
56 static inline void irlap_insert_info(struct irlap_cb *self,
59 struct irda_skb_cb *cb = (struct irda_skb_cb *) skb->cb;
62 * Insert MTT (min. turn time) and speed into skb, so that the
63 * device driver knows which settings to use
65 cb->magic = LAP_MAGIC;
66 cb->mtt = self->mtt_required;
67 cb->next_speed = self->speed;
70 self->mtt_required = 0;
73 * Delay equals negotiated BOFs count, plus the number of BOFs to
74 * force the negotiated minimum turnaround time
76 cb->xbofs = self->bofs_count;
77 cb->next_xbofs = self->next_bofs;
78 cb->xbofs_delay = self->xbofs_delay;
80 /* Reset XBOF's delay (used only for getting min turn time) */
81 self->xbofs_delay = 0;
82 /* Put the correct xbofs value for the next packet */
83 self->bofs_count = self->next_bofs;
87 * Function irlap_queue_xmit (self, skb)
89 * A little wrapper for dev_queue_xmit, so we can insert some common
92 void irlap_queue_xmit(struct irlap_cb *self, struct sk_buff *skb)
94 /* Some common init stuff */
95 skb->dev = self->netdev;
96 skb->h.raw = skb->nh.raw = skb->mac.raw = skb->data;
97 skb->protocol = htons(ETH_P_IRDA);
98 skb->priority = TC_PRIO_BESTEFFORT;
100 irlap_insert_info(self, skb);
106 * Function irlap_send_snrm_cmd (void)
108 * Transmits a connect SNRM command frame
110 void irlap_send_snrm_frame(struct irlap_cb *self, struct qos_info *qos)
112 struct sk_buff *tx_skb;
113 struct snrm_frame *frame;
116 ASSERT(self != NULL, return;);
117 ASSERT(self->magic == LAP_MAGIC, return;);
120 tx_skb = dev_alloc_skb(64);
124 frame = (struct snrm_frame *) skb_put(tx_skb, 2);
126 /* Insert connection address field */
128 frame->caddr = CMD_FRAME | CBROADCAST;
130 frame->caddr = CMD_FRAME | self->caddr;
132 /* Insert control field */
133 frame->control = SNRM_CMD | PF_BIT;
136 * If we are establishing a connection then insert QoS paramerters
139 skb_put(tx_skb, 9); /* 21 left */
140 frame->saddr = cpu_to_le32(self->saddr);
141 frame->daddr = cpu_to_le32(self->daddr);
143 frame->ncaddr = self->caddr;
145 ret = irlap_insert_qos_negotiation_params(self, tx_skb);
147 dev_kfree_skb(tx_skb);
151 irlap_queue_xmit(self, tx_skb);
155 * Function irlap_recv_snrm_cmd (skb, info)
157 * Received SNRM (Set Normal Response Mode) command frame
160 static void irlap_recv_snrm_cmd(struct irlap_cb *self, struct sk_buff *skb,
161 struct irlap_info *info)
163 struct snrm_frame *frame;
165 if (pskb_may_pull(skb,sizeof(struct snrm_frame))) {
166 frame = (struct snrm_frame *) skb->data;
168 /* Copy the new connection address ignoring the C/R bit */
169 info->caddr = frame->ncaddr & 0xFE;
171 /* Check if the new connection address is valid */
172 if ((info->caddr == 0x00) || (info->caddr == 0xfe)) {
173 IRDA_DEBUG(3, "%s(), invalid connection address!\n",
178 /* Copy peer device address */
179 info->daddr = le32_to_cpu(frame->saddr);
180 info->saddr = le32_to_cpu(frame->daddr);
182 /* Only accept if addressed directly to us */
183 if (info->saddr != self->saddr) {
184 IRDA_DEBUG(2, "%s(), not addressed to us!\n",
188 irlap_do_event(self, RECV_SNRM_CMD, skb, info);
190 /* Signal that this SNRM frame does not contain and I-field */
191 irlap_do_event(self, RECV_SNRM_CMD, skb, NULL);
196 * Function irlap_send_ua_response_frame (qos)
198 * Send UA (Unnumbered Acknowledgement) frame
201 void irlap_send_ua_response_frame(struct irlap_cb *self, struct qos_info *qos)
203 struct sk_buff *tx_skb;
204 struct ua_frame *frame;
207 IRDA_DEBUG(2, "%s() <%ld>\n", __FUNCTION__, jiffies);
209 ASSERT(self != NULL, return;);
210 ASSERT(self->magic == LAP_MAGIC, return;);
213 tx_skb = dev_alloc_skb(64);
217 frame = (struct ua_frame *) skb_put(tx_skb, 10);
219 /* Build UA response */
220 frame->caddr = self->caddr;
221 frame->control = UA_RSP | PF_BIT;
223 frame->saddr = cpu_to_le32(self->saddr);
224 frame->daddr = cpu_to_le32(self->daddr);
226 /* Should we send QoS negotiation parameters? */
228 ret = irlap_insert_qos_negotiation_params(self, tx_skb);
230 dev_kfree_skb(tx_skb);
235 irlap_queue_xmit(self, tx_skb);
240 * Function irlap_send_dm_frame (void)
242 * Send disconnected mode (DM) frame
245 void irlap_send_dm_frame( struct irlap_cb *self)
247 struct sk_buff *tx_skb = NULL;
250 ASSERT(self != NULL, return;);
251 ASSERT(self->magic == LAP_MAGIC, return;);
253 tx_skb = dev_alloc_skb(32);
257 frame = skb_put(tx_skb, 2);
259 if (self->state == LAP_NDM)
260 frame[0] = CBROADCAST;
262 frame[0] = self->caddr;
264 frame[1] = DM_RSP | PF_BIT;
266 irlap_queue_xmit(self, tx_skb);
270 * Function irlap_send_disc_frame (void)
272 * Send disconnect (DISC) frame
275 void irlap_send_disc_frame(struct irlap_cb *self)
277 struct sk_buff *tx_skb = NULL;
280 IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
282 ASSERT(self != NULL, return;);
283 ASSERT(self->magic == LAP_MAGIC, return;);
285 tx_skb = dev_alloc_skb(16);
289 frame = skb_put(tx_skb, 2);
291 frame[0] = self->caddr | CMD_FRAME;
292 frame[1] = DISC_CMD | PF_BIT;
294 irlap_queue_xmit(self, tx_skb);
298 * Function irlap_send_discovery_xid_frame (S, s, command)
300 * Build and transmit a XID (eXchange station IDentifier) discovery
303 void irlap_send_discovery_xid_frame(struct irlap_cb *self, int S, __u8 s,
304 __u8 command, discovery_t *discovery)
306 struct sk_buff *tx_skb = NULL;
307 struct xid_frame *frame;
308 __u32 bcast = BROADCAST;
311 IRDA_DEBUG(4, "%s(), s=%d, S=%d, command=%d\n", __FUNCTION__,
314 ASSERT(self != NULL, return;);
315 ASSERT(self->magic == LAP_MAGIC, return;);
316 ASSERT(discovery != NULL, return;);
318 tx_skb = dev_alloc_skb(64);
323 frame = (struct xid_frame *) tx_skb->data;
326 frame->caddr = CBROADCAST | CMD_FRAME;
327 frame->control = XID_CMD | PF_BIT;
329 frame->caddr = CBROADCAST;
330 frame->control = XID_RSP | PF_BIT;
332 frame->ident = XID_FORMAT;
334 frame->saddr = cpu_to_le32(self->saddr);
337 frame->daddr = cpu_to_le32(bcast);
339 frame->daddr = cpu_to_le32(discovery->data.daddr);
360 frame->version = 0x00;
363 * Provide info for final slot only in commands, and for all
364 * responses. Send the second byte of the hint only if the
365 * EXTENSION bit is set in the first byte.
367 if (!command || (frame->slotnr == 0xff)) {
370 if (discovery->data.hints[0] & HINT_EXTENSION) {
371 info = skb_put(tx_skb, 2);
372 info[0] = discovery->data.hints[0];
373 info[1] = discovery->data.hints[1];
375 info = skb_put(tx_skb, 1);
376 info[0] = discovery->data.hints[0];
378 info = skb_put(tx_skb, 1);
379 info[0] = discovery->data.charset;
381 len = IRDA_MIN(discovery->name_len, skb_tailroom(tx_skb));
382 info = skb_put(tx_skb, len);
383 memcpy(info, discovery->data.info, len);
385 irlap_queue_xmit(self, tx_skb);
389 * Function irlap_recv_discovery_xid_rsp (skb, info)
391 * Received a XID discovery response
394 static void irlap_recv_discovery_xid_rsp(struct irlap_cb *self,
396 struct irlap_info *info)
398 struct xid_frame *xid;
399 discovery_t *discovery = NULL;
400 __u8 *discovery_info;
403 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
405 ASSERT(self != NULL, return;);
406 ASSERT(self->magic == LAP_MAGIC, return;);
408 if (!pskb_may_pull(skb, sizeof(struct xid_frame))) {
409 ERROR("%s: frame to short!\n", __FUNCTION__);
413 xid = (struct xid_frame *) skb->data;
415 info->daddr = le32_to_cpu(xid->saddr);
416 info->saddr = le32_to_cpu(xid->daddr);
418 /* Make sure frame is addressed to us */
419 if ((info->saddr != self->saddr) && (info->saddr != BROADCAST)) {
420 IRDA_DEBUG(0, "%s(), frame is not addressed to us!\n",
425 if ((discovery = kmalloc(sizeof(discovery_t), GFP_ATOMIC)) == NULL) {
426 WARNING("%s: kmalloc failed!\n", __FUNCTION__);
429 memset(discovery, 0, sizeof(discovery_t));
431 discovery->data.daddr = info->daddr;
432 discovery->data.saddr = self->saddr;
433 discovery->timestamp = jiffies;
435 IRDA_DEBUG(4, "%s(), daddr=%08x\n", __FUNCTION__,
436 discovery->data.daddr);
438 discovery_info = skb_pull(skb, sizeof(struct xid_frame));
440 /* Get info returned from peer */
441 discovery->data.hints[0] = discovery_info[0];
442 if (discovery_info[0] & HINT_EXTENSION) {
443 IRDA_DEBUG(4, "EXTENSION\n");
444 discovery->data.hints[1] = discovery_info[1];
445 discovery->data.charset = discovery_info[2];
446 text = (char *) &discovery_info[3];
448 discovery->data.hints[1] = 0;
449 discovery->data.charset = discovery_info[1];
450 text = (char *) &discovery_info[2];
453 * Terminate info string, should be safe since this is where the
456 skb->data[skb->len] = '\0';
457 strncpy(discovery->data.info, text, NICKNAME_MAX_LEN);
458 discovery->name_len = strlen(discovery->data.info);
460 info->discovery = discovery;
462 irlap_do_event(self, RECV_DISCOVERY_XID_RSP, skb, info);
466 * Function irlap_recv_discovery_xid_cmd (skb, info)
468 * Received a XID discovery command
471 static void irlap_recv_discovery_xid_cmd(struct irlap_cb *self,
473 struct irlap_info *info)
475 struct xid_frame *xid;
476 discovery_t *discovery = NULL;
477 __u8 *discovery_info;
480 if (!pskb_may_pull(skb, sizeof(struct xid_frame))) {
481 ERROR("%s: frame to short!\n", __FUNCTION__);
485 xid = (struct xid_frame *) skb->data;
487 info->daddr = le32_to_cpu(xid->saddr);
488 info->saddr = le32_to_cpu(xid->daddr);
490 /* Make sure frame is addressed to us */
491 if ((info->saddr != self->saddr) && (info->saddr != BROADCAST)) {
492 IRDA_DEBUG(0, "%s(), frame is not addressed to us!\n",
497 switch (xid->flags & 0x03) {
514 info->s = xid->slotnr;
516 discovery_info = skb_pull(skb, sizeof(struct xid_frame));
519 * Check if last frame
521 if (info->s == 0xff) {
522 /* Check if things are sane at this point... */
523 if((discovery_info == NULL) ||
524 !pskb_may_pull(skb, 3)) {
525 ERROR("%s: discovery frame to short!\n", __FUNCTION__);
530 * We now have some discovery info to deliver!
532 discovery = kmalloc(sizeof(discovery_t), GFP_ATOMIC);
534 WARNING("%s: unable to malloc!\n", __FUNCTION__);
538 discovery->data.daddr = info->daddr;
539 discovery->data.saddr = self->saddr;
540 discovery->timestamp = jiffies;
542 discovery->data.hints[0] = discovery_info[0];
543 if (discovery_info[0] & HINT_EXTENSION) {
544 discovery->data.hints[1] = discovery_info[1];
545 discovery->data.charset = discovery_info[2];
546 text = (char *) &discovery_info[3];
548 discovery->data.hints[1] = 0;
549 discovery->data.charset = discovery_info[1];
550 text = (char *) &discovery_info[2];
553 * Terminate string, should be safe since this is where the
556 skb->data[skb->len] = '\0';
557 strncpy(discovery->data.info, text, NICKNAME_MAX_LEN);
558 discovery->name_len = strlen(discovery->data.info);
560 info->discovery = discovery;
562 info->discovery = NULL;
564 irlap_do_event(self, RECV_DISCOVERY_XID_CMD, skb, info);
568 * Function irlap_send_rr_frame (self, command)
570 * Build and transmit RR (Receive Ready) frame. Notice that it is currently
571 * only possible to send RR frames with the poll bit set.
573 void irlap_send_rr_frame(struct irlap_cb *self, int command)
575 struct sk_buff *tx_skb;
578 tx_skb = dev_alloc_skb(16);
582 frame = skb_put(tx_skb, 2);
584 frame[0] = self->caddr;
585 frame[0] |= (command) ? CMD_FRAME : 0;
587 frame[1] = RR | PF_BIT | (self->vr << 5);
589 irlap_queue_xmit(self, tx_skb);
593 * Function irlap_send_rd_frame (self)
595 * Request disconnect. Used by a secondary station to request the
596 * disconnection of the link.
598 void irlap_send_rd_frame(struct irlap_cb *self)
600 struct sk_buff *tx_skb;
603 tx_skb = dev_alloc_skb(16);
607 frame = skb_put(tx_skb, 2);
609 frame[0] = self->caddr;
610 frame[1] = RD_RSP | PF_BIT;
612 irlap_queue_xmit(self, tx_skb);
616 * Function irlap_recv_rr_frame (skb, info)
618 * Received RR (Receive Ready) frame from peer station, no harm in
619 * making it inline since its called only from one single place
620 * (irlap_driver_rcv).
622 static inline void irlap_recv_rr_frame(struct irlap_cb *self,
624 struct irlap_info *info, int command)
626 info->nr = skb->data[1] >> 5;
628 /* Check if this is a command or a response frame */
630 irlap_do_event(self, RECV_RR_CMD, skb, info);
632 irlap_do_event(self, RECV_RR_RSP, skb, info);
636 * Function irlap_recv_rnr_frame (self, skb, info)
638 * Received RNR (Receive Not Ready) frame from peer station
641 static void irlap_recv_rnr_frame(struct irlap_cb *self, struct sk_buff *skb,
642 struct irlap_info *info, int command)
644 info->nr = skb->data[1] >> 5;
646 IRDA_DEBUG(4, "%s(), nr=%d, %ld\n", __FUNCTION__, info->nr, jiffies);
649 irlap_do_event(self, RECV_RNR_CMD, skb, info);
651 irlap_do_event(self, RECV_RNR_RSP, skb, info);
654 static void irlap_recv_rej_frame(struct irlap_cb *self, struct sk_buff *skb,
655 struct irlap_info *info, int command)
657 IRDA_DEBUG(0, "%s()\n", __FUNCTION__);
659 info->nr = skb->data[1] >> 5;
661 /* Check if this is a command or a response frame */
663 irlap_do_event(self, RECV_REJ_CMD, skb, info);
665 irlap_do_event(self, RECV_REJ_RSP, skb, info);
668 static void irlap_recv_srej_frame(struct irlap_cb *self, struct sk_buff *skb,
669 struct irlap_info *info, int command)
671 IRDA_DEBUG(0, "%s()\n", __FUNCTION__);
673 info->nr = skb->data[1] >> 5;
675 /* Check if this is a command or a response frame */
677 irlap_do_event(self, RECV_SREJ_CMD, skb, info);
679 irlap_do_event(self, RECV_SREJ_RSP, skb, info);
682 static void irlap_recv_disc_frame(struct irlap_cb *self, struct sk_buff *skb,
683 struct irlap_info *info, int command)
685 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
687 /* Check if this is a command or a response frame */
689 irlap_do_event(self, RECV_DISC_CMD, skb, info);
691 irlap_do_event(self, RECV_RD_RSP, skb, info);
695 * Function irlap_recv_ua_frame (skb, frame)
697 * Received UA (Unnumbered Acknowledgement) frame
700 static inline void irlap_recv_ua_frame(struct irlap_cb *self,
702 struct irlap_info *info)
704 irlap_do_event(self, RECV_UA_RSP, skb, info);
708 * Function irlap_send_data_primary(self, skb)
710 * Send I-frames as the primary station but without the poll bit set
713 void irlap_send_data_primary(struct irlap_cb *self, struct sk_buff *skb)
715 struct sk_buff *tx_skb;
717 if (skb->data[1] == I_FRAME) {
720 * Insert frame sequence number (Vs) in control field before
721 * inserting into transmit window queue.
723 skb->data[1] = I_FRAME | (self->vs << 1);
726 * Insert frame in store, in case of retransmissions
727 * Increase skb reference count, see irlap_do_event()
730 skb_queue_tail(&self->wx_list, skb);
733 tx_skb = skb_clone(skb, GFP_ATOMIC);
734 if (tx_skb == NULL) {
738 self->vs = (self->vs + 1) % 8;
739 self->ack_required = FALSE;
742 irlap_send_i_frame( self, tx_skb, CMD_FRAME);
744 IRDA_DEBUG(4, "%s(), sending unreliable frame\n", __FUNCTION__);
745 irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
750 * Function irlap_send_data_primary_poll (self, skb)
752 * Send I(nformation) frame as primary with poll bit set
754 void irlap_send_data_primary_poll(struct irlap_cb *self, struct sk_buff *skb)
756 struct sk_buff *tx_skb;
757 int transmission_time;
760 del_timer(&self->poll_timer);
762 /* Is this reliable or unreliable data? */
763 if (skb->data[1] == I_FRAME) {
766 * Insert frame sequence number (Vs) in control field before
767 * inserting into transmit window queue.
769 skb->data[1] = I_FRAME | (self->vs << 1);
772 * Insert frame in store, in case of retransmissions
773 * Increase skb reference count, see irlap_do_event()
776 skb_queue_tail(&self->wx_list, skb);
779 tx_skb = skb_clone(skb, GFP_ATOMIC);
780 if (tx_skb == NULL) {
785 * Set poll bit if necessary. We do this to the copied
786 * skb, since retransmitted need to set or clear the poll
787 * bit depending on when they are sent.
789 tx_skb->data[1] |= PF_BIT;
791 self->vs = (self->vs + 1) % 8;
792 self->ack_required = FALSE;
794 irlap_send_i_frame(self, tx_skb, CMD_FRAME);
796 IRDA_DEBUG(4, "%s(), sending unreliable frame\n", __FUNCTION__);
798 if (self->ack_required) {
799 irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
800 irlap_send_rr_frame(self, CMD_FRAME);
801 self->ack_required = FALSE;
803 skb->data[1] |= PF_BIT;
804 irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
808 /* How much time we took for transmission of all frames.
809 * We don't know, so let assume we used the full window. Jean II */
810 transmission_time = self->final_timeout;
812 /* Reset parameter so that we can fill next window */
813 self->window = self->window_size;
815 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
816 /* Remove what we have not used. Just do a prorata of the
817 * bytes left in window to window capacity.
818 * See max_line_capacities[][] in qos.c for details. Jean II */
819 transmission_time -= (self->final_timeout * self->bytes_left
820 / self->line_capacity);
821 IRDA_DEBUG(4, "%s() adjusting transmission_time : ft=%d, bl=%d, lc=%d -> tt=%d\n", __FUNCTION__, self->final_timeout, self->bytes_left, self->line_capacity, transmission_time);
823 /* We are allowed to transmit a maximum number of bytes again. */
824 self->bytes_left = self->line_capacity;
825 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
828 * The network layer has a intermediate buffer between IrLAP
829 * and the IrDA driver which can contain 8 frames. So, even
830 * though IrLAP is currently sending the *last* frame of the
831 * tx-window, the driver most likely has only just started
832 * sending the *first* frame of the same tx-window.
833 * I.e. we are always at the very begining of or Tx window.
834 * Now, we are supposed to set the final timer from the end
835 * of our tx-window to let the other peer reply. So, we need
836 * to add extra time to compensate for the fact that we
837 * are really at the start of tx-window, otherwise the final timer
838 * might expire before he can answer...
841 irlap_start_final_timer(self, self->final_timeout + transmission_time);
844 * The clever amongst you might ask why we do this adjustement
845 * only here, and not in all the other cases in irlap_event.c.
846 * In all those other case, we only send a very short management
847 * frame (few bytes), so the adjustement would be lost in the
849 * The exception of course is irlap_resend_rejected_frame().
854 * Function irlap_send_data_secondary_final (self, skb)
856 * Send I(nformation) frame as secondary with final bit set
859 void irlap_send_data_secondary_final(struct irlap_cb *self,
862 struct sk_buff *tx_skb = NULL;
864 ASSERT(self != NULL, return;);
865 ASSERT(self->magic == LAP_MAGIC, return;);
866 ASSERT(skb != NULL, return;);
868 /* Is this reliable or unreliable data? */
869 if (skb->data[1] == I_FRAME) {
872 * Insert frame sequence number (Vs) in control field before
873 * inserting into transmit window queue.
875 skb->data[1] = I_FRAME | (self->vs << 1);
878 * Insert frame in store, in case of retransmissions
879 * Increase skb reference count, see irlap_do_event()
882 skb_queue_tail(&self->wx_list, skb);
884 tx_skb = skb_clone(skb, GFP_ATOMIC);
885 if (tx_skb == NULL) {
889 tx_skb->data[1] |= PF_BIT;
891 self->vs = (self->vs + 1) % 8;
892 self->ack_required = FALSE;
894 irlap_send_i_frame(self, tx_skb, RSP_FRAME);
896 if (self->ack_required) {
897 irlap_send_ui_frame(self, skb_get(skb), self->caddr, RSP_FRAME);
898 irlap_send_rr_frame(self, RSP_FRAME);
899 self->ack_required = FALSE;
901 skb->data[1] |= PF_BIT;
902 irlap_send_ui_frame(self, skb_get(skb), self->caddr, RSP_FRAME);
906 self->window = self->window_size;
907 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
908 /* We are allowed to transmit a maximum number of bytes again. */
909 self->bytes_left = self->line_capacity;
910 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
912 irlap_start_wd_timer(self, self->wd_timeout);
916 * Function irlap_send_data_secondary (self, skb)
918 * Send I(nformation) frame as secondary without final bit set
921 void irlap_send_data_secondary(struct irlap_cb *self, struct sk_buff *skb)
923 struct sk_buff *tx_skb = NULL;
925 /* Is this reliable or unreliable data? */
926 if (skb->data[1] == I_FRAME) {
929 * Insert frame sequence number (Vs) in control field before
930 * inserting into transmit window queue.
932 skb->data[1] = I_FRAME | (self->vs << 1);
935 * Insert frame in store, in case of retransmissions
936 * Increase skb reference count, see irlap_do_event()
939 skb_queue_tail(&self->wx_list, skb);
941 tx_skb = skb_clone(skb, GFP_ATOMIC);
942 if (tx_skb == NULL) {
946 self->vs = (self->vs + 1) % 8;
947 self->ack_required = FALSE;
950 irlap_send_i_frame(self, tx_skb, RSP_FRAME);
952 irlap_send_ui_frame(self, skb_get(skb), self->caddr, RSP_FRAME);
958 * Function irlap_resend_rejected_frames (nr)
960 * Resend frames which has not been acknowledged. Should be safe to
961 * traverse the list without locking it since this function will only be
962 * called from interrupt context (BH)
964 void irlap_resend_rejected_frames(struct irlap_cb *self, int command)
966 struct sk_buff *tx_skb;
970 ASSERT(self != NULL, return;);
971 ASSERT(self->magic == LAP_MAGIC, return;);
973 /* Initialize variables */
974 count = skb_queue_len(&self->wx_list);
976 /* Resend unacknowledged frame(s) */
977 skb = skb_peek(&self->wx_list);
978 while (skb != NULL) {
979 irlap_wait_min_turn_around(self, &self->qos_tx);
981 /* We copy the skb to be retransmitted since we will have to
982 * modify it. Cloning will confuse packet sniffers
984 /* tx_skb = skb_clone( skb, GFP_ATOMIC); */
985 tx_skb = skb_copy(skb, GFP_ATOMIC);
987 IRDA_DEBUG(0, "%s(), unable to copy\n", __FUNCTION__);
990 /* Unlink tx_skb from list */
991 tx_skb->next = tx_skb->prev = NULL;
994 /* Clear old Nr field + poll bit */
995 tx_skb->data[1] &= 0x0f;
998 * Set poll bit on the last frame retransmitted
1001 tx_skb->data[1] |= PF_BIT; /* Set p/f bit */
1003 tx_skb->data[1] &= ~PF_BIT; /* Clear p/f bit */
1005 irlap_send_i_frame(self, tx_skb, command);
1008 * If our skb is the last buffer in the list, then
1009 * we are finished, if not, move to the next sk-buffer
1011 if (skb == skb_peek_tail(&self->wx_list))
1018 * We can now fill the window with additional data frames
1020 while (skb_queue_len( &self->txq) > 0) {
1022 IRDA_DEBUG(0, "%s(), sending additional frames!\n", __FUNCTION__);
1023 if ((skb_queue_len( &self->txq) > 0) &&
1024 (self->window > 0)) {
1025 skb = skb_dequeue( &self->txq);
1026 ASSERT(skb != NULL, return;);
1029 * If send window > 1 then send frame with pf
1032 if ((self->window > 1) &&
1033 skb_queue_len(&self->txq) > 0)
1035 irlap_send_data_primary(self, skb);
1037 irlap_send_data_primary_poll(self, skb);
1045 void irlap_resend_rejected_frame(struct irlap_cb *self, int command)
1047 struct sk_buff *tx_skb;
1048 struct sk_buff *skb;
1050 ASSERT(self != NULL, return;);
1051 ASSERT(self->magic == LAP_MAGIC, return;);
1053 /* Resend unacknowledged frame(s) */
1054 skb = skb_peek(&self->wx_list);
1056 irlap_wait_min_turn_around(self, &self->qos_tx);
1058 /* We copy the skb to be retransmitted since we will have to
1059 * modify it. Cloning will confuse packet sniffers
1061 /* tx_skb = skb_clone( skb, GFP_ATOMIC); */
1062 tx_skb = skb_copy(skb, GFP_ATOMIC);
1064 IRDA_DEBUG(0, "%s(), unable to copy\n", __FUNCTION__);
1067 /* Unlink tx_skb from list */
1068 tx_skb->next = tx_skb->prev = NULL;
1069 tx_skb->list = NULL;
1071 /* Clear old Nr field + poll bit */
1072 tx_skb->data[1] &= 0x0f;
1074 /* Set poll/final bit */
1075 tx_skb->data[1] |= PF_BIT; /* Set p/f bit */
1077 irlap_send_i_frame(self, tx_skb, command);
1082 * Function irlap_send_ui_frame (self, skb, command)
1084 * Contruct and transmit an Unnumbered Information (UI) frame
1087 void irlap_send_ui_frame(struct irlap_cb *self, struct sk_buff *skb,
1088 __u8 caddr, int command)
1090 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1092 ASSERT(self != NULL, return;);
1093 ASSERT(self->magic == LAP_MAGIC, return;);
1094 ASSERT(skb != NULL, return;);
1096 /* Insert connection address */
1097 skb->data[0] = caddr | ((command) ? CMD_FRAME : 0);
1099 irlap_queue_xmit(self, skb);
1103 * Function irlap_send_i_frame (skb)
1105 * Contruct and transmit Information (I) frame
1107 static void irlap_send_i_frame(struct irlap_cb *self, struct sk_buff *skb,
1110 /* Insert connection address */
1111 skb->data[0] = self->caddr;
1112 skb->data[0] |= (command) ? CMD_FRAME : 0;
1114 /* Insert next to receive (Vr) */
1115 skb->data[1] |= (self->vr << 5); /* insert nr */
1117 irlap_queue_xmit(self, skb);
1121 * Function irlap_recv_i_frame (skb, frame)
1123 * Receive and parse an I (Information) frame, no harm in making it inline
1124 * since it's called only from one single place (irlap_driver_rcv).
1126 static inline void irlap_recv_i_frame(struct irlap_cb *self,
1127 struct sk_buff *skb,
1128 struct irlap_info *info, int command)
1130 info->nr = skb->data[1] >> 5; /* Next to receive */
1131 info->pf = skb->data[1] & PF_BIT; /* Final bit */
1132 info->ns = (skb->data[1] >> 1) & 0x07; /* Next to send */
1134 /* Check if this is a command or a response frame */
1136 irlap_do_event(self, RECV_I_CMD, skb, info);
1138 irlap_do_event(self, RECV_I_RSP, skb, info);
1142 * Function irlap_recv_ui_frame (self, skb, info)
1144 * Receive and parse an Unnumbered Information (UI) frame
1147 static void irlap_recv_ui_frame(struct irlap_cb *self, struct sk_buff *skb,
1148 struct irlap_info *info)
1150 IRDA_DEBUG( 4, "%s()\n", __FUNCTION__);
1152 info->pf = skb->data[1] & PF_BIT; /* Final bit */
1154 irlap_do_event(self, RECV_UI_FRAME, skb, info);
1158 * Function irlap_recv_frmr_frame (skb, frame)
1160 * Received Frame Reject response.
1163 static void irlap_recv_frmr_frame(struct irlap_cb *self, struct sk_buff *skb,
1164 struct irlap_info *info)
1169 IRDA_DEBUG(0, "%s()\n", __FUNCTION__);
1171 ASSERT(self != NULL, return;);
1172 ASSERT(self->magic == LAP_MAGIC, return;);
1173 ASSERT(skb != NULL, return;);
1174 ASSERT(info != NULL, return;);
1176 if (!pskb_may_pull(skb, 4)) {
1177 ERROR("%s: frame to short!\n", __FUNCTION__);
1183 info->nr = frame[2] >> 5; /* Next to receive */
1184 info->pf = frame[2] & PF_BIT; /* Final bit */
1185 info->ns = (frame[2] >> 1) & 0x07; /* Next to send */
1187 w = frame[3] & 0x01;
1188 x = frame[3] & 0x02;
1189 y = frame[3] & 0x04;
1190 z = frame[3] & 0x08;
1193 IRDA_DEBUG(0, "Rejected control field is undefined or not "
1197 IRDA_DEBUG(0, "Rejected control field was invalid because it "
1198 "contained a non permitted I field.\n");
1201 IRDA_DEBUG(0, "Received I field exceeded the maximum negotiated "
1202 "for the existing connection or exceeded the maximum "
1203 "this station supports if no connection exists.\n");
1206 IRDA_DEBUG(0, "Rejected control field control field contained an "
1207 "invalid Nr count.\n");
1209 irlap_do_event(self, RECV_FRMR_RSP, skb, info);
1213 * Function irlap_send_test_frame (self, daddr)
1215 * Send a test frame response
1218 void irlap_send_test_frame(struct irlap_cb *self, __u8 caddr, __u32 daddr,
1219 struct sk_buff *cmd)
1221 struct sk_buff *tx_skb;
1222 struct test_frame *frame;
1225 tx_skb = dev_alloc_skb(cmd->len+sizeof(struct test_frame));
1229 /* Broadcast frames must include saddr and daddr fields */
1230 if (caddr == CBROADCAST) {
1231 frame = (struct test_frame *)
1232 skb_put(tx_skb, sizeof(struct test_frame));
1234 /* Insert the swapped addresses */
1235 frame->saddr = cpu_to_le32(self->saddr);
1236 frame->daddr = cpu_to_le32(daddr);
1238 frame = (struct test_frame *) skb_put(tx_skb, LAP_ADDR_HEADER + LAP_CTRL_HEADER);
1240 frame->caddr = caddr;
1241 frame->control = TEST_RSP | PF_BIT;
1244 info = skb_put(tx_skb, cmd->len);
1245 memcpy(info, cmd->data, cmd->len);
1247 /* Return to sender */
1248 irlap_wait_min_turn_around(self, &self->qos_tx);
1249 irlap_queue_xmit(self, tx_skb);
1253 * Function irlap_recv_test_frame (self, skb)
1255 * Receive a test frame
1258 static void irlap_recv_test_frame(struct irlap_cb *self, struct sk_buff *skb,
1259 struct irlap_info *info, int command)
1261 struct test_frame *frame;
1263 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
1265 if (!pskb_may_pull(skb, sizeof(*frame))) {
1266 ERROR("%s: frame to short!\n", __FUNCTION__);
1269 frame = (struct test_frame *) skb->data;
1271 /* Broadcast frames must carry saddr and daddr fields */
1272 if (info->caddr == CBROADCAST) {
1273 if (skb->len < sizeof(struct test_frame)) {
1274 IRDA_DEBUG(0, "%s() test frame to short!\n",
1279 /* Read and swap addresses */
1280 info->daddr = le32_to_cpu(frame->saddr);
1281 info->saddr = le32_to_cpu(frame->daddr);
1283 /* Make sure frame is addressed to us */
1284 if ((info->saddr != self->saddr) &&
1285 (info->saddr != BROADCAST)) {
1291 irlap_do_event(self, RECV_TEST_CMD, skb, info);
1293 irlap_do_event(self, RECV_TEST_RSP, skb, info);
1297 * Function irlap_driver_rcv (skb, netdev, ptype)
1299 * Called when a frame is received. Dispatches the right receive function
1300 * for processing of the frame.
1302 * Note on skb management :
1303 * After calling the higher layers of the IrDA stack, we always
1304 * kfree() the skb, which drop the reference count (and potentially
1306 * If a higher layer of the stack want to keep the skb around (to put
1307 * in a queue or pass it to the higher layer), it will need to use
1308 * skb_get() to keep a reference on it. This is usually done at the
1309 * LMP level in irlmp.c.
1312 int irlap_driver_rcv(struct sk_buff *skb, struct net_device *dev,
1313 struct packet_type *ptype)
1315 struct irlap_info info;
1316 struct irlap_cb *self;
1320 /* FIXME: should we get our own field? */
1321 self = (struct irlap_cb *) dev->atalk_ptr;
1323 /* If the net device is down, then IrLAP is gone! */
1324 if (!self || self->magic != LAP_MAGIC) {
1329 /* We are no longer an "old" protocol, so we need to handle
1330 * share and non linear skbs. This should never happen, so
1331 * we don't need to be clever about it. Jean II */
1332 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
1333 ERROR("%s: can't clone shared skb!\n", __FUNCTION__);
1338 /* Check if frame is large enough for parsing */
1339 if (!pskb_may_pull(skb, 2)) {
1340 ERROR("%s: frame to short!\n", __FUNCTION__);
1345 command = skb->data[0] & CMD_FRAME;
1346 info.caddr = skb->data[0] & CBROADCAST;
1348 info.pf = skb->data[1] & PF_BIT;
1349 info.control = skb->data[1] & ~PF_BIT; /* Mask away poll/final bit */
1351 control = info.control;
1353 /* First we check if this frame has a valid connection address */
1354 if ((info.caddr != self->caddr) && (info.caddr != CBROADCAST)) {
1355 IRDA_DEBUG(0, "%s(), wrong connection address!\n",
1360 * Optimize for the common case and check if the frame is an
1361 * I(nformation) frame. Only I-frames have bit 0 set to 0
1363 if (~control & 0x01) {
1364 irlap_recv_i_frame(self, skb, &info, command);
1368 * We now check is the frame is an S(upervisory) frame. Only
1369 * S-frames have bit 0 set to 1 and bit 1 set to 0
1371 if (~control & 0x02) {
1373 * Received S(upervisory) frame, check which frame type it is
1374 * only the first nibble is of interest
1376 switch (control & 0x0f) {
1378 irlap_recv_rr_frame(self, skb, &info, command);
1381 irlap_recv_rnr_frame(self, skb, &info, command);
1384 irlap_recv_rej_frame(self, skb, &info, command);
1387 irlap_recv_srej_frame(self, skb, &info, command);
1390 WARNING("%s: Unknown S-frame %02x received!\n",
1391 __FUNCTION__, info.control);
1397 * This must be a C(ontrol) frame
1401 irlap_recv_discovery_xid_rsp(self, skb, &info);
1404 irlap_recv_discovery_xid_cmd(self, skb, &info);
1407 irlap_recv_snrm_cmd(self, skb, &info);
1410 irlap_do_event(self, RECV_DM_RSP, skb, &info);
1412 case DISC_CMD: /* And RD_RSP since they have the same value */
1413 irlap_recv_disc_frame(self, skb, &info, command);
1416 irlap_recv_test_frame(self, skb, &info, command);
1419 irlap_recv_ua_frame(self, skb, &info);
1422 irlap_recv_frmr_frame(self, skb, &info);
1425 irlap_recv_ui_frame(self, skb, &info);
1428 WARNING("%s: Unknown frame %02x received!\n",
1429 __FUNCTION__, info.control);
1433 /* Always drop our reference on the skb */