platform.c 30 KB

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