core.c
来自「linux 内核源代码」· C语言 代码 · 共 1,357 行 · 第 1/3 页
C
1,357 行
/** * device_remove_bin_file - remove sysfs binary attribute file * @dev: device. * @attr: device binary attribute descriptor. */void device_remove_bin_file(struct device *dev, struct bin_attribute *attr){ if (dev) sysfs_remove_bin_file(&dev->kobj, attr);}EXPORT_SYMBOL_GPL(device_remove_bin_file);/** * device_schedule_callback_owner - helper to schedule a callback for a device * @dev: device. * @func: callback function to invoke later. * @owner: module owning the callback routine * * Attribute methods must not unregister themselves or their parent device * (which would amount to the same thing). Attempts to do so will deadlock, * since unregistration is mutually exclusive with driver callbacks. * * Instead methods can call this routine, which will attempt to allocate * and schedule a workqueue request to call back @func with @dev as its * argument in the workqueue's process context. @dev will be pinned until * @func returns. * * This routine is usually called via the inline device_schedule_callback(), * which automatically sets @owner to THIS_MODULE. * * Returns 0 if the request was submitted, -ENOMEM if storage could not * be allocated, -ENODEV if a reference to @owner isn't available. * * NOTE: This routine won't work if CONFIG_SYSFS isn't set! It uses an * underlying sysfs routine (since it is intended for use by attribute * methods), and if sysfs isn't available you'll get nothing but -ENOSYS. */int device_schedule_callback_owner(struct device *dev, void (*func)(struct device *), struct module *owner){ return sysfs_schedule_callback(&dev->kobj, (void (*)(void *)) func, dev, owner);}EXPORT_SYMBOL_GPL(device_schedule_callback_owner);static void klist_children_get(struct klist_node *n){ struct device *dev = container_of(n, struct device, knode_parent); get_device(dev);}static void klist_children_put(struct klist_node *n){ struct device *dev = container_of(n, struct device, knode_parent); put_device(dev);}/** * device_initialize - init device structure. * @dev: device. * * This prepares the device for use by other layers, * including adding it to the device hierarchy. * It is the first half of device_register(), if called by * that, though it can also be called separately, so one * may use @dev's fields (e.g. the refcount). */void device_initialize(struct device *dev){ kobj_set_kset_s(dev, devices_subsys); kobject_init(&dev->kobj); klist_init(&dev->klist_children, klist_children_get, klist_children_put); INIT_LIST_HEAD(&dev->dma_pools); INIT_LIST_HEAD(&dev->node); init_MUTEX(&dev->sem); spin_lock_init(&dev->devres_lock); INIT_LIST_HEAD(&dev->devres_head); device_init_wakeup(dev, 0); set_dev_node(dev, -1);}#ifdef CONFIG_SYSFS_DEPRECATEDstatic struct kobject * get_device_parent(struct device *dev, struct device *parent){ /* * Set the parent to the class, not the parent device * for topmost devices in class hierarchy. * This keeps sysfs from having a symlink to make old * udevs happy */ if (dev->class && (!parent || parent->class != dev->class)) return &dev->class->subsys.kobj; else if (parent) return &parent->kobj; return NULL;}#elsestatic struct kobject *virtual_device_parent(struct device *dev){ static struct kobject *virtual_dir = NULL; if (!virtual_dir) virtual_dir = kobject_add_dir(&devices_subsys.kobj, "virtual"); return virtual_dir;}static struct kobject * get_device_parent(struct device *dev, struct device *parent){ if (dev->class) { struct kobject *kobj = NULL; struct kobject *parent_kobj; struct kobject *k; /* * If we have no parent, we live in "virtual". * Class-devices with a bus-device as parent, live * in a class-directory to prevent namespace collisions. */ if (parent == NULL) parent_kobj = virtual_device_parent(dev); else if (parent->class) return &parent->kobj; else parent_kobj = &parent->kobj; /* find our class-directory at the parent and reference it */ spin_lock(&dev->class->class_dirs.list_lock); list_for_each_entry(k, &dev->class->class_dirs.list, entry) if (k->parent == parent_kobj) { kobj = kobject_get(k); break; } spin_unlock(&dev->class->class_dirs.list_lock); if (kobj) return kobj; /* or create a new class-directory at the parent device */ return kobject_kset_add_dir(&dev->class->class_dirs, parent_kobj, dev->class->name); } if (parent) return &parent->kobj; return NULL;}#endifstatic int setup_parent(struct device *dev, struct device *parent){ struct kobject *kobj; kobj = get_device_parent(dev, parent); if (IS_ERR(kobj)) return PTR_ERR(kobj); if (kobj) dev->kobj.parent = kobj; return 0;}static int device_add_class_symlinks(struct device *dev){ int error; if (!dev->class) return 0; error = sysfs_create_link(&dev->kobj, &dev->class->subsys.kobj, "subsystem"); if (error) goto out; /* * If this is not a "fake" compatible device, then create the * symlink from the class to the device. */ if (dev->kobj.parent != &dev->class->subsys.kobj) { error = sysfs_create_link(&dev->class->subsys.kobj, &dev->kobj, dev->bus_id); if (error) goto out_subsys; } if (dev->parent) {#ifdef CONFIG_SYSFS_DEPRECATED { struct device *parent = dev->parent; char *class_name; /* * In old sysfs stacked class devices had 'device' * link pointing to real device instead of parent */ while (parent->class && !parent->bus && parent->parent) parent = parent->parent; error = sysfs_create_link(&dev->kobj, &parent->kobj, "device"); if (error) goto out_busid; class_name = make_class_name(dev->class->name, &dev->kobj); if (class_name) error = sysfs_create_link(&dev->parent->kobj, &dev->kobj, class_name); kfree(class_name); if (error) goto out_device; }#else error = sysfs_create_link(&dev->kobj, &dev->parent->kobj, "device"); if (error) goto out_busid;#endif } return 0;#ifdef CONFIG_SYSFS_DEPRECATEDout_device: if (dev->parent) sysfs_remove_link(&dev->kobj, "device");#endifout_busid: if (dev->kobj.parent != &dev->class->subsys.kobj) sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id);out_subsys: sysfs_remove_link(&dev->kobj, "subsystem");out: return error;}static void device_remove_class_symlinks(struct device *dev){ if (!dev->class) return; if (dev->parent) {#ifdef CONFIG_SYSFS_DEPRECATED char *class_name; class_name = make_class_name(dev->class->name, &dev->kobj); if (class_name) { sysfs_remove_link(&dev->parent->kobj, class_name); kfree(class_name); }#endif sysfs_remove_link(&dev->kobj, "device"); } if (dev->kobj.parent != &dev->class->subsys.kobj) sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id); sysfs_remove_link(&dev->kobj, "subsystem");}/** * device_add - add device to device hierarchy. * @dev: device. * * This is part 2 of device_register(), though may be called * separately _iff_ device_initialize() has been called separately. * * This adds it to the kobject hierarchy via kobject_add(), adds it * to the global and sibling lists for the device, then * adds it to the other relevant subsystems of the driver model. */int device_add(struct device *dev){ struct device *parent = NULL; struct class_interface *class_intf; int error = -EINVAL; dev = get_device(dev); if (!dev || !strlen(dev->bus_id)) goto Error; pr_debug("DEV: registering device: ID = '%s'\n", dev->bus_id); parent = get_device(dev->parent); error = setup_parent(dev, parent); if (error) goto Error; /* first, register with generic layer. */ kobject_set_name(&dev->kobj, "%s", dev->bus_id); error = kobject_add(&dev->kobj); if (error) goto Error; /* notify platform of device entry */ if (platform_notify) platform_notify(dev); /* notify clients of device entry (new way) */ if (dev->bus) blocking_notifier_call_chain(&dev->bus->bus_notifier, BUS_NOTIFY_ADD_DEVICE, dev); error = device_create_file(dev, &uevent_attr); if (error) goto attrError; if (MAJOR(dev->devt)) { error = device_create_file(dev, &devt_attr); if (error) goto ueventattrError; } error = device_add_class_symlinks(dev); if (error) goto SymlinkError; error = device_add_attrs(dev); if (error) goto AttrsError; error = dpm_sysfs_add(dev); if (error) goto PMError; device_pm_add(dev); error = bus_add_device(dev); if (error) goto BusError; kobject_uevent(&dev->kobj, KOBJ_ADD); bus_attach_device(dev); if (parent) klist_add_tail(&dev->knode_parent, &parent->klist_children); if (dev->class) { down(&dev->class->sem); /* tie the class to the device */ list_add_tail(&dev->node, &dev->class->devices); /* notify any interfaces that the device is here */ list_for_each_entry(class_intf, &dev->class->interfaces, node) if (class_intf->add_dev) class_intf->add_dev(dev, class_intf); up(&dev->class->sem); } Done: put_device(dev); return error; BusError: device_pm_remove(dev); dpm_sysfs_remove(dev); PMError: if (dev->bus) blocking_notifier_call_chain(&dev->bus->bus_notifier, BUS_NOTIFY_DEL_DEVICE, dev); device_remove_attrs(dev); AttrsError: device_remove_class_symlinks(dev); SymlinkError: if (MAJOR(dev->devt)) device_remove_file(dev, &devt_attr); if (dev->class) { sysfs_remove_link(&dev->kobj, "subsystem"); /* If this is not a "fake" compatible device, remove the * symlink from the class to the device. */ if (dev->kobj.parent != &dev->class->subsys.kobj) sysfs_remove_link(&dev->class->subsys.kobj, dev->bus_id); if (parent) {#ifdef CONFIG_SYSFS_DEPRECATED char *class_name = make_class_name(dev->class->name, &dev->kobj); if (class_name) sysfs_remove_link(&dev->parent->kobj, class_name); kfree(class_name);#endif sysfs_remove_link(&dev->kobj, "device"); } } ueventattrError: device_remove_file(dev, &uevent_attr); attrError: kobject_uevent(&dev->kobj, KOBJ_REMOVE); kobject_del(&dev->kobj); Error: if (parent) put_device(parent); goto Done;}/** * device_register - register a device with the system. * @dev: pointer to the device structure * * This happens in two clean steps - initialize the device * and add it to the system. The two steps can be called * separately, but this is the easiest and most common. * I.e. you should only call the two helpers separately if * have a clearly defined need to use and refcount the device * before it is added to the hierarchy. */int device_register(struct device *dev){ device_initialize(dev); return device_add(dev);}/** * get_device - increment reference count for device. * @dev: device. * * This simply forwards the call to kobject_get(), though * we do take care to provide for the case that we get a NULL * pointer passed in. */struct device * get_device(struct device * dev){ return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;}/** * put_device - decrement reference count. * @dev: device in question. */void put_device(struct device * dev){ if (dev) kobject_put(&dev->kobj);}/** * device_del - delete device from system. * @dev: device. * * This is the first part of the device unregistration * sequence. This removes the device from the lists we control * from here, has it removed from the other driver model * subsystems it was added to in device_add(), and removes it * from the kobject hierarchy. * * NOTE: this should be called manually _iff_ device_add() was * also called manually. */void device_del(struct device * dev){ struct device * parent = dev->parent; struct class_interface *class_intf;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?