Re: [PATCH v3] leds: add LM3533 LED driver
From: Andrew Morton
Date: Fri May 11 2012 - 18:24:35 EST
On Fri, 11 May 2012 11:54:11 +0200
Johan Hovold <jhovold@xxxxxxxxx> wrote:
> On Thu, May 10, 2012 at 11:48:17AM -0700, Andrew Morton wrote:
> > On Thu, 10 May 2012 20:27:05 +0200
> > Johan Hovold <jhovold@xxxxxxxxx> wrote:
> >
> > > Add sub-driver for the LEDs on National Semiconductor / TI LM3533
> > > lighting power chips.
> > >
> > > The chip provides 256 brightness levels, hardware accelerated blinking
> > > as well as ambient-light-sensor and pwm input control.
> > >
> > > ...
> > >
> > > +#define to_lm3533_led(_cdev) \
> > > + container_of(_cdev, struct lm3533_led, cdev)
> >
> > Minor thing: container_of() is not fully type-safe: it can be passed
> > the address of any struct which contains a field called cdev and will
> > return a struct lm3533_led* (or something like that - it has holes...).
> >
> > A way to fix that is to wrap container_of() in a real C function, not a
> > macro:
> >
> > static inline struct lm3533_led *to_lm3533_led(struct struct led_classdev *cdev)
> > {
> > return container_of(_cdev, struct lm3533_led, cdev);
> > }
> >
> > This has been another episode in the ongoing series "macros are always
> > wrong" :)
>
> Fair enough. :) Seems like the vast majority of drivers still use
> convenience macros such as the this one for this kind of use (where the
> functions are either passed the class device or it is retrieved through
> device driver data).
>
> Do you want me to replace the other three instances of container_of
> convenience macros in the iio-subdriver and core (already added to the
> mfd tree) as well?
Well, it does result in better code. How could I say no? ;)
> > > +static ssize_t store_als(struct device *dev,
> > > + struct device_attribute *attr,
> > > + const char *buf, size_t len)
> > > +{
> > > + struct led_classdev *led_cdev = dev_get_drvdata(dev);
> > > + struct lm3533_led *led = to_lm3533_led(led_cdev);
> > > + u8 als;
> > > + u8 reg;
> > > + u8 mask;
> > > + int ret;
> > > +
> > > + if (kstrtou8(buf, 0, &als))
> > > + return -EINVAL;
> > > +
> > > + if (als != 0 && (als < LM3533_ALS_LV_MIN || als > LM3533_ALS_LV_MAX))
> > > + return -EINVAL;
> >
> > The `als != 0' test doesn't do anything, and looks odd. Is there some
> > magical reason why als==0 would be illegal even if LM3533_ALS_LV_MIN
> > was negative? If so, it should be documented.
>
> The non-zero-test is not redundant as 0 is the only valid input outside
> of [LV_MIN,LV_MAX] (in fact, the only three valid values are 0,2 and 3).
ah, OK. One day I'll get the hang of this C thingy.
> Would you prefer
>
> if ((als < LM3533_ALS_LV_MIN && als != 0) || als > LM3533_ALS_LV_MAX)
> return -EINVAL;
>
> or nested conditionals? Or should I simply add a comment?
A comment would be nice. That 0 is also permitted is a surprise.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/