blob: e97be9960098aecb1ae5a3310c3185c9e3ac9f4f [file] [log] [blame]
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +02001/*
2 * Industry-pack bus support functions.
3 *
4 * (C) 2011 Samuel Iglesias Gonsalvez <siglesia@cern.ch>, CERN
5 * (C) 2012 Samuel Iglesias Gonsalvez <siglesias@igalia.com>, Igalia
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
Samuel Iglesias Gonsalvez416289b2012-05-11 10:17:13 +02009 * Software Foundation; version 2 of the License.
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020010 */
11
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14#include <linux/module.h>
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +020015#include <linux/slab.h>
Samuel Iglesias Gonsalvez3b86bb22012-05-25 10:03:01 +020016#include <linux/idr.h>
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020017#include "ipack.h"
18
19#define to_ipack_dev(device) container_of(device, struct ipack_device, dev)
20#define to_ipack_driver(drv) container_of(drv, struct ipack_driver, driver)
21
Samuel Iglesias Gonsalvez3b86bb22012-05-25 10:03:01 +020022static DEFINE_IDA(ipack_ida);
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020023
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +020024static void ipack_device_release(struct device *dev)
25{
26 struct ipack_device *device = to_ipack_dev(dev);
27 kfree(device);
28}
29
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020030static int ipack_bus_match(struct device *device, struct device_driver *driver)
31{
32 int ret;
33 struct ipack_device *dev = to_ipack_dev(device);
34 struct ipack_driver *drv = to_ipack_driver(driver);
35
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +020036 if ((!drv->ops) || (!drv->ops->match))
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020037 return -EINVAL;
38
39 ret = drv->ops->match(dev);
40 if (ret)
41 dev->driver = drv;
42
Samuel Iglesias Gonsalvezbe98cc12012-05-23 15:54:41 +020043 return ret;
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020044}
45
46static int ipack_bus_probe(struct device *device)
47{
48 struct ipack_device *dev = to_ipack_dev(device);
49
50 if (!dev->driver->ops->probe)
51 return -EINVAL;
52
53 return dev->driver->ops->probe(dev);
54}
55
56static int ipack_bus_remove(struct device *device)
57{
58 struct ipack_device *dev = to_ipack_dev(device);
59
60 if (!dev->driver->ops->remove)
61 return -EINVAL;
62
63 dev->driver->ops->remove(dev);
64 return 0;
65}
66
67static struct bus_type ipack_bus_type = {
68 .name = "ipack",
69 .probe = ipack_bus_probe,
70 .match = ipack_bus_match,
71 .remove = ipack_bus_remove,
72};
73
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +020074struct ipack_bus_device *ipack_bus_register(struct device *parent, int slots,
75 struct ipack_bus_ops *ops)
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020076{
77 int bus_nr;
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +020078 struct ipack_bus_device *bus;
79
80 bus = kzalloc(sizeof(struct ipack_bus_device), GFP_KERNEL);
81 if (!bus)
82 return NULL;
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020083
Samuel Iglesias Gonsalvez3b86bb22012-05-25 10:03:01 +020084 bus_nr = ida_simple_get(&ipack_ida, 0, 0, GFP_KERNEL);
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +020085 if (bus_nr < 0) {
86 kfree(bus);
87 return NULL;
88 }
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020089
90 bus->bus_nr = bus_nr;
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +020091 bus->parent = parent;
92 bus->slots = slots;
93 bus->ops = ops;
94 return bus;
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +020095}
96EXPORT_SYMBOL_GPL(ipack_bus_register);
97
98int ipack_bus_unregister(struct ipack_bus_device *bus)
99{
Samuel Iglesias Gonsalvez3b86bb22012-05-25 10:03:01 +0200100 ida_simple_remove(&ipack_ida, bus->bus_nr);
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200101 kfree(bus);
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200102 return 0;
103}
104EXPORT_SYMBOL_GPL(ipack_bus_unregister);
105
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200106int ipack_driver_register(struct ipack_driver *edrv, struct module *owner,
107 char *name)
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200108{
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200109 edrv->driver.owner = owner;
110 edrv->driver.name = name;
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200111 edrv->driver.bus = &ipack_bus_type;
112 return driver_register(&edrv->driver);
113}
114EXPORT_SYMBOL_GPL(ipack_driver_register);
115
116void ipack_driver_unregister(struct ipack_driver *edrv)
117{
118 driver_unregister(&edrv->driver);
119}
120EXPORT_SYMBOL_GPL(ipack_driver_unregister);
121
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200122struct ipack_device *ipack_device_register(struct ipack_bus_device *bus,
123 int slot, int irqv)
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200124{
125 int ret;
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200126 struct ipack_device *dev;
127
128 dev = kzalloc(sizeof(struct ipack_device), GFP_KERNEL);
129 if (!dev)
130 return NULL;
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200131
132 dev->dev.bus = &ipack_bus_type;
133 dev->dev.release = ipack_device_release;
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200134 dev->dev.parent = bus->parent;
135 dev->slot = slot;
136 dev->bus_nr = bus->bus_nr;
137 dev->irq = irqv;
138 dev->bus = bus;
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200139 dev_set_name(&dev->dev,
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200140 "ipack-dev.%u.%u", dev->bus_nr, dev->slot);
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200141
142 ret = device_register(&dev->dev);
143 if (ret < 0) {
144 pr_err("error registering the device.\n");
145 dev->driver->ops->remove(dev);
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200146 kfree(dev);
147 return NULL;
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200148 }
149
Samuel Iglesias Gonsalvezec440332012-05-18 11:10:05 +0200150 return dev;
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200151}
152EXPORT_SYMBOL_GPL(ipack_device_register);
153
154void ipack_device_unregister(struct ipack_device *dev)
155{
156 device_unregister(&dev->dev);
157}
158EXPORT_SYMBOL_GPL(ipack_device_unregister);
159
160static int __init ipack_init(void)
161{
Samuel Iglesias Gonsalvez3b86bb22012-05-25 10:03:01 +0200162 ida_init(&ipack_ida);
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200163 return bus_register(&ipack_bus_type);
164}
165
166static void __exit ipack_exit(void)
167{
168 bus_unregister(&ipack_bus_type);
Samuel Iglesias Gonsalvez3b86bb22012-05-25 10:03:01 +0200169 ida_destroy(&ipack_ida);
Samuel Iglesias Gonsalvezd3465872012-05-09 15:27:19 +0200170}
171
172module_init(ipack_init);
173module_exit(ipack_exit);
174
175MODULE_AUTHOR("Samuel Iglesias Gonsalvez <siglesias@igalia.com>");
176MODULE_LICENSE("GPL");
177MODULE_DESCRIPTION("Industry-pack bus core");