1*e2fc4d19SMaciej Sosnowski /* 2*e2fc4d19SMaciej Sosnowski * Copyright(c) 2007 - 2009 Intel Corporation. All rights reserved. 3*e2fc4d19SMaciej Sosnowski * 4*e2fc4d19SMaciej Sosnowski * This program is free software; you can redistribute it and/or modify it 5*e2fc4d19SMaciej Sosnowski * under the terms of the GNU General Public License as published by the Free 6*e2fc4d19SMaciej Sosnowski * Software Foundation; either version 2 of the License, or (at your option) 7*e2fc4d19SMaciej Sosnowski * any later version. 8*e2fc4d19SMaciej Sosnowski * 9*e2fc4d19SMaciej Sosnowski * This program is distributed in the hope that it will be useful, but WITHOUT 10*e2fc4d19SMaciej Sosnowski * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11*e2fc4d19SMaciej Sosnowski * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 12*e2fc4d19SMaciej Sosnowski * more details. 13*e2fc4d19SMaciej Sosnowski * 14*e2fc4d19SMaciej Sosnowski * You should have received a copy of the GNU General Public License along with 15*e2fc4d19SMaciej Sosnowski * this program; if not, write to the Free Software Foundation, Inc., 59 16*e2fc4d19SMaciej Sosnowski * Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17*e2fc4d19SMaciej Sosnowski * 18*e2fc4d19SMaciej Sosnowski * The full GNU General Public License is included in this distribution in the 19*e2fc4d19SMaciej Sosnowski * file called COPYING. 20*e2fc4d19SMaciej Sosnowski */ 21*e2fc4d19SMaciej 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> 297589670fSShannon Nelson 307589670fSShannon Nelson static struct class *dca_class; 317589670fSShannon Nelson static struct idr dca_idr; 327589670fSShannon Nelson static spinlock_t dca_idr_lock; 337589670fSShannon Nelson 347589670fSShannon Nelson int dca_sysfs_add_req(struct dca_provider *dca, struct device *dev, int slot) 357589670fSShannon Nelson { 36765cdb6cSKay Sievers struct device *cd; 377f1b358aSMaciej Sosnowski static int req_count; 387589670fSShannon Nelson 39a9b12619SGreg Kroah-Hartman cd = device_create(dca_class, dca->cd, MKDEV(0, slot + 1), NULL, 407f1b358aSMaciej Sosnowski "requester%d", req_count++); 417589670fSShannon Nelson if (IS_ERR(cd)) 427589670fSShannon Nelson return PTR_ERR(cd); 437589670fSShannon Nelson return 0; 447589670fSShannon Nelson } 457589670fSShannon Nelson 467589670fSShannon Nelson void dca_sysfs_remove_req(struct dca_provider *dca, int slot) 477589670fSShannon Nelson { 48765cdb6cSKay Sievers device_destroy(dca_class, MKDEV(0, slot + 1)); 497589670fSShannon Nelson } 507589670fSShannon Nelson 517589670fSShannon Nelson int dca_sysfs_add_provider(struct dca_provider *dca, struct device *dev) 527589670fSShannon Nelson { 53765cdb6cSKay Sievers struct device *cd; 547589670fSShannon Nelson int err = 0; 557589670fSShannon Nelson 567589670fSShannon Nelson idr_try_again: 577589670fSShannon Nelson if (!idr_pre_get(&dca_idr, GFP_KERNEL)) 587589670fSShannon Nelson return -ENOMEM; 597589670fSShannon Nelson spin_lock(&dca_idr_lock); 607589670fSShannon Nelson err = idr_get_new(&dca_idr, dca, &dca->id); 617589670fSShannon Nelson spin_unlock(&dca_idr_lock); 627589670fSShannon Nelson switch (err) { 637589670fSShannon Nelson case 0: 647589670fSShannon Nelson break; 657589670fSShannon Nelson case -EAGAIN: 667589670fSShannon Nelson goto idr_try_again; 677589670fSShannon Nelson default: 687589670fSShannon Nelson return err; 697589670fSShannon Nelson } 707589670fSShannon Nelson 71a9b12619SGreg Kroah-Hartman cd = device_create(dca_class, dev, MKDEV(0, 0), NULL, "dca%d", dca->id); 727589670fSShannon Nelson if (IS_ERR(cd)) { 737589670fSShannon Nelson spin_lock(&dca_idr_lock); 747589670fSShannon Nelson idr_remove(&dca_idr, dca->id); 757589670fSShannon Nelson spin_unlock(&dca_idr_lock); 767589670fSShannon Nelson return PTR_ERR(cd); 777589670fSShannon Nelson } 787589670fSShannon Nelson dca->cd = cd; 797589670fSShannon Nelson return 0; 807589670fSShannon Nelson } 817589670fSShannon Nelson 827589670fSShannon Nelson void dca_sysfs_remove_provider(struct dca_provider *dca) 837589670fSShannon Nelson { 84765cdb6cSKay Sievers device_unregister(dca->cd); 857589670fSShannon Nelson dca->cd = NULL; 867589670fSShannon Nelson spin_lock(&dca_idr_lock); 877589670fSShannon Nelson idr_remove(&dca_idr, dca->id); 887589670fSShannon Nelson spin_unlock(&dca_idr_lock); 897589670fSShannon Nelson } 907589670fSShannon Nelson 917589670fSShannon Nelson int __init dca_sysfs_init(void) 927589670fSShannon Nelson { 937589670fSShannon Nelson idr_init(&dca_idr); 947589670fSShannon Nelson spin_lock_init(&dca_idr_lock); 957589670fSShannon Nelson 967589670fSShannon Nelson dca_class = class_create(THIS_MODULE, "dca"); 977589670fSShannon Nelson if (IS_ERR(dca_class)) { 987589670fSShannon Nelson idr_destroy(&dca_idr); 997589670fSShannon Nelson return PTR_ERR(dca_class); 1007589670fSShannon Nelson } 1017589670fSShannon Nelson return 0; 1027589670fSShannon Nelson } 1037589670fSShannon Nelson 1047589670fSShannon Nelson void __exit dca_sysfs_exit(void) 1057589670fSShannon Nelson { 1067589670fSShannon Nelson class_destroy(dca_class); 1077589670fSShannon Nelson idr_destroy(&dca_idr); 1087589670fSShannon Nelson } 1097589670fSShannon Nelson 110