]> Pileus Git - ~andy/linux/blob - drivers/staging/ipack/ipack.c
Merge tag 'scsi-misc' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[~andy/linux] / drivers / staging / ipack / ipack.c
1 /*
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
9  * Software Foundation; version 2 of the License.
10  */
11
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/slab.h>
17 #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
22 /* used when allocating bus numbers */
23 #define IPACK_MAXBUS              64
24
25 static DEFINE_MUTEX(ipack_mutex);
26
27 struct ipack_busmap {
28         unsigned long busmap[IPACK_MAXBUS / (8*sizeof(unsigned long))];
29 };
30 static struct ipack_busmap busmap;
31
32 static void ipack_device_release(struct device *dev)
33 {
34         struct ipack_device *device = to_ipack_dev(dev);
35         kfree(device);
36 }
37
38 static int ipack_bus_match(struct device *device, struct device_driver *driver)
39 {
40         int ret;
41         struct ipack_device *dev = to_ipack_dev(device);
42         struct ipack_driver *drv = to_ipack_driver(driver);
43
44         if ((!drv->ops) || (!drv->ops->match))
45                 return -EINVAL;
46
47         ret = drv->ops->match(dev);
48         if (ret)
49                 dev->driver = drv;
50
51         return 0;
52 }
53
54 static int ipack_bus_probe(struct device *device)
55 {
56         struct ipack_device *dev = to_ipack_dev(device);
57
58         if (!dev->driver->ops->probe)
59                 return -EINVAL;
60
61         return dev->driver->ops->probe(dev);
62 }
63
64 static int ipack_bus_remove(struct device *device)
65 {
66         struct ipack_device *dev = to_ipack_dev(device);
67
68         if (!dev->driver->ops->remove)
69                 return -EINVAL;
70
71         dev->driver->ops->remove(dev);
72         return 0;
73 }
74
75 static struct bus_type ipack_bus_type = {
76         .name  = "ipack",
77         .probe = ipack_bus_probe,
78         .match = ipack_bus_match,
79         .remove = ipack_bus_remove,
80 };
81
82 static int ipack_assign_bus_number(void)
83 {
84         int busnum;
85
86         mutex_lock(&ipack_mutex);
87         busnum = find_next_zero_bit(busmap.busmap, IPACK_MAXBUS, 1);
88
89         if (busnum >= IPACK_MAXBUS) {
90                 pr_err("too many buses\n");
91                 busnum = -1;
92                 goto error_find_busnum;
93         }
94
95         set_bit(busnum, busmap.busmap);
96
97 error_find_busnum:
98         mutex_unlock(&ipack_mutex);
99         return busnum;
100 }
101
102 struct ipack_bus_device *ipack_bus_register(struct device *parent, int slots,
103                                             struct ipack_bus_ops *ops)
104 {
105         int bus_nr;
106         struct ipack_bus_device *bus;
107
108         bus = kzalloc(sizeof(struct ipack_bus_device), GFP_KERNEL);
109         if (!bus)
110                 return NULL;
111
112         bus_nr = ipack_assign_bus_number();
113         if (bus_nr < 0) {
114                 kfree(bus);
115                 return NULL;
116         }
117
118         bus->bus_nr = bus_nr;
119         bus->parent = parent;
120         bus->slots = slots;
121         bus->ops = ops;
122         return bus;
123 }
124 EXPORT_SYMBOL_GPL(ipack_bus_register);
125
126 int ipack_bus_unregister(struct ipack_bus_device *bus)
127 {
128         mutex_lock(&ipack_mutex);
129         clear_bit(bus->bus_nr, busmap.busmap);
130         mutex_unlock(&ipack_mutex);
131         kfree(bus);
132         return 0;
133 }
134 EXPORT_SYMBOL_GPL(ipack_bus_unregister);
135
136 int ipack_driver_register(struct ipack_driver *edrv, struct module *owner,
137                           char *name)
138 {
139         edrv->driver.owner = owner;
140         edrv->driver.name = name;
141         edrv->driver.bus = &ipack_bus_type;
142         return driver_register(&edrv->driver);
143 }
144 EXPORT_SYMBOL_GPL(ipack_driver_register);
145
146 void ipack_driver_unregister(struct ipack_driver *edrv)
147 {
148         driver_unregister(&edrv->driver);
149 }
150 EXPORT_SYMBOL_GPL(ipack_driver_unregister);
151
152 struct ipack_device *ipack_device_register(struct ipack_bus_device *bus,
153                                            int slot, int irqv)
154 {
155         int ret;
156         struct ipack_device *dev;
157
158         dev = kzalloc(sizeof(struct ipack_device), GFP_KERNEL);
159         if (!dev)
160                 return NULL;
161
162         dev->dev.bus = &ipack_bus_type;
163         dev->dev.release = ipack_device_release;
164         dev->dev.parent = bus->parent;
165         dev->slot = slot;
166         dev->bus_nr = bus->bus_nr;
167         dev->irq = irqv;
168         dev->bus = bus;
169         dev_set_name(&dev->dev,
170                      "ipack-dev.%u.%u", dev->bus_nr, dev->slot);
171
172         ret = device_register(&dev->dev);
173         if (ret < 0) {
174                 pr_err("error registering the device.\n");
175                 dev->driver->ops->remove(dev);
176                 kfree(dev);
177                 return NULL;
178         }
179
180         return dev;
181 }
182 EXPORT_SYMBOL_GPL(ipack_device_register);
183
184 void ipack_device_unregister(struct ipack_device *dev)
185 {
186         device_unregister(&dev->dev);
187 }
188 EXPORT_SYMBOL_GPL(ipack_device_unregister);
189
190 static int __init ipack_init(void)
191 {
192         return bus_register(&ipack_bus_type);
193 }
194
195 static void __exit ipack_exit(void)
196 {
197         bus_unregister(&ipack_bus_type);
198 }
199
200 module_init(ipack_init);
201 module_exit(ipack_exit);
202
203 MODULE_AUTHOR("Samuel Iglesias Gonsalvez <siglesias@igalia.com>");
204 MODULE_LICENSE("GPL");
205 MODULE_DESCRIPTION("Industry-pack bus core");