Blame view

net/bluetooth/a2mp.c 21.5 KB
466f8004f   Andrei Emeltchenko   Bluetooth: A2MP: ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  /*
     Copyright (c) 2010,2011 Code Aurora Forum.  All rights reserved.
     Copyright (c) 2011,2012 Intel Corp.
  
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License version 2 and
     only version 2 as published by the Free Software Foundation.
  
     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.
  */
  
  #include <net/bluetooth/bluetooth.h>
  #include <net/bluetooth/hci_core.h>
  #include <net/bluetooth/l2cap.h>
7ef9fbf08   Marcel Holtmann   Bluetooth: Move a...
18

7024728ee   Marcel Holtmann   Bluetooth: Move a...
19
  #include "a2mp.h"
7ef9fbf08   Marcel Holtmann   Bluetooth: Move a...
20
  #include "amp.h"
466f8004f   Andrei Emeltchenko   Bluetooth: A2MP: ...
21

f97268fcc   Andrei Emeltchenko   Bluetooth: A2MP: ...
22
23
24
  /* Global AMP Manager list */
  LIST_HEAD(amp_mgr_list);
  DEFINE_MUTEX(amp_mgr_list_lock);
f6d3c6e78   Andrei Emeltchenko   Bluetooth: A2MP: ...
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  /* A2MP build & send command helper functions */
  static struct a2mp_cmd *__a2mp_build(u8 code, u8 ident, u16 len, void *data)
  {
  	struct a2mp_cmd *cmd;
  	int plen;
  
  	plen = sizeof(*cmd) + len;
  	cmd = kzalloc(plen, GFP_KERNEL);
  	if (!cmd)
  		return NULL;
  
  	cmd->code = code;
  	cmd->ident = ident;
  	cmd->len = cpu_to_le16(len);
  
  	memcpy(cmd->data, data, len);
  
  	return cmd;
  }
8e2a0d92c   Andrei Emeltchenko   Bluetooth: AMP: U...
44
  void a2mp_send(struct amp_mgr *mgr, u8 code, u8 ident, u16 len, void *data)
f6d3c6e78   Andrei Emeltchenko   Bluetooth: A2MP: ...
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  {
  	struct l2cap_chan *chan = mgr->a2mp_chan;
  	struct a2mp_cmd *cmd;
  	u16 total_len = len + sizeof(*cmd);
  	struct kvec iv;
  	struct msghdr msg;
  
  	cmd = __a2mp_build(code, ident, len, data);
  	if (!cmd)
  		return;
  
  	iv.iov_base = cmd;
  	iv.iov_len = total_len;
  
  	memset(&msg, 0, sizeof(msg));
  
  	msg.msg_iov = (struct iovec *) &iv;
  	msg.msg_iovlen = 1;
8d46321c4   Marcel Holtmann   Bluetooth: Assign...
63
  	l2cap_chan_send(chan, &msg, total_len);
f6d3c6e78   Andrei Emeltchenko   Bluetooth: A2MP: ...
64
65
66
  
  	kfree(cmd);
  }
9495b2ee7   Andrei Emeltchenko   Bluetooth: AMP: P...
67
  u8 __next_ident(struct amp_mgr *mgr)
aa09537d8   Andrei Emeltchenko   Bluetooth: A2MP: ...
68
69
70
71
72
73
  {
  	if (++mgr->ident == 0)
  		mgr->ident = 1;
  
  	return mgr->ident;
  }
8598d064c   Andrei Emeltchenko   Bluetooth: A2MP: ...
74
  /* hci_dev_list shall be locked */
23f0cb41a   Marcel Holtmann   Bluetooth: Remove...
75
  static void __a2mp_add_cl(struct amp_mgr *mgr, struct a2mp_cl *cl)
8598d064c   Andrei Emeltchenko   Bluetooth: A2MP: ...
76
  {
8598d064c   Andrei Emeltchenko   Bluetooth: A2MP: ...
77
  	struct hci_dev *hdev;
e8803534a   Marcel Holtmann   Bluetooth: Simpli...
78
  	int i = 1;
8598d064c   Andrei Emeltchenko   Bluetooth: A2MP: ...
79

346e7099c   Marcel Holtmann   Bluetooth: Remove...
80
81
82
  	cl[0].id = AMP_ID_BREDR;
  	cl[0].type = AMP_TYPE_BREDR;
  	cl[0].status = AMP_STATUS_BLUETOOTH_ONLY;
8598d064c   Andrei Emeltchenko   Bluetooth: A2MP: ...
83
84
  
  	list_for_each_entry(hdev, &hci_dev_list, list) {
e8803534a   Marcel Holtmann   Bluetooth: Simpli...
85
86
87
  		if (hdev->dev_type == HCI_AMP) {
  			cl[i].id = hdev->id;
  			cl[i].type = hdev->amp_type;
cd0a85c22   Marcel Holtmann   Bluetooth: List p...
88
89
90
91
  			if (test_bit(HCI_UP, &hdev->flags))
  				cl[i].status = hdev->amp_status;
  			else
  				cl[i].status = AMP_STATUS_POWERED_DOWN;
e8803534a   Marcel Holtmann   Bluetooth: Simpli...
92
93
  			i++;
  		}
8598d064c   Andrei Emeltchenko   Bluetooth: A2MP: ...
94
95
  	}
  }
21dbd2ce3   Andrei Emeltchenko   Bluetooth: A2MP: ...
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
  /* Processing A2MP messages */
  static int a2mp_command_rej(struct amp_mgr *mgr, struct sk_buff *skb,
  			    struct a2mp_cmd *hdr)
  {
  	struct a2mp_cmd_rej *rej = (void *) skb->data;
  
  	if (le16_to_cpu(hdr->len) < sizeof(*rej))
  		return -EINVAL;
  
  	BT_DBG("ident %d reason %d", hdr->ident, le16_to_cpu(rej->reason));
  
  	skb_pull(skb, sizeof(*rej));
  
  	return 0;
  }
