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 | |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 12 | #include <linux/module.h> |
Samuel Iglesias Gonsalvez | ec44033 | 2012-05-18 11:10:05 +0200 | [diff] [blame] | 13 | #include <linux/slab.h> |
Samuel Iglesias Gonsalvez | 3b86bb2 | 2012-05-25 10:03:01 +0200 | [diff] [blame] | 14 | #include <linux/idr.h> |
Samuel Iglesias Gonsálvez | b9f789f | 2012-09-05 09:08:17 +0200 | [diff] [blame] | 15 | #include <asm/io.h> |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 16 | #include "ipack.h" |
| 17 | |
| 18 | #define to_ipack_dev(device) container_of(device, struct ipack_device, dev) |
| 19 | #define to_ipack_driver(drv) container_of(drv, struct ipack_driver, driver) |
| 20 | |
Samuel Iglesias Gonsalvez | 3b86bb2 | 2012-05-25 10:03:01 +0200 | [diff] [blame] | 21 | static DEFINE_IDA(ipack_ida); |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 22 | |
Samuel Iglesias Gonsalvez | ec44033 | 2012-05-18 11:10:05 +0200 | [diff] [blame] | 23 | static void ipack_device_release(struct device *dev) |
| 24 | { |
| 25 | struct ipack_device *device = to_ipack_dev(dev); |
Jens Taprogge | 187e478 | 2012-09-04 17:01:14 +0200 | [diff] [blame] | 26 | kfree(device->id); |
Samuel Iglesias Gonsalvez | ec44033 | 2012-05-18 11:10:05 +0200 | [diff] [blame] | 27 | kfree(device); |
| 28 | } |
| 29 | |
Jens Taprogge | 4aa09d4 | 2012-09-04 17:01:19 +0200 | [diff] [blame] | 30 | static inline const struct ipack_device_id * |
| 31 | ipack_match_one_device(const struct ipack_device_id *id, |
| 32 | const struct ipack_device *device) |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 33 | { |
Jens Taprogge | 5948ae2 | 2012-09-07 10:29:19 +0200 | [diff] [blame] | 34 | if ((id->format == IPACK_ANY_FORMAT || |
| 35 | id->format == device->id_format) && |
Jens Taprogge | 4aa09d4 | 2012-09-04 17:01:19 +0200 | [diff] [blame] | 36 | (id->vendor == IPACK_ANY_ID || id->vendor == device->id_vendor) && |
| 37 | (id->device == IPACK_ANY_ID || id->device == device->id_device)) |
| 38 | return id; |
| 39 | return NULL; |
| 40 | } |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 41 | |
Jens Taprogge | 4aa09d4 | 2012-09-04 17:01:19 +0200 | [diff] [blame] | 42 | static const struct ipack_device_id * |
| 43 | ipack_match_id(const struct ipack_device_id *ids, struct ipack_device *idev) |
| 44 | { |
| 45 | if (ids) { |
| 46 | while (ids->vendor || ids->device) { |
| 47 | if (ipack_match_one_device(ids, idev)) |
| 48 | return ids; |
| 49 | ids++; |
| 50 | } |
| 51 | } |
| 52 | return NULL; |
| 53 | } |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 54 | |
Jens Taprogge | 4aa09d4 | 2012-09-04 17:01:19 +0200 | [diff] [blame] | 55 | static int ipack_bus_match(struct device *dev, struct device_driver *drv) |
| 56 | { |
| 57 | struct ipack_device *idev = to_ipack_dev(dev); |
| 58 | struct ipack_driver *idrv = to_ipack_driver(drv); |
| 59 | const struct ipack_device_id *found_id; |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 60 | |
Jens Taprogge | 4aa09d4 | 2012-09-04 17:01:19 +0200 | [diff] [blame] | 61 | found_id = ipack_match_id(idrv->id_table, idev); |
Jens Taprogge | 3bea7fc | 2012-09-11 13:34:59 +0200 | [diff] [blame] | 62 | return found_id ? 1 : 0; |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static int ipack_bus_probe(struct device *device) |
| 66 | { |
| 67 | struct ipack_device *dev = to_ipack_dev(device); |
Jens Taprogge | 3bea7fc | 2012-09-11 13:34:59 +0200 | [diff] [blame] | 68 | struct ipack_driver *drv = to_ipack_driver(device->driver); |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 69 | |
Jens Taprogge | 3bea7fc | 2012-09-11 13:34:59 +0200 | [diff] [blame] | 70 | if (!drv->ops->probe) |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 71 | return -EINVAL; |
| 72 | |
Jens Taprogge | 3bea7fc | 2012-09-11 13:34:59 +0200 | [diff] [blame] | 73 | return drv->ops->probe(dev); |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static int ipack_bus_remove(struct device *device) |
| 77 | { |
| 78 | struct ipack_device *dev = to_ipack_dev(device); |
Jens Taprogge | 3bea7fc | 2012-09-11 13:34:59 +0200 | [diff] [blame] | 79 | struct ipack_driver *drv = to_ipack_driver(device->driver); |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 80 | |
Jens Taprogge | 3bea7fc | 2012-09-11 13:34:59 +0200 | [diff] [blame] | 81 | if (!drv->ops->remove) |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 82 | return -EINVAL; |
| 83 | |
Jens Taprogge | 3bea7fc | 2012-09-11 13:34:59 +0200 | [diff] [blame] | 84 | drv->ops->remove(dev); |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 85 | return 0; |
| 86 | } |
| 87 | |
Jens Taprogge | 35eb97b | 2012-09-04 17:01:20 +0200 | [diff] [blame] | 88 | #ifdef CONFIG_HOTPLUG |
| 89 | |
| 90 | static int ipack_uevent(struct device *dev, struct kobj_uevent_env *env) |
| 91 | { |
| 92 | struct ipack_device *idev; |
| 93 | |
| 94 | if (!dev) |
| 95 | return -ENODEV; |
| 96 | |
| 97 | idev = to_ipack_dev(dev); |
| 98 | |
| 99 | if (add_uevent_var(env, |
| 100 | "MODALIAS=ipack:f%02Xv%08Xd%08X", idev->id_format, |
| 101 | idev->id_vendor, idev->id_device)) |
| 102 | return -ENOMEM; |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | #else /* !CONFIG_HOTPLUG */ |
| 108 | |
| 109 | #define ipack_uevent NULL |
| 110 | |
| 111 | #endif /* !CONFIG_HOTPLUG */ |
| 112 | |
Jens Taprogge | e8ed327 | 2012-09-04 17:01:15 +0200 | [diff] [blame] | 113 | #define ipack_device_attr(field, format_string) \ |
| 114 | static ssize_t \ |
| 115 | field##_show(struct device *dev, struct device_attribute *attr, \ |
| 116 | char *buf) \ |
| 117 | { \ |
| 118 | struct ipack_device *idev = to_ipack_dev(dev); \ |
| 119 | return sprintf(buf, format_string, idev->field); \ |
| 120 | } |
| 121 | |
Jens Taprogge | 5d72c848 | 2012-09-04 17:01:21 +0200 | [diff] [blame] | 122 | static ssize_t id_show(struct device *dev, |
| 123 | struct device_attribute *attr, char *buf) |
| 124 | { |
| 125 | unsigned int i, c, l, s; |
| 126 | struct ipack_device *idev = to_ipack_dev(dev); |
| 127 | |
| 128 | |
| 129 | switch (idev->id_format) { |
| 130 | case IPACK_ID_VERSION_1: |
| 131 | l = 0x7; s = 1; break; |
| 132 | case IPACK_ID_VERSION_2: |
| 133 | l = 0xf; s = 2; break; |
| 134 | default: |
| 135 | return -EIO; |
| 136 | } |
| 137 | c = 0; |
| 138 | for (i = 0; i < idev->id_avail; i++) { |
| 139 | if (i > 0) { |
| 140 | if ((i & l) == 0) |
| 141 | buf[c++] = '\n'; |
| 142 | else if ((i & s) == 0) |
| 143 | buf[c++] = ' '; |
| 144 | } |
| 145 | sprintf(&buf[c], "%02x", idev->id[i]); |
| 146 | c += 2; |
| 147 | } |
| 148 | buf[c++] = '\n'; |
| 149 | return c; |
| 150 | } |
| 151 | |
Jens Taprogge | e8ed327 | 2012-09-04 17:01:15 +0200 | [diff] [blame] | 152 | static ssize_t |
| 153 | id_vendor_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 154 | { |
| 155 | struct ipack_device *idev = to_ipack_dev(dev); |
| 156 | switch (idev->id_format) { |
| 157 | case IPACK_ID_VERSION_1: |
| 158 | return sprintf(buf, "0x%02x\n", idev->id_vendor); |
| 159 | case IPACK_ID_VERSION_2: |
| 160 | return sprintf(buf, "0x%06x\n", idev->id_vendor); |
| 161 | default: |
| 162 | return -EIO; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | static ssize_t |
| 167 | id_device_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 168 | { |
| 169 | struct ipack_device *idev = to_ipack_dev(dev); |
| 170 | switch (idev->id_format) { |
| 171 | case IPACK_ID_VERSION_1: |
| 172 | return sprintf(buf, "0x%02x\n", idev->id_device); |
| 173 | case IPACK_ID_VERSION_2: |
| 174 | return sprintf(buf, "0x%04x\n", idev->id_device); |
| 175 | default: |
| 176 | return -EIO; |
| 177 | } |
| 178 | } |
| 179 | |
Jens Taprogge | 35eb97b | 2012-09-04 17:01:20 +0200 | [diff] [blame] | 180 | static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, |
| 181 | char *buf) |
| 182 | { |
| 183 | struct ipack_device *idev = to_ipack_dev(dev); |
| 184 | |
| 185 | return sprintf(buf, "ipac:f%02Xv%08Xd%08X", idev->id_format, |
| 186 | idev->id_vendor, idev->id_device); |
| 187 | } |
| 188 | |
Jens Taprogge | e8ed327 | 2012-09-04 17:01:15 +0200 | [diff] [blame] | 189 | ipack_device_attr(id_format, "0x%hhu\n"); |
| 190 | |
| 191 | static struct device_attribute ipack_dev_attrs[] = { |
Jens Taprogge | 5d72c848 | 2012-09-04 17:01:21 +0200 | [diff] [blame] | 192 | __ATTR_RO(id), |
Jens Taprogge | e8ed327 | 2012-09-04 17:01:15 +0200 | [diff] [blame] | 193 | __ATTR_RO(id_device), |
| 194 | __ATTR_RO(id_format), |
| 195 | __ATTR_RO(id_vendor), |
Jens Taprogge | 35eb97b | 2012-09-04 17:01:20 +0200 | [diff] [blame] | 196 | __ATTR_RO(modalias), |
Jens Taprogge | e8ed327 | 2012-09-04 17:01:15 +0200 | [diff] [blame] | 197 | }; |
| 198 | |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 199 | static struct bus_type ipack_bus_type = { |
Jens Taprogge | e8ed327 | 2012-09-04 17:01:15 +0200 | [diff] [blame] | 200 | .name = "ipack", |
| 201 | .probe = ipack_bus_probe, |
| 202 | .match = ipack_bus_match, |
| 203 | .remove = ipack_bus_remove, |
| 204 | .dev_attrs = ipack_dev_attrs, |
Jens Taprogge | 35eb97b | 2012-09-04 17:01:20 +0200 | [diff] [blame] | 205 | .uevent = ipack_uevent, |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 206 | }; |
| 207 | |
Samuel Iglesias Gonsalvez | ec44033 | 2012-05-18 11:10:05 +0200 | [diff] [blame] | 208 | struct ipack_bus_device *ipack_bus_register(struct device *parent, int slots, |
Stephen Hemminger | 9869a937 | 2012-09-10 11:14:01 -0700 | [diff] [blame] | 209 | const struct ipack_bus_ops *ops) |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 210 | { |
| 211 | int bus_nr; |
Samuel Iglesias Gonsalvez | ec44033 | 2012-05-18 11:10:05 +0200 | [diff] [blame] | 212 | struct ipack_bus_device *bus; |
| 213 | |
| 214 | bus = kzalloc(sizeof(struct ipack_bus_device), GFP_KERNEL); |
| 215 | if (!bus) |
| 216 | return NULL; |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 217 | |
Samuel Iglesias Gonsalvez | 3b86bb2 | 2012-05-25 10:03:01 +0200 | [diff] [blame] | 218 | bus_nr = ida_simple_get(&ipack_ida, 0, 0, GFP_KERNEL); |
Samuel Iglesias Gonsalvez | ec44033 | 2012-05-18 11:10:05 +0200 | [diff] [blame] | 219 | if (bus_nr < 0) { |
| 220 | kfree(bus); |
| 221 | return NULL; |
| 222 | } |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 223 | |
| 224 | bus->bus_nr = bus_nr; |
Samuel Iglesias Gonsalvez | ec44033 | 2012-05-18 11:10:05 +0200 | [diff] [blame] | 225 | bus->parent = parent; |
| 226 | bus->slots = slots; |
| 227 | bus->ops = ops; |
| 228 | return bus; |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 229 | } |
| 230 | EXPORT_SYMBOL_GPL(ipack_bus_register); |
| 231 | |
| 232 | int ipack_bus_unregister(struct ipack_bus_device *bus) |
| 233 | { |
Samuel Iglesias Gonsalvez | 3b86bb2 | 2012-05-25 10:03:01 +0200 | [diff] [blame] | 234 | ida_simple_remove(&ipack_ida, bus->bus_nr); |
Samuel Iglesias Gonsalvez | ec44033 | 2012-05-18 11:10:05 +0200 | [diff] [blame] | 235 | kfree(bus); |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 236 | return 0; |
| 237 | } |
| 238 | EXPORT_SYMBOL_GPL(ipack_bus_unregister); |
| 239 | |
Samuel Iglesias Gonsalvez | ec44033 | 2012-05-18 11:10:05 +0200 | [diff] [blame] | 240 | int ipack_driver_register(struct ipack_driver *edrv, struct module *owner, |
Stephen Hemminger | 9869a937 | 2012-09-10 11:14:01 -0700 | [diff] [blame] | 241 | const char *name) |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 242 | { |
Samuel Iglesias Gonsalvez | ec44033 | 2012-05-18 11:10:05 +0200 | [diff] [blame] | 243 | edrv->driver.owner = owner; |
| 244 | edrv->driver.name = name; |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 245 | edrv->driver.bus = &ipack_bus_type; |
| 246 | return driver_register(&edrv->driver); |
| 247 | } |
| 248 | EXPORT_SYMBOL_GPL(ipack_driver_register); |
| 249 | |
| 250 | void ipack_driver_unregister(struct ipack_driver *edrv) |
| 251 | { |
| 252 | driver_unregister(&edrv->driver); |
| 253 | } |
| 254 | EXPORT_SYMBOL_GPL(ipack_driver_unregister); |
| 255 | |
Jens Taprogge | e8ed327 | 2012-09-04 17:01:15 +0200 | [diff] [blame] | 256 | static void ipack_parse_id1(struct ipack_device *dev) |
| 257 | { |
| 258 | u8 *id = dev->id; |
| 259 | |
| 260 | dev->id_vendor = id[4]; |
| 261 | dev->id_device = id[5]; |
Jens Taprogge | 0b0f3a1 | 2012-09-11 13:34:57 +0200 | [diff] [blame] | 262 | dev->speed_8mhz = 1; |
| 263 | dev->speed_32mhz = (id[7] == 'H'); |
Jens Taprogge | e8ed327 | 2012-09-04 17:01:15 +0200 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | static void ipack_parse_id2(struct ipack_device *dev) |
| 267 | { |
| 268 | __be16 *id = (__be16 *) dev->id; |
Jens Taprogge | 0b0f3a1 | 2012-09-11 13:34:57 +0200 | [diff] [blame] | 269 | u16 flags; |
Jens Taprogge | e8ed327 | 2012-09-04 17:01:15 +0200 | [diff] [blame] | 270 | |
| 271 | dev->id_vendor = ((be16_to_cpu(id[3]) & 0xff) << 16) |
| 272 | + be16_to_cpu(id[4]); |
| 273 | dev->id_device = be16_to_cpu(id[5]); |
Jens Taprogge | 0b0f3a1 | 2012-09-11 13:34:57 +0200 | [diff] [blame] | 274 | flags = be16_to_cpu(id[10]); |
| 275 | dev->speed_8mhz = !!(flags & 2); |
| 276 | dev->speed_32mhz = !!(flags & 4); |
Jens Taprogge | e8ed327 | 2012-09-04 17:01:15 +0200 | [diff] [blame] | 277 | } |
| 278 | |
Jens Taprogge | 187e478 | 2012-09-04 17:01:14 +0200 | [diff] [blame] | 279 | static int ipack_device_read_id(struct ipack_device *dev) |
| 280 | { |
| 281 | u8 __iomem *idmem; |
| 282 | int i; |
| 283 | int ret = 0; |
| 284 | |
| 285 | ret = dev->bus->ops->map_space(dev, 0, IPACK_ID_SPACE); |
| 286 | if (ret) { |
| 287 | dev_err(&dev->dev, "error mapping memory\n"); |
| 288 | return ret; |
| 289 | } |
| 290 | idmem = dev->id_space.address; |
| 291 | |
| 292 | /* Determine ID PROM Data Format. If we find the ids "IPAC" or "IPAH" |
| 293 | * we are dealing with a IndustryPack format 1 device. If we detect |
| 294 | * "VITA4 " (16 bit big endian formatted) we are dealing with a |
| 295 | * IndustryPack format 2 device */ |
| 296 | if ((ioread8(idmem + 1) == 'I') && |
| 297 | (ioread8(idmem + 3) == 'P') && |
| 298 | (ioread8(idmem + 5) == 'A') && |
| 299 | ((ioread8(idmem + 7) == 'C') || |
| 300 | (ioread8(idmem + 7) == 'H'))) { |
| 301 | dev->id_format = IPACK_ID_VERSION_1; |
| 302 | dev->id_avail = ioread8(idmem + 0x15); |
| 303 | if ((dev->id_avail < 0x0c) || (dev->id_avail > 0x40)) { |
| 304 | dev_warn(&dev->dev, "invalid id size"); |
| 305 | dev->id_avail = 0x0c; |
| 306 | } |
| 307 | } else if ((ioread8(idmem + 0) == 'I') && |
| 308 | (ioread8(idmem + 1) == 'V') && |
| 309 | (ioread8(idmem + 2) == 'A') && |
| 310 | (ioread8(idmem + 3) == 'T') && |
| 311 | (ioread8(idmem + 4) == ' ') && |
| 312 | (ioread8(idmem + 5) == '4')) { |
| 313 | dev->id_format = IPACK_ID_VERSION_2; |
| 314 | dev->id_avail = ioread16be(idmem + 0x16); |
| 315 | if ((dev->id_avail < 0x1a) || (dev->id_avail > 0x40)) { |
| 316 | dev_warn(&dev->dev, "invalid id size"); |
| 317 | dev->id_avail = 0x1a; |
| 318 | } |
| 319 | } else { |
| 320 | dev->id_format = IPACK_ID_VERSION_INVALID; |
| 321 | dev->id_avail = 0; |
| 322 | } |
| 323 | |
| 324 | if (!dev->id_avail) { |
| 325 | ret = -ENODEV; |
| 326 | goto out; |
| 327 | } |
| 328 | |
| 329 | /* Obtain the amount of memory required to store a copy of the complete |
| 330 | * ID ROM contents */ |
| 331 | dev->id = kmalloc(dev->id_avail, GFP_KERNEL); |
| 332 | if (!dev->id) { |
| 333 | dev_err(&dev->dev, "dev->id alloc failed.\n"); |
| 334 | ret = -ENOMEM; |
| 335 | goto out; |
| 336 | } |
| 337 | for (i = 0; i < dev->id_avail; i++) { |
| 338 | if (dev->id_format == IPACK_ID_VERSION_1) |
| 339 | dev->id[i] = ioread8(idmem + (i << 1) + 1); |
| 340 | else |
| 341 | dev->id[i] = ioread8(idmem + i); |
| 342 | } |
| 343 | |
Jens Taprogge | e8ed327 | 2012-09-04 17:01:15 +0200 | [diff] [blame] | 344 | /* now we can finally work with the copy */ |
| 345 | switch (dev->id_format) { |
| 346 | case IPACK_ID_VERSION_1: |
| 347 | ipack_parse_id1(dev); |
| 348 | break; |
| 349 | case IPACK_ID_VERSION_2: |
| 350 | ipack_parse_id2(dev); |
| 351 | break; |
| 352 | } |
| 353 | |
Jens Taprogge | 187e478 | 2012-09-04 17:01:14 +0200 | [diff] [blame] | 354 | out: |
| 355 | dev->bus->ops->unmap_space(dev, IPACK_ID_SPACE); |
| 356 | |
| 357 | return ret; |
| 358 | } |
| 359 | |
Samuel Iglesias Gonsalvez | ec44033 | 2012-05-18 11:10:05 +0200 | [diff] [blame] | 360 | struct ipack_device *ipack_device_register(struct ipack_bus_device *bus, |
| 361 | int slot, int irqv) |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 362 | { |
| 363 | int ret; |
Samuel Iglesias Gonsalvez | ec44033 | 2012-05-18 11:10:05 +0200 | [diff] [blame] | 364 | struct ipack_device *dev; |
| 365 | |
| 366 | dev = kzalloc(sizeof(struct ipack_device), GFP_KERNEL); |
| 367 | if (!dev) |
| 368 | return NULL; |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 369 | |
| 370 | dev->dev.bus = &ipack_bus_type; |
| 371 | dev->dev.release = ipack_device_release; |
Samuel Iglesias Gonsalvez | ec44033 | 2012-05-18 11:10:05 +0200 | [diff] [blame] | 372 | dev->dev.parent = bus->parent; |
| 373 | dev->slot = slot; |
| 374 | dev->bus_nr = bus->bus_nr; |
| 375 | dev->irq = irqv; |
| 376 | dev->bus = bus; |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 377 | dev_set_name(&dev->dev, |
Samuel Iglesias Gonsalvez | ec44033 | 2012-05-18 11:10:05 +0200 | [diff] [blame] | 378 | "ipack-dev.%u.%u", dev->bus_nr, dev->slot); |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 379 | |
Jens Taprogge | 07766ab0 | 2012-09-11 13:35:01 +0200 | [diff] [blame^] | 380 | if (bus->ops->set_clockrate(dev, 8)) |
| 381 | dev_warn(&dev->dev, "failed to switch to 8 MHz operation for reading of device ID.\n"); |
| 382 | |
Jens Taprogge | 187e478 | 2012-09-04 17:01:14 +0200 | [diff] [blame] | 383 | ret = ipack_device_read_id(dev); |
| 384 | if (ret < 0) { |
| 385 | dev_err(&dev->dev, "error reading device id section.\n"); |
| 386 | kfree(dev); |
| 387 | return NULL; |
| 388 | } |
| 389 | |
Jens Taprogge | 90cb619 | 2012-09-11 13:34:58 +0200 | [diff] [blame] | 390 | /* if the device supports 32 MHz operation, use it. */ |
Jens Taprogge | 07766ab0 | 2012-09-11 13:35:01 +0200 | [diff] [blame^] | 391 | if (dev->speed_32mhz) { |
| 392 | ret = bus->ops->set_clockrate(dev, 32); |
| 393 | if (ret < 0) |
| 394 | dev_err(&dev->dev, "failed to switch to 32 MHz operation.\n"); |
| 395 | } |
Jens Taprogge | 90cb619 | 2012-09-11 13:34:58 +0200 | [diff] [blame] | 396 | |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 397 | ret = device_register(&dev->dev); |
| 398 | if (ret < 0) { |
Jens Taprogge | 187e478 | 2012-09-04 17:01:14 +0200 | [diff] [blame] | 399 | kfree(dev->id); |
Samuel Iglesias Gonsalvez | ec44033 | 2012-05-18 11:10:05 +0200 | [diff] [blame] | 400 | kfree(dev); |
| 401 | return NULL; |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 402 | } |
| 403 | |
Samuel Iglesias Gonsalvez | ec44033 | 2012-05-18 11:10:05 +0200 | [diff] [blame] | 404 | return dev; |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 405 | } |
| 406 | EXPORT_SYMBOL_GPL(ipack_device_register); |
| 407 | |
| 408 | void ipack_device_unregister(struct ipack_device *dev) |
| 409 | { |
| 410 | device_unregister(&dev->dev); |
| 411 | } |
| 412 | EXPORT_SYMBOL_GPL(ipack_device_unregister); |
| 413 | |
| 414 | static int __init ipack_init(void) |
| 415 | { |
Samuel Iglesias Gonsalvez | 3b86bb2 | 2012-05-25 10:03:01 +0200 | [diff] [blame] | 416 | ida_init(&ipack_ida); |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 417 | return bus_register(&ipack_bus_type); |
| 418 | } |
| 419 | |
| 420 | static void __exit ipack_exit(void) |
| 421 | { |
| 422 | bus_unregister(&ipack_bus_type); |
Samuel Iglesias Gonsalvez | 3b86bb2 | 2012-05-25 10:03:01 +0200 | [diff] [blame] | 423 | ida_destroy(&ipack_ida); |
Samuel Iglesias Gonsalvez | d346587 | 2012-05-09 15:27:19 +0200 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | module_init(ipack_init); |
| 427 | module_exit(ipack_exit); |
| 428 | |
| 429 | MODULE_AUTHOR("Samuel Iglesias Gonsalvez <siglesias@igalia.com>"); |
| 430 | MODULE_LICENSE("GPL"); |
| 431 | MODULE_DESCRIPTION("Industry-pack bus core"); |