umc-dev.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/uwb/umc.h>
  10. #define D_LOCAL 0
  11. #include <linux/uwb/debug.h>
  12. static void umc_device_release(struct device *dev)
  13. {
  14. struct umc_dev *umc = to_umc_dev(dev);
  15. kfree(umc);
  16. }
  17. /**
  18. * umc_device_create - allocate a child UMC device
  19. * @parent: parent of the new UMC device.
  20. * @n: index of the new device.
  21. *
  22. * The new UMC device will have a bus ID of the parent with '-n'
  23. * appended.
  24. */
  25. struct umc_dev *umc_device_create(struct device *parent, int n)
  26. {
  27. struct umc_dev *umc;
  28. umc = kzalloc(sizeof(struct umc_dev), GFP_KERNEL);
  29. if (umc) {
  30. dev_set_name(&umc->dev, "%s-%d", dev_name(parent), n);
  31. umc->dev.parent = parent;
  32. umc->dev.bus = &umc_bus_type;
  33. umc->dev.release = umc_device_release;
  34. umc->dev.dma_mask = parent->dma_mask;
  35. }
  36. return umc;
  37. }
  38. EXPORT_SYMBOL_GPL(umc_device_create);
  39. /**
  40. * umc_device_register - register a UMC device
  41. * @umc: pointer to the UMC device
  42. *
  43. * The memory resource for the UMC device is acquired and the device
  44. * registered with the system.
  45. */
  46. int umc_device_register(struct umc_dev *umc)
  47. {
  48. int err;
  49. d_fnstart(3, &umc->dev, "(umc_dev %p)\n", umc);
  50. err = request_resource(umc->resource.parent, &umc->resource);
  51. if (err < 0) {
  52. dev_err(&umc->dev, "can't allocate resource range "
  53. "%016Lx to %016Lx: %d\n",
  54. (unsigned long long)umc->resource.start,
  55. (unsigned long long)umc->resource.end,
  56. err);
  57. goto error_request_resource;
  58. }
  59. err = device_register(&umc->dev);
  60. if (err < 0)
  61. goto error_device_register;
  62. d_fnend(3, &umc->dev, "(umc_dev %p) = 0\n", umc);
  63. return 0;
  64. error_device_register:
  65. release_resource(&umc->resource);
  66. error_request_resource:
  67. d_fnend(3, &umc->dev, "(umc_dev %p) = %d\n", umc, err);
  68. return err;
  69. }
  70. EXPORT_SYMBOL_GPL(umc_device_register);
  71. /**
  72. * umc_device_unregister - unregister a UMC device
  73. * @umc: pointer to the UMC device
  74. *
  75. * First we unregister the device, make sure the driver can do it's
  76. * resource release thing and then we try to release any left over
  77. * resources. We take a ref to the device, to make sure it doesn't
  78. * dissapear under our feet.
  79. */
  80. void umc_device_unregister(struct umc_dev *umc)
  81. {
  82. struct device *dev;
  83. if (!umc)
  84. return;
  85. dev = get_device(&umc->dev);
  86. d_fnstart(3, dev, "(umc_dev %p)\n", umc);
  87. device_unregister(&umc->dev);
  88. release_resource(&umc->resource);
  89. d_fnend(3, dev, "(umc_dev %p) = void\n", umc);
  90. put_device(dev);
  91. }
  92. EXPORT_SYMBOL_GPL(umc_device_unregister);