8598d064c   Andrei Emeltchenko   Bluetooth: A2MP: ...
111
112
113
114
115
116
117
118
  static int a2mp_discover_req(struct amp_mgr *mgr, struct sk_buff *skb,
  			     struct a2mp_cmd *hdr)
  {
  	struct a2mp_discov_req *req = (void *) skb->data;
  	u16 len = le16_to_cpu(hdr->len);
  	struct a2mp_discov_rsp *rsp;
  	u16 ext_feat;
  	u8 num_ctrl;
f822c411b   Marcel Holtmann   Bluetooth: Remove...
119
  	struct hci_dev *hdev;
8598d064c   Andrei Emeltchenko   Bluetooth: A2MP: ...
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
  
  	if (len < sizeof(*req))
  		return -EINVAL;
  
  	skb_pull(skb, sizeof(*req));
  
  	ext_feat = le16_to_cpu(req->ext_feat);
  
  	BT_DBG("mtu %d efm 0x%4.4x", le16_to_cpu(req->mtu), ext_feat);
  
  	/* check that packet is not broken for now */
  	while (ext_feat & A2MP_FEAT_EXT) {
  		if (len < sizeof(ext_feat))
  			return -EINVAL;
  
  		ext_feat = get_unaligned_le16(skb->data);
  		BT_DBG("efm 0x%4.4x", ext_feat);
  		len -= sizeof(ext_feat);
  		skb_pull(skb, sizeof(ext_feat));
  	}
  
  	read_lock(&hci_dev_list_lock);
f822c411b   Marcel Holtmann   Bluetooth: Remove...
142
143
144
145
146
147
148
  	/* at minimum the BR/EDR needs to be listed */
  	num_ctrl = 1;
  
  	list_for_each_entry(hdev, &hci_dev_list, list) {
  		if (hdev->dev_type == HCI_AMP)
  			num_ctrl++;
  	}
8598d064c   Andrei Emeltchenko   Bluetooth: A2MP: ...
149
150
151
152
153
154
  	len = num_ctrl * sizeof(struct a2mp_cl) + sizeof(*rsp);
  	rsp = kmalloc(len, GFP_ATOMIC);
  	if (!rsp) {
  		read_unlock(&hci_dev_list_lock);
  		return -ENOMEM;
  	}
dcf4adbfd   Joe Perches   Bluetooth: Conver...
155
  	rsp->mtu = cpu_to_le16(L2CAP_A2MP_DEFAULT_MTU);
8598d064c   Andrei Emeltchenko   Bluetooth: A2MP: ...
156
  	rsp->ext_feat = 0;
23f0cb41a   Marcel Holtmann   Bluetooth: Remove...
157
  	__a2mp_add_cl(mgr, rsp->cl);
8598d064c   Andrei Emeltchenko   Bluetooth: A2MP: ...
158
159
160
161
162
163
164
165
  
  	read_unlock(&hci_dev_list_lock);
  
  	a2mp_send(mgr, A2MP_DISCOVER_RSP, hdr->ident, len, rsp);
  
  	kfree(rsp);
  	return 0;
  }
aa09537d8   Andrei Emeltchenko   Bluetooth: A2MP: ...
166
167
168
169
170
171
172
  static int a2mp_discover_rsp(struct amp_mgr *mgr, struct sk_buff *skb,
  			     struct a2mp_cmd *hdr)
  {
  	struct a2mp_discov_rsp *rsp = (void *) skb->data;
  	u16 len = le16_to_cpu(hdr->len);
  	struct a2mp_cl *cl;
  	u16 ext_feat;
2766be48a   Andrei Emeltchenko   Bluetooth: A2MP: ...
173
  	bool found = false;
aa09537d8   Andrei Emeltchenko   Bluetooth: A2MP: ...
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
  
  	if (len < sizeof(*rsp))
  		return -EINVAL;
  
  	len -= sizeof(*rsp);
  	skb_pull(skb, sizeof(*rsp));
  
  	ext_feat = le16_to_cpu(rsp->ext_feat);
  
  	BT_DBG("mtu %d efm 0x%4.4x", le16_to_cpu(rsp->mtu), ext_feat);
  
  	/* check that packet is not broken for now */
  	while (ext_feat & A2MP_FEAT_EXT) {
  		if (len < sizeof(ext_feat))
  			return -EINVAL;
  
  		ext_feat = get_unaligned_le16(skb->data);
  		BT_DBG("efm 0x%4.4x", ext_feat);
  		len -= sizeof(ext_feat);
  		skb_pull(skb, sizeof(ext_feat));
  	}
  
  	cl = (void *) skb->data;
  	while (len >= sizeof(*cl)) {
  		BT_DBG("Remote AMP id %d type %d status %d", cl->id, cl->type,
  		       cl->status);
a646bd819   Marcel Holtmann   Bluetooth: Check ...
200
  		if (cl->id != AMP_ID_BREDR && cl->type != AMP_TYPE_BREDR) {
aa09537d8   Andrei Emeltchenko   Bluetooth: A2MP: ...
201
  			struct a2mp_info_req req;
2766be48a   Andrei Emeltchenko   Bluetooth: A2MP: ...
202
  			found = true;
aa09537d8   Andrei Emeltchenko   Bluetooth: A2MP: ...
203
204
205
206
207
208
209
210
  			req.id = cl->id;
  			a2mp_send(mgr, A2MP_GETINFO_REQ, __next_ident(mgr),
  				  sizeof(req), &req);
  		}
  
  		len -= sizeof(*cl);
  		cl = (void *) skb_pull(skb, sizeof(*cl));
  	}
2766be48a   Andrei Emeltchenko   Bluetooth: A2MP: ...
211
212
213
214
215
216
217
218
219
220
221
  	/* Fall back to L2CAP init sequence */
  	if (!found) {
  		struct l2cap_conn *conn = mgr->l2cap_conn;
  		struct l2cap_chan *chan;
  
  		mutex_lock(&conn->chan_lock);
  
  		list_for_each_entry(chan, &conn->chan_l, list) {
  
  			BT_DBG("chan %p state %s", chan,
  			       state_to_string(chan->state));
2338a7e04   Johan Hedberg   Bluetooth: Rename...
222
  			if (chan->scid == L2CAP_CID_A2MP)
2766be48a   Andrei Emeltchenko   Bluetooth: A2MP: ...
223
224
225
226
227
228
229
230
231
232
233
234
  				continue;
  
  			l2cap_chan_lock(chan);
  
  			if (chan->state == BT_CONNECT)
  				l2cap_send_conn_req(chan);
  
  			l2cap_chan_unlock(chan);
  		}
  
  		mutex_unlock(&conn->chan_lock);
  	}
aa09537d8   Andrei Emeltchenko   Bluetooth: A2MP: ...
235
236
  	return 0;
  }
329d81af2   Andrei Emeltchenko   Bluetooth: A2MP: ...
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
  static int a2mp_change_notify(struct amp_mgr *mgr, struct sk_buff *skb,
  			      struct a2mp_cmd *hdr)
  {
  	struct a2mp_cl *cl = (void *) skb->data;
  
  	while (skb->len >= sizeof(*cl)) {
  		BT_DBG("Controller id %d type %d status %d", cl->id, cl->type,
  		       cl->status);
  		cl = (struct a2mp_cl *) skb_pull(skb, sizeof(*cl));
  	}
  
  	/* TODO send A2MP_CHANGE_RSP */
  
  	return 0;
  }
