platform.c 30 KB

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