xref: /linux/drivers/base/core.c (revision 5e8d780d745c1619aba81fe7166c5a4b5cad2b84)
1 /*
2  * drivers/base/core.c - core driver model code (device registration, etc)
3  *
4  * Copyright (c) 2002-3 Patrick Mochel
5  * Copyright (c) 2002-3 Open Source Development Labs
6  *
7  * This file is released under the GPLv2
8  *
9  */
10 
11 #include <linux/config.h>
12 #include <linux/device.h>
13 #include <linux/err.h>
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/string.h>
18 #include <linux/kdev_t.h>
19 
20 #include <asm/semaphore.h>
21 
22 #include "base.h"
23 #include "power/power.h"
24 
25 int (*platform_notify)(struct device * dev) = NULL;
26 int (*platform_notify_remove)(struct device * dev) = NULL;
27 
28 /*
29  * sysfs bindings for devices.
30  */
31 
32 /**
33  * dev_driver_string - Return a device's driver name, if at all possible
34  * @dev: struct device to get the name of
35  *
36  * Will return the device's driver's name if it is bound to a device.  If
37  * the device is not bound to a device, it will return the name of the bus
38  * it is attached to.  If it is not attached to a bus either, an empty
39  * string will be returned.
40  */
41 const char *dev_driver_string(struct device *dev)
42 {
43 	return dev->driver ? dev->driver->name :
44 			(dev->bus ? dev->bus->name : "");
45 }
46 EXPORT_SYMBOL_GPL(dev_driver_string);
47 
48 #define to_dev(obj) container_of(obj, struct device, kobj)
49 #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
50 
51 static ssize_t
52 dev_attr_show(struct kobject * kobj, struct attribute * attr, char * buf)
53 {
54 	struct device_attribute * dev_attr = to_dev_attr(attr);
55 	struct device * dev = to_dev(kobj);
56 	ssize_t ret = -EIO;
57 
58 	if (dev_attr->show)
59 		ret = dev_attr->show(dev, dev_attr, buf);
60 	return ret;
61 }
62 
63 static ssize_t
64 dev_attr_store(struct kobject * kobj, struct attribute * attr,
65 	       const char * buf, size_t count)
66 {
67 	struct device_attribute * dev_attr = to_dev_attr(attr);
68 	struct device * dev = to_dev(kobj);
69 	ssize_t ret = -EIO;
70 
71 	if (dev_attr->store)
72 		ret = dev_attr->store(dev, dev_attr, buf, count);
73 	return ret;
74 }
75 
76 static struct sysfs_ops dev_sysfs_ops = {
77 	.show	= dev_attr_show,
78 	.store	= dev_attr_store,
79 };
80 
81 
82 /**
83  *	device_release - free device structure.
84  *	@kobj:	device's kobject.
85  *
86  *	This is called once the reference count for the object
87  *	reaches 0. We forward the call to the device's release
88  *	method, which should handle actually freeing the structure.
89  */
90 static void device_release(struct kobject * kobj)
91 {
92 	struct device * dev = to_dev(kobj);
93 
94 	if (dev->release)
95 		dev->release(dev);
96 	else {
97 		printk(KERN_ERR "Device '%s' does not have a release() function, "
98 			"it is broken and must be fixed.\n",
99 			dev->bus_id);
100 		WARN_ON(1);
101 	}
102 }
103 
104 static struct kobj_type ktype_device = {
105 	.release	= device_release,
106 	.sysfs_ops	= &dev_sysfs_ops,
107 };
108 
109 
110 static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
111 {
112 	struct kobj_type *ktype = get_ktype(kobj);
113 
114 	if (ktype == &ktype_device) {
115 		struct device *dev = to_dev(kobj);
116 		if (dev->bus)
117 			return 1;
118 		if (dev->class)
119 			return 1;
120 	}
121 	return 0;
122 }
123 
124 static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
125 {
126 	struct device *dev = to_dev(kobj);
127 
128 	if (dev->bus)
129 		return dev->bus->name;
130 	if (dev->class)
131 		return dev->class->name;
132 	return NULL;
133 }
134 
135 static int dev_uevent(struct kset *kset, struct kobject *kobj, char **envp,
136 			int num_envp, char *buffer, int buffer_size)
137 {
138 	struct device *dev = to_dev(kobj);
139 	int i = 0;
140 	int length = 0;
141 	int retval = 0;
142 
143 	/* add the major/minor if present */
144 	if (MAJOR(dev->devt)) {
145 		add_uevent_var(envp, num_envp, &i,
146 			       buffer, buffer_size, &length,
147 			       "MAJOR=%u", MAJOR(dev->devt));
148 		add_uevent_var(envp, num_envp, &i,
149 			       buffer, buffer_size, &length,
150 			       "MINOR=%u", MINOR(dev->devt));
151 	}
152 
153 	/* add bus name of physical device */
154 	if (dev->bus)
155 		add_uevent_var(envp, num_envp, &i,
156 			       buffer, buffer_size, &length,
157 			       "PHYSDEVBUS=%s", dev->bus->name);
158 
159 	/* add driver name of physical device */
160 	if (dev->driver)
161 		add_uevent_var(envp, num_envp, &i,
162 			       buffer, buffer_size, &length,
163 			       "PHYSDEVDRIVER=%s", dev->driver->name);
164 
165 	/* terminate, set to next free slot, shrink available space */
166 	envp[i] = NULL;
167 	envp = &envp[i];
168 	num_envp -= i;
169 	buffer = &buffer[length];
170 	buffer_size -= length;
171 
172 	if (dev->bus && dev->bus->uevent) {
173 		/* have the bus specific function add its stuff */
174 		retval = dev->bus->uevent(dev, envp, num_envp, buffer, buffer_size);
175 			if (retval) {
176 			pr_debug ("%s - uevent() returned %d\n",
177 				  __FUNCTION__, retval);
178 		}
179 	}
180 
181 	return retval;
182 }
183 
184 static struct kset_uevent_ops device_uevent_ops = {
185 	.filter =	dev_uevent_filter,
186 	.name =		dev_uevent_name,
187 	.uevent =	dev_uevent,
188 };
189 
190 static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
191 			    const char *buf, size_t count)
192 {
193 	kobject_uevent(&dev->kobj, KOBJ_ADD);
194 	return count;
195 }
196 
197 static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
198 			char *buf)
199 {
200 	return print_dev_t(buf, dev->devt);
201 }
202 
203 /*
204  *	devices_subsys - structure to be registered with kobject core.
205  */
206 
207 decl_subsys(devices, &ktype_device, &device_uevent_ops);
208 
209 
210 /**
211  *	device_create_file - create sysfs attribute file for device.
212  *	@dev:	device.
213  *	@attr:	device attribute descriptor.
214  */
215 
216 int device_create_file(struct device * dev, struct device_attribute * attr)
217 {
218 	int error = 0;
219 	if (get_device(dev)) {
220 		error = sysfs_create_file(&dev->kobj, &attr->attr);
221 		put_device(dev);
222 	}
223 	return error;
224 }
225 
226 /**
227  *	device_remove_file - remove sysfs attribute file.
228  *	@dev:	device.
229  *	@attr:	device attribute descriptor.
230  */
231 
232 void device_remove_file(struct device * dev, struct device_attribute * attr)
233 {
234 	if (get_device(dev)) {
235 		sysfs_remove_file(&dev->kobj, &attr->attr);
236 		put_device(dev);
237 	}
238 }
239 
240 static void klist_children_get(struct klist_node *n)
241 {
242 	struct device *dev = container_of(n, struct device, knode_parent);
243 
244 	get_device(dev);
245 }
246 
247 static void klist_children_put(struct klist_node *n)
248 {
249 	struct device *dev = container_of(n, struct device, knode_parent);
250 
251 	put_device(dev);
252 }
253 
254 
255 /**
256  *	device_initialize - init device structure.
257  *	@dev:	device.
258  *
259  *	This prepares the device for use by other layers,
260  *	including adding it to the device hierarchy.
261  *	It is the first half of device_register(), if called by
262  *	that, though it can also be called separately, so one
263  *	may use @dev's fields (e.g. the refcount).
264  */
265 
266 void device_initialize(struct device *dev)
267 {
268 	kobj_set_kset_s(dev, devices_subsys);
269 	kobject_init(&dev->kobj);
270 	klist_init(&dev->klist_children, klist_children_get,
271 		   klist_children_put);
272 	INIT_LIST_HEAD(&dev->dma_pools);
273 	INIT_LIST_HEAD(&dev->node);
274 	init_MUTEX(&dev->sem);
275 	device_init_wakeup(dev, 0);
276 }
277 
278 /**
279  *	device_add - add device to device hierarchy.
280  *	@dev:	device.
281  *
282  *	This is part 2 of device_register(), though may be called
283  *	separately _iff_ device_initialize() has been called separately.
284  *
285  *	This adds it to the kobject hierarchy via kobject_add(), adds it
286  *	to the global and sibling lists for the device, then
287  *	adds it to the other relevant subsystems of the driver model.
288  */
289 int device_add(struct device *dev)
290 {
291 	struct device *parent = NULL;
292 	char *class_name = NULL;
293 	int error = -EINVAL;
294 
295 	dev = get_device(dev);
296 	if (!dev || !strlen(dev->bus_id))
297 		goto Error;
298 
299 	parent = get_device(dev->parent);
300 
301 	pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id);
302 
303 	/* first, register with generic layer. */
304 	kobject_set_name(&dev->kobj, "%s", dev->bus_id);
305 	if (parent)
306 		dev->kobj.parent = &parent->kobj;
307 
308 	if ((error = kobject_add(&dev->kobj)))
309 		goto Error;
310 
311 	dev->uevent_attr.attr.name = "uevent";
312 	dev->uevent_attr.attr.mode = S_IWUSR;
313 	if (dev->driver)
314 		dev->uevent_attr.attr.owner = dev->driver->owner;
315 	dev->uevent_attr.store = store_uevent;
316 	device_create_file(dev, &dev->uevent_attr);
317 
318 	if (MAJOR(dev->devt)) {
319 		struct device_attribute *attr;
320 		attr = kzalloc(sizeof(*attr), GFP_KERNEL);
321 		if (!attr) {
322 			error = -ENOMEM;
323 			goto PMError;
324 		}
325 		attr->attr.name = "dev";
326 		attr->attr.mode = S_IRUGO;
327 		if (dev->driver)
328 			attr->attr.owner = dev->driver->owner;
329 		attr->show = show_dev;
330 		error = device_create_file(dev, attr);
331 		if (error) {
332 			kfree(attr);
333 			goto attrError;
334 		}
335 
336 		dev->devt_attr = attr;
337 	}
338 
339 	if (dev->class) {
340 		sysfs_create_link(&dev->kobj, &dev->class->subsys.kset.kobj,
341 				  "subsystem");
342 		sysfs_create_link(&dev->class->subsys.kset.kobj, &dev->kobj,
343 				  dev->bus_id);
344 
345 		sysfs_create_link(&dev->kobj, &dev->parent->kobj, "device");
346 		class_name = make_class_name(dev->class->name, &dev->kobj);
347 		sysfs_create_link(&dev->parent->kobj, &dev->kobj, class_name);
348 	}
349 
350 	if ((error = device_pm_add(dev)))
351 		goto PMError;
352 	if ((error = bus_add_device(dev)))
353 		goto BusError;
354 	kobject_uevent(&dev->kobj, KOBJ_ADD);
355 	bus_attach_device(dev);
356 	if (parent)
357 		klist_add_tail(&dev->knode_parent, &parent->klist_children);
358 
359 	if (dev->class) {
360 		/* tie the class to the device */
361 		down(&dev->class->sem);
362 		list_add_tail(&dev->node, &dev->class->devices);
363 		up(&dev->class->sem);
364 	}
365 
366 	/* notify platform of device entry */
367 	if (platform_notify)
368 		platform_notify(dev);
369  Done:
370  	kfree(class_name);
371 	put_device(dev);
372 	return error;
373  BusError:
374 	device_pm_remove(dev);
375  PMError:
376 	if (dev->devt_attr) {
377 		device_remove_file(dev, dev->devt_attr);
378 		kfree(dev->devt_attr);
379 	}
380  attrError:
381 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
382 	kobject_del(&dev->kobj);
383  Error:
384 	if (parent)
385 		put_device(parent);
386 	goto Done;
387 }
388 
389 
390 /**
391  *	device_register - register a device with the system.
392  *	@dev:	pointer to the device structure
393  *
394  *	This happens in two clean steps - initialize the device
395  *	and add it to the system. The two steps can be called
396  *	separately, but this is the easiest and most common.
397  *	I.e. you should only call the two helpers separately if
398  *	have a clearly defined need to use and refcount the device
399  *	before it is added to the hierarchy.
400  */
401 
402 int device_register(struct device *dev)
403 {
404 	device_initialize(dev);
405 	return device_add(dev);
406 }
407 
408 
409 /**
410  *	get_device - increment reference count for device.
411  *	@dev:	device.
412  *
413  *	This simply forwards the call to kobject_get(), though
414  *	we do take care to provide for the case that we get a NULL
415  *	pointer passed in.
416  */
417 
418 struct device * get_device(struct device * dev)
419 {
420 	return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
421 }
422 
423 
424 /**
425  *	put_device - decrement reference count.
426  *	@dev:	device in question.
427  */
428 void put_device(struct device * dev)
429 {
430 	if (dev)
431 		kobject_put(&dev->kobj);
432 }
433 
434 
435 /**
436  *	device_del - delete device from system.
437  *	@dev:	device.
438  *
439  *	This is the first part of the device unregistration
440  *	sequence. This removes the device from the lists we control
441  *	from here, has it removed from the other driver model
442  *	subsystems it was added to in device_add(), and removes it
443  *	from the kobject hierarchy.
444  *
445  *	NOTE: this should be called manually _iff_ device_add() was
446  *	also called manually.
447  */
448 
449 void device_del(struct device * dev)
450 {
451 	struct device * parent = dev->parent;
452 	char *class_name = NULL;
453 
454 	if (parent)
455 		klist_del(&dev->knode_parent);
456 	if (dev->devt_attr)
457 		device_remove_file(dev, dev->devt_attr);
458 	if (dev->class) {
459 		sysfs_remove_link(&dev->kobj, "subsystem");
460 		sysfs_remove_link(&dev->class->subsys.kset.kobj, dev->bus_id);
461 		class_name = make_class_name(dev->class->name, &dev->kobj);
462 		sysfs_remove_link(&dev->kobj, "device");
463 		sysfs_remove_link(&dev->parent->kobj, class_name);
464 		kfree(class_name);
465 		down(&dev->class->sem);
466 		list_del_init(&dev->node);
467 		up(&dev->class->sem);
468 	}
469 	device_remove_file(dev, &dev->uevent_attr);
470 
471 	/* Notify the platform of the removal, in case they
472 	 * need to do anything...
473 	 */
474 	if (platform_notify_remove)
475 		platform_notify_remove(dev);
476 	bus_remove_device(dev);
477 	device_pm_remove(dev);
478 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
479 	kobject_del(&dev->kobj);
480 	if (parent)
481 		put_device(parent);
482 }
483 
484 /**
485  *	device_unregister - unregister device from system.
486  *	@dev:	device going away.
487  *
488  *	We do this in two parts, like we do device_register(). First,
489  *	we remove it from all the subsystems with device_del(), then
490  *	we decrement the reference count via put_device(). If that
491  *	is the final reference count, the device will be cleaned up
492  *	via device_release() above. Otherwise, the structure will
493  *	stick around until the final reference to the device is dropped.
494  */
495 void device_unregister(struct device * dev)
496 {
497 	pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
498 	device_del(dev);
499 	put_device(dev);
500 }
501 
502 
503 static struct device * next_device(struct klist_iter * i)
504 {
505 	struct klist_node * n = klist_next(i);
506 	return n ? container_of(n, struct device, knode_parent) : NULL;
507 }
508 
509 /**
510  *	device_for_each_child - device child iterator.
511  *	@parent: parent struct device.
512  *	@data:	data for the callback.
513  *	@fn:	function to be called for each device.
514  *
515  *	Iterate over @parent's child devices, and call @fn for each,
516  *	passing it @data.
517  *
518  *	We check the return of @fn each time. If it returns anything
519  *	other than 0, we break out and return that value.
520  */
521 int device_for_each_child(struct device * parent, void * data,
522 		     int (*fn)(struct device *, void *))
523 {
524 	struct klist_iter i;
525 	struct device * child;
526 	int error = 0;
527 
528 	klist_iter_init(&parent->klist_children, &i);
529 	while ((child = next_device(&i)) && !error)
530 		error = fn(child, data);
531 	klist_iter_exit(&i);
532 	return error;
533 }
534 
535 int __init devices_init(void)
536 {
537 	return subsystem_register(&devices_subsys);
538 }
539 
540 EXPORT_SYMBOL_GPL(device_for_each_child);
541 
542 EXPORT_SYMBOL_GPL(device_initialize);
543 EXPORT_SYMBOL_GPL(device_add);
544 EXPORT_SYMBOL_GPL(device_register);
545 
546 EXPORT_SYMBOL_GPL(device_del);
547 EXPORT_SYMBOL_GPL(device_unregister);
548 EXPORT_SYMBOL_GPL(get_device);
549 EXPORT_SYMBOL_GPL(put_device);
550 
551 EXPORT_SYMBOL_GPL(device_create_file);
552 EXPORT_SYMBOL_GPL(device_remove_file);
553 
554 
555 static void device_create_release(struct device *dev)
556 {
557 	pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
558 	kfree(dev);
559 }
560 
561 /**
562  * device_create - creates a device and registers it with sysfs
563  * @cs: pointer to the struct class that this device should be registered to.
564  * @parent: pointer to the parent struct device of this new device, if any.
565  * @dev: the dev_t for the char device to be added.
566  * @fmt: string for the class device's name
567  *
568  * This function can be used by char device classes.  A struct
569  * device will be created in sysfs, registered to the specified
570  * class.
571  * A "dev" file will be created, showing the dev_t for the device, if
572  * the dev_t is not 0,0.
573  * If a pointer to a parent struct device is passed in, the newly
574  * created struct device will be a child of that device in sysfs.  The
575  * pointer to the struct device will be returned from the call.  Any
576  * further sysfs files that might be required can be created using this
577  * pointer.
578  *
579  * Note: the struct class passed to this function must have previously
580  * been created with a call to class_create().
581  */
582 struct device *device_create(struct class *class, struct device *parent,
583 			     dev_t devt, char *fmt, ...)
584 {
585 	va_list args;
586 	struct device *dev = NULL;
587 	int retval = -ENODEV;
588 
589 	if (class == NULL || IS_ERR(class))
590 		goto error;
591 	if (parent == NULL) {
592 		printk(KERN_WARNING "%s does not work yet for NULL parents\n", __FUNCTION__);
593 		goto error;
594 	}
595 
596 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
597 	if (!dev) {
598 		retval = -ENOMEM;
599 		goto error;
600 	}
601 
602 	dev->devt = devt;
603 	dev->class = class;
604 	dev->parent = parent;
605 	dev->release = device_create_release;
606 
607 	va_start(args, fmt);
608 	vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
609 	va_end(args);
610 	retval = device_register(dev);
611 	if (retval)
612 		goto error;
613 
614 	return dev;
615 
616 error:
617 	kfree(dev);
618 	return ERR_PTR(retval);
619 }
620 EXPORT_SYMBOL_GPL(device_create);
621 
622 /**
623  * device_destroy - removes a device that was created with device_create()
624  * @class: the pointer to the struct class that this device was registered * with.
625  * @dev: the dev_t of the device that was previously registered.
626  *
627  * This call unregisters and cleans up a class device that was created with a
628  * call to class_device_create()
629  */
630 void device_destroy(struct class *class, dev_t devt)
631 {
632 	struct device *dev = NULL;
633 	struct device *dev_tmp;
634 
635 	down(&class->sem);
636 	list_for_each_entry(dev_tmp, &class->devices, node) {
637 		if (dev_tmp->devt == devt) {
638 			dev = dev_tmp;
639 			break;
640 		}
641 	}
642 	up(&class->sem);
643 
644 	if (dev)
645 		device_unregister(dev);
646 }
647 EXPORT_SYMBOL_GPL(device_destroy);
648