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