2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2010 Nokia Corporation
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation;
9 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
10 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
12 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
13 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
19 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
20 SOFTWARE IS DISCLAIMED.
23 /* Bluetooth HCI Management interface */
25 #include <asm/uaccess.h>
26 #include <asm/unaligned.h>
28 #include <net/bluetooth/bluetooth.h>
29 #include <net/bluetooth/hci_core.h>
30 #include <net/bluetooth/mgmt.h>
32 #define MGMT_VERSION 0
33 #define MGMT_REVISION 1
36 struct list_head list;
45 static int cmd_status(struct sock *sk, u16 cmd, u8 status)
49 struct mgmt_ev_cmd_status *ev;
51 BT_DBG("sock %p", sk);
53 skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_ATOMIC);
57 hdr = (void *) skb_put(skb, sizeof(*hdr));
59 hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS);
60 hdr->len = cpu_to_le16(sizeof(*ev));
62 ev = (void *) skb_put(skb, sizeof(*ev));
64 put_unaligned_le16(cmd, &ev->opcode);
66 if (sock_queue_rcv_skb(sk, skb) < 0)
72 static int cmd_complete(struct sock *sk, u16 cmd, void *rp, size_t rp_len)
76 struct mgmt_ev_cmd_complete *ev;
78 BT_DBG("sock %p", sk);
80 skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_ATOMIC);
84 hdr = (void *) skb_put(skb, sizeof(*hdr));
86 hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE);
87 hdr->len = cpu_to_le16(sizeof(*ev) + rp_len);
89 ev = (void *) skb_put(skb, sizeof(*ev) + rp_len);
90 put_unaligned_le16(cmd, &ev->opcode);
91 memcpy(ev->data, rp, rp_len);
93 if (sock_queue_rcv_skb(sk, skb) < 0)
99 static int read_version(struct sock *sk)
101 struct mgmt_rp_read_version rp;
103 BT_DBG("sock %p", sk);
105 rp.version = MGMT_VERSION;
106 put_unaligned_le16(MGMT_REVISION, &rp.revision);
108 return cmd_complete(sk, MGMT_OP_READ_VERSION, &rp, sizeof(rp));
111 static int read_index_list(struct sock *sk)
113 struct mgmt_rp_read_index_list *rp;
119 BT_DBG("sock %p", sk);
121 read_lock(&hci_dev_list_lock);
124 list_for_each(p, &hci_dev_list) {
128 rp_len = sizeof(*rp) + (2 * count);
129 rp = kmalloc(rp_len, GFP_ATOMIC);
131 read_unlock(&hci_dev_list_lock);
135 put_unaligned_le16(count, &rp->num_controllers);
138 list_for_each(p, &hci_dev_list) {
139 struct hci_dev *d = list_entry(p, struct hci_dev, list);
141 hci_del_off_timer(d);
143 set_bit(HCI_MGMT, &d->flags);
145 if (test_bit(HCI_SETUP, &d->flags))
148 put_unaligned_le16(d->id, &rp->index[i++]);
149 BT_DBG("Added hci%u", d->id);
152 read_unlock(&hci_dev_list_lock);
154 err = cmd_complete(sk, MGMT_OP_READ_INDEX_LIST, rp, rp_len);
161 static int read_controller_info(struct sock *sk, unsigned char *data, u16 len)
163 struct mgmt_rp_read_info rp;
164 struct mgmt_cp_read_info *cp = (void *) data;
165 struct hci_dev *hdev;
168 BT_DBG("sock %p", sk);
171 return cmd_status(sk, MGMT_OP_READ_INFO, EINVAL);
173 dev_id = get_unaligned_le16(&cp->index);
175 BT_DBG("request for hci%u", dev_id);
177 hdev = hci_dev_get(dev_id);
179 return cmd_status(sk, MGMT_OP_READ_INFO, ENODEV);
181 hci_del_off_timer(hdev);
183 hci_dev_lock_bh(hdev);
185 set_bit(HCI_MGMT, &hdev->flags);
187 put_unaligned_le16(hdev->id, &rp.index);
188 rp.type = hdev->dev_type;
190 rp.powered = test_bit(HCI_UP, &hdev->flags);
191 rp.connectable = test_bit(HCI_PSCAN, &hdev->flags);
192 rp.discoverable = test_bit(HCI_ISCAN, &hdev->flags);
193 rp.pairable = test_bit(HCI_PSCAN, &hdev->flags);
195 if (test_bit(HCI_AUTH, &hdev->flags))
197 else if (hdev->ssp_mode > 0)
202 bacpy(&rp.bdaddr, &hdev->bdaddr);
203 memcpy(rp.features, hdev->features, 8);
204 memcpy(rp.dev_class, hdev->dev_class, 3);
205 put_unaligned_le16(hdev->manufacturer, &rp.manufacturer);
206 rp.hci_ver = hdev->hci_ver;
207 put_unaligned_le16(hdev->hci_rev, &rp.hci_rev);
209 hci_dev_unlock_bh(hdev);
212 return cmd_complete(sk, MGMT_OP_READ_INFO, &rp, sizeof(rp));
215 static void mgmt_pending_free(struct pending_cmd *cmd)
222 static int mgmt_pending_add(struct sock *sk, u16 opcode, int index,
225 struct pending_cmd *cmd;
227 cmd = kmalloc(sizeof(*cmd), GFP_ATOMIC);
231 cmd->opcode = opcode;
234 cmd->cmd = kmalloc(len, GFP_ATOMIC);
240 memcpy(cmd->cmd, data, len);
245 list_add(&cmd->list, &cmd_list);
250 static void mgmt_pending_foreach(u16 opcode, int index,
251 void (*cb)(struct pending_cmd *cmd, void *data),
254 struct list_head *p, *n;
256 list_for_each_safe(p, n, &cmd_list) {
257 struct pending_cmd *cmd;
259 cmd = list_entry(p, struct pending_cmd, list);
261 if (cmd->opcode != opcode)
264 if (index >= 0 && cmd->index != index)
271 static struct pending_cmd *mgmt_pending_find(u16 opcode, int index)
275 list_for_each(p, &cmd_list) {
276 struct pending_cmd *cmd;
278 cmd = list_entry(p, struct pending_cmd, list);
280 if (cmd->opcode != opcode)
283 if (index >= 0 && cmd->index != index)
292 static void mgmt_pending_remove(u16 opcode, int index)
294 struct pending_cmd *cmd;
296 cmd = mgmt_pending_find(opcode, index);
300 list_del(&cmd->list);
301 mgmt_pending_free(cmd);
304 static int set_powered(struct sock *sk, unsigned char *data, u16 len)
306 struct mgmt_mode *cp;
307 struct hci_dev *hdev;
312 dev_id = get_unaligned_le16(&cp->index);
314 BT_DBG("request for hci%u", dev_id);
316 hdev = hci_dev_get(dev_id);
318 return cmd_status(sk, MGMT_OP_SET_POWERED, ENODEV);
320 hci_dev_lock_bh(hdev);
322 up = test_bit(HCI_UP, &hdev->flags);
323 if ((cp->val && up) || (!cp->val && !up)) {
324 ret = cmd_status(sk, MGMT_OP_SET_POWERED, EALREADY);
328 if (mgmt_pending_find(MGMT_OP_SET_POWERED, dev_id)) {
329 ret = cmd_status(sk, MGMT_OP_SET_POWERED, EBUSY);
333 ret = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, dev_id, data, len);
338 queue_work(hdev->workqueue, &hdev->power_on);
340 queue_work(hdev->workqueue, &hdev->power_off);
345 hci_dev_unlock_bh(hdev);
350 static int set_discoverable(struct sock *sk, unsigned char *data, u16 len)
352 struct mgmt_mode *cp;
353 struct hci_dev *hdev;
359 dev_id = get_unaligned_le16(&cp->index);
361 BT_DBG("request for hci%u", dev_id);
363 hdev = hci_dev_get(dev_id);
365 return cmd_status(sk, MGMT_OP_SET_DISCOVERABLE, ENODEV);
367 hci_dev_lock_bh(hdev);
369 if (!test_bit(HCI_UP, &hdev->flags)) {
370 err = cmd_status(sk, MGMT_OP_SET_DISCOVERABLE, ENETDOWN);
374 if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, dev_id) ||
375 mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, dev_id)) {
376 err = cmd_status(sk, MGMT_OP_SET_DISCOVERABLE, EBUSY);
380 if (cp->val == test_bit(HCI_ISCAN, &hdev->flags) &&
381 test_bit(HCI_PSCAN, &hdev->flags)) {
382 err = cmd_status(sk, MGMT_OP_SET_DISCOVERABLE, EALREADY);
386 err = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, dev_id, data, len);
393 scan |= SCAN_INQUIRY;
395 err = hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
397 mgmt_pending_remove(MGMT_OP_SET_DISCOVERABLE, dev_id);
400 hci_dev_unlock_bh(hdev);
406 static int set_connectable(struct sock *sk, unsigned char *data, u16 len)
408 struct mgmt_mode *cp;
409 struct hci_dev *hdev;
415 dev_id = get_unaligned_le16(&cp->index);
417 BT_DBG("request for hci%u", dev_id);
419 hdev = hci_dev_get(dev_id);
421 return cmd_status(sk, MGMT_OP_SET_CONNECTABLE, ENODEV);
423 hci_dev_lock_bh(hdev);
425 if (!test_bit(HCI_UP, &hdev->flags)) {
426 err = cmd_status(sk, MGMT_OP_SET_CONNECTABLE, ENETDOWN);
430 if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, dev_id) ||
431 mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, dev_id)) {
432 err = cmd_status(sk, MGMT_OP_SET_CONNECTABLE, EBUSY);
436 if (cp->val == test_bit(HCI_PSCAN, &hdev->flags)) {
437 err = cmd_status(sk, MGMT_OP_SET_CONNECTABLE, EALREADY);
441 err = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, dev_id, data, len);
450 err = hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
452 mgmt_pending_remove(MGMT_OP_SET_CONNECTABLE, dev_id);
455 hci_dev_unlock_bh(hdev);
461 static int mgmt_event(u16 event, void *data, u16 data_len, struct sock *skip_sk)
464 struct mgmt_hdr *hdr;
466 skb = alloc_skb(sizeof(*hdr) + data_len, GFP_ATOMIC);
470 bt_cb(skb)->channel = HCI_CHANNEL_CONTROL;
472 hdr = (void *) skb_put(skb, sizeof(*hdr));
473 hdr->opcode = cpu_to_le16(event);
474 hdr->len = cpu_to_le16(data_len);
476 memcpy(skb_put(skb, data_len), data, data_len);
478 hci_send_to_sock(NULL, skb, skip_sk);
484 static int send_mode_rsp(struct sock *sk, u16 opcode, u16 index, u8 val)
488 put_unaligned_le16(index, &rp.index);
491 return cmd_complete(sk, opcode, &rp, sizeof(rp));
494 static int set_pairable(struct sock *sk, unsigned char *data, u16 len)
496 struct mgmt_mode *cp, ev;
497 struct hci_dev *hdev;
502 dev_id = get_unaligned_le16(&cp->index);
504 BT_DBG("request for hci%u", dev_id);
506 hdev = hci_dev_get(dev_id);
508 return cmd_status(sk, MGMT_OP_SET_PAIRABLE, ENODEV);
510 hci_dev_lock_bh(hdev);
513 set_bit(HCI_PAIRABLE, &hdev->flags);
515 clear_bit(HCI_PAIRABLE, &hdev->flags);
517 err = send_mode_rsp(sk, MGMT_OP_SET_PAIRABLE, dev_id, cp->val);
521 put_unaligned_le16(dev_id, &ev.index);
524 err = mgmt_event(MGMT_EV_PAIRABLE, &ev, sizeof(ev), sk);
527 hci_dev_unlock_bh(hdev);
533 static u8 get_service_classes(struct hci_dev *hdev)
538 list_for_each(p, &hdev->uuids) {
539 struct bt_uuid *uuid = list_entry(p, struct bt_uuid, list);
541 val |= uuid->svc_hint;
547 static int update_class(struct hci_dev *hdev)
551 BT_DBG("%s", hdev->name);
553 if (test_bit(HCI_SERVICE_CACHE, &hdev->flags))
556 cod[0] = hdev->minor_class;
557 cod[1] = hdev->major_class;
558 cod[2] = get_service_classes(hdev);
560 if (memcmp(cod, hdev->dev_class, 3) == 0)
563 return hci_send_cmd(hdev, HCI_OP_WRITE_CLASS_OF_DEV, sizeof(cod), cod);
566 static int add_uuid(struct sock *sk, unsigned char *data, u16 len)
568 struct mgmt_cp_add_uuid *cp;
569 struct hci_dev *hdev;
570 struct bt_uuid *uuid;
575 dev_id = get_unaligned_le16(&cp->index);
577 BT_DBG("request for hci%u", dev_id);
579 hdev = hci_dev_get(dev_id);
581 return cmd_status(sk, MGMT_OP_ADD_UUID, ENODEV);
583 hci_dev_lock_bh(hdev);
585 uuid = kmalloc(sizeof(*uuid), GFP_ATOMIC);
591 memcpy(uuid->uuid, cp->uuid, 16);
592 uuid->svc_hint = cp->svc_hint;
594 list_add(&uuid->list, &hdev->uuids);
596 err = update_class(hdev);
600 err = cmd_complete(sk, MGMT_OP_ADD_UUID, &dev_id, sizeof(dev_id));
603 hci_dev_unlock_bh(hdev);
609 static int remove_uuid(struct sock *sk, unsigned char *data, u16 len)
611 struct list_head *p, *n;
612 struct mgmt_cp_add_uuid *cp;
613 struct hci_dev *hdev;
614 u8 bt_uuid_any[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
619 dev_id = get_unaligned_le16(&cp->index);
621 BT_DBG("request for hci%u", dev_id);
623 hdev = hci_dev_get(dev_id);
625 return cmd_status(sk, MGMT_OP_REMOVE_UUID, ENODEV);
627 hci_dev_lock_bh(hdev);
629 if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) {
630 err = hci_uuids_clear(hdev);
636 list_for_each_safe(p, n, &hdev->uuids) {
637 struct bt_uuid *match = list_entry(p, struct bt_uuid, list);
639 if (memcmp(match->uuid, cp->uuid, 16) != 0)
642 list_del(&match->list);
647 err = cmd_status(sk, MGMT_OP_REMOVE_UUID, ENOENT);
651 err = update_class(hdev);
655 err = cmd_complete(sk, MGMT_OP_REMOVE_UUID, &dev_id, sizeof(dev_id));
658 hci_dev_unlock_bh(hdev);
664 static int set_dev_class(struct sock *sk, unsigned char *data, u16 len)
666 struct hci_dev *hdev;
667 struct mgmt_cp_set_dev_class *cp;
672 dev_id = get_unaligned_le16(&cp->index);
674 BT_DBG("request for hci%u", dev_id);
676 hdev = hci_dev_get(dev_id);
678 return cmd_status(sk, MGMT_OP_SET_DEV_CLASS, ENODEV);
680 hci_dev_lock_bh(hdev);
682 hdev->major_class = cp->major;
683 hdev->minor_class = cp->minor;
685 err = update_class(hdev);
688 err = cmd_complete(sk, MGMT_OP_SET_DEV_CLASS, &dev_id,
691 hci_dev_unlock_bh(hdev);
697 static int set_service_cache(struct sock *sk, unsigned char *data, u16 len)
699 struct hci_dev *hdev;
700 struct mgmt_cp_set_service_cache *cp;
705 dev_id = get_unaligned_le16(&cp->index);
707 hdev = hci_dev_get(dev_id);
709 return cmd_status(sk, MGMT_OP_SET_SERVICE_CACHE, ENODEV);
711 hci_dev_lock_bh(hdev);
713 BT_DBG("hci%u enable %d", dev_id, cp->enable);
716 set_bit(HCI_SERVICE_CACHE, &hdev->flags);
719 clear_bit(HCI_SERVICE_CACHE, &hdev->flags);
720 err = update_class(hdev);
724 err = cmd_complete(sk, MGMT_OP_SET_SERVICE_CACHE, &dev_id,
727 hci_dev_unlock_bh(hdev);
733 static int load_keys(struct sock *sk, unsigned char *data, u16 len)
735 struct hci_dev *hdev;
736 struct mgmt_cp_load_keys *cp;
737 u16 dev_id, key_count, expected_len;
741 dev_id = get_unaligned_le16(&cp->index);
742 key_count = get_unaligned_le16(&cp->key_count);
744 expected_len = sizeof(*cp) + key_count * sizeof(struct mgmt_key_info);
745 if (expected_len != len) {
746 BT_ERR("load_keys: expected %u bytes, got %u bytes",
751 hdev = hci_dev_get(dev_id);
753 return cmd_status(sk, MGMT_OP_LOAD_KEYS, ENODEV);
755 BT_DBG("hci%u debug_keys %u key_count %u", dev_id, cp->debug_keys,
758 hci_dev_lock_bh(hdev);
760 hci_link_keys_clear(hdev);
762 set_bit(HCI_LINK_KEYS, &hdev->flags);
765 set_bit(HCI_DEBUG_KEYS, &hdev->flags);
767 clear_bit(HCI_DEBUG_KEYS, &hdev->flags);
769 for (i = 0; i < key_count; i++) {
770 struct mgmt_key_info *key = &cp->keys[i];
772 hci_add_link_key(hdev, 0, &key->bdaddr, key->val, key->type,
776 hci_dev_unlock_bh(hdev);
782 static int remove_key(struct sock *sk, unsigned char *data, u16 len)
784 struct hci_dev *hdev;
785 struct mgmt_cp_remove_key *cp;
786 struct hci_conn *conn;
791 dev_id = get_unaligned_le16(&cp->index);
793 hdev = hci_dev_get(dev_id);
795 return cmd_status(sk, MGMT_OP_REMOVE_KEY, ENODEV);
797 hci_dev_lock_bh(hdev);
799 err = hci_remove_link_key(hdev, &cp->bdaddr);
801 err = cmd_status(sk, MGMT_OP_REMOVE_KEY, -err);
807 if (!test_bit(HCI_UP, &hdev->flags) || !cp->disconnect)
810 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
812 struct hci_cp_disconnect dc;
814 put_unaligned_le16(conn->handle, &dc.handle);
815 dc.reason = 0x13; /* Remote User Terminated Connection */
816 err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, 0, NULL);
820 hci_dev_unlock_bh(hdev);
826 static int disconnect(struct sock *sk, unsigned char *data, u16 len)
828 struct hci_dev *hdev;
829 struct mgmt_cp_disconnect *cp;
830 struct hci_cp_disconnect dc;
831 struct hci_conn *conn;
838 dev_id = get_unaligned_le16(&cp->index);
840 hdev = hci_dev_get(dev_id);
842 return cmd_status(sk, MGMT_OP_DISCONNECT, ENODEV);
844 hci_dev_lock_bh(hdev);
846 if (!test_bit(HCI_UP, &hdev->flags)) {
847 err = cmd_status(sk, MGMT_OP_DISCONNECT, ENETDOWN);
851 if (mgmt_pending_find(MGMT_OP_DISCONNECT, dev_id)) {
852 err = cmd_status(sk, MGMT_OP_DISCONNECT, EBUSY);
856 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
858 err = cmd_status(sk, MGMT_OP_DISCONNECT, ENOTCONN);
862 err = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, dev_id, data, len);
866 put_unaligned_le16(conn->handle, &dc.handle);
867 dc.reason = 0x13; /* Remote User Terminated Connection */
869 err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc);
871 mgmt_pending_remove(MGMT_OP_DISCONNECT, dev_id);
874 hci_dev_unlock_bh(hdev);
880 static int get_connections(struct sock *sk, unsigned char *data, u16 len)
882 struct mgmt_cp_get_connections *cp;
883 struct mgmt_rp_get_connections *rp;
884 struct hci_dev *hdev;
893 dev_id = get_unaligned_le16(&cp->index);
895 hdev = hci_dev_get(dev_id);
897 return cmd_status(sk, MGMT_OP_GET_CONNECTIONS, ENODEV);
899 hci_dev_lock_bh(hdev);
902 list_for_each(p, &hdev->conn_hash.list) {
906 rp_len = sizeof(*rp) + (count * sizeof(bdaddr_t));
907 rp = kmalloc(rp_len, GFP_ATOMIC);
913 put_unaligned_le16(dev_id, &rp->index);
914 put_unaligned_le16(count, &rp->conn_count);
916 read_lock(&hci_dev_list_lock);
919 list_for_each(p, &hdev->conn_hash.list) {
920 struct hci_conn *c = list_entry(p, struct hci_conn, list);
922 bacpy(&rp->conn[i++], &c->dst);
925 read_unlock(&hci_dev_list_lock);
927 err = cmd_complete(sk, MGMT_OP_GET_CONNECTIONS, rp, rp_len);
931 hci_dev_unlock_bh(hdev);
936 static int pin_code_reply(struct sock *sk, unsigned char *data, u16 len)
938 struct hci_dev *hdev;
939 struct mgmt_cp_pin_code_reply *cp;
940 struct hci_cp_pin_code_reply reply;
947 dev_id = get_unaligned_le16(&cp->index);
949 hdev = hci_dev_get(dev_id);
951 return cmd_status(sk, MGMT_OP_DISCONNECT, ENODEV);
953 hci_dev_lock_bh(hdev);
955 if (!test_bit(HCI_UP, &hdev->flags)) {
956 err = cmd_status(sk, MGMT_OP_PIN_CODE_REPLY, ENETDOWN);
960 err = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_REPLY, dev_id, data, len);
964 bacpy(&reply.bdaddr, &cp->bdaddr);
965 reply.pin_len = cp->pin_len;
966 memcpy(reply.pin_code, cp->pin_code, 16);
968 err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply);
970 mgmt_pending_remove(MGMT_OP_PIN_CODE_REPLY, dev_id);
973 hci_dev_unlock_bh(hdev);
979 static int pin_code_neg_reply(struct sock *sk, unsigned char *data, u16 len)
981 struct hci_dev *hdev;
982 struct mgmt_cp_pin_code_neg_reply *cp;
989 dev_id = get_unaligned_le16(&cp->index);
991 hdev = hci_dev_get(dev_id);
993 return cmd_status(sk, MGMT_OP_PIN_CODE_NEG_REPLY, ENODEV);
995 hci_dev_lock_bh(hdev);
997 if (!test_bit(HCI_UP, &hdev->flags)) {
998 err = cmd_status(sk, MGMT_OP_PIN_CODE_NEG_REPLY, ENETDOWN);
1002 err = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, dev_id,
1007 err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, sizeof(bdaddr_t),
1010 mgmt_pending_remove(MGMT_OP_PIN_CODE_NEG_REPLY, dev_id);
1013 hci_dev_unlock_bh(hdev);
1019 static int set_io_capability(struct sock *sk, unsigned char *data, u16 len)
1021 struct hci_dev *hdev;
1022 struct mgmt_cp_set_io_capability *cp;
1028 dev_id = get_unaligned_le16(&cp->index);
1030 hdev = hci_dev_get(dev_id);
1032 return cmd_status(sk, MGMT_OP_SET_IO_CAPABILITY, ENODEV);
1034 hci_dev_lock_bh(hdev);
1036 hdev->io_capability = cp->io_capability;
1038 BT_DBG("%s IO capability set to 0x%02x", hdev->name,
1039 hdev->io_capability);
1041 hci_dev_unlock_bh(hdev);
1044 return cmd_complete(sk, MGMT_OP_SET_IO_CAPABILITY,
1045 &dev_id, sizeof(dev_id));
1048 int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen)
1051 struct mgmt_hdr *hdr;
1055 BT_DBG("got %zu bytes", msglen);
1057 if (msglen < sizeof(*hdr))
1060 buf = kmalloc(msglen, GFP_ATOMIC);
1064 if (memcpy_fromiovec(buf, msg->msg_iov, msglen)) {
1069 hdr = (struct mgmt_hdr *) buf;
1070 opcode = get_unaligned_le16(&hdr->opcode);
1071 len = get_unaligned_le16(&hdr->len);
1073 if (len != msglen - sizeof(*hdr)) {
1079 case MGMT_OP_READ_VERSION:
1080 err = read_version(sk);
1082 case MGMT_OP_READ_INDEX_LIST:
1083 err = read_index_list(sk);
1085 case MGMT_OP_READ_INFO:
1086 err = read_controller_info(sk, buf + sizeof(*hdr), len);
1088 case MGMT_OP_SET_POWERED:
1089 err = set_powered(sk, buf + sizeof(*hdr), len);
1091 case MGMT_OP_SET_DISCOVERABLE:
1092 err = set_discoverable(sk, buf + sizeof(*hdr), len);
1094 case MGMT_OP_SET_CONNECTABLE:
1095 err = set_connectable(sk, buf + sizeof(*hdr), len);
1097 case MGMT_OP_SET_PAIRABLE:
1098 err = set_pairable(sk, buf + sizeof(*hdr), len);
1100 case MGMT_OP_ADD_UUID:
1101 err = add_uuid(sk, buf + sizeof(*hdr), len);
1103 case MGMT_OP_REMOVE_UUID:
1104 err = remove_uuid(sk, buf + sizeof(*hdr), len);
1106 case MGMT_OP_SET_DEV_CLASS:
1107 err = set_dev_class(sk, buf + sizeof(*hdr), len);
1109 case MGMT_OP_SET_SERVICE_CACHE:
1110 err = set_service_cache(sk, buf + sizeof(*hdr), len);
1112 case MGMT_OP_LOAD_KEYS:
1113 err = load_keys(sk, buf + sizeof(*hdr), len);
1115 case MGMT_OP_REMOVE_KEY:
1116 err = remove_key(sk, buf + sizeof(*hdr), len);
1118 case MGMT_OP_DISCONNECT:
1119 err = disconnect(sk, buf + sizeof(*hdr), len);
1121 case MGMT_OP_GET_CONNECTIONS:
1122 err = get_connections(sk, buf + sizeof(*hdr), len);
1124 case MGMT_OP_PIN_CODE_REPLY:
1125 err = pin_code_reply(sk, buf + sizeof(*hdr), len);
1127 case MGMT_OP_PIN_CODE_NEG_REPLY:
1128 err = pin_code_neg_reply(sk, buf + sizeof(*hdr), len);
1130 case MGMT_OP_SET_IO_CAPABILITY:
1131 err = set_io_capability(sk, buf + sizeof(*hdr), len);
1134 BT_DBG("Unknown op %u", opcode);
1135 err = cmd_status(sk, opcode, 0x01);
1149 int mgmt_index_added(u16 index)
1151 struct mgmt_ev_index_added ev;
1153 put_unaligned_le16(index, &ev.index);
1155 return mgmt_event(MGMT_EV_INDEX_ADDED, &ev, sizeof(ev), NULL);
1158 int mgmt_index_removed(u16 index)
1160 struct mgmt_ev_index_added ev;
1162 put_unaligned_le16(index, &ev.index);
1164 return mgmt_event(MGMT_EV_INDEX_REMOVED, &ev, sizeof(ev), NULL);
1172 static void mode_rsp(struct pending_cmd *cmd, void *data)
1174 struct mgmt_mode *cp = cmd->cmd;
1175 struct cmd_lookup *match = data;
1177 if (cp->val != match->val)
1180 send_mode_rsp(cmd->sk, cmd->opcode, cmd->index, cp->val);
1182 list_del(&cmd->list);
1184 if (match->sk == NULL) {
1185 match->sk = cmd->sk;
1186 sock_hold(match->sk);
1189 mgmt_pending_free(cmd);
1192 int mgmt_powered(u16 index, u8 powered)
1194 struct mgmt_mode ev;
1195 struct cmd_lookup match = { powered, NULL };
1198 mgmt_pending_foreach(MGMT_OP_SET_POWERED, index, mode_rsp, &match);
1200 put_unaligned_le16(index, &ev.index);
1203 ret = mgmt_event(MGMT_EV_POWERED, &ev, sizeof(ev), match.sk);
1211 int mgmt_discoverable(u16 index, u8 discoverable)
1213 struct mgmt_mode ev;
1214 struct cmd_lookup match = { discoverable, NULL };
1217 mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, index,
1220 put_unaligned_le16(index, &ev.index);
1221 ev.val = discoverable;
1223 ret = mgmt_event(MGMT_EV_DISCOVERABLE, &ev, sizeof(ev), match.sk);
1231 int mgmt_connectable(u16 index, u8 connectable)
1233 struct mgmt_mode ev;
1234 struct cmd_lookup match = { connectable, NULL };
1237 mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE, index, mode_rsp, &match);
1239 put_unaligned_le16(index, &ev.index);
1240 ev.val = connectable;
1242 ret = mgmt_event(MGMT_EV_CONNECTABLE, &ev, sizeof(ev), match.sk);
1250 int mgmt_new_key(u16 index, struct link_key *key, u8 old_key_type)
1252 struct mgmt_ev_new_key ev;
1254 memset(&ev, 0, sizeof(ev));
1256 put_unaligned_le16(index, &ev.index);
1258 bacpy(&ev.key.bdaddr, &key->bdaddr);
1259 ev.key.type = key->type;
1260 memcpy(ev.key.val, key->val, 16);
1261 ev.key.pin_len = key->pin_len;
1262 ev.old_key_type = old_key_type;
1264 return mgmt_event(MGMT_EV_NEW_KEY, &ev, sizeof(ev), NULL);
1267 int mgmt_connected(u16 index, bdaddr_t *bdaddr)
1269 struct mgmt_ev_connected ev;
1271 put_unaligned_le16(index, &ev.index);
1272 bacpy(&ev.bdaddr, bdaddr);
1274 return mgmt_event(MGMT_EV_CONNECTED, &ev, sizeof(ev), NULL);
1277 static void disconnect_rsp(struct pending_cmd *cmd, void *data)
1279 struct mgmt_cp_disconnect *cp = cmd->cmd;
1280 struct sock **sk = data;
1281 struct mgmt_rp_disconnect rp;
1283 put_unaligned_le16(cmd->index, &rp.index);
1284 bacpy(&rp.bdaddr, &cp->bdaddr);
1286 cmd_complete(cmd->sk, MGMT_OP_DISCONNECT, &rp, sizeof(rp));
1291 list_del(&cmd->list);
1292 mgmt_pending_free(cmd);
1295 int mgmt_disconnected(u16 index, bdaddr_t *bdaddr)
1297 struct mgmt_ev_disconnected ev;
1298 struct sock *sk = NULL;
1301 mgmt_pending_foreach(MGMT_OP_DISCONNECT, index, disconnect_rsp, &sk);
1303 put_unaligned_le16(index, &ev.index);
1304 bacpy(&ev.bdaddr, bdaddr);
1306 err = mgmt_event(MGMT_EV_DISCONNECTED, &ev, sizeof(ev), sk);
1314 int mgmt_disconnect_failed(u16 index)
1316 struct pending_cmd *cmd;
1319 cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, index);
1323 err = cmd_status(cmd->sk, MGMT_OP_DISCONNECT, EIO);
1325 list_del(&cmd->list);
1326 mgmt_pending_free(cmd);
1331 int mgmt_connect_failed(u16 index, bdaddr_t *bdaddr, u8 status)
1333 struct mgmt_ev_connect_failed ev;
1335 put_unaligned_le16(index, &ev.index);
1336 bacpy(&ev.bdaddr, bdaddr);
1339 return mgmt_event(MGMT_EV_CONNECT_FAILED, &ev, sizeof(ev), NULL);
1342 int mgmt_pin_code_request(u16 index, bdaddr_t *bdaddr)
1344 struct mgmt_ev_pin_code_request ev;
1346 put_unaligned_le16(index, &ev.index);
1347 bacpy(&ev.bdaddr, bdaddr);
1349 return mgmt_event(MGMT_EV_PIN_CODE_REQUEST, &ev, sizeof(ev), NULL);
1352 int mgmt_pin_code_reply_complete(u16 index, bdaddr_t *bdaddr, u8 status)
1354 struct pending_cmd *cmd;
1357 cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, index);
1362 err = cmd_status(cmd->sk, MGMT_OP_PIN_CODE_REPLY, status);
1364 err = cmd_complete(cmd->sk, MGMT_OP_PIN_CODE_REPLY,
1365 bdaddr, sizeof(*bdaddr));
1367 list_del(&cmd->list);
1368 mgmt_pending_free(cmd);
1373 int mgmt_pin_code_neg_reply_complete(u16 index, bdaddr_t *bdaddr, u8 status)
1375 struct pending_cmd *cmd;
1378 cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, index);
1383 err = cmd_status(cmd->sk, MGMT_OP_PIN_CODE_NEG_REPLY, status);
1385 err = cmd_complete(cmd->sk, MGMT_OP_PIN_CODE_NEG_REPLY,
1386 bdaddr, sizeof(*bdaddr));
1388 list_del(&cmd->list);
1389 mgmt_pending_free(cmd);