Linux 2.6.19-rc6
[cris-mirror.git] / net / irda / irlap_frame.c
blobdba349c832d02454b8451bbd02aa1935e23d9e15
1 /*********************************************************************
3 * Filename: irlap_frame.c
4 * Version: 1.0
5 * Description: Build and transmit IrLAP frames
6 * Status: Stable
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>
28 #include <linux/if.h>
29 #include <linux/if_ether.h>
30 #include <linux/netdevice.h>
31 #include <linux/irda.h>
33 #include <net/pkt_sched.h>
34 #include <net/sock.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,
47 int command);
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,
57 struct sk_buff *skb)
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;
69 /* Reset */
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
90 * code into it.
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);
102 dev_queue_xmit(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;
114 int ret;
116 IRDA_ASSERT(self != NULL, return;);
117 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
119 /* Allocate frame */
120 tx_skb = alloc_skb(sizeof(struct snrm_frame) +
121 IRLAP_NEGOCIATION_PARAMS_LEN,
122 GFP_ATOMIC);
123 if (!tx_skb)
124 return;
126 frame = (struct snrm_frame *) skb_put(tx_skb, 2);
128 /* Insert connection address field */
129 if (qos)
130 frame->caddr = CMD_FRAME | CBROADCAST;
131 else
132 frame->caddr = CMD_FRAME | self->caddr;
134 /* Insert control field */
135 frame->control = SNRM_CMD | PF_BIT;
138 * If we are establishing a connection then insert QoS paramerters
140 if (qos) {
141 skb_put(tx_skb, 9); /* 25 left */
142 frame->saddr = cpu_to_le32(self->saddr);
143 frame->daddr = cpu_to_le32(self->daddr);
145 frame->ncaddr = self->caddr;
147 ret = irlap_insert_qos_negotiation_params(self, tx_skb);
148 if (ret < 0) {
149 dev_kfree_skb(tx_skb);
150 return;
153 irlap_queue_xmit(self, tx_skb);
157 * Function irlap_recv_snrm_cmd (skb, info)
159 * Received SNRM (Set Normal Response Mode) command frame
162 static void irlap_recv_snrm_cmd(struct irlap_cb *self, struct sk_buff *skb,
163 struct irlap_info *info)
165 struct snrm_frame *frame;
167 if (pskb_may_pull(skb,sizeof(struct snrm_frame))) {
168 frame = (struct snrm_frame *) skb->data;
170 /* Copy the new connection address ignoring the C/R bit */
171 info->caddr = frame->ncaddr & 0xFE;
173 /* Check if the new connection address is valid */
174 if ((info->caddr == 0x00) || (info->caddr == 0xfe)) {
175 IRDA_DEBUG(3, "%s(), invalid connection address!\n",
176 __FUNCTION__);
177 return;
180 /* Copy peer device address */
181 info->daddr = le32_to_cpu(frame->saddr);
182 info->saddr = le32_to_cpu(frame->daddr);
184 /* Only accept if addressed directly to us */
185 if (info->saddr != self->saddr) {
186 IRDA_DEBUG(2, "%s(), not addressed to us!\n",
187 __FUNCTION__);
188 return;
190 irlap_do_event(self, RECV_SNRM_CMD, skb, info);
191 } else {
192 /* Signal that this SNRM frame does not contain and I-field */
193 irlap_do_event(self, RECV_SNRM_CMD, skb, NULL);
198 * Function irlap_send_ua_response_frame (qos)
200 * Send UA (Unnumbered Acknowledgement) frame
203 void irlap_send_ua_response_frame(struct irlap_cb *self, struct qos_info *qos)
205 struct sk_buff *tx_skb;
206 struct ua_frame *frame;
207 int ret;
209 IRDA_DEBUG(2, "%s() <%ld>\n", __FUNCTION__, jiffies);
211 IRDA_ASSERT(self != NULL, return;);
212 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
214 /* Allocate frame */
215 tx_skb = alloc_skb(sizeof(struct ua_frame) +
216 IRLAP_NEGOCIATION_PARAMS_LEN,
217 GFP_ATOMIC);
218 if (!tx_skb)
219 return;
221 frame = (struct ua_frame *) skb_put(tx_skb, 10);
223 /* Build UA response */
224 frame->caddr = self->caddr;
225 frame->control = UA_RSP | PF_BIT;
227 frame->saddr = cpu_to_le32(self->saddr);
228 frame->daddr = cpu_to_le32(self->daddr);
230 /* Should we send QoS negotiation parameters? */
231 if (qos) {
232 ret = irlap_insert_qos_negotiation_params(self, tx_skb);
233 if (ret < 0) {
234 dev_kfree_skb(tx_skb);
235 return;
239 irlap_queue_xmit(self, tx_skb);
244 * Function irlap_send_dm_frame (void)
246 * Send disconnected mode (DM) frame
249 void irlap_send_dm_frame( struct irlap_cb *self)
251 struct sk_buff *tx_skb = NULL;
252 struct dm_frame *frame;
254 IRDA_ASSERT(self != NULL, return;);
255 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
257 tx_skb = alloc_skb(sizeof(struct dm_frame), GFP_ATOMIC);
258 if (!tx_skb)
259 return;
261 frame = (struct dm_frame *)skb_put(tx_skb, 2);
263 if (self->state == LAP_NDM)
264 frame->caddr = CBROADCAST;
265 else
266 frame->caddr = self->caddr;
268 frame->control = DM_RSP | PF_BIT;
270 irlap_queue_xmit(self, tx_skb);
274 * Function irlap_send_disc_frame (void)
276 * Send disconnect (DISC) frame
279 void irlap_send_disc_frame(struct irlap_cb *self)
281 struct sk_buff *tx_skb = NULL;
282 struct disc_frame *frame;
284 IRDA_DEBUG(3, "%s()\n", __FUNCTION__);
286 IRDA_ASSERT(self != NULL, return;);
287 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
289 tx_skb = alloc_skb(sizeof(struct disc_frame), GFP_ATOMIC);
290 if (!tx_skb)
291 return;
293 frame = (struct disc_frame *)skb_put(tx_skb, 2);
295 frame->caddr = self->caddr | CMD_FRAME;
296 frame->control = DISC_CMD | PF_BIT;
298 irlap_queue_xmit(self, tx_skb);
302 * Function irlap_send_discovery_xid_frame (S, s, command)
304 * Build and transmit a XID (eXchange station IDentifier) discovery
305 * frame.
307 void irlap_send_discovery_xid_frame(struct irlap_cb *self, int S, __u8 s,
308 __u8 command, discovery_t *discovery)
310 struct sk_buff *tx_skb = NULL;
311 struct xid_frame *frame;
312 __u32 bcast = BROADCAST;
313 __u8 *info;
315 IRDA_DEBUG(4, "%s(), s=%d, S=%d, command=%d\n", __FUNCTION__,
316 s, S, command);
318 IRDA_ASSERT(self != NULL, return;);
319 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
320 IRDA_ASSERT(discovery != NULL, return;);
322 tx_skb = alloc_skb(sizeof(struct xid_frame) + IRLAP_DISCOVERY_INFO_LEN,
323 GFP_ATOMIC);
324 if (!tx_skb)
325 return;
327 skb_put(tx_skb, 14);
328 frame = (struct xid_frame *) tx_skb->data;
330 if (command) {
331 frame->caddr = CBROADCAST | CMD_FRAME;
332 frame->control = XID_CMD | PF_BIT;
333 } else {
334 frame->caddr = CBROADCAST;
335 frame->control = XID_RSP | PF_BIT;
337 frame->ident = XID_FORMAT;
339 frame->saddr = cpu_to_le32(self->saddr);
341 if (command)
342 frame->daddr = cpu_to_le32(bcast);
343 else
344 frame->daddr = cpu_to_le32(discovery->data.daddr);
346 switch (S) {
347 case 1:
348 frame->flags = 0x00;
349 break;
350 case 6:
351 frame->flags = 0x01;
352 break;
353 case 8:
354 frame->flags = 0x02;
355 break;
356 case 16:
357 frame->flags = 0x03;
358 break;
359 default:
360 frame->flags = 0x02;
361 break;
364 frame->slotnr = s;
365 frame->version = 0x00;
368 * Provide info for final slot only in commands, and for all
369 * responses. Send the second byte of the hint only if the
370 * EXTENSION bit is set in the first byte.
372 if (!command || (frame->slotnr == 0xff)) {
373 int len;
375 if (discovery->data.hints[0] & HINT_EXTENSION) {
376 info = skb_put(tx_skb, 2);
377 info[0] = discovery->data.hints[0];
378 info[1] = discovery->data.hints[1];
379 } else {
380 info = skb_put(tx_skb, 1);
381 info[0] = discovery->data.hints[0];
383 info = skb_put(tx_skb, 1);
384 info[0] = discovery->data.charset;
386 len = IRDA_MIN(discovery->name_len, skb_tailroom(tx_skb));
387 info = skb_put(tx_skb, len);
388 memcpy(info, discovery->data.info, len);
390 irlap_queue_xmit(self, tx_skb);
394 * Function irlap_recv_discovery_xid_rsp (skb, info)
396 * Received a XID discovery response
399 static void irlap_recv_discovery_xid_rsp(struct irlap_cb *self,
400 struct sk_buff *skb,
401 struct irlap_info *info)
403 struct xid_frame *xid;
404 discovery_t *discovery = NULL;
405 __u8 *discovery_info;
406 char *text;
408 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
410 IRDA_ASSERT(self != NULL, return;);
411 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
413 if (!pskb_may_pull(skb, sizeof(struct xid_frame))) {
414 IRDA_ERROR("%s: frame to short!\n", __FUNCTION__);
415 return;
418 xid = (struct xid_frame *) skb->data;
420 info->daddr = le32_to_cpu(xid->saddr);
421 info->saddr = le32_to_cpu(xid->daddr);
423 /* Make sure frame is addressed to us */
424 if ((info->saddr != self->saddr) && (info->saddr != BROADCAST)) {
425 IRDA_DEBUG(0, "%s(), frame is not addressed to us!\n",
426 __FUNCTION__);
427 return;
430 if ((discovery = kzalloc(sizeof(discovery_t), GFP_ATOMIC)) == NULL) {
431 IRDA_WARNING("%s: kmalloc failed!\n", __FUNCTION__);
432 return;
435 discovery->data.daddr = info->daddr;
436 discovery->data.saddr = self->saddr;
437 discovery->timestamp = jiffies;
439 IRDA_DEBUG(4, "%s(), daddr=%08x\n", __FUNCTION__,
440 discovery->data.daddr);
442 discovery_info = skb_pull(skb, sizeof(struct xid_frame));
444 /* Get info returned from peer */
445 discovery->data.hints[0] = discovery_info[0];
446 if (discovery_info[0] & HINT_EXTENSION) {
447 IRDA_DEBUG(4, "EXTENSION\n");
448 discovery->data.hints[1] = discovery_info[1];
449 discovery->data.charset = discovery_info[2];
450 text = (char *) &discovery_info[3];
451 } else {
452 discovery->data.hints[1] = 0;
453 discovery->data.charset = discovery_info[1];
454 text = (char *) &discovery_info[2];
457 * Terminate info string, should be safe since this is where the
458 * FCS bytes resides.
460 skb->data[skb->len] = '\0';
461 strncpy(discovery->data.info, text, NICKNAME_MAX_LEN);
462 discovery->name_len = strlen(discovery->data.info);
464 info->discovery = discovery;
466 irlap_do_event(self, RECV_DISCOVERY_XID_RSP, skb, info);
470 * Function irlap_recv_discovery_xid_cmd (skb, info)
472 * Received a XID discovery command
475 static void irlap_recv_discovery_xid_cmd(struct irlap_cb *self,
476 struct sk_buff *skb,
477 struct irlap_info *info)
479 struct xid_frame *xid;
480 discovery_t *discovery = NULL;
481 __u8 *discovery_info;
482 char *text;
484 if (!pskb_may_pull(skb, sizeof(struct xid_frame))) {
485 IRDA_ERROR("%s: frame to short!\n", __FUNCTION__);
486 return;
489 xid = (struct xid_frame *) skb->data;
491 info->daddr = le32_to_cpu(xid->saddr);
492 info->saddr = le32_to_cpu(xid->daddr);
494 /* Make sure frame is addressed to us */
495 if ((info->saddr != self->saddr) && (info->saddr != BROADCAST)) {
496 IRDA_DEBUG(0, "%s(), frame is not addressed to us!\n",
497 __FUNCTION__);
498 return;
501 switch (xid->flags & 0x03) {
502 case 0x00:
503 info->S = 1;
504 break;
505 case 0x01:
506 info->S = 6;
507 break;
508 case 0x02:
509 info->S = 8;
510 break;
511 case 0x03:
512 info->S = 16;
513 break;
514 default:
515 /* Error!! */
516 return;
518 info->s = xid->slotnr;
520 discovery_info = skb_pull(skb, sizeof(struct xid_frame));
523 * Check if last frame
525 if (info->s == 0xff) {
526 /* Check if things are sane at this point... */
527 if((discovery_info == NULL) ||
528 !pskb_may_pull(skb, 3)) {
529 IRDA_ERROR("%s: discovery frame to short!\n",
530 __FUNCTION__);
531 return;
535 * We now have some discovery info to deliver!
537 discovery = kmalloc(sizeof(discovery_t), GFP_ATOMIC);
538 if (!discovery) {
539 IRDA_WARNING("%s: unable to malloc!\n", __FUNCTION__);
540 return;
543 discovery->data.daddr = info->daddr;
544 discovery->data.saddr = self->saddr;
545 discovery->timestamp = jiffies;
547 discovery->data.hints[0] = discovery_info[0];
548 if (discovery_info[0] & HINT_EXTENSION) {
549 discovery->data.hints[1] = discovery_info[1];
550 discovery->data.charset = discovery_info[2];
551 text = (char *) &discovery_info[3];
552 } else {
553 discovery->data.hints[1] = 0;
554 discovery->data.charset = discovery_info[1];
555 text = (char *) &discovery_info[2];
558 * Terminate string, should be safe since this is where the
559 * FCS bytes resides.
561 skb->data[skb->len] = '\0';
562 strncpy(discovery->data.info, text, NICKNAME_MAX_LEN);
563 discovery->name_len = strlen(discovery->data.info);
565 info->discovery = discovery;
566 } else
567 info->discovery = NULL;
569 irlap_do_event(self, RECV_DISCOVERY_XID_CMD, skb, info);
573 * Function irlap_send_rr_frame (self, command)
575 * Build and transmit RR (Receive Ready) frame. Notice that it is currently
576 * only possible to send RR frames with the poll bit set.
578 void irlap_send_rr_frame(struct irlap_cb *self, int command)
580 struct sk_buff *tx_skb;
581 struct rr_frame *frame;
583 tx_skb = alloc_skb(sizeof(struct rr_frame), GFP_ATOMIC);
584 if (!tx_skb)
585 return;
587 frame = (struct rr_frame *)skb_put(tx_skb, 2);
589 frame->caddr = self->caddr;
590 frame->caddr |= (command) ? CMD_FRAME : 0;
592 frame->control = RR | PF_BIT | (self->vr << 5);
594 irlap_queue_xmit(self, tx_skb);
598 * Function irlap_send_rd_frame (self)
600 * Request disconnect. Used by a secondary station to request the
601 * disconnection of the link.
603 void irlap_send_rd_frame(struct irlap_cb *self)
605 struct sk_buff *tx_skb;
606 struct rd_frame *frame;
608 tx_skb = alloc_skb(sizeof(struct rd_frame), GFP_ATOMIC);
609 if (!tx_skb)
610 return;
612 frame = (struct rd_frame *)skb_put(tx_skb, 2);
614 frame->caddr = self->caddr;
615 frame->caddr = RD_RSP | PF_BIT;
617 irlap_queue_xmit(self, tx_skb);
621 * Function irlap_recv_rr_frame (skb, info)
623 * Received RR (Receive Ready) frame from peer station, no harm in
624 * making it inline since its called only from one single place
625 * (irlap_driver_rcv).
627 static inline void irlap_recv_rr_frame(struct irlap_cb *self,
628 struct sk_buff *skb,
629 struct irlap_info *info, int command)
631 info->nr = skb->data[1] >> 5;
633 /* Check if this is a command or a response frame */
634 if (command)
635 irlap_do_event(self, RECV_RR_CMD, skb, info);
636 else
637 irlap_do_event(self, RECV_RR_RSP, skb, info);
641 * Function irlap_recv_rnr_frame (self, skb, info)
643 * Received RNR (Receive Not Ready) frame from peer station
646 static void irlap_recv_rnr_frame(struct irlap_cb *self, struct sk_buff *skb,
647 struct irlap_info *info, int command)
649 info->nr = skb->data[1] >> 5;
651 IRDA_DEBUG(4, "%s(), nr=%d, %ld\n", __FUNCTION__, info->nr, jiffies);
653 if (command)
654 irlap_do_event(self, RECV_RNR_CMD, skb, info);
655 else
656 irlap_do_event(self, RECV_RNR_RSP, skb, info);
659 static void irlap_recv_rej_frame(struct irlap_cb *self, struct sk_buff *skb,
660 struct irlap_info *info, int command)
662 IRDA_DEBUG(0, "%s()\n", __FUNCTION__);
664 info->nr = skb->data[1] >> 5;
666 /* Check if this is a command or a response frame */
667 if (command)
668 irlap_do_event(self, RECV_REJ_CMD, skb, info);
669 else
670 irlap_do_event(self, RECV_REJ_RSP, skb, info);
673 static void irlap_recv_srej_frame(struct irlap_cb *self, struct sk_buff *skb,
674 struct irlap_info *info, int command)
676 IRDA_DEBUG(0, "%s()\n", __FUNCTION__);
678 info->nr = skb->data[1] >> 5;
680 /* Check if this is a command or a response frame */
681 if (command)
682 irlap_do_event(self, RECV_SREJ_CMD, skb, info);
683 else
684 irlap_do_event(self, RECV_SREJ_RSP, skb, info);
687 static void irlap_recv_disc_frame(struct irlap_cb *self, struct sk_buff *skb,
688 struct irlap_info *info, int command)
690 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
692 /* Check if this is a command or a response frame */
693 if (command)
694 irlap_do_event(self, RECV_DISC_CMD, skb, info);
695 else
696 irlap_do_event(self, RECV_RD_RSP, skb, info);
700 * Function irlap_recv_ua_frame (skb, frame)
702 * Received UA (Unnumbered Acknowledgement) frame
705 static inline void irlap_recv_ua_frame(struct irlap_cb *self,
706 struct sk_buff *skb,
707 struct irlap_info *info)
709 irlap_do_event(self, RECV_UA_RSP, skb, info);
713 * Function irlap_send_data_primary(self, skb)
715 * Send I-frames as the primary station but without the poll bit set
718 void irlap_send_data_primary(struct irlap_cb *self, struct sk_buff *skb)
720 struct sk_buff *tx_skb;
722 if (skb->data[1] == I_FRAME) {
725 * Insert frame sequence number (Vs) in control field before
726 * inserting into transmit window queue.
728 skb->data[1] = I_FRAME | (self->vs << 1);
731 * Insert frame in store, in case of retransmissions
732 * Increase skb reference count, see irlap_do_event()
734 skb_get(skb);
735 skb_queue_tail(&self->wx_list, skb);
737 /* Copy buffer */
738 tx_skb = skb_clone(skb, GFP_ATOMIC);
739 if (tx_skb == NULL) {
740 return;
743 self->vs = (self->vs + 1) % 8;
744 self->ack_required = FALSE;
745 self->window -= 1;
747 irlap_send_i_frame( self, tx_skb, CMD_FRAME);
748 } else {
749 IRDA_DEBUG(4, "%s(), sending unreliable frame\n", __FUNCTION__);
750 irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
751 self->window -= 1;
755 * Function irlap_send_data_primary_poll (self, skb)
757 * Send I(nformation) frame as primary with poll bit set
759 void irlap_send_data_primary_poll(struct irlap_cb *self, struct sk_buff *skb)
761 struct sk_buff *tx_skb;
762 int transmission_time;
764 /* Stop P timer */
765 del_timer(&self->poll_timer);
767 /* Is this reliable or unreliable data? */
768 if (skb->data[1] == I_FRAME) {
771 * Insert frame sequence number (Vs) in control field before
772 * inserting into transmit window queue.
774 skb->data[1] = I_FRAME | (self->vs << 1);
777 * Insert frame in store, in case of retransmissions
778 * Increase skb reference count, see irlap_do_event()
780 skb_get(skb);
781 skb_queue_tail(&self->wx_list, skb);
783 /* Copy buffer */
784 tx_skb = skb_clone(skb, GFP_ATOMIC);
785 if (tx_skb == NULL) {
786 return;
790 * Set poll bit if necessary. We do this to the copied
791 * skb, since retransmitted need to set or clear the poll
792 * bit depending on when they are sent.
794 tx_skb->data[1] |= PF_BIT;
796 self->vs = (self->vs + 1) % 8;
797 self->ack_required = FALSE;
799 irlap_send_i_frame(self, tx_skb, CMD_FRAME);
800 } else {
801 IRDA_DEBUG(4, "%s(), sending unreliable frame\n", __FUNCTION__);
803 if (self->ack_required) {
804 irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
805 irlap_send_rr_frame(self, CMD_FRAME);
806 self->ack_required = FALSE;
807 } else {
808 skb->data[1] |= PF_BIT;
809 irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
813 /* How much time we took for transmission of all frames.
814 * We don't know, so let assume we used the full window. Jean II */
815 transmission_time = self->final_timeout;
817 /* Reset parameter so that we can fill next window */
818 self->window = self->window_size;
820 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
821 /* Remove what we have not used. Just do a prorata of the
822 * bytes left in window to window capacity.
823 * See max_line_capacities[][] in qos.c for details. Jean II */
824 transmission_time -= (self->final_timeout * self->bytes_left
825 / self->line_capacity);
826 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);
828 /* We are allowed to transmit a maximum number of bytes again. */
829 self->bytes_left = self->line_capacity;
830 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
833 * The network layer has a intermediate buffer between IrLAP
834 * and the IrDA driver which can contain 8 frames. So, even
835 * though IrLAP is currently sending the *last* frame of the
836 * tx-window, the driver most likely has only just started
837 * sending the *first* frame of the same tx-window.
838 * I.e. we are always at the very begining of or Tx window.
839 * Now, we are supposed to set the final timer from the end
840 * of our tx-window to let the other peer reply. So, we need
841 * to add extra time to compensate for the fact that we
842 * are really at the start of tx-window, otherwise the final timer
843 * might expire before he can answer...
844 * Jean II
846 irlap_start_final_timer(self, self->final_timeout + transmission_time);
849 * The clever amongst you might ask why we do this adjustement
850 * only here, and not in all the other cases in irlap_event.c.
851 * In all those other case, we only send a very short management
852 * frame (few bytes), so the adjustement would be lost in the
853 * noise...
854 * The exception of course is irlap_resend_rejected_frame().
855 * Jean II */
859 * Function irlap_send_data_secondary_final (self, skb)
861 * Send I(nformation) frame as secondary with final bit set
864 void irlap_send_data_secondary_final(struct irlap_cb *self,
865 struct sk_buff *skb)
867 struct sk_buff *tx_skb = NULL;
869 IRDA_ASSERT(self != NULL, return;);
870 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
871 IRDA_ASSERT(skb != NULL, return;);
873 /* Is this reliable or unreliable data? */
874 if (skb->data[1] == I_FRAME) {
877 * Insert frame sequence number (Vs) in control field before
878 * inserting into transmit window queue.
880 skb->data[1] = I_FRAME | (self->vs << 1);
883 * Insert frame in store, in case of retransmissions
884 * Increase skb reference count, see irlap_do_event()
886 skb_get(skb);
887 skb_queue_tail(&self->wx_list, skb);
889 tx_skb = skb_clone(skb, GFP_ATOMIC);
890 if (tx_skb == NULL) {
891 return;
894 tx_skb->data[1] |= PF_BIT;
896 self->vs = (self->vs + 1) % 8;
897 self->ack_required = FALSE;
899 irlap_send_i_frame(self, tx_skb, RSP_FRAME);
900 } else {
901 if (self->ack_required) {
902 irlap_send_ui_frame(self, skb_get(skb), self->caddr, RSP_FRAME);
903 irlap_send_rr_frame(self, RSP_FRAME);
904 self->ack_required = FALSE;
905 } else {
906 skb->data[1] |= PF_BIT;
907 irlap_send_ui_frame(self, skb_get(skb), self->caddr, RSP_FRAME);
911 self->window = self->window_size;
912 #ifdef CONFIG_IRDA_DYNAMIC_WINDOW
913 /* We are allowed to transmit a maximum number of bytes again. */
914 self->bytes_left = self->line_capacity;
915 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
917 irlap_start_wd_timer(self, self->wd_timeout);
921 * Function irlap_send_data_secondary (self, skb)
923 * Send I(nformation) frame as secondary without final bit set
926 void irlap_send_data_secondary(struct irlap_cb *self, struct sk_buff *skb)
928 struct sk_buff *tx_skb = NULL;
930 /* Is this reliable or unreliable data? */
931 if (skb->data[1] == I_FRAME) {
934 * Insert frame sequence number (Vs) in control field before
935 * inserting into transmit window queue.
937 skb->data[1] = I_FRAME | (self->vs << 1);
940 * Insert frame in store, in case of retransmissions
941 * Increase skb reference count, see irlap_do_event()
943 skb_get(skb);
944 skb_queue_tail(&self->wx_list, skb);
946 tx_skb = skb_clone(skb, GFP_ATOMIC);
947 if (tx_skb == NULL) {
948 return;
951 self->vs = (self->vs + 1) % 8;
952 self->ack_required = FALSE;
953 self->window -= 1;
955 irlap_send_i_frame(self, tx_skb, RSP_FRAME);
956 } else {
957 irlap_send_ui_frame(self, skb_get(skb), self->caddr, RSP_FRAME);
958 self->window -= 1;
963 * Function irlap_resend_rejected_frames (nr)
965 * Resend frames which has not been acknowledged. Should be safe to
966 * traverse the list without locking it since this function will only be
967 * called from interrupt context (BH)
969 void irlap_resend_rejected_frames(struct irlap_cb *self, int command)
971 struct sk_buff *tx_skb;
972 struct sk_buff *skb;
973 int count;
975 IRDA_ASSERT(self != NULL, return;);
976 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
978 /* Initialize variables */
979 count = skb_queue_len(&self->wx_list);
981 /* Resend unacknowledged frame(s) */
982 skb = skb_peek(&self->wx_list);
983 while (skb != NULL) {
984 irlap_wait_min_turn_around(self, &self->qos_tx);
986 /* We copy the skb to be retransmitted since we will have to
987 * modify it. Cloning will confuse packet sniffers
989 /* tx_skb = skb_clone( skb, GFP_ATOMIC); */
990 tx_skb = skb_copy(skb, GFP_ATOMIC);
991 if (!tx_skb) {
992 IRDA_DEBUG(0, "%s(), unable to copy\n", __FUNCTION__);
993 return;
996 /* Clear old Nr field + poll bit */
997 tx_skb->data[1] &= 0x0f;
1000 * Set poll bit on the last frame retransmitted
1002 if (count-- == 1)
1003 tx_skb->data[1] |= PF_BIT; /* Set p/f bit */
1004 else
1005 tx_skb->data[1] &= ~PF_BIT; /* Clear p/f bit */
1007 irlap_send_i_frame(self, tx_skb, command);
1010 * If our skb is the last buffer in the list, then
1011 * we are finished, if not, move to the next sk-buffer
1013 if (skb == skb_peek_tail(&self->wx_list))
1014 skb = NULL;
1015 else
1016 skb = skb->next;
1018 #if 0 /* Not yet */
1020 * We can now fill the window with additional data frames
1022 while (!skb_queue_empty(&self->txq)) {
1024 IRDA_DEBUG(0, "%s(), sending additional frames!\n", __FUNCTION__);
1025 if (self->window > 0) {
1026 skb = skb_dequeue( &self->txq);
1027 IRDA_ASSERT(skb != NULL, return;);
1030 * If send window > 1 then send frame with pf
1031 * bit cleared
1033 if ((self->window > 1) &&
1034 !skb_queue_empty(&self->txq)) {
1035 irlap_send_data_primary(self, skb);
1036 } else {
1037 irlap_send_data_primary_poll(self, skb);
1039 kfree_skb(skb);
1042 #endif
1045 void irlap_resend_rejected_frame(struct irlap_cb *self, int command)
1047 struct sk_buff *tx_skb;
1048 struct sk_buff *skb;
1050 IRDA_ASSERT(self != NULL, return;);
1051 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
1053 /* Resend unacknowledged frame(s) */
1054 skb = skb_peek(&self->wx_list);
1055 if (skb != NULL) {
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);
1063 if (!tx_skb) {
1064 IRDA_DEBUG(0, "%s(), unable to copy\n", __FUNCTION__);
1065 return;
1068 /* Clear old Nr field + poll bit */
1069 tx_skb->data[1] &= 0x0f;
1071 /* Set poll/final bit */
1072 tx_skb->data[1] |= PF_BIT; /* Set p/f bit */
1074 irlap_send_i_frame(self, tx_skb, command);
1079 * Function irlap_send_ui_frame (self, skb, command)
1081 * Contruct and transmit an Unnumbered Information (UI) frame
1084 void irlap_send_ui_frame(struct irlap_cb *self, struct sk_buff *skb,
1085 __u8 caddr, int command)
1087 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
1089 IRDA_ASSERT(self != NULL, return;);
1090 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
1091 IRDA_ASSERT(skb != NULL, return;);
1093 /* Insert connection address */
1094 skb->data[0] = caddr | ((command) ? CMD_FRAME : 0);
1096 irlap_queue_xmit(self, skb);
1100 * Function irlap_send_i_frame (skb)
1102 * Contruct and transmit Information (I) frame
1104 static void irlap_send_i_frame(struct irlap_cb *self, struct sk_buff *skb,
1105 int command)
1107 /* Insert connection address */
1108 skb->data[0] = self->caddr;
1109 skb->data[0] |= (command) ? CMD_FRAME : 0;
1111 /* Insert next to receive (Vr) */
1112 skb->data[1] |= (self->vr << 5); /* insert nr */
1114 irlap_queue_xmit(self, skb);
1118 * Function irlap_recv_i_frame (skb, frame)
1120 * Receive and parse an I (Information) frame, no harm in making it inline
1121 * since it's called only from one single place (irlap_driver_rcv).
1123 static inline void irlap_recv_i_frame(struct irlap_cb *self,
1124 struct sk_buff *skb,
1125 struct irlap_info *info, int command)
1127 info->nr = skb->data[1] >> 5; /* Next to receive */
1128 info->pf = skb->data[1] & PF_BIT; /* Final bit */
1129 info->ns = (skb->data[1] >> 1) & 0x07; /* Next to send */
1131 /* Check if this is a command or a response frame */
1132 if (command)
1133 irlap_do_event(self, RECV_I_CMD, skb, info);
1134 else
1135 irlap_do_event(self, RECV_I_RSP, skb, info);
1139 * Function irlap_recv_ui_frame (self, skb, info)
1141 * Receive and parse an Unnumbered Information (UI) frame
1144 static void irlap_recv_ui_frame(struct irlap_cb *self, struct sk_buff *skb,
1145 struct irlap_info *info)
1147 IRDA_DEBUG( 4, "%s()\n", __FUNCTION__);
1149 info->pf = skb->data[1] & PF_BIT; /* Final bit */
1151 irlap_do_event(self, RECV_UI_FRAME, skb, info);
1155 * Function irlap_recv_frmr_frame (skb, frame)
1157 * Received Frame Reject response.
1160 static void irlap_recv_frmr_frame(struct irlap_cb *self, struct sk_buff *skb,
1161 struct irlap_info *info)
1163 __u8 *frame;
1164 int w, x, y, z;
1166 IRDA_DEBUG(0, "%s()\n", __FUNCTION__);
1168 IRDA_ASSERT(self != NULL, return;);
1169 IRDA_ASSERT(self->magic == LAP_MAGIC, return;);
1170 IRDA_ASSERT(skb != NULL, return;);
1171 IRDA_ASSERT(info != NULL, return;);
1173 if (!pskb_may_pull(skb, 4)) {
1174 IRDA_ERROR("%s: frame to short!\n", __FUNCTION__);
1175 return;
1178 frame = skb->data;
1180 info->nr = frame[2] >> 5; /* Next to receive */
1181 info->pf = frame[2] & PF_BIT; /* Final bit */
1182 info->ns = (frame[2] >> 1) & 0x07; /* Next to send */
1184 w = frame[3] & 0x01;
1185 x = frame[3] & 0x02;
1186 y = frame[3] & 0x04;
1187 z = frame[3] & 0x08;
1189 if (w) {
1190 IRDA_DEBUG(0, "Rejected control field is undefined or not "
1191 "implemented.\n");
1193 if (x) {
1194 IRDA_DEBUG(0, "Rejected control field was invalid because it "
1195 "contained a non permitted I field.\n");
1197 if (y) {
1198 IRDA_DEBUG(0, "Received I field exceeded the maximum negotiated "
1199 "for the existing connection or exceeded the maximum "
1200 "this station supports if no connection exists.\n");
1202 if (z) {
1203 IRDA_DEBUG(0, "Rejected control field control field contained an "
1204 "invalid Nr count.\n");
1206 irlap_do_event(self, RECV_FRMR_RSP, skb, info);
1210 * Function irlap_send_test_frame (self, daddr)
1212 * Send a test frame response
1215 void irlap_send_test_frame(struct irlap_cb *self, __u8 caddr, __u32 daddr,
1216 struct sk_buff *cmd)
1218 struct sk_buff *tx_skb;
1219 struct test_frame *frame;
1220 __u8 *info;
1222 tx_skb = alloc_skb(cmd->len + sizeof(struct test_frame), GFP_ATOMIC);
1223 if (!tx_skb)
1224 return;
1226 /* Broadcast frames must include saddr and daddr fields */
1227 if (caddr == CBROADCAST) {
1228 frame = (struct test_frame *)
1229 skb_put(tx_skb, sizeof(struct test_frame));
1231 /* Insert the swapped addresses */
1232 frame->saddr = cpu_to_le32(self->saddr);
1233 frame->daddr = cpu_to_le32(daddr);
1234 } else
1235 frame = (struct test_frame *) skb_put(tx_skb, LAP_ADDR_HEADER + LAP_CTRL_HEADER);
1237 frame->caddr = caddr;
1238 frame->control = TEST_RSP | PF_BIT;
1240 /* Copy info */
1241 info = skb_put(tx_skb, cmd->len);
1242 memcpy(info, cmd->data, cmd->len);
1244 /* Return to sender */
1245 irlap_wait_min_turn_around(self, &self->qos_tx);
1246 irlap_queue_xmit(self, tx_skb);
1250 * Function irlap_recv_test_frame (self, skb)
1252 * Receive a test frame
1255 static void irlap_recv_test_frame(struct irlap_cb *self, struct sk_buff *skb,
1256 struct irlap_info *info, int command)
1258 struct test_frame *frame;
1260 IRDA_DEBUG(2, "%s()\n", __FUNCTION__);
1262 if (!pskb_may_pull(skb, sizeof(*frame))) {
1263 IRDA_ERROR("%s: frame to short!\n", __FUNCTION__);
1264 return;
1266 frame = (struct test_frame *) skb->data;
1268 /* Broadcast frames must carry saddr and daddr fields */
1269 if (info->caddr == CBROADCAST) {
1270 if (skb->len < sizeof(struct test_frame)) {
1271 IRDA_DEBUG(0, "%s() test frame to short!\n",
1272 __FUNCTION__);
1273 return;
1276 /* Read and swap addresses */
1277 info->daddr = le32_to_cpu(frame->saddr);
1278 info->saddr = le32_to_cpu(frame->daddr);
1280 /* Make sure frame is addressed to us */
1281 if ((info->saddr != self->saddr) &&
1282 (info->saddr != BROADCAST)) {
1283 return;
1287 if (command)
1288 irlap_do_event(self, RECV_TEST_CMD, skb, info);
1289 else
1290 irlap_do_event(self, RECV_TEST_RSP, skb, info);
1294 * Function irlap_driver_rcv (skb, netdev, ptype)
1296 * Called when a frame is received. Dispatches the right receive function
1297 * for processing of the frame.
1299 * Note on skb management :
1300 * After calling the higher layers of the IrDA stack, we always
1301 * kfree() the skb, which drop the reference count (and potentially
1302 * destroy it).
1303 * If a higher layer of the stack want to keep the skb around (to put
1304 * in a queue or pass it to the higher layer), it will need to use
1305 * skb_get() to keep a reference on it. This is usually done at the
1306 * LMP level in irlmp.c.
1307 * Jean II
1309 int irlap_driver_rcv(struct sk_buff *skb, struct net_device *dev,
1310 struct packet_type *ptype, struct net_device *orig_dev)
1312 struct irlap_info info;
1313 struct irlap_cb *self;
1314 int command;
1315 __u8 control;
1317 /* FIXME: should we get our own field? */
1318 self = (struct irlap_cb *) dev->atalk_ptr;
1320 /* If the net device is down, then IrLAP is gone! */
1321 if (!self || self->magic != LAP_MAGIC) {
1322 dev_kfree_skb(skb);
1323 return -1;
1326 /* We are no longer an "old" protocol, so we need to handle
1327 * share and non linear skbs. This should never happen, so
1328 * we don't need to be clever about it. Jean II */
1329 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
1330 IRDA_ERROR("%s: can't clone shared skb!\n", __FUNCTION__);
1331 dev_kfree_skb(skb);
1332 return -1;
1335 /* Check if frame is large enough for parsing */
1336 if (!pskb_may_pull(skb, 2)) {
1337 IRDA_ERROR("%s: frame to short!\n", __FUNCTION__);
1338 dev_kfree_skb(skb);
1339 return -1;
1342 command = skb->data[0] & CMD_FRAME;
1343 info.caddr = skb->data[0] & CBROADCAST;
1345 info.pf = skb->data[1] & PF_BIT;
1346 info.control = skb->data[1] & ~PF_BIT; /* Mask away poll/final bit */
1348 control = info.control;
1350 /* First we check if this frame has a valid connection address */
1351 if ((info.caddr != self->caddr) && (info.caddr != CBROADCAST)) {
1352 IRDA_DEBUG(0, "%s(), wrong connection address!\n",
1353 __FUNCTION__);
1354 goto out;
1357 * Optimize for the common case and check if the frame is an
1358 * I(nformation) frame. Only I-frames have bit 0 set to 0
1360 if (~control & 0x01) {
1361 irlap_recv_i_frame(self, skb, &info, command);
1362 goto out;
1365 * We now check is the frame is an S(upervisory) frame. Only
1366 * S-frames have bit 0 set to 1 and bit 1 set to 0
1368 if (~control & 0x02) {
1370 * Received S(upervisory) frame, check which frame type it is
1371 * only the first nibble is of interest
1373 switch (control & 0x0f) {
1374 case RR:
1375 irlap_recv_rr_frame(self, skb, &info, command);
1376 break;
1377 case RNR:
1378 irlap_recv_rnr_frame(self, skb, &info, command);
1379 break;
1380 case REJ:
1381 irlap_recv_rej_frame(self, skb, &info, command);
1382 break;
1383 case SREJ:
1384 irlap_recv_srej_frame(self, skb, &info, command);
1385 break;
1386 default:
1387 IRDA_WARNING("%s: Unknown S-frame %02x received!\n",
1388 __FUNCTION__, info.control);
1389 break;
1391 goto out;
1394 * This must be a C(ontrol) frame
1396 switch (control) {
1397 case XID_RSP:
1398 irlap_recv_discovery_xid_rsp(self, skb, &info);
1399 break;
1400 case XID_CMD:
1401 irlap_recv_discovery_xid_cmd(self, skb, &info);
1402 break;
1403 case SNRM_CMD:
1404 irlap_recv_snrm_cmd(self, skb, &info);
1405 break;
1406 case DM_RSP:
1407 irlap_do_event(self, RECV_DM_RSP, skb, &info);
1408 break;
1409 case DISC_CMD: /* And RD_RSP since they have the same value */
1410 irlap_recv_disc_frame(self, skb, &info, command);
1411 break;
1412 case TEST_CMD:
1413 irlap_recv_test_frame(self, skb, &info, command);
1414 break;
1415 case UA_RSP:
1416 irlap_recv_ua_frame(self, skb, &info);
1417 break;
1418 case FRMR_RSP:
1419 irlap_recv_frmr_frame(self, skb, &info);
1420 break;
1421 case UI_FRAME:
1422 irlap_recv_ui_frame(self, skb, &info);
1423 break;
1424 default:
1425 IRDA_WARNING("%s: Unknown frame %02x received!\n",
1426 __FUNCTION__, info.control);
1427 break;
1429 out:
1430 /* Always drop our reference on the skb */
1431 dev_kfree_skb(skb);
1432 return 0;