1e2fc4d19SMaciej Sosnowski /* 2e2fc4d19SMaciej Sosnowski * Copyright(c) 2007 - 2009 Intel Corporation. All rights reserved. 3e2fc4d19SMaciej Sosnowski * 4e2fc4d19SMaciej Sosnowski * This program is free software; you can redistribute it and/or modify it 5e2fc4d19SMaciej Sosnowski * under the terms of the GNU General Public License as published by the Free 6e2fc4d19SMaciej Sosnowski * Software Foundation; either version 2 of the License, or (at your option) 7e2fc4d19SMaciej Sosnowski * any later version. 8e2fc4d19SMaciej Sosnowski * 9e2fc4d19SMaciej Sosnowski * This program is distributed in the hope that it will be useful, but WITHOUT 10e2fc4d19SMaciej Sosnowski * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11e2fc4d19SMaciej Sosnowski * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12e2fc4d19SMaciej Sosnowski * more details. 13e2fc4d19SMaciej Sosnowski * 14e2fc4d19SMaciej Sosnowski * You should have received a copy of the GNU General Public License along with 15e2fc4d19SMaciej Sosnowski * this program; if not, write to the Free Software Foundation, Inc., 59 16e2fc4d19SMaciej Sosnowski * Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17e2fc4d19SMaciej Sosnowski * 18e2fc4d19SMaciej Sosnowski * The full GNU General Public License is included in this distribution in the 19e2fc4d19SMaciej Sosnowski * file called COPYING. 20e2fc4d19SMaciej Sosnowski */ 21e2fc4d19SMaciej Sosnowski 227589670fSShannon Nelson #include <linux/kernel.h> 237589670fSShannon Nelson #include <linux/spinlock.h> 247589670fSShannon Nelson #include <linux/device.h> 257589670fSShannon Nelson #include <linux/idr.h> 267589670fSShannon Nelson #include <linux/kdev_t.h> 277589670fSShannon Nelson #include <linux/err.h> 287589670fSShannon Nelson #include <linux/dca.h> 295a0e3ad6STejun Heo #include <linux/gfp.h> 303382416dSPaul Gortmaker #include <linux/export.h> 317589670fSShannon Nelson 327589670fSShannon Nelson static struct class *dca_class; 337589670fSShannon Nelson static struct idr dca_idr; 347589670fSShannon Nelson static spinlock_t dca_idr_lock; 357589670fSShannon Nelson 367589670fSShannon Nelson int dca_sysfs_add_req(struct dca_provider *dca, struct device *dev, int slot) 377589670fSShannon Nelson { 38765cdb6cSKay Sievers struct device *cd; 397f1b358aSMaciej Sosnowski static int req_count; 407589670fSShannon Nelson 41a9b12619SGreg Kroah-Hartman cd = device_create(dca_class, dca->cd, MKDEV(0, slot + 1), NULL, 427f1b358aSMaciej Sosnowski "requester%d", req_count++); 437589670fSShannon Nelson if (IS_ERR(cd)) 447589670fSShannon Nelson return PTR_ERR(cd); 457589670fSShannon Nelson return 0; 467589670fSShannon Nelson } 477589670fSShannon Nelson 487589670fSShannon Nelson void dca_sysfs_remove_req(struct dca_provider *dca, int slot) 497589670fSShannon Nelson { 50765cdb6cSKay Sievers device_destroy(dca_class, MKDEV(0, slot + 1)); 517589670fSShannon Nelson } 527589670fSShannon Nelson 537589670fSShannon Nelson int dca_sysfs_add_provider(struct dca_provider *dca, struct device *dev) 547589670fSShannon Nelson { 55765cdb6cSKay Sievers struct device *cd; 56*615f2e5cSTejun Heo int ret; 577589670fSShannon Nelson 58*615f2e5cSTejun Heo idr_preload(GFP_KERNEL); 597589670fSShannon Nelson spin_lock(&dca_idr_lock); 60*615f2e5cSTejun Heo 61*615f2e5cSTejun Heo ret = idr_alloc(&dca_idr, dca, 0, 0, GFP_NOWAIT); 62*615f2e5cSTejun Heo if (ret >= 0) 63*615f2e5cSTejun Heo dca->id = ret; 64*615f2e5cSTejun Heo 657589670fSShannon Nelson spin_unlock(&dca_idr_lock); 66*615f2e5cSTejun Heo idr_preload_end(); 67*615f2e5cSTejun Heo if (ret < 0) 68*615f2e5cSTejun Heo return ret; 697589670fSShannon Nelson 70a9b12619SGreg Kroah-Hartman cd = device_create(dca_class, dev, MKDEV(0, 0), NULL, "dca%d", dca->id); 717589670fSShannon Nelson if (IS_ERR(cd)) { 727589670fSShannon Nelson spin_lock(&dca_idr_lock); 737589670fSShannon Nelson idr_remove(&dca_idr, dca->id); 747589670fSShannon Nelson spin_unlock(&dca_idr_lock); 757589670fSShannon Nelson return PTR_ERR(cd); 767589670fSShannon Nelson } 777589670fSShannon Nelson dca->cd = cd; 787589670fSShannon Nelson return 0; 797589670fSShannon Nelson } 807589670fSShannon Nelson 817589670fSShannon Nelson void dca_sysfs_remove_provider(struct dca_provider *dca) 827589670fSShannon Nelson { 83765cdb6cSKay Sievers device_unregister(dca->cd); 847589670fSShannon Nelson dca->cd = NULL; 857589670fSShannon Nelson spin_lock(&dca_idr_lock); 867589670fSShannon Nelson idr_remove(&dca_idr, dca->id); 877589670fSShannon Nelson spin_unlock(&dca_idr_lock); 887589670fSShannon Nelson } 897589670fSShannon Nelson 907589670fSShannon Nelson int __init dca_sysfs_init(void) 917589670fSShannon Nelson { 927589670fSShannon Nelson idr_init(&dca_idr); 937589670fSShannon Nelson spin_lock_init(&dca_idr_lock); 947589670fSShannon Nelson 957589670fSShannon Nelson dca_class = class_create(THIS_MODULE, "dca"); 967589670fSShannon Nelson if (IS_ERR(dca_class)) { 977589670fSShannon Nelson idr_destroy(&dca_idr); 987589670fSShannon Nelson return PTR_ERR(dca_class); 997589670fSShannon Nelson } 1007589670fSShannon Nelson return 0; 1017589670fSShannon Nelson } 1027589670fSShannon Nelson 1037589670fSShannon Nelson void __exit dca_sysfs_exit(void) 1047589670fSShannon Nelson { 1057589670fSShannon Nelson class_destroy(dca_class); 1067589670fSShannon Nelson idr_destroy(&dca_idr); 1077589670fSShannon Nelson } 1087589670fSShannon Nelson 109