Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp474592pxy; Wed, 28 Apr 2021 07:57:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzJms3iEEqgBuZDBz4jYfqthefJ/IhCTxgh59atHVt9uXJFx4xh1mvV30m3bZgfkC9urUdg X-Received: by 2002:aa7:d0c9:: with SMTP id u9mr10127166edo.43.1619621825569; Wed, 28 Apr 2021 07:57:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619621825; cv=none; d=google.com; s=arc-20160816; b=z+tUIgnzzVQLrfmuno22UhQQBscRbbVb9xekSX5iqHgXkXpvxPK92RlLARSkCWTiS8 EtPdgRJtt3A1aX8zGtTzESUf8k7UniBcZSQcjio5WBEFqIdbn/8v3uWRDL3+vQf+Q+Pt rhMuF0H+21dwxC1o/h/Wm2CYJ0wzF6xRrt9znN/2C4a8usFwqn8F3ydetJm/CZ3Y3ZlW 8jT5xVbXF+YRKrGoe6p7Sh9QhT1ZqmDYHQvc+V3DA90y/KqnbXFp+Ygh76PPOGNJ3AI6 owSopeROGoNLejMrWX7Mp20YxbwIEzgjL+D3ZnUu5iox4Hjtl9yl64/DoUJMQx+XvjpP hU/w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from:ironport-sdr :ironport-sdr; bh=dhITX3ZJv+NE0mELIT7kpA3auvbrlW4cDijzpDzeOMI=; b=QeoAeqX3LObNi1rp8yEmCsJTR95nxdNtJR/qqpWYewaQjgW3wEPqaRynM1O2Bq+D3n 38nll4ikIHMHtTDcggdQtJJiU6sRsRMGfzgMGLezpRIAT6l008MjbjxusdCDmEkqj9eZ 4gQ09ajMvqlB8AL6MKBIQZJzs+hC8BhNdL6KxCUZYtU9eE3FGdZNAYlR1rjglaYXV632 lE2PvVl9OSfegLLd2cteFU0q1FB0cPDeoBHTZvOEEzhzfZjTUE+WG2+rAhW0GDdmLNyn A0axAXXPXjGYP9Ub6u6ZplOYXdmWsTG1od5DQwk7VY6P0Lroy1Nfmxzlt2ygFLMlt1sD 5YaA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r25si5962469edo.131.2021.04.28.07.56.26; Wed, 28 Apr 2021 07:57:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229965AbhD1N6a (ORCPT + 99 others); Wed, 28 Apr 2021 09:58:30 -0400 Received: from mga01.intel.com ([192.55.52.88]:27554 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239091AbhD1N6S (ORCPT ); Wed, 28 Apr 2021 09:58:18 -0400 IronPort-SDR: skdhMDWPoXpt9k828oTczOavwcmV4WewF3HGieOl8WLq8NiN7OXx9cC3OAeTFthT9c0oORrKY8 zjRo+16LZnQA== X-IronPort-AV: E=McAfee;i="6200,9189,9968"; a="217462958" X-IronPort-AV: E=Sophos;i="5.82,258,1613462400"; d="scan'208";a="217462958" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Apr 2021 06:57:33 -0700 IronPort-SDR: KojbuhkHWuwS4ySIVmYzM0xOBJ9oNNoDUAwhg6VP+ieS2uGT9xpY6xnkCiRJMEJlJJE6XrI3KT JQVakJlN4w5g== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.82,258,1613462400"; d="scan'208";a="423549400" Received: from intel-lenovo-legion-y540-15irh-pg0.iind.intel.com ([10.224.186.95]) by fmsmga008.fm.intel.com with ESMTP; 28 Apr 2021 06:57:32 -0700 From: Kiran K To: linux-bluetooth@vger.kernel.org Cc: Kiran K , Chethan T N , Srivatsa Ravishankar Subject: [PATCH v4 1/2] Bluetooth: enumerate local supported codec and cache details Date: Wed, 28 Apr 2021 19:31:31 +0530 Message-Id: <20210428140132.30316-1-kiran.k@intel.com> X-Mailer: git-send-email 2.17.1 Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org Move reading of supported local codecs into a separate init function, query codecs capabilities and cache the data Signed-off-by: Kiran K Signed-off-by: Chethan T N Signed-off-by: Srivatsa Ravishankar --- * changes in v4 - convert reading of codecs and codecs caps calls from async to sync * changes in v3 move codec enumeration into a new init function * changes in v2 add skb length check before accessing data include/net/bluetooth/hci.h | 18 ++++ include/net/bluetooth/hci_core.h | 23 +++++ net/bluetooth/hci_core.c | 144 ++++++++++++++++++++++++++++++- 3 files changed, 181 insertions(+), 4 deletions(-) diff --git a/include/net/bluetooth/hci.h b/include/net/bluetooth/hci.h index ea4ae551c426..2f7f8c6f7d63 100644 --- a/include/net/bluetooth/hci.h +++ b/include/net/bluetooth/hci.h @@ -1314,6 +1314,24 @@ struct hci_rp_read_local_pairing_opts { __u8 max_key_size; } __packed; +#define HCI_OP_READ_LOCAL_CODEC_CAPS 0x100e +struct hci_op_read_local_codec_caps { + __u8 codec_id[5]; + __u8 transport; + __u8 direction; +} __packed; + +struct hci_codec_caps { + __u8 len; + __u8 caps[]; +} __packed; + +struct hci_rp_read_local_codec_caps { + __u8 status; + __u8 num_caps; + struct hci_codec_caps caps[]; +} __packed; + #define HCI_OP_READ_PAGE_SCAN_ACTIVITY 0x0c1b struct hci_rp_read_page_scan_activity { __u8 status; diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h index 8f5f390363f5..b40c7ed38d18 100644 --- a/include/net/bluetooth/hci_core.h +++ b/include/net/bluetooth/hci_core.h @@ -131,6 +131,14 @@ struct bdaddr_list { u8 bdaddr_type; }; +struct codec_list { + struct list_head list; + u8 transport; + u8 codec_id[5]; + u8 num_caps; + struct hci_codec_caps caps[]; +}; + struct bdaddr_list_with_irk { struct list_head list; bdaddr_t bdaddr; @@ -534,6 +542,7 @@ struct hci_dev { struct list_head pend_le_conns; struct list_head pend_le_reports; struct list_head blocked_keys; + struct list_head local_codecs; struct hci_dev_stats stat; @@ -1843,9 +1852,23 @@ void hci_le_start_enc(struct hci_conn *conn, __le16 ediv, __le64 rand, void hci_copy_identity_address(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 *bdaddr_type); +int hci_codec_list_add(struct list_head *list, struct hci_rp_read_local_codec_caps *rp, + __u32 len, + struct hci_op_read_local_codec_caps *sent); +void hci_codec_list_clear(struct list_head *codec_list); #define SCO_AIRMODE_MASK 0x0003 #define SCO_AIRMODE_CVSD 0x0000 #define SCO_AIRMODE_TRANSP 0x0003 +#define LOCAL_CODEC_ACL_MASK BIT(0) +#define LOCAL_CODEC_SCO_MASK BIT(1) +#define LOCAL_CODEC_CIS_MASK BIT(2) +#define LOCAL_CODEC_BIS_MASK BIT(3) + +#define LOCAL_CODEC_ACL 0x00 +#define LOCAL_CODEC_SCO 0x01 +#define LOCAL_CODEC_CIS 0x02 +#define LOCAL_CODEC_BIS 0x03 + #endif /* __HCI_CORE_H */ diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c index fd12f1652bdf..7201967b6e9c 100644 --- a/net/bluetooth/hci_core.c +++ b/net/bluetooth/hci_core.c @@ -838,10 +838,6 @@ static int hci_init4_req(struct hci_request *req, unsigned long opt) if (hdev->commands[22] & 0x04) hci_set_event_mask_page_2(req); - /* Read local codec list if the HCI command is supported */ - if (hdev->commands[29] & 0x20) - hci_req_add(req, HCI_OP_READ_LOCAL_CODECS, 0, NULL); - /* Read local pairing options if the HCI command is supported */ if (hdev->commands[41] & 0x08) hci_req_add(req, HCI_OP_READ_LOCAL_PAIRING_OPTS, 0, NULL); @@ -907,6 +903,113 @@ static int hci_init4_req(struct hci_request *req, unsigned long opt) return 0; } +static void hci_read_codec_capabilities(struct hci_dev *hdev, __u8 *codec_id, + __u8 transport, bool is_vendor_codec) +{ + struct hci_op_read_local_codec_caps caps; + struct hci_rp_read_local_codec_caps *rp; + struct sk_buff *skb; + + memset(&caps, 0, sizeof(caps)); + + if (is_vendor_codec) { + caps.codec_id[0] = 0xFF; + memcpy(&caps.codec_id[1], codec_id, 4); + } else { + caps.codec_id[0] = codec_id[0]; + } + + caps.direction = 0x00; + caps.transport = transport; + + skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_CODEC_CAPS, sizeof(caps), + &caps, HCI_CMD_TIMEOUT); + if (IS_ERR(skb)) { + bt_dev_err(hdev, "Failed to read local supported codecs v2 (%ld)", + PTR_ERR(skb)); + return; + } + + if (skb->len < sizeof(*rp)) + goto error; + + rp = (void *)skb->data; + + if (rp->status) + goto error; + + hci_dev_lock(hdev); + hci_codec_list_add(&hdev->local_codecs, rp, skb->len - 2, &caps); + hci_dev_unlock(hdev); + +error: + kfree_skb(skb); +} + +static void hci_read_supported_codecs(struct hci_dev *hdev) +{ + struct sk_buff *skb; + __u8 num_codecs; + + skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_CODECS, 0, NULL, + HCI_CMD_TIMEOUT); + + if (IS_ERR(skb)) { + bt_dev_err(hdev, "Failed to read local supported codecs (%ld)", + PTR_ERR(skb)); + return; + } + + if (skb->len < 1 || skb->data[0]) + goto error; + + skb_pull(skb, 1); + + if (skb->len < 1) + goto error; + + /* enumerate standard codecs */ + num_codecs = skb->data[0]; + + skb_pull(skb, 1); + + if (num_codecs && skb->len < num_codecs) + goto error; + + while (num_codecs--) { + hci_read_codec_capabilities(hdev, skb->data, LOCAL_CODEC_ACL, + false); + skb_pull(skb, 1); + } + + /* enumerate vendor specific codecs */ + if (skb->len < 1) + goto error; + + num_codecs = skb->data[0]; + + skb_pull(skb, 1); + + if (num_codecs && skb->len < (num_codecs * 4)) + goto error; + + while (num_codecs--) { + hci_read_codec_capabilities(hdev, skb->data, LOCAL_CODEC_ACL, + true); + skb_pull(skb, 4); + } + +error: + kfree_skb(skb); +} + +static void hci_init5_req(struct hci_dev *hdev) +{ + /* Read local codec list if the HCI command is supported */ + if (hdev->commands[29] & 0x20) + hci_read_supported_codecs(hdev); +} + static int __hci_init(struct hci_dev *hdev) { int err; @@ -937,6 +1040,8 @@ static int __hci_init(struct hci_dev *hdev) if (err < 0) return err; + hci_init5_req(hdev); + /* This function is only called when the controller is actually in * configured state. When the controller is marked as unconfigured, * this initialization procedure is not run. @@ -3559,6 +3664,35 @@ void hci_conn_params_clear_disabled(struct hci_dev *hdev) BT_DBG("All LE disabled connection parameters were removed"); } +int hci_codec_list_add(struct list_head *list, struct hci_rp_read_local_codec_caps *rp, + __u32 len, + struct hci_op_read_local_codec_caps *sent) +{ + struct codec_list *entry; + + entry = kzalloc(sizeof(*entry) + len, GFP_KERNEL); + if (!entry) + return -ENOMEM; + + memcpy(entry->codec_id, sent->codec_id, 5); + entry->transport = sent->transport; + entry->num_caps = rp->num_caps; + if (rp->num_caps) + memcpy(entry->caps, rp->caps, len); + list_add(&entry->list, list); + + return 0; +} + +void hci_codec_list_clear(struct list_head *codec_list) +{ + struct codec_list *c, *n; + + list_for_each_entry_safe(c, n, codec_list, list) { + list_del(&c->list); + kfree(c); + } +} /* This function requires the caller holds hdev->lock */ static void hci_conn_params_clear_all(struct hci_dev *hdev) { @@ -3818,6 +3952,7 @@ struct hci_dev *hci_alloc_dev(void) INIT_LIST_HEAD(&hdev->conn_hash.list); INIT_LIST_HEAD(&hdev->adv_instances); INIT_LIST_HEAD(&hdev->blocked_keys); + INIT_LIST_HEAD(&hdev->local_codecs); INIT_WORK(&hdev->rx_work, hci_rx_work); INIT_WORK(&hdev->cmd_work, hci_cmd_work); @@ -4036,6 +4171,7 @@ void hci_unregister_dev(struct hci_dev *hdev) hci_conn_params_clear_all(hdev); hci_discovery_filter_clear(hdev); hci_blocked_keys_clear(hdev); + hci_codec_list_clear(&hdev->local_codecs); hci_dev_unlock(hdev); hci_dev_put(hdev); -- 2.17.1