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> |
| 9 | #include <linux/uwb/umc.h> |
| 10 | #define D_LOCAL 0 |
| 11 | #include <linux/uwb/debug.h> |
| 12 | |
| 13 | static 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 | */ |
| 28 | struct 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) { |
| 34 | snprintf(umc->dev.bus_id, sizeof(umc->dev.bus_id), "%s-%d", |
| 35 | parent->bus_id, n); |
| 36 | umc->dev.parent = parent; |
| 37 | umc->dev.bus = &umc_bus_type; |
| 38 | umc->dev.release = umc_device_release; |
| 39 | |
| 40 | umc->dev.dma_mask = parent->dma_mask; |
| 41 | } |
| 42 | return umc; |
| 43 | } |
| 44 | EXPORT_SYMBOL_GPL(umc_device_create); |
| 45 | |
| 46 | /** |
| 47 | * umc_device_register - register a UMC device |
| 48 | * @umc: pointer to the UMC device |
| 49 | * |
| 50 | * The memory resource for the UMC device is acquired and the device |
| 51 | * registered with the system. |
| 52 | */ |
| 53 | int umc_device_register(struct umc_dev *umc) |
| 54 | { |
| 55 | int err; |
| 56 | |
| 57 | d_fnstart(3, &umc->dev, "(umc_dev %p)\n", umc); |
| 58 | |
| 59 | err = request_resource(umc->resource.parent, &umc->resource); |
| 60 | if (err < 0) { |
| 61 | dev_err(&umc->dev, "can't allocate resource range " |
| 62 | "%016Lx to %016Lx: %d\n", |
| 63 | (unsigned long long)umc->resource.start, |
| 64 | (unsigned long long)umc->resource.end, |
| 65 | err); |
| 66 | goto error_request_resource; |
| 67 | } |
| 68 | |
| 69 | err = device_register(&umc->dev); |
| 70 | if (err < 0) |
| 71 | goto error_device_register; |
| 72 | d_fnend(3, &umc->dev, "(umc_dev %p) = 0\n", umc); |
| 73 | return 0; |
| 74 | |
| 75 | error_device_register: |
| 76 | release_resource(&umc->resource); |
| 77 | error_request_resource: |
| 78 | d_fnend(3, &umc->dev, "(umc_dev %p) = %d\n", umc, err); |
| 79 | return err; |
| 80 | } |
| 81 | EXPORT_SYMBOL_GPL(umc_device_register); |
| 82 | |
| 83 | /** |
| 84 | * umc_device_unregister - unregister a UMC device |
| 85 | * @umc: pointer to the UMC device |
| 86 | * |
| 87 | * First we unregister the device, make sure the driver can do it's |
| 88 | * resource release thing and then we try to release any left over |
| 89 | * resources. We take a ref to the device, to make sure it doesn't |
| 90 | * dissapear under our feet. |
| 91 | */ |
| 92 | void umc_device_unregister(struct umc_dev *umc) |
| 93 | { |
| 94 | struct device *dev; |
| 95 | if (!umc) |
| 96 | return; |
| 97 | dev = get_device(&umc->dev); |
| 98 | d_fnstart(3, dev, "(umc_dev %p)\n", umc); |
| 99 | device_unregister(&umc->dev); |
| 100 | release_resource(&umc->resource); |
| 101 | d_fnend(3, dev, "(umc_dev %p) = void\n", umc); |
| 102 | put_device(dev); |
| 103 | } |
| 104 | EXPORT_SYMBOL_GPL(umc_device_unregister); |