treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / greybus / module.c
blob36f77f9e1d7439eaf9c16e871a3fd28a6a218bf5
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Greybus Module code
5 * Copyright 2016 Google Inc.
6 * Copyright 2016 Linaro Ltd.
7 */
9 #include <linux/greybus.h>
10 #include "greybus_trace.h"
12 static ssize_t eject_store(struct device *dev,
13 struct device_attribute *attr,
14 const char *buf, size_t len)
16 struct gb_module *module = to_gb_module(dev);
17 struct gb_interface *intf;
18 size_t i;
19 long val;
20 int ret;
22 ret = kstrtol(buf, 0, &val);
23 if (ret)
24 return ret;
26 if (!val)
27 return len;
29 for (i = 0; i < module->num_interfaces; ++i) {
30 intf = module->interfaces[i];
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);
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;
45 return len;
47 static DEVICE_ATTR_WO(eject);
49 static ssize_t module_id_show(struct device *dev,
50 struct device_attribute *attr, char *buf)
52 struct gb_module *module = to_gb_module(dev);
54 return sprintf(buf, "%u\n", module->module_id);
56 static DEVICE_ATTR_RO(module_id);
58 static ssize_t num_interfaces_show(struct device *dev,
59 struct device_attribute *attr, char *buf)
61 struct gb_module *module = to_gb_module(dev);
63 return sprintf(buf, "%zu\n", module->num_interfaces);
65 static DEVICE_ATTR_RO(num_interfaces);
67 static struct attribute *module_attrs[] = {
68 &dev_attr_eject.attr,
69 &dev_attr_module_id.attr,
70 &dev_attr_num_interfaces.attr,
71 NULL,
73 ATTRIBUTE_GROUPS(module);
75 static void gb_module_release(struct device *dev)
77 struct gb_module *module = to_gb_module(dev);
79 trace_gb_module_release(module);
81 kfree(module);
84 struct device_type greybus_module_type = {
85 .name = "greybus_module",
86 .release = gb_module_release,
89 struct gb_module *gb_module_create(struct gb_host_device *hd, u8 module_id,
90 size_t num_interfaces)
92 struct gb_interface *intf;
93 struct gb_module *module;
94 int i;
96 module = kzalloc(struct_size(module, interfaces, num_interfaces),
97 GFP_KERNEL);
98 if (!module)
99 return NULL;
101 module->hd = hd;
102 module->module_id = module_id;
103 module->num_interfaces = num_interfaces;
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);
113 trace_gb_module_create(module);
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",
119 module_id + i);
120 goto err_put_interfaces;
122 module->interfaces[i] = intf;
125 return module;
127 err_put_interfaces:
128 for (--i; i >= 0; --i)
129 gb_interface_put(module->interfaces[i]);
131 put_device(&module->dev);
133 return NULL;
137 * Register and enable an interface after first attempting to activate it.
139 static void gb_module_register_interface(struct gb_interface *intf)
141 struct gb_module *module = intf->module;
142 u8 intf_id = intf->interface_id;
143 int ret;
145 mutex_lock(&intf->mutex);
147 ret = gb_interface_activate(intf);
148 if (ret) {
149 if (intf->type != GB_INTERFACE_TYPE_DUMMY) {
150 dev_err(&module->dev,
151 "failed to activate interface %u: %d\n",
152 intf_id, ret);
155 gb_interface_add(intf);
156 goto err_unlock;
159 ret = gb_interface_add(intf);
160 if (ret)
161 goto err_interface_deactivate;
163 ret = gb_interface_enable(intf);
164 if (ret) {
165 dev_err(&module->dev, "failed to enable interface %u: %d\n",
166 intf_id, ret);
167 goto err_interface_deactivate;
170 mutex_unlock(&intf->mutex);
172 return;
174 err_interface_deactivate:
175 gb_interface_deactivate(intf);
176 err_unlock:
177 mutex_unlock(&intf->mutex);
180 static void gb_module_deregister_interface(struct gb_interface *intf)
182 /* Mark as disconnected to prevent I/O during disable. */
183 if (intf->module->disconnected)
184 intf->disconnected = true;
186 mutex_lock(&intf->mutex);
187 intf->removed = true;
188 gb_interface_disable(intf);
189 gb_interface_deactivate(intf);
190 mutex_unlock(&intf->mutex);
192 gb_interface_del(intf);
195 /* Register a module and its interfaces. */
196 int gb_module_add(struct gb_module *module)
198 size_t i;
199 int ret;
201 ret = device_add(&module->dev);
202 if (ret) {
203 dev_err(&module->dev, "failed to register module: %d\n", ret);
204 return ret;
207 trace_gb_module_add(module);
209 for (i = 0; i < module->num_interfaces; ++i)
210 gb_module_register_interface(module->interfaces[i]);
212 return 0;
215 /* Deregister a module and its interfaces. */
216 void gb_module_del(struct gb_module *module)
218 size_t i;
220 for (i = 0; i < module->num_interfaces; ++i)
221 gb_module_deregister_interface(module->interfaces[i]);
223 trace_gb_module_del(module);
225 device_del(&module->dev);
228 void gb_module_put(struct gb_module *module)
230 size_t i;
232 for (i = 0; i < module->num_interfaces; ++i)
233 gb_interface_put(module->interfaces[i]);
235 put_device(&module->dev);