platform.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. #define to_platform_driver(drv) (container_of((drv), struct platform_driver, driver))
  21. struct device platform_bus = {
  22. .bus_id = "platform",
  23. };
  24. /**
  25. * platform_get_resource - get a resource for a device
  26. * @dev: platform device
  27. * @type: resource type
  28. * @num: resource index
  29. */
  30. struct resource *
  31. platform_get_resource(struct platform_device *dev, unsigned int type,
  32. unsigned int num)
  33. {
  34. int i;
  35. for (i = 0; i < dev->num_resources; i++) {
  36. struct resource *r = &dev->resource[i];
  37. if ((r->flags & (IORESOURCE_IO|IORESOURCE_MEM|
  38. IORESOURCE_IRQ|IORESOURCE_DMA))
  39. == type)
  40. if (num-- == 0)
  41. return r;
  42. }
  43. return NULL;
  44. }
  45. /**
  46. * platform_get_irq - get an IRQ for a device
  47. * @dev: platform device
  48. * @num: IRQ number index
  49. */
  50. int platform_get_irq(struct platform_device *dev, unsigned int num)
  51. {
  52. struct resource *r = platform_get_resource(dev, IORESOURCE_IRQ, num);
  53. return r ? r->start : 0;
  54. }
  55. /**
  56. * platform_get_resource_byname - get a resource for a device by name
  57. * @dev: platform device
  58. * @type: resource type
  59. * @name: resource name
  60. */
  61. struct resource *
  62. platform_get_resource_byname(struct platform_device *dev, unsigned int type,
  63. char *name)
  64. {
  65. int i;
  66. for (i = 0; i < dev->num_resources; i++) {
  67. struct resource *r = &dev->resource[i];
  68. if ((r->flags & (IORESOURCE_IO|IORESOURCE_MEM|
  69. IORESOURCE_IRQ|IORESOURCE_DMA)) == type)
  70. if (!strcmp(r->name, name))
  71. return r;
  72. }
  73. return NULL;
  74. }
  75. /**
  76. * platform_get_irq - get an IRQ for a device
  77. * @dev: platform device
  78. * @name: IRQ name
  79. */
  80. int platform_get_irq_byname(struct platform_device *dev, char *name)
  81. {
  82. struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
  83. return r ? r->start : 0;
  84. }
  85. /**
  86. * platform_add_devices - add a numbers of platform devices
  87. * @devs: array of platform devices to add
  88. * @num: number of platform devices in array
  89. */
  90. int platform_add_devices(struct platform_device **devs, int num)
  91. {
  92. int i, ret = 0;
  93. for (i = 0; i < num; i++) {
  94. ret = platform_device_register(devs[i]);
  95. if (ret) {
  96. while (--i >= 0)
  97. platform_device_unregister(devs[i]);
  98. break;
  99. }
  100. }
  101. return ret;
  102. }
  103. struct platform_object {
  104. struct platform_device pdev;
  105. char name[1];
  106. };
  107. /**
  108. * platform_device_put
  109. * @pdev: platform device to free
  110. *
  111. * Free all memory associated with a platform device. This function
  112. * must _only_ be externally called in error cases. All other usage
  113. * is a bug.
  114. */
  115. void platform_device_put(struct platform_device *pdev)
  116. {
  117. if (pdev)
  118. put_device(&pdev->dev);
  119. }
  120. EXPORT_SYMBOL_GPL(platform_device_put);
  121. static void platform_device_release(struct device *dev)
  122. {
  123. struct platform_object *pa = container_of(dev, struct platform_object, pdev.dev);
  124. kfree(pa->pdev.dev.platform_data);
  125. kfree(pa->pdev.resource);
  126. kfree(pa);
  127. }
  128. /**
  129. * platform_device_alloc
  130. * @name: base name of the device we're adding
  131. * @id: instance id
  132. *
  133. * Create a platform device object which can have other objects attached
  134. * to it, and which will have attached objects freed when it is released.
  135. */
  136. struct platform_device *platform_device_alloc(const char *name, unsigned int id)
  137. {
  138. struct platform_object *pa;
  139. pa = kzalloc(sizeof(struct platform_object) + strlen(name), GFP_KERNEL);
  140. if (pa) {
  141. strcpy(pa->name, name);
  142. pa->pdev.name = pa->name;
  143. pa->pdev.id = id;
  144. device_initialize(&pa->pdev.dev);
  145. pa->pdev.dev.release = platform_device_release;
  146. }
  147. return pa ? &pa->pdev : NULL;
  148. }
  149. EXPORT_SYMBOL_GPL(platform_device_alloc);
  150. /**
  151. * platform_device_add_resources
  152. * @pdev: platform device allocated by platform_device_alloc to add resources to
  153. * @res: set of resources that needs to be allocated for the device
  154. * @num: number of resources
  155. *
  156. * Add a copy of the resources to the platform device. The memory
  157. * associated with the resources will be freed when the platform
  158. * device is released.
  159. */
  160. int platform_device_add_resources(struct platform_device *pdev, struct resource *res, unsigned int num)
  161. {
  162. struct resource *r;
  163. r = kmalloc(sizeof(struct resource) * num, GFP_KERNEL);
  164. if (r) {
  165. memcpy(r, res, sizeof(struct resource) * num);
  166. pdev->resource = r;
  167. pdev->num_resources = num;
  168. }
  169. return r ? 0 : -ENOMEM;
  170. }
  171. EXPORT_SYMBOL_GPL(platform_device_add_resources);
  172. /**
  173. * platform_device_add_data
  174. * @pdev: platform device allocated by platform_device_alloc to add resources to
  175. * @data: platform specific data for this platform device
  176. * @size: size of platform specific data
  177. *
  178. * Add a copy of platform specific data to the platform device's platform_data
  179. * pointer. The memory associated with the platform data will be freed
  180. * when the platform device is released.
  181. */
  182. int platform_device_add_data(struct platform_device *pdev, void *data, size_t size)
  183. {
  184. void *d;
  185. d = kmalloc(size, GFP_KERNEL);
  186. if (d) {
  187. memcpy(d, data, size);
  188. pdev->dev.platform_data = d;
  189. }
  190. return d ? 0 : -ENOMEM;
  191. }
  192. EXPORT_SYMBOL_GPL(platform_device_add_data);
  193. /**
  194. * platform_device_add - add a platform device to device hierarchy
  195. * @pdev: platform device we're adding
  196. *
  197. * This is part 2 of platform_device_register(), though may be called
  198. * separately _iff_ pdev was allocated by platform_device_alloc().
  199. */
  200. int platform_device_add(struct platform_device *pdev)
  201. {
  202. int i, ret = 0;
  203. if (!pdev)
  204. return -EINVAL;
  205. if (!pdev->dev.parent)
  206. pdev->dev.parent = &platform_bus;
  207. pdev->dev.bus = &platform_bus_type;
  208. if (pdev->id != -1)
  209. snprintf(pdev->dev.bus_id, BUS_ID_SIZE, "%s.%u", pdev->name, pdev->id);
  210. else
  211. strlcpy(pdev->dev.bus_id, pdev->name, BUS_ID_SIZE);
  212. for (i = 0; i < pdev->num_resources; i++) {
  213. struct resource *p, *r = &pdev->resource[i];
  214. if (r->name == NULL)
  215. r->name = pdev->dev.bus_id;
  216. p = r->parent;
  217. if (!p) {
  218. if (r->flags & IORESOURCE_MEM)
  219. p = &iomem_resource;
  220. else if (r->flags & IORESOURCE_IO)
  221. p = &ioport_resource;
  222. }
  223. if (p && request_resource(p, r)) {
  224. printk(KERN_ERR
  225. "%s: failed to claim resource %d\n",
  226. pdev->dev.bus_id, i);
  227. ret = -EBUSY;
  228. goto failed;
  229. }
  230. }
  231. pr_debug("Registering platform device '%s'. Parent at %s\n",
  232. pdev->dev.bus_id, pdev->dev.parent->bus_id);
  233. ret = device_register(&pdev->dev);
  234. if (ret == 0)
  235. return ret;
  236. failed:
  237. while (--i >= 0)
  238. if (pdev->resource[i].flags & (IORESOURCE_MEM|IORESOURCE_IO))
  239. release_resource(&pdev->resource[i]);
  240. return ret;
  241. }
  242. EXPORT_SYMBOL_GPL(platform_device_add);
  243. /**
  244. * platform_device_register - add a platform-level device
  245. * @pdev: platform device we're adding
  246. *
  247. */
  248. int platform_device_register(struct platform_device * pdev)
  249. {
  250. device_initialize(&pdev->dev);
  251. return platform_device_add(pdev);
  252. }
  253. /**
  254. * platform_device_unregister - remove a platform-level device
  255. * @pdev: platform device we're removing
  256. *
  257. * Note that this function will also release all memory- and port-based
  258. * resources owned by the device (@dev->resource).
  259. */
  260. void platform_device_unregister(struct platform_device * pdev)
  261. {
  262. int i;
  263. if (pdev) {
  264. for (i = 0; i < pdev->num_resources; i++) {
  265. struct resource *r = &pdev->resource[i];
  266. if (r->flags & (IORESOURCE_MEM|IORESOURCE_IO))
  267. release_resource(r);
  268. }
  269. device_unregister(&pdev->dev);
  270. }
  271. }
  272. /**
  273. * platform_device_register_simple
  274. * @name: base name of the device we're adding
  275. * @id: instance id
  276. * @res: set of resources that needs to be allocated for the device
  277. * @num: number of resources
  278. *
  279. * This function creates a simple platform device that requires minimal
  280. * resource and memory management. Canned release function freeing
  281. * memory allocated for the device allows drivers using such devices
  282. * to be unloaded iwithout waiting for the last reference to the device
  283. * to be dropped.
  284. */
  285. struct platform_device *platform_device_register_simple(char *name, unsigned int id,
  286. struct resource *res, unsigned int num)
  287. {
  288. struct platform_device *pdev;
  289. int retval;
  290. pdev = platform_device_alloc(name, id);
  291. if (!pdev) {
  292. retval = -ENOMEM;
  293. goto error;
  294. }
  295. if (num) {
  296. retval = platform_device_add_resources(pdev, res, num);
  297. if (retval)
  298. goto error;
  299. }
  300. retval = platform_device_add(pdev);
  301. if (retval)
  302. goto error;
  303. return pdev;
  304. error:
  305. platform_device_put(pdev);
  306. return ERR_PTR(retval);
  307. }
  308. static int platform_drv_probe(struct device *_dev)
  309. {
  310. struct platform_driver *drv = to_platform_driver(_dev->driver);
  311. struct platform_device *dev = to_platform_device(_dev);
  312. return drv->probe(dev);
  313. }
  314. static int platform_drv_remove(struct device *_dev)
  315. {
  316. struct platform_driver *drv = to_platform_driver(_dev->driver);
  317. struct platform_device *dev = to_platform_device(_dev);
  318. return drv->remove(dev);
  319. }
  320. static void platform_drv_shutdown(struct device *_dev)
  321. {
  322. struct platform_driver *drv = to_platform_driver(_dev->driver);
  323. struct platform_device *dev = to_platform_device(_dev);
  324. drv->shutdown(dev);
  325. }
  326. static int platform_drv_suspend(struct device *_dev, pm_message_t state)
  327. {
  328. struct platform_driver *drv = to_platform_driver(_dev->driver);
  329. struct platform_device *dev = to_platform_device(_dev);
  330. return drv->suspend(dev, state);
  331. }
  332. static int platform_drv_resume(struct device *_dev)
  333. {
  334. struct platform_driver *drv = to_platform_driver(_dev->driver);
  335. struct platform_device *dev = to_platform_device(_dev);
  336. return drv->resume(dev);
  337. }
  338. /**
  339. * platform_driver_register
  340. * @drv: platform driver structure
  341. */
  342. int platform_driver_register(struct platform_driver *drv)
  343. {
  344. drv->driver.bus = &platform_bus_type;
  345. if (drv->probe)
  346. drv->driver.probe = platform_drv_probe;
  347. if (drv->remove)
  348. drv->driver.remove = platform_drv_remove;
  349. if (drv->shutdown)
  350. drv->driver.shutdown = platform_drv_shutdown;
  351. if (drv->suspend)
  352. drv->driver.suspend = platform_drv_suspend;
  353. if (drv->resume)
  354. drv->driver.resume = platform_drv_resume;
  355. return driver_register(&drv->driver);
  356. }
  357. EXPORT_SYMBOL_GPL(platform_driver_register);
  358. /**
  359. * platform_driver_unregister
  360. * @drv: platform driver structure
  361. */
  362. void platform_driver_unregister(struct platform_driver *drv)
  363. {
  364. driver_unregister(&drv->driver);
  365. }
  366. EXPORT_SYMBOL_GPL(platform_driver_unregister);
  367. /**
  368. * platform_match - bind platform device to platform driver.
  369. * @dev: device.
  370. * @drv: driver.
  371. *
  372. * Platform device IDs are assumed to be encoded like this:
  373. * "<name><instance>", where <name> is a short description of the
  374. * type of device, like "pci" or "floppy", and <instance> is the
  375. * enumerated instance of the device, like '0' or '42'.
  376. * Driver IDs are simply "<name>".
  377. * So, extract the <name> from the platform_device structure,
  378. * and compare it against the name of the driver. Return whether
  379. * they match or not.
  380. */
  381. static int platform_match(struct device * dev, struct device_driver * drv)
  382. {
  383. struct platform_device *pdev = container_of(dev, struct platform_device, dev);
  384. return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0);
  385. }
  386. static int platform_suspend(struct device * dev, pm_message_t state)
  387. {
  388. int ret = 0;
  389. if (dev->driver && dev->driver->suspend)
  390. ret = dev->driver->suspend(dev, state);
  391. return ret;
  392. }
  393. static int platform_resume(struct device * dev)
  394. {
  395. int ret = 0;
  396. if (dev->driver && dev->driver->resume)
  397. ret = dev->driver->resume(dev);
  398. return ret;
  399. }
  400. struct bus_type platform_bus_type = {
  401. .name = "platform",
  402. .match = platform_match,
  403. .suspend = platform_suspend,
  404. .resume = platform_resume,
  405. };
  406. int __init platform_bus_init(void)
  407. {
  408. device_register(&platform_bus);
  409. return bus_register(&platform_bus_type);
  410. }
  411. #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
  412. u64 dma_get_required_mask(struct device *dev)
  413. {
  414. u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
  415. u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
  416. u64 mask;
  417. if (!high_totalram) {
  418. /* convert to mask just covering totalram */
  419. low_totalram = (1 << (fls(low_totalram) - 1));
  420. low_totalram += low_totalram - 1;
  421. mask = low_totalram;
  422. } else {
  423. high_totalram = (1 << (fls(high_totalram) - 1));
  424. high_totalram += high_totalram - 1;
  425. mask = (((u64)high_totalram) << 32) + 0xffffffff;
  426. }
  427. return mask & *dev->dma_mask;
  428. }
  429. EXPORT_SYMBOL_GPL(dma_get_required_mask);
  430. #endif
  431. EXPORT_SYMBOL_GPL(platform_bus);
  432. EXPORT_SYMBOL_GPL(platform_bus_type);
  433. EXPORT_SYMBOL_GPL(platform_add_devices);
  434. EXPORT_SYMBOL_GPL(platform_device_register);
  435. EXPORT_SYMBOL_GPL(platform_device_register_simple);
  436. EXPORT_SYMBOL_GPL(platform_device_unregister);
  437. EXPORT_SYMBOL_GPL(platform_get_irq);
  438. EXPORT_SYMBOL_GPL(platform_get_resource);
  439. EXPORT_SYMBOL_GPL(platform_get_irq_byname);
  440. EXPORT_SYMBOL_GPL(platform_get_resource_byname);