1
2
3
4
5
6
7
8
9#include <linux/module.h>
10#include <linux/errno.h>
11#include <linux/slab.h>
12#include <linux/list.h>
13#include <linux/device.h>
14#include <linux/init.h>
15#include <linux/ctype.h>
16#include <linux/dcache.h>
17
18#include <asm/cio.h>
19#include <asm/ccwdev.h>
20#include <asm/ccwgroup.h>
21
22#include "device.h"
23
24#define CCW_BUS_ID_SIZE 10
25
26
27
28
29
30
31
32
33static struct bus_type ccwgroup_bus_type;
34
35static void __ccwgroup_remove_symlinks(struct ccwgroup_device *gdev)
36{
37 int i;
38 char str[8];
39
40 for (i = 0; i < gdev->count; i++) {
41 sprintf(str, "cdev%d", i);
42 sysfs_remove_link(&gdev->dev.kobj, str);
43 sysfs_remove_link(&gdev->cdev[i]->dev.kobj, "group_device");
44 }
45}
46
47
48
49
50
51static void __ccwgroup_remove_cdev_refs(struct ccwgroup_device *gdev)
52{
53 struct ccw_device *cdev;
54 int i;
55
56 for (i = 0; i < gdev->count; i++) {
57 cdev = gdev->cdev[i];
58 if (!cdev)
59 continue;
60 spin_lock_irq(cdev->ccwlock);
61 dev_set_drvdata(&cdev->dev, NULL);
62 spin_unlock_irq(cdev->ccwlock);
63 gdev->cdev[i] = NULL;
64 put_device(&cdev->dev);
65 }
66}
67
68
69
70
71
72
73
74
75
76int ccwgroup_set_online(struct ccwgroup_device *gdev)
77{
78 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
79 int ret = -EINVAL;
80
81 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
82 return -EAGAIN;
83 if (gdev->state == CCWGROUP_ONLINE)
84 goto out;
85 if (gdrv->set_online)
86 ret = gdrv->set_online(gdev);
87 if (ret)
88 goto out;
89
90 gdev->state = CCWGROUP_ONLINE;
91out:
92 atomic_set(&gdev->onoff, 0);
93 return ret;
94}
95EXPORT_SYMBOL(ccwgroup_set_online);
96
97
98
99
100
101
102
103
104
105int ccwgroup_set_offline(struct ccwgroup_device *gdev)
106{
107 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
108 int ret = -EINVAL;
109
110 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
111 return -EAGAIN;
112 if (gdev->state == CCWGROUP_OFFLINE)
113 goto out;
114 if (gdrv->set_offline)
115 ret = gdrv->set_offline(gdev);
116 if (ret)
117 goto out;
118
119 gdev->state = CCWGROUP_OFFLINE;
120out:
121 atomic_set(&gdev->onoff, 0);
122 return ret;
123}
124EXPORT_SYMBOL(ccwgroup_set_offline);
125
126static ssize_t ccwgroup_online_store(struct device *dev,
127 struct device_attribute *attr,
128 const char *buf, size_t count)
129{
130 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
131 unsigned long value;
132 int ret;
133
134 device_lock(dev);
135 if (!dev->driver) {
136 ret = -EINVAL;
137 goto out;
138 }
139
140 ret = kstrtoul(buf, 0, &value);
141 if (ret)
142 goto out;
143
144 if (value == 1)
145 ret = ccwgroup_set_online(gdev);
146 else if (value == 0)
147 ret = ccwgroup_set_offline(gdev);
148 else
149 ret = -EINVAL;
150out:
151 device_unlock(dev);
152 return (ret == 0) ? count : ret;
153}
154
155static ssize_t ccwgroup_online_show(struct device *dev,
156 struct device_attribute *attr,
157 char *buf)
158{
159 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
160 int online;
161
162 online = (gdev->state == CCWGROUP_ONLINE) ? 1 : 0;
163
164 return scnprintf(buf, PAGE_SIZE, "%d\n", online);
165}
166
167
168
169
170
171static void ccwgroup_ungroup(struct ccwgroup_device *gdev)
172{
173 mutex_lock(&gdev->reg_mutex);
174 if (device_is_registered(&gdev->dev)) {
175 __ccwgroup_remove_symlinks(gdev);
176 device_unregister(&gdev->dev);
177 __ccwgroup_remove_cdev_refs(gdev);
178 }
179 mutex_unlock(&gdev->reg_mutex);
180}
181
182static ssize_t ccwgroup_ungroup_store(struct device *dev,
183 struct device_attribute *attr,
184 const char *buf, size_t count)
185{
186 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
187 int rc = 0;
188
189
190 if (atomic_cmpxchg(&gdev->onoff, 0, 1) != 0)
191 return -EAGAIN;
192 if (gdev->state != CCWGROUP_OFFLINE) {
193 rc = -EINVAL;
194 goto out;
195 }
196
197 if (device_remove_file_self(dev, attr))
198 ccwgroup_ungroup(gdev);
199 else
200 rc = -ENODEV;
201out:
202 if (rc) {
203
204 atomic_set(&gdev->onoff, 0);
205 return rc;
206 }
207 return count;
208}
209static DEVICE_ATTR(ungroup, 0200, NULL, ccwgroup_ungroup_store);
210static DEVICE_ATTR(online, 0644, ccwgroup_online_show, ccwgroup_online_store);
211
212static struct attribute *ccwgroup_attrs[] = {
213 &dev_attr_online.attr,
214 &dev_attr_ungroup.attr,
215 NULL,
216};
217static struct attribute_group ccwgroup_attr_group = {
218 .attrs = ccwgroup_attrs,
219};
220static const struct attribute_group *ccwgroup_attr_groups[] = {
221 &ccwgroup_attr_group,
222 NULL,
223};
224
225static void ccwgroup_ungroup_workfn(struct work_struct *work)
226{
227 struct ccwgroup_device *gdev =
228 container_of(work, struct ccwgroup_device, ungroup_work);
229
230 ccwgroup_ungroup(gdev);
231 put_device(&gdev->dev);
232}
233
234static void ccwgroup_release(struct device *dev)
235{
236 kfree(to_ccwgroupdev(dev));
237}
238
239static int __ccwgroup_create_symlinks(struct ccwgroup_device *gdev)
240{
241 char str[8];
242 int i, rc;
243
244 for (i = 0; i < gdev->count; i++) {
245 rc = sysfs_create_link(&gdev->cdev[i]->dev.kobj,
246 &gdev->dev.kobj, "group_device");
247 if (rc) {
248 for (--i; i >= 0; i--)
249 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
250 "group_device");
251 return rc;
252 }
253 }
254 for (i = 0; i < gdev->count; i++) {
255 sprintf(str, "cdev%d", i);
256 rc = sysfs_create_link(&gdev->dev.kobj,
257 &gdev->cdev[i]->dev.kobj, str);
258 if (rc) {
259 for (--i; i >= 0; i--) {
260 sprintf(str, "cdev%d", i);
261 sysfs_remove_link(&gdev->dev.kobj, str);
262 }
263 for (i = 0; i < gdev->count; i++)
264 sysfs_remove_link(&gdev->cdev[i]->dev.kobj,
265 "group_device");
266 return rc;
267 }
268 }
269 return 0;
270}
271
272static int __get_next_id(const char **buf, struct ccw_dev_id *id)
273{
274 unsigned int cssid, ssid, devno;
275 int ret = 0, len;
276 char *start, *end;
277
278 start = (char *)*buf;
279 end = strchr(start, ',');
280 if (!end) {
281
282 end = strchr(start, '\n');
283 if (end)
284 *end = '\0';
285 len = strlen(start) + 1;
286 } else {
287 len = end - start + 1;
288 end++;
289 }
290 if (len <= CCW_BUS_ID_SIZE) {
291 if (sscanf(start, "%2x.%1x.%04x", &cssid, &ssid, &devno) != 3)
292 ret = -EINVAL;
293 } else
294 ret = -EINVAL;
295
296 if (!ret) {
297 id->ssid = ssid;
298 id->devno = devno;
299 }
300 *buf = end;
301 return ret;
302}
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318int ccwgroup_create_dev(struct device *parent, struct ccwgroup_driver *gdrv,
319 int num_devices, const char *buf)
320{
321 struct ccwgroup_device *gdev;
322 struct ccw_dev_id dev_id;
323 int rc, i;
324
325 gdev = kzalloc(sizeof(*gdev) + num_devices * sizeof(gdev->cdev[0]),
326 GFP_KERNEL);
327 if (!gdev)
328 return -ENOMEM;
329
330 atomic_set(&gdev->onoff, 0);
331 mutex_init(&gdev->reg_mutex);
332 mutex_lock(&gdev->reg_mutex);
333 INIT_WORK(&gdev->ungroup_work, ccwgroup_ungroup_workfn);
334 gdev->count = num_devices;
335 gdev->dev.bus = &ccwgroup_bus_type;
336 gdev->dev.parent = parent;
337 gdev->dev.release = ccwgroup_release;
338 device_initialize(&gdev->dev);
339
340 for (i = 0; i < num_devices && buf; i++) {
341 rc = __get_next_id(&buf, &dev_id);
342 if (rc != 0)
343 goto error;
344 gdev->cdev[i] = get_ccwdev_by_dev_id(&dev_id);
345
346
347
348
349 if (!gdev->cdev[i] || !gdev->cdev[i]->drv ||
350 gdev->cdev[i]->drv != gdev->cdev[0]->drv ||
351 gdev->cdev[i]->id.driver_info !=
352 gdev->cdev[0]->id.driver_info) {
353 rc = -EINVAL;
354 goto error;
355 }
356
357 spin_lock_irq(gdev->cdev[i]->ccwlock);
358 if (dev_get_drvdata(&gdev->cdev[i]->dev)) {
359 spin_unlock_irq(gdev->cdev[i]->ccwlock);
360 rc = -EINVAL;
361 goto error;
362 }
363 dev_set_drvdata(&gdev->cdev[i]->dev, gdev);
364 spin_unlock_irq(gdev->cdev[i]->ccwlock);
365 }
366
367 if (i < num_devices) {
368 rc = -EINVAL;
369 goto error;
370 }
371
372 if (i == num_devices && strlen(buf) > 0) {
373 rc = -EINVAL;
374 goto error;
375 }
376
377 dev_set_name(&gdev->dev, "%s", dev_name(&gdev->cdev[0]->dev));
378 gdev->dev.groups = ccwgroup_attr_groups;
379
380 if (gdrv) {
381 gdev->dev.driver = &gdrv->driver;
382 rc = gdrv->setup ? gdrv->setup(gdev) : 0;
383 if (rc)
384 goto error;
385 }
386 rc = device_add(&gdev->dev);
387 if (rc)
388 goto error;
389 rc = __ccwgroup_create_symlinks(gdev);
390 if (rc) {
391 device_del(&gdev->dev);
392 goto error;
393 }
394 mutex_unlock(&gdev->reg_mutex);
395 return 0;
396error:
397 for (i = 0; i < num_devices; i++)
398 if (gdev->cdev[i]) {
399 spin_lock_irq(gdev->cdev[i]->ccwlock);
400 if (dev_get_drvdata(&gdev->cdev[i]->dev) == gdev)
401 dev_set_drvdata(&gdev->cdev[i]->dev, NULL);
402 spin_unlock_irq(gdev->cdev[i]->ccwlock);
403 put_device(&gdev->cdev[i]->dev);
404 gdev->cdev[i] = NULL;
405 }
406 mutex_unlock(&gdev->reg_mutex);
407 put_device(&gdev->dev);
408 return rc;
409}
410EXPORT_SYMBOL(ccwgroup_create_dev);
411
412static int ccwgroup_notifier(struct notifier_block *nb, unsigned long action,
413 void *data)
414{
415 struct ccwgroup_device *gdev = to_ccwgroupdev(data);
416
417 if (action == BUS_NOTIFY_UNBIND_DRIVER) {
418 get_device(&gdev->dev);
419 schedule_work(&gdev->ungroup_work);
420 }
421
422 return NOTIFY_OK;
423}
424
425static struct notifier_block ccwgroup_nb = {
426 .notifier_call = ccwgroup_notifier
427};
428
429static int __init init_ccwgroup(void)
430{
431 int ret;
432
433 ret = bus_register(&ccwgroup_bus_type);
434 if (ret)
435 return ret;
436
437 ret = bus_register_notifier(&ccwgroup_bus_type, &ccwgroup_nb);
438 if (ret)
439 bus_unregister(&ccwgroup_bus_type);
440
441 return ret;
442}
443
444static void __exit cleanup_ccwgroup(void)
445{
446 bus_unregister_notifier(&ccwgroup_bus_type, &ccwgroup_nb);
447 bus_unregister(&ccwgroup_bus_type);
448}
449
450module_init(init_ccwgroup);
451module_exit(cleanup_ccwgroup);
452
453
454
455static int ccwgroup_remove(struct device *dev)
456{
457 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
458 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver);
459
460 if (!dev->driver)
461 return 0;
462 if (gdrv->remove)
463 gdrv->remove(gdev);
464
465 return 0;
466}
467
468static void ccwgroup_shutdown(struct device *dev)
469{
470 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
471 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver);
472
473 if (!dev->driver)
474 return;
475 if (gdrv->shutdown)
476 gdrv->shutdown(gdev);
477}
478
479static int ccwgroup_pm_prepare(struct device *dev)
480{
481 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
482 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
483
484
485 if (atomic_read(&gdev->onoff))
486 return -EAGAIN;
487
488 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
489 return 0;
490
491 return gdrv->prepare ? gdrv->prepare(gdev) : 0;
492}
493
494static void ccwgroup_pm_complete(struct device *dev)
495{
496 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
497 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(dev->driver);
498
499 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
500 return;
501
502 if (gdrv->complete)
503 gdrv->complete(gdev);
504}
505
506static int ccwgroup_pm_freeze(struct device *dev)
507{
508 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
509 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
510
511 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
512 return 0;
513
514 return gdrv->freeze ? gdrv->freeze(gdev) : 0;
515}
516
517static int ccwgroup_pm_thaw(struct device *dev)
518{
519 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
520 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
521
522 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
523 return 0;
524
525 return gdrv->thaw ? gdrv->thaw(gdev) : 0;
526}
527
528static int ccwgroup_pm_restore(struct device *dev)
529{
530 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
531 struct ccwgroup_driver *gdrv = to_ccwgroupdrv(gdev->dev.driver);
532
533 if (!gdev->dev.driver || gdev->state != CCWGROUP_ONLINE)
534 return 0;
535
536 return gdrv->restore ? gdrv->restore(gdev) : 0;
537}
538
539static const struct dev_pm_ops ccwgroup_pm_ops = {
540 .prepare = ccwgroup_pm_prepare,
541 .complete = ccwgroup_pm_complete,
542 .freeze = ccwgroup_pm_freeze,
543 .thaw = ccwgroup_pm_thaw,
544 .restore = ccwgroup_pm_restore,
545};
546
547static struct bus_type ccwgroup_bus_type = {
548 .name = "ccwgroup",
549 .remove = ccwgroup_remove,
550 .shutdown = ccwgroup_shutdown,
551 .pm = &ccwgroup_pm_ops,
552};
553
554
555
556
557
558
559
560int ccwgroup_driver_register(struct ccwgroup_driver *cdriver)
561{
562
563 cdriver->driver.bus = &ccwgroup_bus_type;
564
565 return driver_register(&cdriver->driver);
566}
567EXPORT_SYMBOL(ccwgroup_driver_register);
568
569static int __ccwgroup_match_all(struct device *dev, void *data)
570{
571 return 1;
572}
573
574
575
576
577
578
579
580void ccwgroup_driver_unregister(struct ccwgroup_driver *cdriver)
581{
582 struct device *dev;
583
584
585 while ((dev = driver_find_device(&cdriver->driver, NULL, NULL,
586 __ccwgroup_match_all))) {
587 struct ccwgroup_device *gdev = to_ccwgroupdev(dev);
588
589 ccwgroup_ungroup(gdev);
590 put_device(dev);
591 }
592 driver_unregister(&cdriver->driver);
593}
594EXPORT_SYMBOL(ccwgroup_driver_unregister);
595
596
597
598
599
600
601
602
603
604
605int ccwgroup_probe_ccwdev(struct ccw_device *cdev)
606{
607 return 0;
608}
609EXPORT_SYMBOL(ccwgroup_probe_ccwdev);
610
611
612
613
614
615
616
617
618
619void ccwgroup_remove_ccwdev(struct ccw_device *cdev)
620{
621 struct ccwgroup_device *gdev;
622
623
624 ccw_device_set_offline(cdev);
625
626 spin_lock_irq(cdev->ccwlock);
627 gdev = dev_get_drvdata(&cdev->dev);
628 if (!gdev) {
629 spin_unlock_irq(cdev->ccwlock);
630 return;
631 }
632
633 get_device(&gdev->dev);
634 spin_unlock_irq(cdev->ccwlock);
635
636 ccwgroup_ungroup(gdev);
637
638 put_device(&gdev->dev);
639}
640EXPORT_SYMBOL(ccwgroup_remove_ccwdev);
641MODULE_LICENSE("GPL");
642