summaryrefslogtreecommitdiff
path: root/drivers/staging/unisys/visorbus/channel_attr.c
blob: 0d7184ac1cbe2067c8530fcfdb3e8fcae7805854 (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
/* channel_attr.c
 *
 * Copyright (C) 2010 - 2013 UNISYS CORPORATION
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
 * NON INFRINGEMENT.  See the GNU General Public License for more
 * details.
 */

/* Implement publishing of channel attributes under:
 *
 *     /sys/bus/visorbus<x>/dev<y>/channel
 *
 */

#include "channel_attr.h"
#define CURRENT_FILE_PC VISOR_BUS_PC_channel_attr_c
#define to_channel_attr(_attr) \
	container_of(_attr, struct channel_attribute, attr)
#define to_visor_device_from_kobjchannel(obj) \
	container_of(obj, struct visor_device, kobjchannel)

struct channel_attribute {
	struct attribute attr;
	 ssize_t (*show)(struct visor_device*, char *buf);
	 ssize_t (*store)(struct visor_device*, const char *buf, size_t count);
};

/* begin implementation of specific channel attributes to appear under
* /sys/bus/visorbus<x>/dev<y>/channel
*/
static ssize_t devicechannel_attr_physaddr(struct visor_device *dev, char *buf)
{
	if (!dev->visorchannel)
		return 0;
	return snprintf(buf, PAGE_SIZE, "0x%Lx\n",
			visorchannel_get_physaddr(dev->visorchannel));
}

static ssize_t devicechannel_attr_nbytes(struct visor_device *dev, char *buf)
{
	if (!dev->visorchannel)
		return 0;
	return snprintf(buf, PAGE_SIZE, "0x%lx\n",
			visorchannel_get_nbytes(dev->visorchannel));
}

static ssize_t devicechannel_attr_clientpartition(struct visor_device *dev,
						  char *buf) {
	if (!dev->visorchannel)
		return 0;
	return snprintf(buf, PAGE_SIZE, "0x%Lx\n",
			visorchannel_get_clientpartition(dev->visorchannel));
}

static ssize_t devicechannel_attr_typeguid(struct visor_device *dev, char *buf)
{
	char s[99];

	if (!dev->visorchannel)
		return 0;
	return snprintf(buf, PAGE_SIZE, "%s\n",
			visorchannel_id(dev->visorchannel, s));
}

static ssize_t devicechannel_attr_zoneguid(struct visor_device *dev, char *buf)
{
	char s[99];

	if (!dev->visorchannel)
		return 0;
	return snprintf(buf, PAGE_SIZE, "%s\n",
			visorchannel_zoneid(dev->visorchannel, s));
}

static ssize_t devicechannel_attr_typename(struct visor_device *dev, char *buf)
{
	int i = 0;
	struct bus_type *xbus = dev->device.bus;
	struct device_driver *xdrv = dev->device.driver;
	struct visor_driver *drv = NULL;

	if (!dev->visorchannel || !xbus || !xdrv)
		return 0;
	i = xbus->match(&dev->device, xdrv);
	if (!i)
		return 0;
	drv = to_visor_driver(xdrv);
	return snprintf(buf, PAGE_SIZE, "%s\n", drv->channel_types[i - 1].name);
}

static ssize_t devicechannel_attr_dump(struct visor_device *dev, char *buf)
{
	int count = 0;
/* TODO: replace this with debugfs code
	struct seq_file *m = NULL;
	if (dev->visorchannel == NULL)
		return 0;
	m = visor_seq_file_new_buffer(buf, PAGE_SIZE - 1);
	if (m == NULL)
		return 0;
	visorchannel_debug(dev->visorchannel, 1, m, 0);
	count = m->count;
	visor_seq_file_done_buffer(m);
	m = NULL;
*/
	return count;
}

static struct channel_attribute all_channel_attrs[] = {
	__ATTR(physaddr, S_IRUGO,
	       devicechannel_attr_physaddr, NULL),
	__ATTR(nbytes, S_IRUGO,
	       devicechannel_attr_nbytes, NULL),
	__ATTR(clientpartition, S_IRUGO,
	       devicechannel_attr_clientpartition, NULL),
	__ATTR(typeguid, S_IRUGO,
	       devicechannel_attr_typeguid, NULL),
	__ATTR(zoneguid, S_IRUGO,
	       devicechannel_attr_zoneguid, NULL),
	__ATTR(typename, S_IRUGO,
	       devicechannel_attr_typename, NULL),
	__ATTR(dump, S_IRUGO,
	       devicechannel_attr_dump, NULL),
};

/* end implementation of specific channel attributes */

static ssize_t channel_attr_show(struct kobject *kobj, struct attribute *attr,
				 char *buf)
{
	struct channel_attribute *channel_attr = to_channel_attr(attr);
	struct visor_device *dev = to_visor_device_from_kobjchannel(kobj);
	ssize_t ret = 0;

	if (channel_attr->show)
		ret = channel_attr->show(dev, buf);
	return ret;
}

static ssize_t channel_attr_store(struct kobject *kobj, struct attribute *attr,
				  const char *buf, size_t count)
{
	struct channel_attribute *channel_attr = to_channel_attr(attr);
	struct visor_device *dev = to_visor_device_from_kobjchannel(kobj);
	ssize_t ret = 0;

	if (channel_attr->store)
		ret = channel_attr->store(dev, buf, count);
	return ret;
}

static int channel_create_file(struct visor_device *dev,
			       struct channel_attribute *attr)
{
	return sysfs_create_file(&dev->kobjchannel, &attr->attr);
}

static void channel_remove_file(struct visor_device *dev,
				struct channel_attribute *attr)
{
	sysfs_remove_file(&dev->kobjchannel, &attr->attr);
}

static const struct sysfs_ops channel_sysfs_ops = {
	.show = channel_attr_show,
	.store = channel_attr_store,
};

static struct kobj_type channel_kobj_type = {
	.sysfs_ops = &channel_sysfs_ops
};

int register_channel_attributes(struct visor_device *dev)
{
	int rc = 0, i = 0, x = 0;

	if (dev->kobjchannel.parent)
		goto away;	/* already registered */
	x = kobject_init_and_add(&dev->kobjchannel, &channel_kobj_type,
				 &dev->device.kobj, "channel");
	if (x < 0) {
		rc = x;
		goto away;
	}

	kobject_uevent(&dev->kobjchannel, KOBJ_ADD);

	for (i = 0;
	     i < sizeof(all_channel_attrs) / sizeof(struct channel_attribute);
	     i++)
		x = channel_create_file(dev, &all_channel_attrs[i]);
	if (x < 0) {
		while (--i >= 0)
			channel_remove_file(dev, &all_channel_attrs[i]);
		kobject_del(&dev->kobjchannel);
		kobject_put(&dev->kobjchannel);
		rc = x;
		goto away;
	}
away:
	return rc;
}

void unregister_channel_attributes(struct visor_device *dev)
{
	int i = 0;

	if (!dev->kobjchannel.parent)
		return;		/* already unregistered */
	for (i = 0;
	     i < sizeof(all_channel_attrs) / sizeof(struct channel_attribute);
	     i++)
		channel_remove_file(dev, &all_channel_attrs[i]);

	kobject_del(&dev->kobjchannel);
	kobject_put(&dev->kobjchannel);
	dev->kobjchannel.parent = NULL;
}