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. * Please see Documentation/driver-model/platform.txt for more
  9. * information.
  10. */
  11. #include <linux/string.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/of_device.h>
  14. #include <linux/of_irq.h>
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/bootmem.h>
  19. #include <linux/err.h>
  20. #include <linux/slab.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/pm_domain.h>
  23. #include <linux/idr.h>
  24. #include <linux/acpi.h>
  25. #include <linux/clk/clk-conf.h>
  26. #include <linux/limits.h>
  27. #include <linux/property.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. if (has_acpi_companion(&dev->dev)) {
  94. if (r && r->flags & IORESOURCE_DISABLED) {
  95. int ret;
  96. ret = acpi_irq_get(ACPI_HANDLE(&dev->dev), num, r);
  97. if (ret)
  98. return ret;
  99. }
  100. }
  101. /*
  102. * The resources may pass trigger flags to the irqs that need
  103. * to be set up. It so happens that the trigger flags for
  104. * IORESOURCE_BITS correspond 1-to-1 to the IRQF_TRIGGER*
  105. * settings.
  106. */
  107. if (r && r->flags & IORESOURCE_BITS) {
  108. struct irq_data *irqd;
  109. irqd = irq_get_irq_data(r->start);
  110. if (!irqd)
  111. return -ENXIO;
  112. irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
  113. }
  114. return r ? r->start : -ENXIO;
  115. #endif
  116. }
  117. EXPORT_SYMBOL_GPL(platform_get_irq);
  118. /**
  119. * platform_irq_count - Count the number of IRQs a platform device uses
  120. * @dev: platform device
  121. *
  122. * Return: Number of IRQs a platform device uses or EPROBE_DEFER
  123. */
  124. int platform_irq_count(struct platform_device *dev)
  125. {
  126. int ret, nr = 0;
  127. while ((ret = platform_get_irq(dev, nr)) >= 0)
  128. nr++;
  129. if (ret == -EPROBE_DEFER)
  130. return ret;
  131. return nr;
  132. }
  133. EXPORT_SYMBOL_GPL(platform_irq_count);
  134. /**
  135. * platform_get_resource_byname - get a resource for a device by name
  136. * @dev: platform device
  137. * @type: resource type
  138. * @name: resource name
  139. */
  140. struct resource *platform_get_resource_byname(struct platform_device *dev,
  141. unsigned int type,
  142. const char *name)
  143. {
  144. int i;
  145. for (i = 0; i < dev->num_resources; i++) {
  146. struct resource *r = &dev->resource[i];
  147. if (unlikely(!r->name))
  148. continue;
  149. if (type == resource_type(r) && !strcmp(r->name, name))
  150. return r;
  151. }
  152. return NULL;
  153. }
  154. EXPORT_SYMBOL_GPL(platform_get_resource_byname);
  155. /**
  156. * platform_get_irq_byname - get an IRQ for a device by name
  157. * @dev: platform device
  158. * @name: IRQ name
  159. */
  160. int platform_get_irq_byname(struct platform_device *dev, const char *name)
  161. {
  162. struct resource *r;
  163. if (IS_ENABLED(CONFIG_OF_IRQ) && dev->dev.of_node) {
  164. int ret;
  165. ret = of_irq_get_byname(dev->dev.of_node, name);
  166. if (ret > 0 || ret == -EPROBE_DEFER)
  167. return ret;
  168. }
  169. r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
  170. return r ? r->start : -ENXIO;
  171. }
  172. EXPORT_SYMBOL_GPL(platform_get_irq_byname);
  173. /**
  174. * platform_add_devices - add a numbers of platform devices
  175. * @devs: array of platform devices to add
  176. * @num: number of platform devices in array
  177. */
  178. int platform_add_devices(struct platform_device **devs, int num)
  179. {
  180. int i, ret = 0;
  181. for (i = 0; i < num; i++) {
  182. ret = platform_device_register(devs[i]);
  183. if (ret) {
  184. while (--i >= 0)
  185. platform_device_unregister(devs[i]);
  186. break;
  187. }
  188. }
  189. return ret;
  190. }
  191. EXPORT_SYMBOL_GPL(platform_add_devices);
  192. struct platform_object {
  193. struct platform_device pdev;
  194. char name[];
  195. };
  196. /**
  197. * platform_device_put - destroy a platform device
  198. * @pdev: platform device to free
  199. *
  200. * Free all memory associated with a platform device. This function must
  201. * _only_ be externally called in error cases. All other usage is a bug.
  202. */
  203. void platform_device_put(struct platform_device *pdev)
  204. {
  205. if (pdev)
  206. put_device(&pdev->dev);
  207. }
  208. EXPORT_SYMBOL_GPL(platform_device_put);
  209. static void platform_device_release(struct device *dev)
  210. {
  211. struct platform_object *pa = container_of(dev, struct platform_object,
  212. pdev.dev);
  213. of_device_node_put(&pa->pdev.dev);
  214. kfree(pa->pdev.dev.platform_data);
  215. kfree(pa->pdev.mfd_cell);
  216. kfree(pa->pdev.resource);
  217. kfree(pa->pdev.driver_override);
  218. kfree(pa);
  219. }
  220. /**
  221. * platform_device_alloc - create a platform device
  222. * @name: base name of the device we're adding
  223. * @id: instance id
  224. *
  225. * Create a platform device object which can have other objects attached
  226. * to it, and which will have attached objects freed when it is released.
  227. */
  228. struct platform_device *platform_device_alloc(const char *name, int id)
  229. {
  230. struct platform_object *pa;
  231. pa = kzalloc(sizeof(*pa) + strlen(name) + 1, GFP_KERNEL);
  232. if (pa) {
  233. strcpy(pa->name, name);
  234. pa->pdev.name = pa->name;
  235. pa->pdev.id = id;
  236. device_initialize(&pa->pdev.dev);
  237. pa->pdev.dev.release = platform_device_release;
  238. arch_setup_pdev_archdata(&pa->pdev);
  239. }
  240. return pa ? &pa->pdev : NULL;
  241. }
  242. EXPORT_SYMBOL_GPL(platform_device_alloc);
  243. /**
  244. * platform_device_add_resources - add resources to a platform device
  245. * @pdev: platform device allocated by platform_device_alloc to add resources to
  246. * @res: set of resources that needs to be allocated for the device
  247. * @num: number of resources
  248. *
  249. * Add a copy of the resources to the platform device. The memory
  250. * associated with the resources will be freed when the platform device is
  251. * released.
  252. */
  253. int platform_device_add_resources(struct platform_device *pdev,
  254. const struct resource *res, unsigned int num)
  255. {
  256. struct resource *r = NULL;
  257. if (res) {
  258. r = kmemdup(res, sizeof(struct resource) * num, GFP_KERNEL);
  259. if (!r)
  260. return -ENOMEM;
  261. }
  262. kfree(pdev->resource);
  263. pdev->resource = r;
  264. pdev->num_resources = num;
  265. return 0;
  266. }
  267. EXPORT_SYMBOL_GPL(platform_device_add_resources);
  268. /**
  269. * platform_device_add_data - add platform-specific data to a platform device
  270. * @pdev: platform device allocated by platform_device_alloc to add resources to
  271. * @data: platform specific data for this platform device
  272. * @size: size of platform specific data
  273. *
  274. * Add a copy of platform specific data to the platform device's
  275. * platform_data pointer. The memory associated with the platform data
  276. * will be freed when the platform device is released.
  277. */
  278. int platform_device_add_data(struct platform_device *pdev, const void *data,
  279. size_t size)
  280. {
  281. void *d = NULL;
  282. if (data) {
  283. d = kmemdup(data, size, GFP_KERNEL);
  284. if (!d)
  285. return -ENOMEM;
  286. }
  287. kfree(pdev->dev.platform_data);
  288. pdev->dev.platform_data = d;
  289. return 0;
  290. }
  291. EXPORT_SYMBOL_GPL(platform_device_add_data);
  292. /**
  293. * platform_device_add_properties - add built-in properties to a platform device
  294. * @pdev: platform device to add properties to
  295. * @properties: null terminated array of properties to add
  296. *
  297. * The function will take deep copy of @properties and attach the copy to the
  298. * platform device. The memory associated with properties will be freed when the
  299. * platform device is released.
  300. */
  301. int platform_device_add_properties(struct platform_device *pdev,
  302. const struct property_entry *properties)
  303. {
  304. return device_add_properties(&pdev->dev, properties);
  305. }
  306. EXPORT_SYMBOL_GPL(platform_device_add_properties);
  307. /**
  308. * platform_device_add - add a platform device to device hierarchy
  309. * @pdev: platform device we're adding
  310. *
  311. * This is part 2 of platform_device_register(), though may be called
  312. * separately _iff_ pdev was allocated by platform_device_alloc().
  313. */
  314. int platform_device_add(struct platform_device *pdev)
  315. {
  316. int i, ret;
  317. if (!pdev)
  318. return -EINVAL;
  319. if (!pdev->dev.parent)
  320. pdev->dev.parent = &platform_bus;
  321. pdev->dev.bus = &platform_bus_type;
  322. switch (pdev->id) {
  323. default:
  324. dev_set_name(&pdev->dev, "%s.%d", pdev->name, pdev->id);
  325. break;
  326. case PLATFORM_DEVID_NONE:
  327. dev_set_name(&pdev->dev, "%s", pdev->name);
  328. break;
  329. case PLATFORM_DEVID_AUTO:
  330. /*
  331. * Automatically allocated device ID. We mark it as such so
  332. * that we remember it must be freed, and we append a suffix
  333. * to avoid namespace collision with explicit IDs.
  334. */
  335. ret = ida_simple_get(&platform_devid_ida, 0, 0, GFP_KERNEL);
  336. if (ret < 0)
  337. goto err_out;
  338. pdev->id = ret;
  339. pdev->id_auto = true;
  340. dev_set_name(&pdev->dev, "%s.%d.auto", pdev->name, pdev->id);
  341. break;
  342. }
  343. for (i = 0; i < pdev->num_resources; i++) {
  344. struct resource *p, *r = &pdev->resource[i];
  345. if (r->name == NULL)
  346. r->name = dev_name(&pdev->dev);
  347. p = r->parent;
  348. if (!p) {
  349. if (resource_type(r) == IORESOURCE_MEM)
  350. p = &iomem_resource;
  351. else if (resource_type(r) == IORESOURCE_IO)
  352. p = &ioport_resource;
  353. }
  354. if (p && insert_resource(p, r)) {
  355. dev_err(&pdev->dev, "failed to claim resource %d: %pR\n", i, r);
  356. ret = -EBUSY;
  357. goto failed;
  358. }
  359. }
  360. pr_debug("Registering platform device '%s'. Parent at %s\n",
  361. dev_name(&pdev->dev), dev_name(pdev->dev.parent));
  362. ret = device_add(&pdev->dev);
  363. if (ret == 0)
  364. return ret;
  365. failed:
  366. if (pdev->id_auto) {
  367. ida_simple_remove(&platform_devid_ida, pdev->id);
  368. pdev->id = PLATFORM_DEVID_AUTO;
  369. }
  370. while (--i >= 0) {
  371. struct resource *r = &pdev->resource[i];
  372. if (r->parent)
  373. release_resource(r);
  374. }
  375. err_out:
  376. return ret;
  377. }
  378. EXPORT_SYMBOL_GPL(platform_device_add);
  379. /**
  380. * platform_device_del - remove a platform-level device
  381. * @pdev: platform device we're removing
  382. *
  383. * Note that this function will also release all memory- and port-based
  384. * resources owned by the device (@dev->resource). This function must
  385. * _only_ be externally called in error cases. All other usage is a bug.
  386. */
  387. void platform_device_del(struct platform_device *pdev)
  388. {
  389. int i;
  390. if (pdev) {
  391. device_remove_properties(&pdev->dev);
  392. device_del(&pdev->dev);
  393. if (pdev->id_auto) {
  394. ida_simple_remove(&platform_devid_ida, pdev->id);
  395. pdev->id = PLATFORM_DEVID_AUTO;
  396. }
  397. for (i = 0; i < pdev->num_resources; i++) {
  398. struct resource *r = &pdev->resource[i];
  399. if (r->parent)
  400. release_resource(r);
  401. }
  402. }
  403. }
  404. EXPORT_SYMBOL_GPL(platform_device_del);
  405. /**
  406. * platform_device_register - add a platform-level device
  407. * @pdev: platform device we're adding
  408. */
  409. int platform_device_register(struct platform_device *pdev)
  410. {
  411. device_initialize(&pdev->dev);
  412. arch_setup_pdev_archdata(pdev);
  413. return platform_device_add(pdev);
  414. }
  415. EXPORT_SYMBOL_GPL(platform_device_register);
  416. /**
  417. * platform_device_unregister - unregister a platform-level device
  418. * @pdev: platform device we're unregistering
  419. *
  420. * Unregistration is done in 2 steps. First we release all resources
  421. * and remove it from the subsystem, then we drop reference count by
  422. * calling platform_device_put().
  423. */
  424. void platform_device_unregister(struct platform_device *pdev)
  425. {
  426. platform_device_del(pdev);
  427. platform_device_put(pdev);
  428. }
  429. EXPORT_SYMBOL_GPL(platform_device_unregister);
  430. /**
  431. * platform_device_register_full - add a platform-level device with
  432. * resources and platform-specific data
  433. *
  434. * @pdevinfo: data used to create device
  435. *
  436. * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
  437. */
  438. struct platform_device *platform_device_register_full(
  439. const struct platform_device_info *pdevinfo)
  440. {
  441. int ret = -ENOMEM;
  442. struct platform_device *pdev;
  443. pdev = platform_device_alloc(pdevinfo->name, pdevinfo->id);
  444. if (!pdev)
  445. goto err_alloc;
  446. pdev->dev.parent = pdevinfo->parent;
  447. pdev->dev.fwnode = pdevinfo->fwnode;
  448. if (pdevinfo->dma_mask) {
  449. /*
  450. * This memory isn't freed when the device is put,
  451. * I don't have a nice idea for that though. Conceptually
  452. * dma_mask in struct device should not be a pointer.
  453. * See http://thread.gmane.org/gmane.linux.kernel.pci/9081
  454. */
  455. pdev->dev.dma_mask =
  456. kmalloc(sizeof(*pdev->dev.dma_mask), GFP_KERNEL);
  457. if (!pdev->dev.dma_mask)
  458. goto err;
  459. *pdev->dev.dma_mask = pdevinfo->dma_mask;
  460. pdev->dev.coherent_dma_mask = pdevinfo->dma_mask;
  461. }
  462. ret = platform_device_add_resources(pdev,
  463. pdevinfo->res, pdevinfo->num_res);
  464. if (ret)
  465. goto err;
  466. ret = platform_device_add_data(pdev,
  467. pdevinfo->data, pdevinfo->size_data);
  468. if (ret)
  469. goto err;
  470. if (pdevinfo->properties) {
  471. ret = platform_device_add_properties(pdev,
  472. pdevinfo->properties);
  473. if (ret)
  474. goto err;
  475. }
  476. ret = platform_device_add(pdev);
  477. if (ret) {
  478. err:
  479. ACPI_COMPANION_SET(&pdev->dev, NULL);
  480. kfree(pdev->dev.dma_mask);
  481. err_alloc:
  482. platform_device_put(pdev);
  483. return ERR_PTR(ret);
  484. }
  485. return pdev;
  486. }
  487. EXPORT_SYMBOL_GPL(platform_device_register_full);
  488. static int platform_drv_probe(struct device *_dev)
  489. {
  490. struct platform_driver *drv = to_platform_driver(_dev->driver);
  491. struct platform_device *dev = to_platform_device(_dev);
  492. int ret;
  493. ret = of_clk_set_defaults(_dev->of_node, false);
  494. if (ret < 0)
  495. return ret;
  496. ret = dev_pm_domain_attach(_dev, true);
  497. if (ret != -EPROBE_DEFER) {
  498. if (drv->probe) {
  499. ret = drv->probe(dev);
  500. if (ret)
  501. dev_pm_domain_detach(_dev, true);
  502. } else {
  503. /* don't fail if just dev_pm_domain_attach failed */
  504. ret = 0;
  505. }
  506. }
  507. if (drv->prevent_deferred_probe && ret == -EPROBE_DEFER) {
  508. dev_warn(_dev, "probe deferral not supported\n");
  509. ret = -ENXIO;
  510. }
  511. return ret;
  512. }
  513. static int platform_drv_probe_fail(struct device *_dev)
  514. {
  515. return -ENXIO;
  516. }
  517. static int platform_drv_remove(struct device *_dev)
  518. {
  519. struct platform_driver *drv = to_platform_driver(_dev->driver);
  520. struct platform_device *dev = to_platform_device(_dev);
  521. int ret = 0;
  522. if (drv->remove)
  523. ret = drv->remove(dev);
  524. dev_pm_domain_detach(_dev, true);
  525. return ret;
  526. }
  527. static void platform_drv_shutdown(struct device *_dev)
  528. {
  529. struct platform_driver *drv = to_platform_driver(_dev->driver);
  530. struct platform_device *dev = to_platform_device(_dev);
  531. if (drv->shutdown)
  532. drv->shutdown(dev);
  533. }
  534. /**
  535. * __platform_driver_register - register a driver for platform-level devices
  536. * @drv: platform driver structure
  537. * @owner: owning module/driver
  538. */
  539. int __platform_driver_register(struct platform_driver *drv,
  540. struct module *owner)
  541. {
  542. drv->driver.owner = owner;
  543. drv->driver.bus = &platform_bus_type;
  544. drv->driver.probe = platform_drv_probe;
  545. drv->driver.remove = platform_drv_remove;
  546. drv->driver.shutdown = platform_drv_shutdown;
  547. return driver_register(&drv->driver);
  548. }
  549. EXPORT_SYMBOL_GPL(__platform_driver_register);
  550. /**
  551. * platform_driver_unregister - unregister a driver for platform-level devices
  552. * @drv: platform driver structure
  553. */
  554. void platform_driver_unregister(struct platform_driver *drv)
  555. {
  556. driver_unregister(&drv->driver);
  557. }
  558. EXPORT_SYMBOL_GPL(platform_driver_unregister);
  559. /**
  560. * __platform_driver_probe - register driver for non-hotpluggable device
  561. * @drv: platform driver structure
  562. * @probe: the driver probe routine, probably from an __init section
  563. * @module: module which will be the owner of the driver
  564. *
  565. * Use this instead of platform_driver_register() when you know the device
  566. * is not hotpluggable and has already been registered, and you want to
  567. * remove its run-once probe() infrastructure from memory after the driver
  568. * has bound to the device.
  569. *
  570. * One typical use for this would be with drivers for controllers integrated
  571. * into system-on-chip processors, where the controller devices have been
  572. * configured as part of board setup.
  573. *
  574. * Note that this is incompatible with deferred probing.
  575. *
  576. * Returns zero if the driver registered and bound to a device, else returns
  577. * a negative error code and with the driver not registered.
  578. */
  579. int __init_or_module __platform_driver_probe(struct platform_driver *drv,
  580. int (*probe)(struct platform_device *), struct module *module)
  581. {
  582. int retval, code;
  583. if (drv->driver.probe_type == PROBE_PREFER_ASYNCHRONOUS) {
  584. pr_err("%s: drivers registered with %s can not be probed asynchronously\n",
  585. drv->driver.name, __func__);
  586. return -EINVAL;
  587. }
  588. /*
  589. * We have to run our probes synchronously because we check if
  590. * we find any devices to bind to and exit with error if there
  591. * are any.
  592. */
  593. drv->driver.probe_type = PROBE_FORCE_SYNCHRONOUS;
  594. /*
  595. * Prevent driver from requesting probe deferral to avoid further
  596. * futile probe attempts.
  597. */
  598. drv->prevent_deferred_probe = true;
  599. /* make sure driver won't have bind/unbind attributes */
  600. drv->driver.suppress_bind_attrs = true;
  601. /* temporary section violation during probe() */
  602. drv->probe = probe;
  603. retval = code = __platform_driver_register(drv, module);
  604. /*
  605. * Fixup that section violation, being paranoid about code scanning
  606. * the list of drivers in order to probe new devices. Check to see
  607. * if the probe was successful, and make sure any forced probes of
  608. * new devices fail.
  609. */
  610. spin_lock(&drv->driver.bus->p->klist_drivers.k_lock);
  611. drv->probe = NULL;
  612. if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
  613. retval = -ENODEV;
  614. drv->driver.probe = platform_drv_probe_fail;
  615. spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock);
  616. if (code != retval)
  617. platform_driver_unregister(drv);
  618. return retval;
  619. }
  620. EXPORT_SYMBOL_GPL(__platform_driver_probe);
  621. /**
  622. * __platform_create_bundle - register driver and create corresponding device
  623. * @driver: platform driver structure
  624. * @probe: the driver probe routine, probably from an __init section
  625. * @res: set of resources that needs to be allocated for the device
  626. * @n_res: number of resources
  627. * @data: platform specific data for this platform device
  628. * @size: size of platform specific data
  629. * @module: module which will be the owner of the driver
  630. *
  631. * Use this in legacy-style modules that probe hardware directly and
  632. * register a single platform device and corresponding platform driver.
  633. *
  634. * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
  635. */
  636. struct platform_device * __init_or_module __platform_create_bundle(
  637. struct platform_driver *driver,
  638. int (*probe)(struct platform_device *),
  639. struct resource *res, unsigned int n_res,
  640. const void *data, size_t size, struct module *module)
  641. {
  642. struct platform_device *pdev;
  643. int error;
  644. pdev = platform_device_alloc(driver->driver.name, -1);
  645. if (!pdev) {
  646. error = -ENOMEM;
  647. goto err_out;
  648. }
  649. error = platform_device_add_resources(pdev, res, n_res);
  650. if (error)
  651. goto err_pdev_put;
  652. error = platform_device_add_data(pdev, data, size);
  653. if (error)
  654. goto err_pdev_put;
  655. error = platform_device_add(pdev);
  656. if (error)
  657. goto err_pdev_put;
  658. error = __platform_driver_probe(driver, probe, module);
  659. if (error)
  660. goto err_pdev_del;
  661. return pdev;
  662. err_pdev_del:
  663. platform_device_del(pdev);
  664. err_pdev_put:
  665. platform_device_put(pdev);
  666. err_out:
  667. return ERR_PTR(error);
  668. }
  669. EXPORT_SYMBOL_GPL(__platform_create_bundle);
  670. /**
  671. * __platform_register_drivers - register an array of platform drivers
  672. * @drivers: an array of drivers to register
  673. * @count: the number of drivers to register
  674. * @owner: module owning the drivers
  675. *
  676. * Registers platform drivers specified by an array. On failure to register a
  677. * driver, all previously registered drivers will be unregistered. Callers of
  678. * this API should use platform_unregister_drivers() to unregister drivers in
  679. * the reverse order.
  680. *
  681. * Returns: 0 on success or a negative error code on failure.
  682. */
  683. int __platform_register_drivers(struct platform_driver * const *drivers,
  684. unsigned int count, struct module *owner)
  685. {
  686. unsigned int i;
  687. int err;
  688. for (i = 0; i < count; i++) {
  689. pr_debug("registering platform driver %ps\n", drivers[i]);
  690. err = __platform_driver_register(drivers[i], owner);
  691. if (err < 0) {
  692. pr_err("failed to register platform driver %ps: %d\n",
  693. drivers[i], err);
  694. goto error;
  695. }
  696. }
  697. return 0;
  698. error:
  699. while (i--) {
  700. pr_debug("unregistering platform driver %ps\n", drivers[i]);
  701. platform_driver_unregister(drivers[i]);
  702. }
  703. return err;
  704. }
  705. EXPORT_SYMBOL_GPL(__platform_register_drivers);
  706. /**
  707. * platform_unregister_drivers - unregister an array of platform drivers
  708. * @drivers: an array of drivers to unregister
  709. * @count: the number of drivers to unregister
  710. *
  711. * Unegisters platform drivers specified by an array. This is typically used
  712. * to complement an earlier call to platform_register_drivers(). Drivers are
  713. * unregistered in the reverse order in which they were registered.
  714. */
  715. void platform_unregister_drivers(struct platform_driver * const *drivers,
  716. unsigned int count)
  717. {
  718. while (count--) {
  719. pr_debug("unregistering platform driver %ps\n", drivers[count]);
  720. platform_driver_unregister(drivers[count]);
  721. }
  722. }
  723. EXPORT_SYMBOL_GPL(platform_unregister_drivers);
  724. /* modalias support enables more hands-off userspace setup:
  725. * (a) environment variable lets new-style hotplug events work once system is
  726. * fully running: "modprobe $MODALIAS"
  727. * (b) sysfs attribute lets new-style coldplug recover from hotplug events
  728. * mishandled before system is fully running: "modprobe $(cat modalias)"
  729. */
  730. static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
  731. char *buf)
  732. {
  733. struct platform_device *pdev = to_platform_device(dev);
  734. int len;
  735. len = of_device_modalias(dev, buf, PAGE_SIZE);
  736. if (len != -ENODEV)
  737. return len;
  738. len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
  739. if (len != -ENODEV)
  740. return len;
  741. len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
  742. return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
  743. }
  744. static DEVICE_ATTR_RO(modalias);
  745. static ssize_t driver_override_store(struct device *dev,
  746. struct device_attribute *attr,
  747. const char *buf, size_t count)
  748. {
  749. struct platform_device *pdev = to_platform_device(dev);
  750. char *driver_override, *old, *cp;
  751. /* We need to keep extra room for a newline */
  752. if (count >= (PAGE_SIZE - 1))
  753. return -EINVAL;
  754. driver_override = kstrndup(buf, count, GFP_KERNEL);
  755. if (!driver_override)
  756. return -ENOMEM;
  757. cp = strchr(driver_override, '\n');
  758. if (cp)
  759. *cp = '\0';
  760. device_lock(dev);
  761. old = pdev->driver_override;
  762. if (strlen(driver_override)) {
  763. pdev->driver_override = driver_override;
  764. } else {
  765. kfree(driver_override);
  766. pdev->driver_override = NULL;
  767. }
  768. device_unlock(dev);
  769. kfree(old);
  770. return count;
  771. }
  772. static ssize_t driver_override_show(struct device *dev,
  773. struct device_attribute *attr, char *buf)
  774. {
  775. struct platform_device *pdev = to_platform_device(dev);
  776. ssize_t len;
  777. device_lock(dev);
  778. len = sprintf(buf, "%s\n", pdev->driver_override);
  779. device_unlock(dev);
  780. return len;
  781. }
  782. static DEVICE_ATTR_RW(driver_override);
  783. static struct attribute *platform_dev_attrs[] = {
  784. &dev_attr_modalias.attr,
  785. &dev_attr_driver_override.attr,
  786. NULL,
  787. };
  788. ATTRIBUTE_GROUPS(platform_dev);
  789. static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
  790. {
  791. struct platform_device *pdev = to_platform_device(dev);
  792. int rc;
  793. /* Some devices have extra OF data and an OF-style MODALIAS */
  794. rc = of_device_uevent_modalias(dev, env);
  795. if (rc != -ENODEV)
  796. return rc;
  797. rc = acpi_device_uevent_modalias(dev, env);
  798. if (rc != -ENODEV)
  799. return rc;
  800. add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
  801. pdev->name);
  802. return 0;
  803. }
  804. static const struct platform_device_id *platform_match_id(
  805. const struct platform_device_id *id,
  806. struct platform_device *pdev)
  807. {
  808. while (id->name[0]) {
  809. if (strcmp(pdev->name, id->name) == 0) {
  810. pdev->id_entry = id;
  811. return id;
  812. }
  813. id++;
  814. }
  815. return NULL;
  816. }
  817. /**
  818. * platform_match - bind platform device to platform driver.
  819. * @dev: device.
  820. * @drv: driver.
  821. *
  822. * Platform device IDs are assumed to be encoded like this:
  823. * "<name><instance>", where <name> is a short description of the type of
  824. * device, like "pci" or "floppy", and <instance> is the enumerated
  825. * instance of the device, like '0' or '42'. Driver IDs are simply
  826. * "<name>". So, extract the <name> from the platform_device structure,
  827. * and compare it against the name of the driver. Return whether they match
  828. * or not.
  829. */
  830. static int platform_match(struct device *dev, struct device_driver *drv)
  831. {
  832. struct platform_device *pdev = to_platform_device(dev);
  833. struct platform_driver *pdrv = to_platform_driver(drv);
  834. /* When driver_override is set, only bind to the matching driver */
  835. if (pdev->driver_override)
  836. return !strcmp(pdev->driver_override, drv->name);
  837. /* Attempt an OF style match first */
  838. if (of_driver_match_device(dev, drv))
  839. return 1;
  840. /* Then try ACPI style match */
  841. if (acpi_driver_match_device(dev, drv))
  842. return 1;
  843. /* Then try to match against the id table */
  844. if (pdrv->id_table)
  845. return platform_match_id(pdrv->id_table, pdev) != NULL;
  846. /* fall-back to driver name match */
  847. return (strcmp(pdev->name, drv->name) == 0);
  848. }
  849. #ifdef CONFIG_PM_SLEEP
  850. static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
  851. {
  852. struct platform_driver *pdrv = to_platform_driver(dev->driver);
  853. struct platform_device *pdev = to_platform_device(dev);
  854. int ret = 0;
  855. if (dev->driver && pdrv->suspend)
  856. ret = pdrv->suspend(pdev, mesg);
  857. return ret;
  858. }
  859. static int platform_legacy_resume(struct device *dev)
  860. {
  861. struct platform_driver *pdrv = to_platform_driver(dev->driver);
  862. struct platform_device *pdev = to_platform_device(dev);
  863. int ret = 0;
  864. if (dev->driver && pdrv->resume)
  865. ret = pdrv->resume(pdev);
  866. return ret;
  867. }
  868. #endif /* CONFIG_PM_SLEEP */
  869. #ifdef CONFIG_SUSPEND
  870. int platform_pm_suspend(struct device *dev)
  871. {
  872. struct device_driver *drv = dev->driver;
  873. int ret = 0;
  874. if (!drv)
  875. return 0;
  876. if (drv->pm) {
  877. if (drv->pm->suspend)
  878. ret = drv->pm->suspend(dev);
  879. } else {
  880. ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
  881. }
  882. return ret;
  883. }
  884. int platform_pm_resume(struct device *dev)
  885. {
  886. struct device_driver *drv = dev->driver;
  887. int ret = 0;
  888. if (!drv)
  889. return 0;
  890. if (drv->pm) {
  891. if (drv->pm->resume)
  892. ret = drv->pm->resume(dev);
  893. } else {
  894. ret = platform_legacy_resume(dev);
  895. }
  896. return ret;
  897. }
  898. #endif /* CONFIG_SUSPEND */
  899. #ifdef CONFIG_HIBERNATE_CALLBACKS
  900. int platform_pm_freeze(struct device *dev)
  901. {
  902. struct device_driver *drv = dev->driver;
  903. int ret = 0;
  904. if (!drv)
  905. return 0;
  906. if (drv->pm) {
  907. if (drv->pm->freeze)
  908. ret = drv->pm->freeze(dev);
  909. } else {
  910. ret = platform_legacy_suspend(dev, PMSG_FREEZE);
  911. }
  912. return ret;
  913. }
  914. int platform_pm_thaw(struct device *dev)
  915. {
  916. struct device_driver *drv = dev->driver;
  917. int ret = 0;
  918. if (!drv)
  919. return 0;
  920. if (drv->pm) {
  921. if (drv->pm->thaw)
  922. ret = drv->pm->thaw(dev);
  923. } else {
  924. ret = platform_legacy_resume(dev);
  925. }
  926. return ret;
  927. }
  928. int platform_pm_poweroff(struct device *dev)
  929. {
  930. struct device_driver *drv = dev->driver;
  931. int ret = 0;
  932. if (!drv)
  933. return 0;
  934. if (drv->pm) {
  935. if (drv->pm->poweroff)
  936. ret = drv->pm->poweroff(dev);
  937. } else {
  938. ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
  939. }
  940. return ret;
  941. }
  942. int platform_pm_restore(struct device *dev)
  943. {
  944. struct device_driver *drv = dev->driver;
  945. int ret = 0;
  946. if (!drv)
  947. return 0;
  948. if (drv->pm) {
  949. if (drv->pm->restore)
  950. ret = drv->pm->restore(dev);
  951. } else {
  952. ret = platform_legacy_resume(dev);
  953. }
  954. return ret;
  955. }
  956. #endif /* CONFIG_HIBERNATE_CALLBACKS */
  957. static const struct dev_pm_ops platform_dev_pm_ops = {
  958. .runtime_suspend = pm_generic_runtime_suspend,
  959. .runtime_resume = pm_generic_runtime_resume,
  960. USE_PLATFORM_PM_SLEEP_OPS
  961. };
  962. struct bus_type platform_bus_type = {
  963. .name = "platform",
  964. .dev_groups = platform_dev_groups,
  965. .match = platform_match,
  966. .uevent = platform_uevent,
  967. .pm = &platform_dev_pm_ops,
  968. .force_dma = true,
  969. };
  970. EXPORT_SYMBOL_GPL(platform_bus_type);
  971. int __init platform_bus_init(void)
  972. {
  973. int error;
  974. early_platform_cleanup();
  975. error = device_register(&platform_bus);
  976. if (error) {
  977. put_device(&platform_bus);
  978. return error;
  979. }
  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. }