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