Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp2514192imu; Thu, 29 Nov 2018 06:15:45 -0800 (PST) X-Google-Smtp-Source: AFSGD/Wy6uAH0cvNV9Hce1BAQdxu/9Z0mUCfEf5GG5yh7P9/LE9F4CYmveM1a93FyDwxiyZoXtwT X-Received: by 2002:a17:902:598e:: with SMTP id p14mr1628055pli.260.1543500945707; Thu, 29 Nov 2018 06:15:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543500945; cv=none; d=google.com; s=arc-20160816; b=CU82QBQQLpg7jsDBemTaTjq7+hIUl0aTMRm04UJuzjh5XMC8ZgIh2TK2Zhk6p26nRb fEDUzTDDUwas3mt3sxmRdFqyauLQAlPp+mwuMmLXf3aG4qUJOZbcqoeywX74jNer6pAi cBfx50f9RSBMV6s4jIo55hp8Xk+fFpo1CoAlKAQfaLE8XrsH5QmnUuY+4WIPDVsfiQ1K OdYjhH4NEeCWF3H4jIte0Puzb7xgkvl5JIDT55rqpOr5kFE3n8k1u+rEYiHP8tB9Yjeo 2wOmXaE9MkhQtvzNFT1vIfiiq7Y8co+r/kVsj/OU0+xe7UfsV4WP0JlUt9NpNbNF7wxt u8nw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=1Atka6l8HUdIZOCqIMW9qF1X8tER4FB1myIeplfKn4I=; b=0/UxnD64lAycZiXZtn5qtAqlEX7HcxZnhMbit2MuByekC1MnOyLDD/N28hnnlBXR1V AFQHfVsPWgaJiYt1s/1y2qjJ8+WxEOKF3EGdQXXAljVm+FVmClmL7sgLpkAjfqjbddG8 UydaCoS9eEiD+fvNSqOHflXpMttnJHLFLnAHN9XNSU/euJGOsvXtLZi+xc5/kDNp2UQP 2lunIzrHakWNIL8+bMEW4AzKh1uXBu2eIAVD6dGB23luyOVz+nFpes8mmOecDxTGMMUF 6nQ6JD9hFexcAVmqJxuEq3fyb3AsAT9zhzFqfNMqb+oaq1Y2LynPVSlEwkUWa8qfGDhW M5Kg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=NcnD0Q8Q; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f189si2224932pfg.123.2018.11.29.06.15.29; Thu, 29 Nov 2018 06:15:45 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=NcnD0Q8Q; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729120AbeK3BTu (ORCPT + 99 others); Thu, 29 Nov 2018 20:19:50 -0500 Received: from mail.kernel.org ([198.145.29.99]:40500 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728198AbeK3BTt (ORCPT ); Thu, 29 Nov 2018 20:19:49 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id E141521019; Thu, 29 Nov 2018 14:14:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1543500859; bh=PYEF3MqGwKIR17j7TLBVlL+On0iM+Xw9U8ZTRIDItaM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=NcnD0Q8Q7ISTFiJk1lFx5gme4T06ce0A9SXKSken+qugOGT6sq/cwa7uLOJYmFHc4 8cT39OFSZ0ygTjSEAwFUnxUBgRRk5EWENnm5Re9TeXbF91NnTGKEmj5eAID954GCTz iNp8qDHjAjlHOiHvE0Smi49TrQtMX1sAOk/7/3/Q= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Pavel Rojtberg , Dmitry Torokhov , Sasha Levin Subject: [PATCH 3.18 27/83] Input: xpad - use ida() for finding the pad_nr Date: Thu, 29 Nov 2018 15:11:45 +0100 Message-Id: <20181129140139.525654428@linuxfoundation.org> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181129140138.002176596@linuxfoundation.org> References: <20181129140138.002176596@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.18-stable review patch. If anyone has any objections, please let me know. ------------------ [ Upstream commit e3b651745a03810efffd7ebf2a9b5be65fb70ec3 ] The pad_nr corresponds to the lit up LED on the controller. Therefore there should be no gaps when enumerating. Currently a LED is only re-assigned after a controller is re-connected 4 times. This patch uses ida to track connected pads - this way we can re-assign freed up pad number immediately. Consider the following case: 1. pad A is connected and gets pad_nr = 0 2. pad B is connected and gets pad_nr = 1 3. pad A is disconnected 4. pad A is connected again using ida_simple_get() controller A now correctly gets pad_nr = 0 again. Signed-off-by: Pavel Rojtberg Signed-off-by: Dmitry Torokhov Signed-off-by: Sasha Levin --- drivers/input/joystick/xpad.c | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/drivers/input/joystick/xpad.c b/drivers/input/joystick/xpad.c index 60ebd78a49a6..a395980bb816 100644 --- a/drivers/input/joystick/xpad.c +++ b/drivers/input/joystick/xpad.c @@ -244,7 +244,6 @@ static const signed short xpad_btn_triggers[] = { -1 }; - static const signed short xpad360_btn[] = { /* buttons for x360 controller */ BTN_TL, BTN_TR, /* Button LB/RB */ BTN_MODE, /* The big X button */ @@ -347,7 +346,7 @@ struct usb_xpad { int mapping; /* map d-pad to buttons or to axes */ int xtype; /* type of xbox device */ - unsigned long pad_nr; /* the order x360 pads were attached */ + int pad_nr; /* the order x360 pads were attached */ }; /* @@ -883,6 +882,9 @@ static int xpad_init_ff(struct usb_xpad *xpad) { return 0; } #if defined(CONFIG_JOYSTICK_XPAD_LEDS) #include +#include + +static DEFINE_IDA(xpad_pad_seq); struct xpad_led { char name[16]; @@ -964,7 +966,6 @@ static void xpad_led_set(struct led_classdev *led_cdev, static int xpad_led_probe(struct usb_xpad *xpad) { - static atomic_t led_seq = ATOMIC_INIT(-1); struct xpad_led *led; struct led_classdev *led_cdev; int error; @@ -976,9 +977,13 @@ static int xpad_led_probe(struct usb_xpad *xpad) if (!led) return -ENOMEM; - xpad->pad_nr = atomic_inc_return(&led_seq); + xpad->pad_nr = ida_simple_get(&xpad_pad_seq, 0, 0, GFP_KERNEL); + if (xpad->pad_nr < 0) { + error = xpad->pad_nr; + goto err_free_mem; + } - snprintf(led->name, sizeof(led->name), "xpad%lu", xpad->pad_nr); + snprintf(led->name, sizeof(led->name), "xpad%d", xpad->pad_nr); led->xpad = xpad; led_cdev = &led->led_cdev; @@ -986,16 +991,19 @@ static int xpad_led_probe(struct usb_xpad *xpad) led_cdev->brightness_set = xpad_led_set; error = led_classdev_register(&xpad->udev->dev, led_cdev); - if (error) { - kfree(led); - xpad->led = NULL; - return error; - } + if (error) + goto err_free_id; /* Light up the segment corresponding to controller number */ xpad_identify_controller(xpad); - return 0; + +err_free_id: + ida_simple_remove(&xpad_pad_seq, xpad->pad_nr); +err_free_mem: + kfree(led); + xpad->led = NULL; + return error; } static void xpad_led_disconnect(struct usb_xpad *xpad) @@ -1004,6 +1012,7 @@ static void xpad_led_disconnect(struct usb_xpad *xpad) if (xpad_led) { led_classdev_unregister(&xpad_led->led_cdev); + ida_simple_remove(&xpad_pad_seq, xpad->pad_nr); kfree(xpad_led); } } @@ -1013,7 +1022,6 @@ static void xpad_led_disconnect(struct usb_xpad *xpad) { } static void xpad_identify_controller(struct usb_xpad *xpad) { } #endif - static int xpad_open(struct input_dev *dev) { struct usb_xpad *xpad = input_get_drvdata(dev); -- 2.17.1