platform.c 32 KB

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