Received: by 2002:ab2:6f44:0:b0:1fd:c486:4f03 with SMTP id l4csp242033lqq; Thu, 13 Jun 2024 01:29:59 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCV7C0wAqGzYfPxKSc6uhzP0ZdMS0vonJmv8xl//MLbKLmKqYb47u5a8cFNIVQcGzBssjpsYdjqbWt3RpPMJsgQzYF6jLe8+kstwISx1Fg== X-Google-Smtp-Source: AGHT+IEvFwm1sOwTVxYIfVljzq2CdBHeAaDn0aS+ayJuBYopBKeLW6qH9HM133pnnA5uyKCwa0r6 X-Received: by 2002:a81:9284:0:b0:631:2ebf:b501 with SMTP id 00721157ae682-6312ebfbaa5mr7717217b3.23.1718267399176; Thu, 13 Jun 2024 01:29:59 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718267399; cv=pass; d=google.com; s=arc-20160816; b=Ent1XvmVJxKvkMu2cgYa4HJCb8hV+aXlb3JdjeWIHl7/V91sTUWEim5T9ju54Cft3M uOAdBy2uSc/wx20dlErYlXcT0yKIqFkd0Y0y4jrfGsyjR1ezkRz64NyqWgeNgreGxpZU 1zyhUHKeNzLRQkfthhXdBeoBws8kOjSCHj58siMyjDJ7ggclnX+u1V9aJV1Oe4lfi6eN DqVm+YKlccBG0puePUxo0L23DC3ntl9LV3iO8KR3Cxk1Rl/65ojsjUMd2O4j3WOk/LBx kAC3toL9kAz72iLR1blgwhneITMgD9X6rpP9SEtURTRYBRol/sKGbNLrV52budEnLBqz iOXw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=j2Im9dGKZIt0RrtNSRiS+mo63+EnUh986UQ1mB7bgEE=; fh=iSazCSy2WqyLlj6bZerAJHUA/Y0TolMq4J1e48wCKPc=; b=w9i6+yy7Y+rHFVsYF4ZsZ3qp6NMtHrkt4+UFTOxvE60d+9QSjj8lda3fr6Lp4NwQvZ zgc4MpP1ZWuCqpSomeoi16g3t1N+y82Z6+xUJPdWSsrMp0e6qflmNjxtlMxBAuTDaoCU lOwXREcdZ2aOUkV+j+Ll1mm30I0cDJSB9IE0ZrS8Kh29QFmdonSs5uaq2J0MJhbkITG4 37uwQSkErd+2ufIJIH879r0wZ8rWuchj0z+AMR3R+DGOEEFrHha5r2DeRlc4CtOCITNt 8s1GO8f0DkKjB2MZCwo7S6bh5P8TBevAa5qgeqEEtgHpgjQgV23wzoIASaACrTjsnD2r g7+g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=DGyjKQdt; arc=pass (i=1 spf=pass spfdomain=linaro.org dkim=pass dkdomain=linaro.org dmarc=pass fromdomain=linaro.org); spf=pass (google.com: domain of linux-kernel+bounces-212811-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-212811-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id 41be03b00d2f7-6fee67ba6b9si829050a12.848.2024.06.13.01.29.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jun 2024 01:29:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-212811-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=DGyjKQdt; arc=pass (i=1 spf=pass spfdomain=linaro.org dkim=pass dkdomain=linaro.org dmarc=pass fromdomain=linaro.org); spf=pass (google.com: domain of linux-kernel+bounces-212811-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-212811-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 0113F28209D for ; Thu, 13 Jun 2024 08:29:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9590E13DB83; Thu, 13 Jun 2024 08:26:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="DGyjKQdt" Received: from mail-yw1-f172.google.com (mail-yw1-f172.google.com [209.85.128.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 68D5513D889 for ; Thu, 13 Jun 2024 08:26:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718267211; cv=none; b=WC3rF4EE06xSoJ5QazWhl3a81DHlN/7Aq6g9TG3hgA2d1ltuSQ0ifnAQ79uOmJce8y299/f+j4xKljsUUf3a4nC7MR+NATyZ6oG1CVgQfonT4Nd3EuumZH1V+vMuMLAiDjyu9lnpaWDo+4ETB8FBflUfYLs+SsNhqgAmLhxT8uo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718267211; c=relaxed/simple; bh=+/Cd54M7taimSwzXrHgDVYRSG+71hfapAIJPbLSt9Hs=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=MZxaThjhzjyKJo4RwuQ7aJuL1tSzZeG+q4crEp2uyEMb/IXXS6CkRqGLJuky5lj7AVNftyZvwGdGBHG6pjsMuvS3olAzEgSn06UNz2MGSdRionLpQIMaUduJZQd3XgA3xU2gykDtuk8fS6n2IkP8ZwyJ10XhQnS3OfQrKLfIrIU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=DGyjKQdt; arc=none smtp.client-ip=209.85.128.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-yw1-f172.google.com with SMTP id 00721157ae682-6301aa3a89eso8331627b3.2 for ; Thu, 13 Jun 2024 01:26:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1718267208; x=1718872008; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=j2Im9dGKZIt0RrtNSRiS+mo63+EnUh986UQ1mB7bgEE=; b=DGyjKQdtRPt725gon6zKX0CDMb/4SvXXbz+a8ucF6xToOi9oHXx0jfJB0kLzln8ZHy foujg175BB+uqQqzck0FOaofgxOmwiFmLBpsBsHilK9lAjLAqxGA/wlOIfbc68OAmzry XzntPyUrT66JoS8Sgm6mn0BJsFE9OiqBxqgDwam76TR0IYX5biAJbXBf/F3EudhUJ3jV jwZk9eiJFBPjMxd7eBpS4abwutKnug+p8yyDXDIyBVWaeigeej/uv97fjGx+xgCEkt/O UODESD3/L4nVVUFpu1J1AQCIq+ZGa/VBIlHUVhr3xD742PvMvp1w1BNCmWuODGAhchgU lC3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718267208; x=1718872008; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=j2Im9dGKZIt0RrtNSRiS+mo63+EnUh986UQ1mB7bgEE=; b=U9qma37pYrJpBY9naBaTbEEdnnUuqP8lBmJM684fTbBuSLkv2lL/MEE9nApsPJBX5p W15taFV/REJrp3DzICrO/riXpJaN//iJS9fEca9gJ6bQGkBPTXRjZHmXMvkeGhlvIbX9 hid3SNO/0/9fs5A3hKwAjo8Y8WQp68H5uwIIoTDle04FkbR6LtPiUPZsUorFhFhUlS99 Vq/5hNO9j3iw9MV76QFt8M4BGPLaQZ819lS5vLwb5/5bnv5gyna0AjVKoWyPZG+bYIqO asBvVmqB/9kpWq104MB3/okyT3uVYAsUPVqM5KVcX1KKzbXy1MIywb/0UvDmOIt/SF+p eAVQ== X-Forwarded-Encrypted: i=1; AJvYcCXPpNVfnS5n6Bgbb2KgAxOeS9O5xWZiHHrc1MIUSN+JUcJyJXe8MK3TJDUtBtfhi4T41iIuexS7s6lHwHW8/AW5zq2NLHZgBt7TitT/ X-Gm-Message-State: AOJu0YzA5v3Tzen0W+T2ODcT9M7gtJUuwcghiBoAObWR5fLTn31w3m2X F5KxzoaygRcYtunGquvr3KCukC8INisybg6JlGnSRfozdOqQgUAdmebYqEiJuC7w1IG0udk3a8Y emQ3VEtuca4HHS2BxKRabt/FOP339tUSMJe1g/g== X-Received: by 2002:a0d:e6cf:0:b0:62c:e62d:561d with SMTP id 00721157ae682-62fbb7f5e6fmr40080067b3.1.1718267208393; Thu, 13 Jun 2024 01:26:48 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240612-yoga-ec-driver-v6-0-8e76ba060439@linaro.org> <20240612-yoga-ec-driver-v6-3-8e76ba060439@linaro.org> In-Reply-To: From: Dmitry Baryshkov Date: Thu, 13 Jun 2024 11:26:37 +0300 Message-ID: Subject: Re: [PATCH v6 3/6] usb: typec: ucsi: add Lenovo Yoga C630 glue driver To: =?UTF-8?Q?Ilpo_J=C3=A4rvinen?= Cc: Sebastian Reichel , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Bjorn Andersson , Hans de Goede , "Bryan O'Donoghue" , Heikki Krogerus , Greg Kroah-Hartman , Konrad Dybcio , linux-pm@vger.kernel.org, devicetree@vger.kernel.org, LKML , platform-driver-x86@vger.kernel.org, linux-usb@vger.kernel.org, linux-arm-msm@vger.kernel.org, Nikita Travkin Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, 13 Jun 2024 at 10:30, Ilpo J=C3=A4rvinen wrote: > > On Wed, 12 Jun 2024, Dmitry Baryshkov wrote: > > > The Lenovo Yoga C630 WOS laptop provides implements UCSI interface in > > the onboard EC. Add glue driver to interface the platform's UCSI > > implementation. > > > > Reviewed-by: Bryan O'Donoghue > > Reviewed-by: Heikki Krogerus > > Signed-off-by: Dmitry Baryshkov > > --- > > > +static int yoga_c630_ucsi_read(struct ucsi *ucsi, unsigned int offset, > > + void *val, size_t val_len) > > +{ > > + struct yoga_c630_ucsi *uec =3D ucsi_get_drvdata(ucsi); > > + u8 buf[YOGA_C630_UCSI_READ_SIZE]; > > + int ret; > > + > > + ret =3D yoga_c630_ec_ucsi_read(uec->ec, buf); > > + if (ret) > > + return ret; > > + > > + if (offset =3D=3D UCSI_VERSION) { > > + memcpy(val, &uec->version, min(val_len, sizeof(uec->versi= on))); > > + return 0; > > + } > > + > > + if (offset =3D=3D UCSI_CCI) > > + memcpy(val, buf, min(val_len, YOGA_C630_UCSI_CCI_SIZE)); > > + else if (offset =3D=3D UCSI_MESSAGE_IN) > > + memcpy(val, buf + YOGA_C630_UCSI_CCI_SIZE, > > + min(val_len, YOGA_C630_UCSI_DATA_SIZE)); > > + else > > + return -EINVAL; > > + > > + return 0; > > Hmm, the inconsistency when to do return 0 is a bit odd. Also, using > switch (offset) would probably be better here anyway to replace all the > ifs. I'll see if I can improve this bit. > > > +} > > + > > +static int yoga_c630_ucsi_async_write(struct ucsi *ucsi, unsigned int = offset, > > + const void *val, size_t val_len) > > +{ > > + struct yoga_c630_ucsi *uec =3D ucsi_get_drvdata(ucsi); > > + > > + if (offset !=3D UCSI_CONTROL || > > + val_len !=3D YOGA_C630_UCSI_WRITE_SIZE) > > + return -EINVAL; > > + > > + return yoga_c630_ec_ucsi_write(uec->ec, val); > > +} > > + > > +static int yoga_c630_ucsi_sync_write(struct ucsi *ucsi, unsigned int o= ffset, > > + const void *val, size_t val_len) > > +{ > > + struct yoga_c630_ucsi *uec =3D ucsi_get_drvdata(ucsi); > > + bool ack =3D UCSI_COMMAND(*(u64 *)val) =3D=3D UCSI_ACK_CC_CI; > > + int ret; > > + > > + if (ack) > > + set_bit(UCSI_C630_ACK_PENDING, &uec->flags); > > + else > > + set_bit(UCSI_C630_COMMAND_PENDING, &uec->flags); > > + > > + reinit_completion(&uec->complete); > > + > > + ret =3D yoga_c630_ucsi_async_write(ucsi, offset, val, val_len); > > + if (ret) > > + goto out_clear_bit; > > + > > + if (!wait_for_completion_timeout(&uec->complete, 5 * HZ)) > > + ret =3D -ETIMEDOUT; > > + > > +out_clear_bit: > > + if (ack) > > + clear_bit(UCSI_C630_ACK_PENDING, &uec->flags); > > + else > > + clear_bit(UCSI_C630_COMMAND_PENDING, &uec->flags); > > + > > + return ret; > > +} > > + > > +const struct ucsi_operations yoga_c630_ucsi_ops =3D { > > + .read =3D yoga_c630_ucsi_read, > > + .sync_write =3D yoga_c630_ucsi_sync_write, > > + .async_write =3D yoga_c630_ucsi_async_write, > > +}; > > + > > +static void yoga_c630_ucsi_notify_ucsi(struct yoga_c630_ucsi *uec, u32= cci) > > +{ > > + if (UCSI_CCI_CONNECTOR(cci)) > > + ucsi_connector_change(uec->ucsi, UCSI_CCI_CONNECTOR(cci))= ; > > + > > + if (cci & UCSI_CCI_ACK_COMPLETE && > > + test_bit(UCSI_C630_ACK_PENDING, &uec->flags)) > > + complete(&uec->complete); > > + > > + if (cci & UCSI_CCI_COMMAND_COMPLETE && > > + test_bit(UCSI_C630_COMMAND_PENDING, &uec->flags)) > > + complete(&uec->complete); > > Is this racy? Can another command start after an ACK in between these two > ifs and complete() is called prematurely for the new command? (Or will > different value in cci protect against that?) No, there is no race. The UCSI is locked for the duration of the command. > > > +} > > + > > +static int yoga_c630_ucsi_notify(struct notifier_block *nb, > > + unsigned long action, void *data) > > +{ > > + struct yoga_c630_ucsi *uec =3D container_of(nb, struct yoga_c630_= ucsi, nb); > > + u32 cci; > > + int ret; > > + > > + switch (action) { > > + case LENOVO_EC_EVENT_USB: > > + case LENOVO_EC_EVENT_HPD: > > + ucsi_connector_change(uec->ucsi, 1); > > + return NOTIFY_OK; > > + > > + case LENOVO_EC_EVENT_UCSI: > > + ret =3D uec->ucsi->ops->read(uec->ucsi, UCSI_CCI, &cci, s= izeof(cci)); > > + if (ret) > > + return NOTIFY_DONE; > > + > > + yoga_c630_ucsi_notify_ucsi(uec, cci); > > + > > + return NOTIFY_OK; > > + > > + default: > > + return NOTIFY_DONE; > > + } > > +} > > + > > +static int yoga_c630_ucsi_probe(struct auxiliary_device *adev, > > + const struct auxiliary_device_id *id) > > +{ > > + struct yoga_c630_ec *ec =3D adev->dev.platform_data; > > + struct yoga_c630_ucsi *uec; > > + int ret; > > + > > + uec =3D devm_kzalloc(&adev->dev, sizeof(*uec), GFP_KERNEL); > > + if (!uec) > > + return -ENOMEM; > > + > > + uec->ec =3D ec; > > + init_completion(&uec->complete); > > + uec->nb.notifier_call =3D yoga_c630_ucsi_notify; > > + > > + uec->ucsi =3D ucsi_create(&adev->dev, &yoga_c630_ucsi_ops); > > + if (IS_ERR(uec->ucsi)) > > + return PTR_ERR(uec->ucsi); > > + > > + ucsi_set_drvdata(uec->ucsi, uec); > > + > > + uec->version =3D yoga_c630_ec_ucsi_get_version(uec->ec); > > + > > + auxiliary_set_drvdata(adev, uec); > > + > > + ret =3D yoga_c630_ec_register_notify(ec, &uec->nb); > > + if (ret) > > + return ret; > > + > > + return ucsi_register(uec->ucsi); > > +} > > + > > +static void yoga_c630_ucsi_remove(struct auxiliary_device *adev) > > +{ > > + struct yoga_c630_ucsi *uec =3D auxiliary_get_drvdata(adev); > > + > > + yoga_c630_ec_unregister_notify(uec->ec, &uec->nb); > > + ucsi_unregister(uec->ucsi); > > Usually, the remove should tear down in reverse order than the probe side= . > Is the divergence from that here intentional? Yes, it's intentional, so that the driver doesn't get a notification while UCSI is being torn down. Consider it to be paired with ucsi_create(). --=20 With best wishes Dmitry