a2mp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  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 = 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 = 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_dev_err(hdev, "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, kref_read(&mgr->kref));
  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, kref_read(&mgr->kref));
  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. }