Linux 4.19.133
[linux/fpc-iii.git] / drivers / uwb / umc-dev.c
blob7b0b268e0c8e0707f6bb34e84a6688e760c0a10d
1 /*
2 * UWB Multi-interface Controller device management.
4 * Copyright (C) 2007 Cambridge Silicon Radio Ltd.
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>
13 static void umc_device_release(struct device *dev)
15 struct umc_dev *umc = to_umc_dev(dev);
17 kfree(umc);
20 /**
21 * umc_device_create - allocate a child UMC device
22 * @parent: parent of the new UMC device.
23 * @n: index of the new device.
25 * The new UMC device will have a bus ID of the parent with '-n'
26 * appended.
28 struct umc_dev *umc_device_create(struct device *parent, int n)
30 struct umc_dev *umc;
32 umc = kzalloc(sizeof(struct umc_dev), GFP_KERNEL);
33 if (umc) {
34 dev_set_name(&umc->dev, "%s-%d", dev_name(parent), n);
35 umc->dev.parent = parent;
36 umc->dev.bus = &umc_bus_type;
37 umc->dev.release = umc_device_release;
39 umc->dev.dma_mask = parent->dma_mask;
41 return umc;
43 EXPORT_SYMBOL_GPL(umc_device_create);
45 /**
46 * umc_device_register - register a UMC device
47 * @umc: pointer to the UMC device
49 * The memory resource for the UMC device is acquired and the device
50 * registered with the system.
52 int umc_device_register(struct umc_dev *umc)
54 int err;
56 err = request_resource(umc->resource.parent, &umc->resource);
57 if (err < 0) {
58 dev_err(&umc->dev, "can't allocate resource range %pR: %d\n",
59 &umc->resource, err);
60 goto error_request_resource;
63 err = device_register(&umc->dev);
64 if (err < 0)
65 goto error_device_register;
66 return 0;
68 error_device_register:
69 put_device(&umc->dev);
70 release_resource(&umc->resource);
71 error_request_resource:
72 return err;
74 EXPORT_SYMBOL_GPL(umc_device_register);
76 /**
77 * umc_device_unregister - unregister a UMC device
78 * @umc: pointer to the UMC device
80 * First we unregister the device, make sure the driver can do it's
81 * resource release thing and then we try to release any left over
82 * resources. We take a ref to the device, to make sure it doesn't
83 * disappear under our feet.
85 void umc_device_unregister(struct umc_dev *umc)
87 struct device *dev;
88 if (!umc)
89 return;
90 dev = get_device(&umc->dev);
91 device_unregister(&umc->dev);
92 release_resource(&umc->resource);
93 put_device(dev);
95 EXPORT_SYMBOL_GPL(umc_device_unregister);