47f2d97d3   Andrei Emeltchenko   Bluetooth: A2MP: ...
252
253
254
255
  static int a2mp_getinfo_req(struct amp_mgr *mgr, struct sk_buff *skb,
  			    struct a2mp_cmd *hdr)
  {
  	struct a2mp_info_req *req  = (void *) skb->data;
47f2d97d3   Andrei Emeltchenko   Bluetooth: A2MP: ...
256
257
258
259
260
261
  	struct hci_dev *hdev;
  
  	if (le16_to_cpu(hdr->len) < sizeof(*req))
  		return -EINVAL;
  
  	BT_DBG("id %d", req->id);
47f2d97d3   Andrei Emeltchenko   Bluetooth: A2MP: ...
262
  	hdev = hci_dev_get(req->id);
bc8dce4f7   Andrei Emeltchenko   Bluetooth: A2MP: ...
263
  	if (!hdev || hdev->dev_type != HCI_AMP) {
8e2a0d92c   Andrei Emeltchenko   Bluetooth: AMP: U...
264
265
266
267
268
269
270
  		struct a2mp_info_rsp rsp;
  
  		rsp.id = req->id;
  		rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
  
  		a2mp_send(mgr, A2MP_GETINFO_RSP, hdr->ident, sizeof(rsp),
  			  &rsp);
47f2d97d3   Andrei Emeltchenko   Bluetooth: A2MP: ...
271

bc8dce4f7   Andrei Emeltchenko   Bluetooth: A2MP: ...
272
  		goto done;
8e2a0d92c   Andrei Emeltchenko   Bluetooth: AMP: U...
273
  	}
47f2d97d3   Andrei Emeltchenko   Bluetooth: A2MP: ...
274

cb6801c64   Andrei Emeltchenko   Bluetooth: AMP: U...
275
  	set_bit(READ_LOC_AMP_INFO, &mgr->state);
bc8dce4f7   Andrei Emeltchenko   Bluetooth: A2MP: ...
276
277
278
279
280
  	hci_send_cmd(hdev, HCI_OP_READ_LOCAL_AMP_INFO, 0, NULL);
  
  done:
  	if (hdev)
  		hci_dev_put(hdev);
47f2d97d3   Andrei Emeltchenko   Bluetooth: A2MP: ...
281
282
283
284
  
  	skb_pull(skb, sizeof(*req));
  	return 0;
  }
0d868de9d   Andrei Emeltchenko   Bluetooth: A2MP: ...
285
286
287
288
289
290
291
292
293
294
295
296
297
298
  static int a2mp_getinfo_rsp(struct amp_mgr *mgr, struct sk_buff *skb,
  			    struct a2mp_cmd *hdr)
  {
  	struct a2mp_info_rsp *rsp = (struct a2mp_info_rsp *) skb->data;
  	struct a2mp_amp_assoc_req req;
  	struct amp_ctrl *ctrl;
  
  	if (le16_to_cpu(hdr->len) < sizeof(*rsp))
  		return -EINVAL;
  
  	BT_DBG("id %d status 0x%2.2x", rsp->id, rsp->status);
  
  	if (rsp->status)
  		return -EINVAL;
fa4ebc66c   Andrei Emeltchenko   Bluetooth: AMP: F...
299
  	ctrl = amp_ctrl_add(mgr, rsp->id);
0d868de9d   Andrei Emeltchenko   Bluetooth: A2MP: ...
300
301
  	if (!ctrl)
  		return -ENOMEM;
0d868de9d   Andrei Emeltchenko   Bluetooth: A2MP: ...
302
303
304
305
306
307
308
  	req.id = rsp->id;
  	a2mp_send(mgr, A2MP_GETAMPASSOC_REQ, __next_ident(mgr), sizeof(req),
  		  &req);
  
  	skb_pull(skb, sizeof(*rsp));
  	return 0;
  }
a28381dc9   Andrei Emeltchenko   Bluetooth: A2MP: ...
309
310
311
312
313
  static int a2mp_getampassoc_req(struct amp_mgr *mgr, struct sk_buff *skb,
  				struct a2mp_cmd *hdr)
  {
  	struct a2mp_amp_assoc_req *req = (void *) skb->data;
  	struct hci_dev *hdev;
903e45411   Andrei Emeltchenko   Bluetooth: AMP: U...
314
  	struct amp_mgr *tmp;
a28381dc9   Andrei Emeltchenko   Bluetooth: A2MP: ...
315
316
317
318
319
  
  	if (le16_to_cpu(hdr->len) < sizeof(*req))
  		return -EINVAL;
  
  	BT_DBG("id %d", req->id);
903e45411   Andrei Emeltchenko   Bluetooth: AMP: U...
320
321
  	/* Make sure that other request is not processed */
  	tmp = amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC);
a28381dc9   Andrei Emeltchenko   Bluetooth: A2MP: ...
322
  	hdev = hci_dev_get(req->id);
ece691264   Marcel Holtmann   Bluetooth: Separa...
323
  	if (!hdev || hdev->amp_type == AMP_TYPE_BREDR || tmp) {
a28381dc9   Andrei Emeltchenko   Bluetooth: A2MP: ...
324
325
  		struct a2mp_amp_assoc_rsp rsp;
  		rsp.id = req->id;
903e45411   Andrei Emeltchenko   Bluetooth: AMP: U...
326
327
328
329
330
331
332
  
  		if (tmp) {
  			rsp.status = A2MP_STATUS_COLLISION_OCCURED;
  			amp_mgr_put(tmp);
  		} else {
  			rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
  		}
a28381dc9   Andrei Emeltchenko   Bluetooth: A2MP: ...
333
334
335
  
  		a2mp_send(mgr, A2MP_GETAMPASSOC_RSP, hdr->ident, sizeof(rsp),
  			  &rsp);
903e45411   Andrei Emeltchenko   Bluetooth: AMP: U...
336
337
  
  		goto done;
a28381dc9   Andrei Emeltchenko   Bluetooth: A2MP: ...
338
  	}
903e45411   Andrei Emeltchenko   Bluetooth: AMP: U...
339
  	amp_read_loc_assoc(hdev, mgr);
a28381dc9   Andrei Emeltchenko   Bluetooth: A2MP: ...
340

903e45411   Andrei Emeltchenko   Bluetooth: AMP: U...
341
  done:
a28381dc9   Andrei Emeltchenko   Bluetooth: A2MP: ...
342
343
344
345
346
347
  	if (hdev)
  		hci_dev_put(hdev);
  
  	skb_pull(skb, sizeof(*req));
  	return 0;
  }
