hci_request.c 60 KB

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