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_eject(struct gb_svc *svc, u8 intf_id)
124 struct gb_svc_intf_eject_request request;
127 request.intf_id = intf_id;
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.
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);
138 dev_err(&svc->dev, "failed to eject interface %u\n", intf_id);
145 int gb_svc_dme_peer_get(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
148 struct gb_svc_dme_peer_get_request request;
149 struct gb_svc_dme_peer_get_response response;
153 request.intf_id = intf_id;
154 request.attr = cpu_to_le16(attr);
155 request.selector = cpu_to_le16(selector);
157 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_GET,
158 &request, sizeof(request),
159 &response, sizeof(response));
161 dev_err(&svc->dev, "failed to get DME attribute (%u 0x%04x %u): %d\n",
162 intf_id, attr, selector, ret);
166 result = le16_to_cpu(response.result_code);
168 dev_err(&svc->dev, "UniPro error while getting DME attribute (%u 0x%04x %u): %u\n",
169 intf_id, attr, selector, result);
174 *value = le32_to_cpu(response.attr_value);
178 EXPORT_SYMBOL_GPL(gb_svc_dme_peer_get);
180 int gb_svc_dme_peer_set(struct gb_svc *svc, u8 intf_id, u16 attr, u16 selector,
183 struct gb_svc_dme_peer_set_request request;
184 struct gb_svc_dme_peer_set_response response;
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);
193 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_DME_PEER_SET,
194 &request, sizeof(request),
195 &response, sizeof(response));
197 dev_err(&svc->dev, "failed to set DME attribute (%u 0x%04x %u %u): %d\n",
198 intf_id, attr, selector, value, ret);
202 result = le16_to_cpu(response.result_code);
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);
211 EXPORT_SYMBOL_GPL(gb_svc_dme_peer_set);
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.
218 * FIXME: This is module-hardware dependent and needs to be extended for every
219 * type of module we want to support.
221 static int gb_svc_read_and_clear_module_boot_status(struct gb_interface *intf)
223 struct gb_host_device *hd = intf->hd;
230 * Check if the module is ES2 or ES3, and choose attr number
232 * FIXME: Remove ES2 support from the kernel entirely.
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;
238 attr = DME_ATTR_ES3_INIT_STATUS;
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);
248 * A nonzero boot status indicates the module has finished
252 dev_err(&intf->dev, "Module not ready yet\n");
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.
262 if (intf->ddbl1_manufacturer_id == ES2_DDBL1_MFR_ID &&
263 intf->ddbl1_product_id == ES2_DDBL1_PROD_ID)
266 init_status = value >> 24;
268 if (init_status == DME_DIS_UNIPRO_BOOT_STARTED ||
269 init_status == DME_DIS_FALLBACK_UNIPRO_BOOT_STARTED)
270 intf->boot_over_unipro = true;
272 return gb_svc_dme_peer_set(hd->svc, intf->interface_id, attr,
273 DME_ATTR_SELECTOR_INDEX, 0);
276 int gb_svc_connection_create(struct gb_svc *svc,
277 u8 intf1_id, u16 cport1_id,
278 u8 intf2_id, u16 cport2_id,
281 struct gb_svc_conn_create_request request;
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;
290 return gb_operation_sync(svc->connection, GB_SVC_TYPE_CONN_CREATE,
291 &request, sizeof(request), NULL, 0);
293 EXPORT_SYMBOL_GPL(gb_svc_connection_create);
295 void gb_svc_connection_destroy(struct gb_svc *svc, u8 intf1_id, u16 cport1_id,
296 u8 intf2_id, u16 cport2_id)
298 struct gb_svc_conn_destroy_request request;
299 struct gb_connection *connection = svc->connection;
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);
307 ret = gb_operation_sync(connection, GB_SVC_TYPE_CONN_DESTROY,
308 &request, sizeof(request), NULL, 0);
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);
314 EXPORT_SYMBOL_GPL(gb_svc_connection_destroy);
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)
320 struct gb_svc_route_create_request request;
322 request.intf1_id = intf1_id;
323 request.dev1_id = dev1_id;
324 request.intf2_id = intf2_id;
325 request.dev2_id = dev2_id;
327 return gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_CREATE,
328 &request, sizeof(request), NULL, 0);
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)
334 struct gb_svc_route_destroy_request request;
337 request.intf1_id = intf1_id;
338 request.intf2_id = intf2_id;
340 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_ROUTE_DESTROY,
341 &request, sizeof(request), NULL, 0);
343 dev_err(&svc->dev, "failed to destroy route (%u %u): %d\n",
344 intf1_id, intf2_id, ret);
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)
353 struct gb_svc_intf_set_pwrm_request request;
354 struct gb_svc_intf_set_pwrm_response response;
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);
368 ret = gb_operation_sync(svc->connection, GB_SVC_TYPE_INTF_SET_PWRM,
369 &request, sizeof(request),
370 &response, sizeof(response));
374 return le16_to_cpu(response.result_code);
376 EXPORT_SYMBOL_GPL(gb_svc_intf_set_power_mode);
378 int gb_svc_ping(struct gb_svc *svc)
380 return gb_operation_sync_timeout(svc->connection, GB_SVC_TYPE_PING,
382 GB_OPERATION_TIMEOUT_DEFAULT * 2);
384 EXPORT_SYMBOL_GPL(gb_svc_ping);
386 static int gb_svc_version_request(struct gb_operation *op)
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;
393 if (op->request->payload_size < sizeof(*request)) {
394 dev_err(&svc->dev, "short version request (%zu < %zu)\n",
395 op->request->payload_size,
400 request = op->request->payload;
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);
408 svc->protocol_major = request->major;
409 svc->protocol_minor = request->minor;
411 if (!gb_operation_response_alloc(op, sizeof(*response), GFP_KERNEL))
414 response = op->response->payload;
415 response->major = svc->protocol_major;
416 response->minor = svc->protocol_minor;
421 static int gb_svc_hello(struct gb_operation *op)
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;
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));
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;
439 ret = device_add(&svc->dev);
441 dev_err(&svc->dev, "failed to register svc device: %d\n", ret);
445 ret = input_register_device(svc->input);
447 dev_err(&svc->dev, "failed to register input: %d\n", ret);
448 device_del(&svc->dev);
452 ret = gb_svc_watchdog_create(svc);
454 dev_err(&svc->dev, "failed to create watchdog: %d\n", ret);
455 input_unregister_device(svc->input);
456 device_del(&svc->dev);
463 static int gb_svc_interface_route_create(struct gb_svc *svc,
464 struct gb_interface *intf)
466 u8 intf_id = intf->interface_id;
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
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?
478 ret = ida_simple_get(&svc->device_id_map,
479 GB_DEVICE_ID_MODULES_START, 0, GFP_KERNEL);
481 dev_err(&svc->dev, "failed to allocate device id for interface %u: %d\n",
487 ret = gb_svc_intf_device_id(svc, intf_id, device_id);
489 dev_err(&svc->dev, "failed to set device id %u for interface %u: %d\n",
490 device_id, intf_id, ret);
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,
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;
503 intf->device_id = device_id;
509 * XXX Should we tell SVC that this id doesn't belong to interface
513 ida_simple_remove(&svc->device_id_map, device_id);
518 static void gb_svc_interface_route_destroy(struct gb_svc *svc,
519 struct gb_interface *intf)
521 if (intf->device_id == GB_DEVICE_ID_BAD)
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;
529 static void gb_svc_intf_remove(struct gb_svc *svc, struct gb_interface *intf)
531 intf->disconnected = true;
533 gb_interface_disable(intf);
534 gb_svc_interface_route_destroy(svc, intf);
535 gb_interface_remove(intf);
538 static void gb_svc_process_intf_hotplug(struct gb_operation *operation)
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;
550 /* The request message size has already been verified. */
551 request = operation->request->payload;
552 intf_id = request->intf_id;
554 dev_dbg(&svc->dev, "%s - id = %u\n", __func__, intf_id);
556 intf = gb_interface_find(hd, intf_id);
559 * For ES2, we need to maintain the same vendor/product ids we
560 * got from bootrom, otherwise userspace can't distinguish
563 vendor_id = intf->vendor_id;
564 product_id = intf->product_id;
567 * We have received a hotplug request for an interface that
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
574 * - Or the firmware on the module crashed and sent hotplug
575 * request again to the SVC, which got propagated to AP.
577 * Remove the interface and add it again, and let user know
578 * about this with a print message.
580 dev_info(&svc->dev, "removing interface %u to add it again\n",
582 gb_svc_intf_remove(svc, intf);
585 intf = gb_interface_create(hd, intf_id);
587 dev_err(&svc->dev, "failed to create interface %u\n",
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);
599 * Use VID/PID specified at hotplug if:
600 * - Bridge ASIC chip isn't ES2
601 * - Received non-zero Vendor/Product ids
603 * Otherwise, use the ids we received from bootrom.
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;
612 ret = gb_svc_read_and_clear_module_boot_status(intf);
614 dev_err(&svc->dev, "failed to clear boot status of interface %u: %d\n",
616 goto out_interface_add;
619 ret = gb_svc_interface_route_create(svc, intf);
621 goto out_interface_add;
623 ret = gb_interface_enable(intf);
625 dev_err(&svc->dev, "failed to enable interface %u: %d\n",
627 goto out_interface_add;
631 gb_interface_add(intf);
634 static void gb_svc_process_intf_hot_unplug(struct gb_operation *operation)
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;
642 /* The request message size has already been verified. */
643 request = operation->request->payload;
644 intf_id = request->intf_id;
646 dev_dbg(&svc->dev, "%s - id = %u\n", __func__, intf_id);
648 intf = gb_interface_find(hd, intf_id);
650 dev_warn(&svc->dev, "could not find hot-unplug interface %u\n",
655 gb_svc_intf_remove(svc, intf);
658 static void gb_svc_process_deferred_request(struct work_struct *work)
660 struct gb_svc_deferred_request *dr;
661 struct gb_operation *operation;
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;
671 case GB_SVC_TYPE_INTF_HOTPLUG:
672 gb_svc_process_intf_hotplug(operation);
674 case GB_SVC_TYPE_INTF_HOT_UNPLUG:
675 gb_svc_process_intf_hot_unplug(operation);
678 dev_err(&svc->dev, "bad deferred request type: 0x%02x\n", type);
681 gb_operation_put(operation);
685 static int gb_svc_queue_deferred_request(struct gb_operation *operation)
687 struct gb_svc *svc = gb_connection_get_data(operation->connection);
688 struct gb_svc_deferred_request *dr;
690 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
694 gb_operation_get(operation);
696 dr->operation = operation;
697 INIT_WORK(&dr->work, gb_svc_process_deferred_request);
699 queue_work(svc->wq, &dr->work);
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.
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.
713 static int gb_svc_intf_hotplug_recv(struct gb_operation *op)
715 struct gb_svc *svc = gb_connection_get_data(op->connection);
716 struct gb_svc_intf_hotplug_request *request;
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));
724 request = op->request->payload;
726 dev_dbg(&svc->dev, "%s - id = %u\n", __func__, request->intf_id);
728 return gb_svc_queue_deferred_request(op);
731 static int gb_svc_intf_hot_unplug_recv(struct gb_operation *op)
733 struct gb_svc *svc = gb_connection_get_data(op->connection);
734 struct gb_svc_intf_hot_unplug_request *request;
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));
742 request = op->request->payload;
744 dev_dbg(&svc->dev, "%s - id = %u\n", __func__, request->intf_id);
746 return gb_svc_queue_deferred_request(op);
749 static int gb_svc_intf_reset_recv(struct gb_operation *op)
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;
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));
761 reset = request->payload;
763 intf_id = reset->intf_id;
765 /* FIXME Reset the interface here */
770 static int gb_svc_key_code_map(struct gb_svc *svc, u16 key_code, u16 *code)
774 *code = SVC_KEY_ARA_BUTTON;
777 dev_warn(&svc->dev, "unknown keycode received: %u\n", key_code);
784 static int gb_svc_key_event_recv(struct gb_operation *op)
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;
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));
799 key = request->payload;
801 ret = gb_svc_key_code_map(svc, le16_to_cpu(key->key_code), &code);
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);
811 input_report_key(svc->input, code, (event == GB_SVC_KEY_PRESSED));
812 input_sync(svc->input);
817 static int gb_svc_request_handler(struct gb_operation *op)
819 struct gb_connection *connection = op->connection;
820 struct gb_svc *svc = gb_connection_get_data(connection);
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:
829 * - Any other request, but the earlier two.
831 * Incoming requests are guaranteed to be serialized and so we don't
832 * need to protect 'state' for any races.
835 case GB_REQUEST_TYPE_PROTOCOL_VERSION:
836 if (svc->state != GB_SVC_STATE_RESET)
839 case GB_SVC_TYPE_SVC_HELLO:
840 if (svc->state != GB_SVC_STATE_PROTOCOL_VERSION)
844 if (svc->state != GB_SVC_STATE_SVC_HELLO)
850 dev_warn(&svc->dev, "unexpected request 0x%02x received (state %u)\n",
856 case GB_REQUEST_TYPE_PROTOCOL_VERSION:
857 ret = gb_svc_version_request(op);
859 svc->state = GB_SVC_STATE_PROTOCOL_VERSION;
861 case GB_SVC_TYPE_SVC_HELLO:
862 ret = gb_svc_hello(op);
864 svc->state = GB_SVC_STATE_SVC_HELLO;
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);
875 dev_warn(&svc->dev, "unsupported request 0x%02x\n", type);
880 static struct input_dev *gb_svc_input_create(struct gb_svc *svc)
882 struct input_dev *input_dev;
884 input_dev = input_allocate_device();
886 return ERR_PTR(-ENOMEM);
888 input_dev->name = dev_name(&svc->dev);
889 svc->input_phys = kasprintf(GFP_KERNEL, "greybus-%s/input0",
891 if (!svc->input_phys)
894 input_dev->phys = svc->input_phys;
895 input_dev->dev.parent = &svc->dev;
897 input_set_drvdata(input_dev, svc);
899 input_set_capability(input_dev, EV_KEY, SVC_KEY_ARA_BUTTON);
904 input_free_device(svc->input);
905 return ERR_PTR(-ENOMEM);
908 static void gb_svc_release(struct device *dev)
910 struct gb_svc *svc = to_gb_svc(dev);
913 gb_connection_destroy(svc->connection);
914 ida_destroy(&svc->device_id_map);
915 destroy_workqueue(svc->wq);
916 kfree(svc->input_phys);
920 struct device_type greybus_svc_type = {
921 .name = "greybus_svc",
922 .release = gb_svc_release,
925 struct gb_svc *gb_svc_create(struct gb_host_device *hd)
929 svc = kzalloc(sizeof(*svc), GFP_KERNEL);
933 svc->wq = alloc_workqueue("%s:svc", WQ_UNBOUND, 1, dev_name(&hd->dev));
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);
946 dev_set_name(&svc->dev, "%d-svc", hd->bus_id);
948 ida_init(&svc->device_id_map);
949 svc->state = GB_SVC_STATE_RESET;
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));
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));
967 gb_connection_set_data(svc->connection, svc);
972 input_free_device(svc->input);
974 put_device(&svc->dev);
978 int gb_svc_add(struct gb_svc *svc)
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.
987 ret = gb_connection_enable(svc->connection);
994 static void gb_svc_remove_interfaces(struct gb_svc *svc)
996 struct gb_interface *intf, *tmp;
998 list_for_each_entry_safe(intf, tmp, &svc->hd->interfaces, links) {
999 gb_interface_disable(intf);
1000 gb_interface_remove(intf);
1004 void gb_svc_del(struct gb_svc *svc)
1006 gb_connection_disable(svc->connection);
1009 * The SVC device and input device may have been registered
1010 * from the request handler.
1012 if (device_is_registered(&svc->dev)) {
1013 gb_svc_watchdog_destroy(svc);
1014 input_unregister_device(svc->input);
1015 device_del(&svc->dev);
1018 flush_workqueue(svc->wq);
1020 gb_svc_remove_interfaces(svc);
1023 void gb_svc_put(struct gb_svc *svc)
1025 put_device(&svc->dev);