platform.c 37 KB

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