blob: 1b370195e4c3e870d3f3ed70adc9e92126ffc562 [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
David Lin95046772016-04-20 16:55:08 -070010#include <linux/debugfs.h>
Rui Miguel Silvaebe99d62016-01-21 01:42:17 +000011#include <linux/input.h>
Viresh Kumar067906f2015-08-06 12:44:55 +053012#include <linux/workqueue.h>
Alex Elder30c6d9d2015-05-22 13:02:08 -050013
Viresh Kumarf66427a2015-09-02 21:27:13 +053014#include "greybus.h"
15
Rui Miguel Silvaebe99d62016-01-21 01:42:17 +000016#define SVC_KEY_ARA_BUTTON KEY_A
Viresh Kumarb45864d2015-07-24 15:32:21 +053017
Johan Hovoldd18da862016-03-09 12:20:46 +010018#define SVC_INTF_EJECT_TIMEOUT 9000
19
Johan Hovold9ae41092015-12-02 18:23:29 +010020struct gb_svc_deferred_request {
Viresh Kumar067906f2015-08-06 12:44:55 +053021 struct work_struct work;
Johan Hovold9ae41092015-12-02 18:23:29 +010022 struct gb_operation *operation;
Viresh Kumar067906f2015-08-06 12:44:55 +053023};
24
Viresh Kumaread35462015-07-21 17:44:19 +053025
Johan Hovold66069fb2015-11-25 15:59:09 +010026static ssize_t endo_id_show(struct device *dev,
27 struct device_attribute *attr, char *buf)
28{
29 struct gb_svc *svc = to_gb_svc(dev);
30
31 return sprintf(buf, "0x%04x\n", svc->endo_id);
32}
33static DEVICE_ATTR_RO(endo_id);
34
35static ssize_t ap_intf_id_show(struct device *dev,
36 struct device_attribute *attr, char *buf)
37{
38 struct gb_svc *svc = to_gb_svc(dev);
39
40 return sprintf(buf, "%u\n", svc->ap_intf_id);
41}
42static DEVICE_ATTR_RO(ap_intf_id);
43
Rui Miguel Silva2c92bd52016-01-11 13:46:33 +000044
45// FIXME
46// This is a hack, we need to do this "right" and clean the interface up
47// properly, not just forcibly yank the thing out of the system and hope for the
48// best. But for now, people want their modules to come out without having to
49// throw the thing to the ground or get out a screwdriver.
50static ssize_t intf_eject_store(struct device *dev,
51 struct device_attribute *attr, const char *buf,
52 size_t len)
53{
54 struct gb_svc *svc = to_gb_svc(dev);
55 unsigned short intf_id;
56 int ret;
57
58 ret = kstrtou16(buf, 10, &intf_id);
59 if (ret < 0)
60 return ret;
61
62 dev_warn(dev, "Forcibly trying to eject interface %d\n", intf_id);
63
64 ret = gb_svc_intf_eject(svc, intf_id);
65 if (ret < 0)
66 return ret;
67
68 return len;
69}
70static DEVICE_ATTR_WO(intf_eject);
71
Greg Kroah-Hartmand5628532016-01-26 15:17:08 -080072static ssize_t watchdog_show(struct device *dev, struct device_attribute *attr,
73 char *buf)
74{
75 struct gb_svc *svc = to_gb_svc(dev);
76
77 return sprintf(buf, "%s\n",
78 gb_svc_watchdog_enabled(svc) ? "enabled" : "disabled");
79}
80
81static ssize_t watchdog_store(struct device *dev,
82 struct device_attribute *attr, const char *buf,
83 size_t len)
84{
85 struct gb_svc *svc = to_gb_svc(dev);
86 int retval;
87 bool user_request;
88
89 retval = strtobool(buf, &user_request);
90 if (retval)
91 return retval;
92
93 if (user_request)
94 retval = gb_svc_watchdog_enable(svc);
95 else
96 retval = gb_svc_watchdog_disable(svc);
97 if (retval)
98 return retval;
99 return len;
100}
101static DEVICE_ATTR_RW(watchdog);
102
David Lin95046772016-04-20 16:55:08 -0700103static int gb_svc_pwrmon_rail_count_get(struct gb_svc *svc, u8 *value)
104{
105 struct gb_svc_pwrmon_rail_count_get_response response;
106 int ret;
107
108 ret = gb_operation_sync(svc->connection,
109 GB_SVC_TYPE_PWRMON_RAIL_COUNT_GET, NULL, 0,
110 &response, sizeof(response));
111 if (ret) {
Johan Hovold89f2df42016-04-21 11:43:36 +0200112 dev_err(&svc->dev, "failed to get rail count: %d\n", ret);
David Lin95046772016-04-20 16:55:08 -0700113 return ret;
114 }
115
116 *value = response.rail_count;
117
118 return 0;
119}
120
121static int gb_svc_pwrmon_rail_names_get(struct gb_svc *svc,
Johan Hovoldf35fdb22016-04-21 11:43:38 +0200122 struct gb_svc_pwrmon_rail_names_get_response *response,
123 size_t bufsize)
David Lin95046772016-04-20 16:55:08 -0700124{
125 int ret;
126
127 ret = gb_operation_sync(svc->connection,
128 GB_SVC_TYPE_PWRMON_RAIL_NAMES_GET, NULL, 0,
129 response, bufsize);
130 if (ret) {
Johan Hovold89f2df42016-04-21 11:43:36 +0200131 dev_err(&svc->dev, "failed to get rail names: %d\n", ret);
David Lin95046772016-04-20 16:55:08 -0700132 return ret;
133 }
134
135 return 0;
136}
137
138static int gb_svc_pwrmon_sample_get(struct gb_svc *svc, u8 rail_id,
139 u8 measurement_type, u32 *value)
140{
141 struct gb_svc_pwrmon_sample_get_request request;
142 struct gb_svc_pwrmon_sample_get_response response;
143 int ret;
144
145 request.rail_id = rail_id;
146 request.measurement_type = measurement_type;
147
148 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_PWRMON_SAMPLE_GET,
149 &request, sizeof(request),
150 &response, sizeof(response));
151 if (ret) {
Johan Hovold89f2df42016-04-21 11:43:36 +0200152 dev_err(&svc->dev, "failed to get rail sample: %d\n", ret);
David Lin95046772016-04-20 16:55:08 -0700153 return ret;
154 }
155
156 if (response.result) {
157 dev_err(&svc->dev,
158 "UniPro error while getting rail power sample (%d %d): %d\n",
159 rail_id, measurement_type, response.result);
160 switch (response.result) {
161 case GB_SVC_PWRMON_GET_SAMPLE_INVAL:
162 return -EINVAL;
163 case GB_SVC_PWRMON_GET_SAMPLE_NOSUPP:
Johan Hovold5b35ef92016-04-21 11:43:37 +0200164 return -ENOMSG;
David Lin95046772016-04-20 16:55:08 -0700165 default:
166 return -EIO;
167 }
168 }
169
170 *value = le32_to_cpu(response.measurement);
171
172 return 0;
173}
174
David Linddb10c82016-04-07 20:15:30 -0700175int gb_svc_pwrmon_intf_sample_get(struct gb_svc *svc, u8 intf_id,
176 u8 measurement_type, u32 *value)
177{
178 struct gb_svc_pwrmon_intf_sample_get_request request;
179 struct gb_svc_pwrmon_intf_sample_get_response response;
180 int ret;
181
182 request.intf_id = intf_id;
183 request.measurement_type = measurement_type;
184
185 ret = gb_operation_sync(svc->connection,
186 GB_SVC_TYPE_PWRMON_INTF_SAMPLE_GET,
187 &request, sizeof(request),
188 &response, sizeof(response));
189 if (ret) {
Johan Hovold89f2df42016-04-21 11:43:36 +0200190 dev_err(&svc->dev, "failed to get intf sample: %d\n", ret);
David Linddb10c82016-04-07 20:15:30 -0700191 return ret;
192 }
193
194 if (response.result) {
195 dev_err(&svc->dev,
196 "UniPro error while getting intf power sample (%d %d): %d\n",
197 intf_id, measurement_type, response.result);
198 switch (response.result) {
199 case GB_SVC_PWRMON_GET_SAMPLE_INVAL:
200 return -EINVAL;
201 case GB_SVC_PWRMON_GET_SAMPLE_NOSUPP:
202 return -ENOSYS;
203 default:
204 return -EIO;
205 }
206 }
207
208 *value = le32_to_cpu(response.measurement);
209
210 return 0;
211}
212
Johan Hovold66069fb2015-11-25 15:59:09 +0100213static struct attribute *svc_attrs[] = {
214 &dev_attr_endo_id.attr,
215 &dev_attr_ap_intf_id.attr,
Rui Miguel Silva2c92bd52016-01-11 13:46:33 +0000216 &dev_attr_intf_eject.attr,
Greg Kroah-Hartmand5628532016-01-26 15:17:08 -0800217 &dev_attr_watchdog.attr,
Johan Hovold66069fb2015-11-25 15:59:09 +0100218 NULL,
219};
220ATTRIBUTE_GROUPS(svc);
221
Johan Hovold4d5f6212016-03-29 18:56:04 -0400222int gb_svc_intf_device_id(struct gb_svc *svc, u8 intf_id, u8 device_id)
Alex Elder30c6d9d2015-05-22 13:02:08 -0500223{
224 struct gb_svc_intf_device_id_request request;
225
226 request.intf_id = intf_id;
227 request.device_id = device_id;
228
229 return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_DEVICE_ID,
230 &request, sizeof(request), NULL, 0);
231}
232
Rui Miguel Silvac5d55fb2016-01-11 13:46:31 +0000233int gb_svc_intf_eject(struct gb_svc *svc, u8 intf_id)
234{
235 struct gb_svc_intf_eject_request request;
Johan Hovolde676ccd2016-03-09 12:20:45 +0100236 int ret;
Rui Miguel Silvac5d55fb2016-01-11 13:46:31 +0000237
238 request.intf_id = intf_id;
239
240 /*
241 * The pulse width for module release in svc is long so we need to
242 * increase the timeout so the operation will not return to soon.
243 */
Johan Hovolde676ccd2016-03-09 12:20:45 +0100244 ret = gb_operation_sync_timeout(svc->connection,
245 GB_SVC_TYPE_INTF_EJECT, &request,
246 sizeof(request), NULL, 0,
Johan Hovoldd18da862016-03-09 12:20:46 +0100247 SVC_INTF_EJECT_TIMEOUT);
Johan Hovolde676ccd2016-03-09 12:20:45 +0100248 if (ret) {
249 dev_err(&svc->dev, "failed to eject interface %u\n", intf_id);
250 return ret;
251 }
252
253 return 0;
Rui Miguel Silvac5d55fb2016-01-11 13:46:31 +0000254}
Rui Miguel Silvac5d55fb2016-01-11 13:46:31 +0000255
Johan Hovold017482b2016-04-23 18:47:27 +0200256int gb_svc_intf_vsys_set(struct gb_svc *svc, u8 intf_id, bool enable)
257{
258 /* FIXME: implement */
259
260 return 0;
261}
262
263int gb_svc_intf_refclk_set(struct gb_svc *svc, u8 intf_id, bool enable)
264{
265 /* FIXME: implement */
266
267 return 0;
268}
269
270int gb_svc_intf_unipro_set(struct gb_svc *svc, u8 intf_id, bool enable)
271{
272 /* FIXME: implement */
273
274 return 0;
275}
276
Johan Hovold1e8e22b2016-04-23 18:47:28 +0200277int gb_svc_intf_activate(struct gb_svc *svc, u8 intf_id, u8 *intf_type)
278{
279 /* FIXME: implement */
280
281 *intf_type = GB_SVC_INTF_TYPE_GREYBUS;
282
283 return 0;
284}
285
Viresh Kumar19151c32015-09-09 21:08:29 +0530286int gb_svc_dme_peer_get(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
287 u32 *value)
288{
289 struct gb_svc_dme_peer_get_request request;
290 struct gb_svc_dme_peer_get_response response;
291 u16 result;
292 int ret;
293
294 request.intf_id = intf_id;
295 request.attr = cpu_to_le16(attr);
296 request.selector = cpu_to_le16(selector);
297
298 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_GET,
299 &request, sizeof(request),
300 &response, sizeof(response));
301 if (ret) {
Viresh Kumarb933fa42015-12-04 21:30:10 +0530302 dev_err(&svc->dev, "failed to get DME attribute (%u 0x%04x %u): %d\n",
Johan Hovold684156a2015-11-25 15:59:19 +0100303 intf_id, attr, selector, ret);
Viresh Kumar19151c32015-09-09 21:08:29 +0530304 return ret;
305 }
306
307 result = le16_to_cpu(response.result_code);
308 if (result) {
Viresh Kumarb933fa42015-12-04 21:30:10 +0530309 dev_err(&svc->dev, "UniPro error while getting DME attribute (%u 0x%04x %u): %u\n",
Johan Hovold684156a2015-11-25 15:59:19 +0100310 intf_id, attr, selector, result);
Viresh Kumar4aac6c52015-12-04 21:30:08 +0530311 return -EIO;
Viresh Kumar19151c32015-09-09 21:08:29 +0530312 }
313
314 if (value)
315 *value = le32_to_cpu(response.attr_value);
316
317 return 0;
318}
319EXPORT_SYMBOL_GPL(gb_svc_dme_peer_get);
320
321int gb_svc_dme_peer_set(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
322 u32 value)
323{
324 struct gb_svc_dme_peer_set_request request;
325 struct gb_svc_dme_peer_set_response response;
326 u16 result;
327 int ret;
328
329 request.intf_id = intf_id;
330 request.attr = cpu_to_le16(attr);
331 request.selector = cpu_to_le16(selector);
332 request.value = cpu_to_le32(value);
333
334 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_SET,
335 &request, sizeof(request),
336 &response, sizeof(response));
337 if (ret) {
Viresh Kumarb933fa42015-12-04 21:30:10 +0530338 dev_err(&svc->dev, "failed to set DME attribute (%u 0x%04x %u %u): %d\n",
Johan Hovold684156a2015-11-25 15:59:19 +0100339 intf_id, attr, selector, value, ret);
Viresh Kumar19151c32015-09-09 21:08:29 +0530340 return ret;
341 }
342
343 result = le16_to_cpu(response.result_code);
344 if (result) {
Viresh Kumarb933fa42015-12-04 21:30:10 +0530345 dev_err(&svc->dev, "UniPro error while setting DME attribute (%u 0x%04x %u %u): %u\n",
Johan Hovold684156a2015-11-25 15:59:19 +0100346 intf_id, attr, selector, value, result);
Viresh Kumar4aac6c52015-12-04 21:30:08 +0530347 return -EIO;
Viresh Kumar19151c32015-09-09 21:08:29 +0530348 }
349
350 return 0;
351}
352EXPORT_SYMBOL_GPL(gb_svc_dme_peer_set);
353
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530354int gb_svc_connection_create(struct gb_svc *svc,
Alex Elder30c6d9d2015-05-22 13:02:08 -0500355 u8 intf1_id, u16 cport1_id,
Viresh Kumar1575ef182015-10-07 15:40:24 -0400356 u8 intf2_id, u16 cport2_id,
Johan Hovold27f25c12016-03-03 13:34:38 +0100357 u8 cport_flags)
Alex Elder30c6d9d2015-05-22 13:02:08 -0500358{
359 struct gb_svc_conn_create_request request;
360
361 request.intf1_id = intf1_id;
Rui Miguel Silva2498050b2015-09-15 15:33:51 +0100362 request.cport1_id = cpu_to_le16(cport1_id);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500363 request.intf2_id = intf2_id;
Rui Miguel Silva2498050b2015-09-15 15:33:51 +0100364 request.cport2_id = cpu_to_le16(cport2_id);
Johan Hovold34145b62016-03-03 13:34:37 +0100365 request.tc = 0; /* TC0 */
Johan Hovold27f25c12016-03-03 13:34:38 +0100366 request.flags = cport_flags;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500367
368 return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_CREATE,
369 &request, sizeof(request), NULL, 0);
370}
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530371EXPORT_SYMBOL_GPL(gb_svc_connection_create);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500372
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530373void gb_svc_connection_destroy(struct gb_svc *svc, u8 intf1_id, u16 cport1_id,
374 u8 intf2_id, u16 cport2_id)
Alex Elder30c6d9d2015-05-22 13:02:08 -0500375{
376 struct gb_svc_conn_destroy_request request;
Viresh Kumard9fcfff2015-08-31 17:21:05 +0530377 struct gb_connection *connection = svc->connection;
378 int ret;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500379
380 request.intf1_id = intf1_id;
Rui Miguel Silva2498050b2015-09-15 15:33:51 +0100381 request.cport1_id = cpu_to_le16(cport1_id);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500382 request.intf2_id = intf2_id;
Rui Miguel Silva2498050b2015-09-15 15:33:51 +0100383 request.cport2_id = cpu_to_le16(cport2_id);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500384
Viresh Kumard9fcfff2015-08-31 17:21:05 +0530385 ret = gb_operation_sync(connection, GB_SVC_TYPE_CONN_DESTROY,
386 &request, sizeof(request), NULL, 0);
Johan Hovold684156a2015-11-25 15:59:19 +0100387 if (ret) {
Viresh Kumar2f3db922015-12-04 21:30:09 +0530388 dev_err(&svc->dev, "failed to destroy connection (%u:%u %u:%u): %d\n",
Johan Hovold684156a2015-11-25 15:59:19 +0100389 intf1_id, cport1_id, intf2_id, cport2_id, ret);
390 }
Alex Elder30c6d9d2015-05-22 13:02:08 -0500391}
Viresh Kumar3f0e9182015-08-31 17:21:06 +0530392EXPORT_SYMBOL_GPL(gb_svc_connection_destroy);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500393
Viresh Kumarbb106852015-09-07 16:01:25 +0530394/* Creates bi-directional routes between the devices */
Johan Hovold4d5f6212016-03-29 18:56:04 -0400395int gb_svc_route_create(struct gb_svc *svc, u8 intf1_id, u8 dev1_id,
Viresh Kumar505f16c2015-08-31 17:21:07 +0530396 u8 intf2_id, u8 dev2_id)
Perry Hunge08aaa42015-07-24 19:02:31 -0400397{
398 struct gb_svc_route_create_request request;
399
400 request.intf1_id = intf1_id;
401 request.dev1_id = dev1_id;
402 request.intf2_id = intf2_id;
403 request.dev2_id = dev2_id;
404
405 return gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_CREATE,
406 &request, sizeof(request), NULL, 0);
407}
Perry Hunge08aaa42015-07-24 19:02:31 -0400408
Viresh Kumar0a020572015-09-07 18:05:26 +0530409/* Destroys bi-directional routes between the devices */
Johan Hovold4d5f6212016-03-29 18:56:04 -0400410void gb_svc_route_destroy(struct gb_svc *svc, u8 intf1_id, u8 intf2_id)
Viresh Kumar0a020572015-09-07 18:05:26 +0530411{
412 struct gb_svc_route_destroy_request request;
413 int ret;
414
415 request.intf1_id = intf1_id;
416 request.intf2_id = intf2_id;
417
418 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_DESTROY,
419 &request, sizeof(request), NULL, 0);
Johan Hovold684156a2015-11-25 15:59:19 +0100420 if (ret) {
Viresh Kumar2f3db922015-12-04 21:30:09 +0530421 dev_err(&svc->dev, "failed to destroy route (%u %u): %d\n",
Johan Hovold684156a2015-11-25 15:59:19 +0100422 intf1_id, intf2_id, ret);
423 }
Viresh Kumar0a020572015-09-07 18:05:26 +0530424}
425
Laurent Pinchartaab4a1a2016-01-06 16:16:46 +0200426int gb_svc_intf_set_power_mode(struct gb_svc *svc, u8 intf_id, u8 hs_series,
427 u8 tx_mode, u8 tx_gear, u8 tx_nlanes,
428 u8 rx_mode, u8 rx_gear, u8 rx_nlanes,
429 u8 flags, u32 quirks)
Laurent Pinchart784f8762015-12-18 21:23:22 +0200430{
Laurent Pinchartaab4a1a2016-01-06 16:16:46 +0200431 struct gb_svc_intf_set_pwrm_request request;
432 struct gb_svc_intf_set_pwrm_response response;
433 int ret;
Laurent Pinchart784f8762015-12-18 21:23:22 +0200434
435 request.intf_id = intf_id;
Laurent Pinchartaab4a1a2016-01-06 16:16:46 +0200436 request.hs_series = hs_series;
437 request.tx_mode = tx_mode;
438 request.tx_gear = tx_gear;
439 request.tx_nlanes = tx_nlanes;
440 request.rx_mode = rx_mode;
441 request.rx_gear = rx_gear;
442 request.rx_nlanes = rx_nlanes;
Laurent Pinchart784f8762015-12-18 21:23:22 +0200443 request.flags = flags;
Laurent Pinchartaab4a1a2016-01-06 16:16:46 +0200444 request.quirks = cpu_to_le32(quirks);
Laurent Pinchart784f8762015-12-18 21:23:22 +0200445
Laurent Pinchartaab4a1a2016-01-06 16:16:46 +0200446 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_SET_PWRM,
447 &request, sizeof(request),
448 &response, sizeof(response));
449 if (ret < 0)
450 return ret;
451
452 return le16_to_cpu(response.result_code);
Laurent Pinchart784f8762015-12-18 21:23:22 +0200453}
Laurent Pinchartaab4a1a2016-01-06 16:16:46 +0200454EXPORT_SYMBOL_GPL(gb_svc_intf_set_power_mode);
Laurent Pinchart784f8762015-12-18 21:23:22 +0200455
Greg Kroah-Hartman55ec09e2016-01-19 23:30:42 -0800456int gb_svc_ping(struct gb_svc *svc)
457{
Greg Kroah-Hartman839ac5b2016-01-26 08:57:50 -0800458 return gb_operation_sync_timeout(svc->connection, GB_SVC_TYPE_PING,
459 NULL, 0, NULL, 0,
460 GB_OPERATION_TIMEOUT_DEFAULT * 2);
Greg Kroah-Hartman55ec09e2016-01-19 23:30:42 -0800461}
462EXPORT_SYMBOL_GPL(gb_svc_ping);
463
Viresh Kumaread35462015-07-21 17:44:19 +0530464static int gb_svc_version_request(struct gb_operation *op)
465{
466 struct gb_connection *connection = op->connection;
Greg Kroah-Hartman0ec30632016-03-22 14:30:35 -0400467 struct gb_svc *svc = gb_connection_get_data(connection);
Johan Hovoldcfb16902015-09-15 10:48:01 +0200468 struct gb_protocol_version_request *request;
469 struct gb_protocol_version_response *response;
Viresh Kumaread35462015-07-21 17:44:19 +0530470
Johan Hovold55510842015-11-19 18:28:01 +0100471 if (op->request->payload_size < sizeof(*request)) {
Johan Hovold684156a2015-11-25 15:59:19 +0100472 dev_err(&svc->dev, "short version request (%zu < %zu)\n",
Johan Hovold55510842015-11-19 18:28:01 +0100473 op->request->payload_size,
474 sizeof(*request));
475 return -EINVAL;
476 }
477
Johan Hovoldcfb16902015-09-15 10:48:01 +0200478 request = op->request->payload;
Viresh Kumaread35462015-07-21 17:44:19 +0530479
Johan Hovoldcfb16902015-09-15 10:48:01 +0200480 if (request->major > GB_SVC_VERSION_MAJOR) {
Viresh Kumar2f3db922015-12-04 21:30:09 +0530481 dev_warn(&svc->dev, "unsupported major version (%u > %u)\n",
Johan Hovold684156a2015-11-25 15:59:19 +0100482 request->major, GB_SVC_VERSION_MAJOR);
Viresh Kumaread35462015-07-21 17:44:19 +0530483 return -ENOTSUPP;
484 }
485
Johan Hovold357de002016-01-19 12:51:19 +0100486 svc->protocol_major = request->major;
487 svc->protocol_minor = request->minor;
Viresh Kumar3ea959e32015-08-11 07:36:14 +0530488
Johan Hovold684156a2015-11-25 15:59:19 +0100489 if (!gb_operation_response_alloc(op, sizeof(*response), GFP_KERNEL))
Viresh Kumaread35462015-07-21 17:44:19 +0530490 return -ENOMEM;
Viresh Kumaread35462015-07-21 17:44:19 +0530491
Johan Hovoldcfb16902015-09-15 10:48:01 +0200492 response = op->response->payload;
Johan Hovold357de002016-01-19 12:51:19 +0100493 response->major = svc->protocol_major;
494 response->minor = svc->protocol_minor;
Johan Hovold59832932015-09-15 10:48:00 +0200495
Viresh Kumaread35462015-07-21 17:44:19 +0530496 return 0;
497}
498
David Lin95046772016-04-20 16:55:08 -0700499static ssize_t pwr_debugfs_voltage_read(struct file *file, char __user *buf,
500 size_t len, loff_t *offset)
501{
502 struct svc_debugfs_pwrmon_rail *pwrmon_rails = file->f_inode->i_private;
503 struct gb_svc *svc = pwrmon_rails->svc;
504 int ret, desc;
505 u32 value;
506 char buff[16];
507
508 ret = gb_svc_pwrmon_sample_get(svc, pwrmon_rails->id,
509 GB_SVC_PWRMON_TYPE_VOL, &value);
510 if (ret) {
511 dev_err(&svc->dev,
Johan Hovold89f2df42016-04-21 11:43:36 +0200512 "failed to get voltage sample %u: %d\n",
513 pwrmon_rails->id, ret);
David Lin95046772016-04-20 16:55:08 -0700514 return ret;
515 }
516
517 desc = scnprintf(buff, sizeof(buff), "%u\n", value);
518
519 return simple_read_from_buffer(buf, len, offset, buff, desc);
520}
521
522static ssize_t pwr_debugfs_current_read(struct file *file, char __user *buf,
523 size_t len, loff_t *offset)
524{
525 struct svc_debugfs_pwrmon_rail *pwrmon_rails = file->f_inode->i_private;
526 struct gb_svc *svc = pwrmon_rails->svc;
527 int ret, desc;
528 u32 value;
529 char buff[16];
530
531 ret = gb_svc_pwrmon_sample_get(svc, pwrmon_rails->id,
532 GB_SVC_PWRMON_TYPE_CURR, &value);
533 if (ret) {
534 dev_err(&svc->dev,
Johan Hovold89f2df42016-04-21 11:43:36 +0200535 "failed to get current sample %u: %d\n",
536 pwrmon_rails->id, ret);
David Lin95046772016-04-20 16:55:08 -0700537 return ret;
538 }
539
540 desc = scnprintf(buff, sizeof(buff), "%u\n", value);
541
542 return simple_read_from_buffer(buf, len, offset, buff, desc);
543}
544
545static ssize_t pwr_debugfs_power_read(struct file *file, char __user *buf,
546 size_t len, loff_t *offset)
547{
548 struct svc_debugfs_pwrmon_rail *pwrmon_rails = file->f_inode->i_private;
549 struct gb_svc *svc = pwrmon_rails->svc;
550 int ret, desc;
551 u32 value;
552 char buff[16];
553
554 ret = gb_svc_pwrmon_sample_get(svc, pwrmon_rails->id,
555 GB_SVC_PWRMON_TYPE_PWR, &value);
556 if (ret) {
Johan Hovold89f2df42016-04-21 11:43:36 +0200557 dev_err(&svc->dev, "failed to get power sample %u: %d\n",
558 pwrmon_rails->id, ret);
David Lin95046772016-04-20 16:55:08 -0700559 return ret;
560 }
561
562 desc = scnprintf(buff, sizeof(buff), "%u\n", value);
563
564 return simple_read_from_buffer(buf, len, offset, buff, desc);
565}
566
567static const struct file_operations pwrmon_debugfs_voltage_fops = {
568 .read = pwr_debugfs_voltage_read,
569};
570
571static const struct file_operations pwrmon_debugfs_current_fops = {
572 .read = pwr_debugfs_current_read,
573};
574
575static const struct file_operations pwrmon_debugfs_power_fops = {
576 .read = pwr_debugfs_power_read,
577};
578
Johan Hovold12185192016-04-23 18:47:20 +0200579static void gb_svc_pwrmon_debugfs_init(struct gb_svc *svc)
David Lin95046772016-04-20 16:55:08 -0700580{
581 int i;
582 size_t bufsize;
583 struct dentry *dent;
584
585 dent = debugfs_create_dir("pwrmon", svc->debugfs_dentry);
586 if (IS_ERR_OR_NULL(dent))
587 return;
588
589 if (gb_svc_pwrmon_rail_count_get(svc, &svc->rail_count))
590 goto err_pwrmon_debugfs;
591
592 if (!svc->rail_count || svc->rail_count > GB_SVC_PWRMON_MAX_RAIL_COUNT)
593 goto err_pwrmon_debugfs;
594
595 bufsize = GB_SVC_PWRMON_RAIL_NAME_BUFSIZE * svc->rail_count;
596
597 svc->rail_names = kzalloc(bufsize, GFP_KERNEL);
598 if (!svc->rail_names)
599 goto err_pwrmon_debugfs;
600
601 svc->pwrmon_rails = kcalloc(svc->rail_count, sizeof(*svc->pwrmon_rails),
602 GFP_KERNEL);
603 if (!svc->pwrmon_rails)
604 goto err_pwrmon_debugfs_free;
605
606 if (gb_svc_pwrmon_rail_names_get(svc, svc->rail_names, bufsize))
607 goto err_pwrmon_debugfs_free;
608
609 for (i = 0; i < svc->rail_count; i++) {
610 struct dentry *dir;
611 struct svc_debugfs_pwrmon_rail *rail = &svc->pwrmon_rails[i];
612 char fname[GB_SVC_PWRMON_RAIL_NAME_BUFSIZE];
613
614 snprintf(fname, sizeof(fname), "%s",
615 (char *)&svc->rail_names->name[i]);
616
617 rail->id = i;
618 rail->svc = svc;
619
620 dir = debugfs_create_dir(fname, dent);
621 debugfs_create_file("voltage_now", S_IRUGO, dir, rail,
622 &pwrmon_debugfs_voltage_fops);
623 debugfs_create_file("current_now", S_IRUGO, dir, rail,
624 &pwrmon_debugfs_current_fops);
625 debugfs_create_file("power_now", S_IRUGO, dir, rail,
626 &pwrmon_debugfs_power_fops);
627 };
628 return;
629
630err_pwrmon_debugfs_free:
631 kfree(svc->rail_names);
632 svc->rail_names = NULL;
633
634 kfree(svc->pwrmon_rails);
635 svc->pwrmon_rails = NULL;
636
637err_pwrmon_debugfs:
638 debugfs_remove(dent);
639}
640
Johan Hovold12185192016-04-23 18:47:20 +0200641static void gb_svc_debugfs_init(struct gb_svc *svc)
David Lin95046772016-04-20 16:55:08 -0700642{
643 svc->debugfs_dentry = debugfs_create_dir(dev_name(&svc->dev),
644 gb_debugfs_get());
Johan Hovold12185192016-04-23 18:47:20 +0200645 gb_svc_pwrmon_debugfs_init(svc);
David Lin95046772016-04-20 16:55:08 -0700646}
647
Johan Hovold12185192016-04-23 18:47:20 +0200648static void gb_svc_debugfs_exit(struct gb_svc *svc)
David Lin95046772016-04-20 16:55:08 -0700649{
650 debugfs_remove_recursive(svc->debugfs_dentry);
651 kfree(svc->rail_names);
652}
653
Viresh Kumaread35462015-07-21 17:44:19 +0530654static int gb_svc_hello(struct gb_operation *op)
655{
656 struct gb_connection *connection = op->connection;
Greg Kroah-Hartman0ec30632016-03-22 14:30:35 -0400657 struct gb_svc *svc = gb_connection_get_data(connection);
Viresh Kumaread35462015-07-21 17:44:19 +0530658 struct gb_svc_hello_request *hello_request;
Viresh Kumaread35462015-07-21 17:44:19 +0530659 int ret;
660
Viresh Kumar0c32d2a2015-08-11 07:29:19 +0530661 if (op->request->payload_size < sizeof(*hello_request)) {
Johan Hovold684156a2015-11-25 15:59:19 +0100662 dev_warn(&svc->dev, "short hello request (%zu < %zu)\n",
663 op->request->payload_size,
664 sizeof(*hello_request));
Viresh Kumaread35462015-07-21 17:44:19 +0530665 return -EINVAL;
666 }
667
668 hello_request = op->request->payload;
Johan Hovold66069fb2015-11-25 15:59:09 +0100669 svc->endo_id = le16_to_cpu(hello_request->endo_id);
670 svc->ap_intf_id = hello_request->interface_id;
Viresh Kumaread35462015-07-21 17:44:19 +0530671
Johan Hovold88f7b962015-11-25 15:59:08 +0100672 ret = device_add(&svc->dev);
673 if (ret) {
674 dev_err(&svc->dev, "failed to register svc device: %d\n", ret);
675 return ret;
676 }
677
Rui Miguel Silvaebe99d62016-01-21 01:42:17 +0000678 ret = input_register_device(svc->input);
679 if (ret) {
680 dev_err(&svc->dev, "failed to register input: %d\n", ret);
681 device_del(&svc->dev);
682 return ret;
683 }
684
Greg Kroah-Hartmaned7279a2016-01-20 22:51:49 -0800685 ret = gb_svc_watchdog_create(svc);
686 if (ret) {
687 dev_err(&svc->dev, "failed to create watchdog: %d\n", ret);
688 input_unregister_device(svc->input);
689 device_del(&svc->dev);
Greg Kroah-Hartman539d6e12016-01-23 17:36:00 -0800690 return ret;
Greg Kroah-Hartmaned7279a2016-01-20 22:51:49 -0800691 }
692
Johan Hovold12185192016-04-23 18:47:20 +0200693 gb_svc_debugfs_init(svc);
David Lin95046772016-04-20 16:55:08 -0700694
Viresh Kumaread35462015-07-21 17:44:19 +0530695 return 0;
696}
697
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200698static struct gb_module *gb_svc_module_lookup(struct gb_svc *svc, u8 module_id)
699{
700 struct gb_host_device *hd = svc->hd;
701 struct gb_module *module;
702
703 list_for_each_entry(module, &hd->modules, hd_node) {
704 if (module->module_id == module_id)
705 return module;
706 }
707
708 return NULL;
709}
710
Johan Hovold844fcbf2016-04-23 18:47:23 +0200711static void gb_svc_intf_reenable(struct gb_svc *svc, struct gb_interface *intf)
712{
713 int ret;
714
Johan Hovold36602a22016-04-23 18:47:25 +0200715 mutex_lock(&intf->mutex);
716
Johan Hovold844fcbf2016-04-23 18:47:23 +0200717 /* Mark as disconnected to prevent I/O during disable. */
718 intf->disconnected = true;
719 gb_interface_disable(intf);
720 intf->disconnected = false;
721
722 ret = gb_interface_enable(intf);
723 if (ret) {
724 dev_err(&svc->dev, "failed to enable interface %u: %d\n",
725 intf->interface_id, ret);
726
727 gb_interface_deactivate(intf);
728 }
Johan Hovold36602a22016-04-23 18:47:25 +0200729
730 mutex_unlock(&intf->mutex);
Johan Hovold844fcbf2016-04-23 18:47:23 +0200731}
732
Johan Hovold9ae41092015-12-02 18:23:29 +0100733static void gb_svc_process_intf_hotplug(struct gb_operation *operation)
Alex Elder30c6d9d2015-05-22 13:02:08 -0500734{
Johan Hovold24456a092015-12-02 18:23:27 +0100735 struct gb_svc_intf_hotplug_request *request;
Johan Hovold9ae41092015-12-02 18:23:29 +0100736 struct gb_connection *connection = operation->connection;
Greg Kroah-Hartman0ec30632016-03-22 14:30:35 -0400737 struct gb_svc *svc = gb_connection_get_data(connection);
Johan Hovold25376362015-11-03 18:03:23 +0100738 struct gb_host_device *hd = connection->hd;
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200739 struct gb_module *module;
Johan Hovoldbb2533a2016-03-09 12:20:34 +0100740 u8 intf_id;
Viresh Kumaread35462015-07-21 17:44:19 +0530741 int ret;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500742
Johan Hovold24456a092015-12-02 18:23:27 +0100743 /* The request message size has already been verified. */
Johan Hovold9ae41092015-12-02 18:23:29 +0100744 request = operation->request->payload;
Johan Hovold24456a092015-12-02 18:23:27 +0100745 intf_id = request->intf_id;
746
747 dev_dbg(&svc->dev, "%s - id = %u\n", __func__, intf_id);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500748
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200749 /* All modules are considered 1x2 for now */
750 module = gb_svc_module_lookup(svc, intf_id);
751 if (module) {
Johan Hovold96fb6c32016-04-13 19:19:08 +0200752 dev_info(&svc->dev, "mode switch detected on interface %u\n",
Johan Hovold684156a2015-11-25 15:59:19 +0100753 intf_id);
Johan Hovold96fb6c32016-04-13 19:19:08 +0200754
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200755 return gb_svc_intf_reenable(svc, module->interfaces[0]);
Viresh Kumarbbaca712015-09-23 16:48:08 -0700756 }
757
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200758 module = gb_module_create(hd, intf_id, 1);
759 if (!module) {
760 dev_err(&svc->dev, "failed to create module\n");
Johan Hovold9ae41092015-12-02 18:23:29 +0100761 return;
Viresh Kumaread35462015-07-21 17:44:19 +0530762 }
763
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200764 ret = gb_module_add(module);
Johan Hovold153ff7e2016-03-29 18:56:08 -0400765 if (ret) {
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200766 gb_module_put(module);
Johan Hovold96fb6c32016-04-13 19:19:08 +0200767 return;
Johan Hovold153ff7e2016-03-29 18:56:08 -0400768 }
Viresh Kumar63d742b2015-12-23 09:07:42 +0530769
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200770 list_add(&module->hd_node, &hd->modules);
Johan Hovold9ae41092015-12-02 18:23:29 +0100771}
772
Johan Hovold57ccd4b2015-12-02 18:23:30 +0100773static void gb_svc_process_intf_hot_unplug(struct gb_operation *operation)
774{
Greg Kroah-Hartman0ec30632016-03-22 14:30:35 -0400775 struct gb_svc *svc = gb_connection_get_data(operation->connection);
Johan Hovold57ccd4b2015-12-02 18:23:30 +0100776 struct gb_svc_intf_hot_unplug_request *request;
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200777 struct gb_module *module;
Johan Hovold57ccd4b2015-12-02 18:23:30 +0100778 u8 intf_id;
779
780 /* The request message size has already been verified. */
781 request = operation->request->payload;
782 intf_id = request->intf_id;
783
784 dev_dbg(&svc->dev, "%s - id = %u\n", __func__, intf_id);
785
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200786 /* All modules are considered 1x2 for now */
787 module = gb_svc_module_lookup(svc, intf_id);
788 if (!module) {
Viresh Kumar2f3db922015-12-04 21:30:09 +0530789 dev_warn(&svc->dev, "could not find hot-unplug interface %u\n",
Johan Hovold57ccd4b2015-12-02 18:23:30 +0100790 intf_id);
791 return;
792 }
793
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200794 module->disconnected = true;
Johan Hovold41d51402016-04-13 19:19:09 +0200795
Johan Hovoldb15d97d2016-04-23 18:47:24 +0200796 gb_module_del(module);
797 list_del(&module->hd_node);
798 gb_module_put(module);
Johan Hovold57ccd4b2015-12-02 18:23:30 +0100799}
800
Johan Hovold9ae41092015-12-02 18:23:29 +0100801static void gb_svc_process_deferred_request(struct work_struct *work)
802{
803 struct gb_svc_deferred_request *dr;
804 struct gb_operation *operation;
805 struct gb_svc *svc;
806 u8 type;
807
808 dr = container_of(work, struct gb_svc_deferred_request, work);
809 operation = dr->operation;
Greg Kroah-Hartman0ec30632016-03-22 14:30:35 -0400810 svc = gb_connection_get_data(operation->connection);
Johan Hovold9ae41092015-12-02 18:23:29 +0100811 type = operation->request->header->type;
812
813 switch (type) {
814 case GB_SVC_TYPE_INTF_HOTPLUG:
815 gb_svc_process_intf_hotplug(operation);
816 break;
Johan Hovold57ccd4b2015-12-02 18:23:30 +0100817 case GB_SVC_TYPE_INTF_HOT_UNPLUG:
818 gb_svc_process_intf_hot_unplug(operation);
819 break;
Johan Hovold9ae41092015-12-02 18:23:29 +0100820 default:
Viresh Kumarb933fa42015-12-04 21:30:10 +0530821 dev_err(&svc->dev, "bad deferred request type: 0x%02x\n", type);
Johan Hovold9ae41092015-12-02 18:23:29 +0100822 }
823
824 gb_operation_put(operation);
825 kfree(dr);
826}
827
828static int gb_svc_queue_deferred_request(struct gb_operation *operation)
829{
Greg Kroah-Hartman0ec30632016-03-22 14:30:35 -0400830 struct gb_svc *svc = gb_connection_get_data(operation->connection);
Johan Hovold9ae41092015-12-02 18:23:29 +0100831 struct gb_svc_deferred_request *dr;
832
833 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
834 if (!dr)
835 return -ENOMEM;
836
837 gb_operation_get(operation);
838
839 dr->operation = operation;
840 INIT_WORK(&dr->work, gb_svc_process_deferred_request);
841
Johan Hovold3e48aca2015-12-02 18:23:31 +0100842 queue_work(svc->wq, &dr->work);
Johan Hovold9ae41092015-12-02 18:23:29 +0100843
844 return 0;
Viresh Kumar067906f2015-08-06 12:44:55 +0530845}
Viresh Kumaread35462015-07-21 17:44:19 +0530846
Viresh Kumar067906f2015-08-06 12:44:55 +0530847/*
848 * Bringing up a module can be time consuming, as that may require lots of
849 * initialization on the module side. Over that, we may also need to download
850 * the firmware first and flash that on the module.
851 *
Johan Hovold3e48aca2015-12-02 18:23:31 +0100852 * In order not to make other svc events wait for all this to finish,
Viresh Kumar067906f2015-08-06 12:44:55 +0530853 * handle most of module hotplug stuff outside of the hotplug callback, with
854 * help of a workqueue.
855 */
856static int gb_svc_intf_hotplug_recv(struct gb_operation *op)
857{
Greg Kroah-Hartman0ec30632016-03-22 14:30:35 -0400858 struct gb_svc *svc = gb_connection_get_data(op->connection);
Johan Hovoldd34a3642015-12-02 18:23:26 +0100859 struct gb_svc_intf_hotplug_request *request;
Viresh Kumar067906f2015-08-06 12:44:55 +0530860
Johan Hovoldd34a3642015-12-02 18:23:26 +0100861 if (op->request->payload_size < sizeof(*request)) {
Johan Hovold684156a2015-11-25 15:59:19 +0100862 dev_warn(&svc->dev, "short hotplug request received (%zu < %zu)\n",
Johan Hovoldd34a3642015-12-02 18:23:26 +0100863 op->request->payload_size, sizeof(*request));
Viresh Kumar067906f2015-08-06 12:44:55 +0530864 return -EINVAL;
865 }
866
Johan Hovoldd34a3642015-12-02 18:23:26 +0100867 request = op->request->payload;
868
869 dev_dbg(&svc->dev, "%s - id = %u\n", __func__, request->intf_id);
870
Johan Hovold9ae41092015-12-02 18:23:29 +0100871 return gb_svc_queue_deferred_request(op);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500872}
873
874static int gb_svc_intf_hot_unplug_recv(struct gb_operation *op)
875{
Greg Kroah-Hartman0ec30632016-03-22 14:30:35 -0400876 struct gb_svc *svc = gb_connection_get_data(op->connection);
Johan Hovoldd34a3642015-12-02 18:23:26 +0100877 struct gb_svc_intf_hot_unplug_request *request;
Alex Elder30c6d9d2015-05-22 13:02:08 -0500878
Johan Hovoldd34a3642015-12-02 18:23:26 +0100879 if (op->request->payload_size < sizeof(*request)) {
Johan Hovold684156a2015-11-25 15:59:19 +0100880 dev_warn(&svc->dev, "short hot unplug request received (%zu < %zu)\n",
Johan Hovoldd34a3642015-12-02 18:23:26 +0100881 op->request->payload_size, sizeof(*request));
Alex Elder30c6d9d2015-05-22 13:02:08 -0500882 return -EINVAL;
883 }
Alex Elder30c6d9d2015-05-22 13:02:08 -0500884
Johan Hovoldd34a3642015-12-02 18:23:26 +0100885 request = op->request->payload;
Johan Hovoldd34a3642015-12-02 18:23:26 +0100886
Johan Hovold57ccd4b2015-12-02 18:23:30 +0100887 dev_dbg(&svc->dev, "%s - id = %u\n", __func__, request->intf_id);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500888
Johan Hovold57ccd4b2015-12-02 18:23:30 +0100889 return gb_svc_queue_deferred_request(op);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500890}
891
892static int gb_svc_intf_reset_recv(struct gb_operation *op)
893{
Greg Kroah-Hartman0ec30632016-03-22 14:30:35 -0400894 struct gb_svc *svc = gb_connection_get_data(op->connection);
Alex Elder30c6d9d2015-05-22 13:02:08 -0500895 struct gb_message *request = op->request;
896 struct gb_svc_intf_reset_request *reset;
897 u8 intf_id;
898
899 if (request->payload_size < sizeof(*reset)) {
Johan Hovold684156a2015-11-25 15:59:19 +0100900 dev_warn(&svc->dev, "short reset request received (%zu < %zu)\n",
901 request->payload_size, sizeof(*reset));
Alex Elder30c6d9d2015-05-22 13:02:08 -0500902 return -EINVAL;
903 }
904 reset = request->payload;
905
906 intf_id = reset->intf_id;
907
908 /* FIXME Reset the interface here */
909
910 return 0;
911}
912
Rui Miguel Silvaebe99d62016-01-21 01:42:17 +0000913static int gb_svc_key_code_map(struct gb_svc *svc, u16 key_code, u16 *code)
914{
915 switch (key_code) {
916 case GB_KEYCODE_ARA:
917 *code = SVC_KEY_ARA_BUTTON;
918 break;
919 default:
920 dev_warn(&svc->dev, "unknown keycode received: %u\n", key_code);
921 return -EINVAL;
922 }
923
924 return 0;
925}
926
927static int gb_svc_key_event_recv(struct gb_operation *op)
928{
Greg Kroah-Hartman0ec30632016-03-22 14:30:35 -0400929 struct gb_svc *svc = gb_connection_get_data(op->connection);
Rui Miguel Silvaebe99d62016-01-21 01:42:17 +0000930 struct gb_message *request = op->request;
931 struct gb_svc_key_event_request *key;
932 u16 code;
933 u8 event;
934 int ret;
935
936 if (request->payload_size < sizeof(*key)) {
937 dev_warn(&svc->dev, "short key request received (%zu < %zu)\n",
938 request->payload_size, sizeof(*key));
939 return -EINVAL;
940 }
941
942 key = request->payload;
943
944 ret = gb_svc_key_code_map(svc, le16_to_cpu(key->key_code), &code);
945 if (ret < 0)
946 return ret;
947
948 event = key->key_event;
949 if ((event != GB_SVC_KEY_PRESSED) && (event != GB_SVC_KEY_RELEASED)) {
950 dev_warn(&svc->dev, "unknown key event received: %u\n", event);
951 return -EINVAL;
952 }
953
954 input_report_key(svc->input, code, (event == GB_SVC_KEY_PRESSED));
955 input_sync(svc->input);
956
957 return 0;
958}
959
Johan Hovold84427942016-01-19 12:51:15 +0100960static int gb_svc_request_handler(struct gb_operation *op)
Alex Elder30c6d9d2015-05-22 13:02:08 -0500961{
Viresh Kumar3ccb1602015-09-03 15:42:22 +0530962 struct gb_connection *connection = op->connection;
Greg Kroah-Hartman0ec30632016-03-22 14:30:35 -0400963 struct gb_svc *svc = gb_connection_get_data(connection);
Johan Hovold84427942016-01-19 12:51:15 +0100964 u8 type = op->type;
Viresh Kumar3ccb1602015-09-03 15:42:22 +0530965 int ret = 0;
966
967 /*
968 * SVC requests need to follow a specific order (at least initially) and
969 * below code takes care of enforcing that. The expected order is:
970 * - PROTOCOL_VERSION
971 * - SVC_HELLO
972 * - Any other request, but the earlier two.
973 *
974 * Incoming requests are guaranteed to be serialized and so we don't
975 * need to protect 'state' for any races.
976 */
Alex Elder30c6d9d2015-05-22 13:02:08 -0500977 switch (type) {
Viresh Kumar0e2462d2015-08-14 07:57:38 +0530978 case GB_REQUEST_TYPE_PROTOCOL_VERSION:
Viresh Kumar3ccb1602015-09-03 15:42:22 +0530979 if (svc->state != GB_SVC_STATE_RESET)
980 ret = -EINVAL;
981 break;
Viresh Kumaread35462015-07-21 17:44:19 +0530982 case GB_SVC_TYPE_SVC_HELLO:
Viresh Kumar3ccb1602015-09-03 15:42:22 +0530983 if (svc->state != GB_SVC_STATE_PROTOCOL_VERSION)
984 ret = -EINVAL;
985 break;
986 default:
987 if (svc->state != GB_SVC_STATE_SVC_HELLO)
988 ret = -EINVAL;
989 break;
990 }
991
992 if (ret) {
Johan Hovold684156a2015-11-25 15:59:19 +0100993 dev_warn(&svc->dev, "unexpected request 0x%02x received (state %u)\n",
994 type, svc->state);
Viresh Kumar3ccb1602015-09-03 15:42:22 +0530995 return ret;
996 }
997
998 switch (type) {
999 case GB_REQUEST_TYPE_PROTOCOL_VERSION:
1000 ret = gb_svc_version_request(op);
1001 if (!ret)
1002 svc->state = GB_SVC_STATE_PROTOCOL_VERSION;
1003 return ret;
1004 case GB_SVC_TYPE_SVC_HELLO:
1005 ret = gb_svc_hello(op);
1006 if (!ret)
1007 svc->state = GB_SVC_STATE_SVC_HELLO;
1008 return ret;
Alex Elder30c6d9d2015-05-22 13:02:08 -05001009 case GB_SVC_TYPE_INTF_HOTPLUG:
1010 return gb_svc_intf_hotplug_recv(op);
1011 case GB_SVC_TYPE_INTF_HOT_UNPLUG:
1012 return gb_svc_intf_hot_unplug_recv(op);
1013 case GB_SVC_TYPE_INTF_RESET:
1014 return gb_svc_intf_reset_recv(op);
Rui Miguel Silvaebe99d62016-01-21 01:42:17 +00001015 case GB_SVC_TYPE_KEY_EVENT:
1016 return gb_svc_key_event_recv(op);
Alex Elder30c6d9d2015-05-22 13:02:08 -05001017 default:
Johan Hovold684156a2015-11-25 15:59:19 +01001018 dev_warn(&svc->dev, "unsupported request 0x%02x\n", type);
Alex Elder30c6d9d2015-05-22 13:02:08 -05001019 return -EINVAL;
1020 }
1021}
1022
Rui Miguel Silvaebe99d62016-01-21 01:42:17 +00001023static struct input_dev *gb_svc_input_create(struct gb_svc *svc)
1024{
1025 struct input_dev *input_dev;
1026
1027 input_dev = input_allocate_device();
1028 if (!input_dev)
1029 return ERR_PTR(-ENOMEM);
1030
1031 input_dev->name = dev_name(&svc->dev);
1032 svc->input_phys = kasprintf(GFP_KERNEL, "greybus-%s/input0",
1033 input_dev->name);
1034 if (!svc->input_phys)
1035 goto err_free_input;
1036
1037 input_dev->phys = svc->input_phys;
1038 input_dev->dev.parent = &svc->dev;
1039
1040 input_set_drvdata(input_dev, svc);
1041
1042 input_set_capability(input_dev, EV_KEY, SVC_KEY_ARA_BUTTON);
1043
1044 return input_dev;
1045
1046err_free_input:
1047 input_free_device(svc->input);
1048 return ERR_PTR(-ENOMEM);
1049}
1050
Johan Hovoldefe6ef72015-11-25 15:59:06 +01001051static void gb_svc_release(struct device *dev)
1052{
Johan Hovold88f7b962015-11-25 15:59:08 +01001053 struct gb_svc *svc = to_gb_svc(dev);
Johan Hovoldefe6ef72015-11-25 15:59:06 +01001054
Johan Hovold7adeaae72015-12-07 15:05:37 +01001055 if (svc->connection)
1056 gb_connection_destroy(svc->connection);
Johan Hovoldefe6ef72015-11-25 15:59:06 +01001057 ida_destroy(&svc->device_id_map);
Johan Hovold3e48aca2015-12-02 18:23:31 +01001058 destroy_workqueue(svc->wq);
Rui Miguel Silvaebe99d62016-01-21 01:42:17 +00001059 kfree(svc->input_phys);
Johan Hovoldefe6ef72015-11-25 15:59:06 +01001060 kfree(svc);
1061}
1062
1063struct device_type greybus_svc_type = {
1064 .name = "greybus_svc",
1065 .release = gb_svc_release,
1066};
1067
Johan Hovold7adeaae72015-12-07 15:05:37 +01001068struct gb_svc *gb_svc_create(struct gb_host_device *hd)
Alex Elder30c6d9d2015-05-22 13:02:08 -05001069{
1070 struct gb_svc *svc;
Alex Elder30c6d9d2015-05-22 13:02:08 -05001071
1072 svc = kzalloc(sizeof(*svc), GFP_KERNEL);
1073 if (!svc)
Johan Hovold7adeaae72015-12-07 15:05:37 +01001074 return NULL;
Alex Elder30c6d9d2015-05-22 13:02:08 -05001075
Johan Hovold3e48aca2015-12-02 18:23:31 +01001076 svc->wq = alloc_workqueue("%s:svc", WQ_UNBOUND, 1, dev_name(&hd->dev));
1077 if (!svc->wq) {
1078 kfree(svc);
Johan Hovold7adeaae72015-12-07 15:05:37 +01001079 return NULL;
Johan Hovold3e48aca2015-12-02 18:23:31 +01001080 }
1081
Johan Hovoldefe6ef72015-11-25 15:59:06 +01001082 svc->dev.parent = &hd->dev;
1083 svc->dev.bus = &greybus_bus_type;
1084 svc->dev.type = &greybus_svc_type;
Johan Hovold66069fb2015-11-25 15:59:09 +01001085 svc->dev.groups = svc_groups;
Johan Hovoldefe6ef72015-11-25 15:59:06 +01001086 svc->dev.dma_mask = svc->dev.parent->dma_mask;
1087 device_initialize(&svc->dev);
1088
1089 dev_set_name(&svc->dev, "%d-svc", hd->bus_id);
1090
Johan Hovold6106e512015-11-25 15:59:07 +01001091 ida_init(&svc->device_id_map);
Viresh Kumar3ccb1602015-09-03 15:42:22 +05301092 svc->state = GB_SVC_STATE_RESET;
Johan Hovoldf0960d02015-12-03 19:18:02 +01001093 svc->hd = hd;
Viresh Kumard3d44842015-07-21 17:44:18 +05301094
Rui Miguel Silvaebe99d62016-01-21 01:42:17 +00001095 svc->input = gb_svc_input_create(svc);
1096 if (IS_ERR(svc->input)) {
1097 dev_err(&svc->dev, "failed to create input device: %ld\n",
1098 PTR_ERR(svc->input));
1099 goto err_put_device;
1100 }
1101
Johan Hovoldf7ee0812016-01-21 17:34:21 +01001102 svc->connection = gb_connection_create_static(hd, GB_SVC_CPORT_ID,
1103 gb_svc_request_handler);
Johan Hovold24e094d2016-01-21 17:34:16 +01001104 if (IS_ERR(svc->connection)) {
1105 dev_err(&svc->dev, "failed to create connection: %ld\n",
1106 PTR_ERR(svc->connection));
Rui Miguel Silvaebe99d62016-01-21 01:42:17 +00001107 goto err_free_input;
Johan Hovold7adeaae72015-12-07 15:05:37 +01001108 }
1109
Greg Kroah-Hartman0ec30632016-03-22 14:30:35 -04001110 gb_connection_set_data(svc->connection, svc);
Johan Hovold7adeaae72015-12-07 15:05:37 +01001111
1112 return svc;
Rui Miguel Silvaebe99d62016-01-21 01:42:17 +00001113
1114err_free_input:
1115 input_free_device(svc->input);
1116err_put_device:
1117 put_device(&svc->dev);
1118 return NULL;
Johan Hovold7adeaae72015-12-07 15:05:37 +01001119}
1120
1121int gb_svc_add(struct gb_svc *svc)
1122{
1123 int ret;
1124
1125 /*
1126 * The SVC protocol is currently driven by the SVC, so the SVC device
1127 * is added from the connection request handler when enough
1128 * information has been received.
1129 */
Johan Hovoldf7ee0812016-01-21 17:34:21 +01001130 ret = gb_connection_enable(svc->connection);
Johan Hovold7adeaae72015-12-07 15:05:37 +01001131 if (ret)
1132 return ret;
1133
1134 return 0;
1135}
1136
Johan Hovoldb15d97d2016-04-23 18:47:24 +02001137static void gb_svc_remove_modules(struct gb_svc *svc)
Johan Hovold66d674c2016-03-09 12:20:41 +01001138{
Johan Hovoldb15d97d2016-04-23 18:47:24 +02001139 struct gb_host_device *hd = svc->hd;
1140 struct gb_module *module, *tmp;
Johan Hovold66d674c2016-03-09 12:20:41 +01001141
Johan Hovoldb15d97d2016-04-23 18:47:24 +02001142 list_for_each_entry_safe(module, tmp, &hd->modules, hd_node) {
1143 gb_module_del(module);
1144 list_del(&module->hd_node);
1145 gb_module_put(module);
Johan Hovold629c0d02016-03-09 12:20:43 +01001146 }
Johan Hovold66d674c2016-03-09 12:20:41 +01001147}
1148
Johan Hovold7adeaae72015-12-07 15:05:37 +01001149void gb_svc_del(struct gb_svc *svc)
1150{
Johan Hovold84427942016-01-19 12:51:15 +01001151 gb_connection_disable(svc->connection);
Johan Hovold7adeaae72015-12-07 15:05:37 +01001152
Rui Miguel Silvaebe99d62016-01-21 01:42:17 +00001153 /*
1154 * The SVC device and input device may have been registered
1155 * from the request handler.
1156 */
1157 if (device_is_registered(&svc->dev)) {
Johan Hovold12185192016-04-23 18:47:20 +02001158 gb_svc_debugfs_exit(svc);
Greg Kroah-Hartmaned7279a2016-01-20 22:51:49 -08001159 gb_svc_watchdog_destroy(svc);
Rui Miguel Silvaebe99d62016-01-21 01:42:17 +00001160 input_unregister_device(svc->input);
1161 device_del(&svc->dev);
1162 }
1163
Johan Hovold7adeaae72015-12-07 15:05:37 +01001164 flush_workqueue(svc->wq);
Johan Hovold66d674c2016-03-09 12:20:41 +01001165
Johan Hovoldb15d97d2016-04-23 18:47:24 +02001166 gb_svc_remove_modules(svc);
Johan Hovold7adeaae72015-12-07 15:05:37 +01001167}
1168
1169void gb_svc_put(struct gb_svc *svc)
1170{
1171 put_device(&svc->dev);
1172}