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