Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/s390/cio/ccwgroup.c |
| 3 | * bus driver for ccwgroup |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH, |
| 6 | * IBM Corporation |
| 7 | * Author(s): Arnd Bergmann (arndb@de.ibm.com) |
Cornelia Huck | 4ce3b30 | 2006-01-14 13:21:04 -0800 | [diff] [blame] | 8 | * Cornelia Huck (cornelia.huck@de.ibm.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/errno.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/list.h> |
| 14 | #include <linux/device.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/ctype.h> |
| 17 | #include <linux/dcache.h> |
| 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <asm/ccwdev.h> |
| 20 | #include <asm/ccwgroup.h> |
| 21 | |
| 22 | /* In Linux 2.4, we had a channel device layer called "chandev" |
| 23 | * that did all sorts of obscure stuff for networking devices. |
| 24 | * This is another driver that serves as a replacement for just |
| 25 | * one of its functions, namely the translation of single subchannels |
| 26 | * to devices that use multiple subchannels. |
| 27 | */ |
| 28 | |
| 29 | /* a device matches a driver if all its slave devices match the same |
| 30 | * entry of the driver */ |
| 31 | static int |
| 32 | ccwgroup_bus_match (struct device * dev, struct device_driver * drv) |
| 33 | { |
| 34 | struct ccwgroup_device *gdev; |
| 35 | struct ccwgroup_driver *gdrv; |
| 36 | |
Cornelia Huck | 084325d | 2008-01-26 14:10:38 +0100 | [diff] [blame] | 37 | gdev = to_ccwgroupdev(dev); |
| 38 | gdrv = to_ccwgroupdrv(drv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | if (gdev->creator_id == gdrv->driver_id) |
| 41 | return 1; |
| 42 | |
| 43 | return 0; |
| 44 | } |
| 45 | static int |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 46 | ccwgroup_uevent (struct device *dev, struct kobj_uevent_env *env) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | { |
| 48 | /* TODO */ |
| 49 | return 0; |
| 50 | } |
| 51 | |
Russell King | f9ccf45 | 2006-01-05 14:42:09 +0000 | [diff] [blame] | 52 | static struct bus_type ccwgroup_bus_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Heiko Carstens | 4d284ca | 2007-02-05 21:18:53 +0100 | [diff] [blame] | 54 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | __ccwgroup_remove_symlinks(struct ccwgroup_device *gdev) |
| 56 | { |
| 57 | int i; |
| 58 | char str[8]; |
| 59 | |
| 60 | for (i = 0; i < gdev->count; i++) { |
| 61 | sprintf(str, "cdev%d", i); |
| 62 | sysfs_remove_link(&gdev->dev.kobj, str); |
| 63 | sysfs_remove_link(&gdev->cdev[i]->dev.kobj, "group_device"); |
| 64 | } |
| 65 | |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * Provide an 'ungroup' attribute so the user can remove group devices no |
| 70 | * longer needed or accidentially created. Saves memory :) |
| 71 | */ |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 72 | static void ccwgroup_ungroup_callback(struct device *dev) |
| 73 | { |
| 74 | struct ccwgroup_device *gdev = to_ccwgroupdev(dev); |
| 75 | |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 76 | mutex_lock(&gdev->reg_mutex); |
Cornelia Huck | 1a908c7 | 2008-01-26 14:10:50 +0100 | [diff] [blame] | 77 | if (device_is_registered(&gdev->dev)) { |
| 78 | __ccwgroup_remove_symlinks(gdev); |
| 79 | device_unregister(dev); |
| 80 | } |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 81 | mutex_unlock(&gdev->reg_mutex); |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 82 | } |
| 83 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | static ssize_t |
Yani Ioannou | 3fd3c0a | 2005-05-17 06:43:27 -0400 | [diff] [blame] | 85 | ccwgroup_ungroup_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | { |
| 87 | struct ccwgroup_device *gdev; |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 88 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
| 90 | gdev = to_ccwgroupdev(dev); |
| 91 | |
| 92 | if (gdev->state != CCWGROUP_OFFLINE) |
| 93 | return -EINVAL; |
| 94 | |
Alan Stern | d9a9cdf | 2007-03-15 15:50:34 -0400 | [diff] [blame] | 95 | /* Note that we cannot unregister the device from one of its |
| 96 | * attribute methods, so we have to use this roundabout approach. |
| 97 | */ |
| 98 | rc = device_schedule_callback(dev, ccwgroup_ungroup_callback); |
| 99 | if (rc) |
| 100 | count = rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | return count; |
| 102 | } |
| 103 | |
| 104 | static DEVICE_ATTR(ungroup, 0200, NULL, ccwgroup_ungroup_store); |
| 105 | |
| 106 | static void |
| 107 | ccwgroup_release (struct device *dev) |
| 108 | { |
| 109 | struct ccwgroup_device *gdev; |
| 110 | int i; |
| 111 | |
| 112 | gdev = to_ccwgroupdev(dev); |
| 113 | |
| 114 | for (i = 0; i < gdev->count; i++) { |
Cornelia Huck | db6a642 | 2008-01-26 14:10:46 +0100 | [diff] [blame] | 115 | dev_set_drvdata(&gdev->cdev[i]->dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | put_device(&gdev->cdev[i]->dev); |
| 117 | } |
| 118 | kfree(gdev); |
| 119 | } |
| 120 | |
Heiko Carstens | 4d284ca | 2007-02-05 21:18:53 +0100 | [diff] [blame] | 121 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | __ccwgroup_create_symlinks(struct ccwgroup_device *gdev) |
| 123 | { |
| 124 | char str[8]; |
| 125 | int i, rc; |
| 126 | |
| 127 | for (i = 0; i < gdev->count; i++) { |
| 128 | rc = sysfs_create_link(&gdev->cdev[i]->dev.kobj, &gdev->dev.kobj, |
| 129 | "group_device"); |
| 130 | if (rc) { |
| 131 | for (--i; i >= 0; i--) |
| 132 | sysfs_remove_link(&gdev->cdev[i]->dev.kobj, |
| 133 | "group_device"); |
| 134 | return rc; |
| 135 | } |
| 136 | } |
| 137 | for (i = 0; i < gdev->count; i++) { |
| 138 | sprintf(str, "cdev%d", i); |
| 139 | rc = sysfs_create_link(&gdev->dev.kobj, &gdev->cdev[i]->dev.kobj, |
| 140 | str); |
| 141 | if (rc) { |
| 142 | for (--i; i >= 0; i--) { |
| 143 | sprintf(str, "cdev%d", i); |
| 144 | sysfs_remove_link(&gdev->dev.kobj, str); |
| 145 | } |
| 146 | for (i = 0; i < gdev->count; i++) |
| 147 | sysfs_remove_link(&gdev->cdev[i]->dev.kobj, |
| 148 | "group_device"); |
| 149 | return rc; |
| 150 | } |
| 151 | } |
| 152 | return 0; |
| 153 | } |
| 154 | |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 155 | static int __get_next_bus_id(const char **buf, char *bus_id) |
| 156 | { |
| 157 | int rc, len; |
| 158 | char *start, *end; |
| 159 | |
| 160 | start = (char *)*buf; |
| 161 | end = strchr(start, ','); |
| 162 | if (!end) { |
| 163 | /* Last entry. Strip trailing newline, if applicable. */ |
| 164 | end = strchr(start, '\n'); |
| 165 | if (end) |
| 166 | *end = '\0'; |
| 167 | len = strlen(start) + 1; |
| 168 | } else { |
| 169 | len = end - start + 1; |
| 170 | end++; |
| 171 | } |
| 172 | if (len < BUS_ID_SIZE) { |
| 173 | strlcpy(bus_id, start, len); |
| 174 | rc = 0; |
| 175 | } else |
| 176 | rc = -EINVAL; |
| 177 | *buf = end; |
| 178 | return rc; |
| 179 | } |
| 180 | |
| 181 | static int __is_valid_bus_id(char bus_id[BUS_ID_SIZE]) |
| 182 | { |
| 183 | int cssid, ssid, devno; |
| 184 | |
| 185 | /* Must be of form %x.%x.%04x */ |
| 186 | if (sscanf(bus_id, "%x.%1x.%04x", &cssid, &ssid, &devno) != 3) |
| 187 | return 0; |
| 188 | return 1; |
| 189 | } |
| 190 | |
Cornelia Huck | b2ffd8e | 2007-10-12 16:11:17 +0200 | [diff] [blame] | 191 | /** |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 192 | * ccwgroup_create_from_string() - create and register a ccw group device |
Cornelia Huck | b2ffd8e | 2007-10-12 16:11:17 +0200 | [diff] [blame] | 193 | * @root: parent device for the new device |
| 194 | * @creator_id: identifier of creating driver |
| 195 | * @cdrv: ccw driver of slave devices |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 196 | * @num_devices: number of slave devices |
| 197 | * @buf: buffer containing comma separated bus ids of slave devices |
Cornelia Huck | b2ffd8e | 2007-10-12 16:11:17 +0200 | [diff] [blame] | 198 | * |
| 199 | * Create and register a new ccw group device as a child of @root. Slave |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 200 | * devices are obtained from the list of bus ids given in @buf and must all |
Cornelia Huck | b2ffd8e | 2007-10-12 16:11:17 +0200 | [diff] [blame] | 201 | * belong to @cdrv. |
| 202 | * Returns: |
| 203 | * %0 on success and an error code on failure. |
| 204 | * Context: |
| 205 | * non-atomic |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | */ |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 207 | int ccwgroup_create_from_string(struct device *root, unsigned int creator_id, |
| 208 | struct ccw_driver *cdrv, int num_devices, |
| 209 | const char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | { |
| 211 | struct ccwgroup_device *gdev; |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 212 | int rc, i; |
| 213 | char tmp_bus_id[BUS_ID_SIZE]; |
| 214 | const char *curr_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 216 | gdev = kzalloc(sizeof(*gdev) + num_devices * sizeof(gdev->cdev[0]), |
| 217 | GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | if (!gdev) |
| 219 | return -ENOMEM; |
| 220 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | atomic_set(&gdev->onoff, 0); |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 222 | mutex_init(&gdev->reg_mutex); |
| 223 | mutex_lock(&gdev->reg_mutex); |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 224 | curr_buf = buf; |
| 225 | for (i = 0; i < num_devices && curr_buf; i++) { |
| 226 | rc = __get_next_bus_id(&curr_buf, tmp_bus_id); |
| 227 | if (rc != 0) |
| 228 | goto error; |
| 229 | if (!__is_valid_bus_id(tmp_bus_id)) { |
| 230 | rc = -EINVAL; |
| 231 | goto error; |
| 232 | } |
| 233 | gdev->cdev[i] = get_ccwdev_by_busid(cdrv, tmp_bus_id); |
| 234 | /* |
| 235 | * All devices have to be of the same type in |
| 236 | * order to be grouped. |
| 237 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | if (!gdev->cdev[i] |
| 239 | || gdev->cdev[i]->id.driver_info != |
| 240 | gdev->cdev[0]->id.driver_info) { |
| 241 | rc = -EINVAL; |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 242 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | } |
| 244 | /* Don't allow a device to belong to more than one group. */ |
Cornelia Huck | db6a642 | 2008-01-26 14:10:46 +0100 | [diff] [blame] | 245 | if (dev_get_drvdata(&gdev->cdev[i]->dev)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | rc = -EINVAL; |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 247 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | } |
Cornelia Huck | db6a642 | 2008-01-26 14:10:46 +0100 | [diff] [blame] | 249 | dev_set_drvdata(&gdev->cdev[i]->dev, gdev); |
Cornelia Huck | 1708822 | 2006-07-27 14:00:33 +0200 | [diff] [blame] | 250 | } |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 251 | /* Check for sufficient number of bus ids. */ |
| 252 | if (i < num_devices && !curr_buf) { |
| 253 | rc = -EINVAL; |
| 254 | goto error; |
| 255 | } |
| 256 | /* Check for trailing stuff. */ |
| 257 | if (i == num_devices && strlen(curr_buf) > 0) { |
| 258 | rc = -EINVAL; |
| 259 | goto error; |
| 260 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | gdev->creator_id = creator_id; |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 262 | gdev->count = num_devices; |
Heiko Carstens | 292888c | 2006-08-30 14:33:35 +0200 | [diff] [blame] | 263 | gdev->dev.bus = &ccwgroup_bus_type; |
| 264 | gdev->dev.parent = root; |
| 265 | gdev->dev.release = ccwgroup_release; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
| 267 | snprintf (gdev->dev.bus_id, BUS_ID_SIZE, "%s", |
| 268 | gdev->cdev[0]->dev.bus_id); |
| 269 | |
| 270 | rc = device_register(&gdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | if (rc) |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 272 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | get_device(&gdev->dev); |
| 274 | rc = device_create_file(&gdev->dev, &dev_attr_ungroup); |
| 275 | |
| 276 | if (rc) { |
| 277 | device_unregister(&gdev->dev); |
| 278 | goto error; |
| 279 | } |
| 280 | |
| 281 | rc = __ccwgroup_create_symlinks(gdev); |
| 282 | if (!rc) { |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 283 | mutex_unlock(&gdev->reg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | put_device(&gdev->dev); |
| 285 | return 0; |
| 286 | } |
| 287 | device_remove_file(&gdev->dev, &dev_attr_ungroup); |
| 288 | device_unregister(&gdev->dev); |
| 289 | error: |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 290 | for (i = 0; i < num_devices; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | if (gdev->cdev[i]) { |
Cornelia Huck | db6a642 | 2008-01-26 14:10:46 +0100 | [diff] [blame] | 292 | if (dev_get_drvdata(&gdev->cdev[i]->dev) == gdev) |
| 293 | dev_set_drvdata(&gdev->cdev[i]->dev, NULL); |
Cornelia Huck | 1708822 | 2006-07-27 14:00:33 +0200 | [diff] [blame] | 294 | put_device(&gdev->cdev[i]->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | } |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 296 | mutex_unlock(&gdev->reg_mutex); |
| 297 | put_device(&gdev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | return rc; |
| 299 | } |
Ursula Braun | 022b660 | 2008-04-24 10:15:20 +0200 | [diff] [blame] | 300 | EXPORT_SYMBOL(ccwgroup_create_from_string); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | |
| 302 | static int __init |
| 303 | init_ccwgroup (void) |
| 304 | { |
| 305 | return bus_register (&ccwgroup_bus_type); |
| 306 | } |
| 307 | |
| 308 | static void __exit |
| 309 | cleanup_ccwgroup (void) |
| 310 | { |
| 311 | bus_unregister (&ccwgroup_bus_type); |
| 312 | } |
| 313 | |
| 314 | module_init(init_ccwgroup); |
| 315 | module_exit(cleanup_ccwgroup); |
| 316 | |
| 317 | /************************** driver stuff ******************************/ |
| 318 | |
| 319 | static int |
| 320 | ccwgroup_set_online(struct ccwgroup_device *gdev) |
| 321 | { |
| 322 | struct ccwgroup_driver *gdrv; |
| 323 | int ret; |
| 324 | |
Martin Schwidefsky | 973bd99 | 2006-01-06 00:19:07 -0800 | [diff] [blame] | 325 | if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | return -EAGAIN; |
| 327 | if (gdev->state == CCWGROUP_ONLINE) { |
| 328 | ret = 0; |
| 329 | goto out; |
| 330 | } |
| 331 | if (!gdev->dev.driver) { |
| 332 | ret = -EINVAL; |
| 333 | goto out; |
| 334 | } |
| 335 | gdrv = to_ccwgroupdrv (gdev->dev.driver); |
Cornelia Huck | a001640 | 2005-11-07 00:59:05 -0800 | [diff] [blame] | 336 | if ((ret = gdrv->set_online ? gdrv->set_online(gdev) : 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | goto out; |
| 338 | |
| 339 | gdev->state = CCWGROUP_ONLINE; |
| 340 | out: |
| 341 | atomic_set(&gdev->onoff, 0); |
| 342 | return ret; |
| 343 | } |
| 344 | |
| 345 | static int |
| 346 | ccwgroup_set_offline(struct ccwgroup_device *gdev) |
| 347 | { |
| 348 | struct ccwgroup_driver *gdrv; |
| 349 | int ret; |
| 350 | |
Martin Schwidefsky | 973bd99 | 2006-01-06 00:19:07 -0800 | [diff] [blame] | 351 | if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | return -EAGAIN; |
| 353 | if (gdev->state == CCWGROUP_OFFLINE) { |
| 354 | ret = 0; |
| 355 | goto out; |
| 356 | } |
| 357 | if (!gdev->dev.driver) { |
| 358 | ret = -EINVAL; |
| 359 | goto out; |
| 360 | } |
| 361 | gdrv = to_ccwgroupdrv (gdev->dev.driver); |
Cornelia Huck | a001640 | 2005-11-07 00:59:05 -0800 | [diff] [blame] | 362 | if ((ret = gdrv->set_offline ? gdrv->set_offline(gdev) : 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | goto out; |
| 364 | |
| 365 | gdev->state = CCWGROUP_OFFLINE; |
| 366 | out: |
| 367 | atomic_set(&gdev->onoff, 0); |
| 368 | return ret; |
| 369 | } |
| 370 | |
| 371 | static ssize_t |
Yani Ioannou | 3fd3c0a | 2005-05-17 06:43:27 -0400 | [diff] [blame] | 372 | ccwgroup_online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | { |
| 374 | struct ccwgroup_device *gdev; |
| 375 | struct ccwgroup_driver *gdrv; |
Cornelia Huck | 2f97220 | 2008-04-30 13:38:33 +0200 | [diff] [blame] | 376 | unsigned long value; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | int ret; |
| 378 | |
| 379 | gdev = to_ccwgroupdev(dev); |
| 380 | if (!dev->driver) |
| 381 | return count; |
| 382 | |
| 383 | gdrv = to_ccwgroupdrv (gdev->dev.driver); |
| 384 | if (!try_module_get(gdrv->owner)) |
| 385 | return -EINVAL; |
| 386 | |
Cornelia Huck | 2f97220 | 2008-04-30 13:38:33 +0200 | [diff] [blame] | 387 | ret = strict_strtoul(buf, 0, &value); |
| 388 | if (ret) |
| 389 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | ret = count; |
| 391 | if (value == 1) |
| 392 | ccwgroup_set_online(gdev); |
| 393 | else if (value == 0) |
| 394 | ccwgroup_set_offline(gdev); |
| 395 | else |
| 396 | ret = -EINVAL; |
Cornelia Huck | 2f97220 | 2008-04-30 13:38:33 +0200 | [diff] [blame] | 397 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | module_put(gdrv->owner); |
| 399 | return ret; |
| 400 | } |
| 401 | |
| 402 | static ssize_t |
Yani Ioannou | 3fd3c0a | 2005-05-17 06:43:27 -0400 | [diff] [blame] | 403 | ccwgroup_online_show (struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | { |
| 405 | int online; |
| 406 | |
| 407 | online = (to_ccwgroupdev(dev)->state == CCWGROUP_ONLINE); |
| 408 | |
| 409 | return sprintf(buf, online ? "1\n" : "0\n"); |
| 410 | } |
| 411 | |
| 412 | static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store); |
| 413 | |
| 414 | static int |
| 415 | ccwgroup_probe (struct device *dev) |
| 416 | { |
| 417 | struct ccwgroup_device *gdev; |
| 418 | struct ccwgroup_driver *gdrv; |
| 419 | |
| 420 | int ret; |
| 421 | |
| 422 | gdev = to_ccwgroupdev(dev); |
| 423 | gdrv = to_ccwgroupdrv(dev->driver); |
| 424 | |
| 425 | if ((ret = device_create_file(dev, &dev_attr_online))) |
| 426 | return ret; |
| 427 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | ret = gdrv->probe ? gdrv->probe(gdev) : -ENODEV; |
| 429 | if (ret) |
| 430 | device_remove_file(dev, &dev_attr_online); |
| 431 | |
| 432 | return ret; |
| 433 | } |
| 434 | |
| 435 | static int |
| 436 | ccwgroup_remove (struct device *dev) |
| 437 | { |
| 438 | struct ccwgroup_device *gdev; |
| 439 | struct ccwgroup_driver *gdrv; |
| 440 | |
| 441 | gdev = to_ccwgroupdev(dev); |
| 442 | gdrv = to_ccwgroupdrv(dev->driver); |
| 443 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | device_remove_file(dev, &dev_attr_online); |
| 445 | |
| 446 | if (gdrv && gdrv->remove) |
| 447 | gdrv->remove(gdev); |
| 448 | return 0; |
| 449 | } |
| 450 | |
Cornelia Huck | 01bc8ad | 2008-02-05 16:50:36 +0100 | [diff] [blame] | 451 | static void ccwgroup_shutdown(struct device *dev) |
| 452 | { |
| 453 | struct ccwgroup_device *gdev; |
| 454 | struct ccwgroup_driver *gdrv; |
| 455 | |
| 456 | gdev = to_ccwgroupdev(dev); |
| 457 | gdrv = to_ccwgroupdrv(dev->driver); |
| 458 | if (gdrv && gdrv->shutdown) |
| 459 | gdrv->shutdown(gdev); |
| 460 | } |
| 461 | |
Russell King | f9ccf45 | 2006-01-05 14:42:09 +0000 | [diff] [blame] | 462 | static struct bus_type ccwgroup_bus_type = { |
| 463 | .name = "ccwgroup", |
| 464 | .match = ccwgroup_bus_match, |
| 465 | .uevent = ccwgroup_uevent, |
| 466 | .probe = ccwgroup_probe, |
| 467 | .remove = ccwgroup_remove, |
Cornelia Huck | 01bc8ad | 2008-02-05 16:50:36 +0100 | [diff] [blame] | 468 | .shutdown = ccwgroup_shutdown, |
Russell King | f9ccf45 | 2006-01-05 14:42:09 +0000 | [diff] [blame] | 469 | }; |
| 470 | |
Cornelia Huck | b2ffd8e | 2007-10-12 16:11:17 +0200 | [diff] [blame] | 471 | /** |
| 472 | * ccwgroup_driver_register() - register a ccw group driver |
| 473 | * @cdriver: driver to be registered |
| 474 | * |
| 475 | * This function is mainly a wrapper around driver_register(). |
| 476 | */ |
| 477 | int ccwgroup_driver_register(struct ccwgroup_driver *cdriver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | { |
| 479 | /* register our new driver with the core */ |
Heiko Carstens | 292888c | 2006-08-30 14:33:35 +0200 | [diff] [blame] | 480 | cdriver->driver.bus = &ccwgroup_bus_type; |
| 481 | cdriver->driver.name = cdriver->name; |
Cornelia Huck | 4beee64 | 2008-01-26 14:10:47 +0100 | [diff] [blame] | 482 | cdriver->driver.owner = cdriver->owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | |
| 484 | return driver_register(&cdriver->driver); |
| 485 | } |
| 486 | |
Cornelia Huck | b0744bd | 2005-06-25 14:55:27 -0700 | [diff] [blame] | 487 | static int |
Cornelia Huck | 887ab59 | 2006-06-29 14:56:52 +0200 | [diff] [blame] | 488 | __ccwgroup_match_all(struct device *dev, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | { |
Cornelia Huck | 887ab59 | 2006-06-29 14:56:52 +0200 | [diff] [blame] | 490 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | } |
| 492 | |
Cornelia Huck | b2ffd8e | 2007-10-12 16:11:17 +0200 | [diff] [blame] | 493 | /** |
| 494 | * ccwgroup_driver_unregister() - deregister a ccw group driver |
| 495 | * @cdriver: driver to be deregistered |
| 496 | * |
| 497 | * This function is mainly a wrapper around driver_unregister(). |
| 498 | */ |
| 499 | void ccwgroup_driver_unregister(struct ccwgroup_driver *cdriver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | { |
Cornelia Huck | 887ab59 | 2006-06-29 14:56:52 +0200 | [diff] [blame] | 501 | struct device *dev; |
| 502 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | /* We don't want ccwgroup devices to live longer than their driver. */ |
| 504 | get_driver(&cdriver->driver); |
Cornelia Huck | 887ab59 | 2006-06-29 14:56:52 +0200 | [diff] [blame] | 505 | while ((dev = driver_find_device(&cdriver->driver, NULL, NULL, |
| 506 | __ccwgroup_match_all))) { |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 507 | struct ccwgroup_device *gdev = to_ccwgroupdev(dev); |
| 508 | |
| 509 | mutex_lock(&gdev->reg_mutex); |
| 510 | __ccwgroup_remove_symlinks(gdev); |
Cornelia Huck | 887ab59 | 2006-06-29 14:56:52 +0200 | [diff] [blame] | 511 | device_unregister(dev); |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 512 | mutex_unlock(&gdev->reg_mutex); |
Cornelia Huck | 887ab59 | 2006-06-29 14:56:52 +0200 | [diff] [blame] | 513 | put_device(dev); |
| 514 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | put_driver(&cdriver->driver); |
| 516 | driver_unregister(&cdriver->driver); |
| 517 | } |
| 518 | |
Cornelia Huck | b2ffd8e | 2007-10-12 16:11:17 +0200 | [diff] [blame] | 519 | /** |
| 520 | * ccwgroup_probe_ccwdev() - probe function for slave devices |
| 521 | * @cdev: ccw device to be probed |
| 522 | * |
| 523 | * This is a dummy probe function for ccw devices that are slave devices in |
| 524 | * a ccw group device. |
| 525 | * Returns: |
| 526 | * always %0 |
| 527 | */ |
| 528 | int ccwgroup_probe_ccwdev(struct ccw_device *cdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | { |
| 530 | return 0; |
| 531 | } |
| 532 | |
Heiko Carstens | 4d284ca | 2007-02-05 21:18:53 +0100 | [diff] [blame] | 533 | static struct ccwgroup_device * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | __ccwgroup_get_gdev_by_cdev(struct ccw_device *cdev) |
| 535 | { |
| 536 | struct ccwgroup_device *gdev; |
| 537 | |
Cornelia Huck | db6a642 | 2008-01-26 14:10:46 +0100 | [diff] [blame] | 538 | gdev = dev_get_drvdata(&cdev->dev); |
| 539 | if (gdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | if (get_device(&gdev->dev)) { |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 541 | mutex_lock(&gdev->reg_mutex); |
Daniel Ritz | d305ef5 | 2005-09-22 00:47:24 -0700 | [diff] [blame] | 542 | if (device_is_registered(&gdev->dev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | return gdev; |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 544 | mutex_unlock(&gdev->reg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | put_device(&gdev->dev); |
| 546 | } |
| 547 | return NULL; |
| 548 | } |
| 549 | return NULL; |
| 550 | } |
| 551 | |
Cornelia Huck | b2ffd8e | 2007-10-12 16:11:17 +0200 | [diff] [blame] | 552 | /** |
| 553 | * ccwgroup_remove_ccwdev() - remove function for slave devices |
| 554 | * @cdev: ccw device to be removed |
| 555 | * |
| 556 | * This is a remove function for ccw devices that are slave devices in a ccw |
| 557 | * group device. It sets the ccw device offline and also deregisters the |
| 558 | * embedding ccw group device. |
| 559 | */ |
| 560 | void ccwgroup_remove_ccwdev(struct ccw_device *cdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | { |
| 562 | struct ccwgroup_device *gdev; |
| 563 | |
| 564 | /* Ignore offlining errors, device is gone anyway. */ |
| 565 | ccw_device_set_offline(cdev); |
| 566 | /* If one of its devices is gone, the whole group is done for. */ |
| 567 | gdev = __ccwgroup_get_gdev_by_cdev(cdev); |
| 568 | if (gdev) { |
| 569 | __ccwgroup_remove_symlinks(gdev); |
| 570 | device_unregister(&gdev->dev); |
Cornelia Huck | d76123e | 2007-04-27 16:01:37 +0200 | [diff] [blame] | 571 | mutex_unlock(&gdev->reg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | put_device(&gdev->dev); |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | MODULE_LICENSE("GPL"); |
| 577 | EXPORT_SYMBOL(ccwgroup_driver_register); |
| 578 | EXPORT_SYMBOL(ccwgroup_driver_unregister); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | EXPORT_SYMBOL(ccwgroup_probe_ccwdev); |
| 580 | EXPORT_SYMBOL(ccwgroup_remove_ccwdev); |