cleanup
[linux-2.4.21-pre4.git] / net / irda / irlap_frame.c
1 /*********************************************************************
2  *                
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>
11  * 
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>
15  *     
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.
20  *
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.
24  *
25  ********************************************************************/
26
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>
32  
33 #include <net/pkt_sched.h>
34 #include <net/sock.h>
35  
36 #include <asm/byteorder.h>
37
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>
45
46 /*
47  * Function irlap_insert_info (self, skb)
48  *
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
52  */
53 static inline void irlap_insert_info(struct irlap_cb *self, 
54                                      struct sk_buff *skb)
55 {
56         struct irda_skb_cb *cb = (struct irda_skb_cb *) skb->cb;
57
58         /*  
59          * Insert MTT (min. turn time) and speed into skb, so that the
60          * device driver knows which settings to use 
61          */
62         cb->magic = LAP_MAGIC;
63         cb->mtt = self->mtt_required;
64         cb->next_speed = self->speed;
65
66         /* Reset */
67         self->mtt_required = 0;
68         
69         /* 
70          * Delay equals negotiated BOFs count, plus the number of BOFs to 
71          * force the negotiated minimum turnaround time 
72          */
73         cb->xbofs = self->bofs_count;
74         cb->next_xbofs = self->next_bofs;
75         cb->xbofs_delay = self->xbofs_delay;
76         
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;
81 }
82
83 /*
84  * Function irlap_queue_xmit (self, skb)
85  *
86  *    A little wrapper for dev_queue_xmit, so we can insert some common
87  *    code into it.
88  */
89 void irlap_queue_xmit(struct irlap_cb *self, struct sk_buff *skb)
90 {
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;
96
97         irlap_insert_info(self, skb);
98
99         dev_queue_xmit(skb);
100 }
101
102 /*
103  * Function irlap_send_snrm_cmd (void)
104  *
105  *    Transmits a connect SNRM command frame
106  */
107 void irlap_send_snrm_frame(struct irlap_cb *self, struct qos_info *qos) 
108 {
109         struct sk_buff *skb;
110         struct snrm_frame *frame;
111         int ret;
112
113         ASSERT(self != NULL, return;);
114         ASSERT(self->magic == LAP_MAGIC, return;);
115
116         /* Allocate frame */
117         skb = dev_alloc_skb(64);
118         if (!skb)
119                 return;
120
121         frame = (struct snrm_frame *) skb_put(skb, 2); 
122
123         /* Insert connection address field */
124         if (qos)
125                 frame->caddr = CMD_FRAME | CBROADCAST;
126         else
127                 frame->caddr = CMD_FRAME | self->caddr;
128
129         /* Insert control field */
130         frame->control = SNRM_CMD | PF_BIT;
131         
132         /*
133          *  If we are establishing a connection then insert QoS paramerters 
134          */
135         if (qos) {
136                 skb_put(skb, 9); /* 21 left */
137                 frame->saddr = cpu_to_le32(self->saddr);
138                 frame->daddr = cpu_to_le32(self->daddr);
139
140                 frame->ncaddr = self->caddr;
141                                 
142                 ret = irlap_insert_qos_negotiation_params(self, skb);
143                 if (ret < 0) {
144                         dev_kfree_skb(skb);
145                         return;
146                 }
147         }
148         irlap_queue_xmit(self, skb);
149 }
150
151 /*
152  * Function irlap_recv_snrm_cmd (skb, info)
153  *
154  *    Received SNRM (Set Normal Response Mode) command frame
155  *
156  */
157 static void irlap_recv_snrm_cmd(struct irlap_cb *self, struct sk_buff *skb, 
158                                 struct irlap_info *info) 
159 {
160         struct snrm_frame *frame;
161
162         frame = (struct snrm_frame *) skb->data;
163         
164         if (skb->len >= sizeof(struct snrm_frame)) {
165                 /* Copy the new connection address */
166                 info->caddr = frame->ncaddr;
167
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");
172                         return;
173                 }
174                 
175                 /* Copy peer device address */
176                 info->daddr = le32_to_cpu(frame->saddr);
177                 info->saddr = le32_to_cpu(frame->daddr);
178                 
179                 /* Only accept if addressed directly to us */
180                 if (info->saddr != self->saddr) {
181                         IRDA_DEBUG(2, __FUNCTION__ "(), not addressed to us!\n");
182                         return;
183                 }
184                 irlap_do_event(self, RECV_SNRM_CMD, skb, info);
185         } else {
186                 /* Signal that this SNRM frame does not contain and I-field */
187                 irlap_do_event(self, RECV_SNRM_CMD, skb, NULL);
188         }
189 }
190
191 /*
192  * Function irlap_send_ua_response_frame (qos)
193  *
194  *    Send UA (Unnumbered Acknowledgement) frame
195  *
196  */
197 void irlap_send_ua_response_frame(struct irlap_cb *self, struct qos_info *qos)
198 {
199         struct sk_buff *skb;
200         struct ua_frame *frame;
201         int ret;
202         
203         IRDA_DEBUG(2, __FUNCTION__ "() <%ld>\n", jiffies);
204         
205         ASSERT(self != NULL, return;);
206         ASSERT(self->magic == LAP_MAGIC, return;);
207
208         skb = NULL;
209
210         /* Allocate frame */
211         skb = dev_alloc_skb(64);
212         if (!skb)
213                 return;
214
215         frame = (struct ua_frame *) skb_put(skb, 10);
216         
217         /* Build UA response */
218         frame->caddr = self->caddr;
219         frame->control = UA_RSP | PF_BIT;
220
221         frame->saddr = cpu_to_le32(self->saddr);
222         frame->daddr = cpu_to_le32(self->daddr);
223
224         /* Should we send QoS negotiation parameters? */
225         if (qos) {
226                 ret = irlap_insert_qos_negotiation_params(self, skb);
227                 if (ret < 0) {
228                         dev_kfree_skb(skb);
229                         return;
230                 }
231         }
232
233         irlap_queue_xmit(self, skb);
234 }
235
236
237 /*
238  * Function irlap_send_dm_frame (void)
239  *
240  *    Send disconnected mode (DM) frame
241  *
242  */
243 void irlap_send_dm_frame( struct irlap_cb *self)
244 {
245         struct sk_buff *skb = NULL;
246         __u8 *frame;
247         
248         ASSERT(self != NULL, return;);
249         ASSERT(self->magic == LAP_MAGIC, return;);
250
251         skb = dev_alloc_skb(32);
252         if (!skb)
253                 return;
254
255         frame = skb_put( skb, 2);
256         
257         if (self->state == LAP_NDM)
258                 frame[0] = CBROADCAST;
259         else
260                 frame[0] = self->caddr;
261
262         frame[1] = DM_RSP | PF_BIT;
263
264         irlap_queue_xmit(self, skb);    
265 }
266
267 /*
268  * Function irlap_send_disc_frame (void)
269  *
270  *    Send disconnect (DISC) frame
271  *
272  */
273 void irlap_send_disc_frame(struct irlap_cb *self) 
274 {
275         struct sk_buff *skb = NULL;
276         __u8 *frame;
277         
278         IRDA_DEBUG(3, __FUNCTION__ "()\n");
279
280         ASSERT(self != NULL, return;);
281         ASSERT(self->magic == LAP_MAGIC, return;);
282
283         skb = dev_alloc_skb(16);
284         if (!skb)
285                 return;
286
287         frame = skb_put(skb, 2);
288         
289         frame[0] = self->caddr | CMD_FRAME;
290         frame[1] = DISC_CMD | PF_BIT;
291
292         irlap_queue_xmit(self, skb);
293 }
294
295 /*
296  * Function irlap_send_discovery_xid_frame (S, s, command)
297  *
298  *    Build and transmit a XID (eXchange station IDentifier) discovery
299  *    frame. 
300  */
301 void irlap_send_discovery_xid_frame(struct irlap_cb *self, int S, __u8 s, 
302                                     __u8 command, discovery_t *discovery) 
303 {
304         struct sk_buff *skb = NULL;
305         struct xid_frame *frame;
306         __u32 bcast = BROADCAST;
307         __u8 *info;
308
309         IRDA_DEBUG(4, __FUNCTION__ "(), s=%d, S=%d, command=%d\n", s, S, 
310                    command);
311
312         ASSERT(self != NULL, return;);
313         ASSERT(self->magic == LAP_MAGIC, return;);
314         ASSERT(discovery != NULL, return;);
315
316         skb = dev_alloc_skb(64);
317         if (!skb)
318                 return;
319
320         skb_put(skb, 14);
321         frame = (struct xid_frame *) skb->data;
322
323         if (command) {
324                 frame->caddr = CBROADCAST | CMD_FRAME;
325                 frame->control =  XID_CMD | PF_BIT;
326         } else {
327                 frame->caddr = CBROADCAST;
328                 frame->control =  XID_RSP | PF_BIT;
329         }
330         frame->ident = XID_FORMAT;
331
332         frame->saddr = cpu_to_le32(self->saddr);
333
334         if (command)
335                 frame->daddr = cpu_to_le32(bcast);
336         else
337                 frame->daddr = cpu_to_le32(discovery->daddr);
338         
339         switch (S) {
340         case 1:
341                 frame->flags = 0x00;
342                 break;
343         case 6:
344                 frame->flags = 0x01;
345                 break;
346         case 8:
347                 frame->flags = 0x02;
348                 break;
349         case 16:
350                 frame->flags = 0x03;
351                 break;
352         default:
353                 frame->flags = 0x02;
354                 break;
355         }
356
357         frame->slotnr = s; 
358         frame->version = 0x00;
359
360         /*  
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.
364          */
365         if (!command || (frame->slotnr == 0xff)) {
366                 int len;
367
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];
372                 } else {
373                         info = skb_put(skb, 1);
374                         info[0] = discovery->hints.byte[0];
375                 }
376                 info = skb_put(skb, 1);
377                 info[0] = discovery->charset;
378
379                 len = IRDA_MIN(discovery->name_len, skb_tailroom(skb));
380                 info = skb_put(skb, len);
381                 memcpy(info, discovery->nickname, len);
382         } 
383         irlap_queue_xmit(self, skb);
384 }
385
386 /*
387  * Function irlap_recv_discovery_xid_rsp (skb, info)
388  *
389  *    Received a XID discovery response
390  *
391  */
392 static void irlap_recv_discovery_xid_rsp(struct irlap_cb *self, 
393                                          struct sk_buff *skb, 
394                                          struct irlap_info *info) 
395 {
396         struct xid_frame *xid;
397         discovery_t *discovery = NULL;
398         __u8 *discovery_info;
399         char *text;
400
401         IRDA_DEBUG(4, __FUNCTION__ "()\n");
402
403         ASSERT(self != NULL, return;);
404         ASSERT(self->magic == LAP_MAGIC, return;);
405
406         xid = (struct xid_frame *) skb->data;
407
408         info->daddr = le32_to_cpu(xid->saddr);
409         info->saddr = le32_to_cpu(xid->daddr);
410
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");
415                 return;
416         }
417
418         if ((discovery = kmalloc(sizeof(discovery_t), GFP_ATOMIC)) == NULL) {
419                 WARNING(__FUNCTION__ "(), kmalloc failed!\n");
420                 return;
421         }
422         memset(discovery, 0, sizeof(discovery_t));
423
424         discovery->daddr = info->daddr;
425         discovery->saddr = self->saddr;
426         discovery->timestamp = jiffies;
427
428         IRDA_DEBUG(4, __FUNCTION__ "(), daddr=%08x\n", discovery->daddr);
429
430         discovery_info = skb_pull(skb, sizeof(struct xid_frame));
431
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];
439         } else {
440                 discovery->hints.byte[1] = 0;
441                 discovery->charset = discovery_info[1];
442                 text = (char *) &discovery_info[2];
443         }
444         /* 
445          *  Terminate info string, should be safe since this is where the 
446          *  FCS bytes resides.
447          */
448         skb->data[skb->len] = '\0'; 
449         strncpy(discovery->nickname, text, NICKNAME_MAX_LEN);
450         discovery->name_len = strlen(discovery->nickname);
451
452         info->discovery = discovery;
453
454         irlap_do_event(self, RECV_DISCOVERY_XID_RSP, skb, info);
455 }
456
457 /*
458  * Function irlap_recv_discovery_xid_cmd (skb, info)
459  *
460  *    Received a XID discovery command
461  *
462  */
463 static void irlap_recv_discovery_xid_cmd(struct irlap_cb *self, 
464                                          struct sk_buff *skb, 
465                                          struct irlap_info *info) 
466 {
467         struct xid_frame *xid;
468         discovery_t *discovery = NULL;
469         __u8 *discovery_info;
470         char *text;
471
472         xid = (struct xid_frame *) skb->data;
473
474         info->daddr = le32_to_cpu(xid->saddr);
475         info->saddr = le32_to_cpu(xid->daddr);
476
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");
481                 return;
482         }
483
484         switch (xid->flags & 0x03) {
485         case 0x00:
486                 info->S = 1;
487                 break;
488         case 0x01:
489                 info->S = 6;
490                 break;
491         case 0x02:
492                 info->S = 8;
493                 break;
494         case 0x03:
495                 info->S = 16;
496                 break;
497         default:
498                 /* Error!! */
499                 dev_kfree_skb(skb);
500                 return;
501         }
502         info->s = xid->slotnr;
503         
504         discovery_info = skb_pull(skb, sizeof(struct xid_frame));
505
506         /* 
507          *  Check if last frame 
508          */
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");
513                         return;
514                 }
515
516                 /*
517                  *  We now have some discovery info to deliver!
518                  */
519                 discovery = kmalloc(sizeof(discovery_t), GFP_ATOMIC);
520                 if (!discovery) {
521                         WARNING(__FUNCTION__ "(), unable to malloc!\n");
522                         return;
523                 }
524               
525                 discovery->daddr = info->daddr;
526                 discovery->saddr = self->saddr;
527                 discovery->timestamp = jiffies;
528
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];
534                 } else {
535                         discovery->hints.byte[1] = 0;
536                         discovery->charset = discovery_info[1];
537                         text = (char *) &discovery_info[2];
538                 }
539                 /* 
540                  *  Terminate string, should be safe since this is where the 
541                  *  FCS bytes resides.
542                  */
543                 skb->data[skb->len] = '\0'; 
544                 strncpy(discovery->nickname, text, NICKNAME_MAX_LEN);
545                 discovery->name_len = strlen(discovery->nickname);
546
547                 info->discovery = discovery;
548         } else
549                 info->discovery = NULL;
550
551         irlap_do_event(self, RECV_DISCOVERY_XID_CMD, skb, info);
552 }
553
554 /*
555  * Function irlap_send_rr_frame (self, command)
556  *
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.
559  */
560 void irlap_send_rr_frame(struct irlap_cb *self, int command) 
561 {
562         struct sk_buff *skb;
563         __u8 *frame;
564
565         skb = dev_alloc_skb(16);
566         if (!skb)
567                 return;
568         
569         frame = skb_put(skb, 2);
570         
571         frame[0] = self->caddr;
572         frame[0] |= (command) ? CMD_FRAME : 0;
573
574         frame[1] = RR | PF_BIT | (self->vr << 5);
575
576         irlap_queue_xmit(self, skb);
577 }
578
579 /*
580  * Function irlap_send_rd_frame (self)
581  *
582  *    Request disconnect. Used by a secondary station to request the 
583  *    disconnection of the link.
584  */
585 void irlap_send_rd_frame(struct irlap_cb *self)
586 {
587         struct sk_buff *skb;
588         __u8 *frame;
589
590         skb = dev_alloc_skb(16);
591         if (!skb)
592                 return;
593         
594         frame = skb_put(skb, 2);
595         
596         frame[0] = self->caddr;
597         frame[1] = RD_RSP | PF_BIT;
598
599         irlap_queue_xmit(self, skb);
600 }
601
602 /*
603  * Function irlap_recv_rr_frame (skb, info)
604  *
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).
608  */
609 static inline void irlap_recv_rr_frame(struct irlap_cb *self, 
610                                        struct sk_buff *skb, 
611                                        struct irlap_info *info, int command)
612 {
613         info->nr = skb->data[1] >> 5;
614
615         /* Check if this is a command or a response frame */
616         if (command)
617                 irlap_do_event(self, RECV_RR_CMD, skb, info);
618         else
619                 irlap_do_event(self, RECV_RR_RSP, skb, info);
620 }
621
622 void irlap_send_frmr_frame( struct irlap_cb *self, int command)
623 {
624         struct sk_buff *skb = NULL;
625         __u8 *frame;
626         
627         ASSERT( self != NULL, return;);
628         ASSERT( self->magic == LAP_MAGIC, return;);
629
630         skb = dev_alloc_skb( 32);
631         if (!skb)
632                 return;
633
634         frame = skb_put( skb, 2);
635         
636         frame[0] = self->caddr;
637         frame[0] |= (command) ? CMD_FRAME : 0;
638
639         frame[1]  = (self->vs << 1);
640         frame[1] |= PF_BIT;
641         frame[1] |= (self->vr << 5);
642
643         frame[2] = 0;
644
645         IRDA_DEBUG(4, __FUNCTION__ "(), vr=%d, %ld\n",self->vr, jiffies); 
646
647         irlap_queue_xmit(self, skb);
648 }
649
650 /*
651  * Function irlap_recv_rnr_frame (self, skb, info)
652  *
653  *    Received RNR (Receive Not Ready) frame from peer station
654  *
655  */
656 static void irlap_recv_rnr_frame(struct irlap_cb *self, struct sk_buff *skb, 
657                                  struct irlap_info *info, int command) 
658 {
659         info->nr = skb->data[1] >> 5;
660
661         IRDA_DEBUG(4, __FUNCTION__ "(), nr=%d, %ld\n", info->nr, jiffies);
662
663         if (command)
664                 irlap_do_event(self, RECV_RNR_CMD, skb, info);
665         else
666                 irlap_do_event(self, RECV_RNR_RSP, skb, info);
667 }
668
669 static void irlap_recv_rej_frame(struct irlap_cb *self, struct sk_buff *skb, 
670                                  struct irlap_info *info, int command)
671 {
672         IRDA_DEBUG(0, __FUNCTION__ "()\n");
673
674         info->nr = skb->data[1] >> 5;
675         
676         /* Check if this is a command or a response frame */
677         if (command)
678                 irlap_do_event(self, RECV_REJ_CMD, skb, info);
679         else
680                 irlap_do_event(self, RECV_REJ_RSP, skb, info);
681 }
682
683 static void irlap_recv_srej_frame(struct irlap_cb *self, struct sk_buff *skb, 
684                                   struct irlap_info *info, int command)
685 {
686         IRDA_DEBUG(0, __FUNCTION__ "()\n");
687
688         info->nr = skb->data[1] >> 5;
689         
690         /* Check if this is a command or a response frame */
691         if (command)
692                 irlap_do_event(self, RECV_SREJ_CMD, skb, info);
693         else
694                 irlap_do_event(self, RECV_SREJ_RSP, skb, info);
695 }
696
697 static void irlap_recv_disc_frame(struct irlap_cb *self, struct sk_buff *skb, 
698                                   struct irlap_info *info, int command)
699 {
700         IRDA_DEBUG(2, __FUNCTION__ "()\n");
701
702         /* Check if this is a command or a response frame */
703         if (command)
704                 irlap_do_event(self, RECV_DISC_CMD, skb, info);
705         else
706                 irlap_do_event(self, RECV_RD_RSP, skb, info);
707 }
708
709 /*
710  * Function irlap_recv_ua_frame (skb, frame)
711  *
712  *    Received UA (Unnumbered Acknowledgement) frame
713  *
714  */
715 static inline void irlap_recv_ua_frame(struct irlap_cb *self, 
716                                        struct sk_buff *skb, 
717                                        struct irlap_info *info) 
718 {
719         irlap_do_event(self, RECV_UA_RSP, skb, info);
720 }
721
722 /*
723  * Function irlap_send_data_primary(self, skb)
724  *
725  *    Send I-frames as the primary station but without the poll bit set
726  *
727  */
728 void irlap_send_data_primary(struct irlap_cb *self, struct sk_buff *skb)
729 {
730         struct sk_buff *tx_skb;
731
732         if (skb->data[1] == I_FRAME) {
733
734                 /*  
735                  *  Insert frame sequence number (Vs) in control field before
736                  *  inserting into transmit window queue.
737                  */
738                 skb->data[1] = I_FRAME | (self->vs << 1);
739                 
740                 /* Copy buffer */
741                 tx_skb = skb_clone(skb, GFP_ATOMIC);
742                 if (tx_skb == NULL) {
743                         return;
744                 }
745                 
746                 /* 
747                  *  Insert frame in store, in case of retransmissions 
748                  */
749                 skb_queue_tail(&self->wx_list, skb_get(skb));
750                 
751                 self->vs = (self->vs + 1) % 8;
752                 self->ack_required = FALSE;             
753                 self->window -= 1;
754
755                 irlap_send_i_frame( self, tx_skb, CMD_FRAME);
756         } else {
757                 IRDA_DEBUG(4, __FUNCTION__ "(), sending unreliable frame\n");
758                 irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
759                 self->window -= 1;
760         }
761 }
762 /*
763  * Function irlap_send_data_primary_poll (self, skb)
764  *
765  *    Send I(nformation) frame as primary with poll bit set
766  */
767 void irlap_send_data_primary_poll(struct irlap_cb *self, struct sk_buff *skb) 
768 {
769         struct sk_buff *tx_skb;
770
771         /* Stop P timer */
772         del_timer(&self->poll_timer);
773                 
774         /* Is this reliable or unreliable data? */
775         if (skb->data[1] == I_FRAME) {
776                 
777                 /*  
778                  *  Insert frame sequence number (Vs) in control field before
779                  *  inserting into transmit window queue.
780                  */
781                 skb->data[1] = I_FRAME | (self->vs << 1);
782                 
783                 /* Copy buffer */
784                 tx_skb = skb_clone(skb, GFP_ATOMIC);
785                 if (tx_skb == NULL) {
786                         return;
787                 }
788                 
789                 /* 
790                  *  Insert frame in store, in case of retransmissions 
791                  */
792                 skb_queue_tail(&self->wx_list, skb_get(skb));
793                 
794                 /*  
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.  
798                  */
799                 tx_skb->data[1] |= PF_BIT;
800                 
801                 self->vs = (self->vs + 1) % 8;
802                 self->ack_required = FALSE;
803
804                 irlap_send_i_frame(self, tx_skb, CMD_FRAME);
805         } else {
806                 IRDA_DEBUG(4, __FUNCTION__ "(), sending unreliable frame\n");
807
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;
812                 } else {
813                         skb->data[1] |= PF_BIT;
814                         irlap_send_ui_frame(self, skb_get(skb), self->caddr, CMD_FRAME);
815                 }
816         }
817
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 */
823
824         irlap_start_final_timer(self, self->final_timeout);
825 }
826
827 /*
828  * Function irlap_send_data_secondary_final (self, skb)
829  *
830  *    Send I(nformation) frame as secondary with final bit set
831  *
832  */
833 void irlap_send_data_secondary_final(struct irlap_cb *self, 
834                                      struct sk_buff *skb) 
835 {
836         struct sk_buff *tx_skb = NULL;
837
838         ASSERT(self != NULL, return;);
839         ASSERT(self->magic == LAP_MAGIC, return;);
840         ASSERT(skb != NULL, return;);
841
842         /* Is this reliable or unreliable data? */
843         if (skb->data[1] == I_FRAME) {
844
845                 /*  
846                  *  Insert frame sequence number (Vs) in control field before
847                  *  inserting into transmit window queue.
848                  */
849                 skb->data[1] = I_FRAME | (self->vs << 1);
850                 
851                 tx_skb = skb_clone(skb, GFP_ATOMIC);
852                 if (tx_skb == NULL) {
853                         return;
854                 }               
855
856                 /* Insert frame in store */
857                 skb_queue_tail(&self->wx_list, skb_get(skb));
858                 
859                 tx_skb->data[1] |= PF_BIT;
860                 
861                 self->vs = (self->vs + 1) % 8; 
862                 self->ack_required = FALSE;
863                 
864                 irlap_send_i_frame(self, tx_skb, RSP_FRAME); 
865         } else {
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;
870                 } else {
871                         skb->data[1] |= PF_BIT;
872                         irlap_send_ui_frame(self, skb_get(skb), self->caddr, RSP_FRAME);
873                 }
874         }
875
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 */
881
882         irlap_start_wd_timer(self, self->wd_timeout);
883 }
884
885 /*
886  * Function irlap_send_data_secondary (self, skb)
887  *
888  *    Send I(nformation) frame as secondary without final bit set
889  *
890  */
891 void irlap_send_data_secondary(struct irlap_cb *self, struct sk_buff *skb) 
892 {
893         struct sk_buff *tx_skb = NULL;
894
895         /* Is this reliable or unreliable data? */
896         if (skb->data[1] == I_FRAME) {
897                 
898                 /*  
899                  *  Insert frame sequence number (Vs) in control field before
900                  *  inserting into transmit window queue.
901                  */
902                 skb->data[1] = I_FRAME | (self->vs << 1);
903                 
904                 tx_skb = skb_clone(skb, GFP_ATOMIC);
905                 if (tx_skb == NULL) {
906                         return;
907                 }               
908                 
909                 /* Insert frame in store */
910                 skb_queue_tail(&self->wx_list, skb_get(skb));
911                 
912                 self->vs = (self->vs + 1) % 8;
913                 self->ack_required = FALSE;             
914                 self->window -= 1;
915
916                 irlap_send_i_frame(self, tx_skb, RSP_FRAME); 
917         } else {
918                 irlap_send_ui_frame(self, skb_get(skb), self->caddr, RSP_FRAME);
919                 self->window -= 1;
920         }
921 }
922
923 /*
924  * Function irlap_resend_rejected_frames (nr)
925  *
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)
929  */
930 void irlap_resend_rejected_frames(struct irlap_cb *self, int command)
931 {
932         struct sk_buff *tx_skb;
933         struct sk_buff *skb;
934         int count;
935
936         ASSERT(self != NULL, return;);
937         ASSERT(self->magic == LAP_MAGIC, return;);
938
939         /* Initialize variables */
940         skb = tx_skb = NULL;
941
942         count = skb_queue_len(&self->wx_list);
943
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);
948
949                 /* We copy the skb to be retransmitted since we will have to 
950                  * modify it. Cloning will confuse packet sniffers 
951                  */
952                 /* tx_skb = skb_clone( skb, GFP_ATOMIC); */
953                 tx_skb = skb_copy(skb, GFP_ATOMIC);
954                 if (!tx_skb) {
955                         IRDA_DEBUG(0, __FUNCTION__ "(), unable to copy\n");
956                         return;
957                 }
958                 /* Unlink tx_skb from list */
959                 tx_skb->next = tx_skb->prev = NULL;
960                 tx_skb->list = NULL;
961
962                 /* Clear old Nr field + poll bit */
963                 tx_skb->data[1] &= 0x0f;
964
965                 /* 
966                  *  Set poll bit on the last frame retransmitted
967                  */
968                 if (count-- == 1)
969                         tx_skb->data[1] |= PF_BIT; /* Set p/f bit */
970                 else
971                         tx_skb->data[1] &= ~PF_BIT; /* Clear p/f bit */
972                 
973                 irlap_send_i_frame(self, tx_skb, command);
974
975                 /* 
976                  *  If our skb is the last buffer in the list, then
977                  *  we are finished, if not, move to the next sk-buffer
978                  */
979                 if (skb == skb_peek_tail(&self->wx_list))
980                         skb = NULL;
981                 else
982                         skb = skb->next;
983         }
984 #if 0 /* Not yet */
985         /* 
986          *  We can now fill the window with additinal data frames
987          */
988         while (skb_queue_len( &self->txq) > 0) {
989                 
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;);
995
996                         /*
997                          *  If send window > 1 then send frame with pf 
998                          *  bit cleared
999                          */ 
1000                         if ((self->window > 1) && 
1001                             skb_queue_len(&self->txq) > 0) 
1002                         {
1003                                 irlap_send_data_primary(self, skb);
1004                         } else {
1005                                 irlap_send_data_primary_poll(self, skb);
1006                         }
1007                         kfree_skb(skb);
1008                 }
1009         }
1010 #endif
1011 }
1012
1013 void irlap_resend_rejected_frame(struct irlap_cb *self, int command)
1014 {
1015         struct sk_buff *tx_skb;
1016         struct sk_buff *skb;
1017
1018         ASSERT(self != NULL, return;);
1019         ASSERT(self->magic == LAP_MAGIC, return;);
1020
1021         /* Initialize variables */
1022         skb = tx_skb = NULL;
1023
1024         /*  Resend unacknowledged frame(s) */
1025         skb = skb_peek(&self->wx_list);
1026         if (skb != NULL) {
1027                 irlap_wait_min_turn_around(self, &self->qos_tx);
1028
1029                 /* We copy the skb to be retransmitted since we will have to 
1030                  * modify it. Cloning will confuse packet sniffers 
1031                  */
1032                 /* tx_skb = skb_clone( skb, GFP_ATOMIC); */
1033                 tx_skb = skb_copy(skb, GFP_ATOMIC);
1034                 if (!tx_skb) {
1035                         IRDA_DEBUG(0, __FUNCTION__ "(), unable to copy\n");
1036                         return; 
1037                 }
1038                 /* Unlink tx_skb from list */
1039                 tx_skb->next = tx_skb->prev = NULL;
1040                 tx_skb->list = NULL;
1041
1042                 /* Clear old Nr field + poll bit */
1043                 tx_skb->data[1] &= 0x0f;
1044
1045                 /*  Set poll/final bit */
1046                 tx_skb->data[1] |= PF_BIT; /* Set p/f bit */
1047                 
1048                 irlap_send_i_frame(self, tx_skb, command);
1049         }
1050 }
1051
1052 /*
1053  * Function irlap_send_ui_frame (self, skb, command)
1054  *
1055  *    Contruct and transmit an Unnumbered Information (UI) frame
1056  *
1057  */
1058 void irlap_send_ui_frame(struct irlap_cb *self, struct sk_buff *skb, 
1059                          __u8 caddr, int command)
1060 {
1061         IRDA_DEBUG(4, __FUNCTION__ "()\n");
1062         
1063         ASSERT(self != NULL, return;);
1064         ASSERT(self->magic == LAP_MAGIC, return;);
1065         ASSERT(skb != NULL, return;);
1066         
1067         /* Insert connection address */
1068         skb->data[0] = caddr | ((command) ? CMD_FRAME : 0);
1069
1070         irlap_queue_xmit(self, skb);
1071 }
1072
1073 /*
1074  * Function irlap_send_i_frame (skb)
1075  *
1076  *    Contruct and transmit Information (I) frame
1077  */
1078 void irlap_send_i_frame(struct irlap_cb *self, struct sk_buff *skb, 
1079                         int command) 
1080 {
1081         /* Insert connection address */
1082         skb->data[0] = self->caddr;
1083         skb->data[0] |= (command) ? CMD_FRAME : 0;
1084         
1085         /* Insert next to receive (Vr) */
1086         skb->data[1] |= (self->vr << 5);  /* insert nr */
1087
1088         irlap_queue_xmit(self, skb);
1089 }
1090
1091 /*
1092  * Function irlap_recv_i_frame (skb, frame)
1093  *
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).
1096  */
1097 static inline void irlap_recv_i_frame(struct irlap_cb *self, 
1098                                       struct sk_buff *skb, 
1099                                       struct irlap_info *info, int command) 
1100 {
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 */
1104
1105         /* Check if this is a command or a response frame */
1106         if (command)
1107                 irlap_do_event(self, RECV_I_CMD, skb, info);
1108         else
1109                 irlap_do_event(self, RECV_I_RSP, skb, info);
1110 }
1111
1112 /*
1113  * Function irlap_recv_ui_frame (self, skb, info)
1114  *
1115  *    Receive and parse an Unnumbered Information (UI) frame
1116  *
1117  */
1118 static void irlap_recv_ui_frame(struct irlap_cb *self, struct sk_buff *skb, 
1119                                 struct irlap_info *info)
1120 {
1121         IRDA_DEBUG( 4, __FUNCTION__ "()\n");
1122
1123         info->pf = skb->data[1] & PF_BIT;      /* Final bit */
1124
1125         irlap_do_event(self, RECV_UI_FRAME, skb, info);
1126 }
1127
1128 /*
1129  * Function irlap_recv_frmr_frame (skb, frame)
1130  *
1131  *    Received Frame Reject response.
1132  *
1133  */
1134 static void irlap_recv_frmr_frame(struct irlap_cb *self, struct sk_buff *skb, 
1135                                   struct irlap_info *info) 
1136 {
1137         __u8 *frame;
1138         int w, x, y, z;
1139
1140         IRDA_DEBUG(0, __FUNCTION__ "()\n");
1141         
1142         ASSERT(self != NULL, return;);
1143         ASSERT(self->magic == LAP_MAGIC, return;);
1144         ASSERT(skb != NULL, return;);
1145         ASSERT(info != NULL, return;);
1146         
1147         frame = skb->data;
1148
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 */
1152
1153         w = frame[3] & 0x01;
1154         x = frame[3] & 0x02;
1155         y = frame[3] & 0x04;
1156         z = frame[3] & 0x08;
1157         
1158         if (w) {
1159                 IRDA_DEBUG(0, "Rejected control field is undefined or not "
1160                       "implemented.\n");
1161         } 
1162         if (x) {
1163                 IRDA_DEBUG(0, "Rejected control field was invalid because it "
1164                       "contained a non permitted I field.\n");
1165         }
1166         if (y) {
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");
1170         }
1171         if (z) {
1172                 IRDA_DEBUG(0, "Rejected control field control field contained an "
1173                       "invalid Nr count.\n");
1174         }
1175         irlap_do_event(self, RECV_FRMR_RSP, skb, info);
1176 }
1177
1178 /*
1179  * Function irlap_send_test_frame (self, daddr)
1180  *
1181  *    Send a test frame response
1182  *
1183  */
1184 void irlap_send_test_frame(struct irlap_cb *self, __u8 caddr, __u32 daddr, 
1185                            struct sk_buff *cmd)
1186 {
1187         struct sk_buff *skb;
1188         struct test_frame *frame;
1189         __u8 *info;
1190
1191         skb = dev_alloc_skb(cmd->len+sizeof(struct test_frame));
1192         if (!skb)
1193                 return;
1194
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));
1199
1200                 /* Insert the swapped addresses */
1201                 frame->saddr = cpu_to_le32(self->saddr);
1202                 frame->daddr = cpu_to_le32(daddr);
1203         } else
1204                 frame = (struct test_frame *) skb_put(skb, LAP_ADDR_HEADER + LAP_CTRL_HEADER);
1205
1206         frame->caddr = caddr;
1207         frame->control = TEST_RSP | PF_BIT;
1208
1209         /* Copy info */
1210         info = skb_put(skb, cmd->len);
1211         memcpy(info, cmd->data, cmd->len);
1212
1213         /* Return to sender */
1214         irlap_wait_min_turn_around(self, &self->qos_tx);
1215         irlap_queue_xmit(self, skb);
1216 }
1217
1218 /*
1219  * Function irlap_recv_test_frame (self, skb)
1220  *
1221  *    Receive a test frame
1222  *
1223  */
1224 static void irlap_recv_test_frame(struct irlap_cb *self, struct sk_buff *skb, 
1225                                   struct irlap_info *info, int command)
1226 {
1227         struct test_frame *frame;
1228
1229         IRDA_DEBUG(2, __FUNCTION__ "()\n");
1230         
1231         frame = (struct test_frame *) skb->data;
1232                 
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");
1238                         return;
1239                 }
1240                 
1241                 /* Read and swap addresses */
1242                 info->daddr = le32_to_cpu(frame->saddr);
1243                 info->saddr = le32_to_cpu(frame->daddr);
1244
1245                 /* Make sure frame is addressed to us */
1246                 if ((info->saddr != self->saddr) && 
1247                     (info->saddr != BROADCAST)) {
1248                         return;
1249                 }
1250         }
1251
1252         if (command)
1253                 irlap_do_event(self, RECV_TEST_CMD, skb, info);
1254         else
1255                 irlap_do_event(self, RECV_TEST_RSP, skb, info);
1256 }
1257
1258 /*
1259  * Function irlap_driver_rcv (skb, netdev, ptype)
1260  *
1261  *    Called when a frame is received. Dispatches the right receive function 
1262  *    for processing of the frame.
1263  *
1264  */
1265 int irlap_driver_rcv(struct sk_buff *skb, struct net_device *dev, 
1266                      struct packet_type *ptype)
1267 {
1268         struct irlap_info info;
1269         struct irlap_cb *self;
1270         int command;
1271         __u8 control;
1272         
1273         /* FIXME: should we get our own field? */
1274         self = (struct irlap_cb *) dev->atalk_ptr;
1275
1276         /* If the net device is down, then IrLAP is gone! */
1277         if (!self || self->magic != LAP_MAGIC) {
1278                 dev_kfree_skb(skb);
1279                 return -1;
1280         }
1281
1282         /* Check if frame is large enough for parsing */
1283         if (skb->len < 2) {
1284                 ERROR(__FUNCTION__ "(), frame to short!\n");
1285                 dev_kfree_skb(skb);
1286                 return -1;
1287         }
1288         
1289         command    = skb->data[0] & CMD_FRAME;
1290         info.caddr = skb->data[0] & CBROADCAST;
1291         
1292         info.pf      = skb->data[1] &  PF_BIT;
1293         info.control = skb->data[1] & ~PF_BIT; /* Mask away poll/final bit */
1294
1295         control = info.control;
1296
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");
1300                 goto out;
1301         }
1302         /*  
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
1305          */
1306         if (~control & 0x01) {
1307                 irlap_recv_i_frame(self, skb, &info, command);
1308                 goto out;
1309         }
1310         /*
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
1313          */
1314         if (~control & 0x02) {
1315                 /* 
1316                  *  Received S(upervisory) frame, check which frame type it is
1317                  *  only the first nibble is of interest
1318                  */
1319                 switch (control & 0x0f) {
1320                 case RR:
1321                         irlap_recv_rr_frame(self, skb, &info, command);
1322                         break;
1323                 case RNR:
1324                         irlap_recv_rnr_frame(self, skb, &info, command);
1325                         break;
1326                 case REJ:
1327                         irlap_recv_rej_frame(self, skb, &info, command);
1328                         break;
1329                 case SREJ:
1330                         irlap_recv_srej_frame(self, skb, &info, command);
1331                         break;
1332                 default:
1333                         WARNING(__FUNCTION__ 
1334                                 "() Unknown S-frame %02x received!\n",
1335                                 info.control);
1336                         break;
1337                 }
1338                 goto out;
1339         }
1340         /* 
1341          *  This must be a C(ontrol) frame 
1342          */
1343         switch (control) {
1344         case XID_RSP:
1345                 irlap_recv_discovery_xid_rsp(self, skb, &info);
1346                 break;
1347         case XID_CMD:
1348                 irlap_recv_discovery_xid_cmd(self, skb, &info);
1349                 break;
1350         case SNRM_CMD:
1351                 irlap_recv_snrm_cmd(self, skb, &info);
1352                 break;
1353         case DM_RSP:
1354                 irlap_do_event(self, RECV_DM_RSP, skb, &info);
1355                 break;
1356         case DISC_CMD: /* And RD_RSP since they have the same value */
1357                 irlap_recv_disc_frame(self, skb, &info, command);
1358                 break;
1359         case TEST_CMD:
1360                 irlap_recv_test_frame(self, skb, &info, command);
1361                 break;
1362         case UA_RSP:
1363                 irlap_recv_ua_frame(self, skb, &info);
1364                 break;
1365         case FRMR_RSP:
1366                 irlap_recv_frmr_frame(self, skb, &info);
1367                 break;
1368         case UI_FRAME:
1369                 irlap_recv_ui_frame(self, skb, &info);
1370                 break;
1371         default:
1372                 WARNING(__FUNCTION__ "(), Unknown frame %02x received!\n", 
1373                         info.control);
1374                 break;
1375         }
1376 out:
1377         dev_kfree_skb(skb); 
1378         return 0;
1379 }