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