platform.c 31 KB

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