1 // SPDX-License-Identifier: GPL-2.0
3 * Greybus interface code
5 * Copyright 2014 Google Inc.
6 * Copyright 2014 Linaro Ltd.
9 #include <linux/delay.h>
10 #include <linux/greybus.h>
12 #include "greybus_trace.h"
14 #define GB_INTERFACE_MODE_SWITCH_TIMEOUT 2000
16 #define GB_INTERFACE_DEVICE_ID_BAD 0xff
18 #define GB_INTERFACE_AUTOSUSPEND_MS 3000
20 /* Time required for interface to enter standby before disabling REFCLK */
21 #define GB_INTERFACE_SUSPEND_HIBERNATE_DELAY_MS 20
23 /* Don't-care selector index */
24 #define DME_SELECTOR_INDEX_NULL 0
27 /* FIXME: remove ES2 support and DME_T_TST_SRC_INCREMENT */
28 #define DME_T_TST_SRC_INCREMENT 0x4083
30 #define DME_DDBL1_MANUFACTURERID 0x5003
31 #define DME_DDBL1_PRODUCTID 0x5004
33 #define DME_TOSHIBA_GMP_VID 0x6000
34 #define DME_TOSHIBA_GMP_PID 0x6001
35 #define DME_TOSHIBA_GMP_SN0 0x6002
36 #define DME_TOSHIBA_GMP_SN1 0x6003
37 #define DME_TOSHIBA_GMP_INIT_STATUS 0x6101
39 /* DDBL1 Manufacturer and Product ids */
40 #define TOSHIBA_DMID 0x0126
41 #define TOSHIBA_ES2_BRIDGE_DPID 0x1000
42 #define TOSHIBA_ES3_APBRIDGE_DPID 0x1001
43 #define TOSHIBA_ES3_GBPHY_DPID 0x1002
45 static int gb_interface_hibernate_link(struct gb_interface *intf);
46 static int gb_interface_refclk_set(struct gb_interface *intf, bool enable);
48 static int gb_interface_dme_attr_get(struct gb_interface *intf,
51 return gb_svc_dme_peer_get(intf->hd->svc, intf->interface_id,
52 attr, DME_SELECTOR_INDEX_NULL, val);
55 static int gb_interface_read_ara_dme(struct gb_interface *intf)
61 * Unless this is a Toshiba bridge, bail out until we have defined
62 * standard GMP attributes.
64 if (intf->ddbl1_manufacturer_id != TOSHIBA_DMID) {
65 dev_err(&intf->dev, "unknown manufacturer %08x\n",
66 intf->ddbl1_manufacturer_id);
70 ret = gb_interface_dme_attr_get(intf, DME_TOSHIBA_GMP_VID,
75 ret = gb_interface_dme_attr_get(intf, DME_TOSHIBA_GMP_PID,
80 ret = gb_interface_dme_attr_get(intf, DME_TOSHIBA_GMP_SN0, &sn0);
84 ret = gb_interface_dme_attr_get(intf, DME_TOSHIBA_GMP_SN1, &sn1);
88 intf->serial_number = (u64)sn1 << 32 | sn0;
93 static int gb_interface_read_dme(struct gb_interface *intf)
97 /* DME attributes have already been read */
101 ret = gb_interface_dme_attr_get(intf, DME_DDBL1_MANUFACTURERID,
102 &intf->ddbl1_manufacturer_id);
106 ret = gb_interface_dme_attr_get(intf, DME_DDBL1_PRODUCTID,
107 &intf->ddbl1_product_id);
111 if (intf->ddbl1_manufacturer_id == TOSHIBA_DMID &&
112 intf->ddbl1_product_id == TOSHIBA_ES2_BRIDGE_DPID) {
113 intf->quirks |= GB_INTERFACE_QUIRK_NO_GMP_IDS;
114 intf->quirks |= GB_INTERFACE_QUIRK_NO_INIT_STATUS;
117 ret = gb_interface_read_ara_dme(intf);
121 intf->dme_read = true;
126 static int gb_interface_route_create(struct gb_interface *intf)
128 struct gb_svc *svc = intf->hd->svc;
129 u8 intf_id = intf->interface_id;
133 /* Allocate an interface device id. */
134 ret = ida_simple_get(&svc->device_id_map,
135 GB_SVC_DEVICE_ID_MIN, GB_SVC_DEVICE_ID_MAX + 1,
138 dev_err(&intf->dev, "failed to allocate device id: %d\n", ret);
143 ret = gb_svc_intf_device_id(svc, intf_id, device_id);
145 dev_err(&intf->dev, "failed to set device id %u: %d\n",
150 /* FIXME: Hard-coded AP device id. */
151 ret = gb_svc_route_create(svc, svc->ap_intf_id, GB_SVC_DEVICE_ID_AP,
154 dev_err(&intf->dev, "failed to create route: %d\n", ret);
155 goto err_svc_id_free;
158 intf->device_id = device_id;
164 * XXX Should we tell SVC that this id doesn't belong to interface
168 ida_simple_remove(&svc->device_id_map, device_id);
173 static void gb_interface_route_destroy(struct gb_interface *intf)
175 struct gb_svc *svc = intf->hd->svc;
177 if (intf->device_id == GB_INTERFACE_DEVICE_ID_BAD)
180 gb_svc_route_destroy(svc, svc->ap_intf_id, intf->interface_id);
181 ida_simple_remove(&svc->device_id_map, intf->device_id);
182 intf->device_id = GB_INTERFACE_DEVICE_ID_BAD;
185 /* Locking: Caller holds the interface mutex. */
186 static int gb_interface_legacy_mode_switch(struct gb_interface *intf)
190 dev_info(&intf->dev, "legacy mode switch detected\n");
192 /* Mark as disconnected to prevent I/O during disable. */
193 intf->disconnected = true;
194 gb_interface_disable(intf);
195 intf->disconnected = false;
197 ret = gb_interface_enable(intf);
199 dev_err(&intf->dev, "failed to re-enable interface: %d\n", ret);
200 gb_interface_deactivate(intf);
206 void gb_interface_mailbox_event(struct gb_interface *intf, u16 result,
209 mutex_lock(&intf->mutex);
213 "mailbox event with UniPro error: 0x%04x\n",
218 if (mailbox != GB_SVC_INTF_MAILBOX_GREYBUS) {
220 "mailbox event with unexpected value: 0x%08x\n",
225 if (intf->quirks & GB_INTERFACE_QUIRK_LEGACY_MODE_SWITCH) {
226 gb_interface_legacy_mode_switch(intf);
230 if (!intf->mode_switch) {
231 dev_warn(&intf->dev, "unexpected mailbox event: 0x%08x\n",
236 dev_info(&intf->dev, "mode switch detected\n");
238 complete(&intf->mode_switch_completion);
241 mutex_unlock(&intf->mutex);
246 gb_interface_disable(intf);
247 gb_interface_deactivate(intf);
248 mutex_unlock(&intf->mutex);
251 static void gb_interface_mode_switch_work(struct work_struct *work)
253 struct gb_interface *intf;
254 struct gb_control *control;
255 unsigned long timeout;
258 intf = container_of(work, struct gb_interface, mode_switch_work);
260 mutex_lock(&intf->mutex);
261 /* Make sure interface is still enabled. */
262 if (!intf->enabled) {
263 dev_dbg(&intf->dev, "mode switch aborted\n");
264 intf->mode_switch = false;
265 mutex_unlock(&intf->mutex);
266 goto out_interface_put;
270 * Prepare the control device for mode switch and make sure to get an
271 * extra reference before it goes away during interface disable.
273 control = gb_control_get(intf->control);
274 gb_control_mode_switch_prepare(control);
275 gb_interface_disable(intf);
276 mutex_unlock(&intf->mutex);
278 timeout = msecs_to_jiffies(GB_INTERFACE_MODE_SWITCH_TIMEOUT);
279 ret = wait_for_completion_interruptible_timeout(
280 &intf->mode_switch_completion, timeout);
282 /* Finalise control-connection mode switch. */
283 gb_control_mode_switch_complete(control);
284 gb_control_put(control);
287 dev_err(&intf->dev, "mode switch interrupted\n");
289 } else if (ret == 0) {
290 dev_err(&intf->dev, "mode switch timed out\n");
294 /* Re-enable (re-enumerate) interface if still active. */
295 mutex_lock(&intf->mutex);
296 intf->mode_switch = false;
298 ret = gb_interface_enable(intf);
300 dev_err(&intf->dev, "failed to re-enable interface: %d\n",
302 gb_interface_deactivate(intf);
305 mutex_unlock(&intf->mutex);
308 gb_interface_put(intf);
313 mutex_lock(&intf->mutex);
314 intf->mode_switch = false;
315 gb_interface_deactivate(intf);
316 mutex_unlock(&intf->mutex);
318 gb_interface_put(intf);
321 int gb_interface_request_mode_switch(struct gb_interface *intf)
325 mutex_lock(&intf->mutex);
326 if (intf->mode_switch) {
331 intf->mode_switch = true;
332 reinit_completion(&intf->mode_switch_completion);
335 * Get a reference to the interface device, which will be put once the
336 * mode switch is complete.
338 get_device(&intf->dev);
340 if (!queue_work(system_long_wq, &intf->mode_switch_work)) {
341 put_device(&intf->dev);
347 mutex_unlock(&intf->mutex);
351 EXPORT_SYMBOL_GPL(gb_interface_request_mode_switch);
354 * T_TstSrcIncrement is written by the module on ES2 as a stand-in for the
355 * init-status attribute DME_TOSHIBA_INIT_STATUS. The AP needs to read and
356 * clear it after reading a non-zero value from it.
358 * FIXME: This is module-hardware dependent and needs to be extended for every
359 * type of module we want to support.
361 static int gb_interface_read_and_clear_init_status(struct gb_interface *intf)
363 struct gb_host_device *hd = intf->hd;
364 unsigned long bootrom_quirks;
365 unsigned long s2l_quirks;
372 * ES2 bridges use T_TstSrcIncrement for the init status.
374 * FIXME: Remove ES2 support
376 if (intf->quirks & GB_INTERFACE_QUIRK_NO_INIT_STATUS)
377 attr = DME_T_TST_SRC_INCREMENT;
379 attr = DME_TOSHIBA_GMP_INIT_STATUS;
381 ret = gb_svc_dme_peer_get(hd->svc, intf->interface_id, attr,
382 DME_SELECTOR_INDEX_NULL, &value);
387 * A nonzero init status indicates the module has finished
391 dev_err(&intf->dev, "invalid init status\n");
396 * Extract the init status.
398 * For ES2: We need to check lowest 8 bits of 'value'.
399 * For ES3: We need to check highest 8 bits out of 32 of 'value'.
401 * FIXME: Remove ES2 support
403 if (intf->quirks & GB_INTERFACE_QUIRK_NO_INIT_STATUS)
404 init_status = value & 0xff;
406 init_status = value >> 24;
409 * Check if the interface is executing the quirky ES3 bootrom that,
410 * for example, requires E2EFC, CSD and CSV to be disabled.
412 bootrom_quirks = GB_INTERFACE_QUIRK_NO_CPORT_FEATURES |
413 GB_INTERFACE_QUIRK_FORCED_DISABLE |
414 GB_INTERFACE_QUIRK_LEGACY_MODE_SWITCH |
415 GB_INTERFACE_QUIRK_NO_BUNDLE_ACTIVATE;
417 s2l_quirks = GB_INTERFACE_QUIRK_NO_PM;
419 switch (init_status) {
420 case GB_INIT_BOOTROM_UNIPRO_BOOT_STARTED:
421 case GB_INIT_BOOTROM_FALLBACK_UNIPRO_BOOT_STARTED:
422 intf->quirks |= bootrom_quirks;
424 case GB_INIT_S2_LOADER_BOOT_STARTED:
425 /* S2 Loader doesn't support runtime PM */
426 intf->quirks &= ~bootrom_quirks;
427 intf->quirks |= s2l_quirks;
430 intf->quirks &= ~bootrom_quirks;
431 intf->quirks &= ~s2l_quirks;
434 /* Clear the init status. */
435 return gb_svc_dme_peer_set(hd->svc, intf->interface_id, attr,
436 DME_SELECTOR_INDEX_NULL, 0);
439 /* interface sysfs attributes */
440 #define gb_interface_attr(field, type) \
441 static ssize_t field##_show(struct device *dev, \
442 struct device_attribute *attr, \
445 struct gb_interface *intf = to_gb_interface(dev); \
446 return scnprintf(buf, PAGE_SIZE, type"\n", intf->field); \
448 static DEVICE_ATTR_RO(field)
450 gb_interface_attr(ddbl1_manufacturer_id, "0x%08x");
451 gb_interface_attr(ddbl1_product_id, "0x%08x");
452 gb_interface_attr(interface_id, "%u");
453 gb_interface_attr(vendor_id, "0x%08x");
454 gb_interface_attr(product_id, "0x%08x");
455 gb_interface_attr(serial_number, "0x%016llx");
457 static ssize_t voltage_now_show(struct device *dev,
458 struct device_attribute *attr, char *buf)
460 struct gb_interface *intf = to_gb_interface(dev);
464 ret = gb_svc_pwrmon_intf_sample_get(intf->hd->svc, intf->interface_id,
465 GB_SVC_PWRMON_TYPE_VOL,
468 dev_err(&intf->dev, "failed to get voltage sample (%d)\n", ret);
472 return sprintf(buf, "%u\n", measurement);
474 static DEVICE_ATTR_RO(voltage_now);
476 static ssize_t current_now_show(struct device *dev,
477 struct device_attribute *attr, char *buf)
479 struct gb_interface *intf = to_gb_interface(dev);
483 ret = gb_svc_pwrmon_intf_sample_get(intf->hd->svc, intf->interface_id,
484 GB_SVC_PWRMON_TYPE_CURR,
487 dev_err(&intf->dev, "failed to get current sample (%d)\n", ret);
491 return sprintf(buf, "%u\n", measurement);
493 static DEVICE_ATTR_RO(current_now);
495 static ssize_t power_now_show(struct device *dev,
496 struct device_attribute *attr, char *buf)
498 struct gb_interface *intf = to_gb_interface(dev);
502 ret = gb_svc_pwrmon_intf_sample_get(intf->hd->svc, intf->interface_id,
503 GB_SVC_PWRMON_TYPE_PWR,
506 dev_err(&intf->dev, "failed to get power sample (%d)\n", ret);
510 return sprintf(buf, "%u\n", measurement);
512 static DEVICE_ATTR_RO(power_now);
514 static ssize_t power_state_show(struct device *dev,
515 struct device_attribute *attr, char *buf)
517 struct gb_interface *intf = to_gb_interface(dev);
520 return scnprintf(buf, PAGE_SIZE, "on\n");
522 return scnprintf(buf, PAGE_SIZE, "off\n");
525 static ssize_t power_state_store(struct device *dev,
526 struct device_attribute *attr, const char *buf,
529 struct gb_interface *intf = to_gb_interface(dev);
533 if (kstrtobool(buf, &activate))
536 mutex_lock(&intf->mutex);
538 if (activate == intf->active)
542 ret = gb_interface_activate(intf);
545 "failed to activate interface: %d\n", ret);
549 ret = gb_interface_enable(intf);
552 "failed to enable interface: %d\n", ret);
553 gb_interface_deactivate(intf);
557 gb_interface_disable(intf);
558 gb_interface_deactivate(intf);
562 mutex_unlock(&intf->mutex);
569 static DEVICE_ATTR_RW(power_state);
571 static const char *gb_interface_type_string(struct gb_interface *intf)
573 static const char * const types[] = {
574 [GB_INTERFACE_TYPE_INVALID] = "invalid",
575 [GB_INTERFACE_TYPE_UNKNOWN] = "unknown",
576 [GB_INTERFACE_TYPE_DUMMY] = "dummy",
577 [GB_INTERFACE_TYPE_UNIPRO] = "unipro",
578 [GB_INTERFACE_TYPE_GREYBUS] = "greybus",
581 return types[intf->type];
584 static ssize_t interface_type_show(struct device *dev,
585 struct device_attribute *attr, char *buf)
587 struct gb_interface *intf = to_gb_interface(dev);
589 return sprintf(buf, "%s\n", gb_interface_type_string(intf));
591 static DEVICE_ATTR_RO(interface_type);
593 static struct attribute *interface_unipro_attrs[] = {
594 &dev_attr_ddbl1_manufacturer_id.attr,
595 &dev_attr_ddbl1_product_id.attr,
599 static struct attribute *interface_greybus_attrs[] = {
600 &dev_attr_vendor_id.attr,
601 &dev_attr_product_id.attr,
602 &dev_attr_serial_number.attr,
606 static struct attribute *interface_power_attrs[] = {
607 &dev_attr_voltage_now.attr,
608 &dev_attr_current_now.attr,
609 &dev_attr_power_now.attr,
610 &dev_attr_power_state.attr,
614 static struct attribute *interface_common_attrs[] = {
615 &dev_attr_interface_id.attr,
616 &dev_attr_interface_type.attr,
620 static umode_t interface_unipro_is_visible(struct kobject *kobj,
621 struct attribute *attr, int n)
623 struct device *dev = kobj_to_dev(kobj);
624 struct gb_interface *intf = to_gb_interface(dev);
626 switch (intf->type) {
627 case GB_INTERFACE_TYPE_UNIPRO:
628 case GB_INTERFACE_TYPE_GREYBUS:
635 static umode_t interface_greybus_is_visible(struct kobject *kobj,
636 struct attribute *attr, int n)
638 struct device *dev = kobj_to_dev(kobj);
639 struct gb_interface *intf = to_gb_interface(dev);
641 switch (intf->type) {
642 case GB_INTERFACE_TYPE_GREYBUS:
649 static umode_t interface_power_is_visible(struct kobject *kobj,
650 struct attribute *attr, int n)
652 struct device *dev = kobj_to_dev(kobj);
653 struct gb_interface *intf = to_gb_interface(dev);
655 switch (intf->type) {
656 case GB_INTERFACE_TYPE_UNIPRO:
657 case GB_INTERFACE_TYPE_GREYBUS:
664 static const struct attribute_group interface_unipro_group = {
665 .is_visible = interface_unipro_is_visible,
666 .attrs = interface_unipro_attrs,
669 static const struct attribute_group interface_greybus_group = {
670 .is_visible = interface_greybus_is_visible,
671 .attrs = interface_greybus_attrs,
674 static const struct attribute_group interface_power_group = {
675 .is_visible = interface_power_is_visible,
676 .attrs = interface_power_attrs,
679 static const struct attribute_group interface_common_group = {
680 .attrs = interface_common_attrs,
683 static const struct attribute_group *interface_groups[] = {
684 &interface_unipro_group,
685 &interface_greybus_group,
686 &interface_power_group,
687 &interface_common_group,
691 static void gb_interface_release(struct device *dev)
693 struct gb_interface *intf = to_gb_interface(dev);
695 trace_gb_interface_release(intf);
701 static int gb_interface_suspend(struct device *dev)
703 struct gb_interface *intf = to_gb_interface(dev);
706 ret = gb_control_interface_suspend_prepare(intf->control);
710 ret = gb_control_suspend(intf->control);
712 goto err_hibernate_abort;
714 ret = gb_interface_hibernate_link(intf);
718 /* Delay to allow interface to enter standby before disabling refclk */
719 msleep(GB_INTERFACE_SUSPEND_HIBERNATE_DELAY_MS);
721 ret = gb_interface_refclk_set(intf, false);
728 gb_control_interface_hibernate_abort(intf->control);
733 static int gb_interface_resume(struct device *dev)
735 struct gb_interface *intf = to_gb_interface(dev);
736 struct gb_svc *svc = intf->hd->svc;
739 ret = gb_interface_refclk_set(intf, true);
743 ret = gb_svc_intf_resume(svc, intf->interface_id);
747 ret = gb_control_resume(intf->control);
754 static int gb_interface_runtime_idle(struct device *dev)
756 pm_runtime_mark_last_busy(dev);
757 pm_request_autosuspend(dev);
763 static const struct dev_pm_ops gb_interface_pm_ops = {
764 SET_RUNTIME_PM_OPS(gb_interface_suspend, gb_interface_resume,
765 gb_interface_runtime_idle)
768 struct device_type greybus_interface_type = {
769 .name = "greybus_interface",
770 .release = gb_interface_release,
771 .pm = &gb_interface_pm_ops,
775 * A Greybus module represents a user-replaceable component on a GMP
776 * phone. An interface is the physical connection on that module. A
777 * module may have more than one interface.
779 * Create a gb_interface structure to represent a discovered interface.
780 * The position of interface within the Endo is encoded in "interface_id"
783 * Returns a pointer to the new interfce or a null pointer if a
784 * failure occurs due to memory exhaustion.
786 struct gb_interface *gb_interface_create(struct gb_module *module,
789 struct gb_host_device *hd = module->hd;
790 struct gb_interface *intf;
792 intf = kzalloc(sizeof(*intf), GFP_KERNEL);
796 intf->hd = hd; /* XXX refcount? */
797 intf->module = module;
798 intf->interface_id = interface_id;
799 INIT_LIST_HEAD(&intf->bundles);
800 INIT_LIST_HEAD(&intf->manifest_descs);
801 mutex_init(&intf->mutex);
802 INIT_WORK(&intf->mode_switch_work, gb_interface_mode_switch_work);
803 init_completion(&intf->mode_switch_completion);
805 /* Invalid device id to start with */
806 intf->device_id = GB_INTERFACE_DEVICE_ID_BAD;
808 intf->dev.parent = &module->dev;
809 intf->dev.bus = &greybus_bus_type;
810 intf->dev.type = &greybus_interface_type;
811 intf->dev.groups = interface_groups;
812 intf->dev.dma_mask = module->dev.dma_mask;
813 device_initialize(&intf->dev);
814 dev_set_name(&intf->dev, "%s.%u", dev_name(&module->dev),
817 pm_runtime_set_autosuspend_delay(&intf->dev,
818 GB_INTERFACE_AUTOSUSPEND_MS);
820 trace_gb_interface_create(intf);
825 static int gb_interface_vsys_set(struct gb_interface *intf, bool enable)
827 struct gb_svc *svc = intf->hd->svc;
830 dev_dbg(&intf->dev, "%s - %d\n", __func__, enable);
832 ret = gb_svc_intf_vsys_set(svc, intf->interface_id, enable);
834 dev_err(&intf->dev, "failed to set v_sys: %d\n", ret);
841 static int gb_interface_refclk_set(struct gb_interface *intf, bool enable)
843 struct gb_svc *svc = intf->hd->svc;
846 dev_dbg(&intf->dev, "%s - %d\n", __func__, enable);
848 ret = gb_svc_intf_refclk_set(svc, intf->interface_id, enable);
850 dev_err(&intf->dev, "failed to set refclk: %d\n", ret);
857 static int gb_interface_unipro_set(struct gb_interface *intf, bool enable)
859 struct gb_svc *svc = intf->hd->svc;
862 dev_dbg(&intf->dev, "%s - %d\n", __func__, enable);
864 ret = gb_svc_intf_unipro_set(svc, intf->interface_id, enable);
866 dev_err(&intf->dev, "failed to set UniPro: %d\n", ret);
873 static int gb_interface_activate_operation(struct gb_interface *intf,
874 enum gb_interface_type *intf_type)
876 struct gb_svc *svc = intf->hd->svc;
880 dev_dbg(&intf->dev, "%s\n", __func__);
882 ret = gb_svc_intf_activate(svc, intf->interface_id, &type);
884 dev_err(&intf->dev, "failed to activate: %d\n", ret);
889 case GB_SVC_INTF_TYPE_DUMMY:
890 *intf_type = GB_INTERFACE_TYPE_DUMMY;
891 /* FIXME: handle as an error for now */
893 case GB_SVC_INTF_TYPE_UNIPRO:
894 *intf_type = GB_INTERFACE_TYPE_UNIPRO;
895 dev_err(&intf->dev, "interface type UniPro not supported\n");
896 /* FIXME: handle as an error for now */
898 case GB_SVC_INTF_TYPE_GREYBUS:
899 *intf_type = GB_INTERFACE_TYPE_GREYBUS;
902 dev_err(&intf->dev, "unknown interface type: %u\n", type);
903 *intf_type = GB_INTERFACE_TYPE_UNKNOWN;
910 static int gb_interface_hibernate_link(struct gb_interface *intf)
912 struct gb_svc *svc = intf->hd->svc;
914 return gb_svc_intf_set_power_mode_hibernate(svc, intf->interface_id);
917 static int _gb_interface_activate(struct gb_interface *intf,
918 enum gb_interface_type *type)
922 *type = GB_INTERFACE_TYPE_UNKNOWN;
924 if (intf->ejected || intf->removed)
927 ret = gb_interface_vsys_set(intf, true);
931 ret = gb_interface_refclk_set(intf, true);
933 goto err_vsys_disable;
935 ret = gb_interface_unipro_set(intf, true);
937 goto err_refclk_disable;
939 ret = gb_interface_activate_operation(intf, type);
942 case GB_INTERFACE_TYPE_UNIPRO:
943 case GB_INTERFACE_TYPE_GREYBUS:
944 goto err_hibernate_link;
946 goto err_unipro_disable;
950 ret = gb_interface_read_dme(intf);
952 goto err_hibernate_link;
954 ret = gb_interface_route_create(intf);
956 goto err_hibernate_link;
960 trace_gb_interface_activate(intf);
965 gb_interface_hibernate_link(intf);
967 gb_interface_unipro_set(intf, false);
969 gb_interface_refclk_set(intf, false);
971 gb_interface_vsys_set(intf, false);
977 * At present, we assume a UniPro-only module to be a Greybus module that
978 * failed to send its mailbox poke. There is some reason to believe that this
979 * is because of a bug in the ES3 bootrom.
981 * FIXME: Check if this is a Toshiba bridge before retrying?
983 static int _gb_interface_activate_es3_hack(struct gb_interface *intf,
984 enum gb_interface_type *type)
990 ret = _gb_interface_activate(intf, type);
991 if (ret == -ENODEV && *type == GB_INTERFACE_TYPE_UNIPRO)
1001 * Activate an interface.
1003 * Locking: Caller holds the interface mutex.
1005 int gb_interface_activate(struct gb_interface *intf)
1007 enum gb_interface_type type;
1010 switch (intf->type) {
1011 case GB_INTERFACE_TYPE_INVALID:
1012 case GB_INTERFACE_TYPE_GREYBUS:
1013 ret = _gb_interface_activate_es3_hack(intf, &type);
1016 ret = _gb_interface_activate(intf, &type);
1019 /* Make sure type is detected correctly during reactivation. */
1020 if (intf->type != GB_INTERFACE_TYPE_INVALID) {
1021 if (type != intf->type) {
1022 dev_err(&intf->dev, "failed to detect interface type\n");
1025 gb_interface_deactivate(intf);
1037 * Deactivate an interface.
1039 * Locking: Caller holds the interface mutex.
1041 void gb_interface_deactivate(struct gb_interface *intf)
1046 trace_gb_interface_deactivate(intf);
1048 /* Abort any ongoing mode switch. */
1049 if (intf->mode_switch)
1050 complete(&intf->mode_switch_completion);
1052 gb_interface_route_destroy(intf);
1053 gb_interface_hibernate_link(intf);
1054 gb_interface_unipro_set(intf, false);
1055 gb_interface_refclk_set(intf, false);
1056 gb_interface_vsys_set(intf, false);
1058 intf->active = false;
1062 * Enable an interface by enabling its control connection, fetching the
1063 * manifest and other information over it, and finally registering its child
1066 * Locking: Caller holds the interface mutex.
1068 int gb_interface_enable(struct gb_interface *intf)
1070 struct gb_control *control;
1071 struct gb_bundle *bundle, *tmp;
1075 ret = gb_interface_read_and_clear_init_status(intf);
1077 dev_err(&intf->dev, "failed to clear init status: %d\n", ret);
1081 /* Establish control connection */
1082 control = gb_control_create(intf);
1083 if (IS_ERR(control)) {
1084 dev_err(&intf->dev, "failed to create control device: %ld\n",
1086 return PTR_ERR(control);
1088 intf->control = control;
1090 ret = gb_control_enable(intf->control);
1092 goto err_put_control;
1094 /* Get manifest size using control protocol on CPort */
1095 size = gb_control_get_manifest_size_operation(intf);
1097 dev_err(&intf->dev, "failed to get manifest size: %d\n", size);
1104 goto err_disable_control;
1107 manifest = kmalloc(size, GFP_KERNEL);
1110 goto err_disable_control;
1113 /* Get manifest using control protocol on CPort */
1114 ret = gb_control_get_manifest_operation(intf, manifest, size);
1116 dev_err(&intf->dev, "failed to get manifest: %d\n", ret);
1117 goto err_free_manifest;
1121 * Parse the manifest and build up our data structures representing
1124 if (!gb_manifest_parse(intf, manifest, size)) {
1125 dev_err(&intf->dev, "failed to parse manifest\n");
1127 goto err_destroy_bundles;
1130 ret = gb_control_get_bundle_versions(intf->control);
1132 goto err_destroy_bundles;
1134 /* Register the control device and any bundles */
1135 ret = gb_control_add(intf->control);
1137 goto err_destroy_bundles;
1139 pm_runtime_use_autosuspend(&intf->dev);
1140 pm_runtime_get_noresume(&intf->dev);
1141 pm_runtime_set_active(&intf->dev);
1142 pm_runtime_enable(&intf->dev);
1144 list_for_each_entry_safe_reverse(bundle, tmp, &intf->bundles, links) {
1145 ret = gb_bundle_add(bundle);
1147 gb_bundle_destroy(bundle);
1154 intf->enabled = true;
1156 pm_runtime_put(&intf->dev);
1158 trace_gb_interface_enable(intf);
1162 err_destroy_bundles:
1163 list_for_each_entry_safe(bundle, tmp, &intf->bundles, links)
1164 gb_bundle_destroy(bundle);
1167 err_disable_control:
1168 gb_control_disable(intf->control);
1170 gb_control_put(intf->control);
1171 intf->control = NULL;
1177 * Disable an interface and destroy its bundles.
1179 * Locking: Caller holds the interface mutex.
1181 void gb_interface_disable(struct gb_interface *intf)
1183 struct gb_bundle *bundle;
1184 struct gb_bundle *next;
1189 trace_gb_interface_disable(intf);
1191 pm_runtime_get_sync(&intf->dev);
1193 /* Set disconnected flag to avoid I/O during connection tear down. */
1194 if (intf->quirks & GB_INTERFACE_QUIRK_FORCED_DISABLE)
1195 intf->disconnected = true;
1197 list_for_each_entry_safe(bundle, next, &intf->bundles, links)
1198 gb_bundle_destroy(bundle);
1200 if (!intf->mode_switch && !intf->disconnected)
1201 gb_control_interface_deactivate_prepare(intf->control);
1203 gb_control_del(intf->control);
1204 gb_control_disable(intf->control);
1205 gb_control_put(intf->control);
1206 intf->control = NULL;
1208 intf->enabled = false;
1210 pm_runtime_disable(&intf->dev);
1211 pm_runtime_set_suspended(&intf->dev);
1212 pm_runtime_dont_use_autosuspend(&intf->dev);
1213 pm_runtime_put_noidle(&intf->dev);
1216 /* Register an interface. */
1217 int gb_interface_add(struct gb_interface *intf)
1221 ret = device_add(&intf->dev);
1223 dev_err(&intf->dev, "failed to register interface: %d\n", ret);
1227 trace_gb_interface_add(intf);
1229 dev_info(&intf->dev, "Interface added (%s)\n",
1230 gb_interface_type_string(intf));
1232 switch (intf->type) {
1233 case GB_INTERFACE_TYPE_GREYBUS:
1234 dev_info(&intf->dev, "GMP VID=0x%08x, PID=0x%08x\n",
1235 intf->vendor_id, intf->product_id);
1237 case GB_INTERFACE_TYPE_UNIPRO:
1238 dev_info(&intf->dev, "DDBL1 Manufacturer=0x%08x, Product=0x%08x\n",
1239 intf->ddbl1_manufacturer_id,
1240 intf->ddbl1_product_id);
1249 /* Deregister an interface. */
1250 void gb_interface_del(struct gb_interface *intf)
1252 if (device_is_registered(&intf->dev)) {
1253 trace_gb_interface_del(intf);
1255 device_del(&intf->dev);
1256 dev_info(&intf->dev, "Interface removed\n");
1260 void gb_interface_put(struct gb_interface *intf)
1262 put_device(&intf->dev);