platform.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  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/string.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/of_device.h>
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/bootmem.h>
  19. #include <linux/err.h>
  20. #include <linux/slab.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/idr.h>
  23. #include <linux/acpi.h>
  24. #include "base.h"
  25. #include "power/power.h"
  26. /* For automatically allocated device IDs */
  27. static DEFINE_IDA(platform_devid_ida);
  28. struct device platform_bus = {
  29. .init_name = "platform",
  30. };
  31. EXPORT_SYMBOL_GPL(platform_bus);
  32. /**
  33. * arch_setup_pdev_archdata - Allow manipulation of archdata before its used
  34. * @pdev: platform device
  35. *
  36. * This is called before platform_device_add() such that any pdev_archdata may
  37. * be setup before the platform_notifier is called. So if a user needs to
  38. * manipulate any relevant information in the pdev_archdata they can do:
  39. *
  40. * platform_device_alloc()
  41. * ... manipulate ...
  42. * platform_device_add()
  43. *
  44. * And if they don't care they can just call platform_device_register() and
  45. * everything will just work out.
  46. */
  47. void __weak arch_setup_pdev_archdata(struct platform_device *pdev)
  48. {
  49. }
  50. /**
  51. * platform_get_resource - get a resource for a device
  52. * @dev: platform device
  53. * @type: resource type
  54. * @num: resource index
  55. */
  56. struct resource *platform_get_resource(struct platform_device *dev,
  57. unsigned int type, unsigned int num)
  58. {
  59. int i;
  60. for (i = 0; i < dev->num_resources; i++) {
  61. struct resource *r = &dev->resource[i];
  62. if (type == resource_type(r) && num-- == 0)
  63. return r;
  64. }
  65. return NULL;
  66. }
  67. EXPORT_SYMBOL_GPL(platform_get_resource);
  68. /**
  69. * platform_get_irq - get an IRQ for a device
  70. * @dev: platform device
  71. * @num: IRQ number index
  72. */
  73. int platform_get_irq(struct platform_device *dev, unsigned int num)
  74. {
  75. #ifdef CONFIG_SPARC
  76. /* sparc does not have irqs represented as IORESOURCE_IRQ resources */
  77. if (!dev || num >= dev->archdata.num_irqs)
  78. return -ENXIO;
  79. return dev->archdata.irqs[num];
  80. #else
  81. struct resource *r = platform_get_resource(dev, IORESOURCE_IRQ, num);
  82. return r ? r->start : -ENXIO;
  83. #endif
  84. }
  85. EXPORT_SYMBOL_GPL(platform_get_irq);
  86. /**
  87. * platform_get_resource_byname - get a resource for a device by name
  88. * @dev: platform device
  89. * @type: resource type
  90. * @name: resource name
  91. */
  92. struct resource *platform_get_resource_byname(struct platform_device *dev,
  93. unsigned int type,
  94. const char *name)
  95. {
  96. int i;
  97. for (i = 0; i < dev->num_resources; i++) {
  98. struct resource *r = &dev->resource[i];
  99. if (unlikely(!r->name))
  100. continue;
  101. if (type == resource_type(r) && !strcmp(r->name, name))
  102. return r;
  103. }
  104. return NULL;
  105. }
  106. EXPORT_SYMBOL_GPL(platform_get_resource_byname);
  107. /**
  108. * platform_get_irq_byname - get an IRQ for a device by name
  109. * @dev: platform device
  110. * @name: IRQ name
  111. */
  112. int platform_get_irq_byname(struct platform_device *dev, const char *name)
  113. {
  114. struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ,
  115. name);
  116. return r ? r->start : -ENXIO;
  117. }
  118. EXPORT_SYMBOL_GPL(platform_get_irq_byname);
  119. /**
  120. * platform_add_devices - add a numbers of platform devices
  121. * @devs: array of platform devices to add
  122. * @num: number of platform devices in array
  123. */
  124. int platform_add_devices(struct platform_device **devs, int num)
  125. {
  126. int i, ret = 0;
  127. for (i = 0; i < num; i++) {
  128. ret = platform_device_register(devs[i]);
  129. if (ret) {
  130. while (--i >= 0)
  131. platform_device_unregister(devs[i]);
  132. break;
  133. }
  134. }
  135. return ret;
  136. }
  137. EXPORT_SYMBOL_GPL(platform_add_devices);
  138. struct platform_object {
  139. struct platform_device pdev;
  140. char name[1];
  141. };
  142. /**
  143. * platform_device_put - destroy a platform device
  144. * @pdev: platform device to free
  145. *
  146. * Free all memory associated with a platform device. This function must
  147. * _only_ be externally called in error cases. All other usage is a bug.
  148. */
  149. void platform_device_put(struct platform_device *pdev)
  150. {
  151. if (pdev)
  152. put_device(&pdev->dev);
  153. }
  154. EXPORT_SYMBOL_GPL(platform_device_put);
  155. static void platform_device_release(struct device *dev)
  156. {
  157. struct platform_object *pa = container_of(dev, struct platform_object,
  158. pdev.dev);
  159. of_device_node_put(&pa->pdev.dev);
  160. kfree(pa->pdev.dev.platform_data);
  161. kfree(pa->pdev.mfd_cell);
  162. kfree(pa->pdev.resource);
  163. kfree(pa);
  164. }
  165. /**
  166. * platform_device_alloc - create a platform device
  167. * @name: base name of the device we're adding
  168. * @id: instance id
  169. *
  170. * Create a platform device object which can have other objects attached
  171. * to it, and which will have attached objects freed when it is released.
  172. */
  173. struct platform_device *platform_device_alloc(const char *name, int id)
  174. {
  175. struct platform_object *pa;
  176. pa = kzalloc(sizeof(struct platform_object) + strlen(name), GFP_KERNEL);
  177. if (pa) {
  178. strcpy(pa->name, name);
  179. pa->pdev.name = pa->name;
  180. pa->pdev.id = id;
  181. device_initialize(&pa->pdev.dev);
  182. pa->pdev.dev.release = platform_device_release;
  183. arch_setup_pdev_archdata(&pa->pdev);
  184. }
  185. return pa ? &pa->pdev : NULL;
  186. }
  187. EXPORT_SYMBOL_GPL(platform_device_alloc);
  188. /**
  189. * platform_device_add_resources - add resources to a platform device
  190. * @pdev: platform device allocated by platform_device_alloc to add resources to
  191. * @res: set of resources that needs to be allocated for the device
  192. * @num: number of resources
  193. *
  194. * Add a copy of the resources to the platform device. The memory
  195. * associated with the resources will be freed when the platform device is
  196. * released.
  197. */
  198. int platform_device_add_resources(struct platform_device *pdev,
  199. const struct resource *res, unsigned int num)
  200. {
  201. struct resource *r = NULL;
  202. if (res) {
  203. r = kmemdup(res, sizeof(struct resource) * num, GFP_KERNEL);
  204. if (!r)
  205. return -ENOMEM;
  206. }
  207. kfree(pdev->resource);
  208. pdev->resource = r;
  209. pdev->num_resources = num;
  210. return 0;
  211. }
  212. EXPORT_SYMBOL_GPL(platform_device_add_resources);
  213. /**
  214. * platform_device_add_data - add platform-specific data to a platform device
  215. * @pdev: platform device allocated by platform_device_alloc to add resources to
  216. * @data: platform specific data for this platform device
  217. * @size: size of platform specific data
  218. *
  219. * Add a copy of platform specific data to the platform device's
  220. * platform_data pointer. The memory associated with the platform data
  221. * will be freed when the platform device is released.
  222. */
  223. int platform_device_add_data(struct platform_device *pdev, const void *data,
  224. size_t size)
  225. {
  226. void *d = NULL;
  227. if (data) {
  228. d = kmemdup(data, size, GFP_KERNEL);
  229. if (!d)
  230. return -ENOMEM;
  231. }
  232. kfree(pdev->dev.platform_data);
  233. pdev->dev.platform_data = d;
  234. return 0;
  235. }
  236. EXPORT_SYMBOL_GPL(platform_device_add_data);
  237. /**
  238. * platform_device_add - add a platform device to device hierarchy
  239. * @pdev: platform device we're adding
  240. *
  241. * This is part 2 of platform_device_register(), though may be called
  242. * separately _iff_ pdev was allocated by platform_device_alloc().
  243. */
  244. int platform_device_add(struct platform_device *pdev)
  245. {
  246. int i, ret;
  247. if (!pdev)
  248. return -EINVAL;
  249. if (!pdev->dev.parent)
  250. pdev->dev.parent = &platform_bus;
  251. pdev->dev.bus = &platform_bus_type;
  252. switch (pdev->id) {
  253. default:
  254. dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
  255. break;
  256. case PLATFORM_DEVID_NONE:
  257. dev_set_name(&pdev->dev, "%s", pdev->name);
  258. break;
  259. case PLATFORM_DEVID_AUTO:
  260. /*
  261. * Automatically allocated device ID. We mark it as such so
  262. * that we remember it must be freed, and we append a suffix
  263. * to avoid namespace collision with explicit IDs.
  264. */
  265. ret = ida_simple_get(&platform_devid_ida, 0, 0, GFP_KERNEL);
  266. if (ret < 0)
  267. goto err_out;
  268. pdev->id = ret;
  269. pdev->id_auto = true;
  270. dev_set_name(&pdev->dev, "%s.%d.auto", pdev->name, pdev->id);
  271. break;
  272. }
  273. for (i = 0; i < pdev->num_resources; i++) {
  274. struct resource *p, *r = &pdev->resource[i];
  275. if (r->name == NULL)
  276. r->name = dev_name(&pdev->dev);
  277. p = r->parent;
  278. if (!p) {
  279. if (resource_type(r) == IORESOURCE_MEM)
  280. p = &iomem_resource;
  281. else if (resource_type(r) == IORESOURCE_IO)
  282. p = &ioport_resource;
  283. }
  284. if (p && insert_resource(p, r)) {
  285. dev_err(&pdev->dev, "failed to claim resource %d\n", i);
  286. ret = -EBUSY;
  287. goto failed;
  288. }
  289. }
  290. pr_debug("Registering platform device '%s'. Parent at %s\n",
  291. dev_name(&pdev->dev), dev_name(pdev->dev.parent));
  292. ret = device_add(&pdev->dev);
  293. if (ret == 0)
  294. return ret;
  295. failed:
  296. if (pdev->id_auto) {
  297. ida_simple_remove(&platform_devid_ida, pdev->id);
  298. pdev->id = PLATFORM_DEVID_AUTO;
  299. }
  300. while (--i >= 0) {
  301. struct resource *r = &pdev->resource[i];
  302. unsigned long type = resource_type(r);
  303. if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
  304. release_resource(r);
  305. }
  306. err_out:
  307. return ret;
  308. }
  309. EXPORT_SYMBOL_GPL(platform_device_add);
  310. /**
  311. * platform_device_del - remove a platform-level device
  312. * @pdev: platform device we're removing
  313. *
  314. * Note that this function will also release all memory- and port-based
  315. * resources owned by the device (@dev->resource). This function must
  316. * _only_ be externally called in error cases. All other usage is a bug.
  317. */
  318. void platform_device_del(struct platform_device *pdev)
  319. {
  320. int i;
  321. if (pdev) {
  322. device_del(&pdev->dev);
  323. if (pdev->id_auto) {
  324. ida_simple_remove(&platform_devid_ida, pdev->id);
  325. pdev->id = PLATFORM_DEVID_AUTO;
  326. }
  327. for (i = 0; i < pdev->num_resources; i++) {
  328. struct resource *r = &pdev->resource[i];
  329. unsigned long type = resource_type(r);
  330. if (type == IORESOURCE_MEM || type == IORESOURCE_IO)
  331. release_resource(r);
  332. }
  333. }
  334. }
  335. EXPORT_SYMBOL_GPL(platform_device_del);
  336. /**
  337. * platform_device_register - add a platform-level device
  338. * @pdev: platform device we're adding
  339. */
  340. int platform_device_register(struct platform_device *pdev)
  341. {
  342. device_initialize(&pdev->dev);
  343. arch_setup_pdev_archdata(pdev);
  344. return platform_device_add(pdev);
  345. }
  346. EXPORT_SYMBOL_GPL(platform_device_register);
  347. /**
  348. * platform_device_unregister - unregister a platform-level device
  349. * @pdev: platform device we're unregistering
  350. *
  351. * Unregistration is done in 2 steps. First we release all resources
  352. * and remove it from the subsystem, then we drop reference count by
  353. * calling platform_device_put().
  354. */
  355. void platform_device_unregister(struct platform_device *pdev)
  356. {
  357. platform_device_del(pdev);
  358. platform_device_put(pdev);
  359. }
  360. EXPORT_SYMBOL_GPL(platform_device_unregister);
  361. /**
  362. * platform_device_register_full - add a platform-level device with
  363. * resources and platform-specific data
  364. *
  365. * @pdevinfo: data used to create device
  366. *
  367. * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
  368. */
  369. struct platform_device *platform_device_register_full(
  370. const struct platform_device_info *pdevinfo)
  371. {
  372. int ret = -ENOMEM;
  373. struct platform_device *pdev;
  374. pdev = platform_device_alloc(pdevinfo->name, pdevinfo->id);
  375. if (!pdev)
  376. goto err_alloc;
  377. pdev->dev.parent = pdevinfo->parent;
  378. ACPI_COMPANION_SET(&pdev->dev, pdevinfo->acpi_node.companion);
  379. if (pdevinfo->dma_mask) {
  380. /*
  381. * This memory isn't freed when the device is put,
  382. * I don't have a nice idea for that though. Conceptually
  383. * dma_mask in struct device should not be a pointer.
  384. * See http://thread.gmane.org/gmane.linux.kernel.pci/9081
  385. */
  386. pdev->dev.dma_mask =
  387. kmalloc(sizeof(*pdev->dev.dma_mask), GFP_KERNEL);
  388. if (!pdev->dev.dma_mask)
  389. goto err;
  390. *pdev->dev.dma_mask = pdevinfo->dma_mask;
  391. pdev->dev.coherent_dma_mask = pdevinfo->dma_mask;
  392. }
  393. ret = platform_device_add_resources(pdev,
  394. pdevinfo->res, pdevinfo->num_res);
  395. if (ret)
  396. goto err;
  397. ret = platform_device_add_data(pdev,
  398. pdevinfo->data, pdevinfo->size_data);
  399. if (ret)
  400. goto err;
  401. ret = platform_device_add(pdev);
  402. if (ret) {
  403. err:
  404. ACPI_COMPANION_SET(&pdev->dev, NULL);
  405. kfree(pdev->dev.dma_mask);
  406. err_alloc:
  407. platform_device_put(pdev);
  408. return ERR_PTR(ret);
  409. }
  410. return pdev;
  411. }
  412. EXPORT_SYMBOL_GPL(platform_device_register_full);
  413. static int platform_drv_probe(struct device *_dev)
  414. {
  415. struct platform_driver *drv = to_platform_driver(_dev->driver);
  416. struct platform_device *dev = to_platform_device(_dev);
  417. int ret;
  418. acpi_dev_pm_attach(_dev, true);
  419. ret = drv->probe(dev);
  420. if (ret)
  421. acpi_dev_pm_detach(_dev, true);
  422. if (drv->prevent_deferred_probe && ret == -EPROBE_DEFER) {
  423. dev_warn(_dev, "probe deferral not supported\n");
  424. ret = -ENXIO;
  425. }
  426. return ret;
  427. }
  428. static int platform_drv_probe_fail(struct device *_dev)
  429. {
  430. return -ENXIO;
  431. }
  432. static int platform_drv_remove(struct device *_dev)
  433. {
  434. struct platform_driver *drv = to_platform_driver(_dev->driver);
  435. struct platform_device *dev = to_platform_device(_dev);
  436. int ret;
  437. ret = drv->remove(dev);
  438. acpi_dev_pm_detach(_dev, true);
  439. return ret;
  440. }
  441. static void platform_drv_shutdown(struct device *_dev)
  442. {
  443. struct platform_driver *drv = to_platform_driver(_dev->driver);
  444. struct platform_device *dev = to_platform_device(_dev);
  445. drv->shutdown(dev);
  446. acpi_dev_pm_detach(_dev, true);
  447. }
  448. /**
  449. * __platform_driver_register - register a driver for platform-level devices
  450. * @drv: platform driver structure
  451. * @owner: owning module/driver
  452. */
  453. int __platform_driver_register(struct platform_driver *drv,
  454. struct module *owner)
  455. {
  456. drv->driver.owner = owner;
  457. drv->driver.bus = &platform_bus_type;
  458. if (drv->probe)
  459. drv->driver.probe = platform_drv_probe;
  460. if (drv->remove)
  461. drv->driver.remove = platform_drv_remove;
  462. if (drv->shutdown)
  463. drv->driver.shutdown = platform_drv_shutdown;
  464. return driver_register(&drv->driver);
  465. }
  466. EXPORT_SYMBOL_GPL(__platform_driver_register);
  467. /**
  468. * platform_driver_unregister - unregister a driver for platform-level devices
  469. * @drv: platform driver structure
  470. */
  471. void platform_driver_unregister(struct platform_driver *drv)
  472. {
  473. driver_unregister(&drv->driver);
  474. }
  475. EXPORT_SYMBOL_GPL(platform_driver_unregister);
  476. /**
  477. * platform_driver_probe - register driver for non-hotpluggable device
  478. * @drv: platform driver structure
  479. * @probe: the driver probe routine, probably from an __init section
  480. *
  481. * Use this instead of platform_driver_register() when you know the device
  482. * is not hotpluggable and has already been registered, and you want to
  483. * remove its run-once probe() infrastructure from memory after the driver
  484. * has bound to the device.
  485. *
  486. * One typical use for this would be with drivers for controllers integrated
  487. * into system-on-chip processors, where the controller devices have been
  488. * configured as part of board setup.
  489. *
  490. * Note that this is incompatible with deferred probing.
  491. *
  492. * Returns zero if the driver registered and bound to a device, else returns
  493. * a negative error code and with the driver not registered.
  494. */
  495. int __init_or_module platform_driver_probe(struct platform_driver *drv,
  496. int (*probe)(struct platform_device *))
  497. {
  498. int retval, code;
  499. /*
  500. * Prevent driver from requesting probe deferral to avoid further
  501. * futile probe attempts.
  502. */
  503. drv->prevent_deferred_probe = true;
  504. /* make sure driver won't have bind/unbind attributes */
  505. drv->driver.suppress_bind_attrs = true;
  506. /* temporary section violation during probe() */
  507. drv->probe = probe;
  508. retval = code = platform_driver_register(drv);
  509. /*
  510. * Fixup that section violation, being paranoid about code scanning
  511. * the list of drivers in order to probe new devices. Check to see
  512. * if the probe was successful, and make sure any forced probes of
  513. * new devices fail.
  514. */
  515. spin_lock(&drv->driver.bus->p->klist_drivers.k_lock);
  516. drv->probe = NULL;
  517. if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
  518. retval = -ENODEV;
  519. drv->driver.probe = platform_drv_probe_fail;
  520. spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock);
  521. if (code != retval)
  522. platform_driver_unregister(drv);
  523. return retval;
  524. }
  525. EXPORT_SYMBOL_GPL(platform_driver_probe);
  526. /**
  527. * platform_create_bundle - register driver and create corresponding device
  528. * @driver: platform driver structure
  529. * @probe: the driver probe routine, probably from an __init section
  530. * @res: set of resources that needs to be allocated for the device
  531. * @n_res: number of resources
  532. * @data: platform specific data for this platform device
  533. * @size: size of platform specific data
  534. *
  535. * Use this in legacy-style modules that probe hardware directly and
  536. * register a single platform device and corresponding platform driver.
  537. *
  538. * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
  539. */
  540. struct platform_device * __init_or_module platform_create_bundle(
  541. struct platform_driver *driver,
  542. int (*probe)(struct platform_device *),
  543. struct resource *res, unsigned int n_res,
  544. const void *data, size_t size)
  545. {
  546. struct platform_device *pdev;
  547. int error;
  548. pdev = platform_device_alloc(driver->driver.name, -1);
  549. if (!pdev) {
  550. error = -ENOMEM;
  551. goto err_out;
  552. }
  553. error = platform_device_add_resources(pdev, res, n_res);
  554. if (error)
  555. goto err_pdev_put;
  556. error = platform_device_add_data(pdev, data, size);
  557. if (error)
  558. goto err_pdev_put;
  559. error = platform_device_add(pdev);
  560. if (error)
  561. goto err_pdev_put;
  562. error = platform_driver_probe(driver, probe);
  563. if (error)
  564. goto err_pdev_del;
  565. return pdev;
  566. err_pdev_del:
  567. platform_device_del(pdev);
  568. err_pdev_put:
  569. platform_device_put(pdev);
  570. err_out:
  571. return ERR_PTR(error);
  572. }
  573. EXPORT_SYMBOL_GPL(platform_create_bundle);
  574. /* modalias support enables more hands-off userspace setup:
  575. * (a) environment variable lets new-style hotplug events work once system is
  576. * fully running: "modprobe $MODALIAS"
  577. * (b) sysfs attribute lets new-style coldplug recover from hotplug events
  578. * mishandled before system is fully running: "modprobe $(cat modalias)"
  579. */
  580. static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
  581. char *buf)
  582. {
  583. struct platform_device *pdev = to_platform_device(dev);
  584. int len;
  585. len = of_device_get_modalias(dev, buf, PAGE_SIZE -1);
  586. if (len != -ENODEV)
  587. return len;
  588. len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
  589. if (len != -ENODEV)
  590. return len;
  591. len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
  592. return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
  593. }
  594. static DEVICE_ATTR_RO(modalias);
  595. static struct attribute *platform_dev_attrs[] = {
  596. &dev_attr_modalias.attr,
  597. NULL,
  598. };
  599. ATTRIBUTE_GROUPS(platform_dev);
  600. static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
  601. {
  602. struct platform_device *pdev = to_platform_device(dev);
  603. int rc;
  604. /* Some devices have extra OF data and an OF-style MODALIAS */
  605. rc = of_device_uevent_modalias(dev, env);
  606. if (rc != -ENODEV)
  607. return rc;
  608. rc = acpi_device_uevent_modalias(dev, env);
  609. if (rc != -ENODEV)
  610. return rc;
  611. add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
  612. pdev->name);
  613. return 0;
  614. }
  615. static const struct platform_device_id *platform_match_id(
  616. const struct platform_device_id *id,
  617. struct platform_device *pdev)
  618. {
  619. while (id->name[0]) {
  620. if (strcmp(pdev->name, id->name) == 0) {
  621. pdev->id_entry = id;
  622. return id;
  623. }
  624. id++;
  625. }
  626. return NULL;
  627. }
  628. /**
  629. * platform_match - bind platform device to platform driver.
  630. * @dev: device.
  631. * @drv: driver.
  632. *
  633. * Platform device IDs are assumed to be encoded like this:
  634. * "<name><instance>", where <name> is a short description of the type of
  635. * device, like "pci" or "floppy", and <instance> is the enumerated
  636. * instance of the device, like '0' or '42'. Driver IDs are simply
  637. * "<name>". So, extract the <name> from the platform_device structure,
  638. * and compare it against the name of the driver. Return whether they match
  639. * or not.
  640. */
  641. static int platform_match(struct device *dev, struct device_driver *drv)
  642. {
  643. struct platform_device *pdev = to_platform_device(dev);
  644. struct platform_driver *pdrv = to_platform_driver(drv);
  645. /* Attempt an OF style match first */
  646. if (of_driver_match_device(dev, drv))
  647. return 1;
  648. /* Then try ACPI style match */
  649. if (acpi_driver_match_device(dev, drv))
  650. return 1;
  651. /* Then try to match against the id table */
  652. if (pdrv->id_table)
  653. return platform_match_id(pdrv->id_table, pdev) != NULL;
  654. /* fall-back to driver name match */
  655. return (strcmp(pdev->name, drv->name) == 0);
  656. }
  657. #ifdef CONFIG_PM_SLEEP
  658. static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
  659. {
  660. struct platform_driver *pdrv = to_platform_driver(dev->driver);
  661. struct platform_device *pdev = to_platform_device(dev);
  662. int ret = 0;
  663. if (dev->driver && pdrv->suspend)
  664. ret = pdrv->suspend(pdev, mesg);
  665. return ret;
  666. }
  667. static int platform_legacy_resume(struct device *dev)
  668. {
  669. struct platform_driver *pdrv = to_platform_driver(dev->driver);
  670. struct platform_device *pdev = to_platform_device(dev);
  671. int ret = 0;
  672. if (dev->driver && pdrv->resume)
  673. ret = pdrv->resume(pdev);
  674. return ret;
  675. }
  676. #endif /* CONFIG_PM_SLEEP */
  677. #ifdef CONFIG_SUSPEND
  678. int platform_pm_suspend(struct device *dev)
  679. {
  680. struct device_driver *drv = dev->driver;
  681. int ret = 0;
  682. if (!drv)
  683. return 0;
  684. if (drv->pm) {
  685. if (drv->pm->suspend)
  686. ret = drv->pm->suspend(dev);
  687. } else {
  688. ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
  689. }
  690. return ret;
  691. }
  692. int platform_pm_resume(struct device *dev)
  693. {
  694. struct device_driver *drv = dev->driver;
  695. int ret = 0;
  696. if (!drv)
  697. return 0;
  698. if (drv->pm) {
  699. if (drv->pm->resume)
  700. ret = drv->pm->resume(dev);
  701. } else {
  702. ret = platform_legacy_resume(dev);
  703. }
  704. return ret;
  705. }
  706. #endif /* CONFIG_SUSPEND */
  707. #ifdef CONFIG_HIBERNATE_CALLBACKS
  708. int platform_pm_freeze(struct device *dev)
  709. {
  710. struct device_driver *drv = dev->driver;
  711. int ret = 0;
  712. if (!drv)
  713. return 0;
  714. if (drv->pm) {
  715. if (drv->pm->freeze)
  716. ret = drv->pm->freeze(dev);
  717. } else {
  718. ret = platform_legacy_suspend(dev, PMSG_FREEZE);
  719. }
  720. return ret;
  721. }
  722. int platform_pm_thaw(struct device *dev)
  723. {
  724. struct device_driver *drv = dev->driver;
  725. int ret = 0;
  726. if (!drv)
  727. return 0;
  728. if (drv->pm) {
  729. if (drv->pm->thaw)
  730. ret = drv->pm->thaw(dev);
  731. } else {
  732. ret = platform_legacy_resume(dev);
  733. }
  734. return ret;
  735. }
  736. int platform_pm_poweroff(struct device *dev)
  737. {
  738. struct device_driver *drv = dev->driver;
  739. int ret = 0;
  740. if (!drv)
  741. return 0;
  742. if (drv->pm) {
  743. if (drv->pm->poweroff)
  744. ret = drv->pm->poweroff(dev);
  745. } else {
  746. ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
  747. }
  748. return ret;
  749. }
  750. int platform_pm_restore(struct device *dev)
  751. {
  752. struct device_driver *drv = dev->driver;
  753. int ret = 0;
  754. if (!drv)
  755. return 0;
  756. if (drv->pm) {
  757. if (drv->pm->restore)
  758. ret = drv->pm->restore(dev);
  759. } else {
  760. ret = platform_legacy_resume(dev);
  761. }
  762. return ret;
  763. }
  764. #endif /* CONFIG_HIBERNATE_CALLBACKS */
  765. static const struct dev_pm_ops platform_dev_pm_ops = {
  766. .runtime_suspend = pm_generic_runtime_suspend,
  767. .runtime_resume = pm_generic_runtime_resume,
  768. USE_PLATFORM_PM_SLEEP_OPS
  769. };
  770. struct bus_type platform_bus_type = {
  771. .name = "platform",
  772. .dev_groups = platform_dev_groups,
  773. .match = platform_match,
  774. .uevent = platform_uevent,
  775. .pm = &platform_dev_pm_ops,
  776. };
  777. EXPORT_SYMBOL_GPL(platform_bus_type);
  778. int __init platform_bus_init(void)
  779. {
  780. int error;
  781. early_platform_cleanup();
  782. error = device_register(&platform_bus);
  783. if (error)
  784. return error;
  785. error = bus_register(&platform_bus_type);
  786. if (error)
  787. device_unregister(&platform_bus);
  788. return error;
  789. }
  790. #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
  791. u64 dma_get_required_mask(struct device *dev)
  792. {
  793. u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
  794. u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
  795. u64 mask;
  796. if (!high_totalram) {
  797. /* convert to mask just covering totalram */
  798. low_totalram = (1 << (fls(low_totalram) - 1));
  799. low_totalram += low_totalram - 1;
  800. mask = low_totalram;
  801. } else {
  802. high_totalram = (1 << (fls(high_totalram) - 1));
  803. high_totalram += high_totalram - 1;
  804. mask = (((u64)high_totalram) << 32) + 0xffffffff;
  805. }
  806. return mask;
  807. }
  808. EXPORT_SYMBOL_GPL(dma_get_required_mask);
  809. #endif
  810. static __initdata LIST_HEAD(early_platform_driver_list);
  811. static __initdata LIST_HEAD(early_platform_device_list);
  812. /**
  813. * early_platform_driver_register - register early platform driver
  814. * @epdrv: early_platform driver structure
  815. * @buf: string passed from early_param()
  816. *
  817. * Helper function for early_platform_init() / early_platform_init_buffer()
  818. */
  819. int __init early_platform_driver_register(struct early_platform_driver *epdrv,
  820. char *buf)
  821. {
  822. char *tmp;
  823. int n;
  824. /* Simply add the driver to the end of the global list.
  825. * Drivers will by default be put on the list in compiled-in order.
  826. */
  827. if (!epdrv->list.next) {
  828. INIT_LIST_HEAD(&epdrv->list);
  829. list_add_tail(&epdrv->list, &early_platform_driver_list);
  830. }
  831. /* If the user has specified device then make sure the driver
  832. * gets prioritized. The driver of the last device specified on
  833. * command line will be put first on the list.
  834. */
  835. n = strlen(epdrv->pdrv->driver.name);
  836. if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) {
  837. list_move(&epdrv->list, &early_platform_driver_list);
  838. /* Allow passing parameters after device name */
  839. if (buf[n] == '\0' || buf[n] == ',')
  840. epdrv->requested_id = -1;
  841. else {
  842. epdrv->requested_id = simple_strtoul(&buf[n + 1],
  843. &tmp, 10);
  844. if (buf[n] != '.' || (tmp == &buf[n + 1])) {
  845. epdrv->requested_id = EARLY_PLATFORM_ID_ERROR;
  846. n = 0;
  847. } else
  848. n += strcspn(&buf[n + 1], ",") + 1;
  849. }
  850. if (buf[n] == ',')
  851. n++;
  852. if (epdrv->bufsize) {
  853. memcpy(epdrv->buffer, &buf[n],
  854. min_t(int, epdrv->bufsize, strlen(&buf[n]) + 1));
  855. epdrv->buffer[epdrv->bufsize - 1] = '\0';
  856. }
  857. }
  858. return 0;
  859. }
  860. /**
  861. * early_platform_add_devices - adds a number of early platform devices
  862. * @devs: array of early platform devices to add
  863. * @num: number of early platform devices in array
  864. *
  865. * Used by early architecture code to register early platform devices and
  866. * their platform data.
  867. */
  868. void __init early_platform_add_devices(struct platform_device **devs, int num)
  869. {
  870. struct device *dev;
  871. int i;
  872. /* simply add the devices to list */
  873. for (i = 0; i < num; i++) {
  874. dev = &devs[i]->dev;
  875. if (!dev->devres_head.next) {
  876. pm_runtime_early_init(dev);
  877. INIT_LIST_HEAD(&dev->devres_head);
  878. list_add_tail(&dev->devres_head,
  879. &early_platform_device_list);
  880. }
  881. }
  882. }
  883. /**
  884. * early_platform_driver_register_all - register early platform drivers
  885. * @class_str: string to identify early platform driver class
  886. *
  887. * Used by architecture code to register all early platform drivers
  888. * for a certain class. If omitted then only early platform drivers
  889. * with matching kernel command line class parameters will be registered.
  890. */
  891. void __init early_platform_driver_register_all(char *class_str)
  892. {
  893. /* The "class_str" parameter may or may not be present on the kernel
  894. * command line. If it is present then there may be more than one
  895. * matching parameter.
  896. *
  897. * Since we register our early platform drivers using early_param()
  898. * we need to make sure that they also get registered in the case
  899. * when the parameter is missing from the kernel command line.
  900. *
  901. * We use parse_early_options() to make sure the early_param() gets
  902. * called at least once. The early_param() may be called more than
  903. * once since the name of the preferred device may be specified on
  904. * the kernel command line. early_platform_driver_register() handles
  905. * this case for us.
  906. */
  907. parse_early_options(class_str);
  908. }
  909. /**
  910. * early_platform_match - find early platform device matching driver
  911. * @epdrv: early platform driver structure
  912. * @id: id to match against
  913. */
  914. static struct platform_device * __init
  915. early_platform_match(struct early_platform_driver *epdrv, int id)
  916. {
  917. struct platform_device *pd;
  918. list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
  919. if (platform_match(&pd->dev, &epdrv->pdrv->driver))
  920. if (pd->id == id)
  921. return pd;
  922. return NULL;
  923. }
  924. /**
  925. * early_platform_left - check if early platform driver has matching devices
  926. * @epdrv: early platform driver structure
  927. * @id: return true if id or above exists
  928. */
  929. static int __init early_platform_left(struct early_platform_driver *epdrv,
  930. int id)
  931. {
  932. struct platform_device *pd;
  933. list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
  934. if (platform_match(&pd->dev, &epdrv->pdrv->driver))
  935. if (pd->id >= id)
  936. return 1;
  937. return 0;
  938. }
  939. /**
  940. * early_platform_driver_probe_id - probe drivers matching class_str and id
  941. * @class_str: string to identify early platform driver class
  942. * @id: id to match against
  943. * @nr_probe: number of platform devices to successfully probe before exiting
  944. */
  945. static int __init early_platform_driver_probe_id(char *class_str,
  946. int id,
  947. int nr_probe)
  948. {
  949. struct early_platform_driver *epdrv;
  950. struct platform_device *match;
  951. int match_id;
  952. int n = 0;
  953. int left = 0;
  954. list_for_each_entry(epdrv, &early_platform_driver_list, list) {
  955. /* only use drivers matching our class_str */
  956. if (strcmp(class_str, epdrv->class_str))
  957. continue;
  958. if (id == -2) {
  959. match_id = epdrv->requested_id;
  960. left = 1;
  961. } else {
  962. match_id = id;
  963. left += early_platform_left(epdrv, id);
  964. /* skip requested id */
  965. switch (epdrv->requested_id) {
  966. case EARLY_PLATFORM_ID_ERROR:
  967. case EARLY_PLATFORM_ID_UNSET:
  968. break;
  969. default:
  970. if (epdrv->requested_id == id)
  971. match_id = EARLY_PLATFORM_ID_UNSET;
  972. }
  973. }
  974. switch (match_id) {
  975. case EARLY_PLATFORM_ID_ERROR:
  976. pr_warn("%s: unable to parse %s parameter\n",
  977. class_str, epdrv->pdrv->driver.name);
  978. /* fall-through */
  979. case EARLY_PLATFORM_ID_UNSET:
  980. match = NULL;
  981. break;
  982. default:
  983. match = early_platform_match(epdrv, match_id);
  984. }
  985. if (match) {
  986. /*
  987. * Set up a sensible init_name to enable
  988. * dev_name() and others to be used before the
  989. * rest of the driver core is initialized.
  990. */
  991. if (!match->dev.init_name && slab_is_available()) {
  992. if (match->id != -1)
  993. match->dev.init_name =
  994. kasprintf(GFP_KERNEL, "%s.%d",
  995. match->name,
  996. match->id);
  997. else
  998. match->dev.init_name =
  999. kasprintf(GFP_KERNEL, "%s",
  1000. match->name);
  1001. if (!match->dev.init_name)
  1002. return -ENOMEM;
  1003. }
  1004. if (epdrv->pdrv->probe(match))
  1005. pr_warn("%s: unable to probe %s early.\n",
  1006. class_str, match->name);
  1007. else
  1008. n++;
  1009. }
  1010. if (n >= nr_probe)
  1011. break;
  1012. }
  1013. if (left)
  1014. return n;
  1015. else
  1016. return -ENODEV;
  1017. }
  1018. /**
  1019. * early_platform_driver_probe - probe a class of registered drivers
  1020. * @class_str: string to identify early platform driver class
  1021. * @nr_probe: number of platform devices to successfully probe before exiting
  1022. * @user_only: only probe user specified early platform devices
  1023. *
  1024. * Used by architecture code to probe registered early platform drivers
  1025. * within a certain class. For probe to happen a registered early platform
  1026. * device matching a registered early platform driver is needed.
  1027. */
  1028. int __init early_platform_driver_probe(char *class_str,
  1029. int nr_probe,
  1030. int user_only)
  1031. {
  1032. int k, n, i;
  1033. n = 0;
  1034. for (i = -2; n < nr_probe; i++) {
  1035. k = early_platform_driver_probe_id(class_str, i, nr_probe - n);
  1036. if (k < 0)
  1037. break;
  1038. n += k;
  1039. if (user_only)
  1040. break;
  1041. }
  1042. return n;
  1043. }
  1044. /**
  1045. * early_platform_cleanup - clean up early platform code
  1046. */
  1047. void __init early_platform_cleanup(void)
  1048. {
  1049. struct platform_device *pd, *pd2;
  1050. /* clean up the devres list used to chain devices */
  1051. list_for_each_entry_safe(pd, pd2, &early_platform_device_list,
  1052. dev.devres_head) {
  1053. list_del(&pd->dev.devres_head);
  1054. memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));
  1055. }
  1056. }