blob: 3ac2c2019f5e28ffae89e7c0d07f965be297f500 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#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 */
31static int
32ccwgroup_bus_match (struct device * dev, struct device_driver * drv)
33{
34 struct ccwgroup_device *gdev;
35 struct ccwgroup_driver *gdrv;
36
Cornelia Huck084325d2008-01-26 14:10:38 +010037 gdev = to_ccwgroupdev(dev);
38 gdrv = to_ccwgroupdrv(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40 if (gdev->creator_id == gdrv->driver_id)
41 return 1;
42
43 return 0;
44}
45static int
Kay Sievers7eff2e72007-08-14 15:15:12 +020046ccwgroup_uevent (struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
48 /* TODO */
49 return 0;
50}
51
Russell Kingf9ccf452006-01-05 14:42:09 +000052static struct bus_type ccwgroup_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Heiko Carstens4d284ca2007-02-05 21:18:53 +010054static void
Linus Torvalds1da177e2005-04-16 15:20:36 -070055__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 Sternd9a9cdf2007-03-15 15:50:34 -040072static void ccwgroup_ungroup_callback(struct device *dev)
73{
74 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
75
Cornelia Huckd76123e2007-04-27 16:01:37 +020076 mutex_lock(&gdev->reg_mutex);
Cornelia Huck1a908c72008-01-26 14:10:50 +010077 if (device_is_registered(&gdev->dev)) {
78 __ccwgroup_remove_symlinks(gdev);
79 device_unregister(dev);
80 }
Cornelia Huckd76123e2007-04-27 16:01:37 +020081 mutex_unlock(&gdev->reg_mutex);
Alan Sternd9a9cdf2007-03-15 15:50:34 -040082}
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -040085ccwgroup_ungroup_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 struct ccwgroup_device *gdev;
Alan Sternd9a9cdf2007-03-15 15:50:34 -040088 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 gdev = to_ccwgroupdev(dev);
91
92 if (gdev->state != CCWGROUP_OFFLINE)
93 return -EINVAL;
94
Alan Sternd9a9cdf2007-03-15 15:50:34 -040095 /* 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 Torvalds1da177e2005-04-16 15:20:36 -0700101 return count;
102}
103
104static DEVICE_ATTR(ungroup, 0200, NULL, ccwgroup_ungroup_store);
105
106static void
107ccwgroup_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++) {
Peter Oberparleiter16f7f952008-08-21 19:46:36 +0200115 if (gdev->cdev[i]) {
Cornelia Huckf26fd5d2008-09-16 09:32:18 -0700116 if (dev_get_drvdata(&gdev->cdev[i]->dev) == gdev)
117 dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
Peter Oberparleiter16f7f952008-08-21 19:46:36 +0200118 put_device(&gdev->cdev[i]->dev);
119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
121 kfree(gdev);
122}
123
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100124static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125__ccwgroup_create_symlinks(struct ccwgroup_device *gdev)
126{
127 char str[8];
128 int i, rc;
129
130 for (i = 0; i < gdev->count; i++) {
131 rc = sysfs_create_link(&gdev->cdev[i]->dev.kobj, &gdev->dev.kobj,
132 "group_device");
133 if (rc) {
134 for (--i; i >= 0; i--)
135 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
136 "group_device");
137 return rc;
138 }
139 }
140 for (i = 0; i < gdev->count; i++) {
141 sprintf(str, "cdev%d", i);
142 rc = sysfs_create_link(&gdev->dev.kobj, &gdev->cdev[i]->dev.kobj,
143 str);
144 if (rc) {
145 for (--i; i >= 0; i--) {
146 sprintf(str, "cdev%d", i);
147 sysfs_remove_link(&gdev->dev.kobj, str);
148 }
149 for (i = 0; i < gdev->count; i++)
150 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
151 "group_device");
152 return rc;
153 }
154 }
155 return 0;
156}
157
Ursula Braun022b6602008-04-24 10:15:20 +0200158static int __get_next_bus_id(const char **buf, char *bus_id)
159{
160 int rc, len;
161 char *start, *end;
162
163 start = (char *)*buf;
164 end = strchr(start, ',');
165 if (!end) {
166 /* Last entry. Strip trailing newline, if applicable. */
167 end = strchr(start, '\n');
168 if (end)
169 *end = '\0';
170 len = strlen(start) + 1;
171 } else {
172 len = end - start + 1;
173 end++;
174 }
175 if (len < BUS_ID_SIZE) {
176 strlcpy(bus_id, start, len);
177 rc = 0;
178 } else
179 rc = -EINVAL;
180 *buf = end;
181 return rc;
182}
183
184static int __is_valid_bus_id(char bus_id[BUS_ID_SIZE])
185{
186 int cssid, ssid, devno;
187
188 /* Must be of form %x.%x.%04x */
189 if (sscanf(bus_id, "%x.%1x.%04x", &cssid, &ssid, &devno) != 3)
190 return 0;
191 return 1;
192}
193
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200194/**
Ursula Braun022b6602008-04-24 10:15:20 +0200195 * ccwgroup_create_from_string() - create and register a ccw group device
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200196 * @root: parent device for the new device
197 * @creator_id: identifier of creating driver
198 * @cdrv: ccw driver of slave devices
Ursula Braun022b6602008-04-24 10:15:20 +0200199 * @num_devices: number of slave devices
200 * @buf: buffer containing comma separated bus ids of slave devices
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200201 *
202 * Create and register a new ccw group device as a child of @root. Slave
Ursula Braun022b6602008-04-24 10:15:20 +0200203 * devices are obtained from the list of bus ids given in @buf and must all
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200204 * belong to @cdrv.
205 * Returns:
206 * %0 on success and an error code on failure.
207 * Context:
208 * non-atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 */
Ursula Braun022b6602008-04-24 10:15:20 +0200210int ccwgroup_create_from_string(struct device *root, unsigned int creator_id,
211 struct ccw_driver *cdrv, int num_devices,
212 const char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 struct ccwgroup_device *gdev;
Ursula Braun022b6602008-04-24 10:15:20 +0200215 int rc, i;
216 char tmp_bus_id[BUS_ID_SIZE];
217 const char *curr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Ursula Braun022b6602008-04-24 10:15:20 +0200219 gdev = kzalloc(sizeof(*gdev) + num_devices * sizeof(gdev->cdev[0]),
220 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if (!gdev)
222 return -ENOMEM;
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 atomic_set(&gdev->onoff, 0);
Cornelia Huckd76123e2007-04-27 16:01:37 +0200225 mutex_init(&gdev->reg_mutex);
226 mutex_lock(&gdev->reg_mutex);
Peter Oberparleiter16f7f952008-08-21 19:46:36 +0200227 gdev->creator_id = creator_id;
228 gdev->count = num_devices;
229 gdev->dev.bus = &ccwgroup_bus_type;
230 gdev->dev.parent = root;
231 gdev->dev.release = ccwgroup_release;
232 device_initialize(&gdev->dev);
233
Ursula Braun022b6602008-04-24 10:15:20 +0200234 curr_buf = buf;
235 for (i = 0; i < num_devices && curr_buf; i++) {
236 rc = __get_next_bus_id(&curr_buf, tmp_bus_id);
237 if (rc != 0)
238 goto error;
239 if (!__is_valid_bus_id(tmp_bus_id)) {
240 rc = -EINVAL;
241 goto error;
242 }
243 gdev->cdev[i] = get_ccwdev_by_busid(cdrv, tmp_bus_id);
244 /*
245 * All devices have to be of the same type in
246 * order to be grouped.
247 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 if (!gdev->cdev[i]
249 || gdev->cdev[i]->id.driver_info !=
250 gdev->cdev[0]->id.driver_info) {
251 rc = -EINVAL;
Cornelia Huckd76123e2007-04-27 16:01:37 +0200252 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254 /* Don't allow a device to belong to more than one group. */
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100255 if (dev_get_drvdata(&gdev->cdev[i]->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 rc = -EINVAL;
Cornelia Huckd76123e2007-04-27 16:01:37 +0200257 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100259 dev_set_drvdata(&gdev->cdev[i]->dev, gdev);
Cornelia Huck17088222006-07-27 14:00:33 +0200260 }
Ursula Braun022b6602008-04-24 10:15:20 +0200261 /* Check for sufficient number of bus ids. */
262 if (i < num_devices && !curr_buf) {
263 rc = -EINVAL;
264 goto error;
265 }
266 /* Check for trailing stuff. */
267 if (i == num_devices && strlen(curr_buf) > 0) {
268 rc = -EINVAL;
269 goto error;
270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Cornelia Huck1bf5b282008-10-10 21:33:10 +0200272 dev_set_name(&gdev->dev, "%s", dev_name(&gdev->cdev[0]->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Peter Oberparleiter16f7f952008-08-21 19:46:36 +0200274 rc = device_add(&gdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (rc)
Cornelia Huckd76123e2007-04-27 16:01:37 +0200276 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 get_device(&gdev->dev);
278 rc = device_create_file(&gdev->dev, &dev_attr_ungroup);
279
280 if (rc) {
281 device_unregister(&gdev->dev);
282 goto error;
283 }
284
285 rc = __ccwgroup_create_symlinks(gdev);
286 if (!rc) {
Cornelia Huckd76123e2007-04-27 16:01:37 +0200287 mutex_unlock(&gdev->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 put_device(&gdev->dev);
289 return 0;
290 }
291 device_remove_file(&gdev->dev, &dev_attr_ungroup);
292 device_unregister(&gdev->dev);
293error:
Ursula Braun022b6602008-04-24 10:15:20 +0200294 for (i = 0; i < num_devices; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (gdev->cdev[i]) {
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100296 if (dev_get_drvdata(&gdev->cdev[i]->dev) == gdev)
297 dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
Cornelia Huck17088222006-07-27 14:00:33 +0200298 put_device(&gdev->cdev[i]->dev);
Cornelia Huckf26fd5d2008-09-16 09:32:18 -0700299 gdev->cdev[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
Cornelia Huckd76123e2007-04-27 16:01:37 +0200301 mutex_unlock(&gdev->reg_mutex);
302 put_device(&gdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 return rc;
304}
Ursula Braun022b6602008-04-24 10:15:20 +0200305EXPORT_SYMBOL(ccwgroup_create_from_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307static int __init
308init_ccwgroup (void)
309{
310 return bus_register (&ccwgroup_bus_type);
311}
312
313static void __exit
314cleanup_ccwgroup (void)
315{
316 bus_unregister (&ccwgroup_bus_type);
317}
318
319module_init(init_ccwgroup);
320module_exit(cleanup_ccwgroup);
321
322/************************** driver stuff ******************************/
323
324static int
325ccwgroup_set_online(struct ccwgroup_device *gdev)
326{
327 struct ccwgroup_driver *gdrv;
328 int ret;
329
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800330 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return -EAGAIN;
332 if (gdev->state == CCWGROUP_ONLINE) {
333 ret = 0;
334 goto out;
335 }
336 if (!gdev->dev.driver) {
337 ret = -EINVAL;
338 goto out;
339 }
340 gdrv = to_ccwgroupdrv (gdev->dev.driver);
Cornelia Hucka0016402005-11-07 00:59:05 -0800341 if ((ret = gdrv->set_online ? gdrv->set_online(gdev) : 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 goto out;
343
344 gdev->state = CCWGROUP_ONLINE;
345 out:
346 atomic_set(&gdev->onoff, 0);
347 return ret;
348}
349
350static int
351ccwgroup_set_offline(struct ccwgroup_device *gdev)
352{
353 struct ccwgroup_driver *gdrv;
354 int ret;
355
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800356 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return -EAGAIN;
358 if (gdev->state == CCWGROUP_OFFLINE) {
359 ret = 0;
360 goto out;
361 }
362 if (!gdev->dev.driver) {
363 ret = -EINVAL;
364 goto out;
365 }
366 gdrv = to_ccwgroupdrv (gdev->dev.driver);
Cornelia Hucka0016402005-11-07 00:59:05 -0800367 if ((ret = gdrv->set_offline ? gdrv->set_offline(gdev) : 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 goto out;
369
370 gdev->state = CCWGROUP_OFFLINE;
371 out:
372 atomic_set(&gdev->onoff, 0);
373 return ret;
374}
375
376static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400377ccwgroup_online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
379 struct ccwgroup_device *gdev;
380 struct ccwgroup_driver *gdrv;
Cornelia Huck2f972202008-04-30 13:38:33 +0200381 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 int ret;
383
384 gdev = to_ccwgroupdev(dev);
385 if (!dev->driver)
386 return count;
387
388 gdrv = to_ccwgroupdrv (gdev->dev.driver);
389 if (!try_module_get(gdrv->owner))
390 return -EINVAL;
391
Cornelia Huck2f972202008-04-30 13:38:33 +0200392 ret = strict_strtoul(buf, 0, &value);
393 if (ret)
394 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 ret = count;
396 if (value == 1)
397 ccwgroup_set_online(gdev);
398 else if (value == 0)
399 ccwgroup_set_offline(gdev);
400 else
401 ret = -EINVAL;
Cornelia Huck2f972202008-04-30 13:38:33 +0200402out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 module_put(gdrv->owner);
404 return ret;
405}
406
407static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400408ccwgroup_online_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
410 int online;
411
412 online = (to_ccwgroupdev(dev)->state == CCWGROUP_ONLINE);
413
414 return sprintf(buf, online ? "1\n" : "0\n");
415}
416
417static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store);
418
419static int
420ccwgroup_probe (struct device *dev)
421{
422 struct ccwgroup_device *gdev;
423 struct ccwgroup_driver *gdrv;
424
425 int ret;
426
427 gdev = to_ccwgroupdev(dev);
428 gdrv = to_ccwgroupdrv(dev->driver);
429
430 if ((ret = device_create_file(dev, &dev_attr_online)))
431 return ret;
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 ret = gdrv->probe ? gdrv->probe(gdev) : -ENODEV;
434 if (ret)
435 device_remove_file(dev, &dev_attr_online);
436
437 return ret;
438}
439
440static int
441ccwgroup_remove (struct device *dev)
442{
443 struct ccwgroup_device *gdev;
444 struct ccwgroup_driver *gdrv;
445
446 gdev = to_ccwgroupdev(dev);
447 gdrv = to_ccwgroupdrv(dev->driver);
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 device_remove_file(dev, &dev_attr_online);
450
451 if (gdrv && gdrv->remove)
452 gdrv->remove(gdev);
453 return 0;
454}
455
Cornelia Huck01bc8ad2008-02-05 16:50:36 +0100456static void ccwgroup_shutdown(struct device *dev)
457{
458 struct ccwgroup_device *gdev;
459 struct ccwgroup_driver *gdrv;
460
461 gdev = to_ccwgroupdev(dev);
462 gdrv = to_ccwgroupdrv(dev->driver);
463 if (gdrv && gdrv->shutdown)
464 gdrv->shutdown(gdev);
465}
466
Russell Kingf9ccf452006-01-05 14:42:09 +0000467static struct bus_type ccwgroup_bus_type = {
468 .name = "ccwgroup",
469 .match = ccwgroup_bus_match,
470 .uevent = ccwgroup_uevent,
471 .probe = ccwgroup_probe,
472 .remove = ccwgroup_remove,
Cornelia Huck01bc8ad2008-02-05 16:50:36 +0100473 .shutdown = ccwgroup_shutdown,
Russell Kingf9ccf452006-01-05 14:42:09 +0000474};
475
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200476/**
477 * ccwgroup_driver_register() - register a ccw group driver
478 * @cdriver: driver to be registered
479 *
480 * This function is mainly a wrapper around driver_register().
481 */
482int ccwgroup_driver_register(struct ccwgroup_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
484 /* register our new driver with the core */
Heiko Carstens292888c2006-08-30 14:33:35 +0200485 cdriver->driver.bus = &ccwgroup_bus_type;
486 cdriver->driver.name = cdriver->name;
Cornelia Huck4beee642008-01-26 14:10:47 +0100487 cdriver->driver.owner = cdriver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 return driver_register(&cdriver->driver);
490}
491
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700492static int
Cornelia Huck887ab592006-06-29 14:56:52 +0200493__ccwgroup_match_all(struct device *dev, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Cornelia Huck887ab592006-06-29 14:56:52 +0200495 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200498/**
499 * ccwgroup_driver_unregister() - deregister a ccw group driver
500 * @cdriver: driver to be deregistered
501 *
502 * This function is mainly a wrapper around driver_unregister().
503 */
504void ccwgroup_driver_unregister(struct ccwgroup_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Cornelia Huck887ab592006-06-29 14:56:52 +0200506 struct device *dev;
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 /* We don't want ccwgroup devices to live longer than their driver. */
509 get_driver(&cdriver->driver);
Cornelia Huck887ab592006-06-29 14:56:52 +0200510 while ((dev = driver_find_device(&cdriver->driver, NULL, NULL,
511 __ccwgroup_match_all))) {
Cornelia Huckd76123e2007-04-27 16:01:37 +0200512 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
513
514 mutex_lock(&gdev->reg_mutex);
515 __ccwgroup_remove_symlinks(gdev);
Cornelia Huck887ab592006-06-29 14:56:52 +0200516 device_unregister(dev);
Cornelia Huckd76123e2007-04-27 16:01:37 +0200517 mutex_unlock(&gdev->reg_mutex);
Cornelia Huck887ab592006-06-29 14:56:52 +0200518 put_device(dev);
519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 put_driver(&cdriver->driver);
521 driver_unregister(&cdriver->driver);
522}
523
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200524/**
525 * ccwgroup_probe_ccwdev() - probe function for slave devices
526 * @cdev: ccw device to be probed
527 *
528 * This is a dummy probe function for ccw devices that are slave devices in
529 * a ccw group device.
530 * Returns:
531 * always %0
532 */
533int ccwgroup_probe_ccwdev(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
535 return 0;
536}
537
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100538static struct ccwgroup_device *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539__ccwgroup_get_gdev_by_cdev(struct ccw_device *cdev)
540{
541 struct ccwgroup_device *gdev;
542
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100543 gdev = dev_get_drvdata(&cdev->dev);
544 if (gdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (get_device(&gdev->dev)) {
Cornelia Huckd76123e2007-04-27 16:01:37 +0200546 mutex_lock(&gdev->reg_mutex);
Daniel Ritzd305ef52005-09-22 00:47:24 -0700547 if (device_is_registered(&gdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return gdev;
Cornelia Huckd76123e2007-04-27 16:01:37 +0200549 mutex_unlock(&gdev->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 put_device(&gdev->dev);
551 }
552 return NULL;
553 }
554 return NULL;
555}
556
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200557/**
558 * ccwgroup_remove_ccwdev() - remove function for slave devices
559 * @cdev: ccw device to be removed
560 *
561 * This is a remove function for ccw devices that are slave devices in a ccw
562 * group device. It sets the ccw device offline and also deregisters the
563 * embedding ccw group device.
564 */
565void ccwgroup_remove_ccwdev(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
567 struct ccwgroup_device *gdev;
568
569 /* Ignore offlining errors, device is gone anyway. */
570 ccw_device_set_offline(cdev);
571 /* If one of its devices is gone, the whole group is done for. */
572 gdev = __ccwgroup_get_gdev_by_cdev(cdev);
573 if (gdev) {
574 __ccwgroup_remove_symlinks(gdev);
575 device_unregister(&gdev->dev);
Cornelia Huckd76123e2007-04-27 16:01:37 +0200576 mutex_unlock(&gdev->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 put_device(&gdev->dev);
578 }
579}
580
581MODULE_LICENSE("GPL");
582EXPORT_SYMBOL(ccwgroup_driver_register);
583EXPORT_SYMBOL(ccwgroup_driver_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584EXPORT_SYMBOL(ccwgroup_probe_ccwdev);
585EXPORT_SYMBOL(ccwgroup_remove_ccwdev);