Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

irttp.c

Go to the documentation of this file.
00001 /*********************************************************************
00002  *                
00003  * Filename:      irttp.c
00004  * Version:       1.2
00005  * Description:   Tiny Transport Protocol (TTP) implementation
00006  * Status:        Stable
00007  * Author:        Dag Brattli <dagb@cs.uit.no>
00008  * Created at:    Sun Aug 31 20:14:31 1997
00009  * Modified at:   Sun Jan 30 14:30:32 2000
00010  * Modified by:   Dag Brattli <dagb@cs.uit.no>
00011  * 
00012  *     Copyright (c) 1998-2000 Dag Brattli <dagb@cs.uit.no>, 
00013  *     All Rights Reserved.
00014  *     
00015  *     This program is free software; you can redistribute it and/or 
00016  *     modify it under the terms of the GNU General Public License as 
00017  *     published by the Free Software Foundation; either version 2 of 
00018  *     the License, or (at your option) any later version.
00019  *
00020  *     Neither Dag Brattli nor University of Tromsų admit liability nor
00021  *     provide warranty for any of this software. This material is 
00022  *     provided "AS-IS" and at no charge.
00023  *
00024  ********************************************************************/
00025 
00026 #include <linux/config.h>
00027 #include <linux/skbuff.h>
00028 #include <linux/init.h>
00029 
00030 #include <asm/byteorder.h>
00031 #include <asm/unaligned.h>
00032 
00033 #include <net/irda/irda.h>
00034 #include <net/irda/irmod.h>
00035 #include <net/irda/irlap.h>
00036 #include <net/irda/irlmp.h>
00037 #include <net/irda/parameters.h>
00038 #include <net/irda/irttp.h>
00039 
00040 static struct irttp_cb *irttp = NULL;
00041 
00042 static void __irttp_close_tsap(struct tsap_cb *self);
00043 
00044 static int irttp_data_indication(void *instance, void *sap, 
00045                                  struct sk_buff *skb);
00046 static int irttp_udata_indication(void *instance, void *sap, 
00047                                   struct sk_buff *skb);
00048 static void irttp_disconnect_indication(void *instance, void *sap,  
00049                                         LM_REASON reason, struct sk_buff *);
00050 static void irttp_connect_indication(void *instance, void *sap, 
00051                                      struct qos_info *qos, __u32 max_sdu_size,
00052                                      __u8 header_size, struct sk_buff *skb);
00053 static void irttp_connect_confirm(void *instance, void *sap, 
00054                                   struct qos_info *qos, __u32 max_sdu_size, 
00055                                   __u8 header_size, struct sk_buff *skb);
00056 static void irttp_run_tx_queue(struct tsap_cb *self);
00057 static void irttp_run_rx_queue(struct tsap_cb *self);
00058 
00059 static void irttp_flush_queues(struct tsap_cb *self);
00060 static void irttp_fragment_skb(struct tsap_cb *self, struct sk_buff *skb);
00061 static void irttp_start_todo_timer(struct tsap_cb *self, int timeout);
00062 static struct sk_buff *irttp_reassemble_skb(struct tsap_cb *self);
00063 static int irttp_param_max_sdu_size(void *instance, irda_param_t *param, 
00064                                     int get);
00065 
00066 /* Information for parsing parameters in IrTTP */
00067 static pi_minor_info_t pi_minor_call_table[] = {
00068         { NULL, 0 },                                             /* 0x00 */
00069         { irttp_param_max_sdu_size, PV_INTEGER | PV_BIG_ENDIAN } /* 0x01 */
00070 };
00071 static pi_major_info_t pi_major_call_table[] = {{ pi_minor_call_table, 2 }};
00072 static pi_param_info_t param_info = { pi_major_call_table, 1, 0x0f, 4 };
00073 
00074 /*
00075  * Function irttp_init (void)
00076  *
00077  *    Initialize the IrTTP layer. Called by module initialization code
00078  *
00079  */
00080 int __init irttp_init(void)
00081 {
00082         /* Initialize the irttp structure. */
00083         if (irttp == NULL) {
00084                 irttp = kmalloc(sizeof(struct irttp_cb), GFP_KERNEL);
00085                 if (irttp == NULL)
00086                         return -ENOMEM;
00087         }
00088         memset(irttp, 0, sizeof(struct irttp_cb));
00089         
00090         irttp->magic = TTP_MAGIC;
00091 
00092         irttp->tsaps = hashbin_new(HB_LOCAL);
00093         if (!irttp->tsaps) {
00094                 ERROR(__FUNCTION__ "(), can't allocate IrTTP hashbin!\n");
00095                 return -ENOMEM;
00096         }
00097         
00098         return 0;
00099 }
00100 
00101 /*
00102  * Function irttp_cleanup (void)
00103  *
00104  *    Called by module destruction/cleanup code
00105  *
00106  */
00107 #ifdef MODULE
00108 void irttp_cleanup(void) 
00109 {
00110         /* Check for main structure */
00111         ASSERT(irttp != NULL, return;);
00112         ASSERT(irttp->magic == TTP_MAGIC, return;);
00113         
00114         /*
00115          *  Delete hashbin and close all TSAP instances in it
00116          */
00117         hashbin_delete(irttp->tsaps, (FREE_FUNC) __irttp_close_tsap);
00118 
00119         irttp->magic = 0;
00120         
00121         /* De-allocate main structure */
00122         kfree(irttp);
00123 
00124         irttp = NULL;
00125 }
00126 #endif
00127 
00128 /*
00129  * Function irttp_open_tsap (stsap, notify)
00130  *
00131  *    Create TSAP connection endpoint,
00132  */
00133 struct tsap_cb *irttp_open_tsap(__u8 stsap_sel, int credit, notify_t *notify) 
00134 {
00135         struct tsap_cb *self;
00136         struct lsap_cb *lsap;
00137         notify_t ttp_notify;
00138 
00139         ASSERT(irttp != NULL, return NULL;);
00140         ASSERT(irttp->magic == TTP_MAGIC, return NULL;);
00141 
00142         self = kmalloc(sizeof(struct tsap_cb), GFP_ATOMIC);
00143         if (self == NULL) {
00144                 IRDA_DEBUG(0, __FUNCTION__ "(), unable to kmalloc!\n");
00145                 return NULL;
00146         }
00147         memset(self, 0, sizeof(struct tsap_cb));
00148         spin_lock_init(&self->lock);
00149 
00150         init_timer(&self->todo_timer);
00151 
00152         /* Initialize callbacks for IrLMP to use */
00153         irda_notify_init(&ttp_notify);
00154         ttp_notify.connect_confirm = irttp_connect_confirm;
00155         ttp_notify.connect_indication = irttp_connect_indication;
00156         ttp_notify.disconnect_indication = irttp_disconnect_indication;
00157         ttp_notify.data_indication = irttp_data_indication;
00158         ttp_notify.udata_indication = irttp_udata_indication;
00159         ttp_notify.instance = self;
00160         strncpy(ttp_notify.name, notify->name, NOTIFY_MAX_NAME);
00161 
00162         self->magic = TTP_TSAP_MAGIC;
00163         self->connected = FALSE;
00164 
00165         skb_queue_head_init(&self->rx_queue);
00166         skb_queue_head_init(&self->tx_queue);
00167         skb_queue_head_init(&self->rx_fragments);
00168         /*
00169          *  Create LSAP at IrLMP layer
00170          */
00171         lsap = irlmp_open_lsap(stsap_sel, &ttp_notify, 0);
00172         if (lsap == NULL) {
00173                 WARNING(__FUNCTION__ "(), unable to allocate LSAP!!\n");
00174                 return NULL;
00175         }
00176         
00177         /*
00178          *  If user specified LSAP_ANY as source TSAP selector, then IrLMP
00179          *  will replace it with whatever source selector which is free, so
00180          *  the stsap_sel we have might not be valid anymore
00181          */
00182         self->stsap_sel = lsap->slsap_sel;
00183         IRDA_DEBUG(4, __FUNCTION__ "(), stsap_sel=%02x\n", self->stsap_sel);
00184 
00185         self->notify = *notify;
00186         self->lsap = lsap;
00187 
00188         hashbin_insert(irttp->tsaps, (queue_t *) self, (int) self, NULL);
00189 
00190         if (credit > TTP_MAX_QUEUE)
00191                 self->initial_credit = TTP_MAX_QUEUE;
00192         else
00193                 self->initial_credit = credit;
00194 
00195         return self;    
00196 }
00197 
00198 /*
00199  * Function irttp_close (handle)
00200  *
00201  *    Remove an instance of a TSAP. This function should only deal with the
00202  *    deallocation of the TSAP, and resetting of the TSAPs values;
00203  *
00204  */
00205 static void __irttp_close_tsap(struct tsap_cb *self)
00206 {
00207         /* First make sure we're connected. */
00208         ASSERT(self != NULL, return;);
00209         ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
00210 
00211         irttp_flush_queues(self);
00212 
00213         del_timer(&self->todo_timer);
00214 
00215         self->connected = FALSE;
00216         self->magic = ~TTP_TSAP_MAGIC;
00217 
00218         kfree(self);
00219 }
00220 
00221 /*
00222  * Function irttp_close (self)
00223  *
00224  *    Remove TSAP from list of all TSAPs and then deallocate all resources
00225  *    associated with this TSAP
00226  *
00227  */
00228 int irttp_close_tsap(struct tsap_cb *self)
00229 {
00230         struct tsap_cb *tsap;
00231 
00232         IRDA_DEBUG(4, __FUNCTION__ "()\n");
00233 
00234         ASSERT(self != NULL, return -1;);
00235         ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
00236 
00237         /* Make sure tsap has been disconnected */
00238         if (self->connected) {
00239                 /* Check if disconnect is not pending */
00240                 if (!self->disconnect_pend) {
00241                         IRDA_DEBUG(0, __FUNCTION__ "(), TSAP still connected!\n");
00242                         irttp_disconnect_request(self, NULL, P_NORMAL);
00243                 }
00244                 self->close_pend = TRUE;
00245                 irttp_start_todo_timer(self, 1*HZ);
00246 
00247                 return 0; /* Will be back! */
00248         }
00249         
00250         tsap = hashbin_remove(irttp->tsaps, (int) self, NULL);
00251 
00252         ASSERT(tsap == self, return -1;);
00253 
00254         /* Close corresponding LSAP */
00255         if (self->lsap) {
00256                 irlmp_close_lsap(self->lsap);
00257                 self->lsap = NULL;
00258         }
00259 
00260         __irttp_close_tsap(self);
00261 
00262         return 0;
00263 }
00264 
00265 /*
00266  * Function irttp_udata_request (self, skb)
00267  *
00268  *    Send unreliable data on this TSAP
00269  *
00270  */
00271 int irttp_udata_request(struct tsap_cb *self, struct sk_buff *skb) 
00272 {
00273         ASSERT(self != NULL, return -1;);
00274         ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
00275         ASSERT(skb != NULL, return -1;);
00276 
00277         IRDA_DEBUG(4, __FUNCTION__ "()\n");
00278 
00279         /* Check that nothing bad happens */
00280         if ((skb->len == 0) || (!self->connected)) {
00281                 IRDA_DEBUG(1, __FUNCTION__ "(), No data, or not connected\n");
00282                 return -1;
00283         }
00284         
00285         if (skb->len > self->max_seg_size) {
00286                 IRDA_DEBUG(1, __FUNCTION__ "(), UData is to large for IrLAP!\n");
00287                 return -1;
00288         }
00289                     
00290         irlmp_udata_request(self->lsap, skb);
00291         self->stats.tx_packets++;
00292 
00293         return 0;
00294 }
00295 
00296 /*
00297  * Function irttp_data_request (handle, skb)
00298  *
00299  *    Queue frame for transmission. If SAR is enabled, fragement the frame 
00300  *    and queue the fragments for transmission
00301  */
00302 int irttp_data_request(struct tsap_cb *self, struct sk_buff *skb) 
00303 {
00304         __u8 *frame;
00305 
00306         /* Check that nothing bad happens */
00307         if ((skb->len == 0) || (!self->connected)) {
00308                 WARNING(__FUNCTION__ "(), No data, or not connected\n");
00309                 return -ENOTCONN;
00310         }
00311 
00312         /*  
00313          *  Check if SAR is disabled, and the frame is larger than what fits
00314          *  inside an IrLAP frame
00315          */
00316         if ((self->tx_max_sdu_size == 0) && (skb->len > self->max_seg_size)) {
00317                 ERROR(__FUNCTION__ 
00318                       "(), SAR disabled, and data is to large for IrLAP!\n");
00319                 return -EMSGSIZE;
00320         }
00321 
00322         /* 
00323          *  Check if SAR is enabled, and the frame is larger than the 
00324          *  TxMaxSduSize 
00325          */
00326         if ((self->tx_max_sdu_size != 0) && 
00327             (self->tx_max_sdu_size != TTP_SAR_UNBOUND) && 
00328             (skb->len > self->tx_max_sdu_size))
00329         {
00330                 ERROR(__FUNCTION__ "(), SAR enabled, "
00331                       "but data is larger than TxMaxSduSize!\n");
00332                 return -EMSGSIZE;
00333         }
00334         /* 
00335          *  Check if transmit queue is full
00336          */
00337         if (skb_queue_len(&self->tx_queue) >= TTP_MAX_QUEUE) {
00338                 /*
00339                  *  Give it a chance to empty itself
00340                  */
00341                 irttp_run_tx_queue(self);
00342                 
00343                 return -ENOBUFS;
00344         }
00345        
00346         /* Queue frame, or queue frame segments */
00347         if ((self->tx_max_sdu_size == 0) || (skb->len < self->max_seg_size)) {
00348                 /* Queue frame */
00349                 ASSERT(skb_headroom(skb) >= TTP_HEADER, return -1;);
00350                 frame = skb_push(skb, TTP_HEADER);
00351                 frame[0] = 0x00; /* Clear more bit */
00352                 
00353                 skb_queue_tail(&self->tx_queue, skb);
00354         } else {
00355                 /*
00356                  *  Fragment the frame, this function will also queue the
00357                  *  fragments, we don't care about the fact the the transmit
00358                  *  queue may be overfilled by all the segments for a little
00359                  *  while
00360                  */
00361                 irttp_fragment_skb(self, skb);
00362         }
00363 
00364         /* Check if we can accept more data from client */
00365         if ((!self->tx_sdu_busy) && 
00366             (skb_queue_len(&self->tx_queue) > TTP_HIGH_THRESHOLD)) {
00367                 
00368                 /* Tx queue filling up, so stop client */
00369                 self->tx_sdu_busy = TRUE;
00370                 
00371                 if (self->notify.flow_indication) {
00372                         self->notify.flow_indication(self->notify.instance, 
00373                                                      self, FLOW_STOP);
00374                 }
00375         }
00376         
00377         /* Try to make some progress */
00378         irttp_run_tx_queue(self);
00379         
00380         return 0;
00381 }
00382 
00383 /*
00384  * Function irttp_run_tx_queue (self)
00385  *
00386  *    Transmit packets queued for transmission (if possible)
00387  *
00388  */
00389 static void irttp_run_tx_queue(struct tsap_cb *self) 
00390 {
00391         struct sk_buff *skb;
00392         unsigned long flags;
00393         int n;
00394 
00395         if (irda_lock(&self->tx_queue_lock) == FALSE)
00396                 return;
00397 
00398         /* Try to send out frames as long as we have credits */
00399         while ((self->send_credit > 0) &&
00400                (skb = skb_dequeue(&self->tx_queue)))
00401         {
00402                 /* 
00403                  * Make sure we don't flood IrLAP with frames just because
00404                  * the remote device has given us a lot of credits
00405                  */
00406                 if (irlmp_get_lap_tx_queue_len(self->lsap) > LAP_MAX_QUEUE) {
00407                         /* Re-queue packet */
00408                         skb_queue_head(&self->tx_queue, skb);
00409 
00410                         /* Try later. Maybe IrLAP could notify us? */
00411                         irttp_start_todo_timer(self, MSECS_TO_JIFFIES(10));
00412                         
00413                         break;
00414                 }
00415 
00416                 /*
00417                  *  Since we can transmit and receive frames concurrently, 
00418                  *  the code below is a critical region and we must assure that
00419                  *  nobody messes with the credits while we update them.
00420                  */
00421                 spin_lock_irqsave(&self->lock, flags);
00422 
00423                 n = self->avail_credit;
00424                 self->avail_credit = 0;
00425                 
00426                 /* Only room for 127 credits in frame */
00427                 if (n > 127) {
00428                         self->avail_credit = n-127;
00429                         n = 127;
00430                 }
00431                 self->remote_credit += n;
00432                 self->send_credit--;
00433 
00434                 spin_unlock_irqrestore(&self->lock, flags);
00435 
00436                 /* 
00437                  *  More bit must be set by the data_request() or fragment() 
00438                  *  functions
00439                  */
00440                 skb->data[0] |= (n & 0x7f);
00441                 
00442                 irlmp_data_request(self->lsap, skb);
00443                 self->stats.tx_packets++;
00444 
00445                 /* Check if we can accept more frames from client */
00446                 if ((self->tx_sdu_busy) && 
00447                     (skb_queue_len(&self->tx_queue) < TTP_LOW_THRESHOLD)) 
00448                 {
00449                         self->tx_sdu_busy = FALSE;
00450                         
00451                         if (self->notify.flow_indication)
00452                                 self->notify.flow_indication(
00453                                         self->notify.instance, self,
00454                                         FLOW_START);
00455                 }
00456         }
00457         self->tx_queue_lock = 0; /* Reset lock */
00458 }
00459 
00460 /*
00461  * Function irttp_give_credit (self)
00462  *
00463  *    Send a dataless flowdata TTP-PDU and give available credit to peer
00464  *    TSAP
00465  */
00466 void irttp_give_credit(struct tsap_cb *self) 
00467 {
00468         struct sk_buff *tx_skb = NULL;
00469         unsigned long flags;
00470         int n;
00471 
00472         ASSERT(self != NULL, return;);
00473         ASSERT(self->magic == TTP_TSAP_MAGIC, return;); 
00474 
00475         IRDA_DEBUG(4, __FUNCTION__ "() send=%d,avail=%d,remote=%d\n", 
00476                    self->send_credit, self->avail_credit, self->remote_credit);
00477         
00478         /* Give credit to peer */
00479         tx_skb = dev_alloc_skb(64);
00480         if (!tx_skb)
00481                 return;
00482 
00483         /* Reserve space for LMP, and LAP header */
00484         skb_reserve(tx_skb, self->max_header_size);
00485 
00486         /*
00487          *  Since we can transmit and receive frames concurrently, 
00488          *  the code below is a critical region and we must assure that
00489          *  nobody messes with the credits while we update them.
00490          */
00491         spin_lock_irqsave(&self->lock, flags);
00492 
00493         n = self->avail_credit;
00494         self->avail_credit = 0;
00495         
00496         /* Only space for 127 credits in frame */
00497         if (n > 127) {
00498                 self->avail_credit = n - 127;
00499                 n = 127;
00500         }
00501         self->remote_credit += n;
00502 
00503         spin_unlock_irqrestore(&self->lock, flags);
00504 
00505         skb_put(tx_skb, 1);
00506         tx_skb->data[0] = (__u8) (n & 0x7f);
00507         
00508         irlmp_data_request(self->lsap, tx_skb);
00509         self->stats.tx_packets++;
00510 }
00511 
00512 /*
00513  * Function irttp_udata_indication (instance, sap, skb)
00514  *
00515  *    Received some unit-data (unreliable)
00516  *
00517  */
00518 static int irttp_udata_indication(void *instance, void *sap, 
00519                                   struct sk_buff *skb) 
00520 {
00521         struct tsap_cb *self;
00522 
00523         IRDA_DEBUG(4, __FUNCTION__ "()\n");
00524 
00525         self = (struct tsap_cb *) instance;
00526 
00527         ASSERT(self != NULL, return -1;);
00528         ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
00529         ASSERT(skb != NULL, return -1;);
00530 
00531         /* Just pass data to layer above */
00532         if (self->notify.udata_indication)
00533                 self->notify.udata_indication(self->notify.instance, self,skb);
00534         else
00535                 dev_kfree_skb(skb);
00536 
00537         self->stats.rx_packets++;
00538 
00539         return 0;
00540 }
00541 
00542 /*
00543  * Function irttp_data_indication (instance, sap, skb)
00544  *
00545  *    Receive segment from IrLMP. 
00546  *
00547  */
00548 static int irttp_data_indication(void *instance, void *sap, 
00549                                  struct sk_buff *skb)
00550 {
00551         struct tsap_cb *self;
00552         int n;
00553 
00554         self = (struct tsap_cb *) instance;
00555 
00556         n = skb->data[0] & 0x7f;     /* Extract the credits */
00557 
00558         self->stats.rx_packets++;
00559 
00560         /* 
00561          *  Data or dataless packet? Dataless frames contains only the 
00562          *  TTP_HEADER. 
00563          */
00564         if (skb->len > 1) {
00565                 /* Deal with inbound credit */
00566                 self->send_credit += n;
00567                 self->remote_credit--;
00568                 
00569                 /* 
00570                  *  We don't remove the TTP header, since we must preserve the
00571                  *  more bit, so the defragment routing knows what to do
00572                  */
00573                 skb_queue_tail(&self->rx_queue, skb);
00574         } else {
00575                 self->send_credit += n; /* Dataless flowdata TTP-PDU */
00576                 dev_kfree_skb(skb);
00577         }
00578 
00579         irttp_run_rx_queue(self);
00580 
00581         /* 
00582          *  Give avay some credits to peer? 
00583          */
00584         if ((skb_queue_empty(&self->tx_queue)) && 
00585             (self->remote_credit < TTP_LOW_THRESHOLD) && 
00586             (self->avail_credit > 0)) 
00587         {
00588                 /* Schedule to start immediately after this thread */
00589                 irttp_start_todo_timer(self, 0);
00590                 /*irttp_give_credit(self);*/
00591         }
00592         
00593         /* 
00594          * If the peer device has given us some credits and we didn't have
00595          * anyone from before, the we need to shedule the tx queue?  
00596          */
00597         if (self->send_credit == n) {
00598                 /*irttp_run_tx_queue(self);*/
00599                 irttp_start_todo_timer(self, 0);
00600         }
00601         return 0;
00602 }
00603 
00604 /*
00605  * Function irttp_flow_request (self, command)
00606  *
00607  *    This funtion could be used by the upper layers to tell IrTTP to stop
00608  *    delivering frames if the receive queues are starting to get full, or 
00609  *    to tell IrTTP to start delivering frames again.
00610  */
00611 void irttp_flow_request(struct tsap_cb *self, LOCAL_FLOW flow)
00612 {
00613         IRDA_DEBUG(1, __FUNCTION__ "()\n");
00614 
00615         ASSERT(self != NULL, return;);
00616         ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
00617 
00618         switch (flow) {
00619         case FLOW_STOP:
00620                 IRDA_DEBUG(1, __FUNCTION__ "(), flow stop\n");
00621                 self->rx_sdu_busy = TRUE;
00622                 break;
00623         case FLOW_START:
00624                 IRDA_DEBUG(1, __FUNCTION__ "(), flow start\n");
00625                 self->rx_sdu_busy = FALSE;
00626                 
00627                 irttp_run_rx_queue(self);
00628                 break;
00629         default:
00630                 IRDA_DEBUG(1, __FUNCTION__ "(), Unknown flow command!\n");
00631         }
00632 }
00633         
00634 /*
00635  * Function irttp_connect_request (self, dtsap_sel, daddr, qos)
00636  *
00637  *    Try to connect to remote destination TSAP selector
00638  *
00639  */
00640 int irttp_connect_request(struct tsap_cb *self, __u8 dtsap_sel, 
00641                           __u32 saddr, __u32 daddr,
00642                           struct qos_info *qos, __u32 max_sdu_size, 
00643                           struct sk_buff *userdata) 
00644 {
00645         struct sk_buff *skb;
00646         __u8 *frame;
00647         __u8 n;
00648         
00649         IRDA_DEBUG(4, __FUNCTION__ "(), max_sdu_size=%d\n", max_sdu_size); 
00650         
00651         ASSERT(self != NULL, return -EBADR;);
00652         ASSERT(self->magic == TTP_TSAP_MAGIC, return -EBADR;);
00653 
00654         if (self->connected)
00655                 return -EISCONN;
00656         
00657         /* Any userdata supplied? */
00658         if (userdata == NULL) {
00659                 skb = dev_alloc_skb(64);
00660                 if (!skb) 
00661                         return -ENOMEM;
00662                 
00663                 /* Reserve space for MUX_CONTROL and LAP header */
00664                 skb_reserve(skb, TTP_MAX_HEADER);
00665         } else {
00666                 skb = userdata;
00667                 /*  
00668                  *  Check that the client has reserved enough space for 
00669                  *  headers
00670                  */
00671                 ASSERT(skb_headroom(userdata) >= TTP_MAX_HEADER, return -1;);
00672         }
00673 
00674         /* Initialize connection parameters */
00675         self->connected = FALSE;
00676         self->avail_credit = 0;
00677         self->rx_max_sdu_size = max_sdu_size;
00678         self->rx_sdu_size = 0;
00679         self->rx_sdu_busy = FALSE;
00680         self->dtsap_sel = dtsap_sel;
00681 
00682         n = self->initial_credit;
00683 
00684         self->remote_credit = 0;
00685         self->send_credit = 0;
00686         
00687         /*
00688          *  Give away max 127 credits for now
00689          */
00690         if (n > 127) {
00691                 self->avail_credit=n-127;
00692                 n = 127;
00693         }
00694 
00695         self->remote_credit = n;
00696 
00697         /* SAR enabled? */
00698         if (max_sdu_size > 0) {
00699                 ASSERT(skb_headroom(skb) >= (TTP_MAX_HEADER + TTP_SAR_HEADER), 
00700                        return -1;);
00701 
00702                 /* Insert SAR parameters */
00703                 frame = skb_push(skb, TTP_HEADER+TTP_SAR_HEADER);
00704                 
00705                 frame[0] = TTP_PARAMETERS | n; 
00706                 frame[1] = 0x04; /* Length */
00707                 frame[2] = 0x01; /* MaxSduSize */
00708                 frame[3] = 0x02; /* Value length */
00709 
00710                 put_unaligned(cpu_to_be16((__u16) max_sdu_size), 
00711                               (__u16 *)(frame+4));
00712         } else {
00713                 /* Insert plain TTP header */
00714                 frame = skb_push(skb, TTP_HEADER);
00715                 
00716                 /* Insert initial credit in frame */
00717                 frame[0] = n & 0x7f;
00718         }
00719 
00720         /* Connect with IrLMP. No QoS parameters for now */
00721         return irlmp_connect_request(self->lsap, dtsap_sel, saddr, daddr, qos, 
00722                                      skb);
00723 }
00724 
00725 /*
00726  * Function irttp_connect_confirm (handle, qos, skb)
00727  *
00728  *    Sevice user confirms TSAP connection with peer. 
00729  *
00730  */
00731 static void irttp_connect_confirm(void *instance, void *sap, 
00732                                   struct qos_info *qos, __u32 max_seg_size,
00733                                   __u8 max_header_size, struct sk_buff *skb) 
00734 {
00735         struct tsap_cb *self;
00736         int parameters;
00737         int ret;
00738         __u8 plen;
00739         __u8 n;
00740 
00741         IRDA_DEBUG(4, __FUNCTION__ "()\n");
00742         
00743         self = (struct tsap_cb *) instance;
00744 
00745         ASSERT(self != NULL, return;);
00746         ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
00747         ASSERT(skb != NULL, return;);
00748 
00749         self->max_seg_size = max_seg_size - TTP_HEADER;
00750         self->max_header_size = max_header_size + TTP_HEADER;
00751 
00752         /*
00753          *  Check if we have got some QoS parameters back! This should be the
00754          *  negotiated QoS for the link.
00755          */
00756         if (qos) {
00757                 IRDA_DEBUG(4, "IrTTP, Negotiated BAUD_RATE: %02x\n", 
00758                        qos->baud_rate.bits);                    
00759                 IRDA_DEBUG(4, "IrTTP, Negotiated BAUD_RATE: %d bps.\n", 
00760                        qos->baud_rate.value);
00761         }
00762 
00763         n = skb->data[0] & 0x7f;
00764         
00765         IRDA_DEBUG(4, __FUNCTION__ "(), Initial send_credit=%d\n", n);
00766         
00767         self->send_credit = n;
00768         self->tx_max_sdu_size = 0;
00769         self->connected = TRUE;
00770 
00771         parameters = skb->data[0] & 0x80;       
00772 
00773         ASSERT(skb->len >= TTP_HEADER, return;);
00774         skb_pull(skb, TTP_HEADER);
00775 
00776         if (parameters) {
00777                 plen = skb->data[0];
00778 
00779                 ret = irda_param_extract_all(self, skb->data+1,
00780                                              IRDA_MIN(skb->len-1, plen), 
00781                                              &param_info);
00782 
00783                 /* Any errors in the parameter list? */
00784                 if (ret < 0) {
00785                         WARNING(__FUNCTION__ 
00786                                 "(), error extracting parameters\n");
00787                         dev_kfree_skb(skb);
00788 
00789                         /* Do not accept this connection attempt */
00790                         return;
00791                 }
00792                 /* Remove parameters */
00793                 skb_pull(skb, IRDA_MIN(skb->len, plen+1));
00794         }
00795         
00796         IRDA_DEBUG(4, __FUNCTION__ "() send=%d,avail=%d,remote=%d\n", 
00797               self->send_credit, self->avail_credit, self->remote_credit);
00798 
00799         IRDA_DEBUG(2, __FUNCTION__ "(), MaxSduSize=%d\n", self->tx_max_sdu_size);
00800 
00801         if (self->notify.connect_confirm) {
00802                 self->notify.connect_confirm(self->notify.instance, self, qos,
00803                                              self->tx_max_sdu_size,
00804                                              self->max_header_size, skb);
00805         }
00806 }
00807 
00808 /*
00809  * Function irttp_connect_indication (handle, skb)
00810  *
00811  *    Some other device is connecting to this TSAP
00812  *
00813  */
00814 void irttp_connect_indication(void *instance, void *sap, struct qos_info *qos,
00815                               __u32 max_seg_size, __u8 max_header_size, 
00816                               struct sk_buff *skb) 
00817 {
00818         struct tsap_cb *self;
00819         struct lsap_cb *lsap;
00820         int parameters;
00821         int ret;
00822         __u8 plen;
00823         __u8 n;
00824 
00825         self = (struct tsap_cb *) instance;
00826 
00827         ASSERT(self != NULL, return;);
00828         ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
00829         ASSERT(skb != NULL, return;);
00830 
00831         lsap = (struct lsap_cb *) sap;
00832 
00833         self->max_seg_size = max_seg_size - TTP_HEADER;;
00834         self->max_header_size = max_header_size+TTP_HEADER;
00835 
00836         IRDA_DEBUG(4, __FUNCTION__ "(), TSAP sel=%02x\n", self->stsap_sel);
00837 
00838         /* Need to update dtsap_sel if its equal to LSAP_ANY */
00839         self->dtsap_sel = lsap->dlsap_sel;
00840 
00841         n = skb->data[0] & 0x7f;
00842 
00843         self->send_credit = n;
00844         self->tx_max_sdu_size = 0;
00845         
00846         parameters = skb->data[0] & 0x80;
00847 
00848         ASSERT(skb->len >= TTP_HEADER, return;);
00849         skb_pull(skb, TTP_HEADER);
00850 
00851         if (parameters) {
00852                 plen = skb->data[0];
00853                 
00854                 ret = irda_param_extract_all(self, skb->data+1,
00855                                              IRDA_MIN(skb->len-1, plen), 
00856                                              &param_info);
00857 
00858                 /* Any errors in the parameter list? */
00859                 if (ret < 0) {
00860                         WARNING(__FUNCTION__ 
00861                                 "(), error extracting parameters\n");
00862                         dev_kfree_skb(skb);
00863                         
00864                         /* Do not accept this connection attempt */
00865                         return;
00866                 }
00867 
00868                 /* Remove parameters */
00869                 skb_pull(skb, IRDA_MIN(skb->len, plen+1));
00870         }
00871 
00872         if (self->notify.connect_indication) {
00873                 self->notify.connect_indication(self->notify.instance, self, 
00874                                                 qos, self->tx_max_sdu_size, 
00875                                                 self->max_header_size, skb);
00876         } else
00877                 dev_kfree_skb(skb);
00878 }
00879 
00880 /*
00881  * Function irttp_connect_response (handle, userdata)
00882  *
00883  *    Service user is accepting the connection, just pass it down to
00884  *    IrLMP!
00885  * 
00886  */
00887 int irttp_connect_response(struct tsap_cb *self, __u32 max_sdu_size, 
00888                            struct sk_buff *userdata)
00889 {
00890         struct sk_buff *skb;
00891         __u8 *frame;
00892         int ret;
00893         __u8 n;
00894 
00895         ASSERT(self != NULL, return -1;);
00896         ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
00897 
00898         IRDA_DEBUG(4, __FUNCTION__ "(), Source TSAP selector=%02x\n", 
00899                    self->stsap_sel);
00900         
00901         /* Any userdata supplied? */
00902         if (userdata == NULL) {
00903                 skb = dev_alloc_skb(64);
00904                 if (!skb)
00905                         return -ENOMEM;
00906 
00907                 /* Reserve space for MUX_CONTROL and LAP header */
00908                 skb_reserve(skb, TTP_MAX_HEADER);
00909         } else {
00910                 skb = userdata;
00911                 /*  
00912                  *  Check that the client has reserved enough space for 
00913                  *  headers
00914                  */
00915                 ASSERT(skb_headroom(skb) >= TTP_MAX_HEADER, return -1;);
00916         }
00917         
00918         self->avail_credit = 0;
00919         self->remote_credit = 0;
00920         self->rx_max_sdu_size = max_sdu_size;
00921         self->rx_sdu_size = 0;
00922         self->rx_sdu_busy = FALSE;
00923 
00924         n = self->initial_credit;
00925 
00926         /* Frame has only space for max 127 credits (7 bits) */
00927         if (n > 127) {
00928                 self->avail_credit = n - 127;
00929                 n = 127;
00930         }
00931 
00932         self->remote_credit = n;
00933         self->connected = TRUE;
00934 
00935         /* SAR enabled? */
00936         if (max_sdu_size > 0) {
00937                 ASSERT(skb_headroom(skb) >= (TTP_MAX_HEADER+TTP_SAR_HEADER), 
00938                        return -1;);
00939                 
00940                 /* Insert TTP header with SAR parameters */
00941                 frame = skb_push(skb, TTP_HEADER+TTP_SAR_HEADER);
00942                 
00943                 frame[0] = TTP_PARAMETERS | n;
00944                 frame[1] = 0x04; /* Length */
00945 
00946                 /* irda_param_insert(self, IRTTP_MAX_SDU_SIZE, frame+1,  */
00947 /*                                TTP_SAR_HEADER, &param_info) */
00948                 
00949                 frame[2] = 0x01; /* MaxSduSize */
00950                 frame[3] = 0x02; /* Value length */
00951 
00952                 put_unaligned(cpu_to_be16((__u16) max_sdu_size), 
00953                               (__u16 *)(frame+4));
00954         } else {
00955                 /* Insert TTP header */
00956                 frame = skb_push(skb, TTP_HEADER);
00957                 
00958                 frame[0] = n & 0x7f;
00959         }
00960          
00961         ret = irlmp_connect_response(self->lsap, skb);
00962 
00963         return ret;
00964 }
00965 
00966 /*
00967  * Function irttp_dup (self, instance)
00968  *
00969  *    Duplicate TSAP, can be used by servers to confirm a connection on a
00970  *    new TSAP so it can keep listening on the old one.
00971  */
00972 struct tsap_cb *irttp_dup(struct tsap_cb *orig, void *instance) 
00973 {
00974         struct tsap_cb *new;
00975 
00976         IRDA_DEBUG(1, __FUNCTION__ "()\n");
00977 
00978         if (!hashbin_find(irttp->tsaps, (int) orig, NULL)) {
00979                 IRDA_DEBUG(0, __FUNCTION__ "(), unable to find TSAP\n");
00980                 return NULL;
00981         }
00982         new = kmalloc(sizeof(struct tsap_cb), GFP_ATOMIC);
00983         if (!new) {
00984                 IRDA_DEBUG(0, __FUNCTION__ "(), unable to kmalloc\n");
00985                 return NULL;
00986         }
00987         /* Dup */
00988         memcpy(new, orig, sizeof(struct tsap_cb));
00989         new->notify.instance = instance;
00990         new->lsap = irlmp_dup(orig->lsap, new);
00991 
00992         /* Not everything should be copied */
00993         init_timer(&new->todo_timer);
00994 
00995         skb_queue_head_init(&new->rx_queue);
00996         skb_queue_head_init(&new->tx_queue);
00997         skb_queue_head_init(&new->rx_fragments);
00998 
00999         hashbin_insert(irttp->tsaps, (queue_t *) new, (int) new, NULL);
01000 
01001         return new;
01002 }
01003 
01004 /*
01005  * Function irttp_disconnect_request (self)
01006  *
01007  *    Close this connection please! If priority is high, the queued data 
01008  *    segments, if any, will be deallocated first
01009  *
01010  */
01011 int irttp_disconnect_request(struct tsap_cb *self, struct sk_buff *userdata, 
01012                              int priority)
01013 {
01014         struct sk_buff *skb;
01015         int ret;
01016 
01017         ASSERT(self != NULL, return -1;);
01018         ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
01019 
01020         /* Already disconnected? */
01021         if (!self->connected) {
01022                 IRDA_DEBUG(4, __FUNCTION__ "(), already disconnected!\n");
01023                 return -1;
01024         }
01025 
01026         /* Disconnect already pending? */
01027         if (self->disconnect_pend) {
01028                 IRDA_DEBUG(1, __FUNCTION__ "(), disconnect already pending\n");
01029                 if (userdata) {
01030                         dev_kfree_skb(userdata);
01031                 }
01032 
01033                 /* Try to make some progress */
01034                 irttp_run_rx_queue(self);
01035                 return -1;
01036         }
01037 
01038         /*
01039          *  Check if there is still data segments in the transmit queue
01040          */
01041         if (skb_queue_len(&self->tx_queue) > 0) {
01042                 if (priority == P_HIGH) {
01043                         IRDA_DEBUG(1, __FUNCTION__  "High priority!!()\n" );
01044                         
01045                         /* 
01046                          *  No need to send the queued data, if we are 
01047                          *  disconnecting right now since the data will
01048                          *  not have any usable connection to be sent on
01049                          */
01050                         irttp_flush_queues(self);
01051                 } else if (priority == P_NORMAL) {
01052                         /* 
01053                          *  Must delay disconnect til after all data segments
01054                          *  have been sent an the tx_queue is empty
01055                          */
01056                         if (userdata)
01057                                 self->disconnect_skb = userdata;
01058                         else
01059                                 self->disconnect_skb = NULL;
01060 
01061                         self->disconnect_pend = TRUE;
01062 
01063                         irttp_run_tx_queue(self);
01064 
01065                         irttp_start_todo_timer(self, MSECS_TO_JIFFIES(1000));
01066                         return -1;
01067                 }
01068         }
01069         IRDA_DEBUG(1, __FUNCTION__ "(), Disconnecting ...\n");
01070 
01071         self->connected = FALSE;
01072         
01073         if (!userdata) {
01074                 skb = dev_alloc_skb(64);
01075                 if (!skb)
01076                         return -ENOMEM;
01077                 
01078                 /* 
01079                  *  Reserve space for MUX and LAP header 
01080                  */
01081                 skb_reserve(skb, TTP_MAX_HEADER);
01082                 
01083                 userdata = skb;
01084         }
01085         ret = irlmp_disconnect_request(self->lsap, userdata);
01086 
01087         return ret;
01088 }
01089 
01090 /*
01091  * Function irttp_disconnect_indication (self, reason)
01092  *
01093  *    Disconnect indication, TSAP disconnected by peer?
01094  *
01095  */
01096 void irttp_disconnect_indication(void *instance, void *sap, LM_REASON reason, 
01097                                  struct sk_buff *skb) 
01098 {
01099         struct tsap_cb *self;
01100 
01101         IRDA_DEBUG(4, __FUNCTION__ "()\n");
01102 
01103         self = (struct tsap_cb *) instance;
01104         
01105         ASSERT(self != NULL, return;);
01106         ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
01107         
01108         self->connected = FALSE;
01109         
01110         /* Check if client has already tried to close the TSAP */
01111         if (self->close_pend) {
01112                 irttp_close_tsap(self);
01113                 return;
01114         }
01115 
01116         /* No need to notify the client if has already tried to disconnect */
01117         if (self->disconnect_pend)
01118                 return;
01119         
01120         if (self->notify.disconnect_indication)
01121                 self->notify.disconnect_indication(self->notify.instance, self,
01122                                                    reason, skb);
01123         else
01124                 if (skb)
01125                         dev_kfree_skb(skb);
01126 }
01127 
01128 /*
01129  * Function irttp_do_data_indication (self, skb)
01130  *
01131  *    Try to deliver reassebled skb to layer above, and requeue it if that
01132  *    for some reason should fail. We mark rx sdu as busy to apply back
01133  *    pressure is necessary.
01134  */
01135 void irttp_do_data_indication(struct tsap_cb *self, struct sk_buff *skb)
01136 {
01137         int err;
01138 
01139         /* Check if client has already tried to close the TSAP */
01140         if (self->close_pend || self->disconnect_pend) {
01141                 dev_kfree_skb(skb);
01142                 return;
01143         }
01144 
01145         err = self->notify.data_indication(self->notify.instance, self, skb);
01146 
01147         /* Usually the layer above will notify that it's input queue is
01148          * starting to get filled by using the flow request, but this may
01149          * be difficult, so it can instead just refuse to eat it and just
01150          * give an error back 
01151          */
01152         if (err == -ENOMEM) {
01153                 IRDA_DEBUG(0, __FUNCTION__ "() requeueing skb!\n");
01154 
01155                 /* Make sure we take a break */
01156                 self->rx_sdu_busy = TRUE;
01157                 
01158                 /* Need to push the header in again */
01159                 skb_push(skb, TTP_HEADER);
01160                 skb->data[0] = 0x00; /* Make sure MORE bit is cleared */
01161                 
01162                 /* Put skb back on queue */
01163                 skb_queue_head(&self->rx_queue, skb);
01164         }
01165 }
01166 
01167 /*
01168  * Function irttp_run_rx_queue (self)
01169  *
01170  *     Check if we have any frames to be transmitted, or if we have any
01171  *     available credit to give away.
01172  */
01173 void irttp_run_rx_queue(struct tsap_cb *self) 
01174 {
01175         struct sk_buff *skb;
01176         int more = 0;
01177 
01178         IRDA_DEBUG(2, __FUNCTION__ "() send=%d,avail=%d,remote=%d\n", 
01179                    self->send_credit, self->avail_credit, self->remote_credit);
01180 
01181         if (irda_lock(&self->rx_queue_lock) == FALSE)
01182                 return;
01183         
01184         /*
01185          *  Reassemble all frames in receive queue and deliver them
01186          */
01187         while (!self->rx_sdu_busy && (skb = skb_dequeue(&self->rx_queue))) {
01188                 self->avail_credit++;
01189 
01190                 more = skb->data[0] & 0x80;
01191 
01192                 /* Remove TTP header */
01193                 skb_pull(skb, TTP_HEADER);
01194 
01195                 /* Add the length of the remaining data */
01196                 self->rx_sdu_size += skb->len;
01197 
01198                 /*  
01199                  * If SAR is disabled, or user has requested no reassembly
01200                  * of received fragements then we just deliver them
01201                  * immediately. This can be requested by clients that
01202                  * implements byte streams without any message boundaries
01203                  */
01204                 if (self->rx_max_sdu_size == TTP_SAR_DISABLE) {
01205                         irttp_do_data_indication(self, skb);
01206                         self->rx_sdu_size = 0;
01207 
01208                         continue;
01209                 }
01210 
01211                 /* Check if this is a fragment, and not the last fragment */
01212                 if (more) {
01213                         /*  
01214                          *  Queue the fragment if we still are within the 
01215                          *  limits of the maximum size of the rx_sdu
01216                          */
01217                         if (self->rx_sdu_size <= self->rx_max_sdu_size) {
01218                                 IRDA_DEBUG(4, __FUNCTION__ "(), queueing frag\n");
01219                                 skb_queue_tail(&self->rx_fragments, skb);
01220                         } else {
01221                                 /* Free the part of the SDU that is too big */
01222                                 dev_kfree_skb(skb);
01223                         }
01224                         continue;
01225                 }
01226                 /*
01227                  *  This is the last fragment, so time to reassemble!
01228                  */
01229                 if ((self->rx_sdu_size <= self->rx_max_sdu_size) ||
01230                     (self->rx_max_sdu_size == TTP_SAR_UNBOUND)) 
01231                 {
01232                         /* 
01233                          * A little optimizing. Only queue the fragment if
01234                          * there are other fragments. Since if this is the
01235                          * last and only fragment, there is no need to
01236                          * reassemble :-) 
01237                          */
01238                         if (!skb_queue_empty(&self->rx_fragments)) {
01239                                 skb_queue_tail(&self->rx_fragments, 
01240                                                skb);
01241                                 
01242                                 skb = irttp_reassemble_skb(self);
01243                         }
01244                         
01245                         /* Now we can deliver the reassembled skb */
01246                         irttp_do_data_indication(self, skb);
01247                 } else {
01248                         IRDA_DEBUG(1, __FUNCTION__ "(), Truncated frame\n");
01249                         
01250                         /* Free the part of the SDU that is too big */
01251                         dev_kfree_skb(skb);
01252 
01253                         /* Deliver only the valid but truncated part of SDU */
01254                         skb = irttp_reassemble_skb(self);
01255                         
01256                         irttp_do_data_indication(self, skb);
01257                 }
01258                 self->rx_sdu_size = 0;
01259         }
01260         /* Reset lock */
01261         self->rx_queue_lock = 0;
01262 }
01263 
01264 /*
01265  * Function irttp_flush_queues (self)
01266  *
01267  *     Flushes (removes all frames) in transitt-buffer (tx_list)
01268  */
01269 void irttp_flush_queues(struct tsap_cb *self)
01270 {
01271         struct sk_buff* skb;
01272         
01273         IRDA_DEBUG(4, __FUNCTION__ "()\n");
01274 
01275         ASSERT(self != NULL, return;);
01276         ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
01277         
01278         /* Deallocate frames waiting to be sent */
01279         while ((skb = skb_dequeue(&self->tx_queue)) != NULL)
01280                 dev_kfree_skb(skb);
01281         
01282         /* Deallocate received frames */
01283         while ((skb = skb_dequeue(&self->rx_queue)) != NULL)
01284                 dev_kfree_skb(skb);
01285         
01286         /* Deallocate received fragments */
01287         while ((skb = skb_dequeue(&self->rx_fragments)) != NULL)
01288                 dev_kfree_skb(skb);
01289 }
01290 
01291 /*
01292  * Function irttp_reasseble (self)
01293  *
01294  *    Makes a new (continuous) skb of all the fragments in the fragment
01295  *    queue
01296  *
01297  */
01298 static struct sk_buff *irttp_reassemble_skb(struct tsap_cb *self)
01299 {
01300         struct sk_buff *skb, *frag;
01301         int n = 0;  /* Fragment index */
01302         
01303         ASSERT(self != NULL, return NULL;);
01304         ASSERT(self->magic == TTP_TSAP_MAGIC, return NULL;);
01305 
01306         IRDA_DEBUG(2, __FUNCTION__ "(), self->rx_sdu_size=%d\n", 
01307                    self->rx_sdu_size);
01308 
01309         skb = dev_alloc_skb(TTP_HEADER + self->rx_sdu_size);
01310         if (!skb)
01311                 return NULL;
01312 
01313         /* 
01314          * Need to reserve space for TTP header in case this skb needs to 
01315          * be requeued in case delivery failes
01316          */
01317         skb_reserve(skb, TTP_HEADER);
01318         skb_put(skb, self->rx_sdu_size);
01319 
01320         /*
01321          *  Copy all fragments to a new buffer
01322          */
01323         while ((frag = skb_dequeue(&self->rx_fragments)) != NULL) {
01324                 memcpy(skb->data+n, frag->data, frag->len);
01325                 n += frag->len;
01326                 
01327                 dev_kfree_skb(frag);
01328         }
01329         IRDA_DEBUG(2, __FUNCTION__ "(), frame len=%d\n", n);
01330 
01331         IRDA_DEBUG(2, __FUNCTION__ "(), rx_sdu_size=%d\n", self->rx_sdu_size);
01332         ASSERT(n <= self->rx_sdu_size, return NULL;);
01333 
01334         /* Set the new length */
01335         skb_trim(skb, n);
01336 
01337         self->rx_sdu_size = 0;
01338 
01339         return skb;
01340 }
01341 
01342 /*
01343  * Function irttp_fragment_skb (skb)
01344  *
01345  *    Fragments a frame and queues all the fragments for transmission
01346  *
01347  */
01348 static void irttp_fragment_skb(struct tsap_cb *self, struct sk_buff *skb)
01349 {
01350         struct sk_buff *frag;
01351         __u8 *frame;
01352 
01353         IRDA_DEBUG(2, __FUNCTION__ "()\n");
01354 
01355         ASSERT(self != NULL, return;);
01356         ASSERT(self->magic == TTP_TSAP_MAGIC, return;);
01357         ASSERT(skb != NULL, return;);
01358 
01359         /*
01360          *  Split frame into a number of segments
01361          */
01362         while (skb->len > self->max_seg_size) {
01363                 IRDA_DEBUG(2, __FUNCTION__  "(), fragmenting ...\n");
01364 
01365                 /* Make new segment */
01366                 frag = dev_alloc_skb(self->max_seg_size+self->max_header_size);
01367                 if (!frag)
01368                         return;
01369 
01370                 skb_reserve(frag, self->max_header_size);
01371 
01372                 /* Copy data from the original skb into this fragment. */
01373                 memcpy(skb_put(frag, self->max_seg_size), skb->data, 
01374                        self->max_seg_size);
01375 
01376                 /* Insert TTP header, with the more bit set */
01377                 frame = skb_push(frag, TTP_HEADER);
01378                 frame[0] = TTP_MORE;
01379                 
01380                 /* Hide the copied data from the original skb */
01381                 skb_pull(skb, self->max_seg_size);
01382 
01383                 /* Queue fragment */
01384                 skb_queue_tail(&self->tx_queue, frag);
01385         }
01386         /* Queue what is left of the original skb */
01387         IRDA_DEBUG(2, __FUNCTION__  "(), queuing last segment\n");
01388         
01389         frame = skb_push(skb, TTP_HEADER);
01390         frame[0] = 0x00; /* Clear more bit */
01391 
01392         /* Queue fragment */
01393         skb_queue_tail(&self->tx_queue, skb);
01394 }
01395 
01396 /*
01397  * Function irttp_param_max_sdu_size (self, param)
01398  *
01399  *    Handle the MaxSduSize parameter in the connect frames, this function
01400  *    will be called both when this parameter needs to be inserted into, and
01401  *    extracted from the connect frames
01402  */
01403 static int irttp_param_max_sdu_size(void *instance, irda_param_t *param, 
01404                                     int get)
01405 {
01406         struct tsap_cb *self;
01407 
01408         self = (struct tsap_cb *) instance;
01409 
01410         ASSERT(self != NULL, return -1;);
01411         ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);
01412 
01413         if (get)
01414                 param->pv.i = self->tx_max_sdu_size;
01415         else
01416                 self->tx_max_sdu_size = param->pv.i;
01417 
01418         IRDA_DEBUG(0, __FUNCTION__ "(), MaxSduSize=%d\n", param->pv.i);
01419         
01420         return 0;
01421 }
01422 
01423 /*
01424  * Function irttp_todo_expired (data)
01425  *
01426  *    Todo timer has expired!
01427  *
01428  */
01429 static void irttp_todo_expired(unsigned long data)
01430 {
01431         struct tsap_cb *self = (struct tsap_cb *) data;
01432 
01433         /* Check that we still exist */
01434         if (!self || self->magic != TTP_TSAP_MAGIC)
01435                 return;
01436         
01437         irttp_run_rx_queue(self);
01438         irttp_run_tx_queue(self);
01439                 
01440         /*  Give avay some credits to peer?  */
01441         if ((self->remote_credit < TTP_LOW_THRESHOLD) && 
01442             (self->avail_credit > 0) && (skb_queue_empty(&self->tx_queue)))
01443         {
01444                 irttp_give_credit(self);
01445         }
01446 
01447         /* Check if time for disconnect */
01448         if (self->disconnect_pend) {
01449                 /* Check if it's possible to disconnect yet */
01450                 if (skb_queue_empty(&self->tx_queue)) {
01451                         
01452                         /* Make sure disconnect is not pending anymore */
01453                         self->disconnect_pend = FALSE;
01454                         if (self->disconnect_skb) {
01455                                 irttp_disconnect_request(
01456                                         self, self->disconnect_skb, P_NORMAL);
01457                                 self->disconnect_skb = NULL;
01458                         } else
01459                                 irttp_disconnect_request(self, NULL,