4 * Copyright 2015 Google Inc.
5 * Copyright 2015 Linaro Ltd.
7 * Released under the GPLv2 only.
10 #include <linux/input.h>
11 #include <linux/workqueue.h>
15 #define SVC_KEY_ARA_BUTTON KEY_A
17 #define SVC_INTF_EJECT_TIMEOUT 9000
19 struct gb_svc_deferred_request {
20 struct work_struct work;
21 struct gb_operation *operation;
25 static ssize_t endo_id_show(struct device *dev,
26 struct device_attribute *attr, char *buf)
28 struct gb_svc *svc = to_gb_svc(dev);
30 return sprintf(buf, "0x%04x\n", svc->endo_id);
32 static DEVICE_ATTR_RO(endo_id);
34 static ssize_t ap_intf_id_show(struct device *dev,
35 struct device_attribute *attr, char *buf)
37 struct gb_svc *svc = to_gb_svc(dev);
39 return sprintf(buf, "%u\n", svc->ap_intf_id);
41 static DEVICE_ATTR_RO(ap_intf_id);
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,
53 struct gb_svc *svc = to_gb_svc(dev);
54 unsigned short intf_id;
57 ret = kstrtou16(buf, 10, &intf_id);
61 dev_warn(dev, "Forcibly trying to eject interface %d\n", intf_id);
63 ret = gb_svc_intf_eject(svc, intf_id);
69 static DEVICE_ATTR_WO(intf_eject);
71 static ssize_t watchdog_show(struct device *dev, struct device_attribute *attr,
74 struct gb_svc *svc = to_gb_svc(dev);
76 return sprintf(buf, "%s\n",
77 gb_svc_watchdog_enabled(svc) ? "enabled" : "disabled");
80 static ssize_t watchdog_store(struct device *dev,
81 struct device_attribute *attr, const char *buf,
84 struct gb_svc *svc = to_gb_svc(dev);
88 retval = strtobool(buf, &user_request);
93 retval = gb_svc_watchdog_enable(svc);
95 retval = gb_svc_watchdog_disable(svc);
100 static DEVICE_ATTR_RW(watchdog);
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,
109 ATTRIBUTE_GROUPS(svc);
111 static int gb_svc_intf_device_id(struct gb_svc *svc, u8 intf_id, u8 device_id)
113 struct gb_svc_intf_device_id_request request;
115 request.intf_id = intf_id;
116 request.device_id = device_id;
118 return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_DEVICE_ID,
119 &request, sizeof(request), NULL, 0);
122 int gb_svc_intf_reset(struct gb_svc *svc, u8 intf_id)
124 struct gb_svc_intf_reset_request request;
126 request.intf_id = intf_id;
128 return gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_RESET,
129 &request, sizeof(request), NULL, 0);
131 EXPORT_SYMBOL_GPL(gb_svc_intf_reset);
133 int gb_svc_intf_eject(struct gb_svc *svc, u8 intf_id)
135 struct gb_svc_intf_eject_request request;
138 request.intf_id = intf_id;
141 * The pulse width for module release in svc is long so we need to
142 * increase the timeout so the operation will not return to soon.
144 ret = gb_operation_sync_timeout(svc->connection,
145 GB_SVC_TYPE_INTF_EJECT, &request,
146 sizeof(request), NULL, 0,
147 SVC_INTF_EJECT_TIMEOUT);
149 dev_err(&svc->dev, "failed to eject interface %u\n", intf_id);
156 int gb_svc_dme_peer_get(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
159 struct gb_svc_dme_peer_get_request request;
160 struct gb_svc_dme_peer_get_response response;
164 request.intf_id = intf_id;
165 request.attr = cpu_to_le16(attr);
166 request.selector = cpu_to_le16(selector);
168 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_GET,
169 &request, sizeof(request),
170 &response, sizeof(response));
172 dev_err(&svc->dev, "failed to get DME attribute (%u 0x%04x %u): %d\n",
173 intf_id, attr, selector, ret);
177 result = le16_to_cpu(response.result_code);
179 dev_err(&svc->dev, "UniPro error while getting DME attribute (%u 0x%04x %u): %u\n",
180 intf_id, attr, selector, result);
185 *value = le32_to_cpu(response.attr_value);
189 EXPORT_SYMBOL_GPL(gb_svc_dme_peer_get);
191 int gb_svc_dme_peer_set(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
194 struct gb_svc_dme_peer_set_request request;
195 struct gb_svc_dme_peer_set_response response;
199 request.intf_id = intf_id;
200 request.attr = cpu_to_le16(attr);
201 request.selector = cpu_to_le16(selector);
202 request.value = cpu_to_le32(value);
204 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_SET,
205 &request, sizeof(request),
206 &response, sizeof(response));
208 dev_err(&svc->dev, "failed to set DME attribute (%u 0x%04x %u %u): %d\n",
209 intf_id, attr, selector, value, ret);
213 result = le16_to_cpu(response.result_code);
215 dev_err(&svc->dev, "UniPro error while setting DME attribute (%u 0x%04x %u %u): %u\n",
216 intf_id, attr, selector, value, result);
222 EXPORT_SYMBOL_GPL(gb_svc_dme_peer_set);
225 * T_TstSrcIncrement is written by the module on ES2 as a stand-in for boot
226 * status attribute ES3_INIT_STATUS. AP needs to read and clear it, after
227 * reading a non-zero value from it.
229 * FIXME: This is module-hardware dependent and needs to be extended for every
230 * type of module we want to support.
232 static int gb_svc_read_and_clear_module_boot_status(struct gb_interface *intf)
234 struct gb_host_device *hd = intf->hd;
241 * Check if the module is ES2 or ES3, and choose attr number
243 * FIXME: Remove ES2 support from the kernel entirely.
245 if (intf->ddbl1_manufacturer_id == ES2_DDBL1_MFR_ID &&
246 intf->ddbl1_product_id == ES2_DDBL1_PROD_ID)
247 attr = DME_ATTR_T_TST_SRC_INCREMENT;
249 attr = DME_ATTR_ES3_INIT_STATUS;
251 /* Read and clear boot status in ES3_INIT_STATUS */
252 ret = gb_svc_dme_peer_get(hd->svc, intf->interface_id, attr,
253 DME_ATTR_SELECTOR_INDEX, &value);
259 * A nonzero boot status indicates the module has finished
263 dev_err(&intf->dev, "Module not ready yet\n");
268 * Check if the module needs to boot from UniPro.
269 * For ES2: We need to check lowest 8 bits of 'value'.
270 * For ES3: We need to check highest 8 bits out of 32 of 'value'.
271 * FIXME: Remove ES2 support from the kernel entirely.
273 if (intf->ddbl1_manufacturer_id == ES2_DDBL1_MFR_ID &&
274 intf->ddbl1_product_id == ES2_DDBL1_PROD_ID)
277 init_status = value >> 24;
279 if (init_status == DME_DIS_UNIPRO_BOOT_STARTED ||
280 init_status == DME_DIS_FALLBACK_UNIPRO_BOOT_STARTED)
281 intf->boot_over_unipro = true;
283 return gb_svc_dme_peer_set(hd->svc, intf->interface_id, attr,
284 DME_ATTR_SELECTOR_INDEX, 0);
287 int gb_svc_connection_create(struct gb_svc *svc,
288 u8 intf1_id, u16 cport1_id,
289 u8 intf2_id, u16 cport2_id,
292 struct gb_svc_conn_create_request request;
294 request.intf1_id = intf1_id;
295 request.cport1_id = cpu_to_le16(cport1_id);
296 request.intf2_id = intf2_id;
297 request.cport2_id = cpu_to_le16(cport2_id);
298 request.tc = 0; /* TC0 */
299 request.flags = cport_flags;
301 return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_CREATE,
302 &request, sizeof(request), NULL, 0);
304 EXPORT_SYMBOL_GPL(gb_svc_connection_create);
306 void gb_svc_connection_destroy(struct gb_svc *svc, u8 intf1_id, u16 cport1_id,
307 u8 intf2_id, u16 cport2_id)
309 struct gb_svc_conn_destroy_request request;
310 struct gb_connection *connection = svc->connection;
313 request.intf1_id = intf1_id;
314 request.cport1_id = cpu_to_le16(cport1_id);
315 request.intf2_id = intf2_id;
316 request.cport2_id = cpu_to_le16(cport2_id);
318 ret = gb_operation_sync(connection, GB_SVC_TYPE_CONN_DESTROY,
319 &request, sizeof(request), NULL, 0);
321 dev_err(&svc->dev, "failed to destroy connection (%u:%u %u:%u): %d\n",
322 intf1_id, cport1_id, intf2_id, cport2_id, ret);
325 EXPORT_SYMBOL_GPL(gb_svc_connection_destroy);
327 /* Creates bi-directional routes between the devices */
328 static int gb_svc_route_create(struct gb_svc *svc, u8 intf1_id, u8 dev1_id,
329 u8 intf2_id, u8 dev2_id)
331 struct gb_svc_route_create_request request;
333 request.intf1_id = intf1_id;
334 request.dev1_id = dev1_id;
335 request.intf2_id = intf2_id;
336 request.dev2_id = dev2_id;
338 return gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_CREATE,
339 &request, sizeof(request), NULL, 0);
342 /* Destroys bi-directional routes between the devices */
343 static void gb_svc_route_destroy(struct gb_svc *svc, u8 intf1_id, u8 intf2_id)
345 struct gb_svc_route_destroy_request request;
348 request.intf1_id = intf1_id;
349 request.intf2_id = intf2_id;
351 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_DESTROY,
352 &request, sizeof(request), NULL, 0);
354 dev_err(&svc->dev, "failed to destroy route (%u %u): %d\n",
355 intf1_id, intf2_id, ret);
359 int gb_svc_intf_set_power_mode(struct gb_svc *svc, u8 intf_id, u8 hs_series,
360 u8 tx_mode, u8 tx_gear, u8 tx_nlanes,
361 u8 rx_mode, u8 rx_gear, u8 rx_nlanes,
362 u8 flags, u32 quirks)
364 struct gb_svc_intf_set_pwrm_request request;
365 struct gb_svc_intf_set_pwrm_response response;
368 request.intf_id = intf_id;
369 request.hs_series = hs_series;
370 request.tx_mode = tx_mode;
371 request.tx_gear = tx_gear;
372 request.tx_nlanes = tx_nlanes;
373 request.rx_mode = rx_mode;
374 request.rx_gear = rx_gear;
375 request.rx_nlanes = rx_nlanes;
376 request.flags = flags;
377 request.quirks = cpu_to_le32(quirks);
379 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_SET_PWRM,
380 &request, sizeof(request),
381 &response, sizeof(response));
385 return le16_to_cpu(response.result_code);
387 EXPORT_SYMBOL_GPL(gb_svc_intf_set_power_mode);
389 int gb_svc_ping(struct gb_svc *svc)
391 return gb_operation_sync_timeout(svc->connection, GB_SVC_TYPE_PING,
393 GB_OPERATION_TIMEOUT_DEFAULT * 2);
395 EXPORT_SYMBOL_GPL(gb_svc_ping);
397 static int gb_svc_version_request(struct gb_operation *op)
399 struct gb_connection *connection = op->connection;
400 struct gb_svc *svc = gb_connection_get_data(connection);
401 struct gb_protocol_version_request *request;
402 struct gb_protocol_version_response *response;
404 if (op->request->payload_size < sizeof(*request)) {
405 dev_err(&svc->dev, "short version request (%zu < %zu)\n",
406 op->request->payload_size,
411 request = op->request->payload;
413 if (request->major > GB_SVC_VERSION_MAJOR) {
414 dev_warn(&svc->dev, "unsupported major version (%u > %u)\n",
415 request->major, GB_SVC_VERSION_MAJOR);
419 svc->protocol_major = request->major;
420 svc->protocol_minor = request->minor;
422 if (!gb_operation_response_alloc(op, sizeof(*response), GFP_KERNEL))
425 response = op->response->payload;
426 response->major = svc->protocol_major;
427 response->minor = svc->protocol_minor;
432 static int gb_svc_hello(struct gb_operation *op)
434 struct gb_connection *connection = op->connection;
435 struct gb_svc *svc = gb_connection_get_data(connection);
436 struct gb_svc_hello_request *hello_request;
439 if (op->request->payload_size < sizeof(*hello_request)) {
440 dev_warn(&svc->dev, "short hello request (%zu < %zu)\n",
441 op->request->payload_size,
442 sizeof(*hello_request));
446 hello_request = op->request->payload;
447 svc->endo_id = le16_to_cpu(hello_request->endo_id);
448 svc->ap_intf_id = hello_request->interface_id;
450 ret = device_add(&svc->dev);
452 dev_err(&svc->dev, "failed to register svc device: %d\n", ret);
456 ret = input_register_device(svc->input);
458 dev_err(&svc->dev, "failed to register input: %d\n", ret);
459 device_del(&svc->dev);
463 ret = gb_svc_watchdog_create(svc);
465 dev_err(&svc->dev, "failed to create watchdog: %d\n", ret);
466 input_unregister_device(svc->input);
467 device_del(&svc->dev);
474 static int gb_svc_interface_route_create(struct gb_svc *svc,
475 struct gb_interface *intf)
477 u8 intf_id = intf->interface_id;
482 * Create a device id for the interface:
483 * - device id 0 (GB_DEVICE_ID_SVC) belongs to the SVC
484 * - device id 1 (GB_DEVICE_ID_AP) belongs to the AP
486 * XXX Do we need to allocate device ID for SVC or the AP here? And what
487 * XXX about an AP with multiple interface blocks?
489 ret = ida_simple_get(&svc->device_id_map,
490 GB_DEVICE_ID_MODULES_START, 0, GFP_KERNEL);
492 dev_err(&svc->dev, "failed to allocate device id for interface %u: %d\n",
498 ret = gb_svc_intf_device_id(svc, intf_id, device_id);
500 dev_err(&svc->dev, "failed to set device id %u for interface %u: %d\n",
501 device_id, intf_id, ret);
505 /* Create a two-way route between the AP and the new interface. */
506 ret = gb_svc_route_create(svc, svc->ap_intf_id, GB_DEVICE_ID_AP,
509 dev_err(&svc->dev, "failed to create route to interface %u (device id %u): %d\n",
510 intf_id, device_id, ret);
511 goto err_svc_id_free;
514 intf->device_id = device_id;
520 * XXX Should we tell SVC that this id doesn't belong to interface
524 ida_simple_remove(&svc->device_id_map, device_id);
529 static void gb_svc_interface_route_destroy(struct gb_svc *svc,
530 struct gb_interface *intf)
532 if (intf->device_id == GB_DEVICE_ID_BAD)
535 gb_svc_route_destroy(svc, svc->ap_intf_id, intf->interface_id);
536 ida_simple_remove(&svc->device_id_map, intf->device_id);
537 intf->device_id = GB_DEVICE_ID_BAD;
540 static void gb_svc_intf_remove(struct gb_svc *svc, struct gb_interface *intf)
542 intf->disconnected = true;
544 gb_interface_disable(intf);
545 gb_svc_interface_route_destroy(svc, intf);
546 gb_interface_remove(intf);
549 static void gb_svc_process_intf_hotplug(struct gb_operation *operation)
551 struct gb_svc_intf_hotplug_request *request;
552 struct gb_connection *connection = operation->connection;
553 struct gb_svc *svc = gb_connection_get_data(connection);
554 struct gb_host_device *hd = connection->hd;
555 struct gb_interface *intf;
561 /* The request message size has already been verified. */
562 request = operation->request->payload;
563 intf_id = request->intf_id;
565 dev_dbg(&svc->dev, "%s - id = %u\n", __func__, intf_id);
567 intf = gb_interface_find(hd, intf_id);
570 * For ES2, we need to maintain the same vendor/product ids we
571 * got from bootrom, otherwise userspace can't distinguish
574 vendor_id = intf->vendor_id;
575 product_id = intf->product_id;
578 * We have received a hotplug request for an interface that
581 * This can happen in cases like:
582 * - bootrom loading the firmware image and booting into that,
583 * which only generates a hotplug event. i.e. no hot-unplug
585 * - Or the firmware on the module crashed and sent hotplug
586 * request again to the SVC, which got propagated to AP.
588 * Remove the interface and add it again, and let user know
589 * about this with a print message.
591 dev_info(&svc->dev, "removing interface %u to add it again\n",
593 gb_svc_intf_remove(svc, intf);
596 intf = gb_interface_create(hd, intf_id);
598 dev_err(&svc->dev, "failed to create interface %u\n",
603 intf->ddbl1_manufacturer_id = le32_to_cpu(request->data.ddbl1_mfr_id);
604 intf->ddbl1_product_id = le32_to_cpu(request->data.ddbl1_prod_id);
605 intf->vendor_id = le32_to_cpu(request->data.ara_vend_id);
606 intf->product_id = le32_to_cpu(request->data.ara_prod_id);
607 intf->serial_number = le64_to_cpu(request->data.serial_number);
610 * Use VID/PID specified at hotplug if:
611 * - Bridge ASIC chip isn't ES2
612 * - Received non-zero Vendor/Product ids
614 * Otherwise, use the ids we received from bootrom.
616 if (intf->ddbl1_manufacturer_id == ES2_DDBL1_MFR_ID &&
617 intf->ddbl1_product_id == ES2_DDBL1_PROD_ID &&
618 intf->vendor_id == 0 && intf->product_id == 0) {
619 intf->vendor_id = vendor_id;
620 intf->product_id = product_id;
623 ret = gb_svc_read_and_clear_module_boot_status(intf);
625 dev_err(&svc->dev, "failed to clear boot status of interface %u: %d\n",
627 goto out_interface_add;
630 ret = gb_svc_interface_route_create(svc, intf);
632 goto out_interface_add;
634 ret = gb_interface_enable(intf);
636 dev_err(&svc->dev, "failed to enable interface %u: %d\n",
638 goto out_interface_add;
642 gb_interface_add(intf);
645 static void gb_svc_process_intf_hot_unplug(struct gb_operation *operation)
647 struct gb_svc *svc = gb_connection_get_data(operation->connection);
648 struct gb_svc_intf_hot_unplug_request *request;
649 struct gb_host_device *hd = operation->connection->hd;
650 struct gb_interface *intf;
653 /* The request message size has already been verified. */
654 request = operation->request->payload;
655 intf_id = request->intf_id;
657 dev_dbg(&svc->dev, "%s - id = %u\n", __func__, intf_id);
659 intf = gb_interface_find(hd, intf_id);
661 dev_warn(&svc->dev, "could not find hot-unplug interface %u\n",
666 gb_svc_intf_remove(svc, intf);
669 static void gb_svc_process_deferred_request(struct work_struct *work)
671 struct gb_svc_deferred_request *dr;
672 struct gb_operation *operation;
676 dr = container_of(work, struct gb_svc_deferred_request, work);
677 operation = dr->operation;
678 svc = gb_connection_get_data(operation->connection);
679 type = operation->request->header->type;
682 case GB_SVC_TYPE_INTF_HOTPLUG:
683 gb_svc_process_intf_hotplug(operation);
685 case GB_SVC_TYPE_INTF_HOT_UNPLUG:
686 gb_svc_process_intf_hot_unplug(operation);
689 dev_err(&svc->dev, "bad deferred request type: 0x%02x\n", type);
692 gb_operation_put(operation);
696 static int gb_svc_queue_deferred_request(struct gb_operation *operation)
698 struct gb_svc *svc = gb_connection_get_data(operation->connection);
699 struct gb_svc_deferred_request *dr;
701 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
705 gb_operation_get(operation);
707 dr->operation = operation;
708 INIT_WORK(&dr->work, gb_svc_process_deferred_request);
710 queue_work(svc->wq, &dr->work);
716 * Bringing up a module can be time consuming, as that may require lots of
717 * initialization on the module side. Over that, we may also need to download
718 * the firmware first and flash that on the module.
720 * In order not to make other svc events wait for all this to finish,
721 * handle most of module hotplug stuff outside of the hotplug callback, with
722 * help of a workqueue.
724 static int gb_svc_intf_hotplug_recv(struct gb_operation *op)
726 struct gb_svc *svc = gb_connection_get_data(op->connection);
727 struct gb_svc_intf_hotplug_request *request;
729 if (op->request->payload_size < sizeof(*request)) {
730 dev_warn(&svc->dev, "short hotplug request received (%zu < %zu)\n",
731 op->request->payload_size, sizeof(*request));
735 request = op->request->payload;
737 dev_dbg(&svc->dev, "%s - id = %u\n", __func__, request->intf_id);
739 return gb_svc_queue_deferred_request(op);
742 static int gb_svc_intf_hot_unplug_recv(struct gb_operation *op)
744 struct gb_svc *svc = gb_connection_get_data(op->connection);
745 struct gb_svc_intf_hot_unplug_request *request;
747 if (op->request->payload_size < sizeof(*request)) {
748 dev_warn(&svc->dev, "short hot unplug request received (%zu < %zu)\n",
749 op->request->payload_size, sizeof(*request));
753 request = op->request->payload;
755 dev_dbg(&svc->dev, "%s - id = %u\n", __func__, request->intf_id);
757 return gb_svc_queue_deferred_request(op);
760 static int gb_svc_intf_reset_recv(struct gb_operation *op)
762 struct gb_svc *svc = gb_connection_get_data(op->connection);
763 struct gb_message *request = op->request;
764 struct gb_svc_intf_reset_request *reset;
767 if (request->payload_size < sizeof(*reset)) {
768 dev_warn(&svc->dev, "short reset request received (%zu < %zu)\n",
769 request->payload_size, sizeof(*reset));
772 reset = request->payload;
774 intf_id = reset->intf_id;
776 /* FIXME Reset the interface here */
781 static int gb_svc_key_code_map(struct gb_svc *svc, u16 key_code, u16 *code)
785 *code = SVC_KEY_ARA_BUTTON;
788 dev_warn(&svc->dev, "unknown keycode received: %u\n", key_code);
795 static int gb_svc_key_event_recv(struct gb_operation *op)
797 struct gb_svc *svc = gb_connection_get_data(op->connection);
798 struct gb_message *request = op->request;
799 struct gb_svc_key_event_request *key;
804 if (request->payload_size < sizeof(*key)) {
805 dev_warn(&svc->dev, "short key request received (%zu < %zu)\n",
806 request->payload_size, sizeof(*key));
810 key = request->payload;
812 ret = gb_svc_key_code_map(svc, le16_to_cpu(key->key_code), &code);
816 event = key->key_event;
817 if ((event != GB_SVC_KEY_PRESSED) && (event != GB_SVC_KEY_RELEASED)) {
818 dev_warn(&svc->dev, "unknown key event received: %u\n", event);
822 input_report_key(svc->input, code, (event == GB_SVC_KEY_PRESSED));
823 input_sync(svc->input);
828 static int gb_svc_request_handler(struct gb_operation *op)
830 struct gb_connection *connection = op->connection;
831 struct gb_svc *svc = gb_connection_get_data(connection);
836 * SVC requests need to follow a specific order (at least initially) and
837 * below code takes care of enforcing that. The expected order is:
840 * - Any other request, but the earlier two.
842 * Incoming requests are guaranteed to be serialized and so we don't
843 * need to protect 'state' for any races.
846 case GB_REQUEST_TYPE_PROTOCOL_VERSION:
847 if (svc->state != GB_SVC_STATE_RESET)
850 case GB_SVC_TYPE_SVC_HELLO:
851 if (svc->state != GB_SVC_STATE_PROTOCOL_VERSION)
855 if (svc->state != GB_SVC_STATE_SVC_HELLO)
861 dev_warn(&svc->dev, "unexpected request 0x%02x received (state %u)\n",
867 case GB_REQUEST_TYPE_PROTOCOL_VERSION:
868 ret = gb_svc_version_request(op);
870 svc->state = GB_SVC_STATE_PROTOCOL_VERSION;
872 case GB_SVC_TYPE_SVC_HELLO:
873 ret = gb_svc_hello(op);
875 svc->state = GB_SVC_STATE_SVC_HELLO;
877 case GB_SVC_TYPE_INTF_HOTPLUG:
878 return gb_svc_intf_hotplug_recv(op);
879 case GB_SVC_TYPE_INTF_HOT_UNPLUG:
880 return gb_svc_intf_hot_unplug_recv(op);
881 case GB_SVC_TYPE_INTF_RESET:
882 return gb_svc_intf_reset_recv(op);
883 case GB_SVC_TYPE_KEY_EVENT:
884 return gb_svc_key_event_recv(op);
886 dev_warn(&svc->dev, "unsupported request 0x%02x\n", type);
891 static struct input_dev *gb_svc_input_create(struct gb_svc *svc)
893 struct input_dev *input_dev;
895 input_dev = input_allocate_device();
897 return ERR_PTR(-ENOMEM);
899 input_dev->name = dev_name(&svc->dev);
900 svc->input_phys = kasprintf(GFP_KERNEL, "greybus-%s/input0",
902 if (!svc->input_phys)
905 input_dev->phys = svc->input_phys;
906 input_dev->dev.parent = &svc->dev;
908 input_set_drvdata(input_dev, svc);
910 input_set_capability(input_dev, EV_KEY, SVC_KEY_ARA_BUTTON);
915 input_free_device(svc->input);
916 return ERR_PTR(-ENOMEM);
919 static void gb_svc_release(struct device *dev)
921 struct gb_svc *svc = to_gb_svc(dev);
924 gb_connection_destroy(svc->connection);
925 ida_destroy(&svc->device_id_map);
926 destroy_workqueue(svc->wq);
927 kfree(svc->input_phys);
931 struct device_type greybus_svc_type = {
932 .name = "greybus_svc",
933 .release = gb_svc_release,
936 struct gb_svc *gb_svc_create(struct gb_host_device *hd)
940 svc = kzalloc(sizeof(*svc), GFP_KERNEL);
944 svc->wq = alloc_workqueue("%s:svc", WQ_UNBOUND, 1, dev_name(&hd->dev));
950 svc->dev.parent = &hd->dev;
951 svc->dev.bus = &greybus_bus_type;
952 svc->dev.type = &greybus_svc_type;
953 svc->dev.groups = svc_groups;
954 svc->dev.dma_mask = svc->dev.parent->dma_mask;
955 device_initialize(&svc->dev);
957 dev_set_name(&svc->dev, "%d-svc", hd->bus_id);
959 ida_init(&svc->device_id_map);
960 svc->state = GB_SVC_STATE_RESET;
963 svc->input = gb_svc_input_create(svc);
964 if (IS_ERR(svc->input)) {
965 dev_err(&svc->dev, "failed to create input device: %ld\n",
966 PTR_ERR(svc->input));
970 svc->connection = gb_connection_create_static(hd, GB_SVC_CPORT_ID,
971 gb_svc_request_handler);
972 if (IS_ERR(svc->connection)) {
973 dev_err(&svc->dev, "failed to create connection: %ld\n",
974 PTR_ERR(svc->connection));
978 gb_connection_set_data(svc->connection, svc);
983 input_free_device(svc->input);
985 put_device(&svc->dev);
989 int gb_svc_add(struct gb_svc *svc)
994 * The SVC protocol is currently driven by the SVC, so the SVC device
995 * is added from the connection request handler when enough
996 * information has been received.
998 ret = gb_connection_enable(svc->connection);
1005 static void gb_svc_remove_interfaces(struct gb_svc *svc)
1007 struct gb_interface *intf, *tmp;
1009 list_for_each_entry_safe(intf, tmp, &svc->hd->interfaces, links) {
1010 gb_interface_disable(intf);
1011 gb_interface_remove(intf);
1015 void gb_svc_del(struct gb_svc *svc)
1017 gb_connection_disable(svc->connection);
1020 * The SVC device and input device may have been registered
1021 * from the request handler.
1023 if (device_is_registered(&svc->dev)) {
1024 gb_svc_watchdog_destroy(svc);
1025 input_unregister_device(svc->input);
1026 device_del(&svc->dev);
1029 flush_workqueue(svc->wq);
1031 gb_svc_remove_interfaces(svc);
1034 void gb_svc_put(struct gb_svc *svc)
1036 put_device(&svc->dev);