2019-05-27 14:55:05 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2010-07-06 22:08:46 +08:00
|
|
|
/*
|
|
|
|
* leds-ns2.c - Driver for the Network Space v2 (and parents) dual-GPIO LED
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 LaCie
|
|
|
|
*
|
|
|
|
* Author: Simon Guinot <sguinot@lacie.com>
|
|
|
|
*
|
|
|
|
* Based on leds-gpio.c by Raphael Assenat <raph@8d.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/slab.h>
|
2020-01-07 22:10:28 +08:00
|
|
|
#include <linux/gpio/consumer.h>
|
2010-07-06 22:08:46 +08:00
|
|
|
#include <linux/leds.h>
|
2011-07-04 01:56:03 +08:00
|
|
|
#include <linux/module.h>
|
2013-09-28 19:38:30 +08:00
|
|
|
#include <linux/of.h>
|
2015-07-03 01:56:42 +08:00
|
|
|
#include "leds.h"
|
2010-07-06 22:08:46 +08:00
|
|
|
|
2020-01-07 22:10:28 +08:00
|
|
|
enum ns2_led_modes {
|
|
|
|
NS_V2_LED_OFF,
|
|
|
|
NS_V2_LED_ON,
|
|
|
|
NS_V2_LED_SATA,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ns2_led_modval {
|
|
|
|
enum ns2_led_modes mode;
|
|
|
|
int cmd_level;
|
|
|
|
int slow_level;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ns2_led {
|
|
|
|
const char *name;
|
|
|
|
const char *default_trigger;
|
2020-01-07 22:10:29 +08:00
|
|
|
struct gpio_desc *cmd;
|
|
|
|
struct gpio_desc *slow;
|
2020-01-07 22:10:28 +08:00
|
|
|
int num_modes;
|
|
|
|
struct ns2_led_modval *modval;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ns2_led_platform_data {
|
|
|
|
int num_leds;
|
|
|
|
struct ns2_led *leds;
|
|
|
|
};
|
|
|
|
|
2010-07-06 22:08:46 +08:00
|
|
|
/*
|
2015-07-03 01:56:40 +08:00
|
|
|
* The Network Space v2 dual-GPIO LED is wired to a CPLD. Three different LED
|
|
|
|
* modes are available: off, on and SATA activity blinking. The LED modes are
|
|
|
|
* controlled through two GPIOs (command and slow): each combination of values
|
|
|
|
* for the command/slow GPIOs corresponds to a LED mode.
|
2010-07-06 22:08:46 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
struct ns2_led_data {
|
|
|
|
struct led_classdev cdev;
|
2020-01-07 22:10:29 +08:00
|
|
|
struct gpio_desc *cmd;
|
|
|
|
struct gpio_desc *slow;
|
2015-07-03 01:56:42 +08:00
|
|
|
bool can_sleep;
|
2010-07-06 22:08:46 +08:00
|
|
|
unsigned char sata; /* True when SATA mode active. */
|
|
|
|
rwlock_t rw_lock; /* Lock GPIOs. */
|
2015-07-03 01:56:40 +08:00
|
|
|
int num_modes;
|
|
|
|
struct ns2_led_modval *modval;
|
2010-07-06 22:08:46 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static int ns2_led_get_mode(struct ns2_led_data *led_dat,
|
|
|
|
enum ns2_led_modes *mode)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int ret = -EINVAL;
|
|
|
|
int cmd_level;
|
|
|
|
int slow_level;
|
|
|
|
|
2020-01-07 22:10:29 +08:00
|
|
|
cmd_level = gpiod_get_value_cansleep(led_dat->cmd);
|
|
|
|
slow_level = gpiod_get_value_cansleep(led_dat->slow);
|
2010-07-06 22:08:46 +08:00
|
|
|
|
2015-07-03 01:56:40 +08:00
|
|
|
for (i = 0; i < led_dat->num_modes; i++) {
|
|
|
|
if (cmd_level == led_dat->modval[i].cmd_level &&
|
|
|
|
slow_level == led_dat->modval[i].slow_level) {
|
|
|
|
*mode = led_dat->modval[i].mode;
|
2010-07-06 22:08:46 +08:00
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ns2_led_set_mode(struct ns2_led_data *led_dat,
|
|
|
|
enum ns2_led_modes mode)
|
|
|
|
{
|
|
|
|
int i;
|
2015-07-03 01:56:42 +08:00
|
|
|
bool found = false;
|
2010-09-19 21:30:59 +08:00
|
|
|
unsigned long flags;
|
2010-07-06 22:08:46 +08:00
|
|
|
|
2015-07-03 01:56:42 +08:00
|
|
|
for (i = 0; i < led_dat->num_modes; i++)
|
2015-07-03 01:56:40 +08:00
|
|
|
if (mode == led_dat->modval[i].mode) {
|
2015-07-03 01:56:42 +08:00
|
|
|
found = true;
|
|
|
|
break;
|
2010-07-06 22:08:46 +08:00
|
|
|
}
|
2015-07-03 01:56:42 +08:00
|
|
|
|
|
|
|
if (!found)
|
|
|
|
return;
|
|
|
|
|
|
|
|
write_lock_irqsave(&led_dat->rw_lock, flags);
|
|
|
|
|
|
|
|
if (!led_dat->can_sleep) {
|
2020-01-07 22:10:29 +08:00
|
|
|
gpiod_set_value(led_dat->cmd,
|
|
|
|
led_dat->modval[i].cmd_level);
|
|
|
|
gpiod_set_value(led_dat->slow,
|
|
|
|
led_dat->modval[i].slow_level);
|
2015-07-03 01:56:42 +08:00
|
|
|
goto exit_unlock;
|
2010-07-06 22:08:46 +08:00
|
|
|
}
|
|
|
|
|
2020-01-07 22:10:29 +08:00
|
|
|
gpiod_set_value_cansleep(led_dat->cmd, led_dat->modval[i].cmd_level);
|
|
|
|
gpiod_set_value_cansleep(led_dat->slow, led_dat->modval[i].slow_level);
|
2015-07-03 01:56:42 +08:00
|
|
|
|
|
|
|
exit_unlock:
|
2010-09-19 21:30:59 +08:00
|
|
|
write_unlock_irqrestore(&led_dat->rw_lock, flags);
|
2010-07-06 22:08:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ns2_led_set(struct led_classdev *led_cdev,
|
|
|
|
enum led_brightness value)
|
|
|
|
{
|
|
|
|
struct ns2_led_data *led_dat =
|
|
|
|
container_of(led_cdev, struct ns2_led_data, cdev);
|
|
|
|
enum ns2_led_modes mode;
|
|
|
|
|
|
|
|
if (value == LED_OFF)
|
|
|
|
mode = NS_V2_LED_OFF;
|
|
|
|
else if (led_dat->sata)
|
|
|
|
mode = NS_V2_LED_SATA;
|
|
|
|
else
|
|
|
|
mode = NS_V2_LED_ON;
|
|
|
|
|
|
|
|
ns2_led_set_mode(led_dat, mode);
|
|
|
|
}
|
|
|
|
|
2015-11-20 18:39:41 +08:00
|
|
|
static int ns2_led_set_blocking(struct led_classdev *led_cdev,
|
|
|
|
enum led_brightness value)
|
|
|
|
{
|
|
|
|
ns2_led_set(led_cdev, value);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-07-06 22:08:46 +08:00
|
|
|
static ssize_t ns2_led_sata_store(struct device *dev,
|
|
|
|
struct device_attribute *attr,
|
|
|
|
const char *buff, size_t count)
|
|
|
|
{
|
2010-10-07 22:35:40 +08:00
|
|
|
struct led_classdev *led_cdev = dev_get_drvdata(dev);
|
|
|
|
struct ns2_led_data *led_dat =
|
|
|
|
container_of(led_cdev, struct ns2_led_data, cdev);
|
2010-07-06 22:08:46 +08:00
|
|
|
int ret;
|
|
|
|
unsigned long enable;
|
|
|
|
|
2012-10-23 20:25:35 +08:00
|
|
|
ret = kstrtoul(buff, 10, &enable);
|
2010-07-06 22:08:46 +08:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
enable = !!enable;
|
|
|
|
|
|
|
|
if (led_dat->sata == enable)
|
2015-07-03 01:56:42 +08:00
|
|
|
goto exit;
|
2010-07-06 22:08:46 +08:00
|
|
|
|
2015-07-03 01:56:42 +08:00
|
|
|
led_dat->sata = enable;
|
|
|
|
|
|
|
|
if (!led_get_brightness(led_cdev))
|
|
|
|
goto exit;
|
2010-07-06 22:08:46 +08:00
|
|
|
|
2015-07-03 01:56:42 +08:00
|
|
|
if (enable)
|
2010-07-06 22:08:46 +08:00
|
|
|
ns2_led_set_mode(led_dat, NS_V2_LED_SATA);
|
2015-07-03 01:56:42 +08:00
|
|
|
else
|
2010-07-06 22:08:46 +08:00
|
|
|
ns2_led_set_mode(led_dat, NS_V2_LED_ON);
|
|
|
|
|
2015-07-03 01:56:42 +08:00
|
|
|
exit:
|
2010-07-06 22:08:46 +08:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t ns2_led_sata_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
2010-10-07 22:35:40 +08:00
|
|
|
struct led_classdev *led_cdev = dev_get_drvdata(dev);
|
|
|
|
struct ns2_led_data *led_dat =
|
|
|
|
container_of(led_cdev, struct ns2_led_data, cdev);
|
2010-07-06 22:08:46 +08:00
|
|
|
|
|
|
|
return sprintf(buf, "%d\n", led_dat->sata);
|
|
|
|
}
|
|
|
|
|
|
|
|
static DEVICE_ATTR(sata, 0644, ns2_led_sata_show, ns2_led_sata_store);
|
|
|
|
|
2014-06-26 01:08:51 +08:00
|
|
|
static struct attribute *ns2_led_attrs[] = {
|
|
|
|
&dev_attr_sata.attr,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
ATTRIBUTE_GROUPS(ns2_led);
|
|
|
|
|
2012-11-20 02:23:02 +08:00
|
|
|
static int
|
2010-07-06 22:08:46 +08:00
|
|
|
create_ns2_led(struct platform_device *pdev, struct ns2_led_data *led_dat,
|
|
|
|
const struct ns2_led *template)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
enum ns2_led_modes mode;
|
|
|
|
|
|
|
|
rwlock_init(&led_dat->rw_lock);
|
|
|
|
|
|
|
|
led_dat->cdev.name = template->name;
|
|
|
|
led_dat->cdev.default_trigger = template->default_trigger;
|
|
|
|
led_dat->cdev.blink_set = NULL;
|
|
|
|
led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
|
2014-06-26 01:08:51 +08:00
|
|
|
led_dat->cdev.groups = ns2_led_groups;
|
2010-07-06 22:08:46 +08:00
|
|
|
led_dat->cmd = template->cmd;
|
|
|
|
led_dat->slow = template->slow;
|
2020-01-07 22:10:29 +08:00
|
|
|
led_dat->can_sleep = gpiod_cansleep(led_dat->cmd) |
|
|
|
|
gpiod_cansleep(led_dat->slow);
|
2015-11-20 18:39:41 +08:00
|
|
|
if (led_dat->can_sleep)
|
|
|
|
led_dat->cdev.brightness_set_blocking = ns2_led_set_blocking;
|
|
|
|
else
|
|
|
|
led_dat->cdev.brightness_set = ns2_led_set;
|
2015-07-03 01:56:40 +08:00
|
|
|
led_dat->modval = template->modval;
|
|
|
|
led_dat->num_modes = template->num_modes;
|
2010-07-06 22:08:46 +08:00
|
|
|
|
|
|
|
ret = ns2_led_get_mode(led_dat, &mode);
|
|
|
|
if (ret < 0)
|
2012-11-25 12:58:10 +08:00
|
|
|
return ret;
|
2010-07-06 22:08:46 +08:00
|
|
|
|
|
|
|
/* Set LED initial state. */
|
|
|
|
led_dat->sata = (mode == NS_V2_LED_SATA) ? 1 : 0;
|
|
|
|
led_dat->cdev.brightness =
|
|
|
|
(mode == NS_V2_LED_OFF) ? LED_OFF : LED_FULL;
|
|
|
|
|
|
|
|
ret = led_classdev_register(&pdev->dev, &led_dat->cdev);
|
|
|
|
if (ret < 0)
|
2012-11-25 12:58:10 +08:00
|
|
|
return ret;
|
2010-07-06 22:08:46 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-05-11 04:01:46 +08:00
|
|
|
static void delete_ns2_led(struct ns2_led_data *led_dat)
|
2010-07-06 22:08:46 +08:00
|
|
|
{
|
|
|
|
led_classdev_unregister(&led_dat->cdev);
|
|
|
|
}
|
|
|
|
|
2012-10-17 18:09:03 +08:00
|
|
|
#ifdef CONFIG_OF_GPIO
|
|
|
|
/*
|
|
|
|
* Translate OpenFirmware node properties into platform_data.
|
|
|
|
*/
|
2012-12-13 04:14:06 +08:00
|
|
|
static int
|
2012-10-17 18:09:03 +08:00
|
|
|
ns2_leds_get_of_pdata(struct device *dev, struct ns2_led_platform_data *pdata)
|
|
|
|
{
|
|
|
|
struct device_node *np = dev->of_node;
|
|
|
|
struct device_node *child;
|
2015-07-03 01:56:40 +08:00
|
|
|
struct ns2_led *led, *leds;
|
2019-07-16 15:24:24 +08:00
|
|
|
int ret, num_leds = 0;
|
2012-10-17 18:09:03 +08:00
|
|
|
|
|
|
|
num_leds = of_get_child_count(np);
|
|
|
|
if (!num_leds)
|
|
|
|
return -ENODEV;
|
|
|
|
|
treewide: devm_kzalloc() -> devm_kcalloc()
The devm_kzalloc() function has a 2-factor argument form, devm_kcalloc().
This patch replaces cases of:
devm_kzalloc(handle, a * b, gfp)
with:
devm_kcalloc(handle, a * b, gfp)
as well as handling cases of:
devm_kzalloc(handle, a * b * c, gfp)
with:
devm_kzalloc(handle, array3_size(a, b, c), gfp)
as it's slightly less ugly than:
devm_kcalloc(handle, array_size(a, b), c, gfp)
This does, however, attempt to ignore constant size factors like:
devm_kzalloc(handle, 4 * 1024, gfp)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
Some manual whitespace fixes were needed in this patch, as Coccinelle
really liked to write "=devm_kcalloc..." instead of "= devm_kcalloc...".
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
expression HANDLE;
type TYPE;
expression THING, E;
@@
(
devm_kzalloc(HANDLE,
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
devm_kzalloc(HANDLE,
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression HANDLE;
expression COUNT;
typedef u8;
typedef __u8;
@@
(
devm_kzalloc(HANDLE,
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(char) * COUNT
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
expression HANDLE;
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
expression HANDLE;
identifier SIZE, COUNT;
@@
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression HANDLE;
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
devm_kzalloc(HANDLE,
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression HANDLE;
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
expression HANDLE;
identifier STRIDE, SIZE, COUNT;
@@
(
devm_kzalloc(HANDLE,
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression HANDLE;
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
devm_kzalloc(HANDLE, C1 * C2 * C3, ...)
|
devm_kzalloc(HANDLE,
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
devm_kzalloc(HANDLE,
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
devm_kzalloc(HANDLE,
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
devm_kzalloc(HANDLE,
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression HANDLE;
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
devm_kzalloc(HANDLE, sizeof(THING) * C2, ...)
|
devm_kzalloc(HANDLE, sizeof(TYPE) * C2, ...)
|
devm_kzalloc(HANDLE, C1 * C2 * C3, ...)
|
devm_kzalloc(HANDLE, C1 * C2, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- (E1) * E2
+ E1, E2
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- (E1) * (E2)
+ E1, E2
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-13 05:07:58 +08:00
|
|
|
leds = devm_kcalloc(dev, num_leds, sizeof(struct ns2_led),
|
2012-10-17 18:09:03 +08:00
|
|
|
GFP_KERNEL);
|
|
|
|
if (!leds)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2015-07-03 01:56:40 +08:00
|
|
|
led = leds;
|
2012-10-17 18:09:03 +08:00
|
|
|
for_each_child_of_node(np, child) {
|
|
|
|
const char *string;
|
2019-07-16 15:24:24 +08:00
|
|
|
int i, num_modes;
|
2015-07-03 01:56:40 +08:00
|
|
|
struct ns2_led_modval *modval;
|
2020-01-07 22:10:29 +08:00
|
|
|
struct gpio_desc *gd;
|
2012-10-17 18:09:03 +08:00
|
|
|
|
|
|
|
ret = of_property_read_string(child, "label", &string);
|
2015-07-03 01:56:40 +08:00
|
|
|
led->name = (ret == 0) ? string : child->name;
|
2020-01-07 22:10:29 +08:00
|
|
|
|
|
|
|
gd = gpiod_get_from_of_node(child, "cmd-gpio", 0,
|
|
|
|
GPIOD_ASIS, led->name);
|
|
|
|
if (IS_ERR(gd)) {
|
|
|
|
ret = PTR_ERR(gd);
|
|
|
|
goto err_node_put;
|
|
|
|
}
|
|
|
|
led->cmd = gd;
|
|
|
|
gd = gpiod_get_from_of_node(child, "slow-gpio", 0,
|
|
|
|
GPIOD_ASIS, led->name);
|
|
|
|
if (IS_ERR(gd)) {
|
|
|
|
ret = PTR_ERR(gd);
|
|
|
|
goto err_node_put;
|
|
|
|
}
|
|
|
|
led->slow = gd;
|
|
|
|
|
2012-10-17 18:09:03 +08:00
|
|
|
ret = of_property_read_string(child, "linux,default-trigger",
|
|
|
|
&string);
|
|
|
|
if (ret == 0)
|
2015-07-03 01:56:40 +08:00
|
|
|
led->default_trigger = string;
|
|
|
|
|
|
|
|
ret = of_property_count_u32_elems(child, "modes-map");
|
|
|
|
if (ret < 0 || ret % 3) {
|
|
|
|
dev_err(dev,
|
|
|
|
"Missing or malformed modes-map property\n");
|
2019-07-16 15:24:24 +08:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto err_node_put;
|
2015-07-03 01:56:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
num_modes = ret / 3;
|
treewide: devm_kzalloc() -> devm_kcalloc()
The devm_kzalloc() function has a 2-factor argument form, devm_kcalloc().
This patch replaces cases of:
devm_kzalloc(handle, a * b, gfp)
with:
devm_kcalloc(handle, a * b, gfp)
as well as handling cases of:
devm_kzalloc(handle, a * b * c, gfp)
with:
devm_kzalloc(handle, array3_size(a, b, c), gfp)
as it's slightly less ugly than:
devm_kcalloc(handle, array_size(a, b), c, gfp)
This does, however, attempt to ignore constant size factors like:
devm_kzalloc(handle, 4 * 1024, gfp)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
Some manual whitespace fixes were needed in this patch, as Coccinelle
really liked to write "=devm_kcalloc..." instead of "= devm_kcalloc...".
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
expression HANDLE;
type TYPE;
expression THING, E;
@@
(
devm_kzalloc(HANDLE,
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
devm_kzalloc(HANDLE,
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression HANDLE;
expression COUNT;
typedef u8;
typedef __u8;
@@
(
devm_kzalloc(HANDLE,
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(char) * COUNT
+ COUNT
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
expression HANDLE;
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
expression HANDLE;
identifier SIZE, COUNT;
@@
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression HANDLE;
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
devm_kzalloc(HANDLE,
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression HANDLE;
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
devm_kzalloc(HANDLE,
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
expression HANDLE;
identifier STRIDE, SIZE, COUNT;
@@
(
devm_kzalloc(HANDLE,
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
devm_kzalloc(HANDLE,
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression HANDLE;
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
devm_kzalloc(HANDLE, C1 * C2 * C3, ...)
|
devm_kzalloc(HANDLE,
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
devm_kzalloc(HANDLE,
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
devm_kzalloc(HANDLE,
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
devm_kzalloc(HANDLE,
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression HANDLE;
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
devm_kzalloc(HANDLE, sizeof(THING) * C2, ...)
|
devm_kzalloc(HANDLE, sizeof(TYPE) * C2, ...)
|
devm_kzalloc(HANDLE, C1 * C2 * C3, ...)
|
devm_kzalloc(HANDLE, C1 * C2, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- (E1) * E2
+ E1, E2
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- (E1) * (E2)
+ E1, E2
, ...)
|
- devm_kzalloc
+ devm_kcalloc
(HANDLE,
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-13 05:07:58 +08:00
|
|
|
modval = devm_kcalloc(dev,
|
|
|
|
num_modes,
|
|
|
|
sizeof(struct ns2_led_modval),
|
2015-07-03 01:56:40 +08:00
|
|
|
GFP_KERNEL);
|
2019-07-16 15:24:24 +08:00
|
|
|
if (!modval) {
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto err_node_put;
|
|
|
|
}
|
2015-07-03 01:56:40 +08:00
|
|
|
|
|
|
|
for (i = 0; i < num_modes; i++) {
|
|
|
|
of_property_read_u32_index(child,
|
|
|
|
"modes-map", 3 * i,
|
|
|
|
(u32 *) &modval[i].mode);
|
|
|
|
of_property_read_u32_index(child,
|
|
|
|
"modes-map", 3 * i + 1,
|
|
|
|
(u32 *) &modval[i].cmd_level);
|
|
|
|
of_property_read_u32_index(child,
|
|
|
|
"modes-map", 3 * i + 2,
|
|
|
|
(u32 *) &modval[i].slow_level);
|
|
|
|
}
|
|
|
|
|
|
|
|
led->num_modes = num_modes;
|
|
|
|
led->modval = modval;
|
2012-10-17 18:09:03 +08:00
|
|
|
|
2015-07-03 01:56:40 +08:00
|
|
|
led++;
|
2012-10-17 18:09:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pdata->leds = leds;
|
|
|
|
pdata->num_leds = num_leds;
|
|
|
|
|
|
|
|
return 0;
|
2019-07-16 15:24:24 +08:00
|
|
|
|
|
|
|
err_node_put:
|
|
|
|
of_node_put(child);
|
|
|
|
return ret;
|
2012-10-17 18:09:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct of_device_id of_ns2_leds_match[] = {
|
|
|
|
{ .compatible = "lacie,ns2-leds", },
|
|
|
|
{},
|
|
|
|
};
|
2015-09-02 05:36:59 +08:00
|
|
|
MODULE_DEVICE_TABLE(of, of_ns2_leds_match);
|
2012-10-17 18:09:03 +08:00
|
|
|
#endif /* CONFIG_OF_GPIO */
|
|
|
|
|
2013-03-20 02:07:29 +08:00
|
|
|
struct ns2_led_priv {
|
|
|
|
int num_leds;
|
|
|
|
struct ns2_led_data leds_data[];
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline int sizeof_ns2_led_priv(int num_leds)
|
|
|
|
{
|
|
|
|
return sizeof(struct ns2_led_priv) +
|
|
|
|
(sizeof(struct ns2_led_data) * num_leds);
|
|
|
|
}
|
|
|
|
|
2012-11-20 02:23:02 +08:00
|
|
|
static int ns2_led_probe(struct platform_device *pdev)
|
2010-07-06 22:08:46 +08:00
|
|
|
{
|
2013-07-30 16:07:35 +08:00
|
|
|
struct ns2_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
|
2013-03-20 02:07:29 +08:00
|
|
|
struct ns2_led_priv *priv;
|
2010-07-06 22:08:46 +08:00
|
|
|
int i;
|
|
|
|
int ret;
|
|
|
|
|
2012-10-17 18:09:03 +08:00
|
|
|
#ifdef CONFIG_OF_GPIO
|
|
|
|
if (!pdata) {
|
|
|
|
pdata = devm_kzalloc(&pdev->dev,
|
|
|
|
sizeof(struct ns2_led_platform_data),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (!pdata)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
ret = ns2_leds_get_of_pdata(&pdev->dev, pdata);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#else
|
2010-07-06 22:08:46 +08:00
|
|
|
if (!pdata)
|
|
|
|
return -EINVAL;
|
2012-10-17 18:09:03 +08:00
|
|
|
#endif /* CONFIG_OF_GPIO */
|
2010-07-06 22:08:46 +08:00
|
|
|
|
2013-03-20 02:07:29 +08:00
|
|
|
priv = devm_kzalloc(&pdev->dev,
|
|
|
|
sizeof_ns2_led_priv(pdata->num_leds), GFP_KERNEL);
|
|
|
|
if (!priv)
|
2010-07-06 22:08:46 +08:00
|
|
|
return -ENOMEM;
|
2013-03-20 02:07:29 +08:00
|
|
|
priv->num_leds = pdata->num_leds;
|
2010-07-06 22:08:46 +08:00
|
|
|
|
2013-03-20 02:07:29 +08:00
|
|
|
for (i = 0; i < priv->num_leds; i++) {
|
|
|
|
ret = create_ns2_led(pdev, &priv->leds_data[i],
|
|
|
|
&pdata->leds[i]);
|
2012-07-04 12:30:50 +08:00
|
|
|
if (ret < 0) {
|
|
|
|
for (i = i - 1; i >= 0; i--)
|
2013-03-20 02:07:29 +08:00
|
|
|
delete_ns2_led(&priv->leds_data[i]);
|
2012-07-04 12:30:50 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2010-07-06 22:08:46 +08:00
|
|
|
}
|
|
|
|
|
2013-03-20 02:07:29 +08:00
|
|
|
platform_set_drvdata(pdev, priv);
|
2010-07-06 22:08:46 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-20 02:26:00 +08:00
|
|
|
static int ns2_led_remove(struct platform_device *pdev)
|
2010-07-06 22:08:46 +08:00
|
|
|
{
|
|
|
|
int i;
|
2013-03-20 02:07:29 +08:00
|
|
|
struct ns2_led_priv *priv;
|
2010-07-06 22:08:46 +08:00
|
|
|
|
2013-03-20 02:07:29 +08:00
|
|
|
priv = platform_get_drvdata(pdev);
|
2010-07-06 22:08:46 +08:00
|
|
|
|
2013-03-20 02:07:29 +08:00
|
|
|
for (i = 0; i < priv->num_leds; i++)
|
|
|
|
delete_ns2_led(&priv->leds_data[i]);
|
2010-07-06 22:08:46 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct platform_driver ns2_led_driver = {
|
|
|
|
.probe = ns2_led_probe,
|
2012-11-20 02:20:20 +08:00
|
|
|
.remove = ns2_led_remove,
|
2010-07-06 22:08:46 +08:00
|
|
|
.driver = {
|
2012-10-17 18:09:03 +08:00
|
|
|
.name = "leds-ns2",
|
|
|
|
.of_match_table = of_match_ptr(of_ns2_leds_match),
|
2010-07-06 22:08:46 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2012-01-11 07:09:24 +08:00
|
|
|
module_platform_driver(ns2_led_driver);
|
2010-07-06 22:08:46 +08:00
|
|
|
|
|
|
|
MODULE_AUTHOR("Simon Guinot <sguinot@lacie.com>");
|
|
|
|
MODULE_DESCRIPTION("Network Space v2 LED driver");
|
|
|
|
MODULE_LICENSE("GPL");
|
2012-01-11 07:09:24 +08:00
|
|
|
MODULE_ALIAS("platform:leds-ns2");
|