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