greybus: svc: remove bogus interface-reset helper
[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 static struct attribute *svc_attrs[] = {
103         &dev_attr_endo_id.attr,
104         &dev_attr_ap_intf_id.attr,
105         &dev_attr_intf_eject.attr,
106         &dev_attr_watchdog.attr,
107         NULL,
108 };
109 ATTRIBUTE_GROUPS(svc);
110
111 static int gb_svc_intf_device_id(struct gb_svc *svc, u8 intf_id, u8 device_id)
112 {
113         struct gb_svc_intf_device_id_request request;
114
115         request.intf_id = intf_id;
116         request.device_id = device_id;
117
118         return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_DEVICE_ID,
119                                  &request, sizeof(request), NULL, 0);
120 }
121
122 int gb_svc_intf_eject(struct gb_svc *svc, u8 intf_id)
123 {
124         struct gb_svc_intf_eject_request request;
125         int ret;
126
127         request.intf_id = intf_id;
128
129         /*
130          * The pulse width for module release in svc is long so we need to
131          * increase the timeout so the operation will not return to soon.
132          */
133         ret = gb_operation_sync_timeout(svc->connection,
134                                         GB_SVC_TYPE_INTF_EJECT, &request,
135                                         sizeof(request), NULL, 0,
136                                         SVC_INTF_EJECT_TIMEOUT);
137         if (ret) {
138                 dev_err(&svc->dev, "failed to eject interface %u\n", intf_id);
139                 return ret;
140         }
141
142         return 0;
143 }
144
145 int gb_svc_dme_peer_get(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
146                         u32 *value)
147 {
148         struct gb_svc_dme_peer_get_request request;
149         struct gb_svc_dme_peer_get_response response;
150         u16 result;
151         int ret;
152
153         request.intf_id = intf_id;
154         request.attr = cpu_to_le16(attr);
155         request.selector = cpu_to_le16(selector);
156
157         ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_GET,
158                                 &request, sizeof(request),
159                                 &response, sizeof(response));
160         if (ret) {
161                 dev_err(&svc->dev, "failed to get DME attribute (%u 0x%04x %u): %d\n",
162                                 intf_id, attr, selector, ret);
163                 return ret;
164         }
165
166         result = le16_to_cpu(response.result_code);
167         if (result) {
168                 dev_err(&svc->dev, "UniPro error while getting DME attribute (%u 0x%04x %u): %u\n",
169                                 intf_id, attr, selector, result);
170                 return -EIO;
171         }
172
173         if (value)
174                 *value = le32_to_cpu(response.attr_value);
175
176         return 0;
177 }
178 EXPORT_SYMBOL_GPL(gb_svc_dme_peer_get);
179
180 int gb_svc_dme_peer_set(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
181                         u32 value)
182 {
183         struct gb_svc_dme_peer_set_request request;
184         struct gb_svc_dme_peer_set_response response;
185         u16 result;
186         int ret;
187
188         request.intf_id = intf_id;
189         request.attr = cpu_to_le16(attr);
190         request.selector = cpu_to_le16(selector);
191         request.value = cpu_to_le32(value);
192
193         ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_SET,
194                                 &request, sizeof(request),
195                                 &response, sizeof(response));
196         if (ret) {
197                 dev_err(&svc->dev, "failed to set DME attribute (%u 0x%04x %u %u): %d\n",
198                                 intf_id, attr, selector, value, ret);
199                 return ret;
200         }
201
202         result = le16_to_cpu(response.result_code);
203         if (result) {
204                 dev_err(&svc->dev, "UniPro error while setting DME attribute (%u 0x%04x %u %u): %u\n",
205                                 intf_id, attr, selector, value, result);
206                 return -EIO;
207         }
208
209         return 0;
210 }
211 EXPORT_SYMBOL_GPL(gb_svc_dme_peer_set);
212
213 /*
214  * T_TstSrcIncrement is written by the module on ES2 as a stand-in for boot
215  * status attribute ES3_INIT_STATUS. AP needs to read and clear it, after
216  * reading a non-zero value from it.
217  *
218  * FIXME: This is module-hardware dependent and needs to be extended for every
219  * type of module we want to support.
220  */
221 static int gb_svc_read_and_clear_module_boot_status(struct gb_interface *intf)
222 {
223         struct gb_host_device *hd = intf->hd;
224         int ret;
225         u32 value;
226         u16 attr;
227         u8 init_status;
228
229         /*
230          * Check if the module is ES2 or ES3, and choose attr number
231          * appropriately.
232          * FIXME: Remove ES2 support from the kernel entirely.
233          */
234         if (intf->ddbl1_manufacturer_id == ES2_DDBL1_MFR_ID &&
235                                 intf->ddbl1_product_id == ES2_DDBL1_PROD_ID)
236                 attr = DME_ATTR_T_TST_SRC_INCREMENT;
237         else
238                 attr = DME_ATTR_ES3_INIT_STATUS;
239
240         /* Read and clear boot status in ES3_INIT_STATUS */
241         ret = gb_svc_dme_peer_get(hd->svc, intf->interface_id, attr,
242                                   DME_ATTR_SELECTOR_INDEX, &value);
243
244         if (ret)
245                 return ret;
246
247         /*
248          * A nonzero boot status indicates the module has finished
249          * booting. Clear it.
250          */
251         if (!value) {
252                 dev_err(&intf->dev, "Module not ready yet\n");
253                 return -ENODEV;
254         }
255
256         /*
257          * Check if the module needs to boot from UniPro.
258          * For ES2: We need to check lowest 8 bits of 'value'.
259          * For ES3: We need to check highest 8 bits out of 32 of 'value'.
260          * FIXME: Remove ES2 support from the kernel entirely.
261          */
262         if (intf->ddbl1_manufacturer_id == ES2_DDBL1_MFR_ID &&
263                                 intf->ddbl1_product_id == ES2_DDBL1_PROD_ID)
264                 init_status = value;
265         else
266                 init_status = value >> 24;
267
268         if (init_status == DME_DIS_UNIPRO_BOOT_STARTED ||
269                                 init_status == DME_DIS_FALLBACK_UNIPRO_BOOT_STARTED)
270                 intf->boot_over_unipro = true;
271
272         return gb_svc_dme_peer_set(hd->svc, intf->interface_id, attr,
273                                    DME_ATTR_SELECTOR_INDEX, 0);
274 }
275
276 int gb_svc_connection_create(struct gb_svc *svc,
277                                 u8 intf1_id, u16 cport1_id,
278                                 u8 intf2_id, u16 cport2_id,
279                                 u8 cport_flags)
280 {
281         struct gb_svc_conn_create_request request;
282
283         request.intf1_id = intf1_id;
284         request.cport1_id = cpu_to_le16(cport1_id);
285         request.intf2_id = intf2_id;
286         request.cport2_id = cpu_to_le16(cport2_id);
287         request.tc = 0;         /* TC0 */
288         request.flags = cport_flags;
289
290         return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_CREATE,
291                                  &request, sizeof(request), NULL, 0);
292 }
293 EXPORT_SYMBOL_GPL(gb_svc_connection_create);
294
295 void gb_svc_connection_destroy(struct gb_svc *svc, u8 intf1_id, u16 cport1_id,
296                                u8 intf2_id, u16 cport2_id)
297 {
298         struct gb_svc_conn_destroy_request request;
299         struct gb_connection *connection = svc->connection;
300         int ret;
301
302         request.intf1_id = intf1_id;
303         request.cport1_id = cpu_to_le16(cport1_id);
304         request.intf2_id = intf2_id;
305         request.cport2_id = cpu_to_le16(cport2_id);
306
307         ret = gb_operation_sync(connection, GB_SVC_TYPE_CONN_DESTROY,
308                                 &request, sizeof(request), NULL, 0);
309         if (ret) {
310                 dev_err(&svc->dev, "failed to destroy connection (%u:%u %u:%u): %d\n",
311                                 intf1_id, cport1_id, intf2_id, cport2_id, ret);
312         }
313 }
314 EXPORT_SYMBOL_GPL(gb_svc_connection_destroy);
315
316 /* Creates bi-directional routes between the devices */
317 static int gb_svc_route_create(struct gb_svc *svc, u8 intf1_id, u8 dev1_id,
318                                u8 intf2_id, u8 dev2_id)
319 {
320         struct gb_svc_route_create_request request;
321
322         request.intf1_id = intf1_id;
323         request.dev1_id = dev1_id;
324         request.intf2_id = intf2_id;
325         request.dev2_id = dev2_id;
326
327         return gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_CREATE,
328                                  &request, sizeof(request), NULL, 0);
329 }
330
331 /* Destroys bi-directional routes between the devices */
332 static void gb_svc_route_destroy(struct gb_svc *svc, u8 intf1_id, u8 intf2_id)
333 {
334         struct gb_svc_route_destroy_request request;
335         int ret;
336
337         request.intf1_id = intf1_id;
338         request.intf2_id = intf2_id;
339
340         ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_DESTROY,
341                                 &request, sizeof(request), NULL, 0);
342         if (ret) {
343                 dev_err(&svc->dev, "failed to destroy route (%u %u): %d\n",
344                                 intf1_id, intf2_id, ret);
345         }
346 }
347
348 int gb_svc_intf_set_power_mode(struct gb_svc *svc, u8 intf_id, u8 hs_series,
349                                u8 tx_mode, u8 tx_gear, u8 tx_nlanes,
350                                u8 rx_mode, u8 rx_gear, u8 rx_nlanes,
351                                u8 flags, u32 quirks)
352 {
353         struct gb_svc_intf_set_pwrm_request request;
354         struct gb_svc_intf_set_pwrm_response response;
355         int ret;
356
357         request.intf_id = intf_id;
358         request.hs_series = hs_series;
359         request.tx_mode = tx_mode;
360         request.tx_gear = tx_gear;
361         request.tx_nlanes = tx_nlanes;
362         request.rx_mode = rx_mode;
363         request.rx_gear = rx_gear;
364         request.rx_nlanes = rx_nlanes;
365         request.flags = flags;
366         request.quirks = cpu_to_le32(quirks);
367
368         ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_SET_PWRM,
369                                 &request, sizeof(request),
370                                 &response, sizeof(response));
371         if (ret < 0)
372                 return ret;
373
374         return le16_to_cpu(response.result_code);
375 }
376 EXPORT_SYMBOL_GPL(gb_svc_intf_set_power_mode);
377
378 int gb_svc_ping(struct gb_svc *svc)
379 {
380         return gb_operation_sync_timeout(svc->connection, GB_SVC_TYPE_PING,
381                                          NULL, 0, NULL, 0,
382                                          GB_OPERATION_TIMEOUT_DEFAULT * 2);
383 }
384 EXPORT_SYMBOL_GPL(gb_svc_ping);
385
386 static int gb_svc_version_request(struct gb_operation *op)
387 {
388         struct gb_connection *connection = op->connection;
389         struct gb_svc *svc = gb_connection_get_data(connection);
390         struct gb_protocol_version_request *request;
391         struct gb_protocol_version_response *response;
392
393         if (op->request->payload_size < sizeof(*request)) {
394                 dev_err(&svc->dev, "short version request (%zu < %zu)\n",
395                                 op->request->payload_size,
396                                 sizeof(*request));
397                 return -EINVAL;
398         }
399
400         request = op->request->payload;
401
402         if (request->major > GB_SVC_VERSION_MAJOR) {
403                 dev_warn(&svc->dev, "unsupported major version (%u > %u)\n",
404                                 request->major, GB_SVC_VERSION_MAJOR);
405                 return -ENOTSUPP;
406         }
407
408         svc->protocol_major = request->major;
409         svc->protocol_minor = request->minor;
410
411         if (!gb_operation_response_alloc(op, sizeof(*response), GFP_KERNEL))
412                 return -ENOMEM;
413
414         response = op->response->payload;
415         response->major = svc->protocol_major;
416         response->minor = svc->protocol_minor;
417
418         return 0;
419 }
420
421 static int gb_svc_hello(struct gb_operation *op)
422 {
423         struct gb_connection *connection = op->connection;
424         struct gb_svc *svc = gb_connection_get_data(connection);
425         struct gb_svc_hello_request *hello_request;
426         int ret;
427
428         if (op->request->payload_size < sizeof(*hello_request)) {
429                 dev_warn(&svc->dev, "short hello request (%zu < %zu)\n",
430                                 op->request->payload_size,
431                                 sizeof(*hello_request));
432                 return -EINVAL;
433         }
434
435         hello_request = op->request->payload;
436         svc->endo_id = le16_to_cpu(hello_request->endo_id);
437         svc->ap_intf_id = hello_request->interface_id;
438
439         ret = device_add(&svc->dev);
440         if (ret) {
441                 dev_err(&svc->dev, "failed to register svc device: %d\n", ret);
442                 return ret;
443         }
444
445         ret = input_register_device(svc->input);
446         if (ret) {
447                 dev_err(&svc->dev, "failed to register input: %d\n", ret);
448                 device_del(&svc->dev);
449                 return ret;
450         }
451
452         ret = gb_svc_watchdog_create(svc);
453         if (ret) {
454                 dev_err(&svc->dev, "failed to create watchdog: %d\n", ret);
455                 input_unregister_device(svc->input);
456                 device_del(&svc->dev);
457                 return ret;
458         }
459
460         return 0;
461 }
462
463 static int gb_svc_interface_route_create(struct gb_svc *svc,
464                                                 struct gb_interface *intf)
465 {
466         u8 intf_id = intf->interface_id;
467         u8 device_id;
468         int ret;
469
470         /*
471          * Create a device id for the interface:
472          * - device id 0 (GB_DEVICE_ID_SVC) belongs to the SVC
473          * - device id 1 (GB_DEVICE_ID_AP) belongs to the AP
474          *
475          * XXX Do we need to allocate device ID for SVC or the AP here? And what
476          * XXX about an AP with multiple interface blocks?
477          */
478         ret = ida_simple_get(&svc->device_id_map,
479                              GB_DEVICE_ID_MODULES_START, 0, GFP_KERNEL);
480         if (ret < 0) {
481                 dev_err(&svc->dev, "failed to allocate device id for interface %u: %d\n",
482                                 intf_id, ret);
483                 return ret;
484         }
485         device_id = ret;
486
487         ret = gb_svc_intf_device_id(svc, intf_id, device_id);
488         if (ret) {
489                 dev_err(&svc->dev, "failed to set device id %u for interface %u: %d\n",
490                                 device_id, intf_id, ret);
491                 goto err_ida_remove;
492         }
493
494         /* Create a two-way route between the AP and the new interface. */
495         ret = gb_svc_route_create(svc, svc->ap_intf_id, GB_DEVICE_ID_AP,
496                                   intf_id, device_id);
497         if (ret) {
498                 dev_err(&svc->dev, "failed to create route to interface %u (device id %u): %d\n",
499                                 intf_id, device_id, ret);
500                 goto err_svc_id_free;
501         }
502
503         intf->device_id = device_id;
504
505         return 0;
506
507 err_svc_id_free:
508         /*
509          * XXX Should we tell SVC that this id doesn't belong to interface
510          * XXX anymore.
511          */
512 err_ida_remove:
513         ida_simple_remove(&svc->device_id_map, device_id);
514
515         return ret;
516 }
517
518 static void gb_svc_interface_route_destroy(struct gb_svc *svc,
519                                                 struct gb_interface *intf)
520 {
521         if (intf->device_id == GB_DEVICE_ID_BAD)
522                 return;
523
524         gb_svc_route_destroy(svc, svc->ap_intf_id, intf->interface_id);
525         ida_simple_remove(&svc->device_id_map, intf->device_id);
526         intf->device_id = GB_DEVICE_ID_BAD;
527 }
528
529 static void gb_svc_intf_remove(struct gb_svc *svc, struct gb_interface *intf)
530 {
531         intf->disconnected = true;
532
533         gb_interface_disable(intf);
534         gb_svc_interface_route_destroy(svc, intf);
535         gb_interface_remove(intf);
536 }
537
538 static void gb_svc_process_intf_hotplug(struct gb_operation *operation)
539 {
540         struct gb_svc_intf_hotplug_request *request;
541         struct gb_connection *connection = operation->connection;
542         struct gb_svc *svc = gb_connection_get_data(connection);
543         struct gb_host_device *hd = connection->hd;
544         struct gb_interface *intf;
545         u8 intf_id;
546         u32 vendor_id = 0;
547         u32 product_id = 0;
548         int ret;
549
550         /* The request message size has already been verified. */
551         request = operation->request->payload;
552         intf_id = request->intf_id;
553
554         dev_dbg(&svc->dev, "%s - id = %u\n", __func__, intf_id);
555
556         intf = gb_interface_find(hd, intf_id);
557         if (intf) {
558                 /*
559                  * For ES2, we need to maintain the same vendor/product ids we
560                  * got from bootrom, otherwise userspace can't distinguish
561                  * between modules.
562                  */
563                 vendor_id = intf->vendor_id;
564                 product_id = intf->product_id;
565
566                 /*
567                  * We have received a hotplug request for an interface that
568                  * already exists.
569                  *
570                  * This can happen in cases like:
571                  * - bootrom loading the firmware image and booting into that,
572                  *   which only generates a hotplug event. i.e. no hot-unplug
573                  *   event.
574                  * - Or the firmware on the module crashed and sent hotplug
575                  *   request again to the SVC, which got propagated to AP.
576                  *
577                  * Remove the interface and add it again, and let user know
578                  * about this with a print message.
579                  */
580                 dev_info(&svc->dev, "removing interface %u to add it again\n",
581                                 intf_id);
582                 gb_svc_intf_remove(svc, intf);
583         }
584
585         intf = gb_interface_create(hd, intf_id);
586         if (!intf) {
587                 dev_err(&svc->dev, "failed to create interface %u\n",
588                                 intf_id);
589                 return;
590         }
591
592         intf->ddbl1_manufacturer_id = le32_to_cpu(request->data.ddbl1_mfr_id);
593         intf->ddbl1_product_id = le32_to_cpu(request->data.ddbl1_prod_id);
594         intf->vendor_id = le32_to_cpu(request->data.ara_vend_id);
595         intf->product_id = le32_to_cpu(request->data.ara_prod_id);
596         intf->serial_number = le64_to_cpu(request->data.serial_number);
597
598         /*
599          * Use VID/PID specified at hotplug if:
600          * - Bridge ASIC chip isn't ES2
601          * - Received non-zero Vendor/Product ids
602          *
603          * Otherwise, use the ids we received from bootrom.
604          */
605         if (intf->ddbl1_manufacturer_id == ES2_DDBL1_MFR_ID &&
606             intf->ddbl1_product_id == ES2_DDBL1_PROD_ID &&
607             intf->vendor_id == 0 && intf->product_id == 0) {
608                 intf->vendor_id = vendor_id;
609                 intf->product_id = product_id;
610         }
611
612         ret = gb_svc_read_and_clear_module_boot_status(intf);
613         if (ret) {
614                 dev_err(&svc->dev, "failed to clear boot status of interface %u: %d\n",
615                                 intf_id, ret);
616                 goto out_interface_add;
617         }
618
619         ret = gb_svc_interface_route_create(svc, intf);
620         if (ret)
621                 goto out_interface_add;
622
623         ret = gb_interface_enable(intf);
624         if (ret) {
625                 dev_err(&svc->dev, "failed to enable interface %u: %d\n",
626                                 intf_id, ret);
627                 goto out_interface_add;
628         }
629
630 out_interface_add:
631         gb_interface_add(intf);
632 }
633
634 static void gb_svc_process_intf_hot_unplug(struct gb_operation *operation)
635 {
636         struct gb_svc *svc = gb_connection_get_data(operation->connection);
637         struct gb_svc_intf_hot_unplug_request *request;
638         struct gb_host_device *hd = operation->connection->hd;
639         struct gb_interface *intf;
640         u8 intf_id;
641
642         /* The request message size has already been verified. */
643         request = operation->request->payload;
644         intf_id = request->intf_id;
645
646         dev_dbg(&svc->dev, "%s - id = %u\n", __func__, intf_id);
647
648         intf = gb_interface_find(hd, intf_id);
649         if (!intf) {
650                 dev_warn(&svc->dev, "could not find hot-unplug interface %u\n",
651                                 intf_id);
652                 return;
653         }
654
655         gb_svc_intf_remove(svc, intf);
656 }
657
658 static void gb_svc_process_deferred_request(struct work_struct *work)
659 {
660         struct gb_svc_deferred_request *dr;
661         struct gb_operation *operation;
662         struct gb_svc *svc;
663         u8 type;
664
665         dr = container_of(work, struct gb_svc_deferred_request, work);
666         operation = dr->operation;
667         svc = gb_connection_get_data(operation->connection);
668         type = operation->request->header->type;
669
670         switch (type) {
671         case GB_SVC_TYPE_INTF_HOTPLUG:
672                 gb_svc_process_intf_hotplug(operation);
673                 break;
674         case GB_SVC_TYPE_INTF_HOT_UNPLUG:
675                 gb_svc_process_intf_hot_unplug(operation);
676                 break;
677         default:
678                 dev_err(&svc->dev, "bad deferred request type: 0x%02x\n", type);
679         }
680
681         gb_operation_put(operation);
682         kfree(dr);
683 }
684
685 static int gb_svc_queue_deferred_request(struct gb_operation *operation)
686 {
687         struct gb_svc *svc = gb_connection_get_data(operation->connection);
688         struct gb_svc_deferred_request *dr;
689
690         dr = kmalloc(sizeof(*dr), GFP_KERNEL);
691         if (!dr)
692                 return -ENOMEM;
693
694         gb_operation_get(operation);
695
696         dr->operation = operation;
697         INIT_WORK(&dr->work, gb_svc_process_deferred_request);
698
699         queue_work(svc->wq, &dr->work);
700
701         return 0;
702 }
703
704 /*
705  * Bringing up a module can be time consuming, as that may require lots of
706  * initialization on the module side. Over that, we may also need to download
707  * the firmware first and flash that on the module.
708  *
709  * In order not to make other svc events wait for all this to finish,
710  * handle most of module hotplug stuff outside of the hotplug callback, with
711  * help of a workqueue.
712  */
713 static int gb_svc_intf_hotplug_recv(struct gb_operation *op)
714 {
715         struct gb_svc *svc = gb_connection_get_data(op->connection);
716         struct gb_svc_intf_hotplug_request *request;
717
718         if (op->request->payload_size < sizeof(*request)) {
719                 dev_warn(&svc->dev, "short hotplug request received (%zu < %zu)\n",
720                                 op->request->payload_size, sizeof(*request));
721                 return -EINVAL;
722         }
723
724         request = op->request->payload;
725
726         dev_dbg(&svc->dev, "%s - id = %u\n", __func__, request->intf_id);
727
728         return gb_svc_queue_deferred_request(op);
729 }
730
731 static int gb_svc_intf_hot_unplug_recv(struct gb_operation *op)
732 {
733         struct gb_svc *svc = gb_connection_get_data(op->connection);
734         struct gb_svc_intf_hot_unplug_request *request;
735
736         if (op->request->payload_size < sizeof(*request)) {
737                 dev_warn(&svc->dev, "short hot unplug request received (%zu < %zu)\n",
738                                 op->request->payload_size, sizeof(*request));
739                 return -EINVAL;
740         }
741
742         request = op->request->payload;
743
744         dev_dbg(&svc->dev, "%s - id = %u\n", __func__, request->intf_id);
745
746         return gb_svc_queue_deferred_request(op);
747 }
748
749 static int gb_svc_intf_reset_recv(struct gb_operation *op)
750 {
751         struct gb_svc *svc = gb_connection_get_data(op->connection);
752         struct gb_message *request = op->request;
753         struct gb_svc_intf_reset_request *reset;
754         u8 intf_id;
755
756         if (request->payload_size < sizeof(*reset)) {
757                 dev_warn(&svc->dev, "short reset request received (%zu < %zu)\n",
758                                 request->payload_size, sizeof(*reset));
759                 return -EINVAL;
760         }
761         reset = request->payload;
762
763         intf_id = reset->intf_id;
764
765         /* FIXME Reset the interface here */
766
767         return 0;
768 }
769
770 static int gb_svc_key_code_map(struct gb_svc *svc, u16 key_code, u16 *code)
771 {
772         switch (key_code) {
773         case GB_KEYCODE_ARA:
774                 *code = SVC_KEY_ARA_BUTTON;
775                 break;
776         default:
777                 dev_warn(&svc->dev, "unknown keycode received: %u\n", key_code);
778                 return -EINVAL;
779         }
780
781         return 0;
782 }
783
784 static int gb_svc_key_event_recv(struct gb_operation *op)
785 {
786         struct gb_svc *svc = gb_connection_get_data(op->connection);
787         struct gb_message *request = op->request;
788         struct gb_svc_key_event_request *key;
789         u16 code;
790         u8 event;
791         int ret;
792
793         if (request->payload_size < sizeof(*key)) {
794                 dev_warn(&svc->dev, "short key request received (%zu < %zu)\n",
795                          request->payload_size, sizeof(*key));
796                 return -EINVAL;
797         }
798
799         key = request->payload;
800
801         ret = gb_svc_key_code_map(svc, le16_to_cpu(key->key_code), &code);
802         if (ret < 0)
803                 return ret;
804
805         event = key->key_event;
806         if ((event != GB_SVC_KEY_PRESSED) && (event != GB_SVC_KEY_RELEASED)) {
807                 dev_warn(&svc->dev, "unknown key event received: %u\n", event);
808                 return -EINVAL;
809         }
810
811         input_report_key(svc->input, code, (event == GB_SVC_KEY_PRESSED));
812         input_sync(svc->input);
813
814         return 0;
815 }
816
817 static int gb_svc_request_handler(struct gb_operation *op)
818 {
819         struct gb_connection *connection = op->connection;
820         struct gb_svc *svc = gb_connection_get_data(connection);
821         u8 type = op->type;
822         int ret = 0;
823
824         /*
825          * SVC requests need to follow a specific order (at least initially) and
826          * below code takes care of enforcing that. The expected order is:
827          * - PROTOCOL_VERSION
828          * - SVC_HELLO
829          * - Any other request, but the earlier two.
830          *
831          * Incoming requests are guaranteed to be serialized and so we don't
832          * need to protect 'state' for any races.
833          */
834         switch (type) {
835         case GB_REQUEST_TYPE_PROTOCOL_VERSION:
836                 if (svc->state != GB_SVC_STATE_RESET)
837                         ret = -EINVAL;
838                 break;
839         case GB_SVC_TYPE_SVC_HELLO:
840                 if (svc->state != GB_SVC_STATE_PROTOCOL_VERSION)
841                         ret = -EINVAL;
842                 break;
843         default:
844                 if (svc->state != GB_SVC_STATE_SVC_HELLO)
845                         ret = -EINVAL;
846                 break;
847         }
848
849         if (ret) {
850                 dev_warn(&svc->dev, "unexpected request 0x%02x received (state %u)\n",
851                                 type, svc->state);
852                 return ret;
853         }
854
855         switch (type) {
856         case GB_REQUEST_TYPE_PROTOCOL_VERSION:
857                 ret = gb_svc_version_request(op);
858                 if (!ret)
859                         svc->state = GB_SVC_STATE_PROTOCOL_VERSION;
860                 return ret;
861         case GB_SVC_TYPE_SVC_HELLO:
862                 ret = gb_svc_hello(op);
863                 if (!ret)
864                         svc->state = GB_SVC_STATE_SVC_HELLO;
865                 return ret;
866         case GB_SVC_TYPE_INTF_HOTPLUG:
867                 return gb_svc_intf_hotplug_recv(op);
868         case GB_SVC_TYPE_INTF_HOT_UNPLUG:
869                 return gb_svc_intf_hot_unplug_recv(op);
870         case GB_SVC_TYPE_INTF_RESET:
871                 return gb_svc_intf_reset_recv(op);
872         case GB_SVC_TYPE_KEY_EVENT:
873                 return gb_svc_key_event_recv(op);
874         default:
875                 dev_warn(&svc->dev, "unsupported request 0x%02x\n", type);
876                 return -EINVAL;
877         }
878 }
879
880 static struct input_dev *gb_svc_input_create(struct gb_svc *svc)
881 {
882         struct input_dev *input_dev;
883
884         input_dev = input_allocate_device();
885         if (!input_dev)
886                 return ERR_PTR(-ENOMEM);
887
888         input_dev->name = dev_name(&svc->dev);
889         svc->input_phys = kasprintf(GFP_KERNEL, "greybus-%s/input0",
890                                     input_dev->name);
891         if (!svc->input_phys)
892                 goto err_free_input;
893
894         input_dev->phys = svc->input_phys;
895         input_dev->dev.parent = &svc->dev;
896
897         input_set_drvdata(input_dev, svc);
898
899         input_set_capability(input_dev, EV_KEY, SVC_KEY_ARA_BUTTON);
900
901         return input_dev;
902
903 err_free_input:
904         input_free_device(svc->input);
905         return ERR_PTR(-ENOMEM);
906 }
907
908 static void gb_svc_release(struct device *dev)
909 {
910         struct gb_svc *svc = to_gb_svc(dev);
911
912         if (svc->connection)
913                 gb_connection_destroy(svc->connection);
914         ida_destroy(&svc->device_id_map);
915         destroy_workqueue(svc->wq);
916         kfree(svc->input_phys);
917         kfree(svc);
918 }
919
920 struct device_type greybus_svc_type = {
921         .name           = "greybus_svc",
922         .release        = gb_svc_release,
923 };
924
925 struct gb_svc *gb_svc_create(struct gb_host_device *hd)
926 {
927         struct gb_svc *svc;
928
929         svc = kzalloc(sizeof(*svc), GFP_KERNEL);
930         if (!svc)
931                 return NULL;
932
933         svc->wq = alloc_workqueue("%s:svc", WQ_UNBOUND, 1, dev_name(&hd->dev));
934         if (!svc->wq) {
935                 kfree(svc);
936                 return NULL;
937         }
938
939         svc->dev.parent = &hd->dev;
940         svc->dev.bus = &greybus_bus_type;
941         svc->dev.type = &greybus_svc_type;
942         svc->dev.groups = svc_groups;
943         svc->dev.dma_mask = svc->dev.parent->dma_mask;
944         device_initialize(&svc->dev);
945
946         dev_set_name(&svc->dev, "%d-svc", hd->bus_id);
947
948         ida_init(&svc->device_id_map);
949         svc->state = GB_SVC_STATE_RESET;
950         svc->hd = hd;
951
952         svc->input = gb_svc_input_create(svc);
953         if (IS_ERR(svc->input)) {
954                 dev_err(&svc->dev, "failed to create input device: %ld\n",
955                         PTR_ERR(svc->input));
956                 goto err_put_device;
957         }
958
959         svc->connection = gb_connection_create_static(hd, GB_SVC_CPORT_ID,
960                                                 gb_svc_request_handler);
961         if (IS_ERR(svc->connection)) {
962                 dev_err(&svc->dev, "failed to create connection: %ld\n",
963                                 PTR_ERR(svc->connection));
964                 goto err_free_input;
965         }
966
967         gb_connection_set_data(svc->connection, svc);
968
969         return svc;
970
971 err_free_input:
972         input_free_device(svc->input);
973 err_put_device:
974         put_device(&svc->dev);
975         return NULL;
976 }
977
978 int gb_svc_add(struct gb_svc *svc)
979 {
980         int ret;
981
982         /*
983          * The SVC protocol is currently driven by the SVC, so the SVC device
984          * is added from the connection request handler when enough
985          * information has been received.
986          */
987         ret = gb_connection_enable(svc->connection);
988         if (ret)
989                 return ret;
990
991         return 0;
992 }
993
994 static void gb_svc_remove_interfaces(struct gb_svc *svc)
995 {
996         struct gb_interface *intf, *tmp;
997
998         list_for_each_entry_safe(intf, tmp, &svc->hd->interfaces, links) {
999                 gb_interface_disable(intf);
1000                 gb_interface_remove(intf);
1001         }
1002 }
1003
1004 void gb_svc_del(struct gb_svc *svc)
1005 {
1006         gb_connection_disable(svc->connection);
1007
1008         /*
1009          * The SVC device and input device may have been registered
1010          * from the request handler.
1011          */
1012         if (device_is_registered(&svc->dev)) {
1013                 gb_svc_watchdog_destroy(svc);
1014                 input_unregister_device(svc->input);
1015                 device_del(&svc->dev);
1016         }
1017
1018         flush_workqueue(svc->wq);
1019
1020         gb_svc_remove_interfaces(svc);
1021 }
1022
1023 void gb_svc_put(struct gb_svc *svc)
1024 {
1025         put_device(&svc->dev);
1026 }