platform.c 35 KB

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