| 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> | 
| David Vrabel | da389ea | 2008-09-17 16:34:12 +0100 | [diff] [blame] | 10 |  | 
 | 11 | static void umc_device_release(struct device *dev) | 
 | 12 | { | 
 | 13 | 	struct umc_dev *umc = to_umc_dev(dev); | 
 | 14 |  | 
 | 15 | 	kfree(umc); | 
 | 16 | } | 
 | 17 |  | 
 | 18 | /** | 
 | 19 |  * umc_device_create - allocate a child UMC device | 
 | 20 |  * @parent: parent of the new UMC device. | 
 | 21 |  * @n:      index of the new device. | 
 | 22 |  * | 
 | 23 |  * The new UMC device will have a bus ID of the parent with '-n' | 
 | 24 |  * appended. | 
 | 25 |  */ | 
 | 26 | struct umc_dev *umc_device_create(struct device *parent, int n) | 
 | 27 | { | 
 | 28 | 	struct umc_dev *umc; | 
 | 29 |  | 
 | 30 | 	umc = kzalloc(sizeof(struct umc_dev), GFP_KERNEL); | 
 | 31 | 	if (umc) { | 
| Kay Sievers | ae9eba0 | 2008-10-30 20:06:16 +0100 | [diff] [blame] | 32 | 		dev_set_name(&umc->dev, "%s-%d", dev_name(parent), n); | 
| David Vrabel | da389ea | 2008-09-17 16:34:12 +0100 | [diff] [blame] | 33 | 		umc->dev.parent  = parent; | 
 | 34 | 		umc->dev.bus     = &umc_bus_type; | 
 | 35 | 		umc->dev.release = umc_device_release; | 
 | 36 |  | 
 | 37 | 		umc->dev.dma_mask = parent->dma_mask; | 
 | 38 | 	} | 
 | 39 | 	return umc; | 
 | 40 | } | 
 | 41 | EXPORT_SYMBOL_GPL(umc_device_create); | 
 | 42 |  | 
 | 43 | /** | 
 | 44 |  * umc_device_register - register a UMC device | 
 | 45 |  * @umc: pointer to the UMC device | 
 | 46 |  * | 
 | 47 |  * The memory resource for the UMC device is acquired and the device | 
 | 48 |  * registered with the system. | 
 | 49 |  */ | 
 | 50 | int umc_device_register(struct umc_dev *umc) | 
 | 51 | { | 
 | 52 | 	int err; | 
 | 53 |  | 
| David Vrabel | da389ea | 2008-09-17 16:34:12 +0100 | [diff] [blame] | 54 | 	err = request_resource(umc->resource.parent, &umc->resource); | 
 | 55 | 	if (err < 0) { | 
 | 56 | 		dev_err(&umc->dev, "can't allocate resource range " | 
 | 57 | 			"%016Lx to %016Lx: %d\n", | 
 | 58 | 			(unsigned long long)umc->resource.start, | 
 | 59 | 			(unsigned long long)umc->resource.end, | 
 | 60 | 			err); | 
 | 61 | 		goto error_request_resource; | 
 | 62 | 	} | 
 | 63 |  | 
 | 64 | 	err = device_register(&umc->dev); | 
 | 65 | 	if (err < 0) | 
 | 66 | 		goto error_device_register; | 
| David Vrabel | da389ea | 2008-09-17 16:34:12 +0100 | [diff] [blame] | 67 | 	return 0; | 
 | 68 |  | 
 | 69 | error_device_register: | 
 | 70 | 	release_resource(&umc->resource); | 
 | 71 | error_request_resource: | 
| David Vrabel | da389ea | 2008-09-17 16:34:12 +0100 | [diff] [blame] | 72 | 	return err; | 
 | 73 | } | 
 | 74 | EXPORT_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 | 
 | 83 |  * dissapear under our feet. | 
 | 84 |  */ | 
 | 85 | void umc_device_unregister(struct umc_dev *umc) | 
 | 86 | { | 
 | 87 | 	struct device *dev; | 
 | 88 | 	if (!umc) | 
 | 89 | 		return; | 
 | 90 | 	dev = get_device(&umc->dev); | 
| David Vrabel | da389ea | 2008-09-17 16:34:12 +0100 | [diff] [blame] | 91 | 	device_unregister(&umc->dev); | 
 | 92 | 	release_resource(&umc->resource); | 
| David Vrabel | da389ea | 2008-09-17 16:34:12 +0100 | [diff] [blame] | 93 | 	put_device(dev); | 
 | 94 | } | 
 | 95 | EXPORT_SYMBOL_GPL(umc_device_unregister); |