ipack.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. {
  179. int bus_nr;
  180. struct ipack_bus_device *bus;
  181. bus = kzalloc(sizeof(struct ipack_bus_device), GFP_KERNEL);
  182. if (!bus)
  183. return NULL;
  184. bus_nr = ida_simple_get(&ipack_ida, 0, 0, GFP_KERNEL);
  185. if (bus_nr < 0) {
  186. kfree(bus);
  187. return NULL;
  188. }
  189. bus->bus_nr = bus_nr;
  190. bus->parent = parent;
  191. bus->slots = slots;
  192. bus->ops = ops;
  193. return bus;
  194. }
  195. EXPORT_SYMBOL_GPL(ipack_bus_register);
  196. static int ipack_unregister_bus_member(struct device *dev, void *data)
  197. {
  198. struct ipack_device *idev = to_ipack_dev(dev);
  199. struct ipack_bus_device *bus = data;
  200. if (idev->bus == bus)
  201. ipack_device_del(idev);
  202. return 1;
  203. }
  204. int ipack_bus_unregister(struct ipack_bus_device *bus)
  205. {
  206. bus_for_each_dev(&ipack_bus_type, NULL, bus,
  207. ipack_unregister_bus_member);
  208. ida_simple_remove(&ipack_ida, bus->bus_nr);
  209. kfree(bus);
  210. return 0;
  211. }
  212. EXPORT_SYMBOL_GPL(ipack_bus_unregister);
  213. int ipack_driver_register(struct ipack_driver *edrv, struct module *owner,
  214. const char *name)
  215. {
  216. edrv->driver.owner = owner;
  217. edrv->driver.name = name;
  218. edrv->driver.bus = &ipack_bus_type;
  219. return driver_register(&edrv->driver);
  220. }
  221. EXPORT_SYMBOL_GPL(ipack_driver_register);
  222. void ipack_driver_unregister(struct ipack_driver *edrv)
  223. {
  224. driver_unregister(&edrv->driver);
  225. }
  226. EXPORT_SYMBOL_GPL(ipack_driver_unregister);
  227. static u16 ipack_crc_byte(u16 crc, u8 c)
  228. {
  229. int i;
  230. crc ^= c << 8;
  231. for (i = 0; i < 8; i++)
  232. crc = (crc << 1) ^ ((crc & 0x8000) ? 0x1021 : 0);
  233. return crc;
  234. }
  235. /*
  236. * The algorithm in lib/crc-ccitt.c does not seem to apply since it uses the
  237. * opposite bit ordering.
  238. */
  239. static u8 ipack_calc_crc1(struct ipack_device *dev)
  240. {
  241. u8 c;
  242. u16 crc;
  243. unsigned int i;
  244. crc = 0xffff;
  245. for (i = 0; i < dev->id_avail; i++) {
  246. c = (i != 11) ? dev->id[i] : 0;
  247. crc = ipack_crc_byte(crc, c);
  248. }
  249. crc = ~crc;
  250. return crc & 0xff;
  251. }
  252. static u16 ipack_calc_crc2(struct ipack_device *dev)
  253. {
  254. u8 c;
  255. u16 crc;
  256. unsigned int i;
  257. crc = 0xffff;
  258. for (i = 0; i < dev->id_avail; i++) {
  259. c = ((i != 0x18) && (i != 0x19)) ? dev->id[i] : 0;
  260. crc = ipack_crc_byte(crc, c);
  261. }
  262. crc = ~crc;
  263. return crc;
  264. }
  265. static void ipack_parse_id1(struct ipack_device *dev)
  266. {
  267. u8 *id = dev->id;
  268. u8 crc;
  269. dev->id_vendor = id[4];
  270. dev->id_device = id[5];
  271. dev->speed_8mhz = 1;
  272. dev->speed_32mhz = (id[7] == 'H');
  273. crc = ipack_calc_crc1(dev);
  274. dev->id_crc_correct = (crc == id[11]);
  275. if (!dev->id_crc_correct) {
  276. dev_warn(&dev->dev, "ID CRC invalid found 0x%x, expected 0x%x.\n",
  277. id[11], crc);
  278. }
  279. }
  280. static void ipack_parse_id2(struct ipack_device *dev)
  281. {
  282. __be16 *id = (__be16 *) dev->id;
  283. u16 flags, crc;
  284. dev->id_vendor = ((be16_to_cpu(id[3]) & 0xff) << 16)
  285. + be16_to_cpu(id[4]);
  286. dev->id_device = be16_to_cpu(id[5]);
  287. flags = be16_to_cpu(id[10]);
  288. dev->speed_8mhz = !!(flags & 2);
  289. dev->speed_32mhz = !!(flags & 4);
  290. crc = ipack_calc_crc2(dev);
  291. dev->id_crc_correct = (crc == be16_to_cpu(id[12]));
  292. if (!dev->id_crc_correct) {
  293. dev_warn(&dev->dev, "ID CRC invalid found 0x%x, expected 0x%x.\n",
  294. id[11], crc);
  295. }
  296. }
  297. static int ipack_device_read_id(struct ipack_device *dev)
  298. {
  299. u8 __iomem *idmem;
  300. int i;
  301. int ret = 0;
  302. idmem = ioremap(dev->region[IPACK_ID_SPACE].start,
  303. dev->region[IPACK_ID_SPACE].size);
  304. if (!idmem) {
  305. dev_err(&dev->dev, "error mapping memory\n");
  306. return -ENOMEM;
  307. }
  308. /* Determine ID PROM Data Format. If we find the ids "IPAC" or "IPAH"
  309. * we are dealing with a IndustryPack format 1 device. If we detect
  310. * "VITA4 " (16 bit big endian formatted) we are dealing with a
  311. * IndustryPack format 2 device */
  312. if ((ioread8(idmem + 1) == 'I') &&
  313. (ioread8(idmem + 3) == 'P') &&
  314. (ioread8(idmem + 5) == 'A') &&
  315. ((ioread8(idmem + 7) == 'C') ||
  316. (ioread8(idmem + 7) == 'H'))) {
  317. dev->id_format = IPACK_ID_VERSION_1;
  318. dev->id_avail = ioread8(idmem + 0x15);
  319. if ((dev->id_avail < 0x0c) || (dev->id_avail > 0x40)) {
  320. dev_warn(&dev->dev, "invalid id size");
  321. dev->id_avail = 0x0c;
  322. }
  323. } else if ((ioread8(idmem + 0) == 'I') &&
  324. (ioread8(idmem + 1) == 'V') &&
  325. (ioread8(idmem + 2) == 'A') &&
  326. (ioread8(idmem + 3) == 'T') &&
  327. (ioread8(idmem + 4) == ' ') &&
  328. (ioread8(idmem + 5) == '4')) {
  329. dev->id_format = IPACK_ID_VERSION_2;
  330. dev->id_avail = ioread16be(idmem + 0x16);
  331. if ((dev->id_avail < 0x1a) || (dev->id_avail > 0x40)) {
  332. dev_warn(&dev->dev, "invalid id size");
  333. dev->id_avail = 0x1a;
  334. }
  335. } else {
  336. dev->id_format = IPACK_ID_VERSION_INVALID;
  337. dev->id_avail = 0;
  338. }
  339. if (!dev->id_avail) {
  340. ret = -ENODEV;
  341. goto out;
  342. }
  343. /* Obtain the amount of memory required to store a copy of the complete
  344. * ID ROM contents */
  345. dev->id = kmalloc(dev->id_avail, GFP_KERNEL);
  346. if (!dev->id) {
  347. dev_err(&dev->dev, "dev->id alloc failed.\n");
  348. ret = -ENOMEM;
  349. goto out;
  350. }
  351. for (i = 0; i < dev->id_avail; i++) {
  352. if (dev->id_format == IPACK_ID_VERSION_1)
  353. dev->id[i] = ioread8(idmem + (i << 1) + 1);
  354. else
  355. dev->id[i] = ioread8(idmem + i);
  356. }
  357. /* now we can finally work with the copy */
  358. switch (dev->id_format) {
  359. case IPACK_ID_VERSION_1:
  360. ipack_parse_id1(dev);
  361. break;
  362. case IPACK_ID_VERSION_2:
  363. ipack_parse_id2(dev);
  364. break;
  365. }
  366. out:
  367. iounmap(idmem);
  368. return ret;
  369. }
  370. int ipack_device_init(struct ipack_device *dev)
  371. {
  372. int ret;
  373. dev->dev.bus = &ipack_bus_type;
  374. dev->dev.release = ipack_device_release;
  375. dev->dev.parent = dev->bus->parent;
  376. dev_set_name(&dev->dev,
  377. "ipack-dev.%u.%u", dev->bus->bus_nr, dev->slot);
  378. device_initialize(&dev->dev);
  379. if (dev->bus->ops->set_clockrate(dev, 8))
  380. dev_warn(&dev->dev, "failed to switch to 8 MHz operation for reading of device ID.\n");
  381. if (dev->bus->ops->reset_timeout(dev))
  382. dev_warn(&dev->dev, "failed to reset potential timeout.");
  383. ret = ipack_device_read_id(dev);
  384. if (ret < 0) {
  385. dev_err(&dev->dev, "error reading device id section.\n");
  386. return ret;
  387. }
  388. /* if the device supports 32 MHz operation, use it. */
  389. if (dev->speed_32mhz) {
  390. ret = dev->bus->ops->set_clockrate(dev, 32);
  391. if (ret < 0)
  392. dev_err(&dev->dev, "failed to switch to 32 MHz operation.\n");
  393. }
  394. return 0;
  395. }
  396. EXPORT_SYMBOL_GPL(ipack_device_init);
  397. int ipack_device_add(struct ipack_device *dev)
  398. {
  399. return device_add(&dev->dev);
  400. }
  401. EXPORT_SYMBOL_GPL(ipack_device_add);
  402. void ipack_device_del(struct ipack_device *dev)
  403. {
  404. device_del(&dev->dev);
  405. ipack_put_device(dev);
  406. }
  407. EXPORT_SYMBOL_GPL(ipack_device_del);
  408. void ipack_get_device(struct ipack_device *dev)
  409. {
  410. get_device(&dev->dev);
  411. }
  412. EXPORT_SYMBOL_GPL(ipack_get_device);
  413. void ipack_put_device(struct ipack_device *dev)
  414. {
  415. put_device(&dev->dev);
  416. }
  417. EXPORT_SYMBOL_GPL(ipack_put_device);
  418. static int __init ipack_init(void)
  419. {
  420. ida_init(&ipack_ida);
  421. return bus_register(&ipack_bus_type);
  422. }
  423. static void __exit ipack_exit(void)
  424. {
  425. bus_unregister(&ipack_bus_type);
  426. ida_destroy(&ipack_ida);
  427. }
  428. module_init(ipack_init);
  429. module_exit(ipack_exit);
  430. MODULE_AUTHOR("Samuel Iglesias Gonsalvez <siglesias@igalia.com>");
  431. MODULE_LICENSE("GPL");
  432. MODULE_DESCRIPTION("Industry-pack bus core");