platform.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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. struct platform_device *platform_device_alloc(const char *name, int id)
  143. {
  144. struct platform_object *pa;
  145. pa = kzalloc(sizeof(struct platform_object) + strlen(name), GFP_KERNEL);
  146. if (pa) {
  147. strcpy(pa->name, name);
  148. pa->pdev.name = pa->name;
  149. pa->pdev.id = id;
  150. device_initialize(&pa->pdev.dev);
  151. pa->pdev.dev.release = platform_device_release;
  152. }
  153. return pa ? &pa->pdev : NULL;
  154. }
  155. EXPORT_SYMBOL_GPL(platform_device_alloc);
  156. /**
  157. * platform_device_add_resources
  158. * @pdev: platform device allocated by platform_device_alloc to add resources to
  159. * @res: set of resources that needs to be allocated for the device
  160. * @num: number of resources
  161. *
  162. * Add a copy of the resources to the platform device. The memory
  163. * associated with the resources will be freed when the platform
  164. * device is released.
  165. */
  166. int platform_device_add_resources(struct platform_device *pdev, struct resource *res, unsigned int num)
  167. {
  168. struct resource *r;
  169. r = kmalloc(sizeof(struct resource) * num, GFP_KERNEL);
  170. if (r) {
  171. memcpy(r, res, sizeof(struct resource) * num);
  172. pdev->resource = r;
  173. pdev->num_resources = num;
  174. }
  175. return r ? 0 : -ENOMEM;
  176. }
  177. EXPORT_SYMBOL_GPL(platform_device_add_resources);
  178. /**
  179. * platform_device_add_data
  180. * @pdev: platform device allocated by platform_device_alloc to add resources to
  181. * @data: platform specific data for this platform device
  182. * @size: size of platform specific data
  183. *
  184. * Add a copy of platform specific data to the platform device's platform_data
  185. * pointer. The memory associated with the platform data will be freed
  186. * when the platform device is released.
  187. */
  188. int platform_device_add_data(struct platform_device *pdev, const void *data, size_t size)
  189. {
  190. void *d;
  191. d = kmalloc(size, GFP_KERNEL);
  192. if (d) {
  193. memcpy(d, data, size);
  194. pdev->dev.platform_data = d;
  195. }
  196. return d ? 0 : -ENOMEM;
  197. }
  198. EXPORT_SYMBOL_GPL(platform_device_add_data);
  199. /**
  200. * platform_device_add - add a platform device to device hierarchy
  201. * @pdev: platform device we're adding
  202. *
  203. * This is part 2 of platform_device_register(), though may be called
  204. * separately _iff_ pdev was allocated by platform_device_alloc().
  205. */
  206. int platform_device_add(struct platform_device *pdev)
  207. {
  208. int i, ret = 0;
  209. if (!pdev)
  210. return -EINVAL;
  211. if (!pdev->dev.parent)
  212. pdev->dev.parent = &platform_bus;
  213. pdev->dev.bus = &platform_bus_type;
  214. if (pdev->id != -1)
  215. snprintf(pdev->dev.bus_id, BUS_ID_SIZE, "%s.%d", pdev->name,
  216. pdev->id);
  217. else
  218. strlcpy(pdev->dev.bus_id, pdev->name, BUS_ID_SIZE);
  219. for (i = 0; i < pdev->num_resources; i++) {
  220. struct resource *p, *r = &pdev->resource[i];
  221. if (r->name == NULL)
  222. r->name = pdev->dev.bus_id;
  223. p = r->parent;
  224. if (!p) {
  225. if (r->flags & IORESOURCE_MEM)
  226. p = &iomem_resource;
  227. else if (r->flags & IORESOURCE_IO)
  228. p = &ioport_resource;
  229. }
  230. if (p && insert_resource(p, r)) {
  231. printk(KERN_ERR
  232. "%s: failed to claim resource %d\n",
  233. pdev->dev.bus_id, i);
  234. ret = -EBUSY;
  235. goto failed;
  236. }
  237. }
  238. pr_debug("Registering platform device '%s'. Parent at %s\n",
  239. pdev->dev.bus_id, pdev->dev.parent->bus_id);
  240. ret = device_add(&pdev->dev);
  241. if (ret == 0)
  242. return ret;
  243. failed:
  244. while (--i >= 0)
  245. if (pdev->resource[i].flags & (IORESOURCE_MEM|IORESOURCE_IO))
  246. release_resource(&pdev->resource[i]);
  247. return ret;
  248. }
  249. EXPORT_SYMBOL_GPL(platform_device_add);
  250. /**
  251. * platform_device_del - remove a platform-level device
  252. * @pdev: platform device we're removing
  253. *
  254. * Note that this function will also release all memory- and port-based
  255. * resources owned by the device (@dev->resource). This function
  256. * must _only_ be externally called in error cases. All other usage
  257. * 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. if (r->flags & (IORESOURCE_MEM|IORESOURCE_IO))
  267. release_resource(r);
  268. }
  269. }
  270. }
  271. EXPORT_SYMBOL_GPL(platform_device_del);
  272. /**
  273. * platform_device_register - add a platform-level device
  274. * @pdev: platform device we're adding
  275. *
  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
  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
  306. * memory allocated for the device allows drivers using such devices
  307. * to be unloaded without waiting for the last reference to the device
  308. * to be dropped.
  309. *
  310. * This interface is primarily intended for use with legacy drivers
  311. * which probe hardware directly. Because such drivers create sysfs
  312. * device nodes themselves, rather than letting system infrastructure
  313. * handle such device enumeration tasks, they don't fully conform to
  314. * the Linux driver model. In particular, when such drivers are built
  315. * as modules, they can't be "hotplugged".
  316. */
  317. struct platform_device *platform_device_register_simple(char *name, int id,
  318. struct resource *res, unsigned int num)
  319. {
  320. struct platform_device *pdev;
  321. int retval;
  322. pdev = platform_device_alloc(name, id);
  323. if (!pdev) {
  324. retval = -ENOMEM;
  325. goto error;
  326. }
  327. if (num) {
  328. retval = platform_device_add_resources(pdev, res, num);
  329. if (retval)
  330. goto error;
  331. }
  332. retval = platform_device_add(pdev);
  333. if (retval)
  334. goto error;
  335. return pdev;
  336. error:
  337. platform_device_put(pdev);
  338. return ERR_PTR(retval);
  339. }
  340. EXPORT_SYMBOL_GPL(platform_device_register_simple);
  341. static int platform_drv_probe(struct device *_dev)
  342. {
  343. struct platform_driver *drv = to_platform_driver(_dev->driver);
  344. struct platform_device *dev = to_platform_device(_dev);
  345. return drv->probe(dev);
  346. }
  347. static int platform_drv_probe_fail(struct device *_dev)
  348. {
  349. return -ENXIO;
  350. }
  351. static int platform_drv_remove(struct device *_dev)
  352. {
  353. struct platform_driver *drv = to_platform_driver(_dev->driver);
  354. struct platform_device *dev = to_platform_device(_dev);
  355. return drv->remove(dev);
  356. }
  357. static void platform_drv_shutdown(struct device *_dev)
  358. {
  359. struct platform_driver *drv = to_platform_driver(_dev->driver);
  360. struct platform_device *dev = to_platform_device(_dev);
  361. drv->shutdown(dev);
  362. }
  363. static int platform_drv_suspend(struct device *_dev, pm_message_t state)
  364. {
  365. struct platform_driver *drv = to_platform_driver(_dev->driver);
  366. struct platform_device *dev = to_platform_device(_dev);
  367. return drv->suspend(dev, state);
  368. }
  369. static int platform_drv_resume(struct device *_dev)
  370. {
  371. struct platform_driver *drv = to_platform_driver(_dev->driver);
  372. struct platform_device *dev = to_platform_device(_dev);
  373. return drv->resume(dev);
  374. }
  375. /**
  376. * platform_driver_register
  377. * @drv: platform driver structure
  378. */
  379. int platform_driver_register(struct platform_driver *drv)
  380. {
  381. drv->driver.bus = &platform_bus_type;
  382. if (drv->probe)
  383. drv->driver.probe = platform_drv_probe;
  384. if (drv->remove)
  385. drv->driver.remove = platform_drv_remove;
  386. if (drv->shutdown)
  387. drv->driver.shutdown = platform_drv_shutdown;
  388. if (drv->suspend)
  389. drv->driver.suspend = platform_drv_suspend;
  390. if (drv->resume)
  391. drv->driver.resume = platform_drv_resume;
  392. return driver_register(&drv->driver);
  393. }
  394. EXPORT_SYMBOL_GPL(platform_driver_register);
  395. /**
  396. * platform_driver_unregister
  397. * @drv: platform driver structure
  398. */
  399. void platform_driver_unregister(struct platform_driver *drv)
  400. {
  401. driver_unregister(&drv->driver);
  402. }
  403. EXPORT_SYMBOL_GPL(platform_driver_unregister);
  404. /**
  405. * platform_driver_probe - register driver for non-hotpluggable device
  406. * @drv: platform driver structure
  407. * @probe: the driver probe routine, probably from an __init section
  408. *
  409. * Use this instead of platform_driver_register() when you know the device
  410. * is not hotpluggable and has already been registered, and you want to
  411. * remove its run-once probe() infrastructure from memory after the driver
  412. * has bound to the device.
  413. *
  414. * One typical use for this would be with drivers for controllers integrated
  415. * into system-on-chip processors, where the controller devices have been
  416. * configured as part of board setup.
  417. *
  418. * Returns zero if the driver registered and bound to a device, else returns
  419. * a negative error code and with the driver not registered.
  420. */
  421. int __init_or_module platform_driver_probe(struct platform_driver *drv,
  422. int (*probe)(struct platform_device *))
  423. {
  424. int retval, code;
  425. /* temporary section violation during probe() */
  426. drv->probe = probe;
  427. retval = code = platform_driver_register(drv);
  428. /* Fixup that section violation, being paranoid about code scanning
  429. * the list of drivers in order to probe new devices. Check to see
  430. * if the probe was successful, and make sure any forced probes of
  431. * new devices fail.
  432. */
  433. spin_lock(&platform_bus_type.klist_drivers.k_lock);
  434. drv->probe = NULL;
  435. if (code == 0 && list_empty(&drv->driver.klist_devices.k_list))
  436. retval = -ENODEV;
  437. drv->driver.probe = platform_drv_probe_fail;
  438. spin_unlock(&platform_bus_type.klist_drivers.k_lock);
  439. if (code != retval)
  440. platform_driver_unregister(drv);
  441. return retval;
  442. }
  443. EXPORT_SYMBOL_GPL(platform_driver_probe);
  444. /* modalias support enables more hands-off userspace setup:
  445. * (a) environment variable lets new-style hotplug events work once system is
  446. * fully running: "modprobe $MODALIAS"
  447. * (b) sysfs attribute lets new-style coldplug recover from hotplug events
  448. * mishandled before system is fully running: "modprobe $(cat modalias)"
  449. */
  450. static ssize_t
  451. modalias_show(struct device *dev, struct device_attribute *a, char *buf)
  452. {
  453. struct platform_device *pdev = to_platform_device(dev);
  454. int len = snprintf(buf, PAGE_SIZE, "platform:%s\n", pdev->name);
  455. return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;
  456. }
  457. static struct device_attribute platform_dev_attrs[] = {
  458. __ATTR_RO(modalias),
  459. __ATTR_NULL,
  460. };
  461. static int platform_uevent(struct device *dev, struct kobj_uevent_env *env)
  462. {
  463. struct platform_device *pdev = to_platform_device(dev);
  464. add_uevent_var(env, "MODALIAS=platform:%s", pdev->name);
  465. return 0;
  466. }
  467. /**
  468. * platform_match - bind platform device to platform driver.
  469. * @dev: device.
  470. * @drv: driver.
  471. *
  472. * Platform device IDs are assumed to be encoded like this:
  473. * "<name><instance>", where <name> is a short description of the
  474. * type of device, like "pci" or "floppy", and <instance> is the
  475. * enumerated instance of the device, like '0' or '42'.
  476. * Driver IDs are simply "<name>".
  477. * So, extract the <name> from the platform_device structure,
  478. * and compare it against the name of the driver. Return whether
  479. * they match or not.
  480. */
  481. static int platform_match(struct device * dev, struct device_driver * drv)
  482. {
  483. struct platform_device *pdev = container_of(dev, struct platform_device, dev);
  484. return (strncmp(pdev->name, drv->name, BUS_ID_SIZE) == 0);
  485. }
  486. static int platform_suspend(struct device *dev, pm_message_t mesg)
  487. {
  488. int ret = 0;
  489. if (dev->driver && dev->driver->suspend)
  490. ret = dev->driver->suspend(dev, mesg);
  491. return ret;
  492. }
  493. static int platform_suspend_late(struct device *dev, pm_message_t mesg)
  494. {
  495. struct platform_driver *drv = to_platform_driver(dev->driver);
  496. struct platform_device *pdev = container_of(dev, struct platform_device, dev);
  497. int ret = 0;
  498. if (dev->driver && drv->suspend_late)
  499. ret = drv->suspend_late(pdev, mesg);
  500. return ret;
  501. }
  502. static int platform_resume_early(struct device *dev)
  503. {
  504. struct platform_driver *drv = to_platform_driver(dev->driver);
  505. struct platform_device *pdev = container_of(dev, struct platform_device, dev);
  506. int ret = 0;
  507. if (dev->driver && drv->resume_early)
  508. ret = drv->resume_early(pdev);
  509. return ret;
  510. }
  511. static int platform_resume(struct device * dev)
  512. {
  513. int ret = 0;
  514. if (dev->driver && dev->driver->resume)
  515. ret = dev->driver->resume(dev);
  516. return ret;
  517. }
  518. struct bus_type platform_bus_type = {
  519. .name = "platform",
  520. .dev_attrs = platform_dev_attrs,
  521. .match = platform_match,
  522. .uevent = platform_uevent,
  523. .suspend = platform_suspend,
  524. .suspend_late = platform_suspend_late,
  525. .resume_early = platform_resume_early,
  526. .resume = platform_resume,
  527. };
  528. EXPORT_SYMBOL_GPL(platform_bus_type);
  529. int __init platform_bus_init(void)
  530. {
  531. int error;
  532. error = device_register(&platform_bus);
  533. if (error)
  534. return error;
  535. error = bus_register(&platform_bus_type);
  536. if (error)
  537. device_unregister(&platform_bus);
  538. return error;
  539. }
  540. #ifndef ARCH_HAS_DMA_GET_REQUIRED_MASK
  541. u64 dma_get_required_mask(struct device *dev)
  542. {
  543. u32 low_totalram = ((max_pfn - 1) << PAGE_SHIFT);
  544. u32 high_totalram = ((max_pfn - 1) >> (32 - PAGE_SHIFT));
  545. u64 mask;
  546. if (!high_totalram) {
  547. /* convert to mask just covering totalram */
  548. low_totalram = (1 << (fls(low_totalram) - 1));
  549. low_totalram += low_totalram - 1;
  550. mask = low_totalram;
  551. } else {
  552. high_totalram = (1 << (fls(high_totalram) - 1));
  553. high_totalram += high_totalram - 1;
  554. mask = (((u64)high_totalram) << 32) + 0xffffffff;
  555. }
  556. return mask & *dev->dma_mask;
  557. }
  558. EXPORT_SYMBOL_GPL(dma_get_required_mask);
  559. #endif