ipack.c 11 KB

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