mcb-core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. struct module *carrier_mod;
  55. int ret;
  56. found_id = mcb_match_id(mdrv->id_table, mdev);
  57. if (!found_id)
  58. return -ENODEV;
  59. carrier_mod = mdev->dev.parent->driver->owner;
  60. if (!try_module_get(carrier_mod))
  61. return -EINVAL;
  62. get_device(dev);
  63. ret = mdrv->probe(mdev, found_id);
  64. if (ret)
  65. module_put(carrier_mod);
  66. return ret;
  67. }
  68. static int mcb_remove(struct device *dev)
  69. {
  70. struct mcb_driver *mdrv = to_mcb_driver(dev->driver);
  71. struct mcb_device *mdev = to_mcb_device(dev);
  72. struct module *carrier_mod;
  73. mdrv->remove(mdev);
  74. carrier_mod = mdev->dev.parent->driver->owner;
  75. module_put(carrier_mod);
  76. put_device(&mdev->dev);
  77. return 0;
  78. }
  79. static void mcb_shutdown(struct device *dev)
  80. {
  81. struct mcb_driver *mdrv = to_mcb_driver(dev->driver);
  82. struct mcb_device *mdev = to_mcb_device(dev);
  83. if (mdrv && mdrv->shutdown)
  84. mdrv->shutdown(mdev);
  85. }
  86. static ssize_t revision_show(struct device *dev, struct device_attribute *attr,
  87. char *buf)
  88. {
  89. struct mcb_bus *bus = to_mcb_bus(dev);
  90. return scnprintf(buf, PAGE_SIZE, "%d\n", bus->revision);
  91. }
  92. static DEVICE_ATTR_RO(revision);
  93. static ssize_t model_show(struct device *dev, struct device_attribute *attr,
  94. char *buf)
  95. {
  96. struct mcb_bus *bus = to_mcb_bus(dev);
  97. return scnprintf(buf, PAGE_SIZE, "%c\n", bus->model);
  98. }
  99. static DEVICE_ATTR_RO(model);
  100. static ssize_t minor_show(struct device *dev, struct device_attribute *attr,
  101. char *buf)
  102. {
  103. struct mcb_bus *bus = to_mcb_bus(dev);
  104. return scnprintf(buf, PAGE_SIZE, "%d\n", bus->minor);
  105. }
  106. static DEVICE_ATTR_RO(minor);
  107. static ssize_t name_show(struct device *dev, struct device_attribute *attr,
  108. char *buf)
  109. {
  110. struct mcb_bus *bus = to_mcb_bus(dev);
  111. return scnprintf(buf, PAGE_SIZE, "%s\n", bus->name);
  112. }
  113. static DEVICE_ATTR_RO(name);
  114. static struct attribute *mcb_bus_attrs[] = {
  115. &dev_attr_revision.attr,
  116. &dev_attr_model.attr,
  117. &dev_attr_minor.attr,
  118. &dev_attr_name.attr,
  119. NULL,
  120. };
  121. static const struct attribute_group mcb_carrier_group = {
  122. .attrs = mcb_bus_attrs,
  123. };
  124. static const struct attribute_group *mcb_carrier_groups[] = {
  125. &mcb_carrier_group,
  126. NULL,
  127. };
  128. static struct bus_type mcb_bus_type = {
  129. .name = "mcb",
  130. .match = mcb_match,
  131. .uevent = mcb_uevent,
  132. .probe = mcb_probe,
  133. .remove = mcb_remove,
  134. .shutdown = mcb_shutdown,
  135. };
  136. static struct device_type mcb_carrier_device_type = {
  137. .name = "mcb-carrier",
  138. .groups = mcb_carrier_groups,
  139. };
  140. /**
  141. * __mcb_register_driver() - Register a @mcb_driver at the system
  142. * @drv: The @mcb_driver
  143. * @owner: The @mcb_driver's module
  144. * @mod_name: The name of the @mcb_driver's module
  145. *
  146. * Register a @mcb_driver at the system. Perform some sanity checks, if
  147. * the .probe and .remove methods are provided by the driver.
  148. */
  149. int __mcb_register_driver(struct mcb_driver *drv, struct module *owner,
  150. const char *mod_name)
  151. {
  152. if (!drv->probe || !drv->remove)
  153. return -EINVAL;
  154. drv->driver.owner = owner;
  155. drv->driver.bus = &mcb_bus_type;
  156. drv->driver.mod_name = mod_name;
  157. return driver_register(&drv->driver);
  158. }
  159. EXPORT_SYMBOL_GPL(__mcb_register_driver);
  160. /**
  161. * mcb_unregister_driver() - Unregister a @mcb_driver from the system
  162. * @drv: The @mcb_driver
  163. *
  164. * Unregister a @mcb_driver from the system.
  165. */
  166. void mcb_unregister_driver(struct mcb_driver *drv)
  167. {
  168. driver_unregister(&drv->driver);
  169. }
  170. EXPORT_SYMBOL_GPL(mcb_unregister_driver);
  171. static void mcb_release_dev(struct device *dev)
  172. {
  173. struct mcb_device *mdev = to_mcb_device(dev);
  174. mcb_bus_put(mdev->bus);
  175. kfree(mdev);
  176. }
  177. /**
  178. * mcb_device_register() - Register a mcb_device
  179. * @bus: The @mcb_bus of the device
  180. * @dev: The @mcb_device
  181. *
  182. * Register a specific @mcb_device at a @mcb_bus and the system itself.
  183. */
  184. int mcb_device_register(struct mcb_bus *bus, struct mcb_device *dev)
  185. {
  186. int ret;
  187. int device_id;
  188. device_initialize(&dev->dev);
  189. mcb_bus_get(bus);
  190. dev->dev.bus = &mcb_bus_type;
  191. dev->dev.parent = bus->dev.parent;
  192. dev->dev.release = mcb_release_dev;
  193. dev->dma_dev = bus->carrier;
  194. device_id = dev->id;
  195. dev_set_name(&dev->dev, "mcb%d-16z%03d-%d:%d:%d",
  196. bus->bus_nr, device_id, dev->inst, dev->group, dev->var);
  197. ret = device_add(&dev->dev);
  198. if (ret < 0) {
  199. pr_err("Failed registering device 16z%03d on bus mcb%d (%d)\n",
  200. device_id, bus->bus_nr, ret);
  201. goto out;
  202. }
  203. return 0;
  204. out:
  205. return ret;
  206. }
  207. EXPORT_SYMBOL_GPL(mcb_device_register);
  208. static void mcb_free_bus(struct device *dev)
  209. {
  210. struct mcb_bus *bus = to_mcb_bus(dev);
  211. put_device(bus->carrier);
  212. ida_simple_remove(&mcb_ida, bus->bus_nr);
  213. kfree(bus);
  214. }
  215. /**
  216. * mcb_alloc_bus() - Allocate a new @mcb_bus
  217. *
  218. * Allocate a new @mcb_bus.
  219. */
  220. struct mcb_bus *mcb_alloc_bus(struct device *carrier)
  221. {
  222. struct mcb_bus *bus;
  223. int bus_nr;
  224. int rc;
  225. bus = kzalloc(sizeof(struct mcb_bus), GFP_KERNEL);
  226. if (!bus)
  227. return ERR_PTR(-ENOMEM);
  228. bus_nr = ida_simple_get(&mcb_ida, 0, 0, GFP_KERNEL);
  229. if (bus_nr < 0) {
  230. rc = bus_nr;
  231. goto err_free;
  232. }
  233. bus->bus_nr = bus_nr;
  234. bus->carrier = get_device(carrier);
  235. device_initialize(&bus->dev);
  236. bus->dev.parent = carrier;
  237. bus->dev.bus = &mcb_bus_type;
  238. bus->dev.type = &mcb_carrier_device_type;
  239. bus->dev.release = &mcb_free_bus;
  240. dev_set_name(&bus->dev, "mcb:%d", bus_nr);
  241. rc = device_add(&bus->dev);
  242. if (rc)
  243. goto err_free;
  244. return bus;
  245. err_free:
  246. put_device(carrier);
  247. kfree(bus);
  248. return ERR_PTR(rc);
  249. }
  250. EXPORT_SYMBOL_GPL(mcb_alloc_bus);
  251. static int __mcb_devices_unregister(struct device *dev, void *data)
  252. {
  253. device_unregister(dev);
  254. return 0;
  255. }
  256. static void mcb_devices_unregister(struct mcb_bus *bus)
  257. {
  258. bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_devices_unregister);
  259. }
  260. /**
  261. * mcb_release_bus() - Free a @mcb_bus
  262. * @bus: The @mcb_bus to release
  263. *
  264. * Release an allocated @mcb_bus from the system.
  265. */
  266. void mcb_release_bus(struct mcb_bus *bus)
  267. {
  268. mcb_devices_unregister(bus);
  269. }
  270. EXPORT_SYMBOL_GPL(mcb_release_bus);
  271. /**
  272. * mcb_bus_put() - Increment refcnt
  273. * @bus: The @mcb_bus
  274. *
  275. * Get a @mcb_bus' ref
  276. */
  277. struct mcb_bus *mcb_bus_get(struct mcb_bus *bus)
  278. {
  279. if (bus)
  280. get_device(&bus->dev);
  281. return bus;
  282. }
  283. EXPORT_SYMBOL_GPL(mcb_bus_get);
  284. /**
  285. * mcb_bus_put() - Decrement refcnt
  286. * @bus: The @mcb_bus
  287. *
  288. * Release a @mcb_bus' ref
  289. */
  290. void mcb_bus_put(struct mcb_bus *bus)
  291. {
  292. if (bus)
  293. put_device(&bus->dev);
  294. }
  295. EXPORT_SYMBOL_GPL(mcb_bus_put);
  296. /**
  297. * mcb_alloc_dev() - Allocate a device
  298. * @bus: The @mcb_bus the device is part of
  299. *
  300. * Allocate a @mcb_device and add bus.
  301. */
  302. struct mcb_device *mcb_alloc_dev(struct mcb_bus *bus)
  303. {
  304. struct mcb_device *dev;
  305. dev = kzalloc(sizeof(struct mcb_device), GFP_KERNEL);
  306. if (!dev)
  307. return NULL;
  308. dev->bus = bus;
  309. return dev;
  310. }
  311. EXPORT_SYMBOL_GPL(mcb_alloc_dev);
  312. /**
  313. * mcb_free_dev() - Free @mcb_device
  314. * @dev: The device to free
  315. *
  316. * Free a @mcb_device
  317. */
  318. void mcb_free_dev(struct mcb_device *dev)
  319. {
  320. kfree(dev);
  321. }
  322. EXPORT_SYMBOL_GPL(mcb_free_dev);
  323. static int __mcb_bus_add_devices(struct device *dev, void *data)
  324. {
  325. struct mcb_device *mdev = to_mcb_device(dev);
  326. int retval;
  327. if (mdev->is_added)
  328. return 0;
  329. retval = device_attach(dev);
  330. if (retval < 0)
  331. dev_err(dev, "Error adding device (%d)\n", retval);
  332. mdev->is_added = true;
  333. return 0;
  334. }
  335. /**
  336. * mcb_bus_add_devices() - Add devices in the bus' internal device list
  337. * @bus: The @mcb_bus we add the devices
  338. *
  339. * Add devices in the bus' internal device list to the system.
  340. */
  341. void mcb_bus_add_devices(const struct mcb_bus *bus)
  342. {
  343. bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_bus_add_devices);
  344. }
  345. EXPORT_SYMBOL_GPL(mcb_bus_add_devices);
  346. /**
  347. * mcb_request_mem() - Request memory
  348. * @dev: The @mcb_device the memory is for
  349. * @name: The name for the memory reference.
  350. *
  351. * Request memory for a @mcb_device. If @name is NULL the driver name will
  352. * be used.
  353. */
  354. struct resource *mcb_request_mem(struct mcb_device *dev, const char *name)
  355. {
  356. struct resource *mem;
  357. u32 size;
  358. if (!name)
  359. name = dev->dev.driver->name;
  360. size = resource_size(&dev->mem);
  361. mem = request_mem_region(dev->mem.start, size, name);
  362. if (!mem)
  363. return ERR_PTR(-EBUSY);
  364. return mem;
  365. }
  366. EXPORT_SYMBOL_GPL(mcb_request_mem);
  367. /**
  368. * mcb_release_mem() - Release memory requested by device
  369. * @dev: The @mcb_device that requested the memory
  370. *
  371. * Release memory that was prior requested via @mcb_request_mem().
  372. */
  373. void mcb_release_mem(struct resource *mem)
  374. {
  375. u32 size;
  376. size = resource_size(mem);
  377. release_mem_region(mem->start, size);
  378. }
  379. EXPORT_SYMBOL_GPL(mcb_release_mem);
  380. static int __mcb_get_irq(struct mcb_device *dev)
  381. {
  382. struct resource *irq = &dev->irq;
  383. return irq->start;
  384. }
  385. /**
  386. * mcb_get_irq() - Get device's IRQ number
  387. * @dev: The @mcb_device the IRQ is for
  388. *
  389. * Get the IRQ number of a given @mcb_device.
  390. */
  391. int mcb_get_irq(struct mcb_device *dev)
  392. {
  393. struct mcb_bus *bus = dev->bus;
  394. if (bus->get_irq)
  395. return bus->get_irq(dev);
  396. return __mcb_get_irq(dev);
  397. }
  398. EXPORT_SYMBOL_GPL(mcb_get_irq);
  399. static int mcb_init(void)
  400. {
  401. return bus_register(&mcb_bus_type);
  402. }
  403. static void mcb_exit(void)
  404. {
  405. ida_destroy(&mcb_ida);
  406. bus_unregister(&mcb_bus_type);
  407. }
  408. /* mcb must be initialized after PCI but before the chameleon drivers.
  409. * That means we must use some initcall between subsys_initcall and
  410. * device_initcall.
  411. */
  412. fs_initcall(mcb_init);
  413. module_exit(mcb_exit);
  414. MODULE_DESCRIPTION("MEN Chameleon Bus Driver");
  415. MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>");
  416. MODULE_LICENSE("GPL v2");