blob: 36f77f9e1d7439eaf9c16e871a3fd28a6a218bf5 [file] [log] [blame]
Greg Kroah-Hartmaneb50fd32017-11-07 14:58:41 +01001// SPDX-License-Identifier: GPL-2.0
Johan Hovoldb15d97d2016-04-23 18:47:24 +02002/*
3 * Greybus Module code
4 *
5 * Copyright 2016 Google Inc.
6 * Copyright 2016 Linaro Ltd.
Johan Hovoldb15d97d2016-04-23 18:47:24 +02007 */
8
Greg Kroah-Hartmanec0ad862019-08-25 07:54:27 +02009#include <linux/greybus.h>
Alex Elder5451ea02016-05-23 23:05:31 -050010#include "greybus_trace.h"
Johan Hovoldb15d97d2016-04-23 18:47:24 +020011
Johan Hovold36602a22016-04-23 18:47:25 +020012static ssize_t eject_store(struct device *dev,
Cristian Sicilia8478c352018-11-25 17:58:15 +010013 struct device_attribute *attr,
14 const char *buf, size_t len)
Johan Hovold36602a22016-04-23 18:47:25 +020015{
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}
47static DEVICE_ATTR_WO(eject);
48
Johan Hovoldb15d97d2016-04-23 18:47:24 +020049static ssize_t module_id_show(struct device *dev,
Cristian Sicilia8478c352018-11-25 17:58:15 +010050 struct device_attribute *attr, char *buf)
Johan Hovoldb15d97d2016-04-23 18:47:24 +020051{
52 struct gb_module *module = to_gb_module(dev);
53
54 return sprintf(buf, "%u\n", module->module_id);
55}
56static DEVICE_ATTR_RO(module_id);
57
58static ssize_t num_interfaces_show(struct device *dev,
Cristian Sicilia8478c352018-11-25 17:58:15 +010059 struct device_attribute *attr, char *buf)
Johan Hovoldb15d97d2016-04-23 18:47:24 +020060{
61 struct gb_module *module = to_gb_module(dev);
62
63 return sprintf(buf, "%zu\n", module->num_interfaces);
64}
65static DEVICE_ATTR_RO(num_interfaces);
66
67static struct attribute *module_attrs[] = {
Johan Hovold36602a22016-04-23 18:47:25 +020068 &dev_attr_eject.attr,
Johan Hovoldb15d97d2016-04-23 18:47:24 +020069 &dev_attr_module_id.attr,
70 &dev_attr_num_interfaces.attr,
71 NULL,
72};
73ATTRIBUTE_GROUPS(module);
74
75static void gb_module_release(struct device *dev)
76{
77 struct gb_module *module = to_gb_module(dev);
78
Alex Elder5451ea02016-05-23 23:05:31 -050079 trace_gb_module_release(module);
80
Johan Hovoldb15d97d2016-04-23 18:47:24 +020081 kfree(module);
82}
83
84struct device_type greybus_module_type = {
85 .name = "greybus_module",
86 .release = gb_module_release,
87};
88
89struct gb_module *gb_module_create(struct gb_host_device *hd, u8 module_id,
Cristian Sicilia8478c352018-11-25 17:58:15 +010090 size_t num_interfaces)
Johan Hovoldb15d97d2016-04-23 18:47:24 +020091{
92 struct gb_interface *intf;
93 struct gb_module *module;
94 int i;
95
Kees Cookacafe7e2018-05-08 13:45:50 -070096 module = kzalloc(struct_size(module, interfaces, num_interfaces),
97 GFP_KERNEL);
Johan Hovoldb15d97d2016-04-23 18:47:24 +020098 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 Elder5451ea02016-05-23 23:05:31 -0500113 trace_gb_module_create(module);
114
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200115 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 Sicilia8478c352018-11-25 17:58:15 +0100119 module_id + i);
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200120 goto err_put_interfaces;
121 }
122 module->interfaces[i] = intf;
123 }
124
125 return module;
126
127err_put_interfaces:
Dan Carpentere866dd82016-10-14 22:18:21 +0300128 for (--i; i >= 0; --i)
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200129 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 */
139static 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 Hovold36602a22016-04-23 18:47:25 +0200145 mutex_lock(&intf->mutex);
146
Johan Hovold3e93cb62016-07-20 16:40:22 +0200147 ret = gb_interface_activate(intf);
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200148 if (ret) {
Johan Hovolda212b752016-07-20 16:40:20 +0200149 if (intf->type != GB_INTERFACE_TYPE_DUMMY) {
Johan Hovold6633d802016-07-19 15:24:50 +0200150 dev_err(&module->dev,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100151 "failed to activate interface %u: %d\n",
152 intf_id, ret);
Johan Hovold6633d802016-07-19 15:24:50 +0200153 }
Jeffrey Carlylee16715c2016-05-18 18:55:13 -0700154
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200155 gb_interface_add(intf);
Johan Hovold36602a22016-04-23 18:47:25 +0200156 goto err_unlock;
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200157 }
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 Sicilia8478c352018-11-25 17:58:15 +0100166 intf_id, ret);
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200167 goto err_interface_deactivate;
168 }
169
Johan Hovold36602a22016-04-23 18:47:25 +0200170 mutex_unlock(&intf->mutex);
171
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200172 return;
173
174err_interface_deactivate:
175 gb_interface_deactivate(intf);
Johan Hovold36602a22016-04-23 18:47:25 +0200176err_unlock:
177 mutex_unlock(&intf->mutex);
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200178}
179
180static 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 Hovold36602a22016-04-23 18:47:25 +0200186 mutex_lock(&intf->mutex);
Johan Hovold12169bc2016-07-20 16:40:24 +0200187 intf->removed = true;
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200188 gb_interface_disable(intf);
189 gb_interface_deactivate(intf);
Johan Hovold36602a22016-04-23 18:47:25 +0200190 mutex_unlock(&intf->mutex);
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200191
192 gb_interface_del(intf);
193}
194
195/* Register a module and its interfaces. */
196int 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 Elder5451ea02016-05-23 23:05:31 -0500207 trace_gb_module_add(module);
208
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200209 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. */
216void 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 Elder5451ea02016-05-23 23:05:31 -0500223 trace_gb_module_del(module);
224
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200225 device_del(&module->dev);
226}
227
228void 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}