Received: by 2002:ab2:2994:0:b0:1ef:ca3e:3cd5 with SMTP id n20csp68163lqb; Thu, 14 Mar 2024 05:54:04 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW/i+CDy5XRmSaINtV4kKzV/6KkBFUH1XUOELykWLEeyjKlREYJnNMEH0B3SBovQq8P1gjLmPqTco7CL9JM3vkTXrrTdp6TCqUKunnz1Q== X-Google-Smtp-Source: AGHT+IHblHwirhaYvZLAE8hNF74OV3fRERA6/2Nx/n3TewHYsB0MHZCUEzlSWUxgi4Xd78HLSWsI X-Received: by 2002:a17:903:2443:b0:1dd:7d64:8f35 with SMTP id l3-20020a170903244300b001dd7d648f35mr2061059pls.31.1710420844395; Thu, 14 Mar 2024 05:54:04 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710420844; cv=pass; d=google.com; s=arc-20160816; b=E6dBvV1pIfXy4/j6qMsMFD+YBtx+ybIRe0Z6BahOxWi1rOS0a7GjOSAkS7K1vsyFwH aM5ETetZKQqtPsEUl0VkeqNEtJdFUIH4X9VSNG8Cxa3xFwNAx501KEw0TwvOY5XlWQ79 72e+rGv+PlW8qwuKZlYLrwNr4yK5kgF8xOn5hsVv4FXEOp8bebgtLI62OvxxtsquZ3Ox q2udaE5N2FA+hiMIPRneWXQ2GVS9TdgKWFi60dwFw4SMphNcmwAStLxqK1CUOCO2Ts4x lW12WcUtKTKYiucDq/0eQyV9FdMZ2kV02NwCUp75Ge77QotnjsNPUZGsGNoaLj1GifB9 y8Ow== 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=3BFiBlcMDCZLtjf4LGfypHiPzExz4Vqh036TGMbgQ1Y=; fh=4lGQI6056MBa4/oovMyIYaKLn+Wz/24RGvUivMmQP8E=; b=SzLybPrjvQBYNEHEyiyKuFbItF9Ew2bQv5E3Lr/ODDJjgxfFr0kbp07L0BxGd0drSd xuR3n1GuVsJY+mgm3VB3rUfV3AHDo75wZp3ubAcs3cS04MJZfu3jCRzBPSdHF8QKG5zg S/JS+LBw8Q+fF1QC5BkZXqtDVWlW46+qxnFcsxf/SbMB5lu8yfEm1PxBHAF5sFsrOsnY hwibUScnLW4S9ydsQWpuWEiqezKkWMkMDLNBg3r+p9g03Ibb6mPUMlHY1Z/PgYlYBEep 2gCJegnAYlJket90JmAI20M1I9ggCIqTbhUZOoL240gst6wkfL3/16X1/+yYAsawLEUN b6WQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=ftnUAlMM; 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-2516-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-bluetooth+bounces-2516-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id d12-20020a170902cecc00b001dd998d4211si1487263plg.103.2024.03.14.05.54.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Mar 2024 05:54:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth+bounces-2516-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=@gmail.com header.s=20230601 header.b=ftnUAlMM; 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-2516-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-bluetooth+bounces-2516-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id A2E14284250 for ; Thu, 14 Mar 2024 12:54:03 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D252E58AD2; Thu, 14 Mar 2024 12:53:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ftnUAlMM" X-Original-To: linux-bluetooth@vger.kernel.org Received: from mail-vk1-f181.google.com (mail-vk1-f181.google.com [209.85.221.181]) (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 ABD9118E20 for ; Thu, 14 Mar 2024 12:53:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.181 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710420838; cv=none; b=Nihe+hdAuvfV3QXF9eqI68YTQ9y2oiZccugyBGXGve6ytOTF4EImmbULZ5wh+35WUCrdbXi1ohyLc3hHTEWe5RUeGfhYCG3oNcX4BwYqLHoA+/QJ/rds1d3lOIFFMAIf1E7SlO1Qc06Ti0uD/1JtaJ65JX+b9VcSGIP87zW44YE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710420838; c=relaxed/simple; bh=0VycfRxjzpsUKbRxcXrS/f59L0pTBiZoS/c5FsU/MeU=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=CyhOuyGTsmC/D5hza2ii7Ik+/LPQcJ3HkPeb/dW9ZoMkzQaPQIfrn6q+nNCeb6uk4aZvhI8GLz7+25V+5y2eyQTIQxMcs9SjtQPO6Hn8UsxzvXwJROElaCadGgRR3jUbTzGyRvc0yEopvgJIQT3xexrudbHwcru8shqIpLMipxw= 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=ftnUAlMM; arc=none smtp.client-ip=209.85.221.181 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-vk1-f181.google.com with SMTP id 71dfb90a1353d-4d41547dc1dso446842e0c.0 for ; Thu, 14 Mar 2024 05:53:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710420834; x=1711025634; 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=3BFiBlcMDCZLtjf4LGfypHiPzExz4Vqh036TGMbgQ1Y=; b=ftnUAlMMKhn8q30AIrp1M4pnKRE5wBPNNGFHuC5rm9LwCvQPxyEuN5LGaVs3NyGiYM gVSn84RrD10sSSncFUCaYocFy0o2bTN4e5Uz7qRk9Gyyy/AAZLOGBEapOR9c6PskkK+P D0OKJSWiaANP6l6OKfekg41fXWrWuID4dbJIs3WA37wn6Mv9LW5cjk+Zj5mD0QTg7Pcv Frln33XbYBK2RL/uAq7qGaw43v0IWkIqfN4qpxLaT8H8PsybrKEd2lpcjCzr+ljPvogz bfnxhAfQS6qyYCGDDf6QxBtKPBIKa9EBaAQ1dkvKXALxVLTfX3oCx3yyK9ytCU+cutrs slJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710420834; x=1711025634; 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=3BFiBlcMDCZLtjf4LGfypHiPzExz4Vqh036TGMbgQ1Y=; b=ihWapEWeiIRk0YCDaQS+n8CxF1DYQOu3wXKSgCF3IrUyO6eaRe37thoF0PvpFoD+4A CFpFLfLdpUrX6PleUsTu5pVNX7H8hXt2/0iT/m9BINbFV+4wZksUkrrfBzVCSnDuHQnz mjYyHLLLBQTIdzyhYKNBL1JbhLFsqiE8N9dKPzfCmawnF+b5DH1C555Mt48sgFceeWif vLug0pX3wjlt6NyGHTyAZXmPdvwCjlc1wHJqifhAz3KM21r3WPi3RqDOt88ZN2mkPe8Y R3AdZPa7rWb8R6txEr1a3SotR4JL+GRQgTBa2SOyTSHIjV4V+Lr6zlkVDkTqcvDXwfgO /cMA== X-Gm-Message-State: AOJu0YyJrNIVqityA/9tvqN5XypYUJNSsdxRMJFzlSP+zWBXzANKky3+ xbGTjcJCnl0DkAAemDNSkrQDHAfSIqmRndgTJxqE0Y87d9vfLMP0PnzzTwbe X-Received: by 2002:a05:6122:98b:b0:4cb:56c5:580e with SMTP id g11-20020a056122098b00b004cb56c5580emr2130372vkd.11.1710420834292; Thu, 14 Mar 2024 05:53:54 -0700 (PDT) Received: from lvondent-mobl4.. (107-146-107-067.biz.spectrum.com. [107.146.107.67]) by smtp.gmail.com with ESMTPSA id y127-20020a1f7d85000000b004d41ded4a4asm74796vkc.15.2024.03.14.05.53.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Mar 2024 05:53:53 -0700 (PDT) From: Luiz Augusto von Dentz To: linux-bluetooth@vger.kernel.org Subject: [PATCH BlueZ v2] input/device: Force UHID_DESTROY on error Date: Thu, 14 Mar 2024 08:53:52 -0400 Message-ID: <20240314125352.15243-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 If we cannot sent a message back to the device destroy the UHID device since it is likely that the driver is trying to reach the remote device which is no longer connected. Fixes: https://github.com/bluez/bluez/issues/777 --- profiles/input/device.c | 65 +++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/profiles/input/device.c b/profiles/input/device.c index c4f75c7442e8..1b28cdc174b1 100644 --- a/profiles/input/device.c +++ b/profiles/input/device.c @@ -105,7 +105,6 @@ bool input_get_classic_bonded_only(void) static void input_device_enter_reconnect_mode(struct input_device *idev); static int connection_disconnect(struct input_device *idev, uint32_t flags); -static int uhid_disconnect(struct input_device *idev); static bool input_device_bonded(struct input_device *idev) { @@ -314,6 +313,28 @@ 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; @@ -356,8 +377,7 @@ static gboolean intr_watch_cb(GIOChannel *chan, GIOCondition cond, gpointer data virtual_cable_unplug(idev); /* If connection abruptly ended, uhid might be not yet disconnected */ - if (bt_uhid_created(idev->uhid)) - uhid_disconnect(idev); + uhid_disconnect(idev, false); return FALSE; } @@ -610,11 +630,14 @@ static void hidp_send_output(struct uhid_event *ev, void *user_data) { struct input_device *idev = user_data; uint8_t hdr = HIDP_TRANS_DATA | HIDP_DATA_RTYPE_OUTPUT; + bool sent; DBG(""); - hidp_send_intr_message(idev, hdr, ev->u.output.data, + sent = hidp_send_intr_message(idev, hdr, ev->u.output.data, ev->u.output.size); + if (!sent) + uhid_disconnect(idev, true); } static void hidp_send_set_report(struct uhid_event *ev, void *user_data) @@ -654,8 +677,11 @@ static void hidp_send_set_report(struct uhid_event *ev, void *user_data) timeout_add_seconds(REPORT_REQ_TIMEOUT, hidp_report_req_timeout, idev, NULL); idev->report_rsp_id = ev->u.set_report.id; - } else + } else { uhid_send_set_report_reply(idev, ev->u.set_report.id, EIO); + /* Force UHID_DESTROY on error */ + uhid_disconnect(idev, true); + } } static void hidp_send_get_report(struct uhid_event *ev, void *user_data) @@ -698,9 +724,12 @@ static void hidp_send_get_report(struct uhid_event *ev, void *user_data) hidp_report_req_timeout, idev, NULL); idev->report_rsp_id = ev->u.get_report.id; - } else + } else { uhid_send_get_report_reply(idev, NULL, 0, ev->u.get_report.id, EIO); + /* Force UHID_DESTROY on error */ + uhid_disconnect(idev, true); + } } static void epox_endian_quirk(unsigned char *data, int size) @@ -934,28 +963,6 @@ static int uhid_connadd(struct input_device *idev, struct hidp_connadd_req *req) return err; } -static int uhid_disconnect(struct input_device *idev) -{ - 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) - 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 encrypt_notify(GIOChannel *io, GIOCondition condition, gpointer data) { @@ -1087,7 +1094,7 @@ static int connection_disconnect(struct input_device *idev, uint32_t flags) idev->virtual_cable_unplug = true; if (idev->uhid) - return uhid_disconnect(idev); + return uhid_disconnect(idev, false); else return ioctl_disconnect(idev, flags); } -- 2.44.0