3 * Copyright � 2010 - 2015 UNISYS CORPORATION
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
13 * NON INFRINGEMENT. See the GNU General Public License for more
17 #include <linux/debugfs.h>
18 #include <linux/uuid.h>
21 #include "visorbus_private.h"
23 #define MYDRVNAME "visorbus"
25 /* Display string that is guaranteed to be no longer the 99 characters*/
28 #define CURRENT_FILE_PC VISOR_BUS_PC_visorbus_main_c
29 #define POLLJIFFIES_NORMALCHANNEL 10
31 static bool initialized
; /* stores whether bus_registration was successful */
32 static struct dentry
*visorbus_debugfs_dir
;
35 * DEVICE type attributes
37 * The modalias file will contain the guid of the device.
39 static ssize_t
modalias_show(struct device
*dev
, struct device_attribute
*attr
,
42 struct visor_device
*vdev
;
45 vdev
= to_visor_device(dev
);
46 guid
= visorchannel_get_uuid(vdev
->visorchannel
);
47 return sprintf(buf
, "visorbus:%pUl\n", &guid
);
49 static DEVICE_ATTR_RO(modalias
);
51 static struct attribute
*visorbus_dev_attrs
[] = {
52 &dev_attr_modalias
.attr
,
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
,
61 static const struct attribute_group
*visorbus_dev_groups
[] = {
66 /* filled in with info about parent chipset driver when we register with it */
67 static struct ultra_vbus_deviceinfo chipset_driverinfo
;
68 /* filled in with info about this driver, wrt it servicing client busses */
69 static struct ultra_vbus_deviceinfo clientbus_driverinfo
;
71 /* list of visor_device structs, linked via .list_all */
72 static LIST_HEAD(list_all_bus_instances
);
73 /* list of visor_device structs, linked via .list_all */
74 static LIST_HEAD(list_all_device_instances
);
77 visorbus_uevent(struct device
*xdev
, struct kobj_uevent_env
*env
)
79 struct visor_device
*dev
;
82 dev
= to_visor_device(xdev
);
83 guid
= visorchannel_get_uuid(dev
->visorchannel
);
85 return add_uevent_var(env
, "MODALIAS=visorbus:%pUl", &guid
);
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
95 * Return: 1 iff the provided driver can control the specified device
98 visorbus_match(struct device
*xdev
, struct device_driver
*xdrv
)
100 uuid_le channel_type
;
102 struct visor_device
*dev
;
103 struct visor_driver
*drv
;
105 dev
= to_visor_device(xdev
);
106 drv
= to_visor_driver(xdrv
);
107 channel_type
= visorchannel_get_uuid(dev
->visorchannel
);
109 if (!drv
->channel_types
)
113 (uuid_le_cmp(drv
->channel_types
[i
].guid
, NULL_UUID_LE
) != 0) ||
114 (drv
->channel_types
[i
].name
);
116 if (uuid_le_cmp(drv
->channel_types
[i
].guid
,
124 * This describes the TYPE of bus.
125 * (Don't confuse this with an INSTANCE of the bus.)
127 struct bus_type visorbus_type
= {
129 .match
= visorbus_match
,
130 .uevent
= visorbus_uevent
,
131 .dev_groups
= visorbus_dev_groups
,
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
138 * @xdev: struct device for the bus being released
141 visorbus_release_busdevice(struct device
*xdev
)
143 struct visor_device
*dev
= dev_get_drvdata(xdev
);
145 debugfs_remove(dev
->debugfs_client_bus_info
);
146 debugfs_remove_recursive(dev
->debugfs_dir
);
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
156 visorbus_release_device(struct device
*xdev
)
158 struct visor_device
*dev
= to_visor_device(xdev
);
160 if (dev
->visorchannel
) {
161 visorchannel_destroy(dev
->visorchannel
);
162 dev
->visorchannel
= NULL
;
168 * begin implementation of specific channel attributes to appear under
169 * /sys/bus/visorbus<x>/dev<y>/channel
172 static ssize_t
physaddr_show(struct device
*dev
, struct device_attribute
*attr
,
175 struct visor_device
*vdev
= to_visor_device(dev
);
177 return sprintf(buf
, "0x%llx\n",
178 visorchannel_get_physaddr(vdev
->visorchannel
));
180 static DEVICE_ATTR_RO(physaddr
);
182 static ssize_t
nbytes_show(struct device
*dev
, struct device_attribute
*attr
,
185 struct visor_device
*vdev
= to_visor_device(dev
);
187 return sprintf(buf
, "0x%lx\n",
188 visorchannel_get_nbytes(vdev
->visorchannel
));
190 static DEVICE_ATTR_RO(nbytes
);
192 static ssize_t
clientpartition_show(struct device
*dev
,
193 struct device_attribute
*attr
, char *buf
)
195 struct visor_device
*vdev
= to_visor_device(dev
);
197 return sprintf(buf
, "0x%llx\n",
198 visorchannel_get_clientpartition(vdev
->visorchannel
));
200 static DEVICE_ATTR_RO(clientpartition
);
202 static ssize_t
typeguid_show(struct device
*dev
, struct device_attribute
*attr
,
205 struct visor_device
*vdev
= to_visor_device(dev
);
206 char typeid[LINESIZE
];
208 return sprintf(buf
, "%s\n",
209 visorchannel_id(vdev
->visorchannel
, typeid));
211 static DEVICE_ATTR_RO(typeguid
);
213 static ssize_t
zoneguid_show(struct device
*dev
, struct device_attribute
*attr
,
216 struct visor_device
*vdev
= to_visor_device(dev
);
217 char zoneid
[LINESIZE
];
219 return sprintf(buf
, "%s\n",
220 visorchannel_zoneid(vdev
->visorchannel
, zoneid
));
222 static DEVICE_ATTR_RO(zoneguid
);
224 static ssize_t
typename_show(struct device
*dev
, struct device_attribute
*attr
,
228 struct bus_type
*xbus
= dev
->bus
;
229 struct device_driver
*xdrv
= dev
->driver
;
230 struct visor_driver
*drv
= NULL
;
234 i
= xbus
->match(dev
, xdrv
);
237 drv
= to_visor_driver(xdrv
);
238 return sprintf(buf
, "%s\n", drv
->channel_types
[i
- 1].name
);
240 static DEVICE_ATTR_RO(typename
);
242 static struct attribute
*channel_attrs
[] = {
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
,
252 static struct attribute_group channel_attr_grp
= {
254 .attrs
= channel_attrs
,
257 static const struct attribute_group
*visorbus_channel_groups
[] = {
262 /* end implementation of specific channel attributes */
265 * BUS instance attributes
267 * define & implement display of bus attributes under
268 * /sys/bus/visorbus/devices/visorbus<n>.
271 static ssize_t
partition_handle_show(struct device
*dev
,
272 struct device_attribute
*attr
,
274 struct visor_device
*vdev
= to_visor_device(dev
);
275 u64 handle
= visorchannel_get_clientpartition(vdev
->visorchannel
);
277 return sprintf(buf
, "0x%llx\n", handle
);
279 static DEVICE_ATTR_RO(partition_handle
);
281 static ssize_t
partition_guid_show(struct device
*dev
,
282 struct device_attribute
*attr
,
284 struct visor_device
*vdev
= to_visor_device(dev
);
286 return sprintf(buf
, "{%pUb}\n", &vdev
->partition_uuid
);
288 static DEVICE_ATTR_RO(partition_guid
);
290 static ssize_t
partition_name_show(struct device
*dev
,
291 struct device_attribute
*attr
,
293 struct visor_device
*vdev
= to_visor_device(dev
);
295 return sprintf(buf
, "%s\n", vdev
->name
);
297 static DEVICE_ATTR_RO(partition_name
);
299 static ssize_t
channel_addr_show(struct device
*dev
,
300 struct device_attribute
*attr
,
302 struct visor_device
*vdev
= to_visor_device(dev
);
303 u64 addr
= visorchannel_get_physaddr(vdev
->visorchannel
);
305 return sprintf(buf
, "0x%llx\n", addr
);
307 static DEVICE_ATTR_RO(channel_addr
);
309 static ssize_t
channel_bytes_show(struct device
*dev
,
310 struct device_attribute
*attr
,
312 struct visor_device
*vdev
= to_visor_device(dev
);
313 u64 nbytes
= visorchannel_get_nbytes(vdev
->visorchannel
);
315 return sprintf(buf
, "0x%llx\n", nbytes
);
317 static DEVICE_ATTR_RO(channel_bytes
);
319 static ssize_t
channel_id_show(struct device
*dev
,
320 struct device_attribute
*attr
,
322 struct visor_device
*vdev
= to_visor_device(dev
);
325 visorchannel_id(vdev
->visorchannel
, buf
);
331 static DEVICE_ATTR_RO(channel_id
);
333 static struct attribute
*dev_attrs
[] = {
334 &dev_attr_partition_handle
.attr
,
335 &dev_attr_partition_guid
.attr
,
336 &dev_attr_partition_name
.attr
,
337 &dev_attr_channel_addr
.attr
,
338 &dev_attr_channel_bytes
.attr
,
339 &dev_attr_channel_id
.attr
,
343 static struct attribute_group dev_attr_grp
= {
347 static const struct attribute_group
*visorbus_groups
[] = {
353 * BUS debugfs entries
355 * define & implement display of debugfs attributes under
356 * /sys/kernel/debug/visorbus/visorbus<n>.
359 * vbuschannel_print_devinfo() - format a struct ultra_vbus_deviceinfo
360 * and write it to a seq_file
361 * @devinfo: the struct ultra_vbus_deviceinfo to format
362 * @seq: seq_file to write to
363 * @devix: the device index to be included in the output data, or -1 if no
364 * device index is to be included
366 * Reads @devInfo, and writes it in human-readable notation to @seq.
369 vbuschannel_print_devinfo(struct ultra_vbus_deviceinfo
*devinfo
,
370 struct seq_file
*seq
, int devix
)
372 if (!isprint(devinfo
->devtype
[0]))
373 return; /* uninitialized vbus device entry */
376 seq_printf(seq
, "[%d]", devix
);
378 /* vbus device entry is for bus or chipset */
382 * Note: because the s-Par back-end is free to scribble in this area,
383 * we never assume '\0'-termination.
385 seq_printf(seq
, "%-*.*s ", (int)sizeof(devinfo
->devtype
),
386 (int)sizeof(devinfo
->devtype
), devinfo
->devtype
);
387 seq_printf(seq
, "%-*.*s ", (int)sizeof(devinfo
->drvname
),
388 (int)sizeof(devinfo
->drvname
), devinfo
->drvname
);
389 seq_printf(seq
, "%.*s\n", (int)sizeof(devinfo
->infostrs
),
393 static int client_bus_info_debugfs_show(struct seq_file
*seq
, void *v
)
395 struct visor_device
*vdev
= seq
->private;
396 struct visorchannel
*channel
= vdev
->visorchannel
;
400 struct ultra_vbus_deviceinfo dev_info
;
406 "Client device / client driver info for %s partition (vbus #%u):\n",
407 ((vdev
->name
) ? (char *)(vdev
->name
) : ""),
408 vdev
->chipset_bus_no
);
409 if (visorchannel_read(channel
,
410 offsetof(struct spar_vbus_channel_protocol
,
412 &dev_info
, sizeof(dev_info
)) >= 0)
413 vbuschannel_print_devinfo(&dev_info
, seq
, -1);
414 if (visorchannel_read(channel
,
415 offsetof(struct spar_vbus_channel_protocol
,
417 &dev_info
, sizeof(dev_info
)) >= 0)
418 vbuschannel_print_devinfo(&dev_info
, seq
, -1);
419 off
= offsetof(struct spar_vbus_channel_protocol
, dev_info
);
421 while (off
+ sizeof(dev_info
) <= visorchannel_get_nbytes(channel
)) {
422 if (visorchannel_read(channel
, off
, &dev_info
,
423 sizeof(dev_info
)) >= 0)
424 vbuschannel_print_devinfo(&dev_info
, seq
, i
);
425 off
+= sizeof(dev_info
);
432 static int client_bus_info_debugfs_open(struct inode
*inode
, struct file
*file
)
434 return single_open(file
, client_bus_info_debugfs_show
,
438 static const struct file_operations client_bus_info_debugfs_fops
= {
439 .owner
= THIS_MODULE
,
440 .open
= client_bus_info_debugfs_open
,
443 .release
= single_release
,
447 dev_periodic_work(unsigned long __opaque
)
449 struct visor_device
*dev
= (struct visor_device
*)__opaque
;
450 struct visor_driver
*drv
= to_visor_driver(dev
->device
.driver
);
452 drv
->channel_interrupt(dev
);
453 mod_timer(&dev
->timer
, jiffies
+ POLLJIFFIES_NORMALCHANNEL
);
457 dev_start_periodic_work(struct visor_device
*dev
)
459 if (dev
->being_removed
|| dev
->timer_active
)
461 /* now up by at least 2 */
462 get_device(&dev
->device
);
463 dev
->timer
.expires
= jiffies
+ POLLJIFFIES_NORMALCHANNEL
;
464 add_timer(&dev
->timer
);
465 dev
->timer_active
= true;
470 dev_stop_periodic_work(struct visor_device
*dev
)
472 if (!dev
->timer_active
)
474 del_timer_sync(&dev
->timer
);
475 dev
->timer_active
= false;
476 put_device(&dev
->device
);
480 * visordriver_remove_device() - handle visor device going away
481 * @xdev: struct device for the visor device being removed
483 * This is called when device_unregister() is called for each child device
484 * instance, to notify the appropriate visorbus function driver that the device
485 * is going away, and to decrease the reference count of the device.
487 * Return: 0 iff successful
490 visordriver_remove_device(struct device
*xdev
)
492 struct visor_device
*dev
;
493 struct visor_driver
*drv
;
495 dev
= to_visor_device(xdev
);
496 drv
= to_visor_driver(xdev
->driver
);
497 mutex_lock(&dev
->visordriver_callback_lock
);
498 dev
->being_removed
= true;
501 mutex_unlock(&dev
->visordriver_callback_lock
);
502 dev_stop_periodic_work(dev
);
504 put_device(&dev
->device
);
509 * visorbus_unregister_visor_driver() - unregisters the provided driver
510 * @drv: the driver to unregister
512 * A visor function driver calls this function to unregister the driver,
513 * i.e., within its module_exit function.
516 visorbus_unregister_visor_driver(struct visor_driver
*drv
)
518 driver_unregister(&drv
->driver
);
520 EXPORT_SYMBOL_GPL(visorbus_unregister_visor_driver
);
523 * visorbus_read_channel() - reads from the designated channel into
524 * the provided buffer
525 * @dev: the device whose channel is read from
526 * @offset: the offset into the channel at which reading starts
527 * @dest: the destination buffer that is written into from the channel
528 * @nbytes: the number of bytes to read from the channel
530 * If receiving a message, use the visorchannel_signalremove()
533 * Return: integer indicating success (zero) or failure (non-zero)
536 visorbus_read_channel(struct visor_device
*dev
, unsigned long offset
,
537 void *dest
, unsigned long nbytes
)
539 return visorchannel_read(dev
->visorchannel
, offset
, dest
, nbytes
);
541 EXPORT_SYMBOL_GPL(visorbus_read_channel
);
544 * visorbus_write_channel() - writes the provided buffer into the designated
546 * @dev: the device whose channel is written to
547 * @offset: the offset into the channel at which writing starts
548 * @src: the source buffer that is written into the channel
549 * @nbytes: the number of bytes to write into the channel
551 * If sending a message, use the visorchannel_signalinsert()
554 * Return: integer indicating success (zero) or failure (non-zero)
557 visorbus_write_channel(struct visor_device
*dev
, unsigned long offset
,
558 void *src
, unsigned long nbytes
)
560 return visorchannel_write(dev
->visorchannel
, offset
, src
, nbytes
);
562 EXPORT_SYMBOL_GPL(visorbus_write_channel
);
565 * visorbus_enable_channel_interrupts() - enables interrupts on the
567 * @dev: the device on which to enable interrupts
569 * Currently we don't yet have a real interrupt, so for now we just call the
570 * interrupt function periodically via a timer.
573 visorbus_enable_channel_interrupts(struct visor_device
*dev
)
575 struct visor_driver
*drv
= to_visor_driver(dev
->device
.driver
);
577 if (!drv
->channel_interrupt
) {
578 dev_err(&dev
->device
, "%s no interrupt function!\n", __func__
);
582 return dev_start_periodic_work(dev
);
584 EXPORT_SYMBOL_GPL(visorbus_enable_channel_interrupts
);
587 * visorbus_disable_channel_interrupts() - disables interrupts on the
589 * @dev: the device on which to disable interrupts
592 visorbus_disable_channel_interrupts(struct visor_device
*dev
)
594 dev_stop_periodic_work(dev
);
596 EXPORT_SYMBOL_GPL(visorbus_disable_channel_interrupts
);
599 * create_visor_device() - create visor device as a result of receiving the
600 * controlvm device_create message for a new device
601 * @dev: a freshly-zeroed struct visor_device, containing only filled-in values
602 * for chipset_bus_no and chipset_dev_no, that will be initialized
604 * This is how everything starts from the device end.
605 * This function is called when a channel first appears via a ControlVM
606 * message. In response, this function allocates a visor_device to
607 * correspond to the new channel, and attempts to connect it the appropriate
608 * driver. If the appropriate driver is found, the visor_driver.probe()
609 * function for that driver will be called, and will be passed the new
610 * visor_device that we just created.
612 * It's ok if the appropriate driver is not yet loaded, because in that case
613 * the new device struct will just stick around in the bus' list of devices.
614 * When the appropriate driver calls visorbus_register_visor_driver(), the
615 * visor_driver.probe() for the new driver will be called with the new
618 * Return: 0 if successful, otherwise the negative value returned by
619 * device_add() indicating the reason for failure
622 create_visor_device(struct visor_device
*dev
)
625 u32 chipset_bus_no
= dev
->chipset_bus_no
;
626 u32 chipset_dev_no
= dev
->chipset_dev_no
;
628 mutex_init(&dev
->visordriver_callback_lock
);
629 dev
->device
.bus
= &visorbus_type
;
630 dev
->device
.groups
= visorbus_channel_groups
;
631 device_initialize(&dev
->device
);
632 dev
->device
.release
= visorbus_release_device
;
633 /* keep a reference just for us (now 2) */
634 get_device(&dev
->device
);
635 setup_timer(&dev
->timer
, dev_periodic_work
, (unsigned long)dev
);
638 * bus_id must be a unique name with respect to this bus TYPE
639 * (NOT bus instance). That's why we need to include the bus
640 * number within the name.
642 err
= dev_set_name(&dev
->device
, "vbus%u:dev%u",
643 chipset_bus_no
, chipset_dev_no
);
648 * device_add does this:
649 * bus_add_device(dev)
650 * ->device_attach(dev)
651 * ->for each driver drv registered on the bus that dev is on
652 * if (dev.drv) ** device already has a driver **
653 * ** not sure we could ever get here... **
655 * if (bus.match(dev,drv)) [visorbus_match]
657 * if (!drv.probe(dev)) [visordriver_probe_device]
660 * Note that device_add does NOT fail if no driver failed to
661 * claim the device. The device will be linked onto
662 * bus_type.klist_devices regardless (use bus_for_each_dev).
664 err
= device_add(&dev
->device
);
668 list_add_tail(&dev
->list_all
, &list_all_device_instances
);
669 return 0; /* success: reference kept via unmatched get_device() */
672 put_device(&dev
->device
);
673 dev_err(&dev
->device
, "Creating visor device failed. %d\n", err
);
678 remove_visor_device(struct visor_device
*dev
)
680 list_del(&dev
->list_all
);
681 put_device(&dev
->device
);
682 device_unregister(&dev
->device
);
686 get_vbus_header_info(struct visorchannel
*chan
,
687 struct spar_vbus_headerinfo
*hdr_info
)
691 if (!spar_check_channel(visorchannel_get_header(chan
),
692 spar_vbus_channel_protocol_uuid
,
694 sizeof(struct spar_vbus_channel_protocol
),
695 SPAR_VBUS_CHANNEL_PROTOCOL_VERSIONID
,
696 SPAR_VBUS_CHANNEL_PROTOCOL_SIGNATURE
))
699 err
= visorchannel_read(chan
, sizeof(struct channel_header
), hdr_info
,
704 if (hdr_info
->struct_bytes
< sizeof(struct spar_vbus_headerinfo
))
707 if (hdr_info
->device_info_struct_bytes
<
708 sizeof(struct ultra_vbus_deviceinfo
))
715 * write_vbus_chp_info() - write the contents of <info> to the struct
716 * spar_vbus_channel_protocol.chp_info
717 * @chan: indentifies the s-Par channel that will be updated
718 * @hdr_info: used to find appropriate channel offset to write data
719 * @info: contains the information to write
721 * Writes chipset info into the channel memory to be used for diagnostic
724 * Returns no value since this is debug information and not needed for
725 * device functionality.
728 write_vbus_chp_info(struct visorchannel
*chan
,
729 struct spar_vbus_headerinfo
*hdr_info
,
730 struct ultra_vbus_deviceinfo
*info
)
732 int off
= sizeof(struct channel_header
) + hdr_info
->chp_info_offset
;
734 if (hdr_info
->chp_info_offset
== 0)
737 visorchannel_write(chan
, off
, info
, sizeof(*info
));
741 * write_vbus_bus_info() - write the contents of <info> to the struct
742 * spar_vbus_channel_protocol.bus_info
743 * @chan: indentifies the s-Par channel that will be updated
744 * @hdr_info: used to find appropriate channel offset to write data
745 * @info: contains the information to write
747 * Writes bus info into the channel memory to be used for diagnostic
750 * Returns no value since this is debug information and not needed for
751 * device functionality.
754 write_vbus_bus_info(struct visorchannel
*chan
,
755 struct spar_vbus_headerinfo
*hdr_info
,
756 struct ultra_vbus_deviceinfo
*info
)
758 int off
= sizeof(struct channel_header
) + hdr_info
->bus_info_offset
;
760 if (hdr_info
->bus_info_offset
== 0)
763 visorchannel_write(chan
, off
, info
, sizeof(*info
));
767 * write_vbus_dev_info() - write the contents of <info> to the struct
768 * spar_vbus_channel_protocol.dev_info[<devix>]
769 * @chan: indentifies the s-Par channel that will be updated
770 * @hdr_info: used to find appropriate channel offset to write data
771 * @info: contains the information to write
772 * @devix: the relative device number (0..n-1) of the device on the bus
774 * Writes device info into the channel memory to be used for diagnostic
777 * Returns no value since this is debug information and not needed for
778 * device functionality.
781 write_vbus_dev_info(struct visorchannel
*chan
,
782 struct spar_vbus_headerinfo
*hdr_info
,
783 struct ultra_vbus_deviceinfo
*info
, unsigned int devix
)
786 (sizeof(struct channel_header
) + hdr_info
->dev_info_offset
) +
787 (hdr_info
->device_info_struct_bytes
* devix
);
789 if (hdr_info
->dev_info_offset
== 0)
792 visorchannel_write(chan
, off
, info
, sizeof(*info
));
795 static void bus_device_info_init(
796 struct ultra_vbus_deviceinfo
*bus_device_info_ptr
,
797 const char *dev_type
, const char *drv_name
)
799 memset(bus_device_info_ptr
, 0, sizeof(struct ultra_vbus_deviceinfo
));
800 snprintf(bus_device_info_ptr
->devtype
,
801 sizeof(bus_device_info_ptr
->devtype
),
802 "%s", (dev_type
) ? dev_type
: "unknownType");
803 snprintf(bus_device_info_ptr
->drvname
,
804 sizeof(bus_device_info_ptr
->drvname
),
805 "%s", (drv_name
) ? drv_name
: "unknownDriver");
806 snprintf(bus_device_info_ptr
->infostrs
,
807 sizeof(bus_device_info_ptr
->infostrs
), "kernel ver. %s",
812 * fix_vbus_dev_info() - for a child device just created on a client bus, fill
813 * in information about the driver that is controlling
814 * this device into the appropriate slot within the
815 * vbus channel of the bus instance
816 * @visordev: struct visor_device for the desired device
819 fix_vbus_dev_info(struct visor_device
*visordev
)
822 struct visor_device
*bdev
;
823 struct visor_driver
*visordrv
;
824 u32 bus_no
= visordev
->chipset_bus_no
;
825 u32 dev_no
= visordev
->chipset_dev_no
;
826 struct ultra_vbus_deviceinfo dev_info
;
827 const char *chan_type_name
= NULL
;
828 struct spar_vbus_headerinfo
*hdr_info
;
830 if (!visordev
->device
.driver
)
833 bdev
= visorbus_get_device_by_id(bus_no
, BUS_ROOT_DEVICE
, NULL
);
836 hdr_info
= (struct spar_vbus_headerinfo
*)bdev
->vbus_hdr_info
;
839 visordrv
= to_visor_driver(visordev
->device
.driver
);
842 * Within the list of device types (by GUID) that the driver
843 * says it supports, find out which one of those types matches
844 * the type of this device, so that we can include the device
847 for (i
= 0; visordrv
->channel_types
[i
].name
; i
++) {
848 if (memcmp(&visordrv
->channel_types
[i
].guid
,
849 &visordev
->channel_type_guid
,
850 sizeof(visordrv
->channel_types
[i
].guid
)) == 0) {
851 chan_type_name
= visordrv
->channel_types
[i
].name
;
856 bus_device_info_init(&dev_info
, chan_type_name
, visordrv
->name
);
857 write_vbus_dev_info(bdev
->visorchannel
, hdr_info
, &dev_info
, dev_no
);
859 write_vbus_chp_info(bdev
->visorchannel
, hdr_info
, &chipset_driverinfo
);
860 write_vbus_bus_info(bdev
->visorchannel
, hdr_info
,
861 &clientbus_driverinfo
);
865 * visordriver_probe_device() - handle new visor device coming online
866 * @xdev: struct device for the visor device being probed
868 * This is called automatically upon adding a visor_device (device_add), or
869 * adding a visor_driver (visorbus_register_visor_driver), but only after
870 * visorbus_match() has returned 1 to indicate a successful match between
873 * If successful, a reference to the device will be held onto via get_device().
875 * Return: 0 if successful, meaning the function driver's probe() function
876 * was successful with this device, otherwise a negative errno
877 * value indicating failure reason
880 visordriver_probe_device(struct device
*xdev
)
883 struct visor_driver
*drv
;
884 struct visor_device
*dev
;
886 drv
= to_visor_driver(xdev
->driver
);
887 dev
= to_visor_device(xdev
);
892 mutex_lock(&dev
->visordriver_callback_lock
);
893 dev
->being_removed
= false;
895 res
= drv
->probe(dev
);
897 /* success: reference kept via unmatched get_device() */
898 get_device(&dev
->device
);
899 fix_vbus_dev_info(dev
);
902 mutex_unlock(&dev
->visordriver_callback_lock
);
907 * visorbus_register_visor_driver() - registers the provided visor driver
908 * for handling one or more visor device
909 * types (channel_types)
910 * @drv: the driver to register
912 * A visor function driver calls this function to register
913 * the driver. The caller MUST fill in the following fields within the
915 * name, version, owner, channel_types, probe, remove
917 * Here's how the whole Linux bus / driver / device model works.
919 * At system start-up, the visorbus kernel module is loaded, which registers
920 * visorbus_type as a bus type, using bus_register().
922 * All kernel modules that support particular device types on a
923 * visorbus bus are loaded. Each of these kernel modules calls
924 * visorbus_register_visor_driver() in their init functions, passing a
925 * visor_driver struct. visorbus_register_visor_driver() in turn calls
926 * register_driver(&visor_driver.driver). This .driver member is
927 * initialized with generic methods (like probe), whose sole responsibility
928 * is to act as a broker for the real methods, which are within the
929 * visor_driver struct. (This is the way the subclass behavior is
930 * implemented, since visor_driver is essentially a subclass of the
931 * generic driver.) Whenever a driver_register() happens, core bus code in
932 * the kernel does (see device_attach() in drivers/base/dd.c):
934 * for each dev associated with the bus (the bus that driver is on) that
935 * does not yet have a driver
936 * if bus.match(dev,newdriver) == yes_matched ** .match specified
937 * ** during bus_register().
938 * newdriver.probe(dev) ** for visor drivers, this will call
939 * ** the generic driver.probe implemented in visorbus.c,
940 * ** which in turn calls the probe specified within the
941 * ** struct visor_driver (which was specified by the
942 * ** actual device driver as part of
943 * ** visorbus_register_visor_driver()).
945 * The above dance also happens when a new device appears.
946 * So the question is, how are devices created within the system?
947 * Basically, just call device_add(dev). See pci_bus_add_devices().
948 * pci_scan_device() shows an example of how to build a device struct. It
949 * returns the newly-created struct to pci_scan_single_device(), who adds it
950 * to the list of devices at PCIBUS.devices. That list of devices is what
951 * is traversed by pci_bus_add_devices().
953 * Return: integer indicating success (zero) or failure (non-zero)
955 int visorbus_register_visor_driver(struct visor_driver
*drv
)
958 return -ENODEV
; /* can't register on a nonexistent bus */
960 drv
->driver
.name
= drv
->name
;
961 drv
->driver
.bus
= &visorbus_type
;
962 drv
->driver
.probe
= visordriver_probe_device
;
963 drv
->driver
.remove
= visordriver_remove_device
;
964 drv
->driver
.owner
= drv
->owner
;
967 * driver_register does this:
968 * bus_add_driver(drv)
969 * ->if (drv.bus) ** (bus_type) **
971 * for each dev with bus type of drv.bus
972 * if (!dev.drv) ** no driver assigned yet **
973 * if (bus.match(dev,drv)) [visorbus_match]
975 * if (!drv.probe(dev)) [visordriver_probe_device]
979 return driver_register(&drv
->driver
);
981 EXPORT_SYMBOL_GPL(visorbus_register_visor_driver
);
984 * create_bus_instance() - create a device instance for the visor bus itself
985 * @dev: struct visor_device indicating the bus instance
987 * Return: 0 for success, otherwise negative errno value indicating reason for
991 create_bus_instance(struct visor_device
*dev
)
993 int id
= dev
->chipset_bus_no
;
995 struct spar_vbus_headerinfo
*hdr_info
;
997 hdr_info
= kzalloc(sizeof(*hdr_info
), GFP_KERNEL
);
1001 dev_set_name(&dev
->device
, "visorbus%d", id
);
1002 dev
->device
.bus
= &visorbus_type
;
1003 dev
->device
.groups
= visorbus_groups
;
1004 dev
->device
.release
= visorbus_release_busdevice
;
1006 dev
->debugfs_dir
= debugfs_create_dir(dev_name(&dev
->device
),
1007 visorbus_debugfs_dir
);
1008 dev
->debugfs_client_bus_info
=
1009 debugfs_create_file("client_bus_info", 0440,
1010 dev
->debugfs_dir
, dev
,
1011 &client_bus_info_debugfs_fops
);
1013 dev_set_drvdata(&dev
->device
, dev
);
1014 err
= get_vbus_header_info(dev
->visorchannel
, hdr_info
);
1016 goto err_debugfs_dir
;
1018 err
= device_register(&dev
->device
);
1020 goto err_debugfs_dir
;
1022 list_add_tail(&dev
->list_all
, &list_all_bus_instances
);
1024 dev
->vbus_hdr_info
= (void *)hdr_info
;
1025 write_vbus_chp_info(dev
->visorchannel
, hdr_info
,
1026 &chipset_driverinfo
);
1027 write_vbus_bus_info(dev
->visorchannel
, hdr_info
,
1028 &clientbus_driverinfo
);
1033 debugfs_remove_recursive(dev
->debugfs_dir
);
1035 dev_err(&dev
->device
, "create_bus_instance failed: %d\n", err
);
1040 * remove_bus_instance() - remove a device instance for the visor bus itself
1041 * @dev: struct visor_device indentifying the bus to remove
1044 remove_bus_instance(struct visor_device
*dev
)
1047 * Note that this will result in the release method for
1048 * dev->dev being called, which will call
1049 * visorbus_release_busdevice(). This has something to do with
1050 * the put_device() done in device_unregister(), but I have never
1051 * successfully been able to trace thru the code to see where/how
1052 * release() gets called. But I know it does.
1054 if (dev
->visorchannel
) {
1055 visorchannel_destroy(dev
->visorchannel
);
1056 dev
->visorchannel
= NULL
;
1058 kfree(dev
->vbus_hdr_info
);
1059 list_del(&dev
->list_all
);
1060 device_unregister(&dev
->device
);
1064 * remove_all_visor_devices() - remove all child visor bus device instances
1067 remove_all_visor_devices(void)
1069 struct list_head
*listentry
, *listtmp
;
1071 list_for_each_safe(listentry
, listtmp
, &list_all_device_instances
) {
1072 struct visor_device
*dev
= list_entry(listentry
,
1073 struct visor_device
,
1075 remove_visor_device(dev
);
1080 chipset_bus_create(struct visor_device
*dev
)
1084 err
= create_bus_instance(dev
);
1089 bus_create_response(dev
, err
);
1095 chipset_bus_destroy(struct visor_device
*dev
)
1097 remove_bus_instance(dev
);
1098 bus_destroy_response(dev
, 0);
1102 chipset_device_create(struct visor_device
*dev_info
)
1106 err
= create_visor_device(dev_info
);
1110 device_create_response(dev_info
, err
);
1116 chipset_device_destroy(struct visor_device
*dev_info
)
1118 remove_visor_device(dev_info
);
1120 device_destroy_response(dev_info
, 0);
1124 * pause_state_change_complete() - the callback function to be called by a
1125 * visorbus function driver when a
1126 * pending "pause device" operation has
1128 * @dev: struct visor_device identifying the paused device
1129 * @status: 0 iff the pause state change completed successfully, otherwise
1130 * a negative errno value indicating the reason for failure
1133 pause_state_change_complete(struct visor_device
*dev
, int status
)
1138 dev
->pausing
= false;
1140 device_pause_response(dev
, status
);
1144 * resume_state_change_complete() - the callback function to be called by a
1145 * visorbus function driver when a
1146 * pending "resume device" operation has
1148 * @dev: struct visor_device identifying the resumed device
1149 * @status: 0 iff the resume state change completed successfully, otherwise
1150 * a negative errno value indicating the reason for failure
1153 resume_state_change_complete(struct visor_device
*dev
, int status
)
1158 dev
->resuming
= false;
1161 * Notify the chipset driver that the resume is complete,
1162 * which will presumably want to send some sort of response to
1165 device_resume_response(dev
, status
);
1169 * initiate_chipset_device_pause_resume() - start a pause or resume operation
1170 * for a visor device
1171 * @dev: struct visor_device identifying the device being paused or resumed
1172 * @is_pause: true to indicate pause operation, false to indicate resume
1174 * Tell the subordinate function driver for a specific device to pause
1175 * or resume that device. Success/failure result is returned asynchronously
1176 * via a callback function; see pause_state_change_complete() and
1177 * resume_state_change_complete().
1180 initiate_chipset_device_pause_resume(struct visor_device
*dev
, bool is_pause
)
1183 struct visor_driver
*drv
= NULL
;
1185 drv
= to_visor_driver(dev
->device
.driver
);
1189 if (dev
->pausing
|| dev
->resuming
)
1196 dev
->pausing
= true;
1197 err
= drv
->pause(dev
, pause_state_change_complete
);
1199 /* The vbus_dev_info structure in the channel was been
1200 * cleared, make sure it is valid.
1202 fix_vbus_dev_info(dev
);
1206 dev
->resuming
= true;
1207 err
= drv
->resume(dev
, resume_state_change_complete
);
1214 * chipset_device_pause() - start a pause operation for a visor device
1215 * @dev_info: struct visor_device identifying the device being paused
1217 * Tell the subordinate function driver for a specific device to pause
1218 * that device. Success/failure result is returned asynchronously
1219 * via a callback function; see pause_state_change_complete().
1222 chipset_device_pause(struct visor_device
*dev_info
)
1226 err
= initiate_chipset_device_pause_resume(dev_info
, true);
1229 dev_info
->pausing
= false;
1237 * chipset_device_resume() - start a resume operation for a visor device
1238 * @dev_info: struct visor_device identifying the device being resumed
1240 * Tell the subordinate function driver for a specific device to resume
1241 * that device. Success/failure result is returned asynchronously
1242 * via a callback function; see resume_state_change_complete().
1245 chipset_device_resume(struct visor_device
*dev_info
)
1249 err
= initiate_chipset_device_pause_resume(dev_info
, false);
1252 dev_info
->resuming
= false;
1264 visorbus_debugfs_dir
= debugfs_create_dir("visorbus", NULL
);
1265 if (!visorbus_debugfs_dir
)
1268 bus_device_info_init(&clientbus_driverinfo
, "clientbus", "visorbus");
1270 err
= bus_register(&visorbus_type
);
1276 bus_device_info_init(&chipset_driverinfo
, "chipset", "visorchipset");
1284 struct list_head
*listentry
, *listtmp
;
1286 remove_all_visor_devices();
1288 list_for_each_safe(listentry
, listtmp
, &list_all_bus_instances
) {
1289 struct visor_device
*dev
= list_entry(listentry
,
1290 struct visor_device
,
1292 remove_bus_instance(dev
);
1295 bus_unregister(&visorbus_type
);
1296 initialized
= false;
1297 debugfs_remove_recursive(visorbus_debugfs_dir
);