public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@cam.ac.uk>
To: Jean Delvare <khali@linux-fr.org>
Cc: Amit Kucheria <amit.kucheria@verdurent.com>,
	List Linux Kernel <linux-kernel@vger.kernel.org>,
	rui.zhang@intel.com, alan@linux.intel.com,
	linux-acpi@vger.kernel.org, gregkh@suse.de
Subject: Re: [PATCH 2/2] als: add unique device-ids to the als device class
Date: Thu, 26 Nov 2009 17:19:13 +0000	[thread overview]
Message-ID: <4B0EB891.4010309@cam.ac.uk> (raw)
In-Reply-To: <20091126160713.5e19eb04@hyperion.delvare>

Jean Delvare wrote:
> Hi Amit,
> 
> On Thu, 26 Nov 2009 14:06:26 +0200, Amit Kucheria wrote:
>> Other devices classes such as hwmon and input class handle assignment of
>> unique device-ids inside the core functions instead of pushing it out to
>> individual drivers. This reduces code duplication and resulting bugs.
>>
>> Signed-off-by: Amit Kucheria <amit.kucheria@verdurent.com>
>> Cc: Zhang Rui <rui.zhang@intel.com>
>> Cc: Jonathan Cameron <jic23@cam.ac.uk>
>>
>> ---
>>  drivers/als/als_sys.c |   48 +++++++++++++++++++++++++++++++++++++++++++++---
>>  1 files changed, 45 insertions(+), 3 deletions(-)
>>
>> diff --git a/drivers/als/als_sys.c b/drivers/als/als_sys.c
>> index e1d6395..aa15ad8 100644
>> --- a/drivers/als/als_sys.c
>> +++ b/drivers/als/als_sys.c
>> @@ -26,22 +26,55 @@
>>  #include <linux/device.h>
>>  #include <linux/err.h>
>>  #include <linux/kdev_t.h>
>> +#include <linux/idr.h>
>>  
>>  MODULE_AUTHOR("Zhang Rui <rui.zhang@intel.com>");
>>  MODULE_DESCRIPTION("Ambient Light Sensor sysfs/class support");
>>  MODULE_LICENSE("GPL");
>>  
>> +#define ALS_ID_PREFIX "als"
>> +#define ALS_ID_FORMAT ALS_ID_PREFIX "%d"
>> +
>>  static struct class *als_class;
>>  
>> +static DEFINE_IDR(als_idr);
>> +static DEFINE_SPINLOCK(idr_lock);
>> +
>>  /**
>>   * als_device_register - register a new Ambient Light Sensor class device
>>   * @parent:	the device to register.
>>   *
>>   * Returns the pointer to the new device
>>   */
>> -struct device *als_device_register(struct device *dev, char *name)
>> +struct device *als_device_register(struct device *dev)
> 
> This is a public function but you forgot to update
> include/linux/als_sys.h accordingly. This will let the build succeed
> but crashes will happen at run time. Fix below.
> 
>>  {
>> -	return device_create(als_class, dev, MKDEV(0, 0), NULL, name);
>> +	int id, err;
>> +	struct device *alsdev;
>> +
>> +again:
>> +	if (unlikely(idr_pre_get(&als_idr, GFP_KERNEL) == 0))
>> +		return ERR_PTR(-ENOMEM);
>> +
>> +	spin_lock(&idr_lock);
>> +	err = idr_get_new(&als_idr, NULL, &id);
>> +	spin_unlock(&idr_lock);
>> +
>> +	if (unlikely(err == -EAGAIN))
>> +		goto again;
>> +	else if (unlikely(err))
>> +		return ERR_PTR(err);
>> +
>> +	id = id & MAX_ID_MASK;
>> +	alsdev = device_create(als_class, dev, MKDEV(0, 0), NULL,
>> +			ALS_ID_FORMAT, id);
>> +
>> +	if (IS_ERR(alsdev)) {
>> +		spin_lock(&idr_lock);
>> +		idr_remove(&als_idr, id);
>> +		spin_unlock(&idr_lock);
>> +	}
>> +
>> +	return alsdev;
>>  }
>>  EXPORT_SYMBOL(als_device_register);
>>  
>> @@ -51,7 +84,16 @@ EXPORT_SYMBOL(als_device_register);
>>   */
>>  void als_device_unregister(struct device *dev)
>>  {
>> -	device_unregister(dev);
>> +	int id;
>> +
>> +	if (likely(sscanf(dev_name(dev), ALS_ID_FORMAT, &id) == 1)) {
>> +		device_unregister(dev);
>> +		spin_lock(&idr_lock);
>> +		idr_remove(&als_idr, id);
>> +		spin_unlock(&idr_lock);
>> +	} else
>> +		dev_dbg(dev->parent,
>> +			"als_device_unregister() failed: bad class ID!\n");
>>  }
>>  EXPORT_SYMBOL(als_device_unregister);
>>  
> 
> Other than that I am very happy with this change, which kills 46 lines
> of code in the tsl2550 driver (and virtually the same in every other
> light sensor driver.) Please merge the following fix:
> 
> --- linux-2.6.32-rc8.orig/include/linux/als_sys.h	2009-11-26 15:32:38.000000000 +0100
> +++ linux-2.6.32-rc8/include/linux/als_sys.h	2009-11-26 15:44:08.000000000 +0100
> @@ -29,7 +29,7 @@
>  #define ALS_ILLUMINANCE_MIN 0
>  #define ALS_ILLUMINANCE_MAX -1
>  
> -struct device *als_device_register(struct device *dev, char *name);
> +struct device *als_device_register(struct device *dev);
>  void als_device_unregister(struct device *dev);
>  
>  #endif /* __ALS_SYS_H__ */
> 
> 
> And then you can add:
> 
> Acked-by: Jean Delvare <khali@linux-fr.org>
> 
> That being said... If we want user-space to know what device is there,
> we may want to still let drivers pass a name string to
> als_device_register() and let the ALS core create a "name" sysfs
> attribute returning the string in question. This would be much lighter
> (for individual drivers) than the previous situation, as the string in
> question would be a constant (e.g. "TSL2550".) Opinions?
> 
Makes sense given we want all drivers to support some form of identification.
We could do it by stating they will all have that attribute, but given it's constant
will save repetition to put it in the driver. Conversely it might complicate the handling
of subsequent attribute_groups so I'd probably favour adding relevant documentation lines
and leaving it up to the drivers to implement this attribute.