9a5e94dbb   Andrei Emeltchenko   Bluetooth: A2MP: ...
348
349
350
351
352
353
354
355
  static int a2mp_getampassoc_rsp(struct amp_mgr *mgr, struct sk_buff *skb,
  				struct a2mp_cmd *hdr)
  {
  	struct a2mp_amp_assoc_rsp *rsp = (void *) skb->data;
  	u16 len = le16_to_cpu(hdr->len);
  	struct hci_dev *hdev;
  	struct amp_ctrl *ctrl;
  	struct hci_conn *hcon;
13465c0ae   Andrei Emeltchenko   Bluetooth: A2MP: ...
356
  	size_t assoc_len;
9a5e94dbb   Andrei Emeltchenko   Bluetooth: A2MP: ...
357
358
359
  
  	if (len < sizeof(*rsp))
  		return -EINVAL;
13465c0ae   Andrei Emeltchenko   Bluetooth: A2MP: ...
360
361
362
363
  	assoc_len = len - sizeof(*rsp);
  
  	BT_DBG("id %d status 0x%2.2x assoc len %zu", rsp->id, rsp->status,
  	       assoc_len);
9a5e94dbb   Andrei Emeltchenko   Bluetooth: A2MP: ...
364
365
366
367
368
369
370
  
  	if (rsp->status)
  		return -EINVAL;
  
  	/* Save remote ASSOC data */
  	ctrl = amp_ctrl_lookup(mgr, rsp->id);
  	if (ctrl) {
13465c0ae   Andrei Emeltchenko   Bluetooth: A2MP: ...
371
  		u8 *assoc;
9a5e94dbb   Andrei Emeltchenko   Bluetooth: A2MP: ...
372

5ae327f0e   Alexandru Gheorghiu   Bluetooth: Replac...
373
  		assoc = kmemdup(rsp->amp_assoc, assoc_len, GFP_KERNEL);
9a5e94dbb   Andrei Emeltchenko   Bluetooth: A2MP: ...
374
375
376
377
  		if (!assoc) {
  			amp_ctrl_put(ctrl);
  			return -ENOMEM;
  		}
9a5e94dbb   Andrei Emeltchenko   Bluetooth: A2MP: ...
378
379
380
381
382
383
384
385
386
387
388
389
  		ctrl->assoc = assoc;
  		ctrl->assoc_len = assoc_len;
  		ctrl->assoc_rem_len = assoc_len;
  		ctrl->assoc_len_so_far = 0;
  
  		amp_ctrl_put(ctrl);
  	}
  
  	/* Create Phys Link */
  	hdev = hci_dev_get(rsp->id);
  	if (!hdev)
  		return -EINVAL;
a0c234fe8   Andrei Emeltchenko   Bluetooth: AMP: F...
390
  	hcon = phylink_add(hdev, mgr, rsp->id, true);
9a5e94dbb   Andrei Emeltchenko   Bluetooth: A2MP: ...
391
392
393
394
  	if (!hcon)
  		goto done;
  
  	BT_DBG("Created hcon %p: loc:%d -> rem:%d", hcon, hdev->id, rsp->id);
fffadc08e   Andrei Emeltchenko   Bluetooth: Rename...
395
  	mgr->bredr_chan->remote_amp_id = rsp->id;
9495b2ee7   Andrei Emeltchenko   Bluetooth: AMP: P...
396

a02226d6f   Andrei Emeltchenko   Bluetooth: AMP: C...
397
  	amp_create_phylink(hdev, mgr, hcon);
9a5e94dbb   Andrei Emeltchenko   Bluetooth: A2MP: ...
398
399
400
401
402
  done:
  	hci_dev_put(hdev);
  	skb_pull(skb, len);
  	return 0;
  }
e072f5dab   Andrei Emeltchenko   Bluetooth: A2MP: ...
403
404
405
406
407
408
409
  static int a2mp_createphyslink_req(struct amp_mgr *mgr, struct sk_buff *skb,
  				   struct a2mp_cmd *hdr)
  {
  	struct a2mp_physlink_req *req = (void *) skb->data;
  
  	struct a2mp_physlink_rsp rsp;
  	struct hci_dev *hdev;
cb8488c0b   Andrei Emeltchenko   Bluetooth: AMP: H...
410
  	struct hci_conn *hcon;
0b26ab9dc   Andrei Emeltchenko   Bluetooth: AMP: H...
411
  	struct amp_ctrl *ctrl;
e072f5dab   Andrei Emeltchenko   Bluetooth: A2MP: ...
412
413
414
415
416
417
418
419
420
421
  
  	if (le16_to_cpu(hdr->len) < sizeof(*req))
  		return -EINVAL;
  
  	BT_DBG("local_id %d, remote_id %d", req->local_id, req->remote_id);
  
  	rsp.local_id = req->remote_id;
  	rsp.remote_id = req->local_id;
  
  	hdev = hci_dev_get(req->remote_id);
ece691264   Marcel Holtmann   Bluetooth: Separa...
422
  	if (!hdev || hdev->amp_type == AMP_TYPE_BREDR) {
e072f5dab   Andrei Emeltchenko   Bluetooth: A2MP: ...
423
424
425
  		rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
  		goto send_rsp;
  	}
0b26ab9dc   Andrei Emeltchenko   Bluetooth: AMP: H...
426
427
  	ctrl = amp_ctrl_lookup(mgr, rsp.remote_id);
  	if (!ctrl) {
fa4ebc66c   Andrei Emeltchenko   Bluetooth: AMP: F...
428
  		ctrl = amp_ctrl_add(mgr, rsp.remote_id);
0b26ab9dc   Andrei Emeltchenko   Bluetooth: AMP: H...
429
430
431
432
433
434
435
436
437
  		if (ctrl) {
  			amp_ctrl_get(ctrl);
  		} else {
  			rsp.status = A2MP_STATUS_UNABLE_START_LINK_CREATION;
  			goto send_rsp;
  		}
  	}
  
  	if (ctrl) {
13465c0ae   Andrei Emeltchenko   Bluetooth: A2MP: ...
438
439
  		size_t assoc_len = le16_to_cpu(hdr->len) - sizeof(*req);
  		u8 *assoc;
0b26ab9dc   Andrei Emeltchenko   Bluetooth: AMP: H...
440

5ae327f0e   Alexandru Gheorghiu   Bluetooth: Replac...
441
  		assoc = kmemdup(req->amp_assoc, assoc_len, GFP_KERNEL);
0b26ab9dc   Andrei Emeltchenko   Bluetooth: AMP: H...
442
443
444
445
  		if (!assoc) {
  			amp_ctrl_put(ctrl);
  			return -ENOMEM;
  		}
0b26ab9dc   Andrei Emeltchenko   Bluetooth: AMP: H...
446
447
448
449
450
451
452
  		ctrl->assoc = assoc;
  		ctrl->assoc_len = assoc_len;
  		ctrl->assoc_rem_len = assoc_len;
  		ctrl->assoc_len_so_far = 0;
  
  		amp_ctrl_put(ctrl);
  	}
a0c234fe8   Andrei Emeltchenko   Bluetooth: AMP: F...
453
  	hcon = phylink_add(hdev, mgr, req->local_id, false);
cb8488c0b   Andrei Emeltchenko   Bluetooth: AMP: H...
454
  	if (hcon) {
dffa38711   Andrei Emeltchenko   Bluetooth: AMP: A...
455
  		amp_accept_phylink(hdev, mgr, hcon);
cb8488c0b   Andrei Emeltchenko   Bluetooth: AMP: H...
456
457
458
459
  		rsp.status = A2MP_STATUS_SUCCESS;
  	} else {
  		rsp.status = A2MP_STATUS_UNABLE_START_LINK_CREATION;
  	}
e072f5dab   Andrei Emeltchenko   Bluetooth: A2MP: ...
460
461
462
463
  
  send_rsp:
  	if (hdev)
  		hci_dev_put(hdev);
8e05e3ba8   Andrei Emeltchenko   Bluetooth: AMP: S...
464
465
466
467
468
469
470
  	/* Reply error now and success after HCI Write Remote AMP Assoc
  	   command complete with success status
  	 */
  	if (rsp.status != A2MP_STATUS_SUCCESS) {
  		a2mp_send(mgr, A2MP_CREATEPHYSLINK_RSP, hdr->ident,
  			  sizeof(rsp), &rsp);
  	} else {
cb6801c64   Andrei Emeltchenko   Bluetooth: AMP: U...
471
  		set_bit(WRITE_REMOTE_AMP_ASSOC, &mgr->state);
8e05e3ba8   Andrei Emeltchenko   Bluetooth: AMP: S...
472
473
  		mgr->ident = hdr->ident;
  	}
e072f5dab   Andrei Emeltchenko   Bluetooth: A2MP: ...
474
475
476
477
  
  	skb_pull(skb, le16_to_cpu(hdr->len));
  	return 0;
  }
