l2cap_misc.c revision 1.1.6.2 1 1.1.6.2 yamt /* $NetBSD: l2cap_misc.c,v 1.1.6.2 2006/06/26 12:53:57 yamt Exp $ */
2 1.1.6.2 yamt
3 1.1.6.2 yamt /*-
4 1.1.6.2 yamt * Copyright (c) 2005 Iain Hibbert.
5 1.1.6.2 yamt * Copyright (c) 2006 Itronix Inc.
6 1.1.6.2 yamt * All rights reserved.
7 1.1.6.2 yamt *
8 1.1.6.2 yamt * Redistribution and use in source and binary forms, with or without
9 1.1.6.2 yamt * modification, are permitted provided that the following conditions
10 1.1.6.2 yamt * are met:
11 1.1.6.2 yamt * 1. Redistributions of source code must retain the above copyright
12 1.1.6.2 yamt * notice, this list of conditions and the following disclaimer.
13 1.1.6.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
14 1.1.6.2 yamt * notice, this list of conditions and the following disclaimer in the
15 1.1.6.2 yamt * documentation and/or other materials provided with the distribution.
16 1.1.6.2 yamt * 3. The name of Itronix Inc. may not be used to endorse
17 1.1.6.2 yamt * or promote products derived from this software without specific
18 1.1.6.2 yamt * prior written permission.
19 1.1.6.2 yamt *
20 1.1.6.2 yamt * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
21 1.1.6.2 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1.6.2 yamt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1.6.2 yamt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
24 1.1.6.2 yamt * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 1.1.6.2 yamt * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 1.1.6.2 yamt * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 1.1.6.2 yamt * ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1.6.2 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1.6.2 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1.6.2 yamt * POSSIBILITY OF SUCH DAMAGE.
31 1.1.6.2 yamt */
32 1.1.6.2 yamt
33 1.1.6.2 yamt #include <sys/cdefs.h>
34 1.1.6.2 yamt __KERNEL_RCSID(0, "$NetBSD: l2cap_misc.c,v 1.1.6.2 2006/06/26 12:53:57 yamt Exp $");
35 1.1.6.2 yamt
36 1.1.6.2 yamt #include <sys/param.h>
37 1.1.6.2 yamt #include <sys/kernel.h>
38 1.1.6.2 yamt #include <sys/mbuf.h>
39 1.1.6.2 yamt #include <sys/proc.h>
40 1.1.6.2 yamt #include <sys/queue.h>
41 1.1.6.2 yamt #include <sys/systm.h>
42 1.1.6.2 yamt
43 1.1.6.2 yamt #include <netbt/bluetooth.h>
44 1.1.6.2 yamt #include <netbt/hci.h>
45 1.1.6.2 yamt #include <netbt/l2cap.h>
46 1.1.6.2 yamt
47 1.1.6.2 yamt struct l2cap_channel_list
48 1.1.6.2 yamt l2cap_active_list = LIST_HEAD_INITIALIZER(l2cap_active_list);
49 1.1.6.2 yamt struct l2cap_channel_list
50 1.1.6.2 yamt l2cap_listen_list = LIST_HEAD_INITIALIZER(l2cap_listen_list);
51 1.1.6.2 yamt
52 1.1.6.2 yamt POOL_INIT(l2cap_req_pool, sizeof(struct l2cap_req), 0, 0, 0, "l2cap_req", NULL);
53 1.1.6.2 yamt POOL_INIT(l2cap_pdu_pool, sizeof(struct l2cap_pdu), 0, 0, 0, "l2cap_pdu", NULL);
54 1.1.6.2 yamt
55 1.1.6.2 yamt const l2cap_qos_t l2cap_default_qos = {
56 1.1.6.2 yamt 0, /* flags */
57 1.1.6.2 yamt L2CAP_QOS_BEST_EFFORT, /* service type */
58 1.1.6.2 yamt 0x00000000, /* token rate */
59 1.1.6.2 yamt 0x00000000, /* token bucket size */
60 1.1.6.2 yamt 0x00000000, /* peak bandwidth */
61 1.1.6.2 yamt 0xffffffff, /* latency */
62 1.1.6.2 yamt 0xffffffff /* delay variation */
63 1.1.6.2 yamt };
64 1.1.6.2 yamt
65 1.1.6.2 yamt /*
66 1.1.6.2 yamt * L2CAP request timeouts
67 1.1.6.2 yamt */
68 1.1.6.2 yamt int l2cap_response_timeout = 30; /* seconds */
69 1.1.6.2 yamt int l2cap_response_extended_timeout = 180; /* seconds */
70 1.1.6.2 yamt
71 1.1.6.2 yamt /*
72 1.1.6.2 yamt * Allocate a new Request structure & ID and set the timer going
73 1.1.6.2 yamt */
74 1.1.6.2 yamt int
75 1.1.6.2 yamt l2cap_request_alloc(struct l2cap_channel *chan, uint8_t code)
76 1.1.6.2 yamt {
77 1.1.6.2 yamt struct hci_link *link = chan->lc_link;
78 1.1.6.2 yamt struct l2cap_req *req;
79 1.1.6.2 yamt int next_id;
80 1.1.6.2 yamt
81 1.1.6.2 yamt if (link == NULL)
82 1.1.6.2 yamt return ENETDOWN;
83 1.1.6.2 yamt
84 1.1.6.2 yamt /* find next ID (0 is not allowed) */
85 1.1.6.2 yamt next_id = link->hl_lastid + 1;
86 1.1.6.2 yamt if (next_id > 0xff)
87 1.1.6.2 yamt next_id = 1;
88 1.1.6.2 yamt
89 1.1.6.2 yamt /* Ouroboros check */
90 1.1.6.2 yamt req = TAILQ_FIRST(&link->hl_reqs);
91 1.1.6.2 yamt if (req && req->lr_id == next_id)
92 1.1.6.2 yamt return ENFILE;
93 1.1.6.2 yamt
94 1.1.6.2 yamt req = pool_get(&l2cap_req_pool, PR_NOWAIT);
95 1.1.6.2 yamt if (req == NULL)
96 1.1.6.2 yamt return ENOMEM;
97 1.1.6.2 yamt
98 1.1.6.2 yamt req->lr_id = link->hl_lastid = next_id;
99 1.1.6.2 yamt
100 1.1.6.2 yamt req->lr_code = code;
101 1.1.6.2 yamt req->lr_chan = chan;
102 1.1.6.2 yamt req->lr_link = link;
103 1.1.6.2 yamt
104 1.1.6.2 yamt callout_init(&req->lr_rtx);
105 1.1.6.2 yamt callout_reset(&req->lr_rtx, l2cap_response_timeout*hz, l2cap_rtx, req);
106 1.1.6.2 yamt
107 1.1.6.2 yamt TAILQ_INSERT_TAIL(&link->hl_reqs, req, lr_next);
108 1.1.6.2 yamt
109 1.1.6.2 yamt return 0;
110 1.1.6.2 yamt }
111 1.1.6.2 yamt
112 1.1.6.2 yamt /*
113 1.1.6.2 yamt * Find a running request for this link
114 1.1.6.2 yamt */
115 1.1.6.2 yamt struct l2cap_req *
116 1.1.6.2 yamt l2cap_request_lookup(struct hci_link *link, uint8_t id)
117 1.1.6.2 yamt {
118 1.1.6.2 yamt struct l2cap_req *req;
119 1.1.6.2 yamt
120 1.1.6.2 yamt TAILQ_FOREACH(req, &link->hl_reqs, lr_next) {
121 1.1.6.2 yamt if (req->lr_id == id)
122 1.1.6.2 yamt return req;
123 1.1.6.2 yamt }
124 1.1.6.2 yamt
125 1.1.6.2 yamt return NULL;
126 1.1.6.2 yamt }
127 1.1.6.2 yamt
128 1.1.6.2 yamt /*
129 1.1.6.2 yamt * Halt and free a request
130 1.1.6.2 yamt */
131 1.1.6.2 yamt void
132 1.1.6.2 yamt l2cap_request_free(struct l2cap_req *req)
133 1.1.6.2 yamt {
134 1.1.6.2 yamt struct hci_link *link = req->lr_link;
135 1.1.6.2 yamt
136 1.1.6.2 yamt callout_stop(&req->lr_rtx);
137 1.1.6.2 yamt if (callout_invoking(&req->lr_rtx))
138 1.1.6.2 yamt return;
139 1.1.6.2 yamt
140 1.1.6.2 yamt TAILQ_REMOVE(&link->hl_reqs, req, lr_next);
141 1.1.6.2 yamt pool_put(&l2cap_req_pool, req);
142 1.1.6.2 yamt }
143 1.1.6.2 yamt
144 1.1.6.2 yamt /*
145 1.1.6.2 yamt * Response Timeout eXpired
146 1.1.6.2 yamt *
147 1.1.6.2 yamt * No response to our request, so deal with it as best we can.
148 1.1.6.2 yamt *
149 1.1.6.2 yamt * XXX should try again at least with ertx?
150 1.1.6.2 yamt */
151 1.1.6.2 yamt void
152 1.1.6.2 yamt l2cap_rtx(void *arg)
153 1.1.6.2 yamt {
154 1.1.6.2 yamt struct l2cap_req *req = arg;
155 1.1.6.2 yamt struct l2cap_channel *chan;
156 1.1.6.2 yamt int s;
157 1.1.6.2 yamt
158 1.1.6.2 yamt s = splsoftnet();
159 1.1.6.2 yamt callout_ack(&req->lr_rtx);
160 1.1.6.2 yamt
161 1.1.6.2 yamt chan = req->lr_chan;
162 1.1.6.2 yamt l2cap_request_free(req);
163 1.1.6.2 yamt
164 1.1.6.2 yamt DPRINTF("cid %d, ident %d\n", (chan ? chan->lc_lcid : 0), req->lr_id);
165 1.1.6.2 yamt
166 1.1.6.2 yamt if (chan && chan->lc_state != L2CAP_CLOSED)
167 1.1.6.2 yamt l2cap_close(chan, ETIMEDOUT);
168 1.1.6.2 yamt
169 1.1.6.2 yamt splx(s);
170 1.1.6.2 yamt }
171 1.1.6.2 yamt
172 1.1.6.2 yamt /*
173 1.1.6.2 yamt * Allocate next available CID to channel. We keep a single
174 1.1.6.2 yamt * ordered list of channels, so find the first gap.
175 1.1.6.2 yamt *
176 1.1.6.2 yamt * If this turns out to be not enough (!), could use a
177 1.1.6.2 yamt * list per HCI unit..
178 1.1.6.2 yamt */
179 1.1.6.2 yamt int
180 1.1.6.2 yamt l2cap_cid_alloc(struct l2cap_channel *chan)
181 1.1.6.2 yamt {
182 1.1.6.2 yamt struct l2cap_channel *used, *prev = NULL;
183 1.1.6.2 yamt uint16_t cid = L2CAP_FIRST_CID;
184 1.1.6.2 yamt
185 1.1.6.2 yamt if (chan->lc_lcid != L2CAP_NULL_CID || chan->lc_state != L2CAP_CLOSED)
186 1.1.6.2 yamt return EISCONN;
187 1.1.6.2 yamt
188 1.1.6.2 yamt LIST_FOREACH(used, &l2cap_active_list, lc_ncid) {
189 1.1.6.2 yamt if (used->lc_lcid > cid)
190 1.1.6.2 yamt break; /* found our gap */
191 1.1.6.2 yamt
192 1.1.6.2 yamt KASSERT(used->lc_lcid == cid);
193 1.1.6.2 yamt cid++;
194 1.1.6.2 yamt
195 1.1.6.2 yamt if (cid == L2CAP_LAST_CID)
196 1.1.6.2 yamt return ENFILE;
197 1.1.6.2 yamt
198 1.1.6.2 yamt prev = used; /* for insert after */
199 1.1.6.2 yamt }
200 1.1.6.2 yamt
201 1.1.6.2 yamt chan->lc_lcid = cid;
202 1.1.6.2 yamt
203 1.1.6.2 yamt if (prev)
204 1.1.6.2 yamt LIST_INSERT_AFTER(prev, chan, lc_ncid);
205 1.1.6.2 yamt else
206 1.1.6.2 yamt LIST_INSERT_HEAD(&l2cap_active_list, chan, lc_ncid);
207 1.1.6.2 yamt
208 1.1.6.2 yamt return 0;
209 1.1.6.2 yamt }
210 1.1.6.2 yamt
211 1.1.6.2 yamt /*
212 1.1.6.2 yamt * Find channel with CID
213 1.1.6.2 yamt */
214 1.1.6.2 yamt struct l2cap_channel *
215 1.1.6.2 yamt l2cap_cid_lookup(uint16_t cid)
216 1.1.6.2 yamt {
217 1.1.6.2 yamt struct l2cap_channel *chan;
218 1.1.6.2 yamt
219 1.1.6.2 yamt LIST_FOREACH(chan, &l2cap_active_list, lc_ncid) {
220 1.1.6.2 yamt if (chan->lc_lcid == cid)
221 1.1.6.2 yamt return chan;
222 1.1.6.2 yamt
223 1.1.6.2 yamt if (chan->lc_lcid > cid)
224 1.1.6.2 yamt return NULL;
225 1.1.6.2 yamt }
226 1.1.6.2 yamt
227 1.1.6.2 yamt return NULL;
228 1.1.6.2 yamt }
229