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