Home | History | Annotate | Line # | Download | only in netbt
l2cap_lower.c revision 1.1.4.2
      1  1.1.4.2  chap /*	$NetBSD: l2cap_lower.c,v 1.1.4.2 2006/06/22 03:39:50 chap Exp $	*/
      2  1.1.4.2  chap 
      3  1.1.4.2  chap /*-
      4  1.1.4.2  chap  * Copyright (c) 2005 Iain Hibbert.
      5  1.1.4.2  chap  * Copyright (c) 2006 Itronix Inc.
      6  1.1.4.2  chap  * All rights reserved.
      7  1.1.4.2  chap  *
      8  1.1.4.2  chap  * Redistribution and use in source and binary forms, with or without
      9  1.1.4.2  chap  * modification, are permitted provided that the following conditions
     10  1.1.4.2  chap  * are met:
     11  1.1.4.2  chap  * 1. Redistributions of source code must retain the above copyright
     12  1.1.4.2  chap  *    notice, this list of conditions and the following disclaimer.
     13  1.1.4.2  chap  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1.4.2  chap  *    notice, this list of conditions and the following disclaimer in the
     15  1.1.4.2  chap  *    documentation and/or other materials provided with the distribution.
     16  1.1.4.2  chap  * 3. The name of Itronix Inc. may not be used to endorse
     17  1.1.4.2  chap  *    or promote products derived from this software without specific
     18  1.1.4.2  chap  *    prior written permission.
     19  1.1.4.2  chap  *
     20  1.1.4.2  chap  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
     21  1.1.4.2  chap  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  1.1.4.2  chap  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  1.1.4.2  chap  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
     24  1.1.4.2  chap  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     25  1.1.4.2  chap  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     26  1.1.4.2  chap  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     27  1.1.4.2  chap  * ON ANY THEORY OF LIABILITY, WHETHER IN
     28  1.1.4.2  chap  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  1.1.4.2  chap  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  1.1.4.2  chap  * POSSIBILITY OF SUCH DAMAGE.
     31  1.1.4.2  chap  */
     32  1.1.4.2  chap 
     33  1.1.4.2  chap #include <sys/cdefs.h>
     34  1.1.4.2  chap __KERNEL_RCSID(0, "$NetBSD: l2cap_lower.c,v 1.1.4.2 2006/06/22 03:39:50 chap Exp $");
     35  1.1.4.2  chap 
     36  1.1.4.2  chap #include <sys/param.h>
     37  1.1.4.2  chap #include <sys/kernel.h>
     38  1.1.4.2  chap #include <sys/malloc.h>
     39  1.1.4.2  chap #include <sys/mbuf.h>
     40  1.1.4.2  chap #include <sys/proc.h>
     41  1.1.4.2  chap #include <sys/queue.h>
     42  1.1.4.2  chap #include <sys/systm.h>
     43  1.1.4.2  chap 
     44  1.1.4.2  chap #include <netbt/bluetooth.h>
     45  1.1.4.2  chap #include <netbt/hci.h>
     46  1.1.4.2  chap #include <netbt/l2cap.h>
     47  1.1.4.2  chap 
     48  1.1.4.2  chap /****************************************************************************
     49  1.1.4.2  chap  *
     50  1.1.4.2  chap  *	L2CAP Channel Lower Layer interface
     51  1.1.4.2  chap  */
     52  1.1.4.2  chap 
     53  1.1.4.2  chap /*
     54  1.1.4.2  chap  * L2CAP channel is disconnected, could be:
     55  1.1.4.2  chap  *
     56  1.1.4.2  chap  * HCI layer received "Disconnect Complete" event for ACL link
     57  1.1.4.2  chap  * some Request timed out
     58  1.1.4.2  chap  * Config failed
     59  1.1.4.2  chap  * Other end reported invalid CID
     60  1.1.4.2  chap  * Normal disconnection
     61  1.1.4.2  chap  */
     62  1.1.4.2  chap void
     63  1.1.4.2  chap l2cap_close(struct l2cap_channel *chan, int err)
     64  1.1.4.2  chap {
     65  1.1.4.2  chap 	struct l2cap_pdu *pdu;
     66  1.1.4.2  chap 	struct l2cap_req *req, *n;
     67  1.1.4.2  chap 
     68  1.1.4.2  chap 	if (chan->lc_state == L2CAP_CLOSED)
     69  1.1.4.2  chap 		return;
     70  1.1.4.2  chap 
     71  1.1.4.2  chap 	/*
     72  1.1.4.2  chap 	 * Since any potential PDU could be half sent we just let it go,
     73  1.1.4.2  chap 	 * but disassociate ourselves from it as links deal with ownerless
     74  1.1.4.2  chap 	 * PDU's in any case.  We could try harder to flush unsent packets
     75  1.1.4.2  chap 	 * but maybe its better to leave them in the queue?
     76  1.1.4.2  chap 	 */
     77  1.1.4.2  chap 	TAILQ_FOREACH(pdu, &chan->lc_link->hl_txq, lp_next) {
     78  1.1.4.2  chap 		if (pdu->lp_chan == chan)
     79  1.1.4.2  chap 			pdu->lp_chan = NULL;
     80  1.1.4.2  chap 	}
     81  1.1.4.2  chap 
     82  1.1.4.2  chap 	/*
     83  1.1.4.2  chap 	 * and clear any outstanding requests..
     84  1.1.4.2  chap 	 */
     85  1.1.4.2  chap 	req = TAILQ_FIRST(&chan->lc_link->hl_reqs);
     86  1.1.4.2  chap 	while (req != NULL) {
     87  1.1.4.2  chap 		n = TAILQ_NEXT(req, lr_next);
     88  1.1.4.2  chap 		if (req->lr_chan == chan)
     89  1.1.4.2  chap 			l2cap_request_free(req);
     90  1.1.4.2  chap 
     91  1.1.4.2  chap 		req = n;
     92  1.1.4.2  chap 	}
     93  1.1.4.2  chap 
     94  1.1.4.2  chap 	chan->lc_pending = 0;
     95  1.1.4.2  chap 	chan->lc_state = L2CAP_CLOSED;
     96  1.1.4.2  chap 	hci_acl_close(chan->lc_link, err);
     97  1.1.4.2  chap 	chan->lc_link = NULL;
     98  1.1.4.2  chap 
     99  1.1.4.2  chap 	(*chan->lc_proto->disconnected)(chan->lc_upper, err);
    100  1.1.4.2  chap }
    101  1.1.4.2  chap 
    102  1.1.4.2  chap /*
    103  1.1.4.2  chap  * Process incoming L2CAP frame from ACL link. We take off the B-Frame
    104  1.1.4.2  chap  * header (which is present in all packets), verify the data length
    105  1.1.4.2  chap  * and distribute the rest of the frame to the relevant channel
    106  1.1.4.2  chap  * handler.
    107  1.1.4.2  chap  */
    108  1.1.4.2  chap void
    109  1.1.4.2  chap l2cap_recv_frame(struct mbuf *m, struct hci_link *link)
    110  1.1.4.2  chap {
    111  1.1.4.2  chap 	struct l2cap_channel *chan;
    112  1.1.4.2  chap 	l2cap_hdr_t hdr;
    113  1.1.4.2  chap 
    114  1.1.4.2  chap 	m_copydata(m, 0, sizeof(hdr), &hdr);
    115  1.1.4.2  chap 	m_adj(m, sizeof(hdr));
    116  1.1.4.2  chap 
    117  1.1.4.2  chap 	hdr.length = le16toh(hdr.length);
    118  1.1.4.2  chap 	hdr.dcid = le16toh(hdr.dcid);
    119  1.1.4.2  chap 
    120  1.1.4.2  chap 	DPRINTFN(5, "(%s) received packet (%d bytes)\n",
    121  1.1.4.2  chap 		    link->hl_unit->hci_devname, hdr.length);
    122  1.1.4.2  chap 
    123  1.1.4.2  chap 	// wasnt this checked in hci_acl_recv() already?
    124  1.1.4.2  chap 	if (hdr.length != m->m_pkthdr.len)
    125  1.1.4.2  chap 		goto failed;
    126  1.1.4.2  chap 
    127  1.1.4.2  chap 	if (hdr.dcid == L2CAP_SIGNAL_CID) {
    128  1.1.4.2  chap 		l2cap_recv_signal(m, link);
    129  1.1.4.2  chap 		return;
    130  1.1.4.2  chap 	}
    131  1.1.4.2  chap 
    132  1.1.4.2  chap 	if (hdr.dcid == L2CAP_CLT_CID) {
    133  1.1.4.2  chap 		m_freem(m);	// TODO
    134  1.1.4.2  chap 		return;
    135  1.1.4.2  chap 	}
    136  1.1.4.2  chap 
    137  1.1.4.2  chap 	chan = l2cap_cid_lookup(hdr.dcid);
    138  1.1.4.2  chap 	if (chan) {
    139  1.1.4.2  chap 		(*chan->lc_proto->input)(chan->lc_upper, m);
    140  1.1.4.2  chap 		return;
    141  1.1.4.2  chap 	}
    142  1.1.4.2  chap 
    143  1.1.4.2  chap 	DPRINTF("(%s) dropping %d L2CAP data bytes for unknown CID #%d\n",
    144  1.1.4.2  chap 		link->hl_unit->hci_devname, hdr.length, hdr.dcid);
    145  1.1.4.2  chap 
    146  1.1.4.2  chap failed:
    147  1.1.4.2  chap 	m_freem(m);
    148  1.1.4.2  chap }
    149  1.1.4.2  chap 
    150  1.1.4.2  chap /*
    151  1.1.4.2  chap  * Start another L2CAP packet on its way. This is called from l2cap_send
    152  1.1.4.2  chap  * (when no PDU is pending) and hci_acl_start (when PDU has been placed on
    153  1.1.4.2  chap  * device queue). Thus we can have more than one PDU waiting at the device
    154  1.1.4.2  chap  * if space is available but no single channel will hog the link.
    155  1.1.4.2  chap  */
    156  1.1.4.2  chap int
    157  1.1.4.2  chap l2cap_start(struct l2cap_channel *chan)
    158  1.1.4.2  chap {
    159  1.1.4.2  chap 	struct mbuf *m;
    160  1.1.4.2  chap 	int err = 0;
    161  1.1.4.2  chap 
    162  1.1.4.2  chap 	if (chan->lc_state != L2CAP_OPEN)
    163  1.1.4.2  chap 		return 0;
    164  1.1.4.2  chap 
    165  1.1.4.2  chap 	if (MBUFQ_FIRST(&chan->lc_txq) == NULL) {
    166  1.1.4.2  chap 		DPRINTFN(5, "no data, pending = %d\n", chan->lc_pending);
    167  1.1.4.2  chap 		/*
    168  1.1.4.2  chap 		 * If we are just waiting for the queue to flush
    169  1.1.4.2  chap 		 * and it has, we may disconnect..
    170  1.1.4.2  chap 		 */
    171  1.1.4.2  chap 		if (chan->lc_flags & L2CAP_SHUTDOWN
    172  1.1.4.2  chap 		    && chan->lc_pending == 0) {
    173  1.1.4.2  chap 			chan->lc_state = L2CAP_WAIT_DISCONNECT;
    174  1.1.4.2  chap 			err = l2cap_send_disconnect_req(chan);
    175  1.1.4.2  chap 			if (err)
    176  1.1.4.2  chap 				l2cap_close(chan, err);
    177  1.1.4.2  chap 		}
    178  1.1.4.2  chap 
    179  1.1.4.2  chap 		return err;
    180  1.1.4.2  chap 	}
    181  1.1.4.2  chap 
    182  1.1.4.2  chap 	/*
    183  1.1.4.2  chap 	 * We could check QoS/RFC mode here and optionally not send
    184  1.1.4.2  chap 	 * the packet if we are not ready for any reason
    185  1.1.4.2  chap 	 *
    186  1.1.4.2  chap 	 * Also to support flush timeout then we might want to start
    187  1.1.4.2  chap 	 * the timer going? (would need to keep some kind of record
    188  1.1.4.2  chap 	 * of packets sent, possibly change it so that we allocate
    189  1.1.4.2  chap 	 * the l2cap_pdu and fragment the packet, then hand it down
    190  1.1.4.2  chap 	 * and get it back when its completed). Hm.
    191  1.1.4.2  chap 	 */
    192  1.1.4.2  chap 
    193  1.1.4.2  chap 	MBUFQ_DEQUEUE(&chan->lc_txq, m);
    194  1.1.4.2  chap 
    195  1.1.4.2  chap 	KASSERT(chan->lc_link);
    196  1.1.4.2  chap 	KASSERT(m);
    197  1.1.4.2  chap 
    198  1.1.4.2  chap 	DPRINTFN(5, "CID #%d sending packet (%d bytes)\n",
    199  1.1.4.2  chap 		chan->lc_lcid, m->m_pkthdr.len);
    200  1.1.4.2  chap 
    201  1.1.4.2  chap 	chan->lc_pending++;
    202  1.1.4.2  chap 	return hci_acl_send(m, chan->lc_link, chan);
    203  1.1.4.2  chap }
    204