/*********************************************************************1*2* Filename: irttp.c3* Version: 1.24* Description: Tiny Transport Protocol (TTP) implementation5* Status: Stable6* Author: Dag Brattli <[email protected]>7* Created at: Sun Aug 31 20:14:31 19978* Modified at: Wed Jan 5 11:31:27 20009* Modified by: Dag Brattli <[email protected]>10*11* Copyright (c) 1998-2000 Dag Brattli <[email protected]>,12* All Rights Reserved.13* Copyright (c) 2000-2003 Jean Tourrilhes <[email protected]>14*15* This program is free software; you can redistribute it and/or16* modify it under the terms of the GNU General Public License as17* published by the Free Software Foundation; either version 2 of18* the License, or (at your option) any later version.19*20* Neither Dag Brattli nor University of Tromsø admit liability nor21* provide warranty for any of this software. This material is22* provided "AS-IS" and at no charge.23*24********************************************************************/2526#include <linux/skbuff.h>27#include <linux/init.h>28#include <linux/fs.h>29#include <linux/seq_file.h>30#include <linux/slab.h>3132#include <asm/byteorder.h>33#include <asm/unaligned.h>3435#include <net/irda/irda.h>36#include <net/irda/irlap.h>37#include <net/irda/irlmp.h>38#include <net/irda/parameters.h>39#include <net/irda/irttp.h>4041static struct irttp_cb *irttp;4243static void __irttp_close_tsap(struct tsap_cb *self);4445static int irttp_data_indication(void *instance, void *sap,46struct sk_buff *skb);47static int irttp_udata_indication(void *instance, void *sap,48struct sk_buff *skb);49static void irttp_disconnect_indication(void *instance, void *sap,50LM_REASON reason, struct sk_buff *);51static void irttp_connect_indication(void *instance, void *sap,52struct qos_info *qos, __u32 max_sdu_size,53__u8 header_size, struct sk_buff *skb);54static void irttp_connect_confirm(void *instance, void *sap,55struct qos_info *qos, __u32 max_sdu_size,56__u8 header_size, struct sk_buff *skb);57static void irttp_run_tx_queue(struct tsap_cb *self);58static void irttp_run_rx_queue(struct tsap_cb *self);5960static void irttp_flush_queues(struct tsap_cb *self);61static void irttp_fragment_skb(struct tsap_cb *self, struct sk_buff *skb);62static struct sk_buff *irttp_reassemble_skb(struct tsap_cb *self);63static void irttp_todo_expired(unsigned long data);64static int irttp_param_max_sdu_size(void *instance, irda_param_t *param,65int get);6667static void irttp_flow_indication(void *instance, void *sap, LOCAL_FLOW flow);68static void irttp_status_indication(void *instance,69LINK_STATUS link, LOCK_STATUS lock);7071/* Information for parsing parameters in IrTTP */72static pi_minor_info_t pi_minor_call_table[] = {73{ NULL, 0 }, /* 0x00 */74{ irttp_param_max_sdu_size, PV_INTEGER | PV_BIG_ENDIAN } /* 0x01 */75};76static pi_major_info_t pi_major_call_table[] = {{ pi_minor_call_table, 2 }};77static pi_param_info_t param_info = { pi_major_call_table, 1, 0x0f, 4 };7879/************************ GLOBAL PROCEDURES ************************/8081/*82* Function irttp_init (void)83*84* Initialize the IrTTP layer. Called by module initialization code85*86*/87int __init irttp_init(void)88{89irttp = kzalloc(sizeof(struct irttp_cb), GFP_KERNEL);90if (irttp == NULL)91return -ENOMEM;9293irttp->magic = TTP_MAGIC;9495irttp->tsaps = hashbin_new(HB_LOCK);96if (!irttp->tsaps) {97IRDA_ERROR("%s: can't allocate IrTTP hashbin!\n",98__func__);99kfree(irttp);100return -ENOMEM;101}102103return 0;104}105106/*107* Function irttp_cleanup (void)108*109* Called by module destruction/cleanup code110*111*/112void irttp_cleanup(void)113{114/* Check for main structure */115IRDA_ASSERT(irttp->magic == TTP_MAGIC, return;);116117/*118* Delete hashbin and close all TSAP instances in it119*/120hashbin_delete(irttp->tsaps, (FREE_FUNC) __irttp_close_tsap);121122irttp->magic = 0;123124/* De-allocate main structure */125kfree(irttp);126127irttp = NULL;128}129130/*************************** SUBROUTINES ***************************/131132/*133* Function irttp_start_todo_timer (self, timeout)134*135* Start todo timer.136*137* Made it more effient and unsensitive to race conditions - Jean II138*/139static inline void irttp_start_todo_timer(struct tsap_cb *self, int timeout)140{141/* Set new value for timer */142mod_timer(&self->todo_timer, jiffies + timeout);143}144145/*146* Function irttp_todo_expired (data)147*148* Todo timer has expired!149*150* One of the restriction of the timer is that it is run only on the timer151* interrupt which run every 10ms. This mean that even if you set the timer152* with a delay of 0, it may take up to 10ms before it's run.153* So, to minimise latency and keep cache fresh, we try to avoid using154* it as much as possible.155* Note : we can't use tasklets, because they can't be asynchronously156* killed (need user context), and we can't guarantee that here...157* Jean II158*/159static void irttp_todo_expired(unsigned long data)160{161struct tsap_cb *self = (struct tsap_cb *) data;162163/* Check that we still exist */164if (!self || self->magic != TTP_TSAP_MAGIC)165return;166167IRDA_DEBUG(4, "%s(instance=%p)\n", __func__, self);168169/* Try to make some progress, especially on Tx side - Jean II */170irttp_run_rx_queue(self);171irttp_run_tx_queue(self);172173/* Check if time for disconnect */174if (test_bit(0, &self->disconnect_pend)) {175/* Check if it's possible to disconnect yet */176if (skb_queue_empty(&self->tx_queue)) {177/* Make sure disconnect is not pending anymore */178clear_bit(0, &self->disconnect_pend); /* FALSE */179180/* Note : self->disconnect_skb may be NULL */181irttp_disconnect_request(self, self->disconnect_skb,182P_NORMAL);183self->disconnect_skb = NULL;184} else {185/* Try again later */186irttp_start_todo_timer(self, HZ/10);187188/* No reason to try and close now */189return;190}191}192193/* Check if it's closing time */194if (self->close_pend)195/* Finish cleanup */196irttp_close_tsap(self);197}198199/*200* Function irttp_flush_queues (self)201*202* Flushes (removes all frames) in transitt-buffer (tx_list)203*/204static void irttp_flush_queues(struct tsap_cb *self)205{206struct sk_buff* skb;207208IRDA_DEBUG(4, "%s()\n", __func__);209210IRDA_ASSERT(self != NULL, return;);211IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);212213/* Deallocate frames waiting to be sent */214while ((skb = skb_dequeue(&self->tx_queue)) != NULL)215dev_kfree_skb(skb);216217/* Deallocate received frames */218while ((skb = skb_dequeue(&self->rx_queue)) != NULL)219dev_kfree_skb(skb);220221/* Deallocate received fragments */222while ((skb = skb_dequeue(&self->rx_fragments)) != NULL)223dev_kfree_skb(skb);224}225226/*227* Function irttp_reassemble (self)228*229* Makes a new (continuous) skb of all the fragments in the fragment230* queue231*232*/233static struct sk_buff *irttp_reassemble_skb(struct tsap_cb *self)234{235struct sk_buff *skb, *frag;236int n = 0; /* Fragment index */237238IRDA_ASSERT(self != NULL, return NULL;);239IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return NULL;);240241IRDA_DEBUG(2, "%s(), self->rx_sdu_size=%d\n", __func__,242self->rx_sdu_size);243244skb = dev_alloc_skb(TTP_HEADER + self->rx_sdu_size);245if (!skb)246return NULL;247248/*249* Need to reserve space for TTP header in case this skb needs to250* be requeued in case delivery failes251*/252skb_reserve(skb, TTP_HEADER);253skb_put(skb, self->rx_sdu_size);254255/*256* Copy all fragments to a new buffer257*/258while ((frag = skb_dequeue(&self->rx_fragments)) != NULL) {259skb_copy_to_linear_data_offset(skb, n, frag->data, frag->len);260n += frag->len;261262dev_kfree_skb(frag);263}264265IRDA_DEBUG(2,266"%s(), frame len=%d, rx_sdu_size=%d, rx_max_sdu_size=%d\n",267__func__, n, self->rx_sdu_size, self->rx_max_sdu_size);268/* Note : irttp_run_rx_queue() calculate self->rx_sdu_size269* by summing the size of all fragments, so we should always270* have n == self->rx_sdu_size, except in cases where we271* droped the last fragment (when self->rx_sdu_size exceed272* self->rx_max_sdu_size), where n < self->rx_sdu_size.273* Jean II */274IRDA_ASSERT(n <= self->rx_sdu_size, n = self->rx_sdu_size;);275276/* Set the new length */277skb_trim(skb, n);278279self->rx_sdu_size = 0;280281return skb;282}283284/*285* Function irttp_fragment_skb (skb)286*287* Fragments a frame and queues all the fragments for transmission288*289*/290static inline void irttp_fragment_skb(struct tsap_cb *self,291struct sk_buff *skb)292{293struct sk_buff *frag;294__u8 *frame;295296IRDA_DEBUG(2, "%s()\n", __func__);297298IRDA_ASSERT(self != NULL, return;);299IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);300IRDA_ASSERT(skb != NULL, return;);301302/*303* Split frame into a number of segments304*/305while (skb->len > self->max_seg_size) {306IRDA_DEBUG(2, "%s(), fragmenting ...\n", __func__);307308/* Make new segment */309frag = alloc_skb(self->max_seg_size+self->max_header_size,310GFP_ATOMIC);311if (!frag)312return;313314skb_reserve(frag, self->max_header_size);315316/* Copy data from the original skb into this fragment. */317skb_copy_from_linear_data(skb, skb_put(frag, self->max_seg_size),318self->max_seg_size);319320/* Insert TTP header, with the more bit set */321frame = skb_push(frag, TTP_HEADER);322frame[0] = TTP_MORE;323324/* Hide the copied data from the original skb */325skb_pull(skb, self->max_seg_size);326327/* Queue fragment */328skb_queue_tail(&self->tx_queue, frag);329}330/* Queue what is left of the original skb */331IRDA_DEBUG(2, "%s(), queuing last segment\n", __func__);332333frame = skb_push(skb, TTP_HEADER);334frame[0] = 0x00; /* Clear more bit */335336/* Queue fragment */337skb_queue_tail(&self->tx_queue, skb);338}339340/*341* Function irttp_param_max_sdu_size (self, param)342*343* Handle the MaxSduSize parameter in the connect frames, this function344* will be called both when this parameter needs to be inserted into, and345* extracted from the connect frames346*/347static int irttp_param_max_sdu_size(void *instance, irda_param_t *param,348int get)349{350struct tsap_cb *self;351352self = (struct tsap_cb *) instance;353354IRDA_ASSERT(self != NULL, return -1;);355IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);356357if (get)358param->pv.i = self->tx_max_sdu_size;359else360self->tx_max_sdu_size = param->pv.i;361362IRDA_DEBUG(1, "%s(), MaxSduSize=%d\n", __func__, param->pv.i);363364return 0;365}366367/*************************** CLIENT CALLS ***************************/368/************************** LMP CALLBACKS **************************/369/* Everything is happily mixed up. Waiting for next clean up - Jean II */370371/*372* Initialization, that has to be done on new tsap373* instance allocation and on duplication374*/375static void irttp_init_tsap(struct tsap_cb *tsap)376{377spin_lock_init(&tsap->lock);378init_timer(&tsap->todo_timer);379380skb_queue_head_init(&tsap->rx_queue);381skb_queue_head_init(&tsap->tx_queue);382skb_queue_head_init(&tsap->rx_fragments);383}384385/*386* Function irttp_open_tsap (stsap, notify)387*388* Create TSAP connection endpoint,389*/390struct tsap_cb *irttp_open_tsap(__u8 stsap_sel, int credit, notify_t *notify)391{392struct tsap_cb *self;393struct lsap_cb *lsap;394notify_t ttp_notify;395396IRDA_ASSERT(irttp->magic == TTP_MAGIC, return NULL;);397398/* The IrLMP spec (IrLMP 1.1 p10) says that we have the right to399* use only 0x01-0x6F. Of course, we can use LSAP_ANY as well.400* JeanII */401if((stsap_sel != LSAP_ANY) &&402((stsap_sel < 0x01) || (stsap_sel >= 0x70))) {403IRDA_DEBUG(0, "%s(), invalid tsap!\n", __func__);404return NULL;405}406407self = kzalloc(sizeof(struct tsap_cb), GFP_ATOMIC);408if (self == NULL) {409IRDA_DEBUG(0, "%s(), unable to kmalloc!\n", __func__);410return NULL;411}412413/* Initialize internal objects */414irttp_init_tsap(self);415416/* Initialise todo timer */417self->todo_timer.data = (unsigned long) self;418self->todo_timer.function = &irttp_todo_expired;419420/* Initialize callbacks for IrLMP to use */421irda_notify_init(&ttp_notify);422ttp_notify.connect_confirm = irttp_connect_confirm;423ttp_notify.connect_indication = irttp_connect_indication;424ttp_notify.disconnect_indication = irttp_disconnect_indication;425ttp_notify.data_indication = irttp_data_indication;426ttp_notify.udata_indication = irttp_udata_indication;427ttp_notify.flow_indication = irttp_flow_indication;428if(notify->status_indication != NULL)429ttp_notify.status_indication = irttp_status_indication;430ttp_notify.instance = self;431strncpy(ttp_notify.name, notify->name, NOTIFY_MAX_NAME);432433self->magic = TTP_TSAP_MAGIC;434self->connected = FALSE;435436/*437* Create LSAP at IrLMP layer438*/439lsap = irlmp_open_lsap(stsap_sel, &ttp_notify, 0);440if (lsap == NULL) {441IRDA_WARNING("%s: unable to allocate LSAP!!\n", __func__);442return NULL;443}444445/*446* If user specified LSAP_ANY as source TSAP selector, then IrLMP447* will replace it with whatever source selector which is free, so448* the stsap_sel we have might not be valid anymore449*/450self->stsap_sel = lsap->slsap_sel;451IRDA_DEBUG(4, "%s(), stsap_sel=%02x\n", __func__, self->stsap_sel);452453self->notify = *notify;454self->lsap = lsap;455456hashbin_insert(irttp->tsaps, (irda_queue_t *) self, (long) self, NULL);457458if (credit > TTP_RX_MAX_CREDIT)459self->initial_credit = TTP_RX_MAX_CREDIT;460else461self->initial_credit = credit;462463return self;464}465EXPORT_SYMBOL(irttp_open_tsap);466467/*468* Function irttp_close (handle)469*470* Remove an instance of a TSAP. This function should only deal with the471* deallocation of the TSAP, and resetting of the TSAPs values;472*473*/474static void __irttp_close_tsap(struct tsap_cb *self)475{476/* First make sure we're connected. */477IRDA_ASSERT(self != NULL, return;);478IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);479480irttp_flush_queues(self);481482del_timer(&self->todo_timer);483484/* This one won't be cleaned up if we are disconnect_pend + close_pend485* and we receive a disconnect_indication */486if (self->disconnect_skb)487dev_kfree_skb(self->disconnect_skb);488489self->connected = FALSE;490self->magic = ~TTP_TSAP_MAGIC;491492kfree(self);493}494495/*496* Function irttp_close (self)497*498* Remove TSAP from list of all TSAPs and then deallocate all resources499* associated with this TSAP500*501* Note : because we *free* the tsap structure, it is the responsibility502* of the caller to make sure we are called only once and to deal with503* possible race conditions. - Jean II504*/505int irttp_close_tsap(struct tsap_cb *self)506{507struct tsap_cb *tsap;508509IRDA_DEBUG(4, "%s()\n", __func__);510511IRDA_ASSERT(self != NULL, return -1;);512IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);513514/* Make sure tsap has been disconnected */515if (self->connected) {516/* Check if disconnect is not pending */517if (!test_bit(0, &self->disconnect_pend)) {518IRDA_WARNING("%s: TSAP still connected!\n",519__func__);520irttp_disconnect_request(self, NULL, P_NORMAL);521}522self->close_pend = TRUE;523irttp_start_todo_timer(self, HZ/10);524525return 0; /* Will be back! */526}527528tsap = hashbin_remove(irttp->tsaps, (long) self, NULL);529530IRDA_ASSERT(tsap == self, return -1;);531532/* Close corresponding LSAP */533if (self->lsap) {534irlmp_close_lsap(self->lsap);535self->lsap = NULL;536}537538__irttp_close_tsap(self);539540return 0;541}542EXPORT_SYMBOL(irttp_close_tsap);543544/*545* Function irttp_udata_request (self, skb)546*547* Send unreliable data on this TSAP548*549*/550int irttp_udata_request(struct tsap_cb *self, struct sk_buff *skb)551{552int ret;553554IRDA_ASSERT(self != NULL, return -1;);555IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);556IRDA_ASSERT(skb != NULL, return -1;);557558IRDA_DEBUG(4, "%s()\n", __func__);559560/* Take shortcut on zero byte packets */561if (skb->len == 0) {562ret = 0;563goto err;564}565566/* Check that nothing bad happens */567if (!self->connected) {568IRDA_WARNING("%s(), Not connected\n", __func__);569ret = -ENOTCONN;570goto err;571}572573if (skb->len > self->max_seg_size) {574IRDA_ERROR("%s(), UData is too large for IrLAP!\n", __func__);575ret = -EMSGSIZE;576goto err;577}578579irlmp_udata_request(self->lsap, skb);580self->stats.tx_packets++;581582return 0;583584err:585dev_kfree_skb(skb);586return ret;587}588EXPORT_SYMBOL(irttp_udata_request);589590591/*592* Function irttp_data_request (handle, skb)593*594* Queue frame for transmission. If SAR is enabled, fragement the frame595* and queue the fragments for transmission596*/597int irttp_data_request(struct tsap_cb *self, struct sk_buff *skb)598{599__u8 *frame;600int ret;601602IRDA_ASSERT(self != NULL, return -1;);603IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);604IRDA_ASSERT(skb != NULL, return -1;);605606IRDA_DEBUG(2, "%s() : queue len = %d\n", __func__,607skb_queue_len(&self->tx_queue));608609/* Take shortcut on zero byte packets */610if (skb->len == 0) {611ret = 0;612goto err;613}614615/* Check that nothing bad happens */616if (!self->connected) {617IRDA_WARNING("%s: Not connected\n", __func__);618ret = -ENOTCONN;619goto err;620}621622/*623* Check if SAR is disabled, and the frame is larger than what fits624* inside an IrLAP frame625*/626if ((self->tx_max_sdu_size == 0) && (skb->len > self->max_seg_size)) {627IRDA_ERROR("%s: SAR disabled, and data is too large for IrLAP!\n",628__func__);629ret = -EMSGSIZE;630goto err;631}632633/*634* Check if SAR is enabled, and the frame is larger than the635* TxMaxSduSize636*/637if ((self->tx_max_sdu_size != 0) &&638(self->tx_max_sdu_size != TTP_SAR_UNBOUND) &&639(skb->len > self->tx_max_sdu_size))640{641IRDA_ERROR("%s: SAR enabled, but data is larger than TxMaxSduSize!\n",642__func__);643ret = -EMSGSIZE;644goto err;645}646/*647* Check if transmit queue is full648*/649if (skb_queue_len(&self->tx_queue) >= TTP_TX_MAX_QUEUE) {650/*651* Give it a chance to empty itself652*/653irttp_run_tx_queue(self);654655/* Drop packet. This error code should trigger the caller656* to resend the data in the client code - Jean II */657ret = -ENOBUFS;658goto err;659}660661/* Queue frame, or queue frame segments */662if ((self->tx_max_sdu_size == 0) || (skb->len < self->max_seg_size)) {663/* Queue frame */664IRDA_ASSERT(skb_headroom(skb) >= TTP_HEADER, return -1;);665frame = skb_push(skb, TTP_HEADER);666frame[0] = 0x00; /* Clear more bit */667668skb_queue_tail(&self->tx_queue, skb);669} else {670/*671* Fragment the frame, this function will also queue the672* fragments, we don't care about the fact the transmit673* queue may be overfilled by all the segments for a little674* while675*/676irttp_fragment_skb(self, skb);677}678679/* Check if we can accept more data from client */680if ((!self->tx_sdu_busy) &&681(skb_queue_len(&self->tx_queue) > TTP_TX_HIGH_THRESHOLD)) {682/* Tx queue filling up, so stop client. */683if (self->notify.flow_indication) {684self->notify.flow_indication(self->notify.instance,685self, FLOW_STOP);686}687/* self->tx_sdu_busy is the state of the client.688* Update state after notifying client to avoid689* race condition with irttp_flow_indication().690* If the queue empty itself after our test but before691* we set the flag, we will fix ourselves below in692* irttp_run_tx_queue().693* Jean II */694self->tx_sdu_busy = TRUE;695}696697/* Try to make some progress */698irttp_run_tx_queue(self);699700return 0;701702err:703dev_kfree_skb(skb);704return ret;705}706EXPORT_SYMBOL(irttp_data_request);707708/*709* Function irttp_run_tx_queue (self)710*711* Transmit packets queued for transmission (if possible)712*713*/714static void irttp_run_tx_queue(struct tsap_cb *self)715{716struct sk_buff *skb;717unsigned long flags;718int n;719720IRDA_DEBUG(2, "%s() : send_credit = %d, queue_len = %d\n",721__func__,722self->send_credit, skb_queue_len(&self->tx_queue));723724/* Get exclusive access to the tx queue, otherwise don't touch it */725if (irda_lock(&self->tx_queue_lock) == FALSE)726return;727728/* Try to send out frames as long as we have credits729* and as long as LAP is not full. If LAP is full, it will730* poll us through irttp_flow_indication() - Jean II */731while ((self->send_credit > 0) &&732(!irlmp_lap_tx_queue_full(self->lsap)) &&733(skb = skb_dequeue(&self->tx_queue)))734{735/*736* Since we can transmit and receive frames concurrently,737* the code below is a critical region and we must assure that738* nobody messes with the credits while we update them.739*/740spin_lock_irqsave(&self->lock, flags);741742n = self->avail_credit;743self->avail_credit = 0;744745/* Only room for 127 credits in frame */746if (n > 127) {747self->avail_credit = n-127;748n = 127;749}750self->remote_credit += n;751self->send_credit--;752753spin_unlock_irqrestore(&self->lock, flags);754755/*756* More bit must be set by the data_request() or fragment()757* functions758*/759skb->data[0] |= (n & 0x7f);760761/* Detach from socket.762* The current skb has a reference to the socket that sent763* it (skb->sk). When we pass it to IrLMP, the skb will be764* stored in in IrLAP (self->wx_list). When we are within765* IrLAP, we lose the notion of socket, so we should not766* have a reference to a socket. So, we drop it here.767*768* Why does it matter ?769* When the skb is freed (kfree_skb), if it is associated770* with a socket, it release buffer space on the socket771* (through sock_wfree() and sock_def_write_space()).772* If the socket no longer exist, we may crash. Hard.773* When we close a socket, we make sure that associated packets774* in IrTTP are freed. However, we have no way to cancel775* the packet that we have passed to IrLAP. So, if a packet776* remains in IrLAP (retry on the link or else) after we777* close the socket, we are dead !778* Jean II */779if (skb->sk != NULL) {780/* IrSOCK application, IrOBEX, ... */781skb_orphan(skb);782}783/* IrCOMM over IrTTP, IrLAN, ... */784785/* Pass the skb to IrLMP - done */786irlmp_data_request(self->lsap, skb);787self->stats.tx_packets++;788}789790/* Check if we can accept more frames from client.791* We don't want to wait until the todo timer to do that, and we792* can't use tasklets (grr...), so we are obliged to give control793* to client. That's ok, this test will be true not too often794* (max once per LAP window) and we are called from places795* where we can spend a bit of time doing stuff. - Jean II */796if ((self->tx_sdu_busy) &&797(skb_queue_len(&self->tx_queue) < TTP_TX_LOW_THRESHOLD) &&798(!self->close_pend))799{800if (self->notify.flow_indication)801self->notify.flow_indication(self->notify.instance,802self, FLOW_START);803804/* self->tx_sdu_busy is the state of the client.805* We don't really have a race here, but it's always safer806* to update our state after the client - Jean II */807self->tx_sdu_busy = FALSE;808}809810/* Reset lock */811self->tx_queue_lock = 0;812}813814/*815* Function irttp_give_credit (self)816*817* Send a dataless flowdata TTP-PDU and give available credit to peer818* TSAP819*/820static inline void irttp_give_credit(struct tsap_cb *self)821{822struct sk_buff *tx_skb = NULL;823unsigned long flags;824int n;825826IRDA_ASSERT(self != NULL, return;);827IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);828829IRDA_DEBUG(4, "%s() send=%d,avail=%d,remote=%d\n",830__func__,831self->send_credit, self->avail_credit, self->remote_credit);832833/* Give credit to peer */834tx_skb = alloc_skb(TTP_MAX_HEADER, GFP_ATOMIC);835if (!tx_skb)836return;837838/* Reserve space for LMP, and LAP header */839skb_reserve(tx_skb, LMP_MAX_HEADER);840841/*842* Since we can transmit and receive frames concurrently,843* the code below is a critical region and we must assure that844* nobody messes with the credits while we update them.845*/846spin_lock_irqsave(&self->lock, flags);847848n = self->avail_credit;849self->avail_credit = 0;850851/* Only space for 127 credits in frame */852if (n > 127) {853self->avail_credit = n - 127;854n = 127;855}856self->remote_credit += n;857858spin_unlock_irqrestore(&self->lock, flags);859860skb_put(tx_skb, 1);861tx_skb->data[0] = (__u8) (n & 0x7f);862863irlmp_data_request(self->lsap, tx_skb);864self->stats.tx_packets++;865}866867/*868* Function irttp_udata_indication (instance, sap, skb)869*870* Received some unit-data (unreliable)871*872*/873static int irttp_udata_indication(void *instance, void *sap,874struct sk_buff *skb)875{876struct tsap_cb *self;877int err;878879IRDA_DEBUG(4, "%s()\n", __func__);880881self = (struct tsap_cb *) instance;882883IRDA_ASSERT(self != NULL, return -1;);884IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);885IRDA_ASSERT(skb != NULL, return -1;);886887self->stats.rx_packets++;888889/* Just pass data to layer above */890if (self->notify.udata_indication) {891err = self->notify.udata_indication(self->notify.instance,892self,skb);893/* Same comment as in irttp_do_data_indication() */894if (!err)895return 0;896}897/* Either no handler, or handler returns an error */898dev_kfree_skb(skb);899900return 0;901}902903/*904* Function irttp_data_indication (instance, sap, skb)905*906* Receive segment from IrLMP.907*908*/909static int irttp_data_indication(void *instance, void *sap,910struct sk_buff *skb)911{912struct tsap_cb *self;913unsigned long flags;914int n;915916self = (struct tsap_cb *) instance;917918n = skb->data[0] & 0x7f; /* Extract the credits */919920self->stats.rx_packets++;921922/* Deal with inbound credit923* Since we can transmit and receive frames concurrently,924* the code below is a critical region and we must assure that925* nobody messes with the credits while we update them.926*/927spin_lock_irqsave(&self->lock, flags);928self->send_credit += n;929if (skb->len > 1)930self->remote_credit--;931spin_unlock_irqrestore(&self->lock, flags);932933/*934* Data or dataless packet? Dataless frames contains only the935* TTP_HEADER.936*/937if (skb->len > 1) {938/*939* We don't remove the TTP header, since we must preserve the940* more bit, so the defragment routing knows what to do941*/942skb_queue_tail(&self->rx_queue, skb);943} else {944/* Dataless flowdata TTP-PDU */945dev_kfree_skb(skb);946}947948949/* Push data to the higher layer.950* We do it synchronously because running the todo timer for each951* receive packet would be too much overhead and latency.952* By passing control to the higher layer, we run the risk that953* it may take time or grab a lock. Most often, the higher layer954* will only put packet in a queue.955* Anyway, packets are only dripping through the IrDA, so we can956* have time before the next packet.957* Further, we are run from NET_BH, so the worse that can happen is958* us missing the optimal time to send back the PF bit in LAP.959* Jean II */960irttp_run_rx_queue(self);961962/* We now give credits to peer in irttp_run_rx_queue().963* We need to send credit *NOW*, otherwise we are going964* to miss the next Tx window. The todo timer may take965* a while before it's run... - Jean II */966967/*968* If the peer device has given us some credits and we didn't have969* anyone from before, then we need to shedule the tx queue.970* We need to do that because our Tx have stopped (so we may not971* get any LAP flow indication) and the user may be stopped as972* well. - Jean II973*/974if (self->send_credit == n) {975/* Restart pushing stuff to LAP */976irttp_run_tx_queue(self);977/* Note : we don't want to schedule the todo timer978* because it has horrible latency. No tasklets979* because the tasklet API is broken. - Jean II */980}981982return 0;983}984985/*986* Function irttp_status_indication (self, reason)987*988* Status_indication, just pass to the higher layer...989*990*/991static void irttp_status_indication(void *instance,992LINK_STATUS link, LOCK_STATUS lock)993{994struct tsap_cb *self;995996IRDA_DEBUG(4, "%s()\n", __func__);997998self = (struct tsap_cb *) instance;9991000IRDA_ASSERT(self != NULL, return;);1001IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);10021003/* Check if client has already closed the TSAP and gone away */1004if (self->close_pend)1005return;10061007/*1008* Inform service user if he has requested it1009*/1010if (self->notify.status_indication != NULL)1011self->notify.status_indication(self->notify.instance,1012link, lock);1013else1014IRDA_DEBUG(2, "%s(), no handler\n", __func__);1015}10161017/*1018* Function irttp_flow_indication (self, reason)1019*1020* Flow_indication : IrLAP tells us to send more data.1021*1022*/1023static void irttp_flow_indication(void *instance, void *sap, LOCAL_FLOW flow)1024{1025struct tsap_cb *self;10261027self = (struct tsap_cb *) instance;10281029IRDA_ASSERT(self != NULL, return;);1030IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);10311032IRDA_DEBUG(4, "%s(instance=%p)\n", __func__, self);10331034/* We are "polled" directly from LAP, and the LAP want to fill1035* its Tx window. We want to do our best to send it data, so that1036* we maximise the window. On the other hand, we want to limit the1037* amount of work here so that LAP doesn't hang forever waiting1038* for packets. - Jean II */10391040/* Try to send some packets. Currently, LAP calls us every time1041* there is one free slot, so we will send only one packet.1042* This allow the scheduler to do its round robin - Jean II */1043irttp_run_tx_queue(self);10441045/* Note regarding the interraction with higher layer.1046* irttp_run_tx_queue() may call the client when its queue1047* start to empty, via notify.flow_indication(). Initially.1048* I wanted this to happen in a tasklet, to avoid client1049* grabbing the CPU, but we can't use tasklets safely. And timer1050* is definitely too slow.1051* This will happen only once per LAP window, and usually at1052* the third packet (unless window is smaller). LAP is still1053* doing mtt and sending first packet so it's sort of OK1054* to do that. Jean II */10551056/* If we need to send disconnect. try to do it now */1057if(self->disconnect_pend)1058irttp_start_todo_timer(self, 0);1059}10601061/*1062* Function irttp_flow_request (self, command)1063*1064* This function could be used by the upper layers to tell IrTTP to stop1065* delivering frames if the receive queues are starting to get full, or1066* to tell IrTTP to start delivering frames again.1067*/1068void irttp_flow_request(struct tsap_cb *self, LOCAL_FLOW flow)1069{1070IRDA_DEBUG(1, "%s()\n", __func__);10711072IRDA_ASSERT(self != NULL, return;);1073IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);10741075switch (flow) {1076case FLOW_STOP:1077IRDA_DEBUG(1, "%s(), flow stop\n", __func__);1078self->rx_sdu_busy = TRUE;1079break;1080case FLOW_START:1081IRDA_DEBUG(1, "%s(), flow start\n", __func__);1082self->rx_sdu_busy = FALSE;10831084/* Client say he can accept more data, try to free our1085* queues ASAP - Jean II */1086irttp_run_rx_queue(self);10871088break;1089default:1090IRDA_DEBUG(1, "%s(), Unknown flow command!\n", __func__);1091}1092}1093EXPORT_SYMBOL(irttp_flow_request);10941095/*1096* Function irttp_connect_request (self, dtsap_sel, daddr, qos)1097*1098* Try to connect to remote destination TSAP selector1099*1100*/1101int irttp_connect_request(struct tsap_cb *self, __u8 dtsap_sel,1102__u32 saddr, __u32 daddr,1103struct qos_info *qos, __u32 max_sdu_size,1104struct sk_buff *userdata)1105{1106struct sk_buff *tx_skb;1107__u8 *frame;1108__u8 n;11091110IRDA_DEBUG(4, "%s(), max_sdu_size=%d\n", __func__, max_sdu_size);11111112IRDA_ASSERT(self != NULL, return -EBADR;);1113IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -EBADR;);11141115if (self->connected) {1116if(userdata)1117dev_kfree_skb(userdata);1118return -EISCONN;1119}11201121/* Any userdata supplied? */1122if (userdata == NULL) {1123tx_skb = alloc_skb(TTP_MAX_HEADER + TTP_SAR_HEADER,1124GFP_ATOMIC);1125if (!tx_skb)1126return -ENOMEM;11271128/* Reserve space for MUX_CONTROL and LAP header */1129skb_reserve(tx_skb, TTP_MAX_HEADER + TTP_SAR_HEADER);1130} else {1131tx_skb = userdata;1132/*1133* Check that the client has reserved enough space for1134* headers1135*/1136IRDA_ASSERT(skb_headroom(userdata) >= TTP_MAX_HEADER,1137{ dev_kfree_skb(userdata); return -1; } );1138}11391140/* Initialize connection parameters */1141self->connected = FALSE;1142self->avail_credit = 0;1143self->rx_max_sdu_size = max_sdu_size;1144self->rx_sdu_size = 0;1145self->rx_sdu_busy = FALSE;1146self->dtsap_sel = dtsap_sel;11471148n = self->initial_credit;11491150self->remote_credit = 0;1151self->send_credit = 0;11521153/*1154* Give away max 127 credits for now1155*/1156if (n > 127) {1157self->avail_credit=n-127;1158n = 127;1159}11601161self->remote_credit = n;11621163/* SAR enabled? */1164if (max_sdu_size > 0) {1165IRDA_ASSERT(skb_headroom(tx_skb) >= (TTP_MAX_HEADER + TTP_SAR_HEADER),1166{ dev_kfree_skb(tx_skb); return -1; } );11671168/* Insert SAR parameters */1169frame = skb_push(tx_skb, TTP_HEADER+TTP_SAR_HEADER);11701171frame[0] = TTP_PARAMETERS | n;1172frame[1] = 0x04; /* Length */1173frame[2] = 0x01; /* MaxSduSize */1174frame[3] = 0x02; /* Value length */11751176put_unaligned(cpu_to_be16((__u16) max_sdu_size),1177(__be16 *)(frame+4));1178} else {1179/* Insert plain TTP header */1180frame = skb_push(tx_skb, TTP_HEADER);11811182/* Insert initial credit in frame */1183frame[0] = n & 0x7f;1184}11851186/* Connect with IrLMP. No QoS parameters for now */1187return irlmp_connect_request(self->lsap, dtsap_sel, saddr, daddr, qos,1188tx_skb);1189}1190EXPORT_SYMBOL(irttp_connect_request);11911192/*1193* Function irttp_connect_confirm (handle, qos, skb)1194*1195* Service user confirms TSAP connection with peer.1196*1197*/1198static void irttp_connect_confirm(void *instance, void *sap,1199struct qos_info *qos, __u32 max_seg_size,1200__u8 max_header_size, struct sk_buff *skb)1201{1202struct tsap_cb *self;1203int parameters;1204int ret;1205__u8 plen;1206__u8 n;12071208IRDA_DEBUG(4, "%s()\n", __func__);12091210self = (struct tsap_cb *) instance;12111212IRDA_ASSERT(self != NULL, return;);1213IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);1214IRDA_ASSERT(skb != NULL, return;);12151216self->max_seg_size = max_seg_size - TTP_HEADER;1217self->max_header_size = max_header_size + TTP_HEADER;12181219/*1220* Check if we have got some QoS parameters back! This should be the1221* negotiated QoS for the link.1222*/1223if (qos) {1224IRDA_DEBUG(4, "IrTTP, Negotiated BAUD_RATE: %02x\n",1225qos->baud_rate.bits);1226IRDA_DEBUG(4, "IrTTP, Negotiated BAUD_RATE: %d bps.\n",1227qos->baud_rate.value);1228}12291230n = skb->data[0] & 0x7f;12311232IRDA_DEBUG(4, "%s(), Initial send_credit=%d\n", __func__, n);12331234self->send_credit = n;1235self->tx_max_sdu_size = 0;1236self->connected = TRUE;12371238parameters = skb->data[0] & 0x80;12391240IRDA_ASSERT(skb->len >= TTP_HEADER, return;);1241skb_pull(skb, TTP_HEADER);12421243if (parameters) {1244plen = skb->data[0];12451246ret = irda_param_extract_all(self, skb->data+1,1247IRDA_MIN(skb->len-1, plen),1248¶m_info);12491250/* Any errors in the parameter list? */1251if (ret < 0) {1252IRDA_WARNING("%s: error extracting parameters\n",1253__func__);1254dev_kfree_skb(skb);12551256/* Do not accept this connection attempt */1257return;1258}1259/* Remove parameters */1260skb_pull(skb, IRDA_MIN(skb->len, plen+1));1261}12621263IRDA_DEBUG(4, "%s() send=%d,avail=%d,remote=%d\n", __func__,1264self->send_credit, self->avail_credit, self->remote_credit);12651266IRDA_DEBUG(2, "%s(), MaxSduSize=%d\n", __func__,1267self->tx_max_sdu_size);12681269if (self->notify.connect_confirm) {1270self->notify.connect_confirm(self->notify.instance, self, qos,1271self->tx_max_sdu_size,1272self->max_header_size, skb);1273} else1274dev_kfree_skb(skb);1275}12761277/*1278* Function irttp_connect_indication (handle, skb)1279*1280* Some other device is connecting to this TSAP1281*1282*/1283static void irttp_connect_indication(void *instance, void *sap,1284struct qos_info *qos, __u32 max_seg_size, __u8 max_header_size,1285struct sk_buff *skb)1286{1287struct tsap_cb *self;1288struct lsap_cb *lsap;1289int parameters;1290int ret;1291__u8 plen;1292__u8 n;12931294self = (struct tsap_cb *) instance;12951296IRDA_ASSERT(self != NULL, return;);1297IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);1298IRDA_ASSERT(skb != NULL, return;);12991300lsap = (struct lsap_cb *) sap;13011302self->max_seg_size = max_seg_size - TTP_HEADER;1303self->max_header_size = max_header_size+TTP_HEADER;13041305IRDA_DEBUG(4, "%s(), TSAP sel=%02x\n", __func__, self->stsap_sel);13061307/* Need to update dtsap_sel if its equal to LSAP_ANY */1308self->dtsap_sel = lsap->dlsap_sel;13091310n = skb->data[0] & 0x7f;13111312self->send_credit = n;1313self->tx_max_sdu_size = 0;13141315parameters = skb->data[0] & 0x80;13161317IRDA_ASSERT(skb->len >= TTP_HEADER, return;);1318skb_pull(skb, TTP_HEADER);13191320if (parameters) {1321plen = skb->data[0];13221323ret = irda_param_extract_all(self, skb->data+1,1324IRDA_MIN(skb->len-1, plen),1325¶m_info);13261327/* Any errors in the parameter list? */1328if (ret < 0) {1329IRDA_WARNING("%s: error extracting parameters\n",1330__func__);1331dev_kfree_skb(skb);13321333/* Do not accept this connection attempt */1334return;1335}13361337/* Remove parameters */1338skb_pull(skb, IRDA_MIN(skb->len, plen+1));1339}13401341if (self->notify.connect_indication) {1342self->notify.connect_indication(self->notify.instance, self,1343qos, self->tx_max_sdu_size,1344self->max_header_size, skb);1345} else1346dev_kfree_skb(skb);1347}13481349/*1350* Function irttp_connect_response (handle, userdata)1351*1352* Service user is accepting the connection, just pass it down to1353* IrLMP!1354*1355*/1356int irttp_connect_response(struct tsap_cb *self, __u32 max_sdu_size,1357struct sk_buff *userdata)1358{1359struct sk_buff *tx_skb;1360__u8 *frame;1361int ret;1362__u8 n;13631364IRDA_ASSERT(self != NULL, return -1;);1365IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);13661367IRDA_DEBUG(4, "%s(), Source TSAP selector=%02x\n", __func__,1368self->stsap_sel);13691370/* Any userdata supplied? */1371if (userdata == NULL) {1372tx_skb = alloc_skb(TTP_MAX_HEADER + TTP_SAR_HEADER,1373GFP_ATOMIC);1374if (!tx_skb)1375return -ENOMEM;13761377/* Reserve space for MUX_CONTROL and LAP header */1378skb_reserve(tx_skb, TTP_MAX_HEADER + TTP_SAR_HEADER);1379} else {1380tx_skb = userdata;1381/*1382* Check that the client has reserved enough space for1383* headers1384*/1385IRDA_ASSERT(skb_headroom(userdata) >= TTP_MAX_HEADER,1386{ dev_kfree_skb(userdata); return -1; } );1387}13881389self->avail_credit = 0;1390self->remote_credit = 0;1391self->rx_max_sdu_size = max_sdu_size;1392self->rx_sdu_size = 0;1393self->rx_sdu_busy = FALSE;13941395n = self->initial_credit;13961397/* Frame has only space for max 127 credits (7 bits) */1398if (n > 127) {1399self->avail_credit = n - 127;1400n = 127;1401}14021403self->remote_credit = n;1404self->connected = TRUE;14051406/* SAR enabled? */1407if (max_sdu_size > 0) {1408IRDA_ASSERT(skb_headroom(tx_skb) >= (TTP_MAX_HEADER + TTP_SAR_HEADER),1409{ dev_kfree_skb(tx_skb); return -1; } );14101411/* Insert TTP header with SAR parameters */1412frame = skb_push(tx_skb, TTP_HEADER+TTP_SAR_HEADER);14131414frame[0] = TTP_PARAMETERS | n;1415frame[1] = 0x04; /* Length */14161417/* irda_param_insert(self, IRTTP_MAX_SDU_SIZE, frame+1, */1418/* TTP_SAR_HEADER, ¶m_info) */14191420frame[2] = 0x01; /* MaxSduSize */1421frame[3] = 0x02; /* Value length */14221423put_unaligned(cpu_to_be16((__u16) max_sdu_size),1424(__be16 *)(frame+4));1425} else {1426/* Insert TTP header */1427frame = skb_push(tx_skb, TTP_HEADER);14281429frame[0] = n & 0x7f;1430}14311432ret = irlmp_connect_response(self->lsap, tx_skb);14331434return ret;1435}1436EXPORT_SYMBOL(irttp_connect_response);14371438/*1439* Function irttp_dup (self, instance)1440*1441* Duplicate TSAP, can be used by servers to confirm a connection on a1442* new TSAP so it can keep listening on the old one.1443*/1444struct tsap_cb *irttp_dup(struct tsap_cb *orig, void *instance)1445{1446struct tsap_cb *new;1447unsigned long flags;14481449IRDA_DEBUG(1, "%s()\n", __func__);14501451/* Protect our access to the old tsap instance */1452spin_lock_irqsave(&irttp->tsaps->hb_spinlock, flags);14531454/* Find the old instance */1455if (!hashbin_find(irttp->tsaps, (long) orig, NULL)) {1456IRDA_DEBUG(0, "%s(), unable to find TSAP\n", __func__);1457spin_unlock_irqrestore(&irttp->tsaps->hb_spinlock, flags);1458return NULL;1459}14601461/* Allocate a new instance */1462new = kmalloc(sizeof(struct tsap_cb), GFP_ATOMIC);1463if (!new) {1464IRDA_DEBUG(0, "%s(), unable to kmalloc\n", __func__);1465spin_unlock_irqrestore(&irttp->tsaps->hb_spinlock, flags);1466return NULL;1467}1468/* Dup */1469memcpy(new, orig, sizeof(struct tsap_cb));1470spin_lock_init(&new->lock);14711472/* We don't need the old instance any more */1473spin_unlock_irqrestore(&irttp->tsaps->hb_spinlock, flags);14741475/* Try to dup the LSAP (may fail if we were too slow) */1476new->lsap = irlmp_dup(orig->lsap, new);1477if (!new->lsap) {1478IRDA_DEBUG(0, "%s(), dup failed!\n", __func__);1479kfree(new);1480return NULL;1481}14821483/* Not everything should be copied */1484new->notify.instance = instance;14851486/* Initialize internal objects */1487irttp_init_tsap(new);14881489/* This is locked */1490hashbin_insert(irttp->tsaps, (irda_queue_t *) new, (long) new, NULL);14911492return new;1493}1494EXPORT_SYMBOL(irttp_dup);14951496/*1497* Function irttp_disconnect_request (self)1498*1499* Close this connection please! If priority is high, the queued data1500* segments, if any, will be deallocated first1501*1502*/1503int irttp_disconnect_request(struct tsap_cb *self, struct sk_buff *userdata,1504int priority)1505{1506int ret;15071508IRDA_ASSERT(self != NULL, return -1;);1509IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return -1;);15101511/* Already disconnected? */1512if (!self->connected) {1513IRDA_DEBUG(4, "%s(), already disconnected!\n", __func__);1514if (userdata)1515dev_kfree_skb(userdata);1516return -1;1517}15181519/* Disconnect already pending ?1520* We need to use an atomic operation to prevent reentry. This1521* function may be called from various context, like user, timer1522* for following a disconnect_indication() (i.e. net_bh).1523* Jean II */1524if(test_and_set_bit(0, &self->disconnect_pend)) {1525IRDA_DEBUG(0, "%s(), disconnect already pending\n",1526__func__);1527if (userdata)1528dev_kfree_skb(userdata);15291530/* Try to make some progress */1531irttp_run_tx_queue(self);1532return -1;1533}15341535/*1536* Check if there is still data segments in the transmit queue1537*/1538if (!skb_queue_empty(&self->tx_queue)) {1539if (priority == P_HIGH) {1540/*1541* No need to send the queued data, if we are1542* disconnecting right now since the data will1543* not have any usable connection to be sent on1544*/1545IRDA_DEBUG(1, "%s(): High priority!!()\n", __func__);1546irttp_flush_queues(self);1547} else if (priority == P_NORMAL) {1548/*1549* Must delay disconnect until after all data segments1550* have been sent and the tx_queue is empty1551*/1552/* We'll reuse this one later for the disconnect */1553self->disconnect_skb = userdata; /* May be NULL */15541555irttp_run_tx_queue(self);15561557irttp_start_todo_timer(self, HZ/10);1558return -1;1559}1560}1561/* Note : we don't need to check if self->rx_queue is full and the1562* state of self->rx_sdu_busy because the disconnect response will1563* be sent at the LMP level (so even if the peer has its Tx queue1564* full of data). - Jean II */15651566IRDA_DEBUG(1, "%s(), Disconnecting ...\n", __func__);1567self->connected = FALSE;15681569if (!userdata) {1570struct sk_buff *tx_skb;1571tx_skb = alloc_skb(LMP_MAX_HEADER, GFP_ATOMIC);1572if (!tx_skb)1573return -ENOMEM;15741575/*1576* Reserve space for MUX and LAP header1577*/1578skb_reserve(tx_skb, LMP_MAX_HEADER);15791580userdata = tx_skb;1581}1582ret = irlmp_disconnect_request(self->lsap, userdata);15831584/* The disconnect is no longer pending */1585clear_bit(0, &self->disconnect_pend); /* FALSE */15861587return ret;1588}1589EXPORT_SYMBOL(irttp_disconnect_request);15901591/*1592* Function irttp_disconnect_indication (self, reason)1593*1594* Disconnect indication, TSAP disconnected by peer?1595*1596*/1597static void irttp_disconnect_indication(void *instance, void *sap,1598LM_REASON reason, struct sk_buff *skb)1599{1600struct tsap_cb *self;16011602IRDA_DEBUG(4, "%s()\n", __func__);16031604self = (struct tsap_cb *) instance;16051606IRDA_ASSERT(self != NULL, return;);1607IRDA_ASSERT(self->magic == TTP_TSAP_MAGIC, return;);16081609/* Prevent higher layer to send more data */1610self->connected = FALSE;16111612/* Check if client has already tried to close the TSAP */1613if (self->close_pend) {1614/* In this case, the higher layer is probably gone. Don't1615* bother it and clean up the remains - Jean II */1616if (skb)1617dev_kfree_skb(skb);1618irttp_close_tsap(self);1619return;1620}16211622/* If we are here, we assume that is the higher layer is still1623* waiting for the disconnect notification and able to process it,1624* even if he tried to disconnect. Otherwise, it would have already1625* attempted to close the tsap and self->close_pend would be TRUE.1626* Jean II */16271628/* No need to notify the client if has already tried to disconnect */1629if(self->notify.disconnect_indication)1630self->notify.disconnect_indication(self->notify.instance, self,1631reason, skb);1632else1633if (skb)1634dev_kfree_skb(skb);1635}16361637/*1638* Function irttp_do_data_indication (self, skb)1639*1640* Try to deliver reassembled skb to layer above, and requeue it if that1641* for some reason should fail. We mark rx sdu as busy to apply back1642* pressure is necessary.1643*/1644static void irttp_do_data_indication(struct tsap_cb *self, struct sk_buff *skb)1645{1646int err;16471648/* Check if client has already closed the TSAP and gone away */1649if (self->close_pend) {1650dev_kfree_skb(skb);1651return;1652}16531654err = self->notify.data_indication(self->notify.instance, self, skb);16551656/* Usually the layer above will notify that it's input queue is1657* starting to get filled by using the flow request, but this may1658* be difficult, so it can instead just refuse to eat it and just1659* give an error back1660*/1661if (err) {1662IRDA_DEBUG(0, "%s() requeueing skb!\n", __func__);16631664/* Make sure we take a break */1665self->rx_sdu_busy = TRUE;16661667/* Need to push the header in again */1668skb_push(skb, TTP_HEADER);1669skb->data[0] = 0x00; /* Make sure MORE bit is cleared */16701671/* Put skb back on queue */1672skb_queue_head(&self->rx_queue, skb);1673}1674}16751676/*1677* Function irttp_run_rx_queue (self)1678*1679* Check if we have any frames to be transmitted, or if we have any1680* available credit to give away.1681*/1682static void irttp_run_rx_queue(struct tsap_cb *self)1683{1684struct sk_buff *skb;1685int more = 0;16861687IRDA_DEBUG(2, "%s() send=%d,avail=%d,remote=%d\n", __func__,1688self->send_credit, self->avail_credit, self->remote_credit);16891690/* Get exclusive access to the rx queue, otherwise don't touch it */1691if (irda_lock(&self->rx_queue_lock) == FALSE)1692return;16931694/*1695* Reassemble all frames in receive queue and deliver them1696*/1697while (!self->rx_sdu_busy && (skb = skb_dequeue(&self->rx_queue))) {1698/* This bit will tell us if it's the last fragment or not */1699more = skb->data[0] & 0x80;17001701/* Remove TTP header */1702skb_pull(skb, TTP_HEADER);17031704/* Add the length of the remaining data */1705self->rx_sdu_size += skb->len;17061707/*1708* If SAR is disabled, or user has requested no reassembly1709* of received fragments then we just deliver them1710* immediately. This can be requested by clients that1711* implements byte streams without any message boundaries1712*/1713if (self->rx_max_sdu_size == TTP_SAR_DISABLE) {1714irttp_do_data_indication(self, skb);1715self->rx_sdu_size = 0;17161717continue;1718}17191720/* Check if this is a fragment, and not the last fragment */1721if (more) {1722/*1723* Queue the fragment if we still are within the1724* limits of the maximum size of the rx_sdu1725*/1726if (self->rx_sdu_size <= self->rx_max_sdu_size) {1727IRDA_DEBUG(4, "%s(), queueing frag\n",1728__func__);1729skb_queue_tail(&self->rx_fragments, skb);1730} else {1731/* Free the part of the SDU that is too big */1732dev_kfree_skb(skb);1733}1734continue;1735}1736/*1737* This is the last fragment, so time to reassemble!1738*/1739if ((self->rx_sdu_size <= self->rx_max_sdu_size) ||1740(self->rx_max_sdu_size == TTP_SAR_UNBOUND))1741{1742/*1743* A little optimizing. Only queue the fragment if1744* there are other fragments. Since if this is the1745* last and only fragment, there is no need to1746* reassemble :-)1747*/1748if (!skb_queue_empty(&self->rx_fragments)) {1749skb_queue_tail(&self->rx_fragments,1750skb);17511752skb = irttp_reassemble_skb(self);1753}17541755/* Now we can deliver the reassembled skb */1756irttp_do_data_indication(self, skb);1757} else {1758IRDA_DEBUG(1, "%s(), Truncated frame\n", __func__);17591760/* Free the part of the SDU that is too big */1761dev_kfree_skb(skb);17621763/* Deliver only the valid but truncated part of SDU */1764skb = irttp_reassemble_skb(self);17651766irttp_do_data_indication(self, skb);1767}1768self->rx_sdu_size = 0;1769}17701771/*1772* It's not trivial to keep track of how many credits are available1773* by incrementing at each packet, because delivery may fail1774* (irttp_do_data_indication() may requeue the frame) and because1775* we need to take care of fragmentation.1776* We want the other side to send up to initial_credit packets.1777* We have some frames in our queues, and we have already allowed it1778* to send remote_credit.1779* No need to spinlock, write is atomic and self correcting...1780* Jean II1781*/1782self->avail_credit = (self->initial_credit -1783(self->remote_credit +1784skb_queue_len(&self->rx_queue) +1785skb_queue_len(&self->rx_fragments)));17861787/* Do we have too much credits to send to peer ? */1788if ((self->remote_credit <= TTP_RX_MIN_CREDIT) &&1789(self->avail_credit > 0)) {1790/* Send explicit credit frame */1791irttp_give_credit(self);1792/* Note : do *NOT* check if tx_queue is non-empty, that1793* will produce deadlocks. I repeat : send a credit frame1794* even if we have something to send in our Tx queue.1795* If we have credits, it means that our Tx queue is blocked.1796*1797* Let's suppose the peer can't keep up with our Tx. He will1798* flow control us by not sending us any credits, and we1799* will stop Tx and start accumulating credits here.1800* Up to the point where the peer will stop its Tx queue,1801* for lack of credits.1802* Let's assume the peer application is single threaded.1803* It will block on Tx and never consume any Rx buffer.1804* Deadlock. Guaranteed. - Jean II1805*/1806}18071808/* Reset lock */1809self->rx_queue_lock = 0;1810}18111812#ifdef CONFIG_PROC_FS1813struct irttp_iter_state {1814int id;1815};18161817static void *irttp_seq_start(struct seq_file *seq, loff_t *pos)1818{1819struct irttp_iter_state *iter = seq->private;1820struct tsap_cb *self;18211822/* Protect our access to the tsap list */1823spin_lock_irq(&irttp->tsaps->hb_spinlock);1824iter->id = 0;18251826for (self = (struct tsap_cb *) hashbin_get_first(irttp->tsaps);1827self != NULL;1828self = (struct tsap_cb *) hashbin_get_next(irttp->tsaps)) {1829if (iter->id == *pos)1830break;1831++iter->id;1832}18331834return self;1835}18361837static void *irttp_seq_next(struct seq_file *seq, void *v, loff_t *pos)1838{1839struct irttp_iter_state *iter = seq->private;18401841++*pos;1842++iter->id;1843return (void *) hashbin_get_next(irttp->tsaps);1844}18451846static void irttp_seq_stop(struct seq_file *seq, void *v)1847{1848spin_unlock_irq(&irttp->tsaps->hb_spinlock);1849}18501851static int irttp_seq_show(struct seq_file *seq, void *v)1852{1853const struct irttp_iter_state *iter = seq->private;1854const struct tsap_cb *self = v;18551856seq_printf(seq, "TSAP %d, ", iter->id);1857seq_printf(seq, "stsap_sel: %02x, ",1858self->stsap_sel);1859seq_printf(seq, "dtsap_sel: %02x\n",1860self->dtsap_sel);1861seq_printf(seq, " connected: %s, ",1862self->connected? "TRUE":"FALSE");1863seq_printf(seq, "avail credit: %d, ",1864self->avail_credit);1865seq_printf(seq, "remote credit: %d, ",1866self->remote_credit);1867seq_printf(seq, "send credit: %d\n",1868self->send_credit);1869seq_printf(seq, " tx packets: %lu, ",1870self->stats.tx_packets);1871seq_printf(seq, "rx packets: %lu, ",1872self->stats.rx_packets);1873seq_printf(seq, "tx_queue len: %u ",1874skb_queue_len(&self->tx_queue));1875seq_printf(seq, "rx_queue len: %u\n",1876skb_queue_len(&self->rx_queue));1877seq_printf(seq, " tx_sdu_busy: %s, ",1878self->tx_sdu_busy? "TRUE":"FALSE");1879seq_printf(seq, "rx_sdu_busy: %s\n",1880self->rx_sdu_busy? "TRUE":"FALSE");1881seq_printf(seq, " max_seg_size: %u, ",1882self->max_seg_size);1883seq_printf(seq, "tx_max_sdu_size: %u, ",1884self->tx_max_sdu_size);1885seq_printf(seq, "rx_max_sdu_size: %u\n",1886self->rx_max_sdu_size);18871888seq_printf(seq, " Used by (%s)\n\n",1889self->notify.name);1890return 0;1891}18921893static const struct seq_operations irttp_seq_ops = {1894.start = irttp_seq_start,1895.next = irttp_seq_next,1896.stop = irttp_seq_stop,1897.show = irttp_seq_show,1898};18991900static int irttp_seq_open(struct inode *inode, struct file *file)1901{1902return seq_open_private(file, &irttp_seq_ops,1903sizeof(struct irttp_iter_state));1904}19051906const struct file_operations irttp_seq_fops = {1907.owner = THIS_MODULE,1908.open = irttp_seq_open,1909.read = seq_read,1910.llseek = seq_lseek,1911.release = seq_release_private,1912};19131914#endif /* PROC_FS */191519161917