Thus we'd require (within reason) all drivers to have illuminance0 and name.

Hence,
Acked-by: Jonathan Cameron <jic23@cam.ac.uk>

Thanks for doing this!

Jonathan



  reply	other threads:[~2009-11-26 17:19 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-11-26 12:04 [PATCH 0/2] Introduding the Ambient Light Sensor (ALS) class Amit Kucheria
2009-11-26 12:06 ` [PATCH 1/2] introduce ALS sysfs class Amit Kucheria
2009-11-26 12:25   ` Jonathan Cameron
2009-11-26 12:27     ` Jonathan Cameron
2009-11-26 12:06 ` [PATCH 2/2] als: add unique device-ids to the als device class Amit Kucheria
2009-11-26 15:07   ` Jean Delvare
2009-11-26 17:19     ` Jonathan Cameron [this message]
2009-11-26 18:06       ` Greg KH
2009-11-26 18:40         ` Jonathan Cameron
2009-12-04  5:20           ` Greg KH
2009-11-26 18:54         ` Jean Delvare
  -- strict thread matches above, loose matches on Subject: below --
2009-11-30 11:45 [PATCHv2 0/2] Introducing the Ambient Light Sensor (ALS) class Amit Kucheria
2009-11-30 11:46 ` [PATCH 2/2] als: add unique device-ids to the als device class Amit Kucheria

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4B0EB891.4010309@cam.ac.uk \
    --to=jic23@cam.ac.uk \
    --cc=alan@linux.intel.com \
    --cc=amit.kucheria@verdurent.com \
    --cc=gregkh@suse.de \
    --cc=khali@linux-fr.org \
    --cc=linux-acpi@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rui.zhang@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox