hci_request.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362
  1. /*
  2. BlueZ - Bluetooth protocol stack for Linux
  3. Copyright (C) 2014 Intel Corporation
  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 as
  6. published by the Free Software Foundation;
  7. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  8. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  9. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
  10. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
  11. CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
  12. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
  16. COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
  17. SOFTWARE IS DISCLAIMED.
  18. */
  19. #include <net/bluetooth/bluetooth.h>
  20. #include <net/bluetooth/hci_core.h>
  21. #include <net/bluetooth/mgmt.h>
  22. #include "smp.h"
  23. #include "hci_request.h"
  24. #define HCI_REQ_DONE 0
  25. #define HCI_REQ_PEND 1
  26. #define HCI_REQ_CANCELED 2
  27. void hci_req_init(struct hci_request *req, struct hci_dev *hdev)
  28. {
  29. skb_queue_head_init(&req->cmd_q);
  30. req->hdev = hdev;
  31. req->err = 0;
  32. }
  33. static int req_run(struct hci_request *req, hci_req_complete_t complete,
  34. hci_req_complete_skb_t complete_skb)
  35. {
  36. struct hci_dev *hdev = req->hdev;
  37. struct sk_buff *skb;
  38. unsigned long flags;
  39. BT_DBG("length %u", skb_queue_len(&req->cmd_q));
  40. /* If an error occurred during request building, remove all HCI
  41. * commands queued on the HCI request queue.
  42. */
  43. if (req->err) {
  44. skb_queue_purge(&req->cmd_q);
  45. return req->err;
  46. }
  47. /* Do not allow empty requests */
  48. if (skb_queue_empty(&req->cmd_q))
  49. return -ENODATA;
  50. skb = skb_peek_tail(&req->cmd_q);
  51. if (complete) {
  52. bt_cb(skb)->hci.req_complete = complete;
  53. } else if (complete_skb) {
  54. bt_cb(skb)->hci.req_complete_skb = complete_skb;
  55. bt_cb(skb)->hci.req_flags |= HCI_REQ_SKB;
  56. }
  57. spin_lock_irqsave(&hdev->cmd_q.lock, flags);
  58. skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q);
  59. spin_unlock_irqrestore(&hdev->cmd_q.lock, flags);
  60. queue_work(hdev->workqueue, &hdev->cmd_work);
  61. return 0;
  62. }
  63. int hci_req_run(struct hci_request *req, hci_req_complete_t complete)
  64. {
  65. return req_run(req, complete, NULL);
  66. }
  67. int hci_req_run_skb(struct hci_request *req, hci_req_complete_skb_t complete)
  68. {
  69. return req_run(req, NULL, complete);
  70. }
  71. static void hci_req_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode,
  72. struct sk_buff *skb)
  73. {
  74. BT_DBG("%s result 0x%2.2x", hdev->name, result);
  75. if (hdev->req_status == HCI_REQ_PEND) {
  76. hdev->req_result = result;
  77. hdev->req_status = HCI_REQ_DONE;
  78. if (skb)
  79. hdev->req_skb = skb_get(skb);
  80. wake_up_interruptible(&hdev->req_wait_q);
  81. }
  82. }
  83. void hci_req_sync_cancel(struct hci_dev *hdev, int err)
  84. {
  85. BT_DBG("%s err 0x%2.2x", hdev->name, err);
  86. if (hdev->req_status == HCI_REQ_PEND) {
  87. hdev->req_result = err;
  88. hdev->req_status = HCI_REQ_CANCELED;
  89. wake_up_interruptible(&hdev->req_wait_q);
  90. }
  91. }
  92. struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
  93. const void *param, u8 event, u32 timeout)
  94. {
  95. DECLARE_WAITQUEUE(wait, current);
  96. struct hci_request req;
  97. struct sk_buff *skb;
  98. int err = 0;
  99. BT_DBG("%s", hdev->name);
  100. hci_req_init(&req, hdev);
  101. hci_req_add_ev(&req, opcode, plen, param, event);
  102. hdev->req_status = HCI_REQ_PEND;
  103. add_wait_queue(&hdev->req_wait_q, &wait);
  104. set_current_state(TASK_INTERRUPTIBLE);
  105. err = hci_req_run_skb(&req, hci_req_sync_complete);
  106. if (err < 0) {
  107. remove_wait_queue(&hdev->req_wait_q, &wait);
  108. set_current_state(TASK_RUNNING);
  109. return ERR_PTR(err);
  110. }
  111. schedule_timeout(timeout);
  112. remove_wait_queue(&hdev->req_wait_q, &wait);
  113. if (signal_pending(current))
  114. return ERR_PTR(-EINTR);
  115. switch (hdev->req_status) {
  116. case HCI_REQ_DONE:
  117. err = -bt_to_errno(hdev->req_result);
  118. break;
  119. case HCI_REQ_CANCELED:
  120. err = -hdev->req_result;
  121. break;
  122. default:
  123. err = -ETIMEDOUT;
  124. break;
  125. }
  126. hdev->req_status = hdev->req_result = 0;
  127. skb = hdev->req_skb;
  128. hdev->req_skb = NULL;
  129. BT_DBG("%s end: err %d", hdev->name, err);
  130. if (err < 0) {
  131. kfree_skb(skb);
  132. return ERR_PTR(err);
  133. }
  134. if (!skb)
  135. return ERR_PTR(-ENODATA);
  136. return skb;
  137. }
  138. EXPORT_SYMBOL(__hci_cmd_sync_ev);
  139. struct sk_buff *__hci_cmd_sync(struct hci_dev *hdev, u16 opcode, u32 plen,
  140. const void *param, u32 timeout)
  141. {
  142. return __hci_cmd_sync_ev(hdev, opcode, plen, param, 0, timeout);
  143. }
  144. EXPORT_SYMBOL(__hci_cmd_sync);
  145. /* Execute request and wait for completion. */
  146. int __hci_req_sync(struct hci_dev *hdev, int (*func)(struct hci_request *req,
  147. unsigned long opt),
  148. unsigned long opt, u32 timeout, u8 *hci_status)
  149. {
  150. struct hci_request req;
  151. DECLARE_WAITQUEUE(wait, current);
  152. int err = 0;
  153. BT_DBG("%s start", hdev->name);
  154. hci_req_init(&req, hdev);
  155. hdev->req_status = HCI_REQ_PEND;
  156. err = func(&req, opt);
  157. if (err) {
  158. if (hci_status)
  159. *hci_status = HCI_ERROR_UNSPECIFIED;
  160. return err;
  161. }
  162. add_wait_queue(&hdev->req_wait_q, &wait);
  163. set_current_state(TASK_INTERRUPTIBLE);
  164. err = hci_req_run_skb(&req, hci_req_sync_complete);
  165. if (err < 0) {
  166. hdev->req_status = 0;
  167. remove_wait_queue(&hdev->req_wait_q, &wait);
  168. set_current_state(TASK_RUNNING);
  169. /* ENODATA means the HCI request command queue is empty.
  170. * This can happen when a request with conditionals doesn't
  171. * trigger any commands to be sent. This is normal behavior
  172. * and should not trigger an error return.
  173. */
  174. if (err == -ENODATA) {
  175. if (hci_status)
  176. *hci_status = 0;
  177. return 0;
  178. }
  179. if (hci_status)
  180. *hci_status = HCI_ERROR_UNSPECIFIED;
  181. return err;
  182. }
  183. schedule_timeout(timeout);
  184. remove_wait_queue(&hdev->req_wait_q, &wait);
  185. if (signal_pending(current))
  186. return -EINTR;
  187. switch (hdev->req_status) {
  188. case HCI_REQ_DONE:
  189. err = -bt_to_errno(hdev->req_result);
  190. if (hci_status)
  191. *hci_status = hdev->req_result;
  192. break;
  193. case HCI_REQ_CANCELED:
  194. err = -hdev->req_result;
  195. if (hci_status)
  196. *hci_status = HCI_ERROR_UNSPECIFIED;
  197. break;
  198. default:
  199. err = -ETIMEDOUT;
  200. if (hci_status)
  201. *hci_status = HCI_ERROR_UNSPECIFIED;
  202. break;
  203. }
  204. kfree_skb(hdev->req_skb);
  205. hdev->req_skb = NULL;
  206. hdev->req_status = hdev->req_result = 0;
  207. BT_DBG("%s end: err %d", hdev->name, err);
  208. return err;
  209. }
  210. int hci_req_sync(struct hci_dev *hdev, int (*req)(struct hci_request *req,
  211. unsigned long opt),
  212. unsigned long opt, u32 timeout, u8 *hci_status)
  213. {
  214. int ret;
  215. if (!test_bit(HCI_UP, &hdev->flags))
  216. return -ENETDOWN;
  217. /* Serialize all requests */
  218. hci_req_sync_lock(hdev);
  219. ret = __hci_req_sync(hdev, req, opt, timeout, hci_status);
  220. hci_req_sync_unlock(hdev);
  221. return ret;
  222. }
  223. struct sk_buff *hci_prepare_cmd(struct hci_dev *hdev, u16 opcode, u32 plen,
  224. const void *param)
  225. {
  226. int len = HCI_COMMAND_HDR_SIZE + plen;
  227. struct hci_command_hdr *hdr;
  228. struct sk_buff *skb;
  229. skb = bt_skb_alloc(len, GFP_ATOMIC);
  230. if (!skb)
  231. return NULL;
  232. hdr = (struct hci_command_hdr *) skb_put(skb, HCI_COMMAND_HDR_SIZE);
  233. hdr->opcode = cpu_to_le16(opcode);
  234. hdr->plen = plen;
  235. if (plen)
  236. memcpy(skb_put(skb, plen), param, plen);
  237. BT_DBG("skb len %d", skb->len);
  238. hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
  239. hci_skb_opcode(skb) = opcode;
  240. return skb;
  241. }
  242. /* Queue a command to an asynchronous HCI request */
  243. void hci_req_add_ev(struct hci_request *req, u16 opcode, u32 plen,
  244. const void *param, u8 event)
  245. {
  246. struct hci_dev *hdev = req->hdev;
  247. struct sk_buff *skb;
  248. BT_DBG("%s opcode 0x%4.4x plen %d", hdev->name, opcode, plen);
  249. /* If an error occurred during request building, there is no point in
  250. * queueing the HCI command. We can simply return.
  251. */
  252. if (req->err)
  253. return;
  254. skb = hci_prepare_cmd(hdev, opcode, plen, param);
  255. if (!skb) {
  256. BT_ERR("%s no memory for command (opcode 0x%4.4x)",
  257. hdev->name, opcode);
  258. req->err = -ENOMEM;
  259. return;
  260. }
  261. if (skb_queue_empty(&req->cmd_q))
  262. bt_cb(skb)->hci.req_flags |= HCI_REQ_START;
  263. bt_cb(skb)->hci.req_event = event;
  264. skb_queue_tail(&req->cmd_q, skb);
  265. }
  266. void hci_req_add(struct hci_request *req, u16 opcode, u32 plen,
  267. const void *param)
  268. {
  269. hci_req_add_ev(req, opcode, plen, param, 0);
  270. }
  271. void __hci_req_write_fast_connectable(struct hci_request *req, bool enable)
  272. {
  273. struct hci_dev *hdev = req->hdev;
  274. struct hci_cp_write_page_scan_activity acp;
  275. u8 type;
  276. if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
  277. return;
  278. if (hdev->hci_ver < BLUETOOTH_VER_1_2)
  279. return;
  280. if (enable) {
  281. type = PAGE_SCAN_TYPE_INTERLACED;
  282. /* 160 msec page scan interval */
  283. acp.interval = cpu_to_le16(0x0100);
  284. } else {
  285. type = PAGE_SCAN_TYPE_STANDARD; /* default */
  286. /* default 1.28 sec page scan */
  287. acp.interval = cpu_to_le16(0x0800);
  288. }
  289. acp.window = cpu_to_le16(0x0012);
  290. if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval ||
  291. __cpu_to_le16(hdev->page_scan_window) != acp.window)
  292. hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY,
  293. sizeof(acp), &acp);
  294. if (hdev->page_scan_type != type)
  295. hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type);
  296. }
  297. /* This function controls the background scanning based on hdev->pend_le_conns
  298. * list. If there are pending LE connection we start the background scanning,
  299. * otherwise we stop it.
  300. *
  301. * This function requires the caller holds hdev->lock.
  302. */
  303. static void __hci_update_background_scan(struct hci_request *req)
  304. {
  305. struct hci_dev *hdev = req->hdev;
  306. if (!test_bit(HCI_UP, &hdev->flags) ||
  307. test_bit(HCI_INIT, &hdev->flags) ||
  308. hci_dev_test_flag(hdev, HCI_SETUP) ||
  309. hci_dev_test_flag(hdev, HCI_CONFIG) ||
  310. hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
  311. hci_dev_test_flag(hdev, HCI_UNREGISTER))
  312. return;
  313. /* No point in doing scanning if LE support hasn't been enabled */
  314. if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
  315. return;
  316. /* If discovery is active don't interfere with it */
  317. if (hdev->discovery.state != DISCOVERY_STOPPED)
  318. return;
  319. /* Reset RSSI and UUID filters when starting background scanning
  320. * since these filters are meant for service discovery only.
  321. *
  322. * The Start Discovery and Start Service Discovery operations
  323. * ensure to set proper values for RSSI threshold and UUID
  324. * filter list. So it is safe to just reset them here.
  325. */
  326. hci_discovery_filter_clear(hdev);
  327. if (list_empty(&hdev->pend_le_conns) &&
  328. list_empty(&hdev->pend_le_reports)) {
  329. /* If there is no pending LE connections or devices
  330. * to be scanned for, we should stop the background
  331. * scanning.
  332. */
  333. /* If controller is not scanning we are done. */
  334. if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
  335. return;
  336. hci_req_add_le_scan_disable(req);
  337. BT_DBG("%s stopping background scanning", hdev->name);
  338. } else {
  339. /* If there is at least one pending LE connection, we should
  340. * keep the background scan running.
  341. */
  342. /* If controller is connecting, we should not start scanning
  343. * since some controllers are not able to scan and connect at
  344. * the same time.
  345. */
  346. if (hci_lookup_le_connect(hdev))
  347. return;
  348. /* If controller is currently scanning, we stop it to ensure we
  349. * don't miss any advertising (due to duplicates filter).
  350. */
  351. if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
  352. hci_req_add_le_scan_disable(req);
  353. hci_req_add_le_passive_scan(req);
  354. BT_DBG("%s starting background scanning", hdev->name);
  355. }
  356. }
  357. void __hci_req_update_name(struct hci_request *req)
  358. {
  359. struct hci_dev *hdev = req->hdev;
  360. struct hci_cp_write_local_name cp;
  361. memcpy(cp.name, hdev->dev_name, sizeof(cp.name));
  362. hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp);
  363. }
  364. #define PNP_INFO_SVCLASS_ID 0x1200
  365. static u8 *create_uuid16_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
  366. {
  367. u8 *ptr = data, *uuids_start = NULL;
  368. struct bt_uuid *uuid;
  369. if (len < 4)
  370. return ptr;
  371. list_for_each_entry(uuid, &hdev->uuids, list) {
  372. u16 uuid16;
  373. if (uuid->size != 16)
  374. continue;
  375. uuid16 = get_unaligned_le16(&uuid->uuid[12]);
  376. if (uuid16 < 0x1100)
  377. continue;
  378. if (uuid16 == PNP_INFO_SVCLASS_ID)
  379. continue;
  380. if (!uuids_start) {
  381. uuids_start = ptr;
  382. uuids_start[0] = 1;
  383. uuids_start[1] = EIR_UUID16_ALL;
  384. ptr += 2;
  385. }
  386. /* Stop if not enough space to put next UUID */
  387. if ((ptr - data) + sizeof(u16) > len) {
  388. uuids_start[1] = EIR_UUID16_SOME;
  389. break;
  390. }
  391. *ptr++ = (uuid16 & 0x00ff);
  392. *ptr++ = (uuid16 & 0xff00) >> 8;
  393. uuids_start[0] += sizeof(uuid16);
  394. }
  395. return ptr;
  396. }
  397. static u8 *create_uuid32_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
  398. {
  399. u8 *ptr = data, *uuids_start = NULL;
  400. struct bt_uuid *uuid;
  401. if (len < 6)
  402. return ptr;
  403. list_for_each_entry(uuid, &hdev->uuids, list) {
  404. if (uuid->size != 32)
  405. continue;
  406. if (!uuids_start) {
  407. uuids_start = ptr;
  408. uuids_start[0] = 1;
  409. uuids_start[1] = EIR_UUID32_ALL;
  410. ptr += 2;
  411. }
  412. /* Stop if not enough space to put next UUID */
  413. if ((ptr - data) + sizeof(u32) > len) {
  414. uuids_start[1] = EIR_UUID32_SOME;
  415. break;
  416. }
  417. memcpy(ptr, &uuid->uuid[12], sizeof(u32));
  418. ptr += sizeof(u32);
  419. uuids_start[0] += sizeof(u32);
  420. }
  421. return ptr;
  422. }
  423. static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
  424. {
  425. u8 *ptr = data, *uuids_start = NULL;
  426. struct bt_uuid *uuid;
  427. if (len < 18)
  428. return ptr;
  429. list_for_each_entry(uuid, &hdev->uuids, list) {
  430. if (uuid->size != 128)
  431. continue;
  432. if (!uuids_start) {
  433. uuids_start = ptr;
  434. uuids_start[0] = 1;
  435. uuids_start[1] = EIR_UUID128_ALL;
  436. ptr += 2;
  437. }
  438. /* Stop if not enough space to put next UUID */
  439. if ((ptr - data) + 16 > len) {
  440. uuids_start[1] = EIR_UUID128_SOME;
  441. break;
  442. }
  443. memcpy(ptr, uuid->uuid, 16);
  444. ptr += 16;
  445. uuids_start[0] += 16;
  446. }
  447. return ptr;
  448. }
  449. static void create_eir(struct hci_dev *hdev, u8 *data)
  450. {
  451. u8 *ptr = data;
  452. size_t name_len;
  453. name_len = strlen(hdev->dev_name);
  454. if (name_len > 0) {
  455. /* EIR Data type */
  456. if (name_len > 48) {
  457. name_len = 48;
  458. ptr[1] = EIR_NAME_SHORT;
  459. } else
  460. ptr[1] = EIR_NAME_COMPLETE;
  461. /* EIR Data length */
  462. ptr[0] = name_len + 1;
  463. memcpy(ptr + 2, hdev->dev_name, name_len);
  464. ptr += (name_len + 2);
  465. }
  466. if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) {
  467. ptr[0] = 2;
  468. ptr[1] = EIR_TX_POWER;
  469. ptr[2] = (u8) hdev->inq_tx_power;
  470. ptr += 3;
  471. }
  472. if (hdev->devid_source > 0) {
  473. ptr[0] = 9;
  474. ptr[1] = EIR_DEVICE_ID;
  475. put_unaligned_le16(hdev->devid_source, ptr + 2);
  476. put_unaligned_le16(hdev->devid_vendor, ptr + 4);
  477. put_unaligned_le16(hdev->devid_product, ptr + 6);
  478. put_unaligned_le16(hdev->devid_version, ptr + 8);
  479. ptr += 10;
  480. }
  481. ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
  482. ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
  483. ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
  484. }
  485. void __hci_req_update_eir(struct hci_request *req)
  486. {
  487. struct hci_dev *hdev = req->hdev;
  488. struct hci_cp_write_eir cp;
  489. if (!hdev_is_powered(hdev))
  490. return;
  491. if (!lmp_ext_inq_capable(hdev))
  492. return;
  493. if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
  494. return;
  495. if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
  496. return;
  497. memset(&cp, 0, sizeof(cp));
  498. create_eir(hdev, cp.data);
  499. if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0)
  500. return;
  501. memcpy(hdev->eir, cp.data, sizeof(cp.data));
  502. hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
  503. }
  504. void hci_req_add_le_scan_disable(struct hci_request *req)
  505. {
  506. struct hci_cp_le_set_scan_enable cp;
  507. memset(&cp, 0, sizeof(cp));
  508. cp.enable = LE_SCAN_DISABLE;
  509. hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp);
  510. }
  511. static void add_to_white_list(struct hci_request *req,
  512. struct hci_conn_params *params)
  513. {
  514. struct hci_cp_le_add_to_white_list cp;
  515. cp.bdaddr_type = params->addr_type;
  516. bacpy(&cp.bdaddr, &params->addr);
  517. hci_req_add(req, HCI_OP_LE_ADD_TO_WHITE_LIST, sizeof(cp), &cp);
  518. }
  519. static u8 update_white_list(struct hci_request *req)
  520. {
  521. struct hci_dev *hdev = req->hdev;
  522. struct hci_conn_params *params;
  523. struct bdaddr_list *b;
  524. uint8_t white_list_entries = 0;
  525. /* Go through the current white list programmed into the
  526. * controller one by one and check if that address is still
  527. * in the list of pending connections or list of devices to
  528. * report. If not present in either list, then queue the
  529. * command to remove it from the controller.
  530. */
  531. list_for_each_entry(b, &hdev->le_white_list, list) {
  532. /* If the device is neither in pend_le_conns nor
  533. * pend_le_reports then remove it from the whitelist.
  534. */
  535. if (!hci_pend_le_action_lookup(&hdev->pend_le_conns,
  536. &b->bdaddr, b->bdaddr_type) &&
  537. !hci_pend_le_action_lookup(&hdev->pend_le_reports,
  538. &b->bdaddr, b->bdaddr_type)) {
  539. struct hci_cp_le_del_from_white_list cp;
  540. cp.bdaddr_type = b->bdaddr_type;
  541. bacpy(&cp.bdaddr, &b->bdaddr);
  542. hci_req_add(req, HCI_OP_LE_DEL_FROM_WHITE_LIST,
  543. sizeof(cp), &cp);
  544. continue;
  545. }
  546. if (hci_find_irk_by_addr(hdev, &b->bdaddr, b->bdaddr_type)) {
  547. /* White list can not be used with RPAs */
  548. return 0x00;
  549. }
  550. white_list_entries++;
  551. }
  552. /* Since all no longer valid white list entries have been
  553. * removed, walk through the list of pending connections
  554. * and ensure that any new device gets programmed into
  555. * the controller.
  556. *
  557. * If the list of the devices is larger than the list of
  558. * available white list entries in the controller, then
  559. * just abort and return filer policy value to not use the
  560. * white list.
  561. */
  562. list_for_each_entry(params, &hdev->pend_le_conns, action) {
  563. if (hci_bdaddr_list_lookup(&hdev->le_white_list,
  564. &params->addr, params->addr_type))
  565. continue;
  566. if (white_list_entries >= hdev->le_white_list_size) {
  567. /* Select filter policy to accept all advertising */
  568. return 0x00;
  569. }
  570. if (hci_find_irk_by_addr(hdev, &params->addr,
  571. params->addr_type)) {
  572. /* White list can not be used with RPAs */
  573. return 0x00;
  574. }
  575. white_list_entries++;
  576. add_to_white_list(req, params);
  577. }
  578. /* After adding all new pending connections, walk through
  579. * the list of pending reports and also add these to the
  580. * white list if there is still space.
  581. */
  582. list_for_each_entry(params, &hdev->pend_le_reports, action) {
  583. if (hci_bdaddr_list_lookup(&hdev->le_white_list,
  584. &params->addr, params->addr_type))
  585. continue;
  586. if (white_list_entries >= hdev->le_white_list_size) {
  587. /* Select filter policy to accept all advertising */
  588. return 0x00;
  589. }
  590. if (hci_find_irk_by_addr(hdev, &params->addr,
  591. params->addr_type)) {
  592. /* White list can not be used with RPAs */
  593. return 0x00;
  594. }
  595. white_list_entries++;
  596. add_to_white_list(req, params);
  597. }
  598. /* Select filter policy to use white list */
  599. return 0x01;
  600. }
  601. static bool scan_use_rpa(struct hci_dev *hdev)
  602. {
  603. return hci_dev_test_flag(hdev, HCI_PRIVACY);
  604. }
  605. void hci_req_add_le_passive_scan(struct hci_request *req)
  606. {
  607. struct hci_cp_le_set_scan_param param_cp;
  608. struct hci_cp_le_set_scan_enable enable_cp;
  609. struct hci_dev *hdev = req->hdev;
  610. u8 own_addr_type;
  611. u8 filter_policy;
  612. /* Set require_privacy to false since no SCAN_REQ are send
  613. * during passive scanning. Not using an non-resolvable address
  614. * here is important so that peer devices using direct
  615. * advertising with our address will be correctly reported
  616. * by the controller.
  617. */
  618. if (hci_update_random_address(req, false, scan_use_rpa(hdev),
  619. &own_addr_type))
  620. return;
  621. /* Adding or removing entries from the white list must
  622. * happen before enabling scanning. The controller does
  623. * not allow white list modification while scanning.
  624. */
  625. filter_policy = update_white_list(req);
  626. /* When the controller is using random resolvable addresses and
  627. * with that having LE privacy enabled, then controllers with
  628. * Extended Scanner Filter Policies support can now enable support
  629. * for handling directed advertising.
  630. *
  631. * So instead of using filter polices 0x00 (no whitelist)
  632. * and 0x01 (whitelist enabled) use the new filter policies
  633. * 0x02 (no whitelist) and 0x03 (whitelist enabled).
  634. */
  635. if (hci_dev_test_flag(hdev, HCI_PRIVACY) &&
  636. (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY))
  637. filter_policy |= 0x02;
  638. memset(&param_cp, 0, sizeof(param_cp));
  639. param_cp.type = LE_SCAN_PASSIVE;
  640. param_cp.interval = cpu_to_le16(hdev->le_scan_interval);
  641. param_cp.window = cpu_to_le16(hdev->le_scan_window);
  642. param_cp.own_address_type = own_addr_type;
  643. param_cp.filter_policy = filter_policy;
  644. hci_req_add(req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp),
  645. &param_cp);
  646. memset(&enable_cp, 0, sizeof(enable_cp));
  647. enable_cp.enable = LE_SCAN_ENABLE;
  648. enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
  649. hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp),
  650. &enable_cp);
  651. }
  652. static u8 get_cur_adv_instance_scan_rsp_len(struct hci_dev *hdev)
  653. {
  654. u8 instance = hdev->cur_adv_instance;
  655. struct adv_info *adv_instance;
  656. /* Ignore instance 0 */
  657. if (instance == 0x00)
  658. return 0;
  659. adv_instance = hci_find_adv_instance(hdev, instance);
  660. if (!adv_instance)
  661. return 0;
  662. /* TODO: Take into account the "appearance" and "local-name" flags here.
  663. * These are currently being ignored as they are not supported.
  664. */
  665. return adv_instance->scan_rsp_len;
  666. }
  667. void __hci_req_disable_advertising(struct hci_request *req)
  668. {
  669. u8 enable = 0x00;
  670. hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
  671. }
  672. static u32 get_adv_instance_flags(struct hci_dev *hdev, u8 instance)
  673. {
  674. u32 flags;
  675. struct adv_info *adv_instance;
  676. if (instance == 0x00) {
  677. /* Instance 0 always manages the "Tx Power" and "Flags"
  678. * fields
  679. */
  680. flags = MGMT_ADV_FLAG_TX_POWER | MGMT_ADV_FLAG_MANAGED_FLAGS;
  681. /* For instance 0, the HCI_ADVERTISING_CONNECTABLE setting
  682. * corresponds to the "connectable" instance flag.
  683. */
  684. if (hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE))
  685. flags |= MGMT_ADV_FLAG_CONNECTABLE;
  686. if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
  687. flags |= MGMT_ADV_FLAG_LIMITED_DISCOV;
  688. else if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
  689. flags |= MGMT_ADV_FLAG_DISCOV;
  690. return flags;
  691. }
  692. adv_instance = hci_find_adv_instance(hdev, instance);
  693. /* Return 0 when we got an invalid instance identifier. */
  694. if (!adv_instance)
  695. return 0;
  696. return adv_instance->flags;
  697. }
  698. static bool adv_use_rpa(struct hci_dev *hdev, uint32_t flags)
  699. {
  700. /* If privacy is not enabled don't use RPA */
  701. if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
  702. return false;
  703. /* If basic privacy mode is enabled use RPA */
  704. if (!hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
  705. return true;
  706. /* If limited privacy mode is enabled don't use RPA if we're
  707. * both discoverable and bondable.
  708. */
  709. if ((flags & MGMT_ADV_FLAG_DISCOV) &&
  710. hci_dev_test_flag(hdev, HCI_BONDABLE))
  711. return false;
  712. /* We're neither bondable nor discoverable in the limited
  713. * privacy mode, therefore use RPA.
  714. */
  715. return true;
  716. }
  717. void __hci_req_enable_advertising(struct hci_request *req)
  718. {
  719. struct hci_dev *hdev = req->hdev;
  720. struct hci_cp_le_set_adv_param cp;
  721. u8 own_addr_type, enable = 0x01;
  722. bool connectable;
  723. u32 flags;
  724. if (hci_conn_num(hdev, LE_LINK) > 0)
  725. return;
  726. if (hci_dev_test_flag(hdev, HCI_LE_ADV))
  727. __hci_req_disable_advertising(req);
  728. /* Clear the HCI_LE_ADV bit temporarily so that the
  729. * hci_update_random_address knows that it's safe to go ahead
  730. * and write a new random address. The flag will be set back on
  731. * as soon as the SET_ADV_ENABLE HCI command completes.
  732. */
  733. hci_dev_clear_flag(hdev, HCI_LE_ADV);
  734. flags = get_adv_instance_flags(hdev, hdev->cur_adv_instance);
  735. /* If the "connectable" instance flag was not set, then choose between
  736. * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
  737. */
  738. connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
  739. mgmt_get_connectable(hdev);
  740. /* Set require_privacy to true only when non-connectable
  741. * advertising is used. In that case it is fine to use a
  742. * non-resolvable private address.
  743. */
  744. if (hci_update_random_address(req, !connectable,
  745. adv_use_rpa(hdev, flags),
  746. &own_addr_type) < 0)
  747. return;
  748. memset(&cp, 0, sizeof(cp));
  749. cp.min_interval = cpu_to_le16(hdev->le_adv_min_interval);
  750. cp.max_interval = cpu_to_le16(hdev->le_adv_max_interval);
  751. if (connectable)
  752. cp.type = LE_ADV_IND;
  753. else if (get_cur_adv_instance_scan_rsp_len(hdev))
  754. cp.type = LE_ADV_SCAN_IND;
  755. else
  756. cp.type = LE_ADV_NONCONN_IND;
  757. cp.own_address_type = own_addr_type;
  758. cp.channel_map = hdev->le_adv_channel_map;
  759. hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp);
  760. hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
  761. }
  762. u8 append_local_name(struct hci_dev *hdev, u8 *ptr, u8 ad_len)
  763. {
  764. size_t short_len;
  765. size_t complete_len;
  766. /* no space left for name (+ NULL + type + len) */
  767. if ((HCI_MAX_AD_LENGTH - ad_len) < HCI_MAX_SHORT_NAME_LENGTH + 3)
  768. return ad_len;
  769. /* use complete name if present and fits */
  770. complete_len = strlen(hdev->dev_name);
  771. if (complete_len && complete_len <= HCI_MAX_SHORT_NAME_LENGTH)
  772. return eir_append_data(ptr, ad_len, EIR_NAME_COMPLETE,
  773. hdev->dev_name, complete_len + 1);
  774. /* use short name if present */
  775. short_len = strlen(hdev->short_name);
  776. if (short_len)
  777. return eir_append_data(ptr, ad_len, EIR_NAME_SHORT,
  778. hdev->short_name, short_len + 1);
  779. /* use shortened full name if present, we already know that name
  780. * is longer then HCI_MAX_SHORT_NAME_LENGTH
  781. */
  782. if (complete_len) {
  783. u8 name[HCI_MAX_SHORT_NAME_LENGTH + 1];
  784. memcpy(name, hdev->dev_name, HCI_MAX_SHORT_NAME_LENGTH);
  785. name[HCI_MAX_SHORT_NAME_LENGTH] = '\0';
  786. return eir_append_data(ptr, ad_len, EIR_NAME_SHORT, name,
  787. sizeof(name));
  788. }
  789. return ad_len;
  790. }
  791. static u8 append_appearance(struct hci_dev *hdev, u8 *ptr, u8 ad_len)
  792. {
  793. return eir_append_le16(ptr, ad_len, EIR_APPEARANCE, hdev->appearance);
  794. }
  795. static u8 create_default_scan_rsp_data(struct hci_dev *hdev, u8 *ptr)
  796. {
  797. u8 scan_rsp_len = 0;
  798. if (hdev->appearance) {
  799. scan_rsp_len = append_appearance(hdev, ptr, scan_rsp_len);
  800. }
  801. return append_local_name(hdev, ptr, scan_rsp_len);
  802. }
  803. static u8 create_instance_scan_rsp_data(struct hci_dev *hdev, u8 instance,
  804. u8 *ptr)
  805. {
  806. struct adv_info *adv_instance;
  807. u32 instance_flags;
  808. u8 scan_rsp_len = 0;
  809. adv_instance = hci_find_adv_instance(hdev, instance);
  810. if (!adv_instance)
  811. return 0;
  812. instance_flags = adv_instance->flags;
  813. if ((instance_flags & MGMT_ADV_FLAG_APPEARANCE) && hdev->appearance) {
  814. scan_rsp_len = append_appearance(hdev, ptr, scan_rsp_len);
  815. }
  816. memcpy(&ptr[scan_rsp_len], adv_instance->scan_rsp_data,
  817. adv_instance->scan_rsp_len);
  818. scan_rsp_len += adv_instance->scan_rsp_len;
  819. if (instance_flags & MGMT_ADV_FLAG_LOCAL_NAME)
  820. scan_rsp_len = append_local_name(hdev, ptr, scan_rsp_len);
  821. return scan_rsp_len;
  822. }
  823. void __hci_req_update_scan_rsp_data(struct hci_request *req, u8 instance)
  824. {
  825. struct hci_dev *hdev = req->hdev;
  826. struct hci_cp_le_set_scan_rsp_data cp;
  827. u8 len;
  828. if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
  829. return;
  830. memset(&cp, 0, sizeof(cp));
  831. if (instance)
  832. len = create_instance_scan_rsp_data(hdev, instance, cp.data);
  833. else
  834. len = create_default_scan_rsp_data(hdev, cp.data);
  835. if (hdev->scan_rsp_data_len == len &&
  836. !memcmp(cp.data, hdev->scan_rsp_data, len))
  837. return;
  838. memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
  839. hdev->scan_rsp_data_len = len;
  840. cp.length = len;
  841. hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp);
  842. }
  843. static u8 create_instance_adv_data(struct hci_dev *hdev, u8 instance, u8 *ptr)
  844. {
  845. struct adv_info *adv_instance = NULL;
  846. u8 ad_len = 0, flags = 0;
  847. u32 instance_flags;
  848. /* Return 0 when the current instance identifier is invalid. */
  849. if (instance) {
  850. adv_instance = hci_find_adv_instance(hdev, instance);
  851. if (!adv_instance)
  852. return 0;
  853. }
  854. instance_flags = get_adv_instance_flags(hdev, instance);
  855. /* The Add Advertising command allows userspace to set both the general
  856. * and limited discoverable flags.
  857. */
  858. if (instance_flags & MGMT_ADV_FLAG_DISCOV)
  859. flags |= LE_AD_GENERAL;
  860. if (instance_flags & MGMT_ADV_FLAG_LIMITED_DISCOV)
  861. flags |= LE_AD_LIMITED;
  862. if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
  863. flags |= LE_AD_NO_BREDR;
  864. if (flags || (instance_flags & MGMT_ADV_FLAG_MANAGED_FLAGS)) {
  865. /* If a discovery flag wasn't provided, simply use the global
  866. * settings.
  867. */
  868. if (!flags)
  869. flags |= mgmt_get_adv_discov_flags(hdev);
  870. /* If flags would still be empty, then there is no need to
  871. * include the "Flags" AD field".
  872. */
  873. if (flags) {
  874. ptr[0] = 0x02;
  875. ptr[1] = EIR_FLAGS;
  876. ptr[2] = flags;
  877. ad_len += 3;
  878. ptr += 3;
  879. }
  880. }
  881. if (adv_instance) {
  882. memcpy(ptr, adv_instance->adv_data,
  883. adv_instance->adv_data_len);
  884. ad_len += adv_instance->adv_data_len;
  885. ptr += adv_instance->adv_data_len;
  886. }
  887. /* Provide Tx Power only if we can provide a valid value for it */
  888. if (hdev->adv_tx_power != HCI_TX_POWER_INVALID &&
  889. (instance_flags & MGMT_ADV_FLAG_TX_POWER)) {
  890. ptr[0] = 0x02;
  891. ptr[1] = EIR_TX_POWER;
  892. ptr[2] = (u8)hdev->adv_tx_power;
  893. ad_len += 3;
  894. ptr += 3;
  895. }
  896. return ad_len;
  897. }
  898. void __hci_req_update_adv_data(struct hci_request *req, u8 instance)
  899. {
  900. struct hci_dev *hdev = req->hdev;
  901. struct hci_cp_le_set_adv_data cp;
  902. u8 len;
  903. if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
  904. return;
  905. memset(&cp, 0, sizeof(cp));
  906. len = create_instance_adv_data(hdev, instance, cp.data);
  907. /* There's nothing to do if the data hasn't changed */
  908. if (hdev->adv_data_len == len &&
  909. memcmp(cp.data, hdev->adv_data, len) == 0)
  910. return;
  911. memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
  912. hdev->adv_data_len = len;
  913. cp.length = len;
  914. hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp);
  915. }
  916. int hci_req_update_adv_data(struct hci_dev *hdev, u8 instance)
  917. {
  918. struct hci_request req;
  919. hci_req_init(&req, hdev);
  920. __hci_req_update_adv_data(&req, instance);
  921. return hci_req_run(&req, NULL);
  922. }
  923. static void adv_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
  924. {
  925. BT_DBG("%s status %u", hdev->name, status);
  926. }
  927. void hci_req_reenable_advertising(struct hci_dev *hdev)
  928. {
  929. struct hci_request req;
  930. if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
  931. list_empty(&hdev->adv_instances))
  932. return;
  933. hci_req_init(&req, hdev);
  934. if (hdev->cur_adv_instance) {
  935. __hci_req_schedule_adv_instance(&req, hdev->cur_adv_instance,
  936. true);
  937. } else {
  938. __hci_req_update_adv_data(&req, 0x00);
  939. __hci_req_update_scan_rsp_data(&req, 0x00);
  940. __hci_req_enable_advertising(&req);
  941. }
  942. hci_req_run(&req, adv_enable_complete);
  943. }
  944. static void adv_timeout_expire(struct work_struct *work)
  945. {
  946. struct hci_dev *hdev = container_of(work, struct hci_dev,
  947. adv_instance_expire.work);
  948. struct hci_request req;
  949. u8 instance;
  950. BT_DBG("%s", hdev->name);
  951. hci_dev_lock(hdev);
  952. hdev->adv_instance_timeout = 0;
  953. instance = hdev->cur_adv_instance;
  954. if (instance == 0x00)
  955. goto unlock;
  956. hci_req_init(&req, hdev);
  957. hci_req_clear_adv_instance(hdev, NULL, &req, instance, false);
  958. if (list_empty(&hdev->adv_instances))
  959. __hci_req_disable_advertising(&req);
  960. hci_req_run(&req, NULL);
  961. unlock:
  962. hci_dev_unlock(hdev);
  963. }
  964. int __hci_req_schedule_adv_instance(struct hci_request *req, u8 instance,
  965. bool force)
  966. {
  967. struct hci_dev *hdev = req->hdev;
  968. struct adv_info *adv_instance = NULL;
  969. u16 timeout;
  970. if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
  971. list_empty(&hdev->adv_instances))
  972. return -EPERM;
  973. if (hdev->adv_instance_timeout)
  974. return -EBUSY;
  975. adv_instance = hci_find_adv_instance(hdev, instance);
  976. if (!adv_instance)
  977. return -ENOENT;
  978. /* A zero timeout means unlimited advertising. As long as there is
  979. * only one instance, duration should be ignored. We still set a timeout
  980. * in case further instances are being added later on.
  981. *
  982. * If the remaining lifetime of the instance is more than the duration
  983. * then the timeout corresponds to the duration, otherwise it will be
  984. * reduced to the remaining instance lifetime.
  985. */
  986. if (adv_instance->timeout == 0 ||
  987. adv_instance->duration <= adv_instance->remaining_time)
  988. timeout = adv_instance->duration;
  989. else
  990. timeout = adv_instance->remaining_time;
  991. /* The remaining time is being reduced unless the instance is being
  992. * advertised without time limit.
  993. */
  994. if (adv_instance->timeout)
  995. adv_instance->remaining_time =
  996. adv_instance->remaining_time - timeout;
  997. hdev->adv_instance_timeout = timeout;
  998. queue_delayed_work(hdev->req_workqueue,
  999. &hdev->adv_instance_expire,
  1000. msecs_to_jiffies(timeout * 1000));
  1001. /* If we're just re-scheduling the same instance again then do not
  1002. * execute any HCI commands. This happens when a single instance is
  1003. * being advertised.
  1004. */
  1005. if (!force && hdev->cur_adv_instance == instance &&
  1006. hci_dev_test_flag(hdev, HCI_LE_ADV))
  1007. return 0;
  1008. hdev->cur_adv_instance = instance;
  1009. __hci_req_update_adv_data(req, instance);
  1010. __hci_req_update_scan_rsp_data(req, instance);
  1011. __hci_req_enable_advertising(req);
  1012. return 0;
  1013. }
  1014. static void cancel_adv_timeout(struct hci_dev *hdev)
  1015. {
  1016. if (hdev->adv_instance_timeout) {
  1017. hdev->adv_instance_timeout = 0;
  1018. cancel_delayed_work(&hdev->adv_instance_expire);
  1019. }
  1020. }
  1021. /* For a single instance:
  1022. * - force == true: The instance will be removed even when its remaining
  1023. * lifetime is not zero.
  1024. * - force == false: the instance will be deactivated but kept stored unless
  1025. * the remaining lifetime is zero.
  1026. *
  1027. * For instance == 0x00:
  1028. * - force == true: All instances will be removed regardless of their timeout
  1029. * setting.
  1030. * - force == false: Only instances that have a timeout will be removed.
  1031. */
  1032. void hci_req_clear_adv_instance(struct hci_dev *hdev, struct sock *sk,
  1033. struct hci_request *req, u8 instance,
  1034. bool force)
  1035. {
  1036. struct adv_info *adv_instance, *n, *next_instance = NULL;
  1037. int err;
  1038. u8 rem_inst;
  1039. /* Cancel any timeout concerning the removed instance(s). */
  1040. if (!instance || hdev->cur_adv_instance == instance)
  1041. cancel_adv_timeout(hdev);
  1042. /* Get the next instance to advertise BEFORE we remove
  1043. * the current one. This can be the same instance again
  1044. * if there is only one instance.
  1045. */
  1046. if (instance && hdev->cur_adv_instance == instance)
  1047. next_instance = hci_get_next_instance(hdev, instance);
  1048. if (instance == 0x00) {
  1049. list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances,
  1050. list) {
  1051. if (!(force || adv_instance->timeout))
  1052. continue;
  1053. rem_inst = adv_instance->instance;
  1054. err = hci_remove_adv_instance(hdev, rem_inst);
  1055. if (!err)
  1056. mgmt_advertising_removed(sk, hdev, rem_inst);
  1057. }
  1058. } else {
  1059. adv_instance = hci_find_adv_instance(hdev, instance);
  1060. if (force || (adv_instance && adv_instance->timeout &&
  1061. !adv_instance->remaining_time)) {
  1062. /* Don't advertise a removed instance. */
  1063. if (next_instance &&
  1064. next_instance->instance == instance)
  1065. next_instance = NULL;
  1066. err = hci_remove_adv_instance(hdev, instance);
  1067. if (!err)
  1068. mgmt_advertising_removed(sk, hdev, instance);
  1069. }
  1070. }
  1071. if (!req || !hdev_is_powered(hdev) ||
  1072. hci_dev_test_flag(hdev, HCI_ADVERTISING))
  1073. return;
  1074. if (next_instance)
  1075. __hci_req_schedule_adv_instance(req, next_instance->instance,
  1076. false);
  1077. }
  1078. static void set_random_addr(struct hci_request *req, bdaddr_t *rpa)
  1079. {
  1080. struct hci_dev *hdev = req->hdev;
  1081. /* If we're advertising or initiating an LE connection we can't
  1082. * go ahead and change the random address at this time. This is
  1083. * because the eventual initiator address used for the
  1084. * subsequently created connection will be undefined (some
  1085. * controllers use the new address and others the one we had
  1086. * when the operation started).
  1087. *
  1088. * In this kind of scenario skip the update and let the random
  1089. * address be updated at the next cycle.
  1090. */
  1091. if (hci_dev_test_flag(hdev, HCI_LE_ADV) ||
  1092. hci_lookup_le_connect(hdev)) {
  1093. BT_DBG("Deferring random address update");
  1094. hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
  1095. return;
  1096. }
  1097. hci_req_add(req, HCI_OP_LE_SET_RANDOM_ADDR, 6, rpa);
  1098. }
  1099. int hci_update_random_address(struct hci_request *req, bool require_privacy,
  1100. bool use_rpa, u8 *own_addr_type)
  1101. {
  1102. struct hci_dev *hdev = req->hdev;
  1103. int err;
  1104. /* If privacy is enabled use a resolvable private address. If
  1105. * current RPA has expired or there is something else than
  1106. * the current RPA in use, then generate a new one.
  1107. */
  1108. if (use_rpa) {
  1109. int to;
  1110. *own_addr_type = ADDR_LE_DEV_RANDOM;
  1111. if (!hci_dev_test_and_clear_flag(hdev, HCI_RPA_EXPIRED) &&
  1112. !bacmp(&hdev->random_addr, &hdev->rpa))
  1113. return 0;
  1114. err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
  1115. if (err < 0) {
  1116. BT_ERR("%s failed to generate new RPA", hdev->name);
  1117. return err;
  1118. }
  1119. set_random_addr(req, &hdev->rpa);
  1120. to = msecs_to_jiffies(hdev->rpa_timeout * 1000);
  1121. queue_delayed_work(hdev->workqueue, &hdev->rpa_expired, to);
  1122. return 0;
  1123. }
  1124. /* In case of required privacy without resolvable private address,
  1125. * use an non-resolvable private address. This is useful for active
  1126. * scanning and non-connectable advertising.
  1127. */
  1128. if (require_privacy) {
  1129. bdaddr_t nrpa;
  1130. while (true) {
  1131. /* The non-resolvable private address is generated
  1132. * from random six bytes with the two most significant
  1133. * bits cleared.
  1134. */
  1135. get_random_bytes(&nrpa, 6);
  1136. nrpa.b[5] &= 0x3f;
  1137. /* The non-resolvable private address shall not be
  1138. * equal to the public address.
  1139. */
  1140. if (bacmp(&hdev->bdaddr, &nrpa))
  1141. break;
  1142. }
  1143. *own_addr_type = ADDR_LE_DEV_RANDOM;
  1144. set_random_addr(req, &nrpa);
  1145. return 0;
  1146. }
  1147. /* If forcing static address is in use or there is no public
  1148. * address use the static address as random address (but skip
  1149. * the HCI command if the current random address is already the
  1150. * static one.
  1151. *
  1152. * In case BR/EDR has been disabled on a dual-mode controller
  1153. * and a static address has been configured, then use that
  1154. * address instead of the public BR/EDR address.
  1155. */
  1156. if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
  1157. !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
  1158. (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
  1159. bacmp(&hdev->static_addr, BDADDR_ANY))) {
  1160. *own_addr_type = ADDR_LE_DEV_RANDOM;
  1161. if (bacmp(&hdev->static_addr, &hdev->random_addr))
  1162. hci_req_add(req, HCI_OP_LE_SET_RANDOM_ADDR, 6,
  1163. &hdev->static_addr);
  1164. return 0;
  1165. }
  1166. /* Neither privacy nor static address is being used so use a
  1167. * public address.
  1168. */
  1169. *own_addr_type = ADDR_LE_DEV_PUBLIC;
  1170. return 0;
  1171. }
  1172. static bool disconnected_whitelist_entries(struct hci_dev *hdev)
  1173. {
  1174. struct bdaddr_list *b;
  1175. list_for_each_entry(b, &hdev->whitelist, list) {
  1176. struct hci_conn *conn;
  1177. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &b->bdaddr);
  1178. if (!conn)
  1179. return true;
  1180. if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
  1181. return true;
  1182. }
  1183. return false;
  1184. }
  1185. void __hci_req_update_scan(struct hci_request *req)
  1186. {
  1187. struct hci_dev *hdev = req->hdev;
  1188. u8 scan;
  1189. if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
  1190. return;
  1191. if (!hdev_is_powered(hdev))
  1192. return;
  1193. if (mgmt_powering_down(hdev))
  1194. return;
  1195. if (hci_dev_test_flag(hdev, HCI_CONNECTABLE) ||
  1196. disconnected_whitelist_entries(hdev))
  1197. scan = SCAN_PAGE;
  1198. else
  1199. scan = SCAN_DISABLED;
  1200. if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
  1201. scan |= SCAN_INQUIRY;
  1202. if (test_bit(HCI_PSCAN, &hdev->flags) == !!(scan & SCAN_PAGE) &&
  1203. test_bit(HCI_ISCAN, &hdev->flags) == !!(scan & SCAN_INQUIRY))
  1204. return;
  1205. hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
  1206. }
  1207. static int update_scan(struct hci_request *req, unsigned long opt)
  1208. {
  1209. hci_dev_lock(req->hdev);
  1210. __hci_req_update_scan(req);
  1211. hci_dev_unlock(req->hdev);
  1212. return 0;
  1213. }
  1214. static void scan_update_work(struct work_struct *work)
  1215. {
  1216. struct hci_dev *hdev = container_of(work, struct hci_dev, scan_update);
  1217. hci_req_sync(hdev, update_scan, 0, HCI_CMD_TIMEOUT, NULL);
  1218. }
  1219. static int connectable_update(struct hci_request *req, unsigned long opt)
  1220. {
  1221. struct hci_dev *hdev = req->hdev;
  1222. hci_dev_lock(hdev);
  1223. __hci_req_update_scan(req);
  1224. /* If BR/EDR is not enabled and we disable advertising as a
  1225. * by-product of disabling connectable, we need to update the
  1226. * advertising flags.
  1227. */
  1228. if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
  1229. __hci_req_update_adv_data(req, hdev->cur_adv_instance);
  1230. /* Update the advertising parameters if necessary */
  1231. if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
  1232. !list_empty(&hdev->adv_instances))
  1233. __hci_req_enable_advertising(req);
  1234. __hci_update_background_scan(req);
  1235. hci_dev_unlock(hdev);
  1236. return 0;
  1237. }
  1238. static void connectable_update_work(struct work_struct *work)
  1239. {
  1240. struct hci_dev *hdev = container_of(work, struct hci_dev,
  1241. connectable_update);
  1242. u8 status;
  1243. hci_req_sync(hdev, connectable_update, 0, HCI_CMD_TIMEOUT, &status);
  1244. mgmt_set_connectable_complete(hdev, status);
  1245. }
  1246. static u8 get_service_classes(struct hci_dev *hdev)
  1247. {
  1248. struct bt_uuid *uuid;
  1249. u8 val = 0;
  1250. list_for_each_entry(uuid, &hdev->uuids, list)
  1251. val |= uuid->svc_hint;
  1252. return val;
  1253. }
  1254. void __hci_req_update_class(struct hci_request *req)
  1255. {
  1256. struct hci_dev *hdev = req->hdev;
  1257. u8 cod[3];
  1258. BT_DBG("%s", hdev->name);
  1259. if (!hdev_is_powered(hdev))
  1260. return;
  1261. if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
  1262. return;
  1263. if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
  1264. return;
  1265. cod[0] = hdev->minor_class;
  1266. cod[1] = hdev->major_class;
  1267. cod[2] = get_service_classes(hdev);
  1268. if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
  1269. cod[1] |= 0x20;
  1270. if (memcmp(cod, hdev->dev_class, 3) == 0)
  1271. return;
  1272. hci_req_add(req, HCI_OP_WRITE_CLASS_OF_DEV, sizeof(cod), cod);
  1273. }
  1274. static void write_iac(struct hci_request *req)
  1275. {
  1276. struct hci_dev *hdev = req->hdev;
  1277. struct hci_cp_write_current_iac_lap cp;
  1278. if (!hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
  1279. return;
  1280. if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE)) {
  1281. /* Limited discoverable mode */
  1282. cp.num_iac = min_t(u8, hdev->num_iac, 2);
  1283. cp.iac_lap[0] = 0x00; /* LIAC */
  1284. cp.iac_lap[1] = 0x8b;
  1285. cp.iac_lap[2] = 0x9e;
  1286. cp.iac_lap[3] = 0x33; /* GIAC */
  1287. cp.iac_lap[4] = 0x8b;
  1288. cp.iac_lap[5] = 0x9e;
  1289. } else {
  1290. /* General discoverable mode */
  1291. cp.num_iac = 1;
  1292. cp.iac_lap[0] = 0x33; /* GIAC */
  1293. cp.iac_lap[1] = 0x8b;
  1294. cp.iac_lap[2] = 0x9e;
  1295. }
  1296. hci_req_add(req, HCI_OP_WRITE_CURRENT_IAC_LAP,
  1297. (cp.num_iac * 3) + 1, &cp);
  1298. }
  1299. static int discoverable_update(struct hci_request *req, unsigned long opt)
  1300. {
  1301. struct hci_dev *hdev = req->hdev;
  1302. hci_dev_lock(hdev);
  1303. if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
  1304. write_iac(req);
  1305. __hci_req_update_scan(req);
  1306. __hci_req_update_class(req);
  1307. }
  1308. /* Advertising instances don't use the global discoverable setting, so
  1309. * only update AD if advertising was enabled using Set Advertising.
  1310. */
  1311. if (hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
  1312. __hci_req_update_adv_data(req, 0x00);
  1313. /* Discoverable mode affects the local advertising
  1314. * address in limited privacy mode.
  1315. */
  1316. if (hci_dev_test_flag(hdev, HCI_LIMITED_PRIVACY))
  1317. __hci_req_enable_advertising(req);
  1318. }
  1319. hci_dev_unlock(hdev);
  1320. return 0;
  1321. }
  1322. static void discoverable_update_work(struct work_struct *work)
  1323. {
  1324. struct hci_dev *hdev = container_of(work, struct hci_dev,
  1325. discoverable_update);
  1326. u8 status;
  1327. hci_req_sync(hdev, discoverable_update, 0, HCI_CMD_TIMEOUT, &status);
  1328. mgmt_set_discoverable_complete(hdev, status);
  1329. }
  1330. void __hci_abort_conn(struct hci_request *req, struct hci_conn *conn,
  1331. u8 reason)
  1332. {
  1333. switch (conn->state) {
  1334. case BT_CONNECTED:
  1335. case BT_CONFIG:
  1336. if (conn->type == AMP_LINK) {
  1337. struct hci_cp_disconn_phy_link cp;
  1338. cp.phy_handle = HCI_PHY_HANDLE(conn->handle);
  1339. cp.reason = reason;
  1340. hci_req_add(req, HCI_OP_DISCONN_PHY_LINK, sizeof(cp),
  1341. &cp);
  1342. } else {
  1343. struct hci_cp_disconnect dc;
  1344. dc.handle = cpu_to_le16(conn->handle);
  1345. dc.reason = reason;
  1346. hci_req_add(req, HCI_OP_DISCONNECT, sizeof(dc), &dc);
  1347. }
  1348. conn->state = BT_DISCONN;
  1349. break;
  1350. case BT_CONNECT:
  1351. if (conn->type == LE_LINK) {
  1352. if (test_bit(HCI_CONN_SCANNING, &conn->flags))
  1353. break;
  1354. hci_req_add(req, HCI_OP_LE_CREATE_CONN_CANCEL,
  1355. 0, NULL);
  1356. } else if (conn->type == ACL_LINK) {
  1357. if (req->hdev->hci_ver < BLUETOOTH_VER_1_2)
  1358. break;
  1359. hci_req_add(req, HCI_OP_CREATE_CONN_CANCEL,
  1360. 6, &conn->dst);
  1361. }
  1362. break;
  1363. case BT_CONNECT2:
  1364. if (conn->type == ACL_LINK) {
  1365. struct hci_cp_reject_conn_req rej;
  1366. bacpy(&rej.bdaddr, &conn->dst);
  1367. rej.reason = reason;
  1368. hci_req_add(req, HCI_OP_REJECT_CONN_REQ,
  1369. sizeof(rej), &rej);
  1370. } else if (conn->type == SCO_LINK || conn->type == ESCO_LINK) {
  1371. struct hci_cp_reject_sync_conn_req rej;
  1372. bacpy(&rej.bdaddr, &conn->dst);
  1373. /* SCO rejection has its own limited set of
  1374. * allowed error values (0x0D-0x0F) which isn't
  1375. * compatible with most values passed to this
  1376. * function. To be safe hard-code one of the
  1377. * values that's suitable for SCO.
  1378. */
  1379. rej.reason = HCI_ERROR_REJ_LIMITED_RESOURCES;
  1380. hci_req_add(req, HCI_OP_REJECT_SYNC_CONN_REQ,
  1381. sizeof(rej), &rej);
  1382. }
  1383. break;
  1384. default:
  1385. conn->state = BT_CLOSED;
  1386. break;
  1387. }
  1388. }
  1389. static void abort_conn_complete(struct hci_dev *hdev, u8 status, u16 opcode)
  1390. {
  1391. if (status)
  1392. BT_DBG("Failed to abort connection: status 0x%2.2x", status);
  1393. }
  1394. int hci_abort_conn(struct hci_conn *conn, u8 reason)
  1395. {
  1396. struct hci_request req;
  1397. int err;
  1398. hci_req_init(&req, conn->hdev);
  1399. __hci_abort_conn(&req, conn, reason);
  1400. err = hci_req_run(&req, abort_conn_complete);
  1401. if (err && err != -ENODATA) {
  1402. BT_ERR("Failed to run HCI request: err %d", err);
  1403. return err;
  1404. }
  1405. return 0;
  1406. }
  1407. static int update_bg_scan(struct hci_request *req, unsigned long opt)
  1408. {
  1409. hci_dev_lock(req->hdev);
  1410. __hci_update_background_scan(req);
  1411. hci_dev_unlock(req->hdev);
  1412. return 0;
  1413. }
  1414. static void bg_scan_update(struct work_struct *work)
  1415. {
  1416. struct hci_dev *hdev = container_of(work, struct hci_dev,
  1417. bg_scan_update);
  1418. struct hci_conn *conn;
  1419. u8 status;
  1420. int err;
  1421. err = hci_req_sync(hdev, update_bg_scan, 0, HCI_CMD_TIMEOUT, &status);
  1422. if (!err)
  1423. return;
  1424. hci_dev_lock(hdev);
  1425. conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT);
  1426. if (conn)
  1427. hci_le_conn_failed(conn, status);
  1428. hci_dev_unlock(hdev);
  1429. }
  1430. static int le_scan_disable(struct hci_request *req, unsigned long opt)
  1431. {
  1432. hci_req_add_le_scan_disable(req);
  1433. return 0;
  1434. }
  1435. static int bredr_inquiry(struct hci_request *req, unsigned long opt)
  1436. {
  1437. u8 length = opt;
  1438. const u8 giac[3] = { 0x33, 0x8b, 0x9e };
  1439. const u8 liac[3] = { 0x00, 0x8b, 0x9e };
  1440. struct hci_cp_inquiry cp;
  1441. BT_DBG("%s", req->hdev->name);
  1442. hci_dev_lock(req->hdev);
  1443. hci_inquiry_cache_flush(req->hdev);
  1444. hci_dev_unlock(req->hdev);
  1445. memset(&cp, 0, sizeof(cp));
  1446. if (req->hdev->discovery.limited)
  1447. memcpy(&cp.lap, liac, sizeof(cp.lap));
  1448. else
  1449. memcpy(&cp.lap, giac, sizeof(cp.lap));
  1450. cp.length = length;
  1451. hci_req_add(req, HCI_OP_INQUIRY, sizeof(cp), &cp);
  1452. return 0;
  1453. }
  1454. static void le_scan_disable_work(struct work_struct *work)
  1455. {
  1456. struct hci_dev *hdev = container_of(work, struct hci_dev,
  1457. le_scan_disable.work);
  1458. u8 status;
  1459. BT_DBG("%s", hdev->name);
  1460. if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
  1461. return;
  1462. cancel_delayed_work(&hdev->le_scan_restart);
  1463. hci_req_sync(hdev, le_scan_disable, 0, HCI_CMD_TIMEOUT, &status);
  1464. if (status) {
  1465. BT_ERR("Failed to disable LE scan: status 0x%02x", status);
  1466. return;
  1467. }
  1468. hdev->discovery.scan_start = 0;
  1469. /* If we were running LE only scan, change discovery state. If
  1470. * we were running both LE and BR/EDR inquiry simultaneously,
  1471. * and BR/EDR inquiry is already finished, stop discovery,
  1472. * otherwise BR/EDR inquiry will stop discovery when finished.
  1473. * If we will resolve remote device name, do not change
  1474. * discovery state.
  1475. */
  1476. if (hdev->discovery.type == DISCOV_TYPE_LE)
  1477. goto discov_stopped;
  1478. if (hdev->discovery.type != DISCOV_TYPE_INTERLEAVED)
  1479. return;
  1480. if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks)) {
  1481. if (!test_bit(HCI_INQUIRY, &hdev->flags) &&
  1482. hdev->discovery.state != DISCOVERY_RESOLVING)
  1483. goto discov_stopped;
  1484. return;
  1485. }
  1486. hci_req_sync(hdev, bredr_inquiry, DISCOV_INTERLEAVED_INQUIRY_LEN,
  1487. HCI_CMD_TIMEOUT, &status);
  1488. if (status) {
  1489. BT_ERR("Inquiry failed: status 0x%02x", status);
  1490. goto discov_stopped;
  1491. }
  1492. return;
  1493. discov_stopped:
  1494. hci_dev_lock(hdev);
  1495. hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
  1496. hci_dev_unlock(hdev);
  1497. }
  1498. static int le_scan_restart(struct hci_request *req, unsigned long opt)
  1499. {
  1500. struct hci_dev *hdev = req->hdev;
  1501. struct hci_cp_le_set_scan_enable cp;
  1502. /* If controller is not scanning we are done. */
  1503. if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
  1504. return 0;
  1505. hci_req_add_le_scan_disable(req);
  1506. memset(&cp, 0, sizeof(cp));
  1507. cp.enable = LE_SCAN_ENABLE;
  1508. cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
  1509. hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp);
  1510. return 0;
  1511. }
  1512. static void le_scan_restart_work(struct work_struct *work)
  1513. {
  1514. struct hci_dev *hdev = container_of(work, struct hci_dev,
  1515. le_scan_restart.work);
  1516. unsigned long timeout, duration, scan_start, now;
  1517. u8 status;
  1518. BT_DBG("%s", hdev->name);
  1519. hci_req_sync(hdev, le_scan_restart, 0, HCI_CMD_TIMEOUT, &status);
  1520. if (status) {
  1521. BT_ERR("Failed to restart LE scan: status %d", status);
  1522. return;
  1523. }
  1524. hci_dev_lock(hdev);
  1525. if (!test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) ||
  1526. !hdev->discovery.scan_start)
  1527. goto unlock;
  1528. /* When the scan was started, hdev->le_scan_disable has been queued
  1529. * after duration from scan_start. During scan restart this job
  1530. * has been canceled, and we need to queue it again after proper
  1531. * timeout, to make sure that scan does not run indefinitely.
  1532. */
  1533. duration = hdev->discovery.scan_duration;
  1534. scan_start = hdev->discovery.scan_start;
  1535. now = jiffies;
  1536. if (now - scan_start <= duration) {
  1537. int elapsed;
  1538. if (now >= scan_start)
  1539. elapsed = now - scan_start;
  1540. else
  1541. elapsed = ULONG_MAX - scan_start + now;
  1542. timeout = duration - elapsed;
  1543. } else {
  1544. timeout = 0;
  1545. }
  1546. queue_delayed_work(hdev->req_workqueue,
  1547. &hdev->le_scan_disable, timeout);
  1548. unlock:
  1549. hci_dev_unlock(hdev);
  1550. }
  1551. static void disable_advertising(struct hci_request *req)
  1552. {
  1553. u8 enable = 0x00;
  1554. hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
  1555. }
  1556. static int active_scan(struct hci_request *req, unsigned long opt)
  1557. {
  1558. uint16_t interval = opt;
  1559. struct hci_dev *hdev = req->hdev;
  1560. struct hci_cp_le_set_scan_param param_cp;
  1561. struct hci_cp_le_set_scan_enable enable_cp;
  1562. u8 own_addr_type;
  1563. int err;
  1564. BT_DBG("%s", hdev->name);
  1565. if (hci_dev_test_flag(hdev, HCI_LE_ADV)) {
  1566. hci_dev_lock(hdev);
  1567. /* Don't let discovery abort an outgoing connection attempt
  1568. * that's using directed advertising.
  1569. */
  1570. if (hci_lookup_le_connect(hdev)) {
  1571. hci_dev_unlock(hdev);
  1572. return -EBUSY;
  1573. }
  1574. cancel_adv_timeout(hdev);
  1575. hci_dev_unlock(hdev);
  1576. disable_advertising(req);
  1577. }
  1578. /* If controller is scanning, it means the background scanning is
  1579. * running. Thus, we should temporarily stop it in order to set the
  1580. * discovery scanning parameters.
  1581. */
  1582. if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
  1583. hci_req_add_le_scan_disable(req);
  1584. /* All active scans will be done with either a resolvable private
  1585. * address (when privacy feature has been enabled) or non-resolvable
  1586. * private address.
  1587. */
  1588. err = hci_update_random_address(req, true, scan_use_rpa(hdev),
  1589. &own_addr_type);
  1590. if (err < 0)
  1591. own_addr_type = ADDR_LE_DEV_PUBLIC;
  1592. memset(&param_cp, 0, sizeof(param_cp));
  1593. param_cp.type = LE_SCAN_ACTIVE;
  1594. param_cp.interval = cpu_to_le16(interval);
  1595. param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN);
  1596. param_cp.own_address_type = own_addr_type;
  1597. hci_req_add(req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp),
  1598. &param_cp);
  1599. memset(&enable_cp, 0, sizeof(enable_cp));
  1600. enable_cp.enable = LE_SCAN_ENABLE;
  1601. enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
  1602. hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp),
  1603. &enable_cp);
  1604. return 0;
  1605. }
  1606. static int interleaved_discov(struct hci_request *req, unsigned long opt)
  1607. {
  1608. int err;
  1609. BT_DBG("%s", req->hdev->name);
  1610. err = active_scan(req, opt);
  1611. if (err)
  1612. return err;
  1613. return bredr_inquiry(req, DISCOV_BREDR_INQUIRY_LEN);
  1614. }
  1615. static void start_discovery(struct hci_dev *hdev, u8 *status)
  1616. {
  1617. unsigned long timeout;
  1618. BT_DBG("%s type %u", hdev->name, hdev->discovery.type);
  1619. switch (hdev->discovery.type) {
  1620. case DISCOV_TYPE_BREDR:
  1621. if (!hci_dev_test_flag(hdev, HCI_INQUIRY))
  1622. hci_req_sync(hdev, bredr_inquiry,
  1623. DISCOV_BREDR_INQUIRY_LEN, HCI_CMD_TIMEOUT,
  1624. status);
  1625. return;
  1626. case DISCOV_TYPE_INTERLEAVED:
  1627. /* When running simultaneous discovery, the LE scanning time
  1628. * should occupy the whole discovery time sine BR/EDR inquiry
  1629. * and LE scanning are scheduled by the controller.
  1630. *
  1631. * For interleaving discovery in comparison, BR/EDR inquiry
  1632. * and LE scanning are done sequentially with separate
  1633. * timeouts.
  1634. */
  1635. if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY,
  1636. &hdev->quirks)) {
  1637. timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
  1638. /* During simultaneous discovery, we double LE scan
  1639. * interval. We must leave some time for the controller
  1640. * to do BR/EDR inquiry.
  1641. */
  1642. hci_req_sync(hdev, interleaved_discov,
  1643. DISCOV_LE_SCAN_INT * 2, HCI_CMD_TIMEOUT,
  1644. status);
  1645. break;
  1646. }
  1647. timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout);
  1648. hci_req_sync(hdev, active_scan, DISCOV_LE_SCAN_INT,
  1649. HCI_CMD_TIMEOUT, status);
  1650. break;
  1651. case DISCOV_TYPE_LE:
  1652. timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
  1653. hci_req_sync(hdev, active_scan, DISCOV_LE_SCAN_INT,
  1654. HCI_CMD_TIMEOUT, status);
  1655. break;
  1656. default:
  1657. *status = HCI_ERROR_UNSPECIFIED;
  1658. return;
  1659. }
  1660. if (*status)
  1661. return;
  1662. BT_DBG("%s timeout %u ms", hdev->name, jiffies_to_msecs(timeout));
  1663. /* When service discovery is used and the controller has a
  1664. * strict duplicate filter, it is important to remember the
  1665. * start and duration of the scan. This is required for
  1666. * restarting scanning during the discovery phase.
  1667. */
  1668. if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks) &&
  1669. hdev->discovery.result_filtering) {
  1670. hdev->discovery.scan_start = jiffies;
  1671. hdev->discovery.scan_duration = timeout;
  1672. }
  1673. queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_disable,
  1674. timeout);
  1675. }
  1676. bool hci_req_stop_discovery(struct hci_request *req)
  1677. {
  1678. struct hci_dev *hdev = req->hdev;
  1679. struct discovery_state *d = &hdev->discovery;
  1680. struct hci_cp_remote_name_req_cancel cp;
  1681. struct inquiry_entry *e;
  1682. bool ret = false;
  1683. BT_DBG("%s state %u", hdev->name, hdev->discovery.state);
  1684. if (d->state == DISCOVERY_FINDING || d->state == DISCOVERY_STOPPING) {
  1685. if (test_bit(HCI_INQUIRY, &hdev->flags))
  1686. hci_req_add(req, HCI_OP_INQUIRY_CANCEL, 0, NULL);
  1687. if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
  1688. cancel_delayed_work(&hdev->le_scan_disable);
  1689. hci_req_add_le_scan_disable(req);
  1690. }
  1691. ret = true;
  1692. } else {
  1693. /* Passive scanning */
  1694. if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
  1695. hci_req_add_le_scan_disable(req);
  1696. ret = true;
  1697. }
  1698. }
  1699. /* No further actions needed for LE-only discovery */
  1700. if (d->type == DISCOV_TYPE_LE)
  1701. return ret;
  1702. if (d->state == DISCOVERY_RESOLVING || d->state == DISCOVERY_STOPPING) {
  1703. e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY,
  1704. NAME_PENDING);
  1705. if (!e)
  1706. return ret;
  1707. bacpy(&cp.bdaddr, &e->data.bdaddr);
  1708. hci_req_add(req, HCI_OP_REMOTE_NAME_REQ_CANCEL, sizeof(cp),
  1709. &cp);
  1710. ret = true;
  1711. }
  1712. return ret;
  1713. }
  1714. static int stop_discovery(struct hci_request *req, unsigned long opt)
  1715. {
  1716. hci_dev_lock(req->hdev);
  1717. hci_req_stop_discovery(req);
  1718. hci_dev_unlock(req->hdev);
  1719. return 0;
  1720. }
  1721. static void discov_update(struct work_struct *work)
  1722. {
  1723. struct hci_dev *hdev = container_of(work, struct hci_dev,
  1724. discov_update);
  1725. u8 status = 0;
  1726. switch (hdev->discovery.state) {
  1727. case DISCOVERY_STARTING:
  1728. start_discovery(hdev, &status);
  1729. mgmt_start_discovery_complete(hdev, status);
  1730. if (status)
  1731. hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
  1732. else
  1733. hci_discovery_set_state(hdev, DISCOVERY_FINDING);
  1734. break;
  1735. case DISCOVERY_STOPPING:
  1736. hci_req_sync(hdev, stop_discovery, 0, HCI_CMD_TIMEOUT, &status);
  1737. mgmt_stop_discovery_complete(hdev, status);
  1738. if (!status)
  1739. hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
  1740. break;
  1741. case DISCOVERY_STOPPED:
  1742. default:
  1743. return;
  1744. }
  1745. }
  1746. static void discov_off(struct work_struct *work)
  1747. {
  1748. struct hci_dev *hdev = container_of(work, struct hci_dev,
  1749. discov_off.work);
  1750. BT_DBG("%s", hdev->name);
  1751. hci_dev_lock(hdev);
  1752. /* When discoverable timeout triggers, then just make sure
  1753. * the limited discoverable flag is cleared. Even in the case
  1754. * of a timeout triggered from general discoverable, it is
  1755. * safe to unconditionally clear the flag.
  1756. */
  1757. hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
  1758. hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
  1759. hdev->discov_timeout = 0;
  1760. hci_dev_unlock(hdev);
  1761. hci_req_sync(hdev, discoverable_update, 0, HCI_CMD_TIMEOUT, NULL);
  1762. mgmt_new_settings(hdev);
  1763. }
  1764. static int powered_update_hci(struct hci_request *req, unsigned long opt)
  1765. {
  1766. struct hci_dev *hdev = req->hdev;
  1767. u8 link_sec;
  1768. hci_dev_lock(hdev);
  1769. if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED) &&
  1770. !lmp_host_ssp_capable(hdev)) {
  1771. u8 mode = 0x01;
  1772. hci_req_add(req, HCI_OP_WRITE_SSP_MODE, sizeof(mode), &mode);
  1773. if (bredr_sc_enabled(hdev) && !lmp_host_sc_capable(hdev)) {
  1774. u8 support = 0x01;
  1775. hci_req_add(req, HCI_OP_WRITE_SC_SUPPORT,
  1776. sizeof(support), &support);
  1777. }
  1778. }
  1779. if (hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
  1780. lmp_bredr_capable(hdev)) {
  1781. struct hci_cp_write_le_host_supported cp;
  1782. cp.le = 0x01;
  1783. cp.simul = 0x00;
  1784. /* Check first if we already have the right
  1785. * host state (host features set)
  1786. */
  1787. if (cp.le != lmp_host_le_capable(hdev) ||
  1788. cp.simul != lmp_host_le_br_capable(hdev))
  1789. hci_req_add(req, HCI_OP_WRITE_LE_HOST_SUPPORTED,
  1790. sizeof(cp), &cp);
  1791. }
  1792. if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
  1793. /* Make sure the controller has a good default for
  1794. * advertising data. This also applies to the case
  1795. * where BR/EDR was toggled during the AUTO_OFF phase.
  1796. */
  1797. if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
  1798. list_empty(&hdev->adv_instances)) {
  1799. __hci_req_update_adv_data(req, 0x00);
  1800. __hci_req_update_scan_rsp_data(req, 0x00);
  1801. if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
  1802. __hci_req_enable_advertising(req);
  1803. } else if (!list_empty(&hdev->adv_instances)) {
  1804. struct adv_info *adv_instance;
  1805. adv_instance = list_first_entry(&hdev->adv_instances,
  1806. struct adv_info, list);
  1807. __hci_req_schedule_adv_instance(req,
  1808. adv_instance->instance,
  1809. true);
  1810. }
  1811. }
  1812. link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY);
  1813. if (link_sec != test_bit(HCI_AUTH, &hdev->flags))
  1814. hci_req_add(req, HCI_OP_WRITE_AUTH_ENABLE,
  1815. sizeof(link_sec), &link_sec);
  1816. if (lmp_bredr_capable(hdev)) {
  1817. if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
  1818. __hci_req_write_fast_connectable(req, true);
  1819. else
  1820. __hci_req_write_fast_connectable(req, false);
  1821. __hci_req_update_scan(req);
  1822. __hci_req_update_class(req);
  1823. __hci_req_update_name(req);
  1824. __hci_req_update_eir(req);
  1825. }
  1826. hci_dev_unlock(hdev);
  1827. return 0;
  1828. }
  1829. int __hci_req_hci_power_on(struct hci_dev *hdev)
  1830. {
  1831. /* Register the available SMP channels (BR/EDR and LE) only when
  1832. * successfully powering on the controller. This late
  1833. * registration is required so that LE SMP can clearly decide if
  1834. * the public address or static address is used.
  1835. */
  1836. smp_register(hdev);
  1837. return __hci_req_sync(hdev, powered_update_hci, 0, HCI_CMD_TIMEOUT,
  1838. NULL);
  1839. }
  1840. void hci_request_setup(struct hci_dev *hdev)
  1841. {
  1842. INIT_WORK(&hdev->discov_update, discov_update);
  1843. INIT_WORK(&hdev->bg_scan_update, bg_scan_update);
  1844. INIT_WORK(&hdev->scan_update, scan_update_work);
  1845. INIT_WORK(&hdev->connectable_update, connectable_update_work);
  1846. INIT_WORK(&hdev->discoverable_update, discoverable_update_work);
  1847. INIT_DELAYED_WORK(&hdev->discov_off, discov_off);
  1848. INIT_DELAYED_WORK(&hdev->le_scan_disable, le_scan_disable_work);
  1849. INIT_DELAYED_WORK(&hdev->le_scan_restart, le_scan_restart_work);
  1850. INIT_DELAYED_WORK(&hdev->adv_instance_expire, adv_timeout_expire);
  1851. }
  1852. void hci_request_cancel_all(struct hci_dev *hdev)
  1853. {
  1854. hci_req_sync_cancel(hdev, ENODEV);
  1855. cancel_work_sync(&hdev->discov_update);
  1856. cancel_work_sync(&hdev->bg_scan_update);
  1857. cancel_work_sync(&hdev->scan_update);
  1858. cancel_work_sync(&hdev->connectable_update);
  1859. cancel_work_sync(&hdev->discoverable_update);
  1860. cancel_delayed_work_sync(&hdev->discov_off);
  1861. cancel_delayed_work_sync(&hdev->le_scan_disable);
  1862. cancel_delayed_work_sync(&hdev->le_scan_restart);
  1863. if (hdev->adv_instance_timeout) {
  1864. cancel_delayed_work_sync(&hdev->adv_instance_expire);
  1865. hdev->adv_instance_timeout = 0;
  1866. }
  1867. }