<commit_before>/*
 * Copyright (C) 2017 <NAME> <<EMAIL>>
 * Copyright (C) 2017 <NAME> <<EMAIL>>
 * Copyright (C) 2017 <NAME> <<EMAIL>>
 *
 * SPDX-License-Identifier: LGPL-2.1+
 */

#include "config.h"
#include "synapticsmst-device.h"
#include "synapticsmst-common.h"
#include "fu-plugin-vfuncs.h"
#include "fu-device-metadata.h"

#define SYNAPTICS_FLASH_MODE_DELAY 3
#define SYNAPTICS_UPDATE_ENUMERATE_TRIES 3

static gboolean
syanpticsmst_check_amdgpu_safe (GError **error)
{
	gsize bufsz = 0;
	g_autofree gchar *buf = NULL;
	g_auto(GStrv) lines = NULL;

	if (!g_file_get_contents ("/proc/modules", &buf, &bufsz, error))
		return FALSE;

	lines = g_strsplit (buf, "\n", -1);
	for (guint i = 0; lines[i] != NULL; i++) {
		if (g_str_has_prefix (lines[i], "amdgpu ")) {
			g_set_error_literal (error,
					     FWUPD_ERROR,
					     FWUPD_ERROR_INTERNAL,
					     "amdgpu has known issues with synapticsmst");
			return FALSE;
		}
	}

	return TRUE;
}

static gboolean
synapticsmst_common_check_supported_system (FuPlugin *plugin, GError **error)
{

	if (g_getenv ("FWUPD_SYNAPTICSMST_FW_DIR") != NULL) {
		g_debug ("Running Synaptics plugin in test mode");
		return TRUE;
	}

	/* See https://github.com/hughsie/fwupd/issues/1121 for more details */
	if (!syanpticsmst_check_amdgpu_safe (error))
		return FALSE;

	if (!g_file_test (SYSFS_DRM_DP_AUX, G_FILE_TEST_IS_DIR)) {
		g_set_error (error,
			     G_IO_ERROR,
			     G_IO_ERROR_INVALID_DATA,
			     "MST firmware updating not supported, missing kernel support.");
		return FALSE;
	}
	return TRUE;
}

/* creates MST-$str-$BOARDID */
static void
fu_plugin_synapticsmst_create_simple_guid (FuDevice *fu_device,
					   SynapticsMSTDevice *device,
					   const gchar *str)
{
	guint16 board_id = synapticsmst_device_get_board_id (device);
	g_autofree gchar *devid = g_strdup_printf ("MST-%s-%u", str, board_id);
	fu_device_add_instance_id (fu_device, devid);
}

/* creates MST-$str-$chipid-$BOARDID */
static void
fu_plugin_synapticsmst_create_complex_guid (FuDevice *fu_device,
					    SynapticsMSTDevice *device,
					    const gchar *device_kind)
{
	const gchar *chip_id_str = synapticsmst_device_get_chip_id_str (device);
	g_autofree gchar *chip_id_down = g_ascii_strdown (chip_id_str, -1);
	g_autofree gchar *tmp = g_strdup_printf ("%s-%s", device_kind, chip_id_down);

	fu_plugin_synapticsmst_create_simple_guid (fu_device, device, tmp);
}

