2 * Industry-pack bus support functions.
4 * (C) 2011 Samuel Iglesias Gonsalvez <siglesia@cern.ch>, CERN
5 * (C) 2012 Samuel Iglesias Gonsalvez <siglesias@igalia.com>, Igalia
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
9 * Software Foundation; version 2 of the License.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/slab.h>
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)
22 /* used when allocating bus numbers */
23 #define IPACK_MAXBUS 64
25 static DEFINE_MUTEX(ipack_mutex
);
28 unsigned long busmap
[IPACK_MAXBUS
/ (8*sizeof(unsigned long))];
30 static struct ipack_busmap busmap
;
32 static void ipack_device_release(struct device
*dev
)
34 struct ipack_device
*device
= to_ipack_dev(dev
);
38 static int ipack_bus_match(struct device
*device
, struct device_driver
*driver
)
41 struct ipack_device
*dev
= to_ipack_dev(device
);
42 struct ipack_driver
*drv
= to_ipack_driver(driver
);
44 if ((!drv
->ops
) || (!drv
->ops
->match
))
47 ret
= drv
->ops
->match(dev
);
54 static int ipack_bus_probe(struct device
*device
)
56 struct ipack_device
*dev
= to_ipack_dev(device
);
58 if (!dev
->driver
->ops
->probe
)
61 return dev
->driver
->ops
->probe(dev
);
64 static int ipack_bus_remove(struct device
*device
)
66 struct ipack_device
*dev
= to_ipack_dev(device
);
68 if (!dev
->driver
->ops
->remove
)
71 dev
->driver
->ops
->remove(dev
);
75 static struct bus_type ipack_bus_type
= {
77 .probe
= ipack_bus_probe
,
78 .match
= ipack_bus_match
,
79 .remove
= ipack_bus_remove
,
82 static int ipack_assign_bus_number(void)
86 mutex_lock(&ipack_mutex
);
87 busnum
= find_next_zero_bit(busmap
.busmap
, IPACK_MAXBUS
, 1);
89 if (busnum
>= IPACK_MAXBUS
) {
90 pr_err("too many buses\n");
92 goto error_find_busnum
;
95 set_bit(busnum
, busmap
.busmap
);
98 mutex_unlock(&ipack_mutex
);
102 struct ipack_bus_device
*ipack_bus_register(struct device
*parent
, int slots
,
103 struct ipack_bus_ops
*ops
)
106 struct ipack_bus_device
*bus
;
108 bus
= kzalloc(sizeof(struct ipack_bus_device
), GFP_KERNEL
);
112 bus_nr
= ipack_assign_bus_number();
118 bus
->bus_nr
= bus_nr
;
119 bus
->parent
= parent
;
124 EXPORT_SYMBOL_GPL(ipack_bus_register
);
126 int ipack_bus_unregister(struct ipack_bus_device
*bus
)
128 mutex_lock(&ipack_mutex
);
129 clear_bit(bus
->bus_nr
, busmap
.busmap
);
130 mutex_unlock(&ipack_mutex
);
134 EXPORT_SYMBOL_GPL(ipack_bus_unregister
);
136 int ipack_driver_register(struct ipack_driver
*edrv
, struct module
*owner
,
139 edrv
->driver
.owner
= owner
;
140 edrv
->driver
.name
= name
;
141 edrv
->driver
.bus
= &ipack_bus_type
;
142 return driver_register(&edrv
->driver
);
144 EXPORT_SYMBOL_GPL(ipack_driver_register
);
146 void ipack_driver_unregister(struct ipack_driver
*edrv
)
148 driver_unregister(&edrv
->driver
);
150 EXPORT_SYMBOL_GPL(ipack_driver_unregister
);
152 struct ipack_device
*ipack_device_register(struct ipack_bus_device
*bus
,
156 struct ipack_device
*dev
;
158 dev
= kzalloc(sizeof(struct ipack_device
), GFP_KERNEL
);
162 dev
->dev
.bus
= &ipack_bus_type
;
163 dev
->dev
.release
= ipack_device_release
;
164 dev
->dev
.parent
= bus
->parent
;
166 dev
->bus_nr
= bus
->bus_nr
;
169 dev_set_name(&dev
->dev
,
170 "ipack-dev.%u.%u", dev
->bus_nr
, dev
->slot
);
172 ret
= device_register(&dev
->dev
);
174 pr_err("error registering the device.\n");
175 dev
->driver
->ops
->remove(dev
);
182 EXPORT_SYMBOL_GPL(ipack_device_register
);
184 void ipack_device_unregister(struct ipack_device
*dev
)
186 device_unregister(&dev
->dev
);
188 EXPORT_SYMBOL_GPL(ipack_device_unregister
);
190 static int __init
ipack_init(void)
192 return bus_register(&ipack_bus_type
);
195 static void __exit
ipack_exit(void)
197 bus_unregister(&ipack_bus_type
);
200 module_init(ipack_init
);
201 module_exit(ipack_exit
);
203 MODULE_AUTHOR("Samuel Iglesias Gonsalvez <siglesias@igalia.com>");
204 MODULE_LICENSE("GPL");
205 MODULE_DESCRIPTION("Industry-pack bus core");