summaryrefslogtreecommitdiff
path: root/drivers/misc/issei/fw_client.c
blob: b8e48dbf1c9c7d725d457f8b7de936ec86b3d0b9 (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// SPDX-License-Identifier: GPL-2.0
/* Copyright (C) 2023-2026 Intel Corporation */
#include <linux/bug.h>
#include <linux/cleanup.h>
#include <linux/container_of.h>
#include <linux/device.h>
#include <linux/dev_printk.h>
#include <linux/kobject.h>
#include <linux/list.h>
#include <linux/slab.h>
#include <linux/sprintf.h>
#include <linux/string.h>
#include <linux/types.h>
#include <linux/uuid.h>

#include "issei_dev.h"
#include "fw_client.h"

/*
 * Specific attribute handlers for fw_clients kset.
 * Provide only show function as all fw_client attributes are read-only.
 */

struct issei_fw_cl_attr {
	struct attribute attr;
	ssize_t (*show)(struct issei_fw_client *fw_cl, const struct issei_fw_cl_attr *attr,
			char *buf);
};
#define to_issei_fw_cl_attr(x) container_of_const(x, struct issei_fw_cl_attr, attr)

static ssize_t fw_cl_attr_show(struct kobject *kobj, struct attribute *attr, char *buf)
{
	const struct issei_fw_cl_attr *issei_attr;
	struct issei_fw_client *fw_cl;

	issei_attr = to_issei_fw_cl_attr(attr);
	fw_cl = to_issei_fw_client(kobj);

	if (!issei_attr->show)
		return -EIO;

	return issei_attr->show(fw_cl, issei_attr, buf);
}

static const struct sysfs_ops fw_cl_sysfs_ops = {
	.show = fw_cl_attr_show,
};

#define FW_CL_ATTR_RO(_name) \
	struct issei_fw_cl_attr fw_cl_attr_##_name = __ATTR_RO(_name)

/* fw_client attributes */

static ssize_t id_show(struct issei_fw_client *fw_cl,
		       const struct issei_fw_cl_attr *attr, char *buf)
{
	return sysfs_emit(buf, "%u\n", fw_cl->id);
}
static FW_CL_ATTR_RO(id);

static ssize_t ver_show(struct issei_fw_client *fw_cl,
			const struct issei_fw_cl_attr *attr, char *buf)
{
	return sysfs_emit(buf, "%u\n", fw_cl->ver);
}
static FW_CL_ATTR_RO(ver);

static ssize_t uuid_show(struct issei_fw_client *fw_cl,
			 const struct issei_fw_cl_attr *attr, char *buf)
{
	return sysfs_emit(buf, "%pUb\n", &fw_cl->uuid);
}
static FW_CL_ATTR_RO(uuid);

static ssize_t mtu_show(struct issei_fw_client *fw_cl,
			const struct issei_fw_cl_attr *attr, char *buf)
{
	return sysfs_emit(buf, "%u\n", fw_cl->mtu);
}
static FW_CL_ATTR_RO(mtu);

static const struct attribute *const fw_cl_attrs[] = {
	&fw_cl_attr_id.attr,
	&fw_cl_attr_ver.attr,
	&fw_cl_attr_uuid.attr,
	&fw_cl_attr_mtu.attr,
	NULL,
};

static const struct attribute_group fw_cl_group = {
	.attrs_const = fw_cl_attrs,
};
__ATTRIBUTE_GROUPS(fw_cl);

static void issei_fw_cl_init(struct issei_fw_client *fw_cl, u16 id, u8 ver, const uuid_t *uuid,
			     u32 mtu, u32 flags)
{
	INIT_LIST_HEAD(&fw_cl->list);
	fw_cl->id = id;
	fw_cl->ver = ver;
	fw_cl->uuid = *uuid;
	fw_cl->mtu = mtu;
	fw_cl->flags = flags;
}

static void fw_cl_release(struct kobject *kobj)
{
	struct issei_fw_client *fw_cl = to_issei_fw_client(kobj);

	kfree(fw_cl);
}

static const struct kobj_type fw_client_ktype = {
	.sysfs_ops = &fw_cl_sysfs_ops,
	.release = fw_cl_release,
	.default_groups = fw_cl_groups,
};

/**
 * issei_fw_cl_create - create firmware client object and add to list
 * @idev: issei device object
 * @id: firmware client id
 * @ver: firmware client version
 * @uuid: firmware client unique id
 * @mtu: firmware client maximum message size
 * @flags: firmware client flags
 *
 * Should be called under idev->client_lock
 *
 * Return: pointer to newly created object on success, ERR_PTR on failure
 */
struct issei_fw_client *issei_fw_cl_create(struct issei_device *idev, u16 id, u8 ver,
					   const uuid_t *uuid, u32 mtu, u32 flags)
{
	int ret;
	struct issei_fw_client *fw_cl = kzalloc_obj(*fw_cl);

	if (!fw_cl)
		return ERR_PTR(-ENOMEM);

	WARN_ON(!mutex_is_locked(&idev->client_lock));

	issei_fw_cl_init(fw_cl, id, ver, uuid, mtu, flags);
	fw_cl->kobj.kset = idev->fw_clients;

	ret = kobject_init_and_add(&fw_cl->kobj, &fw_client_ktype, NULL, "%u", id);
	if (ret) {
		kobject_put(&fw_cl->kobj);
		return ERR_PTR(ret);
	}

	list_add_tail(&fw_cl->list, &idev->fw_client_list);

	dev_dbg(&idev->dev, "FW client %pUb created\n", uuid);

	kobject_uevent(&fw_cl->kobj, KOBJ_ADD);

	return fw_cl;
}

static void __issei_fw_cl_remove(struct issei_device *idev, struct issei_fw_client *fw_cl)
{
	WARN(fw_cl->cl, "Removing connected client!\n");

	dev_dbg(&idev->dev, "FW client %pUb will be removed\n", &fw_cl->uuid);

	list_del(&fw_cl->list);
	kobject_put(&fw_cl->kobj);
}

/**
 * issei_fw_cl_remove_all - remove all firmware client objects
 * @idev: issei device object
 */
void issei_fw_cl_remove_all(struct issei_device *idev)
{
	struct issei_fw_client *fw_cl, *next;

	guard(mutex)(&idev->client_lock);

	list_for_each_entry_safe(fw_cl, next, &idev->fw_client_list, list)
		__issei_fw_cl_remove(idev, fw_cl);
}

/**
 * issei_fw_cl_find_by_uuid - find firmware client by uuid
 * @idev: issei device object
 * @uuid: uuid to search by it
 *
 * Should be called under idev->client_lock
 *
 * Return: pointer to firmware client object if found, NULL on failure
 */
struct issei_fw_client *issei_fw_cl_find_by_uuid(struct issei_device *idev, const uuid_t *uuid)
{
	struct issei_fw_client *fw_cl;

	WARN_ON(!mutex_is_locked(&idev->client_lock));

	list_for_each_entry(fw_cl, &idev->fw_client_list, list) {
		if (uuid_equal(&fw_cl->uuid, uuid)) {
			kobject_get(&fw_cl->kobj);
			return fw_cl;
		}
	}
	return NULL;
}

/**
 * issei_fw_cl_connect - connect firmware and host client
 * @fw_cl: firmware client
 * @cl: host client
 *
 * Should be called under idev->client_lock
 *
 * Return: 0 on success, -EBUSY if already connected
 */
int issei_fw_cl_connect(struct issei_fw_client *fw_cl, struct issei_host_client *cl)
{
	if (fw_cl->cl)
		return -EBUSY;

	kobject_get(&fw_cl->kobj);
	fw_cl->cl = cl;
	return 0;
}

/**
 * issei_fw_cl_disconnect - disconnect firmware and host client
 * @fw_cl: firmware client
 *
 * Should be called under idev->client_lock
 */
void issei_fw_cl_disconnect(struct issei_fw_client *fw_cl)
{
	WARN_ON(!fw_cl->cl);

	fw_cl->cl = NULL;
	kobject_put(&fw_cl->kobj);
}