xref: /linux/drivers/base/core.c (revision f3d9478b2ce468c3115b02ecae7e975990697f15)
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 	/* notify platform of device entry */
360 	if (platform_notify)
361 		platform_notify(dev);
362  Done:
363  	kfree(class_name);
364 	put_device(dev);
365 	return error;
366  BusError:
367 	device_pm_remove(dev);
368  PMError:
369 	if (dev->devt_attr) {
370 		device_remove_file(dev, dev->devt_attr);
371 		kfree(dev->devt_attr);
372 	}
373  attrError:
374 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
375 	kobject_del(&dev->kobj);
376  Error:
377 	if (parent)
378 		put_device(parent);
379 	goto Done;
380 }
381 
382 
383 /**
384  *	device_register - register a device with the system.
385  *	@dev:	pointer to the device structure
386  *
387  *	This happens in two clean steps - initialize the device
388  *	and add it to the system. The two steps can be called
389  *	separately, but this is the easiest and most common.
390  *	I.e. you should only call the two helpers separately if
391  *	have a clearly defined need to use and refcount the device
392  *	before it is added to the hierarchy.
393  */
394 
395 int device_register(struct device *dev)
396 {
397 	device_initialize(dev);
398 	return device_add(dev);
399 }
400 
401 
402 /**
403  *	get_device - increment reference count for device.
404  *	@dev:	device.
405  *
406  *	This simply forwards the call to kobject_get(), though
407  *	we do take care to provide for the case that we get a NULL
408  *	pointer passed in.
409  */
410 
411 struct device * get_device(struct device * dev)
412 {
413 	return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
414 }
415 
416 
417 /**
418  *	put_device - decrement reference count.
419  *	@dev:	device in question.
420  */
421 void put_device(struct device * dev)
422 {
423 	if (dev)
424 		kobject_put(&dev->kobj);
425 }
426 
427 
428 /**
429  *	device_del - delete device from system.
430  *	@dev:	device.
431  *
432  *	This is the first part of the device unregistration
433  *	sequence. This removes the device from the lists we control
434  *	from here, has it removed from the other driver model
435  *	subsystems it was added to in device_add(), and removes it
436  *	from the kobject hierarchy.
437  *
438  *	NOTE: this should be called manually _iff_ device_add() was
439  *	also called manually.
440  */
441 
442 void device_del(struct device * dev)
443 {
444 	struct device * parent = dev->parent;
445 	char *class_name = NULL;
446 
447 	if (parent)
448 		klist_del(&dev->knode_parent);
449 	if (dev->devt_attr)
450 		device_remove_file(dev, dev->devt_attr);
451 	if (dev->class) {
452 		sysfs_remove_link(&dev->kobj, "subsystem");
453 		sysfs_remove_link(&dev->class->subsys.kset.kobj, dev->bus_id);
454 		class_name = make_class_name(dev->class->name, &dev->kobj);
455 		sysfs_remove_link(&dev->kobj, "device");
456 		sysfs_remove_link(&dev->parent->kobj, class_name);
457 		kfree(class_name);
458 	}
459 	device_remove_file(dev, &dev->uevent_attr);
460 
461 	/* Notify the platform of the removal, in case they
462 	 * need to do anything...
463 	 */
464 	if (platform_notify_remove)
465 		platform_notify_remove(dev);
466 	bus_remove_device(dev);
467 	device_pm_remove(dev);
468 	kobject_uevent(&dev->kobj, KOBJ_REMOVE);
469 	kobject_del(&dev->kobj);
470 	if (parent)
471 		put_device(parent);
472 }
473 
474 /**
475  *	device_unregister - unregister device from system.
476  *	@dev:	device going away.
477  *
478  *	We do this in two parts, like we do device_register(). First,
479  *	we remove it from all the subsystems with device_del(), then
480  *	we decrement the reference count via put_device(). If that
481  *	is the final reference count, the device will be cleaned up
482  *	via device_release() above. Otherwise, the structure will
483  *	stick around until the final reference to the device is dropped.
484  */
485 void device_unregister(struct device * dev)
486 {
487 	pr_debug("DEV: Unregistering device. ID = '%s'\n", dev->bus_id);
488 	device_del(dev);
489 	put_device(dev);
490 }
491 
492 
493 static struct device * next_device(struct klist_iter * i)
494 {
495 	struct klist_node * n = klist_next(i);
496 	return n ? container_of(n, struct device, knode_parent) : NULL;
497 }
498 
499 /**
500  *	device_for_each_child - device child iterator.
501  *	@parent: parent struct device.
502  *	@data:	data for the callback.
503  *	@fn:	function to be called for each device.
504  *
505  *	Iterate over @parent's child devices, and call @fn for each,
506  *	passing it @data.
507  *
508  *	We check the return of @fn each time. If it returns anything
509  *	other than 0, we break out and return that value.
510  */
511 int device_for_each_child(struct device * parent, void * data,
512 		     int (*fn)(struct device *, void *))
513 {
514 	struct klist_iter i;
515 	struct device * child;
516 	int error = 0;
517 
518 	klist_iter_init(&parent->klist_children, &i);
519 	while ((child = next_device(&i)) && !error)
520 		error = fn(child, data);
521 	klist_iter_exit(&i);
522 	return error;
523 }
524 
525 int __init devices_init(void)
526 {
527 	return subsystem_register(&devices_subsys);
528 }
529 
530 EXPORT_SYMBOL_GPL(device_for_each_child);
531 
532 EXPORT_SYMBOL_GPL(device_initialize);
533 EXPORT_SYMBOL_GPL(device_add);
534 EXPORT_SYMBOL_GPL(device_register);
535 
536 EXPORT_SYMBOL_GPL(device_del);
537 EXPORT_SYMBOL_GPL(device_unregister);
538 EXPORT_SYMBOL_GPL(get_device);
539 EXPORT_SYMBOL_GPL(put_device);
540 
541 EXPORT_SYMBOL_GPL(device_create_file);
542 EXPORT_SYMBOL_GPL(device_remove_file);
543 
544 
545 static void device_create_release(struct device *dev)
546 {
547 	pr_debug("%s called for %s\n", __FUNCTION__, dev->bus_id);
548 	kfree(dev);
549 }
550 
551 /**
552  * device_create - creates a device and registers it with sysfs
553  * @cs: pointer to the struct class that this device should be registered to.
554  * @parent: pointer to the parent struct device of this new device, if any.
555  * @dev: the dev_t for the char device to be added.
556  * @fmt: string for the class device's name
557  *
558  * This function can be used by char device classes.  A struct
559  * device will be created in sysfs, registered to the specified
560  * class.
561  * A "dev" file will be created, showing the dev_t for the device, if
562  * the dev_t is not 0,0.
563  * If a pointer to a parent struct device is passed in, the newly
564  * created struct device will be a child of that device in sysfs.  The
565  * pointer to the struct device will be returned from the call.  Any
566  * further sysfs files that might be required can be created using this
567  * pointer.
568  *
569  * Note: the struct class passed to this function must have previously
570  * been created with a call to class_create().
571  */
572 struct device *device_create(struct class *class, struct device *parent,
573 			     dev_t devt, char *fmt, ...)
574 {
575 	va_list args;
576 	struct device *dev = NULL;
577 	int retval = -ENODEV;
578 
579 	if (class == NULL || IS_ERR(class))
580 		goto error;
581 	if (parent == NULL) {
582 		printk(KERN_WARNING "%s does not work yet for NULL parents\n", __FUNCTION__);
583 		goto error;
584 	}
585 
586 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
587 	if (!dev) {
588 		retval = -ENOMEM;
589 		goto error;
590 	}
591 
592 	dev->devt = devt;
593 	dev->class = class;
594 	dev->parent = parent;
595 	dev->release = device_create_release;
596 
597 	va_start(args, fmt);
598 	vsnprintf(dev->bus_id, BUS_ID_SIZE, fmt, args);
599 	va_end(args);
600 	retval = device_register(dev);
601 	if (retval)
602 		goto error;
603 
604 	/* tie the class to the device */
605 	down(&class->sem);
606 	list_add_tail(&dev->node, &class->devices);
607 	up(&class->sem);
608 
609 	return dev;
610 
611 error:
612 	kfree(dev);
613 	return ERR_PTR(retval);
614 }
615 EXPORT_SYMBOL_GPL(device_create);
616 
617 /**
618  * device_destroy - removes a device that was created with device_create()
619  * @class: the pointer to the struct class that this device was registered * with.
620  * @dev: the dev_t of the device that was previously registered.
621  *
622  * This call unregisters and cleans up a class device that was created with a
623  * call to class_device_create()
624  */
625 void device_destroy(struct class *class, dev_t devt)
626 {
627 	struct device *dev = NULL;
628 	struct device *dev_tmp;
629 
630 	down(&class->sem);
631 	list_for_each_entry(dev_tmp, &class->devices, node) {
632 		if (dev_tmp->devt == devt) {
633 			dev = dev_tmp;
634 			break;
635 		}
636 	}
637 	up(&class->sem);
638 
639 	if (dev) {
640 		list_del_init(&dev->node);
641 		device_unregister(dev);
642 	}
643 }
644 EXPORT_SYMBOL_GPL(device_destroy);
645