umc-dev.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * UWB Multi-interface Controller device management.
  3. *
  4. * Copyright (C) 2007 Cambridge Silicon Radio Ltd.
  5. *
  6. * This file is released under the GNU GPL v2.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/slab.h>
  10. #include <linux/uwb/umc.h>
  11. static void umc_device_release(struct device *dev)
  12. {
  13. struct umc_dev *umc = to_umc_dev(dev);
  14. kfree(umc);
  15. }
  16. /**
  17. * umc_device_create - allocate a child UMC device
  18. * @parent: parent of the new UMC device.
  19. * @n: index of the new device.
  20. *
  21. * The new UMC device will have a bus ID of the parent with '-n'
  22. * appended.
  23. */
  24. struct umc_dev *umc_device_create(struct device *parent, int n)
  25. {
  26. struct umc_dev *umc;
  27. umc = kzalloc(sizeof(struct umc_dev), GFP_KERNEL);
  28. if (umc) {
  29. dev_set_name(&umc->dev, "%s-%d", dev_name(parent), n);
  30. umc->dev.parent = parent;
  31. umc->dev.bus = &umc_bus_type;
  32. umc->dev.release = umc_device_release;
  33. umc->dev.dma_mask = parent->dma_mask;
  34. }
  35. return umc;
  36. }
  37. EXPORT_SYMBOL_GPL(umc_device_create);
  38. /**
  39. * umc_device_register - register a UMC device
  40. * @umc: pointer to the UMC device
  41. *
  42. * The memory resource for the UMC device is acquired and the device
  43. * registered with the system.
  44. */
  45. int umc_device_register(struct umc_dev *umc)
  46. {
  47. int err;
  48. err = request_resource(umc->resource.parent, &umc->resource);
  49. if (err < 0) {
  50. dev_err(&umc->dev, "can't allocate resource range %pR: %d\n",
  51. &umc->resource, err);
  52. goto error_request_resource;
  53. }
  54. err = device_register(&umc->dev);
  55. if (err < 0)
  56. goto error_device_register;
  57. return 0;
  58. error_device_register:
  59. release_resource(&umc->resource);
  60. error_request_resource:
  61. return err;
  62. }
  63. EXPORT_SYMBOL_GPL(umc_device_register);
  64. /**
  65. * umc_device_unregister - unregister a UMC device
  66. * @umc: pointer to the UMC device
  67. *
  68. * First we unregister the device, make sure the driver can do it's
  69. * resource release thing and then we try to release any left over
  70. * resources. We take a ref to the device, to make sure it doesn't
  71. * disappear under our feet.
  72. */
  73. void umc_device_unregister(struct umc_dev *umc)
  74. {
  75. struct device *dev;
  76. if (!umc)
  77. return;
  78. dev = get_device(&umc->dev);
  79. device_unregister(&umc->dev);
  80. release_resource(&umc->resource);
  81. put_device(dev);
  82. }
  83. EXPORT_SYMBOL_GPL(umc_device_unregister);