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