ipack.c 11 KB

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