Commit | Line | Data |
---|---|---|
466f8004 AE |
1 | /* |
2 | Copyright (c) 2010,2011 Code Aurora Forum. All rights reserved. | |
3 | Copyright (c) 2011,2012 Intel Corp. | |
4 | ||
5 | This program is free software; you can redistribute it and/or modify | |
6 | it under the terms of the GNU General Public License version 2 and | |
7 | only version 2 as published by the Free Software Foundation. | |
8 | ||
9 | This program is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | GNU General Public License for more details. | |
13 | */ | |
14 | ||
15 | #include <net/bluetooth/bluetooth.h> | |
16 | #include <net/bluetooth/hci_core.h> | |
17 | #include <net/bluetooth/l2cap.h> | |
9740e49d | 18 | #include <net/bluetooth/a2mp.h> |
466f8004 | 19 | |
f6d3c6e7 AE |
20 | /* A2MP build & send command helper functions */ |
21 | static struct a2mp_cmd *__a2mp_build(u8 code, u8 ident, u16 len, void *data) | |
22 | { | |
23 | struct a2mp_cmd *cmd; | |
24 | int plen; | |
25 | ||
26 | plen = sizeof(*cmd) + len; | |
27 | cmd = kzalloc(plen, GFP_KERNEL); | |
28 | if (!cmd) | |
29 | return NULL; | |
30 | ||
31 | cmd->code = code; | |
32 | cmd->ident = ident; | |
33 | cmd->len = cpu_to_le16(len); | |
34 | ||
35 | memcpy(cmd->data, data, len); | |
36 | ||
37 | return cmd; | |
38 | } | |
39 | ||
40 | static void a2mp_send(struct amp_mgr *mgr, u8 code, u8 ident, u16 len, | |
41 | void *data) | |
42 | { | |
43 | struct l2cap_chan *chan = mgr->a2mp_chan; | |
44 | struct a2mp_cmd *cmd; | |
45 | u16 total_len = len + sizeof(*cmd); | |
46 | struct kvec iv; | |
47 | struct msghdr msg; | |
48 | ||
49 | cmd = __a2mp_build(code, ident, len, data); | |
50 | if (!cmd) | |
51 | return; | |
52 | ||
53 | iv.iov_base = cmd; | |
54 | iv.iov_len = total_len; | |
55 | ||
56 | memset(&msg, 0, sizeof(msg)); | |
57 | ||
58 | msg.msg_iov = (struct iovec *) &iv; | |
59 | msg.msg_iovlen = 1; | |
60 | ||
61 | l2cap_chan_send(chan, &msg, total_len, 0); | |
62 | ||
63 | kfree(cmd); | |
64 | } | |
65 | ||
8598d064 AE |
66 | static inline void __a2mp_cl_bredr(struct a2mp_cl *cl) |
67 | { | |
68 | cl->id = 0; | |
69 | cl->type = 0; | |
70 | cl->status = 1; | |
71 | } | |
72 | ||
73 | /* hci_dev_list shall be locked */ | |
74 | static void __a2mp_add_cl(struct amp_mgr *mgr, struct a2mp_cl *cl, u8 num_ctrl) | |
75 | { | |
76 | int i = 0; | |
77 | struct hci_dev *hdev; | |
78 | ||
79 | __a2mp_cl_bredr(cl); | |
80 | ||
81 | list_for_each_entry(hdev, &hci_dev_list, list) { | |
82 | /* Iterate through AMP controllers */ | |
83 | if (hdev->id == HCI_BREDR_ID) | |
84 | continue; | |
85 | ||
86 | /* Starting from second entry */ | |
87 | if (++i >= num_ctrl) | |
88 | return; | |
89 | ||
90 | cl[i].id = hdev->id; | |
91 | cl[i].type = hdev->amp_type; | |
92 | cl[i].status = hdev->amp_status; | |
93 | } | |
94 | } | |
95 | ||
21dbd2ce AE |
96 | /* Processing A2MP messages */ |
97 | static int a2mp_command_rej(struct amp_mgr *mgr, struct sk_buff *skb, | |
98 | struct a2mp_cmd *hdr) | |
99 | { | |
100 | struct a2mp_cmd_rej *rej = (void *) skb->data; | |
101 | ||
102 | if (le16_to_cpu(hdr->len) < sizeof(*rej)) | |
103 | return -EINVAL; | |
104 | ||
105 | BT_DBG("ident %d reason %d", hdr->ident, le16_to_cpu(rej->reason)); | |
106 | ||
107 | skb_pull(skb, sizeof(*rej)); | |
108 | ||
109 | return 0; | |
110 | } | |
111 | ||
8598d064 AE |
112 | static int a2mp_discover_req(struct amp_mgr *mgr, struct sk_buff *skb, |
113 | struct a2mp_cmd *hdr) | |
114 | { | |
115 | struct a2mp_discov_req *req = (void *) skb->data; | |
116 | u16 len = le16_to_cpu(hdr->len); | |
117 | struct a2mp_discov_rsp *rsp; | |
118 | u16 ext_feat; | |
119 | u8 num_ctrl; | |
120 | ||
121 | if (len < sizeof(*req)) | |
122 | return -EINVAL; | |
123 | ||
124 | skb_pull(skb, sizeof(*req)); | |
125 | ||
126 | ext_feat = le16_to_cpu(req->ext_feat); | |
127 | ||
128 | BT_DBG("mtu %d efm 0x%4.4x", le16_to_cpu(req->mtu), ext_feat); | |
129 | ||
130 | /* check that packet is not broken for now */ | |
131 | while (ext_feat & A2MP_FEAT_EXT) { | |
132 | if (len < sizeof(ext_feat)) | |
133 | return -EINVAL; | |
134 | ||
135 | ext_feat = get_unaligned_le16(skb->data); | |
136 | BT_DBG("efm 0x%4.4x", ext_feat); | |
137 | len -= sizeof(ext_feat); | |
138 | skb_pull(skb, sizeof(ext_feat)); | |
139 | } | |
140 | ||
141 | read_lock(&hci_dev_list_lock); | |
142 | ||
143 | num_ctrl = __hci_num_ctrl(); | |
144 | len = num_ctrl * sizeof(struct a2mp_cl) + sizeof(*rsp); | |
145 | rsp = kmalloc(len, GFP_ATOMIC); | |
146 | if (!rsp) { | |
147 | read_unlock(&hci_dev_list_lock); | |
148 | return -ENOMEM; | |
149 | } | |
150 | ||
151 | rsp->mtu = __constant_cpu_to_le16(L2CAP_A2MP_DEFAULT_MTU); | |
152 | rsp->ext_feat = 0; | |
153 | ||
154 | __a2mp_add_cl(mgr, rsp->cl, num_ctrl); | |
155 | ||
156 | read_unlock(&hci_dev_list_lock); | |
157 | ||
158 | a2mp_send(mgr, A2MP_DISCOVER_RSP, hdr->ident, len, rsp); | |
159 | ||
160 | kfree(rsp); | |
161 | return 0; | |
162 | } | |
163 | ||
6b44d9b8 AE |
164 | /* Handle A2MP signalling */ |
165 | static int a2mp_chan_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb) | |
166 | { | |
167 | struct a2mp_cmd *hdr = (void *) skb->data; | |
168 | struct amp_mgr *mgr = chan->data; | |
169 | int err = 0; | |
170 | ||
171 | amp_mgr_get(mgr); | |
172 | ||
173 | while (skb->len >= sizeof(*hdr)) { | |
174 | struct a2mp_cmd *hdr = (void *) skb->data; | |
175 | u16 len = le16_to_cpu(hdr->len); | |
176 | ||
177 | BT_DBG("code 0x%02x id %d len %d", hdr->code, hdr->ident, len); | |
178 | ||
179 | skb_pull(skb, sizeof(*hdr)); | |
180 | ||
181 | if (len > skb->len || !hdr->ident) { | |
182 | err = -EINVAL; | |
183 | break; | |
184 | } | |
185 | ||
186 | mgr->ident = hdr->ident; | |
187 | ||
188 | switch (hdr->code) { | |
189 | case A2MP_COMMAND_REJ: | |
21dbd2ce AE |
190 | a2mp_command_rej(mgr, skb, hdr); |
191 | break; | |
192 | ||
6b44d9b8 | 193 | case A2MP_DISCOVER_REQ: |
8598d064 AE |
194 | err = a2mp_discover_req(mgr, skb, hdr); |
195 | break; | |
196 | ||
6b44d9b8 AE |
197 | case A2MP_CHANGE_NOTIFY: |
198 | case A2MP_GETINFO_REQ: | |
199 | case A2MP_GETAMPASSOC_REQ: | |
200 | case A2MP_CREATEPHYSLINK_REQ: | |
201 | case A2MP_DISCONNPHYSLINK_REQ: | |
202 | case A2MP_CHANGE_RSP: | |
203 | case A2MP_DISCOVER_RSP: | |
204 | case A2MP_GETINFO_RSP: | |
205 | case A2MP_GETAMPASSOC_RSP: | |
206 | case A2MP_CREATEPHYSLINK_RSP: | |
207 | case A2MP_DISCONNPHYSLINK_RSP: | |
208 | default: | |
209 | BT_ERR("Unknown A2MP sig cmd 0x%2.2x", hdr->code); | |
210 | err = -EINVAL; | |
211 | break; | |
212 | } | |
213 | } | |
214 | ||
215 | if (err) { | |
216 | struct a2mp_cmd_rej rej; | |
217 | rej.reason = __constant_cpu_to_le16(0); | |
218 | ||
219 | BT_DBG("Send A2MP Rej: cmd 0x%2.2x err %d", hdr->code, err); | |
220 | ||
221 | a2mp_send(mgr, A2MP_COMMAND_REJ, hdr->ident, sizeof(rej), | |
222 | &rej); | |
223 | } | |
224 | ||
225 | /* Always free skb and return success error code to prevent | |
226 | from sending L2CAP Disconnect over A2MP channel */ | |
227 | kfree_skb(skb); | |
228 | ||
229 | amp_mgr_put(mgr); | |
230 | ||
231 | return 0; | |
232 | } | |
233 | ||
46d5c908 AE |
234 | static void a2mp_chan_close_cb(struct l2cap_chan *chan) |
235 | { | |
236 | l2cap_chan_destroy(chan); | |
237 | } | |
238 | ||
239 | static void a2mp_chan_state_change_cb(struct l2cap_chan *chan, int state) | |
240 | { | |
241 | struct amp_mgr *mgr = chan->data; | |
242 | ||
243 | if (!mgr) | |
244 | return; | |
245 | ||
246 | BT_DBG("chan %p state %s", chan, state_to_string(state)); | |
247 | ||
248 | chan->state = state; | |
249 | ||
250 | switch (state) { | |
251 | case BT_CLOSED: | |
252 | if (mgr) | |
253 | amp_mgr_put(mgr); | |
254 | break; | |
255 | } | |
256 | } | |
257 | ||
258 | static struct sk_buff *a2mp_chan_alloc_skb_cb(struct l2cap_chan *chan, | |
259 | unsigned long len, int nb) | |
260 | { | |
261 | return bt_skb_alloc(len, GFP_KERNEL); | |
262 | } | |
263 | ||
264 | static struct l2cap_chan *a2mp_chan_no_new_conn_cb(struct l2cap_chan *chan) | |
265 | { | |
266 | BT_ERR("new_connection for chan %p not implemented", chan); | |
267 | ||
268 | return NULL; | |
269 | } | |
270 | ||
271 | static void a2mp_chan_no_teardown_cb(struct l2cap_chan *chan, int err) | |
272 | { | |
273 | BT_ERR("teardown for chan %p not implemented", chan); | |
274 | } | |
275 | ||
276 | static void a2mp_chan_no_ready(struct l2cap_chan *chan) | |
277 | { | |
278 | BT_ERR("ready for chan %p not implemented", chan); | |
279 | } | |
280 | ||
466f8004 AE |
281 | static struct l2cap_ops a2mp_chan_ops = { |
282 | .name = "L2CAP A2MP channel", | |
6b44d9b8 | 283 | .recv = a2mp_chan_recv_cb, |
46d5c908 AE |
284 | .close = a2mp_chan_close_cb, |
285 | .state_change = a2mp_chan_state_change_cb, | |
286 | .alloc_skb = a2mp_chan_alloc_skb_cb, | |
287 | ||
288 | /* Not implemented for A2MP */ | |
289 | .new_connection = a2mp_chan_no_new_conn_cb, | |
290 | .teardown = a2mp_chan_no_teardown_cb, | |
291 | .ready = a2mp_chan_no_ready, | |
466f8004 AE |
292 | }; |
293 | ||
294 | static struct l2cap_chan *a2mp_chan_open(struct l2cap_conn *conn) | |
295 | { | |
296 | struct l2cap_chan *chan; | |
297 | int err; | |
298 | ||
299 | chan = l2cap_chan_create(); | |
300 | if (!chan) | |
301 | return NULL; | |
302 | ||
303 | BT_DBG("chan %p", chan); | |
304 | ||
305 | hci_conn_hold(conn->hcon); | |
306 | ||
307 | chan->omtu = L2CAP_A2MP_DEFAULT_MTU; | |
308 | chan->imtu = L2CAP_A2MP_DEFAULT_MTU; | |
309 | chan->flush_to = L2CAP_DEFAULT_FLUSH_TO; | |
310 | ||
311 | chan->ops = &a2mp_chan_ops; | |
312 | ||
313 | l2cap_chan_set_defaults(chan); | |
314 | chan->remote_max_tx = chan->max_tx; | |
315 | chan->remote_tx_win = chan->tx_win; | |
316 | ||
317 | chan->retrans_timeout = L2CAP_DEFAULT_RETRANS_TO; | |
318 | chan->monitor_timeout = L2CAP_DEFAULT_MONITOR_TO; | |
319 | ||
320 | skb_queue_head_init(&chan->tx_q); | |
321 | ||
322 | chan->mode = L2CAP_MODE_ERTM; | |
323 | ||
324 | err = l2cap_ertm_init(chan); | |
325 | if (err < 0) { | |
326 | l2cap_chan_del(chan, 0); | |
327 | return NULL; | |
328 | } | |
329 | ||
330 | chan->conf_state = 0; | |
331 | ||
332 | l2cap_chan_add(conn, chan); | |
333 | ||
334 | chan->remote_mps = chan->omtu; | |
335 | chan->mps = chan->omtu; | |
336 | ||
337 | chan->state = BT_CONNECTED; | |
338 | ||
339 | return chan; | |
340 | } | |
9740e49d AE |
341 | |
342 | /* AMP Manager functions */ | |
343 | void amp_mgr_get(struct amp_mgr *mgr) | |
344 | { | |
345 | BT_DBG("mgr %p", mgr); | |
346 | ||
347 | kref_get(&mgr->kref); | |
348 | } | |
349 | ||
350 | static void amp_mgr_destroy(struct kref *kref) | |
351 | { | |
352 | struct amp_mgr *mgr = container_of(kref, struct amp_mgr, kref); | |
353 | ||
354 | BT_DBG("mgr %p", mgr); | |
355 | ||
356 | kfree(mgr); | |
357 | } | |
358 | ||
359 | int amp_mgr_put(struct amp_mgr *mgr) | |
360 | { | |
361 | BT_DBG("mgr %p", mgr); | |
362 | ||
363 | return kref_put(&mgr->kref, &_mgr_destroy); | |
364 | } | |
365 | ||
366 | static struct amp_mgr *amp_mgr_create(struct l2cap_conn *conn) | |
367 | { | |
368 | struct amp_mgr *mgr; | |
369 | struct l2cap_chan *chan; | |
370 | ||
371 | mgr = kzalloc(sizeof(*mgr), GFP_KERNEL); | |
372 | if (!mgr) | |
373 | return NULL; | |
374 | ||
375 | BT_DBG("conn %p mgr %p", conn, mgr); | |
376 | ||
377 | mgr->l2cap_conn = conn; | |
378 | ||
379 | chan = a2mp_chan_open(conn); | |
380 | if (!chan) { | |
381 | kfree(mgr); | |
382 | return NULL; | |
383 | } | |
384 | ||
385 | mgr->a2mp_chan = chan; | |
386 | chan->data = mgr; | |
387 | ||
388 | conn->hcon->amp_mgr = mgr; | |
389 | ||
390 | kref_init(&mgr->kref); | |
391 | ||
392 | return mgr; | |
393 | } |