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;
65 found_id = mcb_match_id(mdrv->id_table, mdev);
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);
79 put_device(&mdev->dev);
84 static void mcb_shutdown(struct device *dev)
86 struct mcb_driver *mdrv = to_mcb_driver(dev->driver);
87 struct mcb_device *mdev = to_mcb_device(dev);
89 if (mdrv && mdrv->shutdown)
93 static ssize_t revision_show(struct device *dev, struct device_attribute *attr,
96 struct mcb_bus *bus = to_mcb_bus(dev);
98 return scnprintf(buf, PAGE_SIZE, "%d\n", bus->revision);
100 static DEVICE_ATTR_RO(revision);
102 static ssize_t model_show(struct device *dev, struct device_attribute *attr,
105 struct mcb_bus *bus = to_mcb_bus(dev);
107 return scnprintf(buf, PAGE_SIZE, "%c\n", bus->model);
109 static DEVICE_ATTR_RO(model);
111 static ssize_t minor_show(struct device *dev, struct device_attribute *attr,
114 struct mcb_bus *bus = to_mcb_bus(dev);
116 return scnprintf(buf, PAGE_SIZE, "%d\n", bus->minor);
118 static DEVICE_ATTR_RO(minor);
120 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
123 struct mcb_bus *bus = to_mcb_bus(dev);
125 return scnprintf(buf, PAGE_SIZE, "%s\n", bus->name);
127 static DEVICE_ATTR_RO(name);
129 static struct attribute *mcb_bus_attrs[] = {
130 &dev_attr_revision.attr,
131 &dev_attr_model.attr,
132 &dev_attr_minor.attr,
137 static const struct attribute_group mcb_carrier_group = {
138 .attrs = mcb_bus_attrs,
141 static const struct attribute_group *mcb_carrier_groups[] = {
147 static struct bus_type mcb_bus_type = {
150 .uevent = mcb_uevent,
152 .remove = mcb_remove,
153 .shutdown = mcb_shutdown,
156 static struct device_type mcb_carrier_device_type = {
157 .name = "mcb-carrier",
158 .groups = mcb_carrier_groups,
162 * __mcb_register_driver() - Register a @mcb_driver at the system
163 * @drv: The @mcb_driver
164 * @owner: The @mcb_driver's module
165 * @mod_name: The name of the @mcb_driver's module
167 * Register a @mcb_driver at the system. Perform some sanity checks, if
168 * the .probe and .remove methods are provided by the driver.
170 int __mcb_register_driver(struct mcb_driver *drv, struct module *owner,
171 const char *mod_name)
173 if (!drv->probe || !drv->remove)
176 drv->driver.owner = owner;
177 drv->driver.bus = &mcb_bus_type;
178 drv->driver.mod_name = mod_name;
180 return driver_register(&drv->driver);
182 EXPORT_SYMBOL_GPL(__mcb_register_driver);
185 * mcb_unregister_driver() - Unregister a @mcb_driver from the system
186 * @drv: The @mcb_driver
188 * Unregister a @mcb_driver from the system.
190 void mcb_unregister_driver(struct mcb_driver *drv)
192 driver_unregister(&drv->driver);
194 EXPORT_SYMBOL_GPL(mcb_unregister_driver);
196 static void mcb_release_dev(struct device *dev)
198 struct mcb_device *mdev = to_mcb_device(dev);
200 mcb_bus_put(mdev->bus);
205 * mcb_device_register() - Register a mcb_device
206 * @bus: The @mcb_bus of the device
207 * @dev: The @mcb_device
209 * Register a specific @mcb_device at a @mcb_bus and the system itself.
211 int mcb_device_register(struct mcb_bus *bus, struct mcb_device *dev)
216 device_initialize(&dev->dev);
218 dev->dev.bus = &mcb_bus_type;
219 dev->dev.parent = bus->dev.parent;
220 dev->dev.release = mcb_release_dev;
223 dev_set_name(&dev->dev, "mcb%d-16z%03d-%d:%d:%d",
224 bus->bus_nr, device_id, dev->inst, dev->group, dev->var);
226 ret = device_add(&dev->dev);
228 pr_err("Failed registering device 16z%03d on bus mcb%d (%d)\n",
229 device_id, bus->bus_nr, ret);
239 EXPORT_SYMBOL_GPL(mcb_device_register);
241 static void mcb_free_bus(struct device *dev)
243 struct mcb_bus *bus = to_mcb_bus(dev);
245 put_device(bus->carrier);
246 ida_simple_remove(&mcb_ida, bus->bus_nr);
251 * mcb_alloc_bus() - Allocate a new @mcb_bus
253 * Allocate a new @mcb_bus.
255 struct mcb_bus *mcb_alloc_bus(struct device *carrier)
261 bus = kzalloc(sizeof(struct mcb_bus), GFP_KERNEL);
263 return ERR_PTR(-ENOMEM);
265 bus_nr = ida_simple_get(&mcb_ida, 0, 0, GFP_KERNEL);
271 bus->bus_nr = bus_nr;
272 bus->carrier = get_device(carrier);
274 device_initialize(&bus->dev);
275 bus->dev.parent = carrier;
276 bus->dev.bus = &mcb_bus_type;
277 bus->dev.type = &mcb_carrier_device_type;
278 bus->dev.release = &mcb_free_bus;
280 dev_set_name(&bus->dev, "mcb:%d", bus_nr);
281 rc = device_add(&bus->dev);
291 EXPORT_SYMBOL_GPL(mcb_alloc_bus);
293 static int __mcb_devices_unregister(struct device *dev, void *data)
295 device_unregister(dev);
299 static void mcb_devices_unregister(struct mcb_bus *bus)
301 bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_devices_unregister);
304 * mcb_release_bus() - Free a @mcb_bus
305 * @bus: The @mcb_bus to release
307 * Release an allocated @mcb_bus from the system.
309 void mcb_release_bus(struct mcb_bus *bus)
311 mcb_devices_unregister(bus);
313 EXPORT_SYMBOL_GPL(mcb_release_bus);
316 * mcb_bus_put() - Increment refcnt
319 * Get a @mcb_bus' ref
321 struct mcb_bus *mcb_bus_get(struct mcb_bus *bus)
324 get_device(&bus->dev);
328 EXPORT_SYMBOL_GPL(mcb_bus_get);
331 * mcb_bus_put() - Decrement refcnt
334 * Release a @mcb_bus' ref
336 void mcb_bus_put(struct mcb_bus *bus)
339 put_device(&bus->dev);
341 EXPORT_SYMBOL_GPL(mcb_bus_put);
344 * mcb_alloc_dev() - Allocate a device
345 * @bus: The @mcb_bus the device is part of
347 * Allocate a @mcb_device and add bus.
349 struct mcb_device *mcb_alloc_dev(struct mcb_bus *bus)
351 struct mcb_device *dev;
353 dev = kzalloc(sizeof(struct mcb_device), GFP_KERNEL);
357 INIT_LIST_HEAD(&dev->bus_list);
362 EXPORT_SYMBOL_GPL(mcb_alloc_dev);
365 * mcb_free_dev() - Free @mcb_device
366 * @dev: The device to free
370 void mcb_free_dev(struct mcb_device *dev)
374 EXPORT_SYMBOL_GPL(mcb_free_dev);
376 static int __mcb_bus_add_devices(struct device *dev, void *data)
378 struct mcb_device *mdev = to_mcb_device(dev);
384 retval = device_attach(dev);
386 dev_err(dev, "Error adding device (%d)\n", retval);
388 mdev->is_added = true;
393 static int __mcb_bus_add_child(struct device *dev, void *data)
395 struct mcb_device *mdev = to_mcb_device(dev);
396 struct mcb_bus *child;
398 BUG_ON(!mdev->is_added);
399 child = mdev->subordinate;
402 mcb_bus_add_devices(child);
408 * mcb_bus_add_devices() - Add devices in the bus' internal device list
409 * @bus: The @mcb_bus we add the devices
411 * Add devices in the bus' internal device list to the system.
413 void mcb_bus_add_devices(const struct mcb_bus *bus)
415 bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_bus_add_devices);
416 bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_bus_add_child);
419 EXPORT_SYMBOL_GPL(mcb_bus_add_devices);
422 * mcb_request_mem() - Request memory
423 * @dev: The @mcb_device the memory is for
424 * @name: The name for the memory reference.
426 * Request memory for a @mcb_device. If @name is NULL the driver name will
429 struct resource *mcb_request_mem(struct mcb_device *dev, const char *name)
431 struct resource *mem;
435 name = dev->dev.driver->name;
437 size = resource_size(&dev->mem);
439 mem = request_mem_region(dev->mem.start, size, name);
441 return ERR_PTR(-EBUSY);
445 EXPORT_SYMBOL_GPL(mcb_request_mem);
448 * mcb_release_mem() - Release memory requested by device
449 * @dev: The @mcb_device that requested the memory
451 * Release memory that was prior requested via @mcb_request_mem().
453 void mcb_release_mem(struct resource *mem)
457 size = resource_size(mem);
458 release_mem_region(mem->start, size);
460 EXPORT_SYMBOL_GPL(mcb_release_mem);
462 static int __mcb_get_irq(struct mcb_device *dev)
464 struct resource *irq = &dev->irq;
470 * mcb_get_irq() - Get device's IRQ number
471 * @dev: The @mcb_device the IRQ is for
473 * Get the IRQ number of a given @mcb_device.
475 int mcb_get_irq(struct mcb_device *dev)
477 struct mcb_bus *bus = dev->bus;
480 return bus->get_irq(dev);
482 return __mcb_get_irq(dev);
484 EXPORT_SYMBOL_GPL(mcb_get_irq);
486 static int mcb_init(void)
488 return bus_register(&mcb_bus_type);
491 static void mcb_exit(void)
493 ida_destroy(&mcb_ida);
494 bus_unregister(&mcb_bus_type);
497 /* mcb must be initialized after PCI but before the chameleon drivers.
498 * That means we must use some initcall between subsys_initcall and
501 fs_initcall(mcb_init);
502 module_exit(mcb_exit);
504 MODULE_DESCRIPTION("MEN Chameleon Bus Driver");
505 MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>");
506 MODULE_LICENSE("GPL v2");