blob: 5c4ca7938387f13ec0909f8b65a116f942e8583d [file] [log] [blame]
Alex Elder30c6d9d2015-05-22 13:02:08 -05001/*
2 * SVC Greybus driver.
3 *
4 * Copyright 2015 Google Inc.
5 * Copyright 2015 Linaro Ltd.
6 *
7 * Released under the GPLv2 only.
8 */
9
Viresh Kumar067906f2015-08-06 12:44:55 +053010#include <linux/workqueue.h>
Alex Elder30c6d9d2015-05-22 13:02:08 -050011
Viresh Kumarf66427a2015-09-02 21:27:13 +053012#include "greybus.h"
13
Johan Hovoldf6c6c132015-11-11 10:07:08 +010014#define CPORT_FLAGS_E2EFC BIT(0)
15#define CPORT_FLAGS_CSD_N BIT(1)
16#define CPORT_FLAGS_CSV_N BIT(2)
Perry Hung0b226492015-07-24 19:02:34 -040017
Viresh Kumarb45864d2015-07-24 15:32:21 +053018
Johan Hovold9ae41092015-12-02 18:23:29 +010019struct gb_svc_deferred_request {
Viresh Kumar067906f2015-08-06 12:44:55 +053020 struct work_struct work;
Johan Hovold9ae41092015-12-02 18:23:29 +010021 struct gb_operation *operation;
Viresh Kumar067906f2015-08-06 12:44:55 +053022};
23
Viresh Kumaread35462015-07-21 17:44:19 +053024
Johan Hovold66069fb2015-11-25 15:59:09 +010025static ssize_t endo_id_show(struct device *dev,
26 struct device_attribute *attr, char *buf)
27{
28 struct gb_svc *svc = to_gb_svc(dev);
29
30 return sprintf(buf, "0x%04x\n", svc->endo_id);
31}
32static DEVICE_ATTR_RO(endo_id);
33
34static ssize_t ap_intf_id_show(struct device *dev,
35 struct device_attribute *attr, char *buf)
36{
37 struct gb_svc *svc = to_gb_svc(dev);
38
39 return sprintf(buf, "%u\n", svc->ap_intf_id);
40}
41static DEVICE_ATTR_RO(ap_intf_id);
42
43static struct attribute *svc_attrs[] = {
44 &dev_attr_endo_id.attr,
45 &dev_attr_ap_intf_id.attr,
46 NULL,
47};
48ATTRIBUTE_GROUPS(svc);
49
Viresh Kumar505f16c2015-08-31 17:21:07 +053050static int gb_svc_intf_device_id(struct gb_svc *svc, u8 intf_id, u8 device_id)
Alex Elder30c6d9d2015-05-22 13:02:08 -050051{
52 struct gb_svc_intf_device_id_request request;
53
54 request.intf_id = intf_id;
55 request.device_id = device_id;
56
57 return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_DEVICE_ID,
58 &request, sizeof(request), NULL, 0);
59}
60
Viresh Kumar3f0e9182015-08-31 17:21:06 +053061int gb_svc_intf_reset(struct gb_svc *svc, u8 intf_id)
Alex Elder30c6d9d2015-05-22 13:02:08 -050062{
63 struct gb_svc_intf_reset_request request;
64
65 request.intf_id = intf_id;
66
67 return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_RESET,
68 &request, sizeof(request), NULL, 0);
69}
Viresh Kumar3f0e9182015-08-31 17:21:06 +053070EXPORT_SYMBOL_GPL(gb_svc_intf_reset);
Alex Elder30c6d9d2015-05-22 13:02:08 -050071
Viresh Kumar19151c32015-09-09 21:08:29 +053072int gb_svc_dme_peer_get(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
73 u32 *value)
74{
75 struct gb_svc_dme_peer_get_request request;
76 struct gb_svc_dme_peer_get_response response;
77 u16 result;
78 int ret;
79
80 request.intf_id = intf_id;
81 request.attr = cpu_to_le16(attr);
82 request.selector = cpu_to_le16(selector);
83
84 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_GET,
85 &request, sizeof(request),
86 &response, sizeof(response));
87 if (ret) {
Viresh Kumarb933fa42015-12-04 21:30:10 +053088 dev_err(&svc->dev, "failed to get DME attribute (%u 0x%04x %u): %d\n",
Johan Hovold684156a2015-11-25 15:59:19 +010089 intf_id, attr, selector, ret);
Viresh Kumar19151c32015-09-09 21:08:29 +053090 return ret;
91 }
92
93 result = le16_to_cpu(response.result_code);
94 if (result) {
Viresh Kumarb933fa42015-12-04 21:30:10 +053095 dev_err(&svc->dev, "UniPro error while getting DME attribute (%u 0x%04x %u): %u\n",
Johan Hovold684156a2015-11-25 15:59:19 +010096 intf_id, attr, selector, result);
Viresh Kumar4aac6c52015-12-04 21:30:08 +053097 return -EIO;
Viresh Kumar19151c32015-09-09 21:08:29 +053098 }
99
100 if (value)
101 *value = le32_to_cpu(response.attr_value);
102
103 return 0;
104}
105EXPORT_SYMBOL_GPL(gb_svc_dme_peer_get);
106
107int gb_svc_dme_peer_set(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
108 u32 value)
109{
110 struct gb_svc_dme_peer_set_request request;
111 struct gb_svc_dme_peer_set_response response;
112 u16 result;
113 int ret;
114
115 request.intf_id = intf_id;
116 request.attr = cpu_to_le16(attr);
117 request.selector = cpu_to_le16(selector);
118 request.value = cpu_to_le32(value);
119
120 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_SET,
121 &request, sizeof(request),
122 &response, sizeof(response));
123 if (ret) {
Viresh Kumarb933fa42015-12-04 21:30:10 +0530124 dev_err(&svc->dev, "failed to set DME attribute (%u 0x%04x %u %u): %d\n",
Johan Hovold684156a2015-11-25 15:59:19 +0100125 intf_id, attr, selector, value, ret);
Viresh Kumar19151c32015-09-09 21:08:29 +0530126 return ret;
127 }
128
129 result = le16_to_cpu(response.result_code);
130 if (result) {
Viresh Kumarb933fa42015-12-04 21:30:10 +0530131 dev_err(&svc->dev, "UniPro error while setting DME attribute (%u 0x%04x %u %u): %u\n",
Johan Hovold684156a2015-11-25 15:59:19 +0100132 intf_id, attr, selector, value, result);
Viresh Kumar4aac6c52015-12-04 21:30:08 +0530133 return -EIO;
Viresh Kumar19151c32015-09-09 21:08:29 +0530134 }
135
136 return 0;
137}
138EXPORT_SYMBOL_GPL(gb_svc_dme_peer_set);
139
Viresh Kumar6bec5c72015-09-24 14:40:29 -0700140/*
141 * T_TstSrcIncrement is written by the module on ES2 as a stand-in for boot
142 * status attribute. AP needs to read and clear it, after reading a non-zero
143 * value from it.
144 *
145 * FIXME: This is module-hardware dependent and needs to be extended for every
146 * type of module we want to support.
147 */
148static int gb_svc_read_and_clear_module_boot_status(struct gb_interface *intf)
149{
Johan Hovold25376362015-11-03 18:03:23 +0100150 struct gb_host_device *hd = intf->hd;
Viresh Kumar6bec5c72015-09-24 14:40:29 -0700151 int ret;
152 u32 value;
153
154 /* Read and clear boot status in T_TstSrcIncrement */
155 ret = gb_svc_dme_peer_get(hd->svc, intf->interface_id,
156 DME_ATTR_T_TST_SRC_INCREMENT,
157 DME_ATTR_SELECTOR_INDEX, &value);
158
159 if (ret)
160 return ret;
161
162 /*
163 * A nonzero boot status indicates the module has finished
164 * booting. Clear it.
165 */
166 if (!value) {
167 dev_err(&intf->dev, "Module not ready yet\n");
168 return -ENODEV;
169 }
170
Viresh Kumar1575ef12015-10-07 15:40:24 -0400171 /*
172 * Check if the module needs to boot from unipro.
173 * For ES2: We need to check lowest 8 bits of 'value'.
174 * For ES3: We need to check highest 8 bits out of 32 of 'value'.
175 *
176 * FIXME: Add code to find if we are on ES2 or ES3 to have separate
177 * checks.
178 */
179 if (value == DME_TSI_UNIPRO_BOOT_STARTED ||
180 value == DME_TSI_FALLBACK_UNIPRO_BOOT_STARTED)
181 intf->boot_over_unipro = true;
182
Viresh Kumar6bec5c72015-09-24 14:40:29 -0700183 return gb_svc_dme_peer_set(hd->svc, intf->interface_id,
184 DME_ATTR_T_TST_SRC_INCREMENT,
185 DME_ATTR_SELECTOR_INDEX, 0);
186}
187
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530188int gb_svc_connection_create(struct gb_svc *svc,
Alex Elder30c6d9d2015-05-22 13:02:08 -0500189 u8 intf1_id, u16 cport1_id,
Viresh Kumar1575ef12015-10-07 15:40:24 -0400190 u8 intf2_id, u16 cport2_id,
191 bool boot_over_unipro)
Alex Elder30c6d9d2015-05-22 13:02:08 -0500192{
193 struct gb_svc_conn_create_request request;
194
195 request.intf1_id = intf1_id;
Rui Miguel Silva24980502015-09-15 15:33:51 +0100196 request.cport1_id = cpu_to_le16(cport1_id);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500197 request.intf2_id = intf2_id;
Rui Miguel Silva24980502015-09-15 15:33:51 +0100198 request.cport2_id = cpu_to_le16(cport2_id);
Perry Hung0b226492015-07-24 19:02:34 -0400199 /*
200 * XXX: fix connections paramaters to TC0 and all CPort flags
201 * for now.
202 */
203 request.tc = 0;
Viresh Kumar1575ef12015-10-07 15:40:24 -0400204
205 /*
206 * We need to skip setting E2EFC and other flags to the connection
207 * create request, for all cports, on an interface that need to boot
208 * over unipro, i.e. interfaces required to download firmware.
209 */
210 if (boot_over_unipro)
211 request.flags = CPORT_FLAGS_CSV_N | CPORT_FLAGS_CSD_N;
212 else
213 request.flags = CPORT_FLAGS_CSV_N | CPORT_FLAGS_E2EFC;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500214
215 return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_CREATE,
216 &request, sizeof(request), NULL, 0);
217}
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530218EXPORT_SYMBOL_GPL(gb_svc_connection_create);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500219
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530220void gb_svc_connection_destroy(struct gb_svc *svc, u8 intf1_id, u16 cport1_id,
221 u8 intf2_id, u16 cport2_id)
Alex Elder30c6d9d2015-05-22 13:02:08 -0500222{
223 struct gb_svc_conn_destroy_request request;
Viresh Kumard9fcfff2015-08-31 17:21:05 +0530224 struct gb_connection *connection = svc->connection;
225 int ret;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500226
227 request.intf1_id = intf1_id;
Rui Miguel Silva24980502015-09-15 15:33:51 +0100228 request.cport1_id = cpu_to_le16(cport1_id);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500229 request.intf2_id = intf2_id;
Rui Miguel Silva24980502015-09-15 15:33:51 +0100230 request.cport2_id = cpu_to_le16(cport2_id);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500231
Viresh Kumard9fcfff2015-08-31 17:21:05 +0530232 ret = gb_operation_sync(connection, GB_SVC_TYPE_CONN_DESTROY,
233 &request, sizeof(request), NULL, 0);
Johan Hovold684156a2015-11-25 15:59:19 +0100234 if (ret) {
Viresh Kumar2f3db922015-12-04 21:30:09 +0530235 dev_err(&svc->dev, "failed to destroy connection (%u:%u %u:%u): %d\n",
Johan Hovold684156a2015-11-25 15:59:19 +0100236 intf1_id, cport1_id, intf2_id, cport2_id, ret);
237 }
Alex Elder30c6d9d2015-05-22 13:02:08 -0500238}
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530239EXPORT_SYMBOL_GPL(gb_svc_connection_destroy);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500240
Viresh Kumarbb106852015-09-07 16:01:25 +0530241/* Creates bi-directional routes between the devices */
Viresh Kumar505f16c2015-08-31 17:21:07 +0530242static int gb_svc_route_create(struct gb_svc *svc, u8 intf1_id, u8 dev1_id,
243 u8 intf2_id, u8 dev2_id)
Perry Hunge08aaa42015-07-24 19:02:31 -0400244{
245 struct gb_svc_route_create_request request;
246
247 request.intf1_id = intf1_id;
248 request.dev1_id = dev1_id;
249 request.intf2_id = intf2_id;
250 request.dev2_id = dev2_id;
251
252 return gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_CREATE,
253 &request, sizeof(request), NULL, 0);
254}
Perry Hunge08aaa42015-07-24 19:02:31 -0400255
Viresh Kumar0a020572015-09-07 18:05:26 +0530256/* Destroys bi-directional routes between the devices */
257static void gb_svc_route_destroy(struct gb_svc *svc, u8 intf1_id, u8 intf2_id)
258{
259 struct gb_svc_route_destroy_request request;
260 int ret;
261
262 request.intf1_id = intf1_id;
263 request.intf2_id = intf2_id;
264
265 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_DESTROY,
266 &request, sizeof(request), NULL, 0);
Johan Hovold684156a2015-11-25 15:59:19 +0100267 if (ret) {
Viresh Kumar2f3db922015-12-04 21:30:09 +0530268 dev_err(&svc->dev, "failed to destroy route (%u %u): %d\n",
Johan Hovold684156a2015-11-25 15:59:19 +0100269 intf1_id, intf2_id, ret);
270 }
Viresh Kumar0a020572015-09-07 18:05:26 +0530271}
272
Viresh Kumaread35462015-07-21 17:44:19 +0530273static int gb_svc_version_request(struct gb_operation *op)
274{
275 struct gb_connection *connection = op->connection;
Johan Hovold684156a2015-11-25 15:59:19 +0100276 struct gb_svc *svc = connection->private;
Johan Hovoldcfb16902015-09-15 10:48:01 +0200277 struct gb_protocol_version_request *request;
278 struct gb_protocol_version_response *response;
Viresh Kumaread35462015-07-21 17:44:19 +0530279
Johan Hovold55510842015-11-19 18:28:01 +0100280 if (op->request->payload_size < sizeof(*request)) {
Johan Hovold684156a2015-11-25 15:59:19 +0100281 dev_err(&svc->dev, "short version request (%zu < %zu)\n",
Johan Hovold55510842015-11-19 18:28:01 +0100282 op->request->payload_size,
283 sizeof(*request));
284 return -EINVAL;
285 }
286
Johan Hovoldcfb16902015-09-15 10:48:01 +0200287 request = op->request->payload;
Viresh Kumaread35462015-07-21 17:44:19 +0530288
Johan Hovoldcfb16902015-09-15 10:48:01 +0200289 if (request->major > GB_SVC_VERSION_MAJOR) {
Viresh Kumar2f3db922015-12-04 21:30:09 +0530290 dev_warn(&svc->dev, "unsupported major version (%u > %u)\n",
Johan Hovold684156a2015-11-25 15:59:19 +0100291 request->major, GB_SVC_VERSION_MAJOR);
Viresh Kumaread35462015-07-21 17:44:19 +0530292 return -ENOTSUPP;
293 }
294
Johan Hovoldcfb16902015-09-15 10:48:01 +0200295 connection->module_major = request->major;
296 connection->module_minor = request->minor;
Viresh Kumar3ea959e32015-08-11 07:36:14 +0530297
Johan Hovold684156a2015-11-25 15:59:19 +0100298 if (!gb_operation_response_alloc(op, sizeof(*response), GFP_KERNEL))
Viresh Kumaread35462015-07-21 17:44:19 +0530299 return -ENOMEM;
Viresh Kumaread35462015-07-21 17:44:19 +0530300
Johan Hovoldcfb16902015-09-15 10:48:01 +0200301 response = op->response->payload;
302 response->major = connection->module_major;
303 response->minor = connection->module_minor;
Johan Hovold59832932015-09-15 10:48:00 +0200304
Viresh Kumaread35462015-07-21 17:44:19 +0530305 return 0;
306}
307
308static int gb_svc_hello(struct gb_operation *op)
309{
310 struct gb_connection *connection = op->connection;
Johan Hovold88f7b962015-11-25 15:59:08 +0100311 struct gb_svc *svc = connection->private;
Viresh Kumaread35462015-07-21 17:44:19 +0530312 struct gb_svc_hello_request *hello_request;
Viresh Kumaread35462015-07-21 17:44:19 +0530313 int ret;
314
Viresh Kumar0c32d2a2015-08-11 07:29:19 +0530315 if (op->request->payload_size < sizeof(*hello_request)) {
Johan Hovold684156a2015-11-25 15:59:19 +0100316 dev_warn(&svc->dev, "short hello request (%zu < %zu)\n",
317 op->request->payload_size,
318 sizeof(*hello_request));
Viresh Kumaread35462015-07-21 17:44:19 +0530319 return -EINVAL;
320 }
321
322 hello_request = op->request->payload;
Johan Hovold66069fb2015-11-25 15:59:09 +0100323 svc->endo_id = le16_to_cpu(hello_request->endo_id);
324 svc->ap_intf_id = hello_request->interface_id;
Viresh Kumaread35462015-07-21 17:44:19 +0530325
Johan Hovold88f7b962015-11-25 15:59:08 +0100326 ret = device_add(&svc->dev);
327 if (ret) {
328 dev_err(&svc->dev, "failed to register svc device: %d\n", ret);
329 return ret;
330 }
331
Viresh Kumaread35462015-07-21 17:44:19 +0530332 return 0;
333}
334
Johan Hovoldb4ee82e2015-12-02 18:23:28 +0100335static void gb_svc_intf_remove(struct gb_svc *svc, struct gb_interface *intf)
Viresh Kumarbbaca712015-09-23 16:48:08 -0700336{
Viresh Kumarbbaca712015-09-23 16:48:08 -0700337 u8 intf_id = intf->interface_id;
Johan Hovold141af4f2015-12-15 15:28:57 +0100338 u8 device_id = intf->device_id;
Viresh Kumarbbaca712015-09-23 16:48:08 -0700339
Johan Hovold141af4f2015-12-15 15:28:57 +0100340 intf->disconnected = true;
341
Viresh Kumar80d1ede2015-09-23 16:48:10 -0700342 gb_interface_remove(intf);
Viresh Kumarbbaca712015-09-23 16:48:08 -0700343
344 /*
345 * Destroy the two-way route between the AP and the interface.
346 */
Johan Hovold66069fb2015-11-25 15:59:09 +0100347 gb_svc_route_destroy(svc, svc->ap_intf_id, intf_id);
Viresh Kumarbbaca712015-09-23 16:48:08 -0700348
349 ida_simple_remove(&svc->device_id_map, device_id);
350}
351
Johan Hovold9ae41092015-12-02 18:23:29 +0100352static void gb_svc_process_intf_hotplug(struct gb_operation *operation)
Alex Elder30c6d9d2015-05-22 13:02:08 -0500353{
Johan Hovold24456a092015-12-02 18:23:27 +0100354 struct gb_svc_intf_hotplug_request *request;
Johan Hovold9ae41092015-12-02 18:23:29 +0100355 struct gb_connection *connection = operation->connection;
Viresh Kumar067906f2015-08-06 12:44:55 +0530356 struct gb_svc *svc = connection->private;
Johan Hovold25376362015-11-03 18:03:23 +0100357 struct gb_host_device *hd = connection->hd;
Viresh Kumaread35462015-07-21 17:44:19 +0530358 struct gb_interface *intf;
359 u8 intf_id, device_id;
Viresh Kumaread35462015-07-21 17:44:19 +0530360 int ret;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500361
Johan Hovold24456a092015-12-02 18:23:27 +0100362 /* The request message size has already been verified. */
Johan Hovold9ae41092015-12-02 18:23:29 +0100363 request = operation->request->payload;
Johan Hovold24456a092015-12-02 18:23:27 +0100364 intf_id = request->intf_id;
365
366 dev_dbg(&svc->dev, "%s - id = %u\n", __func__, intf_id);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500367
Viresh Kumarbbaca712015-09-23 16:48:08 -0700368 intf = gb_interface_find(hd, intf_id);
369 if (intf) {
370 /*
371 * We have received a hotplug request for an interface that
372 * already exists.
373 *
374 * This can happen in cases like:
375 * - bootrom loading the firmware image and booting into that,
376 * which only generates a hotplug event. i.e. no hot-unplug
377 * event.
378 * - Or the firmware on the module crashed and sent hotplug
379 * request again to the SVC, which got propagated to AP.
380 *
381 * Remove the interface and add it again, and let user know
382 * about this with a print message.
383 */
Viresh Kumar2f3db922015-12-04 21:30:09 +0530384 dev_info(&svc->dev, "removing interface %u to add it again\n",
Johan Hovold684156a2015-11-25 15:59:19 +0100385 intf_id);
Johan Hovoldb4ee82e2015-12-02 18:23:28 +0100386 gb_svc_intf_remove(svc, intf);
Viresh Kumarbbaca712015-09-23 16:48:08 -0700387 }
388
Viresh Kumaread35462015-07-21 17:44:19 +0530389 intf = gb_interface_create(hd, intf_id);
390 if (!intf) {
Viresh Kumar2f3db922015-12-04 21:30:09 +0530391 dev_err(&svc->dev, "failed to create interface %u\n",
Johan Hovold684156a2015-11-25 15:59:19 +0100392 intf_id);
Johan Hovold9ae41092015-12-02 18:23:29 +0100393 return;
Viresh Kumaread35462015-07-21 17:44:19 +0530394 }
395
Viresh Kumar6bec5c72015-09-24 14:40:29 -0700396 ret = gb_svc_read_and_clear_module_boot_status(intf);
Johan Hovoldb395754a2015-12-07 15:05:28 +0100397 if (ret) {
398 dev_err(&svc->dev, "failed to clear boot status of interface %u: %d\n",
399 intf_id, ret);
Viresh Kumar6bec5c72015-09-24 14:40:29 -0700400 goto destroy_interface;
Johan Hovoldb395754a2015-12-07 15:05:28 +0100401 }
Viresh Kumar6bec5c72015-09-24 14:40:29 -0700402
Johan Hovold24456a092015-12-02 18:23:27 +0100403 intf->unipro_mfg_id = le32_to_cpu(request->data.unipro_mfg_id);
404 intf->unipro_prod_id = le32_to_cpu(request->data.unipro_prod_id);
405 intf->vendor_id = le32_to_cpu(request->data.ara_vend_id);
406 intf->product_id = le32_to_cpu(request->data.ara_prod_id);
Viresh Kumar3944a452015-08-12 09:19:31 +0530407
Viresh Kumaread35462015-07-21 17:44:19 +0530408 /*
409 * Create a device id for the interface:
410 * - device id 0 (GB_DEVICE_ID_SVC) belongs to the SVC
411 * - device id 1 (GB_DEVICE_ID_AP) belongs to the AP
412 *
413 * XXX Do we need to allocate device ID for SVC or the AP here? And what
414 * XXX about an AP with multiple interface blocks?
415 */
Johan Hovoldc09db182015-09-15 09:18:08 +0200416 device_id = ida_simple_get(&svc->device_id_map,
Johan Hovold89f637f2015-09-01 12:25:25 +0200417 GB_DEVICE_ID_MODULES_START, 0, GFP_KERNEL);
Viresh Kumaread35462015-07-21 17:44:19 +0530418 if (device_id < 0) {
419 ret = device_id;
Viresh Kumar2f3db922015-12-04 21:30:09 +0530420 dev_err(&svc->dev, "failed to allocate device id for interface %u: %d\n",
Johan Hovold684156a2015-11-25 15:59:19 +0100421 intf_id, ret);
Viresh Kumaread35462015-07-21 17:44:19 +0530422 goto destroy_interface;
423 }
424
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530425 ret = gb_svc_intf_device_id(svc, intf_id, device_id);
Viresh Kumaread35462015-07-21 17:44:19 +0530426 if (ret) {
Viresh Kumar2f3db922015-12-04 21:30:09 +0530427 dev_err(&svc->dev, "failed to set device id %u for interface %u: %d\n",
Johan Hovold684156a2015-11-25 15:59:19 +0100428 device_id, intf_id, ret);
Viresh Kumaread35462015-07-21 17:44:19 +0530429 goto ida_put;
430 }
431
Perry Hung7e275462015-07-24 19:02:32 -0400432 /*
433 * Create a two-way route between the AP and the new interface
434 */
Johan Hovold66069fb2015-11-25 15:59:09 +0100435 ret = gb_svc_route_create(svc, svc->ap_intf_id, GB_DEVICE_ID_AP,
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530436 intf_id, device_id);
Perry Hung7e275462015-07-24 19:02:32 -0400437 if (ret) {
Viresh Kumar2f3db922015-12-04 21:30:09 +0530438 dev_err(&svc->dev, "failed to create route to interface %u (device id %u): %d\n",
Johan Hovold684156a2015-11-25 15:59:19 +0100439 intf_id, device_id, ret);
Viresh Kumar0a020572015-09-07 18:05:26 +0530440 goto svc_id_free;
Perry Hung7e275462015-07-24 19:02:32 -0400441 }
442
Viresh Kumaread35462015-07-21 17:44:19 +0530443 ret = gb_interface_init(intf, device_id);
444 if (ret) {
Viresh Kumar2f3db922015-12-04 21:30:09 +0530445 dev_err(&svc->dev, "failed to initialize interface %u (device id %u): %d\n",
Johan Hovold684156a2015-11-25 15:59:19 +0100446 intf_id, device_id, ret);
Viresh Kumar0a020572015-09-07 18:05:26 +0530447 goto destroy_route;
Viresh Kumaread35462015-07-21 17:44:19 +0530448 }
Alex Elder30c6d9d2015-05-22 13:02:08 -0500449
Johan Hovold9ae41092015-12-02 18:23:29 +0100450 return;
Viresh Kumaread35462015-07-21 17:44:19 +0530451
Viresh Kumar0a020572015-09-07 18:05:26 +0530452destroy_route:
Johan Hovold66069fb2015-11-25 15:59:09 +0100453 gb_svc_route_destroy(svc, svc->ap_intf_id, intf_id);
Viresh Kumaread35462015-07-21 17:44:19 +0530454svc_id_free:
455 /*
456 * XXX Should we tell SVC that this id doesn't belong to interface
457 * XXX anymore.
458 */
459ida_put:
Johan Hovoldc09db182015-09-15 09:18:08 +0200460 ida_simple_remove(&svc->device_id_map, device_id);
Viresh Kumaread35462015-07-21 17:44:19 +0530461destroy_interface:
Viresh Kumar80d1ede2015-09-23 16:48:10 -0700462 gb_interface_remove(intf);
Johan Hovold9ae41092015-12-02 18:23:29 +0100463}
464
Johan Hovold57ccd4b2015-12-02 18:23:30 +0100465static void gb_svc_process_intf_hot_unplug(struct gb_operation *operation)
466{
467 struct gb_svc *svc = operation->connection->private;
468 struct gb_svc_intf_hot_unplug_request *request;
469 struct gb_host_device *hd = operation->connection->hd;
470 struct gb_interface *intf;
471 u8 intf_id;
472
473 /* The request message size has already been verified. */
474 request = operation->request->payload;
475 intf_id = request->intf_id;
476
477 dev_dbg(&svc->dev, "%s - id = %u\n", __func__, intf_id);
478
479 intf = gb_interface_find(hd, intf_id);
480 if (!intf) {
Viresh Kumar2f3db922015-12-04 21:30:09 +0530481 dev_warn(&svc->dev, "could not find hot-unplug interface %u\n",
Johan Hovold57ccd4b2015-12-02 18:23:30 +0100482 intf_id);
483 return;
484 }
485
486 gb_svc_intf_remove(svc, intf);
487}
488
Johan Hovold9ae41092015-12-02 18:23:29 +0100489static void gb_svc_process_deferred_request(struct work_struct *work)
490{
491 struct gb_svc_deferred_request *dr;
492 struct gb_operation *operation;
493 struct gb_svc *svc;
494 u8 type;
495
496 dr = container_of(work, struct gb_svc_deferred_request, work);
497 operation = dr->operation;
498 svc = operation->connection->private;
499 type = operation->request->header->type;
500
501 switch (type) {
502 case GB_SVC_TYPE_INTF_HOTPLUG:
503 gb_svc_process_intf_hotplug(operation);
504 break;
Johan Hovold57ccd4b2015-12-02 18:23:30 +0100505 case GB_SVC_TYPE_INTF_HOT_UNPLUG:
506 gb_svc_process_intf_hot_unplug(operation);
507 break;
Johan Hovold9ae41092015-12-02 18:23:29 +0100508 default:
Viresh Kumarb933fa42015-12-04 21:30:10 +0530509 dev_err(&svc->dev, "bad deferred request type: 0x%02x\n", type);
Johan Hovold9ae41092015-12-02 18:23:29 +0100510 }
511
512 gb_operation_put(operation);
513 kfree(dr);
514}
515
516static int gb_svc_queue_deferred_request(struct gb_operation *operation)
517{
Johan Hovold3e48aca2015-12-02 18:23:31 +0100518 struct gb_svc *svc = operation->connection->private;
Johan Hovold9ae41092015-12-02 18:23:29 +0100519 struct gb_svc_deferred_request *dr;
520
521 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
522 if (!dr)
523 return -ENOMEM;
524
525 gb_operation_get(operation);
526
527 dr->operation = operation;
528 INIT_WORK(&dr->work, gb_svc_process_deferred_request);
529
Johan Hovold3e48aca2015-12-02 18:23:31 +0100530 queue_work(svc->wq, &dr->work);
Johan Hovold9ae41092015-12-02 18:23:29 +0100531
532 return 0;
Viresh Kumar067906f2015-08-06 12:44:55 +0530533}
Viresh Kumaread35462015-07-21 17:44:19 +0530534
Viresh Kumar067906f2015-08-06 12:44:55 +0530535/*
536 * Bringing up a module can be time consuming, as that may require lots of
537 * initialization on the module side. Over that, we may also need to download
538 * the firmware first and flash that on the module.
539 *
Johan Hovold3e48aca2015-12-02 18:23:31 +0100540 * In order not to make other svc events wait for all this to finish,
Viresh Kumar067906f2015-08-06 12:44:55 +0530541 * handle most of module hotplug stuff outside of the hotplug callback, with
542 * help of a workqueue.
543 */
544static int gb_svc_intf_hotplug_recv(struct gb_operation *op)
545{
Johan Hovold684156a2015-11-25 15:59:19 +0100546 struct gb_svc *svc = op->connection->private;
Johan Hovoldd34a3642015-12-02 18:23:26 +0100547 struct gb_svc_intf_hotplug_request *request;
Viresh Kumar067906f2015-08-06 12:44:55 +0530548
Johan Hovoldd34a3642015-12-02 18:23:26 +0100549 if (op->request->payload_size < sizeof(*request)) {
Johan Hovold684156a2015-11-25 15:59:19 +0100550 dev_warn(&svc->dev, "short hotplug request received (%zu < %zu)\n",
Johan Hovoldd34a3642015-12-02 18:23:26 +0100551 op->request->payload_size, sizeof(*request));
Viresh Kumar067906f2015-08-06 12:44:55 +0530552 return -EINVAL;
553 }
554
Johan Hovoldd34a3642015-12-02 18:23:26 +0100555 request = op->request->payload;
556
557 dev_dbg(&svc->dev, "%s - id = %u\n", __func__, request->intf_id);
558
Johan Hovold9ae41092015-12-02 18:23:29 +0100559 return gb_svc_queue_deferred_request(op);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500560}
561
562static int gb_svc_intf_hot_unplug_recv(struct gb_operation *op)
563{
Johan Hovold684156a2015-11-25 15:59:19 +0100564 struct gb_svc *svc = op->connection->private;
Johan Hovoldd34a3642015-12-02 18:23:26 +0100565 struct gb_svc_intf_hot_unplug_request *request;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500566
Johan Hovoldd34a3642015-12-02 18:23:26 +0100567 if (op->request->payload_size < sizeof(*request)) {
Johan Hovold684156a2015-11-25 15:59:19 +0100568 dev_warn(&svc->dev, "short hot unplug request received (%zu < %zu)\n",
Johan Hovoldd34a3642015-12-02 18:23:26 +0100569 op->request->payload_size, sizeof(*request));
Alex Elder30c6d9d2015-05-22 13:02:08 -0500570 return -EINVAL;
571 }
Alex Elder30c6d9d2015-05-22 13:02:08 -0500572
Johan Hovoldd34a3642015-12-02 18:23:26 +0100573 request = op->request->payload;
Johan Hovoldd34a3642015-12-02 18:23:26 +0100574
Johan Hovold57ccd4b2015-12-02 18:23:30 +0100575 dev_dbg(&svc->dev, "%s - id = %u\n", __func__, request->intf_id);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500576
Johan Hovold57ccd4b2015-12-02 18:23:30 +0100577 return gb_svc_queue_deferred_request(op);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500578}
579
580static int gb_svc_intf_reset_recv(struct gb_operation *op)
581{
Johan Hovold684156a2015-11-25 15:59:19 +0100582 struct gb_svc *svc = op->connection->private;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500583 struct gb_message *request = op->request;
584 struct gb_svc_intf_reset_request *reset;
585 u8 intf_id;
586
587 if (request->payload_size < sizeof(*reset)) {
Johan Hovold684156a2015-11-25 15:59:19 +0100588 dev_warn(&svc->dev, "short reset request received (%zu < %zu)\n",
589 request->payload_size, sizeof(*reset));
Alex Elder30c6d9d2015-05-22 13:02:08 -0500590 return -EINVAL;
591 }
592 reset = request->payload;
593
594 intf_id = reset->intf_id;
595
596 /* FIXME Reset the interface here */
597
598 return 0;
599}
600
601static int gb_svc_request_recv(u8 type, struct gb_operation *op)
602{
Viresh Kumar3ccb1602015-09-03 15:42:22 +0530603 struct gb_connection *connection = op->connection;
604 struct gb_svc *svc = connection->private;
605 int ret = 0;
606
607 /*
608 * SVC requests need to follow a specific order (at least initially) and
609 * below code takes care of enforcing that. The expected order is:
610 * - PROTOCOL_VERSION
611 * - SVC_HELLO
612 * - Any other request, but the earlier two.
613 *
614 * Incoming requests are guaranteed to be serialized and so we don't
615 * need to protect 'state' for any races.
616 */
Alex Elder30c6d9d2015-05-22 13:02:08 -0500617 switch (type) {
Viresh Kumar0e2462d2015-08-14 07:57:38 +0530618 case GB_REQUEST_TYPE_PROTOCOL_VERSION:
Viresh Kumar3ccb1602015-09-03 15:42:22 +0530619 if (svc->state != GB_SVC_STATE_RESET)
620 ret = -EINVAL;
621 break;
Viresh Kumaread35462015-07-21 17:44:19 +0530622 case GB_SVC_TYPE_SVC_HELLO:
Viresh Kumar3ccb1602015-09-03 15:42:22 +0530623 if (svc->state != GB_SVC_STATE_PROTOCOL_VERSION)
624 ret = -EINVAL;
625 break;
626 default:
627 if (svc->state != GB_SVC_STATE_SVC_HELLO)
628 ret = -EINVAL;
629 break;
630 }
631
632 if (ret) {
Johan Hovold684156a2015-11-25 15:59:19 +0100633 dev_warn(&svc->dev, "unexpected request 0x%02x received (state %u)\n",
634 type, svc->state);
Viresh Kumar3ccb1602015-09-03 15:42:22 +0530635 return ret;
636 }
637
638 switch (type) {
639 case GB_REQUEST_TYPE_PROTOCOL_VERSION:
640 ret = gb_svc_version_request(op);
641 if (!ret)
642 svc->state = GB_SVC_STATE_PROTOCOL_VERSION;
643 return ret;
644 case GB_SVC_TYPE_SVC_HELLO:
645 ret = gb_svc_hello(op);
646 if (!ret)
647 svc->state = GB_SVC_STATE_SVC_HELLO;
648 return ret;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500649 case GB_SVC_TYPE_INTF_HOTPLUG:
650 return gb_svc_intf_hotplug_recv(op);
651 case GB_SVC_TYPE_INTF_HOT_UNPLUG:
652 return gb_svc_intf_hot_unplug_recv(op);
653 case GB_SVC_TYPE_INTF_RESET:
654 return gb_svc_intf_reset_recv(op);
655 default:
Johan Hovold684156a2015-11-25 15:59:19 +0100656 dev_warn(&svc->dev, "unsupported request 0x%02x\n", type);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500657 return -EINVAL;
658 }
659}
660
Johan Hovoldefe6ef72015-11-25 15:59:06 +0100661static void gb_svc_release(struct device *dev)
662{
Johan Hovold88f7b962015-11-25 15:59:08 +0100663 struct gb_svc *svc = to_gb_svc(dev);
Johan Hovoldefe6ef72015-11-25 15:59:06 +0100664
Johan Hovold7adeaae72015-12-07 15:05:37 +0100665 if (svc->connection)
666 gb_connection_destroy(svc->connection);
Johan Hovoldefe6ef72015-11-25 15:59:06 +0100667 ida_destroy(&svc->device_id_map);
Johan Hovold3e48aca2015-12-02 18:23:31 +0100668 destroy_workqueue(svc->wq);
Johan Hovoldefe6ef72015-11-25 15:59:06 +0100669 kfree(svc);
670}
671
672struct device_type greybus_svc_type = {
673 .name = "greybus_svc",
674 .release = gb_svc_release,
675};
676
Johan Hovold7adeaae72015-12-07 15:05:37 +0100677struct gb_svc *gb_svc_create(struct gb_host_device *hd)
Alex Elder30c6d9d2015-05-22 13:02:08 -0500678{
679 struct gb_svc *svc;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500680
681 svc = kzalloc(sizeof(*svc), GFP_KERNEL);
682 if (!svc)
Johan Hovold7adeaae72015-12-07 15:05:37 +0100683 return NULL;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500684
Johan Hovold3e48aca2015-12-02 18:23:31 +0100685 svc->wq = alloc_workqueue("%s:svc", WQ_UNBOUND, 1, dev_name(&hd->dev));
686 if (!svc->wq) {
687 kfree(svc);
Johan Hovold7adeaae72015-12-07 15:05:37 +0100688 return NULL;
Johan Hovold3e48aca2015-12-02 18:23:31 +0100689 }
690
Johan Hovoldefe6ef72015-11-25 15:59:06 +0100691 svc->dev.parent = &hd->dev;
692 svc->dev.bus = &greybus_bus_type;
693 svc->dev.type = &greybus_svc_type;
Johan Hovold66069fb2015-11-25 15:59:09 +0100694 svc->dev.groups = svc_groups;
Johan Hovoldefe6ef72015-11-25 15:59:06 +0100695 svc->dev.dma_mask = svc->dev.parent->dma_mask;
696 device_initialize(&svc->dev);
697
698 dev_set_name(&svc->dev, "%d-svc", hd->bus_id);
699
Johan Hovold6106e512015-11-25 15:59:07 +0100700 ida_init(&svc->device_id_map);
Viresh Kumar3ccb1602015-09-03 15:42:22 +0530701 svc->state = GB_SVC_STATE_RESET;
Johan Hovoldf0960d02015-12-03 19:18:02 +0100702 svc->hd = hd;
Viresh Kumard3d44842015-07-21 17:44:18 +0530703
Johan Hovold7adeaae72015-12-07 15:05:37 +0100704 svc->connection = gb_connection_create_static(hd, GB_SVC_CPORT_ID,
705 GREYBUS_PROTOCOL_SVC);
706 if (!svc->connection) {
707 dev_err(&svc->dev, "failed to create connection\n");
708 put_device(&svc->dev);
709 return NULL;
710 }
711
712 svc->connection->private = svc;
713
714 return svc;
715}
716
717int gb_svc_add(struct gb_svc *svc)
718{
719 int ret;
720
721 /*
722 * The SVC protocol is currently driven by the SVC, so the SVC device
723 * is added from the connection request handler when enough
724 * information has been received.
725 */
726 ret = gb_connection_init(svc->connection);
727 if (ret)
728 return ret;
729
730 return 0;
731}
732
733void gb_svc_del(struct gb_svc *svc)
734{
735 /*
736 * The SVC device may have been registered from the request handler.
737 */
738 if (device_is_registered(&svc->dev))
739 device_del(&svc->dev);
740
741 gb_connection_exit(svc->connection);
742
743 flush_workqueue(svc->wq);
744}
745
746void gb_svc_put(struct gb_svc *svc)
747{
748 put_device(&svc->dev);
749}
750
751static int gb_svc_connection_init(struct gb_connection *connection)
752{
753 struct gb_svc *svc = connection->private;
754
755 dev_dbg(&svc->dev, "%s\n", __func__);
Johan Hovoldefe6ef72015-11-25 15:59:06 +0100756
Viresh Kumar18d777c2015-07-21 17:44:20 +0530757 return 0;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500758}
759
760static void gb_svc_connection_exit(struct gb_connection *connection)
761{
762 struct gb_svc *svc = connection->private;
763
Johan Hovold7adeaae72015-12-07 15:05:37 +0100764 dev_dbg(&svc->dev, "%s\n", __func__);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500765}
766
767static struct gb_protocol svc_protocol = {
768 .name = "svc",
769 .id = GREYBUS_PROTOCOL_SVC,
Viresh Kumar06e305f2015-07-01 12:13:51 +0530770 .major = GB_SVC_VERSION_MAJOR,
771 .minor = GB_SVC_VERSION_MINOR,
Alex Elder30c6d9d2015-05-22 13:02:08 -0500772 .connection_init = gb_svc_connection_init,
773 .connection_exit = gb_svc_connection_exit,
774 .request_recv = gb_svc_request_recv,
Viresh Kumar5a5296b2015-09-07 16:01:24 +0530775 .flags = GB_PROTOCOL_SKIP_CONTROL_CONNECTED |
776 GB_PROTOCOL_SKIP_CONTROL_DISCONNECTED |
Johan Hovold4ec15742015-11-25 15:59:13 +0100777 GB_PROTOCOL_SKIP_VERSION,
Alex Elder30c6d9d2015-05-22 13:02:08 -0500778};
Viresh Kumarab69c4c2015-07-03 17:00:29 +0530779gb_builtin_protocol_driver(svc_protocol);