static gboolean
fu_plugin_synapticsmst_lookup_device (FuPlugin *plugin,
				      FuDevice *fu_device,
				      SynapticsMSTDevice *device,
				      GError **error)
{
	const gchar *board_str;
	const gchar *guid_template;
	guint16 board_id = synapticsmst_device_get_board_id (device);
	const gchar *chip_id_str = synapticsmst_device_get_chip_id_str (device);
	g_autofree gchar *group = NULL;
	g_autofree gchar *name = NULL;

	/* GUIDs used only for test mode */
	if (g_getenv ("FWUPD_SYNAPTICSMST_FW_DIR") != NULL) {
		g_autofree gchar *tmp = NULL;
		tmp = g_strdup_printf ("test-%s", chip_id_str);
		fu_plugin_synapticsmst_create_simple_guid (fu_device, device, tmp);
		return TRUE;
	}

	/* set up the device name via quirks */
	group = g_strdup_printf ("SynapticsMSTBoardID=%u", board_id);
	board_str = fu_plugin_lookup_quirk_by_id (plugin, group,
					      FU_QUIRKS_NAME);
	if (board_str == NULL)
		board_str = "Unknown Platform";
	name = g_strdup_printf ("Synaptics %s inside %s", synapticsmst_device_get_chip_id_str (device),
				board_str);
	fu_device_set_name (fu_device, name);

	/* build the GUIDs for the device */
	guid_template = fu_plugin_lookup_quirk_by_id (plugin, group, "DeviceKind");
	/* no quirks defined for this board */
	if (guid_template == NULL) {
		g_set_error (error,
			     FWUPD_ERROR,
			     FWUPD_ERROR_NOT_SUPPORTED,
			     "Unknown board_id %x",
			     board_id);
		return FALSE;

	/* this is a host system, use system ID */
	} else if (g_strcmp0 (guid_template, "system") == 0) {
		const gchar *system_type = fu_plugin_get_dmi_value (plugin,
								    FU_HWIDS_KEY_PRODUCT_SKU);
		fu_plugin_synapticsmst_create_simple_guid (fu_device, device,
							   system_type);
	/* docks or something else */
	} else {
		g_auto(GStrv) templates = NULL;
		templates = g_strsplit (guid_template, ",", -1);
		for (guint i = 0; templates[i] != NULL; i++) {
			fu_plugin_synapticsmst_create_complex_guid (fu_device,
								    device,
								    templates[i]);
		}
	}

	return TRUE;
}