6113f84fc   Andrei Emeltchenko   Bluetooth: A2MP: ...
478
479
480
481
482
483
  static int a2mp_discphyslink_req(struct amp_mgr *mgr, struct sk_buff *skb,
  				 struct a2mp_cmd *hdr)
  {
  	struct a2mp_physlink_req *req = (void *) skb->data;
  	struct a2mp_physlink_rsp rsp;
  	struct hci_dev *hdev;
cb8488c0b   Andrei Emeltchenko   Bluetooth: AMP: H...
484
  	struct hci_conn *hcon;
6113f84fc   Andrei Emeltchenko   Bluetooth: A2MP: ...
485
486
487
488
489
490
491
492
493
  
  	if (le16_to_cpu(hdr->len) < sizeof(*req))
  		return -EINVAL;
  
  	BT_DBG("local_id %d remote_id %d", req->local_id, req->remote_id);
  
  	rsp.local_id = req->remote_id;
  	rsp.remote_id = req->local_id;
  	rsp.status = A2MP_STATUS_SUCCESS;
cb8488c0b   Andrei Emeltchenko   Bluetooth: AMP: H...
494
  	hdev = hci_dev_get(req->remote_id);
6113f84fc   Andrei Emeltchenko   Bluetooth: A2MP: ...
495
496
497
498
  	if (!hdev) {
  		rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
  		goto send_rsp;
  	}
bdc8ead27   Marcel Holtmann   Bluetooth: Remove...
499
500
  	hcon = hci_conn_hash_lookup_ba(hdev, AMP_LINK,
  				       &mgr->l2cap_conn->hcon->dst);
cb8488c0b   Andrei Emeltchenko   Bluetooth: AMP: H...
501
502
503
504
505
  	if (!hcon) {
  		BT_ERR("No phys link exist");
  		rsp.status = A2MP_STATUS_NO_PHYSICAL_LINK_EXISTS;
  		goto clean;
  	}
6113f84fc   Andrei Emeltchenko   Bluetooth: A2MP: ...
506
  	/* TODO Disconnect Phys Link here */
cb8488c0b   Andrei Emeltchenko   Bluetooth: AMP: H...
507
  clean:
6113f84fc   Andrei Emeltchenko   Bluetooth: A2MP: ...
508
509
510
511
512
513
514
515
  	hci_dev_put(hdev);
  
  send_rsp:
  	a2mp_send(mgr, A2MP_DISCONNPHYSLINK_RSP, hdr->ident, sizeof(rsp), &rsp);
  
  	skb_pull(skb, sizeof(*req));
  	return 0;
  }
f6410a849   Andrei Emeltchenko   Bluetooth: A2MP: ...
516
517
518
  static inline int a2mp_cmd_rsp(struct amp_mgr *mgr, struct sk_buff *skb,
  			       struct a2mp_cmd *hdr)
  {
8e8c7e36f   Andrei Emeltchenko   Bluetooth: debug:...
519
  	BT_DBG("ident %d code 0x%2.2x", hdr->ident, hdr->code);
f6410a849   Andrei Emeltchenko   Bluetooth: A2MP: ...
520
521
522
523
  
  	skb_pull(skb, le16_to_cpu(hdr->len));
  	return 0;
  }
6b44d9b8d   Andrei Emeltchenko   Bluetooth: A2MP: ...
524
525
526
  /* Handle A2MP signalling */
  static int a2mp_chan_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
  {
d9fc1d54f   Andrei Emeltchenko   Bluetooth: Do not...
527
  	struct a2mp_cmd *hdr;
6b44d9b8d   Andrei Emeltchenko   Bluetooth: A2MP: ...
528
529
530
531
532
533
  	struct amp_mgr *mgr = chan->data;
  	int err = 0;
  
  	amp_mgr_get(mgr);
  
  	while (skb->len >= sizeof(*hdr)) {
d9fc1d54f   Andrei Emeltchenko   Bluetooth: Do not...
534
535
536
537
  		u16 len;
  
  		hdr = (void *) skb->data;
  		len = le16_to_cpu(hdr->len);
6b44d9b8d   Andrei Emeltchenko   Bluetooth: A2MP: ...
538

8e8c7e36f   Andrei Emeltchenko   Bluetooth: debug:...
539
  		BT_DBG("code 0x%2.2x id %d len %u", hdr->code, hdr->ident, len);
6b44d9b8d   Andrei Emeltchenko   Bluetooth: A2MP: ...
540
541
542
543
544
545
546
547
548
549
550
551
  
  		skb_pull(skb, sizeof(*hdr));
  
  		if (len > skb->len || !hdr->ident) {
  			err = -EINVAL;
  			break;
  		}
  
  		mgr->ident = hdr->ident;
  
  		switch (hdr->code) {
  		case A2MP_COMMAND_REJ:
21dbd2ce3   Andrei Emeltchenko   Bluetooth: A2MP: ...
552
553
  			a2mp_command_rej(mgr, skb, hdr);
  			break;
6b44d9b8d   Andrei Emeltchenko   Bluetooth: A2MP: ...
554
  		case A2MP_DISCOVER_REQ:
8598d064c   Andrei Emeltchenko   Bluetooth: A2MP: ...
555
556
  			err = a2mp_discover_req(mgr, skb, hdr);
  			break;
6b44d9b8d   Andrei Emeltchenko   Bluetooth: A2MP: ...
557
  		case A2MP_CHANGE_NOTIFY:
329d81af2   Andrei Emeltchenko   Bluetooth: A2MP: ...
558
559
  			err = a2mp_change_notify(mgr, skb, hdr);
  			break;
6b44d9b8d   Andrei Emeltchenko   Bluetooth: A2MP: ...
560
  		case A2MP_GETINFO_REQ:
47f2d97d3   Andrei Emeltchenko   Bluetooth: A2MP: ...
561
562
  			err = a2mp_getinfo_req(mgr, skb, hdr);
  			break;
6b44d9b8d   Andrei Emeltchenko   Bluetooth: A2MP: ...
563
  		case A2MP_GETAMPASSOC_REQ:
a28381dc9   Andrei Emeltchenko   Bluetooth: A2MP: ...
564
565
  			err = a2mp_getampassoc_req(mgr, skb, hdr);
  			break;
6b44d9b8d   Andrei Emeltchenko   Bluetooth: A2MP: ...
566
  		case A2MP_CREATEPHYSLINK_REQ:
e072f5dab   Andrei Emeltchenko   Bluetooth: A2MP: ...
567
568
  			err = a2mp_createphyslink_req(mgr, skb, hdr);
  			break;
6b44d9b8d   Andrei Emeltchenko   Bluetooth: A2MP: ...
569
  		case A2MP_DISCONNPHYSLINK_REQ:
6113f84fc   Andrei Emeltchenko   Bluetooth: A2MP: ...
570
571
  			err = a2mp_discphyslink_req(mgr, skb, hdr);
  			break;
6b44d9b8d   Andrei Emeltchenko   Bluetooth: A2MP: ...
572
  		case A2MP_DISCOVER_RSP:
aa09537d8   Andrei Emeltchenko   Bluetooth: A2MP: ...
573
574
  			err = a2mp_discover_rsp(mgr, skb, hdr);
  			break;
6b44d9b8d   Andrei Emeltchenko   Bluetooth: A2MP: ...
575
  		case A2MP_GETINFO_RSP:
0d868de9d   Andrei Emeltchenko   Bluetooth: A2MP: ...
576
577
  			err = a2mp_getinfo_rsp(mgr, skb, hdr);
  			break;
6b44d9b8d   Andrei Emeltchenko   Bluetooth: A2MP: ...
578
  		case A2MP_GETAMPASSOC_RSP:
9a5e94dbb   Andrei Emeltchenko   Bluetooth: A2MP: ...
579
580
581
582
  			err = a2mp_getampassoc_rsp(mgr, skb, hdr);
  			break;
  
  		case A2MP_CHANGE_RSP:
6b44d9b8d   Andrei Emeltchenko   Bluetooth: A2MP: ...
583
584
  		case A2MP_CREATEPHYSLINK_RSP:
  		case A2MP_DISCONNPHYSLINK_RSP:
f6410a849   Andrei Emeltchenko   Bluetooth: A2MP: ...
585
586
  			err = a2mp_cmd_rsp(mgr, skb, hdr);
  			break;
6b44d9b8d   Andrei Emeltchenko   Bluetooth: A2MP: ...
587
588
589
590
591
592
593
594
595
  		default:
  			BT_ERR("Unknown A2MP sig cmd 0x%2.2x", hdr->code);
  			err = -EINVAL;
  			break;
  		}
  	}
  
  	if (err) {
  		struct a2mp_cmd_rej rej;
d9fc1d54f   Andrei Emeltchenko   Bluetooth: Do not...
596

dcf4adbfd   Joe Perches   Bluetooth: Conver...
597
  		rej.reason = cpu_to_le16(0);
d9fc1d54f   Andrei Emeltchenko   Bluetooth: Do not...
598
  		hdr = (void *) skb->data;
6b44d9b8d   Andrei Emeltchenko   Bluetooth: A2MP: ...
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
  
  		BT_DBG("Send A2MP Rej: cmd 0x%2.2x err %d", hdr->code, err);
  
  		a2mp_send(mgr, A2MP_COMMAND_REJ, hdr->ident, sizeof(rej),
  			  &rej);
  	}
  
  	/* Always free skb and return success error code to prevent
  	   from sending L2CAP Disconnect over A2MP channel */
  	kfree_skb(skb);
  
  	amp_mgr_put(mgr);
  
  	return 0;
  }
