| Maciej Sosnowski | e2fc4d1 | 2009-03-21 13:31:23 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright(c) 2007 - 2009 Intel Corporation. All rights reserved. | 
 | 3 |  * | 
 | 4 |  * This program is free software; you can redistribute it and/or modify it | 
 | 5 |  * under the terms of the GNU General Public License as published by the Free | 
 | 6 |  * Software Foundation; either version 2 of the License, or (at your option) | 
 | 7 |  * any later version. | 
 | 8 |  * | 
 | 9 |  * This program is distributed in the hope that it will be useful, but WITHOUT | 
 | 10 |  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | 
 | 11 |  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for | 
 | 12 |  * more details. | 
 | 13 |  * | 
 | 14 |  * You should have received a copy of the GNU General Public License along with | 
 | 15 |  * this program; if not, write to the Free Software Foundation, Inc., 59 | 
 | 16 |  * Temple Place - Suite 330, Boston, MA  02111-1307, USA. | 
 | 17 |  * | 
 | 18 |  * The full GNU General Public License is included in this distribution in the | 
 | 19 |  * file called COPYING. | 
 | 20 |  */ | 
 | 21 |  | 
| Shannon Nelson | 7589670 | 2007-10-16 01:27:41 -0700 | [diff] [blame] | 22 | #include <linux/kernel.h> | 
 | 23 | #include <linux/spinlock.h> | 
 | 24 | #include <linux/device.h> | 
 | 25 | #include <linux/idr.h> | 
 | 26 | #include <linux/kdev_t.h> | 
 | 27 | #include <linux/err.h> | 
 | 28 | #include <linux/dca.h> | 
 | 29 |  | 
 | 30 | static struct class *dca_class; | 
 | 31 | static struct idr dca_idr; | 
 | 32 | static spinlock_t dca_idr_lock; | 
 | 33 |  | 
 | 34 | int dca_sysfs_add_req(struct dca_provider *dca, struct device *dev, int slot) | 
 | 35 | { | 
| Kay Sievers | 765cdb6 | 2008-02-08 15:00:49 -0800 | [diff] [blame] | 36 | 	struct device *cd; | 
| Maciej Sosnowski | 7f1b358 | 2008-07-22 17:30:57 -0700 | [diff] [blame] | 37 | 	static int req_count; | 
| Shannon Nelson | 7589670 | 2007-10-16 01:27:41 -0700 | [diff] [blame] | 38 |  | 
| Greg Kroah-Hartman | a9b1261 | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 39 | 	cd = device_create(dca_class, dca->cd, MKDEV(0, slot + 1), NULL, | 
 | 40 | 			   "requester%d", req_count++); | 
| Shannon Nelson | 7589670 | 2007-10-16 01:27:41 -0700 | [diff] [blame] | 41 | 	if (IS_ERR(cd)) | 
 | 42 | 		return PTR_ERR(cd); | 
 | 43 | 	return 0; | 
 | 44 | } | 
 | 45 |  | 
 | 46 | void dca_sysfs_remove_req(struct dca_provider *dca, int slot) | 
 | 47 | { | 
| Kay Sievers | 765cdb6 | 2008-02-08 15:00:49 -0800 | [diff] [blame] | 48 | 	device_destroy(dca_class, MKDEV(0, slot + 1)); | 
| Shannon Nelson | 7589670 | 2007-10-16 01:27:41 -0700 | [diff] [blame] | 49 | } | 
 | 50 |  | 
 | 51 | int dca_sysfs_add_provider(struct dca_provider *dca, struct device *dev) | 
 | 52 | { | 
| Kay Sievers | 765cdb6 | 2008-02-08 15:00:49 -0800 | [diff] [blame] | 53 | 	struct device *cd; | 
| Shannon Nelson | 7589670 | 2007-10-16 01:27:41 -0700 | [diff] [blame] | 54 | 	int err = 0; | 
 | 55 |  | 
 | 56 | idr_try_again: | 
 | 57 | 	if (!idr_pre_get(&dca_idr, GFP_KERNEL)) | 
 | 58 | 		return -ENOMEM; | 
 | 59 | 	spin_lock(&dca_idr_lock); | 
 | 60 | 	err = idr_get_new(&dca_idr, dca, &dca->id); | 
 | 61 | 	spin_unlock(&dca_idr_lock); | 
 | 62 | 	switch (err) { | 
 | 63 | 	case 0: | 
 | 64 | 		break; | 
 | 65 | 	case -EAGAIN: | 
 | 66 | 		goto idr_try_again; | 
 | 67 | 	default: | 
 | 68 | 		return err; | 
 | 69 | 	} | 
 | 70 |  | 
| Greg Kroah-Hartman | a9b1261 | 2008-07-21 20:03:34 -0700 | [diff] [blame] | 71 | 	cd = device_create(dca_class, dev, MKDEV(0, 0), NULL, "dca%d", dca->id); | 
| Shannon Nelson | 7589670 | 2007-10-16 01:27:41 -0700 | [diff] [blame] | 72 | 	if (IS_ERR(cd)) { | 
 | 73 | 		spin_lock(&dca_idr_lock); | 
 | 74 | 		idr_remove(&dca_idr, dca->id); | 
 | 75 | 		spin_unlock(&dca_idr_lock); | 
 | 76 | 		return PTR_ERR(cd); | 
 | 77 | 	} | 
 | 78 | 	dca->cd = cd; | 
 | 79 | 	return 0; | 
 | 80 | } | 
 | 81 |  | 
 | 82 | void dca_sysfs_remove_provider(struct dca_provider *dca) | 
 | 83 | { | 
| Kay Sievers | 765cdb6 | 2008-02-08 15:00:49 -0800 | [diff] [blame] | 84 | 	device_unregister(dca->cd); | 
| Shannon Nelson | 7589670 | 2007-10-16 01:27:41 -0700 | [diff] [blame] | 85 | 	dca->cd = NULL; | 
 | 86 | 	spin_lock(&dca_idr_lock); | 
 | 87 | 	idr_remove(&dca_idr, dca->id); | 
 | 88 | 	spin_unlock(&dca_idr_lock); | 
 | 89 | } | 
 | 90 |  | 
 | 91 | int __init dca_sysfs_init(void) | 
 | 92 | { | 
 | 93 | 	idr_init(&dca_idr); | 
 | 94 | 	spin_lock_init(&dca_idr_lock); | 
 | 95 |  | 
 | 96 | 	dca_class = class_create(THIS_MODULE, "dca"); | 
 | 97 | 	if (IS_ERR(dca_class)) { | 
 | 98 | 		idr_destroy(&dca_idr); | 
 | 99 | 		return PTR_ERR(dca_class); | 
 | 100 | 	} | 
 | 101 | 	return 0; | 
 | 102 | } | 
 | 103 |  | 
 | 104 | void __exit dca_sysfs_exit(void) | 
 | 105 | { | 
 | 106 | 	class_destroy(dca_class); | 
 | 107 | 	idr_destroy(&dca_idr); | 
 | 108 | } | 
 | 109 |  |