platform.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  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. const 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. const 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. int __weak 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. int __weak 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. int __weak 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. int __weak 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 pm_generic_runtime_suspend(dev);
  805. };
  806. int __weak platform_pm_runtime_resume(struct device *dev)
  807. {
  808. return pm_generic_runtime_resume(dev);
  809. };
  810. int __weak platform_pm_runtime_idle(struct device *dev)
  811. {
  812. return pm_generic_runtime_idle(dev);
  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. /*
  1054. * Set up a sensible init_name to enable
  1055. * dev_name() and others to be used before the
  1056. * rest of the driver core is initialized.
  1057. */
  1058. if (!match->dev.init_name && slab_is_available()) {
  1059. if (match->id != -1)
  1060. match->dev.init_name =
  1061. kasprintf(GFP_KERNEL, "%s.%d",
  1062. match->name,
  1063. match->id);
  1064. else
  1065. match->dev.init_name =
  1066. kasprintf(GFP_KERNEL, "%s",
  1067. match->name);
  1068. if (!match->dev.init_name)
  1069. return -ENOMEM;
  1070. }
  1071. if (epdrv->pdrv->probe(match))
  1072. pr_warning("%s: unable to probe %s early.\n",
  1073. class_str, match->name);
  1074. else
  1075. n++;
  1076. }
  1077. if (n >= nr_probe)
  1078. break;
  1079. }
  1080. if (left)
  1081. return n;
  1082. else
  1083. return -ENODEV;
  1084. }
  1085. /**
  1086. * early_platform_driver_probe - probe a class of registered drivers
  1087. * @class_str: string to identify early platform driver class
  1088. * @nr_probe: number of platform devices to successfully probe before exiting
  1089. * @user_only: only probe user specified early platform devices
  1090. *
  1091. * Used by architecture code to probe registered early platform drivers
  1092. * within a certain class. For probe to happen a registered early platform
  1093. * device matching a registered early platform driver is needed.
  1094. */
  1095. int __init early_platform_driver_probe(char *class_str,
  1096. int nr_probe,
  1097. int user_only)
  1098. {
  1099. int k, n, i;
  1100. n = 0;
  1101. for (i = -2; n < nr_probe; i++) {
  1102. k = early_platform_driver_probe_id(class_str, i, nr_probe - n);
  1103. if (k < 0)
  1104. break;
  1105. n += k;
  1106. if (user_only)
  1107. break;
  1108. }
  1109. return n;
  1110. }
  1111. /**
  1112. * early_platform_cleanup - clean up early platform code
  1113. */
  1114. void __init early_platform_cleanup(void)
  1115. {
  1116. struct platform_device *pd, *pd2;
  1117. /* clean up the devres list used to chain devices */
  1118. list_for_each_entry_safe(pd, pd2, &early_platform_device_list,
  1119. dev.devres_head) {
  1120. list_del(&pd->dev.devres_head);
  1121. memset(&pd->dev.devres_head, 0, sizeof(pd->dev.devres_head));
  1122. }
  1123. }