platform.c 13 KB

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