46d5c9088   Andrei Emeltchenko   Bluetooth: A2MP: ...
614
615
  static void a2mp_chan_close_cb(struct l2cap_chan *chan)
  {
4af66c691   Jaganath Kanakkassery   Bluetooth: Free t...
616
  	l2cap_chan_put(chan);
46d5c9088   Andrei Emeltchenko   Bluetooth: A2MP: ...
617
  }
53f521212   Gustavo Padovan   Bluetooth: Extend...
618
619
  static void a2mp_chan_state_change_cb(struct l2cap_chan *chan, int state,
  				      int err)
46d5c9088   Andrei Emeltchenko   Bluetooth: A2MP: ...
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
  {
  	struct amp_mgr *mgr = chan->data;
  
  	if (!mgr)
  		return;
  
  	BT_DBG("chan %p state %s", chan, state_to_string(state));
  
  	chan->state = state;
  
  	switch (state) {
  	case BT_CLOSED:
  		if (mgr)
  			amp_mgr_put(mgr);
  		break;
  	}
  }
  
  static struct sk_buff *a2mp_chan_alloc_skb_cb(struct l2cap_chan *chan,
d9fbd02be   Marcel Holtmann   Bluetooth: Use ex...
639
  					      unsigned long hdr_len,
46d5c9088   Andrei Emeltchenko   Bluetooth: A2MP: ...
640
641
  					      unsigned long len, int nb)
  {
0753c182e   Gustavo Padovan   Bluetooth: Fix sk...
642
  	struct sk_buff *skb;
d9fbd02be   Marcel Holtmann   Bluetooth: Use ex...
643
  	skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
0753c182e   Gustavo Padovan   Bluetooth: Fix sk...
644
645
646
647
  	if (!skb)
  		return ERR_PTR(-ENOMEM);
  
  	return skb;
46d5c9088   Andrei Emeltchenko   Bluetooth: A2MP: ...
648
  }
67f86a45b   Marcel Holtmann   Bluetooth: Use co...
649
  static const struct l2cap_ops a2mp_chan_ops = {
466f8004f   Andrei Emeltchenko   Bluetooth: A2MP: ...
650
  	.name = "L2CAP A2MP channel",
6b44d9b8d   Andrei Emeltchenko   Bluetooth: A2MP: ...
651
  	.recv = a2mp_chan_recv_cb,
46d5c9088   Andrei Emeltchenko   Bluetooth: A2MP: ...
652
653
654
655
656
  	.close = a2mp_chan_close_cb,
  	.state_change = a2mp_chan_state_change_cb,
  	.alloc_skb = a2mp_chan_alloc_skb_cb,
  
  	/* Not implemented for A2MP */
7e1af8a3a   Gustavo Padovan   Bluetooth: Create...
657
658
659
  	.new_connection = l2cap_chan_no_new_connection,
  	.teardown = l2cap_chan_no_teardown,
  	.ready = l2cap_chan_no_ready,
2dc4e5105   Gustavo Padovan   Bluetooth: Add ch...
660
  	.defer = l2cap_chan_no_defer,
2ce5fb510   Marcel Holtmann   Bluetooth: Add l2...
661
  	.resume = l2cap_chan_no_resume,
5ec1bbe54   Gustavo Padovan   Bluetooth: Add ch...
662
  	.set_shutdown = l2cap_chan_no_set_shutdown,
8d836d71e   Gustavo Padovan   Bluetooth: Access...
663
  	.get_sndtimeo = l2cap_chan_no_get_sndtimeo,
0498878b1   Jukka Rissanen   Bluetooth: Provid...
664
  	.memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec,
466f8004f   Andrei Emeltchenko   Bluetooth: A2MP: ...
665
  };
93c3e8f5c   Andrei Emeltchenko   Bluetooth: Choose...
666
  static struct l2cap_chan *a2mp_chan_open(struct l2cap_conn *conn, bool locked)
