powerpc/ibmebus: Fix device reference leaks in sysfs interface
[pandora-kernel.git] / arch / powerpc / kernel / ibmebus.c
1 /*
2  * IBM PowerPC IBM eBus Infrastructure Support.
3  *
4  * Copyright (c) 2005 IBM Corporation
5  *  Joachim Fenkes <fenkes@de.ibm.com>
6  *  Heiko J Schick <schickhj@de.ibm.com>
7  *
8  * All rights reserved.
9  *
10  * This source code is distributed under a dual license of GPL v2.0 and OpenIB
11  * BSD.
12  *
13  * OpenIB BSD License
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions are met:
17  *
18  * Redistributions of source code must retain the above copyright notice, this
19  * list of conditions and the following disclaimer.
20  *
21  * Redistributions in binary form must reproduce the above copyright notice,
22  * this list of conditions and the following disclaimer in the documentation
23  * and/or other materials
24  * provided with the distribution.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
33  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38
39 #include <linux/init.h>
40 #include <linux/export.h>
41 #include <linux/console.h>
42 #include <linux/kobject.h>
43 #include <linux/dma-mapping.h>
44 #include <linux/interrupt.h>
45 #include <linux/of.h>
46 #include <linux/slab.h>
47 #include <linux/stat.h>
48 #include <linux/of_platform.h>
49 #include <asm/ibmebus.h>
50 #include <asm/abs_addr.h>
51
52 static struct device ibmebus_bus_device = { /* fake "parent" device */
53         .init_name = "ibmebus",
54 };
55
56 struct bus_type ibmebus_bus_type;
57
58 /* These devices will automatically be added to the bus during init */
59 static struct of_device_id __initdata ibmebus_matches[] = {
60         { .compatible = "IBM,lhca" },
61         { .compatible = "IBM,lhea" },
62         {},
63 };
64
65 static void *ibmebus_alloc_coherent(struct device *dev,
66                                     size_t size,
67                                     dma_addr_t *dma_handle,
68                                     gfp_t flag)
69 {
70         void *mem;
71
72         mem = kmalloc(size, flag);
73         *dma_handle = (dma_addr_t)mem;
74
75         return mem;
76 }
77
78 static void ibmebus_free_coherent(struct device *dev,
79                                   size_t size, void *vaddr,
80                                   dma_addr_t dma_handle)
81 {
82         kfree(vaddr);
83 }
84
85 static dma_addr_t ibmebus_map_page(struct device *dev,
86                                    struct page *page,
87                                    unsigned long offset,
88                                    size_t size,
89                                    enum dma_data_direction direction,
90                                    struct dma_attrs *attrs)
91 {
92         return (dma_addr_t)(page_address(page) + offset);
93 }
94
95 static void ibmebus_unmap_page(struct device *dev,
96                                dma_addr_t dma_addr,
97                                size_t size,
98                                enum dma_data_direction direction,
99                                struct dma_attrs *attrs)
100 {
101         return;
102 }
103
104 static int ibmebus_map_sg(struct device *dev,
105                           struct scatterlist *sgl,
106                           int nents, enum dma_data_direction direction,
107                           struct dma_attrs *attrs)
108 {
109         struct scatterlist *sg;
110         int i;
111
112         for_each_sg(sgl, sg, nents, i) {
113                 sg->dma_address = (dma_addr_t) sg_virt(sg);
114                 sg->dma_length = sg->length;
115         }
116
117         return nents;
118 }
119
120 static void ibmebus_unmap_sg(struct device *dev,
121                              struct scatterlist *sg,
122                              int nents, enum dma_data_direction direction,
123                              struct dma_attrs *attrs)
124 {
125         return;
126 }
127
128 static int ibmebus_dma_supported(struct device *dev, u64 mask)
129 {
130         return mask == DMA_BIT_MASK(64);
131 }
132
133 static u64 ibmebus_dma_get_required_mask(struct device *dev)
134 {
135         return DMA_BIT_MASK(64);
136 }
137
138 static struct dma_map_ops ibmebus_dma_ops = {
139         .alloc_coherent     = ibmebus_alloc_coherent,
140         .free_coherent      = ibmebus_free_coherent,
141         .map_sg             = ibmebus_map_sg,
142         .unmap_sg           = ibmebus_unmap_sg,
143         .dma_supported      = ibmebus_dma_supported,
144         .get_required_mask  = ibmebus_dma_get_required_mask,
145         .map_page           = ibmebus_map_page,
146         .unmap_page         = ibmebus_unmap_page,
147 };
148
149 static int ibmebus_match_path(struct device *dev, void *data)
150 {
151         struct device_node *dn = to_platform_device(dev)->dev.of_node;
152         return (dn->full_name &&
153                 (strcasecmp((char *)data, dn->full_name) == 0));
154 }
155
156 static int ibmebus_match_node(struct device *dev, void *data)
157 {
158         return to_platform_device(dev)->dev.of_node == data;
159 }
160
161 static int ibmebus_create_device(struct device_node *dn)
162 {
163         struct platform_device *dev;
164         int ret;
165
166         dev = of_device_alloc(dn, NULL, &ibmebus_bus_device);
167         if (!dev)
168                 return -ENOMEM;
169
170         dev->dev.bus = &ibmebus_bus_type;
171         dev->dev.archdata.dma_ops = &ibmebus_dma_ops;
172
173         ret = of_device_add(dev);
174         if (ret)
175                 platform_device_put(dev);
176         return ret;
177 }
178
179 static int ibmebus_create_devices(const struct of_device_id *matches)
180 {
181         struct device_node *root, *child;
182         int ret = 0;
183
184         root = of_find_node_by_path("/");
185
186         for_each_child_of_node(root, child) {
187                 if (!of_match_node(matches, child))
188                         continue;
189
190                 if (bus_find_device(&ibmebus_bus_type, NULL, child,
191                                     ibmebus_match_node))
192                         continue;
193
194                 ret = ibmebus_create_device(child);
195                 if (ret) {
196                         printk(KERN_ERR "%s: failed to create device (%i)",
197                                __func__, ret);
198                         of_node_put(child);
199                         break;
200                 }
201         }
202
203         of_node_put(root);
204         return ret;
205 }
206
207 int ibmebus_register_driver(struct of_platform_driver *drv)
208 {
209         /* If the driver uses devices that ibmebus doesn't know, add them */
210         ibmebus_create_devices(drv->driver.of_match_table);
211
212         drv->driver.bus = &ibmebus_bus_type;
213         return driver_register(&drv->driver);
214 }
215 EXPORT_SYMBOL(ibmebus_register_driver);
216
217 void ibmebus_unregister_driver(struct of_platform_driver *drv)
218 {
219         driver_unregister(&drv->driver);
220 }
221 EXPORT_SYMBOL(ibmebus_unregister_driver);
222
223 int ibmebus_request_irq(u32 ist, irq_handler_t handler,
224                         unsigned long irq_flags, const char *devname,
225                         void *dev_id)
226 {
227         unsigned int irq = irq_create_mapping(NULL, ist);
228
229         if (irq == NO_IRQ)
230                 return -EINVAL;
231
232         return request_irq(irq, handler, irq_flags, devname, dev_id);
233 }
234 EXPORT_SYMBOL(ibmebus_request_irq);
235
236 void ibmebus_free_irq(u32 ist, void *dev_id)
237 {
238         unsigned int irq = irq_find_mapping(NULL, ist);
239
240         free_irq(irq, dev_id);
241         irq_dispose_mapping(irq);
242 }
243 EXPORT_SYMBOL(ibmebus_free_irq);
244
245 static char *ibmebus_chomp(const char *in, size_t count)
246 {
247         char *out = kmalloc(count + 1, GFP_KERNEL);
248
249         if (!out)
250                 return NULL;
251
252         memcpy(out, in, count);
253         out[count] = '\0';
254         if (out[count - 1] == '\n')
255                 out[count - 1] = '\0';
256
257         return out;
258 }
259
260 static ssize_t ibmebus_store_probe(struct bus_type *bus,
261                                    const char *buf, size_t count)
262 {
263         struct device_node *dn = NULL;
264         struct device *dev;
265         char *path;
266         ssize_t rc = 0;
267
268         path = ibmebus_chomp(buf, count);
269         if (!path)
270                 return -ENOMEM;
271
272         dev = bus_find_device(&ibmebus_bus_type, NULL, path,
273                               ibmebus_match_path);
274         if (dev) {
275                 put_device(dev);
276                 printk(KERN_WARNING "%s: %s has already been probed\n",
277                        __func__, path);
278                 rc = -EEXIST;
279                 goto out;
280         }
281
282         if ((dn = of_find_node_by_path(path))) {
283                 rc = ibmebus_create_device(dn);
284                 of_node_put(dn);
285         } else {
286                 printk(KERN_WARNING "%s: no such device node: %s\n",
287                        __func__, path);
288                 rc = -ENODEV;
289         }
290
291 out:
292         kfree(path);
293         if (rc)
294                 return rc;
295         return count;
296 }
297
298 static ssize_t ibmebus_store_remove(struct bus_type *bus,
299                                     const char *buf, size_t count)
300 {
301         struct device *dev;
302         char *path;
303
304         path = ibmebus_chomp(buf, count);
305         if (!path)
306                 return -ENOMEM;
307
308         if ((dev = bus_find_device(&ibmebus_bus_type, NULL, path,
309                                    ibmebus_match_path))) {
310                 of_device_unregister(to_platform_device(dev));
311                 put_device(dev);
312
313                 kfree(path);
314                 return count;
315         } else {
316                 printk(KERN_WARNING "%s: %s not on the bus\n",
317                        __func__, path);
318
319                 kfree(path);
320                 return -ENODEV;
321         }
322 }
323
324
325 static struct bus_attribute ibmebus_bus_attrs[] = {
326         __ATTR(probe, S_IWUSR, NULL, ibmebus_store_probe),
327         __ATTR(remove, S_IWUSR, NULL, ibmebus_store_remove),
328         __ATTR_NULL
329 };
330
331 static int ibmebus_bus_bus_match(struct device *dev, struct device_driver *drv)
332 {
333         const struct of_device_id *matches = drv->of_match_table;
334
335         if (!matches)
336                 return 0;
337
338         return of_match_device(matches, dev) != NULL;
339 }
340
341 static int ibmebus_bus_device_probe(struct device *dev)
342 {
343         int error = -ENODEV;
344         struct of_platform_driver *drv;
345         struct platform_device *of_dev;
346         const struct of_device_id *match;
347
348         drv = to_of_platform_driver(dev->driver);
349         of_dev = to_platform_device(dev);
350
351         if (!drv->probe)
352                 return error;
353
354         of_dev_get(of_dev);
355
356         match = of_match_device(drv->driver.of_match_table, dev);
357         if (match)
358                 error = drv->probe(of_dev, match);
359         if (error)
360                 of_dev_put(of_dev);
361
362         return error;
363 }
364
365 static int ibmebus_bus_device_remove(struct device *dev)
366 {
367         struct platform_device *of_dev = to_platform_device(dev);
368         struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
369
370         if (dev->driver && drv->remove)
371                 drv->remove(of_dev);
372         return 0;
373 }
374
375 static void ibmebus_bus_device_shutdown(struct device *dev)
376 {
377         struct platform_device *of_dev = to_platform_device(dev);
378         struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
379
380         if (dev->driver && drv->shutdown)
381                 drv->shutdown(of_dev);
382 }
383
384 /*
385  * ibmebus_bus_device_attrs
386  */
387 static ssize_t devspec_show(struct device *dev,
388                                 struct device_attribute *attr, char *buf)
389 {
390         struct platform_device *ofdev;
391
392         ofdev = to_platform_device(dev);
393         return sprintf(buf, "%s\n", ofdev->dev.of_node->full_name);
394 }
395
396 static ssize_t name_show(struct device *dev,
397                                 struct device_attribute *attr, char *buf)
398 {
399         struct platform_device *ofdev;
400
401         ofdev = to_platform_device(dev);
402         return sprintf(buf, "%s\n", ofdev->dev.of_node->name);
403 }
404
405 static ssize_t modalias_show(struct device *dev,
406                                 struct device_attribute *attr, char *buf)
407 {
408         ssize_t len = of_device_get_modalias(dev, buf, PAGE_SIZE - 2);
409         buf[len] = '\n';
410         buf[len+1] = 0;
411         return len+1;
412 }
413
414 struct device_attribute ibmebus_bus_device_attrs[] = {
415         __ATTR_RO(devspec),
416         __ATTR_RO(name),
417         __ATTR_RO(modalias),
418         __ATTR_NULL
419 };
420
421 #ifdef CONFIG_PM_SLEEP
422 static int ibmebus_bus_legacy_suspend(struct device *dev, pm_message_t mesg)
423 {
424         struct platform_device *of_dev = to_platform_device(dev);
425         struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
426         int ret = 0;
427
428         if (dev->driver && drv->suspend)
429                 ret = drv->suspend(of_dev, mesg);
430         return ret;
431 }
432
433 static int ibmebus_bus_legacy_resume(struct device *dev)
434 {
435         struct platform_device *of_dev = to_platform_device(dev);
436         struct of_platform_driver *drv = to_of_platform_driver(dev->driver);
437         int ret = 0;
438
439         if (dev->driver && drv->resume)
440                 ret = drv->resume(of_dev);
441         return ret;
442 }
443
444 static int ibmebus_bus_pm_prepare(struct device *dev)
445 {
446         struct device_driver *drv = dev->driver;
447         int ret = 0;
448
449         if (drv && drv->pm && drv->pm->prepare)
450                 ret = drv->pm->prepare(dev);
451
452         return ret;
453 }
454
455 static void ibmebus_bus_pm_complete(struct device *dev)
456 {
457         struct device_driver *drv = dev->driver;
458
459         if (drv && drv->pm && drv->pm->complete)
460                 drv->pm->complete(dev);
461 }
462
463 #ifdef CONFIG_SUSPEND
464
465 static int ibmebus_bus_pm_suspend(struct device *dev)
466 {
467         struct device_driver *drv = dev->driver;
468         int ret = 0;
469
470         if (!drv)
471                 return 0;
472
473         if (drv->pm) {
474                 if (drv->pm->suspend)
475                         ret = drv->pm->suspend(dev);
476         } else {
477                 ret = ibmebus_bus_legacy_suspend(dev, PMSG_SUSPEND);
478         }
479
480         return ret;
481 }
482
483 static int ibmebus_bus_pm_suspend_noirq(struct device *dev)
484 {
485         struct device_driver *drv = dev->driver;
486         int ret = 0;
487
488         if (!drv)
489                 return 0;
490
491         if (drv->pm) {
492                 if (drv->pm->suspend_noirq)
493                         ret = drv->pm->suspend_noirq(dev);
494         }
495
496         return ret;
497 }
498
499 static int ibmebus_bus_pm_resume(struct device *dev)
500 {
501         struct device_driver *drv = dev->driver;
502         int ret = 0;
503
504         if (!drv)
505                 return 0;
506
507         if (drv->pm) {
508                 if (drv->pm->resume)
509                         ret = drv->pm->resume(dev);
510         } else {
511                 ret = ibmebus_bus_legacy_resume(dev);
512         }
513
514         return ret;
515 }
516
517 static int ibmebus_bus_pm_resume_noirq(struct device *dev)
518 {
519         struct device_driver *drv = dev->driver;
520         int ret = 0;
521
522         if (!drv)
523                 return 0;
524
525         if (drv->pm) {
526                 if (drv->pm->resume_noirq)
527                         ret = drv->pm->resume_noirq(dev);
528         }
529
530         return ret;
531 }
532
533 #else /* !CONFIG_SUSPEND */
534
535 #define ibmebus_bus_pm_suspend          NULL
536 #define ibmebus_bus_pm_resume           NULL
537 #define ibmebus_bus_pm_suspend_noirq    NULL
538 #define ibmebus_bus_pm_resume_noirq     NULL
539
540 #endif /* !CONFIG_SUSPEND */
541
542 #ifdef CONFIG_HIBERNATE_CALLBACKS
543
544 static int ibmebus_bus_pm_freeze(struct device *dev)
545 {
546         struct device_driver *drv = dev->driver;
547         int ret = 0;
548
549         if (!drv)
550                 return 0;
551
552         if (drv->pm) {
553                 if (drv->pm->freeze)
554                         ret = drv->pm->freeze(dev);
555         } else {
556                 ret = ibmebus_bus_legacy_suspend(dev, PMSG_FREEZE);
557         }
558
559         return ret;
560 }
561
562 static int ibmebus_bus_pm_freeze_noirq(struct device *dev)
563 {
564         struct device_driver *drv = dev->driver;
565         int ret = 0;
566
567         if (!drv)
568                 return 0;
569
570         if (drv->pm) {
571                 if (drv->pm->freeze_noirq)
572                         ret = drv->pm->freeze_noirq(dev);
573         }
574
575         return ret;
576 }
577
578 static int ibmebus_bus_pm_thaw(struct device *dev)
579 {
580         struct device_driver *drv = dev->driver;
581         int ret = 0;
582
583         if (!drv)
584                 return 0;
585
586         if (drv->pm) {
587                 if (drv->pm->thaw)
588                         ret = drv->pm->thaw(dev);
589         } else {
590                 ret = ibmebus_bus_legacy_resume(dev);
591         }
592
593         return ret;
594 }
595
596 static int ibmebus_bus_pm_thaw_noirq(struct device *dev)
597 {
598         struct device_driver *drv = dev->driver;
599         int ret = 0;
600
601         if (!drv)
602                 return 0;
603
604         if (drv->pm) {
605                 if (drv->pm->thaw_noirq)
606                         ret = drv->pm->thaw_noirq(dev);
607         }
608
609         return ret;
610 }
611
612 static int ibmebus_bus_pm_poweroff(struct device *dev)
613 {
614         struct device_driver *drv = dev->driver;
615         int ret = 0;
616
617         if (!drv)
618                 return 0;
619
620         if (drv->pm) {
621                 if (drv->pm->poweroff)
622                         ret = drv->pm->poweroff(dev);
623         } else {
624                 ret = ibmebus_bus_legacy_suspend(dev, PMSG_HIBERNATE);
625         }
626
627         return ret;
628 }
629
630 static int ibmebus_bus_pm_poweroff_noirq(struct device *dev)
631 {
632         struct device_driver *drv = dev->driver;
633         int ret = 0;
634
635         if (!drv)
636                 return 0;
637
638         if (drv->pm) {
639                 if (drv->pm->poweroff_noirq)
640                         ret = drv->pm->poweroff_noirq(dev);
641         }
642
643         return ret;
644 }
645
646 static int ibmebus_bus_pm_restore(struct device *dev)
647 {
648         struct device_driver *drv = dev->driver;
649         int ret = 0;
650
651         if (!drv)
652                 return 0;
653
654         if (drv->pm) {
655                 if (drv->pm->restore)
656                         ret = drv->pm->restore(dev);
657         } else {
658                 ret = ibmebus_bus_legacy_resume(dev);
659         }
660
661         return ret;
662 }
663
664 static int ibmebus_bus_pm_restore_noirq(struct device *dev)
665 {
666         struct device_driver *drv = dev->driver;
667         int ret = 0;
668
669         if (!drv)
670                 return 0;
671
672         if (drv->pm) {
673                 if (drv->pm->restore_noirq)
674                         ret = drv->pm->restore_noirq(dev);
675         }
676
677         return ret;
678 }
679
680 #else /* !CONFIG_HIBERNATE_CALLBACKS */
681
682 #define ibmebus_bus_pm_freeze           NULL
683 #define ibmebus_bus_pm_thaw             NULL
684 #define ibmebus_bus_pm_poweroff         NULL
685 #define ibmebus_bus_pm_restore          NULL
686 #define ibmebus_bus_pm_freeze_noirq     NULL
687 #define ibmebus_bus_pm_thaw_noirq               NULL
688 #define ibmebus_bus_pm_poweroff_noirq   NULL
689 #define ibmebus_bus_pm_restore_noirq    NULL
690
691 #endif /* !CONFIG_HIBERNATE_CALLBACKS */
692
693 static struct dev_pm_ops ibmebus_bus_dev_pm_ops = {
694         .prepare = ibmebus_bus_pm_prepare,
695         .complete = ibmebus_bus_pm_complete,
696         .suspend = ibmebus_bus_pm_suspend,
697         .resume = ibmebus_bus_pm_resume,
698         .freeze = ibmebus_bus_pm_freeze,
699         .thaw = ibmebus_bus_pm_thaw,
700         .poweroff = ibmebus_bus_pm_poweroff,
701         .restore = ibmebus_bus_pm_restore,
702         .suspend_noirq = ibmebus_bus_pm_suspend_noirq,
703         .resume_noirq = ibmebus_bus_pm_resume_noirq,
704         .freeze_noirq = ibmebus_bus_pm_freeze_noirq,
705         .thaw_noirq = ibmebus_bus_pm_thaw_noirq,
706         .poweroff_noirq = ibmebus_bus_pm_poweroff_noirq,
707         .restore_noirq = ibmebus_bus_pm_restore_noirq,
708 };
709
710 #define IBMEBUS_BUS_PM_OPS_PTR  (&ibmebus_bus_dev_pm_ops)
711
712 #else /* !CONFIG_PM_SLEEP */
713
714 #define IBMEBUS_BUS_PM_OPS_PTR  NULL
715
716 #endif /* !CONFIG_PM_SLEEP */
717
718 struct bus_type ibmebus_bus_type = {
719         .name      = "ibmebus",
720         .uevent    = of_device_uevent,
721         .bus_attrs = ibmebus_bus_attrs,
722         .match     = ibmebus_bus_bus_match,
723         .probe     = ibmebus_bus_device_probe,
724         .remove    = ibmebus_bus_device_remove,
725         .shutdown  = ibmebus_bus_device_shutdown,
726         .dev_attrs = ibmebus_bus_device_attrs,
727         .pm        = IBMEBUS_BUS_PM_OPS_PTR,
728 };
729 EXPORT_SYMBOL(ibmebus_bus_type);
730
731 static int __init ibmebus_bus_init(void)
732 {
733         int err;
734
735         printk(KERN_INFO "IBM eBus Device Driver\n");
736
737         err = bus_register(&ibmebus_bus_type);
738         if (err) {
739                 printk(KERN_ERR "%s: failed to register IBM eBus.\n",
740                        __func__);
741                 return err;
742         }
743
744         err = device_register(&ibmebus_bus_device);
745         if (err) {
746                 printk(KERN_WARNING "%s: device_register returned %i\n",
747                        __func__, err);
748                 bus_unregister(&ibmebus_bus_type);
749
750                 return err;
751         }
752
753         err = ibmebus_create_devices(ibmebus_matches);
754         if (err) {
755                 device_unregister(&ibmebus_bus_device);
756                 bus_unregister(&ibmebus_bus_type);
757                 return err;
758         }
759
760         return 0;
761 }
762 postcore_initcall(ibmebus_bus_init);