blob: 26a930e832bdab58c7f649c866124200e1f3f8c8 [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++) {
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100115 dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 put_device(&gdev->cdev[i]->dev);
117 }
118 kfree(gdev);
119}
120
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100121static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122__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 Braun022b6602008-04-24 10:15:20 +0200155static 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
181static 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 Huckb2ffd8e2007-10-12 16:11:17 +0200191/**
Ursula Braun022b6602008-04-24 10:15:20 +0200192 * ccwgroup_create_from_string() - create and register a ccw group device
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200193 * @root: parent device for the new device
194 * @creator_id: identifier of creating driver
195 * @cdrv: ccw driver of slave devices
Ursula Braun022b6602008-04-24 10:15:20 +0200196 * @num_devices: number of slave devices
197 * @buf: buffer containing comma separated bus ids of slave devices
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200198 *
199 * Create and register a new ccw group device as a child of @root. Slave
Ursula Braun022b6602008-04-24 10:15:20 +0200200 * devices are obtained from the list of bus ids given in @buf and must all
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200201 * belong to @cdrv.
202 * Returns:
203 * %0 on success and an error code on failure.
204 * Context:
205 * non-atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 */
Ursula Braun022b6602008-04-24 10:15:20 +0200207int ccwgroup_create_from_string(struct device *root, unsigned int creator_id,
208 struct ccw_driver *cdrv, int num_devices,
209 const char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 struct ccwgroup_device *gdev;
Ursula Braun022b6602008-04-24 10:15:20 +0200212 int rc, i;
213 char tmp_bus_id[BUS_ID_SIZE];
214 const char *curr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Ursula Braun022b6602008-04-24 10:15:20 +0200216 gdev = kzalloc(sizeof(*gdev) + num_devices * sizeof(gdev->cdev[0]),
217 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (!gdev)
219 return -ENOMEM;
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 atomic_set(&gdev->onoff, 0);
Cornelia Huckd76123e2007-04-27 16:01:37 +0200222 mutex_init(&gdev->reg_mutex);
223 mutex_lock(&gdev->reg_mutex);
Ursula Braun022b6602008-04-24 10:15:20 +0200224 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 Torvalds1da177e2005-04-16 15:20:36 -0700238 if (!gdev->cdev[i]
239 || gdev->cdev[i]->id.driver_info !=
240 gdev->cdev[0]->id.driver_info) {
241 rc = -EINVAL;
Cornelia Huckd76123e2007-04-27 16:01:37 +0200242 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244 /* Don't allow a device to belong to more than one group. */
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100245 if (dev_get_drvdata(&gdev->cdev[i]->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 rc = -EINVAL;
Cornelia Huckd76123e2007-04-27 16:01:37 +0200247 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100249 dev_set_drvdata(&gdev->cdev[i]->dev, gdev);
Cornelia Huck17088222006-07-27 14:00:33 +0200250 }
Ursula Braun022b6602008-04-24 10:15:20 +0200251 /* 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 Torvalds1da177e2005-04-16 15:20:36 -0700261 gdev->creator_id = creator_id;
Ursula Braun022b6602008-04-24 10:15:20 +0200262 gdev->count = num_devices;
Heiko Carstens292888c2006-08-30 14:33:35 +0200263 gdev->dev.bus = &ccwgroup_bus_type;
264 gdev->dev.parent = root;
265 gdev->dev.release = ccwgroup_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
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 Torvalds1da177e2005-04-16 15:20:36 -0700271 if (rc)
Cornelia Huckd76123e2007-04-27 16:01:37 +0200272 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 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 Huckd76123e2007-04-27 16:01:37 +0200283 mutex_unlock(&gdev->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 put_device(&gdev->dev);
285 return 0;
286 }
287 device_remove_file(&gdev->dev, &dev_attr_ungroup);
288 device_unregister(&gdev->dev);
289error:
Ursula Braun022b6602008-04-24 10:15:20 +0200290 for (i = 0; i < num_devices; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (gdev->cdev[i]) {
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100292 if (dev_get_drvdata(&gdev->cdev[i]->dev) == gdev)
293 dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
Cornelia Huck17088222006-07-27 14:00:33 +0200294 put_device(&gdev->cdev[i]->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
Cornelia Huckd76123e2007-04-27 16:01:37 +0200296 mutex_unlock(&gdev->reg_mutex);
297 put_device(&gdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 return rc;
299}
Ursula Braun022b6602008-04-24 10:15:20 +0200300EXPORT_SYMBOL(ccwgroup_create_from_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302static int __init
303init_ccwgroup (void)
304{
305 return bus_register (&ccwgroup_bus_type);
306}
307
308static void __exit
309cleanup_ccwgroup (void)
310{
311 bus_unregister (&ccwgroup_bus_type);
312}
313
314module_init(init_ccwgroup);
315module_exit(cleanup_ccwgroup);
316
317/************************** driver stuff ******************************/
318
319static int
320ccwgroup_set_online(struct ccwgroup_device *gdev)
321{
322 struct ccwgroup_driver *gdrv;
323 int ret;
324
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800325 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 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 Hucka0016402005-11-07 00:59:05 -0800336 if ((ret = gdrv->set_online ? gdrv->set_online(gdev) : 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 goto out;
338
339 gdev->state = CCWGROUP_ONLINE;
340 out:
341 atomic_set(&gdev->onoff, 0);
342 return ret;
343}
344
345static int
346ccwgroup_set_offline(struct ccwgroup_device *gdev)
347{
348 struct ccwgroup_driver *gdrv;
349 int ret;
350
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800351 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 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 Hucka0016402005-11-07 00:59:05 -0800362 if ((ret = gdrv->set_offline ? gdrv->set_offline(gdev) : 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 goto out;
364
365 gdev->state = CCWGROUP_OFFLINE;
366 out:
367 atomic_set(&gdev->onoff, 0);
368 return ret;
369}
370
371static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400372ccwgroup_online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 struct ccwgroup_device *gdev;
375 struct ccwgroup_driver *gdrv;
Cornelia Huck2f972202008-04-30 13:38:33 +0200376 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 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 Huck2f972202008-04-30 13:38:33 +0200387 ret = strict_strtoul(buf, 0, &value);
388 if (ret)
389 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 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 Huck2f972202008-04-30 13:38:33 +0200397out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 module_put(gdrv->owner);
399 return ret;
400}
401
402static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400403ccwgroup_online_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
405 int online;
406
407 online = (to_ccwgroupdev(dev)->state == CCWGROUP_ONLINE);
408
409 return sprintf(buf, online ? "1\n" : "0\n");
410}
411
412static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store);
413
414static int
415ccwgroup_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 Torvalds1da177e2005-04-16 15:20:36 -0700428 ret = gdrv->probe ? gdrv->probe(gdev) : -ENODEV;
429 if (ret)
430 device_remove_file(dev, &dev_attr_online);
431
432 return ret;
433}
434
435static int
436ccwgroup_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 Torvalds1da177e2005-04-16 15:20:36 -0700444 device_remove_file(dev, &dev_attr_online);
445
446 if (gdrv && gdrv->remove)
447 gdrv->remove(gdev);
448 return 0;
449}
450
Cornelia Huck01bc8ad2008-02-05 16:50:36 +0100451static 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 Kingf9ccf452006-01-05 14:42:09 +0000462static 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 Huck01bc8ad2008-02-05 16:50:36 +0100468 .shutdown = ccwgroup_shutdown,
Russell Kingf9ccf452006-01-05 14:42:09 +0000469};
470
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200471/**
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 */
477int ccwgroup_driver_register(struct ccwgroup_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 /* register our new driver with the core */
Heiko Carstens292888c2006-08-30 14:33:35 +0200480 cdriver->driver.bus = &ccwgroup_bus_type;
481 cdriver->driver.name = cdriver->name;
Cornelia Huck4beee642008-01-26 14:10:47 +0100482 cdriver->driver.owner = cdriver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 return driver_register(&cdriver->driver);
485}
486
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700487static int
Cornelia Huck887ab592006-06-29 14:56:52 +0200488__ccwgroup_match_all(struct device *dev, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Cornelia Huck887ab592006-06-29 14:56:52 +0200490 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200493/**
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 */
499void ccwgroup_driver_unregister(struct ccwgroup_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Cornelia Huck887ab592006-06-29 14:56:52 +0200501 struct device *dev;
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 /* We don't want ccwgroup devices to live longer than their driver. */
504 get_driver(&cdriver->driver);
Cornelia Huck887ab592006-06-29 14:56:52 +0200505 while ((dev = driver_find_device(&cdriver->driver, NULL, NULL,
506 __ccwgroup_match_all))) {
Cornelia Huckd76123e2007-04-27 16:01:37 +0200507 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
508
509 mutex_lock(&gdev->reg_mutex);
510 __ccwgroup_remove_symlinks(gdev);
Cornelia Huck887ab592006-06-29 14:56:52 +0200511 device_unregister(dev);
Cornelia Huckd76123e2007-04-27 16:01:37 +0200512 mutex_unlock(&gdev->reg_mutex);
Cornelia Huck887ab592006-06-29 14:56:52 +0200513 put_device(dev);
514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 put_driver(&cdriver->driver);
516 driver_unregister(&cdriver->driver);
517}
518
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200519/**
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 */
528int ccwgroup_probe_ccwdev(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
530 return 0;
531}
532
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100533static struct ccwgroup_device *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534__ccwgroup_get_gdev_by_cdev(struct ccw_device *cdev)
535{
536 struct ccwgroup_device *gdev;
537
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100538 gdev = dev_get_drvdata(&cdev->dev);
539 if (gdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (get_device(&gdev->dev)) {
Cornelia Huckd76123e2007-04-27 16:01:37 +0200541 mutex_lock(&gdev->reg_mutex);
Daniel Ritzd305ef52005-09-22 00:47:24 -0700542 if (device_is_registered(&gdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return gdev;
Cornelia Huckd76123e2007-04-27 16:01:37 +0200544 mutex_unlock(&gdev->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 put_device(&gdev->dev);
546 }
547 return NULL;
548 }
549 return NULL;
550}
551
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200552/**
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 */
560void ccwgroup_remove_ccwdev(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
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 Huckd76123e2007-04-27 16:01:37 +0200571 mutex_unlock(&gdev->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 put_device(&gdev->dev);
573 }
574}
575
576MODULE_LICENSE("GPL");
577EXPORT_SYMBOL(ccwgroup_driver_register);
578EXPORT_SYMBOL(ccwgroup_driver_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579EXPORT_SYMBOL(ccwgroup_probe_ccwdev);
580EXPORT_SYMBOL(ccwgroup_remove_ccwdev);