mcb-core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. device_id = dev->id;
  194. dev_set_name(&dev->dev, "mcb%d-16z%03d-%d:%d:%d",
  195. bus->bus_nr, device_id, dev->inst, dev->group, dev->var);
  196. ret = device_add(&dev->dev);
  197. if (ret < 0) {
  198. pr_err("Failed registering device 16z%03d on bus mcb%d (%d)\n",
  199. device_id, bus->bus_nr, ret);
  200. goto out;
  201. }
  202. return 0;
  203. out:
  204. return ret;
  205. }
  206. EXPORT_SYMBOL_GPL(mcb_device_register);
  207. static void mcb_free_bus(struct device *dev)
  208. {
  209. struct mcb_bus *bus = to_mcb_bus(dev);
  210. put_device(bus->carrier);
  211. ida_simple_remove(&mcb_ida, bus->bus_nr);
  212. kfree(bus);
  213. }
  214. /**
  215. * mcb_alloc_bus() - Allocate a new @mcb_bus
  216. *
  217. * Allocate a new @mcb_bus.
  218. */
  219. struct mcb_bus *mcb_alloc_bus(struct device *carrier)
  220. {
  221. struct mcb_bus *bus;
  222. int bus_nr;
  223. int rc;
  224. bus = kzalloc(sizeof(struct mcb_bus), GFP_KERNEL);
  225. if (!bus)
  226. return ERR_PTR(-ENOMEM);
  227. bus_nr = ida_simple_get(&mcb_ida, 0, 0, GFP_KERNEL);
  228. if (bus_nr < 0) {
  229. rc = bus_nr;
  230. goto err_free;
  231. }
  232. bus->bus_nr = bus_nr;
  233. bus->carrier = get_device(carrier);
  234. device_initialize(&bus->dev);
  235. bus->dev.parent = carrier;
  236. bus->dev.bus = &mcb_bus_type;
  237. bus->dev.type = &mcb_carrier_device_type;
  238. bus->dev.release = &mcb_free_bus;
  239. dev_set_name(&bus->dev, "mcb:%d", bus_nr);
  240. rc = device_add(&bus->dev);
  241. if (rc)
  242. goto err_free;
  243. return bus;
  244. err_free:
  245. put_device(carrier);
  246. kfree(bus);
  247. return ERR_PTR(rc);
  248. }
  249. EXPORT_SYMBOL_GPL(mcb_alloc_bus);
  250. static int __mcb_devices_unregister(struct device *dev, void *data)
  251. {
  252. device_unregister(dev);
  253. return 0;
  254. }
  255. static void mcb_devices_unregister(struct mcb_bus *bus)
  256. {
  257. bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_devices_unregister);
  258. }
  259. /**
  260. * mcb_release_bus() - Free a @mcb_bus
  261. * @bus: The @mcb_bus to release
  262. *
  263. * Release an allocated @mcb_bus from the system.
  264. */
  265. void mcb_release_bus(struct mcb_bus *bus)
  266. {
  267. mcb_devices_unregister(bus);
  268. }
  269. EXPORT_SYMBOL_GPL(mcb_release_bus);
  270. /**
  271. * mcb_bus_put() - Increment refcnt
  272. * @bus: The @mcb_bus
  273. *
  274. * Get a @mcb_bus' ref
  275. */
  276. struct mcb_bus *mcb_bus_get(struct mcb_bus *bus)
  277. {
  278. if (bus)
  279. get_device(&bus->dev);
  280. return bus;
  281. }
  282. EXPORT_SYMBOL_GPL(mcb_bus_get);
  283. /**
  284. * mcb_bus_put() - Decrement refcnt
  285. * @bus: The @mcb_bus
  286. *
  287. * Release a @mcb_bus' ref
  288. */
  289. void mcb_bus_put(struct mcb_bus *bus)
  290. {
  291. if (bus)
  292. put_device(&bus->dev);
  293. }
  294. EXPORT_SYMBOL_GPL(mcb_bus_put);
  295. /**
  296. * mcb_alloc_dev() - Allocate a device
  297. * @bus: The @mcb_bus the device is part of
  298. *
  299. * Allocate a @mcb_device and add bus.
  300. */
  301. struct mcb_device *mcb_alloc_dev(struct mcb_bus *bus)
  302. {
  303. struct mcb_device *dev;
  304. dev = kzalloc(sizeof(struct mcb_device), GFP_KERNEL);
  305. if (!dev)
  306. return NULL;
  307. INIT_LIST_HEAD(&dev->bus_list);
  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. static int __mcb_bus_add_child(struct device *dev, void *data)
  336. {
  337. struct mcb_device *mdev = to_mcb_device(dev);
  338. struct mcb_bus *child;
  339. BUG_ON(!mdev->is_added);
  340. child = mdev->subordinate;
  341. if (child)
  342. mcb_bus_add_devices(child);
  343. return 0;
  344. }
  345. /**
  346. * mcb_bus_add_devices() - Add devices in the bus' internal device list
  347. * @bus: The @mcb_bus we add the devices
  348. *
  349. * Add devices in the bus' internal device list to the system.
  350. */
  351. void mcb_bus_add_devices(const struct mcb_bus *bus)
  352. {
  353. bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_bus_add_devices);
  354. bus_for_each_dev(&mcb_bus_type, NULL, NULL, __mcb_bus_add_child);
  355. }
  356. EXPORT_SYMBOL_GPL(mcb_bus_add_devices);
  357. /**
  358. * mcb_request_mem() - Request memory
  359. * @dev: The @mcb_device the memory is for
  360. * @name: The name for the memory reference.
  361. *
  362. * Request memory for a @mcb_device. If @name is NULL the driver name will
  363. * be used.
  364. */
  365. struct resource *mcb_request_mem(struct mcb_device *dev, const char *name)
  366. {
  367. struct resource *mem;
  368. u32 size;
  369. if (!name)
  370. name = dev->dev.driver->name;
  371. size = resource_size(&dev->mem);
  372. mem = request_mem_region(dev->mem.start, size, name);
  373. if (!mem)
  374. return ERR_PTR(-EBUSY);
  375. return mem;
  376. }
  377. EXPORT_SYMBOL_GPL(mcb_request_mem);
  378. /**
  379. * mcb_release_mem() - Release memory requested by device
  380. * @dev: The @mcb_device that requested the memory
  381. *
  382. * Release memory that was prior requested via @mcb_request_mem().
  383. */
  384. void mcb_release_mem(struct resource *mem)
  385. {
  386. u32 size;
  387. size = resource_size(mem);
  388. release_mem_region(mem->start, size);
  389. }
  390. EXPORT_SYMBOL_GPL(mcb_release_mem);
  391. static int __mcb_get_irq(struct mcb_device *dev)
  392. {
  393. struct resource *irq = &dev->irq;
  394. return irq->start;
  395. }
  396. /**
  397. * mcb_get_irq() - Get device's IRQ number
  398. * @dev: The @mcb_device the IRQ is for
  399. *
  400. * Get the IRQ number of a given @mcb_device.
  401. */
  402. int mcb_get_irq(struct mcb_device *dev)
  403. {
  404. struct mcb_bus *bus = dev->bus;
  405. if (bus->get_irq)
  406. return bus->get_irq(dev);
  407. return __mcb_get_irq(dev);
  408. }
  409. EXPORT_SYMBOL_GPL(mcb_get_irq);
  410. static int mcb_init(void)
  411. {
  412. return bus_register(&mcb_bus_type);
  413. }
  414. static void mcb_exit(void)
  415. {
  416. ida_destroy(&mcb_ida);
  417. bus_unregister(&mcb_bus_type);
  418. }
  419. /* mcb must be initialized after PCI but before the chameleon drivers.
  420. * That means we must use some initcall between subsys_initcall and
  421. * device_initcall.
  422. */
  423. fs_initcall(mcb_init);
  424. module_exit(mcb_exit);
  425. MODULE_DESCRIPTION("MEN Chameleon Bus Driver");
  426. MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>");
  427. MODULE_LICENSE("GPL v2");