fmc-core.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Copyright (C) 2012 CERN (www.cern.ch)
  3. * Author: Alessandro Rubini <rubini@gnudd.com>
  4. *
  5. * Released according to the GNU GPL, version 2 or any later version.
  6. *
  7. * This work is part of the White Rabbit project, a research effort led
  8. * by CERN, the European Institute for Nuclear Research.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/init.h>
  14. #include <linux/device.h>
  15. #include <linux/fmc.h>
  16. static int fmc_check_version(unsigned long version, const char *name)
  17. {
  18. if (__FMC_MAJOR(version) != FMC_MAJOR) {
  19. pr_err("%s: \"%s\" has wrong major (has %li, expected %i)\n",
  20. __func__, name, __FMC_MAJOR(version), FMC_MAJOR);
  21. return -EINVAL;
  22. }
  23. if (__FMC_MINOR(version) != FMC_MINOR)
  24. pr_info("%s: \"%s\" has wrong minor (has %li, expected %i)\n",
  25. __func__, name, __FMC_MINOR(version), FMC_MINOR);
  26. return 0;
  27. }
  28. static int fmc_uevent(struct device *dev, struct kobj_uevent_env *env)
  29. {
  30. /* struct fmc_device *fdev = to_fmc_device(dev); */
  31. /* FIXME: The MODALIAS */
  32. add_uevent_var(env, "MODALIAS=%s", "fmc");
  33. return 0;
  34. }
  35. static int fmc_probe(struct device *dev)
  36. {
  37. struct fmc_driver *fdrv = to_fmc_driver(dev->driver);
  38. struct fmc_device *fdev = to_fmc_device(dev);
  39. return fdrv->probe(fdev);
  40. }
  41. static int fmc_remove(struct device *dev)
  42. {
  43. struct fmc_driver *fdrv = to_fmc_driver(dev->driver);
  44. struct fmc_device *fdev = to_fmc_device(dev);
  45. return fdrv->remove(fdev);
  46. }
  47. static void fmc_shutdown(struct device *dev)
  48. {
  49. /* not implemented but mandatory */
  50. }
  51. static struct bus_type fmc_bus_type = {
  52. .name = "fmc",
  53. .match = fmc_match,
  54. .uevent = fmc_uevent,
  55. .probe = fmc_probe,
  56. .remove = fmc_remove,
  57. .shutdown = fmc_shutdown,
  58. };
  59. static void fmc_release(struct device *dev)
  60. {
  61. struct fmc_device *fmc = container_of(dev, struct fmc_device, dev);
  62. kfree(fmc);
  63. }
  64. /*
  65. * The eeprom is exported in sysfs, through a binary attribute
  66. */
  67. static ssize_t fmc_read_eeprom(struct file *file, struct kobject *kobj,
  68. struct bin_attribute *bin_attr,
  69. char *buf, loff_t off, size_t count)
  70. {
  71. struct device *dev;
  72. struct fmc_device *fmc;
  73. int eelen;
  74. dev = container_of(kobj, struct device, kobj);
  75. fmc = container_of(dev, struct fmc_device, dev);
  76. eelen = fmc->eeprom_len;
  77. if (off > eelen)
  78. return -ESPIPE;
  79. if (off == eelen)
  80. return 0; /* EOF */
  81. if (off + count > eelen)
  82. count = eelen - off;
  83. memcpy(buf, fmc->eeprom + off, count);
  84. return count;
  85. }
  86. static ssize_t fmc_write_eeprom(struct file *file, struct kobject *kobj,
  87. struct bin_attribute *bin_attr,
  88. char *buf, loff_t off, size_t count)
  89. {
  90. struct device *dev;
  91. struct fmc_device *fmc;
  92. dev = container_of(kobj, struct device, kobj);
  93. fmc = container_of(dev, struct fmc_device, dev);
  94. return fmc->op->write_ee(fmc, off, buf, count);
  95. }
  96. static struct bin_attribute fmc_eeprom_attr = {
  97. .attr = { .name = "eeprom", .mode = S_IRUGO | S_IWUSR, },
  98. .size = 8192, /* more or less standard */
  99. .read = fmc_read_eeprom,
  100. .write = fmc_write_eeprom,
  101. };
  102. /*
  103. * Functions for client modules follow
  104. */
  105. int fmc_driver_register(struct fmc_driver *drv)
  106. {
  107. if (fmc_check_version(drv->version, drv->driver.name))
  108. return -EINVAL;
  109. drv->driver.bus = &fmc_bus_type;
  110. return driver_register(&drv->driver);
  111. }
  112. EXPORT_SYMBOL(fmc_driver_register);
  113. void fmc_driver_unregister(struct fmc_driver *drv)
  114. {
  115. driver_unregister(&drv->driver);
  116. }
  117. EXPORT_SYMBOL(fmc_driver_unregister);
  118. /*
  119. * When a device set is registered, all eeproms must be read
  120. * and all FRUs must be parsed
  121. */
  122. int fmc_device_register_n(struct fmc_device **devs, int n)
  123. {
  124. struct fmc_device *fmc, **devarray;
  125. uint32_t device_id;
  126. int i, ret = 0;
  127. if (n < 1)
  128. return 0;
  129. /* Check the version of the first data structure (function prints) */
  130. if (fmc_check_version(devs[0]->version, devs[0]->carrier_name))
  131. return -EINVAL;
  132. devarray = kmemdup(devs, n * sizeof(*devs), GFP_KERNEL);
  133. if (!devarray)
  134. return -ENOMEM;
  135. /* Make all other checks before continuing, for all devices */
  136. for (i = 0; i < n; i++) {
  137. fmc = devarray[i];
  138. if (!fmc->hwdev) {
  139. pr_err("%s: device nr. %i has no hwdev pointer\n",
  140. __func__, i);
  141. ret = -EINVAL;
  142. break;
  143. }
  144. if (fmc->flags & FMC_DEVICE_NO_MEZZANINE) {
  145. dev_info(fmc->hwdev, "absent mezzanine in slot %d\n",
  146. fmc->slot_id);
  147. continue;
  148. }
  149. if (!fmc->eeprom) {
  150. dev_err(fmc->hwdev, "no eeprom provided for slot %i\n",
  151. fmc->slot_id);
  152. ret = -EINVAL;
  153. }
  154. if (!fmc->eeprom_addr) {
  155. dev_err(fmc->hwdev, "no eeprom_addr for slot %i\n",
  156. fmc->slot_id);
  157. ret = -EINVAL;
  158. }
  159. if (!fmc->carrier_name || !fmc->carrier_data ||
  160. !fmc->device_id) {
  161. dev_err(fmc->hwdev,
  162. "deivce nr %i: carrier name, "
  163. "data or dev_id not set\n", i);
  164. ret = -EINVAL;
  165. }
  166. if (ret)
  167. break;
  168. }
  169. if (ret) {
  170. kfree(devarray);
  171. return ret;
  172. }
  173. /* Validation is ok. Now init and register the devices */
  174. for (i = 0; i < n; i++) {
  175. fmc = devarray[i];
  176. fmc->nr_slots = n; /* each slot must know how many are there */
  177. fmc->devarray = devarray;
  178. device_initialize(&fmc->dev);
  179. fmc->dev.release = fmc_release;
  180. fmc->dev.parent = fmc->hwdev;
  181. /* Fill the identification stuff (may fail) */
  182. fmc_fill_id_info(fmc);
  183. fmc->dev.bus = &fmc_bus_type;
  184. /* Name from mezzanine info or carrier info. Or 0,1,2.. */
  185. device_id = fmc->device_id;
  186. if (!fmc->mezzanine_name)
  187. dev_set_name(&fmc->dev, "fmc-%04x", device_id);
  188. else
  189. dev_set_name(&fmc->dev, "%s-%04x", fmc->mezzanine_name,
  190. device_id);
  191. ret = device_add(&fmc->dev);
  192. if (ret < 0) {
  193. dev_err(fmc->hwdev, "Slot %i: Failed in registering "
  194. "\"%s\"\n", fmc->slot_id, fmc->dev.kobj.name);
  195. goto out;
  196. }
  197. ret = sysfs_create_bin_file(&fmc->dev.kobj, &fmc_eeprom_attr);
  198. if (ret < 0) {
  199. dev_err(&fmc->dev, "Failed in registering eeprom\n");
  200. goto out1;
  201. }
  202. /* This device went well, give information to the user */
  203. fmc_dump_eeprom(fmc);
  204. fmc_dump_sdb(fmc);
  205. }
  206. return 0;
  207. out1:
  208. device_del(&fmc->dev);
  209. out:
  210. fmc_free_id_info(fmc);
  211. put_device(&fmc->dev);
  212. kfree(devarray);
  213. for (i--; i >= 0; i--) {
  214. sysfs_remove_bin_file(&devs[i]->dev.kobj, &fmc_eeprom_attr);
  215. device_del(&devs[i]->dev);
  216. fmc_free_id_info(devs[i]);
  217. put_device(&devs[i]->dev);
  218. }
  219. return ret;
  220. }
  221. EXPORT_SYMBOL(fmc_device_register_n);
  222. int fmc_device_register(struct fmc_device *fmc)
  223. {
  224. return fmc_device_register_n(&fmc, 1);
  225. }
  226. EXPORT_SYMBOL(fmc_device_register);
  227. void fmc_device_unregister_n(struct fmc_device **devs, int n)
  228. {
  229. int i;
  230. if (n < 1)
  231. return;
  232. /* Free devarray first, not used by the later loop */
  233. kfree(devs[0]->devarray);
  234. for (i = 0; i < n; i++) {
  235. sysfs_remove_bin_file(&devs[i]->dev.kobj, &fmc_eeprom_attr);
  236. device_del(&devs[i]->dev);
  237. fmc_free_id_info(devs[i]);
  238. put_device(&devs[i]->dev);
  239. }
  240. }
  241. EXPORT_SYMBOL(fmc_device_unregister_n);
  242. void fmc_device_unregister(struct fmc_device *fmc)
  243. {
  244. fmc_device_unregister_n(&fmc, 1);
  245. }
  246. EXPORT_SYMBOL(fmc_device_unregister);
  247. /* Init and exit are trivial */
  248. static int fmc_init(void)
  249. {
  250. return bus_register(&fmc_bus_type);
  251. }
  252. static void fmc_exit(void)
  253. {
  254. bus_unregister(&fmc_bus_type);
  255. }
  256. module_init(fmc_init);
  257. module_exit(fmc_exit);
  258. MODULE_LICENSE("GPL");