platform.c 8.4 KB

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