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