]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/mcb/mcb-core.c
mcb: Correctly initialize the bus's device
[mirror_ubuntu-artful-kernel.git] / drivers / mcb / mcb-core.c
CommitLineData
3764e82e
JT
1/*
2 * MEN Chameleon Bus.
3 *
4 * Copyright (C) 2013 MEN Mikroelektronik GmbH (www.men.de)
5 * Author: Johannes Thumshirn <johannes.thumshirn@men.de>
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#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/slab.h>
14#include <linux/types.h>
15#include <linux/idr.h>
16#include <linux/mcb.h>
17
18static DEFINE_IDA(mcb_ida);
19
20static const struct mcb_device_id *mcb_match_id(const struct mcb_device_id *ids,
21 struct mcb_device *dev)
22{
23 if (ids) {
24 while (ids->device) {
25 if (ids->device == dev->id)
26 return ids;
27 ids++;
28 }
29 }
30
31 return NULL;
32}
33
34static int mcb_match(struct device *dev, struct device_driver *drv)
35{
36 struct mcb_driver *mdrv = to_mcb_driver(drv);
37 struct mcb_device *mdev = to_mcb_device(dev);
38 const struct mcb_device_id *found_id;
39
40 found_id = mcb_match_id(mdrv->id_table, mdev);
41 if (found_id)
42 return 1;
43
44 return 0;
45}
46
47static int mcb_uevent(struct device *dev, struct kobj_uevent_env *env)
48{
49 struct mcb_device *mdev = to_mcb_device(dev);
50 int ret;
51
52 ret = add_uevent_var(env, "MODALIAS=mcb:16z%03d", mdev->id);
53 if (ret)
54 return -ENOMEM;
55
56 return 0;
57}
58
59static int mcb_probe(struct device *dev)
60{
61 struct mcb_driver *mdrv = to_mcb_driver(dev->driver);
62 struct mcb_device *mdev = to_mcb_device(dev);
63 const struct mcb_device_id *found_id;
64
65 found_id = mcb_match_id(mdrv->id_table, mdev);
66 if (!found_id)
67 return -ENODEV;
68
69 return mdrv->probe(mdev, found_id);
70}
71
72static int mcb_remove(struct device *dev)
73{
74 struct mcb_driver *mdrv = to_mcb_driver(dev->driver);
75 struct mcb_device *mdev = to_mcb_device(dev);
76
77 mdrv->remove(mdev);
78
79 put_device(&mdev->dev);
80
81 return 0;
82}
83
84static void mcb_shutdown(struct device *dev)
85{
86 struct mcb_device *mdev = to_mcb_device(dev);
87 struct mcb_driver *mdrv = mdev->driver;
88
89 if (mdrv && mdrv->shutdown)
90 mdrv->shutdown(mdev);
91}
92
93static struct bus_type mcb_bus_type = {
94 .name = "mcb",
95 .match = mcb_match,
96 .uevent = mcb_uevent,
97 .probe = mcb_probe,
98 .remove = mcb_remove,
99 .shutdown = mcb_shutdown,
100};
101
102/**
103 * __mcb_register_driver() - Register a @mcb_driver at the system
104 * @drv: The @mcb_driver
105 * @owner: The @mcb_driver's module
106 * @mod_name: The name of the @mcb_driver's module
107 *
108 * Register a @mcb_driver at the system. Perform some sanity checks, if
109 * the .probe and .remove methods are provided by the driver.
110 */
111int __mcb_register_driver(struct mcb_driver *drv, struct module *owner,
112 const char *mod_name)
113{
114 if (!drv->probe || !drv->remove)
115 return -EINVAL;
116
117 drv->driver.owner = owner;
118 drv->driver.bus = &mcb_bus_type;
119 drv->driver.mod_name = mod_name;
120
121 return driver_register(&drv->driver);
122}
123EXPORT_SYMBOL_GPL(__mcb_register_driver);
124
125/**
126 * mcb_unregister_driver() - Unregister a @mcb_driver from the system
127 * @drv: The @mcb_driver
128 *
129 * Unregister a @mcb_driver from the system.
130 */
131void mcb_unregister_driver(struct mcb_driver *drv)
132{
133 driver_unregister(&drv->driver);
134}
135EXPORT_SYMBOL_GPL(mcb_unregister_driver);
136
137static void mcb_release_dev(struct device *dev)
138{
139 struct mcb_device *mdev = to_mcb_device(dev);
140
141 mcb_bus_put(mdev->bus);
142 kfree(mdev);
143}
144
145/**
146 * mcb_device_register() - Register a mcb_device
147 * @bus: The @mcb_bus of the device
148 * @dev: The @mcb_device
149 *
150 * Register a specific @mcb_device at a @mcb_bus and the system itself.
151 */
152int mcb_device_register(struct mcb_bus *bus, struct mcb_device *dev)
153{
154 int ret;
155 int device_id;
156
157 device_initialize(&dev->dev);
158 dev->dev.bus = &mcb_bus_type;
159 dev->dev.parent = bus->dev.parent;
160 dev->dev.release = mcb_release_dev;
161
162 device_id = dev->id;
163 dev_set_name(&dev->dev, "mcb%d-16z%03d-%d:%d:%d",
164 bus->bus_nr, device_id, dev->inst, dev->group, dev->var);
165
166 ret = device_add(&dev->dev);
167 if (ret < 0) {
168 pr_err("Failed registering device 16z%03d on bus mcb%d (%d)\n",
169 device_id, bus->bus_nr, ret);
170 goto out;
171 }
172
173 return 0;
174
175out:
176
177 return ret;
178}
179EXPORT_SYMBOL_GPL(mcb_device_register);
180
181/**
182 * mcb_alloc_bus() - Allocate a new @mcb_bus
183 *
184 * Allocate a new @mcb_bus.
185 */
4ec65b77 186struct mcb_bus *mcb_alloc_bus(struct device *carrier)
3764e82e
JT
187{
188 struct mcb_bus *bus;
189 int bus_nr;
18d28819 190 int rc;
3764e82e
JT
191
192 bus = kzalloc(sizeof(struct mcb_bus), GFP_KERNEL);
193 if (!bus)
4ec65b77 194 return ERR_PTR(-ENOMEM);
3764e82e
JT
195
196 bus_nr = ida_simple_get(&mcb_ida, 0, 0, GFP_KERNEL);
197 if (bus_nr < 0) {
18d28819
JT
198 rc = bus_nr;
199 goto err_free;
3764e82e
JT
200 }
201
3764e82e 202 bus->bus_nr = bus_nr;
4ec65b77 203 bus->carrier = carrier;
18d28819
JT
204
205 device_initialize(&bus->dev);
206 bus->dev.parent = carrier;
207 bus->dev.bus = &mcb_bus_type;
208
209 dev_set_name(&bus->dev, "mcb:%d", bus_nr);
210 rc = device_add(&bus->dev);
211 if (rc)
212 goto err_free;
213
3764e82e 214 return bus;
18d28819
JT
215err_free:
216 kfree(bus);
217 return ERR_PTR(rc);
3764e82e
JT
218}
219EXPORT_SYMBOL_GPL(mcb_alloc_bus);
220
221static int __mcb_devices_unregister(struct device *dev, void *data)
222{
223 device_unregister(dev);
224 return 0;
225}
226
227static void mcb_devices_unregister(struct mcb_bus *bus)
228{
229 bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_devices_unregister);
230}
231/**
232 * mcb_release_bus() - Free a @mcb_bus
233 * @bus: The @mcb_bus to release
234 *
235 * Release an allocated @mcb_bus from the system.
236 */
237void mcb_release_bus(struct mcb_bus *bus)
238{
239 mcb_devices_unregister(bus);
240
241 ida_simple_remove(&mcb_ida, bus->bus_nr);
242
243 kfree(bus);
244}
245EXPORT_SYMBOL_GPL(mcb_release_bus);
246
247/**
248 * mcb_bus_put() - Increment refcnt
249 * @bus: The @mcb_bus
250 *
251 * Get a @mcb_bus' ref
252 */
253struct mcb_bus *mcb_bus_get(struct mcb_bus *bus)
254{
255 if (bus)
256 get_device(&bus->dev);
257
258 return bus;
259}
260EXPORT_SYMBOL_GPL(mcb_bus_get);
261
262/**
263 * mcb_bus_put() - Decrement refcnt
264 * @bus: The @mcb_bus
265 *
266 * Release a @mcb_bus' ref
267 */
268void mcb_bus_put(struct mcb_bus *bus)
269{
270 if (bus)
271 put_device(&bus->dev);
272}
273EXPORT_SYMBOL_GPL(mcb_bus_put);
274
275/**
276 * mcb_alloc_dev() - Allocate a device
277 * @bus: The @mcb_bus the device is part of
278 *
279 * Allocate a @mcb_device and add bus.
280 */
281struct mcb_device *mcb_alloc_dev(struct mcb_bus *bus)
282{
283 struct mcb_device *dev;
284
285 dev = kzalloc(sizeof(struct mcb_device), GFP_KERNEL);
286 if (!dev)
287 return NULL;
288
289 INIT_LIST_HEAD(&dev->bus_list);
290 dev->bus = bus;
291
292 return dev;
293}
294EXPORT_SYMBOL_GPL(mcb_alloc_dev);
295
296/**
297 * mcb_free_dev() - Free @mcb_device
298 * @dev: The device to free
299 *
300 * Free a @mcb_device
301 */
302void mcb_free_dev(struct mcb_device *dev)
303{
304 kfree(dev);
305}
306EXPORT_SYMBOL_GPL(mcb_free_dev);
307
308static int __mcb_bus_add_devices(struct device *dev, void *data)
309{
310 struct mcb_device *mdev = to_mcb_device(dev);
311 int retval;
312
313 if (mdev->is_added)
314 return 0;
315
316 retval = device_attach(dev);
317 if (retval < 0)
318 dev_err(dev, "Error adding device (%d)\n", retval);
319
320 mdev->is_added = true;
321
322 return 0;
323}
324
325static int __mcb_bus_add_child(struct device *dev, void *data)
326{
327 struct mcb_device *mdev = to_mcb_device(dev);
328 struct mcb_bus *child;
329
330 BUG_ON(!mdev->is_added);
331 child = mdev->subordinate;
332
333 if (child)
334 mcb_bus_add_devices(child);
335
336 return 0;
337}
338
339/**
340 * mcb_bus_add_devices() - Add devices in the bus' internal device list
341 * @bus: The @mcb_bus we add the devices
342 *
343 * Add devices in the bus' internal device list to the system.
344 */
345void mcb_bus_add_devices(const struct mcb_bus *bus)
346{
347 bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_bus_add_devices);
348 bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_bus_add_child);
349
350}
351EXPORT_SYMBOL_GPL(mcb_bus_add_devices);
352
353/**
354 * mcb_request_mem() - Request memory
355 * @dev: The @mcb_device the memory is for
356 * @name: The name for the memory reference.
357 *
358 * Request memory for a @mcb_device. If @name is NULL the driver name will
359 * be used.
360 */
361struct resource *mcb_request_mem(struct mcb_device *dev, const char *name)
362{
363 struct resource *mem;
364 u32 size;
365
366 if (!name)
367 name = dev->dev.driver->name;
368
369 size = resource_size(&dev->mem);
370
371 mem = request_mem_region(dev->mem.start, size, name);
372 if (!mem)
373 return ERR_PTR(-EBUSY);
374
375 return mem;
376}
377EXPORT_SYMBOL_GPL(mcb_request_mem);
378
379/**
380 * mcb_release_mem() - Release memory requested by device
381 * @dev: The @mcb_device that requested the memory
382 *
383 * Release memory that was prior requested via @mcb_request_mem().
384 */
385void mcb_release_mem(struct resource *mem)
386{
387 u32 size;
388
389 size = resource_size(mem);
390 release_mem_region(mem->start, size);
391}
392EXPORT_SYMBOL_GPL(mcb_release_mem);
393
4ec65b77
JT
394static int __mcb_get_irq(struct mcb_device *dev)
395{
396 struct resource *irq = &dev->irq;
397
398 return irq->start;
399}
400
3764e82e
JT
401/**
402 * mcb_get_irq() - Get device's IRQ number
403 * @dev: The @mcb_device the IRQ is for
404 *
405 * Get the IRQ number of a given @mcb_device.
406 */
407int mcb_get_irq(struct mcb_device *dev)
408{
4ec65b77 409 struct mcb_bus *bus = dev->bus;
3764e82e 410
4ec65b77
JT
411 if (bus->get_irq)
412 return bus->get_irq(dev);
413
414 return __mcb_get_irq(dev);
3764e82e
JT
415}
416EXPORT_SYMBOL_GPL(mcb_get_irq);
417
418static int mcb_init(void)
419{
420 return bus_register(&mcb_bus_type);
421}
422
423static void mcb_exit(void)
424{
169883a6 425 ida_destroy(&mcb_ida);
3764e82e
JT
426 bus_unregister(&mcb_bus_type);
427}
428
429/* mcb must be initialized after PCI but before the chameleon drivers.
430 * That means we must use some initcall between subsys_initcall and
431 * device_initcall.
432 */
433fs_initcall(mcb_init);
434module_exit(mcb_exit);
435
436MODULE_DESCRIPTION("MEN Chameleon Bus Driver");
437MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>");
438MODULE_LICENSE("GPL v2");