2017-11-07 20:58:41 +07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2016-04-23 23:47:24 +07:00
|
|
|
/*
|
|
|
|
* Greybus Module code
|
|
|
|
*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
* Copyright 2016 Linaro Ltd.
|
|
|
|
*/
|
|
|
|
|
2019-08-25 12:54:27 +07:00
|
|
|
#include <linux/greybus.h>
|
2016-05-24 11:05:31 +07:00
|
|
|
#include "greybus_trace.h"
|
2016-04-23 23:47:24 +07:00
|
|
|
|
2016-04-23 23:47:25 +07:00
|
|
|
static ssize_t eject_store(struct device *dev,
|
2018-11-25 23:58:15 +07:00
|
|
|
struct device_attribute *attr,
|
|
|
|
const char *buf, size_t len)
|
2016-04-23 23:47:25 +07:00
|
|
|
{
|
|
|
|
struct gb_module *module = to_gb_module(dev);
|
|
|
|
struct gb_interface *intf;
|
|
|
|
size_t i;
|
|
|
|
long val;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = kstrtol(buf, 0, &val);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (!val)
|
|
|
|
return len;
|
|
|
|
|
|
|
|
for (i = 0; i < module->num_interfaces; ++i) {
|
|
|
|
intf = module->interfaces[i];
|
|
|
|
|
|
|
|
mutex_lock(&intf->mutex);
|
|
|
|
/* Set flag to prevent concurrent activation. */
|
|
|
|
intf->ejected = true;
|
|
|
|
gb_interface_disable(intf);
|
|
|
|
gb_interface_deactivate(intf);
|
|
|
|
mutex_unlock(&intf->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Tell the SVC to eject the primary interface. */
|
|
|
|
ret = gb_svc_intf_eject(module->hd->svc, module->module_id);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
static DEVICE_ATTR_WO(eject);
|
|
|
|
|
2016-04-23 23:47:24 +07:00
|
|
|
static ssize_t module_id_show(struct device *dev,
|
2018-11-25 23:58:15 +07:00
|
|
|
struct device_attribute *attr, char *buf)
|
2016-04-23 23:47:24 +07:00
|
|
|
{
|
|
|
|
struct gb_module *module = to_gb_module(dev);
|
|
|
|
|
|
|
|
return sprintf(buf, "%u\n", module->module_id);
|
|
|
|
}
|
|
|
|
static DEVICE_ATTR_RO(module_id);
|
|
|
|
|
|
|
|
static ssize_t num_interfaces_show(struct device *dev,
|
2018-11-25 23:58:15 +07:00
|
|
|
struct device_attribute *attr, char *buf)
|
2016-04-23 23:47:24 +07:00
|
|
|
{
|
|
|
|
struct gb_module *module = to_gb_module(dev);
|
|
|
|
|
|
|
|
return sprintf(buf, "%zu\n", module->num_interfaces);
|
|
|
|
}
|
|
|
|
static DEVICE_ATTR_RO(num_interfaces);
|
|
|
|
|
|
|
|
static struct attribute *module_attrs[] = {
|
2016-04-23 23:47:25 +07:00
|
|
|
&dev_attr_eject.attr,
|
2016-04-23 23:47:24 +07:00
|
|
|
&dev_attr_module_id.attr,
|
|
|
|
&dev_attr_num_interfaces.attr,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
ATTRIBUTE_GROUPS(module);
|
|
|
|
|
|
|
|
static void gb_module_release(struct device *dev)
|
|
|
|
{
|
|
|
|
struct gb_module *module = to_gb_module(dev);
|
|
|
|
|
2016-05-24 11:05:31 +07:00
|
|
|
trace_gb_module_release(module);
|
|
|
|
|
2016-04-23 23:47:24 +07:00
|
|
|
kfree(module);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct device_type greybus_module_type = {
|
|
|
|
.name = "greybus_module",
|
|
|
|
.release = gb_module_release,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct gb_module *gb_module_create(struct gb_host_device *hd, u8 module_id,
|
2018-11-25 23:58:15 +07:00
|
|
|
size_t num_interfaces)
|
2016-04-23 23:47:24 +07:00
|
|
|
{
|
|
|
|
struct gb_interface *intf;
|
|
|
|
struct gb_module *module;
|
|
|
|
int i;
|
|
|
|
|
treewide: Use struct_size() for kmalloc()-family
One of the more common cases of allocation size calculations is finding
the size of a structure that has a zero-sized array at the end, along
with memory for some number of elements for that array. For example:
struct foo {
int stuff;
void *entry[];
};
instance = kmalloc(sizeof(struct foo) + sizeof(void *) * count, GFP_KERNEL);
Instead of leaving these open-coded and prone to type mistakes, we can
now use the new struct_size() helper:
instance = kmalloc(struct_size(instance, entry, count), GFP_KERNEL);
This patch makes the changes for kmalloc()-family (and kvmalloc()-family)
uses. It was done via automatic conversion with manual review for the
"CHECKME" non-standard cases noted below, using the following Coccinelle
script:
// pkey_cache = kmalloc(sizeof *pkey_cache + tprops->pkey_tbl_len *
// sizeof *pkey_cache->table, GFP_KERNEL);
@@
identifier alloc =~ "kmalloc|kzalloc|kvmalloc|kvzalloc";
expression GFP;
identifier VAR, ELEMENT;
expression COUNT;
@@
- alloc(sizeof(*VAR) + COUNT * sizeof(*VAR->ELEMENT), GFP)
+ alloc(struct_size(VAR, ELEMENT, COUNT), GFP)
// mr = kzalloc(sizeof(*mr) + m * sizeof(mr->map[0]), GFP_KERNEL);
@@
identifier alloc =~ "kmalloc|kzalloc|kvmalloc|kvzalloc";
expression GFP;
identifier VAR, ELEMENT;
expression COUNT;
@@
- alloc(sizeof(*VAR) + COUNT * sizeof(VAR->ELEMENT[0]), GFP)
+ alloc(struct_size(VAR, ELEMENT, COUNT), GFP)
// Same pattern, but can't trivially locate the trailing element name,
// or variable name.
@@
identifier alloc =~ "kmalloc|kzalloc|kvmalloc|kvzalloc";
expression GFP;
expression SOMETHING, COUNT, ELEMENT;
@@
- alloc(sizeof(SOMETHING) + COUNT * sizeof(ELEMENT), GFP)
+ alloc(CHECKME_struct_size(&SOMETHING, ELEMENT, COUNT), GFP)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-05-09 03:45:50 +07:00
|
|
|
module = kzalloc(struct_size(module, interfaces, num_interfaces),
|
|
|
|
GFP_KERNEL);
|
2016-04-23 23:47:24 +07:00
|
|
|
if (!module)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
module->hd = hd;
|
|
|
|
module->module_id = module_id;
|
|
|
|
module->num_interfaces = num_interfaces;
|
|
|
|
|
|
|
|
module->dev.parent = &hd->dev;
|
|
|
|
module->dev.bus = &greybus_bus_type;
|
|
|
|
module->dev.type = &greybus_module_type;
|
|
|
|
module->dev.groups = module_groups;
|
|
|
|
module->dev.dma_mask = hd->dev.dma_mask;
|
|
|
|
device_initialize(&module->dev);
|
|
|
|
dev_set_name(&module->dev, "%d-%u", hd->bus_id, module_id);
|
|
|
|
|
2016-05-24 11:05:31 +07:00
|
|
|
trace_gb_module_create(module);
|
|
|
|
|
2016-04-23 23:47:24 +07:00
|
|
|
for (i = 0; i < num_interfaces; ++i) {
|
|
|
|
intf = gb_interface_create(module, module_id + i);
|
|
|
|
if (!intf) {
|
|
|
|
dev_err(&module->dev, "failed to create interface %u\n",
|
2018-11-25 23:58:15 +07:00
|
|
|
module_id + i);
|
2016-04-23 23:47:24 +07:00
|
|
|
goto err_put_interfaces;
|
|
|
|
}
|
|
|
|
module->interfaces[i] = intf;
|
|
|
|
}
|
|
|
|
|
|
|
|
return module;
|
|
|
|
|
|
|
|
err_put_interfaces:
|
2016-10-15 02:18:21 +07:00
|
|
|
for (--i; i >= 0; --i)
|
2016-04-23 23:47:24 +07:00
|
|
|
gb_interface_put(module->interfaces[i]);
|
|
|
|
|
|
|
|
put_device(&module->dev);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Register and enable an interface after first attempting to activate it.
|
|
|
|
*/
|
|
|
|
static void gb_module_register_interface(struct gb_interface *intf)
|
|
|
|
{
|
|
|
|
struct gb_module *module = intf->module;
|
|
|
|
u8 intf_id = intf->interface_id;
|
|
|
|
int ret;
|
|
|
|
|
2016-04-23 23:47:25 +07:00
|
|
|
mutex_lock(&intf->mutex);
|
|
|
|
|
2016-07-20 21:40:22 +07:00
|
|
|
ret = gb_interface_activate(intf);
|
2016-04-23 23:47:24 +07:00
|
|
|
if (ret) {
|
2016-07-20 21:40:20 +07:00
|
|
|
if (intf->type != GB_INTERFACE_TYPE_DUMMY) {
|
2016-07-19 20:24:50 +07:00
|
|
|
dev_err(&module->dev,
|
2018-11-25 23:58:15 +07:00
|
|
|
"failed to activate interface %u: %d\n",
|
|
|
|
intf_id, ret);
|
2016-07-19 20:24:50 +07:00
|
|
|
}
|
2016-05-19 08:55:13 +07:00
|
|
|
|
2016-04-23 23:47:24 +07:00
|
|
|
gb_interface_add(intf);
|
2016-04-23 23:47:25 +07:00
|
|
|
goto err_unlock;
|
2016-04-23 23:47:24 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = gb_interface_add(intf);
|
|
|
|
if (ret)
|
|
|
|
goto err_interface_deactivate;
|
|
|
|
|
|
|
|
ret = gb_interface_enable(intf);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(&module->dev, "failed to enable interface %u: %d\n",
|
2018-11-25 23:58:15 +07:00
|
|
|
intf_id, ret);
|
2016-04-23 23:47:24 +07:00
|
|
|
goto err_interface_deactivate;
|
|
|
|
}
|
|
|
|
|
2016-04-23 23:47:25 +07:00
|
|
|
mutex_unlock(&intf->mutex);
|
|
|
|
|
2016-04-23 23:47:24 +07:00
|
|
|
return;
|
|
|
|
|
|
|
|
err_interface_deactivate:
|
|
|
|
gb_interface_deactivate(intf);
|
2016-04-23 23:47:25 +07:00
|
|
|
err_unlock:
|
|
|
|
mutex_unlock(&intf->mutex);
|
2016-04-23 23:47:24 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void gb_module_deregister_interface(struct gb_interface *intf)
|
|
|
|
{
|
|
|
|
/* Mark as disconnected to prevent I/O during disable. */
|
|
|
|
if (intf->module->disconnected)
|
|
|
|
intf->disconnected = true;
|
|
|
|
|
2016-04-23 23:47:25 +07:00
|
|
|
mutex_lock(&intf->mutex);
|
2016-07-20 21:40:24 +07:00
|
|
|
intf->removed = true;
|
2016-04-23 23:47:24 +07:00
|
|
|
gb_interface_disable(intf);
|
|
|
|
gb_interface_deactivate(intf);
|
2016-04-23 23:47:25 +07:00
|
|
|
mutex_unlock(&intf->mutex);
|
2016-04-23 23:47:24 +07:00
|
|
|
|
|
|
|
gb_interface_del(intf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Register a module and its interfaces. */
|
|
|
|
int gb_module_add(struct gb_module *module)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = device_add(&module->dev);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(&module->dev, "failed to register module: %d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-05-24 11:05:31 +07:00
|
|
|
trace_gb_module_add(module);
|
|
|
|
|
2016-04-23 23:47:24 +07:00
|
|
|
for (i = 0; i < module->num_interfaces; ++i)
|
|
|
|
gb_module_register_interface(module->interfaces[i]);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Deregister a module and its interfaces. */
|
|
|
|
void gb_module_del(struct gb_module *module)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < module->num_interfaces; ++i)
|
|
|
|
gb_module_deregister_interface(module->interfaces[i]);
|
|
|
|
|
2016-05-24 11:05:31 +07:00
|
|
|
trace_gb_module_del(module);
|
|
|
|
|
2016-04-23 23:47:24 +07:00
|
|
|
device_del(&module->dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
void gb_module_put(struct gb_module *module)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < module->num_interfaces; ++i)
|
|
|
|
gb_interface_put(module->interfaces[i]);
|
|
|
|
|
|
|
|
put_device(&module->dev);
|
|
|
|
}
|