Linux 4.19.133
[linux/fpc-iii.git] / drivers / staging / greybus / module.c
blob894d02e8d8b7b0ff34e534807d41af31d31baf1d
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 "greybus.h"
10 #include "greybus_trace.h"
13 static ssize_t eject_store(struct device *dev,
14 struct device_attribute *attr,
15 const char *buf, size_t len)
17 struct gb_module *module = to_gb_module(dev);
18 struct gb_interface *intf;
19 size_t i;
20 long val;
21 int ret;
23 ret = kstrtol(buf, 0, &val);
24 if (ret)
25 return ret;
27 if (!val)
28 return len;
30 for (i = 0; i < module->num_interfaces; ++i) {
31 intf = module->interfaces[i];
33 mutex_lock(&intf->mutex);
34 /* Set flag to prevent concurrent activation. */
35 intf->ejected = true;
36 gb_interface_disable(intf);
37 gb_interface_deactivate(intf);
38 mutex_unlock(&intf->mutex);
41 /* Tell the SVC to eject the primary interface. */
42 ret = gb_svc_intf_eject(module->hd->svc, module->module_id);
43 if (ret)
44 return ret;
46 return len;
48 static DEVICE_ATTR_WO(eject);
50 static ssize_t module_id_show(struct device *dev,
51 struct device_attribute *attr, char *buf)
53 struct gb_module *module = to_gb_module(dev);
55 return sprintf(buf, "%u\n", module->module_id);
57 static DEVICE_ATTR_RO(module_id);
59 static ssize_t num_interfaces_show(struct device *dev,
60 struct device_attribute *attr, char *buf)
62 struct gb_module *module = to_gb_module(dev);
64 return sprintf(buf, "%zu\n", module->num_interfaces);
66 static DEVICE_ATTR_RO(num_interfaces);
68 static struct attribute *module_attrs[] = {
69 &dev_attr_eject.attr,
70 &dev_attr_module_id.attr,
71 &dev_attr_num_interfaces.attr,
72 NULL,
74 ATTRIBUTE_GROUPS(module);
76 static void gb_module_release(struct device *dev)
78 struct gb_module *module = to_gb_module(dev);
80 trace_gb_module_release(module);
82 kfree(module);
85 struct device_type greybus_module_type = {
86 .name = "greybus_module",
87 .release = gb_module_release,
90 struct gb_module *gb_module_create(struct gb_host_device *hd, u8 module_id,
91 size_t num_interfaces)
93 struct gb_interface *intf;
94 struct gb_module *module;
95 int i;
97 module = kzalloc(struct_size(module, interfaces, num_interfaces),
98 GFP_KERNEL);
99 if (!module)
100 return NULL;
102 module->hd = hd;
103 module->module_id = module_id;
104 module->num_interfaces = num_interfaces;
106 module->dev.parent = &hd->dev;
107 module->dev.bus = &greybus_bus_type;
108 module->dev.type = &greybus_module_type;
109 module->dev.groups = module_groups;
110 module->dev.dma_mask = hd->dev.dma_mask;
111 device_initialize(&module->dev);
112 dev_set_name(&module->dev, "%d-%u", hd->bus_id, module_id);
114 trace_gb_module_create(module);
116 for (i = 0; i < num_interfaces; ++i) {
117 intf = gb_interface_create(module, module_id + i);
118 if (!intf) {
119 dev_err(&module->dev, "failed to create interface %u\n",
120 module_id + i);
121 goto err_put_interfaces;
123 module->interfaces[i] = intf;
126 return module;
128 err_put_interfaces:
129 for (--i; i >= 0; --i)
130 gb_interface_put(module->interfaces[i]);
132 put_device(&module->dev);
134 return NULL;
138 * Register and enable an interface after first attempting to activate it.
140 static void gb_module_register_interface(struct gb_interface *intf)
142 struct gb_module *module = intf->module;
143 u8 intf_id = intf->interface_id;
144 int ret;
146 mutex_lock(&intf->mutex);
148 ret = gb_interface_activate(intf);
149 if (ret) {
150 if (intf->type != GB_INTERFACE_TYPE_DUMMY) {
151 dev_err(&module->dev,
152 "failed to activate interface %u: %d\n",
153 intf_id, ret);
156 gb_interface_add(intf);
157 goto err_unlock;
160 ret = gb_interface_add(intf);
161 if (ret)
162 goto err_interface_deactivate;
164 ret = gb_interface_enable(intf);
165 if (ret) {
166 dev_err(&module->dev, "failed to enable interface %u: %d\n",
167 intf_id, ret);
168 goto err_interface_deactivate;
171 mutex_unlock(&intf->mutex);
173 return;
175 err_interface_deactivate:
176 gb_interface_deactivate(intf);
177 err_unlock:
178 mutex_unlock(&intf->mutex);
181 static void gb_module_deregister_interface(struct gb_interface *intf)
183 /* Mark as disconnected to prevent I/O during disable. */
184 if (intf->module->disconnected)
185 intf->disconnected = true;
187 mutex_lock(&intf->mutex);
188 intf->removed = true;
189 gb_interface_disable(intf);
190 gb_interface_deactivate(intf);
191 mutex_unlock(&intf->mutex);
193 gb_interface_del(intf);
196 /* Register a module and its interfaces. */
197 int gb_module_add(struct gb_module *module)
199 size_t i;
200 int ret;
202 ret = device_add(&module->dev);
203 if (ret) {
204 dev_err(&module->dev, "failed to register module: %d\n", ret);
205 return ret;
208 trace_gb_module_add(module);
210 for (i = 0; i < module->num_interfaces; ++i)
211 gb_module_register_interface(module->interfaces[i]);
213 return 0;
216 /* Deregister a module and its interfaces. */
217 void gb_module_del(struct gb_module *module)
219 size_t i;
221 for (i = 0; i < module->num_interfaces; ++i)
222 gb_module_deregister_interface(module->interfaces[i]);
224 trace_gb_module_del(module);
226 device_del(&module->dev);
229 void gb_module_put(struct gb_module *module)
231 size_t i;
233 for (i = 0; i < module->num_interfaces; ++i)
234 gb_interface_put(module->interfaces[i]);
236 put_device(&module->dev);