platform.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  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/platform_device.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/bootmem.h>
  17. #include <linux/err.h>
  18. #include <linux/slab.h>
  19. #include "base.h"
  20. #define to_platform_driver(drv) (container_of((drv), struct platform_driver, driver))
  21. struct device platform_bus = {
  22. .bus_id = "platform",
  23. };
  24. EXPORT_SYMBOL_GPL(platform_bus);
  25. /**
  26. * platform_get_resource - get a resource for a device
  27. * @dev: platform device
  28. * @type: resource type
  29. * @num: resource index
  30. */
  31. struct resource *
  32. platform_get_resource(struct platform_device *dev, unsigned int type,
  33. unsigned int num)
  34. {
  35. int i;
  36. for (i = 0; i < dev->num_resources; i++) {
  37. struct resource *r = &dev->resource[i];
  38. if ((r->flags & (IORESOURCE_IO|IORESOURCE_MEM|
  39. IORESOURCE_IRQ|IORESOURCE_DMA))
  40. == type)
  41. if (num-- == 0)
  42. return r;
  43. }
  44. return NULL;
  45. }
  46. EXPORT_SYMBOL_GPL(platform_get_resource);
  47. /**
  48. * platform_get_irq - get an IRQ for a device
  49. * @dev: platform device
  50. * @num: IRQ number index
  51. */
  52. int platform_get_irq(struct platform_device *dev, unsigned int num)
  53. {
  54. struct resource *r = platform_get_resource(dev, IORESOURCE_IRQ, num);
  55. return r ? r->start : -ENXIO;
  56. }
  57. EXPORT_SYMBOL_GPL(platform_get_irq);
  58. /**
  59. * platform_get_resource_byname - get a resource for a device by name
  60. * @dev: platform device
  61. * @type: resource type
  62. * @name: resource name
  63. */
  64. struct resource *
  65. platform_get_resource_byname(struct platform_device *dev, unsigned int type,
  66. char *name)
  67. {
  68. int i;
  69. for (i = 0; i < dev->num_resources; i++) {
  70. struct resource *r = &dev->resource[i];
  71. if ((r->flags & (IORESOURCE_IO|IORESOURCE_MEM|
  72. IORESOURCE_IRQ|IORESOURCE_DMA)) == type)
  73. if (!strcmp(r->name, name))
  74. return r;
  75. }
  76. return NULL;
  77. }
  78. EXPORT_SYMBOL_GPL(platform_get_resource_byname);
  79. /**
  80. * platform_get_irq - get an IRQ for a device
  81. * @dev: platform device
  82. * @name: IRQ name
  83. */
  84. int platform_get_irq_byname(struct platform_device *dev, char *name)
  85. {
  86. struct resource *r = platform_get_resource_byname(dev, IORESOURCE_IRQ, name);
  87. return r ? r->start : -ENXIO;
  88. }
  89. EXPORT_SYMBOL_GPL(platform_get_irq_byname);
  90. /**
  91. * platform_add_devices - add a numbers of platform devices
  92. * @devs: array of platform devices to add
  93. * @num: number of platform devices in array
  94. */
  95. int platform_add_devices(struct platform_device **devs, int num)
  96. {
  97. int i, ret = 0;
  98. for (i = 0; i < num; i++) {
  99. ret = platform_device_register(devs[i]);
  100. if (ret) {
  101. while (--i >= 0)
  102. platform_device_unregister(devs[i]);
  103. break;
  104. }
  105. }
  106. return ret;
  107. }
  108. EXPORT_SYMBOL_GPL(platform_add_devices);
  109. struct platform_object {
  110. struct platform_device pdev;
  111. char name[1];
  112. };
  113. /**
  114. * platform_device_put
  115. * @pdev: platform device to free
  116. *
  117. * Free all memory associated with a platform device. This function
  118. * must _only_ be externally called in error cases. All other usage
  119. * is a bug.
  120. */
  121. void platform_device_put(struct platform_device *pdev)
  122. {
  123. if (pdev)
  124. put_device(&pdev->dev);
  125. }
  126. EXPORT_SYMBOL_GPL(platform_device_put);
  127. static void platform_device_release(struct device *dev)
  128. {
  129. struct platform_object *pa = container_of(dev, struct platform_object, pdev.dev);
  130. kfree(pa->pdev.dev.platform_data);
  131. kfree(pa->pdev.resource);
  132. kfree(pa);
  133. }
  134. /**
  135. * platform_device_alloc
  136. * @name: base name of the device we're adding
  137. * @id: instance id
  138. *
  139. * Create a platform device object which can have other objects attached
  140. * to it, and which will have attached objects freed when it is released.
  141. *
  142. * This device will be marked as not supporting hotpluggable drivers; no
  143. * device add/remove uevents will be generated. In the unusual case that
  144. * the device isn't being dynamically allocated as a legacy "probe the
  145. * hardware" driver, infrastructure code should reverse this marking.
  146. */
  147. struct platform_device *platform_device_alloc(const char *name, unsigned int id)
  148. {
  149. struct platform_object *pa;
  150. pa = kzalloc(sizeof(struct platform_object) + strlen(name), GFP_KERNEL);
  151. if (pa) {
  152. strcpy(pa->name, name);
  153. pa->pdev.name = pa->name;
  154. pa->pdev.id = id;
  155. device_initialize(&pa->pdev.dev);
  156. pa->pdev.dev.release = platform_device_release;
  157. /* prevent hotplug "modprobe $(MODALIAS)" from causing trouble in
  158. * legacy probe-the-hardware drivers, which don't properly split
  159. * out device enumeration logic from drivers.
  160. */
  161. pa->pdev.dev.uevent_suppress = 1;
  162. }
  163. return pa ? &pa->pdev : NULL;
  164. }
  165. EXPORT_SYMBOL_GPL(platform_device_alloc);
  166. /**
  167. * platform_device_add_resources
  168. * @pdev: platform device allocated by platform_device_alloc to add resources to
  169. * @res: set of resources that needs to be allocated for the device
  170. * @num: number of resources
  171. *
  172. * Add a copy of the resources to the platform device. The memory
  173. * associated with the resources will be freed when the platform
  174. * device is released.
  175. */
  176. int platform_device_add_resources(struct platform_device *pdev, struct resource *res, unsigned int num)
  177. {
  178. struct resource *r;
  179. r = kmalloc(sizeof(struct resource) * num, GFP_KERNEL);
  180. if (r) {
  181. memcpy(r, res, sizeof(struct resource) * num);
  182. pdev->resource = r;
  183. pdev->num_resources = num;
  184. }
  185. return r ? 0 : -ENOMEM;
  186. }
  187. EXPORT_SYMBOL_GPL(platform_device_add_resources);
  188. /**
  189. * platform_device_add_data
  190. * @pdev: platform device allocated by platform_device_alloc to add resources to
  191. * @data: platform specific data for this platform device
  192. * @size: size of platform specific data
  193. *
  194. * Add a copy of platform specific data to the platform device's platform_data
  195. * pointer. The memory associated with the platform data will be freed
  196. * when the platform device is released.
  197. */
  198. int platform_device_add_data(struct platform_device *pdev, const void *data, size_t size)
  199. {
  200. void *d;
  201. d = kmalloc(size, GFP_KERNEL);
  202. if (d) {
  203. memcpy(d, data, size);
  204. pdev->dev.platform_data = d;
  205. }
  206. return d ? 0 : -ENOMEM;
  207. }
  208. EXPORT_SYMBOL_GPL(platform_device_add_data);
  209. /**
  210. * platform_device_add - add a platform device to device hierarchy
  211. * @pdev: platform device we're adding
  212. *
  213. * This is part 2 of platform_device_register(), though may be called
  214. * separately _iff_ pdev was allocated by platform_device_alloc().
  215. */
  216. int platform_device_add(struct platform_device *pdev)
  217. {
  218. int i, ret = 0;
  219. if (!pdev)
  220. return -EINVAL;
  221. if (!pdev->dev.parent)
  222. pdev->dev.parent = &platform_bus;
  223. pdev->dev.bus = &platform_bus_type;
  224. if (pdev->id != -1)
  225. snprintf(pdev->dev.bus_id, BUS_ID_SIZE, "%s.%u", pdev->name, pdev->id);
  226. else
  227. strlcpy(pdev->dev.bus_id, pdev->name, BUS_ID_SIZE);
  228. for (i = 0; i < pdev->num_resources; i++) {
  229. struct resource *p, *r = &pdev->resource[i];
  230. if (r->name == NULL)
  231. r->name = pdev->dev.bus_id;
  232. p = r->parent;
  233. if (!p) {
  234. if (r->flags & IORESOURCE_MEM)
  235. p = &iomem_resource;
  236. else if (r->flags & IORESOURCE_IO)
  237. p = &ioport_resource;
  238. }
  239. if (p && insert_resource(p, r)) {
  240. printk(KERN_ERR
  241. "%s: failed to claim resource %d\n",
  242. pdev->dev.bus_id, i);
  243. ret = -EBUSY;
  244. goto failed;
  245. }
  246. }
  247. pr_debug("Registering platform device '%s'. Parent at %s\n",
  248. pdev->dev.bus_id, pdev->dev.parent->bus_id);
  249. ret = device_add(&pdev->dev);
  250. if (ret == 0)
  251. return ret;
  252. failed:
  253. while (--i >= 0)
  254. if (pdev->resource[i].flags & (IORESOURCE_MEM|IORESOURCE_IO))
  255. release_resource(&pdev->resource[i]);
  256. return ret;
  257. }
  258. EXPORT_SYMBOL_GPL(platform_device_add);
  259. /**
  260. * platform_device_del - remove a platform-level device
  261. * @pdev: platform device we're removing
  262. *
  263. * Note that this function will also release all memory- and port-based
  264. * resources owned by the device (@dev->resource). This function
  265. * must _only_ be externally called in error cases. All other usage
  266. * is a bug.
  267. */
  268. void platform_device_del(struct platform_device *pdev)
  269. {
  270. int i;
  271. if (pdev) {
  272. device_del(&pdev->dev);
  273. for (i = 0; i < pdev->num_resources; i++) {
  274. struct resource *r = &pdev->resource[i];
  275. if (r->flags & (IORESOURCE_MEM|IORESOURCE_IO))
  276. release_resource(r);
  277. }
  278. }
  279. }
  280. EXPORT_SYMBOL_GPL(platform_device_del);
  281. /**
  282. * platform_device_register - add a platform-level device
  283. * @pdev: platform device we're adding
  284. *
  285. */
  286. int platform_device_register(struct platform_device * pdev)
  287. {
  288. device_initialize(&pdev->dev);
  289. return platform_device_add(pdev);
  290. }
  291. EXPORT_SYMBOL_GPL(platform_device_register);
  292. /**
  293. * platform_device_unregister - unregister a platform-level device
  294. * @pdev: platform device we're unregistering
  295. *
  296. * Unregistration is done in 2 steps. First we release all resources
  297. * and remove it from the subsystem, then we drop reference count by
  298. * calling platform_device_put().
  299. */
  300. void platform_device_unregister(struct platform_device * pdev)
  301. {
  302. platform_device_del(pdev);
  303. platform_device_put(pdev);
  304. }
  305. EXPORT_SYMBOL_GPL(platform_device_unregister);
  306. /**
  307. * platform_device_register_simple
  308. * @name: base name of the device we're adding
  309. * @id: instance id
  310. * @res: set of resources that needs to be allocated for the device
  311. * @num: number of resources
  312. *
  313. * This function creates a simple platform device that requires minimal
  314. * resource and memory management. Canned release function freeing
  315. * memory allocated for the device allows drivers using such devices
  316. * to be unloaded without waiting for the last reference to the device
  317. * to be dropped.
  318. *
  319. * This interface is primarily intended for use with legacy drivers
  320. * which probe hardware directly. Because such drivers create sysfs
  321. * device nodes themselves, rather than letting system infrastructure
  322. * handle such device enumeration tasks, they don't fully conform to
  323. * the Linux driver model. In particular, when such drivers are built
  324. * as modules, they can't be "hotplugged".
  325. */
  326. struct platform_device *platform_device_register_simple(char *name, unsigned int id,
  327. struct resource *res, unsigned int num)
  328. {
  329. struct platform_device *pdev;
  330. int retval;
  331. pdev = platform_device_alloc(name, id);
  332. if (!pdev) {
  333. retval = -ENOMEM;
  334. goto error;
  335. }
  336. if (num) {
  337. retval = platform_device_add_resources(pdev, res, num);
  338. if (retval)
  339. goto error;
  340. }
  341. retval = platform_device_add(pdev);
  342. if (retval)
  343. goto error;
  344. return pdev;
  345. error:
  346. platform_device_put(pdev);
  347. return ERR_PTR(retval);
  348. }
  349. EXPORT_SYMBOL_GPL(platform_device_register_simple);
  350. static int platform_drv_probe(struct device *_dev)
  351. {
  352. struct platform_driver *drv = to_platform_driver(_dev->driver);
  353. struct platform_device *dev = to_platform_device(_dev);
  354. return drv->probe(dev);
  355. }
  356. static int platform_drv_probe_fail(struct device *_dev)
  357. {
  358. return -ENXIO;
  359. }
  360. static int platform_drv_remove(struct device *_dev)
  361. {
  362. struct platform_driver *drv = to_platform_driver(_dev->driver);
  363. struct platform_device *dev = to_platform_device(_dev);
  364. return drv->remove(dev);
  365. }
  366. static void platform_drv_shutdown(struct device *_dev)
  367. {
  368. struct platform_driver *drv = to_platform_driver(_dev->driver);
  369. struct platform_device *dev = to_platform_device(_dev);
  370. drv->shutdown(dev);
  371. }
  372. static int platform_drv_suspend(struct device *_dev, pm_message_t state)
  373. {
  374. struct platform_driver *drv = to_platform_driver(_dev->driver);
  375. struct platform_device *dev = to_platform_device(_dev);
  376. return drv->suspend(dev, state);
  377. }
  378. static int platform_drv_resume(struct device *_dev)
  379. {
  380. struct platform_driver *drv = to_platform_driver(_dev->driver);
  381. struct platform_device *dev = to_platform_device(_dev);
  382. return drv->resume(dev);
  383. }
  384. /**
  385. * platform_driver_register
  386. * @drv: platform driver structure
  387. */
  388. int platform_driver_register(struct platform_driver *drv)
  389. {
  390. drv->driver.bus = &platform_bus_type;
  391. if (drv->probe)
  392. drv->driver.probe = platform_drv_probe;
  393. if (drv->remove)
  394. drv->driver.remove = platform_drv_remove;
  395. if (drv->shutdown)
  396. drv->driver.shutdown = platform_drv_shutdown;
  397. if (drv->suspend)
  398. drv->driver.suspend = platform_drv_suspend;
  399. if (drv->resume)
  400. drv->driver.resume = platform_drv_resume;
  401. return driver_register(&drv->driver);
  402. }
  403. EXPORT_SYMBOL_GPL(platform_driver_register);
  404. /**
  405. * platform_driver_unregister
  406. * @drv: platform driver structure
  407. */
  408. void platform_driver_unregister(struct platform_driver *drv)
  409. {
  410. driver_unregister(&drv->driver);
  411. }
  412. EXPORT_SYMBOL_GPL(platform_driver_unregister);
  413. /**
  414. * platform_driver_probe - register driver for non-hotpluggable device
  415. * @drv: platform driver structure
  416. * @probe: the driver probe routine, probably from an __init section
  417. *
  418. * Use this instead of platform_driver_register() when you know the device
  419. * is not hotpluggable and has already been registered, and you want to
  420. * remove its run-once probe() infrastructure from memory after the driver
  421. * has bound to the device.
  422. *
  423. * One typical use for this would be with drivers for controllers integrated
  424. * into system-on-chip processors, where the controller devices have been
  425. * configured as part of board setup.
  426. *
  427. * Returns zero if the driver registered and bound to a device, else returns
  428. * a negative error code and with the driver not registered.
  429. */
  430. int __init_or_module platform_driver_probe(struct platform_driver *drv,
  431. int (*probe)(struct platform_device *))
  432. {
  433. int retval, code;
  434. /* temporary section violation during probe() */
  435. drv->probe = probe;
  436. retval = code = platform_driver_register(drv);
  437. /* Fixup that section violation, being paranoid about code scanning
  438. * the list of drivers in order to probe new devices. Check to see
  439. * if the probe was successful, and make sure any forced probes of
  440. * new devices fail.
  441. */
  442. spin_lock(&platform_bus_type.klist_drivers.k_lock);
  443. drv->probe = NULL;
  444. if (code == 0 && list_empty(&drv->driver.klist_devices.k_list))
  445. retval = -ENODEV;
  446. drv->driver.probe = platform_drv_probe_fail;
  447. spin_unlock(&platform_bus_type.klist_drivers.k_lock);
  448. if (code != retval)
  449. platform_driver_unregister(drv);
  450. return retval;
  451. }
  452. EXPORT_SYMBOL_GPL(platform_driver_probe);
  453. /* modalias support enables more hands-off userspace setup:
  454. * (a) environment variable lets new-style hotplug events work once system is
  455. * fully running: "modprobe $MODALIAS"
  456. * (b) sysfs attribute lets new-style coldplug recover from hotplug events
  457. * mishandled before system is fully running: "modprobe $(cat modalias)"
  458. */
  459. static ssize_t
  460. modalias_show(struct device *dev, struct device_attribute *a, char *buf)
  461. {
  462. struct platform_device *pdev = to_platform_device(dev);
  463. int len = snprintf(buf, PAGE_SIZE, "%s\n", pdev->name);
  464. return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
  465. }
  466. static struct device_attribute platform_dev_attrs[] = {
  467. __ATTR_RO(modalias),
  468. __ATTR_NULL,
  469. };
  470. static int platform_uevent(struct device *dev, char **envp, int num_envp,
  471. char *buffer, int buffer_size)
  472. {
  473. struct platform_device *pdev = to_platform_device(dev);
  474. envp[0] = buffer;
  475. snprintf(buffer, buffer_size, "MODALIAS=%s", pdev->name);
  476. return 0;
  477. }
  478. /**
  479. * platform_match - bind platform device to platform driver.
  480. * @dev: device.
  481. * @drv: driver.
  482. *
  483. * Platform device IDs are assumed to be encoded like this:
  484. * "<name><instance>", where <name> is a short description of the
  485. * type of device, like "pci" or "floppy", and <instance> is the
  486. * enumerated instance of the device, like '0' or '42'.
  487. * Driver IDs are simply "<name>".
  488. * So, extract the <name> from the platform_device structure,
  489. * and compare it against the name of the driver. Return whether
  490. * they match or not.
  491. */
  492. static int platform_match(struct device * dev, struct device_driver * drv)
  493. {
  494. struct platform_device *pdev = container_of(dev, struct platform_device, dev);
  495. return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0);
  496. }
  497. static int platform_suspend(struct device *dev, pm_message_t mesg)
  498. {
  499. int ret = 0;
  500. if (dev->driver && dev->driver->suspend)
  501. ret = dev->driver->suspend(dev, mesg);
  502. return ret;
  503. }
  504. static int platform_suspend_late(struct device *dev, pm_message_t mesg)
  505. {
  506. struct platform_driver *drv = to_platform_driver(dev->driver);
  507. struct platform_device *pdev = container_of(dev, struct platform_device, dev);
  508. int ret = 0;
  509. if (dev->driver && drv->suspend_late)
  510. ret = drv->suspend_late(pdev, mesg);
  511. return ret;
  512. }
  513. static int platform_resume_early(struct device *dev)
  514. {
  515. struct platform_driver *drv = to_platform_driver(dev->driver);
  516. struct platform_device *pdev = container_of(dev, struct platform_device, dev);
  517. int ret = 0;
  518. if (dev->driver && drv->resume_early)
  519. ret = drv->resume_early(pdev);
  520. return ret;
  521. }
  522. static int platform_resume(struct device * dev)
  523. {
  524. int ret = 0;
  525. if (dev->driver && dev->driver->resume)
  526. ret = dev->driver->resume(dev);
  527. return ret;
  528. }
  529. struct bus_type platform_bus_type = {
  530. .name = "platform",
  531. .dev_attrs = platform_dev_attrs,
  532. .match = platform_match,
  533. .uevent = platform_uevent,
  534. .suspend = platform_suspend,
  535. .suspend_late = platform_suspend_late,
  536. .resume_early = platform_resume_early,
  537. .resume = platform_resume,
  538. };
  539. EXPORT_SYMBOL_GPL(platform_bus_type);
  540. int __init platform_bus_init(void)
  541. {
  542. int error;
  543. error = device_register(&platform_bus);
  544. if (error)
  545. return error;
  546. error = bus_register(&platform_bus_type);
  547. if (error)
  548. device_unregister(&platform_bus);
  549. return error;
  550. }
  551. #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
  552. u64 dma_get_required_mask(struct device *dev)
  553. {
  554. u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
  555. u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
  556. u64 mask;
  557. if (!high_totalram) {
  558. /* convert to mask just covering totalram */
  559. low_totalram = (1 << (fls(low_totalram) - 1));
  560. low_totalram += low_totalram - 1;
  561. mask = low_totalram;
  562. } else {
  563. high_totalram = (1 << (fls(high_totalram) - 1));
  564. high_totalram += high_totalram - 1;
  565. mask = (((u64)high_totalram) << 32) + 0xffffffff;
  566. }
  567. return mask & *dev->dma_mask;
  568. }
  569. EXPORT_SYMBOL_GPL(dma_get_required_mask);
  570. #endif