blob: ce7740ef449babaf0f7bfd1afd2021cb6c7a7aec [file] [log] [blame]
Greg Kroah-Hartmaneb50fd32017-11-07 14:58:41 +01001// SPDX-License-Identifier: GPL-2.0
Alex Elder30c6d9d2015-05-22 13:02:08 -05002/*
3 * SVC Greybus driver.
4 *
5 * Copyright 2015 Google Inc.
6 * Copyright 2015 Linaro Ltd.
Alex Elder30c6d9d2015-05-22 13:02:08 -05007 */
8
David Lin95046772016-04-20 16:55:08 -07009#include <linux/debugfs.h>
Viresh Kumar067906f2015-08-06 12:44:55 +053010#include <linux/workqueue.h>
Greg Kroah-Hartmanec0ad862019-08-25 07:54:27 +020011#include <linux/greybus.h>
Viresh Kumarf66427a2015-09-02 21:27:13 +053012
Jeffrey Carlyle140026b2016-05-06 12:43:53 -070013#define SVC_INTF_EJECT_TIMEOUT 9000
14#define SVC_INTF_ACTIVATE_TIMEOUT 6000
Johan Hovold1f3e09e2016-08-26 12:59:45 +020015#define SVC_INTF_RESUME_TIMEOUT 3000
Johan Hovoldd18da862016-03-09 12:20:46 +010016
Johan Hovold9ae41092015-12-02 18:23:29 +010017struct gb_svc_deferred_request {
Viresh Kumar067906f2015-08-06 12:44:55 +053018 struct work_struct work;
Johan Hovold9ae41092015-12-02 18:23:29 +010019 struct gb_operation *operation;
Viresh Kumar067906f2015-08-06 12:44:55 +053020};
21
Mitchell Tasmanee2f2072016-05-04 17:30:23 -040022static int gb_svc_queue_deferred_request(struct gb_operation *operation);
23
Johan Hovold66069fb2015-11-25 15:59:09 +010024static ssize_t endo_id_show(struct device *dev,
Cristian Sicilia8478c352018-11-25 17:58:15 +010025 struct device_attribute *attr, char *buf)
Johan Hovold66069fb2015-11-25 15:59:09 +010026{
27 struct gb_svc *svc = to_gb_svc(dev);
28
29 return sprintf(buf, "0x%04x\n", svc->endo_id);
30}
31static DEVICE_ATTR_RO(endo_id);
32
33static ssize_t ap_intf_id_show(struct device *dev,
Cristian Sicilia8478c352018-11-25 17:58:15 +010034 struct device_attribute *attr, char *buf)
Johan Hovold66069fb2015-11-25 15:59:09 +010035{
36 struct gb_svc *svc = to_gb_svc(dev);
37
38 return sprintf(buf, "%u\n", svc->ap_intf_id);
39}
40static DEVICE_ATTR_RO(ap_intf_id);
41
Rui Miguel Silva2c92bd52016-01-11 13:46:33 +000042// FIXME
43// This is a hack, we need to do this "right" and clean the interface up
44// properly, not just forcibly yank the thing out of the system and hope for the
45// best. But for now, people want their modules to come out without having to
46// throw the thing to the ground or get out a screwdriver.
47static ssize_t intf_eject_store(struct device *dev,
48 struct device_attribute *attr, const char *buf,
49 size_t len)
50{
51 struct gb_svc *svc = to_gb_svc(dev);
52 unsigned short intf_id;
53 int ret;
54
55 ret = kstrtou16(buf, 10, &intf_id);
56 if (ret < 0)
57 return ret;
58
59 dev_warn(dev, "Forcibly trying to eject interface %d\n", intf_id);
60
61 ret = gb_svc_intf_eject(svc, intf_id);
62 if (ret < 0)
63 return ret;
64
65 return len;
66}
67static DEVICE_ATTR_WO(intf_eject);
68
Greg Kroah-Hartmand5628532016-01-26 15:17:08 -080069static ssize_t watchdog_show(struct device *dev, struct device_attribute *attr,
70 char *buf)
71{
72 struct gb_svc *svc = to_gb_svc(dev);
73
74 return sprintf(buf, "%s\n",
75 gb_svc_watchdog_enabled(svc) ? "enabled" : "disabled");
76}
77
78static ssize_t watchdog_store(struct device *dev,
79 struct device_attribute *attr, const char *buf,
80 size_t len)
81{
82 struct gb_svc *svc = to_gb_svc(dev);
83 int retval;
84 bool user_request;
85
86 retval = strtobool(buf, &user_request);
87 if (retval)
88 return retval;
89
90 if (user_request)
91 retval = gb_svc_watchdog_enable(svc);
92 else
93 retval = gb_svc_watchdog_disable(svc);
94 if (retval)
95 return retval;
96 return len;
97}
98static DEVICE_ATTR_RW(watchdog);
99
David Lin7c4a0ed2016-07-26 16:27:28 -0700100static ssize_t watchdog_action_show(struct device *dev,
101 struct device_attribute *attr, char *buf)
102{
103 struct gb_svc *svc = to_gb_svc(dev);
104
105 if (svc->action == GB_SVC_WATCHDOG_BITE_PANIC_KERNEL)
106 return sprintf(buf, "panic\n");
107 else if (svc->action == GB_SVC_WATCHDOG_BITE_RESET_UNIPRO)
108 return sprintf(buf, "reset\n");
109
110 return -EINVAL;
111}
112
113static ssize_t watchdog_action_store(struct device *dev,
114 struct device_attribute *attr,
115 const char *buf, size_t len)
116{
117 struct gb_svc *svc = to_gb_svc(dev);
118
119 if (sysfs_streq(buf, "panic"))
120 svc->action = GB_SVC_WATCHDOG_BITE_PANIC_KERNEL;
121 else if (sysfs_streq(buf, "reset"))
122 svc->action = GB_SVC_WATCHDOG_BITE_RESET_UNIPRO;
123 else
124 return -EINVAL;
125
126 return len;
127}
128static DEVICE_ATTR_RW(watchdog_action);
129
David Lin95046772016-04-20 16:55:08 -0700130static int gb_svc_pwrmon_rail_count_get(struct gb_svc *svc, u8 *value)
131{
132 struct gb_svc_pwrmon_rail_count_get_response response;
133 int ret;
134
135 ret = gb_operation_sync(svc->connection,
136 GB_SVC_TYPE_PWRMON_RAIL_COUNT_GET, NULL, 0,
137 &response, sizeof(response));
138 if (ret) {
Johan Hovold89f2df42016-04-21 11:43:36 +0200139 dev_err(&svc->dev, "failed to get rail count: %d\n", ret);
David Lin95046772016-04-20 16:55:08 -0700140 return ret;
141 }
142
143 *value = response.rail_count;
144
145 return 0;
146}
147
148static int gb_svc_pwrmon_rail_names_get(struct gb_svc *svc,
Johan Hovoldf35fdb22016-04-21 11:43:38 +0200149 struct gb_svc_pwrmon_rail_names_get_response *response,
150 size_t bufsize)
David Lin95046772016-04-20 16:55:08 -0700151{
152 int ret;
153
154 ret = gb_operation_sync(svc->connection,
155 GB_SVC_TYPE_PWRMON_RAIL_NAMES_GET, NULL, 0,
156 response, bufsize);
157 if (ret) {
Johan Hovold89f2df42016-04-21 11:43:36 +0200158 dev_err(&svc->dev, "failed to get rail names: %d\n", ret);
David Lin95046772016-04-20 16:55:08 -0700159 return ret;
160 }
161
David Lin8fb76c32016-06-08 09:39:00 +0200162 if (response->status != GB_SVC_OP_SUCCESS) {
163 dev_err(&svc->dev,
164 "SVC error while getting rail names: %u\n",
165 response->status);
166 return -EREMOTEIO;
167 }
168
David Lin95046772016-04-20 16:55:08 -0700169 return 0;
170}
171
172static int gb_svc_pwrmon_sample_get(struct gb_svc *svc, u8 rail_id,
173 u8 measurement_type, u32 *value)
174{
175 struct gb_svc_pwrmon_sample_get_request request;
176 struct gb_svc_pwrmon_sample_get_response response;
177 int ret;
178
179 request.rail_id = rail_id;
180 request.measurement_type = measurement_type;
181
182 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_PWRMON_SAMPLE_GET,
183 &request, sizeof(request),
184 &response, sizeof(response));
185 if (ret) {
Johan Hovold89f2df42016-04-21 11:43:36 +0200186 dev_err(&svc->dev, "failed to get rail sample: %d\n", ret);
David Lin95046772016-04-20 16:55:08 -0700187 return ret;
188 }
189
190 if (response.result) {
191 dev_err(&svc->dev,
192 "UniPro error while getting rail power sample (%d %d): %d\n",
193 rail_id, measurement_type, response.result);
194 switch (response.result) {
195 case GB_SVC_PWRMON_GET_SAMPLE_INVAL:
196 return -EINVAL;
197 case GB_SVC_PWRMON_GET_SAMPLE_NOSUPP:
Johan Hovold5b35ef92016-04-21 11:43:37 +0200198 return -ENOMSG;
David Lin95046772016-04-20 16:55:08 -0700199 default:
Johan Hovold4aea5a12016-05-19 16:20:16 +0200200 return -EREMOTEIO;
David Lin95046772016-04-20 16:55:08 -0700201 }
202 }
203
204 *value = le32_to_cpu(response.measurement);
205
206 return 0;
207}
208
David Linddb10c82016-04-07 20:15:30 -0700209int gb_svc_pwrmon_intf_sample_get(struct gb_svc *svc, u8 intf_id,
210 u8 measurement_type, u32 *value)
211{
212 struct gb_svc_pwrmon_intf_sample_get_request request;
213 struct gb_svc_pwrmon_intf_sample_get_response response;
214 int ret;
215
216 request.intf_id = intf_id;
217 request.measurement_type = measurement_type;
218
219 ret = gb_operation_sync(svc->connection,
220 GB_SVC_TYPE_PWRMON_INTF_SAMPLE_GET,
221 &request, sizeof(request),
222 &response, sizeof(response));
223 if (ret) {
Johan Hovold89f2df42016-04-21 11:43:36 +0200224 dev_err(&svc->dev, "failed to get intf sample: %d\n", ret);
David Linddb10c82016-04-07 20:15:30 -0700225 return ret;
226 }
227
228 if (response.result) {
229 dev_err(&svc->dev,
230 "UniPro error while getting intf power sample (%d %d): %d\n",
231 intf_id, measurement_type, response.result);
232 switch (response.result) {
233 case GB_SVC_PWRMON_GET_SAMPLE_INVAL:
234 return -EINVAL;
235 case GB_SVC_PWRMON_GET_SAMPLE_NOSUPP:
Johan Hovold0bba4fb2016-05-19 16:20:14 +0200236 return -ENOMSG;
David Linddb10c82016-04-07 20:15:30 -0700237 default:
Johan Hovold4aea5a12016-05-19 16:20:16 +0200238 return -EREMOTEIO;
David Linddb10c82016-04-07 20:15:30 -0700239 }
240 }
241
242 *value = le32_to_cpu(response.measurement);
243
244 return 0;
245}
246
Johan Hovold66069fb2015-11-25 15:59:09 +0100247static struct attribute *svc_attrs[] = {
248 &dev_attr_endo_id.attr,
249 &dev_attr_ap_intf_id.attr,
Rui Miguel Silva2c92bd52016-01-11 13:46:33 +0000250 &dev_attr_intf_eject.attr,
Greg Kroah-Hartmand5628532016-01-26 15:17:08 -0800251 &dev_attr_watchdog.attr,
David Lin7c4a0ed2016-07-26 16:27:28 -0700252 &dev_attr_watchdog_action.attr,
Johan Hovold66069fb2015-11-25 15:59:09 +0100253 NULL,
254};
255ATTRIBUTE_GROUPS(svc);
256
Johan Hovold4d5f6212016-03-29 18:56:04 -0400257int gb_svc_intf_device_id(struct gb_svc *svc, u8 intf_id, u8 device_id)
Alex Elder30c6d9d2015-05-22 13:02:08 -0500258{
259 struct gb_svc_intf_device_id_request request;
260
261 request.intf_id = intf_id;
262 request.device_id = device_id;
263
264 return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_DEVICE_ID,
265 &request, sizeof(request), NULL, 0);
266}
267
Rui Miguel Silvac5d55fb2016-01-11 13:46:31 +0000268int gb_svc_intf_eject(struct gb_svc *svc, u8 intf_id)
269{
270 struct gb_svc_intf_eject_request request;
Johan Hovolde676ccd2016-03-09 12:20:45 +0100271 int ret;
Rui Miguel Silvac5d55fb2016-01-11 13:46:31 +0000272
273 request.intf_id = intf_id;
274
275 /*
276 * The pulse width for module release in svc is long so we need to
277 * increase the timeout so the operation will not return to soon.
278 */
Johan Hovolde676ccd2016-03-09 12:20:45 +0100279 ret = gb_operation_sync_timeout(svc->connection,
280 GB_SVC_TYPE_INTF_EJECT, &request,
281 sizeof(request), NULL, 0,
Johan Hovoldd18da862016-03-09 12:20:46 +0100282 SVC_INTF_EJECT_TIMEOUT);
Johan Hovolde676ccd2016-03-09 12:20:45 +0100283 if (ret) {
284 dev_err(&svc->dev, "failed to eject interface %u\n", intf_id);
285 return ret;
286 }
287
288 return 0;
Rui Miguel Silvac5d55fb2016-01-11 13:46:31 +0000289}
Rui Miguel Silvac5d55fb2016-01-11 13:46:31 +0000290
Johan Hovold017482b2016-04-23 18:47:27 +0200291int gb_svc_intf_vsys_set(struct gb_svc *svc, u8 intf_id, bool enable)
292{
Jeffrey Carlyle144763b2016-05-06 12:43:52 -0700293 struct gb_svc_intf_vsys_request request;
294 struct gb_svc_intf_vsys_response response;
295 int type, ret;
Johan Hovold017482b2016-04-23 18:47:27 +0200296
Jeffrey Carlyle144763b2016-05-06 12:43:52 -0700297 request.intf_id = intf_id;
298
299 if (enable)
300 type = GB_SVC_TYPE_INTF_VSYS_ENABLE;
301 else
302 type = GB_SVC_TYPE_INTF_VSYS_DISABLE;
303
304 ret = gb_operation_sync(svc->connection, type,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100305 &request, sizeof(request),
306 &response, sizeof(response));
Jeffrey Carlyle144763b2016-05-06 12:43:52 -0700307 if (ret < 0)
308 return ret;
309 if (response.result_code != GB_SVC_INTF_VSYS_OK)
310 return -EREMOTEIO;
Johan Hovold017482b2016-04-23 18:47:27 +0200311 return 0;
312}
313
314int gb_svc_intf_refclk_set(struct gb_svc *svc, u8 intf_id, bool enable)
315{
Jeffrey Carlyle144763b2016-05-06 12:43:52 -0700316 struct gb_svc_intf_refclk_request request;
317 struct gb_svc_intf_refclk_response response;
318 int type, ret;
Johan Hovold017482b2016-04-23 18:47:27 +0200319
Jeffrey Carlyle144763b2016-05-06 12:43:52 -0700320 request.intf_id = intf_id;
321
322 if (enable)
323 type = GB_SVC_TYPE_INTF_REFCLK_ENABLE;
324 else
325 type = GB_SVC_TYPE_INTF_REFCLK_DISABLE;
326
327 ret = gb_operation_sync(svc->connection, type,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100328 &request, sizeof(request),
329 &response, sizeof(response));
Jeffrey Carlyle144763b2016-05-06 12:43:52 -0700330 if (ret < 0)
331 return ret;
332 if (response.result_code != GB_SVC_INTF_REFCLK_OK)
333 return -EREMOTEIO;
Johan Hovold017482b2016-04-23 18:47:27 +0200334 return 0;
335}
336
337int gb_svc_intf_unipro_set(struct gb_svc *svc, u8 intf_id, bool enable)
338{
Jeffrey Carlyle144763b2016-05-06 12:43:52 -0700339 struct gb_svc_intf_unipro_request request;
340 struct gb_svc_intf_unipro_response response;
341 int type, ret;
Johan Hovold017482b2016-04-23 18:47:27 +0200342
Jeffrey Carlyle144763b2016-05-06 12:43:52 -0700343 request.intf_id = intf_id;
344
345 if (enable)
346 type = GB_SVC_TYPE_INTF_UNIPRO_ENABLE;
347 else
348 type = GB_SVC_TYPE_INTF_UNIPRO_DISABLE;
349
350 ret = gb_operation_sync(svc->connection, type,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100351 &request, sizeof(request),
352 &response, sizeof(response));
Jeffrey Carlyle144763b2016-05-06 12:43:52 -0700353 if (ret < 0)
354 return ret;
355 if (response.result_code != GB_SVC_INTF_UNIPRO_OK)
356 return -EREMOTEIO;
Johan Hovold017482b2016-04-23 18:47:27 +0200357 return 0;
358}
359
Johan Hovold1e8e22b2016-04-23 18:47:28 +0200360int gb_svc_intf_activate(struct gb_svc *svc, u8 intf_id, u8 *intf_type)
361{
Jeffrey Carlyle140026b2016-05-06 12:43:53 -0700362 struct gb_svc_intf_activate_request request;
363 struct gb_svc_intf_activate_response response;
364 int ret;
Johan Hovold1e8e22b2016-04-23 18:47:28 +0200365
Jeffrey Carlyle140026b2016-05-06 12:43:53 -0700366 request.intf_id = intf_id;
367
368 ret = gb_operation_sync_timeout(svc->connection,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100369 GB_SVC_TYPE_INTF_ACTIVATE,
370 &request, sizeof(request),
371 &response, sizeof(response),
372 SVC_INTF_ACTIVATE_TIMEOUT);
Jeffrey Carlyle140026b2016-05-06 12:43:53 -0700373 if (ret < 0)
374 return ret;
Jeffrey Carlyle03fba182016-05-11 10:08:55 -0700375 if (response.status != GB_SVC_OP_SUCCESS) {
376 dev_err(&svc->dev, "failed to activate interface %u: %u\n",
Cristian Sicilia8478c352018-11-25 17:58:15 +0100377 intf_id, response.status);
Jeffrey Carlyle03fba182016-05-11 10:08:55 -0700378 return -EREMOTEIO;
379 }
Jeffrey Carlyle140026b2016-05-06 12:43:53 -0700380
381 *intf_type = response.intf_type;
Johan Hovold1e8e22b2016-04-23 18:47:28 +0200382
383 return 0;
384}
385
David Linfc8a4022016-07-07 22:07:00 -0500386int gb_svc_intf_resume(struct gb_svc *svc, u8 intf_id)
387{
388 struct gb_svc_intf_resume_request request;
389 struct gb_svc_intf_resume_response response;
390 int ret;
391
392 request.intf_id = intf_id;
393
394 ret = gb_operation_sync_timeout(svc->connection,
395 GB_SVC_TYPE_INTF_RESUME,
396 &request, sizeof(request),
397 &response, sizeof(response),
398 SVC_INTF_RESUME_TIMEOUT);
399 if (ret < 0) {
400 dev_err(&svc->dev, "failed to send interface resume %u: %d\n",
401 intf_id, ret);
402 return ret;
403 }
404
405 if (response.status != GB_SVC_OP_SUCCESS) {
406 dev_err(&svc->dev, "failed to resume interface %u: %u\n",
407 intf_id, response.status);
408 return -EREMOTEIO;
409 }
410
411 return 0;
412}
413
Viresh Kumar19151c32015-09-09 21:08:29 +0530414int gb_svc_dme_peer_get(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
415 u32 *value)
416{
417 struct gb_svc_dme_peer_get_request request;
418 struct gb_svc_dme_peer_get_response response;
419 u16 result;
420 int ret;
421
422 request.intf_id = intf_id;
423 request.attr = cpu_to_le16(attr);
424 request.selector = cpu_to_le16(selector);
425
426 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_GET,
427 &request, sizeof(request),
428 &response, sizeof(response));
429 if (ret) {
Viresh Kumarb933fa42015-12-04 21:30:10 +0530430 dev_err(&svc->dev, "failed to get DME attribute (%u 0x%04x %u): %d\n",
Cristian Sicilia8478c352018-11-25 17:58:15 +0100431 intf_id, attr, selector, ret);
Viresh Kumar19151c32015-09-09 21:08:29 +0530432 return ret;
433 }
434
435 result = le16_to_cpu(response.result_code);
436 if (result) {
Viresh Kumarb933fa42015-12-04 21:30:10 +0530437 dev_err(&svc->dev, "UniPro error while getting DME attribute (%u 0x%04x %u): %u\n",
Cristian Sicilia8478c352018-11-25 17:58:15 +0100438 intf_id, attr, selector, result);
Johan Hovold4aea5a12016-05-19 16:20:16 +0200439 return -EREMOTEIO;
Viresh Kumar19151c32015-09-09 21:08:29 +0530440 }
441
442 if (value)
443 *value = le32_to_cpu(response.attr_value);
444
445 return 0;
446}
Viresh Kumar19151c32015-09-09 21:08:29 +0530447
448int gb_svc_dme_peer_set(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
449 u32 value)
450{
451 struct gb_svc_dme_peer_set_request request;
452 struct gb_svc_dme_peer_set_response response;
453 u16 result;
454 int ret;
455
456 request.intf_id = intf_id;
457 request.attr = cpu_to_le16(attr);
458 request.selector = cpu_to_le16(selector);
459 request.value = cpu_to_le32(value);
460
461 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_SET,
462 &request, sizeof(request),
463 &response, sizeof(response));
464 if (ret) {
Viresh Kumarb933fa42015-12-04 21:30:10 +0530465 dev_err(&svc->dev, "failed to set DME attribute (%u 0x%04x %u %u): %d\n",
Cristian Sicilia8478c352018-11-25 17:58:15 +0100466 intf_id, attr, selector, value, ret);
Viresh Kumar19151c32015-09-09 21:08:29 +0530467 return ret;
468 }
469
470 result = le16_to_cpu(response.result_code);
471 if (result) {
Viresh Kumarb933fa42015-12-04 21:30:10 +0530472 dev_err(&svc->dev, "UniPro error while setting DME attribute (%u 0x%04x %u %u): %u\n",
Cristian Sicilia8478c352018-11-25 17:58:15 +0100473 intf_id, attr, selector, value, result);
Johan Hovold4aea5a12016-05-19 16:20:16 +0200474 return -EREMOTEIO;
Viresh Kumar19151c32015-09-09 21:08:29 +0530475 }
476
477 return 0;
478}
Viresh Kumar19151c32015-09-09 21:08:29 +0530479
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530480int gb_svc_connection_create(struct gb_svc *svc,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100481 u8 intf1_id, u16 cport1_id,
482 u8 intf2_id, u16 cport2_id,
483 u8 cport_flags)
Alex Elder30c6d9d2015-05-22 13:02:08 -0500484{
485 struct gb_svc_conn_create_request request;
486
487 request.intf1_id = intf1_id;
Rui Miguel Silva2498050b2015-09-15 15:33:51 +0100488 request.cport1_id = cpu_to_le16(cport1_id);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500489 request.intf2_id = intf2_id;
Rui Miguel Silva2498050b2015-09-15 15:33:51 +0100490 request.cport2_id = cpu_to_le16(cport2_id);
Johan Hovold34145b62016-03-03 13:34:37 +0100491 request.tc = 0; /* TC0 */
Johan Hovold27f25c12016-03-03 13:34:38 +0100492 request.flags = cport_flags;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500493
494 return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_CREATE,
495 &request, sizeof(request), NULL, 0);
496}
497
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530498void gb_svc_connection_destroy(struct gb_svc *svc, u8 intf1_id, u16 cport1_id,
499 u8 intf2_id, u16 cport2_id)
Alex Elder30c6d9d2015-05-22 13:02:08 -0500500{
501 struct gb_svc_conn_destroy_request request;
Viresh Kumard9fcfff2015-08-31 17:21:05 +0530502 struct gb_connection *connection = svc->connection;
503 int ret;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500504
505 request.intf1_id = intf1_id;
Rui Miguel Silva2498050b2015-09-15 15:33:51 +0100506 request.cport1_id = cpu_to_le16(cport1_id);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500507 request.intf2_id = intf2_id;
Rui Miguel Silva2498050b2015-09-15 15:33:51 +0100508 request.cport2_id = cpu_to_le16(cport2_id);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500509
Viresh Kumard9fcfff2015-08-31 17:21:05 +0530510 ret = gb_operation_sync(connection, GB_SVC_TYPE_CONN_DESTROY,
511 &request, sizeof(request), NULL, 0);
Johan Hovold684156a2015-11-25 15:59:19 +0100512 if (ret) {
Viresh Kumar2f3db922015-12-04 21:30:09 +0530513 dev_err(&svc->dev, "failed to destroy connection (%u:%u %u:%u): %d\n",
Cristian Sicilia8478c352018-11-25 17:58:15 +0100514 intf1_id, cport1_id, intf2_id, cport2_id, ret);
Johan Hovold684156a2015-11-25 15:59:19 +0100515 }
Alex Elder30c6d9d2015-05-22 13:02:08 -0500516}
517
Viresh Kumarbb106852015-09-07 16:01:25 +0530518/* Creates bi-directional routes between the devices */
Johan Hovold4d5f6212016-03-29 18:56:04 -0400519int gb_svc_route_create(struct gb_svc *svc, u8 intf1_id, u8 dev1_id,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100520 u8 intf2_id, u8 dev2_id)
Perry Hunge08aaa42015-07-24 19:02:31 -0400521{
522 struct gb_svc_route_create_request request;
523
524 request.intf1_id = intf1_id;
525 request.dev1_id = dev1_id;
526 request.intf2_id = intf2_id;
527 request.dev2_id = dev2_id;
528
529 return gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_CREATE,
530 &request, sizeof(request), NULL, 0);
531}
Perry Hunge08aaa42015-07-24 19:02:31 -0400532
Viresh Kumar0a020572015-09-07 18:05:26 +0530533/* Destroys bi-directional routes between the devices */
Johan Hovold4d5f6212016-03-29 18:56:04 -0400534void gb_svc_route_destroy(struct gb_svc *svc, u8 intf1_id, u8 intf2_id)
Viresh Kumar0a020572015-09-07 18:05:26 +0530535{
536 struct gb_svc_route_destroy_request request;
537 int ret;
538
539 request.intf1_id = intf1_id;
540 request.intf2_id = intf2_id;
541
542 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_DESTROY,
543 &request, sizeof(request), NULL, 0);
Johan Hovold684156a2015-11-25 15:59:19 +0100544 if (ret) {
Viresh Kumar2f3db922015-12-04 21:30:09 +0530545 dev_err(&svc->dev, "failed to destroy route (%u %u): %d\n",
Cristian Sicilia8478c352018-11-25 17:58:15 +0100546 intf1_id, intf2_id, ret);
Johan Hovold684156a2015-11-25 15:59:19 +0100547 }
Viresh Kumar0a020572015-09-07 18:05:26 +0530548}
549
Laurent Pinchartaab4a1a2016-01-06 16:16:46 +0200550int gb_svc_intf_set_power_mode(struct gb_svc *svc, u8 intf_id, u8 hs_series,
551 u8 tx_mode, u8 tx_gear, u8 tx_nlanes,
Eli Sennesh8c2522d2016-06-03 11:24:44 -0400552 u8 tx_amplitude, u8 tx_hs_equalizer,
Laurent Pinchartaab4a1a2016-01-06 16:16:46 +0200553 u8 rx_mode, u8 rx_gear, u8 rx_nlanes,
Eli Sennesh8c2522d2016-06-03 11:24:44 -0400554 u8 flags, u32 quirks,
555 struct gb_svc_l2_timer_cfg *local,
556 struct gb_svc_l2_timer_cfg *remote)
Laurent Pinchart784f8762015-12-18 21:23:22 +0200557{
Laurent Pinchartaab4a1a2016-01-06 16:16:46 +0200558 struct gb_svc_intf_set_pwrm_request request;
559 struct gb_svc_intf_set_pwrm_response response;
560 int ret;
Eli Sennesh8c2522d2016-06-03 11:24:44 -0400561 u16 result_code;
562
563 memset(&request, 0, sizeof(request));
Laurent Pinchart784f8762015-12-18 21:23:22 +0200564
565 request.intf_id = intf_id;
Laurent Pinchartaab4a1a2016-01-06 16:16:46 +0200566 request.hs_series = hs_series;
567 request.tx_mode = tx_mode;
568 request.tx_gear = tx_gear;
569 request.tx_nlanes = tx_nlanes;
Eli Sennesh8c2522d2016-06-03 11:24:44 -0400570 request.tx_amplitude = tx_amplitude;
571 request.tx_hs_equalizer = tx_hs_equalizer;
Laurent Pinchartaab4a1a2016-01-06 16:16:46 +0200572 request.rx_mode = rx_mode;
573 request.rx_gear = rx_gear;
574 request.rx_nlanes = rx_nlanes;
Laurent Pinchart784f8762015-12-18 21:23:22 +0200575 request.flags = flags;
Laurent Pinchartaab4a1a2016-01-06 16:16:46 +0200576 request.quirks = cpu_to_le32(quirks);
Eli Sennesh8c2522d2016-06-03 11:24:44 -0400577 if (local)
578 request.local_l2timerdata = *local;
579 if (remote)
580 request.remote_l2timerdata = *remote;
Laurent Pinchart784f8762015-12-18 21:23:22 +0200581
Laurent Pinchartaab4a1a2016-01-06 16:16:46 +0200582 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_SET_PWRM,
583 &request, sizeof(request),
584 &response, sizeof(response));
585 if (ret < 0)
586 return ret;
587
Eli Sennesh8c2522d2016-06-03 11:24:44 -0400588 result_code = response.result_code;
589 if (result_code != GB_SVC_SETPWRM_PWR_LOCAL) {
590 dev_err(&svc->dev, "set power mode = %d\n", result_code);
591 return -EIO;
592 }
593
594 return 0;
Laurent Pinchart784f8762015-12-18 21:23:22 +0200595}
Greg Kroah-Hartman46bb6472016-08-18 21:30:19 +0200596EXPORT_SYMBOL_GPL(gb_svc_intf_set_power_mode);
Laurent Pinchart784f8762015-12-18 21:23:22 +0200597
David Linc7dc28f2016-07-07 22:07:00 -0500598int gb_svc_intf_set_power_mode_hibernate(struct gb_svc *svc, u8 intf_id)
599{
600 struct gb_svc_intf_set_pwrm_request request;
601 struct gb_svc_intf_set_pwrm_response response;
602 int ret;
603 u16 result_code;
604
605 memset(&request, 0, sizeof(request));
606
607 request.intf_id = intf_id;
608 request.hs_series = GB_SVC_UNIPRO_HS_SERIES_A;
609 request.tx_mode = GB_SVC_UNIPRO_HIBERNATE_MODE;
610 request.rx_mode = GB_SVC_UNIPRO_HIBERNATE_MODE;
611
612 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_SET_PWRM,
613 &request, sizeof(request),
614 &response, sizeof(response));
615 if (ret < 0) {
616 dev_err(&svc->dev,
617 "failed to send set power mode operation to interface %u: %d\n",
618 intf_id, ret);
619 return ret;
620 }
621
622 result_code = response.result_code;
623 if (result_code != GB_SVC_SETPWRM_PWR_OK) {
624 dev_err(&svc->dev,
625 "failed to hibernate the link for interface %u: %u\n",
626 intf_id, result_code);
627 return -EIO;
628 }
629
630 return 0;
631}
632
Greg Kroah-Hartman55ec09e2016-01-19 23:30:42 -0800633int gb_svc_ping(struct gb_svc *svc)
634{
Greg Kroah-Hartman839ac5b2016-01-26 08:57:50 -0800635 return gb_operation_sync_timeout(svc->connection, GB_SVC_TYPE_PING,
636 NULL, 0, NULL, 0,
637 GB_OPERATION_TIMEOUT_DEFAULT * 2);
Greg Kroah-Hartman55ec09e2016-01-19 23:30:42 -0800638}
Greg Kroah-Hartman55ec09e2016-01-19 23:30:42 -0800639
Viresh Kumaread35462015-07-21 17:44:19 +0530640static int gb_svc_version_request(struct gb_operation *op)
641{
642 struct gb_connection *connection = op->connection;
Greg Kroah-Hartman0ec30632016-03-22 14:30:35 -0400643 struct gb_svc *svc = gb_connection_get_data(connection);
Johan Hovolda2cf2e52016-04-29 17:08:36 +0200644 struct gb_svc_version_request *request;
645 struct gb_svc_version_response *response;
Viresh Kumaread35462015-07-21 17:44:19 +0530646
Johan Hovold55510842015-11-19 18:28:01 +0100647 if (op->request->payload_size < sizeof(*request)) {
Johan Hovold684156a2015-11-25 15:59:19 +0100648 dev_err(&svc->dev, "short version request (%zu < %zu)\n",
Cristian Sicilia8478c352018-11-25 17:58:15 +0100649 op->request->payload_size,
650 sizeof(*request));
Johan Hovold55510842015-11-19 18:28:01 +0100651 return -EINVAL;
652 }
653
Johan Hovoldcfb16902015-09-15 10:48:01 +0200654 request = op->request->payload;
Viresh Kumaread35462015-07-21 17:44:19 +0530655
Johan Hovoldcfb16902015-09-15 10:48:01 +0200656 if (request->major > GB_SVC_VERSION_MAJOR) {
Viresh Kumar2f3db922015-12-04 21:30:09 +0530657 dev_warn(&svc->dev, "unsupported major version (%u > %u)\n",
Cristian Sicilia8478c352018-11-25 17:58:15 +0100658 request->major, GB_SVC_VERSION_MAJOR);
Viresh Kumaread35462015-07-21 17:44:19 +0530659 return -ENOTSUPP;
660 }
661
Johan Hovold357de002016-01-19 12:51:19 +0100662 svc->protocol_major = request->major;
663 svc->protocol_minor = request->minor;
Viresh Kumar3ea959e32015-08-11 07:36:14 +0530664
Johan Hovold684156a2015-11-25 15:59:19 +0100665 if (!gb_operation_response_alloc(op, sizeof(*response), GFP_KERNEL))
Viresh Kumaread35462015-07-21 17:44:19 +0530666 return -ENOMEM;
Viresh Kumaread35462015-07-21 17:44:19 +0530667
Johan Hovoldcfb16902015-09-15 10:48:01 +0200668 response = op->response->payload;
Johan Hovold357de002016-01-19 12:51:19 +0100669 response->major = svc->protocol_major;
670 response->minor = svc->protocol_minor;
Johan Hovold59832932015-09-15 10:48:00 +0200671
Viresh Kumaread35462015-07-21 17:44:19 +0530672 return 0;
673}
674
David Lin95046772016-04-20 16:55:08 -0700675static ssize_t pwr_debugfs_voltage_read(struct file *file, char __user *buf,
676 size_t len, loff_t *offset)
677{
Gioh Kim461ab802017-02-09 17:30:11 +0100678 struct svc_debugfs_pwrmon_rail *pwrmon_rails =
679 file_inode(file)->i_private;
David Lin95046772016-04-20 16:55:08 -0700680 struct gb_svc *svc = pwrmon_rails->svc;
681 int ret, desc;
682 u32 value;
683 char buff[16];
684
685 ret = gb_svc_pwrmon_sample_get(svc, pwrmon_rails->id,
686 GB_SVC_PWRMON_TYPE_VOL, &value);
687 if (ret) {
688 dev_err(&svc->dev,
Johan Hovold89f2df42016-04-21 11:43:36 +0200689 "failed to get voltage sample %u: %d\n",
690 pwrmon_rails->id, ret);
David Lin95046772016-04-20 16:55:08 -0700691 return ret;
692 }
693
694 desc = scnprintf(buff, sizeof(buff), "%u\n", value);
695
696 return simple_read_from_buffer(buf, len, offset, buff, desc);
697}
698
699static ssize_t pwr_debugfs_current_read(struct file *file, char __user *buf,
700 size_t len, loff_t *offset)
701{
Gioh Kim461ab802017-02-09 17:30:11 +0100702 struct svc_debugfs_pwrmon_rail *pwrmon_rails =
703 file_inode(file)->i_private;
David Lin95046772016-04-20 16:55:08 -0700704 struct gb_svc *svc = pwrmon_rails->svc;
705 int ret, desc;
706 u32 value;
707 char buff[16];
708
709 ret = gb_svc_pwrmon_sample_get(svc, pwrmon_rails->id,
710 GB_SVC_PWRMON_TYPE_CURR, &value);
711 if (ret) {
712 dev_err(&svc->dev,
Johan Hovold89f2df42016-04-21 11:43:36 +0200713 "failed to get current sample %u: %d\n",
714 pwrmon_rails->id, ret);
David Lin95046772016-04-20 16:55:08 -0700715 return ret;
716 }
717
718 desc = scnprintf(buff, sizeof(buff), "%u\n", value);
719
720 return simple_read_from_buffer(buf, len, offset, buff, desc);
721}
722
723static ssize_t pwr_debugfs_power_read(struct file *file, char __user *buf,
724 size_t len, loff_t *offset)
725{
Gioh Kim461ab802017-02-09 17:30:11 +0100726 struct svc_debugfs_pwrmon_rail *pwrmon_rails =
727 file_inode(file)->i_private;
David Lin95046772016-04-20 16:55:08 -0700728 struct gb_svc *svc = pwrmon_rails->svc;
729 int ret, desc;
730 u32 value;
731 char buff[16];
732
733 ret = gb_svc_pwrmon_sample_get(svc, pwrmon_rails->id,
734 GB_SVC_PWRMON_TYPE_PWR, &value);
735 if (ret) {
Johan Hovold89f2df42016-04-21 11:43:36 +0200736 dev_err(&svc->dev, "failed to get power sample %u: %d\n",
737 pwrmon_rails->id, ret);
David Lin95046772016-04-20 16:55:08 -0700738 return ret;
739 }
740
741 desc = scnprintf(buff, sizeof(buff), "%u\n", value);
742
743 return simple_read_from_buffer(buf, len, offset, buff, desc);
744}
745
746static const struct file_operations pwrmon_debugfs_voltage_fops = {
747 .read = pwr_debugfs_voltage_read,
748};
749
750static const struct file_operations pwrmon_debugfs_current_fops = {
751 .read = pwr_debugfs_current_read,
752};
753
754static const struct file_operations pwrmon_debugfs_power_fops = {
755 .read = pwr_debugfs_power_read,
756};
757
Johan Hovold12185192016-04-23 18:47:20 +0200758static void gb_svc_pwrmon_debugfs_init(struct gb_svc *svc)
David Lin95046772016-04-20 16:55:08 -0700759{
760 int i;
761 size_t bufsize;
762 struct dentry *dent;
David Lin3fd747a2016-04-22 19:03:42 -0700763 struct gb_svc_pwrmon_rail_names_get_response *rail_names;
764 u8 rail_count;
David Lin95046772016-04-20 16:55:08 -0700765
766 dent = debugfs_create_dir("pwrmon", svc->debugfs_dentry);
767 if (IS_ERR_OR_NULL(dent))
768 return;
769
David Lin3fd747a2016-04-22 19:03:42 -0700770 if (gb_svc_pwrmon_rail_count_get(svc, &rail_count))
David Lin95046772016-04-20 16:55:08 -0700771 goto err_pwrmon_debugfs;
772
David Lin3fd747a2016-04-22 19:03:42 -0700773 if (!rail_count || rail_count > GB_SVC_PWRMON_MAX_RAIL_COUNT)
David Lin95046772016-04-20 16:55:08 -0700774 goto err_pwrmon_debugfs;
775
David Lin8fb76c32016-06-08 09:39:00 +0200776 bufsize = sizeof(*rail_names) +
777 GB_SVC_PWRMON_RAIL_NAME_BUFSIZE * rail_count;
David Lin95046772016-04-20 16:55:08 -0700778
David Lin3fd747a2016-04-22 19:03:42 -0700779 rail_names = kzalloc(bufsize, GFP_KERNEL);
780 if (!rail_names)
David Lin95046772016-04-20 16:55:08 -0700781 goto err_pwrmon_debugfs;
782
David Lin3fd747a2016-04-22 19:03:42 -0700783 svc->pwrmon_rails = kcalloc(rail_count, sizeof(*svc->pwrmon_rails),
David Lin95046772016-04-20 16:55:08 -0700784 GFP_KERNEL);
785 if (!svc->pwrmon_rails)
786 goto err_pwrmon_debugfs_free;
787
David Lin3fd747a2016-04-22 19:03:42 -0700788 if (gb_svc_pwrmon_rail_names_get(svc, rail_names, bufsize))
David Lin95046772016-04-20 16:55:08 -0700789 goto err_pwrmon_debugfs_free;
790
David Lin3fd747a2016-04-22 19:03:42 -0700791 for (i = 0; i < rail_count; i++) {
David Lin95046772016-04-20 16:55:08 -0700792 struct dentry *dir;
793 struct svc_debugfs_pwrmon_rail *rail = &svc->pwrmon_rails[i];
794 char fname[GB_SVC_PWRMON_RAIL_NAME_BUFSIZE];
795
796 snprintf(fname, sizeof(fname), "%s",
David Lin3fd747a2016-04-22 19:03:42 -0700797 (char *)&rail_names->name[i]);
David Lin95046772016-04-20 16:55:08 -0700798
799 rail->id = i;
800 rail->svc = svc;
801
802 dir = debugfs_create_dir(fname, dent);
Gioh Kim563a8412017-02-09 17:30:12 +0100803 debugfs_create_file("voltage_now", 0444, dir, rail,
David Lin95046772016-04-20 16:55:08 -0700804 &pwrmon_debugfs_voltage_fops);
Gioh Kim563a8412017-02-09 17:30:12 +0100805 debugfs_create_file("current_now", 0444, dir, rail,
David Lin95046772016-04-20 16:55:08 -0700806 &pwrmon_debugfs_current_fops);
Gioh Kim563a8412017-02-09 17:30:12 +0100807 debugfs_create_file("power_now", 0444, dir, rail,
David Lin95046772016-04-20 16:55:08 -0700808 &pwrmon_debugfs_power_fops);
Alex Elder898d75f2016-05-24 13:34:52 -0500809 }
David Lin3fd747a2016-04-22 19:03:42 -0700810
811 kfree(rail_names);
David Lin95046772016-04-20 16:55:08 -0700812 return;
813
814err_pwrmon_debugfs_free:
David Lin3fd747a2016-04-22 19:03:42 -0700815 kfree(rail_names);
David Lin95046772016-04-20 16:55:08 -0700816 kfree(svc->pwrmon_rails);
817 svc->pwrmon_rails = NULL;
818
819err_pwrmon_debugfs:
820 debugfs_remove(dent);
821}
822
Johan Hovold12185192016-04-23 18:47:20 +0200823static void gb_svc_debugfs_init(struct gb_svc *svc)
David Lin95046772016-04-20 16:55:08 -0700824{
825 svc->debugfs_dentry = debugfs_create_dir(dev_name(&svc->dev),
826 gb_debugfs_get());
Johan Hovold12185192016-04-23 18:47:20 +0200827 gb_svc_pwrmon_debugfs_init(svc);
David Lin95046772016-04-20 16:55:08 -0700828}
829
Johan Hovold12185192016-04-23 18:47:20 +0200830static void gb_svc_debugfs_exit(struct gb_svc *svc)
David Lin95046772016-04-20 16:55:08 -0700831{
832 debugfs_remove_recursive(svc->debugfs_dentry);
David Lin9983ea62016-04-22 19:03:43 -0700833 kfree(svc->pwrmon_rails);
834 svc->pwrmon_rails = NULL;
David Lin95046772016-04-20 16:55:08 -0700835}
836
Viresh Kumaread35462015-07-21 17:44:19 +0530837static int gb_svc_hello(struct gb_operation *op)
838{
839 struct gb_connection *connection = op->connection;
Greg Kroah-Hartman0ec30632016-03-22 14:30:35 -0400840 struct gb_svc *svc = gb_connection_get_data(connection);
Viresh Kumaread35462015-07-21 17:44:19 +0530841 struct gb_svc_hello_request *hello_request;
Viresh Kumaread35462015-07-21 17:44:19 +0530842 int ret;
843
Viresh Kumar0c32d2a2015-08-11 07:29:19 +0530844 if (op->request->payload_size < sizeof(*hello_request)) {
Johan Hovold684156a2015-11-25 15:59:19 +0100845 dev_warn(&svc->dev, "short hello request (%zu < %zu)\n",
Cristian Sicilia8478c352018-11-25 17:58:15 +0100846 op->request->payload_size,
847 sizeof(*hello_request));
Viresh Kumaread35462015-07-21 17:44:19 +0530848 return -EINVAL;
849 }
850
851 hello_request = op->request->payload;
Johan Hovold66069fb2015-11-25 15:59:09 +0100852 svc->endo_id = le16_to_cpu(hello_request->endo_id);
853 svc->ap_intf_id = hello_request->interface_id;
Viresh Kumaread35462015-07-21 17:44:19 +0530854
Johan Hovold88f7b962015-11-25 15:59:08 +0100855 ret = device_add(&svc->dev);
856 if (ret) {
857 dev_err(&svc->dev, "failed to register svc device: %d\n", ret);
858 return ret;
859 }
860
Greg Kroah-Hartmaned7279a2016-01-20 22:51:49 -0800861 ret = gb_svc_watchdog_create(svc);
862 if (ret) {
863 dev_err(&svc->dev, "failed to create watchdog: %d\n", ret);
Bryan O'Donoghue4a448422016-06-05 14:03:27 +0100864 goto err_unregister_device;
Greg Kroah-Hartmaned7279a2016-01-20 22:51:49 -0800865 }
866
Johan Hovold12185192016-04-23 18:47:20 +0200867 gb_svc_debugfs_init(svc);
David Lin95046772016-04-20 16:55:08 -0700868
Mitchell Tasmanee2f2072016-05-04 17:30:23 -0400869 return gb_svc_queue_deferred_request(op);
Bryan O'Donoghue4a448422016-06-05 14:03:27 +0100870
871err_unregister_device:
872 gb_svc_watchdog_destroy(svc);
Bryan O'Donoghue4a448422016-06-05 14:03:27 +0100873 device_del(&svc->dev);
874 return ret;
Viresh Kumaread35462015-07-21 17:44:19 +0530875}
876
Johan Hovoldb482b0d2016-04-23 18:47:31 +0200877static struct gb_interface *gb_svc_interface_lookup(struct gb_svc *svc,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100878 u8 intf_id)
Johan Hovoldb482b0d2016-04-23 18:47:31 +0200879{
880 struct gb_host_device *hd = svc->hd;
881 struct gb_module *module;
882 size_t num_interfaces;
883 u8 module_id;
884
885 list_for_each_entry(module, &hd->modules, hd_node) {
886 module_id = module->module_id;
887 num_interfaces = module->num_interfaces;
888
889 if (intf_id >= module_id &&
Cristian Sicilia8478c352018-11-25 17:58:15 +0100890 intf_id < module_id + num_interfaces) {
Johan Hovoldb482b0d2016-04-23 18:47:31 +0200891 return module->interfaces[intf_id - module_id];
892 }
893 }
894
895 return NULL;
896}
897
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200898static struct gb_module *gb_svc_module_lookup(struct gb_svc *svc, u8 module_id)
899{
900 struct gb_host_device *hd = svc->hd;
901 struct gb_module *module;
902
903 list_for_each_entry(module, &hd->modules, hd_node) {
904 if (module->module_id == module_id)
905 return module;
906 }
907
908 return NULL;
909}
910
Mitchell Tasmanee2f2072016-05-04 17:30:23 -0400911static void gb_svc_process_hello_deferred(struct gb_operation *operation)
912{
913 struct gb_connection *connection = operation->connection;
914 struct gb_svc *svc = gb_connection_get_data(connection);
915 int ret;
916
917 /*
918 * XXX This is a hack/work-around to reconfigure the APBridgeA-Switch
919 * link to PWM G2, 1 Lane, Slow Auto, so that it has sufficient
920 * bandwidth for 3 audio streams plus boot-over-UniPro of a hot-plugged
921 * module.
922 *
923 * The code should be removed once SW-2217, Heuristic for UniPro
924 * Power Mode Changes is resolved.
925 */
926 ret = gb_svc_intf_set_power_mode(svc, svc->ap_intf_id,
Gioh Kim461ab802017-02-09 17:30:11 +0100927 GB_SVC_UNIPRO_HS_SERIES_A,
928 GB_SVC_UNIPRO_SLOW_AUTO_MODE,
929 2, 1,
930 GB_SVC_SMALL_AMPLITUDE,
931 GB_SVC_NO_DE_EMPHASIS,
932 GB_SVC_UNIPRO_SLOW_AUTO_MODE,
933 2, 1,
934 0, 0,
935 NULL, NULL);
Mitchell Tasmanee2f2072016-05-04 17:30:23 -0400936
937 if (ret)
938 dev_warn(&svc->dev,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100939 "power mode change failed on AP to switch link: %d\n",
940 ret);
Mitchell Tasmanee2f2072016-05-04 17:30:23 -0400941}
942
Johan Hovold22bb9382016-04-23 18:47:30 +0200943static void gb_svc_process_module_inserted(struct gb_operation *operation)
944{
945 struct gb_svc_module_inserted_request *request;
946 struct gb_connection *connection = operation->connection;
947 struct gb_svc *svc = gb_connection_get_data(connection);
948 struct gb_host_device *hd = svc->hd;
949 struct gb_module *module;
950 size_t num_interfaces;
951 u8 module_id;
952 u16 flags;
953 int ret;
954
955 /* The request message size has already been verified. */
956 request = operation->request->payload;
957 module_id = request->primary_intf_id;
958 num_interfaces = request->intf_count;
959 flags = le16_to_cpu(request->flags);
960
961 dev_dbg(&svc->dev, "%s - id = %u, num_interfaces = %zu, flags = 0x%04x\n",
Cristian Sicilia8478c352018-11-25 17:58:15 +0100962 __func__, module_id, num_interfaces, flags);
Johan Hovold22bb9382016-04-23 18:47:30 +0200963
964 if (flags & GB_SVC_MODULE_INSERTED_FLAG_NO_PRIMARY) {
965 dev_warn(&svc->dev, "no primary interface detected on module %u\n",
Cristian Sicilia8478c352018-11-25 17:58:15 +0100966 module_id);
Johan Hovold22bb9382016-04-23 18:47:30 +0200967 }
968
969 module = gb_svc_module_lookup(svc, module_id);
970 if (module) {
971 dev_warn(&svc->dev, "unexpected module-inserted event %u\n",
Cristian Sicilia8478c352018-11-25 17:58:15 +0100972 module_id);
Johan Hovold22bb9382016-04-23 18:47:30 +0200973 return;
974 }
975
976 module = gb_module_create(hd, module_id, num_interfaces);
977 if (!module) {
978 dev_err(&svc->dev, "failed to create module\n");
979 return;
980 }
981
982 ret = gb_module_add(module);
983 if (ret) {
984 gb_module_put(module);
985 return;
986 }
987
988 list_add(&module->hd_node, &hd->modules);
989}
990
991static void gb_svc_process_module_removed(struct gb_operation *operation)
992{
993 struct gb_svc_module_removed_request *request;
994 struct gb_connection *connection = operation->connection;
995 struct gb_svc *svc = gb_connection_get_data(connection);
996 struct gb_module *module;
997 u8 module_id;
998
999 /* The request message size has already been verified. */
1000 request = operation->request->payload;
1001 module_id = request->primary_intf_id;
1002
1003 dev_dbg(&svc->dev, "%s - id = %u\n", __func__, module_id);
1004
1005 module = gb_svc_module_lookup(svc, module_id);
1006 if (!module) {
1007 dev_warn(&svc->dev, "unexpected module-removed event %u\n",
Cristian Sicilia8478c352018-11-25 17:58:15 +01001008 module_id);
Johan Hovold22bb9382016-04-23 18:47:30 +02001009 return;
1010 }
1011
1012 module->disconnected = true;
1013
1014 gb_module_del(module);
1015 list_del(&module->hd_node);
1016 gb_module_put(module);
1017}
1018
Georgi Dobrev57fa2de2016-08-09 14:37:32 -07001019static void gb_svc_process_intf_oops(struct gb_operation *operation)
1020{
1021 struct gb_svc_intf_oops_request *request;
1022 struct gb_connection *connection = operation->connection;
1023 struct gb_svc *svc = gb_connection_get_data(connection);
1024 struct gb_interface *intf;
1025 u8 intf_id;
1026 u8 reason;
1027
1028 /* The request message size has already been verified. */
1029 request = operation->request->payload;
1030 intf_id = request->intf_id;
1031 reason = request->reason;
1032
1033 intf = gb_svc_interface_lookup(svc, intf_id);
1034 if (!intf) {
1035 dev_warn(&svc->dev, "unexpected interface-oops event %u\n",
1036 intf_id);
1037 return;
1038 }
1039
1040 dev_info(&svc->dev, "Deactivating interface %u, interface oops reason = %u\n",
1041 intf_id, reason);
1042
1043 mutex_lock(&intf->mutex);
1044 intf->disconnected = true;
1045 gb_interface_disable(intf);
1046 gb_interface_deactivate(intf);
1047 mutex_unlock(&intf->mutex);
1048}
1049
Johan Hovoldb482b0d2016-04-23 18:47:31 +02001050static void gb_svc_process_intf_mailbox_event(struct gb_operation *operation)
1051{
1052 struct gb_svc_intf_mailbox_event_request *request;
1053 struct gb_connection *connection = operation->connection;
1054 struct gb_svc *svc = gb_connection_get_data(connection);
1055 struct gb_interface *intf;
1056 u8 intf_id;
1057 u16 result_code;
1058 u32 mailbox;
1059
1060 /* The request message size has already been verified. */
1061 request = operation->request->payload;
1062 intf_id = request->intf_id;
1063 result_code = le16_to_cpu(request->result_code);
1064 mailbox = le32_to_cpu(request->mailbox);
1065
1066 dev_dbg(&svc->dev, "%s - id = %u, result = 0x%04x, mailbox = 0x%08x\n",
Cristian Sicilia8478c352018-11-25 17:58:15 +01001067 __func__, intf_id, result_code, mailbox);
Johan Hovoldb482b0d2016-04-23 18:47:31 +02001068
1069 intf = gb_svc_interface_lookup(svc, intf_id);
1070 if (!intf) {
1071 dev_warn(&svc->dev, "unexpected mailbox event %u\n", intf_id);
1072 return;
1073 }
1074
Johan Hovold55742d22016-05-27 17:26:40 +02001075 gb_interface_mailbox_event(intf, result_code, mailbox);
Johan Hovoldb482b0d2016-04-23 18:47:31 +02001076}
1077
Johan Hovold9ae41092015-12-02 18:23:29 +01001078static void gb_svc_process_deferred_request(struct work_struct *work)
1079{
1080 struct gb_svc_deferred_request *dr;
1081 struct gb_operation *operation;
1082 struct gb_svc *svc;
1083 u8 type;
1084
1085 dr = container_of(work, struct gb_svc_deferred_request, work);
1086 operation = dr->operation;
Greg Kroah-Hartman0ec30632016-03-22 14:30:35 -04001087 svc = gb_connection_get_data(operation->connection);
Johan Hovold9ae41092015-12-02 18:23:29 +01001088 type = operation->request->header->type;
1089
1090 switch (type) {
Mitchell Tasmanee2f2072016-05-04 17:30:23 -04001091 case GB_SVC_TYPE_SVC_HELLO:
1092 gb_svc_process_hello_deferred(operation);
1093 break;
Johan Hovold22bb9382016-04-23 18:47:30 +02001094 case GB_SVC_TYPE_MODULE_INSERTED:
1095 gb_svc_process_module_inserted(operation);
1096 break;
1097 case GB_SVC_TYPE_MODULE_REMOVED:
1098 gb_svc_process_module_removed(operation);
1099 break;
Johan Hovoldb482b0d2016-04-23 18:47:31 +02001100 case GB_SVC_TYPE_INTF_MAILBOX_EVENT:
1101 gb_svc_process_intf_mailbox_event(operation);
1102 break;
Georgi Dobrev57fa2de2016-08-09 14:37:32 -07001103 case GB_SVC_TYPE_INTF_OOPS:
1104 gb_svc_process_intf_oops(operation);
1105 break;
Johan Hovold9ae41092015-12-02 18:23:29 +01001106 default:
Viresh Kumarb933fa42015-12-04 21:30:10 +05301107 dev_err(&svc->dev, "bad deferred request type: 0x%02x\n", type);
Johan Hovold9ae41092015-12-02 18:23:29 +01001108 }
1109
1110 gb_operation_put(operation);
1111 kfree(dr);
1112}
1113
1114static int gb_svc_queue_deferred_request(struct gb_operation *operation)
1115{
Greg Kroah-Hartman0ec30632016-03-22 14:30:35 -04001116 struct gb_svc *svc = gb_connection_get_data(operation->connection);
Johan Hovold9ae41092015-12-02 18:23:29 +01001117 struct gb_svc_deferred_request *dr;
1118
1119 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1120 if (!dr)
1121 return -ENOMEM;
1122
1123 gb_operation_get(operation);
1124
1125 dr->operation = operation;
1126 INIT_WORK(&dr->work, gb_svc_process_deferred_request);
1127
Johan Hovold3e48aca2015-12-02 18:23:31 +01001128 queue_work(svc->wq, &dr->work);
Johan Hovold9ae41092015-12-02 18:23:29 +01001129
1130 return 0;
Viresh Kumar067906f2015-08-06 12:44:55 +05301131}
Viresh Kumaread35462015-07-21 17:44:19 +05301132
Alex Elder30c6d9d2015-05-22 13:02:08 -05001133static int gb_svc_intf_reset_recv(struct gb_operation *op)
1134{
Greg Kroah-Hartman0ec30632016-03-22 14:30:35 -04001135 struct gb_svc *svc = gb_connection_get_data(op->connection);
Alex Elder30c6d9d2015-05-22 13:02:08 -05001136 struct gb_message *request = op->request;
1137 struct gb_svc_intf_reset_request *reset;
Alex Elder30c6d9d2015-05-22 13:02:08 -05001138
1139 if (request->payload_size < sizeof(*reset)) {
Johan Hovold684156a2015-11-25 15:59:19 +01001140 dev_warn(&svc->dev, "short reset request received (%zu < %zu)\n",
Cristian Sicilia8478c352018-11-25 17:58:15 +01001141 request->payload_size, sizeof(*reset));
Alex Elder30c6d9d2015-05-22 13:02:08 -05001142 return -EINVAL;
1143 }
1144 reset = request->payload;
1145
Alex Elder30c6d9d2015-05-22 13:02:08 -05001146 /* FIXME Reset the interface here */
1147
1148 return 0;
1149}
1150
Johan Hovold22bb9382016-04-23 18:47:30 +02001151static int gb_svc_module_inserted_recv(struct gb_operation *op)
1152{
1153 struct gb_svc *svc = gb_connection_get_data(op->connection);
1154 struct gb_svc_module_inserted_request *request;
1155
1156 if (op->request->payload_size < sizeof(*request)) {
1157 dev_warn(&svc->dev, "short module-inserted request received (%zu < %zu)\n",
Cristian Sicilia8478c352018-11-25 17:58:15 +01001158 op->request->payload_size, sizeof(*request));
Johan Hovold22bb9382016-04-23 18:47:30 +02001159 return -EINVAL;
1160 }
1161
1162 request = op->request->payload;
1163
1164 dev_dbg(&svc->dev, "%s - id = %u\n", __func__,
Cristian Sicilia8478c352018-11-25 17:58:15 +01001165 request->primary_intf_id);
Johan Hovold22bb9382016-04-23 18:47:30 +02001166
1167 return gb_svc_queue_deferred_request(op);
1168}
1169
1170static int gb_svc_module_removed_recv(struct gb_operation *op)
1171{
1172 struct gb_svc *svc = gb_connection_get_data(op->connection);
1173 struct gb_svc_module_removed_request *request;
1174
1175 if (op->request->payload_size < sizeof(*request)) {
1176 dev_warn(&svc->dev, "short module-removed request received (%zu < %zu)\n",
Cristian Sicilia8478c352018-11-25 17:58:15 +01001177 op->request->payload_size, sizeof(*request));
Johan Hovold22bb9382016-04-23 18:47:30 +02001178 return -EINVAL;
1179 }
1180
1181 request = op->request->payload;
1182
1183 dev_dbg(&svc->dev, "%s - id = %u\n", __func__,
Cristian Sicilia8478c352018-11-25 17:58:15 +01001184 request->primary_intf_id);
Johan Hovold22bb9382016-04-23 18:47:30 +02001185
1186 return gb_svc_queue_deferred_request(op);
1187}
1188
Georgi Dobrev57fa2de2016-08-09 14:37:32 -07001189static int gb_svc_intf_oops_recv(struct gb_operation *op)
1190{
1191 struct gb_svc *svc = gb_connection_get_data(op->connection);
1192 struct gb_svc_intf_oops_request *request;
1193
1194 if (op->request->payload_size < sizeof(*request)) {
1195 dev_warn(&svc->dev, "short intf-oops request received (%zu < %zu)\n",
1196 op->request->payload_size, sizeof(*request));
1197 return -EINVAL;
1198 }
1199
1200 return gb_svc_queue_deferred_request(op);
1201}
1202
Johan Hovoldb482b0d2016-04-23 18:47:31 +02001203static int gb_svc_intf_mailbox_event_recv(struct gb_operation *op)
1204{
1205 struct gb_svc *svc = gb_connection_get_data(op->connection);
1206 struct gb_svc_intf_mailbox_event_request *request;
1207
1208 if (op->request->payload_size < sizeof(*request)) {
1209 dev_warn(&svc->dev, "short mailbox request received (%zu < %zu)\n",
Cristian Sicilia8478c352018-11-25 17:58:15 +01001210 op->request->payload_size, sizeof(*request));
Johan Hovoldb482b0d2016-04-23 18:47:31 +02001211 return -EINVAL;
1212 }
1213
1214 request = op->request->payload;
1215
1216 dev_dbg(&svc->dev, "%s - id = %u\n", __func__, request->intf_id);
1217
1218 return gb_svc_queue_deferred_request(op);
1219}
1220
Johan Hovold84427942016-01-19 12:51:15 +01001221static int gb_svc_request_handler(struct gb_operation *op)
Alex Elder30c6d9d2015-05-22 13:02:08 -05001222{
Viresh Kumar3ccb1602015-09-03 15:42:22 +05301223 struct gb_connection *connection = op->connection;
Greg Kroah-Hartman0ec30632016-03-22 14:30:35 -04001224 struct gb_svc *svc = gb_connection_get_data(connection);
Johan Hovold84427942016-01-19 12:51:15 +01001225 u8 type = op->type;
Viresh Kumar3ccb1602015-09-03 15:42:22 +05301226 int ret = 0;
1227
1228 /*
1229 * SVC requests need to follow a specific order (at least initially) and
1230 * below code takes care of enforcing that. The expected order is:
1231 * - PROTOCOL_VERSION
1232 * - SVC_HELLO
1233 * - Any other request, but the earlier two.
1234 *
1235 * Incoming requests are guaranteed to be serialized and so we don't
1236 * need to protect 'state' for any races.
1237 */
Alex Elder30c6d9d2015-05-22 13:02:08 -05001238 switch (type) {
Johan Hovolda2cf2e52016-04-29 17:08:36 +02001239 case GB_SVC_TYPE_PROTOCOL_VERSION:
Viresh Kumar3ccb1602015-09-03 15:42:22 +05301240 if (svc->state != GB_SVC_STATE_RESET)
1241 ret = -EINVAL;
1242 break;
Viresh Kumaread35462015-07-21 17:44:19 +05301243 case GB_SVC_TYPE_SVC_HELLO:
Viresh Kumar3ccb1602015-09-03 15:42:22 +05301244 if (svc->state != GB_SVC_STATE_PROTOCOL_VERSION)
1245 ret = -EINVAL;
1246 break;
1247 default:
1248 if (svc->state != GB_SVC_STATE_SVC_HELLO)
1249 ret = -EINVAL;
1250 break;
1251 }
1252
1253 if (ret) {
Johan Hovold684156a2015-11-25 15:59:19 +01001254 dev_warn(&svc->dev, "unexpected request 0x%02x received (state %u)\n",
Cristian Sicilia8478c352018-11-25 17:58:15 +01001255 type, svc->state);
Viresh Kumar3ccb1602015-09-03 15:42:22 +05301256 return ret;
1257 }
1258
1259 switch (type) {
Johan Hovolda2cf2e52016-04-29 17:08:36 +02001260 case GB_SVC_TYPE_PROTOCOL_VERSION:
Viresh Kumar3ccb1602015-09-03 15:42:22 +05301261 ret = gb_svc_version_request(op);
1262 if (!ret)
1263 svc->state = GB_SVC_STATE_PROTOCOL_VERSION;
1264 return ret;
1265 case GB_SVC_TYPE_SVC_HELLO:
1266 ret = gb_svc_hello(op);
1267 if (!ret)
1268 svc->state = GB_SVC_STATE_SVC_HELLO;
1269 return ret;
Alex Elder30c6d9d2015-05-22 13:02:08 -05001270 case GB_SVC_TYPE_INTF_RESET:
1271 return gb_svc_intf_reset_recv(op);
Johan Hovold22bb9382016-04-23 18:47:30 +02001272 case GB_SVC_TYPE_MODULE_INSERTED:
1273 return gb_svc_module_inserted_recv(op);
1274 case GB_SVC_TYPE_MODULE_REMOVED:
1275 return gb_svc_module_removed_recv(op);
Johan Hovoldb482b0d2016-04-23 18:47:31 +02001276 case GB_SVC_TYPE_INTF_MAILBOX_EVENT:
1277 return gb_svc_intf_mailbox_event_recv(op);
Georgi Dobrev57fa2de2016-08-09 14:37:32 -07001278 case GB_SVC_TYPE_INTF_OOPS:
1279 return gb_svc_intf_oops_recv(op);
Alex Elder30c6d9d2015-05-22 13:02:08 -05001280 default:
Johan Hovold684156a2015-11-25 15:59:19 +01001281 dev_warn(&svc->dev, "unsupported request 0x%02x\n", type);
Alex Elder30c6d9d2015-05-22 13:02:08 -05001282 return -EINVAL;
1283 }
1284}
1285
Johan Hovoldefe6ef72015-11-25 15:59:06 +01001286static void gb_svc_release(struct device *dev)
1287{
Johan Hovold88f7b962015-11-25 15:59:08 +01001288 struct gb_svc *svc = to_gb_svc(dev);
Johan Hovoldefe6ef72015-11-25 15:59:06 +01001289
Johan Hovold7adeaae72015-12-07 15:05:37 +01001290 if (svc->connection)
1291 gb_connection_destroy(svc->connection);
Johan Hovoldefe6ef72015-11-25 15:59:06 +01001292 ida_destroy(&svc->device_id_map);
Johan Hovold3e48aca2015-12-02 18:23:31 +01001293 destroy_workqueue(svc->wq);
Johan Hovoldefe6ef72015-11-25 15:59:06 +01001294 kfree(svc);
1295}
1296
1297struct device_type greybus_svc_type = {
1298 .name = "greybus_svc",
1299 .release = gb_svc_release,
1300};
1301
Johan Hovold7adeaae72015-12-07 15:05:37 +01001302struct gb_svc *gb_svc_create(struct gb_host_device *hd)
Alex Elder30c6d9d2015-05-22 13:02:08 -05001303{
1304 struct gb_svc *svc;
Alex Elder30c6d9d2015-05-22 13:02:08 -05001305
1306 svc = kzalloc(sizeof(*svc), GFP_KERNEL);
1307 if (!svc)
Johan Hovold7adeaae72015-12-07 15:05:37 +01001308 return NULL;
Alex Elder30c6d9d2015-05-22 13:02:08 -05001309
Johan Hovold3e48aca2015-12-02 18:23:31 +01001310 svc->wq = alloc_workqueue("%s:svc", WQ_UNBOUND, 1, dev_name(&hd->dev));
1311 if (!svc->wq) {
1312 kfree(svc);
Johan Hovold7adeaae72015-12-07 15:05:37 +01001313 return NULL;
Johan Hovold3e48aca2015-12-02 18:23:31 +01001314 }
1315
Johan Hovoldefe6ef72015-11-25 15:59:06 +01001316 svc->dev.parent = &hd->dev;
1317 svc->dev.bus = &greybus_bus_type;
1318 svc->dev.type = &greybus_svc_type;
Johan Hovold66069fb2015-11-25 15:59:09 +01001319 svc->dev.groups = svc_groups;
Johan Hovoldefe6ef72015-11-25 15:59:06 +01001320 svc->dev.dma_mask = svc->dev.parent->dma_mask;
1321 device_initialize(&svc->dev);
1322
1323 dev_set_name(&svc->dev, "%d-svc", hd->bus_id);
1324
Johan Hovold6106e512015-11-25 15:59:07 +01001325 ida_init(&svc->device_id_map);
Viresh Kumar3ccb1602015-09-03 15:42:22 +05301326 svc->state = GB_SVC_STATE_RESET;
Johan Hovoldf0960d02015-12-03 19:18:02 +01001327 svc->hd = hd;
Viresh Kumard3d44842015-07-21 17:44:18 +05301328
Johan Hovoldf7ee0812016-01-21 17:34:21 +01001329 svc->connection = gb_connection_create_static(hd, GB_SVC_CPORT_ID,
Cristian Sicilia8478c352018-11-25 17:58:15 +01001330 gb_svc_request_handler);
Johan Hovold24e094d2016-01-21 17:34:16 +01001331 if (IS_ERR(svc->connection)) {
1332 dev_err(&svc->dev, "failed to create connection: %ld\n",
Cristian Sicilia8478c352018-11-25 17:58:15 +01001333 PTR_ERR(svc->connection));
Sandeep Patil1beb1fa2016-06-28 12:10:14 -07001334 goto err_put_device;
Johan Hovold7adeaae72015-12-07 15:05:37 +01001335 }
1336
Greg Kroah-Hartman0ec30632016-03-22 14:30:35 -04001337 gb_connection_set_data(svc->connection, svc);
Johan Hovold7adeaae72015-12-07 15:05:37 +01001338
1339 return svc;
Rui Miguel Silvaebe99d62016-01-21 01:42:17 +00001340
Rui Miguel Silvaebe99d62016-01-21 01:42:17 +00001341err_put_device:
1342 put_device(&svc->dev);
1343 return NULL;
Johan Hovold7adeaae72015-12-07 15:05:37 +01001344}
1345
1346int gb_svc_add(struct gb_svc *svc)
1347{
1348 int ret;
1349
1350 /*
1351 * The SVC protocol is currently driven by the SVC, so the SVC device
1352 * is added from the connection request handler when enough
1353 * information has been received.
1354 */
Johan Hovoldf7ee0812016-01-21 17:34:21 +01001355 ret = gb_connection_enable(svc->connection);
Johan Hovold7adeaae72015-12-07 15:05:37 +01001356 if (ret)
1357 return ret;
1358
1359 return 0;
1360}
1361
Johan Hovoldb15d97d2016-04-23 18:47:24 +02001362static void gb_svc_remove_modules(struct gb_svc *svc)
Johan Hovold66d674c2016-03-09 12:20:41 +01001363{
Johan Hovoldb15d97d2016-04-23 18:47:24 +02001364 struct gb_host_device *hd = svc->hd;
1365 struct gb_module *module, *tmp;
Johan Hovold66d674c2016-03-09 12:20:41 +01001366
Johan Hovoldb15d97d2016-04-23 18:47:24 +02001367 list_for_each_entry_safe(module, tmp, &hd->modules, hd_node) {
1368 gb_module_del(module);
1369 list_del(&module->hd_node);
1370 gb_module_put(module);
Johan Hovold629c0d02016-03-09 12:20:43 +01001371 }
Johan Hovold66d674c2016-03-09 12:20:41 +01001372}
1373
Johan Hovold7adeaae72015-12-07 15:05:37 +01001374void gb_svc_del(struct gb_svc *svc)
1375{
Viresh Kumard1a8c362016-06-13 21:48:28 +05301376 gb_connection_disable_rx(svc->connection);
Johan Hovold7adeaae72015-12-07 15:05:37 +01001377
Rui Miguel Silvaebe99d62016-01-21 01:42:17 +00001378 /*
Sandeep Patil1beb1fa2016-06-28 12:10:14 -07001379 * The SVC device may have been registered from the request handler.
Rui Miguel Silvaebe99d62016-01-21 01:42:17 +00001380 */
1381 if (device_is_registered(&svc->dev)) {
Johan Hovold12185192016-04-23 18:47:20 +02001382 gb_svc_debugfs_exit(svc);
Greg Kroah-Hartmaned7279a2016-01-20 22:51:49 -08001383 gb_svc_watchdog_destroy(svc);
Rui Miguel Silvaebe99d62016-01-21 01:42:17 +00001384 device_del(&svc->dev);
1385 }
1386
Johan Hovold7adeaae72015-12-07 15:05:37 +01001387 flush_workqueue(svc->wq);
Johan Hovold66d674c2016-03-09 12:20:41 +01001388
Johan Hovoldb15d97d2016-04-23 18:47:24 +02001389 gb_svc_remove_modules(svc);
Viresh Kumard1a8c362016-06-13 21:48:28 +05301390
1391 gb_connection_disable(svc->connection);
Johan Hovold7adeaae72015-12-07 15:05:37 +01001392}
1393
1394void gb_svc_put(struct gb_svc *svc)
1395{
1396 put_device(&svc->dev);
1397}