466f8004f   Andrei Emeltchenko   Bluetooth: A2MP: ...
667
668
669
670
671
672
673
674
675
  {
  	struct l2cap_chan *chan;
  	int err;
  
  	chan = l2cap_chan_create();
  	if (!chan)
  		return NULL;
  
  	BT_DBG("chan %p", chan);
2338a7e04   Johan Hedberg   Bluetooth: Rename...
676
677
678
679
680
  	chan->chan_type = L2CAP_CHAN_FIXED;
  	chan->scid = L2CAP_CID_A2MP;
  	chan->dcid = L2CAP_CID_A2MP;
  	chan->omtu = L2CAP_A2MP_DEFAULT_MTU;
  	chan->imtu = L2CAP_A2MP_DEFAULT_MTU;
466f8004f   Andrei Emeltchenko   Bluetooth: A2MP: ...
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
  	chan->flush_to = L2CAP_DEFAULT_FLUSH_TO;
  
  	chan->ops = &a2mp_chan_ops;
  
  	l2cap_chan_set_defaults(chan);
  	chan->remote_max_tx = chan->max_tx;
  	chan->remote_tx_win = chan->tx_win;
  
  	chan->retrans_timeout = L2CAP_DEFAULT_RETRANS_TO;
  	chan->monitor_timeout = L2CAP_DEFAULT_MONITOR_TO;
  
  	skb_queue_head_init(&chan->tx_q);
  
  	chan->mode = L2CAP_MODE_ERTM;
  
  	err = l2cap_ertm_init(chan);
  	if (err < 0) {
  		l2cap_chan_del(chan, 0);
  		return NULL;
  	}
  
  	chan->conf_state = 0;
93c3e8f5c   Andrei Emeltchenko   Bluetooth: Choose...
703
704
705
706
  	if (locked)
  		__l2cap_chan_add(conn, chan);
  	else
  		l2cap_chan_add(conn, chan);
466f8004f   Andrei Emeltchenko   Bluetooth: A2MP: ...
707
708
709
710
711
712
713
714
  
  	chan->remote_mps = chan->omtu;
  	chan->mps = chan->omtu;
  
  	chan->state = BT_CONNECTED;
  
  	return chan;
  }
9740e49d1   Andrei Emeltchenko   Bluetooth: A2MP: ...
715
716
  
  /* AMP Manager functions */
f706adfea   Andrei Emeltchenko   Bluetooth: AMP: G...
717
  struct amp_mgr *amp_mgr_get(struct amp_mgr *mgr)
9740e49d1   Andrei Emeltchenko   Bluetooth: A2MP: ...
718
  {
a0dfe0ab6   Andrei Emeltchenko   Bluetooth: debug:...
719
  	BT_DBG("mgr %p orig refcnt %d", mgr, atomic_read(&mgr->kref.refcount));
9740e49d1   Andrei Emeltchenko   Bluetooth: A2MP: ...
720
721
  
  	kref_get(&mgr->kref);
f706adfea   Andrei Emeltchenko   Bluetooth: AMP: G...
722
723
  
  	return mgr;
9740e49d1   Andrei Emeltchenko   Bluetooth: A2MP: ...
724
725
726
727
728
729
730
  }
  
  static void amp_mgr_destroy(struct kref *kref)
  {
  	struct amp_mgr *mgr = container_of(kref, struct amp_mgr, kref);
  
  	BT_DBG("mgr %p", mgr);
f97268fcc   Andrei Emeltchenko   Bluetooth: A2MP: ...
731
732
733
  	mutex_lock(&amp_mgr_list_lock);
  	list_del(&mgr->list);
  	mutex_unlock(&amp_mgr_list_lock);
52c0d6e56   Andrei Emeltchenko   Bluetooth: AMP: R...
734
  	amp_ctrl_list_flush(mgr);
9740e49d1   Andrei Emeltchenko   Bluetooth: A2MP: ...
735
736
737
738
739
  	kfree(mgr);
  }
  
  int amp_mgr_put(struct amp_mgr *mgr)
  {
a0dfe0ab6   Andrei Emeltchenko   Bluetooth: debug:...
740
  	BT_DBG("mgr %p orig refcnt %d", mgr, atomic_read(&mgr->kref.refcount));
9740e49d1   Andrei Emeltchenko   Bluetooth: A2MP: ...
741
742
743
  
  	return kref_put(&mgr->kref, &amp_mgr_destroy);
  }
93c3e8f5c   Andrei Emeltchenko   Bluetooth: Choose...
744
  static struct amp_mgr *amp_mgr_create(struct l2cap_conn *conn, bool locked)
9740e49d1   Andrei Emeltchenko   Bluetooth: A2MP: ...
745
746
747
748
749
750
751
752
753
754
755
  {
  	struct amp_mgr *mgr;
  	struct l2cap_chan *chan;
  
  	mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
  	if (!mgr)
  		return NULL;
  
  	BT_DBG("conn %p mgr %p", conn, mgr);
  
  	mgr->l2cap_conn = conn;
93c3e8f5c   Andrei Emeltchenko   Bluetooth: Choose...
756
  	chan = a2mp_chan_open(conn, locked);
9740e49d1   Andrei Emeltchenko   Bluetooth: A2MP: ...
757
758
759
760
761
762
763
764
765
766
767
  	if (!chan) {
  		kfree(mgr);
  		return NULL;
  	}
  
  	mgr->a2mp_chan = chan;
  	chan->data = mgr;
  
  	conn->hcon->amp_mgr = mgr;
  
  	kref_init(&mgr->kref);
52c0d6e56   Andrei Emeltchenko   Bluetooth: AMP: R...
768
769
770
  	/* Remote AMP ctrl list initialization */
  	INIT_LIST_HEAD(&mgr->amp_ctrls);
  	mutex_init(&mgr->amp_ctrls_lock);
f97268fcc   Andrei Emeltchenko   Bluetooth: A2MP: ...
771
772
773
  	mutex_lock(&amp_mgr_list_lock);
  	list_add(&mgr->list, &amp_mgr_list);
  	mutex_unlock(&amp_mgr_list_lock);
9740e49d1   Andrei Emeltchenko   Bluetooth: A2MP: ...
774
775
  	return mgr;
  }
97e8e89d2   Andrei Emeltchenko   Bluetooth: A2MP: ...
776
777
778
779
780
  
  struct l2cap_chan *a2mp_channel_create(struct l2cap_conn *conn,
  				       struct sk_buff *skb)
  {
  	struct amp_mgr *mgr;
07e307f80   Johan Hedberg   Bluetooth: Ignore...
781
782
  	if (conn->hcon->type != ACL_LINK)
  		return NULL;
93c3e8f5c   Andrei Emeltchenko   Bluetooth: Choose...
783
  	mgr = amp_mgr_create(conn, false);
97e8e89d2   Andrei Emeltchenko   Bluetooth: A2MP: ...
784
785
786
787
788
789
790
791
792
  	if (!mgr) {
  		BT_ERR("Could not create AMP manager");
  		return NULL;
  	}
  
  	BT_DBG("mgr: %p chan %p", mgr, mgr->a2mp_chan);
  
  	return mgr->a2mp_chan;
  }
