umc-dev.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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/export.h>
  10. #include <linux/slab.h>
  11. #include <linux/uwb/umc.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. err = request_resource(umc->resource.parent, &umc->resource);
  50. if (err < 0) {
  51. dev_err(&umc->dev, "can't allocate resource range %pR: %d\n",
  52. &umc->resource, err);
  53. goto error_request_resource;
  54. }
  55. err = device_register(&umc->dev);
  56. if (err < 0)
  57. goto error_device_register;
  58. return 0;
  59. error_device_register:
  60. put_device(&umc->dev);
  61. release_resource(&umc->resource);
  62. error_request_resource:
  63. return err;
  64. }
  65. EXPORT_SYMBOL_GPL(umc_device_register);
  66. /**
  67. * umc_device_unregister - unregister a UMC device
  68. * @umc: pointer to the UMC device
  69. *
  70. * First we unregister the device, make sure the driver can do it's
  71. * resource release thing and then we try to release any left over
  72. * resources. We take a ref to the device, to make sure it doesn't
  73. * disappear under our feet.
  74. */
  75. void umc_device_unregister(struct umc_dev *umc)
  76. {
  77. struct device *dev;
  78. if (!umc)
  79. return;
  80. dev = get_device(&umc->dev);
  81. device_unregister(&umc->dev);
  82. release_resource(&umc->resource);
  83. put_device(dev);
  84. }
  85. EXPORT_SYMBOL_GPL(umc_device_unregister);