blob: 7b0b268e0c8e0707f6bb34e84a6688e760c0a10d [file] [log] [blame]
David Vrabelda389ea2008-09-17 16:34:12 +01001/*
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>
Paul Gortmaker475c0a62011-07-10 13:18:02 -04009#include <linux/export.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
David Vrabelda389ea2008-09-17 16:34:12 +010011#include <linux/uwb/umc.h>
David Vrabelda389ea2008-09-17 16:34:12 +010012
13static void umc_device_release(struct device *dev)
14{
15 struct umc_dev *umc = to_umc_dev(dev);
16
17 kfree(umc);
18}
19
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.
24 *
25 * The new UMC device will have a bus ID of the parent with '-n'
26 * appended.
27 */
28struct umc_dev *umc_device_create(struct device *parent, int n)
29{
30 struct umc_dev *umc;
31
32 umc = kzalloc(sizeof(struct umc_dev), GFP_KERNEL);
33 if (umc) {
Kay Sieversae9eba02008-10-30 20:06:16 +010034 dev_set_name(&umc->dev, "%s-%d", dev_name(parent), n);
David Vrabelda389ea2008-09-17 16:34:12 +010035 umc->dev.parent = parent;
36 umc->dev.bus = &umc_bus_type;
37 umc->dev.release = umc_device_release;
38
39 umc->dev.dma_mask = parent->dma_mask;
40 }
41 return umc;
42}
43EXPORT_SYMBOL_GPL(umc_device_create);
44
45/**
46 * umc_device_register - register a UMC device
47 * @umc: pointer to the UMC device
48 *
49 * The memory resource for the UMC device is acquired and the device
50 * registered with the system.
51 */
52int umc_device_register(struct umc_dev *umc)
53{
54 int err;
55
David Vrabelda389ea2008-09-17 16:34:12 +010056 err = request_resource(umc->resource.parent, &umc->resource);
57 if (err < 0) {
Joe Perches7189ba92010-11-12 13:38:02 -080058 dev_err(&umc->dev, "can't allocate resource range %pR: %d\n",
59 &umc->resource, err);
David Vrabelda389ea2008-09-17 16:34:12 +010060 goto error_request_resource;
61 }
62
63 err = device_register(&umc->dev);
64 if (err < 0)
65 goto error_device_register;
David Vrabelda389ea2008-09-17 16:34:12 +010066 return 0;
67
68error_device_register:
Levente Kurusa1ce0fd12013-12-19 16:06:51 +010069 put_device(&umc->dev);
David Vrabelda389ea2008-09-17 16:34:12 +010070 release_resource(&umc->resource);
71error_request_resource:
David Vrabelda389ea2008-09-17 16:34:12 +010072 return err;
73}
74EXPORT_SYMBOL_GPL(umc_device_register);
75
76/**
77 * umc_device_unregister - unregister a UMC device
78 * @umc: pointer to the UMC device
79 *
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
Lucas De Marchi25985ed2011-03-30 22:57:33 -030083 * disappear under our feet.
David Vrabelda389ea2008-09-17 16:34:12 +010084 */
85void umc_device_unregister(struct umc_dev *umc)
86{
87 struct device *dev;
88 if (!umc)
89 return;
90 dev = get_device(&umc->dev);
David Vrabelda389ea2008-09-17 16:34:12 +010091 device_unregister(&umc->dev);
92 release_resource(&umc->resource);
David Vrabelda389ea2008-09-17 16:34:12 +010093 put_device(dev);
94}
95EXPORT_SYMBOL_GPL(umc_device_unregister);