[v3] usb: typec: tcpm: Export partner Source Capabilities
diff mbox series

Message ID 20210214111730.1436506-1-kyletso@google.com
State New, archived
Headers show
Series
  • [v3] usb: typec: tcpm: Export partner Source Capabilities
Related show

Commit Message

Kyle Tso Feb. 14, 2021, 11:17 a.m. UTC
Export a function for other drivers to get the partner Source
Capabilities.

Signed-off-by: Kyle Tso <kyletso@google.com>
---
Changes since v1:
- add a put function to free the memory

Changes since v2:
- bring back the function tcpm_copy_pdos

 drivers/usb/typec/tcpm/tcpm.c | 47 +++++++++++++++++++++++++++++++++++
 include/linux/usb/tcpm.h      |  2 ++
 2 files changed, 49 insertions(+)

Comments

Dan Carpenter Feb. 15, 2021, 1:37 p.m. UTC | #1
Hi Kyle,

url:    https://github.com/0day-ci/linux/commits/Kyle-Tso/usb-typec-tcpm-Export-partner-Source-Capabilities/20210214-192125
base:   https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb.git usb-testing
config: i386-randconfig-m021-20210214 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-15) 9.3.0

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>
Reported-by: Dan Carpenter <dan.carpenter@oracle.com>

smatch warnings:
drivers/usb/typec/tcpm/tcpm.c:5770 tcpm_get_partner_src_caps() warn: variable dereferenced before check 'src_pdo' (see line 5769)

vim +/src_pdo +5770 drivers/usb/typec/tcpm/tcpm.c

bfd31a180a3d55 Kyle Tso 2021-02-14  5759  int tcpm_get_partner_src_caps(struct tcpm_port *port, u32 **src_pdo)
bfd31a180a3d55 Kyle Tso 2021-02-14  5760  {
bfd31a180a3d55 Kyle Tso 2021-02-14  5761  	unsigned int nr_pdo;
bfd31a180a3d55 Kyle Tso 2021-02-14  5762  
bfd31a180a3d55 Kyle Tso 2021-02-14  5763  	mutex_lock(&port->lock);
bfd31a180a3d55 Kyle Tso 2021-02-14  5764  	if (port->nr_source_caps == 0) {
bfd31a180a3d55 Kyle Tso 2021-02-14  5765  		mutex_unlock(&port->lock);
bfd31a180a3d55 Kyle Tso 2021-02-14  5766  		return -ENODATA;
bfd31a180a3d55 Kyle Tso 2021-02-14  5767  	}
bfd31a180a3d55 Kyle Tso 2021-02-14  5768  
bfd31a180a3d55 Kyle Tso 2021-02-14 @5769  	*src_pdo = kcalloc(port->nr_source_caps, sizeof(u32), GFP_KERNEL);
bfd31a180a3d55 Kyle Tso 2021-02-14 @5770  	if (!src_pdo) {

Typo.  Missing * char.

bfd31a180a3d55 Kyle Tso 2021-02-14  5771  		mutex_unlock(&port->lock);
bfd31a180a3d55 Kyle Tso 2021-02-14  5772  		return -ENOMEM;
bfd31a180a3d55 Kyle Tso 2021-02-14  5773  	}
bfd31a180a3d55 Kyle Tso 2021-02-14  5774  
bfd31a180a3d55 Kyle Tso 2021-02-14  5775  	nr_pdo = tcpm_copy_pdos(*src_pdo, port->source_caps,
bfd31a180a3d55 Kyle Tso 2021-02-14  5776  				port->nr_source_caps);
bfd31a180a3d55 Kyle Tso 2021-02-14  5777  	mutex_unlock(&port->lock);
bfd31a180a3d55 Kyle Tso 2021-02-14  5778  	return nr_pdo;
bfd31a180a3d55 Kyle Tso 2021-02-14  5779  }

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
gregkh@linuxfoundation.org March 2, 2021, 7:12 a.m. UTC | #2
On Sun, Feb 14, 2021 at 07:17:30PM +0800, Kyle Tso wrote:
> Export a function for other drivers to get the partner Source
> Capabilities.

What driver?  We need a user of new functions in the kernel, otherwise
they are ripe for removal.

thanks,

greg k-h

Patch
diff mbox series

diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
index be0b6469dd3d..1679c75e5a3d 100644
--- a/drivers/usb/typec/tcpm/tcpm.c
+++ b/drivers/usb/typec/tcpm/tcpm.c
@@ -5739,6 +5739,53 @@  static int tcpm_fw_get_caps(struct tcpm_port *port,
 	return 0;
 }
 
+static int tcpm_copy_pdos(u32 *dest_pdo, const u32 *src_pdo, unsigned int nr_pdo)
+{
+	unsigned int i;
+
+	if (nr_pdo > PDO_MAX_OBJECTS)
+		nr_pdo = PDO_MAX_OBJECTS;
+
+	for (i = 0; i < nr_pdo; i++)
+		dest_pdo[i] = src_pdo[i];
+
+	return nr_pdo;
+}
+
+/*
+ * Don't call this function in interrupt context. Caller needs to free the
+ * memory by calling tcpm_put_partner_src_caps.
+ */
+int tcpm_get_partner_src_caps(struct tcpm_port *port, u32 **src_pdo)
+{
+	unsigned int nr_pdo;
+
+	mutex_lock(&port->lock);
+	if (port->nr_source_caps == 0) {
+		mutex_unlock(&port->lock);
+		return -ENODATA;
+	}
+
+	*src_pdo = kcalloc(port->nr_source_caps, sizeof(u32), GFP_KERNEL);
+	if (!src_pdo) {
+		mutex_unlock(&port->lock);
+		return -ENOMEM;
+	}
+
+	nr_pdo = tcpm_copy_pdos(*src_pdo, port->source_caps,
+				port->nr_source_caps);
+	mutex_unlock(&port->lock);
+	return nr_pdo;
+}
+EXPORT_SYMBOL_GPL(tcpm_get_partner_src_caps);
+
+void tcpm_put_partner_src_caps(u32 **src_pdo)
+{
+	kfree(*src_pdo);
+	*src_pdo = NULL;
+}
+EXPORT_SYMBOL_GPL(tcpm_put_partner_src_caps);
+
 /* Power Supply access to expose source power information */
 enum tcpm_psy_online_states {
 	TCPM_PSY_OFFLINE = 0,
diff --git a/include/linux/usb/tcpm.h b/include/linux/usb/tcpm.h
index 42fcfbe10590..f83d9ff89a13 100644
--- a/include/linux/usb/tcpm.h
+++ b/include/linux/usb/tcpm.h
@@ -161,5 +161,7 @@  void tcpm_pd_transmit_complete(struct tcpm_port *port,
 			       enum tcpm_transmit_status status);
 void tcpm_pd_hard_reset(struct tcpm_port *port);
 void tcpm_tcpc_reset(struct tcpm_port *port);
+int tcpm_get_partner_src_caps(struct tcpm_port *port, u32 **pdo);
+void tcpm_put_partner_src_caps(u32 **pdo);
 
 #endif /* __LINUX_USB_TCPM_H */