1d569d5bbSJames Bottomley /* 2d569d5bbSJames Bottomley * Enclosure Services 3d569d5bbSJames Bottomley * 4d569d5bbSJames Bottomley * Copyright (C) 2008 James Bottomley <James.Bottomley@HansenPartnership.com> 5d569d5bbSJames Bottomley * 6d569d5bbSJames Bottomley **----------------------------------------------------------------------------- 7d569d5bbSJames Bottomley ** 8d569d5bbSJames Bottomley ** This program is free software; you can redistribute it and/or 9d569d5bbSJames Bottomley ** modify it under the terms of the GNU General Public License 10d569d5bbSJames Bottomley ** version 2 as published by the Free Software Foundation. 11d569d5bbSJames Bottomley ** 12d569d5bbSJames Bottomley ** This program is distributed in the hope that it will be useful, 13d569d5bbSJames Bottomley ** but WITHOUT ANY WARRANTY; without even the implied warranty of 14d569d5bbSJames Bottomley ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15d569d5bbSJames Bottomley ** GNU General Public License for more details. 16d569d5bbSJames Bottomley ** 17d569d5bbSJames Bottomley ** You should have received a copy of the GNU General Public License 18d569d5bbSJames Bottomley ** along with this program; if not, write to the Free Software 19d569d5bbSJames Bottomley ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20d569d5bbSJames Bottomley ** 21d569d5bbSJames Bottomley **----------------------------------------------------------------------------- 22d569d5bbSJames Bottomley */ 23d569d5bbSJames Bottomley #include <linux/device.h> 24d569d5bbSJames Bottomley #include <linux/enclosure.h> 25d569d5bbSJames Bottomley #include <linux/err.h> 26d569d5bbSJames Bottomley #include <linux/list.h> 27d569d5bbSJames Bottomley #include <linux/kernel.h> 28d569d5bbSJames Bottomley #include <linux/module.h> 29d569d5bbSJames Bottomley #include <linux/mutex.h> 305a0e3ad6STejun Heo #include <linux/slab.h> 31d569d5bbSJames Bottomley 32d569d5bbSJames Bottomley static LIST_HEAD(container_list); 33d569d5bbSJames Bottomley static DEFINE_MUTEX(container_list_lock); 34d569d5bbSJames Bottomley static struct class enclosure_class; 35d569d5bbSJames Bottomley 36d569d5bbSJames Bottomley /** 37163f52b6SJames Bottomley * enclosure_find - find an enclosure given a parent device 38163f52b6SJames Bottomley * @dev: the parent to match against 39163f52b6SJames Bottomley * @start: Optional enclosure device to start from (NULL if none) 40d569d5bbSJames Bottomley * 41163f52b6SJames Bottomley * Looks through the list of registered enclosures to find all those 42163f52b6SJames Bottomley * with @dev as a parent. Returns NULL if no enclosure is 43163f52b6SJames Bottomley * found. @start can be used as a starting point to obtain multiple 44163f52b6SJames Bottomley * enclosures per parent (should begin with NULL and then be set to 45163f52b6SJames Bottomley * each returned enclosure device). Obtains a reference to the 46163f52b6SJames Bottomley * enclosure class device which must be released with device_put(). 47163f52b6SJames Bottomley * If @start is not NULL, a reference must be taken on it which is 48163f52b6SJames Bottomley * released before returning (this allows a loop through all 49163f52b6SJames Bottomley * enclosures to exit with only the reference on the enclosure of 50163f52b6SJames Bottomley * interest held). Note that the @dev may correspond to the actual 51163f52b6SJames Bottomley * device housing the enclosure, in which case no iteration via @start 52163f52b6SJames Bottomley * is required. 53d569d5bbSJames Bottomley */ 54163f52b6SJames Bottomley struct enclosure_device *enclosure_find(struct device *dev, 55163f52b6SJames Bottomley struct enclosure_device *start) 56d569d5bbSJames Bottomley { 57ee959b00STony Jones struct enclosure_device *edev; 58d569d5bbSJames Bottomley 59d569d5bbSJames Bottomley mutex_lock(&container_list_lock); 60163f52b6SJames Bottomley edev = list_prepare_entry(start, &container_list, node); 61163f52b6SJames Bottomley if (start) 62163f52b6SJames Bottomley put_device(&start->edev); 63163f52b6SJames Bottomley 64163f52b6SJames Bottomley list_for_each_entry_continue(edev, &container_list, node) { 65163f52b6SJames Bottomley struct device *parent = edev->edev.parent; 66163f52b6SJames Bottomley /* parent might not be immediate, so iterate up to 67163f52b6SJames Bottomley * the root of the tree if necessary */ 68163f52b6SJames Bottomley while (parent) { 69163f52b6SJames Bottomley if (parent == dev) { 70ee959b00STony Jones get_device(&edev->edev); 71d569d5bbSJames Bottomley mutex_unlock(&container_list_lock); 72d569d5bbSJames Bottomley return edev; 73d569d5bbSJames Bottomley } 74163f52b6SJames Bottomley parent = parent->parent; 75163f52b6SJames Bottomley } 76d569d5bbSJames Bottomley } 77d569d5bbSJames Bottomley mutex_unlock(&container_list_lock); 78d569d5bbSJames Bottomley 79d569d5bbSJames Bottomley return NULL; 80d569d5bbSJames Bottomley } 81d569d5bbSJames Bottomley EXPORT_SYMBOL_GPL(enclosure_find); 82d569d5bbSJames Bottomley 83d569d5bbSJames Bottomley /** 84d569d5bbSJames Bottomley * enclosure_for_each_device - calls a function for each enclosure 85d569d5bbSJames Bottomley * @fn: the function to call 86d569d5bbSJames Bottomley * @data: the data to pass to each call 87d569d5bbSJames Bottomley * 88d569d5bbSJames Bottomley * Loops over all the enclosures calling the function. 89d569d5bbSJames Bottomley * 90d569d5bbSJames Bottomley * Note, this function uses a mutex which will be held across calls to 91d569d5bbSJames Bottomley * @fn, so it must have non atomic context, and @fn may (although it 92d569d5bbSJames Bottomley * should not) sleep or otherwise cause the mutex to be held for 93d569d5bbSJames Bottomley * indefinite periods 94d569d5bbSJames Bottomley */ 95d569d5bbSJames Bottomley int enclosure_for_each_device(int (*fn)(struct enclosure_device *, void *), 96d569d5bbSJames Bottomley void *data) 97d569d5bbSJames Bottomley { 98d569d5bbSJames Bottomley int error = 0; 99d569d5bbSJames Bottomley struct enclosure_device *edev; 100d569d5bbSJames Bottomley 101d569d5bbSJames Bottomley mutex_lock(&container_list_lock); 102d569d5bbSJames Bottomley list_for_each_entry(edev, &container_list, node) { 103d569d5bbSJames Bottomley error = fn(edev, data); 104d569d5bbSJames Bottomley if (error) 105d569d5bbSJames Bottomley break; 106d569d5bbSJames Bottomley } 107d569d5bbSJames Bottomley mutex_unlock(&container_list_lock); 108d569d5bbSJames Bottomley 109d569d5bbSJames Bottomley return error; 110d569d5bbSJames Bottomley } 111d569d5bbSJames Bottomley EXPORT_SYMBOL_GPL(enclosure_for_each_device); 112d569d5bbSJames Bottomley 113d569d5bbSJames Bottomley /** 114d569d5bbSJames Bottomley * enclosure_register - register device as an enclosure 115d569d5bbSJames Bottomley * 116d569d5bbSJames Bottomley * @dev: device containing the enclosure 117d569d5bbSJames Bottomley * @components: number of components in the enclosure 118d569d5bbSJames Bottomley * 119d569d5bbSJames Bottomley * This sets up the device for being an enclosure. Note that @dev does 120d569d5bbSJames Bottomley * not have to be a dedicated enclosure device. It may be some other type 121d569d5bbSJames Bottomley * of device that additionally responds to enclosure services 122d569d5bbSJames Bottomley */ 123d569d5bbSJames Bottomley struct enclosure_device * 124d569d5bbSJames Bottomley enclosure_register(struct device *dev, const char *name, int components, 125d569d5bbSJames Bottomley struct enclosure_component_callbacks *cb) 126d569d5bbSJames Bottomley { 127d569d5bbSJames Bottomley struct enclosure_device *edev = 128d569d5bbSJames Bottomley kzalloc(sizeof(struct enclosure_device) + 129d569d5bbSJames Bottomley sizeof(struct enclosure_component)*components, 130d569d5bbSJames Bottomley GFP_KERNEL); 131d569d5bbSJames Bottomley int err, i; 132d569d5bbSJames Bottomley 133d569d5bbSJames Bottomley BUG_ON(!cb); 134d569d5bbSJames Bottomley 135d569d5bbSJames Bottomley if (!edev) 136d569d5bbSJames Bottomley return ERR_PTR(-ENOMEM); 137d569d5bbSJames Bottomley 138d569d5bbSJames Bottomley edev->components = components; 139d569d5bbSJames Bottomley 140ee959b00STony Jones edev->edev.class = &enclosure_class; 141ee959b00STony Jones edev->edev.parent = get_device(dev); 142d569d5bbSJames Bottomley edev->cb = cb; 1435e43754fSYinghai Lu dev_set_name(&edev->edev, "%s", name); 144ee959b00STony Jones err = device_register(&edev->edev); 145d569d5bbSJames Bottomley if (err) 146d569d5bbSJames Bottomley goto err; 147d569d5bbSJames Bottomley 148921ce7f5SDan Williams for (i = 0; i < components; i++) { 149d569d5bbSJames Bottomley edev->component[i].number = -1; 150921ce7f5SDan Williams edev->component[i].slot = -1; 15175106523SMauricio Faria de Oliveira edev->component[i].power_status = -1; 152921ce7f5SDan Williams } 153d569d5bbSJames Bottomley 154d569d5bbSJames Bottomley mutex_lock(&container_list_lock); 155d569d5bbSJames Bottomley list_add_tail(&edev->node, &container_list); 156d569d5bbSJames Bottomley mutex_unlock(&container_list_lock); 157d569d5bbSJames Bottomley 158d569d5bbSJames Bottomley return edev; 159d569d5bbSJames Bottomley 160d569d5bbSJames Bottomley err: 161ee959b00STony Jones put_device(edev->edev.parent); 162d569d5bbSJames Bottomley kfree(edev); 163d569d5bbSJames Bottomley return ERR_PTR(err); 164d569d5bbSJames Bottomley } 165d569d5bbSJames Bottomley EXPORT_SYMBOL_GPL(enclosure_register); 166d569d5bbSJames Bottomley 167d569d5bbSJames Bottomley static struct enclosure_component_callbacks enclosure_null_callbacks; 168d569d5bbSJames Bottomley 169d569d5bbSJames Bottomley /** 170d569d5bbSJames Bottomley * enclosure_unregister - remove an enclosure 171d569d5bbSJames Bottomley * 172d569d5bbSJames Bottomley * @edev: the registered enclosure to remove; 173d569d5bbSJames Bottomley */ 174d569d5bbSJames Bottomley void enclosure_unregister(struct enclosure_device *edev) 175d569d5bbSJames Bottomley { 176d569d5bbSJames Bottomley int i; 177d569d5bbSJames Bottomley 178d569d5bbSJames Bottomley mutex_lock(&container_list_lock); 179d569d5bbSJames Bottomley list_del(&edev->node); 180d569d5bbSJames Bottomley mutex_unlock(&container_list_lock); 181d569d5bbSJames Bottomley 182d569d5bbSJames Bottomley for (i = 0; i < edev->components; i++) 183d569d5bbSJames Bottomley if (edev->component[i].number != -1) 184ee959b00STony Jones device_unregister(&edev->component[i].cdev); 185d569d5bbSJames Bottomley 186d569d5bbSJames Bottomley /* prevent any callbacks into service user */ 187d569d5bbSJames Bottomley edev->cb = &enclosure_null_callbacks; 188ee959b00STony Jones device_unregister(&edev->edev); 189d569d5bbSJames Bottomley } 190d569d5bbSJames Bottomley EXPORT_SYMBOL_GPL(enclosure_unregister); 191d569d5bbSJames Bottomley 192cb6b7f40SJames Bottomley #define ENCLOSURE_NAME_SIZE 64 193d2fd76e6SMarkus Stockhausen #define COMPONENT_NAME_SIZE 64 194cb6b7f40SJames Bottomley 195cb6b7f40SJames Bottomley static void enclosure_link_name(struct enclosure_component *cdev, char *name) 196cb6b7f40SJames Bottomley { 197cb6b7f40SJames Bottomley strcpy(name, "enclosure_device:"); 19871610f55SKay Sievers strcat(name, dev_name(&cdev->cdev)); 199cb6b7f40SJames Bottomley } 200cb6b7f40SJames Bottomley 201cb6b7f40SJames Bottomley static void enclosure_remove_links(struct enclosure_component *cdev) 202cb6b7f40SJames Bottomley { 203cb6b7f40SJames Bottomley char name[ENCLOSURE_NAME_SIZE]; 204cb6b7f40SJames Bottomley 20511e52a69SJames Bottomley enclosure_link_name(cdev, name); 20611e52a69SJames Bottomley 207a1470c7bSJames Bottomley /* 208a1470c7bSJames Bottomley * In odd circumstances, like multipath devices, something else may 209a1470c7bSJames Bottomley * already have removed the links, so check for this condition first. 210a1470c7bSJames Bottomley */ 21111e52a69SJames Bottomley if (cdev->dev->kobj.sd) 212cb6b7f40SJames Bottomley sysfs_remove_link(&cdev->dev->kobj, name); 21311e52a69SJames Bottomley 21411e52a69SJames Bottomley if (cdev->cdev.kobj.sd) 215cb6b7f40SJames Bottomley sysfs_remove_link(&cdev->cdev.kobj, "device"); 216cb6b7f40SJames Bottomley } 217cb6b7f40SJames Bottomley 218cb6b7f40SJames Bottomley static int enclosure_add_links(struct enclosure_component *cdev) 219cb6b7f40SJames Bottomley { 220cb6b7f40SJames Bottomley int error; 221cb6b7f40SJames Bottomley char name[ENCLOSURE_NAME_SIZE]; 222cb6b7f40SJames Bottomley 223cb6b7f40SJames Bottomley error = sysfs_create_link(&cdev->cdev.kobj, &cdev->dev->kobj, "device"); 224cb6b7f40SJames Bottomley if (error) 225cb6b7f40SJames Bottomley return error; 226cb6b7f40SJames Bottomley 227cb6b7f40SJames Bottomley enclosure_link_name(cdev, name); 228cb6b7f40SJames Bottomley error = sysfs_create_link(&cdev->dev->kobj, &cdev->cdev.kobj, name); 229cb6b7f40SJames Bottomley if (error) 230cb6b7f40SJames Bottomley sysfs_remove_link(&cdev->cdev.kobj, "device"); 231cb6b7f40SJames Bottomley 232cb6b7f40SJames Bottomley return error; 233cb6b7f40SJames Bottomley } 234cb6b7f40SJames Bottomley 235ee959b00STony Jones static void enclosure_release(struct device *cdev) 236d569d5bbSJames Bottomley { 237d569d5bbSJames Bottomley struct enclosure_device *edev = to_enclosure_device(cdev); 238d569d5bbSJames Bottomley 239ee959b00STony Jones put_device(cdev->parent); 240d569d5bbSJames Bottomley kfree(edev); 241d569d5bbSJames Bottomley } 242d569d5bbSJames Bottomley 243ee959b00STony Jones static void enclosure_component_release(struct device *dev) 244d569d5bbSJames Bottomley { 245ee959b00STony Jones struct enclosure_component *cdev = to_enclosure_component(dev); 246ee959b00STony Jones 247cb6b7f40SJames Bottomley if (cdev->dev) { 248cb6b7f40SJames Bottomley enclosure_remove_links(cdev); 249d569d5bbSJames Bottomley put_device(cdev->dev); 250cb6b7f40SJames Bottomley } 251ee959b00STony Jones put_device(dev->parent); 252d569d5bbSJames Bottomley } 253d569d5bbSJames Bottomley 254d2fd76e6SMarkus Stockhausen static struct enclosure_component * 255d2fd76e6SMarkus Stockhausen enclosure_component_find_by_name(struct enclosure_device *edev, 256d2fd76e6SMarkus Stockhausen const char *name) 257d2fd76e6SMarkus Stockhausen { 258d2fd76e6SMarkus Stockhausen int i; 259d2fd76e6SMarkus Stockhausen const char *cname; 260d2fd76e6SMarkus Stockhausen struct enclosure_component *ecomp; 261d2fd76e6SMarkus Stockhausen 262d2fd76e6SMarkus Stockhausen if (!edev || !name || !name[0]) 263d2fd76e6SMarkus Stockhausen return NULL; 264d2fd76e6SMarkus Stockhausen 265d2fd76e6SMarkus Stockhausen for (i = 0; i < edev->components; i++) { 266d2fd76e6SMarkus Stockhausen ecomp = &edev->component[i]; 267d2fd76e6SMarkus Stockhausen cname = dev_name(&ecomp->cdev); 268d2fd76e6SMarkus Stockhausen if (ecomp->number != -1 && 269d2fd76e6SMarkus Stockhausen cname && cname[0] && 270d2fd76e6SMarkus Stockhausen !strcmp(cname, name)) 271d2fd76e6SMarkus Stockhausen return ecomp; 272d2fd76e6SMarkus Stockhausen } 273d2fd76e6SMarkus Stockhausen 274d2fd76e6SMarkus Stockhausen return NULL; 275d2fd76e6SMarkus Stockhausen } 276d2fd76e6SMarkus Stockhausen 277899826f1SGreg Kroah-Hartman static const struct attribute_group *enclosure_component_groups[]; 278cb6b7f40SJames Bottomley 279d569d5bbSJames Bottomley /** 280ed09dcc8SDan Williams * enclosure_component_alloc - prepare a new enclosure component 281d569d5bbSJames Bottomley * @edev: the enclosure to add the component 282d569d5bbSJames Bottomley * @num: the device number 283d569d5bbSJames Bottomley * @type: the type of component being added 284d569d5bbSJames Bottomley * @name: an optional name to appear in sysfs (leave NULL if none) 285d569d5bbSJames Bottomley * 286ed09dcc8SDan Williams * The name is optional for enclosures that give their components a unique 287ed09dcc8SDan Williams * name. If not, leave the field NULL and a name will be assigned. 288d569d5bbSJames Bottomley * 289d569d5bbSJames Bottomley * Returns a pointer to the enclosure component or an error. 290d569d5bbSJames Bottomley */ 291d569d5bbSJames Bottomley struct enclosure_component * 292ed09dcc8SDan Williams enclosure_component_alloc(struct enclosure_device *edev, 293d569d5bbSJames Bottomley unsigned int number, 294d569d5bbSJames Bottomley enum enclosure_component_type type, 295d569d5bbSJames Bottomley const char *name) 296d569d5bbSJames Bottomley { 297d569d5bbSJames Bottomley struct enclosure_component *ecomp; 298ee959b00STony Jones struct device *cdev; 299ed09dcc8SDan Williams int i; 300d2fd76e6SMarkus Stockhausen char newname[COMPONENT_NAME_SIZE]; 301d569d5bbSJames Bottomley 302d569d5bbSJames Bottomley if (number >= edev->components) 303d569d5bbSJames Bottomley return ERR_PTR(-EINVAL); 304d569d5bbSJames Bottomley 305d569d5bbSJames Bottomley ecomp = &edev->component[number]; 306d569d5bbSJames Bottomley 307d569d5bbSJames Bottomley if (ecomp->number != -1) 308d569d5bbSJames Bottomley return ERR_PTR(-EINVAL); 309d569d5bbSJames Bottomley 310d569d5bbSJames Bottomley ecomp->type = type; 311d569d5bbSJames Bottomley ecomp->number = number; 312d569d5bbSJames Bottomley cdev = &ecomp->cdev; 313ee959b00STony Jones cdev->parent = get_device(&edev->edev); 314d2fd76e6SMarkus Stockhausen 315d2fd76e6SMarkus Stockhausen if (name && name[0]) { 316d2fd76e6SMarkus Stockhausen /* Some hardware (e.g. enclosure in RX300 S6) has components 317d2fd76e6SMarkus Stockhausen * with non unique names. Registering duplicates in sysfs 318d2fd76e6SMarkus Stockhausen * will lead to warnings during bootup. So make the names 319d2fd76e6SMarkus Stockhausen * unique by appending consecutive numbers -1, -2, ... */ 320d2fd76e6SMarkus Stockhausen i = 1; 321d2fd76e6SMarkus Stockhausen snprintf(newname, COMPONENT_NAME_SIZE, 322d2fd76e6SMarkus Stockhausen "%s", name); 323d2fd76e6SMarkus Stockhausen while (enclosure_component_find_by_name(edev, newname)) 324d2fd76e6SMarkus Stockhausen snprintf(newname, COMPONENT_NAME_SIZE, 325d2fd76e6SMarkus Stockhausen "%s-%i", name, i++); 326d2fd76e6SMarkus Stockhausen dev_set_name(cdev, "%s", newname); 327d2fd76e6SMarkus Stockhausen } else 32871610f55SKay Sievers dev_set_name(cdev, "%u", number); 329d569d5bbSJames Bottomley 330cb6b7f40SJames Bottomley cdev->release = enclosure_component_release; 331899826f1SGreg Kroah-Hartman cdev->groups = enclosure_component_groups; 332cb6b7f40SJames Bottomley 333ed09dcc8SDan Williams return ecomp; 334ed09dcc8SDan Williams } 335ed09dcc8SDan Williams EXPORT_SYMBOL_GPL(enclosure_component_alloc); 336ed09dcc8SDan Williams 337ed09dcc8SDan Williams /** 338ed09dcc8SDan Williams * enclosure_component_register - publishes an initialized enclosure component 339ed09dcc8SDan Williams * @ecomp: component to add 340ed09dcc8SDan Williams * 341ed09dcc8SDan Williams * Returns 0 on successful registration, releases the component otherwise 342ed09dcc8SDan Williams */ 343ed09dcc8SDan Williams int enclosure_component_register(struct enclosure_component *ecomp) 344ed09dcc8SDan Williams { 345ed09dcc8SDan Williams struct device *cdev; 346ed09dcc8SDan Williams int err; 347ed09dcc8SDan Williams 348ed09dcc8SDan Williams cdev = &ecomp->cdev; 349ee959b00STony Jones err = device_register(cdev); 350a91c1be2SJames Bottomley if (err) { 351a91c1be2SJames Bottomley ecomp->number = -1; 352a91c1be2SJames Bottomley put_device(cdev); 353ed09dcc8SDan Williams return err; 354a91c1be2SJames Bottomley } 355d569d5bbSJames Bottomley 356ed09dcc8SDan Williams return 0; 357d569d5bbSJames Bottomley } 358d569d5bbSJames Bottomley EXPORT_SYMBOL_GPL(enclosure_component_register); 359d569d5bbSJames Bottomley 360d569d5bbSJames Bottomley /** 361d569d5bbSJames Bottomley * enclosure_add_device - add a device as being part of an enclosure 362d569d5bbSJames Bottomley * @edev: the enclosure device being added to. 363d569d5bbSJames Bottomley * @num: the number of the component 364d569d5bbSJames Bottomley * @dev: the device being added 365d569d5bbSJames Bottomley * 366d569d5bbSJames Bottomley * Declares a real device to reside in slot (or identifier) @num of an 367d569d5bbSJames Bottomley * enclosure. This will cause the relevant sysfs links to appear. 368d569d5bbSJames Bottomley * This function may also be used to change a device associated with 369d569d5bbSJames Bottomley * an enclosure without having to call enclosure_remove_device() in 370d569d5bbSJames Bottomley * between. 371d569d5bbSJames Bottomley * 372d569d5bbSJames Bottomley * Returns zero on success or an error. 373d569d5bbSJames Bottomley */ 374d569d5bbSJames Bottomley int enclosure_add_device(struct enclosure_device *edev, int component, 375d569d5bbSJames Bottomley struct device *dev) 376d569d5bbSJames Bottomley { 377ee959b00STony Jones struct enclosure_component *cdev; 37862e62ffdSMaurizio Lombardi int err; 379d569d5bbSJames Bottomley 380d569d5bbSJames Bottomley if (!edev || component >= edev->components) 381d569d5bbSJames Bottomley return -EINVAL; 382d569d5bbSJames Bottomley 383ee959b00STony Jones cdev = &edev->component[component]; 384d569d5bbSJames Bottomley 38521fab1d0SJames Bottomley if (cdev->dev == dev) 38621fab1d0SJames Bottomley return -EEXIST; 38721fab1d0SJames Bottomley 38862e62ffdSMaurizio Lombardi if (cdev->dev) { 389cb6b7f40SJames Bottomley enclosure_remove_links(cdev); 390d569d5bbSJames Bottomley put_device(cdev->dev); 39162e62ffdSMaurizio Lombardi } 392d569d5bbSJames Bottomley cdev->dev = get_device(dev); 39362e62ffdSMaurizio Lombardi err = enclosure_add_links(cdev); 39462e62ffdSMaurizio Lombardi if (err) { 39562e62ffdSMaurizio Lombardi put_device(cdev->dev); 39662e62ffdSMaurizio Lombardi cdev->dev = NULL; 39762e62ffdSMaurizio Lombardi } 39862e62ffdSMaurizio Lombardi return err; 399d569d5bbSJames Bottomley } 400d569d5bbSJames Bottomley EXPORT_SYMBOL_GPL(enclosure_add_device); 401d569d5bbSJames Bottomley 402d569d5bbSJames Bottomley /** 403d569d5bbSJames Bottomley * enclosure_remove_device - remove a device from an enclosure 404d569d5bbSJames Bottomley * @edev: the enclosure device 405d569d5bbSJames Bottomley * @num: the number of the component to remove 406d569d5bbSJames Bottomley * 407d569d5bbSJames Bottomley * Returns zero on success or an error. 408d569d5bbSJames Bottomley * 409d569d5bbSJames Bottomley */ 41043d8eb9cSJames Bottomley int enclosure_remove_device(struct enclosure_device *edev, struct device *dev) 411d569d5bbSJames Bottomley { 412ee959b00STony Jones struct enclosure_component *cdev; 41343d8eb9cSJames Bottomley int i; 414d569d5bbSJames Bottomley 41543d8eb9cSJames Bottomley if (!edev || !dev) 416d569d5bbSJames Bottomley return -EINVAL; 417d569d5bbSJames Bottomley 41843d8eb9cSJames Bottomley for (i = 0; i < edev->components; i++) { 41943d8eb9cSJames Bottomley cdev = &edev->component[i]; 42043d8eb9cSJames Bottomley if (cdev->dev == dev) { 42143d8eb9cSJames Bottomley enclosure_remove_links(cdev); 422ee959b00STony Jones device_del(&cdev->cdev); 42343d8eb9cSJames Bottomley put_device(dev); 424d569d5bbSJames Bottomley cdev->dev = NULL; 425ee959b00STony Jones return device_add(&cdev->cdev); 426d569d5bbSJames Bottomley } 42743d8eb9cSJames Bottomley } 42843d8eb9cSJames Bottomley return -ENODEV; 42943d8eb9cSJames Bottomley } 430d569d5bbSJames Bottomley EXPORT_SYMBOL_GPL(enclosure_remove_device); 431d569d5bbSJames Bottomley 432d569d5bbSJames Bottomley /* 433d569d5bbSJames Bottomley * sysfs pieces below 434d569d5bbSJames Bottomley */ 435d569d5bbSJames Bottomley 436899826f1SGreg Kroah-Hartman static ssize_t components_show(struct device *cdev, 437899826f1SGreg Kroah-Hartman struct device_attribute *attr, char *buf) 438d569d5bbSJames Bottomley { 439d569d5bbSJames Bottomley struct enclosure_device *edev = to_enclosure_device(cdev); 440d569d5bbSJames Bottomley 441d569d5bbSJames Bottomley return snprintf(buf, 40, "%d\n", edev->components); 442d569d5bbSJames Bottomley } 443899826f1SGreg Kroah-Hartman static DEVICE_ATTR_RO(components); 444d569d5bbSJames Bottomley 445967f7babSDan Williams static ssize_t id_show(struct device *cdev, 446967f7babSDan Williams struct device_attribute *attr, 447967f7babSDan Williams char *buf) 448967f7babSDan Williams { 449967f7babSDan Williams struct enclosure_device *edev = to_enclosure_device(cdev); 450967f7babSDan Williams 451967f7babSDan Williams if (edev->cb->show_id) 452967f7babSDan Williams return edev->cb->show_id(edev, buf); 453967f7babSDan Williams return -EINVAL; 454967f7babSDan Williams } 455967f7babSDan Williams static DEVICE_ATTR_RO(id); 456967f7babSDan Williams 457899826f1SGreg Kroah-Hartman static struct attribute *enclosure_class_attrs[] = { 458899826f1SGreg Kroah-Hartman &dev_attr_components.attr, 459967f7babSDan Williams &dev_attr_id.attr, 460899826f1SGreg Kroah-Hartman NULL, 461d569d5bbSJames Bottomley }; 462899826f1SGreg Kroah-Hartman ATTRIBUTE_GROUPS(enclosure_class); 463d569d5bbSJames Bottomley 464d569d5bbSJames Bottomley static struct class enclosure_class = { 465d569d5bbSJames Bottomley .name = "enclosure", 466d569d5bbSJames Bottomley .owner = THIS_MODULE, 467ee959b00STony Jones .dev_release = enclosure_release, 468899826f1SGreg Kroah-Hartman .dev_groups = enclosure_class_groups, 469d569d5bbSJames Bottomley }; 470d569d5bbSJames Bottomley 471d569d5bbSJames Bottomley static const char *const enclosure_status[] = { 472d569d5bbSJames Bottomley [ENCLOSURE_STATUS_UNSUPPORTED] = "unsupported", 473d569d5bbSJames Bottomley [ENCLOSURE_STATUS_OK] = "OK", 474d569d5bbSJames Bottomley [ENCLOSURE_STATUS_CRITICAL] = "critical", 475d569d5bbSJames Bottomley [ENCLOSURE_STATUS_NON_CRITICAL] = "non-critical", 476d569d5bbSJames Bottomley [ENCLOSURE_STATUS_UNRECOVERABLE] = "unrecoverable", 477d569d5bbSJames Bottomley [ENCLOSURE_STATUS_NOT_INSTALLED] = "not installed", 478d569d5bbSJames Bottomley [ENCLOSURE_STATUS_UNKNOWN] = "unknown", 479d569d5bbSJames Bottomley [ENCLOSURE_STATUS_UNAVAILABLE] = "unavailable", 480cc9b2e9fSJames Bottomley [ENCLOSURE_STATUS_MAX] = NULL, 481d569d5bbSJames Bottomley }; 482d569d5bbSJames Bottomley 483d569d5bbSJames Bottomley static const char *const enclosure_type[] = { 484d569d5bbSJames Bottomley [ENCLOSURE_COMPONENT_DEVICE] = "device", 485d569d5bbSJames Bottomley [ENCLOSURE_COMPONENT_ARRAY_DEVICE] = "array device", 486d569d5bbSJames Bottomley }; 487d569d5bbSJames Bottomley 488ee959b00STony Jones static ssize_t get_component_fault(struct device *cdev, 489ee959b00STony Jones struct device_attribute *attr, char *buf) 490d569d5bbSJames Bottomley { 491d569d5bbSJames Bottomley struct enclosure_device *edev = to_enclosure_device(cdev->parent); 492d569d5bbSJames Bottomley struct enclosure_component *ecomp = to_enclosure_component(cdev); 493d569d5bbSJames Bottomley 494d569d5bbSJames Bottomley if (edev->cb->get_fault) 495d569d5bbSJames Bottomley edev->cb->get_fault(edev, ecomp); 496d569d5bbSJames Bottomley return snprintf(buf, 40, "%d\n", ecomp->fault); 497d569d5bbSJames Bottomley } 498d569d5bbSJames Bottomley 499ee959b00STony Jones static ssize_t set_component_fault(struct device *cdev, 500ee959b00STony Jones struct device_attribute *attr, 501ee959b00STony Jones const char *buf, size_t count) 502d569d5bbSJames Bottomley { 503d569d5bbSJames Bottomley struct enclosure_device *edev = to_enclosure_device(cdev->parent); 504d569d5bbSJames Bottomley struct enclosure_component *ecomp = to_enclosure_component(cdev); 505d569d5bbSJames Bottomley int val = simple_strtoul(buf, NULL, 0); 506d569d5bbSJames Bottomley 507d569d5bbSJames Bottomley if (edev->cb->set_fault) 508d569d5bbSJames Bottomley edev->cb->set_fault(edev, ecomp, val); 509d569d5bbSJames Bottomley return count; 510d569d5bbSJames Bottomley } 511d569d5bbSJames Bottomley 512ee959b00STony Jones static ssize_t get_component_status(struct device *cdev, 513ee959b00STony Jones struct device_attribute *attr,char *buf) 514d569d5bbSJames Bottomley { 515d569d5bbSJames Bottomley struct enclosure_device *edev = to_enclosure_device(cdev->parent); 516d569d5bbSJames Bottomley struct enclosure_component *ecomp = to_enclosure_component(cdev); 517d569d5bbSJames Bottomley 518d569d5bbSJames Bottomley if (edev->cb->get_status) 519d569d5bbSJames Bottomley edev->cb->get_status(edev, ecomp); 520d569d5bbSJames Bottomley return snprintf(buf, 40, "%s\n", enclosure_status[ecomp->status]); 521d569d5bbSJames Bottomley } 522d569d5bbSJames Bottomley 523ee959b00STony Jones static ssize_t set_component_status(struct device *cdev, 524ee959b00STony Jones struct device_attribute *attr, 525ee959b00STony Jones const char *buf, size_t count) 526d569d5bbSJames Bottomley { 527d569d5bbSJames Bottomley struct enclosure_device *edev = to_enclosure_device(cdev->parent); 528d569d5bbSJames Bottomley struct enclosure_component *ecomp = to_enclosure_component(cdev); 529d569d5bbSJames Bottomley int i; 530d569d5bbSJames Bottomley 531d569d5bbSJames Bottomley for (i = 0; enclosure_status[i]; i++) { 532d569d5bbSJames Bottomley if (strncmp(buf, enclosure_status[i], 533d569d5bbSJames Bottomley strlen(enclosure_status[i])) == 0 && 534d569d5bbSJames Bottomley (buf[strlen(enclosure_status[i])] == '\n' || 535d569d5bbSJames Bottomley buf[strlen(enclosure_status[i])] == '\0')) 536d569d5bbSJames Bottomley break; 537d569d5bbSJames Bottomley } 538d569d5bbSJames Bottomley 539d569d5bbSJames Bottomley if (enclosure_status[i] && edev->cb->set_status) { 540d569d5bbSJames Bottomley edev->cb->set_status(edev, ecomp, i); 541d569d5bbSJames Bottomley return count; 542d569d5bbSJames Bottomley } else 543d569d5bbSJames Bottomley return -EINVAL; 544d569d5bbSJames Bottomley } 545d569d5bbSJames Bottomley 546ee959b00STony Jones static ssize_t get_component_active(struct device *cdev, 547ee959b00STony Jones struct device_attribute *attr, char *buf) 548d569d5bbSJames Bottomley { 549d569d5bbSJames Bottomley struct enclosure_device *edev = to_enclosure_device(cdev->parent); 550d569d5bbSJames Bottomley struct enclosure_component *ecomp = to_enclosure_component(cdev); 551d569d5bbSJames Bottomley 552d569d5bbSJames Bottomley if (edev->cb->get_active) 553d569d5bbSJames Bottomley edev->cb->get_active(edev, ecomp); 554d569d5bbSJames Bottomley return snprintf(buf, 40, "%d\n", ecomp->active); 555d569d5bbSJames Bottomley } 556d569d5bbSJames Bottomley 557ee959b00STony Jones static ssize_t set_component_active(struct device *cdev, 558ee959b00STony Jones struct device_attribute *attr, 559ee959b00STony Jones const char *buf, size_t count) 560d569d5bbSJames Bottomley { 561d569d5bbSJames Bottomley struct enclosure_device *edev = to_enclosure_device(cdev->parent); 562d569d5bbSJames Bottomley struct enclosure_component *ecomp = to_enclosure_component(cdev); 563d569d5bbSJames Bottomley int val = simple_strtoul(buf, NULL, 0); 564d569d5bbSJames Bottomley 565d569d5bbSJames Bottomley if (edev->cb->set_active) 566d569d5bbSJames Bottomley edev->cb->set_active(edev, ecomp, val); 567d569d5bbSJames Bottomley return count; 568d569d5bbSJames Bottomley } 569d569d5bbSJames Bottomley 570ee959b00STony Jones static ssize_t get_component_locate(struct device *cdev, 571ee959b00STony Jones struct device_attribute *attr, char *buf) 572d569d5bbSJames Bottomley { 573d569d5bbSJames Bottomley struct enclosure_device *edev = to_enclosure_device(cdev->parent); 574d569d5bbSJames Bottomley struct enclosure_component *ecomp = to_enclosure_component(cdev); 575d569d5bbSJames Bottomley 576d569d5bbSJames Bottomley if (edev->cb->get_locate) 577d569d5bbSJames Bottomley edev->cb->get_locate(edev, ecomp); 578d569d5bbSJames Bottomley return snprintf(buf, 40, "%d\n", ecomp->locate); 579d569d5bbSJames Bottomley } 580d569d5bbSJames Bottomley 581ee959b00STony Jones static ssize_t set_component_locate(struct device *cdev, 582ee959b00STony Jones struct device_attribute *attr, 583ee959b00STony Jones const char *buf, size_t count) 584d569d5bbSJames Bottomley { 585d569d5bbSJames Bottomley struct enclosure_device *edev = to_enclosure_device(cdev->parent); 586d569d5bbSJames Bottomley struct enclosure_component *ecomp = to_enclosure_component(cdev); 587d569d5bbSJames Bottomley int val = simple_strtoul(buf, NULL, 0); 588d569d5bbSJames Bottomley 589d569d5bbSJames Bottomley if (edev->cb->set_locate) 590d569d5bbSJames Bottomley edev->cb->set_locate(edev, ecomp, val); 591d569d5bbSJames Bottomley return count; 592d569d5bbSJames Bottomley } 593d569d5bbSJames Bottomley 59408024885SSong Liu static ssize_t get_component_power_status(struct device *cdev, 59508024885SSong Liu struct device_attribute *attr, 59608024885SSong Liu char *buf) 59708024885SSong Liu { 59808024885SSong Liu struct enclosure_device *edev = to_enclosure_device(cdev->parent); 59908024885SSong Liu struct enclosure_component *ecomp = to_enclosure_component(cdev); 60008024885SSong Liu 60108024885SSong Liu if (edev->cb->get_power_status) 60208024885SSong Liu edev->cb->get_power_status(edev, ecomp); 60375106523SMauricio Faria de Oliveira 60475106523SMauricio Faria de Oliveira /* If still uninitialized, the callback failed or does not exist. */ 60575106523SMauricio Faria de Oliveira if (ecomp->power_status == -1) 60675106523SMauricio Faria de Oliveira return (edev->cb->get_power_status) ? -EIO : -ENOTTY; 60775106523SMauricio Faria de Oliveira 60808024885SSong Liu return snprintf(buf, 40, "%s\n", ecomp->power_status ? "on" : "off"); 60908024885SSong Liu } 61008024885SSong Liu 61108024885SSong Liu static ssize_t set_component_power_status(struct device *cdev, 61208024885SSong Liu struct device_attribute *attr, 61308024885SSong Liu const char *buf, size_t count) 61408024885SSong Liu { 61508024885SSong Liu struct enclosure_device *edev = to_enclosure_device(cdev->parent); 61608024885SSong Liu struct enclosure_component *ecomp = to_enclosure_component(cdev); 61708024885SSong Liu int val; 61808024885SSong Liu 61908024885SSong Liu if (strncmp(buf, "on", 2) == 0 && 62008024885SSong Liu (buf[2] == '\n' || buf[2] == '\0')) 62108024885SSong Liu val = 1; 62208024885SSong Liu else if (strncmp(buf, "off", 3) == 0 && 62308024885SSong Liu (buf[3] == '\n' || buf[3] == '\0')) 62408024885SSong Liu val = 0; 62508024885SSong Liu else 62608024885SSong Liu return -EINVAL; 62708024885SSong Liu 62808024885SSong Liu if (edev->cb->set_power_status) 62908024885SSong Liu edev->cb->set_power_status(edev, ecomp, val); 63008024885SSong Liu return count; 63108024885SSong Liu } 63208024885SSong Liu 633ee959b00STony Jones static ssize_t get_component_type(struct device *cdev, 634ee959b00STony Jones struct device_attribute *attr, char *buf) 635d569d5bbSJames Bottomley { 636d569d5bbSJames Bottomley struct enclosure_component *ecomp = to_enclosure_component(cdev); 637d569d5bbSJames Bottomley 638d569d5bbSJames Bottomley return snprintf(buf, 40, "%s\n", enclosure_type[ecomp->type]); 639d569d5bbSJames Bottomley } 640d569d5bbSJames Bottomley 641921ce7f5SDan Williams static ssize_t get_component_slot(struct device *cdev, 642921ce7f5SDan Williams struct device_attribute *attr, char *buf) 643921ce7f5SDan Williams { 644921ce7f5SDan Williams struct enclosure_component *ecomp = to_enclosure_component(cdev); 645921ce7f5SDan Williams int slot; 646921ce7f5SDan Williams 647921ce7f5SDan Williams /* if the enclosure does not override then use 'number' as a stand-in */ 648921ce7f5SDan Williams if (ecomp->slot >= 0) 649921ce7f5SDan Williams slot = ecomp->slot; 650921ce7f5SDan Williams else 651921ce7f5SDan Williams slot = ecomp->number; 652921ce7f5SDan Williams 653921ce7f5SDan Williams return snprintf(buf, 40, "%d\n", slot); 654921ce7f5SDan Williams } 655d569d5bbSJames Bottomley 656cb6b7f40SJames Bottomley static DEVICE_ATTR(fault, S_IRUGO | S_IWUSR, get_component_fault, 657cb6b7f40SJames Bottomley set_component_fault); 658cb6b7f40SJames Bottomley static DEVICE_ATTR(status, S_IRUGO | S_IWUSR, get_component_status, 659cb6b7f40SJames Bottomley set_component_status); 660cb6b7f40SJames Bottomley static DEVICE_ATTR(active, S_IRUGO | S_IWUSR, get_component_active, 661cb6b7f40SJames Bottomley set_component_active); 662cb6b7f40SJames Bottomley static DEVICE_ATTR(locate, S_IRUGO | S_IWUSR, get_component_locate, 663cb6b7f40SJames Bottomley set_component_locate); 66408024885SSong Liu static DEVICE_ATTR(power_status, S_IRUGO | S_IWUSR, get_component_power_status, 66508024885SSong Liu set_component_power_status); 666cb6b7f40SJames Bottomley static DEVICE_ATTR(type, S_IRUGO, get_component_type, NULL); 667921ce7f5SDan Williams static DEVICE_ATTR(slot, S_IRUGO, get_component_slot, NULL); 668cb6b7f40SJames Bottomley 669cb6b7f40SJames Bottomley static struct attribute *enclosure_component_attrs[] = { 670cb6b7f40SJames Bottomley &dev_attr_fault.attr, 671cb6b7f40SJames Bottomley &dev_attr_status.attr, 672cb6b7f40SJames Bottomley &dev_attr_active.attr, 673cb6b7f40SJames Bottomley &dev_attr_locate.attr, 67408024885SSong Liu &dev_attr_power_status.attr, 675cb6b7f40SJames Bottomley &dev_attr_type.attr, 676921ce7f5SDan Williams &dev_attr_slot.attr, 677cb6b7f40SJames Bottomley NULL 678d569d5bbSJames Bottomley }; 679899826f1SGreg Kroah-Hartman ATTRIBUTE_GROUPS(enclosure_component); 680d569d5bbSJames Bottomley 681d569d5bbSJames Bottomley static int __init enclosure_init(void) 682d569d5bbSJames Bottomley { 683*750b54deSArvind Yadav return class_register(&enclosure_class); 684d569d5bbSJames Bottomley } 685d569d5bbSJames Bottomley 686d569d5bbSJames Bottomley static void __exit enclosure_exit(void) 687d569d5bbSJames Bottomley { 688d569d5bbSJames Bottomley class_unregister(&enclosure_class); 689d569d5bbSJames Bottomley } 690d569d5bbSJames Bottomley 691d569d5bbSJames Bottomley module_init(enclosure_init); 692d569d5bbSJames Bottomley module_exit(enclosure_exit); 693d569d5bbSJames Bottomley 694d569d5bbSJames Bottomley MODULE_AUTHOR("James Bottomley"); 695d569d5bbSJames Bottomley MODULE_DESCRIPTION("Enclosure Services"); 696d569d5bbSJames Bottomley MODULE_LICENSE("GPL v2"); 697