blob: e0ce65fca4e797ed2da7370093a56f1a86d04c81 [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]) {
116 dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
117 put_device(&gdev->cdev[i]->dev);
118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
120 kfree(gdev);
121}
122
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100123static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124__ccwgroup_create_symlinks(struct ccwgroup_device *gdev)
125{
126 char str[8];
127 int i, rc;
128
129 for (i = 0; i < gdev->count; i++) {
130 rc = sysfs_create_link(&gdev->cdev[i]->dev.kobj, &gdev->dev.kobj,
131 "group_device");
132 if (rc) {
133 for (--i; i >= 0; i--)
134 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
135 "group_device");
136 return rc;
137 }
138 }
139 for (i = 0; i < gdev->count; i++) {
140 sprintf(str, "cdev%d", i);
141 rc = sysfs_create_link(&gdev->dev.kobj, &gdev->cdev[i]->dev.kobj,
142 str);
143 if (rc) {
144 for (--i; i >= 0; i--) {
145 sprintf(str, "cdev%d", i);
146 sysfs_remove_link(&gdev->dev.kobj, str);
147 }
148 for (i = 0; i < gdev->count; i++)
149 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
150 "group_device");
151 return rc;
152 }
153 }
154 return 0;
155}
156
Ursula Braun022b6602008-04-24 10:15:20 +0200157static int __get_next_bus_id(const char **buf, char *bus_id)
158{
159 int rc, len;
160 char *start, *end;
161
162 start = (char *)*buf;
163 end = strchr(start, ',');
164 if (!end) {
165 /* Last entry. Strip trailing newline, if applicable. */
166 end = strchr(start, '\n');
167 if (end)
168 *end = '\0';
169 len = strlen(start) + 1;
170 } else {
171 len = end - start + 1;
172 end++;
173 }
174 if (len < BUS_ID_SIZE) {
175 strlcpy(bus_id, start, len);
176 rc = 0;
177 } else
178 rc = -EINVAL;
179 *buf = end;
180 return rc;
181}
182
183static int __is_valid_bus_id(char bus_id[BUS_ID_SIZE])
184{
185 int cssid, ssid, devno;
186
187 /* Must be of form %x.%x.%04x */
188 if (sscanf(bus_id, "%x.%1x.%04x", &cssid, &ssid, &devno) != 3)
189 return 0;
190 return 1;
191}
192
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200193/**
Ursula Braun022b6602008-04-24 10:15:20 +0200194 * ccwgroup_create_from_string() - create and register a ccw group device
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200195 * @root: parent device for the new device
196 * @creator_id: identifier of creating driver
197 * @cdrv: ccw driver of slave devices
Ursula Braun022b6602008-04-24 10:15:20 +0200198 * @num_devices: number of slave devices
199 * @buf: buffer containing comma separated bus ids of slave devices
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200200 *
201 * Create and register a new ccw group device as a child of @root. Slave
Ursula Braun022b6602008-04-24 10:15:20 +0200202 * devices are obtained from the list of bus ids given in @buf and must all
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200203 * belong to @cdrv.
204 * Returns:
205 * %0 on success and an error code on failure.
206 * Context:
207 * non-atomic
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 */
Ursula Braun022b6602008-04-24 10:15:20 +0200209int ccwgroup_create_from_string(struct device *root, unsigned int creator_id,
210 struct ccw_driver *cdrv, int num_devices,
211 const char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 struct ccwgroup_device *gdev;
Ursula Braun022b6602008-04-24 10:15:20 +0200214 int rc, i;
215 char tmp_bus_id[BUS_ID_SIZE];
216 const char *curr_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Ursula Braun022b6602008-04-24 10:15:20 +0200218 gdev = kzalloc(sizeof(*gdev) + num_devices * sizeof(gdev->cdev[0]),
219 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (!gdev)
221 return -ENOMEM;
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 atomic_set(&gdev->onoff, 0);
Cornelia Huckd76123e2007-04-27 16:01:37 +0200224 mutex_init(&gdev->reg_mutex);
225 mutex_lock(&gdev->reg_mutex);
Peter Oberparleiter16f7f952008-08-21 19:46:36 +0200226 gdev->creator_id = creator_id;
227 gdev->count = num_devices;
228 gdev->dev.bus = &ccwgroup_bus_type;
229 gdev->dev.parent = root;
230 gdev->dev.release = ccwgroup_release;
231 device_initialize(&gdev->dev);
232
Ursula Braun022b6602008-04-24 10:15:20 +0200233 curr_buf = buf;
234 for (i = 0; i < num_devices && curr_buf; i++) {
235 rc = __get_next_bus_id(&curr_buf, tmp_bus_id);
236 if (rc != 0)
237 goto error;
238 if (!__is_valid_bus_id(tmp_bus_id)) {
239 rc = -EINVAL;
240 goto error;
241 }
242 gdev->cdev[i] = get_ccwdev_by_busid(cdrv, tmp_bus_id);
243 /*
244 * All devices have to be of the same type in
245 * order to be grouped.
246 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (!gdev->cdev[i]
248 || gdev->cdev[i]->id.driver_info !=
249 gdev->cdev[0]->id.driver_info) {
250 rc = -EINVAL;
Cornelia Huckd76123e2007-04-27 16:01:37 +0200251 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253 /* Don't allow a device to belong to more than one group. */
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100254 if (dev_get_drvdata(&gdev->cdev[i]->dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 rc = -EINVAL;
Cornelia Huckd76123e2007-04-27 16:01:37 +0200256 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100258 dev_set_drvdata(&gdev->cdev[i]->dev, gdev);
Cornelia Huck17088222006-07-27 14:00:33 +0200259 }
Ursula Braun022b6602008-04-24 10:15:20 +0200260 /* Check for sufficient number of bus ids. */
261 if (i < num_devices && !curr_buf) {
262 rc = -EINVAL;
263 goto error;
264 }
265 /* Check for trailing stuff. */
266 if (i == num_devices && strlen(curr_buf) > 0) {
267 rc = -EINVAL;
268 goto error;
269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 snprintf (gdev->dev.bus_id, BUS_ID_SIZE, "%s",
272 gdev->cdev[0]->dev.bus_id);
273
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
Cornelia Huckd76123e2007-04-27 16:01:37 +0200300 mutex_unlock(&gdev->reg_mutex);
301 put_device(&gdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return rc;
303}
Ursula Braun022b6602008-04-24 10:15:20 +0200304EXPORT_SYMBOL(ccwgroup_create_from_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306static int __init
307init_ccwgroup (void)
308{
309 return bus_register (&ccwgroup_bus_type);
310}
311
312static void __exit
313cleanup_ccwgroup (void)
314{
315 bus_unregister (&ccwgroup_bus_type);
316}
317
318module_init(init_ccwgroup);
319module_exit(cleanup_ccwgroup);
320
321/************************** driver stuff ******************************/
322
323static int
324ccwgroup_set_online(struct ccwgroup_device *gdev)
325{
326 struct ccwgroup_driver *gdrv;
327 int ret;
328
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800329 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return -EAGAIN;
331 if (gdev->state == CCWGROUP_ONLINE) {
332 ret = 0;
333 goto out;
334 }
335 if (!gdev->dev.driver) {
336 ret = -EINVAL;
337 goto out;
338 }
339 gdrv = to_ccwgroupdrv (gdev->dev.driver);
Cornelia Hucka0016402005-11-07 00:59:05 -0800340 if ((ret = gdrv->set_online ? gdrv->set_online(gdev) : 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 goto out;
342
343 gdev->state = CCWGROUP_ONLINE;
344 out:
345 atomic_set(&gdev->onoff, 0);
346 return ret;
347}
348
349static int
350ccwgroup_set_offline(struct ccwgroup_device *gdev)
351{
352 struct ccwgroup_driver *gdrv;
353 int ret;
354
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800355 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 return -EAGAIN;
357 if (gdev->state == CCWGROUP_OFFLINE) {
358 ret = 0;
359 goto out;
360 }
361 if (!gdev->dev.driver) {
362 ret = -EINVAL;
363 goto out;
364 }
365 gdrv = to_ccwgroupdrv (gdev->dev.driver);
Cornelia Hucka0016402005-11-07 00:59:05 -0800366 if ((ret = gdrv->set_offline ? gdrv->set_offline(gdev) : 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 goto out;
368
369 gdev->state = CCWGROUP_OFFLINE;
370 out:
371 atomic_set(&gdev->onoff, 0);
372 return ret;
373}
374
375static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400376ccwgroup_online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
378 struct ccwgroup_device *gdev;
379 struct ccwgroup_driver *gdrv;
Cornelia Huck2f972202008-04-30 13:38:33 +0200380 unsigned long value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 int ret;
382
383 gdev = to_ccwgroupdev(dev);
384 if (!dev->driver)
385 return count;
386
387 gdrv = to_ccwgroupdrv (gdev->dev.driver);
388 if (!try_module_get(gdrv->owner))
389 return -EINVAL;
390
Cornelia Huck2f972202008-04-30 13:38:33 +0200391 ret = strict_strtoul(buf, 0, &value);
392 if (ret)
393 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 ret = count;
395 if (value == 1)
396 ccwgroup_set_online(gdev);
397 else if (value == 0)
398 ccwgroup_set_offline(gdev);
399 else
400 ret = -EINVAL;
Cornelia Huck2f972202008-04-30 13:38:33 +0200401out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 module_put(gdrv->owner);
403 return ret;
404}
405
406static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400407ccwgroup_online_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
409 int online;
410
411 online = (to_ccwgroupdev(dev)->state == CCWGROUP_ONLINE);
412
413 return sprintf(buf, online ? "1\n" : "0\n");
414}
415
416static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store);
417
418static int
419ccwgroup_probe (struct device *dev)
420{
421 struct ccwgroup_device *gdev;
422 struct ccwgroup_driver *gdrv;
423
424 int ret;
425
426 gdev = to_ccwgroupdev(dev);
427 gdrv = to_ccwgroupdrv(dev->driver);
428
429 if ((ret = device_create_file(dev, &dev_attr_online)))
430 return ret;
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 ret = gdrv->probe ? gdrv->probe(gdev) : -ENODEV;
433 if (ret)
434 device_remove_file(dev, &dev_attr_online);
435
436 return ret;
437}
438
439static int
440ccwgroup_remove (struct device *dev)
441{
442 struct ccwgroup_device *gdev;
443 struct ccwgroup_driver *gdrv;
444
445 gdev = to_ccwgroupdev(dev);
446 gdrv = to_ccwgroupdrv(dev->driver);
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 device_remove_file(dev, &dev_attr_online);
449
450 if (gdrv && gdrv->remove)
451 gdrv->remove(gdev);
452 return 0;
453}
454
Cornelia Huck01bc8ad2008-02-05 16:50:36 +0100455static void ccwgroup_shutdown(struct device *dev)
456{
457 struct ccwgroup_device *gdev;
458 struct ccwgroup_driver *gdrv;
459
460 gdev = to_ccwgroupdev(dev);
461 gdrv = to_ccwgroupdrv(dev->driver);
462 if (gdrv && gdrv->shutdown)
463 gdrv->shutdown(gdev);
464}
465
Russell Kingf9ccf452006-01-05 14:42:09 +0000466static struct bus_type ccwgroup_bus_type = {
467 .name = "ccwgroup",
468 .match = ccwgroup_bus_match,
469 .uevent = ccwgroup_uevent,
470 .probe = ccwgroup_probe,
471 .remove = ccwgroup_remove,
Cornelia Huck01bc8ad2008-02-05 16:50:36 +0100472 .shutdown = ccwgroup_shutdown,
Russell Kingf9ccf452006-01-05 14:42:09 +0000473};
474
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200475/**
476 * ccwgroup_driver_register() - register a ccw group driver
477 * @cdriver: driver to be registered
478 *
479 * This function is mainly a wrapper around driver_register().
480 */
481int ccwgroup_driver_register(struct ccwgroup_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
483 /* register our new driver with the core */
Heiko Carstens292888c2006-08-30 14:33:35 +0200484 cdriver->driver.bus = &ccwgroup_bus_type;
485 cdriver->driver.name = cdriver->name;
Cornelia Huck4beee642008-01-26 14:10:47 +0100486 cdriver->driver.owner = cdriver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 return driver_register(&cdriver->driver);
489}
490
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700491static int
Cornelia Huck887ab592006-06-29 14:56:52 +0200492__ccwgroup_match_all(struct device *dev, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Cornelia Huck887ab592006-06-29 14:56:52 +0200494 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200497/**
498 * ccwgroup_driver_unregister() - deregister a ccw group driver
499 * @cdriver: driver to be deregistered
500 *
501 * This function is mainly a wrapper around driver_unregister().
502 */
503void ccwgroup_driver_unregister(struct ccwgroup_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Cornelia Huck887ab592006-06-29 14:56:52 +0200505 struct device *dev;
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 /* We don't want ccwgroup devices to live longer than their driver. */
508 get_driver(&cdriver->driver);
Cornelia Huck887ab592006-06-29 14:56:52 +0200509 while ((dev = driver_find_device(&cdriver->driver, NULL, NULL,
510 __ccwgroup_match_all))) {
Cornelia Huckd76123e2007-04-27 16:01:37 +0200511 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
512
513 mutex_lock(&gdev->reg_mutex);
514 __ccwgroup_remove_symlinks(gdev);
Cornelia Huck887ab592006-06-29 14:56:52 +0200515 device_unregister(dev);
Cornelia Huckd76123e2007-04-27 16:01:37 +0200516 mutex_unlock(&gdev->reg_mutex);
Cornelia Huck887ab592006-06-29 14:56:52 +0200517 put_device(dev);
518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 put_driver(&cdriver->driver);
520 driver_unregister(&cdriver->driver);
521}
522
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200523/**
524 * ccwgroup_probe_ccwdev() - probe function for slave devices
525 * @cdev: ccw device to be probed
526 *
527 * This is a dummy probe function for ccw devices that are slave devices in
528 * a ccw group device.
529 * Returns:
530 * always %0
531 */
532int ccwgroup_probe_ccwdev(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 return 0;
535}
536
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100537static struct ccwgroup_device *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538__ccwgroup_get_gdev_by_cdev(struct ccw_device *cdev)
539{
540 struct ccwgroup_device *gdev;
541
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100542 gdev = dev_get_drvdata(&cdev->dev);
543 if (gdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (get_device(&gdev->dev)) {
Cornelia Huckd76123e2007-04-27 16:01:37 +0200545 mutex_lock(&gdev->reg_mutex);
Daniel Ritzd305ef52005-09-22 00:47:24 -0700546 if (device_is_registered(&gdev->dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return gdev;
Cornelia Huckd76123e2007-04-27 16:01:37 +0200548 mutex_unlock(&gdev->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 put_device(&gdev->dev);
550 }
551 return NULL;
552 }
553 return NULL;
554}
555
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200556/**
557 * ccwgroup_remove_ccwdev() - remove function for slave devices
558 * @cdev: ccw device to be removed
559 *
560 * This is a remove function for ccw devices that are slave devices in a ccw
561 * group device. It sets the ccw device offline and also deregisters the
562 * embedding ccw group device.
563 */
564void ccwgroup_remove_ccwdev(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
566 struct ccwgroup_device *gdev;
567
568 /* Ignore offlining errors, device is gone anyway. */
569 ccw_device_set_offline(cdev);
570 /* If one of its devices is gone, the whole group is done for. */
571 gdev = __ccwgroup_get_gdev_by_cdev(cdev);
572 if (gdev) {
573 __ccwgroup_remove_symlinks(gdev);
574 device_unregister(&gdev->dev);
Cornelia Huckd76123e2007-04-27 16:01:37 +0200575 mutex_unlock(&gdev->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 put_device(&gdev->dev);
577 }
578}
579
580MODULE_LICENSE("GPL");
581EXPORT_SYMBOL(ccwgroup_driver_register);
582EXPORT_SYMBOL(ccwgroup_driver_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583EXPORT_SYMBOL(ccwgroup_probe_ccwdev);
584EXPORT_SYMBOL(ccwgroup_remove_ccwdev);