1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
/*
* Industry-pack bus support functions.
*
* (C) 2011 Samuel Iglesias Gonsalvez <siglesia@cern.ch>, CERN
* (C) 2012 Samuel Iglesias Gonsalvez <siglesias@igalia.com>, Igalia
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; version 2 of the License.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/idr.h>
#include "ipack.h"
#define to_ipack_dev(device) container_of(device, struct ipack_device, dev)
#define to_ipack_driver(drv) container_of(drv, struct ipack_driver, driver)
static DEFINE_IDA(ipack_ida);
static void ipack_device_release(struct device *dev)
{
struct ipack_device *device = to_ipack_dev(dev);
kfree(device);
}
static int ipack_bus_match(struct device *device, struct device_driver *driver)
{
int ret;
struct ipack_device *dev = to_ipack_dev(device);
struct ipack_driver *drv = to_ipack_driver(driver);
if ((!drv->ops) || (!drv->ops->match))
return -EINVAL;
ret = drv->ops->match(dev);
if (ret)
dev->driver = drv;
return ret;
}
static int ipack_bus_probe(struct device *device)
{
struct ipack_device *dev = to_ipack_dev(device);
if (!dev->driver->ops->probe)
return -EINVAL;
return dev->driver->ops->probe(dev);
}
static int ipack_bus_remove(struct device *device)
{
struct ipack_device *dev = to_ipack_dev(device);
if (!dev->driver->ops->remove)
return -EINVAL;
dev->driver->ops->remove(dev);
return 0;
}
static struct bus_type ipack_bus_type = {
.name = "ipack",
.probe = ipack_bus_probe,
.match = ipack_bus_match,
.remove = ipack_bus_remove,
};
struct ipack_bus_device *ipack_bus_register(struct device *parent, int slots,
struct ipack_bus_ops *ops)
{
int bus_nr;
struct ipack_bus_device *bus;
bus = kzalloc(sizeof(struct ipack_bus_device), GFP_KERNEL);
if (!bus)
return NULL;
bus_nr = ida_simple_get(&ipack_ida, 0, 0, GFP_KERNEL);
if (bus_nr < 0) {
kfree(bus);
return NULL;
}
bus->bus_nr = bus_nr;
bus->parent = parent;
bus->slots = slots;
bus->ops = ops;
return bus;
}
EXPORT_SYMBOL_GPL(ipack_bus_register);
int ipack_bus_unregister(struct ipack_bus_device *bus)
{
ida_simple_remove(&ipack_ida, bus->bus_nr);
kfree(bus);
return 0;
}
EXPORT_SYMBOL_GPL(ipack_bus_unregister);
int ipack_driver_register(struct ipack_driver *edrv, struct module *owner,
char *name)
{
edrv->driver.owner = owner;
edrv->driver.name = name;
edrv->driver.bus = &ipack_bus_type;
return driver_register(&edrv->driver);
}
EXPORT_SYMBOL_GPL(ipack_driver_register);
void ipack_driver_unregister(struct ipack_driver *edrv)
{
driver_unregister(&edrv->driver);
}
EXPORT_SYMBOL_GPL(ipack_driver_unregister);
struct ipack_device *ipack_device_register(struct ipack_bus_device *bus,
int slot, int irqv)
{
int ret;
struct ipack_device *dev;
dev = kzalloc(sizeof(struct ipack_device), GFP_KERNEL);
if (!dev)
return NULL;
dev->dev.bus = &ipack_bus_type;
dev->dev.release = ipack_device_release;
dev->dev.parent = bus->parent;
dev->slot = slot;
dev->bus_nr = bus->bus_nr;
dev->irq = irqv;
dev->bus = bus;
dev_set_name(&dev->dev,
"ipack-dev.%u.%u", dev->bus_nr, dev->slot);
ret = device_register(&dev->dev);
if (ret < 0) {
pr_err("error registering the device.\n");
dev->driver->ops->remove(dev);
kfree(dev);
return NULL;
}
return dev;
}
EXPORT_SYMBOL_GPL(ipack_device_register);
void ipack_device_unregister(struct ipack_device *dev)
{
device_unregister(&dev->dev);
}
EXPORT_SYMBOL_GPL(ipack_device_unregister);
static int __init ipack_init(void)
{
ida_init(&ipack_ida);
return bus_register(&ipack_bus_type);
}
static void __exit ipack_exit(void)
{
bus_unregister(&ipack_bus_type);
ida_destroy(&ipack_ida);
}
module_init(ipack_init);
module_exit(ipack_exit);
MODULE_AUTHOR("Samuel Iglesias Gonsalvez <siglesias@igalia.com>");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Industry-pack bus core");
|