a2mp.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  1. /*
  2. Copyright (c) 2010,2011 Code Aurora Forum. All rights reserved.
  3. Copyright (c) 2011,2012 Intel Corp.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License version 2 and
  6. only version 2 as published by the Free Software Foundation.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. */
  12. #include <net/bluetooth/bluetooth.h>
  13. #include <net/bluetooth/hci_core.h>
  14. #include <net/bluetooth/l2cap.h>
  15. #include "hci_request.h"
  16. #include "a2mp.h"
  17. #include "amp.h"
  18. #define A2MP_FEAT_EXT 0x8000
  19. /* Global AMP Manager list */
  20. static LIST_HEAD(amp_mgr_list);
  21. static DEFINE_MUTEX(amp_mgr_list_lock);
  22. /* A2MP build & send command helper functions */
  23. static struct a2mp_cmd *__a2mp_build(u8 code, u8 ident, u16 len, void *data)
  24. {
  25. struct a2mp_cmd *cmd;
  26. int plen;
  27. plen = sizeof(*cmd) + len;
  28. cmd = kzalloc(plen, GFP_KERNEL);
  29. if (!cmd)
  30. return NULL;
  31. cmd->code = code;
  32. cmd->ident = ident;
  33. cmd->len = cpu_to_le16(len);
  34. memcpy(cmd->data, data, len);
  35. return cmd;
  36. }
  37. static void a2mp_send(struct amp_mgr *mgr, u8 code, u8 ident, u16 len, void *data)
  38. {
  39. struct l2cap_chan *chan = mgr->a2mp_chan;
  40. struct a2mp_cmd *cmd;
  41. u16 total_len = len + sizeof(*cmd);
  42. struct kvec iv;
  43. struct msghdr msg;
  44. cmd = __a2mp_build(code, ident, len, data);
  45. if (!cmd)
  46. return;
  47. iv.iov_base = cmd;
  48. iv.iov_len = total_len;
  49. memset(&msg, 0, sizeof(msg));
  50. iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, &iv, 1, total_len);
  51. l2cap_chan_send(chan, &msg, total_len);
  52. kfree(cmd);
  53. }
  54. static u8 __next_ident(struct amp_mgr *mgr)
  55. {
  56. if (++mgr->ident == 0)
  57. mgr->ident = 1;
  58. return mgr->ident;
  59. }
  60. static struct amp_mgr *amp_mgr_lookup_by_state(u8 state)
  61. {
  62. struct amp_mgr *mgr;
  63. mutex_lock(&amp_mgr_list_lock);
  64. list_for_each_entry(mgr, &amp_mgr_list, list) {
  65. if (test_and_clear_bit(state, &mgr->state)) {
  66. amp_mgr_get(mgr);
  67. mutex_unlock(&amp_mgr_list_lock);
  68. return mgr;
  69. }
  70. }
  71. mutex_unlock(&amp_mgr_list_lock);
  72. return NULL;
  73. }
  74. /* hci_dev_list shall be locked */
  75. static void __a2mp_add_cl(struct amp_mgr *mgr, struct a2mp_cl *cl)
  76. {
  77. struct hci_dev *hdev;
  78. int i = 1;
  79. cl[0].id = AMP_ID_BREDR;
  80. cl[0].type = AMP_TYPE_BREDR;
  81. cl[0].status = AMP_STATUS_BLUETOOTH_ONLY;
  82. list_for_each_entry(hdev, &hci_dev_list, list) {
  83. if (hdev->dev_type == HCI_AMP) {
  84. cl[i].id = hdev->id;
  85. cl[i].type = hdev->amp_type;
  86. if (test_bit(HCI_UP, &hdev->flags))
  87. cl[i].status = hdev->amp_status;
  88. else
  89. cl[i].status = AMP_STATUS_POWERED_DOWN;
  90. i++;
  91. }
  92. }
  93. }
  94. /* Processing A2MP messages */
  95. static int a2mp_command_rej(struct amp_mgr *mgr, struct sk_buff *skb,
  96. struct a2mp_cmd *hdr)
  97. {
  98. struct a2mp_cmd_rej *rej = (void *) skb->data;
  99. if (le16_to_cpu(hdr->len) < sizeof(*rej))
  100. return -EINVAL;
  101. BT_DBG("ident %d reason %d", hdr->ident, le16_to_cpu(rej->reason));
  102. skb_pull(skb, sizeof(*rej));
  103. return 0;
  104. }
  105. static int a2mp_discover_req(struct amp_mgr *mgr, struct sk_buff *skb,
  106. struct a2mp_cmd *hdr)
  107. {
  108. struct a2mp_discov_req *req = (void *) skb->data;
  109. u16 len = le16_to_cpu(hdr->len);
  110. struct a2mp_discov_rsp *rsp;
  111. u16 ext_feat;
  112. u8 num_ctrl;
  113. struct hci_dev *hdev;
  114. if (len < sizeof(*req))
  115. return -EINVAL;
  116. skb_pull(skb, sizeof(*req));
  117. ext_feat = le16_to_cpu(req->ext_feat);
  118. BT_DBG("mtu %d efm 0x%4.4x", le16_to_cpu(req->mtu), ext_feat);
  119. /* check that packet is not broken for now */
  120. while (ext_feat & A2MP_FEAT_EXT) {
  121. if (len < sizeof(ext_feat))
  122. return -EINVAL;
  123. ext_feat = get_unaligned_le16(skb->data);
  124. BT_DBG("efm 0x%4.4x", ext_feat);
  125. len -= sizeof(ext_feat);
  126. skb_pull(skb, sizeof(ext_feat));
  127. }
  128. read_lock(&hci_dev_list_lock);
  129. /* at minimum the BR/EDR needs to be listed */
  130. num_ctrl = 1;
  131. list_for_each_entry(hdev, &hci_dev_list, list) {
  132. if (hdev->dev_type == HCI_AMP)
  133. num_ctrl++;
  134. }
  135. len = num_ctrl * sizeof(struct a2mp_cl) + sizeof(*rsp);
  136. rsp = kmalloc(len, GFP_ATOMIC);
  137. if (!rsp) {
  138. read_unlock(&hci_dev_list_lock);
  139. return -ENOMEM;
  140. }
  141. rsp->mtu = cpu_to_le16(L2CAP_A2MP_DEFAULT_MTU);
  142. rsp->ext_feat = 0;
  143. __a2mp_add_cl(mgr, rsp->cl);
  144. read_unlock(&hci_dev_list_lock);
  145. a2mp_send(mgr, A2MP_DISCOVER_RSP, hdr->ident, len, rsp);
  146. kfree(rsp);
  147. return 0;
  148. }
  149. static int a2mp_discover_rsp(struct amp_mgr *mgr, struct sk_buff *skb,
  150. struct a2mp_cmd *hdr)
  151. {
  152. struct a2mp_discov_rsp *rsp = (void *) skb->data;
  153. u16 len = le16_to_cpu(hdr->len);
  154. struct a2mp_cl *cl;
  155. u16 ext_feat;
  156. bool found = false;
  157. if (len < sizeof(*rsp))
  158. return -EINVAL;
  159. len -= sizeof(*rsp);
  160. skb_pull(skb, sizeof(*rsp));
  161. ext_feat = le16_to_cpu(rsp->ext_feat);
  162. BT_DBG("mtu %d efm 0x%4.4x", le16_to_cpu(rsp->mtu), ext_feat);
  163. /* check that packet is not broken for now */
  164. while (ext_feat & A2MP_FEAT_EXT) {
  165. if (len < sizeof(ext_feat))
  166. return -EINVAL;
  167. ext_feat = get_unaligned_le16(skb->data);
  168. BT_DBG("efm 0x%4.4x", ext_feat);
  169. len -= sizeof(ext_feat);
  170. skb_pull(skb, sizeof(ext_feat));
  171. }
  172. cl = (void *) skb->data;
  173. while (len >= sizeof(*cl)) {
  174. BT_DBG("Remote AMP id %d type %d status %d", cl->id, cl->type,
  175. cl->status);
  176. if (cl->id != AMP_ID_BREDR && cl->type != AMP_TYPE_BREDR) {
  177. struct a2mp_info_req req;
  178. found = true;
  179. req.id = cl->id;
  180. a2mp_send(mgr, A2MP_GETINFO_REQ, __next_ident(mgr),
  181. sizeof(req), &req);
  182. }
  183. len -= sizeof(*cl);
  184. cl = (void *) skb_pull(skb, sizeof(*cl));
  185. }
  186. /* Fall back to L2CAP init sequence */
  187. if (!found) {
  188. struct l2cap_conn *conn = mgr->l2cap_conn;
  189. struct l2cap_chan *chan;
  190. mutex_lock(&conn->chan_lock);
  191. list_for_each_entry(chan, &conn->chan_l, list) {
  192. BT_DBG("chan %p state %s", chan,
  193. state_to_string(chan->state));
  194. if (chan->scid == L2CAP_CID_A2MP)
  195. continue;
  196. l2cap_chan_lock(chan);
  197. if (chan->state == BT_CONNECT)
  198. l2cap_send_conn_req(chan);
  199. l2cap_chan_unlock(chan);
  200. }
  201. mutex_unlock(&conn->chan_lock);
  202. }
  203. return 0;
  204. }
  205. static int a2mp_change_notify(struct amp_mgr *mgr, struct sk_buff *skb,
  206. struct a2mp_cmd *hdr)
  207. {
  208. struct a2mp_cl *cl = (void *) skb->data;
  209. while (skb->len >= sizeof(*cl)) {
  210. BT_DBG("Controller id %d type %d status %d", cl->id, cl->type,
  211. cl->status);
  212. cl = (struct a2mp_cl *) skb_pull(skb, sizeof(*cl));
  213. }
  214. /* TODO send A2MP_CHANGE_RSP */
  215. return 0;
  216. }
  217. static void read_local_amp_info_complete(struct hci_dev *hdev, u8 status,
  218. u16 opcode)
  219. {
  220. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  221. a2mp_send_getinfo_rsp(hdev);
  222. }
  223. static int a2mp_getinfo_req(struct amp_mgr *mgr, struct sk_buff *skb,
  224. struct a2mp_cmd *hdr)
  225. {
  226. struct a2mp_info_req *req = (void *) skb->data;
  227. struct hci_dev *hdev;
  228. struct hci_request hreq;
  229. int err = 0;
  230. if (le16_to_cpu(hdr->len) < sizeof(*req))
  231. return -EINVAL;
  232. BT_DBG("id %d", req->id);
  233. hdev = hci_dev_get(req->id);
  234. if (!hdev || hdev->dev_type != HCI_AMP) {
  235. struct a2mp_info_rsp rsp;
  236. rsp.id = req->id;
  237. rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
  238. a2mp_send(mgr, A2MP_GETINFO_RSP, hdr->ident, sizeof(rsp),
  239. &rsp);
  240. goto done;
  241. }
  242. set_bit(READ_LOC_AMP_INFO, &mgr->state);
  243. hci_req_init(&hreq, hdev);
  244. hci_req_add(&hreq, HCI_OP_READ_LOCAL_AMP_INFO, 0, NULL);
  245. err = hci_req_run(&hreq, read_local_amp_info_complete);
  246. if (err < 0)
  247. a2mp_send_getinfo_rsp(hdev);
  248. done:
  249. if (hdev)
  250. hci_dev_put(hdev);
  251. skb_pull(skb, sizeof(*req));
  252. return 0;
  253. }
  254. static int a2mp_getinfo_rsp(struct amp_mgr *mgr, struct sk_buff *skb,
  255. struct a2mp_cmd *hdr)
  256. {
  257. struct a2mp_info_rsp *rsp = (struct a2mp_info_rsp *) skb->data;
  258. struct a2mp_amp_assoc_req req;
  259. struct amp_ctrl *ctrl;
  260. if (le16_to_cpu(hdr->len) < sizeof(*rsp))
  261. return -EINVAL;
  262. BT_DBG("id %d status 0x%2.2x", rsp->id, rsp->status);
  263. if (rsp->status)
  264. return -EINVAL;
  265. ctrl = amp_ctrl_add(mgr, rsp->id);
  266. if (!ctrl)
  267. return -ENOMEM;
  268. req.id = rsp->id;
  269. a2mp_send(mgr, A2MP_GETAMPASSOC_REQ, __next_ident(mgr), sizeof(req),
  270. &req);
  271. skb_pull(skb, sizeof(*rsp));
  272. return 0;
  273. }
  274. static int a2mp_getampassoc_req(struct amp_mgr *mgr, struct sk_buff *skb,
  275. struct a2mp_cmd *hdr)
  276. {
  277. struct a2mp_amp_assoc_req *req = (void *) skb->data;
  278. struct hci_dev *hdev;
  279. struct amp_mgr *tmp;
  280. if (le16_to_cpu(hdr->len) < sizeof(*req))
  281. return -EINVAL;
  282. BT_DBG("id %d", req->id);
  283. /* Make sure that other request is not processed */
  284. tmp = amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC);
  285. hdev = hci_dev_get(req->id);
  286. if (!hdev || hdev->amp_type == AMP_TYPE_BREDR || tmp) {
  287. struct a2mp_amp_assoc_rsp rsp;
  288. rsp.id = req->id;
  289. if (tmp) {
  290. rsp.status = A2MP_STATUS_COLLISION_OCCURED;
  291. amp_mgr_put(tmp);
  292. } else {
  293. rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
  294. }
  295. a2mp_send(mgr, A2MP_GETAMPASSOC_RSP, hdr->ident, sizeof(rsp),
  296. &rsp);
  297. goto done;
  298. }
  299. amp_read_loc_assoc(hdev, mgr);
  300. done:
  301. if (hdev)
  302. hci_dev_put(hdev);
  303. skb_pull(skb, sizeof(*req));
  304. return 0;
  305. }
  306. static int a2mp_getampassoc_rsp(struct amp_mgr *mgr, struct sk_buff *skb,
  307. struct a2mp_cmd *hdr)
  308. {
  309. struct a2mp_amp_assoc_rsp *rsp = (void *) skb->data;
  310. u16 len = le16_to_cpu(hdr->len);
  311. struct hci_dev *hdev;
  312. struct amp_ctrl *ctrl;
  313. struct hci_conn *hcon;
  314. size_t assoc_len;
  315. if (len < sizeof(*rsp))
  316. return -EINVAL;
  317. assoc_len = len - sizeof(*rsp);
  318. BT_DBG("id %d status 0x%2.2x assoc len %zu", rsp->id, rsp->status,
  319. assoc_len);
  320. if (rsp->status)
  321. return -EINVAL;
  322. /* Save remote ASSOC data */
  323. ctrl = amp_ctrl_lookup(mgr, rsp->id);
  324. if (ctrl) {
  325. u8 *assoc;
  326. assoc = kmemdup(rsp->amp_assoc, assoc_len, GFP_KERNEL);
  327. if (!assoc) {
  328. amp_ctrl_put(ctrl);
  329. return -ENOMEM;
  330. }
  331. ctrl->assoc = assoc;
  332. ctrl->assoc_len = assoc_len;
  333. ctrl->assoc_rem_len = assoc_len;
  334. ctrl->assoc_len_so_far = 0;
  335. amp_ctrl_put(ctrl);
  336. }
  337. /* Create Phys Link */
  338. hdev = hci_dev_get(rsp->id);
  339. if (!hdev)
  340. return -EINVAL;
  341. hcon = phylink_add(hdev, mgr, rsp->id, true);
  342. if (!hcon)
  343. goto done;
  344. BT_DBG("Created hcon %p: loc:%d -> rem:%d", hcon, hdev->id, rsp->id);
  345. mgr->bredr_chan->remote_amp_id = rsp->id;
  346. amp_create_phylink(hdev, mgr, hcon);
  347. done:
  348. hci_dev_put(hdev);
  349. skb_pull(skb, len);
  350. return 0;
  351. }
  352. static int a2mp_createphyslink_req(struct amp_mgr *mgr, struct sk_buff *skb,
  353. struct a2mp_cmd *hdr)
  354. {
  355. struct a2mp_physlink_req *req = (void *) skb->data;
  356. struct a2mp_physlink_rsp rsp;
  357. struct hci_dev *hdev;
  358. struct hci_conn *hcon;
  359. struct amp_ctrl *ctrl;
  360. if (le16_to_cpu(hdr->len) < sizeof(*req))
  361. return -EINVAL;
  362. BT_DBG("local_id %d, remote_id %d", req->local_id, req->remote_id);
  363. rsp.local_id = req->remote_id;
  364. rsp.remote_id = req->local_id;
  365. hdev = hci_dev_get(req->remote_id);
  366. if (!hdev || hdev->amp_type == AMP_TYPE_BREDR) {
  367. rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
  368. goto send_rsp;
  369. }
  370. ctrl = amp_ctrl_lookup(mgr, rsp.remote_id);
  371. if (!ctrl) {
  372. ctrl = amp_ctrl_add(mgr, rsp.remote_id);
  373. if (ctrl) {
  374. amp_ctrl_get(ctrl);
  375. } else {
  376. rsp.status = A2MP_STATUS_UNABLE_START_LINK_CREATION;
  377. goto send_rsp;
  378. }
  379. }
  380. if (ctrl) {
  381. size_t assoc_len = le16_to_cpu(hdr->len) - sizeof(*req);
  382. u8 *assoc;
  383. assoc = kmemdup(req->amp_assoc, assoc_len, GFP_KERNEL);
  384. if (!assoc) {
  385. amp_ctrl_put(ctrl);
  386. return -ENOMEM;
  387. }
  388. ctrl->assoc = assoc;
  389. ctrl->assoc_len = assoc_len;
  390. ctrl->assoc_rem_len = assoc_len;
  391. ctrl->assoc_len_so_far = 0;
  392. amp_ctrl_put(ctrl);
  393. }
  394. hcon = phylink_add(hdev, mgr, req->local_id, false);
  395. if (hcon) {
  396. amp_accept_phylink(hdev, mgr, hcon);
  397. rsp.status = A2MP_STATUS_SUCCESS;
  398. } else {
  399. rsp.status = A2MP_STATUS_UNABLE_START_LINK_CREATION;
  400. }
  401. send_rsp:
  402. if (hdev)
  403. hci_dev_put(hdev);
  404. /* Reply error now and success after HCI Write Remote AMP Assoc
  405. command complete with success status
  406. */
  407. if (rsp.status != A2MP_STATUS_SUCCESS) {
  408. a2mp_send(mgr, A2MP_CREATEPHYSLINK_RSP, hdr->ident,
  409. sizeof(rsp), &rsp);
  410. } else {
  411. set_bit(WRITE_REMOTE_AMP_ASSOC, &mgr->state);
  412. mgr->ident = hdr->ident;
  413. }
  414. skb_pull(skb, le16_to_cpu(hdr->len));
  415. return 0;
  416. }
  417. static int a2mp_discphyslink_req(struct amp_mgr *mgr, struct sk_buff *skb,
  418. struct a2mp_cmd *hdr)
  419. {
  420. struct a2mp_physlink_req *req = (void *) skb->data;
  421. struct a2mp_physlink_rsp rsp;
  422. struct hci_dev *hdev;
  423. struct hci_conn *hcon;
  424. if (le16_to_cpu(hdr->len) < sizeof(*req))
  425. return -EINVAL;
  426. BT_DBG("local_id %d remote_id %d", req->local_id, req->remote_id);
  427. rsp.local_id = req->remote_id;
  428. rsp.remote_id = req->local_id;
  429. rsp.status = A2MP_STATUS_SUCCESS;
  430. hdev = hci_dev_get(req->remote_id);
  431. if (!hdev) {
  432. rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
  433. goto send_rsp;
  434. }
  435. hcon = hci_conn_hash_lookup_ba(hdev, AMP_LINK,
  436. &mgr->l2cap_conn->hcon->dst);
  437. if (!hcon) {
  438. BT_ERR("No phys link exist");
  439. rsp.status = A2MP_STATUS_NO_PHYSICAL_LINK_EXISTS;
  440. goto clean;
  441. }
  442. /* TODO Disconnect Phys Link here */
  443. clean:
  444. hci_dev_put(hdev);
  445. send_rsp:
  446. a2mp_send(mgr, A2MP_DISCONNPHYSLINK_RSP, hdr->ident, sizeof(rsp), &rsp);
  447. skb_pull(skb, sizeof(*req));
  448. return 0;
  449. }
  450. static inline int a2mp_cmd_rsp(struct amp_mgr *mgr, struct sk_buff *skb,
  451. struct a2mp_cmd *hdr)
  452. {
  453. BT_DBG("ident %d code 0x%2.2x", hdr->ident, hdr->code);
  454. skb_pull(skb, le16_to_cpu(hdr->len));
  455. return 0;
  456. }
  457. /* Handle A2MP signalling */
  458. static int a2mp_chan_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
  459. {
  460. struct a2mp_cmd *hdr;
  461. struct amp_mgr *mgr = chan->data;
  462. int err = 0;
  463. amp_mgr_get(mgr);
  464. while (skb->len >= sizeof(*hdr)) {
  465. u16 len;
  466. hdr = (void *) skb->data;
  467. len = le16_to_cpu(hdr->len);
  468. BT_DBG("code 0x%2.2x id %d len %u", hdr->code, hdr->ident, len);
  469. skb_pull(skb, sizeof(*hdr));
  470. if (len > skb->len || !hdr->ident) {
  471. err = -EINVAL;
  472. break;
  473. }
  474. mgr->ident = hdr->ident;
  475. switch (hdr->code) {
  476. case A2MP_COMMAND_REJ:
  477. a2mp_command_rej(mgr, skb, hdr);
  478. break;
  479. case A2MP_DISCOVER_REQ:
  480. err = a2mp_discover_req(mgr, skb, hdr);
  481. break;
  482. case A2MP_CHANGE_NOTIFY:
  483. err = a2mp_change_notify(mgr, skb, hdr);
  484. break;
  485. case A2MP_GETINFO_REQ:
  486. err = a2mp_getinfo_req(mgr, skb, hdr);
  487. break;
  488. case A2MP_GETAMPASSOC_REQ:
  489. err = a2mp_getampassoc_req(mgr, skb, hdr);
  490. break;
  491. case A2MP_CREATEPHYSLINK_REQ:
  492. err = a2mp_createphyslink_req(mgr, skb, hdr);
  493. break;
  494. case A2MP_DISCONNPHYSLINK_REQ:
  495. err = a2mp_discphyslink_req(mgr, skb, hdr);
  496. break;
  497. case A2MP_DISCOVER_RSP:
  498. err = a2mp_discover_rsp(mgr, skb, hdr);
  499. break;
  500. case A2MP_GETINFO_RSP:
  501. err = a2mp_getinfo_rsp(mgr, skb, hdr);
  502. break;
  503. case A2MP_GETAMPASSOC_RSP:
  504. err = a2mp_getampassoc_rsp(mgr, skb, hdr);
  505. break;
  506. case A2MP_CHANGE_RSP:
  507. case A2MP_CREATEPHYSLINK_RSP:
  508. case A2MP_DISCONNPHYSLINK_RSP:
  509. err = a2mp_cmd_rsp(mgr, skb, hdr);
  510. break;
  511. default:
  512. BT_ERR("Unknown A2MP sig cmd 0x%2.2x", hdr->code);
  513. err = -EINVAL;
  514. break;
  515. }
  516. }
  517. if (err) {
  518. struct a2mp_cmd_rej rej;
  519. rej.reason = cpu_to_le16(0);
  520. hdr = (void *) skb->data;
  521. BT_DBG("Send A2MP Rej: cmd 0x%2.2x err %d", hdr->code, err);
  522. a2mp_send(mgr, A2MP_COMMAND_REJ, hdr->ident, sizeof(rej),
  523. &rej);
  524. }
  525. /* Always free skb and return success error code to prevent
  526. from sending L2CAP Disconnect over A2MP channel */
  527. kfree_skb(skb);
  528. amp_mgr_put(mgr);
  529. return 0;
  530. }
  531. static void a2mp_chan_close_cb(struct l2cap_chan *chan)
  532. {
  533. l2cap_chan_put(chan);
  534. }
  535. static void a2mp_chan_state_change_cb(struct l2cap_chan *chan, int state,
  536. int err)
  537. {
  538. struct amp_mgr *mgr = chan->data;
  539. if (!mgr)
  540. return;
  541. BT_DBG("chan %p state %s", chan, state_to_string(state));
  542. chan->state = state;
  543. switch (state) {
  544. case BT_CLOSED:
  545. if (mgr)
  546. amp_mgr_put(mgr);
  547. break;
  548. }
  549. }
  550. static struct sk_buff *a2mp_chan_alloc_skb_cb(struct l2cap_chan *chan,
  551. unsigned long hdr_len,
  552. unsigned long len, int nb)
  553. {
  554. struct sk_buff *skb;
  555. skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
  556. if (!skb)
  557. return ERR_PTR(-ENOMEM);
  558. return skb;
  559. }
  560. static const struct l2cap_ops a2mp_chan_ops = {
  561. .name = "L2CAP A2MP channel",
  562. .recv = a2mp_chan_recv_cb,
  563. .close = a2mp_chan_close_cb,
  564. .state_change = a2mp_chan_state_change_cb,
  565. .alloc_skb = a2mp_chan_alloc_skb_cb,
  566. /* Not implemented for A2MP */
  567. .new_connection = l2cap_chan_no_new_connection,
  568. .teardown = l2cap_chan_no_teardown,
  569. .ready = l2cap_chan_no_ready,
  570. .defer = l2cap_chan_no_defer,
  571. .resume = l2cap_chan_no_resume,
  572. .set_shutdown = l2cap_chan_no_set_shutdown,
  573. .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
  574. };
  575. static struct l2cap_chan *a2mp_chan_open(struct l2cap_conn *conn, bool locked)
  576. {
  577. struct l2cap_chan *chan;
  578. int err;
  579. chan = l2cap_chan_create();
  580. if (!chan)
  581. return NULL;
  582. BT_DBG("chan %p", chan);
  583. chan->chan_type = L2CAP_CHAN_FIXED;
  584. chan->scid = L2CAP_CID_A2MP;
  585. chan->dcid = L2CAP_CID_A2MP;
  586. chan->omtu = L2CAP_A2MP_DEFAULT_MTU;
  587. chan->imtu = L2CAP_A2MP_DEFAULT_MTU;
  588. chan->flush_to = L2CAP_DEFAULT_FLUSH_TO;
  589. chan->ops = &a2mp_chan_ops;
  590. l2cap_chan_set_defaults(chan);
  591. chan->remote_max_tx = chan->max_tx;
  592. chan->remote_tx_win = chan->tx_win;
  593. chan->retrans_timeout = L2CAP_DEFAULT_RETRANS_TO;
  594. chan->monitor_timeout = L2CAP_DEFAULT_MONITOR_TO;
  595. skb_queue_head_init(&chan->tx_q);
  596. chan->mode = L2CAP_MODE_ERTM;
  597. err = l2cap_ertm_init(chan);
  598. if (err < 0) {
  599. l2cap_chan_del(chan, 0);
  600. return NULL;
  601. }
  602. chan->conf_state = 0;
  603. if (locked)
  604. __l2cap_chan_add(conn, chan);
  605. else
  606. l2cap_chan_add(conn, chan);
  607. chan->remote_mps = chan->omtu;
  608. chan->mps = chan->omtu;
  609. chan->state = BT_CONNECTED;
  610. return chan;
  611. }
  612. /* AMP Manager functions */
  613. struct amp_mgr *amp_mgr_get(struct amp_mgr *mgr)
  614. {
  615. BT_DBG("mgr %p orig refcnt %d", mgr, atomic_read(&mgr->kref.refcount));
  616. kref_get(&mgr->kref);
  617. return mgr;
  618. }
  619. static void amp_mgr_destroy(struct kref *kref)
  620. {
  621. struct amp_mgr *mgr = container_of(kref, struct amp_mgr, kref);
  622. BT_DBG("mgr %p", mgr);
  623. mutex_lock(&amp_mgr_list_lock);
  624. list_del(&mgr->list);
  625. mutex_unlock(&amp_mgr_list_lock);
  626. amp_ctrl_list_flush(mgr);
  627. kfree(mgr);
  628. }
  629. int amp_mgr_put(struct amp_mgr *mgr)
  630. {
  631. BT_DBG("mgr %p orig refcnt %d", mgr, atomic_read(&mgr->kref.refcount));
  632. return kref_put(&mgr->kref, &amp_mgr_destroy);
  633. }
  634. static struct amp_mgr *amp_mgr_create(struct l2cap_conn *conn, bool locked)
  635. {
  636. struct amp_mgr *mgr;
  637. struct l2cap_chan *chan;
  638. mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
  639. if (!mgr)
  640. return NULL;
  641. BT_DBG("conn %p mgr %p", conn, mgr);
  642. mgr->l2cap_conn = conn;
  643. chan = a2mp_chan_open(conn, locked);
  644. if (!chan) {
  645. kfree(mgr);
  646. return NULL;
  647. }
  648. mgr->a2mp_chan = chan;
  649. chan->data = mgr;
  650. conn->hcon->amp_mgr = mgr;
  651. kref_init(&mgr->kref);
  652. /* Remote AMP ctrl list initialization */
  653. INIT_LIST_HEAD(&mgr->amp_ctrls);
  654. mutex_init(&mgr->amp_ctrls_lock);
  655. mutex_lock(&amp_mgr_list_lock);
  656. list_add(&mgr->list, &amp_mgr_list);
  657. mutex_unlock(&amp_mgr_list_lock);
  658. return mgr;
  659. }
  660. struct l2cap_chan *a2mp_channel_create(struct l2cap_conn *conn,
  661. struct sk_buff *skb)
  662. {
  663. struct amp_mgr *mgr;
  664. if (conn->hcon->type != ACL_LINK)
  665. return NULL;
  666. mgr = amp_mgr_create(conn, false);
  667. if (!mgr) {
  668. BT_ERR("Could not create AMP manager");
  669. return NULL;
  670. }
  671. BT_DBG("mgr: %p chan %p", mgr, mgr->a2mp_chan);
  672. return mgr->a2mp_chan;
  673. }
  674. void a2mp_send_getinfo_rsp(struct hci_dev *hdev)
  675. {
  676. struct amp_mgr *mgr;
  677. struct a2mp_info_rsp rsp;
  678. mgr = amp_mgr_lookup_by_state(READ_LOC_AMP_INFO);
  679. if (!mgr)
  680. return;
  681. BT_DBG("%s mgr %p", hdev->name, mgr);
  682. rsp.id = hdev->id;
  683. rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
  684. if (hdev->amp_type != AMP_TYPE_BREDR) {
  685. rsp.status = 0;
  686. rsp.total_bw = cpu_to_le32(hdev->amp_total_bw);
  687. rsp.max_bw = cpu_to_le32(hdev->amp_max_bw);
  688. rsp.min_latency = cpu_to_le32(hdev->amp_min_latency);
  689. rsp.pal_cap = cpu_to_le16(hdev->amp_pal_cap);
  690. rsp.assoc_size = cpu_to_le16(hdev->amp_assoc_size);
  691. }
  692. a2mp_send(mgr, A2MP_GETINFO_RSP, mgr->ident, sizeof(rsp), &rsp);
  693. amp_mgr_put(mgr);
  694. }
  695. void a2mp_send_getampassoc_rsp(struct hci_dev *hdev, u8 status)
  696. {
  697. struct amp_mgr *mgr;
  698. struct amp_assoc *loc_assoc = &hdev->loc_assoc;
  699. struct a2mp_amp_assoc_rsp *rsp;
  700. size_t len;
  701. mgr = amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC);
  702. if (!mgr)
  703. return;
  704. BT_DBG("%s mgr %p", hdev->name, mgr);
  705. len = sizeof(struct a2mp_amp_assoc_rsp) + loc_assoc->len;
  706. rsp = kzalloc(len, GFP_KERNEL);
  707. if (!rsp) {
  708. amp_mgr_put(mgr);
  709. return;
  710. }
  711. rsp->id = hdev->id;
  712. if (status) {
  713. rsp->status = A2MP_STATUS_INVALID_CTRL_ID;
  714. } else {
  715. rsp->status = A2MP_STATUS_SUCCESS;
  716. memcpy(rsp->amp_assoc, loc_assoc->data, loc_assoc->len);
  717. }
  718. a2mp_send(mgr, A2MP_GETAMPASSOC_RSP, mgr->ident, len, rsp);
  719. amp_mgr_put(mgr);
  720. kfree(rsp);
  721. }
  722. void a2mp_send_create_phy_link_req(struct hci_dev *hdev, u8 status)
  723. {
  724. struct amp_mgr *mgr;
  725. struct amp_assoc *loc_assoc = &hdev->loc_assoc;
  726. struct a2mp_physlink_req *req;
  727. struct l2cap_chan *bredr_chan;
  728. size_t len;
  729. mgr = amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC_FINAL);
  730. if (!mgr)
  731. return;
  732. len = sizeof(*req) + loc_assoc->len;
  733. BT_DBG("%s mgr %p assoc_len %zu", hdev->name, mgr, len);
  734. req = kzalloc(len, GFP_KERNEL);
  735. if (!req) {
  736. amp_mgr_put(mgr);
  737. return;
  738. }
  739. bredr_chan = mgr->bredr_chan;
  740. if (!bredr_chan)
  741. goto clean;
  742. req->local_id = hdev->id;
  743. req->remote_id = bredr_chan->remote_amp_id;
  744. memcpy(req->amp_assoc, loc_assoc->data, loc_assoc->len);
  745. a2mp_send(mgr, A2MP_CREATEPHYSLINK_REQ, __next_ident(mgr), len, req);
  746. clean:
  747. amp_mgr_put(mgr);
  748. kfree(req);
  749. }
  750. void a2mp_send_create_phy_link_rsp(struct hci_dev *hdev, u8 status)
  751. {
  752. struct amp_mgr *mgr;
  753. struct a2mp_physlink_rsp rsp;
  754. struct hci_conn *hs_hcon;
  755. mgr = amp_mgr_lookup_by_state(WRITE_REMOTE_AMP_ASSOC);
  756. if (!mgr)
  757. return;
  758. hs_hcon = hci_conn_hash_lookup_state(hdev, AMP_LINK, BT_CONNECT);
  759. if (!hs_hcon) {
  760. rsp.status = A2MP_STATUS_UNABLE_START_LINK_CREATION;
  761. } else {
  762. rsp.remote_id = hs_hcon->remote_id;
  763. rsp.status = A2MP_STATUS_SUCCESS;
  764. }
  765. BT_DBG("%s mgr %p hs_hcon %p status %u", hdev->name, mgr, hs_hcon,
  766. status);
  767. rsp.local_id = hdev->id;
  768. a2mp_send(mgr, A2MP_CREATEPHYSLINK_RSP, mgr->ident, sizeof(rsp), &rsp);
  769. amp_mgr_put(mgr);
  770. }
  771. void a2mp_discover_amp(struct l2cap_chan *chan)
  772. {
  773. struct l2cap_conn *conn = chan->conn;
  774. struct amp_mgr *mgr = conn->hcon->amp_mgr;
  775. struct a2mp_discov_req req;
  776. BT_DBG("chan %p conn %p mgr %p", chan, conn, mgr);
  777. if (!mgr) {
  778. mgr = amp_mgr_create(conn, true);
  779. if (!mgr)
  780. return;
  781. }
  782. mgr->bredr_chan = chan;
  783. req.mtu = cpu_to_le16(L2CAP_A2MP_DEFAULT_MTU);
  784. req.ext_feat = 0;
  785. a2mp_send(mgr, A2MP_DISCOVER_REQ, 1, sizeof(req), &req);
  786. }