Received: by 2002:ab2:620c:0:b0:1ef:ffd0:ce49 with SMTP id o12csp1495106lqt; Wed, 20 Mar 2024 06:01:38 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXsr6unTSSku+l2Sf8Om3It6boXnTkT1w5hMGAJJVdk3ZUmTJhd6y273PscYxJt036OvYuj2N9/w3aYEaqg/6Wj2hGz0/YRK6ClllBmqA== X-Google-Smtp-Source: AGHT+IGjAxI+g7cfnEXvveqSSDtc6soNHGARtlNOf8/n7ZlEguaWPnMczP9+Y8Cn7INSLXuZB+eY X-Received: by 2002:a05:6402:500e:b0:566:4dc1:522c with SMTP id p14-20020a056402500e00b005664dc1522cmr4450003eda.15.1710939698538; Wed, 20 Mar 2024 06:01:38 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710939698; cv=pass; d=google.com; s=arc-20160816; b=a9tpzphto/98sT7M13ZB2kfHx2owXjE0FdinjbampGvbl4DoTDjSJ0F9DWil6D1pUe ErK2envVJosm0KUU31QdrDXdR9lqni+wvv1sJImtexApG6o2+P4yb/OG/jJf+9qOABN9 kcFr97v+wQqSukbF+bhHPXuZ0SPIYRvkPwfiWEybs4CivZVxp3UtHkQqsadRh7FK4pfO eST6d0Oy+N+QDS7U9D3oXNpxeZoWSloKcFeJm0b3xwsawhNz75L7vdQVrDLGpE6b0WUk bYt0uhfEfb0+gUorwcyVBIcCYSYv1nI63/2hEG39fD9mHE/zC6H0sCxBux+toDY928Dl y5Jg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:to:from :dkim-signature; bh=LF1S664mil8PDb8kZeofMd5EY7q01REtMQOrJthdT4g=; fh=4lGQI6056MBa4/oovMyIYaKLn+Wz/24RGvUivMmQP8E=; b=qvr0PqDlo230mTp/Z45McNqRCUbiazxrHczEXW458c9NOQo4UnQj/MdscwE+c9H7Fa IPrDQMmQpZwxWeAsZBZMCUhAMjvNeyFUDuGcC9QxnL/1aqS1Cc5512ecNto/hnJ6cGCj WX9YByqKubQh+v6prUV/mBHQnOE3V5/IJsohvtGRLQt1fhJxOTXRQa9al0G4CQmAnNjT YpCWpayWTDPU2Z926oznct+YHH+/9WgowGZ4cT3CWTckToDLIaY+AQvBY89Dwyoij4wp qtn9OhWMyFiNGSyKVhICDPFYvRKEsbk95+ZfYEHozV1jDnyQ1CG5uF2MSYrDGcmVwZw+ WdmQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="YXR0u0q/"; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-bluetooth+bounces-2667-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-bluetooth+bounces-2667-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id e6-20020a056402190600b0056bae77c454si710582edz.398.2024.03.20.06.01.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Mar 2024 06:01:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth+bounces-2667-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="YXR0u0q/"; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-bluetooth+bounces-2667-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-bluetooth+bounces-2667-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com 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 am.mirrors.kernel.org (Postfix) with ESMTPS id EB7681F24D15 for ; Wed, 20 Mar 2024 13:01:35 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3143C4084D; Wed, 20 Mar 2024 13:01:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="YXR0u0q/" X-Original-To: linux-bluetooth@vger.kernel.org Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.41]) (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 D89BF41206 for ; Wed, 20 Mar 2024 13:01:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.41 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710939684; cv=none; b=N9KJjH4OXVM6dprjnTq11/M6lX9Ah6+ym0srAYhYJUAeqggKLit72UB0XTwV9SHS9Y0PGURdoT490gvBaCAtTHZH6YI3NWxMS/6oJrm4/6UJiagiX+pIrKfvOSGDCxVhx4TltHgtGOrxWhwZ/Mg9NS90r2yMKBwffpx6FiemMew= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710939684; c=relaxed/simple; bh=VfVIVcbo/fE/aSHGNz+Hrv1ZldpsFHc94vUfaSp0bUk=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=g9Mb2FSrvpUb83GkDmGHwxd9qvemhzLYpPJzsHS95g3V6tmD+BbigOiPNxRC1mxqFX3C3EDfqFPYl9rd9A0MTzOqIZtTvDInZZBsO4yTPAJICpB2TLf2HL4TWg5Czxs0rQbN7+DVLBMieN8vbGl5u5Hivq2AwleVBr862fQSNZI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=YXR0u0q/; arc=none smtp.client-ip=209.85.221.41 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wr1-f41.google.com with SMTP id ffacd0b85a97d-33e162b1b71so5651408f8f.1 for ; Wed, 20 Mar 2024 06:01:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710939681; x=1711544481; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=LF1S664mil8PDb8kZeofMd5EY7q01REtMQOrJthdT4g=; b=YXR0u0q/a54KT7mLFQ2n3xecqIuRYe8M++/P4dTSRRIfEhkPC4aNfVcY2yZ20wmaH4 M0KTHvS8PS/AwawkulnhB5cRIYSLXUPJCCIb38az86Ma3G+vzFJTjkizSlxJo0XI7r/v Jh7iUttO5BJOGBba8T0o9hg9ZaQ2DKeeYlXMNkGkBQxoZnz5RhaWK3MsiQUOFjBSuruy LK1H2UnqBvvN+6RHqwbMmUte8HZuqU0xSDQjTriz3eNVb3SnkwDWmhF2K2rlnE2HShRj GcFrSpMWVcpOarE9gipOnr0AinwZlZDf7hDNv8phLDggUU+knz22j/SUdidBHbyCrXRq kF1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710939681; x=1711544481; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=LF1S664mil8PDb8kZeofMd5EY7q01REtMQOrJthdT4g=; b=mBqhbChSskE8FuhwPKTqYn8ojUmNA0JDmBBjdqHnExMZVvVVObJb+xBnKAUKDzK9jx zjXlXI2oYp+tC6Mmnuj9C7PWxDW4LxdOZX7DLcetwq73e+rAE5wTVAL89d4Nm18IC6t/ 2mYr41O2Gez7yzarBx/p4fN2cr9yiIn26QmzB7YdeT5t06NzXbrzCBwzppb0sKogXeP5 MSm5i5fjjbxjZ0Mv2xsD93+YGfWs8BdZvq9YDqU7pXpDEOdWNBoTUFWFIzK8qnPS9uyX wLwajeQEJtopxkA2yoqHbBfi9s9ebArxy4zAU7tyOfklSwz0Hh7qcgr0Th4jKvxhIT0N 7msw== X-Gm-Message-State: AOJu0YxO20jQYLyLVgUAsnklAm0h4/ncDsujOZgVKnobZaa0YSq/z51o 2AHtGiwwa2jti+qBvPOw9rtK58nJ2Vxjyx6M9fX0/xvdSeErscoHXLn0DU1apd3uoQ== X-Received: by 2002:a5d:408b:0:b0:33e:745a:88f5 with SMTP id o11-20020a5d408b000000b0033e745a88f5mr3610394wrp.57.1710939680449; Wed, 20 Mar 2024 06:01:20 -0700 (PDT) Received: from lvondent-mobl3.. (ftip004290803.acc4.faraday.21cn-nte.bt.net. [109.144.76.63]) by smtp.gmail.com with ESMTPSA id l4-20020adfe584000000b0033e91509224sm14700283wrm.22.2024.03.20.06.01.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Mar 2024 06:01:19 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH BlueZ v2] input/device: Fix not handling IdleTimeout when uhid is in use Date: Wed, 20 Mar 2024 13:01:19 +0000 Message-ID: <20240320130119.854959-1-luiz.dentz@gmail.com> X-Mailer: git-send-email 2.44.0 Precedence: bulk X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Luiz Augusto von Dentz When uhid is in use IdleTimeout was not taking any effect, this also attempt to force the destroy the input device node to make it useful for users that don't want to keep the input node forever. Fixes: https://github.com/bluez/bluez/issues/777 --- profiles/input/device.c | 89 +++++++++++++++++++++++++++------------ profiles/input/input.conf | 2 +- 2 files changed, 64 insertions(+), 27 deletions(-) diff --git a/profiles/input/device.c b/profiles/input/device.c index 1b28cdc17..5c52ec98a 100644 --- a/profiles/input/device.c +++ b/profiles/input/device.c @@ -77,6 +77,7 @@ struct input_device { unsigned int report_req_timer; uint32_t report_rsp_id; bool virtual_cable_unplug; + unsigned int idle_timer; }; static int idle_timeout = 0; @@ -139,6 +140,9 @@ static void input_device_free(struct input_device *idev) g_free(idev->req); } + if (idev->idle_timer) + timeout_remove(idev->idle_timer); + if (idev->reconnect_timer > 0) timeout_remove(idev->reconnect_timer); @@ -156,8 +160,57 @@ static void virtual_cable_unplug(struct input_device *idev) idev->virtual_cable_unplug = false; } -static bool hidp_send_message(GIOChannel *chan, uint8_t hdr, - const uint8_t *data, size_t size) +static int uhid_disconnect(struct input_device *idev, bool force) +{ + int err; + + if (!bt_uhid_created(idev->uhid)) + return 0; + + /* Only destroy the node if virtual cable unplug flag has been set */ + if (!idev->virtual_cable_unplug && !force) + return 0; + + bt_uhid_unregister_all(idev->uhid); + + err = bt_uhid_destroy(idev->uhid); + if (err < 0) { + error("bt_uhid_destroy: %s", strerror(-err)); + return err; + } + + return err; +} + +static bool input_device_idle_timeout(gpointer user_data) +{ + struct input_device *idev = user_data; + + idev->idle_timer = 0; + + DBG("path=%s", idev->path); + + if (connection_disconnect(idev, 0) == -ENOTCONN) { + /* Force UHID_DESTROY on idle */ + if (idev->uhid) + uhid_disconnect(idev, true); + } + + return false; +} + +static void input_device_idle_reset(struct input_device *idev) +{ + timeout_remove(idev->idle_timer); + + if (idle_timeout) + idev->idle_timer = timeout_add_seconds(idle_timeout, + input_device_idle_timeout, idev, + NULL); +} + +static bool hidp_send_message(struct input_device *idev, GIOChannel *chan, + uint8_t hdr, const uint8_t *data, size_t size) { int fd; ssize_t len; @@ -191,6 +244,8 @@ static bool hidp_send_message(GIOChannel *chan, uint8_t hdr, return false; } + input_device_idle_reset(idev); + return true; } @@ -200,13 +255,13 @@ static bool hidp_send_ctrl_message(struct input_device *idev, uint8_t hdr, if (hdr == (HIDP_TRANS_HID_CONTROL | HIDP_CTRL_VIRTUAL_CABLE_UNPLUG)) idev->virtual_cable_unplug = true; - return hidp_send_message(idev->ctrl_io, hdr, data, size); + return hidp_send_message(idev, idev->ctrl_io, hdr, data, size); } static bool hidp_send_intr_message(struct input_device *idev, uint8_t hdr, const uint8_t *data, size_t size) { - return hidp_send_message(idev->intr_io, hdr, data, size); + return hidp_send_message(idev, idev->intr_io, hdr, data, size); } static bool uhid_send_get_report_reply(struct input_device *idev, @@ -297,6 +352,8 @@ static bool hidp_recv_intr_data(GIOChannel *chan, struct input_device *idev) return true; } + input_device_idle_reset(idev); + hdr = data[0]; if (hdr != (HIDP_TRANS_DATA | HIDP_DATA_RTYPE_INPUT)) { DBG("unsupported HIDP protocol header 0x%02x", hdr); @@ -313,28 +370,6 @@ static bool hidp_recv_intr_data(GIOChannel *chan, struct input_device *idev) return true; } -static int uhid_disconnect(struct input_device *idev, bool force) -{ - int err; - - if (!bt_uhid_created(idev->uhid)) - return 0; - - /* Only destroy the node if virtual cable unplug flag has been set */ - if (!idev->virtual_cable_unplug && !force) - return 0; - - bt_uhid_unregister_all(idev->uhid); - - err = bt_uhid_destroy(idev->uhid); - if (err < 0) { - error("bt_uhid_destroy: %s", strerror(-err)); - return err; - } - - return err; -} - static gboolean intr_watch_cb(GIOChannel *chan, GIOCondition cond, gpointer data) { struct input_device *idev = data; @@ -520,6 +555,8 @@ static bool hidp_recv_ctrl_message(GIOChannel *chan, struct input_device *idev) return true; } + input_device_idle_reset(idev); + hdr = data[0]; type = hdr & HIDP_HEADER_TRANS_MASK; param = hdr & HIDP_HEADER_PARAM_MASK; diff --git a/profiles/input/input.conf b/profiles/input/input.conf index 00a34eb63..fc20c58b6 100644 --- a/profiles/input/input.conf +++ b/profiles/input/input.conf @@ -6,7 +6,7 @@ # Set idle timeout (in minutes) before the connection will # be disconnect (defaults to 0 for no timeout) -#IdleTimeout=30 +#IdleTimeout=0 # Enable HID protocol handling in userspace input profile # Defaults to true (Use UHID instead of kernel HIDP) -- 2.44.0