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