platform.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * platform.c - platform 'pseudo' bus for legacy devices
  3. *
  4. * Copyright (c) 2002-3 Patrick Mochel
  5. * Copyright (c) 2002-3 Open Source Development Labs
  6. *
  7. * This file is released under the GPLv2
  8. *
  9. * Please see Documentation/driver-model/platform.txt for more
  10. * information.
  11. */
  12. #include <linux/platform_device.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/bootmem.h>
  17. #include <linux/err.h>
  18. #include <linux/slab.h>
  19. #include "base.h"
  20. struct device platform_bus = {
  21. .bus_id = "platform",
  22. };
  23. /**
  24. * platform_get_resource - get a resource for a device
  25. * @dev: platform device
  26. * @type: resource type
  27. * @num: resource index
  28. */
  29. struct resource *
  30. platform_get_resource(struct platform_device *dev, unsigned int type,
  31. unsigned int num)
  32. {
  33. int i;
  34. for (i = 0; i < dev->num_resources; i++) {
  35. struct resource *r = &dev->resource[i];
  36. if ((r->flags & (IORESOURCE_IO|IORESOURCE_MEM|
  37. IORESOURCE_IRQ|IORESOURCE_DMA))
  38. == type)
  39. if (num-- == 0)
  40. return r;
  41. }
  42. return NULL;
  43. }
  44. /**
  45. * platform_get_irq - get an IRQ for a device
  46. * @dev: platform device
  47. * @num: IRQ number index
  48. */
  49. int platform_get_irq(struct platform_device *dev, unsigned int num)
  50. {
  51. struct resource *r = platform_get_resource(dev, IORESOURCE_IRQ, num);
  52. return r ? r->start : 0;
  53. }
  54. /**
  55. * platform_get_resource_byname - get a resource for a device by name
  56. * @dev: platform device
  57. * @type: resource type
  58. * @name: resource name
  59. */
  60. struct resource *
  61. platform_get_resource_byname(struct platform_device *dev, unsigned int type,
  62. char *name)
  63. {
  64. int i;
  65. for (i = 0; i < dev->num_resources; i++) {
  66. struct resource *r = &dev->resource[i];
  67. if ((r->flags & (IORESOURCE_IO|IORESOURCE_MEM|
  68. IORESOURCE_IRQ|IORESOURCE_DMA)) == type)
  69. if (!strcmp(r->name, name))
  70. return r;
  71. }
  72. return NULL;
  73. }
  74. /**
  75. * platform_get_irq - get an IRQ for a device
  76. * @dev: platform device
  77. * @name: IRQ name
  78. */
  79. int platform_get_irq_byname(struct platform_device *dev, char *name)
  80. {
  81. struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
  82. return r ? r->start : 0;
  83. }
  84. /**
  85. * platform_add_devices - add a numbers of platform devices
  86. * @devs: array of platform devices to add
  87. * @num: number of platform devices in array
  88. */
  89. int platform_add_devices(struct platform_device **devs, int num)
  90. {
  91. int i, ret = 0;
  92. for (i = 0; i < num; i++) {
  93. ret = platform_device_register(devs[i]);
  94. if (ret) {
  95. while (--i >= 0)
  96. platform_device_unregister(devs[i]);
  97. break;
  98. }
  99. }
  100. return ret;
  101. }
  102. /**
  103. * platform_device_register - add a platform-level device
  104. * @pdev: platform device we're adding
  105. *
  106. */
  107. int platform_device_register(struct platform_device * pdev)
  108. {
  109. int i, ret = 0;
  110. if (!pdev)
  111. return -EINVAL;
  112. if (!pdev->dev.parent)
  113. pdev->dev.parent = &platform_bus;
  114. pdev->dev.bus = &platform_bus_type;
  115. if (pdev->id != -1)
  116. snprintf(pdev->dev.bus_id, BUS_ID_SIZE, "%s.%u", pdev->name, pdev->id);
  117. else
  118. strlcpy(pdev->dev.bus_id, pdev->name, BUS_ID_SIZE);
  119. for (i = 0; i < pdev->num_resources; i++) {
  120. struct resource *p, *r = &pdev->resource[i];
  121. if (r->name == NULL)
  122. r->name = pdev->dev.bus_id;
  123. p = r->parent;
  124. if (!p) {
  125. if (r->flags & IORESOURCE_MEM)
  126. p = &iomem_resource;
  127. else if (r->flags & IORESOURCE_IO)
  128. p = &ioport_resource;
  129. }
  130. if (p && request_resource(p, r)) {
  131. printk(KERN_ERR
  132. "%s: failed to claim resource %d\n",
  133. pdev->dev.bus_id, i);
  134. ret = -EBUSY;
  135. goto failed;
  136. }
  137. }
  138. pr_debug("Registering platform device '%s'. Parent at %s\n",
  139. pdev->dev.bus_id, pdev->dev.parent->bus_id);
  140. ret = device_register(&pdev->dev);
  141. if (ret == 0)
  142. return ret;
  143. failed:
  144. while (--i >= 0)
  145. if (pdev->resource[i].flags & (IORESOURCE_MEM|IORESOURCE_IO))
  146. release_resource(&pdev->resource[i]);
  147. return ret;
  148. }
  149. /**
  150. * platform_device_unregister - remove a platform-level device
  151. * @pdev: platform device we're removing
  152. *
  153. * Note that this function will also release all memory- and port-based
  154. * resources owned by the device (@dev->resource).
  155. */
  156. void platform_device_unregister(struct platform_device * pdev)
  157. {
  158. int i;
  159. if (pdev) {
  160. for (i = 0; i < pdev->num_resources; i++) {
  161. struct resource *r = &pdev->resource[i];
  162. if (r->flags & (IORESOURCE_MEM|IORESOURCE_IO))
  163. release_resource(r);
  164. }
  165. device_unregister(&pdev->dev);
  166. }
  167. }
  168. struct platform_object {
  169. struct platform_device pdev;
  170. struct resource resources[0];
  171. };
  172. static void platform_device_release_simple(struct device *dev)
  173. {
  174. struct platform_device *pdev = to_platform_device(dev);
  175. kfree(container_of(pdev, struct platform_object, pdev));
  176. }
  177. /**
  178. * platform_device_register_simple
  179. * @name: base name of the device we're adding
  180. * @id: instance id
  181. * @res: set of resources that needs to be allocated for the device
  182. * @num: number of resources
  183. *
  184. * This function creates a simple platform device that requires minimal
  185. * resource and memory management. Canned release function freeing
  186. * memory allocated for the device allows drivers using such devices
  187. * to be unloaded iwithout waiting for the last reference to the device
  188. * to be dropped.
  189. */
  190. struct platform_device *platform_device_register_simple(char *name, unsigned int id,
  191. struct resource *res, unsigned int num)
  192. {
  193. struct platform_object *pobj;
  194. int retval;
  195. pobj = kzalloc(sizeof(*pobj) + sizeof(struct resource) * num, GFP_KERNEL);
  196. if (!pobj) {
  197. retval = -ENOMEM;
  198. goto error;
  199. }
  200. pobj->pdev.name = name;
  201. pobj->pdev.id = id;
  202. pobj->pdev.dev.release = platform_device_release_simple;
  203. if (num) {
  204. memcpy(pobj->resources, res, sizeof(struct resource) * num);
  205. pobj->pdev.resource = pobj->resources;
  206. pobj->pdev.num_resources = num;
  207. }
  208. retval = platform_device_register(&pobj->pdev);
  209. if (retval)
  210. goto error;
  211. return &pobj->pdev;
  212. error:
  213. kfree(pobj);
  214. return ERR_PTR(retval);
  215. }
  216. /**
  217. * platform_match - bind platform device to platform driver.
  218. * @dev: device.
  219. * @drv: driver.
  220. *
  221. * Platform device IDs are assumed to be encoded like this:
  222. * "<name><instance>", where <name> is a short description of the
  223. * type of device, like "pci" or "floppy", and <instance> is the
  224. * enumerated instance of the device, like '0' or '42'.
  225. * Driver IDs are simply "<name>".
  226. * So, extract the <name> from the platform_device structure,
  227. * and compare it against the name of the driver. Return whether
  228. * they match or not.
  229. */
  230. static int platform_match(struct device * dev, struct device_driver * drv)
  231. {
  232. struct platform_device *pdev = container_of(dev, struct platform_device, dev);
  233. return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0);
  234. }
  235. static int platform_suspend(struct device * dev, pm_message_t state)
  236. {
  237. int ret = 0;
  238. if (dev->driver && dev->driver->suspend)
  239. ret = dev->driver->suspend(dev, state);
  240. return ret;
  241. }
  242. static int platform_resume(struct device * dev)
  243. {
  244. int ret = 0;
  245. if (dev->driver && dev->driver->resume)
  246. ret = dev->driver->resume(dev);
  247. return ret;
  248. }
  249. struct bus_type platform_bus_type = {
  250. .name = "platform",
  251. .match = platform_match,
  252. .suspend = platform_suspend,
  253. .resume = platform_resume,
  254. };
  255. int __init platform_bus_init(void)
  256. {
  257. device_register(&platform_bus);
  258. return bus_register(&platform_bus_type);
  259. }
  260. #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
  261. u64 dma_get_required_mask(struct device *dev)
  262. {
  263. u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
  264. u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
  265. u64 mask;
  266. if (!high_totalram) {
  267. /* convert to mask just covering totalram */
  268. low_totalram = (1 << (fls(low_totalram) - 1));
  269. low_totalram += low_totalram - 1;
  270. mask = low_totalram;
  271. } else {
  272. high_totalram = (1 << (fls(high_totalram) - 1));
  273. high_totalram += high_totalram - 1;
  274. mask = (((u64)high_totalram) << 32) + 0xffffffff;
  275. }
  276. return mask & *dev->dma_mask;
  277. }
  278. EXPORT_SYMBOL_GPL(dma_get_required_mask);
  279. #endif
  280. EXPORT_SYMBOL_GPL(platform_bus);
  281. EXPORT_SYMBOL_GPL(platform_bus_type);
  282. EXPORT_SYMBOL_GPL(platform_add_devices);
  283. EXPORT_SYMBOL_GPL(platform_device_register);
  284. EXPORT_SYMBOL_GPL(platform_device_register_simple);
  285. EXPORT_SYMBOL_GPL(platform_device_unregister);
  286. EXPORT_SYMBOL_GPL(platform_get_irq);
  287. EXPORT_SYMBOL_GPL(platform_get_resource);
  288. EXPORT_SYMBOL_GPL(platform_get_irq_byname);
  289. EXPORT_SYMBOL_GPL(platform_get_resource_byname);