blob: b2948ec578784b30f2a6fd740fb098196876af7a [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/slab.h>
David Vrabelda389ea2008-09-17 16:34:12 +010010#include <linux/uwb/umc.h>
David Vrabelda389ea2008-09-17 16:34:12 +010011
12static void umc_device_release(struct device *dev)
13{
14 struct umc_dev *umc = to_umc_dev(dev);
15
16 kfree(umc);
17}
18
19/**
20 * umc_device_create - allocate a child UMC device
21 * @parent: parent of the new UMC device.
22 * @n: index of the new device.
23 *
24 * The new UMC device will have a bus ID of the parent with '-n'
25 * appended.
26 */
27struct umc_dev *umc_device_create(struct device *parent, int n)
28{
29 struct umc_dev *umc;
30
31 umc = kzalloc(sizeof(struct umc_dev), GFP_KERNEL);
32 if (umc) {
Kay Sieversae9eba02008-10-30 20:06:16 +010033 dev_set_name(&umc->dev, "%s-%d", dev_name(parent), n);
David Vrabelda389ea2008-09-17 16:34:12 +010034 umc->dev.parent = parent;
35 umc->dev.bus = &umc_bus_type;
36 umc->dev.release = umc_device_release;
37
38 umc->dev.dma_mask = parent->dma_mask;
39 }
40 return umc;
41}
42EXPORT_SYMBOL_GPL(umc_device_create);
43
44/**
45 * umc_device_register - register a UMC device
46 * @umc: pointer to the UMC device
47 *
48 * The memory resource for the UMC device is acquired and the device
49 * registered with the system.
50 */
51int umc_device_register(struct umc_dev *umc)
52{
53 int err;
54
David Vrabelda389ea2008-09-17 16:34:12 +010055 err = request_resource(umc->resource.parent, &umc->resource);
56 if (err < 0) {
Joe Perches7189ba92010-11-12 13:38:02 -080057 dev_err(&umc->dev, "can't allocate resource range %pR: %d\n",
58 &umc->resource, err);
David Vrabelda389ea2008-09-17 16:34:12 +010059 goto error_request_resource;
60 }
61
62 err = device_register(&umc->dev);
63 if (err < 0)
64 goto error_device_register;
David Vrabelda389ea2008-09-17 16:34:12 +010065 return 0;
66
67error_device_register:
68 release_resource(&umc->resource);
69error_request_resource:
David Vrabelda389ea2008-09-17 16:34:12 +010070 return err;
71}
72EXPORT_SYMBOL_GPL(umc_device_register);
73
74/**
75 * umc_device_unregister - unregister a UMC device
76 * @umc: pointer to the UMC device
77 *
78 * First we unregister the device, make sure the driver can do it's
79 * resource release thing and then we try to release any left over
80 * resources. We take a ref to the device, to make sure it doesn't
Lucas De Marchi25985ed2011-03-30 22:57:33 -030081 * disappear under our feet.
David Vrabelda389ea2008-09-17 16:34:12 +010082 */
83void umc_device_unregister(struct umc_dev *umc)
84{
85 struct device *dev;
86 if (!umc)
87 return;
88 dev = get_device(&umc->dev);
David Vrabelda389ea2008-09-17 16:34:12 +010089 device_unregister(&umc->dev);
90 release_resource(&umc->resource);
David Vrabelda389ea2008-09-17 16:34:12 +010091 put_device(dev);
92}
93EXPORT_SYMBOL_GPL(umc_device_unregister);