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)
25 if (ids->device == dev->id)
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);
47 static int mcb_uevent(struct device *dev, struct kobj_uevent_env *env)
49 struct mcb_device *mdev = to_mcb_device(dev);
52 ret = add_uevent_var(env, "MODALIAS=mcb:16z%03d", mdev->id);
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;
64 struct module *carrier_mod;
67 found_id = mcb_match_id(mdrv->id_table, mdev);
71 carrier_mod = mdev->dev.parent->driver->owner;
72 if (!try_module_get(carrier_mod))
76 ret = mdrv->probe(mdev, found_id);
78 module_put(carrier_mod);
85 static int mcb_remove(struct device *dev)
87 struct mcb_driver *mdrv = to_mcb_driver(dev->driver);
88 struct mcb_device *mdev = to_mcb_device(dev);
89 struct module *carrier_mod;
93 carrier_mod = mdev->dev.parent->driver->owner;
94 module_put(carrier_mod);
96 put_device(&mdev->dev);
101 static void mcb_shutdown(struct device *dev)
103 struct mcb_driver *mdrv = to_mcb_driver(dev->driver);
104 struct mcb_device *mdev = to_mcb_device(dev);
106 if (mdrv && mdrv->shutdown)
107 mdrv->shutdown(mdev);
110 static ssize_t revision_show(struct device *dev, struct device_attribute *attr,
113 struct mcb_bus *bus = to_mcb_bus(dev);
115 return scnprintf(buf, PAGE_SIZE, "%d\n", bus->revision);
117 static DEVICE_ATTR_RO(revision);
119 static ssize_t model_show(struct device *dev, struct device_attribute *attr,
122 struct mcb_bus *bus = to_mcb_bus(dev);
124 return scnprintf(buf, PAGE_SIZE, "%c\n", bus->model);
126 static DEVICE_ATTR_RO(model);
128 static ssize_t minor_show(struct device *dev, struct device_attribute *attr,
131 struct mcb_bus *bus = to_mcb_bus(dev);
133 return scnprintf(buf, PAGE_SIZE, "%d\n", bus->minor);
135 static DEVICE_ATTR_RO(minor);
137 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
140 struct mcb_bus *bus = to_mcb_bus(dev);
142 return scnprintf(buf, PAGE_SIZE, "%s\n", bus->name);
144 static DEVICE_ATTR_RO(name);
146 static struct attribute *mcb_bus_attrs[] = {
147 &dev_attr_revision.attr,
148 &dev_attr_model.attr,
149 &dev_attr_minor.attr,
154 static const struct attribute_group mcb_carrier_group = {
155 .attrs = mcb_bus_attrs,
158 static const struct attribute_group *mcb_carrier_groups[] = {
164 static struct bus_type mcb_bus_type = {
167 .uevent = mcb_uevent,
169 .remove = mcb_remove,
170 .shutdown = mcb_shutdown,
173 static struct device_type mcb_carrier_device_type = {
174 .name = "mcb-carrier",
175 .groups = mcb_carrier_groups,
179 * __mcb_register_driver() - Register a @mcb_driver at the system
180 * @drv: The @mcb_driver
181 * @owner: The @mcb_driver's module
182 * @mod_name: The name of the @mcb_driver's module
184 * Register a @mcb_driver at the system. Perform some sanity checks, if
185 * the .probe and .remove methods are provided by the driver.
187 int __mcb_register_driver(struct mcb_driver *drv, struct module *owner,
188 const char *mod_name)
190 if (!drv->probe || !drv->remove)
193 drv->driver.owner = owner;
194 drv->driver.bus = &mcb_bus_type;
195 drv->driver.mod_name = mod_name;
197 return driver_register(&drv->driver);
199 EXPORT_SYMBOL_GPL(__mcb_register_driver);
202 * mcb_unregister_driver() - Unregister a @mcb_driver from the system
203 * @drv: The @mcb_driver
205 * Unregister a @mcb_driver from the system.
207 void mcb_unregister_driver(struct mcb_driver *drv)
209 driver_unregister(&drv->driver);
211 EXPORT_SYMBOL_GPL(mcb_unregister_driver);
213 static void mcb_release_dev(struct device *dev)
215 struct mcb_device *mdev = to_mcb_device(dev);
217 mcb_bus_put(mdev->bus);
222 * mcb_device_register() - Register a mcb_device
223 * @bus: The @mcb_bus of the device
224 * @dev: The @mcb_device
226 * Register a specific @mcb_device at a @mcb_bus and the system itself.
228 int mcb_device_register(struct mcb_bus *bus, struct mcb_device *dev)
233 device_initialize(&dev->dev);
235 dev->dev.bus = &mcb_bus_type;
236 dev->dev.parent = bus->dev.parent;
237 dev->dev.release = mcb_release_dev;
238 dev->dma_dev = bus->carrier;
241 dev_set_name(&dev->dev, "mcb%d-16z%03d-%d:%d:%d",
242 bus->bus_nr, device_id, dev->inst, dev->group, dev->var);
244 ret = device_add(&dev->dev);
246 pr_err("Failed registering device 16z%03d on bus mcb%d (%d)\n",
247 device_id, bus->bus_nr, ret);
257 EXPORT_SYMBOL_GPL(mcb_device_register);
259 static void mcb_free_bus(struct device *dev)
261 struct mcb_bus *bus = to_mcb_bus(dev);
263 put_device(bus->carrier);
264 ida_simple_remove(&mcb_ida, bus->bus_nr);
269 * mcb_alloc_bus() - Allocate a new @mcb_bus
271 * Allocate a new @mcb_bus.
273 struct mcb_bus *mcb_alloc_bus(struct device *carrier)
279 bus = kzalloc(sizeof(struct mcb_bus), GFP_KERNEL);
281 return ERR_PTR(-ENOMEM);
283 bus_nr = ida_simple_get(&mcb_ida, 0, 0, GFP_KERNEL);
286 return ERR_PTR(bus_nr);
289 bus->bus_nr = bus_nr;
290 bus->carrier = get_device(carrier);
292 device_initialize(&bus->dev);
293 bus->dev.parent = carrier;
294 bus->dev.bus = &mcb_bus_type;
295 bus->dev.type = &mcb_carrier_device_type;
296 bus->dev.release = &mcb_free_bus;
298 dev_set_name(&bus->dev, "mcb:%d", bus_nr);
299 rc = device_add(&bus->dev);
306 put_device(&bus->dev);
309 EXPORT_SYMBOL_GPL(mcb_alloc_bus);
311 static int __mcb_devices_unregister(struct device *dev, void *data)
313 device_unregister(dev);
317 static void mcb_devices_unregister(struct mcb_bus *bus)
319 bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_devices_unregister);
322 * mcb_release_bus() - Free a @mcb_bus
323 * @bus: The @mcb_bus to release
325 * Release an allocated @mcb_bus from the system.
327 void mcb_release_bus(struct mcb_bus *bus)
329 mcb_devices_unregister(bus);
331 EXPORT_SYMBOL_GPL(mcb_release_bus);
334 * mcb_bus_put() - Increment refcnt
337 * Get a @mcb_bus' ref
339 struct mcb_bus *mcb_bus_get(struct mcb_bus *bus)
342 get_device(&bus->dev);
346 EXPORT_SYMBOL_GPL(mcb_bus_get);
349 * mcb_bus_put() - Decrement refcnt
352 * Release a @mcb_bus' ref
354 void mcb_bus_put(struct mcb_bus *bus)
357 put_device(&bus->dev);
359 EXPORT_SYMBOL_GPL(mcb_bus_put);
362 * mcb_alloc_dev() - Allocate a device
363 * @bus: The @mcb_bus the device is part of
365 * Allocate a @mcb_device and add bus.
367 struct mcb_device *mcb_alloc_dev(struct mcb_bus *bus)
369 struct mcb_device *dev;
371 dev = kzalloc(sizeof(struct mcb_device), GFP_KERNEL);
379 EXPORT_SYMBOL_GPL(mcb_alloc_dev);
382 * mcb_free_dev() - Free @mcb_device
383 * @dev: The device to free
387 void mcb_free_dev(struct mcb_device *dev)
391 EXPORT_SYMBOL_GPL(mcb_free_dev);
393 static int __mcb_bus_add_devices(struct device *dev, void *data)
397 retval = device_attach(dev);
399 dev_err(dev, "Error adding device (%d)\n", retval);
407 * mcb_bus_add_devices() - Add devices in the bus' internal device list
408 * @bus: The @mcb_bus we add the devices
410 * Add devices in the bus' internal device list to the system.
412 void mcb_bus_add_devices(const struct mcb_bus *bus)
414 bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_bus_add_devices);
416 EXPORT_SYMBOL_GPL(mcb_bus_add_devices);
419 * mcb_get_resource() - get a resource for a mcb device
420 * @dev: the mcb device
421 * @type: the type of resource
423 struct resource *mcb_get_resource(struct mcb_device *dev, unsigned int type)
425 if (type == IORESOURCE_MEM)
427 else if (type == IORESOURCE_IRQ)
432 EXPORT_SYMBOL_GPL(mcb_get_resource);
435 * mcb_request_mem() - Request memory
436 * @dev: The @mcb_device the memory is for
437 * @name: The name for the memory reference.
439 * Request memory for a @mcb_device. If @name is NULL the driver name will
442 struct resource *mcb_request_mem(struct mcb_device *dev, const char *name)
444 struct resource *mem;
448 name = dev->dev.driver->name;
450 size = resource_size(&dev->mem);
452 mem = request_mem_region(dev->mem.start, size, name);
454 return ERR_PTR(-EBUSY);
458 EXPORT_SYMBOL_GPL(mcb_request_mem);
461 * mcb_release_mem() - Release memory requested by device
462 * @dev: The @mcb_device that requested the memory
464 * Release memory that was prior requested via @mcb_request_mem().
466 void mcb_release_mem(struct resource *mem)
470 size = resource_size(mem);
471 release_mem_region(mem->start, size);
473 EXPORT_SYMBOL_GPL(mcb_release_mem);
475 static int __mcb_get_irq(struct mcb_device *dev)
477 struct resource *irq;
479 irq = mcb_get_resource(dev, IORESOURCE_IRQ);
485 * mcb_get_irq() - Get device's IRQ number
486 * @dev: The @mcb_device the IRQ is for
488 * Get the IRQ number of a given @mcb_device.
490 int mcb_get_irq(struct mcb_device *dev)
492 struct mcb_bus *bus = dev->bus;
495 return bus->get_irq(dev);
497 return __mcb_get_irq(dev);
499 EXPORT_SYMBOL_GPL(mcb_get_irq);
501 static int mcb_init(void)
503 return bus_register(&mcb_bus_type);
506 static void mcb_exit(void)
508 ida_destroy(&mcb_ida);
509 bus_unregister(&mcb_bus_type);
512 /* mcb must be initialized after PCI but before the chameleon drivers.
513 * That means we must use some initcall between subsys_initcall and
516 fs_initcall(mcb_init);
517 module_exit(mcb_exit);
519 MODULE_DESCRIPTION("MEN Chameleon Bus Driver");
520 MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>");
521 MODULE_LICENSE("GPL v2");