ipack.c 12 KB

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