platform.c 8.1 KB

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