summaryrefslogtreecommitdiff
path: root/drivers/block/host-uclass.c
blob: 6460d968c23f7a039e36fdef86d6765eecb34405 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// SPDX-License-Identifier: GPL-2.0+
/*
 * Uclass for sandbox host interface, used to access files on the host which
 * contain partitions and filesystem
 *
 * Copyright 2022 Google LLC
 * Written by Simon Glass <sjg@chromium.org>
 */

#define LOG_CATEGORY UCLASS_HOST

#include <common.h>
#include <blk.h>
#include <dm.h>
#include <malloc.h>
#include <sandbox_host.h>
#include <dm/device-internal.h>
#include <dm/lists.h>
#include <dm/uclass-internal.h>

DECLARE_GLOBAL_DATA_PTR;

/**
 * struct host_priv - information kept by the host uclass
 *
 * @cur_dev: Currently selected host device, or NULL if none
 */
struct host_priv {
	struct udevice *cur_dev;
};

int host_create_device(const char *label, bool removable, struct udevice **devp)
{
	char dev_name[30], *str, *label_new;
	struct host_sb_plat *plat;
	struct udevice *dev, *blk;
	int ret;

	/* unbind any existing device with this label */
	dev = host_find_by_label(label);
	if (dev) {
		ret = host_detach_file(dev);
		if (ret)
			return log_msg_ret("det", ret);

		ret = device_unbind(dev);
		if (ret)
			return log_msg_ret("unb", ret);
	}

	snprintf(dev_name, sizeof(dev_name), "host-%s", label);
	str = strdup(dev_name);
	if (!str)
		return -ENOMEM;

	label_new = strdup(label);
	if (!label_new) {
		ret = -ENOMEM;
		goto no_label;
	}

	ret = device_bind_driver(gd->dm_root, "host_sb_drv", str, &dev);
	if (ret)
		goto no_dev;
	device_set_name_alloced(dev);

	if (!blk_find_from_parent(dev, &blk)) {
		struct blk_desc *desc = dev_get_uclass_plat(blk);

		desc->removable = removable;
	}

	plat = dev_get_plat(dev);
	plat->label = label_new;
	*devp = dev;

	return 0;

no_dev:
	free(label_new);
no_label:
	free(str);

	return ret;
}

int host_attach_file(struct udevice *dev, const char *filename)
{
	struct host_ops *ops = host_get_ops(dev);

	if (!ops->attach_file)
		return -ENOSYS;

	return ops->attach_file(dev, filename);
}

int host_create_attach_file(const char *label, const char *filename,
			    bool removable, struct udevice **devp)
{
	struct udevice *dev;
	int ret;

	ret = host_create_device(label, removable, &dev);
	if (ret)
		return log_msg_ret("cre", ret);

	ret = host_attach_file(dev, filename);
	if (ret) {
		device_unbind(dev);
		return log_msg_ret("att", ret);
	}
	*devp = dev;

	return 0;
}

int host_detach_file(struct udevice *dev)
{
	struct host_ops *ops = host_get_ops(dev);

	if (!ops->detach_file)
		return -ENOSYS;

	if (dev == host_get_cur_dev())
		host_set_cur_dev(NULL);

	return ops->detach_file(dev);
}

struct udevice *host_find_by_label(const char *label)
{
	struct udevice *dev;
	struct uclass *uc;

	uclass_id_foreach_dev(UCLASS_HOST, dev, uc) {
		struct host_sb_plat *plat = dev_get_plat(dev);

		if (plat->label && !strcmp(label, plat->label))
			return dev;
	}

	return NULL;
}

struct udevice *host_get_cur_dev(void)
{
	struct uclass *uc = uclass_find(UCLASS_HOST);

	if (uc) {
		struct host_priv *priv = uclass_get_priv(uc);

		return priv->cur_dev;
	}

	return NULL;
}

void host_set_cur_dev(struct udevice *dev)
{
	struct uclass *uc = uclass_find(UCLASS_HOST);

	if (uc) {
		struct host_priv *priv = uclass_get_priv(uc);

		priv->cur_dev = dev;
	}
}

UCLASS_DRIVER(host) = {
	.id		= UCLASS_HOST,
	.name		= "host",
#if CONFIG_IS_ENABLED(OF_REAL)
	.post_bind	= dm_scan_fdt_dev,
#endif
	.priv_auto	= sizeof(struct host_priv),
};