blob: f38923e38e92be588d915d8616bd5bb897419b2e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/s390/cio/ccwgroup.c
3 * bus driver for ccwgroup
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
6 * IBM Corporation
7 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
Cornelia Huck4ce3b302006-01-14 13:21:04 -08008 * Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
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
19#include <asm/semaphore.h>
20#include <asm/ccwdev.h>
21#include <asm/ccwgroup.h>
22
23/* In Linux 2.4, we had a channel device layer called "chandev"
24 * that did all sorts of obscure stuff for networking devices.
25 * This is another driver that serves as a replacement for just
26 * one of its functions, namely the translation of single subchannels
27 * to devices that use multiple subchannels.
28 */
29
30/* a device matches a driver if all its slave devices match the same
31 * entry of the driver */
32static int
33ccwgroup_bus_match (struct device * dev, struct device_driver * drv)
34{
35 struct ccwgroup_device *gdev;
36 struct ccwgroup_driver *gdrv;
37
Cornelia Huck084325d2008-01-26 14:10:38 +010038 gdev = to_ccwgroupdev(dev);
39 gdrv = to_ccwgroupdrv(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41 if (gdev->creator_id == gdrv->driver_id)
42 return 1;
43
44 return 0;
45}
46static int
Kay Sievers7eff2e72007-08-14 15:15:12 +020047ccwgroup_uevent (struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 /* TODO */
50 return 0;
51}
52
Russell Kingf9ccf452006-01-05 14:42:09 +000053static struct bus_type ccwgroup_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Heiko Carstens4d284ca2007-02-05 21:18:53 +010055static void
Linus Torvalds1da177e2005-04-16 15:20:36 -070056__ccwgroup_remove_symlinks(struct ccwgroup_device *gdev)
57{
58 int i;
59 char str[8];
60
61 for (i = 0; i < gdev->count; i++) {
62 sprintf(str, "cdev%d", i);
63 sysfs_remove_link(&gdev->dev.kobj, str);
64 sysfs_remove_link(&gdev->cdev[i]->dev.kobj, "group_device");
65 }
66
67}
68
69/*
70 * Provide an 'ungroup' attribute so the user can remove group devices no
71 * longer needed or accidentially created. Saves memory :)
72 */
Alan Sternd9a9cdf2007-03-15 15:50:34 -040073static void ccwgroup_ungroup_callback(struct device *dev)
74{
75 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
76
Cornelia Huckd76123e2007-04-27 16:01:37 +020077 mutex_lock(&gdev->reg_mutex);
Cornelia Huck1a908c72008-01-26 14:10:50 +010078 if (device_is_registered(&gdev->dev)) {
79 __ccwgroup_remove_symlinks(gdev);
80 device_unregister(dev);
81 }
Cornelia Huckd76123e2007-04-27 16:01:37 +020082 mutex_unlock(&gdev->reg_mutex);
Alan Sternd9a9cdf2007-03-15 15:50:34 -040083}
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -040086ccwgroup_ungroup_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
88 struct ccwgroup_device *gdev;
Alan Sternd9a9cdf2007-03-15 15:50:34 -040089 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 gdev = to_ccwgroupdev(dev);
92
93 if (gdev->state != CCWGROUP_OFFLINE)
94 return -EINVAL;
95
Alan Sternd9a9cdf2007-03-15 15:50:34 -040096 /* Note that we cannot unregister the device from one of its
97 * attribute methods, so we have to use this roundabout approach.
98 */
99 rc = device_schedule_callback(dev, ccwgroup_ungroup_callback);
100 if (rc)
101 count = rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return count;
103}
104
105static DEVICE_ATTR(ungroup, 0200, NULL, ccwgroup_ungroup_store);
106
107static void
108ccwgroup_release (struct device *dev)
109{
110 struct ccwgroup_device *gdev;
111 int i;
112
113 gdev = to_ccwgroupdev(dev);
114
115 for (i = 0; i < gdev->count; i++) {
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100116 dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 put_device(&gdev->cdev[i]->dev);
118 }
119 kfree(gdev);
120}
121
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100122static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123__ccwgroup_create_symlinks(struct ccwgroup_device *gdev)
124{
125 char str[8];
126 int i, rc;
127
128 for (i = 0; i < gdev->count; i++) {
129 rc = sysfs_create_link(&gdev->cdev[i]->dev.kobj, &gdev->dev.kobj,
130 "group_device");
131 if (rc) {
132 for (--i; i >= 0; i--)
133 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
134 "group_device");
135 return rc;
136 }
137 }
138 for (i = 0; i < gdev->count; i++) {
139 sprintf(str, "cdev%d", i);
140 rc = sysfs_create_link(&gdev->dev.kobj, &gdev->cdev[i]->dev.kobj,
141 str);
142 if (rc) {
143 for (--i; i >= 0; i--) {
144 sprintf(str, "cdev%d", i);
145 sysfs_remove_link(&gdev->dev.kobj, str);
146 }
147 for (i = 0; i < gdev->count; i++)
148 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
149 "group_device");
150 return rc;
151 }
152 }
153 return 0;
154}
155
Ursula Braun022b6602008-04-24 10:15:20 +0200156static int __get_next_bus_id(const char **buf, char *bus_id)
157{
158 int rc, len;
159 char *start, *end;
160
161 start = (char *)*buf;
162 end = strchr(start, ',');
163 if (!end) {
164 /* Last entry. Strip trailing newline, if applicable. */
165 end = strchr(start, '\n');
166 if (end)
167 *end = '\0';
168 len = strlen(start) + 1;
169 } else {
170 len = end - start + 1;
171 end++;
172 }
173 if (len < BUS_ID_SIZE) {
174 strlcpy(bus_id, start, len);
175 rc = 0;
176 } else
177 rc = -EINVAL;
178 *buf = end;
179 return rc;
180}
181
182static int __is_valid_bus_id(char bus_id[BUS_ID_SIZE])
183{
184 int cssid, ssid, devno;
185
186 /* Must be of form %x.%x.%04x */
187 if (sscanf(bus_id, "%x.%1x.%04x", &cssid, &ssid, &devno) != 3)
188 return 0;
189 return 1;
190}
191
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200192/**
Ursula Braun022b6602008-04-24 10:15:20 +0200193 * ccwgroup_create_from_string() - create and register a ccw group device
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200194 * @root: parent device for the new device
195 * @creator_id: identifier of creating driver
196 * @cdrv: ccw driver of slave devices
Ursula Braun022b6602008-04-24 10:15:20 +0200197 * @num_devices: number of slave devices
198 * @buf: buffer containing comma separated bus ids of slave devices
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200199 *
200 * Create and register a new ccw group device as a child of @root. Slave
Ursula Braun022b6602008-04-24 10:15:20 +0200201 * devices are obtained from the list of bus ids given in @buf and must all
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200202 * belong to @cdrv.
203 * Returns:
204 * %0 on success and an error code on failure.
205 * Context:
206 * non-atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 */
Ursula Braun022b6602008-04-24 10:15:20 +0200208int ccwgroup_create_from_string(struct device *root, unsigned int creator_id,
209 struct ccw_driver *cdrv, int num_devices,
210 const char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 struct ccwgroup_device *gdev;
Ursula Braun022b6602008-04-24 10:15:20 +0200213 int rc, i;
214 char tmp_bus_id[BUS_ID_SIZE];
215 const char *curr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Ursula Braun022b6602008-04-24 10:15:20 +0200217 gdev = kzalloc(sizeof(*gdev) + num_devices * sizeof(gdev->cdev[0]),
218 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (!gdev)
220 return -ENOMEM;
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 atomic_set(&gdev->onoff, 0);
Cornelia Huckd76123e2007-04-27 16:01:37 +0200223 mutex_init(&gdev->reg_mutex);
224 mutex_lock(&gdev->reg_mutex);
Ursula Braun022b6602008-04-24 10:15:20 +0200225 curr_buf = buf;
226 for (i = 0; i < num_devices && curr_buf; i++) {
227 rc = __get_next_bus_id(&curr_buf, tmp_bus_id);
228 if (rc != 0)
229 goto error;
230 if (!__is_valid_bus_id(tmp_bus_id)) {
231 rc = -EINVAL;
232 goto error;
233 }
234 gdev->cdev[i] = get_ccwdev_by_busid(cdrv, tmp_bus_id);
235 /*
236 * All devices have to be of the same type in
237 * order to be grouped.
238 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (!gdev->cdev[i]
240 || gdev->cdev[i]->id.driver_info !=
241 gdev->cdev[0]->id.driver_info) {
242 rc = -EINVAL;
Cornelia Huckd76123e2007-04-27 16:01:37 +0200243 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245 /* Don't allow a device to belong to more than one group. */
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100246 if (dev_get_drvdata(&gdev->cdev[i]->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 rc = -EINVAL;
Cornelia Huckd76123e2007-04-27 16:01:37 +0200248 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100250 dev_set_drvdata(&gdev->cdev[i]->dev, gdev);
Cornelia Huck17088222006-07-27 14:00:33 +0200251 }
Ursula Braun022b6602008-04-24 10:15:20 +0200252 /* Check for sufficient number of bus ids. */
253 if (i < num_devices && !curr_buf) {
254 rc = -EINVAL;
255 goto error;
256 }
257 /* Check for trailing stuff. */
258 if (i == num_devices && strlen(curr_buf) > 0) {
259 rc = -EINVAL;
260 goto error;
261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 gdev->creator_id = creator_id;
Ursula Braun022b6602008-04-24 10:15:20 +0200263 gdev->count = num_devices;
Heiko Carstens292888c2006-08-30 14:33:35 +0200264 gdev->dev.bus = &ccwgroup_bus_type;
265 gdev->dev.parent = root;
266 gdev->dev.release = ccwgroup_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 snprintf (gdev->dev.bus_id, BUS_ID_SIZE, "%s",
269 gdev->cdev[0]->dev.bus_id);
270
271 rc = device_register(&gdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (rc)
Cornelia Huckd76123e2007-04-27 16:01:37 +0200273 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 get_device(&gdev->dev);
275 rc = device_create_file(&gdev->dev, &dev_attr_ungroup);
276
277 if (rc) {
278 device_unregister(&gdev->dev);
279 goto error;
280 }
281
282 rc = __ccwgroup_create_symlinks(gdev);
283 if (!rc) {
Cornelia Huckd76123e2007-04-27 16:01:37 +0200284 mutex_unlock(&gdev->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 put_device(&gdev->dev);
286 return 0;
287 }
288 device_remove_file(&gdev->dev, &dev_attr_ungroup);
289 device_unregister(&gdev->dev);
290error:
Ursula Braun022b6602008-04-24 10:15:20 +0200291 for (i = 0; i < num_devices; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (gdev->cdev[i]) {
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100293 if (dev_get_drvdata(&gdev->cdev[i]->dev) == gdev)
294 dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
Cornelia Huck17088222006-07-27 14:00:33 +0200295 put_device(&gdev->cdev[i]->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
Cornelia Huckd76123e2007-04-27 16:01:37 +0200297 mutex_unlock(&gdev->reg_mutex);
298 put_device(&gdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return rc;
300}
Ursula Braun022b6602008-04-24 10:15:20 +0200301EXPORT_SYMBOL(ccwgroup_create_from_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303static int __init
304init_ccwgroup (void)
305{
306 return bus_register (&ccwgroup_bus_type);
307}
308
309static void __exit
310cleanup_ccwgroup (void)
311{
312 bus_unregister (&ccwgroup_bus_type);
313}
314
315module_init(init_ccwgroup);
316module_exit(cleanup_ccwgroup);
317
318/************************** driver stuff ******************************/
319
320static int
321ccwgroup_set_online(struct ccwgroup_device *gdev)
322{
323 struct ccwgroup_driver *gdrv;
324 int ret;
325
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800326 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return -EAGAIN;
328 if (gdev->state == CCWGROUP_ONLINE) {
329 ret = 0;
330 goto out;
331 }
332 if (!gdev->dev.driver) {
333 ret = -EINVAL;
334 goto out;
335 }
336 gdrv = to_ccwgroupdrv (gdev->dev.driver);
Cornelia Hucka0016402005-11-07 00:59:05 -0800337 if ((ret = gdrv->set_online ? gdrv->set_online(gdev) : 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 goto out;
339
340 gdev->state = CCWGROUP_ONLINE;
341 out:
342 atomic_set(&gdev->onoff, 0);
343 return ret;
344}
345
346static int
347ccwgroup_set_offline(struct ccwgroup_device *gdev)
348{
349 struct ccwgroup_driver *gdrv;
350 int ret;
351
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800352 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return -EAGAIN;
354 if (gdev->state == CCWGROUP_OFFLINE) {
355 ret = 0;
356 goto out;
357 }
358 if (!gdev->dev.driver) {
359 ret = -EINVAL;
360 goto out;
361 }
362 gdrv = to_ccwgroupdrv (gdev->dev.driver);
Cornelia Hucka0016402005-11-07 00:59:05 -0800363 if ((ret = gdrv->set_offline ? gdrv->set_offline(gdev) : 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 goto out;
365
366 gdev->state = CCWGROUP_OFFLINE;
367 out:
368 atomic_set(&gdev->onoff, 0);
369 return ret;
370}
371
372static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400373ccwgroup_online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
375 struct ccwgroup_device *gdev;
376 struct ccwgroup_driver *gdrv;
377 unsigned int value;
378 int ret;
379
380 gdev = to_ccwgroupdev(dev);
381 if (!dev->driver)
382 return count;
383
384 gdrv = to_ccwgroupdrv (gdev->dev.driver);
385 if (!try_module_get(gdrv->owner))
386 return -EINVAL;
387
Heiko Carstensd2c993d2006-07-12 16:41:55 +0200388 value = simple_strtoul(buf, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 ret = count;
390 if (value == 1)
391 ccwgroup_set_online(gdev);
392 else if (value == 0)
393 ccwgroup_set_offline(gdev);
394 else
395 ret = -EINVAL;
396 module_put(gdrv->owner);
397 return ret;
398}
399
400static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400401ccwgroup_online_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
403 int online;
404
405 online = (to_ccwgroupdev(dev)->state == CCWGROUP_ONLINE);
406
407 return sprintf(buf, online ? "1\n" : "0\n");
408}
409
410static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store);
411
412static int
413ccwgroup_probe (struct device *dev)
414{
415 struct ccwgroup_device *gdev;
416 struct ccwgroup_driver *gdrv;
417
418 int ret;
419
420 gdev = to_ccwgroupdev(dev);
421 gdrv = to_ccwgroupdrv(dev->driver);
422
423 if ((ret = device_create_file(dev, &dev_attr_online)))
424 return ret;
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 ret = gdrv->probe ? gdrv->probe(gdev) : -ENODEV;
427 if (ret)
428 device_remove_file(dev, &dev_attr_online);
429
430 return ret;
431}
432
433static int
434ccwgroup_remove (struct device *dev)
435{
436 struct ccwgroup_device *gdev;
437 struct ccwgroup_driver *gdrv;
438
439 gdev = to_ccwgroupdev(dev);
440 gdrv = to_ccwgroupdrv(dev->driver);
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 device_remove_file(dev, &dev_attr_online);
443
444 if (gdrv && gdrv->remove)
445 gdrv->remove(gdev);
446 return 0;
447}
448
Cornelia Huck01bc8ad2008-02-05 16:50:36 +0100449static void ccwgroup_shutdown(struct device *dev)
450{
451 struct ccwgroup_device *gdev;
452 struct ccwgroup_driver *gdrv;
453
454 gdev = to_ccwgroupdev(dev);
455 gdrv = to_ccwgroupdrv(dev->driver);
456 if (gdrv && gdrv->shutdown)
457 gdrv->shutdown(gdev);
458}
459
Russell Kingf9ccf452006-01-05 14:42:09 +0000460static struct bus_type ccwgroup_bus_type = {
461 .name = "ccwgroup",
462 .match = ccwgroup_bus_match,
463 .uevent = ccwgroup_uevent,
464 .probe = ccwgroup_probe,
465 .remove = ccwgroup_remove,
Cornelia Huck01bc8ad2008-02-05 16:50:36 +0100466 .shutdown = ccwgroup_shutdown,
Russell Kingf9ccf452006-01-05 14:42:09 +0000467};
468
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200469/**
470 * ccwgroup_driver_register() - register a ccw group driver
471 * @cdriver: driver to be registered
472 *
473 * This function is mainly a wrapper around driver_register().
474 */
475int ccwgroup_driver_register(struct ccwgroup_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 /* register our new driver with the core */
Heiko Carstens292888c2006-08-30 14:33:35 +0200478 cdriver->driver.bus = &ccwgroup_bus_type;
479 cdriver->driver.name = cdriver->name;
Cornelia Huck4beee642008-01-26 14:10:47 +0100480 cdriver->driver.owner = cdriver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 return driver_register(&cdriver->driver);
483}
484
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700485static int
Cornelia Huck887ab592006-06-29 14:56:52 +0200486__ccwgroup_match_all(struct device *dev, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Cornelia Huck887ab592006-06-29 14:56:52 +0200488 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200491/**
492 * ccwgroup_driver_unregister() - deregister a ccw group driver
493 * @cdriver: driver to be deregistered
494 *
495 * This function is mainly a wrapper around driver_unregister().
496 */
497void ccwgroup_driver_unregister(struct ccwgroup_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Cornelia Huck887ab592006-06-29 14:56:52 +0200499 struct device *dev;
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 /* We don't want ccwgroup devices to live longer than their driver. */
502 get_driver(&cdriver->driver);
Cornelia Huck887ab592006-06-29 14:56:52 +0200503 while ((dev = driver_find_device(&cdriver->driver, NULL, NULL,
504 __ccwgroup_match_all))) {
Cornelia Huckd76123e2007-04-27 16:01:37 +0200505 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
506
507 mutex_lock(&gdev->reg_mutex);
508 __ccwgroup_remove_symlinks(gdev);
Cornelia Huck887ab592006-06-29 14:56:52 +0200509 device_unregister(dev);
Cornelia Huckd76123e2007-04-27 16:01:37 +0200510 mutex_unlock(&gdev->reg_mutex);
Cornelia Huck887ab592006-06-29 14:56:52 +0200511 put_device(dev);
512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 put_driver(&cdriver->driver);
514 driver_unregister(&cdriver->driver);
515}
516
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200517/**
518 * ccwgroup_probe_ccwdev() - probe function for slave devices
519 * @cdev: ccw device to be probed
520 *
521 * This is a dummy probe function for ccw devices that are slave devices in
522 * a ccw group device.
523 * Returns:
524 * always %0
525 */
526int ccwgroup_probe_ccwdev(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 return 0;
529}
530
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100531static struct ccwgroup_device *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532__ccwgroup_get_gdev_by_cdev(struct ccw_device *cdev)
533{
534 struct ccwgroup_device *gdev;
535
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100536 gdev = dev_get_drvdata(&cdev->dev);
537 if (gdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (get_device(&gdev->dev)) {
Cornelia Huckd76123e2007-04-27 16:01:37 +0200539 mutex_lock(&gdev->reg_mutex);
Daniel Ritzd305ef52005-09-22 00:47:24 -0700540 if (device_is_registered(&gdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return gdev;
Cornelia Huckd76123e2007-04-27 16:01:37 +0200542 mutex_unlock(&gdev->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 put_device(&gdev->dev);
544 }
545 return NULL;
546 }
547 return NULL;
548}
549
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200550/**
551 * ccwgroup_remove_ccwdev() - remove function for slave devices
552 * @cdev: ccw device to be removed
553 *
554 * This is a remove function for ccw devices that are slave devices in a ccw
555 * group device. It sets the ccw device offline and also deregisters the
556 * embedding ccw group device.
557 */
558void ccwgroup_remove_ccwdev(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 struct ccwgroup_device *gdev;
561
562 /* Ignore offlining errors, device is gone anyway. */
563 ccw_device_set_offline(cdev);
564 /* If one of its devices is gone, the whole group is done for. */
565 gdev = __ccwgroup_get_gdev_by_cdev(cdev);
566 if (gdev) {
567 __ccwgroup_remove_symlinks(gdev);
568 device_unregister(&gdev->dev);
Cornelia Huckd76123e2007-04-27 16:01:37 +0200569 mutex_unlock(&gdev->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 put_device(&gdev->dev);
571 }
572}
573
574MODULE_LICENSE("GPL");
575EXPORT_SYMBOL(ccwgroup_driver_register);
576EXPORT_SYMBOL(ccwgroup_driver_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577EXPORT_SYMBOL(ccwgroup_probe_ccwdev);
578EXPORT_SYMBOL(ccwgroup_remove_ccwdev);