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/uuid.h>
20 #include "visorbus_private.h"
21 #include "vmcallinterface.h"
23 #define MYDRVNAME "visorbus"
25 /* module parameters */
26 static int visorbus_forcematch;
27 static int visorbus_forcenomatch;
29 /* Display string that is guaranteed to be no longer the 99 characters*/
32 #define CURRENT_FILE_PC VISOR_BUS_PC_visorbus_main_c
33 #define POLLJIFFIES_NORMALCHANNEL 10
35 static int busreg_rc = -ENODEV; /* stores the result from bus registration */
38 * DEVICE type attributes
40 * The modalias file will contain the guid of the device.
42 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
45 struct visor_device *vdev;
48 vdev = to_visor_device(dev);
49 guid = visorchannel_get_uuid(vdev->visorchannel);
50 return snprintf(buf, PAGE_SIZE, "visorbus:%pUl\n", &guid);
52 static DEVICE_ATTR_RO(modalias);
54 static struct attribute *visorbus_dev_attrs[] = {
55 &dev_attr_modalias.attr,
59 /* sysfs example for bridge-only sysfs files using device_type's */
60 static const struct attribute_group visorbus_dev_group = {
61 .attrs = visorbus_dev_attrs,
64 static const struct attribute_group *visorbus_dev_groups[] = {
69 /* filled in with info about parent chipset driver when we register with it */
70 static struct ultra_vbus_deviceinfo chipset_driverinfo;
71 /* filled in with info about this driver, wrt it servicing client busses */
72 static struct ultra_vbus_deviceinfo clientbus_driverinfo;
74 /* list of visor_device structs, linked via .list_all */
75 static LIST_HEAD(list_all_bus_instances);
76 /* list of visor_device structs, linked via .list_all */
77 static LIST_HEAD(list_all_device_instances);
80 visorbus_uevent(struct device *xdev, struct kobj_uevent_env *env)
82 struct visor_device *dev;
85 dev = to_visor_device(xdev);
86 guid = visorchannel_get_uuid(dev->visorchannel);
88 if (add_uevent_var(env, "MODALIAS=visorbus:%pUl", &guid))
94 * visorbus_match() - called automatically upon adding a visor_device
95 * (device_add), or adding a visor_driver
96 * (visorbus_register_visor_driver)
97 * @xdev: struct device for the device being matched
98 * @xdrv: struct device_driver for driver to match device against
100 * Return: 1 iff the provided driver can control the specified device
103 visorbus_match(struct device *xdev, struct device_driver *xdrv)
105 uuid_le channel_type;
107 struct visor_device *dev;
108 struct visor_driver *drv;
110 dev = to_visor_device(xdev);
111 drv = to_visor_driver(xdrv);
112 channel_type = visorchannel_get_uuid(dev->visorchannel);
114 if (visorbus_forcematch)
116 if (visorbus_forcenomatch)
118 if (!drv->channel_types)
122 (uuid_le_cmp(drv->channel_types[i].guid, NULL_UUID_LE) != 0) ||
123 (drv->channel_types[i].name);
125 if (uuid_le_cmp(drv->channel_types[i].guid,
133 * This describes the TYPE of bus.
134 * (Don't confuse this with an INSTANCE of the bus.)
136 struct bus_type visorbus_type = {
138 .match = visorbus_match,
139 .uevent = visorbus_uevent,
140 .dev_groups = visorbus_dev_groups,
144 * visorbus_releae_busdevice() - called when device_unregister() is called for
145 * the bus device instance, after all other tasks
146 * involved with destroying the dev are complete
147 * @xdev: struct device for the bus being released
150 visorbus_release_busdevice(struct device *xdev)
152 struct visor_device *dev = dev_get_drvdata(xdev);
158 * visorbus_release_device() - called when device_unregister() is called for
159 * each child device instance
160 * @xdev: struct device for the visor device being released
163 visorbus_release_device(struct device *xdev)
165 struct visor_device *dev = to_visor_device(xdev);
167 if (dev->visorchannel) {
168 visorchannel_destroy(dev->visorchannel);
169 dev->visorchannel = NULL;
175 * begin implementation of specific channel attributes to appear under
176 * /sys/bus/visorbus<x>/dev<y>/channel
179 static ssize_t physaddr_show(struct device *dev, struct device_attribute *attr,
182 struct visor_device *vdev = to_visor_device(dev);
184 if (!vdev->visorchannel)
186 return snprintf(buf, PAGE_SIZE, "0x%llx\n",
187 visorchannel_get_physaddr(vdev->visorchannel));
190 static ssize_t nbytes_show(struct device *dev, struct device_attribute *attr,
193 struct visor_device *vdev = to_visor_device(dev);
195 if (!vdev->visorchannel)
197 return snprintf(buf, PAGE_SIZE, "0x%lx\n",
198 visorchannel_get_nbytes(vdev->visorchannel));
201 static ssize_t clientpartition_show(struct device *dev,
202 struct device_attribute *attr, char *buf)
204 struct visor_device *vdev = to_visor_device(dev);
206 if (!vdev->visorchannel)
208 return snprintf(buf, PAGE_SIZE, "0x%llx\n",
209 visorchannel_get_clientpartition(vdev->visorchannel));
212 static ssize_t typeguid_show(struct device *dev, struct device_attribute *attr,
215 struct visor_device *vdev = to_visor_device(dev);
216 char typeid[LINESIZE];
218 if (!vdev->visorchannel)
220 return snprintf(buf, PAGE_SIZE, "%s\n",
221 visorchannel_id(vdev->visorchannel, typeid));
224 static ssize_t zoneguid_show(struct device *dev, struct device_attribute *attr,
227 struct visor_device *vdev = to_visor_device(dev);
228 char zoneid[LINESIZE];
230 if (!vdev->visorchannel)
232 return snprintf(buf, PAGE_SIZE, "%s\n",
233 visorchannel_zoneid(vdev->visorchannel, zoneid));
236 static ssize_t typename_show(struct device *dev, struct device_attribute *attr,
239 struct visor_device *vdev = to_visor_device(dev);
241 struct bus_type *xbus = dev->bus;
242 struct device_driver *xdrv = dev->driver;
243 struct visor_driver *drv = NULL;
245 if (!vdev->visorchannel || !xbus || !xdrv)
247 i = xbus->match(dev, xdrv);
250 drv = to_visor_driver(xdrv);
251 return snprintf(buf, PAGE_SIZE, "%s\n", drv->channel_types[i - 1].name);
254 static DEVICE_ATTR_RO(physaddr);
255 static DEVICE_ATTR_RO(nbytes);
256 static DEVICE_ATTR_RO(clientpartition);
257 static DEVICE_ATTR_RO(typeguid);
258 static DEVICE_ATTR_RO(zoneguid);
259 static DEVICE_ATTR_RO(typename);
261 static struct attribute *channel_attrs[] = {
262 &dev_attr_physaddr.attr,
263 &dev_attr_nbytes.attr,
264 &dev_attr_clientpartition.attr,
265 &dev_attr_typeguid.attr,
266 &dev_attr_zoneguid.attr,
267 &dev_attr_typename.attr,
271 static struct attribute_group channel_attr_grp = {
273 .attrs = channel_attrs,
276 static const struct attribute_group *visorbus_channel_groups[] = {
281 /* end implementation of specific channel attributes */
284 * BUS instance attributes
286 * define & implement display of bus attributes under
287 * /sys/bus/visorbus/devices/visorbus<n>.
290 static ssize_t partition_handle_show(struct device *dev,
291 struct device_attribute *attr,
293 struct visor_device *vdev = to_visor_device(dev);
294 u64 handle = visorchannel_get_clientpartition(vdev->visorchannel);
296 return snprintf(buf, PAGE_SIZE, "0x%llx\n", handle);
299 static ssize_t partition_guid_show(struct device *dev,
300 struct device_attribute *attr,
302 struct visor_device *vdev = to_visor_device(dev);
304 return snprintf(buf, PAGE_SIZE, "{%pUb}\n", &vdev->partition_uuid);
307 static ssize_t partition_name_show(struct device *dev,
308 struct device_attribute *attr,
310 struct visor_device *vdev = to_visor_device(dev);
312 return snprintf(buf, PAGE_SIZE, "%s\n", vdev->name);
315 static ssize_t channel_addr_show(struct device *dev,
316 struct device_attribute *attr,
318 struct visor_device *vdev = to_visor_device(dev);
319 u64 addr = visorchannel_get_physaddr(vdev->visorchannel);
321 return snprintf(buf, PAGE_SIZE, "0x%llx\n", addr);
324 static ssize_t channel_bytes_show(struct device *dev,
325 struct device_attribute *attr,
327 struct visor_device *vdev = to_visor_device(dev);
328 u64 nbytes = visorchannel_get_nbytes(vdev->visorchannel);
330 return snprintf(buf, PAGE_SIZE, "0x%llx\n", nbytes);
333 static ssize_t channel_id_show(struct device *dev,
334 struct device_attribute *attr,
336 struct visor_device *vdev = to_visor_device(dev);
339 if (vdev->visorchannel) {
340 visorchannel_id(vdev->visorchannel, buf);
347 static ssize_t client_bus_info_show(struct device *dev,
348 struct device_attribute *attr,
350 struct visor_device *vdev = to_visor_device(dev);
351 struct visorchannel *channel = vdev->visorchannel;
353 int i, shift, remain = PAGE_SIZE;
357 struct ultra_vbus_deviceinfo dev_info;
362 partition_name = vdev->name;
363 shift = snprintf(pos, remain,
364 "Client device / client driver info for %s partition (vbus #%u):\n",
365 partition_name, vdev->chipset_bus_no);
368 shift = visorchannel_read(channel,
370 spar_vbus_channel_protocol,
372 &dev_info, sizeof(dev_info));
374 shift = vbuschannel_devinfo_to_string(&dev_info, pos,
379 shift = visorchannel_read(channel,
381 spar_vbus_channel_protocol,
383 &dev_info, sizeof(dev_info));
385 shift = vbuschannel_devinfo_to_string(&dev_info, pos,
390 off = offsetof(struct spar_vbus_channel_protocol, dev_info);
392 while (off + sizeof(dev_info) <=
393 visorchannel_get_nbytes(channel)) {
394 shift = visorchannel_read(channel,
398 shift = vbuschannel_devinfo_to_string
399 (&dev_info, pos, remain, i);
403 off += sizeof(dev_info);
407 return PAGE_SIZE - remain;
410 static DEVICE_ATTR_RO(partition_handle);
411 static DEVICE_ATTR_RO(partition_guid);
412 static DEVICE_ATTR_RO(partition_name);
413 static DEVICE_ATTR_RO(channel_addr);
414 static DEVICE_ATTR_RO(channel_bytes);
415 static DEVICE_ATTR_RO(channel_id);
416 static DEVICE_ATTR_RO(client_bus_info);
418 static struct attribute *dev_attrs[] = {
419 &dev_attr_partition_handle.attr,
420 &dev_attr_partition_guid.attr,
421 &dev_attr_partition_name.attr,
422 &dev_attr_channel_addr.attr,
423 &dev_attr_channel_bytes.attr,
424 &dev_attr_channel_id.attr,
425 &dev_attr_client_bus_info.attr,
429 static struct attribute_group dev_attr_grp = {
433 static const struct attribute_group *visorbus_groups[] = {
439 dev_periodic_work(unsigned long __opaque)
441 struct visor_device *dev = (struct visor_device *)__opaque;
442 struct visor_driver *drv = to_visor_driver(dev->device.driver);
444 if (drv->channel_interrupt)
445 drv->channel_interrupt(dev);
446 mod_timer(&dev->timer, jiffies + POLLJIFFIES_NORMALCHANNEL);
450 dev_start_periodic_work(struct visor_device *dev)
452 if (dev->being_removed || dev->timer_active)
454 /* now up by at least 2 */
455 get_device(&dev->device);
456 dev->timer.expires = jiffies + POLLJIFFIES_NORMALCHANNEL;
457 add_timer(&dev->timer);
458 dev->timer_active = true;
462 dev_stop_periodic_work(struct visor_device *dev)
464 if (!dev->timer_active)
466 del_timer_sync(&dev->timer);
467 dev->timer_active = false;
468 put_device(&dev->device);
472 * visordriver_remove_device() - handle visor device going away
473 * @xdev: struct device for the visor device being removed
475 * This is called when device_unregister() is called for each child device
476 * instance, to notify the appropriate visorbus function driver that the device
477 * is going away, and to decrease the reference count of the device.
479 * Return: 0 iff successful
482 visordriver_remove_device(struct device *xdev)
484 struct visor_device *dev;
485 struct visor_driver *drv;
487 dev = to_visor_device(xdev);
488 drv = to_visor_driver(xdev->driver);
489 mutex_lock(&dev->visordriver_callback_lock);
490 dev->being_removed = true;
493 mutex_unlock(&dev->visordriver_callback_lock);
494 dev_stop_periodic_work(dev);
496 put_device(&dev->device);
501 * visorbus_unregister_visor_driver() - unregisters the provided driver
502 * @drv: the driver to unregister
504 * A visor function driver calls this function to unregister the driver,
505 * i.e., within its module_exit function.
508 visorbus_unregister_visor_driver(struct visor_driver *drv)
510 driver_unregister(&drv->driver);
512 EXPORT_SYMBOL_GPL(visorbus_unregister_visor_driver);
515 * visorbus_read_channel() - reads from the designated channel into
516 * the provided buffer
517 * @dev: the device whose channel is read from
518 * @offset: the offset into the channel at which reading starts
519 * @dest: the destination buffer that is written into from the channel
520 * @nbytes: the number of bytes to read from the channel
522 * If receiving a message, use the visorchannel_signalremove()
525 * Return: integer indicating success (zero) or failure (non-zero)
528 visorbus_read_channel(struct visor_device *dev, unsigned long offset,
529 void *dest, unsigned long nbytes)
531 return visorchannel_read(dev->visorchannel, offset, dest, nbytes);
533 EXPORT_SYMBOL_GPL(visorbus_read_channel);
536 * visorbus_write_channel() - writes the provided buffer into the designated
538 * @dev: the device whose channel is written to
539 * @offset: the offset into the channel at which writing starts
540 * @src: the source buffer that is written into the channel
541 * @nbytes: the number of bytes to write into the channel
543 * If sending a message, use the visorchannel_signalinsert()
546 * Return: integer indicating success (zero) or failure (non-zero)
549 visorbus_write_channel(struct visor_device *dev, unsigned long offset,
550 void *src, unsigned long nbytes)
552 return visorchannel_write(dev->visorchannel, offset, src, nbytes);
554 EXPORT_SYMBOL_GPL(visorbus_write_channel);
557 * visorbus_enable_channel_interrupts() - enables interrupts on the
559 * @dev: the device on which to enable interrupts
561 * Currently we don't yet have a real interrupt, so for now we just call the
562 * interrupt function periodically via a timer.
565 visorbus_enable_channel_interrupts(struct visor_device *dev)
567 dev_start_periodic_work(dev);
569 EXPORT_SYMBOL_GPL(visorbus_enable_channel_interrupts);
572 * visorbus_disable_channel_interrupts() - disables interrupts on the
574 * @dev: the device on which to disable interrupts
577 visorbus_disable_channel_interrupts(struct visor_device *dev)
579 dev_stop_periodic_work(dev);
581 EXPORT_SYMBOL_GPL(visorbus_disable_channel_interrupts);
584 * create_visor_device() - create visor device as a result of receiving the
585 * controlvm device_create message for a new device
586 * @dev: a freshly-zeroed struct visor_device, containing only filled-in values
587 * for chipset_bus_no and chipset_dev_no, that will be initialized
589 * This is how everything starts from the device end.
590 * This function is called when a channel first appears via a ControlVM
591 * message. In response, this function allocates a visor_device to
592 * correspond to the new channel, and attempts to connect it the appropriate
593 * driver. If the appropriate driver is found, the visor_driver.probe()
594 * function for that driver will be called, and will be passed the new
595 * visor_device that we just created.
597 * It's ok if the appropriate driver is not yet loaded, because in that case
598 * the new device struct will just stick around in the bus' list of devices.
599 * When the appropriate driver calls visorbus_register_visor_driver(), the
600 * visor_driver.probe() for the new driver will be called with the new
603 * Return: 0 if successful, otherwise the negative value returned by
604 * device_add() indicating the reason for failure
607 create_visor_device(struct visor_device *dev)
610 u32 chipset_bus_no = dev->chipset_bus_no;
611 u32 chipset_dev_no = dev->chipset_dev_no;
613 POSTCODE_LINUX_4(DEVICE_CREATE_ENTRY_PC, chipset_dev_no, chipset_bus_no,
614 POSTCODE_SEVERITY_INFO);
616 mutex_init(&dev->visordriver_callback_lock);
617 dev->device.bus = &visorbus_type;
618 dev->device.groups = visorbus_channel_groups;
619 device_initialize(&dev->device);
620 dev->device.release = visorbus_release_device;
621 /* keep a reference just for us (now 2) */
622 get_device(&dev->device);
623 init_timer(&dev->timer);
624 dev->timer.data = (unsigned long)(dev);
625 dev->timer.function = dev_periodic_work;
628 * bus_id must be a unique name with respect to this bus TYPE
629 * (NOT bus instance). That's why we need to include the bus
630 * number within the name.
632 dev_set_name(&dev->device, "vbus%u:dev%u",
633 chipset_bus_no, chipset_dev_no);
636 * device_add does this:
637 * bus_add_device(dev)
638 * ->device_attach(dev)
639 * ->for each driver drv registered on the bus that dev is on
640 * if (dev.drv) ** device already has a driver **
641 * ** not sure we could ever get here... **
643 * if (bus.match(dev,drv)) [visorbus_match]
645 * if (!drv.probe(dev)) [visordriver_probe_device]
648 * Note that device_add does NOT fail if no driver failed to
649 * claim the device. The device will be linked onto
650 * bus_type.klist_devices regardless (use bus_for_each_dev).
652 err = device_add(&dev->device);
654 POSTCODE_LINUX_3(DEVICE_ADD_PC, chipset_bus_no,
659 list_add_tail(&dev->list_all, &list_all_device_instances);
660 return 0; /* success: reference kept via unmatched get_device() */
663 put_device(&dev->device);
668 remove_visor_device(struct visor_device *dev)
670 list_del(&dev->list_all);
671 put_device(&dev->device);
672 device_unregister(&dev->device);
676 get_vbus_header_info(struct visorchannel *chan,
677 struct spar_vbus_headerinfo *hdr_info)
679 if (!SPAR_VBUS_CHANNEL_OK_CLIENT(visorchannel_get_header(chan)))
682 if (visorchannel_read(chan, sizeof(struct channel_header), hdr_info,
683 sizeof(*hdr_info)) < 0) {
686 if (hdr_info->struct_bytes < sizeof(struct spar_vbus_headerinfo))
689 if (hdr_info->device_info_struct_bytes <
690 sizeof(struct ultra_vbus_deviceinfo)) {
697 * write_vbus_chp_info() - write the contents of <info> to the struct
698 * spar_vbus_channel_protocol.chp_info
699 * @chan: indentifies the s-Par channel that will be updated
700 * @hdr_info: used to find appropriate channel offset to write data
701 * @info: contains the information to write
703 * Writes chipset info into the channel memory to be used for diagnostic
706 * Returns no value since this is debug information and not needed for
707 * device functionality.
710 write_vbus_chp_info(struct visorchannel *chan,
711 struct spar_vbus_headerinfo *hdr_info,
712 struct ultra_vbus_deviceinfo *info)
714 int off = sizeof(struct channel_header) + hdr_info->chp_info_offset;
716 if (hdr_info->chp_info_offset == 0)
719 visorchannel_write(chan, off, info, sizeof(*info));
723 * write_vbus_bus_info() - write the contents of <info> to the struct
724 * spar_vbus_channel_protocol.bus_info
725 * @chan: indentifies the s-Par channel that will be updated
726 * @hdr_info: used to find appropriate channel offset to write data
727 * @info: contains the information to write
729 * Writes bus info into the channel memory to be used for diagnostic
732 * Returns no value since this is debug information and not needed for
733 * device functionality.
736 write_vbus_bus_info(struct visorchannel *chan,
737 struct spar_vbus_headerinfo *hdr_info,
738 struct ultra_vbus_deviceinfo *info)
740 int off = sizeof(struct channel_header) + hdr_info->bus_info_offset;
742 if (hdr_info->bus_info_offset == 0)
745 visorchannel_write(chan, off, info, sizeof(*info));
749 * write_vbus_dev_info() - write the contents of <info> to the struct
750 * spar_vbus_channel_protocol.dev_info[<devix>]
751 * @chan: indentifies the s-Par channel that will be updated
752 * @hdr_info: used to find appropriate channel offset to write data
753 * @info: contains the information to write
754 * @devix: the relative device number (0..n-1) of the device on the bus
756 * Writes device info into the channel memory to be used for diagnostic
759 * Returns no value since this is debug information and not needed for
760 * device functionality.
763 write_vbus_dev_info(struct visorchannel *chan,
764 struct spar_vbus_headerinfo *hdr_info,
765 struct ultra_vbus_deviceinfo *info, unsigned int devix)
768 (sizeof(struct channel_header) + hdr_info->dev_info_offset) +
769 (hdr_info->device_info_struct_bytes * devix);
771 if (hdr_info->dev_info_offset == 0)
774 visorchannel_write(chan, off, info, sizeof(*info));
778 * fix_vbus_dev_info() - for a child device just created on a client bus, fill
779 * in information about the driver that is controlling
780 * this device into the the appropriate slot within the
781 * vbus channel of the bus instance
782 * @visordev: struct visor_device for the desired device
785 fix_vbus_dev_info(struct visor_device *visordev)
788 struct visor_device *bdev;
789 struct visor_driver *visordrv;
790 u32 bus_no = visordev->chipset_bus_no;
791 u32 dev_no = visordev->chipset_dev_no;
792 struct ultra_vbus_deviceinfo dev_info;
793 const char *chan_type_name = NULL;
794 struct spar_vbus_headerinfo *hdr_info;
796 if (!visordev->device.driver)
799 bdev = visorbus_get_device_by_id(bus_no, BUS_ROOT_DEVICE, NULL);
802 hdr_info = (struct spar_vbus_headerinfo *)bdev->vbus_hdr_info;
805 visordrv = to_visor_driver(visordev->device.driver);
808 * Within the list of device types (by GUID) that the driver
809 * says it supports, find out which one of those types matches
810 * the type of this device, so that we can include the device
813 for (i = 0; visordrv->channel_types[i].name; i++) {
814 if (memcmp(&visordrv->channel_types[i].guid,
815 &visordev->channel_type_guid,
816 sizeof(visordrv->channel_types[i].guid)) == 0) {
817 chan_type_name = visordrv->channel_types[i].name;
822 bus_device_info_init(&dev_info, chan_type_name, visordrv->name);
823 write_vbus_dev_info(bdev->visorchannel, hdr_info, &dev_info, dev_no);
826 * Re-write bus+chipset info, because it is possible that this
827 * was previously written by our evil counterpart, virtpci.
829 write_vbus_chp_info(bdev->visorchannel, hdr_info, &chipset_driverinfo);
830 write_vbus_bus_info(bdev->visorchannel, hdr_info,
831 &clientbus_driverinfo);
835 * visordriver_probe_device() - handle new visor device coming online
836 * @xdev: struct device for the visor device being probed
838 * This is called automatically upon adding a visor_device (device_add), or
839 * adding a visor_driver (visorbus_register_visor_driver), but only after
840 * visorbus_match() has returned 1 to indicate a successful match between
843 * If successful, a reference to the device will be held onto via get_device().
845 * Return: 0 if successful, meaning the function driver's probe() function
846 * was successful with this device, otherwise a negative errno
847 * value indicating failure reason
850 visordriver_probe_device(struct device *xdev)
853 struct visor_driver *drv;
854 struct visor_device *dev;
856 drv = to_visor_driver(xdev->driver);
857 dev = to_visor_device(xdev);
862 mutex_lock(&dev->visordriver_callback_lock);
863 dev->being_removed = false;
865 res = drv->probe(dev);
867 /* success: reference kept via unmatched get_device() */
868 get_device(&dev->device);
869 fix_vbus_dev_info(dev);
872 mutex_unlock(&dev->visordriver_callback_lock);
877 * visorbus_register_visor_driver() - registers the provided visor driver
878 * for handling one or more visor device
879 * types (channel_types)
880 * @drv: the driver to register
882 * A visor function driver calls this function to register
883 * the driver. The caller MUST fill in the following fields within the
885 * name, version, owner, channel_types, probe, remove
887 * Here's how the whole Linux bus / driver / device model works.
889 * At system start-up, the visorbus kernel module is loaded, which registers
890 * visorbus_type as a bus type, using bus_register().
892 * All kernel modules that support particular device types on a
893 * visorbus bus are loaded. Each of these kernel modules calls
894 * visorbus_register_visor_driver() in their init functions, passing a
895 * visor_driver struct. visorbus_register_visor_driver() in turn calls
896 * register_driver(&visor_driver.driver). This .driver member is
897 * initialized with generic methods (like probe), whose sole responsibility
898 * is to act as a broker for the real methods, which are within the
899 * visor_driver struct. (This is the way the subclass behavior is
900 * implemented, since visor_driver is essentially a subclass of the
901 * generic driver.) Whenever a driver_register() happens, core bus code in
902 * the kernel does (see device_attach() in drivers/base/dd.c):
904 * for each dev associated with the bus (the bus that driver is on) that
905 * does not yet have a driver
906 * if bus.match(dev,newdriver) == yes_matched ** .match specified
907 * ** during bus_register().
908 * newdriver.probe(dev) ** for visor drivers, this will call
909 * ** the generic driver.probe implemented in visorbus.c,
910 * ** which in turn calls the probe specified within the
911 * ** struct visor_driver (which was specified by the
912 * ** actual device driver as part of
913 * ** visorbus_register_visor_driver()).
915 * The above dance also happens when a new device appears.
916 * So the question is, how are devices created within the system?
917 * Basically, just call device_add(dev). See pci_bus_add_devices().
918 * pci_scan_device() shows an example of how to build a device struct. It
919 * returns the newly-created struct to pci_scan_single_device(), who adds it
920 * to the list of devices at PCIBUS.devices. That list of devices is what
921 * is traversed by pci_bus_add_devices().
923 * Return: integer indicating success (zero) or failure (non-zero)
925 int visorbus_register_visor_driver(struct visor_driver *drv)
930 return -ENODEV; /*can't register on a nonexistent bus*/
932 drv->driver.name = drv->name;
933 drv->driver.bus = &visorbus_type;
934 drv->driver.probe = visordriver_probe_device;
935 drv->driver.remove = visordriver_remove_device;
936 drv->driver.owner = drv->owner;
939 * driver_register does this:
940 * bus_add_driver(drv)
941 * ->if (drv.bus) ** (bus_type) **
943 * for each dev with bus type of drv.bus
944 * if (!dev.drv) ** no driver assigned yet **
945 * if (bus.match(dev,drv)) [visorbus_match]
947 * if (!drv.probe(dev)) [visordriver_probe_device]
951 rc = driver_register(&drv->driver);
953 driver_unregister(&drv->driver);
956 EXPORT_SYMBOL_GPL(visorbus_register_visor_driver);
959 * create_bus_instance() - create a device instance for the visor bus itself
960 * @dev: struct visor_device indicating the bus instance
962 * Return: 0 for success, otherwise negative errno value indicating reason for
966 create_bus_instance(struct visor_device *dev)
968 int id = dev->chipset_bus_no;
969 struct spar_vbus_headerinfo *hdr_info;
971 POSTCODE_LINUX_2(BUS_CREATE_ENTRY_PC, POSTCODE_SEVERITY_INFO);
973 hdr_info = kzalloc(sizeof(*hdr_info), GFP_KERNEL);
977 dev_set_name(&dev->device, "visorbus%d", id);
978 dev->device.bus = &visorbus_type;
979 dev->device.groups = visorbus_groups;
980 dev->device.release = visorbus_release_busdevice;
982 if (device_register(&dev->device) < 0) {
983 POSTCODE_LINUX_3(DEVICE_CREATE_FAILURE_PC, id,
984 POSTCODE_SEVERITY_ERR);
989 if (get_vbus_header_info(dev->visorchannel, hdr_info) >= 0) {
990 dev->vbus_hdr_info = (void *)hdr_info;
991 write_vbus_chp_info(dev->visorchannel, hdr_info,
992 &chipset_driverinfo);
993 write_vbus_bus_info(dev->visorchannel, hdr_info,
994 &clientbus_driverinfo);
998 list_add_tail(&dev->list_all, &list_all_bus_instances);
999 dev_set_drvdata(&dev->device, dev);
1004 * remove_bus_instance() - remove a device instance for the visor bus itself
1005 * @dev: struct visor_device indentifying the bus to remove
1008 remove_bus_instance(struct visor_device *dev)
1011 * Note that this will result in the release method for
1012 * dev->dev being called, which will call
1013 * visorbus_release_busdevice(). This has something to do with
1014 * the put_device() done in device_unregister(), but I have never
1015 * successfully been able to trace thru the code to see where/how
1016 * release() gets called. But I know it does.
1018 if (dev->visorchannel) {
1019 visorchannel_destroy(dev->visorchannel);
1020 dev->visorchannel = NULL;
1022 kfree(dev->vbus_hdr_info);
1023 list_del(&dev->list_all);
1024 device_unregister(&dev->device);
1028 * create_bus_type() - create and register the one-and-only one instance of
1029 * the visor bus type (visorbus_type)
1030 * Return: 0 for success, otherwise negative errno value returned by
1031 * bus_register() indicating the reason for failure
1034 create_bus_type(void)
1036 busreg_rc = bus_register(&visorbus_type);
1041 * remove_bus_type() - remove the one-and-only one instance of the visor bus
1042 * type (visorbus_type)
1045 remove_bus_type(void)
1047 bus_unregister(&visorbus_type);
1051 * remove_all_visor_devices() - remove all child visor bus device instances
1054 remove_all_visor_devices(void)
1056 struct list_head *listentry, *listtmp;
1058 list_for_each_safe(listentry, listtmp, &list_all_device_instances) {
1059 struct visor_device *dev = list_entry(listentry,
1060 struct visor_device,
1062 remove_visor_device(dev);
1067 chipset_bus_create(struct visor_device *dev)
1070 u32 bus_no = dev->chipset_bus_no;
1072 POSTCODE_LINUX_3(BUS_CREATE_ENTRY_PC, bus_no, POSTCODE_SEVERITY_INFO);
1073 rc = create_bus_instance(dev);
1074 POSTCODE_LINUX_3(BUS_CREATE_EXIT_PC, bus_no, POSTCODE_SEVERITY_INFO);
1077 POSTCODE_LINUX_3(BUS_CREATE_FAILURE_PC, bus_no,
1078 POSTCODE_SEVERITY_ERR);
1080 POSTCODE_LINUX_3(CHIPSET_INIT_SUCCESS_PC, bus_no,
1081 POSTCODE_SEVERITY_INFO);
1083 bus_create_response(dev, rc);
1087 chipset_bus_destroy(struct visor_device *dev)
1089 remove_bus_instance(dev);
1090 bus_destroy_response(dev, 0);
1094 chipset_device_create(struct visor_device *dev_info)
1097 u32 bus_no = dev_info->chipset_bus_no;
1098 u32 dev_no = dev_info->chipset_dev_no;
1100 POSTCODE_LINUX_4(DEVICE_CREATE_ENTRY_PC, dev_no, bus_no,
1101 POSTCODE_SEVERITY_INFO);
1103 rc = create_visor_device(dev_info);
1104 device_create_response(dev_info, rc);
1107 POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, dev_no, bus_no,
1108 POSTCODE_SEVERITY_ERR);
1110 POSTCODE_LINUX_4(DEVICE_CREATE_SUCCESS_PC, dev_no, bus_no,
1111 POSTCODE_SEVERITY_INFO);
1115 chipset_device_destroy(struct visor_device *dev_info)
1117 remove_visor_device(dev_info);
1119 device_destroy_response(dev_info, 0);
1123 * pause_state_change_complete() - the callback function to be called by a
1124 * visorbus function driver when a
1125 * pending "pause device" operation has
1127 * @dev: struct visor_device identifying the paused device
1128 * @status: 0 iff the pause state change completed successfully, otherwise
1129 * a negative errno value indicating the reason for failure
1132 pause_state_change_complete(struct visor_device *dev, int status)
1137 dev->pausing = false;
1139 device_pause_response(dev, status);
1143 * resume_state_change_complete() - the callback function to be called by a
1144 * visorbus function driver when a
1145 * pending "resume device" operation has
1147 * @dev: struct visor_device identifying the resumed device
1148 * @status: 0 iff the resume state change completed successfully, otherwise
1149 * a negative errno value indicating the reason for failure
1152 resume_state_change_complete(struct visor_device *dev, int status)
1157 dev->resuming = false;
1160 * Notify the chipset driver that the resume is complete,
1161 * which will presumably want to send some sort of response to
1164 device_resume_response(dev, status);
1168 * initiate_chipset_device_pause_resume() - start a pause or resume operation
1169 * for a visor device
1170 * @dev: struct visor_device identifying the device being paused or resumed
1171 * @is_pause: true to indicate pause operation, false to indicate resume
1173 * Tell the subordinate function driver for a specific device to pause
1174 * or resume that device. Success/failure result is returned asynchronously
1175 * via a callback function; see pause_state_change_complete() and
1176 * resume_state_change_complete().
1179 initiate_chipset_device_pause_resume(struct visor_device *dev, bool is_pause)
1182 struct visor_driver *drv = NULL;
1183 void (*notify_func)(struct visor_device *dev, int response) = NULL;
1186 notify_func = device_pause_response;
1188 notify_func = device_resume_response;
1192 drv = to_visor_driver(dev->device.driver);
1194 (*notify_func)(dev, -ENODEV);
1198 if (dev->pausing || dev->resuming) {
1199 (*notify_func)(dev, -EBUSY);
1204 * Note that even though both drv->pause() and drv->resume
1205 * specify a callback function, it is NOT necessary for us to
1206 * increment our local module usage count. Reason is, there
1207 * is already a linkage dependency between child function
1208 * drivers and visorbus, so it is already IMPOSSIBLE to unload
1209 * visorbus while child function drivers are still running.
1213 (*notify_func)(dev, -EINVAL);
1217 dev->pausing = true;
1218 rc = drv->pause(dev, pause_state_change_complete);
1220 /* This should be done at BUS resume time, but an
1221 * existing problem prevents us from ever getting a bus
1222 * resume... This hack would fail to work should we
1223 * ever have a bus that contains NO devices, since we
1224 * would never even get here in that case.
1226 fix_vbus_dev_info(dev);
1228 (*notify_func)(dev, -EINVAL);
1232 dev->resuming = true;
1233 rc = drv->resume(dev, resume_state_change_complete);
1237 dev->pausing = false;
1239 dev->resuming = false;
1240 (*notify_func)(dev, -EINVAL);
1245 * chipset_device_pause() - start a pause operation for a visor device
1246 * @dev_info: struct visor_device identifying the device being paused
1248 * Tell the subordinate function driver for a specific device to pause
1249 * that device. Success/failure result is returned asynchronously
1250 * via a callback function; see pause_state_change_complete().
1253 chipset_device_pause(struct visor_device *dev_info)
1255 initiate_chipset_device_pause_resume(dev_info, true);
1259 * chipset_device_resume() - start a resume operation for a visor device
1260 * @dev_info: struct visor_device identifying the device being resumed
1262 * Tell the subordinate function driver for a specific device to resume
1263 * that device. Success/failure result is returned asynchronously
1264 * via a callback function; see resume_state_change_complete().
1267 chipset_device_resume(struct visor_device *dev_info)
1269 initiate_chipset_device_pause_resume(dev_info, false);
1277 POSTCODE_LINUX_3(DRIVER_ENTRY_PC, 0, POSTCODE_SEVERITY_INFO);
1278 bus_device_info_init(&clientbus_driverinfo, "clientbus", "visorbus");
1280 err = create_bus_type();
1282 POSTCODE_LINUX_2(BUS_CREATE_ENTRY_PC, DIAG_SEVERITY_ERR);
1286 bus_device_info_init(&chipset_driverinfo, "chipset", "visorchipset");
1291 POSTCODE_LINUX_3(CHIPSET_INIT_FAILURE_PC, err, POSTCODE_SEVERITY_ERR);
1298 struct list_head *listentry, *listtmp;
1300 remove_all_visor_devices();
1302 list_for_each_safe(listentry, listtmp, &list_all_bus_instances) {
1303 struct visor_device *dev = list_entry(listentry,
1304 struct visor_device,
1306 remove_bus_instance(dev);
1311 module_param_named(forcematch, visorbus_forcematch, int, S_IRUGO);
1312 MODULE_PARM_DESC(visorbus_forcematch,
1313 "1 to force a successful dev <--> drv match");
1315 module_param_named(forcenomatch, visorbus_forcenomatch, int, S_IRUGO);
1316 MODULE_PARM_DESC(visorbus_forcenomatch,
1317 "1 to force an UNsuccessful dev <--> drv match");