platform.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. struct platform_object {
  103. struct platform_device pdev;
  104. char name[1];
  105. };
  106. /**
  107. * platform_device_put
  108. * @pdev: platform device to free
  109. *
  110. * Free all memory associated with a platform device. This function
  111. * must _only_ be externally called in error cases. All other usage
  112. * is a bug.
  113. */
  114. void platform_device_put(struct platform_device *pdev)
  115. {
  116. if (pdev)
  117. put_device(&pdev->dev);
  118. }
  119. EXPORT_SYMBOL_GPL(platform_device_put);
  120. static void platform_device_release(struct device *dev)
  121. {
  122. struct platform_object *pa = container_of(dev, struct platform_object, pdev.dev);
  123. kfree(pa->pdev.dev.platform_data);
  124. kfree(pa->pdev.resource);
  125. kfree(pa);
  126. }
  127. /**
  128. * platform_device_alloc
  129. * @name: base name of the device we're adding
  130. * @id: instance id
  131. *
  132. * Create a platform device object which can have other objects attached
  133. * to it, and which will have attached objects freed when it is released.
  134. */
  135. struct platform_device *platform_device_alloc(const char *name, unsigned int id)
  136. {
  137. struct platform_object *pa;
  138. pa = kzalloc(sizeof(struct platform_object) + strlen(name), GFP_KERNEL);
  139. if (pa) {
  140. strcpy(pa->name, name);
  141. pa->pdev.name = pa->name;
  142. pa->pdev.id = id;
  143. device_initialize(&pa->pdev.dev);
  144. pa->pdev.dev.release = platform_device_release;
  145. }
  146. return pa ? &pa->pdev : NULL;
  147. }
  148. EXPORT_SYMBOL_GPL(platform_device_alloc);
  149. /**
  150. * platform_device_add_resources
  151. * @pdev: platform device allocated by platform_device_alloc to add resources to
  152. * @res: set of resources that needs to be allocated for the device
  153. * @num: number of resources
  154. *
  155. * Add a copy of the resources to the platform device. The memory
  156. * associated with the resources will be freed when the platform
  157. * device is released.
  158. */
  159. int platform_device_add_resources(struct platform_device *pdev, struct resource *res, unsigned int num)
  160. {
  161. struct resource *r;
  162. r = kmalloc(sizeof(struct resource) * num, GFP_KERNEL);
  163. if (r) {
  164. memcpy(r, res, sizeof(struct resource) * num);
  165. pdev->resource = r;
  166. pdev->num_resources = num;
  167. }
  168. return r ? 0 : -ENOMEM;
  169. }
  170. EXPORT_SYMBOL_GPL(platform_device_add_resources);
  171. /**
  172. * platform_device_add_data
  173. * @pdev: platform device allocated by platform_device_alloc to add resources to
  174. * @data: platform specific data for this platform device
  175. * @size: size of platform specific data
  176. *
  177. * Add a copy of platform specific data to the platform device's platform_data
  178. * pointer. The memory associated with the platform data will be freed
  179. * when the platform device is released.
  180. */
  181. int platform_device_add_data(struct platform_device *pdev, void *data, size_t size)
  182. {
  183. void *d;
  184. d = kmalloc(size, GFP_KERNEL);
  185. if (d) {
  186. memcpy(d, data, size);
  187. pdev->dev.platform_data = d;
  188. }
  189. return d ? 0 : -ENOMEM;
  190. }
  191. EXPORT_SYMBOL_GPL(platform_device_add_data);
  192. /**
  193. * platform_device_add - add a platform device to device hierarchy
  194. * @pdev: platform device we're adding
  195. *
  196. * This is part 2 of platform_device_register(), though may be called
  197. * separately _iff_ pdev was allocated by platform_device_alloc().
  198. */
  199. int platform_device_add(struct platform_device *pdev)
  200. {
  201. int i, ret = 0;
  202. if (!pdev)
  203. return -EINVAL;
  204. if (!pdev->dev.parent)
  205. pdev->dev.parent = &platform_bus;
  206. pdev->dev.bus = &platform_bus_type;
  207. if (pdev->id != -1)
  208. snprintf(pdev->dev.bus_id, BUS_ID_SIZE, "%s.%u", pdev->name, pdev->id);
  209. else
  210. strlcpy(pdev->dev.bus_id, pdev->name, BUS_ID_SIZE);
  211. for (i = 0; i < pdev->num_resources; i++) {
  212. struct resource *p, *r = &pdev->resource[i];
  213. if (r->name == NULL)
  214. r->name = pdev->dev.bus_id;
  215. p = r->parent;
  216. if (!p) {
  217. if (r->flags & IORESOURCE_MEM)
  218. p = &iomem_resource;
  219. else if (r->flags & IORESOURCE_IO)
  220. p = &ioport_resource;
  221. }
  222. if (p && request_resource(p, r)) {
  223. printk(KERN_ERR
  224. "%s: failed to claim resource %d\n",
  225. pdev->dev.bus_id, i);
  226. ret = -EBUSY;
  227. goto failed;
  228. }
  229. }
  230. pr_debug("Registering platform device '%s'. Parent at %s\n",
  231. pdev->dev.bus_id, pdev->dev.parent->bus_id);
  232. ret = device_register(&pdev->dev);
  233. if (ret == 0)
  234. return ret;
  235. failed:
  236. while (--i >= 0)
  237. if (pdev->resource[i].flags & (IORESOURCE_MEM|IORESOURCE_IO))
  238. release_resource(&pdev->resource[i]);
  239. return ret;
  240. }
  241. EXPORT_SYMBOL_GPL(platform_device_add);
  242. /**
  243. * platform_device_register - add a platform-level device
  244. * @pdev: platform device we're adding
  245. *
  246. */
  247. int platform_device_register(struct platform_device * pdev)
  248. {
  249. device_initialize(&pdev->dev);
  250. return platform_device_add(pdev);
  251. }
  252. /**
  253. * platform_device_unregister - remove a platform-level device
  254. * @pdev: platform device we're removing
  255. *
  256. * Note that this function will also release all memory- and port-based
  257. * resources owned by the device (@dev->resource).
  258. */
  259. void platform_device_unregister(struct platform_device * pdev)
  260. {
  261. int i;
  262. if (pdev) {
  263. for (i = 0; i < pdev->num_resources; i++) {
  264. struct resource *r = &pdev->resource[i];
  265. if (r->flags & (IORESOURCE_MEM|IORESOURCE_IO))
  266. release_resource(r);
  267. }
  268. device_unregister(&pdev->dev);
  269. }
  270. }
  271. /**
  272. * platform_device_register_simple
  273. * @name: base name of the device we're adding
  274. * @id: instance id
  275. * @res: set of resources that needs to be allocated for the device
  276. * @num: number of resources
  277. *
  278. * This function creates a simple platform device that requires minimal
  279. * resource and memory management. Canned release function freeing
  280. * memory allocated for the device allows drivers using such devices
  281. * to be unloaded iwithout waiting for the last reference to the device
  282. * to be dropped.
  283. */
  284. struct platform_device *platform_device_register_simple(char *name, unsigned int id,
  285. struct resource *res, unsigned int num)
  286. {
  287. struct platform_device *pdev;
  288. int retval;
  289. pdev = platform_device_alloc(name, id);
  290. if (!pdev) {
  291. retval = -ENOMEM;
  292. goto error;
  293. }
  294. if (num) {
  295. retval = platform_device_add_resources(pdev, res, num);
  296. if (retval)
  297. goto error;
  298. }
  299. retval = platform_device_add(pdev);
  300. if (retval)
  301. goto error;
  302. return pdev;
  303. error:
  304. platform_device_put(pdev);
  305. return ERR_PTR(retval);
  306. }
  307. /**
  308. * platform_match - bind platform device to platform driver.
  309. * @dev: device.
  310. * @drv: driver.
  311. *
  312. * Platform device IDs are assumed to be encoded like this:
  313. * "<name><instance>", where <name> is a short description of the
  314. * type of device, like "pci" or "floppy", and <instance> is the
  315. * enumerated instance of the device, like '0' or '42'.
  316. * Driver IDs are simply "<name>".
  317. * So, extract the <name> from the platform_device structure,
  318. * and compare it against the name of the driver. Return whether
  319. * they match or not.
  320. */
  321. static int platform_match(struct device * dev, struct device_driver * drv)
  322. {
  323. struct platform_device *pdev = container_of(dev, struct platform_device, dev);
  324. return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0);
  325. }
  326. static int platform_suspend(struct device * dev, pm_message_t state)
  327. {
  328. int ret = 0;
  329. if (dev->driver && dev->driver->suspend)
  330. ret = dev->driver->suspend(dev, state);
  331. return ret;
  332. }
  333. static int platform_resume(struct device * dev)
  334. {
  335. int ret = 0;
  336. if (dev->driver && dev->driver->resume)
  337. ret = dev->driver->resume(dev);
  338. return ret;
  339. }
  340. struct bus_type platform_bus_type = {
  341. .name = "platform",
  342. .match = platform_match,
  343. .suspend = platform_suspend,
  344. .resume = platform_resume,
  345. };
  346. int __init platform_bus_init(void)
  347. {
  348. device_register(&platform_bus);
  349. return bus_register(&platform_bus_type);
  350. }
  351. #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
  352. u64 dma_get_required_mask(struct device *dev)
  353. {
  354. u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
  355. u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
  356. u64 mask;
  357. if (!high_totalram) {
  358. /* convert to mask just covering totalram */
  359. low_totalram = (1 << (fls(low_totalram) - 1));
  360. low_totalram += low_totalram - 1;
  361. mask = low_totalram;
  362. } else {
  363. high_totalram = (1 << (fls(high_totalram) - 1));
  364. high_totalram += high_totalram - 1;
  365. mask = (((u64)high_totalram) << 32) + 0xffffffff;
  366. }
  367. return mask & *dev->dma_mask;
  368. }
  369. EXPORT_SYMBOL_GPL(dma_get_required_mask);
  370. #endif
  371. EXPORT_SYMBOL_GPL(platform_bus);
  372. EXPORT_SYMBOL_GPL(platform_bus_type);
  373. EXPORT_SYMBOL_GPL(platform_add_devices);
  374. EXPORT_SYMBOL_GPL(platform_device_register);
  375. EXPORT_SYMBOL_GPL(platform_device_register_simple);
  376. EXPORT_SYMBOL_GPL(platform_device_unregister);
  377. EXPORT_SYMBOL_GPL(platform_get_irq);
  378. EXPORT_SYMBOL_GPL(platform_get_resource);
  379. EXPORT_SYMBOL_GPL(platform_get_irq_byname);
  380. EXPORT_SYMBOL_GPL(platform_get_resource_byname);