From patchwork Wed Dec 12 17:00:02 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Tissoires X-Patchwork-Id: 1868411 X-Patchwork-Delegate: jikos@jikos.cz Return-Path: X-Original-To: patchwork-linux-input@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork1.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork1.kernel.org (Postfix) with ESMTP id B91533FC81 for ; Wed, 12 Dec 2012 17:00:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754143Ab2LLRAS (ORCPT ); Wed, 12 Dec 2012 12:00:18 -0500 Received: from mail-bk0-f46.google.com ([209.85.214.46]:48323 "EHLO mail-bk0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753920Ab2LLRAQ (ORCPT ); Wed, 12 Dec 2012 12:00:16 -0500 Received: by mail-bk0-f46.google.com with SMTP id q16so511887bkw.19 for ; Wed, 12 Dec 2012 09:00:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer; bh=B33yqhFZjwycgGhXQKCvpx54h6hcWK9PRcduOvQCHwY=; b=DyyTWfiKKCPkfHx+ccSOgvjqBUx3vxzgXBgl0QJHuOePmaAcJphSspQWJMMVoKY8PD BcXVyFVYlRBtqLlfUNNmHqqIDssBjjNs2/tx3DPJS7E4Us81mQ2SUYyOwcYgNTi1BEeN ZVczWAfCZlqJ2UgTgjhV/S6eQ8DBa69+qL6xfDA3ETufpY3eE+kR53Pk57DCSp6SDOJd A4m8WQ6Q7Ws+ErJZXMRZ4ZgvmcLQO9IRDsE+j72bC8LXnDy2+2mnhR2cmGVE4258/LIG DMN7b2evi2uCqnHW1YAw5H+S9VHd7srkrI0LV82MHgu0leeBlTtF9xqRKyve176jkCn0 G+YA== Received: by 10.204.3.206 with SMTP id 14mr879648bko.120.1355331615308; Wed, 12 Dec 2012 09:00:15 -0800 (PST) Received: from localhost.localdomain.com (lan31-8-82-247-176-67.fbx.proxad.net. [82.247.176.67]) by mx.google.com with ESMTPS id i20sm20936866bkw.5.2012.12.12.09.00.12 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 12 Dec 2012 09:00:13 -0800 (PST) From: Benjamin Tissoires To: Jiri Kosina , Jean Delvare , linux-input@vger.kernel.org, linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Benjamin Tissoires Subject: [PATCH v2] HID: i2c-hid: add mutex protecting open/close race Date: Wed, 12 Dec 2012 18:00:02 +0100 Message-Id: <1355331602-1164-1-git-send-email-benjamin.tissoires@gmail.com> X-Mailer: git-send-email 1.8.0.2 Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org We should not enter close function while someone else is in open. This mutex prevents this race. There is also no need to override the ret value with -EIO in case of a failure of i2c_hid_set_power. Signed-off-by: Benjamin Tissoires Reviewed-by: Jean Delvare --- Changes in v2: * add mutex.h Cheers, Benjamin drivers/hid/i2c-hid/i2c-hid.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/drivers/hid/i2c-hid/i2c-hid.c b/drivers/hid/i2c-hid/i2c-hid.c index 6e1774c..9ef22244 100644 --- a/drivers/hid/i2c-hid/i2c-hid.c +++ b/drivers/hid/i2c-hid/i2c-hid.c @@ -33,6 +33,7 @@ #include #include #include +#include #include @@ -117,6 +118,8 @@ static const struct i2c_hid_cmd hid_set_power_cmd = { I2C_HID_CMD(0x08) }; * static const struct i2c_hid_cmd hid_set_protocol_cmd = { I2C_HID_CMD(0x07) }; */ +static DEFINE_MUTEX(i2c_hid_open_mut); + /* The main device structure */ struct i2c_hid { struct i2c_client *client; /* i2c client */ @@ -641,17 +644,20 @@ static int i2c_hid_open(struct hid_device *hid) { struct i2c_client *client = hid->driver_data; struct i2c_hid *ihid = i2c_get_clientdata(client); - int ret; + int ret = 0; + mutex_lock(&i2c_hid_open_mut); if (!hid->open++) { ret = i2c_hid_set_power(client, I2C_HID_PWR_ON); if (ret) { hid->open--; - return -EIO; + goto done; } set_bit(I2C_HID_STARTED, &ihid->flags); } - return 0; +done: + mutex_unlock(&i2c_hid_open_mut); + return ret; } static void i2c_hid_close(struct hid_device *hid) @@ -663,12 +669,14 @@ static void i2c_hid_close(struct hid_device *hid) * data acquistion due to a resumption we no longer * care about */ + mutex_lock(&i2c_hid_open_mut); if (!--hid->open) { clear_bit(I2C_HID_STARTED, &ihid->flags); /* Save some power */ i2c_hid_set_power(client, I2C_HID_PWR_SLEEP); } + mutex_unlock(&i2c_hid_open_mut); } static int i2c_hid_power(struct hid_device *hid, int lvl)