Linux 4.1.16
[linux/fpc-iii.git] / drivers / mcb / mcb-core.c
blob9018ab83517adcc8aedb239744a2842a0391b3dc
1 /*
2 * MEN Chameleon Bus.
4 * Copyright (C) 2013 MEN Mikroelektronik GmbH (www.men.de)
5 * Author: Johannes Thumshirn <johannes.thumshirn@men.de>
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.
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>
18 static DEFINE_IDA(mcb_ida);
20 static const struct mcb_device_id *mcb_match_id(const struct mcb_device_id *ids,
21 struct mcb_device *dev)
23 if (ids) {
24 while (ids->device) {
25 if (ids->device == dev->id)
26 return ids;
27 ids++;
31 return NULL;
34 static int mcb_match(struct device *dev, struct device_driver *drv)
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;
40 found_id = mcb_match_id(mdrv->id_table, mdev);
41 if (found_id)
42 return 1;
44 return 0;
47 static int mcb_uevent(struct device *dev, struct kobj_uevent_env *env)
49 struct mcb_device *mdev = to_mcb_device(dev);
50 int ret;
52 ret = add_uevent_var(env, "MODALIAS=mcb:16z%03d", mdev->id);
53 if (ret)
54 return -ENOMEM;
56 return 0;
59 static int mcb_probe(struct device *dev)
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;
65 found_id = mcb_match_id(mdrv->id_table, mdev);
66 if (!found_id)
67 return -ENODEV;
69 return mdrv->probe(mdev, found_id);
72 static int mcb_remove(struct device *dev)
74 struct mcb_driver *mdrv = to_mcb_driver(dev->driver);
75 struct mcb_device *mdev = to_mcb_device(dev);
77 mdrv->remove(mdev);
79 put_device(&mdev->dev);
81 return 0;
84 static void mcb_shutdown(struct device *dev)
86 struct mcb_device *mdev = to_mcb_device(dev);
87 struct mcb_driver *mdrv = mdev->driver;
89 if (mdrv && mdrv->shutdown)
90 mdrv->shutdown(mdev);
93 static 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,
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
108 * Register a @mcb_driver at the system. Perform some sanity checks, if
109 * the .probe and .remove methods are provided by the driver.
111 int __mcb_register_driver(struct mcb_driver *drv, struct module *owner,
112 const char *mod_name)
114 if (!drv->probe || !drv->remove)
115 return -EINVAL;
117 drv->driver.owner = owner;
118 drv->driver.bus = &mcb_bus_type;
119 drv->driver.mod_name = mod_name;
121 return driver_register(&drv->driver);
123 EXPORT_SYMBOL_GPL(__mcb_register_driver);
126 * mcb_unregister_driver() - Unregister a @mcb_driver from the system
127 * @drv: The @mcb_driver
129 * Unregister a @mcb_driver from the system.
131 void mcb_unregister_driver(struct mcb_driver *drv)
133 driver_unregister(&drv->driver);
135 EXPORT_SYMBOL_GPL(mcb_unregister_driver);
137 static void mcb_release_dev(struct device *dev)
139 struct mcb_device *mdev = to_mcb_device(dev);
141 mcb_bus_put(mdev->bus);
142 kfree(mdev);
146 * mcb_device_register() - Register a mcb_device
147 * @bus: The @mcb_bus of the device
148 * @dev: The @mcb_device
150 * Register a specific @mcb_device at a @mcb_bus and the system itself.
152 int mcb_device_register(struct mcb_bus *bus, struct mcb_device *dev)
154 int ret;
155 int device_id;
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;
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);
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;
173 return 0;
175 out:
177 return ret;
179 EXPORT_SYMBOL_GPL(mcb_device_register);
182 * mcb_alloc_bus() - Allocate a new @mcb_bus
184 * Allocate a new @mcb_bus.
186 struct mcb_bus *mcb_alloc_bus(struct device *carrier)
188 struct mcb_bus *bus;
189 int bus_nr;
191 bus = kzalloc(sizeof(struct mcb_bus), GFP_KERNEL);
192 if (!bus)
193 return ERR_PTR(-ENOMEM);
195 bus_nr = ida_simple_get(&mcb_ida, 0, 0, GFP_KERNEL);
196 if (bus_nr < 0) {
197 kfree(bus);
198 return ERR_PTR(bus_nr);
201 INIT_LIST_HEAD(&bus->children);
202 bus->bus_nr = bus_nr;
203 bus->carrier = carrier;
204 return bus;
206 EXPORT_SYMBOL_GPL(mcb_alloc_bus);
208 static int __mcb_devices_unregister(struct device *dev, void *data)
210 device_unregister(dev);
211 return 0;
214 static void mcb_devices_unregister(struct mcb_bus *bus)
216 bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_devices_unregister);
219 * mcb_release_bus() - Free a @mcb_bus
220 * @bus: The @mcb_bus to release
222 * Release an allocated @mcb_bus from the system.
224 void mcb_release_bus(struct mcb_bus *bus)
226 mcb_devices_unregister(bus);
228 ida_simple_remove(&mcb_ida, bus->bus_nr);
230 kfree(bus);
232 EXPORT_SYMBOL_GPL(mcb_release_bus);
235 * mcb_bus_put() - Increment refcnt
236 * @bus: The @mcb_bus
238 * Get a @mcb_bus' ref
240 struct mcb_bus *mcb_bus_get(struct mcb_bus *bus)
242 if (bus)
243 get_device(&bus->dev);
245 return bus;
247 EXPORT_SYMBOL_GPL(mcb_bus_get);
250 * mcb_bus_put() - Decrement refcnt
251 * @bus: The @mcb_bus
253 * Release a @mcb_bus' ref
255 void mcb_bus_put(struct mcb_bus *bus)
257 if (bus)
258 put_device(&bus->dev);
260 EXPORT_SYMBOL_GPL(mcb_bus_put);
263 * mcb_alloc_dev() - Allocate a device
264 * @bus: The @mcb_bus the device is part of
266 * Allocate a @mcb_device and add bus.
268 struct mcb_device *mcb_alloc_dev(struct mcb_bus *bus)
270 struct mcb_device *dev;
272 dev = kzalloc(sizeof(struct mcb_device), GFP_KERNEL);
273 if (!dev)
274 return NULL;
276 INIT_LIST_HEAD(&dev->bus_list);
277 dev->bus = bus;
279 return dev;
281 EXPORT_SYMBOL_GPL(mcb_alloc_dev);
284 * mcb_free_dev() - Free @mcb_device
285 * @dev: The device to free
287 * Free a @mcb_device
289 void mcb_free_dev(struct mcb_device *dev)
291 kfree(dev);
293 EXPORT_SYMBOL_GPL(mcb_free_dev);
295 static int __mcb_bus_add_devices(struct device *dev, void *data)
297 struct mcb_device *mdev = to_mcb_device(dev);
298 int retval;
300 if (mdev->is_added)
301 return 0;
303 retval = device_attach(dev);
304 if (retval < 0)
305 dev_err(dev, "Error adding device (%d)\n", retval);
307 mdev->is_added = true;
309 return 0;
312 static int __mcb_bus_add_child(struct device *dev, void *data)
314 struct mcb_device *mdev = to_mcb_device(dev);
315 struct mcb_bus *child;
317 BUG_ON(!mdev->is_added);
318 child = mdev->subordinate;
320 if (child)
321 mcb_bus_add_devices(child);
323 return 0;
327 * mcb_bus_add_devices() - Add devices in the bus' internal device list
328 * @bus: The @mcb_bus we add the devices
330 * Add devices in the bus' internal device list to the system.
332 void mcb_bus_add_devices(const struct mcb_bus *bus)
334 bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_bus_add_devices);
335 bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_bus_add_child);
338 EXPORT_SYMBOL_GPL(mcb_bus_add_devices);
341 * mcb_request_mem() - Request memory
342 * @dev: The @mcb_device the memory is for
343 * @name: The name for the memory reference.
345 * Request memory for a @mcb_device. If @name is NULL the driver name will
346 * be used.
348 struct resource *mcb_request_mem(struct mcb_device *dev, const char *name)
350 struct resource *mem;
351 u32 size;
353 if (!name)
354 name = dev->dev.driver->name;
356 size = resource_size(&dev->mem);
358 mem = request_mem_region(dev->mem.start, size, name);
359 if (!mem)
360 return ERR_PTR(-EBUSY);
362 return mem;
364 EXPORT_SYMBOL_GPL(mcb_request_mem);
367 * mcb_release_mem() - Release memory requested by device
368 * @dev: The @mcb_device that requested the memory
370 * Release memory that was prior requested via @mcb_request_mem().
372 void mcb_release_mem(struct resource *mem)
374 u32 size;
376 size = resource_size(mem);
377 release_mem_region(mem->start, size);
379 EXPORT_SYMBOL_GPL(mcb_release_mem);
381 static int __mcb_get_irq(struct mcb_device *dev)
383 struct resource *irq = &dev->irq;
385 return irq->start;
389 * mcb_get_irq() - Get device's IRQ number
390 * @dev: The @mcb_device the IRQ is for
392 * Get the IRQ number of a given @mcb_device.
394 int mcb_get_irq(struct mcb_device *dev)
396 struct mcb_bus *bus = dev->bus;
398 if (bus->get_irq)
399 return bus->get_irq(dev);
401 return __mcb_get_irq(dev);
403 EXPORT_SYMBOL_GPL(mcb_get_irq);
405 static int mcb_init(void)
407 return bus_register(&mcb_bus_type);
410 static void mcb_exit(void)
412 bus_unregister(&mcb_bus_type);
415 /* mcb must be initialized after PCI but before the chameleon drivers.
416 * That means we must use some initcall between subsys_initcall and
417 * device_initcall.
419 fs_initcall(mcb_init);
420 module_exit(mcb_exit);
422 MODULE_DESCRIPTION("MEN Chameleon Bus Driver");
423 MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>");
424 MODULE_LICENSE("GPL v2");