blob: ea5895475181f25a6dc5c1749246d4a964c4a0a6 [file] [log] [blame]
Johan Hovoldb15d97d2016-04-23 18:47:24 +02001/*
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
12
Johan Hovold36602a22016-04-23 18:47:25 +020013static ssize_t eject_store(struct device *dev,
14 struct device_attribute *attr,
15 const char *buf, size_t len)
16{
17 struct gb_module *module = to_gb_module(dev);
18 struct gb_interface *intf;
19 size_t i;
20 long val;
21 int ret;
22
23 ret = kstrtol(buf, 0, &val);
24 if (ret)
25 return ret;
26
27 if (!val)
28 return len;
29
30 for (i = 0; i < module->num_interfaces; ++i) {
31 intf = module->interfaces[i];
32
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);
39 }
40
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;
45
46 return len;
47}
48static DEVICE_ATTR_WO(eject);
49
Johan Hovoldb15d97d2016-04-23 18:47:24 +020050static ssize_t module_id_show(struct device *dev,
51 struct device_attribute *attr, char *buf)
52{
53 struct gb_module *module = to_gb_module(dev);
54
55 return sprintf(buf, "%u\n", module->module_id);
56}
57static DEVICE_ATTR_RO(module_id);
58
59static ssize_t num_interfaces_show(struct device *dev,
60 struct device_attribute *attr, char *buf)
61{
62 struct gb_module *module = to_gb_module(dev);
63
64 return sprintf(buf, "%zu\n", module->num_interfaces);
65}
66static DEVICE_ATTR_RO(num_interfaces);
67
68static struct attribute *module_attrs[] = {
Johan Hovold36602a22016-04-23 18:47:25 +020069 &dev_attr_eject.attr,
Johan Hovoldb15d97d2016-04-23 18:47:24 +020070 &dev_attr_module_id.attr,
71 &dev_attr_num_interfaces.attr,
72 NULL,
73};
74ATTRIBUTE_GROUPS(module);
75
76static void gb_module_release(struct device *dev)
77{
78 struct gb_module *module = to_gb_module(dev);
79
80 kfree(module);
81}
82
83struct device_type greybus_module_type = {
84 .name = "greybus_module",
85 .release = gb_module_release,
86};
87
88struct gb_module *gb_module_create(struct gb_host_device *hd, u8 module_id,
89 size_t num_interfaces)
90{
91 struct gb_interface *intf;
92 struct gb_module *module;
93 int i;
94
95 module = kzalloc(sizeof(*module) + num_interfaces * sizeof(intf),
96 GFP_KERNEL);
97 if (!module)
98 return NULL;
99
100 module->hd = hd;
101 module->module_id = module_id;
102 module->num_interfaces = num_interfaces;
103
104 module->dev.parent = &hd->dev;
105 module->dev.bus = &greybus_bus_type;
106 module->dev.type = &greybus_module_type;
107 module->dev.groups = module_groups;
108 module->dev.dma_mask = hd->dev.dma_mask;
109 device_initialize(&module->dev);
110 dev_set_name(&module->dev, "%d-%u", hd->bus_id, module_id);
111
112 for (i = 0; i < num_interfaces; ++i) {
113 intf = gb_interface_create(module, module_id + i);
114 if (!intf) {
115 dev_err(&module->dev, "failed to create interface %u\n",
116 module_id + i);
117 goto err_put_interfaces;
118 }
119 module->interfaces[i] = intf;
120 }
121
122 return module;
123
124err_put_interfaces:
125 for (--i; i > 0; --i)
126 gb_interface_put(module->interfaces[i]);
127
128 put_device(&module->dev);
129
130 return NULL;
131}
132
133/*
134 * Register and enable an interface after first attempting to activate it.
135 */
136static void gb_module_register_interface(struct gb_interface *intf)
137{
138 struct gb_module *module = intf->module;
139 u8 intf_id = intf->interface_id;
140 int ret;
Jeffrey Carlylee16715c2016-05-18 18:55:13 -0700141 int retries = 3;
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200142
Johan Hovold36602a22016-04-23 18:47:25 +0200143 mutex_lock(&intf->mutex);
144
Jeffrey Carlylee16715c2016-05-18 18:55:13 -0700145 while (retries--) {
146 ret = gb_interface_activate(intf);
147 if (ret != -EAGAIN)
148 break;
149 }
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200150 if (ret) {
151 dev_err(&module->dev, "failed to activate interface %u: %d\n",
152 intf_id, ret);
Jeffrey Carlylee16715c2016-05-18 18:55:13 -0700153
154 /*
155 * -EAGAIN indicates that the Greybus operation
156 * interface_activate determined the remote interface to be
157 * UniPro-only. At present, we assume a UniPro-only module
158 * to be a Greybus module that failed to send its mailbox
159 * poke. There is some reason to believe that this is
160 * because of a bug in the ES3 bootrom. If we exhause our
161 * retries trying to activate such an interface, convert
162 * the error code back into a "no device" error.
163 */
164 if (ret == -EAGAIN)
165 ret = -ENODEV;
166
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200167 gb_interface_add(intf);
Johan Hovold36602a22016-04-23 18:47:25 +0200168 goto err_unlock;
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200169 }
170
171 ret = gb_interface_add(intf);
172 if (ret)
173 goto err_interface_deactivate;
174
175 ret = gb_interface_enable(intf);
176 if (ret) {
177 dev_err(&module->dev, "failed to enable interface %u: %d\n",
178 intf_id, ret);
179 goto err_interface_deactivate;
180 }
181
Johan Hovold36602a22016-04-23 18:47:25 +0200182 mutex_unlock(&intf->mutex);
183
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200184 return;
185
186err_interface_deactivate:
187 gb_interface_deactivate(intf);
Johan Hovold36602a22016-04-23 18:47:25 +0200188err_unlock:
189 mutex_unlock(&intf->mutex);
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200190}
191
192static void gb_module_deregister_interface(struct gb_interface *intf)
193{
194 /* Mark as disconnected to prevent I/O during disable. */
195 if (intf->module->disconnected)
196 intf->disconnected = true;
197
Johan Hovold36602a22016-04-23 18:47:25 +0200198 mutex_lock(&intf->mutex);
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200199 gb_interface_disable(intf);
200 gb_interface_deactivate(intf);
Johan Hovold36602a22016-04-23 18:47:25 +0200201 mutex_unlock(&intf->mutex);
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200202
203 gb_interface_del(intf);
204}
205
206/* Register a module and its interfaces. */
207int gb_module_add(struct gb_module *module)
208{
209 size_t i;
210 int ret;
211
212 ret = device_add(&module->dev);
213 if (ret) {
214 dev_err(&module->dev, "failed to register module: %d\n", ret);
215 return ret;
216 }
217
218 for (i = 0; i < module->num_interfaces; ++i)
219 gb_module_register_interface(module->interfaces[i]);
220
221 return 0;
222}
223
224/* Deregister a module and its interfaces. */
225void gb_module_del(struct gb_module *module)
226{
227 size_t i;
228
229 for (i = 0; i < module->num_interfaces; ++i)
230 gb_module_deregister_interface(module->interfaces[i]);
231
232 device_del(&module->dev);
233}
234
235void gb_module_put(struct gb_module *module)
236{
237 size_t i;
238
239 for (i = 0; i < module->num_interfaces; ++i)
240 gb_interface_put(module->interfaces[i]);
241
242 put_device(&module->dev);
243}