hci_request.c 58 KB

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