mcb-core.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * MEN Chameleon Bus.
  3. *
  4. * Copyright (C) 2013 MEN Mikroelektronik GmbH (www.men.de)
  5. * Author: Johannes Thumshirn <johannes.thumshirn@men.de>
  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/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/types.h>
  15. #include <linux/idr.h>
  16. #include <linux/mcb.h>
  17. static DEFINE_IDA(mcb_ida);
  18. static const struct mcb_device_id *mcb_match_id(const struct mcb_device_id *ids,
  19. struct mcb_device *dev)
  20. {
  21. if (ids) {
  22. while (ids->device) {
  23. if (ids->device == dev->id)
  24. return ids;
  25. ids++;
  26. }
  27. }
  28. return NULL;
  29. }
  30. static int mcb_match(struct device *dev, struct device_driver *drv)
  31. {
  32. struct mcb_driver *mdrv = to_mcb_driver(drv);
  33. struct mcb_device *mdev = to_mcb_device(dev);
  34. const struct mcb_device_id *found_id;
  35. found_id = mcb_match_id(mdrv->id_table, mdev);
  36. if (found_id)
  37. return 1;
  38. return 0;
  39. }
  40. static int mcb_uevent(struct device *dev, struct kobj_uevent_env *env)
  41. {
  42. struct mcb_device *mdev = to_mcb_device(dev);
  43. int ret;
  44. ret = add_uevent_var(env, "MODALIAS=mcb:16z%03d", mdev->id);
  45. if (ret)
  46. return -ENOMEM;
  47. return 0;
  48. }
  49. static int mcb_probe(struct device *dev)
  50. {
  51. struct mcb_driver *mdrv = to_mcb_driver(dev->driver);
  52. struct mcb_device *mdev = to_mcb_device(dev);
  53. const struct mcb_device_id *found_id;
  54. found_id = mcb_match_id(mdrv->id_table, mdev);
  55. if (!found_id)
  56. return -ENODEV;
  57. return mdrv->probe(mdev, found_id);
  58. }
  59. static int mcb_remove(struct device *dev)
  60. {
  61. struct mcb_driver *mdrv = to_mcb_driver(dev->driver);
  62. struct mcb_device *mdev = to_mcb_device(dev);
  63. mdrv->remove(mdev);
  64. put_device(&mdev->dev);
  65. return 0;
  66. }
  67. static void mcb_shutdown(struct device *dev)
  68. {
  69. struct mcb_device *mdev = to_mcb_device(dev);
  70. struct mcb_driver *mdrv = mdev->driver;
  71. if (mdrv && mdrv->shutdown)
  72. mdrv->shutdown(mdev);
  73. }
  74. static struct bus_type mcb_bus_type = {
  75. .name = "mcb",
  76. .match = mcb_match,
  77. .uevent = mcb_uevent,
  78. .probe = mcb_probe,
  79. .remove = mcb_remove,
  80. .shutdown = mcb_shutdown,
  81. };
  82. /**
  83. * __mcb_register_driver() - Register a @mcb_driver at the system
  84. * @drv: The @mcb_driver
  85. * @owner: The @mcb_driver's module
  86. * @mod_name: The name of the @mcb_driver's module
  87. *
  88. * Register a @mcb_driver at the system. Perform some sanity checks, if
  89. * the .probe and .remove methods are provided by the driver.
  90. */
  91. int __mcb_register_driver(struct mcb_driver *drv, struct module *owner,
  92. const char *mod_name)
  93. {
  94. if (!drv->probe || !drv->remove)
  95. return -EINVAL;
  96. drv->driver.owner = owner;
  97. drv->driver.bus = &mcb_bus_type;
  98. drv->driver.mod_name = mod_name;
  99. return driver_register(&drv->driver);
  100. }
  101. EXPORT_SYMBOL_GPL(__mcb_register_driver);
  102. /**
  103. * mcb_unregister_driver() - Unregister a @mcb_driver from the system
  104. * @drv: The @mcb_driver
  105. *
  106. * Unregister a @mcb_driver from the system.
  107. */
  108. void mcb_unregister_driver(struct mcb_driver *drv)
  109. {
  110. driver_unregister(&drv->driver);
  111. }
  112. EXPORT_SYMBOL_GPL(mcb_unregister_driver);
  113. static void mcb_release_dev(struct device *dev)
  114. {
  115. struct mcb_device *mdev = to_mcb_device(dev);
  116. mcb_bus_put(mdev->bus);
  117. kfree(mdev);
  118. }
  119. /**
  120. * mcb_device_register() - Register a mcb_device
  121. * @bus: The @mcb_bus of the device
  122. * @dev: The @mcb_device
  123. *
  124. * Register a specific @mcb_device at a @mcb_bus and the system itself.
  125. */
  126. int mcb_device_register(struct mcb_bus *bus, struct mcb_device *dev)
  127. {
  128. int ret;
  129. int device_id;
  130. device_initialize(&dev->dev);
  131. dev->dev.bus = &mcb_bus_type;
  132. dev->dev.parent = bus->dev.parent;
  133. dev->dev.release = mcb_release_dev;
  134. device_id = dev->id;
  135. dev_set_name(&dev->dev, "mcb%d-16z%03d-%d:%d:%d",
  136. bus->bus_nr, device_id, dev->inst, dev->group, dev->var);
  137. ret = device_add(&dev->dev);
  138. if (ret < 0) {
  139. pr_err("Failed registering device 16z%03d on bus mcb%d (%d)\n",
  140. device_id, bus->bus_nr, ret);
  141. goto out;
  142. }
  143. return 0;
  144. out:
  145. return ret;
  146. }
  147. EXPORT_SYMBOL_GPL(mcb_device_register);
  148. /**
  149. * mcb_alloc_bus() - Allocate a new @mcb_bus
  150. *
  151. * Allocate a new @mcb_bus.
  152. */
  153. struct mcb_bus *mcb_alloc_bus(struct device *carrier)
  154. {
  155. struct mcb_bus *bus;
  156. int bus_nr;
  157. bus = kzalloc(sizeof(struct mcb_bus), GFP_KERNEL);
  158. if (!bus)
  159. return ERR_PTR(-ENOMEM);
  160. bus_nr = ida_simple_get(&mcb_ida, 0, 0, GFP_KERNEL);
  161. if (bus_nr < 0) {
  162. kfree(bus);
  163. return ERR_PTR(bus_nr);
  164. }
  165. INIT_LIST_HEAD(&bus->children);
  166. bus->bus_nr = bus_nr;
  167. bus->carrier = carrier;
  168. return bus;
  169. }
  170. EXPORT_SYMBOL_GPL(mcb_alloc_bus);
  171. static int __mcb_devices_unregister(struct device *dev, void *data)
  172. {
  173. device_unregister(dev);
  174. return 0;
  175. }
  176. static void mcb_devices_unregister(struct mcb_bus *bus)
  177. {
  178. bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_devices_unregister);
  179. }
  180. /**
  181. * mcb_release_bus() - Free a @mcb_bus
  182. * @bus: The @mcb_bus to release
  183. *
  184. * Release an allocated @mcb_bus from the system.
  185. */
  186. void mcb_release_bus(struct mcb_bus *bus)
  187. {
  188. mcb_devices_unregister(bus);
  189. ida_simple_remove(&mcb_ida, bus->bus_nr);
  190. kfree(bus);
  191. }
  192. EXPORT_SYMBOL_GPL(mcb_release_bus);
  193. /**
  194. * mcb_bus_put() - Increment refcnt
  195. * @bus: The @mcb_bus
  196. *
  197. * Get a @mcb_bus' ref
  198. */
  199. struct mcb_bus *mcb_bus_get(struct mcb_bus *bus)
  200. {
  201. if (bus)
  202. get_device(&bus->dev);
  203. return bus;
  204. }
  205. EXPORT_SYMBOL_GPL(mcb_bus_get);
  206. /**
  207. * mcb_bus_put() - Decrement refcnt
  208. * @bus: The @mcb_bus
  209. *
  210. * Release a @mcb_bus' ref
  211. */
  212. void mcb_bus_put(struct mcb_bus *bus)
  213. {
  214. if (bus)
  215. put_device(&bus->dev);
  216. }
  217. EXPORT_SYMBOL_GPL(mcb_bus_put);
  218. /**
  219. * mcb_alloc_dev() - Allocate a device
  220. * @bus: The @mcb_bus the device is part of
  221. *
  222. * Allocate a @mcb_device and add bus.
  223. */
  224. struct mcb_device *mcb_alloc_dev(struct mcb_bus *bus)
  225. {
  226. struct mcb_device *dev;
  227. dev = kzalloc(sizeof(struct mcb_device), GFP_KERNEL);
  228. if (!dev)
  229. return NULL;
  230. INIT_LIST_HEAD(&dev->bus_list);
  231. dev->bus = bus;
  232. return dev;
  233. }
  234. EXPORT_SYMBOL_GPL(mcb_alloc_dev);
  235. /**
  236. * mcb_free_dev() - Free @mcb_device
  237. * @dev: The device to free
  238. *
  239. * Free a @mcb_device
  240. */
  241. void mcb_free_dev(struct mcb_device *dev)
  242. {
  243. kfree(dev);
  244. }
  245. EXPORT_SYMBOL_GPL(mcb_free_dev);
  246. static int __mcb_bus_add_devices(struct device *dev, void *data)
  247. {
  248. struct mcb_device *mdev = to_mcb_device(dev);
  249. int retval;
  250. if (mdev->is_added)
  251. return 0;
  252. retval = device_attach(dev);
  253. if (retval < 0)
  254. dev_err(dev, "Error adding device (%d)\n", retval);
  255. mdev->is_added = true;
  256. return 0;
  257. }
  258. static int __mcb_bus_add_child(struct device *dev, void *data)
  259. {
  260. struct mcb_device *mdev = to_mcb_device(dev);
  261. struct mcb_bus *child;
  262. BUG_ON(!mdev->is_added);
  263. child = mdev->subordinate;
  264. if (child)
  265. mcb_bus_add_devices(child);
  266. return 0;
  267. }
  268. /**
  269. * mcb_bus_add_devices() - Add devices in the bus' internal device list
  270. * @bus: The @mcb_bus we add the devices
  271. *
  272. * Add devices in the bus' internal device list to the system.
  273. */
  274. void mcb_bus_add_devices(const struct mcb_bus *bus)
  275. {
  276. bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_bus_add_devices);
  277. bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_bus_add_child);
  278. }
  279. EXPORT_SYMBOL_GPL(mcb_bus_add_devices);
  280. /**
  281. * mcb_request_mem() - Request memory
  282. * @dev: The @mcb_device the memory is for
  283. * @name: The name for the memory reference.
  284. *
  285. * Request memory for a @mcb_device. If @name is NULL the driver name will
  286. * be used.
  287. */
  288. struct resource *mcb_request_mem(struct mcb_device *dev, const char *name)
  289. {
  290. struct resource *mem;
  291. u32 size;
  292. if (!name)
  293. name = dev->dev.driver->name;
  294. size = resource_size(&dev->mem);
  295. mem = request_mem_region(dev->mem.start, size, name);
  296. if (!mem)
  297. return ERR_PTR(-EBUSY);
  298. return mem;
  299. }
  300. EXPORT_SYMBOL_GPL(mcb_request_mem);
  301. /**
  302. * mcb_release_mem() - Release memory requested by device
  303. * @dev: The @mcb_device that requested the memory
  304. *
  305. * Release memory that was prior requested via @mcb_request_mem().
  306. */
  307. void mcb_release_mem(struct resource *mem)
  308. {
  309. u32 size;
  310. size = resource_size(mem);
  311. release_mem_region(mem->start, size);
  312. }
  313. EXPORT_SYMBOL_GPL(mcb_release_mem);
  314. static int __mcb_get_irq(struct mcb_device *dev)
  315. {
  316. struct resource *irq = &dev->irq;
  317. return irq->start;
  318. }
  319. /**
  320. * mcb_get_irq() - Get device's IRQ number
  321. * @dev: The @mcb_device the IRQ is for
  322. *
  323. * Get the IRQ number of a given @mcb_device.
  324. */
  325. int mcb_get_irq(struct mcb_device *dev)
  326. {
  327. struct mcb_bus *bus = dev->bus;
  328. if (bus->get_irq)
  329. return bus->get_irq(dev);
  330. return __mcb_get_irq(dev);
  331. }
  332. EXPORT_SYMBOL_GPL(mcb_get_irq);
  333. static int mcb_init(void)
  334. {
  335. return bus_register(&mcb_bus_type);
  336. }
  337. static void mcb_exit(void)
  338. {
  339. ida_destroy(&mcb_ida);
  340. bus_unregister(&mcb_bus_type);
  341. }
  342. /* mcb must be initialized after PCI but before the chameleon drivers.
  343. * That means we must use some initcall between subsys_initcall and
  344. * device_initcall.
  345. */
  346. fs_initcall(mcb_init);
  347. module_exit(mcb_exit);
  348. MODULE_DESCRIPTION("MEN Chameleon Bus Driver");
  349. MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>");
  350. MODULE_LICENSE("GPL v2");