platform.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  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. */
  451. int __platform_driver_register(struct platform_driver *drv,
  452. struct module *owner)
  453. {
  454. drv->driver.owner = owner;
  455. drv->driver.bus = &platform_bus_type;
  456. if (drv->probe)
  457. drv->driver.probe = platform_drv_probe;
  458. if (drv->remove)
  459. drv->driver.remove = platform_drv_remove;
  460. if (drv->shutdown)
  461. drv->driver.shutdown = platform_drv_shutdown;
  462. return driver_register(&drv->driver);
  463. }
  464. EXPORT_SYMBOL_GPL(__platform_driver_register);
  465. /**
  466. * platform_driver_unregister - unregister a driver for platform-level devices
  467. * @drv: platform driver structure
  468. */
  469. void platform_driver_unregister(struct platform_driver *drv)
  470. {
  471. driver_unregister(&drv->driver);
  472. }
  473. EXPORT_SYMBOL_GPL(platform_driver_unregister);
  474. /**
  475. * platform_driver_probe - register driver for non-hotpluggable device
  476. * @drv: platform driver structure
  477. * @probe: the driver probe routine, probably from an __init section,
  478. * must not return -EPROBE_DEFER.
  479. *
  480. * Use this instead of platform_driver_register() when you know the device
  481. * is not hotpluggable and has already been registered, and you want to
  482. * remove its run-once probe() infrastructure from memory after the driver
  483. * has bound to the device.
  484. *
  485. * One typical use for this would be with drivers for controllers integrated
  486. * into system-on-chip processors, where the controller devices have been
  487. * configured as part of board setup.
  488. *
  489. * This is incompatible with deferred probing so probe() must not
  490. * return -EPROBE_DEFER.
  491. *
  492. * Returns zero if the driver registered and bound to a device, else returns
  493. * a negative error code and with the driver not registered.
  494. */
  495. int __init_or_module platform_driver_probe(struct platform_driver *drv,
  496. int (*probe)(struct platform_device *))
  497. {
  498. int retval, code;
  499. /* make sure driver won't have bind/unbind attributes */
  500. drv->driver.suppress_bind_attrs = true;
  501. /* temporary section violation during probe() */
  502. drv->probe = probe;
  503. retval = code = platform_driver_register(drv);
  504. /*
  505. * Fixup that section violation, being paranoid about code scanning
  506. * the list of drivers in order to probe new devices. Check to see
  507. * if the probe was successful, and make sure any forced probes of
  508. * new devices fail.
  509. */
  510. spin_lock(&drv->driver.bus->p->klist_drivers.k_lock);
  511. drv->probe = NULL;
  512. if (code == 0 && list_empty(&drv->driver.p->klist_devices.k_list))
  513. retval = -ENODEV;
  514. drv->driver.probe = platform_drv_probe_fail;
  515. spin_unlock(&drv->driver.bus->p->klist_drivers.k_lock);
  516. if (code != retval)
  517. platform_driver_unregister(drv);
  518. return retval;
  519. }
  520. EXPORT_SYMBOL_GPL(platform_driver_probe);
  521. /**
  522. * platform_create_bundle - register driver and create corresponding device
  523. * @driver: platform driver structure
  524. * @probe: the driver probe routine, probably from an __init section
  525. * @res: set of resources that needs to be allocated for the device
  526. * @n_res: number of resources
  527. * @data: platform specific data for this platform device
  528. * @size: size of platform specific data
  529. *
  530. * Use this in legacy-style modules that probe hardware directly and
  531. * register a single platform device and corresponding platform driver.
  532. *
  533. * Returns &struct platform_device pointer on success, or ERR_PTR() on error.
  534. */
  535. struct platform_device * __init_or_module platform_create_bundle(
  536. struct platform_driver *driver,
  537. int (*probe)(struct platform_device *),
  538. struct resource *res, unsigned int n_res,
  539. const void *data, size_t size)
  540. {
  541. struct platform_device *pdev;
  542. int error;
  543. pdev = platform_device_alloc(driver->driver.name, -1);
  544. if (!pdev) {
  545. error = -ENOMEM;
  546. goto err_out;
  547. }
  548. error = platform_device_add_resources(pdev, res, n_res);
  549. if (error)
  550. goto err_pdev_put;
  551. error = platform_device_add_data(pdev, data, size);
  552. if (error)
  553. goto err_pdev_put;
  554. error = platform_device_add(pdev);
  555. if (error)
  556. goto err_pdev_put;
  557. error = platform_driver_probe(driver, probe);
  558. if (error)
  559. goto err_pdev_del;
  560. return pdev;
  561. err_pdev_del:
  562. platform_device_del(pdev);
  563. err_pdev_put:
  564. platform_device_put(pdev);
  565. err_out:
  566. return ERR_PTR(error);
  567. }
  568. EXPORT_SYMBOL_GPL(platform_create_bundle);
  569. /* modalias support enables more hands-off userspace setup:
  570. * (a) environment variable lets new-style hotplug events work once system is
  571. * fully running: "modprobe $MODALIAS"
  572. * (b) sysfs attribute lets new-style coldplug recover from hotplug events
  573. * mishandled before system is fully running: "modprobe $(cat modalias)"
  574. */
  575. static ssize_t modalias_show(struct device *dev, struct device_attribute *a,
  576. char *buf)
  577. {
  578. struct platform_device *pdev = to_platform_device(dev);
  579. int len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
  580. return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
  581. }
  582. static struct device_attribute platform_dev_attrs[] = {
  583. __ATTR_RO(modalias),
  584. __ATTR_NULL,
  585. };
  586. static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
  587. {
  588. struct platform_device *pdev = to_platform_device(dev);
  589. int rc;
  590. /* Some devices have extra OF data and an OF-style MODALIAS */
  591. rc = of_device_uevent_modalias(dev, env);
  592. if (rc != -ENODEV)
  593. return rc;
  594. add_uevent_var(env, "MODALIAS=%s%s", PLATFORM_MODULE_PREFIX,
  595. pdev->name);
  596. return 0;
  597. }
  598. static const struct platform_device_id *platform_match_id(
  599. const struct platform_device_id *id,
  600. struct platform_device *pdev)
  601. {
  602. while (id->name[0]) {
  603. if (strcmp(pdev->name, id->name) == 0) {
  604. pdev->id_entry = id;
  605. return id;
  606. }
  607. id++;
  608. }
  609. return NULL;
  610. }
  611. /**
  612. * platform_match - bind platform device to platform driver.
  613. * @dev: device.
  614. * @drv: driver.
  615. *
  616. * Platform device IDs are assumed to be encoded like this:
  617. * "<name><instance>", where <name> is a short description of the type of
  618. * device, like "pci" or "floppy", and <instance> is the enumerated
  619. * instance of the device, like '0' or '42'. Driver IDs are simply
  620. * "<name>". So, extract the <name> from the platform_device structure,
  621. * and compare it against the name of the driver. Return whether they match
  622. * or not.
  623. */
  624. static int platform_match(struct device *dev, struct device_driver *drv)
  625. {
  626. struct platform_device *pdev = to_platform_device(dev);
  627. struct platform_driver *pdrv = to_platform_driver(drv);
  628. /* Attempt an OF style match first */
  629. if (of_driver_match_device(dev, drv))
  630. return 1;
  631. /* Then try ACPI style match */
  632. if (acpi_driver_match_device(dev, drv))
  633. return 1;
  634. /* Then try to match against the id table */
  635. if (pdrv->id_table)
  636. return platform_match_id(pdrv->id_table, pdev) != NULL;
  637. /* fall-back to driver name match */
  638. return (strcmp(pdev->name, drv->name) == 0);
  639. }
  640. #ifdef CONFIG_PM_SLEEP
  641. static int platform_legacy_suspend(struct device *dev, pm_message_t mesg)
  642. {
  643. struct platform_driver *pdrv = to_platform_driver(dev->driver);
  644. struct platform_device *pdev = to_platform_device(dev);
  645. int ret = 0;
  646. if (dev->driver && pdrv->suspend)
  647. ret = pdrv->suspend(pdev, mesg);
  648. return ret;
  649. }
  650. static int platform_legacy_resume(struct device *dev)
  651. {
  652. struct platform_driver *pdrv = to_platform_driver(dev->driver);
  653. struct platform_device *pdev = to_platform_device(dev);
  654. int ret = 0;
  655. if (dev->driver && pdrv->resume)
  656. ret = pdrv->resume(pdev);
  657. return ret;
  658. }
  659. #endif /* CONFIG_PM_SLEEP */
  660. #ifdef CONFIG_SUSPEND
  661. int platform_pm_suspend(struct device *dev)
  662. {
  663. struct device_driver *drv = dev->driver;
  664. int ret = 0;
  665. if (!drv)
  666. return 0;
  667. if (drv->pm) {
  668. if (drv->pm->suspend)
  669. ret = drv->pm->suspend(dev);
  670. } else {
  671. ret = platform_legacy_suspend(dev, PMSG_SUSPEND);
  672. }
  673. return ret;
  674. }
  675. int platform_pm_resume(struct device *dev)
  676. {
  677. struct device_driver *drv = dev->driver;
  678. int ret = 0;
  679. if (!drv)
  680. return 0;
  681. if (drv->pm) {
  682. if (drv->pm->resume)
  683. ret = drv->pm->resume(dev);
  684. } else {
  685. ret = platform_legacy_resume(dev);
  686. }
  687. return ret;
  688. }
  689. #endif /* CONFIG_SUSPEND */
  690. #ifdef CONFIG_HIBERNATE_CALLBACKS
  691. int platform_pm_freeze(struct device *dev)
  692. {
  693. struct device_driver *drv = dev->driver;
  694. int ret = 0;
  695. if (!drv)
  696. return 0;
  697. if (drv->pm) {
  698. if (drv->pm->freeze)
  699. ret = drv->pm->freeze(dev);
  700. } else {
  701. ret = platform_legacy_suspend(dev, PMSG_FREEZE);
  702. }
  703. return ret;
  704. }
  705. int platform_pm_thaw(struct device *dev)
  706. {
  707. struct device_driver *drv = dev->driver;
  708. int ret = 0;
  709. if (!drv)
  710. return 0;
  711. if (drv->pm) {
  712. if (drv->pm->thaw)
  713. ret = drv->pm->thaw(dev);
  714. } else {
  715. ret = platform_legacy_resume(dev);
  716. }
  717. return ret;
  718. }
  719. int platform_pm_poweroff(struct device *dev)
  720. {
  721. struct device_driver *drv = dev->driver;
  722. int ret = 0;
  723. if (!drv)
  724. return 0;
  725. if (drv->pm) {
  726. if (drv->pm->poweroff)
  727. ret = drv->pm->poweroff(dev);
  728. } else {
  729. ret = platform_legacy_suspend(dev, PMSG_HIBERNATE);
  730. }
  731. return ret;
  732. }
  733. int platform_pm_restore(struct device *dev)
  734. {
  735. struct device_driver *drv = dev->driver;
  736. int ret = 0;
  737. if (!drv)
  738. return 0;
  739. if (drv->pm) {
  740. if (drv->pm->restore)
  741. ret = drv->pm->restore(dev);
  742. } else {
  743. ret = platform_legacy_resume(dev);
  744. }
  745. return ret;
  746. }
  747. #endif /* CONFIG_HIBERNATE_CALLBACKS */
  748. static const struct dev_pm_ops platform_dev_pm_ops = {
  749. .runtime_suspend = pm_generic_runtime_suspend,
  750. .runtime_resume = pm_generic_runtime_resume,
  751. USE_PLATFORM_PM_SLEEP_OPS
  752. };
  753. struct bus_type platform_bus_type = {
  754. .name = "platform",
  755. .dev_attrs = platform_dev_attrs,
  756. .match = platform_match,
  757. .uevent = platform_uevent,
  758. .pm = &platform_dev_pm_ops,
  759. };
  760. EXPORT_SYMBOL_GPL(platform_bus_type);
  761. int __init platform_bus_init(void)
  762. {
  763. int error;
  764. early_platform_cleanup();
  765. error = device_register(&platform_bus);
  766. if (error)
  767. return error;
  768. error = bus_register(&platform_bus_type);
  769. if (error)
  770. device_unregister(&platform_bus);
  771. return error;
  772. }
  773. #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
  774. u64 dma_get_required_mask(struct device *dev)
  775. {
  776. u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
  777. u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
  778. u64 mask;
  779. if (!high_totalram) {
  780. /* convert to mask just covering totalram */
  781. low_totalram = (1 << (fls(low_totalram) - 1));
  782. low_totalram += low_totalram - 1;
  783. mask = low_totalram;
  784. } else {
  785. high_totalram = (1 << (fls(high_totalram) - 1));
  786. high_totalram += high_totalram - 1;
  787. mask = (((u64)high_totalram) << 32) + 0xffffffff;
  788. }
  789. return mask;
  790. }
  791. EXPORT_SYMBOL_GPL(dma_get_required_mask);
  792. #endif
  793. static __initdata LIST_HEAD(early_platform_driver_list);
  794. static __initdata LIST_HEAD(early_platform_device_list);
  795. /**
  796. * early_platform_driver_register - register early platform driver
  797. * @epdrv: early_platform driver structure
  798. * @buf: string passed from early_param()
  799. *
  800. * Helper function for early_platform_init() / early_platform_init_buffer()
  801. */
  802. int __init early_platform_driver_register(struct early_platform_driver *epdrv,
  803. char *buf)
  804. {
  805. char *tmp;
  806. int n;
  807. /* Simply add the driver to the end of the global list.
  808. * Drivers will by default be put on the list in compiled-in order.
  809. */
  810. if (!epdrv->list.next) {
  811. INIT_LIST_HEAD(&epdrv->list);
  812. list_add_tail(&epdrv->list, &early_platform_driver_list);
  813. }
  814. /* If the user has specified device then make sure the driver
  815. * gets prioritized. The driver of the last device specified on
  816. * command line will be put first on the list.
  817. */
  818. n = strlen(epdrv->pdrv->driver.name);
  819. if (buf && !strncmp(buf, epdrv->pdrv->driver.name, n)) {
  820. list_move(&epdrv->list, &early_platform_driver_list);
  821. /* Allow passing parameters after device name */
  822. if (buf[n] == '\0' || buf[n] == ',')
  823. epdrv->requested_id = -1;
  824. else {
  825. epdrv->requested_id = simple_strtoul(&buf[n + 1],
  826. &tmp, 10);
  827. if (buf[n] != '.' || (tmp == &buf[n + 1])) {
  828. epdrv->requested_id = EARLY_PLATFORM_ID_ERROR;
  829. n = 0;
  830. } else
  831. n += strcspn(&buf[n + 1], ",") + 1;
  832. }
  833. if (buf[n] == ',')
  834. n++;
  835. if (epdrv->bufsize) {
  836. memcpy(epdrv->buffer, &buf[n],
  837. min_t(int, epdrv->bufsize, strlen(&buf[n]) + 1));
  838. epdrv->buffer[epdrv->bufsize - 1] = '\0';
  839. }
  840. }
  841. return 0;
  842. }
  843. /**
  844. * early_platform_add_devices - adds a number of early platform devices
  845. * @devs: array of early platform devices to add
  846. * @num: number of early platform devices in array
  847. *
  848. * Used by early architecture code to register early platform devices and
  849. * their platform data.
  850. */
  851. void __init early_platform_add_devices(struct platform_device **devs, int num)
  852. {
  853. struct device *dev;
  854. int i;
  855. /* simply add the devices to list */
  856. for (i = 0; i < num; i++) {
  857. dev = &devs[i]->dev;
  858. if (!dev->devres_head.next) {
  859. pm_runtime_early_init(dev);
  860. INIT_LIST_HEAD(&dev->devres_head);
  861. list_add_tail(&dev->devres_head,
  862. &early_platform_device_list);
  863. }
  864. }
  865. }
  866. /**
  867. * early_platform_driver_register_all - register early platform drivers
  868. * @class_str: string to identify early platform driver class
  869. *
  870. * Used by architecture code to register all early platform drivers
  871. * for a certain class. If omitted then only early platform drivers
  872. * with matching kernel command line class parameters will be registered.
  873. */
  874. void __init early_platform_driver_register_all(char *class_str)
  875. {
  876. /* The "class_str" parameter may or may not be present on the kernel
  877. * command line. If it is present then there may be more than one
  878. * matching parameter.
  879. *
  880. * Since we register our early platform drivers using early_param()
  881. * we need to make sure that they also get registered in the case
  882. * when the parameter is missing from the kernel command line.
  883. *
  884. * We use parse_early_options() to make sure the early_param() gets
  885. * called at least once. The early_param() may be called more than
  886. * once since the name of the preferred device may be specified on
  887. * the kernel command line. early_platform_driver_register() handles
  888. * this case for us.
  889. */
  890. parse_early_options(class_str);
  891. }
  892. /**
  893. * early_platform_match - find early platform device matching driver
  894. * @epdrv: early platform driver structure
  895. * @id: id to match against
  896. */
  897. static __init struct platform_device *
  898. early_platform_match(struct early_platform_driver *epdrv, int id)
  899. {
  900. struct platform_device *pd;
  901. list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
  902. if (platform_match(&pd->dev, &epdrv->pdrv->driver))
  903. if (pd->id == id)
  904. return pd;
  905. return NULL;
  906. }
  907. /**
  908. * early_platform_left - check if early platform driver has matching devices
  909. * @epdrv: early platform driver structure
  910. * @id: return true if id or above exists
  911. */
  912. static __init int early_platform_left(struct early_platform_driver *epdrv,
  913. int id)
  914. {
  915. struct platform_device *pd;
  916. list_for_each_entry(pd, &early_platform_device_list, dev.devres_head)
  917. if (platform_match(&pd->dev, &epdrv->pdrv->driver))
  918. if (pd->id >= id)
  919. return 1;
  920. return 0;
  921. }
  922. /**
  923. * early_platform_driver_probe_id - probe drivers matching class_str and id
  924. * @class_str: string to identify early platform driver class
  925. * @id: id to match against
  926. * @nr_probe: number of platform devices to successfully probe before exiting
  927. */
  928. static int __init early_platform_driver_probe_id(char *class_str,
  929. int id,
  930. int nr_probe)
  931. {
  932. struct early_platform_driver *epdrv;
  933. struct platform_device *match;
  934. int match_id;
  935. int n = 0;
  936. int left = 0;
  937. list_for_each_entry(epdrv, &early_platform_driver_list, list) {
  938. /* only use drivers matching our class_str */
  939. if (strcmp(class_str, epdrv->class_str))
  940. continue;
  941. if (id == -2) {
  942. match_id = epdrv->requested_id;
  943. left = 1;
  944. } else {
  945. match_id = id;
  946. left += early_platform_left(epdrv, id);
  947. /* skip requested id */
  948. switch (epdrv->requested_id) {
  949. case EARLY_PLATFORM_ID_ERROR:
  950. case EARLY_PLATFORM_ID_UNSET:
  951. break;
  952. default:
  953. if (epdrv->requested_id == id)
  954. match_id = EARLY_PLATFORM_ID_UNSET;
  955. }
  956. }
  957. switch (match_id) {
  958. case EARLY_PLATFORM_ID_ERROR:
  959. pr_warn("%s: unable to parse %s parameter\n",
  960. class_str, epdrv->pdrv->driver.name);
  961. /* fall-through */
  962. case EARLY_PLATFORM_ID_UNSET:
  963. match = NULL;
  964. break;
  965. default:
  966. match = early_platform_match(epdrv, match_id);
  967. }
  968. if (match) {
  969. /*
  970. * Set up a sensible init_name to enable
  971. * dev_name() and others to be used before the
  972. * rest of the driver core is initialized.
  973. */
  974. if (!match->dev.init_name && slab_is_available()) {
  975. if (match->id != -1)
  976. match->dev.init_name =
  977. kasprintf(GFP_KERNEL, "%s.%d",
  978. match->name,
  979. match->id);
  980. else
  981. match->dev.init_name =
  982. kasprintf(GFP_KERNEL, "%s",
  983. match->name);
  984. if (!match->dev.init_name)
  985. return -ENOMEM;
  986. }
  987. if (epdrv->pdrv->probe(match))
  988. pr_warn("%s: unable to probe %s early.\n",
  989. class_str, match->name);
  990. else
  991. n++;
  992. }
  993. if (n >= nr_probe)
  994. break;
  995. }
  996. if (left)
  997. return n;
  998. else
  999. return -ENODEV;
  1000. }
  1001. /**
  1002. * early_platform_driver_probe - probe a class of registered drivers
  1003. * @class_str: string to identify early platform driver class
  1004. * @nr_probe: number of platform devices to successfully probe before exiting
  1005. * @user_only: only probe user specified early platform devices
  1006. *
  1007. * Used by architecture code to probe registered early platform drivers
  1008. * within a certain class. For probe to happen a registered early platform
  1009. * device matching a registered early platform driver is needed.
  1010. */
  1011. int __init early_platform_driver_probe(char *class_str,
  1012. int nr_probe,
  1013. int user_only)
  1014. {
  1015. int k, n, i;
  1016. n = 0;
  1017. for (i = -2; n < nr_probe; i++) {
  1018. k = early_platform_driver_probe_id(class_str, i, nr_probe - n);
  1019. if (k < 0)
  1020. break;
  1021. n += k;
  1022. if (user_only)
  1023. break;
  1024. }
  1025. return n;
  1026. }
  1027. /**
  1028. * early_platform_cleanup - clean up early platform code
  1029. */
  1030. void __init early_platform_cleanup(void)
  1031. {
  1032. struct platform_device *pd, *pd2;
  1033. /* clean up the devres list used to chain devices */
  1034. list_for_each_entry_safe(pd, pd2, &early_platform_device_list,
  1035. dev.devres_head) {
  1036. list_del(&pd->dev.devres_head);
  1037. memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));
  1038. }
  1039. }