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