^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Greybus Module code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright 2016 Google Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright 2016 Linaro Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/greybus.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "greybus_trace.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) static ssize_t eject_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) struct gb_module *module = to_gb_module(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct gb_interface *intf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) long val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) ret = kstrtol(buf, 0, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) if (!val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) for (i = 0; i < module->num_interfaces; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) intf = module->interfaces[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) mutex_lock(&intf->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /* Set flag to prevent concurrent activation. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) intf->ejected = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) gb_interface_disable(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) gb_interface_deactivate(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) mutex_unlock(&intf->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) /* Tell the SVC to eject the primary interface. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) ret = gb_svc_intf_eject(module->hd->svc, module->module_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static DEVICE_ATTR_WO(eject);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static ssize_t module_id_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct gb_module *module = to_gb_module(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return sprintf(buf, "%u\n", module->module_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static DEVICE_ATTR_RO(module_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static ssize_t num_interfaces_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct gb_module *module = to_gb_module(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) return sprintf(buf, "%zu\n", module->num_interfaces);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static DEVICE_ATTR_RO(num_interfaces);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static struct attribute *module_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) &dev_attr_eject.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) &dev_attr_module_id.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) &dev_attr_num_interfaces.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) ATTRIBUTE_GROUPS(module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) static void gb_module_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) struct gb_module *module = to_gb_module(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) trace_gb_module_release(module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) kfree(module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) struct device_type greybus_module_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) .name = "greybus_module",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) .release = gb_module_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) struct gb_module *gb_module_create(struct gb_host_device *hd, u8 module_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) size_t num_interfaces)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) struct gb_interface *intf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct gb_module *module;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) module = kzalloc(struct_size(module, interfaces, num_interfaces),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (!module)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) module->hd = hd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) module->module_id = module_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) module->num_interfaces = num_interfaces;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) module->dev.parent = &hd->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) module->dev.bus = &greybus_bus_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) module->dev.type = &greybus_module_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) module->dev.groups = module_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) module->dev.dma_mask = hd->dev.dma_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) device_initialize(&module->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) dev_set_name(&module->dev, "%d-%u", hd->bus_id, module_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) trace_gb_module_create(module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) for (i = 0; i < num_interfaces; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) intf = gb_interface_create(module, module_id + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (!intf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) dev_err(&module->dev, "failed to create interface %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) module_id + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) goto err_put_interfaces;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) module->interfaces[i] = intf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return module;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) err_put_interfaces:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) for (--i; i >= 0; --i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) gb_interface_put(module->interfaces[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) put_device(&module->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * Register and enable an interface after first attempting to activate it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static void gb_module_register_interface(struct gb_interface *intf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) struct gb_module *module = intf->module;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) u8 intf_id = intf->interface_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) mutex_lock(&intf->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) ret = gb_interface_activate(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (intf->type != GB_INTERFACE_TYPE_DUMMY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) dev_err(&module->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) "failed to activate interface %u: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) intf_id, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) gb_interface_add(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) goto err_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) ret = gb_interface_add(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) goto err_interface_deactivate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) ret = gb_interface_enable(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) dev_err(&module->dev, "failed to enable interface %u: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) intf_id, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) goto err_interface_deactivate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) mutex_unlock(&intf->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) err_interface_deactivate:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) gb_interface_deactivate(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) err_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) mutex_unlock(&intf->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) static void gb_module_deregister_interface(struct gb_interface *intf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) /* Mark as disconnected to prevent I/O during disable. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (intf->module->disconnected)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) intf->disconnected = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) mutex_lock(&intf->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) intf->removed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) gb_interface_disable(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) gb_interface_deactivate(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) mutex_unlock(&intf->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) gb_interface_del(intf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) /* Register a module and its interfaces. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) int gb_module_add(struct gb_module *module)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) ret = device_add(&module->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) dev_err(&module->dev, "failed to register module: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) trace_gb_module_add(module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) for (i = 0; i < module->num_interfaces; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) gb_module_register_interface(module->interfaces[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) /* Deregister a module and its interfaces. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) void gb_module_del(struct gb_module *module)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) for (i = 0; i < module->num_interfaces; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) gb_module_deregister_interface(module->interfaces[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) trace_gb_module_del(module);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) device_del(&module->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) void gb_module_put(struct gb_module *module)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) for (i = 0; i < module->num_interfaces; ++i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) gb_interface_put(module->interfaces[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) put_device(&module->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }