xref: /openbmc/linux/drivers/misc/enclosure.c (revision 75106523f39751390b5789b36ee1d213b3af1945)
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;
151*75106523SMauricio 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;
378d569d5bbSJames Bottomley 
379d569d5bbSJames Bottomley 	if (!edev || component >= edev->components)
380d569d5bbSJames Bottomley 		return -EINVAL;
381d569d5bbSJames Bottomley 
382ee959b00STony Jones 	cdev = &edev->component[component];
383d569d5bbSJames Bottomley 
38421fab1d0SJames Bottomley 	if (cdev->dev == dev)
38521fab1d0SJames Bottomley 		return -EEXIST;
38621fab1d0SJames Bottomley 
387cb6b7f40SJames Bottomley 	if (cdev->dev)
388cb6b7f40SJames Bottomley 		enclosure_remove_links(cdev);
389cb6b7f40SJames Bottomley 
390d569d5bbSJames Bottomley 	put_device(cdev->dev);
391d569d5bbSJames Bottomley 	cdev->dev = get_device(dev);
392cb6b7f40SJames Bottomley 	return enclosure_add_links(cdev);
393d569d5bbSJames Bottomley }
394d569d5bbSJames Bottomley EXPORT_SYMBOL_GPL(enclosure_add_device);
395d569d5bbSJames Bottomley 
396d569d5bbSJames Bottomley /**
397d569d5bbSJames Bottomley  * enclosure_remove_device - remove a device from an enclosure
398d569d5bbSJames Bottomley  * @edev:	the enclosure device
399d569d5bbSJames Bottomley  * @num:	the number of the component to remove
400d569d5bbSJames Bottomley  *
401d569d5bbSJames Bottomley  * Returns zero on success or an error.
402d569d5bbSJames Bottomley  *
403d569d5bbSJames Bottomley  */
40443d8eb9cSJames Bottomley int enclosure_remove_device(struct enclosure_device *edev, struct device *dev)
405d569d5bbSJames Bottomley {
406ee959b00STony Jones 	struct enclosure_component *cdev;
40743d8eb9cSJames Bottomley 	int i;
408d569d5bbSJames Bottomley 
40943d8eb9cSJames Bottomley 	if (!edev || !dev)
410d569d5bbSJames Bottomley 		return -EINVAL;
411d569d5bbSJames Bottomley 
41243d8eb9cSJames Bottomley 	for (i = 0; i < edev->components; i++) {
41343d8eb9cSJames Bottomley 		cdev = &edev->component[i];
41443d8eb9cSJames Bottomley 		if (cdev->dev == dev) {
41543d8eb9cSJames Bottomley 			enclosure_remove_links(cdev);
416ee959b00STony Jones 			device_del(&cdev->cdev);
41743d8eb9cSJames Bottomley 			put_device(dev);
418d569d5bbSJames Bottomley 			cdev->dev = NULL;
419ee959b00STony Jones 			return device_add(&cdev->cdev);
420d569d5bbSJames Bottomley 		}
42143d8eb9cSJames Bottomley 	}
42243d8eb9cSJames Bottomley 	return -ENODEV;
42343d8eb9cSJames Bottomley }
424d569d5bbSJames Bottomley EXPORT_SYMBOL_GPL(enclosure_remove_device);
425d569d5bbSJames Bottomley 
426d569d5bbSJames Bottomley /*
427d569d5bbSJames Bottomley  * sysfs pieces below
428d569d5bbSJames Bottomley  */
429d569d5bbSJames Bottomley 
430899826f1SGreg Kroah-Hartman static ssize_t components_show(struct device *cdev,
431899826f1SGreg Kroah-Hartman 			       struct device_attribute *attr, char *buf)
432d569d5bbSJames Bottomley {
433d569d5bbSJames Bottomley 	struct enclosure_device *edev = to_enclosure_device(cdev);
434d569d5bbSJames Bottomley 
435d569d5bbSJames Bottomley 	return snprintf(buf, 40, "%d\n", edev->components);
436d569d5bbSJames Bottomley }
437899826f1SGreg Kroah-Hartman static DEVICE_ATTR_RO(components);
438d569d5bbSJames Bottomley 
439967f7babSDan Williams static ssize_t id_show(struct device *cdev,
440967f7babSDan Williams 				 struct device_attribute *attr,
441967f7babSDan Williams 				 char *buf)
442967f7babSDan Williams {
443967f7babSDan Williams 	struct enclosure_device *edev = to_enclosure_device(cdev);
444967f7babSDan Williams 
445967f7babSDan Williams 	if (edev->cb->show_id)
446967f7babSDan Williams 		return edev->cb->show_id(edev, buf);
447967f7babSDan Williams 	return -EINVAL;
448967f7babSDan Williams }
449967f7babSDan Williams static DEVICE_ATTR_RO(id);
450967f7babSDan Williams 
451899826f1SGreg Kroah-Hartman static struct attribute *enclosure_class_attrs[] = {
452899826f1SGreg Kroah-Hartman 	&dev_attr_components.attr,
453967f7babSDan Williams 	&dev_attr_id.attr,
454899826f1SGreg Kroah-Hartman 	NULL,
455d569d5bbSJames Bottomley };
456899826f1SGreg Kroah-Hartman ATTRIBUTE_GROUPS(enclosure_class);
457d569d5bbSJames Bottomley 
458d569d5bbSJames Bottomley static struct class enclosure_class = {
459d569d5bbSJames Bottomley 	.name			= "enclosure",
460d569d5bbSJames Bottomley 	.owner			= THIS_MODULE,
461ee959b00STony Jones 	.dev_release		= enclosure_release,
462899826f1SGreg Kroah-Hartman 	.dev_groups		= enclosure_class_groups,
463d569d5bbSJames Bottomley };
464d569d5bbSJames Bottomley 
465d569d5bbSJames Bottomley static const char *const enclosure_status [] = {
466d569d5bbSJames Bottomley 	[ENCLOSURE_STATUS_UNSUPPORTED] = "unsupported",
467d569d5bbSJames Bottomley 	[ENCLOSURE_STATUS_OK] = "OK",
468d569d5bbSJames Bottomley 	[ENCLOSURE_STATUS_CRITICAL] = "critical",
469d569d5bbSJames Bottomley 	[ENCLOSURE_STATUS_NON_CRITICAL] = "non-critical",
470d569d5bbSJames Bottomley 	[ENCLOSURE_STATUS_UNRECOVERABLE] = "unrecoverable",
471d569d5bbSJames Bottomley 	[ENCLOSURE_STATUS_NOT_INSTALLED] = "not installed",
472d569d5bbSJames Bottomley 	[ENCLOSURE_STATUS_UNKNOWN] = "unknown",
473d569d5bbSJames Bottomley 	[ENCLOSURE_STATUS_UNAVAILABLE] = "unavailable",
474cc9b2e9fSJames Bottomley 	[ENCLOSURE_STATUS_MAX] = NULL,
475d569d5bbSJames Bottomley };
476d569d5bbSJames Bottomley 
477d569d5bbSJames Bottomley static const char *const enclosure_type [] = {
478d569d5bbSJames Bottomley 	[ENCLOSURE_COMPONENT_DEVICE] = "device",
479d569d5bbSJames Bottomley 	[ENCLOSURE_COMPONENT_ARRAY_DEVICE] = "array device",
480d569d5bbSJames Bottomley };
481d569d5bbSJames Bottomley 
482ee959b00STony Jones static ssize_t get_component_fault(struct device *cdev,
483ee959b00STony Jones 				   struct device_attribute *attr, char *buf)
484d569d5bbSJames Bottomley {
485d569d5bbSJames Bottomley 	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
486d569d5bbSJames Bottomley 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
487d569d5bbSJames Bottomley 
488d569d5bbSJames Bottomley 	if (edev->cb->get_fault)
489d569d5bbSJames Bottomley 		edev->cb->get_fault(edev, ecomp);
490d569d5bbSJames Bottomley 	return snprintf(buf, 40, "%d\n", ecomp->fault);
491d569d5bbSJames Bottomley }
492d569d5bbSJames Bottomley 
493ee959b00STony Jones static ssize_t set_component_fault(struct device *cdev,
494ee959b00STony Jones 				   struct device_attribute *attr,
495ee959b00STony Jones 				   const char *buf, size_t count)
496d569d5bbSJames Bottomley {
497d569d5bbSJames Bottomley 	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
498d569d5bbSJames Bottomley 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
499d569d5bbSJames Bottomley 	int val = simple_strtoul(buf, NULL, 0);
500d569d5bbSJames Bottomley 
501d569d5bbSJames Bottomley 	if (edev->cb->set_fault)
502d569d5bbSJames Bottomley 		edev->cb->set_fault(edev, ecomp, val);
503d569d5bbSJames Bottomley 	return count;
504d569d5bbSJames Bottomley }
505d569d5bbSJames Bottomley 
506ee959b00STony Jones static ssize_t get_component_status(struct device *cdev,
507ee959b00STony Jones 				    struct device_attribute *attr,char *buf)
508d569d5bbSJames Bottomley {
509d569d5bbSJames Bottomley 	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
510d569d5bbSJames Bottomley 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
511d569d5bbSJames Bottomley 
512d569d5bbSJames Bottomley 	if (edev->cb->get_status)
513d569d5bbSJames Bottomley 		edev->cb->get_status(edev, ecomp);
514d569d5bbSJames Bottomley 	return snprintf(buf, 40, "%s\n", enclosure_status[ecomp->status]);
515d569d5bbSJames Bottomley }
516d569d5bbSJames Bottomley 
517ee959b00STony Jones static ssize_t set_component_status(struct device *cdev,
518ee959b00STony Jones 				    struct device_attribute *attr,
519ee959b00STony Jones 				    const char *buf, size_t count)
520d569d5bbSJames Bottomley {
521d569d5bbSJames Bottomley 	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
522d569d5bbSJames Bottomley 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
523d569d5bbSJames Bottomley 	int i;
524d569d5bbSJames Bottomley 
525d569d5bbSJames Bottomley 	for (i = 0; enclosure_status[i]; i++) {
526d569d5bbSJames Bottomley 		if (strncmp(buf, enclosure_status[i],
527d569d5bbSJames Bottomley 			    strlen(enclosure_status[i])) == 0 &&
528d569d5bbSJames Bottomley 		    (buf[strlen(enclosure_status[i])] == '\n' ||
529d569d5bbSJames Bottomley 		     buf[strlen(enclosure_status[i])] == '\0'))
530d569d5bbSJames Bottomley 			break;
531d569d5bbSJames Bottomley 	}
532d569d5bbSJames Bottomley 
533d569d5bbSJames Bottomley 	if (enclosure_status[i] && edev->cb->set_status) {
534d569d5bbSJames Bottomley 		edev->cb->set_status(edev, ecomp, i);
535d569d5bbSJames Bottomley 		return count;
536d569d5bbSJames Bottomley 	} else
537d569d5bbSJames Bottomley 		return -EINVAL;
538d569d5bbSJames Bottomley }
539d569d5bbSJames Bottomley 
540ee959b00STony Jones static ssize_t get_component_active(struct device *cdev,
541ee959b00STony Jones 				    struct device_attribute *attr, char *buf)
542d569d5bbSJames Bottomley {
543d569d5bbSJames Bottomley 	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
544d569d5bbSJames Bottomley 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
545d569d5bbSJames Bottomley 
546d569d5bbSJames Bottomley 	if (edev->cb->get_active)
547d569d5bbSJames Bottomley 		edev->cb->get_active(edev, ecomp);
548d569d5bbSJames Bottomley 	return snprintf(buf, 40, "%d\n", ecomp->active);
549d569d5bbSJames Bottomley }
550d569d5bbSJames Bottomley 
551ee959b00STony Jones static ssize_t set_component_active(struct device *cdev,
552ee959b00STony Jones 				    struct device_attribute *attr,
553ee959b00STony Jones 				    const char *buf, size_t count)
554d569d5bbSJames Bottomley {
555d569d5bbSJames Bottomley 	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
556d569d5bbSJames Bottomley 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
557d569d5bbSJames Bottomley 	int val = simple_strtoul(buf, NULL, 0);
558d569d5bbSJames Bottomley 
559d569d5bbSJames Bottomley 	if (edev->cb->set_active)
560d569d5bbSJames Bottomley 		edev->cb->set_active(edev, ecomp, val);
561d569d5bbSJames Bottomley 	return count;
562d569d5bbSJames Bottomley }
563d569d5bbSJames Bottomley 
564ee959b00STony Jones static ssize_t get_component_locate(struct device *cdev,
565ee959b00STony Jones 				    struct device_attribute *attr, char *buf)
566d569d5bbSJames Bottomley {
567d569d5bbSJames Bottomley 	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
568d569d5bbSJames Bottomley 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
569d569d5bbSJames Bottomley 
570d569d5bbSJames Bottomley 	if (edev->cb->get_locate)
571d569d5bbSJames Bottomley 		edev->cb->get_locate(edev, ecomp);
572d569d5bbSJames Bottomley 	return snprintf(buf, 40, "%d\n", ecomp->locate);
573d569d5bbSJames Bottomley }
574d569d5bbSJames Bottomley 
575ee959b00STony Jones static ssize_t set_component_locate(struct device *cdev,
576ee959b00STony Jones 				    struct device_attribute *attr,
577ee959b00STony Jones 				    const char *buf, size_t count)
578d569d5bbSJames Bottomley {
579d569d5bbSJames Bottomley 	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
580d569d5bbSJames Bottomley 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
581d569d5bbSJames Bottomley 	int val = simple_strtoul(buf, NULL, 0);
582d569d5bbSJames Bottomley 
583d569d5bbSJames Bottomley 	if (edev->cb->set_locate)
584d569d5bbSJames Bottomley 		edev->cb->set_locate(edev, ecomp, val);
585d569d5bbSJames Bottomley 	return count;
586d569d5bbSJames Bottomley }
587d569d5bbSJames Bottomley 
58808024885SSong Liu static ssize_t get_component_power_status(struct device *cdev,
58908024885SSong Liu 					  struct device_attribute *attr,
59008024885SSong Liu 					  char *buf)
59108024885SSong Liu {
59208024885SSong Liu 	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
59308024885SSong Liu 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
59408024885SSong Liu 
59508024885SSong Liu 	if (edev->cb->get_power_status)
59608024885SSong Liu 		edev->cb->get_power_status(edev, ecomp);
597*75106523SMauricio Faria de Oliveira 
598*75106523SMauricio Faria de Oliveira 	/* If still uninitialized, the callback failed or does not exist. */
599*75106523SMauricio Faria de Oliveira 	if (ecomp->power_status == -1)
600*75106523SMauricio Faria de Oliveira 		return (edev->cb->get_power_status) ? -EIO : -ENOTTY;
601*75106523SMauricio Faria de Oliveira 
60208024885SSong Liu 	return snprintf(buf, 40, "%s\n", ecomp->power_status ? "on" : "off");
60308024885SSong Liu }
60408024885SSong Liu 
60508024885SSong Liu static ssize_t set_component_power_status(struct device *cdev,
60608024885SSong Liu 					  struct device_attribute *attr,
60708024885SSong Liu 					  const char *buf, size_t count)
60808024885SSong Liu {
60908024885SSong Liu 	struct enclosure_device *edev = to_enclosure_device(cdev->parent);
61008024885SSong Liu 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
61108024885SSong Liu 	int val;
61208024885SSong Liu 
61308024885SSong Liu 	if (strncmp(buf, "on", 2) == 0 &&
61408024885SSong Liu 	    (buf[2] == '\n' || buf[2] == '\0'))
61508024885SSong Liu 		val = 1;
61608024885SSong Liu 	else if (strncmp(buf, "off", 3) == 0 &&
61708024885SSong Liu 	    (buf[3] == '\n' || buf[3] == '\0'))
61808024885SSong Liu 		val = 0;
61908024885SSong Liu 	else
62008024885SSong Liu 		return -EINVAL;
62108024885SSong Liu 
62208024885SSong Liu 	if (edev->cb->set_power_status)
62308024885SSong Liu 		edev->cb->set_power_status(edev, ecomp, val);
62408024885SSong Liu 	return count;
62508024885SSong Liu }
62608024885SSong Liu 
627ee959b00STony Jones static ssize_t get_component_type(struct device *cdev,
628ee959b00STony Jones 				  struct device_attribute *attr, char *buf)
629d569d5bbSJames Bottomley {
630d569d5bbSJames Bottomley 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
631d569d5bbSJames Bottomley 
632d569d5bbSJames Bottomley 	return snprintf(buf, 40, "%s\n", enclosure_type[ecomp->type]);
633d569d5bbSJames Bottomley }
634d569d5bbSJames Bottomley 
635921ce7f5SDan Williams static ssize_t get_component_slot(struct device *cdev,
636921ce7f5SDan Williams 				  struct device_attribute *attr, char *buf)
637921ce7f5SDan Williams {
638921ce7f5SDan Williams 	struct enclosure_component *ecomp = to_enclosure_component(cdev);
639921ce7f5SDan Williams 	int slot;
640921ce7f5SDan Williams 
641921ce7f5SDan Williams 	/* if the enclosure does not override then use 'number' as a stand-in */
642921ce7f5SDan Williams 	if (ecomp->slot >= 0)
643921ce7f5SDan Williams 		slot = ecomp->slot;
644921ce7f5SDan Williams 	else
645921ce7f5SDan Williams 		slot = ecomp->number;
646921ce7f5SDan Williams 
647921ce7f5SDan Williams 	return snprintf(buf, 40, "%d\n", slot);
648921ce7f5SDan Williams }
649d569d5bbSJames Bottomley 
650cb6b7f40SJames Bottomley static DEVICE_ATTR(fault, S_IRUGO | S_IWUSR, get_component_fault,
651cb6b7f40SJames Bottomley 		    set_component_fault);
652cb6b7f40SJames Bottomley static DEVICE_ATTR(status, S_IRUGO | S_IWUSR, get_component_status,
653cb6b7f40SJames Bottomley 		   set_component_status);
654cb6b7f40SJames Bottomley static DEVICE_ATTR(active, S_IRUGO | S_IWUSR, get_component_active,
655cb6b7f40SJames Bottomley 		   set_component_active);
656cb6b7f40SJames Bottomley static DEVICE_ATTR(locate, S_IRUGO | S_IWUSR, get_component_locate,
657cb6b7f40SJames Bottomley 		   set_component_locate);
65808024885SSong Liu static DEVICE_ATTR(power_status, S_IRUGO | S_IWUSR, get_component_power_status,
65908024885SSong Liu 		   set_component_power_status);
660cb6b7f40SJames Bottomley static DEVICE_ATTR(type, S_IRUGO, get_component_type, NULL);
661921ce7f5SDan Williams static DEVICE_ATTR(slot, S_IRUGO, get_component_slot, NULL);
662cb6b7f40SJames Bottomley 
663cb6b7f40SJames Bottomley static struct attribute *enclosure_component_attrs[] = {
664cb6b7f40SJames Bottomley 	&dev_attr_fault.attr,
665cb6b7f40SJames Bottomley 	&dev_attr_status.attr,
666cb6b7f40SJames Bottomley 	&dev_attr_active.attr,
667cb6b7f40SJames Bottomley 	&dev_attr_locate.attr,
66808024885SSong Liu 	&dev_attr_power_status.attr,
669cb6b7f40SJames Bottomley 	&dev_attr_type.attr,
670921ce7f5SDan Williams 	&dev_attr_slot.attr,
671cb6b7f40SJames Bottomley 	NULL
672d569d5bbSJames Bottomley };
673899826f1SGreg Kroah-Hartman ATTRIBUTE_GROUPS(enclosure_component);
674d569d5bbSJames Bottomley 
675d569d5bbSJames Bottomley static int __init enclosure_init(void)
676d569d5bbSJames Bottomley {
677d569d5bbSJames Bottomley 	int err;
678d569d5bbSJames Bottomley 
679d569d5bbSJames Bottomley 	err = class_register(&enclosure_class);
680d569d5bbSJames Bottomley 	if (err)
681d569d5bbSJames Bottomley 		return err;
682d569d5bbSJames Bottomley 
683d569d5bbSJames Bottomley 	return 0;
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