5a16ad33b74b2dc91512d6dc6751a660b1e6dee2
[cascardo/linux.git] / drivers / staging / greybus / svc.c
1 /*
2  * SVC Greybus driver.
3  *
4  * Copyright 2015 Google Inc.
5  * Copyright 2015 Linaro Ltd.
6  *
7  * Released under the GPLv2 only.
8  */
9
10 #include <linux/input.h>
11 #include <linux/workqueue.h>
12
13 #include "greybus.h"
14
15 #define SVC_KEY_ARA_BUTTON      KEY_A
16
17 #define SVC_INTF_EJECT_TIMEOUT  9000
18
19 struct gb_svc_deferred_request {
20         struct work_struct work;
21         struct gb_operation *operation;
22 };
23
24
25 static ssize_t endo_id_show(struct device *dev,
26                         struct device_attribute *attr, char *buf)
27 {
28         struct gb_svc *svc = to_gb_svc(dev);
29
30         return sprintf(buf, "0x%04x\n", svc->endo_id);
31 }
32 static DEVICE_ATTR_RO(endo_id);
33
34 static ssize_t ap_intf_id_show(struct device *dev,
35                         struct device_attribute *attr, char *buf)
36 {
37         struct gb_svc *svc = to_gb_svc(dev);
38
39         return sprintf(buf, "%u\n", svc->ap_intf_id);
40 }
41 static DEVICE_ATTR_RO(ap_intf_id);
42
43
44 // FIXME
45 // This is a hack, we need to do this "right" and clean the interface up
46 // properly, not just forcibly yank the thing out of the system and hope for the
47 // best.  But for now, people want their modules to come out without having to
48 // throw the thing to the ground or get out a screwdriver.
49 static ssize_t intf_eject_store(struct device *dev,
50                                 struct device_attribute *attr, const char *buf,
51                                 size_t len)
52 {
53         struct gb_svc *svc = to_gb_svc(dev);
54         unsigned short intf_id;
55         int ret;
56
57         ret = kstrtou16(buf, 10, &intf_id);
58         if (ret < 0)
59                 return ret;
60
61         dev_warn(dev, "Forcibly trying to eject interface %d\n", intf_id);
62
63         ret = gb_svc_intf_eject(svc, intf_id);
64         if (ret < 0)
65                 return ret;
66
67         return len;
68 }
69 static DEVICE_ATTR_WO(intf_eject);
70
71 static ssize_t watchdog_show(struct device *dev, struct device_attribute *attr,
72                              char *buf)
73 {
74         struct gb_svc *svc = to_gb_svc(dev);
75
76         return sprintf(buf, "%s\n",
77                        gb_svc_watchdog_enabled(svc) ? "enabled" : "disabled");
78 }
79
80 static ssize_t watchdog_store(struct device *dev,
81                               struct device_attribute *attr, const char *buf,
82                               size_t len)
83 {
84         struct gb_svc *svc = to_gb_svc(dev);
85         int retval;
86         bool user_request;
87
88         retval = strtobool(buf, &user_request);
89         if (retval)
90                 return retval;
91
92         if (user_request)
93                 retval = gb_svc_watchdog_enable(svc);
94         else
95                 retval = gb_svc_watchdog_disable(svc);
96         if (retval)
97                 return retval;
98         return len;
99 }
100 static DEVICE_ATTR_RW(watchdog);
101
102 int gb_svc_pwrmon_intf_sample_get(struct gb_svc *svc, u8 intf_id,
103                                   u8 measurement_type, u32 *value)
104 {
105         struct gb_svc_pwrmon_intf_sample_get_request request;
106         struct gb_svc_pwrmon_intf_sample_get_response response;
107         int ret;
108
109         request.intf_id = intf_id;
110         request.measurement_type = measurement_type;
111
112         ret = gb_operation_sync(svc->connection,
113                                 GB_SVC_TYPE_PWRMON_INTF_SAMPLE_GET,
114                                 &request, sizeof(request),
115                                 &response, sizeof(response));
116         if (ret) {
117                 dev_err(&svc->dev, "failed to get intf sample (%d)\n", ret);
118                 return ret;
119         }
120
121         if (response.result) {
122                 dev_err(&svc->dev,
123                         "UniPro error while getting intf power sample (%d %d): %d\n",
124                         intf_id, measurement_type, response.result);
125                 switch (response.result) {
126                 case GB_SVC_PWRMON_GET_SAMPLE_INVAL:
127                         return -EINVAL;
128                 case GB_SVC_PWRMON_GET_SAMPLE_NOSUPP:
129                         return -ENOSYS;
130                 default:
131                         return -EIO;
132                 }
133         }
134
135         *value = le32_to_cpu(response.measurement);
136
137         return 0;
138 }
139
140 static struct attribute *svc_attrs[] = {
141         &dev_attr_endo_id.attr,
142         &dev_attr_ap_intf_id.attr,
143         &dev_attr_intf_eject.attr,
144         &dev_attr_watchdog.attr,
145         NULL,
146 };
147 ATTRIBUTE_GROUPS(svc);
148
149 int gb_svc_intf_device_id(struct gb_svc *svc, u8 intf_id, u8 device_id)
150 {
151         struct gb_svc_intf_device_id_request request;
152
153         request.intf_id = intf_id;
154         request.device_id = device_id;
155
156         return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_DEVICE_ID,
157                                  &request, sizeof(request), NULL, 0);
158 }
159
160 int gb_svc_intf_eject(struct gb_svc *svc, u8 intf_id)
161 {
162         struct gb_svc_intf_eject_request request;
163         int ret;
164
165         request.intf_id = intf_id;
166
167         /*
168          * The pulse width for module release in svc is long so we need to
169          * increase the timeout so the operation will not return to soon.
170          */
171         ret = gb_operation_sync_timeout(svc->connection,
172                                         GB_SVC_TYPE_INTF_EJECT, &request,
173                                         sizeof(request), NULL, 0,
174                                         SVC_INTF_EJECT_TIMEOUT);
175         if (ret) {
176                 dev_err(&svc->dev, "failed to eject interface %u\n", intf_id);
177                 return ret;
178         }
179
180         return 0;
181 }
182
183 int gb_svc_dme_peer_get(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
184                         u32 *value)
185 {
186         struct gb_svc_dme_peer_get_request request;
187         struct gb_svc_dme_peer_get_response response;
188         u16 result;
189         int ret;
190
191         request.intf_id = intf_id;
192         request.attr = cpu_to_le16(attr);
193         request.selector = cpu_to_le16(selector);
194
195         ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_GET,
196                                 &request, sizeof(request),
197                                 &response, sizeof(response));
198         if (ret) {
199                 dev_err(&svc->dev, "failed to get DME attribute (%u 0x%04x %u): %d\n",
200                                 intf_id, attr, selector, ret);
201                 return ret;
202         }
203
204         result = le16_to_cpu(response.result_code);
205         if (result) {
206                 dev_err(&svc->dev, "UniPro error while getting DME attribute (%u 0x%04x %u): %u\n",
207                                 intf_id, attr, selector, result);
208                 return -EIO;
209         }
210
211         if (value)
212                 *value = le32_to_cpu(response.attr_value);
213
214         return 0;
215 }
216 EXPORT_SYMBOL_GPL(gb_svc_dme_peer_get);
217
218 int gb_svc_dme_peer_set(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
219                         u32 value)
220 {
221         struct gb_svc_dme_peer_set_request request;
222         struct gb_svc_dme_peer_set_response response;
223         u16 result;
224         int ret;
225
226         request.intf_id = intf_id;
227         request.attr = cpu_to_le16(attr);
228         request.selector = cpu_to_le16(selector);
229         request.value = cpu_to_le32(value);
230
231         ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_SET,
232                                 &request, sizeof(request),
233                                 &response, sizeof(response));
234         if (ret) {
235                 dev_err(&svc->dev, "failed to set DME attribute (%u 0x%04x %u %u): %d\n",
236                                 intf_id, attr, selector, value, ret);
237                 return ret;
238         }
239
240         result = le16_to_cpu(response.result_code);
241         if (result) {
242                 dev_err(&svc->dev, "UniPro error while setting DME attribute (%u 0x%04x %u %u): %u\n",
243                                 intf_id, attr, selector, value, result);
244                 return -EIO;
245         }
246
247         return 0;
248 }
249 EXPORT_SYMBOL_GPL(gb_svc_dme_peer_set);
250
251 int gb_svc_connection_create(struct gb_svc *svc,
252                                 u8 intf1_id, u16 cport1_id,
253                                 u8 intf2_id, u16 cport2_id,
254                                 u8 cport_flags)
255 {
256         struct gb_svc_conn_create_request request;
257
258         request.intf1_id = intf1_id;
259         request.cport1_id = cpu_to_le16(cport1_id);
260         request.intf2_id = intf2_id;
261         request.cport2_id = cpu_to_le16(cport2_id);
262         request.tc = 0;         /* TC0 */
263         request.flags = cport_flags;
264
265         return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_CREATE,
266                                  &request, sizeof(request), NULL, 0);
267 }
268 EXPORT_SYMBOL_GPL(gb_svc_connection_create);
269
270 void gb_svc_connection_destroy(struct gb_svc *svc, u8 intf1_id, u16 cport1_id,
271                                u8 intf2_id, u16 cport2_id)
272 {
273         struct gb_svc_conn_destroy_request request;
274         struct gb_connection *connection = svc->connection;
275         int ret;
276
277         request.intf1_id = intf1_id;
278         request.cport1_id = cpu_to_le16(cport1_id);
279         request.intf2_id = intf2_id;
280         request.cport2_id = cpu_to_le16(cport2_id);
281
282         ret = gb_operation_sync(connection, GB_SVC_TYPE_CONN_DESTROY,
283                                 &request, sizeof(request), NULL, 0);
284         if (ret) {
285                 dev_err(&svc->dev, "failed to destroy connection (%u:%u %u:%u): %d\n",
286                                 intf1_id, cport1_id, intf2_id, cport2_id, ret);
287         }
288 }
289 EXPORT_SYMBOL_GPL(gb_svc_connection_destroy);
290
291 /* Creates bi-directional routes between the devices */
292 int gb_svc_route_create(struct gb_svc *svc, u8 intf1_id, u8 dev1_id,
293                                u8 intf2_id, u8 dev2_id)
294 {
295         struct gb_svc_route_create_request request;
296
297         request.intf1_id = intf1_id;
298         request.dev1_id = dev1_id;
299         request.intf2_id = intf2_id;
300         request.dev2_id = dev2_id;
301
302         return gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_CREATE,
303                                  &request, sizeof(request), NULL, 0);
304 }
305
306 /* Destroys bi-directional routes between the devices */
307 void gb_svc_route_destroy(struct gb_svc *svc, u8 intf1_id, u8 intf2_id)
308 {
309         struct gb_svc_route_destroy_request request;
310         int ret;
311
312         request.intf1_id = intf1_id;
313         request.intf2_id = intf2_id;
314
315         ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_DESTROY,
316                                 &request, sizeof(request), NULL, 0);
317         if (ret) {
318                 dev_err(&svc->dev, "failed to destroy route (%u %u): %d\n",
319                                 intf1_id, intf2_id, ret);
320         }
321 }
322
323 int gb_svc_intf_set_power_mode(struct gb_svc *svc, u8 intf_id, u8 hs_series,
324                                u8 tx_mode, u8 tx_gear, u8 tx_nlanes,
325                                u8 rx_mode, u8 rx_gear, u8 rx_nlanes,
326                                u8 flags, u32 quirks)
327 {
328         struct gb_svc_intf_set_pwrm_request request;
329         struct gb_svc_intf_set_pwrm_response response;
330         int ret;
331
332         request.intf_id = intf_id;
333         request.hs_series = hs_series;
334         request.tx_mode = tx_mode;
335         request.tx_gear = tx_gear;
336         request.tx_nlanes = tx_nlanes;
337         request.rx_mode = rx_mode;
338         request.rx_gear = rx_gear;
339         request.rx_nlanes = rx_nlanes;
340         request.flags = flags;
341         request.quirks = cpu_to_le32(quirks);
342
343         ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_SET_PWRM,
344                                 &request, sizeof(request),
345                                 &response, sizeof(response));
346         if (ret < 0)
347                 return ret;
348
349         return le16_to_cpu(response.result_code);
350 }
351 EXPORT_SYMBOL_GPL(gb_svc_intf_set_power_mode);
352
353 int gb_svc_ping(struct gb_svc *svc)
354 {
355         return gb_operation_sync_timeout(svc->connection, GB_SVC_TYPE_PING,
356                                          NULL, 0, NULL, 0,
357                                          GB_OPERATION_TIMEOUT_DEFAULT * 2);
358 }
359 EXPORT_SYMBOL_GPL(gb_svc_ping);
360
361 static int gb_svc_version_request(struct gb_operation *op)
362 {
363         struct gb_connection *connection = op->connection;
364         struct gb_svc *svc = gb_connection_get_data(connection);
365         struct gb_protocol_version_request *request;
366         struct gb_protocol_version_response *response;
367
368         if (op->request->payload_size < sizeof(*request)) {
369                 dev_err(&svc->dev, "short version request (%zu < %zu)\n",
370                                 op->request->payload_size,
371                                 sizeof(*request));
372                 return -EINVAL;
373         }
374
375         request = op->request->payload;
376
377         if (request->major > GB_SVC_VERSION_MAJOR) {
378                 dev_warn(&svc->dev, "unsupported major version (%u > %u)\n",
379                                 request->major, GB_SVC_VERSION_MAJOR);
380                 return -ENOTSUPP;
381         }
382
383         svc->protocol_major = request->major;
384         svc->protocol_minor = request->minor;
385
386         if (!gb_operation_response_alloc(op, sizeof(*response), GFP_KERNEL))
387                 return -ENOMEM;
388
389         response = op->response->payload;
390         response->major = svc->protocol_major;
391         response->minor = svc->protocol_minor;
392
393         return 0;
394 }
395
396 static int gb_svc_hello(struct gb_operation *op)
397 {
398         struct gb_connection *connection = op->connection;
399         struct gb_svc *svc = gb_connection_get_data(connection);
400         struct gb_svc_hello_request *hello_request;
401         int ret;
402
403         if (op->request->payload_size < sizeof(*hello_request)) {
404                 dev_warn(&svc->dev, "short hello request (%zu < %zu)\n",
405                                 op->request->payload_size,
406                                 sizeof(*hello_request));
407                 return -EINVAL;
408         }
409
410         hello_request = op->request->payload;
411         svc->endo_id = le16_to_cpu(hello_request->endo_id);
412         svc->ap_intf_id = hello_request->interface_id;
413
414         ret = device_add(&svc->dev);
415         if (ret) {
416                 dev_err(&svc->dev, "failed to register svc device: %d\n", ret);
417                 return ret;
418         }
419
420         ret = input_register_device(svc->input);
421         if (ret) {
422                 dev_err(&svc->dev, "failed to register input: %d\n", ret);
423                 device_del(&svc->dev);
424                 return ret;
425         }
426
427         ret = gb_svc_watchdog_create(svc);
428         if (ret) {
429                 dev_err(&svc->dev, "failed to create watchdog: %d\n", ret);
430                 input_unregister_device(svc->input);
431                 device_del(&svc->dev);
432                 return ret;
433         }
434
435         return 0;
436 }
437
438 static void gb_svc_intf_remove(struct gb_svc *svc, struct gb_interface *intf)
439 {
440         intf->disconnected = true;
441
442         gb_interface_disable(intf);
443         gb_interface_deactivate(intf);
444         gb_interface_remove(intf);
445 }
446
447 static void gb_svc_process_intf_hotplug(struct gb_operation *operation)
448 {
449         struct gb_svc_intf_hotplug_request *request;
450         struct gb_connection *connection = operation->connection;
451         struct gb_svc *svc = gb_connection_get_data(connection);
452         struct gb_host_device *hd = connection->hd;
453         struct gb_interface *intf;
454         u8 intf_id;
455         u32 vendor_id = 0;
456         u32 product_id = 0;
457         int ret;
458
459         /* The request message size has already been verified. */
460         request = operation->request->payload;
461         intf_id = request->intf_id;
462
463         dev_dbg(&svc->dev, "%s - id = %u\n", __func__, intf_id);
464
465         intf = gb_interface_find(hd, intf_id);
466         if (intf) {
467                 /* HACK: Save Ara VID/PID for ES2 hack below */
468                 vendor_id = intf->vendor_id;
469                 product_id = intf->product_id;
470
471                 /*
472                  * We have received a hotplug request for an interface that
473                  * already exists.
474                  *
475                  * This can happen in cases like:
476                  * - bootrom loading the firmware image and booting into that,
477                  *   which only generates a hotplug event. i.e. no hot-unplug
478                  *   event.
479                  * - Or the firmware on the module crashed and sent hotplug
480                  *   request again to the SVC, which got propagated to AP.
481                  *
482                  * Remove the interface and add it again, and let user know
483                  * about this with a print message.
484                  */
485                 dev_info(&svc->dev, "removing interface %u to add it again\n",
486                                 intf_id);
487                 gb_svc_intf_remove(svc, intf);
488         }
489
490         intf = gb_interface_create(hd, intf_id);
491         if (!intf) {
492                 dev_err(&svc->dev, "failed to create interface %u\n",
493                                 intf_id);
494                 return;
495         }
496
497         ret = gb_interface_activate(intf);
498         if (ret) {
499                 dev_err(&svc->dev, "failed to activate interface %u: %d\n",
500                                 intf_id, ret);
501                 goto err_interface_add;
502         }
503
504         /*
505          * HACK: Use Ara VID/PID from earlier boot stage.
506          *
507          * FIXME: remove quirk with ES2 support
508          */
509         if (intf->quirks & GB_INTERFACE_QUIRK_NO_ARA_IDS) {
510                 intf->vendor_id = vendor_id;
511                 intf->product_id = product_id;
512         }
513
514         ret = gb_interface_enable(intf);
515         if (ret) {
516                 dev_err(&svc->dev, "failed to enable interface %u: %d\n",
517                                 intf_id, ret);
518                 goto err_interface_deactivate;
519         }
520
521         ret = gb_interface_add(intf);
522         if (ret) {
523                 gb_interface_disable(intf);
524                 gb_interface_deactivate(intf);
525                 return;
526         }
527
528         return;
529
530 err_interface_deactivate:
531         gb_interface_deactivate(intf);
532 err_interface_add:
533         gb_interface_add(intf);
534 }
535
536 static void gb_svc_process_intf_hot_unplug(struct gb_operation *operation)
537 {
538         struct gb_svc *svc = gb_connection_get_data(operation->connection);
539         struct gb_svc_intf_hot_unplug_request *request;
540         struct gb_host_device *hd = operation->connection->hd;
541         struct gb_interface *intf;
542         u8 intf_id;
543
544         /* The request message size has already been verified. */
545         request = operation->request->payload;
546         intf_id = request->intf_id;
547
548         dev_dbg(&svc->dev, "%s - id = %u\n", __func__, intf_id);
549
550         intf = gb_interface_find(hd, intf_id);
551         if (!intf) {
552                 dev_warn(&svc->dev, "could not find hot-unplug interface %u\n",
553                                 intf_id);
554                 return;
555         }
556
557         gb_svc_intf_remove(svc, intf);
558 }
559
560 static void gb_svc_process_deferred_request(struct work_struct *work)
561 {
562         struct gb_svc_deferred_request *dr;
563         struct gb_operation *operation;
564         struct gb_svc *svc;
565         u8 type;
566
567         dr = container_of(work, struct gb_svc_deferred_request, work);
568         operation = dr->operation;
569         svc = gb_connection_get_data(operation->connection);
570         type = operation->request->header->type;
571
572         switch (type) {
573         case GB_SVC_TYPE_INTF_HOTPLUG:
574                 gb_svc_process_intf_hotplug(operation);
575                 break;
576         case GB_SVC_TYPE_INTF_HOT_UNPLUG:
577                 gb_svc_process_intf_hot_unplug(operation);
578                 break;
579         default:
580                 dev_err(&svc->dev, "bad deferred request type: 0x%02x\n", type);
581         }
582
583         gb_operation_put(operation);
584         kfree(dr);
585 }
586
587 static int gb_svc_queue_deferred_request(struct gb_operation *operation)
588 {
589         struct gb_svc *svc = gb_connection_get_data(operation->connection);
590         struct gb_svc_deferred_request *dr;
591
592         dr = kmalloc(sizeof(*dr), GFP_KERNEL);
593         if (!dr)
594                 return -ENOMEM;
595
596         gb_operation_get(operation);
597
598         dr->operation = operation;
599         INIT_WORK(&dr->work, gb_svc_process_deferred_request);
600
601         queue_work(svc->wq, &dr->work);
602
603         return 0;
604 }
605
606 /*
607  * Bringing up a module can be time consuming, as that may require lots of
608  * initialization on the module side. Over that, we may also need to download
609  * the firmware first and flash that on the module.
610  *
611  * In order not to make other svc events wait for all this to finish,
612  * handle most of module hotplug stuff outside of the hotplug callback, with
613  * help of a workqueue.
614  */
615 static int gb_svc_intf_hotplug_recv(struct gb_operation *op)
616 {
617         struct gb_svc *svc = gb_connection_get_data(op->connection);
618         struct gb_svc_intf_hotplug_request *request;
619
620         if (op->request->payload_size < sizeof(*request)) {
621                 dev_warn(&svc->dev, "short hotplug request received (%zu < %zu)\n",
622                                 op->request->payload_size, sizeof(*request));
623                 return -EINVAL;
624         }
625
626         request = op->request->payload;
627
628         dev_dbg(&svc->dev, "%s - id = %u\n", __func__, request->intf_id);
629
630         return gb_svc_queue_deferred_request(op);
631 }
632
633 static int gb_svc_intf_hot_unplug_recv(struct gb_operation *op)
634 {
635         struct gb_svc *svc = gb_connection_get_data(op->connection);
636         struct gb_svc_intf_hot_unplug_request *request;
637
638         if (op->request->payload_size < sizeof(*request)) {
639                 dev_warn(&svc->dev, "short hot unplug request received (%zu < %zu)\n",
640                                 op->request->payload_size, sizeof(*request));
641                 return -EINVAL;
642         }
643
644         request = op->request->payload;
645
646         dev_dbg(&svc->dev, "%s - id = %u\n", __func__, request->intf_id);
647
648         return gb_svc_queue_deferred_request(op);
649 }
650
651 static int gb_svc_intf_reset_recv(struct gb_operation *op)
652 {
653         struct gb_svc *svc = gb_connection_get_data(op->connection);
654         struct gb_message *request = op->request;
655         struct gb_svc_intf_reset_request *reset;
656         u8 intf_id;
657
658         if (request->payload_size < sizeof(*reset)) {
659                 dev_warn(&svc->dev, "short reset request received (%zu < %zu)\n",
660                                 request->payload_size, sizeof(*reset));
661                 return -EINVAL;
662         }
663         reset = request->payload;
664
665         intf_id = reset->intf_id;
666
667         /* FIXME Reset the interface here */
668
669         return 0;
670 }
671
672 static int gb_svc_key_code_map(struct gb_svc *svc, u16 key_code, u16 *code)
673 {
674         switch (key_code) {
675         case GB_KEYCODE_ARA:
676                 *code = SVC_KEY_ARA_BUTTON;
677                 break;
678         default:
679                 dev_warn(&svc->dev, "unknown keycode received: %u\n", key_code);
680                 return -EINVAL;
681         }
682
683         return 0;
684 }
685
686 static int gb_svc_key_event_recv(struct gb_operation *op)
687 {
688         struct gb_svc *svc = gb_connection_get_data(op->connection);
689         struct gb_message *request = op->request;
690         struct gb_svc_key_event_request *key;
691         u16 code;
692         u8 event;
693         int ret;
694
695         if (request->payload_size < sizeof(*key)) {
696                 dev_warn(&svc->dev, "short key request received (%zu < %zu)\n",
697                          request->payload_size, sizeof(*key));
698                 return -EINVAL;
699         }
700
701         key = request->payload;
702
703         ret = gb_svc_key_code_map(svc, le16_to_cpu(key->key_code), &code);
704         if (ret < 0)
705                 return ret;
706
707         event = key->key_event;
708         if ((event != GB_SVC_KEY_PRESSED) && (event != GB_SVC_KEY_RELEASED)) {
709                 dev_warn(&svc->dev, "unknown key event received: %u\n", event);
710                 return -EINVAL;
711         }
712
713         input_report_key(svc->input, code, (event == GB_SVC_KEY_PRESSED));
714         input_sync(svc->input);
715
716         return 0;
717 }
718
719 static int gb_svc_request_handler(struct gb_operation *op)
720 {
721         struct gb_connection *connection = op->connection;
722         struct gb_svc *svc = gb_connection_get_data(connection);
723         u8 type = op->type;
724         int ret = 0;
725
726         /*
727          * SVC requests need to follow a specific order (at least initially) and
728          * below code takes care of enforcing that. The expected order is:
729          * - PROTOCOL_VERSION
730          * - SVC_HELLO
731          * - Any other request, but the earlier two.
732          *
733          * Incoming requests are guaranteed to be serialized and so we don't
734          * need to protect 'state' for any races.
735          */
736         switch (type) {
737         case GB_REQUEST_TYPE_PROTOCOL_VERSION:
738                 if (svc->state != GB_SVC_STATE_RESET)
739                         ret = -EINVAL;
740                 break;
741         case GB_SVC_TYPE_SVC_HELLO:
742                 if (svc->state != GB_SVC_STATE_PROTOCOL_VERSION)
743                         ret = -EINVAL;
744                 break;
745         default:
746                 if (svc->state != GB_SVC_STATE_SVC_HELLO)
747                         ret = -EINVAL;
748                 break;
749         }
750
751         if (ret) {
752                 dev_warn(&svc->dev, "unexpected request 0x%02x received (state %u)\n",
753                                 type, svc->state);
754                 return ret;
755         }
756
757         switch (type) {
758         case GB_REQUEST_TYPE_PROTOCOL_VERSION:
759                 ret = gb_svc_version_request(op);
760                 if (!ret)
761                         svc->state = GB_SVC_STATE_PROTOCOL_VERSION;
762                 return ret;
763         case GB_SVC_TYPE_SVC_HELLO:
764                 ret = gb_svc_hello(op);
765                 if (!ret)
766                         svc->state = GB_SVC_STATE_SVC_HELLO;
767                 return ret;
768         case GB_SVC_TYPE_INTF_HOTPLUG:
769                 return gb_svc_intf_hotplug_recv(op);
770         case GB_SVC_TYPE_INTF_HOT_UNPLUG:
771                 return gb_svc_intf_hot_unplug_recv(op);
772         case GB_SVC_TYPE_INTF_RESET:
773                 return gb_svc_intf_reset_recv(op);
774         case GB_SVC_TYPE_KEY_EVENT:
775                 return gb_svc_key_event_recv(op);
776         default:
777                 dev_warn(&svc->dev, "unsupported request 0x%02x\n", type);
778                 return -EINVAL;
779         }
780 }
781
782 static struct input_dev *gb_svc_input_create(struct gb_svc *svc)
783 {
784         struct input_dev *input_dev;
785
786         input_dev = input_allocate_device();
787         if (!input_dev)
788                 return ERR_PTR(-ENOMEM);
789
790         input_dev->name = dev_name(&svc->dev);
791         svc->input_phys = kasprintf(GFP_KERNEL, "greybus-%s/input0",
792                                     input_dev->name);
793         if (!svc->input_phys)
794                 goto err_free_input;
795
796         input_dev->phys = svc->input_phys;
797         input_dev->dev.parent = &svc->dev;
798
799         input_set_drvdata(input_dev, svc);
800
801         input_set_capability(input_dev, EV_KEY, SVC_KEY_ARA_BUTTON);
802
803         return input_dev;
804
805 err_free_input:
806         input_free_device(svc->input);
807         return ERR_PTR(-ENOMEM);
808 }
809
810 static void gb_svc_release(struct device *dev)
811 {
812         struct gb_svc *svc = to_gb_svc(dev);
813
814         if (svc->connection)
815                 gb_connection_destroy(svc->connection);
816         ida_destroy(&svc->device_id_map);
817         destroy_workqueue(svc->wq);
818         kfree(svc->input_phys);
819         kfree(svc);
820 }
821
822 struct device_type greybus_svc_type = {
823         .name           = "greybus_svc",
824         .release        = gb_svc_release,
825 };
826
827 struct gb_svc *gb_svc_create(struct gb_host_device *hd)
828 {
829         struct gb_svc *svc;
830
831         svc = kzalloc(sizeof(*svc), GFP_KERNEL);
832         if (!svc)
833                 return NULL;
834
835         svc->wq = alloc_workqueue("%s:svc", WQ_UNBOUND, 1, dev_name(&hd->dev));
836         if (!svc->wq) {
837                 kfree(svc);
838                 return NULL;
839         }
840
841         svc->dev.parent = &hd->dev;
842         svc->dev.bus = &greybus_bus_type;
843         svc->dev.type = &greybus_svc_type;
844         svc->dev.groups = svc_groups;
845         svc->dev.dma_mask = svc->dev.parent->dma_mask;
846         device_initialize(&svc->dev);
847
848         dev_set_name(&svc->dev, "%d-svc", hd->bus_id);
849
850         ida_init(&svc->device_id_map);
851         svc->state = GB_SVC_STATE_RESET;
852         svc->hd = hd;
853
854         svc->input = gb_svc_input_create(svc);
855         if (IS_ERR(svc->input)) {
856                 dev_err(&svc->dev, "failed to create input device: %ld\n",
857                         PTR_ERR(svc->input));
858                 goto err_put_device;
859         }
860
861         svc->connection = gb_connection_create_static(hd, GB_SVC_CPORT_ID,
862                                                 gb_svc_request_handler);
863         if (IS_ERR(svc->connection)) {
864                 dev_err(&svc->dev, "failed to create connection: %ld\n",
865                                 PTR_ERR(svc->connection));
866                 goto err_free_input;
867         }
868
869         gb_connection_set_data(svc->connection, svc);
870
871         return svc;
872
873 err_free_input:
874         input_free_device(svc->input);
875 err_put_device:
876         put_device(&svc->dev);
877         return NULL;
878 }
879
880 int gb_svc_add(struct gb_svc *svc)
881 {
882         int ret;
883
884         /*
885          * The SVC protocol is currently driven by the SVC, so the SVC device
886          * is added from the connection request handler when enough
887          * information has been received.
888          */
889         ret = gb_connection_enable(svc->connection);
890         if (ret)
891                 return ret;
892
893         return 0;
894 }
895
896 static void gb_svc_remove_interfaces(struct gb_svc *svc)
897 {
898         struct gb_interface *intf, *tmp;
899
900         list_for_each_entry_safe(intf, tmp, &svc->hd->interfaces, links) {
901                 gb_interface_disable(intf);
902                 gb_interface_remove(intf);
903         }
904 }
905
906 void gb_svc_del(struct gb_svc *svc)
907 {
908         gb_connection_disable(svc->connection);
909
910         /*
911          * The SVC device and input device may have been registered
912          * from the request handler.
913          */
914         if (device_is_registered(&svc->dev)) {
915                 gb_svc_watchdog_destroy(svc);
916                 input_unregister_device(svc->input);
917                 device_del(&svc->dev);
918         }
919
920         flush_workqueue(svc->wq);
921
922         gb_svc_remove_interfaces(svc);
923 }
924
925 void gb_svc_put(struct gb_svc *svc)
926 {
927         put_device(&svc->dev);
928 }