static gboolean
fu_plugin_synaptics_add_device (FuPlugin *plugin,
				SynapticsMSTDevice *device,
				GError **error)
{
	g_autoptr(FuDevice) dev = NULL;
	const gchar *kind_str = NULL;
	g_autofree gchar *dev_id_str = NULL;
	g_autofree gchar *layer_str = NULL;
	g_autofree gchar *rad_str = NULL;
	const gchar *aux_node;
	guint8 layer;
	guint16 rad;

	aux_node = synapticsmst_device_get_aux_node (device);
	if (!synapticsmst_device_enumerate_device (device,
						   error)) {
		g_prefix_error (error, "Error enumerating device at %s: ", aux_node);
		return FALSE;
	}

	/* create the device */
	dev = fu_device_new ();
	/* Store $KIND-$AUXNODE-$LAYER-$RAD as device ID */
	layer = synapticsmst_device_get_layer (device);
	rad = synapticsmst_device_get_rad (device);
	kind_str = synapticsmst_device_kind_to_string (synapticsmst_device_get_kind (device));
	dev_id_str = g_strdup_printf ("MST-%s-%s-%u-%u",
				      kind_str, aux_node, layer, rad);
	fu_device_set_id (dev, dev_id_str);
	fu_device_set_physical_id (dev, aux_node);
	fu_device_set_metadata (dev, "SynapticsMSTKind", kind_str);
	fu_device_set_metadata (dev, "SynapticsMSTAuxNode", aux_node);
	layer_str = g_strdup_printf ("%u", layer);
	fu_device_set_metadata (dev, "SynapticsMSTLayer", layer_str);
	rad_str = g_strdup_printf ("%u", rad);
	fu_device_set_metadata (dev, "SynapticsMSTRad", rad_str);
	fu_device_add_flag (dev, FWUPD_DEVICE_FLAG_UPDATABLE);
	fu_device_set_vendor (dev, "Synaptics");
	fu_device_set_summary (dev, "Multi-Stream Transport Device");
	fu_device_add_icon (dev, "computer");
	fu_device_set_version (dev, synapticsmst_device_get_version (device),
			       FWUPD_VERSION_FORMAT_TRIPLET);
	fu_device_set_quirks (dev, fu_plugin_get_quirks (plugin));

	/* create GUIDs and name */
	if (!fu_plugin_synapticsmst_lookup_device (plugin, dev, device, error))
		return FALSE;

	fu_plugin_device_add (plugin, dev);
	fu_plugin_cache_add (plugin, dev_id_str, dev);

	/* inhibit the idle sleep of the daemon */<commit_msg>synapticsmst: fix GUID generation (Closes: #1207)<commit_after>/*
 * Copyright (C) 2017 <NAME> <<EMAIL>>
 * Copyright (C) 2017 <NAME> <<EMAIL>>
 * Copyright (C) 2017 <NAME> <<EMAIL>>
 *
 * SPDX-License-Identifier: LGPL-2.1+
 */

#include "config.h"
#include "synapticsmst-device.h"
#include "synapticsmst-common.h"
#include "fu-plugin-vfuncs.h"
#include "fu-device-metadata.h"

#define SYNAPTICS_FLASH_MODE_DELAY 3
#define SYNAPTICS_UPDATE_ENUMERATE_TRIES 3

static gboolean
syanpticsmst_check_amdgpu_safe (GError **error)
{
	gsize bufsz = 0;
	g_autofree gchar *buf = NULL;
	g_auto(GStrv) lines = NULL;

	if (!g_file_get_contents ("/proc/modules", &buf, &bufsz, error))
		return FALSE;

	lines = g_strsplit (buf, "\n", -1);
	for (guint i = 0; lines[i] != NULL; i++) {
		if (g_str_has_prefix (lines[i], "amdgpu ")) {
			g_set_error_literal (error,
					     FWUPD_ERROR,
					     FWUPD_ERROR_INTERNAL,
					     "amdgpu has known issues with synapticsmst");
			return FALSE;
		}
	}

	return TRUE;
}

static gboolean
synapticsmst_common_check_supported_system (FuPlugin *plugin, GError **error)
{

	if (g_getenv ("FWUPD_SYNAPTICSMST_FW_DIR") != NULL) {
		g_debug ("Running Synaptics plugin in test mode");
		return TRUE;
	}

	/* See https://github.com/hughsie/fwupd/issues/1121 for more details */
	if (!syanpticsmst_check_amdgpu_safe (error))
		return FALSE;

	if (!g_file_test (SYSFS_DRM_DP_AUX, G_FILE_TEST_IS_DIR)) {
		g_set_error (error,
			     G_IO_ERROR,
			     G_IO_ERROR_INVALID_DATA,
			     "MST firmware updating not supported, missing kernel support.");
		return FALSE;
	}
	return TRUE;
}

/* creates MST-$str-$BOARDID */
static void
fu_plugin_synapticsmst_create_simple_guid (FuDevice *fu_device,
					   SynapticsMSTDevice *device,
					   const gchar *str)
{
	guint16 board_id = synapticsmst_device_get_board_id (device);
	g_autofree gchar *devid = g_strdup_printf ("MST-%s-%u", str, board_id);
	fu_device_add_instance_id (fu_device, devid);
}

/* creates MST-$str-$chipid-$BOARDID */
static void
fu_plugin_synapticsmst_create_complex_guid (FuDevice *fu_device,
					    SynapticsMSTDevice *device,
					    const gchar *device_kind)
{
	const gchar *chip_id_str = synapticsmst_device_get_chip_id_str (device);
	g_autofree gchar *chip_id_down = g_ascii_strdown (chip_id_str, -1);
	g_autofree gchar *tmp = g_strdup_printf ("%s-%s", device_kind, chip_id_down);

	fu_plugin_synapticsmst_create_simple_guid (fu_device, device, tmp);
}

static gboolean
fu_plugin_synapticsmst_lookup_device (FuPlugin *plugin,
				      FuDevice *fu_device,
				      SynapticsMSTDevice *device,
				      GError **error)
{
	const gchar *board_str;
	const gchar *guid_template;
	guint16 board_id = synapticsmst_device_get_board_id (device);
	const gchar *chip_id_str = synapticsmst_device_get_chip_id_str (device);
	g_autofree gchar *group = NULL;
	g_autofree gchar *name = NULL;

	/* GUIDs used only for test mode */
	if (g_getenv ("FWUPD_SYNAPTICSMST_FW_DIR") != NULL) {
		g_autofree gchar *tmp = NULL;
		tmp = g_strdup_printf ("test-%s", chip_id_str);
		fu_plugin_synapticsmst_create_simple_guid (fu_device, device, tmp);
		return TRUE;
	}

	/* set up the device name via quirks */
	group = g_strdup_printf ("SynapticsMSTBoardID=%u", board_id);
	board_str = fu_plugin_lookup_quirk_by_id (plugin, group,
					      FU_QUIRKS_NAME);
	if (board_str == NULL)
		board_str = "Unknown Platform";
	name = g_strdup_printf ("Synaptics %s inside %s", synapticsmst_device_get_chip_id_str (device),
				board_str);
	fu_device_set_name (fu_device, name);

	/* build the GUIDs for the device */
	guid_template = fu_plugin_lookup_quirk_by_id (plugin, group, "DeviceKind");
	/* no quirks defined for this board */
	if (guid_template == NULL) {
		g_set_error (error,
			     FWUPD_ERROR,
			     FWUPD_ERROR_NOT_SUPPORTED,
			     "Unknown board_id %x",
			     board_id);
		return FALSE;

	/* this is a host system, use system ID */
	} else if (g_strcmp0 (guid_template, "system") == 0) {
		const gchar *system_type = fu_plugin_get_dmi_value (plugin,
								    FU_HWIDS_KEY_PRODUCT_SKU);
		fu_plugin_synapticsmst_create_simple_guid (fu_device, device,
							   system_type);
	/* docks or something else */
	} else {
		g_auto(GStrv) templates = NULL;
		templates = g_strsplit (guid_template, ",", -1);
		for (guint i = 0; templates[i] != NULL; i++) {
			fu_plugin_synapticsmst_create_complex_guid (fu_device,
								    device,
								    templates[i]);
		}
	}

	return TRUE;
}

static gboolean
fu_plugin_synaptics_add_device (FuPlugin *plugin,
				SynapticsMSTDevice *device,
				GError **error)
{
	g_autoptr(FuDevice) dev = NULL;
	const gchar *kind_str = NULL;
	g_autofree gchar *dev_id_str = NULL;
	g_autofree gchar *layer_str = NULL;
	g_autofree gchar *rad_str = NULL;
	const gchar *aux_node;
	guint8 layer;
	guint16 rad;

	aux_node = synapticsmst_device_get_aux_node (device);
	if (!synapticsmst_device_enumerate_device (device,
						   error)) {
		g_prefix_error (error, "Error enumerating device at %s: ", aux_node);
		return FALSE;
	}

	/* create the device */
	dev = fu_device_new ();
	/* Store $KIND-$AUXNODE-$LAYER-$RAD as device ID */
	layer = synapticsmst_device_get_layer (device);
	rad = synapticsmst_device_get_rad (device);
	kind_str = synapticsmst_device_kind_to_string (synapticsmst_device_get_kind (device));
	dev_id_str = g_strdup_printf ("MST-%s-%s-%u-%u",
				      kind_str, aux_node, layer, rad);
	fu_device_set_id (dev, dev_id_str);
	fu_device_set_physical_id (dev, aux_node);
	fu_device_set_metadata (dev, "SynapticsMSTKind", kind_str);
	fu_device_set_metadata (dev, "SynapticsMSTAuxNode", aux_node);
	layer_str = g_strdup_printf ("%u", layer);
	fu_device_set_metadata (dev, "SynapticsMSTLayer", layer_str);
	rad_str = g_strdup_printf ("%u", rad);
	fu_device_set_metadata (dev, "SynapticsMSTRad", rad_str);
	fu_device_add_flag (dev, FWUPD_DEVICE_FLAG_UPDATABLE);
	fu_device_set_vendor (dev, "Synaptics");
	fu_device_set_summary (dev, "Multi-Stream Transport Device");
	fu_device_add_icon (dev, "computer");
	fu_device_set_version (dev, synapticsmst_device_get_version (device),
			       FWUPD_VERSION_FORMAT_TRIPLET);
	fu_device_set_quirks (dev, fu_plugin_get_quirks (plugin));

	/* create GUIDs and name */
	if (!fu_plugin_synapticsmst_lookup_device (plugin, dev, device, error))
		return FALSE;
	if (!fu_device_setup (dev, error))
		return FALSE;
	fu_plugin_device_add (plugin, dev);
	fu_plugin_cache_add (plugin, dev_id_str, dev);

	/* inhibit the idle sleep of the daemon */
{'fraction_non_alphanumeric': 0.11184363608159466, 'fraction_numerical': 0.005739548592259365, 'mean_word_length': 4.6652021089630935, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2264746', 'n_tokens_mistral': 5399, 'n_tokens_neox': 4949, 'n_words': 1063}