f97268fcc   Andrei Emeltchenko   Bluetooth: A2MP: ...
793
794
795
796
797
798
799
  
  struct amp_mgr *amp_mgr_lookup_by_state(u8 state)
  {
  	struct amp_mgr *mgr;
  
  	mutex_lock(&amp_mgr_list_lock);
  	list_for_each_entry(mgr, &amp_mgr_list, list) {
cb6801c64   Andrei Emeltchenko   Bluetooth: AMP: U...
800
  		if (test_and_clear_bit(state, &mgr->state)) {
f97268fcc   Andrei Emeltchenko   Bluetooth: A2MP: ...
801
802
803
804
805
806
807
808
809
  			amp_mgr_get(mgr);
  			mutex_unlock(&amp_mgr_list_lock);
  			return mgr;
  		}
  	}
  	mutex_unlock(&amp_mgr_list_lock);
  
  	return NULL;
  }
8e2a0d92c   Andrei Emeltchenko   Bluetooth: AMP: U...
810
811
812
813
814
815
816
817
818
819
820
821
822
823
  
  void a2mp_send_getinfo_rsp(struct hci_dev *hdev)
  {
  	struct amp_mgr *mgr;
  	struct a2mp_info_rsp rsp;
  
  	mgr = amp_mgr_lookup_by_state(READ_LOC_AMP_INFO);
  	if (!mgr)
  		return;
  
  	BT_DBG("%s mgr %p", hdev->name, mgr);
  
  	rsp.id = hdev->id;
  	rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
ece691264   Marcel Holtmann   Bluetooth: Separa...
824
  	if (hdev->amp_type != AMP_TYPE_BREDR) {
8e2a0d92c   Andrei Emeltchenko   Bluetooth: AMP: U...
825
826
827
828
829
830
831
832
833
834
835
  		rsp.status = 0;
  		rsp.total_bw = cpu_to_le32(hdev->amp_total_bw);
  		rsp.max_bw = cpu_to_le32(hdev->amp_max_bw);
  		rsp.min_latency = cpu_to_le32(hdev->amp_min_latency);
  		rsp.pal_cap = cpu_to_le16(hdev->amp_pal_cap);
  		rsp.assoc_size = cpu_to_le16(hdev->amp_assoc_size);
  	}
  
  	a2mp_send(mgr, A2MP_GETINFO_RSP, mgr->ident, sizeof(rsp), &rsp);
  	amp_mgr_put(mgr);
  }
903e45411   Andrei Emeltchenko   Bluetooth: AMP: U...
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
  
  void a2mp_send_getampassoc_rsp(struct hci_dev *hdev, u8 status)
  {
  	struct amp_mgr *mgr;
  	struct amp_assoc *loc_assoc = &hdev->loc_assoc;
  	struct a2mp_amp_assoc_rsp *rsp;
  	size_t len;
  
  	mgr = amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC);
  	if (!mgr)
  		return;
  
  	BT_DBG("%s mgr %p", hdev->name, mgr);
  
  	len = sizeof(struct a2mp_amp_assoc_rsp) + loc_assoc->len;
  	rsp = kzalloc(len, GFP_KERNEL);
  	if (!rsp) {
  		amp_mgr_put(mgr);
  		return;
  	}
  
  	rsp->id = hdev->id;
  
  	if (status) {
  		rsp->status = A2MP_STATUS_INVALID_CTRL_ID;
  	} else {
  		rsp->status = A2MP_STATUS_SUCCESS;
  		memcpy(rsp->amp_assoc, loc_assoc->data, loc_assoc->len);
  	}
  
  	a2mp_send(mgr, A2MP_GETAMPASSOC_RSP, mgr->ident, len, rsp);
  	amp_mgr_put(mgr);
  	kfree(rsp);
  }
93c3e8f5c   Andrei Emeltchenko   Bluetooth: Choose...
870

9495b2ee7   Andrei Emeltchenko   Bluetooth: AMP: P...
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
  void a2mp_send_create_phy_link_req(struct hci_dev *hdev, u8 status)
  {
  	struct amp_mgr *mgr;
  	struct amp_assoc *loc_assoc = &hdev->loc_assoc;
  	struct a2mp_physlink_req *req;
  	struct l2cap_chan *bredr_chan;
  	size_t len;
  
  	mgr = amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC_FINAL);
  	if (!mgr)
  		return;
  
  	len = sizeof(*req) + loc_assoc->len;
  
  	BT_DBG("%s mgr %p assoc_len %zu", hdev->name, mgr, len);
  
  	req = kzalloc(len, GFP_KERNEL);
  	if (!req) {
  		amp_mgr_put(mgr);
  		return;
  	}
  
  	bredr_chan = mgr->bredr_chan;
  	if (!bredr_chan)
  		goto clean;
  
  	req->local_id = hdev->id;
fffadc08e   Andrei Emeltchenko   Bluetooth: Rename...
898
  	req->remote_id = bredr_chan->remote_amp_id;
9495b2ee7   Andrei Emeltchenko   Bluetooth: AMP: P...
899
900
901
902
903
904
905
906
  	memcpy(req->amp_assoc, loc_assoc->data, loc_assoc->len);
  
  	a2mp_send(mgr, A2MP_CREATEPHYSLINK_REQ, __next_ident(mgr), len, req);
  
  clean:
  	amp_mgr_put(mgr);
  	kfree(req);
  }
8e05e3ba8   Andrei Emeltchenko   Bluetooth: AMP: S...
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
  void a2mp_send_create_phy_link_rsp(struct hci_dev *hdev, u8 status)
  {
  	struct amp_mgr *mgr;
  	struct a2mp_physlink_rsp rsp;
  	struct hci_conn *hs_hcon;
  
  	mgr = amp_mgr_lookup_by_state(WRITE_REMOTE_AMP_ASSOC);
  	if (!mgr)
  		return;
  
  	hs_hcon = hci_conn_hash_lookup_state(hdev, AMP_LINK, BT_CONNECT);
  	if (!hs_hcon) {
  		rsp.status = A2MP_STATUS_UNABLE_START_LINK_CREATION;
  	} else {
  		rsp.remote_id = hs_hcon->remote_id;
  		rsp.status = A2MP_STATUS_SUCCESS;
  	}
  
  	BT_DBG("%s mgr %p hs_hcon %p status %u", hdev->name, mgr, hs_hcon,
  	       status);
  
  	rsp.local_id = hdev->id;
  	a2mp_send(mgr, A2MP_CREATEPHYSLINK_RSP, mgr->ident, sizeof(rsp), &rsp);
  	amp_mgr_put(mgr);
  }
93c3e8f5c   Andrei Emeltchenko   Bluetooth: Choose...
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
  void a2mp_discover_amp(struct l2cap_chan *chan)
  {
  	struct l2cap_conn *conn = chan->conn;
  	struct amp_mgr *mgr = conn->hcon->amp_mgr;
  	struct a2mp_discov_req req;
  
  	BT_DBG("chan %p conn %p mgr %p", chan, conn, mgr);
  
  	if (!mgr) {
  		mgr = amp_mgr_create(conn, true);
  		if (!mgr)
  			return;
  	}
  
  	mgr->bredr_chan = chan;
  
  	req.mtu = cpu_to_le16(L2CAP_A2MP_DEFAULT_MTU);
  	req.ext_feat = 0;
  	a2mp_send(mgr, A2MP_DISCOVER_REQ, 1, sizeof(req), &req);
  }