File: /usr/src/linux/net/irda/irttp.c

1     /*********************************************************************
2      *                
3      * Filename:      irttp.c
4      * Version:       1.2
5      * Description:   Tiny Transport Protocol (TTP) implementation
6      * Status:        Stable
7      * Author:        Dag Brattli <dagb@cs.uit.no>
8      * Created at:    Sun Aug 31 20:14:31 1997
9      * Modified at:   Wed Jan  5 11:31:27 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      *     
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      *     Neither Dag Brattli nor University of Tromsų admit liability nor
21      *     provide warranty for any of this software. This material is 
22      *     provided "AS-IS" and at no charge.
23      *
24      ********************************************************************/
25     
26     #include <linux/config.h>
27     #include <linux/skbuff.h>
28     #include <linux/init.h>
29     
30     #include <asm/byteorder.h>
31     #include <asm/unaligned.h>
32     
33     #include <net/irda/irda.h>
34     #include <net/irda/irmod.h>
35     #include <net/irda/irlap.h>
36     #include <net/irda/irlmp.h>
37     #include <net/irda/parameters.h>
38     #include <net/irda/irttp.h>
39     
40     static struct irttp_cb *irttp = NULL;
41     
42     static void __irttp_close_tsap(struct tsap_cb *self);
43     
44     static int irttp_data_indication(void *instance, void *sap, 
45     				 struct sk_buff *skb);
46     static int irttp_udata_indication(void *instance, void *sap, 
47     				  struct sk_buff *skb);
48     static void irttp_disconnect_indication(void *instance, void *sap,  
49     					LM_REASON reason, struct sk_buff *);
50     static void irttp_connect_indication(void *instance, void *sap, 
51     				     struct qos_info *qos, __u32 max_sdu_size,
52     				     __u8 header_size, struct sk_buff *skb);
53     static void irttp_connect_confirm(void *instance, void *sap, 
54     				  struct qos_info *qos, __u32 max_sdu_size, 
55     				  __u8 header_size, struct sk_buff *skb);
56     static void irttp_run_tx_queue(struct tsap_cb *self);
57     static void irttp_run_rx_queue(struct tsap_cb *self);
58     
59     static void irttp_flush_queues(struct tsap_cb *self);
60     static void irttp_fragment_skb(struct tsap_cb *self, struct sk_buff *skb);
61     static void irttp_start_todo_timer(struct tsap_cb *self, int timeout);
62     static struct sk_buff *irttp_reassemble_skb(struct tsap_cb *self);
63     static int irttp_param_max_sdu_size(void *instance, irda_param_t *param, 
64     				    int get);
65     
66     /* Information for parsing parameters in IrTTP */
67     static pi_minor_info_t pi_minor_call_table[] = {
68     	{ NULL, 0 },                                             /* 0x00 */
69     	{ irttp_param_max_sdu_size, PV_INTEGER | PV_BIG_ENDIAN } /* 0x01 */
70     };
71     static pi_major_info_t pi_major_call_table[] = {{ pi_minor_call_table, 2 }};
72     static pi_param_info_t param_info = { pi_major_call_table, 1, 0x0f, 4 };
73     
74     /*
75      * Function irttp_init (void)
76      *
77      *    Initialize the IrTTP layer. Called by module initialization code
78      *
79      */
80     int __init irttp_init(void)
81     {
82     	/* Initialize the irttp structure. */
83     	if (irttp == NULL) {
84     		irttp = kmalloc(sizeof(struct irttp_cb), GFP_KERNEL);
85     		if (irttp == NULL)
86     			return -ENOMEM;
87     	}
88     	memset(irttp, 0, sizeof(struct irttp_cb));
89     	
90     	irttp->magic = TTP_MAGIC;
91     
92     	irttp->tsaps = hashbin_new(HB_LOCAL);
93     	if (!irttp->tsaps) {
94     		ERROR(__FUNCTION__ "(), can't allocate IrTTP hashbin!\n");
95     		return -ENOMEM;
96     	}
97     	
98     	return 0;
99     }
100     
101     /*
102      * Function irttp_cleanup (void)
103      *
104      *    Called by module destruction/cleanup code
105      *
106      */
107     #ifdef MODULE
108     void irttp_cleanup(void) 
109     {
110     	/* Check for main structure */
111     	ASSERT(irttp != NULL, return;);
112     	ASSERT(irttp->magic == TTP_MAGIC, return;);
113     	
114     	/*
115     	 *  Delete hashbin and close all TSAP instances in it
116     	 */
117     	hashbin_delete(irttp->tsaps, (FREE_FUNC) __irttp_close_tsap);
118     
119     	irttp->magic = 0;
120     	
121     	/* De-allocate main structure */
122     	kfree(irttp);
123     
124     	irttp = NULL;
125     }
126     #endif
127     
128     /*
129      * Function irttp_open_tsap (stsap, notify)
130      *
131      *    Create TSAP connection endpoint,
132      */
133     struct tsap_cb *irttp_open_tsap(__u8 stsap_sel, int credit, notify_t *notify) 
134     {
135     	struct tsap_cb *self;
136     	struct lsap_cb *lsap;
137     	notify_t ttp_notify;
138     
139     	ASSERT(irttp != NULL, return NULL;);
140     	ASSERT(irttp->magic == TTP_MAGIC, return NULL;);
141     
142     	/* The IrLMP spec (IrLMP 1.1 p10) says that we have the right to
143     	 * use only 0x01-0x6F. Of course, we can use LSAP_ANY as well.
144     	 * JeanII */
145     	if((stsap_sel != LSAP_ANY) &&
146     	   ((stsap_sel < 0x01) || (stsap_sel >= 0x70))) {
147     		IRDA_DEBUG(0, __FUNCTION__ "(), invalid tsap!\n");
148     		return NULL;
149     	}
150     
151     	self = kmalloc(sizeof(struct tsap_cb), GFP_ATOMIC);
152     	if (self == NULL) {
153     		IRDA_DEBUG(0, __FUNCTION__ "(), unable to kmalloc!\n");
154     		return NULL;
155     	}
156     	memset(self, 0, sizeof(struct tsap_cb));
157     	spin_lock_init(&self->lock);
158     
159     	init_timer(&self->todo_timer);
160     
161     	/* Initialize callbacks for IrLMP to use */
162     	irda_notify_init(&ttp_notify);
163     	ttp_notify.connect_confirm = irttp_connect_confirm;
164     	ttp_notify.connect_indication = irttp_connect_indication;
165     	ttp_notify.disconnect_indication = irttp_disconnect_indication;
166     	ttp_notify.data_indication = irttp_data_indication;
167     	ttp_notify.udata_indication = irttp_udata_indication;
168     	if(notify->status_indication != NULL)
169     		ttp_notify.status_indication = irttp_status_indication;
170     	ttp_notify.instance = self;
171     	strncpy(ttp_notify.name, notify->name, NOTIFY_MAX_NAME);
172     
173     	self->magic = TTP_TSAP_MAGIC;
174     	self->connected = FALSE;
175     
176     	skb_queue_head_init(&self->rx_queue);
177     	skb_queue_head_init(&self->tx_queue);
178     	skb_queue_head_init(&self->rx_fragments);
179     	/*
180     	 *  Create LSAP at IrLMP layer
181     	 */
182     	lsap = irlmp_open_lsap(stsap_sel, &ttp_notify, 0);
183     	if (lsap == NULL) {
184     		WARNING(__FUNCTION__ "(), unable to allocate LSAP!!\n");
185     		return NULL;
186     	}
187     	
188     	/*
189     	 *  If user specified LSAP_ANY as source TSAP selector, then IrLMP
190     	 *  will replace it with whatever source selector which is free, so
191     	 *  the stsap_sel we have might not be valid anymore
192     	 */
193     	self->stsap_sel = lsap->slsap_sel;
194     	IRDA_DEBUG(4, __FUNCTION__ "(), stsap_sel=%02x\n", self->stsap_sel);
195     
196     	self->notify = *notify;
197     	self->lsap = lsap;
198     
199     	hashbin_insert(irttp->tsaps, (irda_queue_t *) self, (int) self, NULL);
200     
201     	if (credit > TTP_MAX_QUEUE)
202     		self->initial_credit = TTP_MAX_QUEUE;
203     	else
204     		self->initial_credit = credit;
205     
206     	return self;	
207     }
208     
209     /*
210      * Function irttp_close (handle)
211      *
212      *    Remove an instance of a TSAP. This function should only deal with the
213      *    deallocation of the TSAP, and resetting of the TSAPs values;
214      *
215      */
216     static void __irttp_close_tsap(struct tsap_cb *self)
217     {
218     	/* First make sure we're connected. */
219     	ASSERT(self != NULL, return;);
220     	ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
221     
222     	irttp_flush_queues(self);
223     
224     	del_timer(&self->todo_timer);
225     
226     	self->connected = FALSE;
227     	self->magic = ~TTP_TSAP_MAGIC;
228     
229     	kfree(self);
230     }
231     
232     /*
233      * Function irttp_close (self)
234      *
235      *    Remove TSAP from list of all TSAPs and then deallocate all resources
236      *    associated with this TSAP
237      *
238      */
239     int irttp_close_tsap(struct tsap_cb *self)
240     {
241     	struct tsap_cb *tsap;
242     
243     	IRDA_DEBUG(4, __FUNCTION__ "()\n");
244     
245     	ASSERT(self != NULL, return -1;);
246     	ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
247     
248     	/* Make sure tsap has been disconnected */
249     	if (self->connected) {
250     		/* Check if disconnect is not pending */
251     		if (!self->disconnect_pend) {
252     			IRDA_DEBUG(0, __FUNCTION__ "(), TSAP still connected!\n");
253     			irttp_disconnect_request(self, NULL, P_NORMAL);
254     		}
255     		self->close_pend = TRUE;
256     		irttp_start_todo_timer(self, 1*HZ);
257     
258     		return 0; /* Will be back! */
259     	}
260     	
261     	tsap = hashbin_remove(irttp->tsaps, (int) self, NULL);
262     
263     	ASSERT(tsap == self, return -1;);
264     
265     	/* Close corresponding LSAP */
266     	if (self->lsap) {
267     		irlmp_close_lsap(self->lsap);
268     		self->lsap = NULL;
269     	}
270     
271     	__irttp_close_tsap(self);
272     
273     	return 0;
274     }
275     
276     /*
277      * Function irttp_udata_request (self, skb)
278      *
279      *    Send unreliable data on this TSAP
280      *
281      */
282     int irttp_udata_request(struct tsap_cb *self, struct sk_buff *skb) 
283     {
284     	ASSERT(self != NULL, return -1;);
285     	ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
286     	ASSERT(skb != NULL, return -1;);
287     
288     	IRDA_DEBUG(4, __FUNCTION__ "()\n");
289     
290     	/* Check that nothing bad happens */
291     	if ((skb->len == 0) || (!self->connected)) {
292     		IRDA_DEBUG(1, __FUNCTION__ "(), No data, or not connected\n");
293     		return -1;
294     	}
295     	
296     	if (skb->len > self->max_seg_size) {
297     		IRDA_DEBUG(1, __FUNCTION__ "(), UData is to large for IrLAP!\n");
298     		return -1;
299     	}
300     		    
301     	irlmp_udata_request(self->lsap, skb);
302     	self->stats.tx_packets++;
303     
304     	return 0;
305     }
306     
307     /*
308      * Function irttp_data_request (handle, skb)
309      *
310      *    Queue frame for transmission. If SAR is enabled, fragement the frame 
311      *    and queue the fragments for transmission
312      */
313     int irttp_data_request(struct tsap_cb *self, struct sk_buff *skb) 
314     {
315     	__u8 *frame;
316     
317     	ASSERT(self != NULL, return -1;);
318     	ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
319     	ASSERT(skb != NULL, return -1;);
320     
321     	/* Check that nothing bad happens */
322     	if ((skb->len == 0) || (!self->connected)) {
323     		WARNING(__FUNCTION__ "(), No data, or not connected\n");
324     		return -ENOTCONN;
325     	}
326     
327     	/*  
328     	 *  Check if SAR is disabled, and the frame is larger than what fits
329     	 *  inside an IrLAP frame
330     	 */
331     	if ((self->tx_max_sdu_size == 0) && (skb->len > self->max_seg_size)) {
332     		ERROR(__FUNCTION__ 
333     		      "(), SAR disabled, and data is to large for IrLAP!\n");
334     		return -EMSGSIZE;
335     	}
336     
337     	/* 
338     	 *  Check if SAR is enabled, and the frame is larger than the 
339     	 *  TxMaxSduSize 
340     	 */
341     	if ((self->tx_max_sdu_size != 0) && 
342     	    (self->tx_max_sdu_size != TTP_SAR_UNBOUND) && 
343     	    (skb->len > self->tx_max_sdu_size))
344     	{
345     		ERROR(__FUNCTION__ "(), SAR enabled, "
346     		      "but data is larger than TxMaxSduSize!\n");
347     		return -EMSGSIZE;
348     	}
349     	/* 
350     	 *  Check if transmit queue is full
351     	 */
352     	if (skb_queue_len(&self->tx_queue) >= TTP_MAX_QUEUE) {
353     		/*
354     		 *  Give it a chance to empty itself
355     		 */
356     		irttp_run_tx_queue(self);
357     		
358     		return -ENOBUFS;
359     	}
360            
361     	/* Queue frame, or queue frame segments */
362     	if ((self->tx_max_sdu_size == 0) || (skb->len < self->max_seg_size)) {
363     		/* Queue frame */
364     		ASSERT(skb_headroom(skb) >= TTP_HEADER, return -1;);
365     		frame = skb_push(skb, TTP_HEADER);
366     		frame[0] = 0x00; /* Clear more bit */
367     		
368     		skb_queue_tail(&self->tx_queue, skb);
369     	} else {
370     		/*
371     		 *  Fragment the frame, this function will also queue the
372     		 *  fragments, we don't care about the fact the transmit
373     		 *  queue may be overfilled by all the segments for a little
374     		 *  while
375     		 */
376     		irttp_fragment_skb(self, skb);
377     	}
378     
379     	/* Check if we can accept more data from client */
380     	if ((!self->tx_sdu_busy) && 
381     	    (skb_queue_len(&self->tx_queue) > TTP_HIGH_THRESHOLD)) {
382     		
383     		/* Tx queue filling up, so stop client */
384     		self->tx_sdu_busy = TRUE;
385     		
386     	 	if (self->notify.flow_indication) {
387      			self->notify.flow_indication(self->notify.instance, 
388     						     self, FLOW_STOP);
389     		}
390      	}
391     	
392     	/* Try to make some progress */
393     	irttp_run_tx_queue(self);
394     	
395     	return 0;
396     }
397     
398     /*
399      * Function irttp_run_tx_queue (self)
400      *
401      *    Transmit packets queued for transmission (if possible)
402      *
403      */
404     static void irttp_run_tx_queue(struct tsap_cb *self) 
405     {
406     	struct sk_buff *skb;
407     	unsigned long flags;
408     	int n;
409     
410     	if (irda_lock(&self->tx_queue_lock) == FALSE)
411     		return;
412     
413     	/* Try to send out frames as long as we have credits */
414     	while ((self->send_credit > 0) &&
415     	       (skb = skb_dequeue(&self->tx_queue)))
416     	{
417     		/* 
418     		 * Make sure we don't flood IrLAP with frames just because
419     		 * the remote device has given us a lot of credits
420     		 */
421     		if (irlmp_get_lap_tx_queue_len(self->lsap) > LAP_MAX_QUEUE) {
422     			/* Re-queue packet */
423     			skb_queue_head(&self->tx_queue, skb);
424     
425     			/* Try later. Would be better if IrLAP could notify us */
426     			irttp_start_todo_timer(self, MSECS_TO_JIFFIES(10));
427     			
428     			break;
429     		}
430     
431     		/*
432     		 *  Since we can transmit and receive frames concurrently, 
433     		 *  the code below is a critical region and we must assure that
434     		 *  nobody messes with the credits while we update them.
435     		 */
436     		spin_lock_irqsave(&self->lock, flags);
437     
438     		n = self->avail_credit;
439     		self->avail_credit = 0;
440     		
441     		/* Only room for 127 credits in frame */
442     		if (n > 127) {
443     			self->avail_credit = n-127;
444     			n = 127;
445     		}
446     		self->remote_credit += n;
447     		self->send_credit--;
448     
449     		spin_unlock_irqrestore(&self->lock, flags);
450     
451     		/* 
452     		 *  More bit must be set by the data_request() or fragment() 
453     		 *  functions
454     		 */
455     		skb->data[0] |= (n & 0x7f);
456     		
457     		/* Detach from socket.
458     		 * The current skb has a reference to the socket that sent
459     		 * it (skb->sk). When we pass it to IrLMP, the skb will be
460     		 * stored in in IrLAP (self->wx_list). When we are within
461     		 * IrLAP, we loose the notion of socket, so we should not
462     		 * have a reference to a socket. So, we drop it here.
463     		 * 
464     		 * Why does it matter ?
465     		 * When the skb is freed (kfree_skb), if it is associated
466     		 * with a socket, it release buffer space on the socket
467     		 * (through sock_wfree() and sock_def_write_space()).
468     		 * If the socket no longer exist, we may crash. Hard.
469     		 * When we close a socket, we make sure that associated packets
470     		 * in IrTTP are freed. However, we have no way to cancel
471     		 * the packet that we have passed to IrLAP. So, if a packet
472     		 * remains in IrLAP (retry on the link or else) after we
473     		 * close the socket, we are dead !
474     		 * Jean II */
475     		if (skb->sk != NULL) {
476     			struct sk_buff *tx_skb;
477     
478     			/* IrSOCK application, IrOBEX, ... */
479     			IRDA_DEBUG(4, __FUNCTION__ "() : Detaching SKB from socket.\n");
480     			/* Note : still looking for a more efficient way
481     			 * to do that - Jean II */
482     
483     			/* Get another skb on the same buffer, but without
484     			 * a reference to the socket (skb->sk = NULL) */
485     			tx_skb = skb_clone(skb, GFP_ATOMIC);
486     			if (tx_skb != NULL) {
487     				/* Release the skb associated with the
488     				 * socket, and use the new skb insted */
489     				kfree_skb(skb);
490     				skb = tx_skb;
491     			}
492     		} else {
493     			/* IrCOMM over IrTTP, IrLAN, ... */
494     			IRDA_DEBUG(4, __FUNCTION__ "() : Got SKB not attached to a socket.\n");
495     		}
496     
497     		irlmp_data_request(self->lsap, skb);
498     		self->stats.tx_packets++;
499     
500     		/* Check if we can accept more frames from client */
501     		if ((self->tx_sdu_busy) && 
502     		    (skb_queue_len(&self->tx_queue) < TTP_LOW_THRESHOLD)) 
503     		{
504     			self->tx_sdu_busy = FALSE;
505     			
506     			if (self->notify.flow_indication)
507     				self->notify.flow_indication(
508     					self->notify.instance, self,
509     					FLOW_START);
510     		}
511     	}
512     	
513     	/* Reset lock */
514     	self->tx_queue_lock = 0;
515     }
516     
517     /*
518      * Function irttp_give_credit (self)
519      *
520      *    Send a dataless flowdata TTP-PDU and give available credit to peer
521      *    TSAP
522      */
523     void irttp_give_credit(struct tsap_cb *self) 
524     {
525     	struct sk_buff *tx_skb = NULL;
526     	unsigned long flags;
527     	int n;
528     
529     	ASSERT(self != NULL, return;);
530     	ASSERT(self->magic == TTP_TSAP_MAGIC, return;);	
531     
532     	IRDA_DEBUG(4, __FUNCTION__ "() send=%d,avail=%d,remote=%d\n", 
533     		   self->send_credit, self->avail_credit, self->remote_credit);
534     	
535     	/* Give credit to peer */
536     	tx_skb = dev_alloc_skb(64);
537     	if (!tx_skb)
538     		return;
539     
540     	/* Reserve space for LMP, and LAP header */
541     	skb_reserve(tx_skb, self->max_header_size);
542     
543     	/*
544     	 *  Since we can transmit and receive frames concurrently, 
545     	 *  the code below is a critical region and we must assure that
546     	 *  nobody messes with the credits while we update them.
547     	 */
548     	spin_lock_irqsave(&self->lock, flags);
549     
550     	n = self->avail_credit;
551     	self->avail_credit = 0;
552     	
553     	/* Only space for 127 credits in frame */
554     	if (n > 127) {
555     		self->avail_credit = n - 127;
556     		n = 127;
557     	}
558     	self->remote_credit += n;
559     
560     	spin_unlock_irqrestore(&self->lock, flags);
561     
562     	skb_put(tx_skb, 1);
563     	tx_skb->data[0] = (__u8) (n & 0x7f);
564     	
565     	irlmp_data_request(self->lsap, tx_skb);
566     	self->stats.tx_packets++;
567     }
568     
569     /*
570      * Function irttp_udata_indication (instance, sap, skb)
571      *
572      *    Received some unit-data (unreliable)
573      *
574      */
575     static int irttp_udata_indication(void *instance, void *sap, 
576     				  struct sk_buff *skb) 
577     {
578     	struct tsap_cb *self;
579     
580     	IRDA_DEBUG(4, __FUNCTION__ "()\n");
581     
582     	self = (struct tsap_cb *) instance;
583     
584     	ASSERT(self != NULL, return -1;);
585     	ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
586     	ASSERT(skb != NULL, return -1;);
587     
588     	/* Just pass data to layer above */
589     	if (self->notify.udata_indication)
590     		self->notify.udata_indication(self->notify.instance, self,skb);
591     	else
592     		dev_kfree_skb(skb);
593     
594     	self->stats.rx_packets++;
595     
596     	return 0;
597     }
598     
599     /*
600      * Function irttp_data_indication (instance, sap, skb)
601      *
602      *    Receive segment from IrLMP. 
603      *
604      */
605     static int irttp_data_indication(void *instance, void *sap, 
606     				 struct sk_buff *skb)
607     {
608     	struct tsap_cb *self;
609     	int n;
610     
611     	self = (struct tsap_cb *) instance;
612     
613     	n = skb->data[0] & 0x7f;     /* Extract the credits */
614     
615     	self->stats.rx_packets++;
616     
617     	/* 
618     	 *  Data or dataless packet? Dataless frames contains only the 
619     	 *  TTP_HEADER. 
620     	 */
621     	if (skb->len > 1) {
622     		/* Deal with inbound credit */
623     		self->send_credit += n;
624     		self->remote_credit--;
625     		
626     		/* 
627     		 *  We don't remove the TTP header, since we must preserve the
628     		 *  more bit, so the defragment routing knows what to do
629     		 */
630     		skb_queue_tail(&self->rx_queue, skb);
631     	} else {
632     		self->send_credit += n;	/* Dataless flowdata TTP-PDU */
633     		dev_kfree_skb(skb);
634     	}
635     
636     	irttp_run_rx_queue(self);
637     
638     	/* 
639     	 *  Give avay some credits to peer? 
640     	 */
641     	if ((skb_queue_empty(&self->tx_queue)) && 
642     	    (self->remote_credit < TTP_LOW_THRESHOLD) && 
643     	    (self->avail_credit > 0)) 
644     	{
645     		/* Schedule to start immediately after this thread */
646     		irttp_start_todo_timer(self, 0);
647     		/*irttp_give_credit(self);*/
648     	}
649     	
650     	/* 
651     	 * If the peer device has given us some credits and we didn't have
652              * anyone from before, the we need to shedule the tx queue?  
653     	 */
654     	if (self->send_credit == n) {
655     		/*irttp_run_tx_queue(self);*/
656     		irttp_start_todo_timer(self, 0);
657     	}
658     	return 0;
659     }
660     
661     /*
662      * Function irttp_status_indication (self, reason)
663      *
664      *    Status_indication, just pass to the higher layer...
665      *
666      */
667     void irttp_status_indication(void *instance,
668     			     LINK_STATUS link, LOCK_STATUS lock)
669     {
670     	struct tsap_cb *self;
671     
672     	IRDA_DEBUG(4, __FUNCTION__ "()\n");
673     
674     	self = (struct tsap_cb *) instance;
675     	
676     	ASSERT(self != NULL, return;);
677     	ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
678     	
679     	/*
680     	 *  Inform service user if he has requested it
681     	 */
682     	if (self->notify.status_indication != NULL)
683     		self->notify.status_indication(self->notify.instance, 
684     					       link, lock);
685     	else
686     		IRDA_DEBUG(2, __FUNCTION__ "(), no handler\n");
687     }
688     
689     /*
690      * Function irttp_flow_request (self, command)
691      *
692      *    This funtion could be used by the upper layers to tell IrTTP to stop
693      *    delivering frames if the receive queues are starting to get full, or 
694      *    to tell IrTTP to start delivering frames again.
695      */
696     void irttp_flow_request(struct tsap_cb *self, LOCAL_FLOW flow)
697     {
698     	IRDA_DEBUG(1, __FUNCTION__ "()\n");
699     
700     	ASSERT(self != NULL, return;);
701     	ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
702     
703     	switch (flow) {
704     	case FLOW_STOP:
705     		IRDA_DEBUG(1, __FUNCTION__ "(), flow stop\n");
706     		self->rx_sdu_busy = TRUE;
707     		break;
708     	case FLOW_START:
709     		IRDA_DEBUG(1, __FUNCTION__ "(), flow start\n");
710     		self->rx_sdu_busy = FALSE;
711     		
712     		irttp_run_rx_queue(self);
713     		break;
714     	default:
715     		IRDA_DEBUG(1, __FUNCTION__ "(), Unknown flow command!\n");
716     	}
717     }
718     	
719     /*
720      * Function irttp_connect_request (self, dtsap_sel, daddr, qos)
721      *
722      *    Try to connect to remote destination TSAP selector
723      *
724      */
725     int irttp_connect_request(struct tsap_cb *self, __u8 dtsap_sel, 
726     			  __u32 saddr, __u32 daddr,
727     			  struct qos_info *qos, __u32 max_sdu_size, 
728     			  struct sk_buff *userdata) 
729     {
730     	struct sk_buff *skb;
731     	__u8 *frame;
732     	__u8 n;
733     	
734     	IRDA_DEBUG(4, __FUNCTION__ "(), max_sdu_size=%d\n", max_sdu_size); 
735     	
736     	ASSERT(self != NULL, return -EBADR;);
737     	ASSERT(self->magic == TTP_TSAP_MAGIC, return -EBADR;);
738     
739     	if (self->connected)
740     		return -EISCONN;
741     	
742     	/* Any userdata supplied? */
743     	if (userdata == NULL) {
744     		skb = dev_alloc_skb(64);
745     		if (!skb) 
746     			return -ENOMEM;
747     		
748     		/* Reserve space for MUX_CONTROL and LAP header */
749     		skb_reserve(skb, TTP_MAX_HEADER);
750     	} else {
751     		skb = userdata;
752     		/*  
753     		 *  Check that the client has reserved enough space for 
754     		 *  headers
755     		 */
756     		ASSERT(skb_headroom(userdata) >= TTP_MAX_HEADER, return -1;);
757     	}
758     
759     	/* Initialize connection parameters */
760     	self->connected = FALSE;
761     	self->avail_credit = 0;
762     	self->rx_max_sdu_size = max_sdu_size;
763     	self->rx_sdu_size = 0;
764     	self->rx_sdu_busy = FALSE;
765     	self->dtsap_sel = dtsap_sel;
766     
767     	n = self->initial_credit;
768     
769     	self->remote_credit = 0;
770     	self->send_credit = 0;
771     	
772     	/*
773     	 *  Give away max 127 credits for now
774     	 */
775     	if (n > 127) {
776     		self->avail_credit=n-127;
777     		n = 127;
778     	}
779     
780     	self->remote_credit = n;
781     
782     	/* SAR enabled? */
783     	if (max_sdu_size > 0) {
784     		ASSERT(skb_headroom(skb) >= (TTP_MAX_HEADER + TTP_SAR_HEADER), 
785     		       return -1;);
786     
787     		/* Insert SAR parameters */
788     		frame = skb_push(skb, TTP_HEADER+TTP_SAR_HEADER);
789     		
790     		frame[0] = TTP_PARAMETERS | n; 
791     		frame[1] = 0x04; /* Length */
792     		frame[2] = 0x01; /* MaxSduSize */
793     		frame[3] = 0x02; /* Value length */
794     
795     		put_unaligned(cpu_to_be16((__u16) max_sdu_size), 
796     			      (__u16 *)(frame+4));
797     	} else {
798     		/* Insert plain TTP header */
799     		frame = skb_push(skb, TTP_HEADER);
800     		
801     		/* Insert initial credit in frame */
802     		frame[0] = n & 0x7f;
803     	}
804     
805     	/* Connect with IrLMP. No QoS parameters for now */
806     	return irlmp_connect_request(self->lsap, dtsap_sel, saddr, daddr, qos, 
807     				     skb);
808     }
809     
810     /*
811      * Function irttp_connect_confirm (handle, qos, skb)
812      *
813      *    Sevice user confirms TSAP connection with peer. 
814      *
815      */
816     static void irttp_connect_confirm(void *instance, void *sap, 
817     				  struct qos_info *qos, __u32 max_seg_size,
818     				  __u8 max_header_size, struct sk_buff *skb) 
819     {
820     	struct tsap_cb *self;
821     	int parameters;
822     	int ret;
823     	__u8 plen;
824     	__u8 n;
825     
826     	IRDA_DEBUG(4, __FUNCTION__ "()\n");
827     	
828     	self = (struct tsap_cb *) instance;
829     
830     	ASSERT(self != NULL, return;);
831     	ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
832     	ASSERT(skb != NULL, return;);
833     
834     	self->max_seg_size = max_seg_size - TTP_HEADER;
835     	self->max_header_size = max_header_size + TTP_HEADER;
836     
837     	/*
838     	 *  Check if we have got some QoS parameters back! This should be the
839     	 *  negotiated QoS for the link.
840     	 */
841     	if (qos) {
842     		IRDA_DEBUG(4, "IrTTP, Negotiated BAUD_RATE: %02x\n", 
843     		       qos->baud_rate.bits);			
844     		IRDA_DEBUG(4, "IrTTP, Negotiated BAUD_RATE: %d bps.\n", 
845     		       qos->baud_rate.value);
846     	}
847     
848     	n = skb->data[0] & 0x7f;
849     	
850     	IRDA_DEBUG(4, __FUNCTION__ "(), Initial send_credit=%d\n", n);
851     	
852     	self->send_credit = n;
853     	self->tx_max_sdu_size = 0;
854     	self->connected = TRUE;
855     
856     	parameters = skb->data[0] & 0x80;	
857     
858     	ASSERT(skb->len >= TTP_HEADER, return;);
859     	skb_pull(skb, TTP_HEADER);
860     
861     	if (parameters) {
862     		plen = skb->data[0];
863     
864     		ret = irda_param_extract_all(self, skb->data+1,
865     					     IRDA_MIN(skb->len-1, plen), 
866     					     &param_info);
867     
868     		/* Any errors in the parameter list? */
869     		if (ret < 0) {
870     			WARNING(__FUNCTION__ 
871     				"(), error extracting parameters\n");
872     			dev_kfree_skb(skb);
873     
874     			/* Do not accept this connection attempt */
875     			return;
876     		}
877     		/* Remove parameters */
878     		skb_pull(skb, IRDA_MIN(skb->len, plen+1));
879     	}
880     	
881     	IRDA_DEBUG(4, __FUNCTION__ "() send=%d,avail=%d,remote=%d\n", 
882     	      self->send_credit, self->avail_credit, self->remote_credit);
883     
884     	IRDA_DEBUG(2, __FUNCTION__ "(), MaxSduSize=%d\n", self->tx_max_sdu_size);
885     
886     	if (self->notify.connect_confirm) {
887     		self->notify.connect_confirm(self->notify.instance, self, qos,
888     					     self->tx_max_sdu_size,
889     					     self->max_header_size, skb);
890     	}
891     }
892     
893     /*
894      * Function irttp_connect_indication (handle, skb)
895      *
896      *    Some other device is connecting to this TSAP
897      *
898      */
899     void irttp_connect_indication(void *instance, void *sap, struct qos_info *qos,
900     			      __u32 max_seg_size, __u8 max_header_size, 
901     			      struct sk_buff *skb) 
902     {
903     	struct tsap_cb *self;
904     	struct lsap_cb *lsap;
905     	int parameters;
906     	int ret;
907     	__u8 plen;
908     	__u8 n;
909     
910     	self = (struct tsap_cb *) instance;
911     
912     	ASSERT(self != NULL, return;);
913     	ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
914      	ASSERT(skb != NULL, return;);
915     
916     	lsap = (struct lsap_cb *) sap;
917     
918     	self->max_seg_size = max_seg_size - TTP_HEADER;;
919     	self->max_header_size = max_header_size+TTP_HEADER;
920     
921     	IRDA_DEBUG(4, __FUNCTION__ "(), TSAP sel=%02x\n", self->stsap_sel);
922     
923     	/* Need to update dtsap_sel if its equal to LSAP_ANY */
924     	self->dtsap_sel = lsap->dlsap_sel;
925     
926     	n = skb->data[0] & 0x7f;
927     
928     	self->send_credit = n;
929     	self->tx_max_sdu_size = 0;
930     	
931     	parameters = skb->data[0] & 0x80;
932     
933     	ASSERT(skb->len >= TTP_HEADER, return;);
934     	skb_pull(skb, TTP_HEADER);
935     
936     	if (parameters) {
937     		plen = skb->data[0];
938     		
939     		ret = irda_param_extract_all(self, skb->data+1,
940     					     IRDA_MIN(skb->len-1, plen), 
941     					     &param_info);
942     
943     		/* Any errors in the parameter list? */
944     		if (ret < 0) {
945     			WARNING(__FUNCTION__ 
946     				"(), error extracting parameters\n");
947     			dev_kfree_skb(skb);
948     			
949     			/* Do not accept this connection attempt */
950     			return;
951     		}
952     
953     		/* Remove parameters */
954     		skb_pull(skb, IRDA_MIN(skb->len, plen+1));
955     	}
956     
957     	if (self->notify.connect_indication) {
958     		self->notify.connect_indication(self->notify.instance, self, 
959     						qos, self->tx_max_sdu_size, 
960     						self->max_header_size, skb);
961     	} else
962     		dev_kfree_skb(skb);
963     }
964     
965     /*
966      * Function irttp_connect_response (handle, userdata)
967      *
968      *    Service user is accepting the connection, just pass it down to
969      *    IrLMP!
970      * 
971      */
972     int irttp_connect_response(struct tsap_cb *self, __u32 max_sdu_size, 
973     			   struct sk_buff *userdata)
974     {
975     	struct sk_buff *skb;
976     	__u8 *frame;
977     	int ret;
978     	__u8 n;
979     
980     	ASSERT(self != NULL, return -1;);
981     	ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
982     
983     	IRDA_DEBUG(4, __FUNCTION__ "(), Source TSAP selector=%02x\n", 
984     		   self->stsap_sel);
985     	
986     	/* Any userdata supplied? */
987     	if (userdata == NULL) {
988     		skb = dev_alloc_skb(64);
989     		if (!skb)
990     			return -ENOMEM;
991     
992     		/* Reserve space for MUX_CONTROL and LAP header */
993     		skb_reserve(skb, TTP_MAX_HEADER);
994     	} else {
995     		skb = userdata;
996     		/*  
997     		 *  Check that the client has reserved enough space for 
998     		 *  headers
999     		 */
1000     		ASSERT(skb_headroom(skb) >= TTP_MAX_HEADER, return -1;);
1001     	}
1002     	
1003     	self->avail_credit = 0;
1004     	self->remote_credit = 0;
1005     	self->rx_max_sdu_size = max_sdu_size;
1006     	self->rx_sdu_size = 0;
1007     	self->rx_sdu_busy = FALSE;
1008     
1009     	n = self->initial_credit;
1010     
1011     	/* Frame has only space for max 127 credits (7 bits) */
1012     	if (n > 127) {
1013     		self->avail_credit = n - 127;
1014     		n = 127;
1015     	}
1016     
1017     	self->remote_credit = n;
1018     	self->connected = TRUE;
1019     
1020     	/* SAR enabled? */
1021     	if (max_sdu_size > 0) {
1022     		ASSERT(skb_headroom(skb) >= (TTP_MAX_HEADER+TTP_SAR_HEADER), 
1023     		       return -1;);
1024     		
1025     		/* Insert TTP header with SAR parameters */
1026     		frame = skb_push(skb, TTP_HEADER+TTP_SAR_HEADER);
1027     		
1028     		frame[0] = TTP_PARAMETERS | n;
1029     		frame[1] = 0x04; /* Length */
1030     
1031     		/* irda_param_insert(self, IRTTP_MAX_SDU_SIZE, frame+1,  */
1032     /* 				  TTP_SAR_HEADER, &param_info) */
1033     		
1034     		frame[2] = 0x01; /* MaxSduSize */
1035     		frame[3] = 0x02; /* Value length */
1036     
1037     		put_unaligned(cpu_to_be16((__u16) max_sdu_size), 
1038     			      (__u16 *)(frame+4));
1039     	} else {
1040     		/* Insert TTP header */
1041     		frame = skb_push(skb, TTP_HEADER);
1042     		
1043     		frame[0] = n & 0x7f;
1044     	}
1045     	 
1046     	ret = irlmp_connect_response(self->lsap, skb);
1047     
1048     	return ret;
1049     }
1050     
1051     /*
1052      * Function irttp_dup (self, instance)
1053      *
1054      *    Duplicate TSAP, can be used by servers to confirm a connection on a
1055      *    new TSAP so it can keep listening on the old one.
1056      */
1057     struct tsap_cb *irttp_dup(struct tsap_cb *orig, void *instance) 
1058     {
1059     	struct tsap_cb *new;
1060     
1061     	IRDA_DEBUG(1, __FUNCTION__ "()\n");
1062     
1063     	if (!hashbin_find(irttp->tsaps, (int) orig, NULL)) {
1064     		IRDA_DEBUG(0, __FUNCTION__ "(), unable to find TSAP\n");
1065     		return NULL;
1066     	}
1067     	new = kmalloc(sizeof(struct tsap_cb), GFP_ATOMIC);
1068     	if (!new) {
1069     		IRDA_DEBUG(0, __FUNCTION__ "(), unable to kmalloc\n");
1070     		return NULL;
1071     	}
1072     	/* Dup */
1073     	memcpy(new, orig, sizeof(struct tsap_cb));
1074     	new->notify.instance = instance;
1075     	new->lsap = irlmp_dup(orig->lsap, new);
1076     
1077     	/* Not everything should be copied */
1078     	init_timer(&new->todo_timer);
1079     
1080     	skb_queue_head_init(&new->rx_queue);
1081     	skb_queue_head_init(&new->tx_queue);
1082     	skb_queue_head_init(&new->rx_fragments);
1083     
1084     	hashbin_insert(irttp->tsaps, (irda_queue_t *) new, (int) new, NULL);
1085     
1086     	return new;
1087     }
1088     
1089     /*
1090      * Function irttp_disconnect_request (self)
1091      *
1092      *    Close this connection please! If priority is high, the queued data 
1093      *    segments, if any, will be deallocated first
1094      *
1095      */
1096     int irttp_disconnect_request(struct tsap_cb *self, struct sk_buff *userdata, 
1097     			     int priority)
1098     {
1099     	struct sk_buff *skb;
1100     	int ret;
1101     
1102     	ASSERT(self != NULL, return -1;);
1103     	ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
1104     
1105     	/* Already disconnected? */
1106     	if (!self->connected) {
1107     		IRDA_DEBUG(4, __FUNCTION__ "(), already disconnected!\n");
1108     		return -1;
1109     	}
1110     
1111     	/* Disconnect already pending? */
1112     	if (self->disconnect_pend) {
1113     		IRDA_DEBUG(1, __FUNCTION__ "(), disconnect already pending\n");
1114     		if (userdata) {
1115     			dev_kfree_skb(userdata);
1116     		}
1117     
1118     		/* Try to make some progress */
1119     		irttp_run_rx_queue(self);
1120     		return -1;
1121     	}
1122     
1123     	/*
1124     	 *  Check if there is still data segments in the transmit queue
1125     	 */
1126     	if (skb_queue_len(&self->tx_queue) > 0) {
1127     		if (priority == P_HIGH) {
1128     			IRDA_DEBUG(1, __FUNCTION__  "High priority!!()\n" );
1129     			
1130     			/* 
1131     			 *  No need to send the queued data, if we are 
1132     			 *  disconnecting right now since the data will
1133     			 *  not have any usable connection to be sent on
1134     			 */
1135     			irttp_flush_queues(self);
1136     		} else if (priority == P_NORMAL) {
1137     			/* 
1138     			 *  Must delay disconnect til after all data segments
1139     			 *  have been sent an the tx_queue is empty
1140     			 */
1141     			if (userdata)
1142     				self->disconnect_skb = userdata;
1143     			else
1144     				self->disconnect_skb = NULL;
1145     
1146     			self->disconnect_pend = TRUE;
1147     
1148     			irttp_run_tx_queue(self);
1149     
1150     			irttp_start_todo_timer(self, MSECS_TO_JIFFIES(1000));
1151     			return -1;
1152     		}
1153     	}
1154     	IRDA_DEBUG(1, __FUNCTION__ "(), Disconnecting ...\n");
1155     
1156     	self->connected = FALSE;
1157     	
1158     	if (!userdata) {
1159     		skb = dev_alloc_skb(64);
1160     		if (!skb)
1161     			return -ENOMEM;
1162     		
1163     		/* 
1164     		 *  Reserve space for MUX and LAP header 
1165     		 */
1166     		skb_reserve(skb, TTP_MAX_HEADER);
1167     		
1168     		userdata = skb;
1169     	}
1170     	ret = irlmp_disconnect_request(self->lsap, userdata);
1171     
1172     	return ret;
1173     }
1174     
1175     /*
1176      * Function irttp_disconnect_indication (self, reason)
1177      *
1178      *    Disconnect indication, TSAP disconnected by peer?
1179      *
1180      */
1181     void irttp_disconnect_indication(void *instance, void *sap, LM_REASON reason, 
1182     				 struct sk_buff *skb) 
1183     {
1184     	struct tsap_cb *self;
1185     
1186     	IRDA_DEBUG(4, __FUNCTION__ "()\n");
1187     
1188     	self = (struct tsap_cb *) instance;
1189     	
1190     	ASSERT(self != NULL, return;);
1191     	ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1192     	
1193     	self->connected = FALSE;
1194     	
1195     	/* Check if client has already tried to close the TSAP */
1196     	if (self->close_pend) {
1197     		irttp_close_tsap(self);
1198     		return;
1199     	}
1200     
1201     	/* No need to notify the client if has already tried to disconnect */
1202     	if (self->disconnect_pend)
1203     		return;
1204     	
1205     	if (self->notify.disconnect_indication)
1206     		self->notify.disconnect_indication(self->notify.instance, self,
1207     						   reason, skb);
1208     	else
1209     		if (skb)
1210     			dev_kfree_skb(skb);
1211     }
1212     
1213     /*
1214      * Function irttp_do_data_indication (self, skb)
1215      *
1216      *    Try to deliver reassebled skb to layer above, and requeue it if that
1217      *    for some reason should fail. We mark rx sdu as busy to apply back
1218      *    pressure is necessary.
1219      */
1220     void irttp_do_data_indication(struct tsap_cb *self, struct sk_buff *skb)
1221     {
1222     	int err;
1223     
1224     	/* Check if client has already tried to close the TSAP */
1225     	if (self->close_pend || self->disconnect_pend) {
1226     		dev_kfree_skb(skb);
1227     		return;
1228     	}
1229     
1230     	err = self->notify.data_indication(self->notify.instance, self, skb);
1231     
1232     	/* Usually the layer above will notify that it's input queue is
1233     	 * starting to get filled by using the flow request, but this may
1234     	 * be difficult, so it can instead just refuse to eat it and just
1235     	 * give an error back 
1236     	 */
1237     	if (err == -ENOMEM) {
1238     		IRDA_DEBUG(0, __FUNCTION__ "() requeueing skb!\n");
1239     
1240     		/* Make sure we take a break */
1241     		self->rx_sdu_busy = TRUE;
1242     		
1243     		/* Need to push the header in again */
1244     		skb_push(skb, TTP_HEADER);
1245     		skb->data[0] = 0x00; /* Make sure MORE bit is cleared */
1246     		
1247     		/* Put skb back on queue */
1248     		skb_queue_head(&self->rx_queue, skb);
1249     	}
1250     }
1251     
1252     /*
1253      * Function irttp_run_rx_queue (self)
1254      *
1255      *     Check if we have any frames to be transmitted, or if we have any
1256      *     available credit to give away.
1257      */
1258     void irttp_run_rx_queue(struct tsap_cb *self) 
1259     {
1260     	struct sk_buff *skb;
1261     	int more = 0;
1262     
1263     	IRDA_DEBUG(2, __FUNCTION__ "() send=%d,avail=%d,remote=%d\n", 
1264     		   self->send_credit, self->avail_credit, self->remote_credit);
1265     
1266     	if (irda_lock(&self->rx_queue_lock) == FALSE)
1267     		return;
1268     	
1269     	/*
1270     	 *  Reassemble all frames in receive queue and deliver them
1271     	 */
1272     	while (!self->rx_sdu_busy && (skb = skb_dequeue(&self->rx_queue))) {
1273     		self->avail_credit++;
1274     
1275     		more = skb->data[0] & 0x80;
1276     
1277     		/* Remove TTP header */
1278     		skb_pull(skb, TTP_HEADER);
1279     
1280     		/* Add the length of the remaining data */
1281     		self->rx_sdu_size += skb->len;
1282     
1283     		/*  
1284     		 * If SAR is disabled, or user has requested no reassembly
1285     		 * of received fragements then we just deliver them
1286     		 * immediately. This can be requested by clients that
1287     		 * implements byte streams without any message boundaries
1288     		 */
1289     		if (self->rx_max_sdu_size == TTP_SAR_DISABLE) {
1290     			irttp_do_data_indication(self, skb);
1291     			self->rx_sdu_size = 0;
1292     
1293     			continue;
1294     		}
1295     
1296     		/* Check if this is a fragment, and not the last fragment */
1297     		if (more) {
1298     			/*  
1299     			 *  Queue the fragment if we still are within the 
1300     			 *  limits of the maximum size of the rx_sdu
1301     			 */
1302     			if (self->rx_sdu_size <= self->rx_max_sdu_size) {
1303     				IRDA_DEBUG(4, __FUNCTION__ "(), queueing frag\n");
1304     				skb_queue_tail(&self->rx_fragments, skb);
1305     			} else {
1306     				/* Free the part of the SDU that is too big */
1307     				dev_kfree_skb(skb);
1308     			}
1309     			continue;
1310     		}
1311     		/*
1312     		 *  This is the last fragment, so time to reassemble!
1313     		 */
1314     		if ((self->rx_sdu_size <= self->rx_max_sdu_size) ||
1315     		    (self->rx_max_sdu_size == TTP_SAR_UNBOUND)) 
1316     		{
1317     			/* 
1318     			 * A little optimizing. Only queue the fragment if
1319     			 * there are other fragments. Since if this is the
1320     			 * last and only fragment, there is no need to
1321     			 * reassemble :-) 
1322     			 */
1323     			if (!skb_queue_empty(&self->rx_fragments)) {
1324     				skb_queue_tail(&self->rx_fragments, 
1325     					       skb);
1326     				
1327     				skb = irttp_reassemble_skb(self);
1328     			}
1329     			
1330     			/* Now we can deliver the reassembled skb */
1331     			irttp_do_data_indication(self, skb);
1332     		} else {
1333     			IRDA_DEBUG(1, __FUNCTION__ "(), Truncated frame\n");
1334     			
1335     			/* Free the part of the SDU that is too big */
1336     			dev_kfree_skb(skb);
1337     
1338     			/* Deliver only the valid but truncated part of SDU */
1339     			skb = irttp_reassemble_skb(self);
1340     			
1341     			irttp_do_data_indication(self, skb);
1342     		}
1343     		self->rx_sdu_size = 0;
1344     	}
1345     	/* Reset lock */
1346     	self->rx_queue_lock = 0;
1347     }
1348     
1349     /*
1350      * Function irttp_flush_queues (self)
1351      *
1352      *     Flushes (removes all frames) in transitt-buffer (tx_list)
1353      */
1354     void irttp_flush_queues(struct tsap_cb *self)
1355     {
1356     	struct sk_buff* skb;
1357     	
1358     	IRDA_DEBUG(4, __FUNCTION__ "()\n");
1359     
1360     	ASSERT(self != NULL, return;);
1361     	ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1362     	
1363     	/* Deallocate frames waiting to be sent */
1364     	while ((skb = skb_dequeue(&self->tx_queue)) != NULL)
1365     		dev_kfree_skb(skb);
1366     	
1367     	/* Deallocate received frames */
1368     	while ((skb = skb_dequeue(&self->rx_queue)) != NULL)
1369     		dev_kfree_skb(skb);
1370     	
1371     	/* Deallocate received fragments */
1372     	while ((skb = skb_dequeue(&self->rx_fragments)) != NULL)
1373     		dev_kfree_skb(skb);
1374     }
1375     
1376     /*
1377      * Function irttp_reasseble (self)
1378      *
1379      *    Makes a new (continuous) skb of all the fragments in the fragment
1380      *    queue
1381      *
1382      */
1383     static struct sk_buff *irttp_reassemble_skb(struct tsap_cb *self)
1384     {
1385     	struct sk_buff *skb, *frag;
1386     	int n = 0;  /* Fragment index */
1387     	
1388           	ASSERT(self != NULL, return NULL;);
1389     	ASSERT(self->magic == TTP_TSAP_MAGIC, return NULL;);
1390     
1391     	IRDA_DEBUG(2, __FUNCTION__ "(), self->rx_sdu_size=%d\n", 
1392     		   self->rx_sdu_size);
1393     
1394     	skb = dev_alloc_skb(TTP_HEADER + self->rx_sdu_size);
1395     	if (!skb)
1396     		return NULL;
1397     
1398     	/* 
1399     	 * Need to reserve space for TTP header in case this skb needs to 
1400     	 * be requeued in case delivery failes
1401     	 */
1402     	skb_reserve(skb, TTP_HEADER);
1403     	skb_put(skb, self->rx_sdu_size);
1404     
1405     	/*
1406     	 *  Copy all fragments to a new buffer
1407     	 */
1408     	while ((frag = skb_dequeue(&self->rx_fragments)) != NULL) {
1409     		memcpy(skb->data+n, frag->data, frag->len);
1410     		n += frag->len;
1411     		
1412     		dev_kfree_skb(frag);
1413     	}
1414     	IRDA_DEBUG(2, __FUNCTION__ "(), frame len=%d\n", n);
1415     
1416     	IRDA_DEBUG(2, __FUNCTION__ "(), rx_sdu_size=%d\n", self->rx_sdu_size);
1417     	ASSERT(n <= self->rx_sdu_size, return NULL;);
1418     
1419     	/* Set the new length */
1420     	skb_trim(skb, n);
1421     
1422     	self->rx_sdu_size = 0;
1423     
1424     	return skb;
1425     }
1426     
1427     /*
1428      * Function irttp_fragment_skb (skb)
1429      *
1430      *    Fragments a frame and queues all the fragments for transmission
1431      *
1432      */
1433     static void irttp_fragment_skb(struct tsap_cb *self, struct sk_buff *skb)
1434     {
1435     	struct sk_buff *frag;
1436     	__u8 *frame;
1437     
1438     	IRDA_DEBUG(2, __FUNCTION__ "()\n");
1439     
1440     	ASSERT(self != NULL, return;);
1441     	ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1442     	ASSERT(skb != NULL, return;);
1443     
1444     	/*
1445     	 *  Split frame into a number of segments
1446     	 */
1447     	while (skb->len > self->max_seg_size) {
1448     		IRDA_DEBUG(2, __FUNCTION__  "(), fragmenting ...\n");
1449     
1450     		/* Make new segment */
1451     		frag = dev_alloc_skb(self->max_seg_size+self->max_header_size);
1452     		if (!frag)
1453     			return;
1454     
1455     		skb_reserve(frag, self->max_header_size);
1456     
1457     		/* Copy data from the original skb into this fragment. */
1458     		memcpy(skb_put(frag, self->max_seg_size), skb->data, 
1459     		       self->max_seg_size);
1460     
1461     		/* Insert TTP header, with the more bit set */
1462     		frame = skb_push(frag, TTP_HEADER);
1463     		frame[0] = TTP_MORE;
1464     		
1465     		/* Hide the copied data from the original skb */
1466     		skb_pull(skb, self->max_seg_size);
1467     
1468     		/* Queue fragment */
1469     		skb_queue_tail(&self->tx_queue, frag);
1470     	}
1471     	/* Queue what is left of the original skb */
1472     	IRDA_DEBUG(2, __FUNCTION__  "(), queuing last segment\n");
1473     	
1474     	frame = skb_push(skb, TTP_HEADER);
1475     	frame[0] = 0x00; /* Clear more bit */
1476     
1477     	/* Queue fragment */
1478     	skb_queue_tail(&self->tx_queue, skb);
1479     }
1480     
1481     /*
1482      * Function irttp_param_max_sdu_size (self, param)
1483      *
1484      *    Handle the MaxSduSize parameter in the connect frames, this function
1485      *    will be called both when this parameter needs to be inserted into, and
1486      *    extracted from the connect frames
1487      */
1488     static int irttp_param_max_sdu_size(void *instance, irda_param_t *param, 
1489     				    int get)
1490     {
1491     	struct tsap_cb *self;
1492     
1493     	self = (struct tsap_cb *) instance;
1494     
1495     	ASSERT(self != NULL, return -1;);
1496     	ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
1497     
1498     	if (get)
1499     		param->pv.i = self->tx_max_sdu_size;
1500     	else
1501     		self->tx_max_sdu_size = param->pv.i;
1502     
1503     	IRDA_DEBUG(0, __FUNCTION__ "(), MaxSduSize=%d\n", param->pv.i);
1504     	
1505     	return 0;
1506     }
1507     
1508     /*
1509      * Function irttp_todo_expired (data)
1510      *
1511      *    Todo timer has expired!
1512      *
1513      */
1514     static void irttp_todo_expired(unsigned long data)
1515     {
1516     	struct tsap_cb *self = (struct tsap_cb *) data;
1517     
1518     	/* Check that we still exist */
1519     	if (!self || self->magic != TTP_TSAP_MAGIC)
1520     		return;
1521     	
1522     	irttp_run_rx_queue(self);
1523     	irttp_run_tx_queue(self);
1524     		
1525     	/*  Give avay some credits to peer?  */
1526     	if ((self->remote_credit < TTP_LOW_THRESHOLD) && 
1527     	    (self->avail_credit > 0) && (skb_queue_empty(&self->tx_queue)))
1528     	{
1529     		irttp_give_credit(self);
1530     	}
1531     
1532     	/* Check if time for disconnect */
1533     	if (self->disconnect_pend) {
1534     		/* Check if it's possible to disconnect yet */
1535     		if (skb_queue_empty(&self->tx_queue)) {
1536     			
1537     			/* Make sure disconnect is not pending anymore */
1538     			self->disconnect_pend = FALSE;
1539     			if (self->disconnect_skb) {
1540     				irttp_disconnect_request(
1541     					self, self->disconnect_skb, P_NORMAL);
1542     				self->disconnect_skb = NULL;
1543     			} else
1544     				irttp_disconnect_request(self, NULL, P_NORMAL);
1545     		} else {
1546     			/* Try again later */
1547     			irttp_start_todo_timer(self, 1*HZ);
1548     			
1549     			/* No reason to try and close now */
1550     			return;
1551     		}
1552     	}
1553     	
1554     	/* Check if it's closing time */
1555     	if (self->close_pend)
1556     		irttp_close_tsap(self);
1557     }
1558     
1559     /*
1560      * Function irttp_start_todo_timer (self, timeout)
1561      *
1562      *    Start todo timer. 
1563      *
1564      */
1565     static void irttp_start_todo_timer(struct tsap_cb *self, int timeout)
1566     {
1567     	ASSERT(self != NULL, return;);
1568     	ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
1569     
1570     	del_timer(&self->todo_timer);
1571     	
1572     	self->todo_timer.data     = (unsigned long) self;
1573     	self->todo_timer.function = &irttp_todo_expired;
1574     	self->todo_timer.expires  = jiffies + timeout;
1575     
1576     	add_timer(&self->todo_timer);
1577     }
1578     
1579     #ifdef CONFIG_PROC_FS
1580     /*
1581      * Function irttp_proc_read (buf, start, offset, len, unused)
1582      *
1583      *    Give some info to the /proc file system
1584      */
1585     int irttp_proc_read(char *buf, char **start, off_t offset, int len)
1586     {
1587     	struct tsap_cb *self;
1588     	unsigned long flags;
1589     	int i = 0;
1590     	
1591     	ASSERT(irttp != NULL, return 0;);
1592     	
1593     	len = 0;
1594     	
1595     	save_flags(flags);
1596     	cli();
1597     
1598     	self = (struct tsap_cb *) hashbin_get_first(irttp->tsaps);
1599     	while (self != NULL) {
1600     		if (!self || self->magic != TTP_TSAP_MAGIC)
1601     			break;
1602     
1603     		len += sprintf(buf+len, "TSAP %d, ", i++);
1604     		len += sprintf(buf+len, "stsap_sel: %02x, ", 
1605     			       self->stsap_sel);
1606     		len += sprintf(buf+len, "dtsap_sel: %02x\n", 
1607     			       self->dtsap_sel);
1608     		len += sprintf(buf+len, "  connected: %s, ",
1609     			       self->connected? "TRUE":"FALSE");
1610     		len += sprintf(buf+len, "avail credit: %d, ",
1611     			       self->avail_credit);
1612     		len += sprintf(buf+len, "remote credit: %d, ",
1613     			       self->remote_credit);
1614     		len += sprintf(buf+len, "send credit: %d\n",
1615     			       self->send_credit);
1616     		len += sprintf(buf+len, "  tx packets: %ld, ",
1617     			       self->stats.tx_packets);
1618     		len += sprintf(buf+len, "rx packets: %ld, ",
1619     			       self->stats.rx_packets);
1620     		len += sprintf(buf+len, "tx_queue len: %d ", 
1621     			       skb_queue_len(&self->tx_queue));
1622     		len += sprintf(buf+len, "rx_queue len: %d\n", 
1623     			       skb_queue_len(&self->rx_queue));
1624     		len += sprintf(buf+len, "  tx_sdu_busy: %s, ",
1625     			       self->tx_sdu_busy? "TRUE":"FALSE");
1626     		len += sprintf(buf+len, "rx_sdu_busy: %s\n",
1627     			       self->rx_sdu_busy? "TRUE":"FALSE");
1628     		len += sprintf(buf+len, "  max_seg_size: %d, ",
1629     			       self->max_seg_size);
1630     		len += sprintf(buf+len, "tx_max_sdu_size: %d, ",
1631     			       self->tx_max_sdu_size);
1632     		len += sprintf(buf+len, "rx_max_sdu_size: %d\n",
1633     			       self->rx_max_sdu_size);
1634     
1635     		len += sprintf(buf+len, "  Used by (%s)\n", 
1636     				self->notify.name);
1637     
1638     		len += sprintf(buf+len, "\n");
1639     		
1640     		self = (struct tsap_cb *) hashbin_get_next(irttp->tsaps);
1641     	}
1642     	restore_flags(flags);
1643     
1644     	return len;
1645     }
1646     
1647     #endif /* PROC_FS */
1648