~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/net/irda/irlap.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*********************************************************************
  2  *                
  3  * Filename:      irlap.c
  4  * Version:       1.0
  5  * Description:   IrLAP implementation for Linux
  6  * Status:        Stable
  7  * Author:        Dag Brattli <dagb@cs.uit.no>
  8  * Created at:    Mon Aug  4 20:40:53 1997
  9  * Modified at:   Tue Dec 14 09:26:44 1999
 10  * Modified by:   Dag Brattli <dagb@cs.uit.no>
 11  * 
 12  *     Copyright (c) 1998-1999 Dag Brattli, All Rights Reserved.
 13  *     
 14  *     This program is free software; you can redistribute it and/or 
 15  *     modify it under the terms of the GNU General Public License as 
 16  *     published by the Free Software Foundation; either version 2 of 
 17  *     the License, or (at your option) any later version.
 18  * 
 19  *     This program is distributed in the hope that it will be useful,
 20  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 21  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 22  *     GNU General Public License for more details.
 23  * 
 24  *     You should have received a copy of the GNU General Public License 
 25  *     along with this program; if not, write to the Free Software 
 26  *     Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
 27  *     MA 02111-1307 USA
 28  *     
 29  ********************************************************************/
 30 
 31 #include <linux/config.h>
 32 #include <linux/malloc.h>
 33 #include <linux/string.h>
 34 #include <linux/skbuff.h>
 35 #include <linux/delay.h>
 36 #include <linux/proc_fs.h>
 37 #include <linux/init.h>
 38 #include <linux/random.h>
 39 
 40 #include <net/irda/irda.h>
 41 #include <net/irda/irda_device.h>
 42 #include <net/irda/irqueue.h>
 43 #include <net/irda/irlmp.h>
 44 #include <net/irda/irlmp_frame.h>
 45 #include <net/irda/irlap_frame.h>
 46 #include <net/irda/irlap.h>
 47 #include <net/irda/timer.h>
 48 #include <net/irda/qos.h>
 49 #include <net/irda/irlap_comp.h>
 50 
 51 hashbin_t *irlap = NULL;
 52 int sysctl_slot_timeout = SLOT_TIMEOUT * 1000 / HZ;
 53 
 54 static void __irlap_close(struct irlap_cb *self);
 55 
 56 static char *lap_reasons[] = {
 57         "ERROR, NOT USED",
 58         "LAP_DISC_INDICATION",
 59         "LAP_NO_RESPONSE",
 60         "LAP_RESET_INDICATION",
 61         "LAP_FOUND_NONE",
 62         "LAP_MEDIA_BUSY",
 63         "LAP_PRIMARY_CONFLICT",
 64         "ERROR, NOT USED",
 65 };
 66 
 67 #ifdef CONFIG_PROC_FS
 68 int irlap_proc_read(char *, char **, off_t, int);
 69 
 70 #endif /* CONFIG_PROC_FS */
 71 
 72 int __init irlap_init(void)
 73 {
 74         /* Allocate master array */
 75         irlap = hashbin_new(HB_LOCAL);
 76         if (irlap == NULL) {
 77                 ERROR(__FUNCTION__ "(), can't allocate irlap hashbin!\n");
 78                 return -ENOMEM;
 79         }
 80 
 81 #ifdef CONFIG_IRDA_COMPRESSION
 82         irlap_compressors = hashbin_new(HB_LOCAL);
 83         if (irlap_compressors == NULL) {
 84                 WARNING(__FUNCTION__ 
 85                         "(), can't allocate compressors hashbin!\n");
 86                 return -ENOMEM;
 87         }
 88 #endif
 89 
 90         return 0;
 91 }
 92 
 93 void irlap_cleanup(void)
 94 {
 95         ASSERT(irlap != NULL, return;);
 96 
 97         hashbin_delete(irlap, (FREE_FUNC) __irlap_close);
 98 
 99 #ifdef CONFIG_IRDA_COMPRESSION
100         hashbin_delete(irlap_compressors, (FREE_FUNC) kfree);
101 #endif
102 }
103 
104 /*
105  * Function irlap_open (driver)
106  *
107  *    Initialize IrLAP layer
108  *
109  */
110 struct irlap_cb *irlap_open(struct net_device *dev, struct qos_info *qos)
111 {
112         struct irlap_cb *self;
113 
114         IRDA_DEBUG(4, __FUNCTION__ "()\n");
115         
116         /* Initialize the irlap structure. */
117         self = kmalloc(sizeof(struct irlap_cb), GFP_KERNEL);
118         if (self == NULL)
119                 return NULL;
120         
121         memset(self, 0, sizeof(struct irlap_cb));
122         self->magic = LAP_MAGIC;
123 
124         /* Make a binding between the layers */
125         self->netdev = dev;
126         self->qos_dev = qos;
127 
128         /* FIXME: should we get our own field? */
129         dev->atalk_ptr = self;
130 
131         irlap_next_state(self, LAP_OFFLINE);
132 
133         /* Initialize transmit queue */
134         skb_queue_head_init(&self->txq);
135         skb_queue_head_init(&self->txq_ultra);
136         skb_queue_head_init(&self->wx_list);
137 
138         /* My unique IrLAP device address! */
139         get_random_bytes(&self->saddr, sizeof(self->saddr));
140         memcpy(dev->dev_addr, &self->saddr, 4);
141 
142         init_timer(&self->slot_timer);
143         init_timer(&self->query_timer);
144         init_timer(&self->discovery_timer);
145         init_timer(&self->final_timer);         
146         init_timer(&self->poll_timer);
147         init_timer(&self->wd_timer);
148         init_timer(&self->backoff_timer);
149         init_timer(&self->media_busy_timer);    
150 
151         irlap_apply_default_connection_parameters(self);
152 
153         self->N3 = 3; /* # connections attemts to try before giving up */
154         
155         irlap_next_state(self, LAP_NDM);
156 
157         hashbin_insert(irlap, (irda_queue_t *) self, self->saddr, NULL);
158 
159         irlmp_register_link(self, self->saddr, &self->notify);
160         
161         return self;
162 }
163 
164 /*
165  * Function __irlap_close (self)
166  *
167  *    Remove IrLAP and all allocated memory. Stop any pending timers.
168  *
169  */
170 static void __irlap_close(struct irlap_cb *self)
171 {
172         ASSERT(self != NULL, return;);
173         ASSERT(self->magic == LAP_MAGIC, return;);
174 
175         /* Stop timers */
176         del_timer(&self->slot_timer);
177         del_timer(&self->query_timer);
178         del_timer(&self->discovery_timer);
179         del_timer(&self->final_timer);          
180         del_timer(&self->poll_timer);
181         del_timer(&self->wd_timer);
182         del_timer(&self->backoff_timer);
183         del_timer(&self->media_busy_timer);
184 
185         irlap_flush_all_queues(self);
186        
187         self->magic = 0;
188         
189         kfree(self);
190 }
191 
192 /*
193  * Function irlap_close (self)
194  *
195  *    Remove IrLAP instance
196  *
197  */
198 void irlap_close(struct irlap_cb *self) 
199 {
200         struct irlap_cb *lap;
201 
202         IRDA_DEBUG(4, __FUNCTION__ "()\n");
203         
204         ASSERT(self != NULL, return;);
205         ASSERT(self->magic == LAP_MAGIC, return;);
206 
207         irlap_disconnect_indication(self, LAP_DISC_INDICATION);
208 
209         irlmp_unregister_link(self->saddr);
210         self->notify.instance = NULL;
211 
212         /* Be sure that we manage to remove ourself from the hash */
213         lap = hashbin_remove(irlap, self->saddr, NULL);
214         if (!lap) {
215                 IRDA_DEBUG(1, __FUNCTION__ "(), Didn't find myself!\n");
216                 return;
217         }
218         __irlap_close(lap);
219 }
220 
221 /*
222  * Function irlap_connect_indication (self, skb)
223  *
224  *    Another device is attempting to make a connection
225  *
226  */
227 void irlap_connect_indication(struct irlap_cb *self, struct sk_buff *skb) 
228 {
229         IRDA_DEBUG(4, __FUNCTION__ "()\n");
230 
231         ASSERT(self != NULL, return;);
232         ASSERT(self->magic == LAP_MAGIC, return;);
233 
234         irlap_init_qos_capabilities(self, NULL); /* No user QoS! */
235 
236         skb_get(skb); /*LEVEL4*/
237         irlmp_link_connect_indication(self->notify.instance, self->saddr, 
238                                       self->daddr, &self->qos_tx, skb);
239 }
240 
241 /*
242  * Function irlap_connect_response (self, skb)
243  *
244  *    Service user has accepted incomming connection
245  *
246  */
247 void irlap_connect_response(struct irlap_cb *self, struct sk_buff *skb) 
248 {
249         IRDA_DEBUG(4, __FUNCTION__ "()\n");
250         
251         irlap_do_event(self, CONNECT_RESPONSE, skb, NULL);
252         kfree_skb(skb);
253 }
254 
255 /*
256  * Function irlap_connect_request (self, daddr, qos_user, sniff)
257  *
258  *    Request connection with another device, sniffing is not implemented 
259  *    yet.
260  *
261  */
262 void irlap_connect_request(struct irlap_cb *self, __u32 daddr, 
263                            struct qos_info *qos_user, int sniff) 
264 {
265         IRDA_DEBUG(3, __FUNCTION__ "(), daddr=0x%08x\n", daddr);
266 
267         ASSERT(self != NULL, return;);
268         ASSERT(self->magic == LAP_MAGIC, return;);
269 
270         self->daddr = daddr;
271         
272         /*
273          *  If the service user specifies QoS values for this connection, 
274          *  then use them
275          */
276         irlap_init_qos_capabilities(self, qos_user);
277         
278         if ((self->state == LAP_NDM) && !self->media_busy)
279                 irlap_do_event(self, CONNECT_REQUEST, NULL, NULL);
280         else
281                 self->connect_pending = TRUE;
282 }
283 
284 /*
285  * Function irlap_connect_confirm (self, skb)
286  *
287  *    Connection request has been accepted
288  *
289  */
290 void irlap_connect_confirm(struct irlap_cb *self, struct sk_buff *skb)
291 {
292         IRDA_DEBUG(4, __FUNCTION__ "()\n");
293 
294         ASSERT(self != NULL, return;);
295         ASSERT(self->magic == LAP_MAGIC, return;);
296 
297         skb_get(skb); /*LEVEL4*/
298         irlmp_link_connect_confirm(self->notify.instance, &self->qos_tx, skb);
299 }
300 
301 /*
302  * Function irlap_data_indication (self, skb)
303  *
304  *    Received data frames from IR-port, so we just pass them up to 
305  *    IrLMP for further processing
306  *
307  */
308 void irlap_data_indication(struct irlap_cb *self, struct sk_buff *skb,
309                            int unreliable) 
310 {
311         /* Hide LAP header from IrLMP layer */
312         skb_pull(skb, LAP_ADDR_HEADER+LAP_CTRL_HEADER);
313 
314 #ifdef CONFIG_IRDA_COMPRESSION
315         if (self->qos_tx.compression.value) {
316                 skb_get(skb); /*LEVEL4*/
317                 skb = irlap_decompress_frame(self, skb);
318                 if (!skb) {
319                         IRDA_DEBUG(1, __FUNCTION__ "(), Decompress error!\n");
320                         return;
321                 }
322         }
323 #endif
324         skb_get(skb); /*LEVEL4*/
325         irlmp_link_data_indication(self->notify.instance, skb, unreliable);
326 }
327 
328 
329 /*
330  * Function irlap_data_request (self, skb)
331  *
332  *    Queue data for transmission, must wait until XMIT state
333  *
334  */
335 void irlap_data_request(struct irlap_cb *self, struct sk_buff *skb, 
336                         int unreliable)
337 {
338         ASSERT(self != NULL, return;);
339         ASSERT(self->magic == LAP_MAGIC, return;);
340 
341         IRDA_DEBUG(3, __FUNCTION__ "()\n");
342 
343 #ifdef CONFIG_IRDA_COMPRESSION
344         if (self->qos_tx.compression.value) {
345                 skb = irlap_compress_frame(self, skb);
346                 if (!skb) {
347                         IRDA_DEBUG(1, __FUNCTION__ "(), Compress error!\n");
348                         return;
349                 }
350         }
351 #endif
352         ASSERT(skb_headroom(skb) >= (LAP_ADDR_HEADER+LAP_CTRL_HEADER), 
353                return;);
354         skb_push(skb, LAP_ADDR_HEADER+LAP_CTRL_HEADER);
355 
356         /*  
357          *  Must set frame format now so that the rest of the code knows 
358          *  if its dealing with an I or an UI frame
359          */
360         if (unreliable)
361                 skb->data[1] = UI_FRAME;
362         else
363                 skb->data[1] = I_FRAME;
364 
365         /* 
366          *  Send event if this frame only if we are in the right state 
367          *  FIXME: udata should be sent first! (skb_queue_head?)
368          */
369         if ((self->state == LAP_XMIT_P) || (self->state == LAP_XMIT_S)) {
370                 /*
371                  *  Check if the transmit queue contains some unsent frames,
372                  *  and if so, make sure they are sent first
373                  */
374                 if (!skb_queue_empty(&self->txq)) {
375                         skb_queue_tail(&self->txq, skb);
376                         skb = skb_dequeue(&self->txq);
377                         
378                         ASSERT(skb != NULL, return;);
379                 }
380                 irlap_do_event(self, SEND_I_CMD, skb, NULL);
381                 kfree_skb(skb);
382         } else
383                 skb_queue_tail(&self->txq, skb);
384 }
385 
386 /*
387  * Function irlap_unitdata_request (self, skb)
388  *
389  *    Send Ultra data. This is data that must be sent outside any connection
390  *
391  */
392 #ifdef CONFIG_IRDA_ULTRA
393 void irlap_unitdata_request(struct irlap_cb *self, struct sk_buff *skb)
394 {
395         ASSERT(self != NULL, return;);
396         ASSERT(self->magic == LAP_MAGIC, return;);
397 
398         IRDA_DEBUG(3, __FUNCTION__ "()\n");
399 
400         ASSERT(skb_headroom(skb) >= (LAP_ADDR_HEADER+LAP_CTRL_HEADER), 
401                return;);
402         skb_push(skb, LAP_ADDR_HEADER+LAP_CTRL_HEADER);
403 
404         skb->data[0] = CBROADCAST;
405         skb->data[1] = UI_FRAME;
406 
407         skb_queue_tail(&self->txq_ultra, skb);
408 
409         irlap_do_event(self, SEND_UI_FRAME, NULL, NULL);
410 }
411 #endif /*CONFIG_IRDA_ULTRA */
412 
413 /*
414  * Function irlap_udata_indication (self, skb)
415  *
416  *    Receive Ultra data. This is data that is received outside any connection
417  *
418  */
419 #ifdef CONFIG_IRDA_ULTRA
420 void irlap_unitdata_indication(struct irlap_cb *self, struct sk_buff *skb)
421 {
422         IRDA_DEBUG(1, __FUNCTION__ "()\n"); 
423 
424         ASSERT(self != NULL, return;);
425         ASSERT(self->magic == LAP_MAGIC, return;);
426         ASSERT(skb != NULL, return;);
427 
428         /* Hide LAP header from IrLMP layer */
429         skb_pull(skb, LAP_ADDR_HEADER+LAP_CTRL_HEADER);
430 
431         skb_get(skb); /*LEVEL4*/
432         irlmp_link_unitdata_indication(self->notify.instance, skb);
433 }
434 #endif /* CONFIG_IRDA_ULTRA */
435 
436 /*
437  * Function irlap_disconnect_request (void)
438  *
439  *    Request to disconnect connection by service user
440  */
441 void irlap_disconnect_request(struct irlap_cb *self) 
442 {
443         IRDA_DEBUG(3, __FUNCTION__ "()\n");
444 
445         ASSERT(self != NULL, return;);
446         ASSERT(self->magic == LAP_MAGIC, return;);
447         
448         /* Don't disconnect until all data frames are successfully sent */
449         if (skb_queue_len(&self->txq) > 0) {
450                 self->disconnect_pending = TRUE;
451                 
452                 return;
453         }
454 
455         /* Check if we are in the right state for disconnecting */
456         switch (self->state) {
457         case LAP_XMIT_P:        /* FALLTROUGH */
458         case LAP_XMIT_S:        /* FALLTROUGH */
459         case LAP_CONN:          /* FALLTROUGH */
460         case LAP_RESET_WAIT:    /* FALLTROUGH */
461         case LAP_RESET_CHECK:   
462                 irlap_do_event(self, DISCONNECT_REQUEST, NULL, NULL);
463                 break;
464         default:
465                 IRDA_DEBUG(2, __FUNCTION__ "(), disconnect pending!\n");
466                 self->disconnect_pending = TRUE;
467                 break;
468         }
469 }
470 
471 /*
472  * Function irlap_disconnect_indication (void)
473  *
474  *    Disconnect request from other device
475  *
476  */
477 void irlap_disconnect_indication(struct irlap_cb *self, LAP_REASON reason) 
478 {
479         IRDA_DEBUG(1, __FUNCTION__ "(), reason=%s\n", lap_reasons[reason]); 
480 
481         ASSERT(self != NULL, return;);
482         ASSERT(self->magic == LAP_MAGIC, return;);
483 
484 #ifdef CONFIG_IRDA_COMPRESSION
485         irda_free_compression(self);
486 #endif
487         /* Flush queues */
488         irlap_flush_all_queues(self);
489         
490         switch (reason) {
491         case LAP_RESET_INDICATION:
492                 IRDA_DEBUG(1, __FUNCTION__ "(), Sending reset request!\n");
493                 irlap_do_event(self, RESET_REQUEST, NULL, NULL);
494                 break;
495         case LAP_NO_RESPONSE:      /* FALLTROUGH */     
496         case LAP_DISC_INDICATION:  /* FALLTROUGH */
497         case LAP_FOUND_NONE:       /* FALLTROUGH */
498         case LAP_MEDIA_BUSY:
499                 irlmp_link_disconnect_indication(self->notify.instance, self, 
500                                                  reason, NULL);
501                 break;
502         default:
503                 ERROR(__FUNCTION__ "(), Unknown reason %d\n", reason);
504         }
505 }
506 
507 /*
508  * Function irlap_discovery_request (gen_addr_bit)
509  *
510  *    Start one single discovery operation.
511  *
512  */
513 void irlap_discovery_request(struct irlap_cb *self, discovery_t *discovery) 
514 {
515         struct irlap_info info;
516         
517         ASSERT(self != NULL, return;);
518         ASSERT(self->magic == LAP_MAGIC, return;);
519         ASSERT(discovery != NULL, return;);
520         
521         IRDA_DEBUG(4, __FUNCTION__ "(), nslots = %d\n", discovery->nslots);
522 
523         ASSERT((discovery->nslots == 1) || (discovery->nslots == 6) ||
524                (discovery->nslots == 8) || (discovery->nslots == 16), 
525                return;);
526         
527         /* Discovery is only possible in NDM mode */
528         if (self->state != LAP_NDM) {
529                 IRDA_DEBUG(4, __FUNCTION__ 
530                            "(), discovery only possible in NDM mode\n");
531                 irlap_discovery_confirm(self, NULL);
532                 return;
533         }
534 
535         /* Check if last discovery request finished in time */
536         if (self->discovery_log != NULL) {
537                 hashbin_delete(self->discovery_log, (FREE_FUNC) kfree);
538                 self->discovery_log = NULL;
539         }
540         
541         self->discovery_log= hashbin_new(HB_LOCAL);
542         
543         info.S = discovery->nslots; /* Number of slots */
544         info.s = 0; /* Current slot */
545         
546         self->discovery_cmd = discovery;
547         info.discovery = discovery;
548         
549         /* Check if the slot timeout is within limits */
550         if (sysctl_slot_timeout < 20) {
551                 ERROR(__FUNCTION__ 
552                       "(), to low value for slot timeout!\n");
553                 sysctl_slot_timeout = 20;
554         }
555         /* 
556          * Highest value is actually 8, but we allow higher since
557          * some devices seems to require it.
558          */
559         if (sysctl_slot_timeout > 160) {
560                 ERROR(__FUNCTION__ 
561                       "(), to high value for slot timeout!\n");
562                 sysctl_slot_timeout = 160;
563         }
564         
565         self->slot_timeout = sysctl_slot_timeout * HZ / 1000;
566         
567         irlap_do_event(self, DISCOVERY_REQUEST, NULL, &info);
568 }
569 
570 /*
571  * Function irlap_discovery_confirm (log)
572  *
573  *    A device has been discovered in front of this station, we
574  *    report directly to LMP.
575  */
576 void irlap_discovery_confirm(struct irlap_cb *self, hashbin_t *discovery_log) 
577 {
578         ASSERT(self != NULL, return;);
579         ASSERT(self->magic == LAP_MAGIC, return;);
580         
581         ASSERT(self->notify.instance != NULL, return;);
582         
583         /* 
584          * Check for successful discovery, since we are then allowed to clear 
585          * the media busy condition (irlap p.94). This should allow us to make 
586          * connection attempts much easier.
587          */
588         if (discovery_log && HASHBIN_GET_SIZE(discovery_log) > 0)
589                 irda_device_set_media_busy(self->netdev, FALSE);
590         
591         /* Inform IrLMP */
592         irlmp_link_discovery_confirm(self->notify.instance, discovery_log);
593 }
594 
595 /*
596  * Function irlap_discovery_indication (log)
597  *
598  *    Somebody is trying to discover us!
599  *
600  */
601 void irlap_discovery_indication(struct irlap_cb *self, discovery_t *discovery) 
602 {
603         IRDA_DEBUG(4, __FUNCTION__ "()\n");
604 
605         ASSERT(self != NULL, return;);
606         ASSERT(self->magic == LAP_MAGIC, return;);
607         ASSERT(discovery != NULL, return;);
608 
609         ASSERT(self->notify.instance != NULL, return;);
610         
611         irlmp_link_discovery_indication(self->notify.instance, discovery);
612 }
613 
614 /*
615  * Function irlap_status_indication (quality_of_link)
616  *
617  *    
618  *
619  */
620 void irlap_status_indication(struct irlap_cb *self, int quality_of_link) 
621 {
622         switch (quality_of_link) {
623         case STATUS_NO_ACTIVITY:
624                 MESSAGE("IrLAP, no activity on link!\n");
625                 break;
626         case STATUS_NOISY:
627                 MESSAGE("IrLAP, noisy link!\n");
628                 break;
629         default:
630                 break;
631         }
632         irlmp_status_indication(self->notify.instance,
633                                 quality_of_link, LOCK_NO_CHANGE);
634 }
635 
636 /*
637  * Function irlap_reset_indication (void)
638  *
639  *    
640  *
641  */
642 void irlap_reset_indication(struct irlap_cb *self)
643 {
644         IRDA_DEBUG(1, __FUNCTION__ "()\n");
645 
646         ASSERT(self != NULL, return;);
647         ASSERT(self->magic == LAP_MAGIC, return;);
648         
649         if (self->state == LAP_RESET_WAIT)
650                 irlap_do_event(self, RESET_REQUEST, NULL, NULL);
651         else
652                 irlap_do_event(self, RESET_RESPONSE, NULL, NULL);
653 }
654 
655 /*
656  * Function irlap_reset_confirm (void)
657  *
658  *    
659  *
660  */
661 void irlap_reset_confirm(void)
662 {
663         IRDA_DEBUG(1, __FUNCTION__ "()\n");
664 }
665 
666 /*
667  * Function irlap_generate_rand_time_slot (S, s)
668  *
669  *    Generate a random time slot between s and S-1 where
670  *    S = Number of slots (0 -> S-1)
671  *    s = Current slot
672  */
673 int irlap_generate_rand_time_slot(int S, int s) 
674 {
675         static int rand;
676         int slot;
677         
678         ASSERT((S - s) > 0, return 0;);
679 
680         rand += jiffies;
681         rand ^= (rand << 12);
682         rand ^= (rand >> 20);
683 
684         slot = s + rand % (S-s);
685         
686         ASSERT((slot >= s) || (slot < S), return 0;);
687         
688         return slot;
689 }
690 
691 /*
692  * Function irlap_update_nr_received (nr)
693  *
694  *    Remove all acknowledged frames in current window queue. This code is 
695  *    not intuitive and you should not try to change it. If you think it
696  *    contains bugs, please mail a patch to the author instead.
697  */
698 void irlap_update_nr_received(struct irlap_cb *self, int nr) 
699 {
700         struct sk_buff *skb = NULL;
701         int count = 0;
702 
703         /*
704          * Remove all the ack-ed frames from the window queue.
705          */
706 
707         /* 
708          *  Optimize for the common case. It is most likely that the receiver
709          *  will acknowledge all the frames we have sent! So in that case we
710          *  delete all frames stored in window.
711          */
712         if (nr == self->vs) {
713                 while ((skb = skb_dequeue(&self->wx_list)) != NULL) {
714                         dev_kfree_skb(skb);
715                 }
716                 /* The last acked frame is the next to send minus one */
717                 self->va = nr - 1;
718         } else {
719                 /* Remove all acknowledged frames in current window */
720                 while ((skb_peek(&self->wx_list) != NULL) && 
721                        (((self->va+1) % 8) != nr)) 
722                 {
723                         skb = skb_dequeue(&self->wx_list);
724                         dev_kfree_skb(skb);
725                         
726                         self->va = (self->va + 1) % 8;
727                         count++;
728                 }
729         }
730         
731         /* Advance window */
732         self->window = self->window_size - skb_queue_len(&self->wx_list);
733 }
734 
735 /*
736  * Function irlap_validate_ns_received (ns)
737  *
738  *    Validate the next to send (ns) field from received frame.
739  */
740 int irlap_validate_ns_received(struct irlap_cb *self, int ns) 
741 {
742         /*  ns as expected?  */
743         if (ns == self->vr)
744                 return NS_EXPECTED;
745         /*
746          *  Stations are allowed to treat invalid NS as unexpected NS
747          *  IrLAP, Recv ... with-invalid-Ns. p. 84
748          */
749         return NS_UNEXPECTED;
750         
751         /* return NR_INVALID; */
752 }
753 /*
754  * Function irlap_validate_nr_received (nr)
755  *
756  *    Validate the next to receive (nr) field from received frame.
757  *
758  */
759 int irlap_validate_nr_received(struct irlap_cb *self, int nr) 
760 {
761         /*  nr as expected?  */
762         if (nr == self->vs) {
763                 IRDA_DEBUG(4, __FUNCTION__ "(), expected!\n");
764                 return NR_EXPECTED;
765         }
766 
767         /*
768          *  unexpected nr? (but within current window), first we check if the 
769          *  ns numbers of the frames in the current window wrap.
770          */
771         if (self->va < self->vs) {
772                 if ((nr >= self->va) && (nr <= self->vs))
773                         return NR_UNEXPECTED;
774         } else {
775                 if ((nr >= self->va) || (nr <= self->vs)) 
776                         return NR_UNEXPECTED;
777         }
778         
779         /* Invalid nr!  */
780         return NR_INVALID;
781 }
782 
783 /*
784  * Function irlap_initiate_connection_state ()
785  *
786  *    Initialize the connection state parameters
787  *
788  */
789 void irlap_initiate_connection_state(struct irlap_cb *self) 
790 {
791         IRDA_DEBUG(4, __FUNCTION__ "()\n");
792         
793         ASSERT(self != NULL, return;);
794         ASSERT(self->magic == LAP_MAGIC, return;);
795 
796         /* Next to send and next to receive */
797         self->vs = self->vr = 0;
798 
799         /* Last frame which got acked (0 - 1) % 8 */
800         self->va = 7;
801 
802         self->window = 1;
803 
804         self->remote_busy = FALSE;
805         self->retry_count = 0;
806 }
807 
808 /*
809  * Function irlap_wait_min_turn_around (self, qos)
810  *
811  *    Wait negotiated minimum turn around time, this function actually sets
812  *    the number of BOS's that must be sent before the next transmitted
813  *    frame in order to delay for the specified amount of time. This is
814  *    done to avoid using timers, and the forbidden udelay!
815  */
816 void irlap_wait_min_turn_around(struct irlap_cb *self, struct qos_info *qos) 
817 {
818         __u32 min_turn_time;
819         __u32 speed;
820         
821         /* Get QoS values.  */
822         speed = qos->baud_rate.value;
823         min_turn_time = qos->min_turn_time.value;
824 
825         /* No need to calculate XBOFs for speeds over 115200 bps */
826         if (speed > 115200) {
827                 self->mtt_required = min_turn_time;
828                 return;
829         }
830         
831         /*  
832          *  Send additional BOF's for the next frame for the requested
833          *  min turn time, so now we must calculate how many chars (XBOF's) we 
834          *  must send for the requested time period (min turn time)
835          */
836         self->xbofs_delay = irlap_min_turn_time_in_bytes(speed, min_turn_time);
837 }
838 
839 /*
840  * Function irlap_flush_all_queues (void)
841  *
842  *    Flush all queues
843  *
844  */
845 void irlap_flush_all_queues(struct irlap_cb *self) 
846 {
847         struct sk_buff* skb;
848 
849         ASSERT(self != NULL, return;);
850         ASSERT(self->magic == LAP_MAGIC, return;);
851 
852         /* Free transmission queue */
853         while ((skb = skb_dequeue(&self->txq)) != NULL)
854                 dev_kfree_skb(skb);
855         
856         while ((skb = skb_dequeue(&self->txq_ultra)) != NULL)
857                 dev_kfree_skb(skb);
858 
859         /* Free sliding window buffered packets */
860         while ((skb = skb_dequeue(&self->wx_list)) != NULL)
861                 dev_kfree_skb(skb);
862 
863 #ifdef CONFIG_IRDA_RECYCLE_RR
864         if (self->recycle_rr_skb) { 
865                 dev_kfree_skb(self->recycle_rr_skb);
866                 self->recycle_rr_skb = NULL;
867         }
868 #endif
869 }
870 
871 /*
872  * Function irlap_setspeed (self, speed)
873  *
874  *    Change the speed of the IrDA port
875  *
876  */
877 void irlap_change_speed(struct irlap_cb *self, __u32 speed, int now)
878 {
879         struct sk_buff *skb;
880 
881         IRDA_DEBUG(0, __FUNCTION__ "(), setting speed to %d\n", speed);
882 
883         ASSERT(self != NULL, return;);
884         ASSERT(self->magic == LAP_MAGIC, return;);
885 
886         self->speed = speed;
887 
888         /* Change speed now, or just piggyback speed on frames */
889         if (now) {
890                 /* Send down empty frame to trigger speed change */
891                 skb = dev_alloc_skb(0);
892                 irlap_queue_xmit(self, skb);
893         }
894 }
895 
896 #ifdef CONFIG_IRDA_COMPRESSION
897 void irlap_init_comp_qos_capabilities(struct irlap_cb *self)
898 {
899         struct irda_compressor *comp;
900         __u8 mask; /* Current bit tested */
901         int i;
902 
903         ASSERT(self != NULL, return;);
904         ASSERT(self->magic == LAP_MAGIC, return;);
905         
906         /* 
907          *  Find out which compressors we support. We do this be checking that
908          *  the corresponding compressor for each bit set in the QoS bits has 
909          *  actually been loaded. Ths is sort of hairy code but that is what 
910          *  you get when you do a little bit flicking :-)
911          */
912         IRDA_DEBUG(4, __FUNCTION__ "(), comp bits 0x%02x\n", 
913                    self->qos_rx.compression.bits); 
914         mask = 0x80; /* Start with testing MSB */
915         for (i=0;i<8;i++) {
916                 IRDA_DEBUG(4, __FUNCTION__ "(), testing bit %d\n", 8-i);
917                 if (self->qos_rx.compression.bits & mask) {
918                         IRDA_DEBUG(4, __FUNCTION__ 
919                                    "(), bit %d is set by defalt\n", 8-i);
920                         comp = hashbin_find(irlap_compressors, 
921                                             compressions[msb_index(mask)], 
922                                             NULL);
923                         if (!comp) {
924                                 /* Protocol not supported, so clear the bit */
925                                 IRDA_DEBUG(4, __FUNCTION__ "(), Compression "
926                                            "protocol %d has not been loaded!\n", 
927                                            compressions[msb_index(mask)]);
928                                 self->qos_rx.compression.bits &= ~mask;
929                                 IRDA_DEBUG(4, __FUNCTION__ 
930                                            "(), comp bits 0x%02x\n", 
931                                            self->qos_rx.compression.bits); 
932                         }
933                 }
934                 /* Try the next bit */
935                 mask >>= 1;
936         }
937 }
938 #endif  
939 
940 /*
941  * Function irlap_init_qos_capabilities (self, qos)
942  *
943  *    Initialize QoS for this IrLAP session, What we do is to compute the
944  *    intersection of the QoS capabilities for the user, driver and for
945  *    IrLAP itself. Normally, IrLAP will not specify any values, but it can
946  *    be used to restrict certain values.
947  */
948 void irlap_init_qos_capabilities(struct irlap_cb *self,
949                                  struct qos_info *qos_user)
950 {
951         ASSERT(self != NULL, return;);
952         ASSERT(self->magic == LAP_MAGIC, return;);
953         ASSERT(self->netdev != NULL, return;);
954 
955         /* Start out with the maximum QoS support possible */
956         irda_init_max_qos_capabilies(&self->qos_rx);
957 
958 #ifdef CONFIG_IRDA_COMPRESSION
959         irlap_init_comp_qos_capabilities(self);
960 #endif
961 
962         /* Apply drivers QoS capabilities */
963         irda_qos_compute_intersection(&self->qos_rx, self->qos_dev);
964 
965         /*
966          *  Check for user supplied QoS parameters. The service user is only 
967          *  allowed to supply these values. We check each parameter since the
968          *  user may not have set all of them.
969          */
970         if (qos_user) {
971                 IRDA_DEBUG(1, __FUNCTION__ "(), Found user specified QoS!\n");
972 
973                 if (qos_user->baud_rate.bits)
974                         self->qos_rx.baud_rate.bits &= qos_user->baud_rate.bits;
975 
976                 if (qos_user->max_turn_time.bits)
977                         self->qos_rx.max_turn_time.bits &= qos_user->max_turn_time.bits;
978                 if (qos_user->data_size.bits)
979                         self->qos_rx.data_size.bits &= qos_user->data_size.bits;
980 
981                 if (qos_user->link_disc_time.bits)
982                         self->qos_rx.link_disc_time.bits &= qos_user->link_disc_time.bits;
983 #ifdef CONFIG_IRDA_COMPRESSION
984                 self->qos_rx.compression.bits &= qos_user->compression.bits;
985 #endif
986         }
987 
988         /* Use 500ms in IrLAP for now */
989         self->qos_rx.max_turn_time.bits &= 0x01;
990 
991         /* Set data size */
992         /*self->qos_rx.data_size.bits &= 0x03;*/
993 
994         /* Set disconnect time -> done properly in qos.c */
995         /*self->qos_rx.link_disc_time.bits &= 0x07;*/
996 
997         irda_qos_bits_to_value(&self->qos_rx);
998 }
999 
1000 /*
1001  * Function irlap_apply_default_connection_parameters (void)
1002  *
1003  *    Use the default connection and transmission parameters
1004  * 
1005  */
1006 void irlap_apply_default_connection_parameters(struct irlap_cb *self)
1007 {
1008         IRDA_DEBUG(4, __FUNCTION__ "()\n");
1009 
1010         ASSERT(self != NULL, return;);
1011         ASSERT(self->magic == LAP_MAGIC, return;);
1012 
1013         irlap_change_speed(self, 9600, TRUE);
1014 
1015         /* Set mbusy when going to NDM state */
1016         irda_device_set_media_busy(self->netdev, TRUE);
1017 
1018         /* Default value in NDM */
1019         self->bofs_count = 12;
1020 
1021         /* 
1022          * Generate random connection address for this session, which must
1023          * be 7 bits wide and different from 0x00 and 0xfe 
1024          */
1025         while ((self->caddr == 0x00) || (self->caddr == 0xfe)) {
1026                 get_random_bytes(&self->caddr, sizeof(self->caddr));
1027                 self->caddr &= 0xfe;
1028         }
1029 
1030         /* Use default values until connection has been negitiated */
1031         self->slot_timeout = sysctl_slot_timeout;
1032         self->final_timeout = FINAL_TIMEOUT;
1033         self->poll_timeout = POLL_TIMEOUT;
1034         self->wd_timeout = WD_TIMEOUT;
1035 
1036         /* Set some default values */
1037         self->qos_tx.baud_rate.value = 9600;
1038         self->qos_rx.baud_rate.value = 9600;
1039         self->qos_tx.max_turn_time.value = 0;
1040         self->qos_rx.max_turn_time.value = 0;
1041         self->qos_tx.min_turn_time.value = 0;
1042         self->qos_rx.min_turn_time.value = 0;
1043         self->qos_tx.data_size.value = 64;
1044         self->qos_rx.data_size.value = 64;
1045         self->qos_tx.window_size.value = 1;
1046         self->qos_rx.window_size.value = 1;
1047         self->qos_tx.additional_bofs.value = 12;
1048         self->qos_rx.additional_bofs.value = 12;
1049         self->qos_tx.link_disc_time.value = 0;
1050         self->qos_rx.link_disc_time.value = 0;
1051 
1052         irlap_flush_all_queues(self);
1053 
1054         self->disconnect_pending = FALSE;
1055         self->connect_pending = FALSE;
1056 }
1057 
1058 /*
1059  * Function irlap_apply_connection_parameters (qos)
1060  *
1061  *    Initialize IrLAP with the negotiated QoS values
1062  *
1063  */
1064 void irlap_apply_connection_parameters(struct irlap_cb *self) 
1065 {
1066         IRDA_DEBUG(4, __FUNCTION__ "()\n");
1067         
1068         ASSERT(self != NULL, return;);
1069         ASSERT(self->magic == LAP_MAGIC, return;);
1070 
1071         irlap_change_speed(self, self->qos_tx.baud_rate.value, FALSE);
1072 
1073         self->window_size = self->qos_tx.window_size.value;
1074         self->window      = self->qos_tx.window_size.value;
1075         self->bofs_count  = self->qos_tx.additional_bofs.value;
1076 
1077         /*
1078          *  Calculate how many bytes it is possible to transmit before the
1079          *  link must be turned around
1080          */
1081         self->line_capacity = 
1082                 irlap_max_line_capacity(self->qos_tx.baud_rate.value,
1083                                         self->qos_tx.max_turn_time.value);
1084         /*
1085          *  Set N1 to 0 if Link Disconnect/Threshold Time = 3 and set it to 
1086          *  3 seconds otherwise. See page 71 in IrLAP for more details.
1087          *  TODO: these values should be calculated from the final timer
1088          *  as well
1089          */
1090         ASSERT(self->qos_tx.max_turn_time.value != 0, return;);
1091         if (self->qos_tx.link_disc_time.value == 3)
1092                 /* 
1093                  * If we set N1 to 0, it will trigger immediately, which is
1094                  * not what we want. What we really want is to disable it,
1095                  * Jean II 
1096                  */
1097                 self->N1 = -1; /* Disable */
1098         else
1099                 self->N1 = 3000 / self->qos_tx.max_turn_time.value;
1100         
1101         IRDA_DEBUG(4, "Setting N1 = %d\n", self->N1);
1102         
1103         
1104         self->N2 = self->qos_tx.link_disc_time.value * 1000 / 
1105                 self->qos_tx.max_turn_time.value;
1106         IRDA_DEBUG(4, "Setting N2 = %d\n", self->N2);
1107 
1108         /* 
1109          *  Initialize timeout values, some of the rules are listed on 
1110          *  page 92 in IrLAP.
1111          */
1112         self->poll_timeout = self->qos_tx.max_turn_time.value * HZ / 1000;
1113         self->wd_timeout = self->poll_timeout * 2;
1114 
1115         /* 
1116          * Be careful to keep our promises to the peer device about how long
1117          * time it can keep the pf bit. So here we must use the rx_qos value
1118          */
1119         self->final_timeout = self->qos_rx.max_turn_time.value * HZ / 1000;
1120 
1121 #ifdef CONFIG_IRDA_COMPRESSION
1122         if (self->qos_tx.compression.value) {
1123                 IRDA_DEBUG(1, __FUNCTION__ "(), Initializing compression\n");
1124                 irda_set_compression(self, self->qos_tx.compression.value);
1125 
1126                 irlap_compressor_init(self, 0);
1127         }
1128 #endif
1129 }
1130 
1131 /*
1132  * Function irlap_set_local_busy (self, status)
1133  *
1134  *    
1135  *
1136  */
1137 void irlap_set_local_busy(struct irlap_cb *self, int status)
1138 {
1139         IRDA_DEBUG(0, __FUNCTION__ "()\n");
1140 
1141         self->local_busy = status;
1142         
1143         if (status)
1144                 IRDA_DEBUG(0, __FUNCTION__ "(), local busy ON\n");
1145         else
1146                 IRDA_DEBUG(0, __FUNCTION__ "(), local busy OFF\n");
1147 }
1148 
1149 #ifdef CONFIG_PROC_FS
1150 /*
1151  * Function irlap_proc_read (buf, start, offset, len, unused)
1152  *
1153  *    Give some info to the /proc file system
1154  *
1155  */
1156 int irlap_proc_read(char *buf, char **start, off_t offset, int len)
1157 {
1158         struct irlap_cb *self;
1159         unsigned long flags;
1160         int i = 0;
1161      
1162         save_flags(flags);
1163         cli();
1164 
1165         len = 0;
1166 
1167         self = (struct irlap_cb *) hashbin_get_first(irlap);
1168         while (self != NULL) {
1169                 ASSERT(self != NULL, return -ENODEV;);
1170                 ASSERT(self->magic == LAP_MAGIC, return -EBADR;);
1171 
1172                 len += sprintf(buf+len, "irlap%d ", i++);
1173                 len += sprintf(buf+len, "state: %s\n", 
1174                                irlap_state[self->state]);
1175                 
1176                 len += sprintf(buf+len, "  caddr: %#02x, ", self->caddr);
1177                 len += sprintf(buf+len, "saddr: %#08x, ", self->saddr);
1178                 len += sprintf(buf+len, "daddr: %#08x\n", self->daddr);
1179                 
1180                 len += sprintf(buf+len, "  win size: %d, ", 
1181                                self->window_size);
1182                 len += sprintf(buf+len, "win: %d, ", self->window);
1183 #if CONFIG_IRDA_DYNAMIC_WINDOW
1184                 len += sprintf(buf+len, "line capacity: %d, ", 
1185                                self->line_capacity);
1186                 len += sprintf(buf+len, "bytes left: %d\n", self->bytes_left);
1187 #endif /* CONFIG_IRDA_DYNAMIC_WINDOW */
1188                 len += sprintf(buf+len, "  tx queue len: %d ", 
1189                                skb_queue_len(&self->txq));
1190                 len += sprintf(buf+len, "win queue len: %d ", 
1191                                skb_queue_len(&self->wx_list));
1192                 len += sprintf(buf+len, "rbusy: %s", self->remote_busy ?
1193                                "TRUE" : "FALSE");
1194                 len += sprintf(buf+len, " mbusy: %s\n", self->media_busy ?
1195                                "TRUE" : "FALSE");
1196                 
1197                 len += sprintf(buf+len, "  retrans: %d ", self->retry_count);
1198                 len += sprintf(buf+len, "vs: %d ", self->vs);
1199                 len += sprintf(buf+len, "vr: %d ", self->vr);
1200                 len += sprintf(buf+len, "va: %d\n", self->va);
1201                 
1202                 len += sprintf(buf+len, "  qos\tbps\tmaxtt\tdsize\twinsize\taddbofs\tmintt\tldisc\tcomp\n");
1203                 
1204                 len += sprintf(buf+len, "  tx\t%d\t", 
1205                                self->qos_tx.baud_rate.value);
1206                 len += sprintf(buf+len, "%d\t", 
1207                                self->qos_tx.max_turn_time.value);
1208                 len += sprintf(buf+len, "%d\t",
1209                                self->qos_tx.data_size.value);
1210                 len += sprintf(buf+len, "%d\t",
1211                                self->qos_tx.window_size.value);
1212                 len += sprintf(buf+len, "%d\t",
1213                                self->qos_tx.additional_bofs.value);
1214                 len += sprintf(buf+len, "%d\t", 
1215                                self->qos_tx.min_turn_time.value);
1216                 len += sprintf(buf+len, "%d\t", 
1217                                self->qos_tx.link_disc_time.value);
1218 #ifdef CONFIG_IRDA_COMPRESSION
1219                 len += sprintf(buf+len, "%d",
1220                                self->qos_tx.compression.value);
1221 #endif
1222                 len += sprintf(buf+len, "\n");
1223 
1224                 len += sprintf(buf+len, "  rx\t%d\t", 
1225                                self->qos_rx.baud_rate.value);
1226                 len += sprintf(buf+len, "%d\t", 
1227                                self->qos_rx.max_turn_time.value);
1228                 len += sprintf(buf+len, "%d\t",
1229                                self->qos_rx.data_size.value);
1230                 len += sprintf(buf+len, "%d\t",
1231                                self->qos_rx.window_size.value);
1232                 len += sprintf(buf+len, "%d\t",
1233                                self->qos_rx.additional_bofs.value);
1234                 len += sprintf(buf+len, "%d\t", 
1235                                self->qos_rx.min_turn_time.value);
1236                 len += sprintf(buf+len, "%d\t", 
1237                                self->qos_rx.link_disc_time.value);
1238 #ifdef CONFIG_IRDA_COMPRESSION
1239                 len += sprintf(buf+len, "%d",
1240                                self->qos_rx.compression.value);
1241 #endif
1242                 len += sprintf(buf+len, "\n");
1243                 
1244                 self = (struct irlap_cb *) hashbin_get_next(irlap);
1245         }
1246         restore_flags(flags);
1247 
1248         return len;
1249 }
1250 
1251 #endif /* CONFIG_PROC_FS */
1252 
1253 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.