From patchwork Mon Sep 5 20:40:14 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Lechner X-Patchwork-Id: 666060 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3sSjj20l9wz9s9N for ; Tue, 6 Sep 2016 07:36:22 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=lechnology.com header.i=@lechnology.com header.b=0jPddD0+; dkim-atps=neutral Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755200AbcIEVgU (ORCPT ); Mon, 5 Sep 2016 17:36:20 -0400 Received: from vern.gendns.com ([206.190.152.46]:35585 "EHLO vern.gendns.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755160AbcIEVgU (ORCPT ); Mon, 5 Sep 2016 17:36:20 -0400 X-Greylist: delayed 3328 seconds by postgrey-1.27 at vger.kernel.org; Mon, 05 Sep 2016 17:36:20 EDT DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lechnology.com; s=default; h=References:In-Reply-To:Message-Id:Date:Subject :Cc:To:From:Sender:Reply-To:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=bstQiebLMuWf76ANf6RzlXCA9X/YMtYtXOUFzgg6WOY=; b=0jPddD0+Iuj2bablPRY27pFeI dZUX8LPKwSTpBGvAHvxchVcB8vDfZKfH2chhtFi14nKZdJfGAtuWimfP8V2RSG5T6Ak9XSAvmZJy/ Q8NG11MQg5saYpNB7SibAOlh4+JhsU6IPOV1Pp8bE8SndudRLLUpuVez7awWkb/LQFtL3s4F0MWgQ tDtaoAStxw08I5ahGiufimA/eH9H3fYVpsBMmHjLnCck7zYhrGRWnhS7YOKPEg2fpakmc7E8n5rpz F3DQzKT96l0KEyTj2Qmx+apiVXeVyCOX1Tocm3Fq9n3ySyvx6jgSSOOu7/uoejVVQDO/or+yms7DD asEJ48NUw==; Received: from 108-198-5-147.lightspeed.okcbok.sbcglobal.net ([108.198.5.147]:55950 helo=freyr.lechnology.com) by vern.gendns.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES128-SHA256:128) (Exim 4.87) (envelope-from ) id 1bh0hJ-002Cuf-Fq; Mon, 05 Sep 2016 16:40:53 -0400 From: David Lechner To: Wolfram Sang Cc: David Lechner , linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 3/3] i2c: expose adapter probe and remove probed functions Date: Mon, 5 Sep 2016 15:40:14 -0500 Message-Id: <1473108014-30787-4-git-send-email-david@lechnology.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1473108014-30787-1-git-send-email-david@lechnology.com> References: <1473108014-30787-1-git-send-email-david@lechnology.com> X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - vern.gendns.com X-AntiAbuse: Original Domain - vger.kernel.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - lechnology.com X-Get-Message-Sender-Via: vern.gendns.com: authenticated_id: davidmain+lechnology.com/only user confirmed/virtual account not confirmed X-Authenticated-Sender: vern.gendns.com: davidmain@lechnology.com X-Source: X-Source-Args: X-Source-Dir: Sender: linux-i2c-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org Publicly expose functions for detecting devices and removing detected devices. Currently an i2c adapter only probes/detects new devices when the adapter is added or when a new i2c driver is added. This adds a new function, i2c_adapter_probe(), that allows detecting sensors at any time. Furthermore, the function i2c_adapter_remove_probed() is added to remove the devices added by i2c_adapter_probe() at any time. The intended use of these functions is for LEGO MINDSTORMS sensors. These are hot-plugable I2C devices. When a sensor is connected, i2c_adapter_probe() is called to automatically configure the sensor. When the sensor is disconnected, i2c_adapter_remove_probed() is called to remove the automatically configured device. Signed-off-by: David Lechner --- drivers/i2c/i2c-core.c | 45 ++++++++++++++++++++++++++++++++++++++------- include/linux/i2c.h | 3 +++ 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c index 28436d9..2781a88 100644 --- a/drivers/i2c/i2c-core.c +++ b/drivers/i2c/i2c-core.c @@ -1691,6 +1691,23 @@ static int __process_new_adapter(struct device_driver *d, void *data) return i2c_do_add_adapter(to_i2c_driver(d), data); } +/** + * i2c_adapter_probe - probe adapter for clients + * @adap: The i2c adapter. + * + * This calls the detect function of each driver that matches the class of the + * adapter. This function is called when an adapter is added, so there is no + * need to call this manually, unless you have hot-plugable i2c devices that + * are connected after the adapter is created. + */ +void i2c_adapter_probe(struct i2c_adapter *adap) +{ + mutex_lock(&core_lock); + bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter); + mutex_unlock(&core_lock); +} +EXPORT_SYMBOL(i2c_adapter_probe); + static int i2c_register_adapter(struct i2c_adapter *adap) { int res = -EINVAL; @@ -1759,9 +1776,7 @@ static int i2c_register_adapter(struct i2c_adapter *adap) i2c_scan_static_board_info(adap); /* Notify drivers */ - mutex_lock(&core_lock); - bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter); - mutex_unlock(&core_lock); + i2c_adapter_probe(adap); return 0; @@ -1904,6 +1919,25 @@ static int __process_removed_adapter(struct device_driver *d, void *data) } /** + * i2c_adapter_remove_probed - Remove clients that were automatically detected. + * @adap: The i2c adapter. + * + * This removes all i2c clients from this adapter that were added via driver + * detect() functions. This function is called when an adapter is removed, so + * there is no need to call this manually, unless you have hot-plugable i2c + * devices that are disconnected before the adapter is destroyed. + * + * Any clients that were added manually (e.g. via sysfs) are not removed. + */ +void i2c_adapter_remove_probed(struct i2c_adapter *adap) +{ + mutex_lock(&core_lock); + bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_removed_adapter); + mutex_unlock(&core_lock); +} +EXPORT_SYMBOL(i2c_adapter_remove_probed); + +/** * i2c_del_adapter - unregister I2C adapter * @adap: the adapter being unregistered * Context: can sleep @@ -1927,10 +1961,7 @@ void i2c_del_adapter(struct i2c_adapter *adap) acpi_i2c_remove_space_handler(adap); /* Tell drivers about this removal */ - mutex_lock(&core_lock); - bus_for_each_drv(&i2c_bus_type, NULL, adap, - __process_removed_adapter); - mutex_unlock(&core_lock); + i2c_adapter_remove_probed(adap); /* Remove devices instantiated from sysfs */ mutex_lock_nested(&adap->userspace_clients_lock, diff --git a/include/linux/i2c.h b/include/linux/i2c.h index 3eab858..0016623 100644 --- a/include/linux/i2c.h +++ b/include/linux/i2c.h @@ -657,6 +657,9 @@ extern int i2c_add_adapter(struct i2c_adapter *); extern void i2c_del_adapter(struct i2c_adapter *); extern int i2c_add_numbered_adapter(struct i2c_adapter *); +extern void i2c_adapter_probe(struct i2c_adapter *adap); +extern void i2c_adapter_remove_probed(struct i2c_adapter *adap); + extern int i2c_register_driver(struct module *, struct i2c_driver *); extern void i2c_del_driver(struct i2c_driver *);