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