2 * Greybus interface code
4 * Copyright 2014 Google Inc.
5 * Copyright 2014 Linaro Ltd.
7 * Released under the GPLv2 only.
10 #include <linux/delay.h>
13 #include "greybus_trace.h"
15 #define GB_INTERFACE_MODE_SWITCH_TIMEOUT 2000
17 #define GB_INTERFACE_DEVICE_ID_BAD 0xff
19 #define GB_INTERFACE_AUTOSUSPEND_MS 3000
21 /* Time required for interface to enter standby before disabling REFCLK */
22 #define GB_INTERFACE_SUSPEND_HIBERNATE_DELAY_MS 20
24 /* Don't-care selector index */
25 #define DME_SELECTOR_INDEX_NULL 0
28 /* FIXME: remove ES2 support and DME_T_TST_SRC_INCREMENT */
29 #define DME_T_TST_SRC_INCREMENT 0x4083
31 #define DME_DDBL1_MANUFACTURERID 0x5003
32 #define DME_DDBL1_PRODUCTID 0x5004
34 #define DME_TOSHIBA_GMP_VID 0x6000
35 #define DME_TOSHIBA_GMP_PID 0x6001
36 #define DME_TOSHIBA_GMP_SN0 0x6002
37 #define DME_TOSHIBA_GMP_SN1 0x6003
38 #define DME_TOSHIBA_GMP_INIT_STATUS 0x6101
40 /* DDBL1 Manufacturer and Product ids */
41 #define TOSHIBA_DMID 0x0126
42 #define TOSHIBA_ES2_BRIDGE_DPID 0x1000
43 #define TOSHIBA_ES3_APBRIDGE_DPID 0x1001
44 #define TOSHIBA_ES3_GBPHY_DPID 0x1002
46 static int gb_interface_hibernate_link(struct gb_interface *intf);
47 static int gb_interface_refclk_set(struct gb_interface *intf, bool enable);
49 static int gb_interface_dme_attr_get(struct gb_interface *intf,
52 return gb_svc_dme_peer_get(intf->hd->svc, intf->interface_id,
53 attr, DME_SELECTOR_INDEX_NULL, val);
56 static int gb_interface_read_ara_dme(struct gb_interface *intf)
62 * Unless this is a Toshiba bridge, bail out until we have defined
63 * standard GMP attributes.
65 if (intf->ddbl1_manufacturer_id != TOSHIBA_DMID) {
66 dev_err(&intf->dev, "unknown manufacturer %08x\n",
67 intf->ddbl1_manufacturer_id);
71 ret = gb_interface_dme_attr_get(intf, DME_TOSHIBA_GMP_VID,
76 ret = gb_interface_dme_attr_get(intf, DME_TOSHIBA_GMP_PID,
81 ret = gb_interface_dme_attr_get(intf, DME_TOSHIBA_GMP_SN0, &sn0);
85 ret = gb_interface_dme_attr_get(intf, DME_TOSHIBA_GMP_SN1, &sn1);
89 intf->serial_number = (u64)sn1 << 32 | sn0;
94 static int gb_interface_read_dme(struct gb_interface *intf)
98 /* DME attributes have already been read */
102 ret = gb_interface_dme_attr_get(intf, DME_DDBL1_MANUFACTURERID,
103 &intf->ddbl1_manufacturer_id);
107 ret = gb_interface_dme_attr_get(intf, DME_DDBL1_PRODUCTID,
108 &intf->ddbl1_product_id);
112 if (intf->ddbl1_manufacturer_id == TOSHIBA_DMID &&
113 intf->ddbl1_product_id == TOSHIBA_ES2_BRIDGE_DPID) {
114 intf->quirks |= GB_INTERFACE_QUIRK_NO_GMP_IDS;
115 intf->quirks |= GB_INTERFACE_QUIRK_NO_INIT_STATUS;
118 ret = gb_interface_read_ara_dme(intf);
122 intf->dme_read = true;
127 static int gb_interface_route_create(struct gb_interface *intf)
129 struct gb_svc *svc = intf->hd->svc;
130 u8 intf_id = intf->interface_id;
134 /* Allocate an interface device id. */
135 ret = ida_simple_get(&svc->device_id_map,
136 GB_SVC_DEVICE_ID_MIN, GB_SVC_DEVICE_ID_MAX + 1,
139 dev_err(&intf->dev, "failed to allocate device id: %d\n", ret);
144 ret = gb_svc_intf_device_id(svc, intf_id, device_id);
146 dev_err(&intf->dev, "failed to set device id %u: %d\n",
151 /* FIXME: Hard-coded AP device id. */
152 ret = gb_svc_route_create(svc, svc->ap_intf_id, GB_SVC_DEVICE_ID_AP,
155 dev_err(&intf->dev, "failed to create route: %d\n", ret);
156 goto err_svc_id_free;
159 intf->device_id = device_id;
165 * XXX Should we tell SVC that this id doesn't belong to interface
169 ida_simple_remove(&svc->device_id_map, device_id);
174 static void gb_interface_route_destroy(struct gb_interface *intf)
176 struct gb_svc *svc = intf->hd->svc;
178 if (intf->device_id == GB_INTERFACE_DEVICE_ID_BAD)
181 gb_svc_route_destroy(svc, svc->ap_intf_id, intf->interface_id);
182 ida_simple_remove(&svc->device_id_map, intf->device_id);
183 intf->device_id = GB_INTERFACE_DEVICE_ID_BAD;
186 /* Locking: Caller holds the interface mutex. */
187 static int gb_interface_legacy_mode_switch(struct gb_interface *intf)
191 dev_info(&intf->dev, "legacy mode switch detected\n");
193 /* Mark as disconnected to prevent I/O during disable. */
194 intf->disconnected = true;
195 gb_interface_disable(intf);
196 intf->disconnected = false;
198 ret = gb_interface_enable(intf);
200 dev_err(&intf->dev, "failed to re-enable interface: %d\n", ret);
201 gb_interface_deactivate(intf);
207 void gb_interface_mailbox_event(struct gb_interface *intf, u16 result,
210 mutex_lock(&intf->mutex);
214 "mailbox event with UniPro error: 0x%04x\n",
219 if (mailbox != GB_SVC_INTF_MAILBOX_GREYBUS) {
221 "mailbox event with unexpected value: 0x%08x\n",
226 if (intf->quirks & GB_INTERFACE_QUIRK_LEGACY_MODE_SWITCH) {
227 gb_interface_legacy_mode_switch(intf);
231 if (!intf->mode_switch) {
232 dev_warn(&intf->dev, "unexpected mailbox event: 0x%08x\n",
237 dev_info(&intf->dev, "mode switch detected\n");
239 complete(&intf->mode_switch_completion);
242 mutex_unlock(&intf->mutex);
247 gb_interface_disable(intf);
248 gb_interface_deactivate(intf);
249 mutex_unlock(&intf->mutex);
252 static void gb_interface_mode_switch_work(struct work_struct *work)
254 struct gb_interface *intf;
255 struct gb_control *control;
256 unsigned long timeout;
259 intf = container_of(work, struct gb_interface, mode_switch_work);
261 mutex_lock(&intf->mutex);
262 /* Make sure interface is still enabled. */
263 if (!intf->enabled) {
264 dev_dbg(&intf->dev, "mode switch aborted\n");
265 intf->mode_switch = false;
266 mutex_unlock(&intf->mutex);
267 goto out_interface_put;
271 * Prepare the control device for mode switch and make sure to get an
272 * extra reference before it goes away during interface disable.
274 control = gb_control_get(intf->control);
275 gb_control_mode_switch_prepare(control);
276 gb_interface_disable(intf);
277 mutex_unlock(&intf->mutex);
279 timeout = msecs_to_jiffies(GB_INTERFACE_MODE_SWITCH_TIMEOUT);
280 ret = wait_for_completion_interruptible_timeout(
281 &intf->mode_switch_completion, timeout);
283 /* Finalise control-connection mode switch. */
284 gb_control_mode_switch_complete(control);
285 gb_control_put(control);
288 dev_err(&intf->dev, "mode switch interrupted\n");
290 } else if (ret == 0) {
291 dev_err(&intf->dev, "mode switch timed out\n");
295 /* Re-enable (re-enumerate) interface if still active. */
296 mutex_lock(&intf->mutex);
297 intf->mode_switch = false;
299 ret = gb_interface_enable(intf);
301 dev_err(&intf->dev, "failed to re-enable interface: %d\n",
303 gb_interface_deactivate(intf);
306 mutex_unlock(&intf->mutex);
309 gb_interface_put(intf);
314 mutex_lock(&intf->mutex);
315 intf->mode_switch = false;
316 gb_interface_deactivate(intf);
317 mutex_unlock(&intf->mutex);
319 gb_interface_put(intf);
322 int gb_interface_request_mode_switch(struct gb_interface *intf)
326 mutex_lock(&intf->mutex);
327 if (intf->mode_switch) {
332 intf->mode_switch = true;
333 reinit_completion(&intf->mode_switch_completion);
336 * Get a reference to the interface device, which will be put once the
337 * mode switch is complete.
339 get_device(&intf->dev);
341 if (!queue_work(system_long_wq, &intf->mode_switch_work)) {
342 put_device(&intf->dev);
348 mutex_unlock(&intf->mutex);
352 EXPORT_SYMBOL_GPL(gb_interface_request_mode_switch);
355 * T_TstSrcIncrement is written by the module on ES2 as a stand-in for the
356 * init-status attribute DME_TOSHIBA_INIT_STATUS. The AP needs to read and
357 * clear it after reading a non-zero value from it.
359 * FIXME: This is module-hardware dependent and needs to be extended for every
360 * type of module we want to support.
362 static int gb_interface_read_and_clear_init_status(struct gb_interface *intf)
364 struct gb_host_device *hd = intf->hd;
365 unsigned long bootrom_quirks;
366 unsigned long s2l_quirks;
373 * ES2 bridges use T_TstSrcIncrement for the init status.
375 * FIXME: Remove ES2 support
377 if (intf->quirks & GB_INTERFACE_QUIRK_NO_INIT_STATUS)
378 attr = DME_T_TST_SRC_INCREMENT;
380 attr = DME_TOSHIBA_GMP_INIT_STATUS;
382 ret = gb_svc_dme_peer_get(hd->svc, intf->interface_id, attr,
383 DME_SELECTOR_INDEX_NULL, &value);
388 * A nonzero init status indicates the module has finished
392 dev_err(&intf->dev, "invalid init status\n");
397 * Extract the init status.
399 * For ES2: We need to check lowest 8 bits of 'value'.
400 * For ES3: We need to check highest 8 bits out of 32 of 'value'.
402 * FIXME: Remove ES2 support
404 if (intf->quirks & GB_INTERFACE_QUIRK_NO_INIT_STATUS)
405 init_status = value & 0xff;
407 init_status = value >> 24;
410 * Check if the interface is executing the quirky ES3 bootrom that,
411 * for example, requires E2EFC, CSD and CSV to be disabled.
413 bootrom_quirks = GB_INTERFACE_QUIRK_NO_CPORT_FEATURES |
414 GB_INTERFACE_QUIRK_FORCED_DISABLE |
415 GB_INTERFACE_QUIRK_LEGACY_MODE_SWITCH |
416 GB_INTERFACE_QUIRK_NO_BUNDLE_ACTIVATE;
418 s2l_quirks = GB_INTERFACE_QUIRK_NO_PM;
420 switch (init_status) {
421 case GB_INIT_BOOTROM_UNIPRO_BOOT_STARTED:
422 case GB_INIT_BOOTROM_FALLBACK_UNIPRO_BOOT_STARTED:
423 intf->quirks |= bootrom_quirks;
425 case GB_INIT_S2_LOADER_BOOT_STARTED:
426 /* S2 Loader doesn't support runtime PM */
427 intf->quirks &= ~bootrom_quirks;
428 intf->quirks |= s2l_quirks;
431 intf->quirks &= ~bootrom_quirks;
432 intf->quirks &= ~s2l_quirks;
435 /* Clear the init status. */
436 return gb_svc_dme_peer_set(hd->svc, intf->interface_id, attr,
437 DME_SELECTOR_INDEX_NULL, 0);
440 /* interface sysfs attributes */
441 #define gb_interface_attr(field, type) \
442 static ssize_t field##_show(struct device *dev, \
443 struct device_attribute *attr, \
446 struct gb_interface *intf = to_gb_interface(dev); \
447 return scnprintf(buf, PAGE_SIZE, type"\n", intf->field); \
449 static DEVICE_ATTR_RO(field)
451 gb_interface_attr(ddbl1_manufacturer_id, "0x%08x");
452 gb_interface_attr(ddbl1_product_id, "0x%08x");
453 gb_interface_attr(interface_id, "%u");
454 gb_interface_attr(vendor_id, "0x%08x");
455 gb_interface_attr(product_id, "0x%08x");
456 gb_interface_attr(serial_number, "0x%016llx");
458 static ssize_t voltage_now_show(struct device *dev,
459 struct device_attribute *attr, char *buf)
461 struct gb_interface *intf = to_gb_interface(dev);
465 ret = gb_svc_pwrmon_intf_sample_get(intf->hd->svc, intf->interface_id,
466 GB_SVC_PWRMON_TYPE_VOL,
469 dev_err(&intf->dev, "failed to get voltage sample (%d)\n", ret);
473 return sprintf(buf, "%u\n", measurement);
475 static DEVICE_ATTR_RO(voltage_now);
477 static ssize_t current_now_show(struct device *dev,
478 struct device_attribute *attr, char *buf)
480 struct gb_interface *intf = to_gb_interface(dev);
484 ret = gb_svc_pwrmon_intf_sample_get(intf->hd->svc, intf->interface_id,
485 GB_SVC_PWRMON_TYPE_CURR,
488 dev_err(&intf->dev, "failed to get current sample (%d)\n", ret);
492 return sprintf(buf, "%u\n", measurement);
494 static DEVICE_ATTR_RO(current_now);
496 static ssize_t power_now_show(struct device *dev,
497 struct device_attribute *attr, char *buf)
499 struct gb_interface *intf = to_gb_interface(dev);
503 ret = gb_svc_pwrmon_intf_sample_get(intf->hd->svc, intf->interface_id,
504 GB_SVC_PWRMON_TYPE_PWR,
507 dev_err(&intf->dev, "failed to get power sample (%d)\n", ret);
511 return sprintf(buf, "%u\n", measurement);
513 static DEVICE_ATTR_RO(power_now);
515 static ssize_t power_state_show(struct device *dev,
516 struct device_attribute *attr, char *buf)
518 struct gb_interface *intf = to_gb_interface(dev);
521 return scnprintf(buf, PAGE_SIZE, "on\n");
523 return scnprintf(buf, PAGE_SIZE, "off\n");
526 static ssize_t power_state_store(struct device *dev,
527 struct device_attribute *attr, const char *buf,
530 struct gb_interface *intf = to_gb_interface(dev);
534 if (kstrtobool(buf, &activate))
537 mutex_lock(&intf->mutex);
539 if (activate == intf->active)
543 ret = gb_interface_activate(intf);
546 "failed to activate interface: %d\n", ret);
550 ret = gb_interface_enable(intf);
553 "failed to enable interface: %d\n", ret);
554 gb_interface_deactivate(intf);
558 gb_interface_disable(intf);
559 gb_interface_deactivate(intf);
563 mutex_unlock(&intf->mutex);
570 static DEVICE_ATTR_RW(power_state);
572 static const char *gb_interface_type_string(struct gb_interface *intf)
574 static const char * const types[] = {
575 [GB_INTERFACE_TYPE_INVALID] = "invalid",
576 [GB_INTERFACE_TYPE_UNKNOWN] = "unknown",
577 [GB_INTERFACE_TYPE_DUMMY] = "dummy",
578 [GB_INTERFACE_TYPE_UNIPRO] = "unipro",
579 [GB_INTERFACE_TYPE_GREYBUS] = "greybus",
582 return types[intf->type];
585 static ssize_t interface_type_show(struct device *dev,
586 struct device_attribute *attr, char *buf)
588 struct gb_interface *intf = to_gb_interface(dev);
590 return sprintf(buf, "%s\n", gb_interface_type_string(intf));
592 static DEVICE_ATTR_RO(interface_type);
594 static struct attribute *interface_unipro_attrs[] = {
595 &dev_attr_ddbl1_manufacturer_id.attr,
596 &dev_attr_ddbl1_product_id.attr,
600 static struct attribute *interface_greybus_attrs[] = {
601 &dev_attr_vendor_id.attr,
602 &dev_attr_product_id.attr,
603 &dev_attr_serial_number.attr,
607 static struct attribute *interface_power_attrs[] = {
608 &dev_attr_voltage_now.attr,
609 &dev_attr_current_now.attr,
610 &dev_attr_power_now.attr,
611 &dev_attr_power_state.attr,
615 static struct attribute *interface_common_attrs[] = {
616 &dev_attr_interface_id.attr,
617 &dev_attr_interface_type.attr,
621 static umode_t interface_unipro_is_visible(struct kobject *kobj,
622 struct attribute *attr, int n)
624 struct device *dev = container_of(kobj, struct device, kobj);
625 struct gb_interface *intf = to_gb_interface(dev);
627 switch (intf->type) {
628 case GB_INTERFACE_TYPE_UNIPRO:
629 case GB_INTERFACE_TYPE_GREYBUS:
636 static umode_t interface_greybus_is_visible(struct kobject *kobj,
637 struct attribute *attr, int n)
639 struct device *dev = container_of(kobj, struct device, kobj);
640 struct gb_interface *intf = to_gb_interface(dev);
642 switch (intf->type) {
643 case GB_INTERFACE_TYPE_GREYBUS:
650 static umode_t interface_power_is_visible(struct kobject *kobj,
651 struct attribute *attr, int n)
653 struct device *dev = container_of(kobj, struct device, kobj);
654 struct gb_interface *intf = to_gb_interface(dev);
656 switch (intf->type) {
657 case GB_INTERFACE_TYPE_UNIPRO:
658 case GB_INTERFACE_TYPE_GREYBUS:
665 static const struct attribute_group interface_unipro_group = {
666 .is_visible = interface_unipro_is_visible,
667 .attrs = interface_unipro_attrs,
670 static const struct attribute_group interface_greybus_group = {
671 .is_visible = interface_greybus_is_visible,
672 .attrs = interface_greybus_attrs,
675 static const struct attribute_group interface_power_group = {
676 .is_visible = interface_power_is_visible,
677 .attrs = interface_power_attrs,
680 static const struct attribute_group interface_common_group = {
681 .attrs = interface_common_attrs,
684 static const struct attribute_group *interface_groups[] = {
685 &interface_unipro_group,
686 &interface_greybus_group,
687 &interface_power_group,
688 &interface_common_group,
692 static void gb_interface_release(struct device *dev)
694 struct gb_interface *intf = to_gb_interface(dev);
696 trace_gb_interface_release(intf);
702 static int gb_interface_suspend(struct device *dev)
704 struct gb_interface *intf = to_gb_interface(dev);
707 ret = gb_control_interface_suspend_prepare(intf->control);
711 ret = gb_control_suspend(intf->control);
713 goto err_hibernate_abort;
715 ret = gb_interface_hibernate_link(intf);
719 /* Delay to allow interface to enter standby before disabling refclk */
720 msleep(GB_INTERFACE_SUSPEND_HIBERNATE_DELAY_MS);
722 ret = gb_interface_refclk_set(intf, false);
729 gb_control_interface_hibernate_abort(intf->control);
734 static int gb_interface_resume(struct device *dev)
736 struct gb_interface *intf = to_gb_interface(dev);
737 struct gb_svc *svc = intf->hd->svc;
740 ret = gb_interface_refclk_set(intf, true);
744 ret = gb_svc_intf_resume(svc, intf->interface_id);
748 ret = gb_control_resume(intf->control);
755 static int gb_interface_runtime_idle(struct device *dev)
757 pm_runtime_mark_last_busy(dev);
758 pm_request_autosuspend(dev);
764 static const struct dev_pm_ops gb_interface_pm_ops = {
765 SET_RUNTIME_PM_OPS(gb_interface_suspend, gb_interface_resume,
766 gb_interface_runtime_idle)
769 struct device_type greybus_interface_type = {
770 .name = "greybus_interface",
771 .release = gb_interface_release,
772 .pm = &gb_interface_pm_ops,
776 * A Greybus module represents a user-replaceable component on a GMP
777 * phone. An interface is the physical connection on that module. A
778 * module may have more than one interface.
780 * Create a gb_interface structure to represent a discovered interface.
781 * The position of interface within the Endo is encoded in "interface_id"
784 * Returns a pointer to the new interfce or a null pointer if a
785 * failure occurs due to memory exhaustion.
787 struct gb_interface *gb_interface_create(struct gb_module *module,
790 struct gb_host_device *hd = module->hd;
791 struct gb_interface *intf;
793 intf = kzalloc(sizeof(*intf), GFP_KERNEL);
797 intf->hd = hd; /* XXX refcount? */
798 intf->module = module;
799 intf->interface_id = interface_id;
800 INIT_LIST_HEAD(&intf->bundles);
801 INIT_LIST_HEAD(&intf->manifest_descs);
802 mutex_init(&intf->mutex);
803 INIT_WORK(&intf->mode_switch_work, gb_interface_mode_switch_work);
804 init_completion(&intf->mode_switch_completion);
806 /* Invalid device id to start with */
807 intf->device_id = GB_INTERFACE_DEVICE_ID_BAD;
809 intf->dev.parent = &module->dev;
810 intf->dev.bus = &greybus_bus_type;
811 intf->dev.type = &greybus_interface_type;
812 intf->dev.groups = interface_groups;
813 intf->dev.dma_mask = module->dev.dma_mask;
814 device_initialize(&intf->dev);
815 dev_set_name(&intf->dev, "%s.%u", dev_name(&module->dev),
818 pm_runtime_set_autosuspend_delay(&intf->dev,
819 GB_INTERFACE_AUTOSUSPEND_MS);
821 trace_gb_interface_create(intf);
826 static int gb_interface_vsys_set(struct gb_interface *intf, bool enable)
828 struct gb_svc *svc = intf->hd->svc;
831 dev_dbg(&intf->dev, "%s - %d\n", __func__, enable);
833 ret = gb_svc_intf_vsys_set(svc, intf->interface_id, enable);
835 dev_err(&intf->dev, "failed to set v_sys: %d\n", ret);
842 static int gb_interface_refclk_set(struct gb_interface *intf, bool enable)
844 struct gb_svc *svc = intf->hd->svc;
847 dev_dbg(&intf->dev, "%s - %d\n", __func__, enable);
849 ret = gb_svc_intf_refclk_set(svc, intf->interface_id, enable);
851 dev_err(&intf->dev, "failed to set refclk: %d\n", ret);
858 static int gb_interface_unipro_set(struct gb_interface *intf, bool enable)
860 struct gb_svc *svc = intf->hd->svc;
863 dev_dbg(&intf->dev, "%s - %d\n", __func__, enable);
865 ret = gb_svc_intf_unipro_set(svc, intf->interface_id, enable);
867 dev_err(&intf->dev, "failed to set UniPro: %d\n", ret);
874 static int gb_interface_activate_operation(struct gb_interface *intf,
875 enum gb_interface_type *intf_type)
877 struct gb_svc *svc = intf->hd->svc;
881 dev_dbg(&intf->dev, "%s\n", __func__);
883 ret = gb_svc_intf_activate(svc, intf->interface_id, &type);
885 dev_err(&intf->dev, "failed to activate: %d\n", ret);
890 case GB_SVC_INTF_TYPE_DUMMY:
891 *intf_type = GB_INTERFACE_TYPE_DUMMY;
892 /* FIXME: handle as an error for now */
894 case GB_SVC_INTF_TYPE_UNIPRO:
895 *intf_type = GB_INTERFACE_TYPE_UNIPRO;
896 dev_err(&intf->dev, "interface type UniPro not supported\n");
897 /* FIXME: handle as an error for now */
899 case GB_SVC_INTF_TYPE_GREYBUS:
900 *intf_type = GB_INTERFACE_TYPE_GREYBUS;
903 dev_err(&intf->dev, "unknown interface type: %u\n", type);
904 *intf_type = GB_INTERFACE_TYPE_UNKNOWN;
911 static int gb_interface_hibernate_link(struct gb_interface *intf)
913 struct gb_svc *svc = intf->hd->svc;
915 return gb_svc_intf_set_power_mode_hibernate(svc, intf->interface_id);
918 static int _gb_interface_activate(struct gb_interface *intf,
919 enum gb_interface_type *type)
923 *type = GB_INTERFACE_TYPE_UNKNOWN;
925 if (intf->ejected || intf->removed)
928 ret = gb_interface_vsys_set(intf, true);
932 ret = gb_interface_refclk_set(intf, true);
934 goto err_vsys_disable;
936 ret = gb_interface_unipro_set(intf, true);
938 goto err_refclk_disable;
940 ret = gb_interface_activate_operation(intf, type);
943 case GB_INTERFACE_TYPE_UNIPRO:
944 case GB_INTERFACE_TYPE_GREYBUS:
945 goto err_hibernate_link;
947 goto err_unipro_disable;
951 ret = gb_interface_read_dme(intf);
953 goto err_hibernate_link;
955 ret = gb_interface_route_create(intf);
957 goto err_hibernate_link;
961 trace_gb_interface_activate(intf);
966 gb_interface_hibernate_link(intf);
968 gb_interface_unipro_set(intf, false);
970 gb_interface_refclk_set(intf, false);
972 gb_interface_vsys_set(intf, false);
978 * At present, we assume a UniPro-only module to be a Greybus module that
979 * failed to send its mailbox poke. There is some reason to believe that this
980 * is because of a bug in the ES3 bootrom.
982 * FIXME: Check if this is a Toshiba bridge before retrying?
984 static int _gb_interface_activate_es3_hack(struct gb_interface *intf,
985 enum gb_interface_type *type)
991 ret = _gb_interface_activate(intf, type);
992 if (ret == -ENODEV && *type == GB_INTERFACE_TYPE_UNIPRO)
1002 * Activate an interface.
1004 * Locking: Caller holds the interface mutex.
1006 int gb_interface_activate(struct gb_interface *intf)
1008 enum gb_interface_type type;
1011 switch (intf->type) {
1012 case GB_INTERFACE_TYPE_INVALID:
1013 case GB_INTERFACE_TYPE_GREYBUS:
1014 ret = _gb_interface_activate_es3_hack(intf, &type);
1017 ret = _gb_interface_activate(intf, &type);
1020 /* Make sure type is detected correctly during reactivation. */
1021 if (intf->type != GB_INTERFACE_TYPE_INVALID) {
1022 if (type != intf->type) {
1023 dev_err(&intf->dev, "failed to detect interface type\n");
1026 gb_interface_deactivate(intf);
1038 * Deactivate an interface.
1040 * Locking: Caller holds the interface mutex.
1042 void gb_interface_deactivate(struct gb_interface *intf)
1047 trace_gb_interface_deactivate(intf);
1049 /* Abort any ongoing mode switch. */
1050 if (intf->mode_switch)
1051 complete(&intf->mode_switch_completion);
1053 gb_interface_route_destroy(intf);
1054 gb_interface_hibernate_link(intf);
1055 gb_interface_unipro_set(intf, false);
1056 gb_interface_refclk_set(intf, false);
1057 gb_interface_vsys_set(intf, false);
1059 intf->active = false;
1063 * Enable an interface by enabling its control connection, fetching the
1064 * manifest and other information over it, and finally registering its child
1067 * Locking: Caller holds the interface mutex.
1069 int gb_interface_enable(struct gb_interface *intf)
1071 struct gb_control *control;
1072 struct gb_bundle *bundle, *tmp;
1076 ret = gb_interface_read_and_clear_init_status(intf);
1078 dev_err(&intf->dev, "failed to clear init status: %d\n", ret);
1082 /* Establish control connection */
1083 control = gb_control_create(intf);
1084 if (IS_ERR(control)) {
1085 dev_err(&intf->dev, "failed to create control device: %ld\n",
1087 return PTR_ERR(control);
1089 intf->control = control;
1091 ret = gb_control_enable(intf->control);
1093 goto err_put_control;
1095 /* Get manifest size using control protocol on CPort */
1096 size = gb_control_get_manifest_size_operation(intf);
1098 dev_err(&intf->dev, "failed to get manifest size: %d\n", size);
1105 goto err_disable_control;
1108 manifest = kmalloc(size, GFP_KERNEL);
1111 goto err_disable_control;
1114 /* Get manifest using control protocol on CPort */
1115 ret = gb_control_get_manifest_operation(intf, manifest, size);
1117 dev_err(&intf->dev, "failed to get manifest: %d\n", ret);
1118 goto err_free_manifest;
1122 * Parse the manifest and build up our data structures representing
1125 if (!gb_manifest_parse(intf, manifest, size)) {
1126 dev_err(&intf->dev, "failed to parse manifest\n");
1128 goto err_destroy_bundles;
1131 ret = gb_control_get_bundle_versions(intf->control);
1133 goto err_destroy_bundles;
1135 /* Register the control device and any bundles */
1136 ret = gb_control_add(intf->control);
1138 goto err_destroy_bundles;
1140 pm_runtime_use_autosuspend(&intf->dev);
1141 pm_runtime_get_noresume(&intf->dev);
1142 pm_runtime_set_active(&intf->dev);
1143 pm_runtime_enable(&intf->dev);
1145 list_for_each_entry_safe_reverse(bundle, tmp, &intf->bundles, links) {
1146 ret = gb_bundle_add(bundle);
1148 gb_bundle_destroy(bundle);
1155 intf->enabled = true;
1157 pm_runtime_put(&intf->dev);
1159 trace_gb_interface_enable(intf);
1163 err_destroy_bundles:
1164 list_for_each_entry_safe(bundle, tmp, &intf->bundles, links)
1165 gb_bundle_destroy(bundle);
1168 err_disable_control:
1169 gb_control_disable(intf->control);
1171 gb_control_put(intf->control);
1172 intf->control = NULL;
1178 * Disable an interface and destroy its bundles.
1180 * Locking: Caller holds the interface mutex.
1182 void gb_interface_disable(struct gb_interface *intf)
1184 struct gb_bundle *bundle;
1185 struct gb_bundle *next;
1190 trace_gb_interface_disable(intf);
1192 pm_runtime_get_sync(&intf->dev);
1194 /* Set disconnected flag to avoid I/O during connection tear down. */
1195 if (intf->quirks & GB_INTERFACE_QUIRK_FORCED_DISABLE)
1196 intf->disconnected = true;
1198 list_for_each_entry_safe(bundle, next, &intf->bundles, links)
1199 gb_bundle_destroy(bundle);
1201 if (!intf->mode_switch && !intf->disconnected)
1202 gb_control_interface_deactivate_prepare(intf->control);
1204 gb_control_del(intf->control);
1205 gb_control_disable(intf->control);
1206 gb_control_put(intf->control);
1207 intf->control = NULL;
1209 intf->enabled = false;
1211 pm_runtime_disable(&intf->dev);
1212 pm_runtime_set_suspended(&intf->dev);
1213 pm_runtime_dont_use_autosuspend(&intf->dev);
1214 pm_runtime_put_noidle(&intf->dev);
1217 /* Register an interface. */
1218 int gb_interface_add(struct gb_interface *intf)
1222 ret = device_add(&intf->dev);
1224 dev_err(&intf->dev, "failed to register interface: %d\n", ret);
1228 trace_gb_interface_add(intf);
1230 dev_info(&intf->dev, "Interface added (%s)\n",
1231 gb_interface_type_string(intf));
1233 switch (intf->type) {
1234 case GB_INTERFACE_TYPE_GREYBUS:
1235 dev_info(&intf->dev, "GMP VID=0x%08x, PID=0x%08x\n",
1236 intf->vendor_id, intf->product_id);
1238 case GB_INTERFACE_TYPE_UNIPRO:
1239 dev_info(&intf->dev, "DDBL1 Manufacturer=0x%08x, Product=0x%08x\n",
1240 intf->ddbl1_manufacturer_id,
1241 intf->ddbl1_product_id);
1250 /* Deregister an interface. */
1251 void gb_interface_del(struct gb_interface *intf)
1253 if (device_is_registered(&intf->dev)) {
1254 trace_gb_interface_del(intf);
1256 device_del(&intf->dev);
1257 dev_info(&intf->dev, "Interface removed\n");
1261 void gb_interface_put(struct gb_interface *intf)
1263 put_device(&intf->dev);