]>
Commit | Line | Data |
---|---|---|
060b293b SW |
1 | /* |
2 | * visorbus_main.c | |
3703987c | 3 | * |
6f14cc18 | 4 | * Copyright � 2010 - 2015 UNISYS CORPORATION |
3703987c EA |
5 | * All rights reserved. |
6 | * | |
6f14cc18 BR |
7 | * This program is free software; you can redistribute it and/or modify it |
8 | * under the terms and conditions of the GNU General Public License, | |
9 | * version 2, as published by the Free Software Foundation. | |
3703987c EA |
10 | * |
11 | * This program is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or | |
14 | * NON INFRINGEMENT. See the GNU General Public License for more | |
15 | * details. | |
16 | */ | |
17 | ||
8217becc | 18 | #include <linux/debugfs.h> |
3703987c EA |
19 | #include <linux/uuid.h> |
20 | ||
4b78000e | 21 | #include "visorbus.h" |
c79b28f7 | 22 | #include "visorbus_private.h" |
4b78000e | 23 | |
060b293b | 24 | /* Display string that is guaranteed to be no longer the 99 characters */ |
38d56c2f EA |
25 | #define LINESIZE 99 |
26 | ||
3703987c | 27 | #define CURRENT_FILE_PC VISOR_BUS_PC_visorbus_main_c |
ffcdb101 | 28 | #define POLLJIFFIES_NORMALCHANNEL 10 |
3703987c | 29 | |
060b293b SW |
30 | /* stores whether bus_registration was successful */ |
31 | static bool initialized; | |
8217becc | 32 | static struct dentry *visorbus_debugfs_dir; |
6155a3cf | 33 | |
59fd2c8b PB |
34 | /* |
35 | * DEVICE type attributes | |
36 | * | |
37 | * The modalias file will contain the guid of the device. | |
38 | */ | |
39 | static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, | |
40 | char *buf) | |
41 | { | |
42 | struct visor_device *vdev; | |
43 | uuid_le guid; | |
44 | ||
45 | vdev = to_visor_device(dev); | |
46 | guid = visorchannel_get_uuid(vdev->visorchannel); | |
7a41385e | 47 | return sprintf(buf, "visorbus:%pUl\n", &guid); |
59fd2c8b PB |
48 | } |
49 | static DEVICE_ATTR_RO(modalias); | |
50 | ||
51 | static struct attribute *visorbus_dev_attrs[] = { | |
52 | &dev_attr_modalias.attr, | |
53 | NULL, | |
54 | }; | |
55 | ||
56 | /* sysfs example for bridge-only sysfs files using device_type's */ | |
57 | static const struct attribute_group visorbus_dev_group = { | |
58 | .attrs = visorbus_dev_attrs, | |
59 | }; | |
60 | ||
cf7281c9 | 61 | static const struct attribute_group *visorbus_dev_groups[] = { |
59fd2c8b PB |
62 | &visorbus_dev_group, |
63 | NULL, | |
64 | }; | |
65 | ||
3703987c | 66 | /* filled in with info about parent chipset driver when we register with it */ |
55c71eba | 67 | static struct visor_vbus_deviceinfo chipset_driverinfo; |
3703987c | 68 | /* filled in with info about this driver, wrt it servicing client busses */ |
55c71eba | 69 | static struct visor_vbus_deviceinfo clientbus_driverinfo; |
3703987c | 70 | |
3fd1b3b6 | 71 | /* list of visor_device structs, linked via .list_all */ |
3703987c | 72 | static LIST_HEAD(list_all_bus_instances); |
3fd1b3b6 | 73 | /* list of visor_device structs, linked via .list_all */ |
3703987c EA |
74 | static LIST_HEAD(list_all_device_instances); |
75 | ||
76 | static int | |
77 | visorbus_uevent(struct device *xdev, struct kobj_uevent_env *env) | |
78 | { | |
59fd2c8b PB |
79 | struct visor_device *dev; |
80 | uuid_le guid; | |
81 | ||
82 | dev = to_visor_device(xdev); | |
83 | guid = visorchannel_get_uuid(dev->visorchannel); | |
84 | ||
f894c88a | 85 | return add_uevent_var(env, "MODALIAS=visorbus:%pUl", &guid); |
3703987c EA |
86 | } |
87 | ||
b84be59a | 88 | /* |
3fd1b3b6 DB |
89 | * visorbus_match() - called automatically upon adding a visor_device |
90 | * (device_add), or adding a visor_driver | |
91 | * (visorbus_register_visor_driver) | |
92 | * @xdev: struct device for the device being matched | |
93 | * @xdrv: struct device_driver for driver to match device against | |
94 | * | |
95 | * Return: 1 iff the provided driver can control the specified device | |
3703987c EA |
96 | */ |
97 | static int | |
98 | visorbus_match(struct device *xdev, struct device_driver *xdrv) | |
99 | { | |
100 | uuid_le channel_type; | |
3703987c EA |
101 | int i; |
102 | struct visor_device *dev; | |
103 | struct visor_driver *drv; | |
104 | ||
105 | dev = to_visor_device(xdev); | |
106 | drv = to_visor_driver(xdrv); | |
107 | channel_type = visorchannel_get_uuid(dev->visorchannel); | |
3703987c EA |
108 | |
109 | if (!drv->channel_types) | |
8e33f48c DK |
110 | return 0; |
111 | ||
3703987c EA |
112 | for (i = 0; |
113 | (uuid_le_cmp(drv->channel_types[i].guid, NULL_UUID_LE) != 0) || | |
114 | (drv->channel_types[i].name); | |
115 | i++) | |
116 | if (uuid_le_cmp(drv->channel_types[i].guid, | |
8e33f48c DK |
117 | channel_type) == 0) |
118 | return i + 1; | |
119 | ||
120 | return 0; | |
3703987c EA |
121 | } |
122 | ||
7993b40c DK |
123 | /* |
124 | * This describes the TYPE of bus. | |
060b293b | 125 | * (Don't confuse this with an INSTANCE of the bus.) |
7993b40c DK |
126 | */ |
127 | struct bus_type visorbus_type = { | |
128 | .name = "visorbus", | |
129 | .match = visorbus_match, | |
130 | .uevent = visorbus_uevent, | |
131 | .dev_groups = visorbus_dev_groups, | |
7993b40c DK |
132 | }; |
133 | ||
b84be59a | 134 | /* |
ce4617ef DB |
135 | * visorbus_release_busdevice() - called when device_unregister() is called for |
136 | * the bus device instance, after all other tasks | |
137 | * involved with destroying the dev are complete | |
3fd1b3b6 | 138 | * @xdev: struct device for the bus being released |
3703987c EA |
139 | */ |
140 | static void | |
141 | visorbus_release_busdevice(struct device *xdev) | |
142 | { | |
343506bf | 143 | struct visor_device *dev = dev_get_drvdata(xdev); |
3703987c | 144 | |
8217becc TS |
145 | debugfs_remove(dev->debugfs_client_bus_info); |
146 | debugfs_remove_recursive(dev->debugfs_dir); | |
343506bf | 147 | kfree(dev); |
3703987c EA |
148 | } |
149 | ||
b84be59a | 150 | /* |
3fd1b3b6 DB |
151 | * visorbus_release_device() - called when device_unregister() is called for |
152 | * each child device instance | |
153 | * @xdev: struct device for the visor device being released | |
3703987c EA |
154 | */ |
155 | static void | |
156 | visorbus_release_device(struct device *xdev) | |
157 | { | |
158 | struct visor_device *dev = to_visor_device(xdev); | |
159 | ||
3703987c EA |
160 | if (dev->visorchannel) { |
161 | visorchannel_destroy(dev->visorchannel); | |
162 | dev->visorchannel = NULL; | |
163 | } | |
164 | kfree(dev); | |
165 | } | |
166 | ||
3fd1b3b6 DB |
167 | /* |
168 | * begin implementation of specific channel attributes to appear under | |
169 | * /sys/bus/visorbus<x>/dev<y>/channel | |
170 | */ | |
171 | ||
79573162 DZ |
172 | static ssize_t physaddr_show(struct device *dev, struct device_attribute *attr, |
173 | char *buf) | |
826b6a0f | 174 | { |
79573162 DZ |
175 | struct visor_device *vdev = to_visor_device(dev); |
176 | ||
7a41385e DB |
177 | return sprintf(buf, "0x%llx\n", |
178 | visorchannel_get_physaddr(vdev->visorchannel)); | |
826b6a0f | 179 | } |
a25280b3 | 180 | static DEVICE_ATTR_RO(physaddr); |
826b6a0f | 181 | |
79573162 DZ |
182 | static ssize_t nbytes_show(struct device *dev, struct device_attribute *attr, |
183 | char *buf) | |
826b6a0f | 184 | { |
79573162 DZ |
185 | struct visor_device *vdev = to_visor_device(dev); |
186 | ||
7a41385e | 187 | return sprintf(buf, "0x%lx\n", |
79573162 | 188 | visorchannel_get_nbytes(vdev->visorchannel)); |
826b6a0f | 189 | } |
a25280b3 | 190 | static DEVICE_ATTR_RO(nbytes); |
826b6a0f | 191 | |
79573162 DZ |
192 | static ssize_t clientpartition_show(struct device *dev, |
193 | struct device_attribute *attr, char *buf) | |
194 | { | |
195 | struct visor_device *vdev = to_visor_device(dev); | |
196 | ||
7a41385e DB |
197 | return sprintf(buf, "0x%llx\n", |
198 | visorchannel_get_clientpartition(vdev->visorchannel)); | |
826b6a0f | 199 | } |
a25280b3 | 200 | static DEVICE_ATTR_RO(clientpartition); |
826b6a0f | 201 | |
79573162 DZ |
202 | static ssize_t typeguid_show(struct device *dev, struct device_attribute *attr, |
203 | char *buf) | |
826b6a0f | 204 | { |
79573162 | 205 | struct visor_device *vdev = to_visor_device(dev); |
38d56c2f | 206 | char typeid[LINESIZE]; |
826b6a0f | 207 | |
7a41385e DB |
208 | return sprintf(buf, "%s\n", |
209 | visorchannel_id(vdev->visorchannel, typeid)); | |
826b6a0f | 210 | } |
a25280b3 | 211 | static DEVICE_ATTR_RO(typeguid); |
826b6a0f | 212 | |
79573162 DZ |
213 | static ssize_t zoneguid_show(struct device *dev, struct device_attribute *attr, |
214 | char *buf) | |
826b6a0f | 215 | { |
79573162 | 216 | struct visor_device *vdev = to_visor_device(dev); |
38d56c2f | 217 | char zoneid[LINESIZE]; |
826b6a0f | 218 | |
7a41385e DB |
219 | return sprintf(buf, "%s\n", |
220 | visorchannel_zoneid(vdev->visorchannel, zoneid)); | |
826b6a0f | 221 | } |
a25280b3 | 222 | static DEVICE_ATTR_RO(zoneguid); |
826b6a0f | 223 | |
79573162 DZ |
224 | static ssize_t typename_show(struct device *dev, struct device_attribute *attr, |
225 | char *buf) | |
826b6a0f PB |
226 | { |
227 | int i = 0; | |
79573162 DZ |
228 | struct bus_type *xbus = dev->bus; |
229 | struct device_driver *xdrv = dev->driver; | |
826b6a0f PB |
230 | struct visor_driver *drv = NULL; |
231 | ||
bf8d0e94 | 232 | if (!xbus || !xdrv) |
826b6a0f | 233 | return 0; |
79573162 | 234 | i = xbus->match(dev, xdrv); |
826b6a0f PB |
235 | if (!i) |
236 | return 0; | |
237 | drv = to_visor_driver(xdrv); | |
7a41385e | 238 | return sprintf(buf, "%s\n", drv->channel_types[i - 1].name); |
826b6a0f | 239 | } |
79573162 | 240 | static DEVICE_ATTR_RO(typename); |
79573162 DZ |
241 | |
242 | static struct attribute *channel_attrs[] = { | |
b372fee1 ZD |
243 | &dev_attr_physaddr.attr, |
244 | &dev_attr_nbytes.attr, | |
245 | &dev_attr_clientpartition.attr, | |
246 | &dev_attr_typeguid.attr, | |
247 | &dev_attr_zoneguid.attr, | |
248 | &dev_attr_typename.attr, | |
249 | NULL | |
826b6a0f PB |
250 | }; |
251 | ||
79573162 | 252 | static struct attribute_group channel_attr_grp = { |
b372fee1 ZD |
253 | .name = "channel", |
254 | .attrs = channel_attrs, | |
826b6a0f PB |
255 | }; |
256 | ||
59fd2c8b | 257 | static const struct attribute_group *visorbus_channel_groups[] = { |
b372fee1 ZD |
258 | &channel_attr_grp, |
259 | NULL | |
826b6a0f PB |
260 | }; |
261 | ||
79573162 | 262 | /* end implementation of specific channel attributes */ |
826b6a0f | 263 | |
3fd1b3b6 DB |
264 | /* |
265 | * BUS instance attributes | |
3703987c EA |
266 | * |
267 | * define & implement display of bus attributes under | |
3fd1b3b6 | 268 | * /sys/bus/visorbus/devices/visorbus<n>. |
3703987c EA |
269 | */ |
270 | ||
d181dd03 DZ |
271 | static ssize_t partition_handle_show(struct device *dev, |
272 | struct device_attribute *attr, | |
7b9de71b EA |
273 | char *buf) |
274 | { | |
5ecbd5d4 DZ |
275 | struct visor_device *vdev = to_visor_device(dev); |
276 | u64 handle = visorchannel_get_clientpartition(vdev->visorchannel); | |
3703987c | 277 | |
7a41385e | 278 | return sprintf(buf, "0x%llx\n", handle); |
3703987c | 279 | } |
a25280b3 | 280 | static DEVICE_ATTR_RO(partition_handle); |
3703987c | 281 | |
d181dd03 DZ |
282 | static ssize_t partition_guid_show(struct device *dev, |
283 | struct device_attribute *attr, | |
7b9de71b EA |
284 | char *buf) |
285 | { | |
5ecbd5d4 | 286 | struct visor_device *vdev = to_visor_device(dev); |
3703987c | 287 | |
7a41385e | 288 | return sprintf(buf, "{%pUb}\n", &vdev->partition_uuid); |
3703987c | 289 | } |
a25280b3 | 290 | static DEVICE_ATTR_RO(partition_guid); |
3703987c | 291 | |
d181dd03 DZ |
292 | static ssize_t partition_name_show(struct device *dev, |
293 | struct device_attribute *attr, | |
7b9de71b EA |
294 | char *buf) |
295 | { | |
5ecbd5d4 | 296 | struct visor_device *vdev = to_visor_device(dev); |
3703987c | 297 | |
7a41385e | 298 | return sprintf(buf, "%s\n", vdev->name); |
3703987c | 299 | } |
a25280b3 | 300 | static DEVICE_ATTR_RO(partition_name); |
3703987c | 301 | |
d181dd03 DZ |
302 | static ssize_t channel_addr_show(struct device *dev, |
303 | struct device_attribute *attr, | |
7b9de71b EA |
304 | char *buf) |
305 | { | |
5ecbd5d4 DZ |
306 | struct visor_device *vdev = to_visor_device(dev); |
307 | u64 addr = visorchannel_get_physaddr(vdev->visorchannel); | |
3703987c | 308 | |
7a41385e | 309 | return sprintf(buf, "0x%llx\n", addr); |
3703987c | 310 | } |
a25280b3 | 311 | static DEVICE_ATTR_RO(channel_addr); |
3703987c | 312 | |
d181dd03 DZ |
313 | static ssize_t channel_bytes_show(struct device *dev, |
314 | struct device_attribute *attr, | |
7b9de71b EA |
315 | char *buf) |
316 | { | |
5ecbd5d4 DZ |
317 | struct visor_device *vdev = to_visor_device(dev); |
318 | u64 nbytes = visorchannel_get_nbytes(vdev->visorchannel); | |
3703987c | 319 | |
7a41385e | 320 | return sprintf(buf, "0x%llx\n", nbytes); |
3703987c | 321 | } |
a25280b3 | 322 | static DEVICE_ATTR_RO(channel_bytes); |
3703987c | 323 | |
d181dd03 DZ |
324 | static ssize_t channel_id_show(struct device *dev, |
325 | struct device_attribute *attr, | |
7b9de71b EA |
326 | char *buf) |
327 | { | |
5ecbd5d4 | 328 | struct visor_device *vdev = to_visor_device(dev); |
3703987c EA |
329 | int len = 0; |
330 | ||
bf8d0e94 DB |
331 | visorchannel_id(vdev->visorchannel, buf); |
332 | len = strlen(buf); | |
333 | buf[len++] = '\n'; | |
334 | ||
3703987c EA |
335 | return len; |
336 | } | |
a25280b3 | 337 | static DEVICE_ATTR_RO(channel_id); |
3703987c | 338 | |
d181dd03 | 339 | static struct attribute *dev_attrs[] = { |
b372fee1 ZD |
340 | &dev_attr_partition_handle.attr, |
341 | &dev_attr_partition_guid.attr, | |
342 | &dev_attr_partition_name.attr, | |
343 | &dev_attr_channel_addr.attr, | |
344 | &dev_attr_channel_bytes.attr, | |
345 | &dev_attr_channel_id.attr, | |
346 | NULL | |
d181dd03 | 347 | }; |
3703987c | 348 | |
d181dd03 | 349 | static struct attribute_group dev_attr_grp = { |
b372fee1 | 350 | .attrs = dev_attrs, |
d181dd03 | 351 | }; |
3703987c | 352 | |
d181dd03 | 353 | static const struct attribute_group *visorbus_groups[] = { |
b372fee1 ZD |
354 | &dev_attr_grp, |
355 | NULL | |
d181dd03 | 356 | }; |
3703987c | 357 | |
8217becc TS |
358 | /* |
359 | * BUS debugfs entries | |
360 | * | |
361 | * define & implement display of debugfs attributes under | |
362 | * /sys/kernel/debug/visorbus/visorbus<n>. | |
363 | */ | |
738561a8 | 364 | /* |
55c71eba | 365 | * vbuschannel_print_devinfo() - format a struct visor_vbus_deviceinfo |
738561a8 | 366 | * and write it to a seq_file |
55c71eba | 367 | * @devinfo: the struct visor_vbus_deviceinfo to format |
738561a8 DK |
368 | * @seq: seq_file to write to |
369 | * @devix: the device index to be included in the output data, or -1 if no | |
370 | * device index is to be included | |
371 | * | |
372 | * Reads @devInfo, and writes it in human-readable notation to @seq. | |
373 | */ | |
374 | static void | |
55c71eba | 375 | vbuschannel_print_devinfo(struct visor_vbus_deviceinfo *devinfo, |
738561a8 DK |
376 | struct seq_file *seq, int devix) |
377 | { | |
060b293b | 378 | /* uninitialized vbus device entry */ |
738561a8 | 379 | if (!isprint(devinfo->devtype[0])) |
060b293b | 380 | return; |
738561a8 DK |
381 | |
382 | if (devix >= 0) | |
383 | seq_printf(seq, "[%d]", devix); | |
384 | else | |
385 | /* vbus device entry is for bus or chipset */ | |
386 | seq_puts(seq, " "); | |
387 | ||
388 | /* | |
389 | * Note: because the s-Par back-end is free to scribble in this area, | |
390 | * we never assume '\0'-termination. | |
391 | */ | |
392 | seq_printf(seq, "%-*.*s ", (int)sizeof(devinfo->devtype), | |
393 | (int)sizeof(devinfo->devtype), devinfo->devtype); | |
394 | seq_printf(seq, "%-*.*s ", (int)sizeof(devinfo->drvname), | |
395 | (int)sizeof(devinfo->drvname), devinfo->drvname); | |
396 | seq_printf(seq, "%.*s\n", (int)sizeof(devinfo->infostrs), | |
397 | devinfo->infostrs); | |
398 | } | |
8217becc TS |
399 | |
400 | static int client_bus_info_debugfs_show(struct seq_file *seq, void *v) | |
401 | { | |
402 | struct visor_device *vdev = seq->private; | |
403 | struct visorchannel *channel = vdev->visorchannel; | |
404 | ||
405 | int i; | |
406 | unsigned long off; | |
55c71eba | 407 | struct visor_vbus_deviceinfo dev_info; |
8217becc TS |
408 | |
409 | if (!channel) | |
410 | return 0; | |
411 | ||
412 | seq_printf(seq, | |
413 | "Client device / client driver info for %s partition (vbus #%u):\n", | |
414 | ((vdev->name) ? (char *)(vdev->name) : ""), | |
415 | vdev->chipset_bus_no); | |
416 | if (visorchannel_read(channel, | |
55c71eba | 417 | offsetof(struct visor_vbus_channel, chp_info), |
8217becc TS |
418 | &dev_info, sizeof(dev_info)) >= 0) |
419 | vbuschannel_print_devinfo(&dev_info, seq, -1); | |
420 | if (visorchannel_read(channel, | |
55c71eba | 421 | offsetof(struct visor_vbus_channel, bus_info), |
8217becc TS |
422 | &dev_info, sizeof(dev_info)) >= 0) |
423 | vbuschannel_print_devinfo(&dev_info, seq, -1); | |
55c71eba | 424 | off = offsetof(struct visor_vbus_channel, dev_info); |
8217becc TS |
425 | i = 0; |
426 | while (off + sizeof(dev_info) <= visorchannel_get_nbytes(channel)) { | |
427 | if (visorchannel_read(channel, off, &dev_info, | |
428 | sizeof(dev_info)) >= 0) | |
429 | vbuschannel_print_devinfo(&dev_info, seq, i); | |
430 | off += sizeof(dev_info); | |
431 | i++; | |
432 | } | |
433 | ||
434 | return 0; | |
435 | } | |
436 | ||
437 | static int client_bus_info_debugfs_open(struct inode *inode, struct file *file) | |
438 | { | |
439 | return single_open(file, client_bus_info_debugfs_show, | |
440 | inode->i_private); | |
441 | } | |
442 | ||
443 | static const struct file_operations client_bus_info_debugfs_fops = { | |
444 | .owner = THIS_MODULE, | |
445 | .open = client_bus_info_debugfs_open, | |
446 | .read = seq_read, | |
447 | .llseek = seq_lseek, | |
448 | .release = single_release, | |
449 | }; | |
450 | ||
3703987c | 451 | static void |
9ebab649 | 452 | dev_periodic_work(unsigned long __opaque) |
3703987c | 453 | { |
9ebab649 | 454 | struct visor_device *dev = (struct visor_device *)__opaque; |
3703987c EA |
455 | struct visor_driver *drv = to_visor_driver(dev->device.driver); |
456 | ||
396e36c9 | 457 | drv->channel_interrupt(dev); |
9ebab649 | 458 | mod_timer(&dev->timer, jiffies + POLLJIFFIES_NORMALCHANNEL); |
3703987c EA |
459 | } |
460 | ||
b90194d9 | 461 | static int |
3703987c EA |
462 | dev_start_periodic_work(struct visor_device *dev) |
463 | { | |
9ebab649 | 464 | if (dev->being_removed || dev->timer_active) |
b90194d9 | 465 | return -EINVAL; |
3703987c EA |
466 | /* now up by at least 2 */ |
467 | get_device(&dev->device); | |
9ebab649 TS |
468 | dev->timer.expires = jiffies + POLLJIFFIES_NORMALCHANNEL; |
469 | add_timer(&dev->timer); | |
470 | dev->timer_active = true; | |
b90194d9 | 471 | return 0; |
3703987c EA |
472 | } |
473 | ||
474 | static void | |
475 | dev_stop_periodic_work(struct visor_device *dev) | |
476 | { | |
9ebab649 TS |
477 | if (!dev->timer_active) |
478 | return; | |
479 | del_timer_sync(&dev->timer); | |
480 | dev->timer_active = false; | |
481 | put_device(&dev->device); | |
3703987c EA |
482 | } |
483 | ||
b84be59a | 484 | /* |
3fd1b3b6 DB |
485 | * visordriver_remove_device() - handle visor device going away |
486 | * @xdev: struct device for the visor device being removed | |
487 | * | |
488 | * This is called when device_unregister() is called for each child device | |
489 | * instance, to notify the appropriate visorbus function driver that the device | |
490 | * is going away, and to decrease the reference count of the device. | |
491 | * | |
492 | * Return: 0 iff successful | |
3703987c EA |
493 | */ |
494 | static int | |
495 | visordriver_remove_device(struct device *xdev) | |
496 | { | |
3703987c EA |
497 | struct visor_device *dev; |
498 | struct visor_driver *drv; | |
499 | ||
500 | dev = to_visor_device(xdev); | |
501 | drv = to_visor_driver(xdev->driver); | |
d505855e | 502 | mutex_lock(&dev->visordriver_callback_lock); |
779d0752 | 503 | dev->being_removed = true; |
64938182 DK |
504 | if (drv->remove) |
505 | drv->remove(dev); | |
d505855e | 506 | mutex_unlock(&dev->visordriver_callback_lock); |
3703987c | 507 | dev_stop_periodic_work(dev); |
3703987c EA |
508 | |
509 | put_device(&dev->device); | |
df7f46e8 | 510 | return 0; |
3703987c EA |
511 | } |
512 | ||
060b293b | 513 | /* |
3fd1b3b6 DB |
514 | * visorbus_unregister_visor_driver() - unregisters the provided driver |
515 | * @drv: the driver to unregister | |
516 | * | |
517 | * A visor function driver calls this function to unregister the driver, | |
518 | * i.e., within its module_exit function. | |
3703987c EA |
519 | */ |
520 | void | |
521 | visorbus_unregister_visor_driver(struct visor_driver *drv) | |
522 | { | |
3703987c EA |
523 | driver_unregister(&drv->driver); |
524 | } | |
525 | EXPORT_SYMBOL_GPL(visorbus_unregister_visor_driver); | |
526 | ||
060b293b | 527 | /* |
3fd1b3b6 DB |
528 | * visorbus_read_channel() - reads from the designated channel into |
529 | * the provided buffer | |
530 | * @dev: the device whose channel is read from | |
531 | * @offset: the offset into the channel at which reading starts | |
532 | * @dest: the destination buffer that is written into from the channel | |
533 | * @nbytes: the number of bytes to read from the channel | |
534 | * | |
535 | * If receiving a message, use the visorchannel_signalremove() | |
536 | * function instead. | |
537 | * | |
538 | * Return: integer indicating success (zero) or failure (non-zero) | |
539 | */ | |
3703987c EA |
540 | int |
541 | visorbus_read_channel(struct visor_device *dev, unsigned long offset, | |
542 | void *dest, unsigned long nbytes) | |
543 | { | |
544 | return visorchannel_read(dev->visorchannel, offset, dest, nbytes); | |
545 | } | |
546 | EXPORT_SYMBOL_GPL(visorbus_read_channel); | |
547 | ||
060b293b | 548 | /* |
3fd1b3b6 DB |
549 | * visorbus_write_channel() - writes the provided buffer into the designated |
550 | * channel | |
551 | * @dev: the device whose channel is written to | |
552 | * @offset: the offset into the channel at which writing starts | |
553 | * @src: the source buffer that is written into the channel | |
554 | * @nbytes: the number of bytes to write into the channel | |
555 | * | |
556 | * If sending a message, use the visorchannel_signalinsert() | |
557 | * function instead. | |
558 | * | |
559 | * Return: integer indicating success (zero) or failure (non-zero) | |
560 | */ | |
3703987c EA |
561 | int |
562 | visorbus_write_channel(struct visor_device *dev, unsigned long offset, | |
563 | void *src, unsigned long nbytes) | |
564 | { | |
565 | return visorchannel_write(dev->visorchannel, offset, src, nbytes); | |
566 | } | |
567 | EXPORT_SYMBOL_GPL(visorbus_write_channel); | |
568 | ||
060b293b | 569 | /* |
3fd1b3b6 DB |
570 | * visorbus_enable_channel_interrupts() - enables interrupts on the |
571 | * designated device | |
572 | * @dev: the device on which to enable interrupts | |
573 | * | |
574 | * Currently we don't yet have a real interrupt, so for now we just call the | |
575 | * interrupt function periodically via a timer. | |
3703987c | 576 | */ |
5dca9b29 | 577 | int |
3703987c EA |
578 | visorbus_enable_channel_interrupts(struct visor_device *dev) |
579 | { | |
396e36c9 TS |
580 | struct visor_driver *drv = to_visor_driver(dev->device.driver); |
581 | ||
582 | if (!drv->channel_interrupt) { | |
583 | dev_err(&dev->device, "%s no interrupt function!\n", __func__); | |
5dca9b29 | 584 | return -ENOENT; |
396e36c9 TS |
585 | } |
586 | ||
5dca9b29 | 587 | return dev_start_periodic_work(dev); |
3703987c EA |
588 | } |
589 | EXPORT_SYMBOL_GPL(visorbus_enable_channel_interrupts); | |
590 | ||
060b293b | 591 | /* |
3fd1b3b6 DB |
592 | * visorbus_disable_channel_interrupts() - disables interrupts on the |
593 | * designated device | |
594 | * @dev: the device on which to disable interrupts | |
595 | */ | |
3703987c EA |
596 | void |
597 | visorbus_disable_channel_interrupts(struct visor_device *dev) | |
598 | { | |
599 | dev_stop_periodic_work(dev); | |
600 | } | |
601 | EXPORT_SYMBOL_GPL(visorbus_disable_channel_interrupts); | |
602 | ||
b84be59a | 603 | /* |
3fd1b3b6 DB |
604 | * create_visor_device() - create visor device as a result of receiving the |
605 | * controlvm device_create message for a new device | |
606 | * @dev: a freshly-zeroed struct visor_device, containing only filled-in values | |
607 | * for chipset_bus_no and chipset_dev_no, that will be initialized | |
608 | * | |
609 | * This is how everything starts from the device end. | |
610 | * This function is called when a channel first appears via a ControlVM | |
611 | * message. In response, this function allocates a visor_device to | |
612 | * correspond to the new channel, and attempts to connect it the appropriate | |
613 | * driver. If the appropriate driver is found, the visor_driver.probe() | |
614 | * function for that driver will be called, and will be passed the new | |
615 | * visor_device that we just created. | |
3703987c | 616 | * |
3fd1b3b6 DB |
617 | * It's ok if the appropriate driver is not yet loaded, because in that case |
618 | * the new device struct will just stick around in the bus' list of devices. | |
619 | * When the appropriate driver calls visorbus_register_visor_driver(), the | |
620 | * visor_driver.probe() for the new driver will be called with the new | |
621 | * device. | |
622 | * | |
623 | * Return: 0 if successful, otherwise the negative value returned by | |
624 | * device_add() indicating the reason for failure | |
3703987c EA |
625 | */ |
626 | static int | |
a298bc0b | 627 | create_visor_device(struct visor_device *dev) |
3703987c | 628 | { |
0b2acf34 | 629 | int err; |
a298bc0b DZ |
630 | u32 chipset_bus_no = dev->chipset_bus_no; |
631 | u32 chipset_dev_no = dev->chipset_dev_no; | |
3703987c | 632 | |
d505855e | 633 | mutex_init(&dev->visordriver_callback_lock); |
3703987c | 634 | dev->device.bus = &visorbus_type; |
59fd2c8b | 635 | dev->device.groups = visorbus_channel_groups; |
3703987c EA |
636 | device_initialize(&dev->device); |
637 | dev->device.release = visorbus_release_device; | |
638 | /* keep a reference just for us (now 2) */ | |
639 | get_device(&dev->device); | |
614dd644 | 640 | setup_timer(&dev->timer, dev_periodic_work, (unsigned long)dev); |
3703987c | 641 | |
3fd1b3b6 DB |
642 | /* |
643 | * bus_id must be a unique name with respect to this bus TYPE | |
3703987c EA |
644 | * (NOT bus instance). That's why we need to include the bus |
645 | * number within the name. | |
646 | */ | |
e0d210ae DK |
647 | err = dev_set_name(&dev->device, "vbus%u:dev%u", |
648 | chipset_bus_no, chipset_dev_no); | |
649 | if (err) | |
650 | goto err_put; | |
3703987c | 651 | |
3fd1b3b6 DB |
652 | /* |
653 | * device_add does this: | |
3703987c EA |
654 | * bus_add_device(dev) |
655 | * ->device_attach(dev) | |
656 | * ->for each driver drv registered on the bus that dev is on | |
657 | * if (dev.drv) ** device already has a driver ** | |
658 | * ** not sure we could ever get here... ** | |
659 | * else | |
660 | * if (bus.match(dev,drv)) [visorbus_match] | |
661 | * dev.drv = drv | |
662 | * if (!drv.probe(dev)) [visordriver_probe_device] | |
663 | * dev.drv = NULL | |
664 | * | |
665 | * Note that device_add does NOT fail if no driver failed to | |
666 | * claim the device. The device will be linked onto | |
667 | * bus_type.klist_devices regardless (use bus_for_each_dev). | |
668 | */ | |
0b2acf34 | 669 | err = device_add(&dev->device); |
ebdc1b88 | 670 | if (err < 0) |
0b2acf34 | 671 | goto err_put; |
3703987c | 672 | |
a298bc0b | 673 | list_add_tail(&dev->list_all, &list_all_device_instances); |
060b293b SW |
674 | /* success: reference kept via unmatched get_device() */ |
675 | return 0; | |
3703987c | 676 | |
0b2acf34 | 677 | err_put: |
a298bc0b | 678 | put_device(&dev->device); |
ebdc1b88 | 679 | dev_err(&dev->device, "Creating visor device failed. %d\n", err); |
0b2acf34 | 680 | return err; |
3703987c EA |
681 | } |
682 | ||
683 | static void | |
684 | remove_visor_device(struct visor_device *dev) | |
685 | { | |
686 | list_del(&dev->list_all); | |
3703987c EA |
687 | put_device(&dev->device); |
688 | device_unregister(&dev->device); | |
689 | } | |
690 | ||
3703987c EA |
691 | static int |
692 | get_vbus_header_info(struct visorchannel *chan, | |
55c71eba | 693 | struct visor_vbus_headerinfo *hdr_info) |
3703987c | 694 | { |
711c67f4 DK |
695 | int err; |
696 | ||
315dfc84 SW |
697 | if (!visor_check_channel(visorchannel_get_header(chan), |
698 | visor_vbus_channel_uuid, | |
699 | "vbus", | |
700 | sizeof(struct visor_vbus_channel), | |
701 | VISOR_VBUS_CHANNEL_VERSIONID, | |
702 | VISOR_VBUS_CHANNEL_SIGNATURE)) | |
f748f64f BR |
703 | return -EINVAL; |
704 | ||
711c67f4 DK |
705 | err = visorchannel_read(chan, sizeof(struct channel_header), hdr_info, |
706 | sizeof(*hdr_info)); | |
707 | if (err < 0) | |
708 | return err; | |
709 | ||
55c71eba | 710 | if (hdr_info->struct_bytes < sizeof(struct visor_vbus_headerinfo)) |
f748f64f BR |
711 | return -EINVAL; |
712 | ||
3703987c | 713 | if (hdr_info->device_info_struct_bytes < |
55c71eba | 714 | sizeof(struct visor_vbus_deviceinfo)) |
f748f64f | 715 | return -EINVAL; |
493d1597 | 716 | |
f748f64f | 717 | return 0; |
3703987c EA |
718 | } |
719 | ||
b84be59a | 720 | /* |
3fd1b3b6 | 721 | * write_vbus_chp_info() - write the contents of <info> to the struct |
55c71eba | 722 | * visor_vbus_channel.chp_info |
3fd1b3b6 DB |
723 | * @chan: indentifies the s-Par channel that will be updated |
724 | * @hdr_info: used to find appropriate channel offset to write data | |
725 | * @info: contains the information to write | |
726 | * | |
727 | * Writes chipset info into the channel memory to be used for diagnostic | |
728 | * purposes. | |
ff13cf37 | 729 | * |
3fd1b3b6 | 730 | * Returns no value since this is debug information and not needed for |
ff13cf37 | 731 | * device functionality. |
48117895 | 732 | */ |
ff13cf37 | 733 | static void |
3703987c | 734 | write_vbus_chp_info(struct visorchannel *chan, |
55c71eba SW |
735 | struct visor_vbus_headerinfo *hdr_info, |
736 | struct visor_vbus_deviceinfo *info) | |
3703987c EA |
737 | { |
738 | int off = sizeof(struct channel_header) + hdr_info->chp_info_offset; | |
739 | ||
740 | if (hdr_info->chp_info_offset == 0) | |
ff13cf37 | 741 | return; |
3703987c | 742 | |
ff13cf37 | 743 | visorchannel_write(chan, off, info, sizeof(*info)); |
3703987c EA |
744 | } |
745 | ||
b84be59a | 746 | /* |
3fd1b3b6 | 747 | * write_vbus_bus_info() - write the contents of <info> to the struct |
55c71eba | 748 | * visor_vbus_channel.bus_info |
3fd1b3b6 DB |
749 | * @chan: indentifies the s-Par channel that will be updated |
750 | * @hdr_info: used to find appropriate channel offset to write data | |
751 | * @info: contains the information to write | |
752 | * | |
753 | * Writes bus info into the channel memory to be used for diagnostic | |
754 | * purposes. | |
ff13cf37 | 755 | * |
3fd1b3b6 | 756 | * Returns no value since this is debug information and not needed for |
ff13cf37 | 757 | * device functionality. |
48117895 | 758 | */ |
ff13cf37 | 759 | static void |
3703987c | 760 | write_vbus_bus_info(struct visorchannel *chan, |
55c71eba SW |
761 | struct visor_vbus_headerinfo *hdr_info, |
762 | struct visor_vbus_deviceinfo *info) | |
3703987c EA |
763 | { |
764 | int off = sizeof(struct channel_header) + hdr_info->bus_info_offset; | |
765 | ||
766 | if (hdr_info->bus_info_offset == 0) | |
ff13cf37 | 767 | return; |
3703987c | 768 | |
ff13cf37 | 769 | visorchannel_write(chan, off, info, sizeof(*info)); |
3703987c EA |
770 | } |
771 | ||
b84be59a | 772 | /* |
3fd1b3b6 | 773 | * write_vbus_dev_info() - write the contents of <info> to the struct |
55c71eba | 774 | * visor_vbus_channel.dev_info[<devix>] |
3fd1b3b6 DB |
775 | * @chan: indentifies the s-Par channel that will be updated |
776 | * @hdr_info: used to find appropriate channel offset to write data | |
777 | * @info: contains the information to write | |
778 | * @devix: the relative device number (0..n-1) of the device on the bus | |
ff13cf37 | 779 | * |
3fd1b3b6 DB |
780 | * Writes device info into the channel memory to be used for diagnostic |
781 | * purposes. | |
782 | * | |
783 | * Returns no value since this is debug information and not needed for | |
ff13cf37 | 784 | * device functionality. |
3703987c | 785 | */ |
ff13cf37 | 786 | static void |
3703987c | 787 | write_vbus_dev_info(struct visorchannel *chan, |
55c71eba SW |
788 | struct visor_vbus_headerinfo *hdr_info, |
789 | struct visor_vbus_deviceinfo *info, unsigned int devix) | |
3703987c EA |
790 | { |
791 | int off = | |
792 | (sizeof(struct channel_header) + hdr_info->dev_info_offset) + | |
793 | (hdr_info->device_info_struct_bytes * devix); | |
794 | ||
795 | if (hdr_info->dev_info_offset == 0) | |
ff13cf37 | 796 | return; |
3703987c | 797 | |
ff13cf37 | 798 | visorchannel_write(chan, off, info, sizeof(*info)); |
3703987c EA |
799 | } |
800 | ||
b5577d79 | 801 | static void bus_device_info_init( |
55c71eba | 802 | struct visor_vbus_deviceinfo *bus_device_info_ptr, |
b5577d79 DK |
803 | const char *dev_type, const char *drv_name) |
804 | { | |
55c71eba | 805 | memset(bus_device_info_ptr, 0, sizeof(struct visor_vbus_deviceinfo)); |
b5577d79 DK |
806 | snprintf(bus_device_info_ptr->devtype, |
807 | sizeof(bus_device_info_ptr->devtype), | |
808 | "%s", (dev_type) ? dev_type : "unknownType"); | |
809 | snprintf(bus_device_info_ptr->drvname, | |
810 | sizeof(bus_device_info_ptr->drvname), | |
811 | "%s", (drv_name) ? drv_name : "unknownDriver"); | |
812 | snprintf(bus_device_info_ptr->infostrs, | |
813 | sizeof(bus_device_info_ptr->infostrs), "kernel ver. %s", | |
814 | utsname()->release); | |
815 | } | |
816 | ||
b84be59a | 817 | /* |
3fd1b3b6 DB |
818 | * fix_vbus_dev_info() - for a child device just created on a client bus, fill |
819 | * in information about the driver that is controlling | |
bffa320f | 820 | * this device into the appropriate slot within the |
3fd1b3b6 DB |
821 | * vbus channel of the bus instance |
822 | * @visordev: struct visor_device for the desired device | |
3703987c EA |
823 | */ |
824 | static void | |
825 | fix_vbus_dev_info(struct visor_device *visordev) | |
826 | { | |
827 | int i; | |
d32517e3 | 828 | struct visor_device *bdev; |
3703987c | 829 | struct visor_driver *visordrv; |
f7a34ff7 TS |
830 | u32 bus_no = visordev->chipset_bus_no; |
831 | u32 dev_no = visordev->chipset_dev_no; | |
55c71eba | 832 | struct visor_vbus_deviceinfo dev_info; |
3703987c | 833 | const char *chan_type_name = NULL; |
55c71eba | 834 | struct visor_vbus_headerinfo *hdr_info; |
3703987c EA |
835 | |
836 | if (!visordev->device.driver) | |
216c3e2c | 837 | return; |
3703987c | 838 | |
d32517e3 DZ |
839 | bdev = visorbus_get_device_by_id(bus_no, BUS_ROOT_DEVICE, NULL); |
840 | if (!bdev) | |
841 | return; | |
55c71eba | 842 | hdr_info = (struct visor_vbus_headerinfo *)bdev->vbus_hdr_info; |
5990b39e TS |
843 | if (!hdr_info) |
844 | return; | |
d32517e3 | 845 | visordrv = to_visor_driver(visordev->device.driver); |
3703987c | 846 | |
3fd1b3b6 DB |
847 | /* |
848 | * Within the list of device types (by GUID) that the driver | |
3703987c EA |
849 | * says it supports, find out which one of those types matches |
850 | * the type of this device, so that we can include the device | |
851 | * type name | |
852 | */ | |
853 | for (i = 0; visordrv->channel_types[i].name; i++) { | |
d5b3f1dc EA |
854 | if (memcmp(&visordrv->channel_types[i].guid, |
855 | &visordev->channel_type_guid, | |
856 | sizeof(visordrv->channel_types[i].guid)) == 0) { | |
3703987c EA |
857 | chan_type_name = visordrv->channel_types[i].name; |
858 | break; | |
859 | } | |
860 | } | |
861 | ||
e82ed633 | 862 | bus_device_info_init(&dev_info, chan_type_name, visordrv->name); |
d32517e3 | 863 | write_vbus_dev_info(bdev->visorchannel, hdr_info, &dev_info, dev_no); |
3703987c | 864 | |
d32517e3 DZ |
865 | write_vbus_chp_info(bdev->visorchannel, hdr_info, &chipset_driverinfo); |
866 | write_vbus_bus_info(bdev->visorchannel, hdr_info, | |
867 | &clientbus_driverinfo); | |
3703987c EA |
868 | } |
869 | ||
b84be59a | 870 | /* |
7a0ee694 DK |
871 | * visordriver_probe_device() - handle new visor device coming online |
872 | * @xdev: struct device for the visor device being probed | |
873 | * | |
874 | * This is called automatically upon adding a visor_device (device_add), or | |
875 | * adding a visor_driver (visorbus_register_visor_driver), but only after | |
876 | * visorbus_match() has returned 1 to indicate a successful match between | |
877 | * driver and device. | |
878 | * | |
879 | * If successful, a reference to the device will be held onto via get_device(). | |
880 | * | |
881 | * Return: 0 if successful, meaning the function driver's probe() function | |
882 | * was successful with this device, otherwise a negative errno | |
883 | * value indicating failure reason | |
884 | */ | |
885 | static int | |
886 | visordriver_probe_device(struct device *xdev) | |
887 | { | |
888 | int res; | |
889 | struct visor_driver *drv; | |
890 | struct visor_device *dev; | |
891 | ||
892 | drv = to_visor_driver(xdev->driver); | |
893 | dev = to_visor_device(xdev); | |
894 | ||
895 | if (!drv->probe) | |
896 | return -ENODEV; | |
897 | ||
898 | mutex_lock(&dev->visordriver_callback_lock); | |
899 | dev->being_removed = false; | |
900 | ||
901 | res = drv->probe(dev); | |
902 | if (res >= 0) { | |
903 | /* success: reference kept via unmatched get_device() */ | |
904 | get_device(&dev->device); | |
905 | fix_vbus_dev_info(dev); | |
906 | } | |
907 | ||
908 | mutex_unlock(&dev->visordriver_callback_lock); | |
909 | return res; | |
910 | } | |
911 | ||
060b293b | 912 | /* |
7a0ee694 DK |
913 | * visorbus_register_visor_driver() - registers the provided visor driver |
914 | * for handling one or more visor device | |
915 | * types (channel_types) | |
916 | * @drv: the driver to register | |
917 | * | |
918 | * A visor function driver calls this function to register | |
919 | * the driver. The caller MUST fill in the following fields within the | |
920 | * #drv structure: | |
921 | * name, version, owner, channel_types, probe, remove | |
922 | * | |
923 | * Here's how the whole Linux bus / driver / device model works. | |
924 | * | |
925 | * At system start-up, the visorbus kernel module is loaded, which registers | |
926 | * visorbus_type as a bus type, using bus_register(). | |
927 | * | |
928 | * All kernel modules that support particular device types on a | |
929 | * visorbus bus are loaded. Each of these kernel modules calls | |
930 | * visorbus_register_visor_driver() in their init functions, passing a | |
931 | * visor_driver struct. visorbus_register_visor_driver() in turn calls | |
932 | * register_driver(&visor_driver.driver). This .driver member is | |
933 | * initialized with generic methods (like probe), whose sole responsibility | |
934 | * is to act as a broker for the real methods, which are within the | |
935 | * visor_driver struct. (This is the way the subclass behavior is | |
936 | * implemented, since visor_driver is essentially a subclass of the | |
937 | * generic driver.) Whenever a driver_register() happens, core bus code in | |
938 | * the kernel does (see device_attach() in drivers/base/dd.c): | |
939 | * | |
940 | * for each dev associated with the bus (the bus that driver is on) that | |
941 | * does not yet have a driver | |
942 | * if bus.match(dev,newdriver) == yes_matched ** .match specified | |
943 | * ** during bus_register(). | |
944 | * newdriver.probe(dev) ** for visor drivers, this will call | |
945 | * ** the generic driver.probe implemented in visorbus.c, | |
946 | * ** which in turn calls the probe specified within the | |
947 | * ** struct visor_driver (which was specified by the | |
948 | * ** actual device driver as part of | |
949 | * ** visorbus_register_visor_driver()). | |
950 | * | |
951 | * The above dance also happens when a new device appears. | |
952 | * So the question is, how are devices created within the system? | |
953 | * Basically, just call device_add(dev). See pci_bus_add_devices(). | |
954 | * pci_scan_device() shows an example of how to build a device struct. It | |
955 | * returns the newly-created struct to pci_scan_single_device(), who adds it | |
956 | * to the list of devices at PCIBUS.devices. That list of devices is what | |
957 | * is traversed by pci_bus_add_devices(). | |
958 | * | |
959 | * Return: integer indicating success (zero) or failure (non-zero) | |
960 | */ | |
961 | int visorbus_register_visor_driver(struct visor_driver *drv) | |
962 | { | |
060b293b | 963 | /* can't register on a nonexistent bus */ |
b0512faf | 964 | if (!initialized) |
060b293b | 965 | return -ENODEV; |
b0512faf | 966 | |
7a0ee694 DK |
967 | drv->driver.name = drv->name; |
968 | drv->driver.bus = &visorbus_type; | |
969 | drv->driver.probe = visordriver_probe_device; | |
970 | drv->driver.remove = visordriver_remove_device; | |
971 | drv->driver.owner = drv->owner; | |
972 | ||
973 | /* | |
974 | * driver_register does this: | |
975 | * bus_add_driver(drv) | |
976 | * ->if (drv.bus) ** (bus_type) ** | |
977 | * driver_attach(drv) | |
978 | * for each dev with bus type of drv.bus | |
979 | * if (!dev.drv) ** no driver assigned yet ** | |
980 | * if (bus.match(dev,drv)) [visorbus_match] | |
981 | * dev.drv = drv | |
982 | * if (!drv.probe(dev)) [visordriver_probe_device] | |
983 | * dev.drv = NULL | |
984 | */ | |
985 | ||
293deb2c | 986 | return driver_register(&drv->driver); |
7a0ee694 DK |
987 | } |
988 | EXPORT_SYMBOL_GPL(visorbus_register_visor_driver); | |
989 | ||
b84be59a | 990 | /* |
9e78fd35 | 991 | * visorbus_create_instance() - create a device instance for the visorbus itself |
3fd1b3b6 DB |
992 | * @dev: struct visor_device indicating the bus instance |
993 | * | |
994 | * Return: 0 for success, otherwise negative errno value indicating reason for | |
995 | * failure | |
3703987c | 996 | */ |
d32517e3 | 997 | static int |
9e78fd35 | 998 | visorbus_create_instance(struct visor_device *dev) |
3703987c | 999 | { |
d32517e3 | 1000 | int id = dev->chipset_bus_no; |
8217becc | 1001 | int err; |
55c71eba | 1002 | struct visor_vbus_headerinfo *hdr_info; |
3703987c | 1003 | |
7726f813 | 1004 | hdr_info = kzalloc(sizeof(*hdr_info), GFP_KERNEL); |
c2c667d6 BR |
1005 | if (!hdr_info) |
1006 | return -ENOMEM; | |
7726f813 | 1007 | |
343506bf DZ |
1008 | dev_set_name(&dev->device, "visorbus%d", id); |
1009 | dev->device.bus = &visorbus_type; | |
1010 | dev->device.groups = visorbus_groups; | |
1011 | dev->device.release = visorbus_release_busdevice; | |
d32517e3 | 1012 | |
8217becc TS |
1013 | dev->debugfs_dir = debugfs_create_dir(dev_name(&dev->device), |
1014 | visorbus_debugfs_dir); | |
8217becc | 1015 | dev->debugfs_client_bus_info = |
b3d76e80 | 1016 | debugfs_create_file("client_bus_info", 0440, |
8217becc TS |
1017 | dev->debugfs_dir, dev, |
1018 | &client_bus_info_debugfs_fops); | |
362f87f3 DK |
1019 | |
1020 | dev_set_drvdata(&dev->device, dev); | |
1021 | err = get_vbus_header_info(dev->visorchannel, hdr_info); | |
1022 | if (err < 0) | |
8217becc | 1023 | goto err_debugfs_dir; |
8217becc | 1024 | |
362f87f3 | 1025 | err = device_register(&dev->device); |
450333f1 | 1026 | if (err < 0) |
362f87f3 | 1027 | goto err_debugfs_dir; |
d32517e3 | 1028 | |
343506bf | 1029 | list_add_tail(&dev->list_all, &list_all_bus_instances); |
8217becc | 1030 | |
362f87f3 DK |
1031 | dev->vbus_hdr_info = (void *)hdr_info; |
1032 | write_vbus_chp_info(dev->visorchannel, hdr_info, | |
1033 | &chipset_driverinfo); | |
1034 | write_vbus_bus_info(dev->visorchannel, hdr_info, | |
1035 | &clientbus_driverinfo); | |
1036 | ||
1037 | return 0; | |
8217becc TS |
1038 | |
1039 | err_debugfs_dir: | |
1040 | debugfs_remove_recursive(dev->debugfs_dir); | |
8217becc | 1041 | kfree(hdr_info); |
9e78fd35 | 1042 | dev_err(&dev->device, "visorbus_create_instance failed: %d\n", err); |
8217becc | 1043 | return err; |
3703987c EA |
1044 | } |
1045 | ||
b84be59a | 1046 | /* |
9e78fd35 | 1047 | * visorbus_remove_instance() - remove a device instance for the visorbus itself |
3fd1b3b6 | 1048 | * @dev: struct visor_device indentifying the bus to remove |
3703987c EA |
1049 | */ |
1050 | static void | |
9e78fd35 | 1051 | visorbus_remove_instance(struct visor_device *dev) |
3703987c | 1052 | { |
3fd1b3b6 DB |
1053 | /* |
1054 | * Note that this will result in the release method for | |
343506bf | 1055 | * dev->dev being called, which will call |
3703987c EA |
1056 | * visorbus_release_busdevice(). This has something to do with |
1057 | * the put_device() done in device_unregister(), but I have never | |
1058 | * successfully been able to trace thru the code to see where/how | |
1059 | * release() gets called. But I know it does. | |
1060 | */ | |
343506bf DZ |
1061 | if (dev->visorchannel) { |
1062 | visorchannel_destroy(dev->visorchannel); | |
1063 | dev->visorchannel = NULL; | |
3703987c | 1064 | } |
343506bf DZ |
1065 | kfree(dev->vbus_hdr_info); |
1066 | list_del(&dev->list_all); | |
1067 | device_unregister(&dev->device); | |
3703987c EA |
1068 | } |
1069 | ||
b84be59a | 1070 | /* |
9e78fd35 | 1071 | * remove_all_visor_devices() - remove all child visorbus device instances |
3703987c EA |
1072 | */ |
1073 | static void | |
1074 | remove_all_visor_devices(void) | |
1075 | { | |
1076 | struct list_head *listentry, *listtmp; | |
1077 | ||
1078 | list_for_each_safe(listentry, listtmp, &list_all_device_instances) { | |
1079 | struct visor_device *dev = list_entry(listentry, | |
1080 | struct visor_device, | |
1081 | list_all); | |
1082 | remove_visor_device(dev); | |
1083 | } | |
1084 | } | |
1085 | ||
621f5e18 | 1086 | int |
4f96c730 | 1087 | visorchipset_bus_create(struct visor_device *dev) |
3703987c | 1088 | { |
621f5e18 | 1089 | int err; |
3703987c | 1090 | |
9e78fd35 | 1091 | err = visorbus_create_instance(dev); |
d32517e3 | 1092 | |
646c7eb5 | 1093 | if (err < 0) |
621f5e18 | 1094 | return err; |
d32517e3 | 1095 | |
63847f17 | 1096 | visorbus_create_response(dev, err); |
621f5e18 DK |
1097 | |
1098 | return 0; | |
3703987c EA |
1099 | } |
1100 | ||
87241ab8 | 1101 | void |
4f96c730 | 1102 | visorchipset_bus_destroy(struct visor_device *dev) |
3703987c | 1103 | { |
9e78fd35 | 1104 | visorbus_remove_instance(dev); |
63847f17 | 1105 | visorbus_destroy_response(dev, 0); |
3703987c EA |
1106 | } |
1107 | ||
3f49a21d | 1108 | int |
c0b44136 | 1109 | visorchipset_device_create(struct visor_device *dev_info) |
3703987c | 1110 | { |
3f49a21d | 1111 | int err; |
3703987c | 1112 | |
3f49a21d | 1113 | err = create_visor_device(dev_info); |
646c7eb5 | 1114 | if (err < 0) |
3f49a21d | 1115 | return err; |
3f49a21d | 1116 | |
f8b5a21f | 1117 | visorbus_device_create_response(dev_info, err); |
3f49a21d DK |
1118 | |
1119 | return 0; | |
3703987c EA |
1120 | } |
1121 | ||
87241ab8 | 1122 | void |
c0b44136 | 1123 | visorchipset_device_destroy(struct visor_device *dev_info) |
3703987c | 1124 | { |
a298bc0b | 1125 | remove_visor_device(dev_info); |
3703987c | 1126 | |
f8b5a21f | 1127 | visorbus_device_destroy_response(dev_info, 0); |
3703987c EA |
1128 | } |
1129 | ||
b84be59a | 1130 | /* |
3fd1b3b6 DB |
1131 | * pause_state_change_complete() - the callback function to be called by a |
1132 | * visorbus function driver when a | |
1133 | * pending "pause device" operation has | |
1134 | * completed | |
1135 | * @dev: struct visor_device identifying the paused device | |
1136 | * @status: 0 iff the pause state change completed successfully, otherwise | |
1137 | * a negative errno value indicating the reason for failure | |
3703987c EA |
1138 | */ |
1139 | static void | |
a298bc0b | 1140 | pause_state_change_complete(struct visor_device *dev, int status) |
3703987c EA |
1141 | { |
1142 | if (!dev->pausing) | |
216c3e2c | 1143 | return; |
3703987c | 1144 | |
779d0752 | 1145 | dev->pausing = false; |
3703987c | 1146 | |
f8b5a21f | 1147 | visorbus_device_pause_response(dev, status); |
3703987c EA |
1148 | } |
1149 | ||
b84be59a | 1150 | /* |
3fd1b3b6 DB |
1151 | * resume_state_change_complete() - the callback function to be called by a |
1152 | * visorbus function driver when a | |
1153 | * pending "resume device" operation has | |
1154 | * completed | |
1155 | * @dev: struct visor_device identifying the resumed device | |
1156 | * @status: 0 iff the resume state change completed successfully, otherwise | |
1157 | * a negative errno value indicating the reason for failure | |
3703987c EA |
1158 | */ |
1159 | static void | |
a298bc0b | 1160 | resume_state_change_complete(struct visor_device *dev, int status) |
3703987c EA |
1161 | { |
1162 | if (!dev->resuming) | |
216c3e2c | 1163 | return; |
3703987c | 1164 | |
779d0752 | 1165 | dev->resuming = false; |
3703987c | 1166 | |
3fd1b3b6 DB |
1167 | /* |
1168 | * Notify the chipset driver that the resume is complete, | |
3703987c | 1169 | * which will presumably want to send some sort of response to |
48117895 GL |
1170 | * the initiator. |
1171 | */ | |
f8b5a21f | 1172 | visorbus_device_resume_response(dev, status); |
3703987c EA |
1173 | } |
1174 | ||
b84be59a | 1175 | /* |
451072e3 SW |
1176 | * visorchipset_initiate_device_pause_resume() - start a pause or resume |
1177 | * operation for a visor device | |
3fd1b3b6 DB |
1178 | * @dev: struct visor_device identifying the device being paused or resumed |
1179 | * @is_pause: true to indicate pause operation, false to indicate resume | |
1180 | * | |
1181 | * Tell the subordinate function driver for a specific device to pause | |
1182 | * or resume that device. Success/failure result is returned asynchronously | |
1183 | * via a callback function; see pause_state_change_complete() and | |
1184 | * resume_state_change_complete(). | |
3703987c | 1185 | */ |
15d9ce9d | 1186 | static int |
451072e3 SW |
1187 | visorchipset_initiate_device_pause_resume(struct visor_device *dev, |
1188 | bool is_pause) | |
3703987c | 1189 | { |
15d9ce9d | 1190 | int err; |
3703987c | 1191 | struct visor_driver *drv = NULL; |
3703987c | 1192 | |
3703987c | 1193 | drv = to_visor_driver(dev->device.driver); |
15d9ce9d DK |
1194 | if (!drv) |
1195 | return -ENODEV; | |
3703987c | 1196 | |
15d9ce9d DK |
1197 | if (dev->pausing || dev->resuming) |
1198 | return -EBUSY; | |
3703987c | 1199 | |
3703987c | 1200 | if (is_pause) { |
15d9ce9d DK |
1201 | if (!drv->pause) |
1202 | return -EINVAL; | |
3703987c | 1203 | |
779d0752 | 1204 | dev->pausing = true; |
15d9ce9d | 1205 | err = drv->pause(dev, pause_state_change_complete); |
3703987c | 1206 | } else { |
060b293b SW |
1207 | /* |
1208 | * The vbus_dev_info structure in the channel was been cleared, | |
1209 | * make sure it is valid. | |
48117895 | 1210 | */ |
3703987c | 1211 | fix_vbus_dev_info(dev); |
15d9ce9d DK |
1212 | if (!drv->resume) |
1213 | return -EINVAL; | |
3703987c | 1214 | |
779d0752 | 1215 | dev->resuming = true; |
15d9ce9d | 1216 | err = drv->resume(dev, resume_state_change_complete); |
3703987c | 1217 | } |
15d9ce9d DK |
1218 | |
1219 | return err; | |
3703987c EA |
1220 | } |
1221 | ||
060b293b | 1222 | /* |
c0b44136 | 1223 | * visorchipset_device_pause() - start a pause operation for a visor device |
3fd1b3b6 DB |
1224 | * @dev_info: struct visor_device identifying the device being paused |
1225 | * | |
1226 | * Tell the subordinate function driver for a specific device to pause | |
1227 | * that device. Success/failure result is returned asynchronously | |
1228 | * via a callback function; see pause_state_change_complete(). | |
1229 | */ | |
b4a8e6ae | 1230 | int |
c0b44136 | 1231 | visorchipset_device_pause(struct visor_device *dev_info) |
3703987c | 1232 | { |
15d9ce9d DK |
1233 | int err; |
1234 | ||
451072e3 | 1235 | err = visorchipset_initiate_device_pause_resume(dev_info, true); |
15d9ce9d DK |
1236 | |
1237 | if (err < 0) { | |
1238 | dev_info->pausing = false; | |
b4a8e6ae | 1239 | return err; |
15d9ce9d | 1240 | } |
b4a8e6ae DK |
1241 | |
1242 | return 0; | |
3703987c EA |
1243 | } |
1244 | ||
060b293b | 1245 | /* |
c0b44136 | 1246 | * visorchipset_device_resume() - start a resume operation for a visor device |
3fd1b3b6 DB |
1247 | * @dev_info: struct visor_device identifying the device being resumed |
1248 | * | |
1249 | * Tell the subordinate function driver for a specific device to resume | |
1250 | * that device. Success/failure result is returned asynchronously | |
1251 | * via a callback function; see resume_state_change_complete(). | |
1252 | */ | |
b4a8e6ae | 1253 | int |
c0b44136 | 1254 | visorchipset_device_resume(struct visor_device *dev_info) |
3703987c | 1255 | { |
15d9ce9d DK |
1256 | int err; |
1257 | ||
451072e3 | 1258 | err = visorchipset_initiate_device_pause_resume(dev_info, false); |
15d9ce9d DK |
1259 | |
1260 | if (err < 0) { | |
15d9ce9d | 1261 | dev_info->resuming = false; |
b4a8e6ae | 1262 | return err; |
15d9ce9d | 1263 | } |
b4a8e6ae DK |
1264 | |
1265 | return 0; | |
3703987c EA |
1266 | } |
1267 | ||
55c67dca | 1268 | int |
3703987c EA |
1269 | visorbus_init(void) |
1270 | { | |
78af1aef | 1271 | int err; |
3703987c | 1272 | |
8217becc TS |
1273 | visorbus_debugfs_dir = debugfs_create_dir("visorbus", NULL); |
1274 | if (!visorbus_debugfs_dir) | |
1275 | return -ENOMEM; | |
1276 | ||
e82ed633 | 1277 | bus_device_info_init(&clientbus_driverinfo, "clientbus", "visorbus"); |
3703987c | 1278 | |
5b6f9b95 | 1279 | err = bus_register(&visorbus_type); |
804aab3b DK |
1280 | if (err < 0) |
1281 | return err; | |
1282 | ||
b0512faf | 1283 | initialized = true; |
3703987c | 1284 | |
e82ed633 | 1285 | bus_device_info_init(&chipset_driverinfo, "chipset", "visorchipset"); |
3703987c | 1286 | |
78af1aef | 1287 | return 0; |
3703987c EA |
1288 | } |
1289 | ||
c79b28f7 | 1290 | void |
3703987c EA |
1291 | visorbus_exit(void) |
1292 | { | |
1293 | struct list_head *listentry, *listtmp; | |
1294 | ||
3703987c EA |
1295 | remove_all_visor_devices(); |
1296 | ||
3703987c | 1297 | list_for_each_safe(listentry, listtmp, &list_all_bus_instances) { |
343506bf | 1298 | struct visor_device *dev = list_entry(listentry, |
216c3e2c CJ |
1299 | struct visor_device, |
1300 | list_all); | |
9e78fd35 | 1301 | visorbus_remove_instance(dev); |
3703987c | 1302 | } |
5b6f9b95 DK |
1303 | |
1304 | bus_unregister(&visorbus_type); | |
b0512faf | 1305 | initialized = false; |
8217becc | 1306 | debugfs_remove_recursive(visorbus_debugfs_dir); |
3703987c | 1307 | } |