a2mp.c 20 KB

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