Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *  bus driver for ccwgroup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *  Copyright IBM Corp. 2002, 2012
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *  Author(s): Arnd Bergmann (arndb@de.ibm.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *	       Cornelia Huck (cornelia.huck@de.ibm.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/dcache.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <asm/cio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <asm/ccwdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <asm/ccwgroup.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include "device.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define CCW_BUS_ID_SIZE		10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) /* In Linux 2.4, we had a channel device layer called "chandev"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * that did all sorts of obscure stuff for networking devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * This is another driver that serves as a replacement for just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * one of its functions, namely the translation of single subchannels
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * to devices that use multiple subchannels.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) static struct bus_type ccwgroup_bus_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) static void __ccwgroup_remove_symlinks(struct ccwgroup_device *gdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	char str[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	for (i = 0; i < gdev->count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		sprintf(str, "cdev%d", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		sysfs_remove_link(&gdev->dev.kobj, str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		sysfs_remove_link(&gdev->cdev[i]->dev.kobj, "group_device");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  * Remove references from ccw devices to ccw group device and from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  * ccw group device to ccw devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) static void __ccwgroup_remove_cdev_refs(struct ccwgroup_device *gdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	struct ccw_device *cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	for (i = 0; i < gdev->count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		cdev = gdev->cdev[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		if (!cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		spin_lock_irq(cdev->ccwlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		dev_set_drvdata(&cdev->dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		spin_unlock_irq(cdev->ccwlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		gdev->cdev[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		put_device(&cdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70)  * ccwgroup_set_online() - enable a ccwgroup device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71)  * @gdev: target ccwgroup device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73)  * This function attempts to put the ccwgroup device into the online state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74)  * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75)  *  %0 on success and a negative error value on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) int ccwgroup_set_online(struct ccwgroup_device *gdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	int ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	if (gdev->state == CCWGROUP_ONLINE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	if (gdrv->set_online)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		ret = gdrv->set_online(gdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	gdev->state = CCWGROUP_ONLINE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	atomic_set(&gdev->onoff, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) EXPORT_SYMBOL(ccwgroup_set_online);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99)  * ccwgroup_set_offline() - disable a ccwgroup device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)  * @gdev: target ccwgroup device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)  * This function attempts to put the ccwgroup device into the offline state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)  * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)  *  %0 on success and a negative error value on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) int ccwgroup_set_offline(struct ccwgroup_device *gdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	int ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	if (gdev->state == CCWGROUP_OFFLINE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	if (gdrv->set_offline)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		ret = gdrv->set_offline(gdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	gdev->state = CCWGROUP_OFFLINE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	atomic_set(&gdev->onoff, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) EXPORT_SYMBOL(ccwgroup_set_offline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static ssize_t ccwgroup_online_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 				     struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 				     const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	unsigned long value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	device_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	if (!dev->driver) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	ret = kstrtoul(buf, 0, &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	if (value == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		ret = ccwgroup_set_online(gdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	else if (value == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		ret = ccwgroup_set_offline(gdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	device_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	return (ret == 0) ? count : ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) static ssize_t ccwgroup_online_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 				    struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 				    char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	int online;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	online = (gdev->state == CCWGROUP_ONLINE) ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	return scnprintf(buf, PAGE_SIZE, "%d\n", online);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)  * Provide an 'ungroup' attribute so the user can remove group devices no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)  * longer needed or accidentially created. Saves memory :)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static void ccwgroup_ungroup(struct ccwgroup_device *gdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	mutex_lock(&gdev->reg_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	if (device_is_registered(&gdev->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		__ccwgroup_remove_symlinks(gdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		device_unregister(&gdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		__ccwgroup_remove_cdev_refs(gdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	mutex_unlock(&gdev->reg_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static ssize_t ccwgroup_ungroup_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 				      struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 				      const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	/* Prevent concurrent online/offline processing and ungrouping. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	if (gdev->state != CCWGROUP_OFFLINE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	if (device_remove_file_self(dev, attr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		ccwgroup_ungroup(gdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		rc = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		/* Release onoff "lock" when ungrouping failed. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		atomic_set(&gdev->onoff, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) static DEVICE_ATTR(ungroup, 0200, NULL, ccwgroup_ungroup_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) static struct attribute *ccwgroup_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	&dev_attr_online.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	&dev_attr_ungroup.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static struct attribute_group ccwgroup_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	.attrs = ccwgroup_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) static const struct attribute_group *ccwgroup_attr_groups[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	&ccwgroup_attr_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) static void ccwgroup_ungroup_workfn(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	struct ccwgroup_device *gdev =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		container_of(work, struct ccwgroup_device, ungroup_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	ccwgroup_ungroup(gdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	put_device(&gdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) static void ccwgroup_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	kfree(to_ccwgroupdev(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) static int __ccwgroup_create_symlinks(struct ccwgroup_device *gdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	char str[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	int i, rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	for (i = 0; i < gdev->count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		rc = sysfs_create_link(&gdev->cdev[i]->dev.kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 				       &gdev->dev.kobj, "group_device");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 			for (--i; i >= 0; i--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 				sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 						  "group_device");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	for (i = 0; i < gdev->count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		sprintf(str, "cdev%d", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		rc = sysfs_create_link(&gdev->dev.kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 				       &gdev->cdev[i]->dev.kobj, str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 		if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 			for (--i; i >= 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 				sprintf(str, "cdev%d", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 				sysfs_remove_link(&gdev->dev.kobj, str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 			for (i = 0; i < gdev->count; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 				sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 						  "group_device");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 			return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) static int __get_next_id(const char **buf, struct ccw_dev_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	unsigned int cssid, ssid, devno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	int ret = 0, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	char *start, *end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	start = (char *)*buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	end = strchr(start, ',');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	if (!end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		/* Last entry. Strip trailing newline, if applicable. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		end = strchr(start, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		if (end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 			*end = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		len = strlen(start) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		len = end - start + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		end++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	if (len <= CCW_BUS_ID_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		if (sscanf(start, "%2x.%1x.%04x", &cssid, &ssid, &devno) != 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		id->ssid = ssid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		id->devno = devno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	*buf = end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)  * ccwgroup_create_dev() - create and register a ccw group device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)  * @parent: parent device for the new device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)  * @gdrv: driver for the new group device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)  * @num_devices: number of slave devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)  * @buf: buffer containing comma separated bus ids of slave devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)  * Create and register a new ccw group device as a child of @parent. Slave
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)  * devices are obtained from the list of bus ids given in @buf.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)  * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)  *  %0 on success and an error code on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)  * Context:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)  *  non-atomic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) int ccwgroup_create_dev(struct device *parent, struct ccwgroup_driver *gdrv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 			int num_devices, const char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	struct ccwgroup_device *gdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	struct ccw_dev_id dev_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	int rc, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	if (num_devices < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	gdev = kzalloc(struct_size(gdev, cdev, num_devices), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	if (!gdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	atomic_set(&gdev->onoff, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	mutex_init(&gdev->reg_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	mutex_lock(&gdev->reg_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	INIT_WORK(&gdev->ungroup_work, ccwgroup_ungroup_workfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	gdev->count = num_devices;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	gdev->dev.bus = &ccwgroup_bus_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	gdev->dev.parent = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	gdev->dev.release = ccwgroup_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	device_initialize(&gdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	for (i = 0; i < num_devices && buf; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		rc = __get_next_id(&buf, &dev_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		if (rc != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		gdev->cdev[i] = get_ccwdev_by_dev_id(&dev_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 		 * All devices have to be of the same type in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		 * order to be grouped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 		if (!gdev->cdev[i] || !gdev->cdev[i]->drv ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		    gdev->cdev[i]->drv != gdev->cdev[0]->drv ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		    gdev->cdev[i]->id.driver_info !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		    gdev->cdev[0]->id.driver_info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 			rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		/* Don't allow a device to belong to more than one group. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		spin_lock_irq(gdev->cdev[i]->ccwlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		if (dev_get_drvdata(&gdev->cdev[i]->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 			spin_unlock_irq(gdev->cdev[i]->ccwlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 			rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		dev_set_drvdata(&gdev->cdev[i]->dev, gdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		spin_unlock_irq(gdev->cdev[i]->ccwlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	/* Check for sufficient number of bus ids. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	if (i < num_devices) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	/* Check for trailing stuff. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	if (i == num_devices && buf && strlen(buf) > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	/* Check if the devices are bound to the required ccw driver. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	if (gdrv && gdrv->ccw_driver &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	    gdev->cdev[0]->drv != gdrv->ccw_driver) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	dev_set_name(&gdev->dev, "%s", dev_name(&gdev->cdev[0]->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	gdev->dev.groups = ccwgroup_attr_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	if (gdrv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		gdev->dev.driver = &gdrv->driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		rc = gdrv->setup ? gdrv->setup(gdev) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 			goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	rc = device_add(&gdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	rc = __ccwgroup_create_symlinks(gdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		device_del(&gdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	mutex_unlock(&gdev->reg_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	for (i = 0; i < num_devices; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		if (gdev->cdev[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 			spin_lock_irq(gdev->cdev[i]->ccwlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 			if (dev_get_drvdata(&gdev->cdev[i]->dev) == gdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 				dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 			spin_unlock_irq(gdev->cdev[i]->ccwlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 			put_device(&gdev->cdev[i]->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 			gdev->cdev[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	mutex_unlock(&gdev->reg_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	put_device(&gdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) EXPORT_SYMBOL(ccwgroup_create_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) static int ccwgroup_notifier(struct notifier_block *nb, unsigned long action,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 			     void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	struct ccwgroup_device *gdev = to_ccwgroupdev(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	if (action == BUS_NOTIFY_UNBIND_DRIVER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 		get_device(&gdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 		schedule_work(&gdev->ungroup_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) static struct notifier_block ccwgroup_nb = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	.notifier_call = ccwgroup_notifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) static int __init init_ccwgroup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	ret = bus_register(&ccwgroup_bus_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	ret = bus_register_notifier(&ccwgroup_bus_type, &ccwgroup_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 		bus_unregister(&ccwgroup_bus_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) static void __exit cleanup_ccwgroup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	bus_unregister_notifier(&ccwgroup_bus_type, &ccwgroup_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	bus_unregister(&ccwgroup_bus_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) module_init(init_ccwgroup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) module_exit(cleanup_ccwgroup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) /************************** driver stuff ******************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) static int ccwgroup_remove(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	if (!dev->driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	if (gdrv->remove)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		gdrv->remove(gdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) static void ccwgroup_shutdown(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	if (!dev->driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	if (gdrv->shutdown)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		gdrv->shutdown(gdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) static struct bus_type ccwgroup_bus_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	.name   = "ccwgroup",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	.remove = ccwgroup_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	.shutdown = ccwgroup_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) bool dev_is_ccwgroup(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	return dev->bus == &ccwgroup_bus_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) EXPORT_SYMBOL(dev_is_ccwgroup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501)  * ccwgroup_driver_register() - register a ccw group driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)  * @cdriver: driver to be registered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)  * This function is mainly a wrapper around driver_register().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) int ccwgroup_driver_register(struct ccwgroup_driver *cdriver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	/* register our new driver with the core */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	cdriver->driver.bus = &ccwgroup_bus_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	return driver_register(&cdriver->driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) EXPORT_SYMBOL(ccwgroup_driver_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)  * ccwgroup_driver_unregister() - deregister a ccw group driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)  * @cdriver: driver to be deregistered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)  * This function is mainly a wrapper around driver_unregister().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) void ccwgroup_driver_unregister(struct ccwgroup_driver *cdriver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	/* We don't want ccwgroup devices to live longer than their driver. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	while ((dev = driver_find_next_device(&cdriver->driver, NULL))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 		ccwgroup_ungroup(gdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 		put_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	driver_unregister(&cdriver->driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) EXPORT_SYMBOL(ccwgroup_driver_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)  * get_ccwgroupdev_by_busid() - obtain device from a bus id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538)  * @gdrv: driver the device is owned by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)  * @bus_id: bus id of the device to be searched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)  * This function searches all devices owned by @gdrv for a device with a bus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)  * id matching @bus_id.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543)  * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544)  *  If a match is found, its reference count of the found device is increased
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)  *  and it is returned; else %NULL is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) struct ccwgroup_device *get_ccwgroupdev_by_busid(struct ccwgroup_driver *gdrv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 						 char *bus_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	dev = driver_find_device_by_name(&gdrv->driver, bus_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	return dev ? to_ccwgroupdev(dev) : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) EXPORT_SYMBOL_GPL(get_ccwgroupdev_by_busid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559)  * ccwgroup_probe_ccwdev() - probe function for slave devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)  * @cdev: ccw device to be probed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)  * This is a dummy probe function for ccw devices that are slave devices in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)  * a ccw group device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564)  * Returns:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565)  *  always %0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) int ccwgroup_probe_ccwdev(struct ccw_device *cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) EXPORT_SYMBOL(ccwgroup_probe_ccwdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574)  * ccwgroup_remove_ccwdev() - remove function for slave devices
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)  * @cdev: ccw device to be removed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577)  * This is a remove function for ccw devices that are slave devices in a ccw
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578)  * group device. It sets the ccw device offline and also deregisters the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579)  * embedding ccw group device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) void ccwgroup_remove_ccwdev(struct ccw_device *cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	struct ccwgroup_device *gdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 	/* Ignore offlining errors, device is gone anyway. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 	ccw_device_set_offline(cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 	/* If one of its devices is gone, the whole group is done for. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 	spin_lock_irq(cdev->ccwlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	gdev = dev_get_drvdata(&cdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 	if (!gdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 		spin_unlock_irq(cdev->ccwlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 	/* Get ccwgroup device reference for local processing. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	get_device(&gdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 	spin_unlock_irq(cdev->ccwlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	/* Unregister group device. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 	ccwgroup_ungroup(gdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 	/* Release ccwgroup device reference for local processing. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	put_device(&gdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) EXPORT_SYMBOL(ccwgroup_remove_ccwdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) MODULE_LICENSE("GPL");