David Vrabel | da389ea | 2008-09-17 16:34:12 +0100 | [diff] [blame] | 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 9 | #include <linux/slab.h> |
David Vrabel | da389ea | 2008-09-17 16:34:12 +0100 | [diff] [blame] | 10 | #include <linux/uwb/umc.h> |
David Vrabel | da389ea | 2008-09-17 16:34:12 +0100 | [diff] [blame] | 11 | |
| 12 | static 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 | */ |
| 27 | struct 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 Sievers | ae9eba0 | 2008-10-30 20:06:16 +0100 | [diff] [blame] | 33 | dev_set_name(&umc->dev, "%s-%d", dev_name(parent), n); |
David Vrabel | da389ea | 2008-09-17 16:34:12 +0100 | [diff] [blame] | 34 | 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 | } |
| 42 | EXPORT_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 | */ |
| 51 | int umc_device_register(struct umc_dev *umc) |
| 52 | { |
| 53 | int err; |
| 54 | |
David Vrabel | da389ea | 2008-09-17 16:34:12 +0100 | [diff] [blame] | 55 | err = request_resource(umc->resource.parent, &umc->resource); |
| 56 | if (err < 0) { |
| 57 | dev_err(&umc->dev, "can't allocate resource range " |
| 58 | "%016Lx to %016Lx: %d\n", |
| 59 | (unsigned long long)umc->resource.start, |
| 60 | (unsigned long long)umc->resource.end, |
| 61 | err); |
| 62 | goto error_request_resource; |
| 63 | } |
| 64 | |
| 65 | err = device_register(&umc->dev); |
| 66 | if (err < 0) |
| 67 | goto error_device_register; |
David Vrabel | da389ea | 2008-09-17 16:34:12 +0100 | [diff] [blame] | 68 | return 0; |
| 69 | |
| 70 | error_device_register: |
| 71 | release_resource(&umc->resource); |
| 72 | error_request_resource: |
David Vrabel | da389ea | 2008-09-17 16:34:12 +0100 | [diff] [blame] | 73 | return err; |
| 74 | } |
| 75 | EXPORT_SYMBOL_GPL(umc_device_register); |
| 76 | |
| 77 | /** |
| 78 | * umc_device_unregister - unregister a UMC device |
| 79 | * @umc: pointer to the UMC device |
| 80 | * |
| 81 | * First we unregister the device, make sure the driver can do it's |
| 82 | * resource release thing and then we try to release any left over |
| 83 | * resources. We take a ref to the device, to make sure it doesn't |
| 84 | * dissapear under our feet. |
| 85 | */ |
| 86 | void umc_device_unregister(struct umc_dev *umc) |
| 87 | { |
| 88 | struct device *dev; |
| 89 | if (!umc) |
| 90 | return; |
| 91 | dev = get_device(&umc->dev); |
David Vrabel | da389ea | 2008-09-17 16:34:12 +0100 | [diff] [blame] | 92 | device_unregister(&umc->dev); |
| 93 | release_resource(&umc->resource); |
David Vrabel | da389ea | 2008-09-17 16:34:12 +0100 | [diff] [blame] | 94 | put_device(dev); |
| 95 | } |
| 96 | EXPORT_SYMBOL_GPL(umc_device_unregister); |