From patchwork Wed Mar 15 14:15:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alvin_=C5=A0ipraga?= X-Patchwork-Id: 70254 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp2365783wrd; Wed, 15 Mar 2023 07:36:10 -0700 (PDT) X-Google-Smtp-Source: AK7set8KeVijQJXLhjVCDWVIyuHbN7RaVy0H+hFbISyn7m2RgLV6vrgjARj1dWxYk7EBlH234Xhe X-Received: by 2002:a17:90b:33d0:b0:237:97a1:b302 with SMTP id lk16-20020a17090b33d000b0023797a1b302mr50180pjb.5.1678890970016; Wed, 15 Mar 2023 07:36:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678890969; cv=none; d=google.com; s=arc-20160816; b=jUjAVWCLZm/1cI6rMnKKrANVKH/vU0zjZyGsC7lenucUcJXlNEOciJXJEn3BiFUb3Y YCJLtcqlYrC0pCmMGfKuxxstlKq+zqLvHQbok8mcDcqjIOZQDhEnuSucBDy1Tq8UeC8i 7vbfytnLZZ6e/QvWmQzqRglkVuHYIS339Zan9iV4V0nnbKKjL/rKCrU/UThmEKa8UJ37 bm62RkVBDEbpVsZjhnkaOFjy8YptcnGABVKeppadK4mhrAm9fLpywNlXjOB2draq+8E8 /xNx0HxXRiGC1dnswiSno5oBXzkNqY8ZzCPQhwBweN4tmVF817QkP/Bd+3RLXKvQF460 R4MQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=oFlsa6HtfdlDO+5W0pHOXKXz4uVBrG6uhwA+mTGh/F4=; b=CkdL9hulBrw+1jluAn31/+JAU2Qxj9t/h1N8CqPOCXNEB/3stYbiZl1/kK7WGVhL7D G+VgfuV/ZCnv7Q7Di0fn8xul/vINn3KjDyChft4XSqBxoxOuaOj0fRxpbt5nDxnPzBtv McSz8Hq0jco6YV9pw8oTXK8PELiSjJBQayJ2j4Bw1uug4dmFxSwoFWavczkH132Qf5gS R14EGXjM6pYLM0PmBkBtHRuVYtgEW20EW8eb4x69xo24AS1C4wxwmxQUbmBOQiqL7qsA Cq6i2DkeCAdOdicpWE1+CiMUuzNw4cvDd1O9YGHVd9jlWZ7MfYRACPYeCsFc3/tQsakp XEBA== ARC-Authentication-Results: i=1; mx.google.com; dkim=temperror (no key for signature) header.i=@pqrs.dk header.s=google header.b=UGMpDSGL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t5-20020a17090abc4500b0023d269ed307si1829066pjv.32.2023.03.15.07.35.56; Wed, 15 Mar 2023 07:36:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=temperror (no key for signature) header.i=@pqrs.dk header.s=google header.b=UGMpDSGL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231917AbjCOOQr (ORCPT + 99 others); Wed, 15 Mar 2023 10:16:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59518 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233065AbjCOOQa (ORCPT ); Wed, 15 Mar 2023 10:16:30 -0400 Received: from mail-ed1-x535.google.com (mail-ed1-x535.google.com [IPv6:2a00:1450:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 334DE3433D for ; Wed, 15 Mar 2023 07:16:01 -0700 (PDT) Received: by mail-ed1-x535.google.com with SMTP id eh3so20233241edb.11 for ; Wed, 15 Mar 2023 07:16:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pqrs.dk; s=google; t=1678889759; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=oFlsa6HtfdlDO+5W0pHOXKXz4uVBrG6uhwA+mTGh/F4=; b=UGMpDSGL8X5RFbzk+SEfLtR+9GJKEnTWGNIdPmBJGWII5fmtVJp57KaAbMkCgDbJb+ +8GCmf9p0z3p6oHvimw007lQGVU/hqtHOT1SAewubK0Ucm6lG0u2Eg+J6aal9KzsMyXv Lw6KTdWkHzGq95NRgsqVjI/+4RQWYPFFYUiWY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678889759; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=oFlsa6HtfdlDO+5W0pHOXKXz4uVBrG6uhwA+mTGh/F4=; b=J+hX1VAZ9RS8h2tjFxQIdTJyqsYKF3MU+epJ0kxObm8kCvjc12UJt4YjQ8a0Y/GgQ4 5aIF4OrMOztXUljwzDjdrvSwEUp0hqu1DSR2CYYFOAnEHdp64wFk/ijkRCqrJ2nena/f 8BgBAWpkGFykeIJrJsnypuAXQetHFqyV13cdvBYL3Rm2RneGvVZupGWS11jpgDXzNLbG Hya8LMXfiYxTgh+ox4tgRMNblAdgAEPVRsrX+aSusGzxdp2ldHNPpsWAZJ8RE5BXB8IL IpdH4NXZgfBTrd8CdzxXpRXGIQZyK/rOfBkLhJ8H2WUyf1/kqa/rfA0iMgTNg/zebtCo PCog== X-Gm-Message-State: AO0yUKWCuR/KUg6L6e/9i29p2mJ/jIZ/61PMtExuhS+9q4Rw80Fr7m65 caaQadW3UVFtiHLDJ2HywJFprw== X-Received: by 2002:a17:907:a804:b0:92a:edd4:638 with SMTP id vo4-20020a170907a80400b0092aedd40638mr2911718ejc.22.1678889758698; Wed, 15 Mar 2023 07:15:58 -0700 (PDT) Received: from localhost.localdomain (80.71.142.18.ipv4.parknet.dk. [80.71.142.18]) by smtp.gmail.com with ESMTPSA id m20-20020a509994000000b004aef147add6sm2532280edb.47.2023.03.15.07.15.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Mar 2023 07:15:58 -0700 (PDT) From: =?utf-8?q?Alvin_=C5=A0ipraga?= To: MyungJoo Ham , Chanwoo Choi , Marek Vasut , Heikki Krogerus , =?utf-8?q?Alvin_=C5=A0ip?= =?utf-8?q?raga?= Cc: linux-usb@vger.kernel.org, stable@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] extcon: usbc-tusb320: unregister typec port on driver removal Date: Wed, 15 Mar 2023 15:15:47 +0100 Message-Id: <20230315141547.825057-1-alvin@pqrs.dk> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1760444777428634469?= X-GMAIL-MSGID: =?utf-8?q?1760444777428634469?= From: Alvin Šipraga The driver can register a typec port if suitable firmware properties are present. But if the driver is removed through sysfs unbind, rmmod or similar, then it does not clean up after itself and the typec port device remains registered. This can be seen in sysfs, where stale typec ports get left over in /sys/class/typec. In order to fix this we have to add an i2c_driver remove function and call typec_unregister_port(), which is a no-op in the case where no typec port is created and the pointer remains NULL. In the process we should also put the fwnode_handle when the typec port isn't registered anymore, including if an error occurs during probe. The typec subsystem does not increase or decrease the reference counter for us, so we track it in the driver's private data. Note that the conditional check on TYPEC_PWR_MODE_PD was removed in the probe path because a call to tusb320_set_adv_pwr_mode() will perform an even more robust validation immediately after, hence there is no functional change here. Fixes: bf7571c00dca ("extcon: usbc-tusb320: Add USB TYPE-C support") Cc: stable@vger.kernel.org Signed-off-by: Alvin Šipraga --- v2: properly assign priv->connector_fwnode = connector; --- drivers/extcon/extcon-usbc-tusb320.c | 42 ++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/drivers/extcon/extcon-usbc-tusb320.c b/drivers/extcon/extcon-usbc-tusb320.c index b408ce989c22..10dff1c512c4 100644 --- a/drivers/extcon/extcon-usbc-tusb320.c +++ b/drivers/extcon/extcon-usbc-tusb320.c @@ -78,6 +78,7 @@ struct tusb320_priv { struct typec_capability cap; enum typec_port_type port_type; enum typec_pwr_opmode pwr_opmode; + struct fwnode_handle *connector_fwnode; }; static const char * const tusb_attached_states[] = { @@ -391,27 +392,25 @@ static int tusb320_typec_probe(struct i2c_client *client, /* Type-C connector found. */ ret = typec_get_fw_cap(&priv->cap, connector); if (ret) - return ret; + goto err_put; priv->port_type = priv->cap.type; /* This goes into register 0x8 field CURRENT_MODE_ADVERTISE */ ret = fwnode_property_read_string(connector, "typec-power-opmode", &cap_str); if (ret) - return ret; + goto err_put; ret = typec_find_pwr_opmode(cap_str); if (ret < 0) - return ret; - if (ret == TYPEC_PWR_MODE_PD) - return -EINVAL; + goto err_put; priv->pwr_opmode = ret; /* Initialize the hardware with the devicetree settings. */ ret = tusb320_set_adv_pwr_mode(priv); if (ret) - return ret; + goto err_put; priv->cap.revision = USB_TYPEC_REV_1_1; priv->cap.accessory[0] = TYPEC_ACCESSORY_AUDIO; @@ -422,10 +421,25 @@ static int tusb320_typec_probe(struct i2c_client *client, priv->cap.fwnode = connector; priv->port = typec_register_port(&client->dev, &priv->cap); - if (IS_ERR(priv->port)) - return PTR_ERR(priv->port); + if (IS_ERR(priv->port)) { + ret = PTR_ERR(priv->port); + goto err_put; + } + + priv->connector_fwnode = connector; return 0; + +err_put: + fwnode_handle_put(connector); + + return ret; +} + +static void tusb320_typec_remove(struct tusb320_priv *priv) +{ + typec_unregister_port(priv->port); + fwnode_handle_put(priv->connector_fwnode); } static int tusb320_probe(struct i2c_client *client) @@ -438,7 +452,9 @@ static int tusb320_probe(struct i2c_client *client) priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL); if (!priv) return -ENOMEM; + priv->dev = &client->dev; + i2c_set_clientdata(client, priv); priv->regmap = devm_regmap_init_i2c(client, &tusb320_regmap_config); if (IS_ERR(priv->regmap)) @@ -489,10 +505,19 @@ static int tusb320_probe(struct i2c_client *client) tusb320_irq_handler, IRQF_TRIGGER_FALLING | IRQF_ONESHOT, client->name, priv); + if (ret) + tusb320_typec_remove(priv); return ret; } +static void tusb320_remove(struct i2c_client *client) +{ + struct tusb320_priv *priv = i2c_get_clientdata(client); + + tusb320_typec_remove(priv); +} + static const struct of_device_id tusb320_extcon_dt_match[] = { { .compatible = "ti,tusb320", .data = &tusb320_ops, }, { .compatible = "ti,tusb320l", .data = &tusb320l_ops, }, @@ -502,6 +527,7 @@ MODULE_DEVICE_TABLE(of, tusb320_extcon_dt_match); static struct i2c_driver tusb320_extcon_driver = { .probe_new = tusb320_probe, + .remove = tusb320_remove, .driver = { .name = "extcon-tusb320", .of_match_table = tusb320_extcon_dt_match,