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> 30*3382416dSPaul 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; 567589670fSShannon Nelson int err = 0; 577589670fSShannon Nelson 587589670fSShannon Nelson idr_try_again: 597589670fSShannon Nelson if (!idr_pre_get(&dca_idr, GFP_KERNEL)) 607589670fSShannon Nelson return -ENOMEM; 617589670fSShannon Nelson spin_lock(&dca_idr_lock); 627589670fSShannon Nelson err = idr_get_new(&dca_idr, dca, &dca->id); 637589670fSShannon Nelson spin_unlock(&dca_idr_lock); 647589670fSShannon Nelson switch (err) { 657589670fSShannon Nelson case 0: 667589670fSShannon Nelson break; 677589670fSShannon Nelson case -EAGAIN: 687589670fSShannon Nelson goto idr_try_again; 697589670fSShannon Nelson default: 707589670fSShannon Nelson return err; 717589670fSShannon Nelson } 727589670fSShannon Nelson 73a9b12619SGreg Kroah-Hartman cd = device_create(dca_class, dev, MKDEV(0, 0), NULL, "dca%d", dca->id); 747589670fSShannon Nelson if (IS_ERR(cd)) { 757589670fSShannon Nelson spin_lock(&dca_idr_lock); 767589670fSShannon Nelson idr_remove(&dca_idr, dca->id); 777589670fSShannon Nelson spin_unlock(&dca_idr_lock); 787589670fSShannon Nelson return PTR_ERR(cd); 797589670fSShannon Nelson } 807589670fSShannon Nelson dca->cd = cd; 817589670fSShannon Nelson return 0; 827589670fSShannon Nelson } 837589670fSShannon Nelson 847589670fSShannon Nelson void dca_sysfs_remove_provider(struct dca_provider *dca) 857589670fSShannon Nelson { 86765cdb6cSKay Sievers device_unregister(dca->cd); 877589670fSShannon Nelson dca->cd = NULL; 887589670fSShannon Nelson spin_lock(&dca_idr_lock); 897589670fSShannon Nelson idr_remove(&dca_idr, dca->id); 907589670fSShannon Nelson spin_unlock(&dca_idr_lock); 917589670fSShannon Nelson } 927589670fSShannon Nelson 937589670fSShannon Nelson int __init dca_sysfs_init(void) 947589670fSShannon Nelson { 957589670fSShannon Nelson idr_init(&dca_idr); 967589670fSShannon Nelson spin_lock_init(&dca_idr_lock); 977589670fSShannon Nelson 987589670fSShannon Nelson dca_class = class_create(THIS_MODULE, "dca"); 997589670fSShannon Nelson if (IS_ERR(dca_class)) { 1007589670fSShannon Nelson idr_destroy(&dca_idr); 1017589670fSShannon Nelson return PTR_ERR(dca_class); 1027589670fSShannon Nelson } 1037589670fSShannon Nelson return 0; 1047589670fSShannon Nelson } 1057589670fSShannon Nelson 1067589670fSShannon Nelson void __exit dca_sysfs_exit(void) 1077589670fSShannon Nelson { 1087589670fSShannon Nelson class_destroy(dca_class); 1097589670fSShannon Nelson idr_destroy(&dca_idr); 1107589670fSShannon Nelson } 1117589670fSShannon Nelson 112