a2mp.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  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 || hdev->dev_type != HCI_AMP) {
  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. goto done;
  216. }
  217. mgr->state = READ_LOC_AMP_INFO;
  218. hci_send_cmd(hdev, HCI_OP_READ_LOCAL_AMP_INFO, 0, NULL);
  219. done:
  220. if (hdev)
  221. hci_dev_put(hdev);
  222. skb_pull(skb, sizeof(*req));
  223. return 0;
  224. }
  225. static int a2mp_getinfo_rsp(struct amp_mgr *mgr, struct sk_buff *skb,
  226. struct a2mp_cmd *hdr)
  227. {
  228. struct a2mp_info_rsp *rsp = (struct a2mp_info_rsp *) skb->data;
  229. struct a2mp_amp_assoc_req req;
  230. struct amp_ctrl *ctrl;
  231. if (le16_to_cpu(hdr->len) < sizeof(*rsp))
  232. return -EINVAL;
  233. BT_DBG("id %d status 0x%2.2x", rsp->id, rsp->status);
  234. if (rsp->status)
  235. return -EINVAL;
  236. ctrl = amp_ctrl_add(mgr, rsp->id);
  237. if (!ctrl)
  238. return -ENOMEM;
  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. size_t assoc_len;
  286. if (len < sizeof(*rsp))
  287. return -EINVAL;
  288. assoc_len = len - sizeof(*rsp);
  289. BT_DBG("id %d status 0x%2.2x assoc len %zu", rsp->id, rsp->status,
  290. assoc_len);
  291. if (rsp->status)
  292. return -EINVAL;
  293. /* Save remote ASSOC data */
  294. ctrl = amp_ctrl_lookup(mgr, rsp->id);
  295. if (ctrl) {
  296. u8 *assoc;
  297. assoc = kzalloc(assoc_len, GFP_KERNEL);
  298. if (!assoc) {
  299. amp_ctrl_put(ctrl);
  300. return -ENOMEM;
  301. }
  302. memcpy(assoc, rsp->amp_assoc, assoc_len);
  303. ctrl->assoc = assoc;
  304. ctrl->assoc_len = assoc_len;
  305. ctrl->assoc_rem_len = assoc_len;
  306. ctrl->assoc_len_so_far = 0;
  307. amp_ctrl_put(ctrl);
  308. }
  309. /* Create Phys Link */
  310. hdev = hci_dev_get(rsp->id);
  311. if (!hdev)
  312. return -EINVAL;
  313. hcon = phylink_add(hdev, mgr, rsp->id, true);
  314. if (!hcon)
  315. goto done;
  316. BT_DBG("Created hcon %p: loc:%d -> rem:%d", hcon, hdev->id, rsp->id);
  317. mgr->bredr_chan->remote_amp_id = rsp->id;
  318. amp_create_phylink(hdev, mgr, hcon);
  319. done:
  320. hci_dev_put(hdev);
  321. skb_pull(skb, len);
  322. return 0;
  323. }
  324. static int a2mp_createphyslink_req(struct amp_mgr *mgr, struct sk_buff *skb,
  325. struct a2mp_cmd *hdr)
  326. {
  327. struct a2mp_physlink_req *req = (void *) skb->data;
  328. struct a2mp_physlink_rsp rsp;
  329. struct hci_dev *hdev;
  330. struct hci_conn *hcon;
  331. struct amp_ctrl *ctrl;
  332. if (le16_to_cpu(hdr->len) < sizeof(*req))
  333. return -EINVAL;
  334. BT_DBG("local_id %d, remote_id %d", req->local_id, req->remote_id);
  335. rsp.local_id = req->remote_id;
  336. rsp.remote_id = req->local_id;
  337. hdev = hci_dev_get(req->remote_id);
  338. if (!hdev || hdev->amp_type != HCI_AMP) {
  339. rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
  340. goto send_rsp;
  341. }
  342. ctrl = amp_ctrl_lookup(mgr, rsp.remote_id);
  343. if (!ctrl) {
  344. ctrl = amp_ctrl_add(mgr, rsp.remote_id);
  345. if (ctrl) {
  346. amp_ctrl_get(ctrl);
  347. } else {
  348. rsp.status = A2MP_STATUS_UNABLE_START_LINK_CREATION;
  349. goto send_rsp;
  350. }
  351. }
  352. if (ctrl) {
  353. size_t assoc_len = le16_to_cpu(hdr->len) - sizeof(*req);
  354. u8 *assoc;
  355. assoc = kzalloc(assoc_len, GFP_KERNEL);
  356. if (!assoc) {
  357. amp_ctrl_put(ctrl);
  358. return -ENOMEM;
  359. }
  360. memcpy(assoc, req->amp_assoc, assoc_len);
  361. ctrl->assoc = assoc;
  362. ctrl->assoc_len = assoc_len;
  363. ctrl->assoc_rem_len = assoc_len;
  364. ctrl->assoc_len_so_far = 0;
  365. amp_ctrl_put(ctrl);
  366. }
  367. hcon = phylink_add(hdev, mgr, req->local_id, false);
  368. if (hcon) {
  369. amp_accept_phylink(hdev, mgr, hcon);
  370. rsp.status = A2MP_STATUS_SUCCESS;
  371. } else {
  372. rsp.status = A2MP_STATUS_UNABLE_START_LINK_CREATION;
  373. }
  374. send_rsp:
  375. if (hdev)
  376. hci_dev_put(hdev);
  377. a2mp_send(mgr, A2MP_CREATEPHYSLINK_RSP, hdr->ident, sizeof(rsp),
  378. &rsp);
  379. skb_pull(skb, le16_to_cpu(hdr->len));
  380. return 0;
  381. }
  382. static int a2mp_discphyslink_req(struct amp_mgr *mgr, struct sk_buff *skb,
  383. struct a2mp_cmd *hdr)
  384. {
  385. struct a2mp_physlink_req *req = (void *) skb->data;
  386. struct a2mp_physlink_rsp rsp;
  387. struct hci_dev *hdev;
  388. struct hci_conn *hcon;
  389. if (le16_to_cpu(hdr->len) < sizeof(*req))
  390. return -EINVAL;
  391. BT_DBG("local_id %d remote_id %d", req->local_id, req->remote_id);
  392. rsp.local_id = req->remote_id;
  393. rsp.remote_id = req->local_id;
  394. rsp.status = A2MP_STATUS_SUCCESS;
  395. hdev = hci_dev_get(req->remote_id);
  396. if (!hdev) {
  397. rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
  398. goto send_rsp;
  399. }
  400. hcon = hci_conn_hash_lookup_ba(hdev, AMP_LINK, mgr->l2cap_conn->dst);
  401. if (!hcon) {
  402. BT_ERR("No phys link exist");
  403. rsp.status = A2MP_STATUS_NO_PHYSICAL_LINK_EXISTS;
  404. goto clean;
  405. }
  406. /* TODO Disconnect Phys Link here */
  407. clean:
  408. hci_dev_put(hdev);
  409. send_rsp:
  410. a2mp_send(mgr, A2MP_DISCONNPHYSLINK_RSP, hdr->ident, sizeof(rsp), &rsp);
  411. skb_pull(skb, sizeof(*req));
  412. return 0;
  413. }
  414. static inline int a2mp_cmd_rsp(struct amp_mgr *mgr, struct sk_buff *skb,
  415. struct a2mp_cmd *hdr)
  416. {
  417. BT_DBG("ident %d code 0x%2.2x", hdr->ident, hdr->code);
  418. skb_pull(skb, le16_to_cpu(hdr->len));
  419. return 0;
  420. }
  421. /* Handle A2MP signalling */
  422. static int a2mp_chan_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
  423. {
  424. struct a2mp_cmd *hdr;
  425. struct amp_mgr *mgr = chan->data;
  426. int err = 0;
  427. amp_mgr_get(mgr);
  428. while (skb->len >= sizeof(*hdr)) {
  429. u16 len;
  430. hdr = (void *) skb->data;
  431. len = le16_to_cpu(hdr->len);
  432. BT_DBG("code 0x%2.2x id %d len %u", hdr->code, hdr->ident, len);
  433. skb_pull(skb, sizeof(*hdr));
  434. if (len > skb->len || !hdr->ident) {
  435. err = -EINVAL;
  436. break;
  437. }
  438. mgr->ident = hdr->ident;
  439. switch (hdr->code) {
  440. case A2MP_COMMAND_REJ:
  441. a2mp_command_rej(mgr, skb, hdr);
  442. break;
  443. case A2MP_DISCOVER_REQ:
  444. err = a2mp_discover_req(mgr, skb, hdr);
  445. break;
  446. case A2MP_CHANGE_NOTIFY:
  447. err = a2mp_change_notify(mgr, skb, hdr);
  448. break;
  449. case A2MP_GETINFO_REQ:
  450. err = a2mp_getinfo_req(mgr, skb, hdr);
  451. break;
  452. case A2MP_GETAMPASSOC_REQ:
  453. err = a2mp_getampassoc_req(mgr, skb, hdr);
  454. break;
  455. case A2MP_CREATEPHYSLINK_REQ:
  456. err = a2mp_createphyslink_req(mgr, skb, hdr);
  457. break;
  458. case A2MP_DISCONNPHYSLINK_REQ:
  459. err = a2mp_discphyslink_req(mgr, skb, hdr);
  460. break;
  461. case A2MP_DISCOVER_RSP:
  462. err = a2mp_discover_rsp(mgr, skb, hdr);
  463. break;
  464. case A2MP_GETINFO_RSP:
  465. err = a2mp_getinfo_rsp(mgr, skb, hdr);
  466. break;
  467. case A2MP_GETAMPASSOC_RSP:
  468. err = a2mp_getampassoc_rsp(mgr, skb, hdr);
  469. break;
  470. case A2MP_CHANGE_RSP:
  471. case A2MP_CREATEPHYSLINK_RSP:
  472. case A2MP_DISCONNPHYSLINK_RSP:
  473. err = a2mp_cmd_rsp(mgr, skb, hdr);
  474. break;
  475. default:
  476. BT_ERR("Unknown A2MP sig cmd 0x%2.2x", hdr->code);
  477. err = -EINVAL;
  478. break;
  479. }
  480. }
  481. if (err) {
  482. struct a2mp_cmd_rej rej;
  483. rej.reason = __constant_cpu_to_le16(0);
  484. hdr = (void *) skb->data;
  485. BT_DBG("Send A2MP Rej: cmd 0x%2.2x err %d", hdr->code, err);
  486. a2mp_send(mgr, A2MP_COMMAND_REJ, hdr->ident, sizeof(rej),
  487. &rej);
  488. }
  489. /* Always free skb and return success error code to prevent
  490. from sending L2CAP Disconnect over A2MP channel */
  491. kfree_skb(skb);
  492. amp_mgr_put(mgr);
  493. return 0;
  494. }
  495. static void a2mp_chan_close_cb(struct l2cap_chan *chan)
  496. {
  497. l2cap_chan_put(chan);
  498. }
  499. static void a2mp_chan_state_change_cb(struct l2cap_chan *chan, int state)
  500. {
  501. struct amp_mgr *mgr = chan->data;
  502. if (!mgr)
  503. return;
  504. BT_DBG("chan %p state %s", chan, state_to_string(state));
  505. chan->state = state;
  506. switch (state) {
  507. case BT_CLOSED:
  508. if (mgr)
  509. amp_mgr_put(mgr);
  510. break;
  511. }
  512. }
  513. static struct sk_buff *a2mp_chan_alloc_skb_cb(struct l2cap_chan *chan,
  514. unsigned long len, int nb)
  515. {
  516. return bt_skb_alloc(len, GFP_KERNEL);
  517. }
  518. static struct l2cap_ops a2mp_chan_ops = {
  519. .name = "L2CAP A2MP channel",
  520. .recv = a2mp_chan_recv_cb,
  521. .close = a2mp_chan_close_cb,
  522. .state_change = a2mp_chan_state_change_cb,
  523. .alloc_skb = a2mp_chan_alloc_skb_cb,
  524. /* Not implemented for A2MP */
  525. .new_connection = l2cap_chan_no_new_connection,
  526. .teardown = l2cap_chan_no_teardown,
  527. .ready = l2cap_chan_no_ready,
  528. .defer = l2cap_chan_no_defer,
  529. };
  530. static struct l2cap_chan *a2mp_chan_open(struct l2cap_conn *conn, bool locked)
  531. {
  532. struct l2cap_chan *chan;
  533. int err;
  534. chan = l2cap_chan_create();
  535. if (!chan)
  536. return NULL;
  537. BT_DBG("chan %p", chan);
  538. chan->chan_type = L2CAP_CHAN_CONN_FIX_A2MP;
  539. chan->flush_to = L2CAP_DEFAULT_FLUSH_TO;
  540. chan->ops = &a2mp_chan_ops;
  541. l2cap_chan_set_defaults(chan);
  542. chan->remote_max_tx = chan->max_tx;
  543. chan->remote_tx_win = chan->tx_win;
  544. chan->retrans_timeout = L2CAP_DEFAULT_RETRANS_TO;
  545. chan->monitor_timeout = L2CAP_DEFAULT_MONITOR_TO;
  546. skb_queue_head_init(&chan->tx_q);
  547. chan->mode = L2CAP_MODE_ERTM;
  548. err = l2cap_ertm_init(chan);
  549. if (err < 0) {
  550. l2cap_chan_del(chan, 0);
  551. return NULL;
  552. }
  553. chan->conf_state = 0;
  554. if (locked)
  555. __l2cap_chan_add(conn, chan);
  556. else
  557. l2cap_chan_add(conn, chan);
  558. chan->remote_mps = chan->omtu;
  559. chan->mps = chan->omtu;
  560. chan->state = BT_CONNECTED;
  561. return chan;
  562. }
  563. /* AMP Manager functions */
  564. struct amp_mgr *amp_mgr_get(struct amp_mgr *mgr)
  565. {
  566. BT_DBG("mgr %p orig refcnt %d", mgr, atomic_read(&mgr->kref.refcount));
  567. kref_get(&mgr->kref);
  568. return mgr;
  569. }
  570. static void amp_mgr_destroy(struct kref *kref)
  571. {
  572. struct amp_mgr *mgr = container_of(kref, struct amp_mgr, kref);
  573. BT_DBG("mgr %p", mgr);
  574. mutex_lock(&amp_mgr_list_lock);
  575. list_del(&mgr->list);
  576. mutex_unlock(&amp_mgr_list_lock);
  577. amp_ctrl_list_flush(mgr);
  578. kfree(mgr);
  579. }
  580. int amp_mgr_put(struct amp_mgr *mgr)
  581. {
  582. BT_DBG("mgr %p orig refcnt %d", mgr, atomic_read(&mgr->kref.refcount));
  583. return kref_put(&mgr->kref, &amp_mgr_destroy);
  584. }
  585. static struct amp_mgr *amp_mgr_create(struct l2cap_conn *conn, bool locked)
  586. {
  587. struct amp_mgr *mgr;
  588. struct l2cap_chan *chan;
  589. mgr = kzalloc(sizeof(*mgr), GFP_KERNEL);
  590. if (!mgr)
  591. return NULL;
  592. BT_DBG("conn %p mgr %p", conn, mgr);
  593. mgr->l2cap_conn = conn;
  594. chan = a2mp_chan_open(conn, locked);
  595. if (!chan) {
  596. kfree(mgr);
  597. return NULL;
  598. }
  599. mgr->a2mp_chan = chan;
  600. chan->data = mgr;
  601. conn->hcon->amp_mgr = mgr;
  602. kref_init(&mgr->kref);
  603. /* Remote AMP ctrl list initialization */
  604. INIT_LIST_HEAD(&mgr->amp_ctrls);
  605. mutex_init(&mgr->amp_ctrls_lock);
  606. mutex_lock(&amp_mgr_list_lock);
  607. list_add(&mgr->list, &amp_mgr_list);
  608. mutex_unlock(&amp_mgr_list_lock);
  609. return mgr;
  610. }
  611. struct l2cap_chan *a2mp_channel_create(struct l2cap_conn *conn,
  612. struct sk_buff *skb)
  613. {
  614. struct amp_mgr *mgr;
  615. mgr = amp_mgr_create(conn, false);
  616. if (!mgr) {
  617. BT_ERR("Could not create AMP manager");
  618. return NULL;
  619. }
  620. BT_DBG("mgr: %p chan %p", mgr, mgr->a2mp_chan);
  621. return mgr->a2mp_chan;
  622. }
  623. struct amp_mgr *amp_mgr_lookup_by_state(u8 state)
  624. {
  625. struct amp_mgr *mgr;
  626. mutex_lock(&amp_mgr_list_lock);
  627. list_for_each_entry(mgr, &amp_mgr_list, list) {
  628. if (mgr->state == state) {
  629. amp_mgr_get(mgr);
  630. mutex_unlock(&amp_mgr_list_lock);
  631. return mgr;
  632. }
  633. }
  634. mutex_unlock(&amp_mgr_list_lock);
  635. return NULL;
  636. }
  637. void a2mp_send_getinfo_rsp(struct hci_dev *hdev)
  638. {
  639. struct amp_mgr *mgr;
  640. struct a2mp_info_rsp rsp;
  641. mgr = amp_mgr_lookup_by_state(READ_LOC_AMP_INFO);
  642. if (!mgr)
  643. return;
  644. BT_DBG("%s mgr %p", hdev->name, mgr);
  645. rsp.id = hdev->id;
  646. rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
  647. if (hdev->amp_type != HCI_BREDR) {
  648. rsp.status = 0;
  649. rsp.total_bw = cpu_to_le32(hdev->amp_total_bw);
  650. rsp.max_bw = cpu_to_le32(hdev->amp_max_bw);
  651. rsp.min_latency = cpu_to_le32(hdev->amp_min_latency);
  652. rsp.pal_cap = cpu_to_le16(hdev->amp_pal_cap);
  653. rsp.assoc_size = cpu_to_le16(hdev->amp_assoc_size);
  654. }
  655. a2mp_send(mgr, A2MP_GETINFO_RSP, mgr->ident, sizeof(rsp), &rsp);
  656. amp_mgr_put(mgr);
  657. }
  658. void a2mp_send_getampassoc_rsp(struct hci_dev *hdev, u8 status)
  659. {
  660. struct amp_mgr *mgr;
  661. struct amp_assoc *loc_assoc = &hdev->loc_assoc;
  662. struct a2mp_amp_assoc_rsp *rsp;
  663. size_t len;
  664. mgr = amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC);
  665. if (!mgr)
  666. return;
  667. BT_DBG("%s mgr %p", hdev->name, mgr);
  668. len = sizeof(struct a2mp_amp_assoc_rsp) + loc_assoc->len;
  669. rsp = kzalloc(len, GFP_KERNEL);
  670. if (!rsp) {
  671. amp_mgr_put(mgr);
  672. return;
  673. }
  674. rsp->id = hdev->id;
  675. if (status) {
  676. rsp->status = A2MP_STATUS_INVALID_CTRL_ID;
  677. } else {
  678. rsp->status = A2MP_STATUS_SUCCESS;
  679. memcpy(rsp->amp_assoc, loc_assoc->data, loc_assoc->len);
  680. }
  681. a2mp_send(mgr, A2MP_GETAMPASSOC_RSP, mgr->ident, len, rsp);
  682. amp_mgr_put(mgr);
  683. kfree(rsp);
  684. }
  685. void a2mp_send_create_phy_link_req(struct hci_dev *hdev, u8 status)
  686. {
  687. struct amp_mgr *mgr;
  688. struct amp_assoc *loc_assoc = &hdev->loc_assoc;
  689. struct a2mp_physlink_req *req;
  690. struct l2cap_chan *bredr_chan;
  691. size_t len;
  692. mgr = amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC_FINAL);
  693. if (!mgr)
  694. return;
  695. len = sizeof(*req) + loc_assoc->len;
  696. BT_DBG("%s mgr %p assoc_len %zu", hdev->name, mgr, len);
  697. req = kzalloc(len, GFP_KERNEL);
  698. if (!req) {
  699. amp_mgr_put(mgr);
  700. return;
  701. }
  702. bredr_chan = mgr->bredr_chan;
  703. if (!bredr_chan)
  704. goto clean;
  705. req->local_id = hdev->id;
  706. req->remote_id = bredr_chan->remote_amp_id;
  707. memcpy(req->amp_assoc, loc_assoc->data, loc_assoc->len);
  708. a2mp_send(mgr, A2MP_CREATEPHYSLINK_REQ, __next_ident(mgr), len, req);
  709. clean:
  710. amp_mgr_put(mgr);
  711. kfree(req);
  712. }
  713. void a2mp_discover_amp(struct l2cap_chan *chan)
  714. {
  715. struct l2cap_conn *conn = chan->conn;
  716. struct amp_mgr *mgr = conn->hcon->amp_mgr;
  717. struct a2mp_discov_req req;
  718. BT_DBG("chan %p conn %p mgr %p", chan, conn, mgr);
  719. if (!mgr) {
  720. mgr = amp_mgr_create(conn, true);
  721. if (!mgr)
  722. return;
  723. }
  724. mgr->bredr_chan = chan;
  725. req.mtu = cpu_to_le16(L2CAP_A2MP_DEFAULT_MTU);
  726. req.ext_feat = 0;
  727. a2mp_send(mgr, A2MP_DISCOVER_REQ, 1, sizeof(req), &req);
  728. }